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