Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.63
      1  1.63       chs /*	$NetBSD: linux_sched.c,v 1.63 2010/07/07 01:30:35 chs Exp $	*/
      2   1.1      tron 
      3   1.1      tron /*-
      4   1.1      tron  * Copyright (c) 1999 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.63       chs __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.63 2010/07/07 01:30:35 chs 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.22      manu #include <sys/malloc.h>
     46   1.1      tron #include <sys/syscallargs.h>
     47  1.14  jdolecek #include <sys/wait.h>
     48  1.30      elad #include <sys/kauth.h>
     49  1.34      manu #include <sys/ptrace.h>
     50  1.63       chs #include <sys/atomic.h>
     51   1.3     itohy 
     52  1.43        ad #include <sys/cpu.h>
     53   1.1      tron 
     54   1.1      tron #include <compat/linux/common/linux_types.h>
     55   1.1      tron #include <compat/linux/common/linux_signal.h>
     56  1.19      manu #include <compat/linux/common/linux_emuldata.h>
     57  1.44     njoly #include <compat/linux/common/linux_ipc.h>
     58  1.44     njoly #include <compat/linux/common/linux_sem.h>
     59  1.58  christos #include <compat/linux/common/linux_exec.h>
     60  1.63       chs #include <compat/linux/common/linux_machdep.h>
     61   1.1      tron 
     62   1.1      tron #include <compat/linux/linux_syscallargs.h>
     63   1.1      tron 
     64   1.1      tron #include <compat/linux/common/linux_sched.h>
     65   1.1      tron 
     66  1.63       chs static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *, register_t *);
     67  1.63       chs 
     68  1.63       chs static void
     69  1.63       chs linux_child_return(void *arg)
     70  1.63       chs {
     71  1.63       chs 	struct lwp *l = arg;
     72  1.63       chs 	struct proc *p = l->l_proc;
     73  1.63       chs 	struct linux_emuldata *led = l->l_emuldata;
     74  1.63       chs 	void *ctp = led->led_child_tidptr;
     75  1.63       chs 
     76  1.63       chs 	if (ctp) {
     77  1.63       chs 		if (copyout(&p->p_pid, ctp, sizeof(p->p_pid)) != 0)
     78  1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
     79  1.63       chs 			    "failed (child_tidptr = %p, tid = %d)\n",
     80  1.63       chs 			    __func__, ctp, p->p_pid);
     81  1.63       chs 	}
     82  1.63       chs 	child_return(arg);
     83  1.63       chs }
     84  1.63       chs 
     85   1.1      tron int
     86  1.46       dsl linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
     87   1.1      tron {
     88  1.46       dsl 	/* {
     89   1.1      tron 		syscallarg(int) flags;
     90   1.1      tron 		syscallarg(void *) stack;
     91  1.19      manu 		syscallarg(void *) parent_tidptr;
     92  1.63       chs 		syscallarg(void *) tls;
     93  1.19      manu 		syscallarg(void *) child_tidptr;
     94  1.46       dsl 	} */
     95  1.58  christos 	struct proc *p;
     96  1.19      manu 	struct linux_emuldata *led;
     97  1.63       chs 	int flags, sig, error;
     98   1.1      tron 
     99   1.1      tron 	/*
    100   1.1      tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
    101   1.1      tron 	 */
    102   1.1      tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
    103   1.1      tron 		return (EINVAL);
    104   1.1      tron 
    105  1.13  jdolecek 	/*
    106  1.13  jdolecek 	 * Thread group implies shared signals. Shared signals
    107  1.13  jdolecek 	 * imply shared VM. This matches what Linux kernel does.
    108  1.13  jdolecek 	 */
    109  1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
    110  1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    111  1.13  jdolecek 		return (EINVAL);
    112  1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    113  1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    114  1.13  jdolecek 		return (EINVAL);
    115  1.13  jdolecek 
    116  1.63       chs 	/*
    117  1.63       chs 	 * The thread group flavor is implemented totally differently.
    118  1.63       chs 	 */
    119  1.63       chs 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD) {
    120  1.63       chs 		return linux_clone_nptl(l, uap, retval);
    121  1.63       chs 	}
    122  1.63       chs 
    123   1.1      tron 	flags = 0;
    124   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    125   1.1      tron 		flags |= FORK_SHAREVM;
    126   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    127   1.1      tron 		flags |= FORK_SHARECWD;
    128   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    129   1.1      tron 		flags |= FORK_SHAREFILES;
    130   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    131   1.1      tron 		flags |= FORK_SHARESIGS;
    132   1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    133   1.1      tron 		flags |= FORK_PPWAIT;
    134   1.1      tron 
    135  1.34      manu 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    136  1.34      manu 	if (sig < 0 || sig >= LINUX__NSIG)
    137  1.34      manu 		return (EINVAL);
    138  1.34      manu 	sig = linux_to_native_signo[sig];
    139   1.1      tron 
    140  1.63       chs 	if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
    141  1.63       chs 		led = l->l_emuldata;
    142  1.63       chs 		led->led_child_tidptr = SCARG(uap, child_tidptr);
    143  1.63       chs 	}
    144  1.19      manu 
    145   1.1      tron 	/*
    146   1.1      tron 	 * Note that Linux does not provide a portable way of specifying
    147   1.1      tron 	 * the stack area; the caller must know if the stack grows up
    148   1.1      tron 	 * or down.  So, we pass a stack size of 0, so that the code
    149   1.1      tron 	 * that makes this adjustment is a noop.
    150   1.1      tron 	 */
    151  1.19      manu 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    152  1.63       chs 	    linux_child_return, NULL, retval, &p)) != 0)
    153  1.63       chs 		return error;
    154  1.63       chs 
    155  1.63       chs 	return 0;
    156  1.63       chs }
    157  1.63       chs 
    158  1.63       chs static int
    159  1.63       chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
    160  1.63       chs {
    161  1.63       chs 	/* {
    162  1.63       chs 		syscallarg(int) flags;
    163  1.63       chs 		syscallarg(void *) stack;
    164  1.63       chs 		syscallarg(void *) parent_tidptr;
    165  1.63       chs 		syscallarg(void *) tls;
    166  1.63       chs 		syscallarg(void *) child_tidptr;
    167  1.63       chs 	} */
    168  1.63       chs 	struct proc *p;
    169  1.63       chs 	struct lwp *l2;
    170  1.63       chs 	struct linux_emuldata *led;
    171  1.63       chs 	void *parent_tidptr, *tls, *child_tidptr;
    172  1.63       chs 	struct schedstate_percpu *spc;
    173  1.63       chs 	vaddr_t uaddr;
    174  1.63       chs 	lwpid_t lid;
    175  1.63       chs 	int flags, tnprocs, error;
    176  1.63       chs 
    177  1.63       chs 	p = l->l_proc;
    178  1.63       chs 	flags = SCARG(uap, flags);
    179  1.63       chs 	parent_tidptr = SCARG(uap, parent_tidptr);
    180  1.63       chs 	tls = SCARG(uap, tls);
    181  1.63       chs 	child_tidptr = SCARG(uap, child_tidptr);
    182  1.63       chs 
    183  1.63       chs 	tnprocs = atomic_inc_uint_nv(&nprocs);
    184  1.63       chs 	if (__predict_false(tnprocs >= maxproc) ||
    185  1.63       chs 	    kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p,
    186  1.63       chs 				    KAUTH_ARG(tnprocs), NULL, NULL) != 0) {
    187  1.63       chs 		atomic_dec_uint(&nprocs);
    188  1.63       chs 		return EAGAIN;
    189  1.63       chs 	}
    190  1.63       chs 
    191  1.63       chs 	uaddr = uvm_uarea_alloc();
    192  1.63       chs 	if (__predict_false(uaddr == 0)) {
    193  1.63       chs 		atomic_dec_uint(&nprocs);
    194  1.63       chs 		return ENOMEM;
    195  1.63       chs 	}
    196  1.63       chs 
    197  1.63       chs 	error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
    198  1.63       chs 			   SCARG(uap, stack), 0, child_return, NULL, &l2,
    199  1.63       chs 			   l->l_class);
    200  1.63       chs 	if (__predict_false(error)) {
    201  1.63       chs 		atomic_dec_uint(&nprocs);
    202  1.63       chs 		uvm_uarea_free(uaddr);
    203  1.19      manu 		return error;
    204  1.63       chs 	}
    205  1.63       chs 	lid = l2->l_lid;
    206  1.19      manu 
    207  1.63       chs 	/* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
    208  1.63       chs 	if (flags & LINUX_CLONE_CHILD_CLEARTID) {
    209  1.63       chs 		led = l2->l_emuldata;
    210  1.63       chs 		led->led_clear_tid = child_tidptr;
    211  1.63       chs 	}
    212  1.63       chs 
    213  1.63       chs 	/* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
    214  1.63       chs 	if (flags & LINUX_CLONE_PARENT_SETTID) {
    215  1.63       chs 		if (copyout(&lid, parent_tidptr, sizeof(lid)) != 0)
    216  1.63       chs 			printf("%s: LINUX_CLONE_PARENT_SETTID "
    217  1.63       chs 			    "failed (parent_tidptr = %p tid = %d)\n",
    218  1.63       chs 			    __func__, parent_tidptr, lid);
    219  1.63       chs 	}
    220  1.63       chs 
    221  1.63       chs 	/* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory  */
    222  1.63       chs 	if (flags & LINUX_CLONE_CHILD_SETTID) {
    223  1.63       chs 		if (copyout(&lid, child_tidptr, sizeof(lid)) != 0)
    224  1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
    225  1.63       chs 			    "failed (child_tidptr = %p, tid = %d)\n",
    226  1.63       chs 			    __func__, child_tidptr, lid);
    227  1.63       chs 	}
    228  1.63       chs 
    229  1.63       chs 	if (flags & LINUX_CLONE_SETTLS) {
    230  1.63       chs 		error = LINUX_LWP_SETPRIVATE(l2, tls);
    231  1.63       chs 		if (error) {
    232  1.63       chs 			lwp_exit(l2);
    233  1.63       chs 			return error;
    234  1.63       chs 		}
    235  1.63       chs 	}
    236  1.63       chs 
    237  1.63       chs 	/*
    238  1.63       chs 	 * Set the new LWP running, unless the process is stopping,
    239  1.63       chs 	 * then the LWP is created stopped.
    240  1.63       chs 	 */
    241  1.63       chs 	mutex_enter(p->p_lock);
    242  1.63       chs 	lwp_lock(l2);
    243  1.63       chs 	spc = &l2->l_cpu->ci_schedstate;
    244  1.63       chs 	if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    245  1.63       chs 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
    246  1.63       chs 			KASSERT(l2->l_wchan == NULL);
    247  1.63       chs 	    		l2->l_stat = LSSTOP;
    248  1.63       chs 			p->p_nrlwps--;
    249  1.63       chs 			lwp_unlock_to(l2, spc->spc_lwplock);
    250  1.63       chs 		} else {
    251  1.63       chs 			KASSERT(lwp_locked(l2, spc->spc_mutex));
    252  1.63       chs 			l2->l_stat = LSRUN;
    253  1.63       chs 			sched_enqueue(l2, false);
    254  1.63       chs 			lwp_unlock(l2);
    255  1.63       chs 		}
    256  1.63       chs 	} else {
    257  1.63       chs 		l2->l_stat = LSSUSPENDED;
    258  1.63       chs 		p->p_nrlwps--;
    259  1.63       chs 		lwp_unlock_to(l2, spc->spc_lwplock);
    260  1.63       chs 	}
    261  1.63       chs 	mutex_exit(p->p_lock);
    262  1.58  christos 
    263  1.63       chs 	retval[0] = lid;
    264  1.63       chs 	retval[1] = 0;
    265  1.19      manu 	return 0;
    266   1.1      tron }
    267   1.1      tron 
    268  1.49      elad /*
    269  1.49      elad  * linux realtime priority
    270  1.49      elad  *
    271  1.49      elad  * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
    272  1.49      elad  *
    273  1.49      elad  * - SCHED_OTHER tasks don't have realtime priorities.
    274  1.49      elad  *   in particular, sched_param::sched_priority is always 0.
    275  1.49      elad  */
    276  1.49      elad 
    277  1.49      elad #define	LINUX_SCHED_RTPRIO_MIN	1
    278  1.49      elad #define	LINUX_SCHED_RTPRIO_MAX	99
    279  1.49      elad 
    280  1.49      elad static int
    281  1.49      elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
    282  1.49      elad     int *native_policy, struct sched_param *native_params)
    283  1.49      elad {
    284  1.49      elad 
    285  1.49      elad 	switch (linux_policy) {
    286  1.49      elad 	case LINUX_SCHED_OTHER:
    287  1.49      elad 		if (native_policy != NULL) {
    288  1.49      elad 			*native_policy = SCHED_OTHER;
    289  1.49      elad 		}
    290  1.49      elad 		break;
    291  1.49      elad 
    292  1.49      elad 	case LINUX_SCHED_FIFO:
    293  1.49      elad 		if (native_policy != NULL) {
    294  1.49      elad 			*native_policy = SCHED_FIFO;
    295  1.49      elad 		}
    296  1.49      elad 		break;
    297  1.49      elad 
    298  1.49      elad 	case LINUX_SCHED_RR:
    299  1.49      elad 		if (native_policy != NULL) {
    300  1.49      elad 			*native_policy = SCHED_RR;
    301  1.49      elad 		}
    302  1.49      elad 		break;
    303  1.49      elad 
    304  1.49      elad 	default:
    305  1.49      elad 		return EINVAL;
    306  1.49      elad 	}
    307  1.49      elad 
    308  1.49      elad 	if (linux_params != NULL) {
    309  1.49      elad 		int prio = linux_params->sched_priority;
    310  1.49      elad 
    311  1.49      elad 		KASSERT(native_params != NULL);
    312  1.49      elad 
    313  1.49      elad 		if (linux_policy == LINUX_SCHED_OTHER) {
    314  1.49      elad 			if (prio != 0) {
    315  1.49      elad 				return EINVAL;
    316  1.49      elad 			}
    317  1.49      elad 			native_params->sched_priority = PRI_NONE; /* XXX */
    318  1.49      elad 		} else {
    319  1.49      elad 			if (prio < LINUX_SCHED_RTPRIO_MIN ||
    320  1.49      elad 			    prio > LINUX_SCHED_RTPRIO_MAX) {
    321  1.49      elad 				return EINVAL;
    322  1.49      elad 			}
    323  1.49      elad 			native_params->sched_priority =
    324  1.49      elad 			    (prio - LINUX_SCHED_RTPRIO_MIN)
    325  1.49      elad 			    * (SCHED_PRI_MAX - SCHED_PRI_MIN)
    326  1.49      elad 			    / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    327  1.49      elad 			    + SCHED_PRI_MIN;
    328  1.49      elad 		}
    329  1.49      elad 	}
    330  1.49      elad 
    331  1.49      elad 	return 0;
    332  1.49      elad }
    333  1.49      elad 
    334  1.49      elad static int
    335  1.49      elad sched_native2linux(int native_policy, struct sched_param *native_params,
    336  1.49      elad     int *linux_policy, struct linux_sched_param *linux_params)
    337  1.49      elad {
    338  1.49      elad 
    339  1.49      elad 	switch (native_policy) {
    340  1.49      elad 	case SCHED_OTHER:
    341  1.49      elad 		if (linux_policy != NULL) {
    342  1.49      elad 			*linux_policy = LINUX_SCHED_OTHER;
    343  1.49      elad 		}
    344  1.49      elad 		break;
    345  1.49      elad 
    346  1.49      elad 	case SCHED_FIFO:
    347  1.49      elad 		if (linux_policy != NULL) {
    348  1.49      elad 			*linux_policy = LINUX_SCHED_FIFO;
    349  1.49      elad 		}
    350  1.49      elad 		break;
    351  1.49      elad 
    352  1.49      elad 	case SCHED_RR:
    353  1.49      elad 		if (linux_policy != NULL) {
    354  1.49      elad 			*linux_policy = LINUX_SCHED_RR;
    355  1.49      elad 		}
    356  1.49      elad 		break;
    357  1.49      elad 
    358  1.49      elad 	default:
    359  1.49      elad 		panic("%s: unknown policy %d\n", __func__, native_policy);
    360  1.49      elad 	}
    361  1.49      elad 
    362  1.49      elad 	if (native_params != NULL) {
    363  1.49      elad 		int prio = native_params->sched_priority;
    364  1.49      elad 
    365  1.49      elad 		KASSERT(prio >= SCHED_PRI_MIN);
    366  1.49      elad 		KASSERT(prio <= SCHED_PRI_MAX);
    367  1.49      elad 		KASSERT(linux_params != NULL);
    368  1.56  jmcneill 
    369  1.56  jmcneill #ifdef DEBUG_LINUX
    370  1.55     njoly 		printf("native2linux: native: policy %d, priority %d\n",
    371  1.55     njoly 		    native_policy, prio);
    372  1.56  jmcneill #endif
    373  1.49      elad 
    374  1.49      elad 		if (native_policy == SCHED_OTHER) {
    375  1.49      elad 			linux_params->sched_priority = 0;
    376  1.49      elad 		} else {
    377  1.49      elad 			linux_params->sched_priority =
    378  1.49      elad 			    (prio - SCHED_PRI_MIN)
    379  1.49      elad 			    * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    380  1.49      elad 			    / (SCHED_PRI_MAX - SCHED_PRI_MIN)
    381  1.49      elad 			    + LINUX_SCHED_RTPRIO_MIN;
    382  1.49      elad 		}
    383  1.56  jmcneill #ifdef DEBUG_LINUX
    384  1.55     njoly 		printf("native2linux: linux: policy %d, priority %d\n",
    385  1.55     njoly 		    -1, linux_params->sched_priority);
    386  1.56  jmcneill #endif
    387  1.49      elad 	}
    388  1.49      elad 
    389  1.49      elad 	return 0;
    390  1.49      elad }
    391  1.49      elad 
    392   1.1      tron int
    393  1.46       dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
    394   1.1      tron {
    395  1.46       dsl 	/* {
    396   1.1      tron 		syscallarg(linux_pid_t) pid;
    397   1.1      tron 		syscallarg(const struct linux_sched_param *) sp;
    398  1.46       dsl 	} */
    399  1.49      elad 	int error, policy;
    400   1.1      tron 	struct linux_sched_param lp;
    401  1.49      elad 	struct sched_param sp;
    402  1.49      elad 
    403  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    404  1.49      elad 		error = EINVAL;
    405  1.49      elad 		goto out;
    406  1.49      elad 	}
    407   1.1      tron 
    408  1.49      elad 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    409  1.49      elad 	if (error)
    410  1.49      elad 		goto out;
    411   1.1      tron 
    412  1.49      elad 	/* We need the current policy in Linux terms. */
    413  1.63       chs 	error = do_sched_getparam(0, SCARG(uap, pid), &policy, NULL);
    414  1.49      elad 	if (error)
    415  1.49      elad 		goto out;
    416  1.49      elad 	error = sched_native2linux(policy, NULL, &policy, NULL);
    417  1.49      elad 	if (error)
    418  1.49      elad 		goto out;
    419   1.1      tron 
    420  1.49      elad 	error = sched_linux2native(policy, &lp, &policy, &sp);
    421   1.1      tron 	if (error)
    422  1.49      elad 		goto out;
    423   1.1      tron 
    424  1.63       chs 	error = do_sched_setparam(0, SCARG(uap, pid), policy, &sp);
    425  1.49      elad 	if (error)
    426  1.49      elad 		goto out;
    427   1.1      tron 
    428  1.49      elad  out:
    429  1.49      elad 	return error;
    430   1.1      tron }
    431   1.1      tron 
    432   1.1      tron int
    433  1.46       dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
    434   1.1      tron {
    435  1.46       dsl 	/* {
    436   1.1      tron 		syscallarg(linux_pid_t) pid;
    437   1.1      tron 		syscallarg(struct linux_sched_param *) sp;
    438  1.46       dsl 	} */
    439   1.1      tron 	struct linux_sched_param lp;
    440  1.49      elad 	struct sched_param sp;
    441  1.50      elad 	int error, policy;
    442  1.49      elad 
    443  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    444  1.49      elad 		error = EINVAL;
    445  1.49      elad 		goto out;
    446  1.49      elad 	}
    447   1.1      tron 
    448  1.63       chs 	error = do_sched_getparam(0, SCARG(uap, pid), &policy, &sp);
    449  1.49      elad 	if (error)
    450  1.49      elad 		goto out;
    451  1.56  jmcneill #ifdef DEBUG_LINUX
    452  1.55     njoly 	printf("getparam: native: policy %d, priority %d\n",
    453  1.55     njoly 	    policy, sp.sched_priority);
    454  1.56  jmcneill #endif
    455   1.1      tron 
    456  1.50      elad 	error = sched_native2linux(policy, &sp, NULL, &lp);
    457  1.49      elad 	if (error)
    458  1.49      elad 		goto out;
    459  1.56  jmcneill #ifdef DEBUG_LINUX
    460  1.55     njoly 	printf("getparam: linux: policy %d, priority %d\n",
    461  1.55     njoly 	    policy, lp.sched_priority);
    462  1.56  jmcneill #endif
    463  1.47      elad 
    464  1.49      elad 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    465  1.49      elad 	if (error)
    466  1.49      elad 		goto out;
    467   1.1      tron 
    468  1.49      elad  out:
    469  1.49      elad 	return error;
    470   1.1      tron }
    471   1.1      tron 
    472   1.1      tron int
    473  1.46       dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
    474   1.1      tron {
    475  1.46       dsl 	/* {
    476   1.1      tron 		syscallarg(linux_pid_t) pid;
    477   1.1      tron 		syscallarg(int) policy;
    478  1.61     njoly 		syscallarg(cont struct linux_sched_param *) sp;
    479  1.46       dsl 	} */
    480  1.49      elad 	int error, policy;
    481   1.1      tron 	struct linux_sched_param lp;
    482  1.49      elad 	struct sched_param sp;
    483   1.1      tron 
    484  1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    485  1.49      elad 		error = EINVAL;
    486  1.49      elad 		goto out;
    487  1.49      elad 	}
    488   1.1      tron 
    489   1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    490   1.1      tron 	if (error)
    491  1.49      elad 		goto out;
    492  1.56  jmcneill #ifdef DEBUG_LINUX
    493  1.55     njoly 	printf("setscheduler: linux: policy %d, priority %d\n",
    494  1.55     njoly 	    SCARG(uap, policy), lp.sched_priority);
    495  1.56  jmcneill #endif
    496   1.1      tron 
    497  1.49      elad 	error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
    498  1.49      elad 	if (error)
    499  1.49      elad 		goto out;
    500  1.56  jmcneill #ifdef DEBUG_LINUX
    501  1.55     njoly 	printf("setscheduler: native: policy %d, priority %d\n",
    502  1.55     njoly 	    policy, sp.sched_priority);
    503  1.56  jmcneill #endif
    504   1.1      tron 
    505  1.63       chs 	error = do_sched_setparam(0, SCARG(uap, pid), policy, &sp);
    506  1.49      elad 	if (error)
    507  1.49      elad 		goto out;
    508   1.1      tron 
    509  1.49      elad  out:
    510  1.49      elad 	return error;
    511   1.1      tron }
    512   1.1      tron 
    513   1.1      tron int
    514  1.46       dsl linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
    515   1.1      tron {
    516  1.46       dsl 	/* {
    517   1.1      tron 		syscallarg(linux_pid_t) pid;
    518  1.46       dsl 	} */
    519  1.49      elad 	int error, policy;
    520   1.1      tron 
    521   1.1      tron 	*retval = -1;
    522   1.1      tron 
    523  1.63       chs 	error = do_sched_getparam(0, SCARG(uap, pid), &policy, NULL);
    524  1.49      elad 	if (error)
    525  1.49      elad 		goto out;
    526  1.49      elad 
    527  1.49      elad 	error = sched_native2linux(policy, NULL, &policy, NULL);
    528  1.49      elad 	if (error)
    529  1.49      elad 		goto out;
    530  1.49      elad 
    531  1.49      elad 	*retval = policy;
    532   1.1      tron 
    533  1.49      elad  out:
    534  1.49      elad 	return error;
    535   1.1      tron }
    536   1.1      tron 
    537   1.1      tron int
    538  1.46       dsl linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    539   1.1      tron {
    540  1.11  gmcgarry 
    541  1.11  gmcgarry 	yield();
    542   1.1      tron 	return 0;
    543   1.1      tron }
    544   1.1      tron 
    545   1.1      tron int
    546  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)
    547   1.1      tron {
    548  1.46       dsl 	/* {
    549   1.1      tron 		syscallarg(int) policy;
    550  1.46       dsl 	} */
    551   1.1      tron 
    552  1.55     njoly 	switch (SCARG(uap, policy)) {
    553  1.55     njoly 	case LINUX_SCHED_OTHER:
    554  1.55     njoly 		*retval = 0;
    555  1.55     njoly 		break;
    556  1.55     njoly 	case LINUX_SCHED_FIFO:
    557  1.55     njoly 	case LINUX_SCHED_RR:
    558  1.55     njoly 		*retval = LINUX_SCHED_RTPRIO_MAX;
    559  1.55     njoly 		break;
    560  1.55     njoly 	default:
    561   1.1      tron 		return EINVAL;
    562   1.1      tron 	}
    563   1.1      tron 
    564   1.1      tron 	return 0;
    565   1.1      tron }
    566   1.1      tron 
    567   1.1      tron int
    568  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)
    569   1.1      tron {
    570  1.46       dsl 	/* {
    571   1.1      tron 		syscallarg(int) policy;
    572  1.46       dsl 	} */
    573   1.1      tron 
    574  1.55     njoly 	switch (SCARG(uap, policy)) {
    575  1.55     njoly 	case LINUX_SCHED_OTHER:
    576  1.55     njoly 		*retval = 0;
    577  1.55     njoly 		break;
    578  1.55     njoly 	case LINUX_SCHED_FIFO:
    579  1.55     njoly 	case LINUX_SCHED_RR:
    580  1.55     njoly 		*retval = LINUX_SCHED_RTPRIO_MIN;
    581  1.55     njoly 		break;
    582  1.55     njoly 	default:
    583   1.1      tron 		return EINVAL;
    584   1.1      tron 	}
    585   1.1      tron 
    586   1.1      tron 	return 0;
    587   1.1      tron }
    588  1.14  jdolecek 
    589  1.63       chs int
    590  1.63       chs linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
    591  1.63       chs {
    592  1.63       chs 
    593  1.63       chs 	lwp_exit(l);
    594  1.63       chs 	return 0;
    595  1.63       chs }
    596  1.63       chs 
    597  1.14  jdolecek #ifndef __m68k__
    598  1.14  jdolecek /* Present on everything but m68k */
    599  1.14  jdolecek int
    600  1.46       dsl linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
    601  1.14  jdolecek {
    602  1.14  jdolecek 
    603  1.46       dsl 	return sys_exit(l, (const void *)uap, retval);
    604  1.14  jdolecek }
    605  1.14  jdolecek #endif /* !__m68k__ */
    606  1.19      manu 
    607  1.19      manu int
    608  1.46       dsl linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
    609  1.19      manu {
    610  1.46       dsl 	/* {
    611  1.19      manu 		syscallarg(int *) tidptr;
    612  1.46       dsl 	} */
    613  1.19      manu 	struct linux_emuldata *led;
    614  1.19      manu 
    615  1.63       chs 	led = (struct linux_emuldata *)l->l_emuldata;
    616  1.63       chs 	led->led_clear_tid = SCARG(uap, tid);
    617  1.63       chs 	*retval = l->l_lid;
    618  1.19      manu 
    619  1.19      manu 	return 0;
    620  1.19      manu }
    621  1.20      manu 
    622  1.20      manu /* ARGUSED1 */
    623  1.20      manu int
    624  1.46       dsl linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
    625  1.20      manu {
    626  1.31      manu 
    627  1.63       chs 	*retval = l->l_lid;
    628  1.31      manu 	return 0;
    629  1.31      manu }
    630  1.31      manu 
    631  1.22      manu int
    632  1.46       dsl linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
    633  1.22      manu {
    634  1.46       dsl 	/* {
    635  1.63       chs 		syscallarg(linux_pid_t) pid;
    636  1.22      manu 		syscallarg(unsigned int) len;
    637  1.22      manu 		syscallarg(unsigned long *) mask;
    638  1.46       dsl 	} */
    639  1.63       chs 	proc_t *p;
    640  1.63       chs 	unsigned long *lp, *data;
    641  1.60     njoly 	int error, size, nb = ncpu;
    642  1.22      manu 
    643  1.60     njoly 	/* Unlike Linux, dynamically calculate cpu mask size */
    644  1.60     njoly 	size = sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT);
    645  1.60     njoly 	if (SCARG(uap, len) < size)
    646  1.22      manu 		return EINVAL;
    647  1.22      manu 
    648  1.62     rmind 	/* XXX: Pointless check.  TODO: Actually implement this. */
    649  1.62     rmind 	mutex_enter(proc_lock);
    650  1.62     rmind 	p = proc_find(SCARG(uap, pid));
    651  1.62     rmind 	mutex_exit(proc_lock);
    652  1.62     rmind 	if (p == NULL) {
    653  1.22      manu 		return ESRCH;
    654  1.62     rmind 	}
    655  1.22      manu 
    656  1.22      manu 	/*
    657  1.22      manu 	 * return the actual number of CPU, tag all of them as available
    658  1.22      manu 	 * The result is a mask, the first CPU being in the least significant
    659  1.22      manu 	 * bit.
    660  1.22      manu 	 */
    661  1.62     rmind 	data = kmem_zalloc(size, KM_SLEEP);
    662  1.63       chs 	lp = data;
    663  1.60     njoly 	while (nb > LONG_BIT) {
    664  1.63       chs 		*lp++ = ~0UL;
    665  1.60     njoly 		nb -= LONG_BIT;
    666  1.60     njoly 	}
    667  1.60     njoly 	if (nb)
    668  1.63       chs 		*lp = (1 << ncpu) - 1;
    669  1.22      manu 
    670  1.60     njoly 	error = copyout(data, SCARG(uap, mask), size);
    671  1.62     rmind 	kmem_free(data, size);
    672  1.60     njoly 	*retval = size;
    673  1.59     njoly 	return error;
    674  1.22      manu }
    675  1.22      manu 
    676  1.22      manu int
    677  1.46       dsl linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
    678  1.22      manu {
    679  1.46       dsl 	/* {
    680  1.63       chs 		syscallarg(linux_pid_t) pid;
    681  1.22      manu 		syscallarg(unsigned int) len;
    682  1.22      manu 		syscallarg(unsigned long *) mask;
    683  1.46       dsl 	} */
    684  1.62     rmind 	proc_t *p;
    685  1.22      manu 
    686  1.62     rmind 	/* XXX: Pointless check.  TODO: Actually implement this. */
    687  1.62     rmind 	mutex_enter(proc_lock);
    688  1.62     rmind 	p = proc_find(SCARG(uap, pid));
    689  1.62     rmind 	mutex_exit(proc_lock);
    690  1.62     rmind 	if (p == NULL) {
    691  1.22      manu 		return ESRCH;
    692  1.62     rmind 	}
    693  1.22      manu 
    694  1.22      manu 	/* Let's ignore it */
    695  1.22      manu #ifdef DEBUG_LINUX
    696  1.22      manu 	printf("linux_sys_sched_setaffinity\n");
    697  1.22      manu #endif
    698  1.22      manu 	return 0;
    699  1.22      manu };
    700