Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.65.12.1
      1  1.65.12.1       tls /*	$NetBSD: linux_sched.c,v 1.65.12.1 2014/08/20 00:03:32 tls 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.65.12.1       tls __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.65.12.1 2014/08/20 00:03:32 tls 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.65  christos static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *,
     67       1.65  christos     register_t *);
     68       1.65  christos 
     69       1.65  christos #if DEBUG_LINUX
     70       1.65  christos #define DPRINTF(x) uprintf x
     71       1.65  christos #else
     72       1.65  christos #define DPRINTF(x)
     73       1.65  christos #endif
     74       1.63       chs 
     75       1.63       chs static void
     76       1.63       chs linux_child_return(void *arg)
     77       1.63       chs {
     78       1.63       chs 	struct lwp *l = arg;
     79       1.63       chs 	struct proc *p = l->l_proc;
     80       1.63       chs 	struct linux_emuldata *led = l->l_emuldata;
     81       1.63       chs 	void *ctp = led->led_child_tidptr;
     82       1.65  christos 	int error;
     83       1.63       chs 
     84       1.63       chs 	if (ctp) {
     85       1.65  christos 		if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0)
     86       1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
     87       1.65  christos 			    "failed (child_tidptr = %p, tid = %d error =%d)\n",
     88       1.65  christos 			    __func__, ctp, p->p_pid, error);
     89       1.63       chs 	}
     90       1.63       chs 	child_return(arg);
     91       1.63       chs }
     92       1.63       chs 
     93        1.1      tron int
     94       1.65  christos linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap,
     95       1.65  christos     register_t *retval)
     96        1.1      tron {
     97       1.46       dsl 	/* {
     98        1.1      tron 		syscallarg(int) flags;
     99        1.1      tron 		syscallarg(void *) stack;
    100       1.19      manu 		syscallarg(void *) parent_tidptr;
    101       1.63       chs 		syscallarg(void *) tls;
    102       1.19      manu 		syscallarg(void *) child_tidptr;
    103       1.46       dsl 	} */
    104       1.58  christos 	struct proc *p;
    105       1.19      manu 	struct linux_emuldata *led;
    106       1.63       chs 	int flags, sig, error;
    107        1.1      tron 
    108        1.1      tron 	/*
    109        1.1      tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
    110        1.1      tron 	 */
    111        1.1      tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
    112       1.65  christos 		return EINVAL;
    113        1.1      tron 
    114       1.13  jdolecek 	/*
    115       1.13  jdolecek 	 * Thread group implies shared signals. Shared signals
    116       1.13  jdolecek 	 * imply shared VM. This matches what Linux kernel does.
    117       1.13  jdolecek 	 */
    118       1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
    119       1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    120       1.65  christos 		return EINVAL;
    121       1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    122       1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    123       1.65  christos 		return EINVAL;
    124       1.13  jdolecek 
    125       1.63       chs 	/*
    126       1.63       chs 	 * The thread group flavor is implemented totally differently.
    127       1.63       chs 	 */
    128       1.65  christos 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD)
    129       1.63       chs 		return linux_clone_nptl(l, uap, retval);
    130       1.63       chs 
    131        1.1      tron 	flags = 0;
    132        1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    133        1.1      tron 		flags |= FORK_SHAREVM;
    134        1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    135        1.1      tron 		flags |= FORK_SHARECWD;
    136        1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    137        1.1      tron 		flags |= FORK_SHAREFILES;
    138        1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    139        1.1      tron 		flags |= FORK_SHARESIGS;
    140        1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    141        1.1      tron 		flags |= FORK_PPWAIT;
    142        1.1      tron 
    143       1.34      manu 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    144       1.34      manu 	if (sig < 0 || sig >= LINUX__NSIG)
    145       1.65  christos 		return EINVAL;
    146       1.34      manu 	sig = linux_to_native_signo[sig];
    147        1.1      tron 
    148       1.63       chs 	if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
    149       1.63       chs 		led = l->l_emuldata;
    150       1.63       chs 		led->led_child_tidptr = SCARG(uap, child_tidptr);
    151       1.63       chs 	}
    152       1.19      manu 
    153        1.1      tron 	/*
    154        1.1      tron 	 * Note that Linux does not provide a portable way of specifying
    155        1.1      tron 	 * the stack area; the caller must know if the stack grows up
    156        1.1      tron 	 * or down.  So, we pass a stack size of 0, so that the code
    157        1.1      tron 	 * that makes this adjustment is a noop.
    158        1.1      tron 	 */
    159       1.19      manu 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    160       1.65  christos 	    linux_child_return, NULL, retval, &p)) != 0) {
    161       1.65  christos 		DPRINTF(("%s: fork1: error %d\n", __func__, error));
    162       1.63       chs 		return error;
    163       1.65  christos 	}
    164       1.63       chs 
    165       1.63       chs 	return 0;
    166       1.63       chs }
    167       1.63       chs 
    168       1.63       chs static int
    169       1.63       chs linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
    170       1.63       chs {
    171       1.63       chs 	/* {
    172       1.63       chs 		syscallarg(int) flags;
    173       1.63       chs 		syscallarg(void *) stack;
    174       1.63       chs 		syscallarg(void *) parent_tidptr;
    175       1.63       chs 		syscallarg(void *) tls;
    176       1.63       chs 		syscallarg(void *) child_tidptr;
    177       1.63       chs 	} */
    178       1.63       chs 	struct proc *p;
    179       1.63       chs 	struct lwp *l2;
    180       1.63       chs 	struct linux_emuldata *led;
    181       1.63       chs 	void *parent_tidptr, *tls, *child_tidptr;
    182       1.63       chs 	struct schedstate_percpu *spc;
    183       1.63       chs 	vaddr_t uaddr;
    184       1.63       chs 	lwpid_t lid;
    185       1.63       chs 	int flags, tnprocs, 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 	tnprocs = atomic_inc_uint_nv(&nprocs);
    194       1.63       chs 	if (__predict_false(tnprocs >= maxproc) ||
    195       1.63       chs 	    kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p,
    196       1.65  christos 	    KAUTH_ARG(tnprocs), NULL, NULL) != 0) {
    197       1.63       chs 		atomic_dec_uint(&nprocs);
    198       1.63       chs 		return EAGAIN;
    199       1.63       chs 	}
    200       1.63       chs 
    201       1.63       chs 	uaddr = uvm_uarea_alloc();
    202       1.63       chs 	if (__predict_false(uaddr == 0)) {
    203       1.63       chs 		atomic_dec_uint(&nprocs);
    204       1.63       chs 		return ENOMEM;
    205       1.63       chs 	}
    206       1.63       chs 
    207       1.63       chs 	error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
    208       1.65  christos 	    SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class);
    209       1.63       chs 	if (__predict_false(error)) {
    210       1.65  christos 		DPRINTF(("%s: lwp_create error=%d\n", __func__, error));
    211       1.63       chs 		atomic_dec_uint(&nprocs);
    212       1.63       chs 		uvm_uarea_free(uaddr);
    213       1.19      manu 		return error;
    214       1.63       chs 	}
    215       1.63       chs 	lid = l2->l_lid;
    216       1.19      manu 
    217       1.63       chs 	/* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
    218       1.63       chs 	if (flags & LINUX_CLONE_CHILD_CLEARTID) {
    219       1.63       chs 		led = l2->l_emuldata;
    220       1.63       chs 		led->led_clear_tid = child_tidptr;
    221       1.63       chs 	}
    222       1.63       chs 
    223       1.63       chs 	/* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
    224       1.63       chs 	if (flags & LINUX_CLONE_PARENT_SETTID) {
    225       1.65  christos 		if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0)
    226       1.63       chs 			printf("%s: LINUX_CLONE_PARENT_SETTID "
    227       1.65  christos 			    "failed (parent_tidptr = %p tid = %d error=%d)\n",
    228       1.65  christos 			    __func__, parent_tidptr, lid, error);
    229       1.63       chs 	}
    230       1.63       chs 
    231       1.63       chs 	/* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory  */
    232       1.63       chs 	if (flags & LINUX_CLONE_CHILD_SETTID) {
    233       1.65  christos 		if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0)
    234       1.63       chs 			printf("%s: LINUX_CLONE_CHILD_SETTID "
    235       1.65  christos 			    "failed (child_tidptr = %p, tid = %d error=%d)\n",
    236       1.65  christos 			    __func__, child_tidptr, lid, error);
    237       1.63       chs 	}
    238       1.63       chs 
    239       1.63       chs 	if (flags & LINUX_CLONE_SETTLS) {
    240       1.63       chs 		error = LINUX_LWP_SETPRIVATE(l2, tls);
    241       1.63       chs 		if (error) {
    242       1.65  christos 			DPRINTF(("%s: LINUX_LWP_SETPRIVATE %d\n", __func__,
    243       1.65  christos 			    error));
    244       1.63       chs 			lwp_exit(l2);
    245       1.63       chs 			return error;
    246       1.63       chs 		}
    247       1.63       chs 	}
    248       1.63       chs 
    249       1.63       chs 	/*
    250       1.63       chs 	 * Set the new LWP running, unless the process is stopping,
    251       1.63       chs 	 * then the LWP is created stopped.
    252       1.63       chs 	 */
    253       1.63       chs 	mutex_enter(p->p_lock);
    254       1.63       chs 	lwp_lock(l2);
    255       1.63       chs 	spc = &l2->l_cpu->ci_schedstate;
    256       1.63       chs 	if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    257       1.63       chs 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
    258       1.63       chs 			KASSERT(l2->l_wchan == NULL);
    259       1.63       chs 	    		l2->l_stat = LSSTOP;
    260       1.63       chs 			p->p_nrlwps--;
    261       1.63       chs 			lwp_unlock_to(l2, spc->spc_lwplock);
    262       1.63       chs 		} else {
    263       1.63       chs 			KASSERT(lwp_locked(l2, spc->spc_mutex));
    264       1.63       chs 			l2->l_stat = LSRUN;
    265       1.63       chs 			sched_enqueue(l2, false);
    266       1.63       chs 			lwp_unlock(l2);
    267       1.63       chs 		}
    268       1.63       chs 	} else {
    269       1.63       chs 		l2->l_stat = LSSUSPENDED;
    270       1.63       chs 		p->p_nrlwps--;
    271       1.63       chs 		lwp_unlock_to(l2, spc->spc_lwplock);
    272       1.63       chs 	}
    273       1.63       chs 	mutex_exit(p->p_lock);
    274       1.58  christos 
    275       1.63       chs 	retval[0] = lid;
    276       1.63       chs 	retval[1] = 0;
    277       1.19      manu 	return 0;
    278        1.1      tron }
    279        1.1      tron 
    280       1.49      elad /*
    281       1.49      elad  * linux realtime priority
    282       1.49      elad  *
    283       1.49      elad  * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
    284       1.49      elad  *
    285       1.49      elad  * - SCHED_OTHER tasks don't have realtime priorities.
    286       1.49      elad  *   in particular, sched_param::sched_priority is always 0.
    287       1.49      elad  */
    288       1.49      elad 
    289       1.49      elad #define	LINUX_SCHED_RTPRIO_MIN	1
    290       1.49      elad #define	LINUX_SCHED_RTPRIO_MAX	99
    291       1.49      elad 
    292       1.49      elad static int
    293       1.49      elad sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
    294       1.49      elad     int *native_policy, struct sched_param *native_params)
    295       1.49      elad {
    296       1.49      elad 
    297       1.49      elad 	switch (linux_policy) {
    298       1.49      elad 	case LINUX_SCHED_OTHER:
    299       1.49      elad 		if (native_policy != NULL) {
    300       1.49      elad 			*native_policy = SCHED_OTHER;
    301       1.49      elad 		}
    302       1.49      elad 		break;
    303       1.49      elad 
    304       1.49      elad 	case LINUX_SCHED_FIFO:
    305       1.49      elad 		if (native_policy != NULL) {
    306       1.49      elad 			*native_policy = SCHED_FIFO;
    307       1.49      elad 		}
    308       1.49      elad 		break;
    309       1.49      elad 
    310       1.49      elad 	case LINUX_SCHED_RR:
    311       1.49      elad 		if (native_policy != NULL) {
    312       1.49      elad 			*native_policy = SCHED_RR;
    313       1.49      elad 		}
    314       1.49      elad 		break;
    315       1.49      elad 
    316       1.49      elad 	default:
    317       1.49      elad 		return EINVAL;
    318       1.49      elad 	}
    319       1.49      elad 
    320       1.49      elad 	if (linux_params != NULL) {
    321       1.49      elad 		int prio = linux_params->sched_priority;
    322       1.49      elad 
    323       1.49      elad 		KASSERT(native_params != NULL);
    324       1.49      elad 
    325       1.49      elad 		if (linux_policy == LINUX_SCHED_OTHER) {
    326       1.49      elad 			if (prio != 0) {
    327       1.49      elad 				return EINVAL;
    328       1.49      elad 			}
    329       1.49      elad 			native_params->sched_priority = PRI_NONE; /* XXX */
    330       1.49      elad 		} else {
    331       1.49      elad 			if (prio < LINUX_SCHED_RTPRIO_MIN ||
    332       1.49      elad 			    prio > LINUX_SCHED_RTPRIO_MAX) {
    333       1.49      elad 				return EINVAL;
    334       1.49      elad 			}
    335       1.49      elad 			native_params->sched_priority =
    336       1.49      elad 			    (prio - LINUX_SCHED_RTPRIO_MIN)
    337       1.49      elad 			    * (SCHED_PRI_MAX - SCHED_PRI_MIN)
    338       1.49      elad 			    / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    339       1.49      elad 			    + SCHED_PRI_MIN;
    340       1.49      elad 		}
    341       1.49      elad 	}
    342       1.49      elad 
    343       1.49      elad 	return 0;
    344       1.49      elad }
    345       1.49      elad 
    346       1.49      elad static int
    347       1.49      elad sched_native2linux(int native_policy, struct sched_param *native_params,
    348       1.49      elad     int *linux_policy, struct linux_sched_param *linux_params)
    349       1.49      elad {
    350       1.49      elad 
    351       1.49      elad 	switch (native_policy) {
    352       1.49      elad 	case SCHED_OTHER:
    353       1.49      elad 		if (linux_policy != NULL) {
    354       1.49      elad 			*linux_policy = LINUX_SCHED_OTHER;
    355       1.49      elad 		}
    356       1.49      elad 		break;
    357       1.49      elad 
    358       1.49      elad 	case SCHED_FIFO:
    359       1.49      elad 		if (linux_policy != NULL) {
    360       1.49      elad 			*linux_policy = LINUX_SCHED_FIFO;
    361       1.49      elad 		}
    362       1.49      elad 		break;
    363       1.49      elad 
    364       1.49      elad 	case SCHED_RR:
    365       1.49      elad 		if (linux_policy != NULL) {
    366       1.49      elad 			*linux_policy = LINUX_SCHED_RR;
    367       1.49      elad 		}
    368       1.49      elad 		break;
    369       1.49      elad 
    370       1.49      elad 	default:
    371       1.49      elad 		panic("%s: unknown policy %d\n", __func__, native_policy);
    372       1.49      elad 	}
    373       1.49      elad 
    374       1.49      elad 	if (native_params != NULL) {
    375       1.49      elad 		int prio = native_params->sched_priority;
    376       1.49      elad 
    377       1.49      elad 		KASSERT(prio >= SCHED_PRI_MIN);
    378       1.49      elad 		KASSERT(prio <= SCHED_PRI_MAX);
    379       1.49      elad 		KASSERT(linux_params != NULL);
    380       1.56  jmcneill 
    381       1.65  christos 		DPRINTF(("%s: native: policy %d, priority %d\n",
    382       1.65  christos 		    __func__, native_policy, prio));
    383       1.49      elad 
    384       1.49      elad 		if (native_policy == SCHED_OTHER) {
    385       1.49      elad 			linux_params->sched_priority = 0;
    386       1.49      elad 		} else {
    387       1.49      elad 			linux_params->sched_priority =
    388       1.49      elad 			    (prio - SCHED_PRI_MIN)
    389       1.49      elad 			    * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    390       1.49      elad 			    / (SCHED_PRI_MAX - SCHED_PRI_MIN)
    391       1.49      elad 			    + LINUX_SCHED_RTPRIO_MIN;
    392       1.49      elad 		}
    393       1.65  christos 		DPRINTF(("%s: linux: policy %d, priority %d\n",
    394       1.65  christos 		    __func__, -1, linux_params->sched_priority));
    395       1.49      elad 	}
    396       1.49      elad 
    397       1.49      elad 	return 0;
    398       1.49      elad }
    399       1.49      elad 
    400        1.1      tron int
    401       1.46       dsl linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
    402        1.1      tron {
    403       1.46       dsl 	/* {
    404        1.1      tron 		syscallarg(linux_pid_t) pid;
    405        1.1      tron 		syscallarg(const struct linux_sched_param *) sp;
    406       1.46       dsl 	} */
    407       1.49      elad 	int error, policy;
    408        1.1      tron 	struct linux_sched_param lp;
    409       1.49      elad 	struct sched_param sp;
    410       1.49      elad 
    411       1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    412       1.49      elad 		error = EINVAL;
    413       1.49      elad 		goto out;
    414       1.49      elad 	}
    415        1.1      tron 
    416       1.49      elad 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    417       1.49      elad 	if (error)
    418       1.49      elad 		goto out;
    419        1.1      tron 
    420       1.49      elad 	/* We need the current policy in Linux terms. */
    421  1.65.12.1       tls 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    422       1.49      elad 	if (error)
    423       1.49      elad 		goto out;
    424       1.49      elad 	error = sched_native2linux(policy, NULL, &policy, NULL);
    425       1.49      elad 	if (error)
    426       1.49      elad 		goto out;
    427        1.1      tron 
    428       1.49      elad 	error = sched_linux2native(policy, &lp, &policy, &sp);
    429        1.1      tron 	if (error)
    430       1.49      elad 		goto out;
    431        1.1      tron 
    432  1.65.12.1       tls 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    433       1.49      elad 	if (error)
    434       1.49      elad 		goto out;
    435        1.1      tron 
    436       1.49      elad  out:
    437       1.49      elad 	return error;
    438        1.1      tron }
    439        1.1      tron 
    440        1.1      tron int
    441       1.46       dsl linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
    442        1.1      tron {
    443       1.46       dsl 	/* {
    444        1.1      tron 		syscallarg(linux_pid_t) pid;
    445        1.1      tron 		syscallarg(struct linux_sched_param *) sp;
    446       1.46       dsl 	} */
    447        1.1      tron 	struct linux_sched_param lp;
    448       1.49      elad 	struct sched_param sp;
    449       1.50      elad 	int error, policy;
    450       1.49      elad 
    451       1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    452       1.49      elad 		error = EINVAL;
    453       1.49      elad 		goto out;
    454       1.49      elad 	}
    455        1.1      tron 
    456  1.65.12.1       tls 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
    457       1.49      elad 	if (error)
    458       1.49      elad 		goto out;
    459       1.65  christos 	DPRINTF(("%s: native: policy %d, priority %d\n",
    460       1.65  christos 	    __func__, policy, sp.sched_priority));
    461        1.1      tron 
    462       1.50      elad 	error = sched_native2linux(policy, &sp, NULL, &lp);
    463       1.49      elad 	if (error)
    464       1.49      elad 		goto out;
    465       1.65  christos 	DPRINTF(("%s: linux: policy %d, priority %d\n",
    466       1.65  christos 	    __func__, policy, lp.sched_priority));
    467       1.47      elad 
    468       1.49      elad 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    469       1.49      elad 	if (error)
    470       1.49      elad 		goto out;
    471        1.1      tron 
    472       1.49      elad  out:
    473       1.49      elad 	return error;
    474        1.1      tron }
    475        1.1      tron 
    476        1.1      tron int
    477       1.46       dsl linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
    478        1.1      tron {
    479       1.46       dsl 	/* {
    480        1.1      tron 		syscallarg(linux_pid_t) pid;
    481        1.1      tron 		syscallarg(int) policy;
    482       1.61     njoly 		syscallarg(cont struct linux_sched_param *) sp;
    483       1.46       dsl 	} */
    484       1.49      elad 	int error, policy;
    485        1.1      tron 	struct linux_sched_param lp;
    486       1.49      elad 	struct sched_param sp;
    487        1.1      tron 
    488       1.49      elad 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    489       1.49      elad 		error = EINVAL;
    490       1.49      elad 		goto out;
    491       1.49      elad 	}
    492        1.1      tron 
    493        1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    494        1.1      tron 	if (error)
    495       1.49      elad 		goto out;
    496       1.65  christos 	DPRINTF(("%s: linux: policy %d, priority %d\n",
    497       1.65  christos 	    __func__, SCARG(uap, policy), lp.sched_priority));
    498        1.1      tron 
    499       1.49      elad 	error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
    500       1.49      elad 	if (error)
    501       1.49      elad 		goto out;
    502       1.65  christos 	DPRINTF(("%s: native: policy %d, priority %d\n",
    503       1.65  christos 	    __func__, policy, sp.sched_priority));
    504        1.1      tron 
    505  1.65.12.1       tls 	error = do_sched_setparam(SCARG(uap, pid), 0, 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.65.12.1       tls 	error = do_sched_getparam(SCARG(uap, pid), 0, &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.65  christos 	DPRINTF(("%s\n", __func__));
    696       1.22      manu 	return 0;
    697       1.64       dsl }
    698