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