Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.87
      1  1.87  christos /*	$NetBSD: linux_sched.c,v 1.87 2025/11/10 15:41:38 christos 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.87  christos __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.87 2025/11/10 15:41:38 christos 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.86  christos #include <compat/linux/common/linux_prctl.h>
     66  1.86  christos 
     67  1.65  christos static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *,
     68  1.65  christos     register_t *);
     69  1.65  christos 
     70  1.68  christos /* Unlike Linux, dynamically calculate CPU mask size */
     71  1.68  christos #define	LINUX_CPU_MASK_SIZE (sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT))
     72  1.68  christos 
     73  1.65  christos #if DEBUG_LINUX
     74  1.80  christos #define DPRINTF(x, ...) uprintf(x, __VA_ARGS__)
     75  1.65  christos #else
     76  1.80  christos #define DPRINTF(x, ...)
     77  1.65  christos #endif
     78  1.63       chs 
     79  1.63       chs static void
     80  1.63       chs linux_child_return(void *arg)
     81  1.63       chs {
     82  1.63       chs 	struct lwp *l = arg;
     83  1.63       chs 	struct proc *p = l->l_proc;
     84  1.63       chs 	struct linux_emuldata *led = l->l_emuldata;
     85  1.63       chs 	void *ctp = led->led_child_tidptr;
     86  1.65  christos 	int error;
     87  1.63       chs 
     88  1.63       chs 	if (ctp) {
     89  1.65  christos 		if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0)
     90  1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
     91  1.65  christos 			    "failed (child_tidptr = %p, tid = %d error =%d)\n",
     92  1.65  christos 			    __func__, ctp, p->p_pid, error);
     93  1.63       chs 	}
     94  1.63       chs 	child_return(arg);
     95  1.63       chs }
     96  1.63       chs 
     97   1.1      tron int
     98  1.65  christos linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap,
     99  1.65  christos     register_t *retval)
    100   1.1      tron {
    101  1.46       dsl 	/* {
    102   1.1      tron 		syscallarg(int) flags;
    103   1.1      tron 		syscallarg(void *) stack;
    104  1.19      manu 		syscallarg(void *) parent_tidptr;
    105  1.63       chs 		syscallarg(void *) tls;
    106  1.19      manu 		syscallarg(void *) child_tidptr;
    107  1.46       dsl 	} */
    108  1.19      manu 	struct linux_emuldata *led;
    109  1.63       chs 	int flags, sig, error;
    110   1.1      tron 
    111   1.1      tron 	/*
    112   1.1      tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
    113   1.1      tron 	 */
    114   1.1      tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
    115  1.65  christos 		return EINVAL;
    116   1.1      tron 
    117  1.13  jdolecek 	/*
    118  1.13  jdolecek 	 * Thread group implies shared signals. Shared signals
    119  1.13  jdolecek 	 * imply shared VM. This matches what Linux kernel does.
    120  1.13  jdolecek 	 */
    121  1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
    122  1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    123  1.65  christos 		return EINVAL;
    124  1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    125  1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    126  1.65  christos 		return EINVAL;
    127  1.13  jdolecek 
    128  1.63       chs 	/*
    129  1.63       chs 	 * The thread group flavor is implemented totally differently.
    130  1.63       chs 	 */
    131  1.65  christos 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD)
    132  1.63       chs 		return linux_clone_nptl(l, uap, retval);
    133  1.63       chs 
    134   1.1      tron 	flags = 0;
    135   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    136   1.1      tron 		flags |= FORK_SHAREVM;
    137   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    138   1.1      tron 		flags |= FORK_SHARECWD;
    139   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    140   1.1      tron 		flags |= FORK_SHAREFILES;
    141   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    142   1.1      tron 		flags |= FORK_SHARESIGS;
    143   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    144   1.1      tron 		flags |= FORK_PPWAIT;
    145   1.1      tron 
    146  1.34      manu 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    147  1.34      manu 	if (sig < 0 || sig >= LINUX__NSIG)
    148  1.65  christos 		return EINVAL;
    149  1.34      manu 	sig = linux_to_native_signo[sig];
    150   1.1      tron 
    151  1.63       chs 	if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
    152  1.63       chs 		led = l->l_emuldata;
    153  1.63       chs 		led->led_child_tidptr = SCARG(uap, child_tidptr);
    154  1.63       chs 	}
    155  1.19      manu 
    156   1.1      tron 	/*
    157   1.1      tron 	 * Note that Linux does not provide a portable way of specifying
    158   1.1      tron 	 * the stack area; the caller must know if the stack grows up
    159   1.1      tron 	 * or down.  So, we pass a stack size of 0, so that the code
    160   1.1      tron 	 * that makes this adjustment is a noop.
    161   1.1      tron 	 */
    162  1.19      manu 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    163  1.71     kamil 	    linux_child_return, NULL, retval)) != 0) {
    164  1.80  christos 		DPRINTF("%s: fork1: error %d\n", __func__, error);
    165  1.63       chs 		return error;
    166  1.65  christos 	}
    167  1.63       chs 
    168  1.63       chs 	return 0;
    169  1.63       chs }
    170  1.63       chs 
    171  1.80  christos 
    172  1.80  christos int
    173  1.80  christos linux_sys_clone3(struct lwp *l, const struct linux_sys_clone3_args *uap, register_t *retval)
    174  1.80  christos {
    175  1.80  christos 	struct linux_user_clone3_args cl_args;
    176  1.80  christos 	struct linux_sys_clone_args clone_args;
    177  1.80  christos 	int error;
    178  1.80  christos 
    179  1.80  christos 	if (SCARG(uap, size) != sizeof(cl_args)) {
    180  1.80  christos 	    DPRINTF("%s: Invalid size less or more\n", __func__);
    181  1.80  christos 	    return EINVAL;
    182  1.80  christos 	}
    183  1.80  christos 
    184  1.80  christos 	error = copyin(SCARG(uap, cl_args), &cl_args, SCARG(uap, size));
    185  1.80  christos 	if (error) {
    186  1.80  christos 		DPRINTF("%s: Copyin failed: %d\n", __func__, error);
    187  1.80  christos 		return error;
    188  1.80  christos 	}
    189  1.80  christos 
    190  1.80  christos 	DPRINTF("%s: Flags: %#jx\n", __func__, (intmax_t)cl_args.flags);
    191  1.80  christos 
    192  1.80  christos 	/* Define allowed flags */
    193  1.80  christos 	if (cl_args.flags & LINUX_CLONE_UNIMPLEMENTED_FLAGS) {
    194  1.83   hannken 		DPRINTF("%s: Unsupported flags for clone3: %#" PRIx64 "\n",
    195  1.83   hannken 		    __func__, cl_args.flags & LINUX_CLONE_UNIMPLEMENTED_FLAGS);
    196  1.80  christos 		return EOPNOTSUPP;
    197  1.80  christos 	}
    198  1.80  christos 	if (cl_args.flags & ~LINUX_CLONE_ALLOWED_FLAGS) {
    199  1.83   hannken 		DPRINTF("%s: Disallowed flags for clone3: %#" PRIx64 "\n",
    200  1.83   hannken 		    __func__, cl_args.flags & ~LINUX_CLONE_ALLOWED_FLAGS);
    201  1.80  christos 		return EINVAL;
    202  1.80  christos 	}
    203  1.80  christos 
    204  1.80  christos #if 0
    205  1.80  christos 	// XXX: this is wrong, exit_signal is the signal to deliver to the
    206  1.80  christos 	// process upon exit.
    207  1.80  christos 	if ((cl_args.exit_signal & ~(uint64_t)LINUX_CLONE_CSIGNAL) != 0){
    208  1.80  christos 		DPRINTF("%s: Disallowed flags for clone3: %#x\n", __func__,
    209  1.80  christos 		    cl_args.exit_signal & ~(uint64_t)LINUX_CLONE_CSIGNAL);
    210  1.80  christos 		return EINVAL;
    211  1.80  christos 	}
    212  1.80  christos #endif
    213  1.82  riastrad 
    214  1.80  christos 	if (cl_args.stack == 0 && cl_args.stack_size != 0) {
    215  1.80  christos 		DPRINTF("%s: Stack is NULL but stack size is not 0\n",
    216  1.80  christos 		    __func__);
    217  1.80  christos 		return EINVAL;
    218  1.80  christos 	}
    219  1.80  christos 	if (cl_args.stack != 0 && cl_args.stack_size == 0) {
    220  1.80  christos 		DPRINTF("%s: Stack is not NULL but stack size is 0\n",
    221  1.80  christos 		    __func__);
    222  1.80  christos 		return EINVAL;
    223  1.80  christos 	}
    224  1.80  christos 
    225  1.80  christos 	int flags = cl_args.flags & LINUX_CLONE_ALLOWED_FLAGS;
    226  1.80  christos #if 0
    227  1.80  christos 	int sig = cl_args.exit_signal & LINUX_CLONE_CSIGNAL;
    228  1.80  christos #endif
    229  1.82  riastrad 	// XXX: Pidfd member handling
    230  1.80  christos 	// XXX: we don't have cgroups
    231  1.80  christos 	// XXX: what to do with tid_set and tid_set_size
    232  1.80  christos 	// XXX: clone3 has stacksize, instead implement clone as a clone3
    233  1.80  christos 	// wrapper.
    234  1.80  christos 	SCARG(&clone_args, flags) = flags;
    235  1.84  christos #ifdef __MACHINE_STACK_GROWS_UP
    236  1.81       kre 	SCARG(&clone_args, stack) = (void *)(uintptr_t)cl_args.stack;
    237  1.84  christos #else
    238  1.84  christos 	SCARG(&clone_args, stack) =
    239  1.85       kre 	    (void *)(uintptr_t)((uintptr_t)cl_args.stack + cl_args.stack_size);
    240  1.84  christos #endif
    241  1.80  christos 	SCARG(&clone_args, parent_tidptr) =
    242  1.80  christos 	    (void *)(intptr_t)cl_args.parent_tid;
    243  1.80  christos 	SCARG(&clone_args, tls) =
    244  1.80  christos 	    (void *)(intptr_t)cl_args.tls;
    245  1.80  christos 	SCARG(&clone_args, child_tidptr) =
    246  1.80  christos 	    (void *)(intptr_t)cl_args.child_tid;
    247  1.80  christos 
    248  1.80  christos 	return linux_sys_clone(l, &clone_args, retval);
    249  1.80  christos }
    250  1.80  christos 
    251  1.63       chs static int
    252  1.63       chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
    253  1.63       chs {
    254  1.63       chs 	/* {
    255  1.63       chs 		syscallarg(int) flags;
    256  1.63       chs 		syscallarg(void *) stack;
    257  1.63       chs 		syscallarg(void *) parent_tidptr;
    258  1.63       chs 		syscallarg(void *) tls;
    259  1.63       chs 		syscallarg(void *) child_tidptr;
    260  1.63       chs 	} */
    261  1.63       chs 	struct proc *p;
    262  1.63       chs 	struct lwp *l2;
    263  1.63       chs 	struct linux_emuldata *led;
    264  1.63       chs 	void *parent_tidptr, *tls, *child_tidptr;
    265  1.63       chs 	vaddr_t uaddr;
    266  1.63       chs 	lwpid_t lid;
    267  1.74   thorpej 	int flags, error;
    268  1.63       chs 
    269  1.63       chs 	p = l->l_proc;
    270  1.63       chs 	flags = SCARG(uap, flags);
    271  1.63       chs 	parent_tidptr = SCARG(uap, parent_tidptr);
    272  1.63       chs 	tls = SCARG(uap, tls);
    273  1.63       chs 	child_tidptr = SCARG(uap, child_tidptr);
    274  1.63       chs 
    275  1.63       chs 	uaddr = uvm_uarea_alloc();
    276  1.63       chs 	if (__predict_false(uaddr == 0)) {
    277  1.63       chs 		return ENOMEM;
    278  1.63       chs 	}
    279  1.63       chs 
    280  1.75   thorpej 	error = lwp_create(l, p, uaddr, LWP_DETACHED,
    281  1.69  christos 	    SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class,
    282  1.69  christos 	    &l->l_sigmask, &l->l_sigstk);
    283  1.63       chs 	if (__predict_false(error)) {
    284  1.83   hannken 		DPRINTF("%s: lwp_create error=%d\n", __func__, error);
    285  1.63       chs 		uvm_uarea_free(uaddr);
    286  1.19      manu 		return error;
    287  1.63       chs 	}
    288  1.63       chs 	lid = l2->l_lid;
    289  1.19      manu 
    290  1.63       chs 	/* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
    291  1.63       chs 	if (flags & LINUX_CLONE_CHILD_CLEARTID) {
    292  1.63       chs 		led = l2->l_emuldata;
    293  1.63       chs 		led->led_clear_tid = child_tidptr;
    294  1.63       chs 	}
    295  1.63       chs 
    296  1.63       chs 	/* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
    297  1.63       chs 	if (flags & LINUX_CLONE_PARENT_SETTID) {
    298  1.65  christos 		if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
    299  1.63       chs 			printf("%s: LINUX_CLONE_PARENT_SETTID "
    300  1.65  christos 			    "failed (parent_tidptr = %p tid = %d error=%d)\n",
    301  1.65  christos 			    __func__, parent_tidptr, lid, error);
    302  1.63       chs 	}
    303  1.63       chs 
    304  1.63       chs 	/* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory  */
    305  1.63       chs 	if (flags & LINUX_CLONE_CHILD_SETTID) {
    306  1.65  christos 		if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
    307  1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
    308  1.65  christos 			    "failed (child_tidptr = %p, tid = %d error=%d)\n",
    309  1.65  christos 			    __func__, child_tidptr, lid, error);
    310  1.63       chs 	}
    311  1.63       chs 
    312  1.63       chs 	if (flags & LINUX_CLONE_SETTLS) {
    313  1.63       chs 		error = LINUX_LWP_SETPRIVATE(l2, tls);
    314  1.63       chs 		if (error) {
    315  1.83   hannken 			DPRINTF("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
    316  1.83   hannken 			    error);
    317  1.63       chs 			lwp_exit(l2);
    318  1.63       chs 			return error;
    319  1.63       chs 		}
    320  1.63       chs 	}
    321  1.63       chs 
    322  1.73        ad 	/* Set the new LWP running. */
    323  1.73        ad 	lwp_start(l2, 0);
    324  1.58  christos 
    325  1.63       chs 	retval[0] = lid;
    326  1.63       chs 	retval[1] = 0;
    327  1.19      manu 	return 0;
    328   1.1      tron }
    329   1.1      tron 
    330  1.49      elad /*
    331  1.49      elad  * linux realtime priority
    332  1.49      elad  *
    333  1.49      elad  * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
    334  1.49      elad  *
    335  1.49      elad  * - SCHED_OTHER tasks don't have realtime priorities.
    336  1.49      elad  *   in particular, sched_param::sched_priority is always 0.
    337  1.49      elad  */
    338  1.49      elad 
    339  1.49      elad #define	LINUX_SCHED_RTPRIO_MIN	1
    340  1.49      elad #define	LINUX_SCHED_RTPRIO_MAX	99
    341  1.49      elad 
    342  1.49      elad static int
    343  1.49      elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
    344  1.49      elad     int *native_policy, struct sched_param *native_params)
    345  1.49      elad {
    346  1.49      elad 
    347  1.49      elad 	switch (linux_policy) {
    348  1.49      elad 	case LINUX_SCHED_OTHER:
    349  1.49      elad 		if (native_policy != NULL) {
    350  1.49      elad 			*native_policy = SCHED_OTHER;
    351  1.49      elad 		}
    352  1.49      elad 		break;
    353  1.49      elad 
    354  1.49      elad 	case LINUX_SCHED_FIFO:
    355  1.49      elad 		if (native_policy != NULL) {
    356  1.49      elad 			*native_policy = SCHED_FIFO;
    357  1.49      elad 		}
    358  1.49      elad 		break;
    359  1.49      elad 
    360  1.49      elad 	case LINUX_SCHED_RR:
    361  1.49      elad 		if (native_policy != NULL) {
    362  1.49      elad 			*native_policy = SCHED_RR;
    363  1.49      elad 		}
    364  1.49      elad 		break;
    365  1.49      elad 
    366  1.49      elad 	default:
    367  1.49      elad 		return EINVAL;
    368  1.49      elad 	}
    369  1.49      elad 
    370  1.49      elad 	if (linux_params != NULL) {
    371  1.49      elad 		int prio = linux_params->sched_priority;
    372  1.82  riastrad 
    373  1.49      elad 		KASSERT(native_params != NULL);
    374  1.49      elad 
    375  1.49      elad 		if (linux_policy == LINUX_SCHED_OTHER) {
    376  1.49      elad 			if (prio != 0) {
    377  1.49      elad 				return EINVAL;
    378  1.49      elad 			}
    379  1.49      elad 			native_params->sched_priority = PRI_NONE; /* XXX */
    380  1.49      elad 		} else {
    381  1.49      elad 			if (prio < LINUX_SCHED_RTPRIO_MIN ||
    382  1.49      elad 			    prio > LINUX_SCHED_RTPRIO_MAX) {
    383  1.49      elad 				return EINVAL;
    384  1.49      elad 			}
    385  1.49      elad 			native_params->sched_priority =
    386  1.49      elad 			    (prio - LINUX_SCHED_RTPRIO_MIN)
    387  1.49      elad 			    * (SCHED_PRI_MAX - SCHED_PRI_MIN)
    388  1.49      elad 			    / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    389  1.49      elad 			    + SCHED_PRI_MIN;
    390  1.49      elad 		}
    391  1.49      elad 	}
    392  1.49      elad 
    393  1.49      elad 	return 0;
    394  1.49      elad }
    395  1.49      elad 
    396  1.49      elad static int
    397  1.49      elad sched_native2linux(int native_policy, struct sched_param *native_params,
    398  1.49      elad     int *linux_policy, struct linux_sched_param *linux_params)
    399  1.49      elad {
    400  1.49      elad 
    401  1.49      elad 	switch (native_policy) {
    402  1.49      elad 	case SCHED_OTHER:
    403  1.49      elad 		if (linux_policy != NULL) {
    404  1.49      elad 			*linux_policy = LINUX_SCHED_OTHER;
    405  1.49      elad 		}
    406  1.49      elad 		break;
    407  1.49      elad 
    408  1.49      elad 	case SCHED_FIFO:
    409  1.49      elad 		if (linux_policy != NULL) {
    410  1.49      elad 			*linux_policy = LINUX_SCHED_FIFO;
    411  1.49      elad 		}
    412  1.49      elad 		break;
    413  1.49      elad 
    414  1.49      elad 	case SCHED_RR:
    415  1.49      elad 		if (linux_policy != NULL) {
    416  1.49      elad 			*linux_policy = LINUX_SCHED_RR;
    417  1.49      elad 		}
    418  1.49      elad 		break;
    419  1.49      elad 
    420  1.49      elad 	default:
    421  1.49      elad 		panic("%s: unknown policy %d\n", __func__, native_policy);
    422  1.49      elad 	}
    423  1.49      elad 
    424  1.49      elad 	if (native_params != NULL) {
    425  1.49      elad 		int prio = native_params->sched_priority;
    426  1.49      elad 
    427  1.49      elad 		KASSERT(prio >= SCHED_PRI_MIN);
    428  1.49      elad 		KASSERT(prio <= SCHED_PRI_MAX);
    429  1.49      elad 		KASSERT(linux_params != NULL);
    430  1.56  jmcneill 
    431  1.79  riastrad 		memset(linux_params, 0, sizeof(*linux_params));
    432  1.79  riastrad 
    433  1.80  christos 		DPRINTF("%s: native: policy %d, priority %d\n",
    434  1.80  christos 		    __func__, native_policy, prio);
    435  1.49      elad 
    436  1.49      elad 		if (native_policy == SCHED_OTHER) {
    437  1.49      elad 			linux_params->sched_priority = 0;
    438  1.49      elad 		} else {
    439  1.49      elad 			linux_params->sched_priority =
    440  1.49      elad 			    (prio - SCHED_PRI_MIN)
    441  1.49      elad 			    * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    442  1.49      elad 			    / (SCHED_PRI_MAX - SCHED_PRI_MIN)
    443  1.49      elad 			    + LINUX_SCHED_RTPRIO_MIN;
    444  1.49      elad 		}
    445  1.80  christos 		DPRINTF("%s: linux: policy %d, priority %d\n",
    446  1.80  christos 		    __func__, -1, linux_params->sched_priority);
    447  1.49      elad 	}
    448  1.49      elad 
    449  1.49      elad 	return 0;
    450  1.49      elad }
    451  1.49      elad 
    452   1.1      tron int
    453  1.46       dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
    454   1.1      tron {
    455  1.46       dsl 	/* {
    456   1.1      tron 		syscallarg(linux_pid_t) pid;
    457   1.1      tron 		syscallarg(const struct linux_sched_param *) sp;
    458  1.46       dsl 	} */
    459  1.49      elad 	int error, policy;
    460   1.1      tron 	struct linux_sched_param lp;
    461  1.49      elad 	struct sched_param sp;
    462  1.49      elad 
    463  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    464  1.49      elad 		error = EINVAL;
    465  1.49      elad 		goto out;
    466  1.49      elad 	}
    467   1.1      tron 
    468  1.49      elad 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    469  1.49      elad 	if (error)
    470  1.49      elad 		goto out;
    471   1.1      tron 
    472  1.49      elad 	/* We need the current policy in Linux terms. */
    473  1.66     njoly 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    474  1.49      elad 	if (error)
    475  1.49      elad 		goto out;
    476  1.49      elad 	error = sched_native2linux(policy, NULL, &policy, NULL);
    477  1.49      elad 	if (error)
    478  1.49      elad 		goto out;
    479   1.1      tron 
    480  1.49      elad 	error = sched_linux2native(policy, &lp, &policy, &sp);
    481   1.1      tron 	if (error)
    482  1.49      elad 		goto out;
    483   1.1      tron 
    484  1.66     njoly 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    485  1.49      elad 	if (error)
    486  1.49      elad 		goto out;
    487   1.1      tron 
    488  1.49      elad  out:
    489  1.49      elad 	return error;
    490   1.1      tron }
    491   1.1      tron 
    492   1.1      tron int
    493  1.46       dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
    494   1.1      tron {
    495  1.46       dsl 	/* {
    496   1.1      tron 		syscallarg(linux_pid_t) pid;
    497   1.1      tron 		syscallarg(struct linux_sched_param *) sp;
    498  1.46       dsl 	} */
    499   1.1      tron 	struct linux_sched_param lp;
    500  1.49      elad 	struct sched_param sp;
    501  1.50      elad 	int error, policy;
    502  1.49      elad 
    503  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    504  1.49      elad 		error = EINVAL;
    505  1.49      elad 		goto out;
    506  1.49      elad 	}
    507   1.1      tron 
    508  1.66     njoly 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
    509  1.49      elad 	if (error)
    510  1.49      elad 		goto out;
    511  1.80  christos 	DPRINTF("%s: native: policy %d, priority %d\n",
    512  1.80  christos 	    __func__, policy, sp.sched_priority);
    513   1.1      tron 
    514  1.50      elad 	error = sched_native2linux(policy, &sp, NULL, &lp);
    515  1.49      elad 	if (error)
    516  1.49      elad 		goto out;
    517  1.80  christos 	DPRINTF("%s: linux: policy %d, priority %d\n",
    518  1.80  christos 	    __func__, policy, lp.sched_priority);
    519  1.47      elad 
    520  1.49      elad 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    521  1.49      elad 	if (error)
    522  1.49      elad 		goto out;
    523   1.1      tron 
    524  1.49      elad  out:
    525  1.49      elad 	return error;
    526   1.1      tron }
    527   1.1      tron 
    528   1.1      tron int
    529  1.46       dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
    530   1.1      tron {
    531  1.46       dsl 	/* {
    532   1.1      tron 		syscallarg(linux_pid_t) pid;
    533   1.1      tron 		syscallarg(int) policy;
    534  1.61     njoly 		syscallarg(cont struct linux_sched_param *) sp;
    535  1.46       dsl 	} */
    536  1.49      elad 	int error, policy;
    537   1.1      tron 	struct linux_sched_param lp;
    538  1.49      elad 	struct sched_param sp;
    539   1.1      tron 
    540  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    541  1.49      elad 		error = EINVAL;
    542  1.49      elad 		goto out;
    543  1.49      elad 	}
    544   1.1      tron 
    545   1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    546   1.1      tron 	if (error)
    547  1.49      elad 		goto out;
    548  1.80  christos 	DPRINTF("%s: linux: policy %d, priority %d\n",
    549  1.80  christos 	    __func__, SCARG(uap, policy), lp.sched_priority);
    550   1.1      tron 
    551  1.49      elad 	error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
    552  1.49      elad 	if (error)
    553  1.49      elad 		goto out;
    554  1.80  christos 	DPRINTF("%s: native: policy %d, priority %d\n",
    555  1.80  christos 	    __func__, policy, sp.sched_priority);
    556   1.1      tron 
    557  1.66     njoly 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    558  1.49      elad 	if (error)
    559  1.49      elad 		goto out;
    560   1.1      tron 
    561  1.49      elad  out:
    562  1.49      elad 	return error;
    563   1.1      tron }
    564   1.1      tron 
    565   1.1      tron int
    566  1.46       dsl linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
    567   1.1      tron {
    568  1.46       dsl 	/* {
    569   1.1      tron 		syscallarg(linux_pid_t) pid;
    570  1.46       dsl 	} */
    571  1.49      elad 	int error, policy;
    572   1.1      tron 
    573   1.1      tron 	*retval = -1;
    574   1.1      tron 
    575  1.66     njoly 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    576  1.49      elad 	if (error)
    577  1.49      elad 		goto out;
    578  1.49      elad 
    579  1.49      elad 	error = sched_native2linux(policy, NULL, &policy, NULL);
    580  1.49      elad 	if (error)
    581  1.49      elad 		goto out;
    582  1.49      elad 
    583  1.49      elad 	*retval = policy;
    584   1.1      tron 
    585  1.49      elad  out:
    586  1.49      elad 	return error;
    587   1.1      tron }
    588   1.1      tron 
    589   1.1      tron int
    590  1.46       dsl linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    591   1.1      tron {
    592  1.11  gmcgarry 
    593  1.11  gmcgarry 	yield();
    594   1.1      tron 	return 0;
    595   1.1      tron }
    596   1.1      tron 
    597   1.1      tron int
    598  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)
    599   1.1      tron {
    600  1.46       dsl 	/* {
    601   1.1      tron 		syscallarg(int) policy;
    602  1.46       dsl 	} */
    603   1.1      tron 
    604  1.55     njoly 	switch (SCARG(uap, policy)) {
    605  1.55     njoly 	case LINUX_SCHED_OTHER:
    606  1.55     njoly 		*retval = 0;
    607  1.55     njoly 		break;
    608  1.55     njoly 	case LINUX_SCHED_FIFO:
    609  1.55     njoly 	case LINUX_SCHED_RR:
    610  1.55     njoly 		*retval = LINUX_SCHED_RTPRIO_MAX;
    611  1.55     njoly 		break;
    612  1.55     njoly 	default:
    613   1.1      tron 		return EINVAL;
    614   1.1      tron 	}
    615   1.1      tron 
    616   1.1      tron 	return 0;
    617   1.1      tron }
    618   1.1      tron 
    619   1.1      tron int
    620  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)
    621   1.1      tron {
    622  1.46       dsl 	/* {
    623   1.1      tron 		syscallarg(int) policy;
    624  1.46       dsl 	} */
    625   1.1      tron 
    626  1.55     njoly 	switch (SCARG(uap, policy)) {
    627  1.55     njoly 	case LINUX_SCHED_OTHER:
    628  1.55     njoly 		*retval = 0;
    629  1.55     njoly 		break;
    630  1.55     njoly 	case LINUX_SCHED_FIFO:
    631  1.55     njoly 	case LINUX_SCHED_RR:
    632  1.55     njoly 		*retval = LINUX_SCHED_RTPRIO_MIN;
    633  1.55     njoly 		break;
    634  1.55     njoly 	default:
    635   1.1      tron 		return EINVAL;
    636   1.1      tron 	}
    637   1.1      tron 
    638   1.1      tron 	return 0;
    639   1.1      tron }
    640  1.14  jdolecek 
    641  1.63       chs int
    642  1.63       chs linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
    643  1.63       chs {
    644  1.63       chs 
    645  1.63       chs 	lwp_exit(l);
    646  1.63       chs 	return 0;
    647  1.63       chs }
    648  1.63       chs 
    649  1.14  jdolecek #ifndef __m68k__
    650  1.14  jdolecek /* Present on everything but m68k */
    651  1.14  jdolecek int
    652  1.46       dsl linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
    653  1.14  jdolecek {
    654  1.14  jdolecek 
    655  1.46       dsl 	return sys_exit(l, (const void *)uap, retval);
    656  1.14  jdolecek }
    657  1.14  jdolecek #endif /* !__m68k__ */
    658  1.19      manu 
    659  1.19      manu int
    660  1.46       dsl linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
    661  1.19      manu {
    662  1.46       dsl 	/* {
    663  1.19      manu 		syscallarg(int *) tidptr;
    664  1.46       dsl 	} */
    665  1.19      manu 	struct linux_emuldata *led;
    666  1.19      manu 
    667  1.63       chs 	led = (struct linux_emuldata *)l->l_emuldata;
    668  1.63       chs 	led->led_clear_tid = SCARG(uap, tid);
    669  1.63       chs 	*retval = l->l_lid;
    670  1.19      manu 
    671  1.19      manu 	return 0;
    672  1.19      manu }
    673  1.20      manu 
    674  1.20      manu /* ARGUSED1 */
    675  1.20      manu int
    676  1.46       dsl linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
    677  1.20      manu {
    678  1.31      manu 
    679  1.63       chs 	*retval = l->l_lid;
    680  1.31      manu 	return 0;
    681  1.31      manu }
    682  1.31      manu 
    683  1.68  christos /*
    684  1.68  christos  * The affinity syscalls assume that the layout of our cpu kcpuset is
    685  1.68  christos  * the same as linux's: a linear bitmask.
    686  1.68  christos  */
    687  1.22      manu int
    688  1.46       dsl linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
    689  1.22      manu {
    690  1.46       dsl 	/* {
    691  1.63       chs 		syscallarg(linux_pid_t) pid;
    692  1.22      manu 		syscallarg(unsigned int) len;
    693  1.22      manu 		syscallarg(unsigned long *) mask;
    694  1.46       dsl 	} */
    695  1.76   thorpej 	struct proc *p;
    696  1.68  christos 	struct lwp *t;
    697  1.68  christos 	kcpuset_t *kcset;
    698  1.68  christos 	size_t size;
    699  1.68  christos 	cpuid_t i;
    700  1.68  christos 	int error;
    701  1.22      manu 
    702  1.68  christos 	size = LINUX_CPU_MASK_SIZE;
    703  1.60     njoly 	if (SCARG(uap, len) < size)
    704  1.22      manu 		return EINVAL;
    705  1.22      manu 
    706  1.76   thorpej 	if (SCARG(uap, pid) == 0) {
    707  1.76   thorpej 		p = curproc;
    708  1.76   thorpej 		mutex_enter(p->p_lock);
    709  1.76   thorpej 		t = curlwp;
    710  1.76   thorpej 	} else {
    711  1.76   thorpej 		t = lwp_find2(-1, SCARG(uap, pid));
    712  1.76   thorpej 		if (__predict_false(t == NULL)) {
    713  1.76   thorpej 			return ESRCH;
    714  1.76   thorpej 		}
    715  1.76   thorpej 		p = t->l_proc;
    716  1.76   thorpej 		KASSERT(mutex_owned(p->p_lock));
    717  1.76   thorpej 	}
    718  1.22      manu 
    719  1.68  christos 	/* Check the permission */
    720  1.68  christos 	if (kauth_authorize_process(l->l_cred,
    721  1.76   thorpej 	    KAUTH_PROCESS_SCHEDULER_GETAFFINITY, p, NULL, NULL, NULL)) {
    722  1.76   thorpej 		mutex_exit(p->p_lock);
    723  1.68  christos 		return EPERM;
    724  1.68  christos 	}
    725  1.68  christos 
    726  1.68  christos 	kcpuset_create(&kcset, true);
    727  1.68  christos 	lwp_lock(t);
    728  1.68  christos 	if (t->l_affinity != NULL)
    729  1.68  christos 		kcpuset_copy(kcset, t->l_affinity);
    730  1.68  christos 	else {
    731  1.68  christos 		/*
    732  1.68  christos 		 * All available CPUs should be masked when affinity has not
    733  1.68  christos 		 * been set.
    734  1.68  christos 		 */
    735  1.68  christos 		kcpuset_zero(kcset);
    736  1.68  christos 		for (i = 0; i < ncpu; i++)
    737  1.68  christos 			kcpuset_set(kcset, i);
    738  1.68  christos 	}
    739  1.68  christos 	lwp_unlock(t);
    740  1.76   thorpej 	mutex_exit(p->p_lock);
    741  1.68  christos 	error = kcpuset_copyout(kcset, (cpuset_t *)SCARG(uap, mask), size);
    742  1.68  christos 	kcpuset_unuse(kcset, NULL);
    743  1.60     njoly 	*retval = size;
    744  1.59     njoly 	return error;
    745  1.22      manu }
    746  1.22      manu 
    747  1.22      manu int
    748  1.46       dsl linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
    749  1.22      manu {
    750  1.46       dsl 	/* {
    751  1.63       chs 		syscallarg(linux_pid_t) pid;
    752  1.22      manu 		syscallarg(unsigned int) len;
    753  1.22      manu 		syscallarg(unsigned long *) mask;
    754  1.46       dsl 	} */
    755  1.68  christos 	struct sys__sched_setaffinity_args ssa;
    756  1.68  christos 	size_t size;
    757  1.76   thorpej 	pid_t pid;
    758  1.77       rin 	lwpid_t lid;
    759  1.68  christos 
    760  1.68  christos 	size = LINUX_CPU_MASK_SIZE;
    761  1.68  christos 	if (SCARG(uap, len) < size)
    762  1.68  christos 		return EINVAL;
    763  1.22      manu 
    764  1.77       rin 	lid = SCARG(uap, pid);
    765  1.77       rin 	if (lid != 0) {
    766  1.76   thorpej 		/* Get the canonical PID for the process. */
    767  1.78        ad 		mutex_enter(&proc_lock);
    768  1.76   thorpej 		struct proc *p = proc_find_lwpid(SCARG(uap, pid));
    769  1.76   thorpej 		if (p == NULL) {
    770  1.78        ad 			mutex_exit(&proc_lock);
    771  1.76   thorpej 			return ESRCH;
    772  1.76   thorpej 		}
    773  1.76   thorpej 		pid = p->p_pid;
    774  1.78        ad 		mutex_exit(&proc_lock);
    775  1.76   thorpej 	} else {
    776  1.77       rin 		pid = curproc->p_pid;
    777  1.77       rin 		lid = curlwp->l_lid;
    778  1.76   thorpej 	}
    779  1.76   thorpej 
    780  1.76   thorpej 	SCARG(&ssa, pid) = pid;
    781  1.77       rin 	SCARG(&ssa, lid) = lid;
    782  1.68  christos 	SCARG(&ssa, size) = size;
    783  1.68  christos 	SCARG(&ssa, cpuset) = (cpuset_t *)SCARG(uap, mask);
    784  1.22      manu 
    785  1.68  christos 	return sys__sched_setaffinity(l, &ssa, retval);
    786  1.64       dsl }
    787  1.86  christos 
    788  1.86  christos int
    789  1.86  christos linux_sys___prctl(struct lwp *l, const struct linux_sys___prctl_args *uap,
    790  1.86  christos     register_t *retval)
    791  1.86  christos {
    792  1.86  christos 	/* {
    793  1.86  christos 		syscallarg(int)    code;
    794  1.87  christos 		syscallarg(void *) args[LINUX_SYS_MAXSYSARGS];
    795  1.86  christos 	} */
    796  1.86  christos 
    797  1.86  christos 	unsigned int c = SCARG(uap, code);
    798  1.86  christos 
    799  1.86  christos         /* TODO: add other commonly used prctl codes */
    800  1.86  christos 	switch(c) {
    801  1.86  christos 	case LINUX_PR_SET_NAME: {
    802  1.86  christos 		struct sys__lwp_setname_args sls;
    803  1.86  christos 		SCARG(&sls, name) = (char *) SCARG(uap, args[0]);
    804  1.86  christos 		return sys__lwp_setname(l, &sls, retval);
    805  1.86  christos 	}
    806  1.86  christos 
    807  1.86  christos 	case LINUX_PR_GET_NAME: {
    808  1.86  christos 		struct sys__lwp_getname_args slg;
    809  1.86  christos 		SCARG(&slg, name) = (char *) SCARG(uap, args[0]);
    810  1.86  christos 		SCARG(&slg, len) = MAXCOMLEN;
    811  1.86  christos 		return sys__lwp_getname(l, &slg, retval);
    812  1.86  christos 		}
    813  1.86  christos 	default:
    814  1.86  christos 		printf("Unimplemented linux prctl code: (%d)", c);
    815  1.86  christos 		return ENOSYS;
    816  1.86  christos 	}
    817  1.86  christos 
    818  1.86  christos }
    819