Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.71.6.1
      1 /*	$NetBSD: linux_sched.c,v 1.71.6.1 2022/08/03 11:11:32 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center; by Matthias Scheler.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Linux compatibility module. Try to deal with scheduler related syscalls.
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.71.6.1 2022/08/03 11:11:32 martin Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/mount.h>
     42 #include <sys/proc.h>
     43 #include <sys/systm.h>
     44 #include <sys/sysctl.h>
     45 #include <sys/syscallargs.h>
     46 #include <sys/wait.h>
     47 #include <sys/kauth.h>
     48 #include <sys/ptrace.h>
     49 #include <sys/atomic.h>
     50 
     51 #include <sys/cpu.h>
     52 
     53 #include <compat/linux/common/linux_types.h>
     54 #include <compat/linux/common/linux_signal.h>
     55 #include <compat/linux/common/linux_emuldata.h>
     56 #include <compat/linux/common/linux_ipc.h>
     57 #include <compat/linux/common/linux_sem.h>
     58 #include <compat/linux/common/linux_exec.h>
     59 #include <compat/linux/common/linux_machdep.h>
     60 
     61 #include <compat/linux/linux_syscallargs.h>
     62 
     63 #include <compat/linux/common/linux_sched.h>
     64 
     65 static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *,
     66     register_t *);
     67 
     68 /* Unlike Linux, dynamically calculate CPU mask size */
     69 #define	LINUX_CPU_MASK_SIZE (sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT))
     70 
     71 #if DEBUG_LINUX
     72 #define DPRINTF(x) uprintf x
     73 #else
     74 #define DPRINTF(x)
     75 #endif
     76 
     77 static void
     78 linux_child_return(void *arg)
     79 {
     80 	struct lwp *l = arg;
     81 	struct proc *p = l->l_proc;
     82 	struct linux_emuldata *led = l->l_emuldata;
     83 	void *ctp = led->led_child_tidptr;
     84 	int error;
     85 
     86 	if (ctp) {
     87 		if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0)
     88 			printf("%s: LINUX_CLONE_CHILD_SETTID "
     89 			    "failed (child_tidptr = %p, tid = %d error =%d)\n",
     90 			    __func__, ctp, p->p_pid, error);
     91 	}
     92 	child_return(arg);
     93 }
     94 
     95 int
     96 linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap,
     97     register_t *retval)
     98 {
     99 	/* {
    100 		syscallarg(int) flags;
    101 		syscallarg(void *) stack;
    102 		syscallarg(void *) parent_tidptr;
    103 		syscallarg(void *) tls;
    104 		syscallarg(void *) child_tidptr;
    105 	} */
    106 	struct linux_emuldata *led;
    107 	int flags, sig, error;
    108 
    109 	/*
    110 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
    111 	 */
    112 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
    113 		return EINVAL;
    114 
    115 	/*
    116 	 * Thread group implies shared signals. Shared signals
    117 	 * imply shared VM. This matches what Linux kernel does.
    118 	 */
    119 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
    120 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    121 		return EINVAL;
    122 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    123 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    124 		return EINVAL;
    125 
    126 	/*
    127 	 * The thread group flavor is implemented totally differently.
    128 	 */
    129 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD)
    130 		return linux_clone_nptl(l, uap, retval);
    131 
    132 	flags = 0;
    133 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    134 		flags |= FORK_SHAREVM;
    135 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    136 		flags |= FORK_SHARECWD;
    137 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    138 		flags |= FORK_SHAREFILES;
    139 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    140 		flags |= FORK_SHARESIGS;
    141 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    142 		flags |= FORK_PPWAIT;
    143 
    144 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    145 	if (sig < 0 || sig >= LINUX__NSIG)
    146 		return EINVAL;
    147 	sig = linux_to_native_signo[sig];
    148 
    149 	if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
    150 		led = l->l_emuldata;
    151 		led->led_child_tidptr = SCARG(uap, child_tidptr);
    152 	}
    153 
    154 	/*
    155 	 * Note that Linux does not provide a portable way of specifying
    156 	 * the stack area; the caller must know if the stack grows up
    157 	 * or down.  So, we pass a stack size of 0, so that the code
    158 	 * that makes this adjustment is a noop.
    159 	 */
    160 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    161 	    linux_child_return, NULL, retval)) != 0) {
    162 		DPRINTF(("%s: fork1: error %d\n", __func__, error));
    163 		return error;
    164 	}
    165 
    166 	return 0;
    167 }
    168 
    169 static int
    170 linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
    171 {
    172 	/* {
    173 		syscallarg(int) flags;
    174 		syscallarg(void *) stack;
    175 		syscallarg(void *) parent_tidptr;
    176 		syscallarg(void *) tls;
    177 		syscallarg(void *) child_tidptr;
    178 	} */
    179 	struct proc *p;
    180 	struct lwp *l2;
    181 	struct linux_emuldata *led;
    182 	void *parent_tidptr, *tls, *child_tidptr;
    183 	struct schedstate_percpu *spc;
    184 	vaddr_t uaddr;
    185 	lwpid_t lid;
    186 	int flags, tnprocs, error;
    187 
    188 	p = l->l_proc;
    189 	flags = SCARG(uap, flags);
    190 	parent_tidptr = SCARG(uap, parent_tidptr);
    191 	tls = SCARG(uap, tls);
    192 	child_tidptr = SCARG(uap, child_tidptr);
    193 
    194 	tnprocs = atomic_inc_uint_nv(&nprocs);
    195 	if (__predict_false(tnprocs >= maxproc) ||
    196 	    kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p,
    197 	    KAUTH_ARG(tnprocs), NULL, NULL) != 0) {
    198 		atomic_dec_uint(&nprocs);
    199 		return EAGAIN;
    200 	}
    201 
    202 	uaddr = uvm_uarea_alloc();
    203 	if (__predict_false(uaddr == 0)) {
    204 		atomic_dec_uint(&nprocs);
    205 		return ENOMEM;
    206 	}
    207 
    208 	error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
    209 	    SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class,
    210 	    &l->l_sigmask, &l->l_sigstk);
    211 	if (__predict_false(error)) {
    212 		DPRINTF(("%s: lwp_create error=%d\n", __func__, error));
    213 		atomic_dec_uint(&nprocs);
    214 		uvm_uarea_free(uaddr);
    215 		return error;
    216 	}
    217 	lid = l2->l_lid;
    218 
    219 	/* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
    220 	if (flags & LINUX_CLONE_CHILD_CLEARTID) {
    221 		led = l2->l_emuldata;
    222 		led->led_clear_tid = child_tidptr;
    223 	}
    224 
    225 	/* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
    226 	if (flags & LINUX_CLONE_PARENT_SETTID) {
    227 		if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
    228 			printf("%s: LINUX_CLONE_PARENT_SETTID "
    229 			    "failed (parent_tidptr = %p tid = %d error=%d)\n",
    230 			    __func__, parent_tidptr, lid, error);
    231 	}
    232 
    233 	/* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory  */
    234 	if (flags & LINUX_CLONE_CHILD_SETTID) {
    235 		if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
    236 			printf("%s: LINUX_CLONE_CHILD_SETTID "
    237 			    "failed (child_tidptr = %p, tid = %d error=%d)\n",
    238 			    __func__, child_tidptr, lid, error);
    239 	}
    240 
    241 	if (flags & LINUX_CLONE_SETTLS) {
    242 		error = LINUX_LWP_SETPRIVATE(l2, tls);
    243 		if (error) {
    244 			DPRINTF(("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
    245 			    error));
    246 			lwp_exit(l2);
    247 			return error;
    248 		}
    249 	}
    250 
    251 	/*
    252 	 * Set the new LWP running, unless the process is stopping,
    253 	 * then the LWP is created stopped.
    254 	 */
    255 	mutex_enter(p->p_lock);
    256 	lwp_lock(l2);
    257 	spc = &l2->l_cpu->ci_schedstate;
    258 	if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    259 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
    260 			KASSERT(l2->l_wchan == NULL);
    261 	    		l2->l_stat = LSSTOP;
    262 			p->p_nrlwps--;
    263 			lwp_unlock_to(l2, spc->spc_lwplock);
    264 		} else {
    265 			KASSERT(lwp_locked(l2, spc->spc_mutex));
    266 			l2->l_stat = LSRUN;
    267 			sched_enqueue(l2, false);
    268 			lwp_unlock(l2);
    269 		}
    270 	} else {
    271 		l2->l_stat = LSSUSPENDED;
    272 		p->p_nrlwps--;
    273 		lwp_unlock_to(l2, spc->spc_lwplock);
    274 	}
    275 	mutex_exit(p->p_lock);
    276 
    277 	retval[0] = lid;
    278 	retval[1] = 0;
    279 	return 0;
    280 }
    281 
    282 /*
    283  * linux realtime priority
    284  *
    285  * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
    286  *
    287  * - SCHED_OTHER tasks don't have realtime priorities.
    288  *   in particular, sched_param::sched_priority is always 0.
    289  */
    290 
    291 #define	LINUX_SCHED_RTPRIO_MIN	1
    292 #define	LINUX_SCHED_RTPRIO_MAX	99
    293 
    294 static int
    295 sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
    296     int *native_policy, struct sched_param *native_params)
    297 {
    298 
    299 	switch (linux_policy) {
    300 	case LINUX_SCHED_OTHER:
    301 		if (native_policy != NULL) {
    302 			*native_policy = SCHED_OTHER;
    303 		}
    304 		break;
    305 
    306 	case LINUX_SCHED_FIFO:
    307 		if (native_policy != NULL) {
    308 			*native_policy = SCHED_FIFO;
    309 		}
    310 		break;
    311 
    312 	case LINUX_SCHED_RR:
    313 		if (native_policy != NULL) {
    314 			*native_policy = SCHED_RR;
    315 		}
    316 		break;
    317 
    318 	default:
    319 		return EINVAL;
    320 	}
    321 
    322 	if (linux_params != NULL) {
    323 		int prio = linux_params->sched_priority;
    324 
    325 		KASSERT(native_params != NULL);
    326 
    327 		if (linux_policy == LINUX_SCHED_OTHER) {
    328 			if (prio != 0) {
    329 				return EINVAL;
    330 			}
    331 			native_params->sched_priority = PRI_NONE; /* XXX */
    332 		} else {
    333 			if (prio < LINUX_SCHED_RTPRIO_MIN ||
    334 			    prio > LINUX_SCHED_RTPRIO_MAX) {
    335 				return EINVAL;
    336 			}
    337 			native_params->sched_priority =
    338 			    (prio - LINUX_SCHED_RTPRIO_MIN)
    339 			    * (SCHED_PRI_MAX - SCHED_PRI_MIN)
    340 			    / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    341 			    + SCHED_PRI_MIN;
    342 		}
    343 	}
    344 
    345 	return 0;
    346 }
    347 
    348 static int
    349 sched_native2linux(int native_policy, struct sched_param *native_params,
    350     int *linux_policy, struct linux_sched_param *linux_params)
    351 {
    352 
    353 	switch (native_policy) {
    354 	case SCHED_OTHER:
    355 		if (linux_policy != NULL) {
    356 			*linux_policy = LINUX_SCHED_OTHER;
    357 		}
    358 		break;
    359 
    360 	case SCHED_FIFO:
    361 		if (linux_policy != NULL) {
    362 			*linux_policy = LINUX_SCHED_FIFO;
    363 		}
    364 		break;
    365 
    366 	case SCHED_RR:
    367 		if (linux_policy != NULL) {
    368 			*linux_policy = LINUX_SCHED_RR;
    369 		}
    370 		break;
    371 
    372 	default:
    373 		panic("%s: unknown policy %d\n", __func__, native_policy);
    374 	}
    375 
    376 	if (native_params != NULL) {
    377 		int prio = native_params->sched_priority;
    378 
    379 		KASSERT(prio >= SCHED_PRI_MIN);
    380 		KASSERT(prio <= SCHED_PRI_MAX);
    381 		KASSERT(linux_params != NULL);
    382 
    383 		memset(linux_params, 0, sizeof(*linux_params));
    384 
    385 		DPRINTF(("%s: native: policy %d, priority %d\n",
    386 		    __func__, native_policy, prio));
    387 
    388 		if (native_policy == SCHED_OTHER) {
    389 			linux_params->sched_priority = 0;
    390 		} else {
    391 			linux_params->sched_priority =
    392 			    (prio - SCHED_PRI_MIN)
    393 			    * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    394 			    / (SCHED_PRI_MAX - SCHED_PRI_MIN)
    395 			    + LINUX_SCHED_RTPRIO_MIN;
    396 		}
    397 		DPRINTF(("%s: linux: policy %d, priority %d\n",
    398 		    __func__, -1, linux_params->sched_priority));
    399 	}
    400 
    401 	return 0;
    402 }
    403 
    404 int
    405 linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
    406 {
    407 	/* {
    408 		syscallarg(linux_pid_t) pid;
    409 		syscallarg(const struct linux_sched_param *) sp;
    410 	} */
    411 	int error, policy;
    412 	struct linux_sched_param lp;
    413 	struct sched_param sp;
    414 
    415 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    416 		error = EINVAL;
    417 		goto out;
    418 	}
    419 
    420 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    421 	if (error)
    422 		goto out;
    423 
    424 	/* We need the current policy in Linux terms. */
    425 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    426 	if (error)
    427 		goto out;
    428 	error = sched_native2linux(policy, NULL, &policy, NULL);
    429 	if (error)
    430 		goto out;
    431 
    432 	error = sched_linux2native(policy, &lp, &policy, &sp);
    433 	if (error)
    434 		goto out;
    435 
    436 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    437 	if (error)
    438 		goto out;
    439 
    440  out:
    441 	return error;
    442 }
    443 
    444 int
    445 linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
    446 {
    447 	/* {
    448 		syscallarg(linux_pid_t) pid;
    449 		syscallarg(struct linux_sched_param *) sp;
    450 	} */
    451 	struct linux_sched_param lp;
    452 	struct sched_param sp;
    453 	int error, policy;
    454 
    455 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    456 		error = EINVAL;
    457 		goto out;
    458 	}
    459 
    460 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
    461 	if (error)
    462 		goto out;
    463 	DPRINTF(("%s: native: policy %d, priority %d\n",
    464 	    __func__, policy, sp.sched_priority));
    465 
    466 	error = sched_native2linux(policy, &sp, NULL, &lp);
    467 	if (error)
    468 		goto out;
    469 	DPRINTF(("%s: linux: policy %d, priority %d\n",
    470 	    __func__, policy, lp.sched_priority));
    471 
    472 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    473 	if (error)
    474 		goto out;
    475 
    476  out:
    477 	return error;
    478 }
    479 
    480 int
    481 linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
    482 {
    483 	/* {
    484 		syscallarg(linux_pid_t) pid;
    485 		syscallarg(int) policy;
    486 		syscallarg(cont struct linux_sched_param *) sp;
    487 	} */
    488 	int error, policy;
    489 	struct linux_sched_param lp;
    490 	struct sched_param sp;
    491 
    492 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    493 		error = EINVAL;
    494 		goto out;
    495 	}
    496 
    497 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    498 	if (error)
    499 		goto out;
    500 	DPRINTF(("%s: linux: policy %d, priority %d\n",
    501 	    __func__, SCARG(uap, policy), lp.sched_priority));
    502 
    503 	error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
    504 	if (error)
    505 		goto out;
    506 	DPRINTF(("%s: native: policy %d, priority %d\n",
    507 	    __func__, policy, sp.sched_priority));
    508 
    509 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    510 	if (error)
    511 		goto out;
    512 
    513  out:
    514 	return error;
    515 }
    516 
    517 int
    518 linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
    519 {
    520 	/* {
    521 		syscallarg(linux_pid_t) pid;
    522 	} */
    523 	int error, policy;
    524 
    525 	*retval = -1;
    526 
    527 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    528 	if (error)
    529 		goto out;
    530 
    531 	error = sched_native2linux(policy, NULL, &policy, NULL);
    532 	if (error)
    533 		goto out;
    534 
    535 	*retval = policy;
    536 
    537  out:
    538 	return error;
    539 }
    540 
    541 int
    542 linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    543 {
    544 
    545 	yield();
    546 	return 0;
    547 }
    548 
    549 int
    550 linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval)
    551 {
    552 	/* {
    553 		syscallarg(int) policy;
    554 	} */
    555 
    556 	switch (SCARG(uap, policy)) {
    557 	case LINUX_SCHED_OTHER:
    558 		*retval = 0;
    559 		break;
    560 	case LINUX_SCHED_FIFO:
    561 	case LINUX_SCHED_RR:
    562 		*retval = LINUX_SCHED_RTPRIO_MAX;
    563 		break;
    564 	default:
    565 		return EINVAL;
    566 	}
    567 
    568 	return 0;
    569 }
    570 
    571 int
    572 linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval)
    573 {
    574 	/* {
    575 		syscallarg(int) policy;
    576 	} */
    577 
    578 	switch (SCARG(uap, policy)) {
    579 	case LINUX_SCHED_OTHER:
    580 		*retval = 0;
    581 		break;
    582 	case LINUX_SCHED_FIFO:
    583 	case LINUX_SCHED_RR:
    584 		*retval = LINUX_SCHED_RTPRIO_MIN;
    585 		break;
    586 	default:
    587 		return EINVAL;
    588 	}
    589 
    590 	return 0;
    591 }
    592 
    593 int
    594 linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
    595 {
    596 
    597 	lwp_exit(l);
    598 	return 0;
    599 }
    600 
    601 #ifndef __m68k__
    602 /* Present on everything but m68k */
    603 int
    604 linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
    605 {
    606 
    607 	return sys_exit(l, (const void *)uap, retval);
    608 }
    609 #endif /* !__m68k__ */
    610 
    611 int
    612 linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
    613 {
    614 	/* {
    615 		syscallarg(int *) tidptr;
    616 	} */
    617 	struct linux_emuldata *led;
    618 
    619 	led = (struct linux_emuldata *)l->l_emuldata;
    620 	led->led_clear_tid = SCARG(uap, tid);
    621 	*retval = l->l_lid;
    622 
    623 	return 0;
    624 }
    625 
    626 /* ARGUSED1 */
    627 int
    628 linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
    629 {
    630 
    631 	*retval = l->l_lid;
    632 	return 0;
    633 }
    634 
    635 /*
    636  * The affinity syscalls assume that the layout of our cpu kcpuset is
    637  * the same as linux's: a linear bitmask.
    638  */
    639 int
    640 linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
    641 {
    642 	/* {
    643 		syscallarg(linux_pid_t) pid;
    644 		syscallarg(unsigned int) len;
    645 		syscallarg(unsigned long *) mask;
    646 	} */
    647 	struct lwp *t;
    648 	kcpuset_t *kcset;
    649 	size_t size;
    650 	cpuid_t i;
    651 	int error;
    652 
    653 	size = LINUX_CPU_MASK_SIZE;
    654 	if (SCARG(uap, len) < size)
    655 		return EINVAL;
    656 
    657 	/* Lock the LWP */
    658 	t = lwp_find2(SCARG(uap, pid), l->l_lid);
    659 	if (t == NULL)
    660 		return ESRCH;
    661 
    662 	/* Check the permission */
    663 	if (kauth_authorize_process(l->l_cred,
    664 	    KAUTH_PROCESS_SCHEDULER_GETAFFINITY, t->l_proc, NULL, NULL, NULL)) {
    665 		mutex_exit(t->l_proc->p_lock);
    666 		return EPERM;
    667 	}
    668 
    669 	kcpuset_create(&kcset, true);
    670 	lwp_lock(t);
    671 	if (t->l_affinity != NULL)
    672 		kcpuset_copy(kcset, t->l_affinity);
    673 	else {
    674 		/*
    675 		 * All available CPUs should be masked when affinity has not
    676 		 * been set.
    677 		 */
    678 		kcpuset_zero(kcset);
    679 		for (i = 0; i < ncpu; i++)
    680 			kcpuset_set(kcset, i);
    681 	}
    682 	lwp_unlock(t);
    683 	mutex_exit(t->l_proc->p_lock);
    684 	error = kcpuset_copyout(kcset, (cpuset_t *)SCARG(uap, mask), size);
    685 	kcpuset_unuse(kcset, NULL);
    686 	*retval = size;
    687 	return error;
    688 }
    689 
    690 int
    691 linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
    692 {
    693 	/* {
    694 		syscallarg(linux_pid_t) pid;
    695 		syscallarg(unsigned int) len;
    696 		syscallarg(unsigned long *) mask;
    697 	} */
    698 	struct sys__sched_setaffinity_args ssa;
    699 	size_t size;
    700 
    701 	size = LINUX_CPU_MASK_SIZE;
    702 	if (SCARG(uap, len) < size)
    703 		return EINVAL;
    704 
    705 	SCARG(&ssa, pid) = SCARG(uap, pid);
    706 	SCARG(&ssa, lid) = l->l_lid;
    707 	SCARG(&ssa, size) = size;
    708 	SCARG(&ssa, cpuset) = (cpuset_t *)SCARG(uap, mask);
    709 
    710 	return sys__sched_setaffinity(l, &ssa, retval);
    711 }
    712