Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.19.2.8
      1  1.19.2.8      yamt /*	$NetBSD: linux_sched.c,v 1.19.2.8 2008/03/17 09:14:36 yamt 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  * 3. All advertising materials mentioning features or use of this software
     20       1.1      tron  *    must display the following acknowledgement:
     21       1.1      tron  *	This product includes software developed by the NetBSD
     22       1.1      tron  *	Foundation, Inc. and its contributors.
     23       1.1      tron  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24       1.1      tron  *    contributors may be used to endorse or promote products derived
     25       1.1      tron  *    from this software without specific prior written permission.
     26       1.1      tron  *
     27       1.1      tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28       1.1      tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29       1.1      tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30       1.1      tron  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31       1.1      tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32       1.1      tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33       1.1      tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34       1.1      tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35       1.1      tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36       1.1      tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37       1.1      tron  * POSSIBILITY OF SUCH DAMAGE.
     38       1.1      tron  */
     39       1.1      tron 
     40       1.1      tron /*
     41       1.1      tron  * Linux compatibility module. Try to deal with scheduler related syscalls.
     42       1.1      tron  */
     43       1.8     lukem 
     44       1.8     lukem #include <sys/cdefs.h>
     45  1.19.2.8      yamt __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.19.2.8 2008/03/17 09:14:36 yamt Exp $");
     46       1.1      tron 
     47       1.1      tron #include <sys/param.h>
     48       1.1      tron #include <sys/mount.h>
     49       1.1      tron #include <sys/proc.h>
     50       1.1      tron #include <sys/systm.h>
     51  1.19.2.1      yamt #include <sys/sysctl.h>
     52  1.19.2.1      yamt #include <sys/malloc.h>
     53       1.1      tron #include <sys/syscallargs.h>
     54      1.14  jdolecek #include <sys/wait.h>
     55  1.19.2.1      yamt #include <sys/kauth.h>
     56  1.19.2.2      yamt #include <sys/ptrace.h>
     57       1.3     itohy 
     58  1.19.2.4      yamt #include <sys/cpu.h>
     59       1.1      tron 
     60       1.1      tron #include <compat/linux/common/linux_types.h>
     61       1.1      tron #include <compat/linux/common/linux_signal.h>
     62  1.19.2.1      yamt #include <compat/linux/common/linux_machdep.h> /* For LINUX_NPTL */
     63      1.19      manu #include <compat/linux/common/linux_emuldata.h>
     64  1.19.2.4      yamt #include <compat/linux/common/linux_ipc.h>
     65  1.19.2.4      yamt #include <compat/linux/common/linux_sem.h>
     66       1.1      tron 
     67       1.1      tron #include <compat/linux/linux_syscallargs.h>
     68       1.1      tron 
     69       1.1      tron #include <compat/linux/common/linux_sched.h>
     70       1.1      tron 
     71       1.1      tron int
     72  1.19.2.5      yamt linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
     73       1.1      tron {
     74  1.19.2.5      yamt 	/* {
     75       1.1      tron 		syscallarg(int) flags;
     76       1.1      tron 		syscallarg(void *) stack;
     77  1.19.2.1      yamt #ifdef LINUX_NPTL
     78      1.19      manu 		syscallarg(void *) parent_tidptr;
     79      1.19      manu 		syscallarg(void *) child_tidptr;
     80      1.19      manu #endif
     81  1.19.2.5      yamt 	} */
     82       1.1      tron 	int flags, sig;
     83      1.19      manu 	int error;
     84  1.19.2.1      yamt #ifdef LINUX_NPTL
     85      1.19      manu 	struct linux_emuldata *led;
     86      1.19      manu #endif
     87       1.1      tron 
     88       1.1      tron 	/*
     89       1.1      tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
     90       1.1      tron 	 */
     91       1.1      tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
     92       1.1      tron 		return (EINVAL);
     93       1.1      tron 
     94      1.13  jdolecek 	/*
     95      1.13  jdolecek 	 * Thread group implies shared signals. Shared signals
     96      1.13  jdolecek 	 * imply shared VM. This matches what Linux kernel does.
     97      1.13  jdolecek 	 */
     98      1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
     99      1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    100      1.13  jdolecek 		return (EINVAL);
    101      1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    102      1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    103      1.13  jdolecek 		return (EINVAL);
    104      1.13  jdolecek 
    105       1.1      tron 	flags = 0;
    106       1.1      tron 
    107       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    108       1.1      tron 		flags |= FORK_SHAREVM;
    109       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    110       1.1      tron 		flags |= FORK_SHARECWD;
    111       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    112       1.1      tron 		flags |= FORK_SHAREFILES;
    113       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    114       1.1      tron 		flags |= FORK_SHARESIGS;
    115       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    116       1.1      tron 		flags |= FORK_PPWAIT;
    117       1.1      tron 
    118  1.19.2.2      yamt 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    119  1.19.2.2      yamt 	if (sig < 0 || sig >= LINUX__NSIG)
    120  1.19.2.2      yamt 		return (EINVAL);
    121  1.19.2.2      yamt 	sig = linux_to_native_signo[sig];
    122       1.1      tron 
    123  1.19.2.1      yamt #ifdef LINUX_NPTL
    124      1.19      manu 	led = (struct linux_emuldata *)l->l_proc->p_emuldata;
    125      1.19      manu 
    126  1.19.2.2      yamt 	led->parent_tidptr = SCARG(uap, parent_tidptr);
    127  1.19.2.2      yamt 	led->child_tidptr = SCARG(uap, child_tidptr);
    128  1.19.2.2      yamt 	led->clone_flags = SCARG(uap, flags);
    129  1.19.2.1      yamt #endif /* LINUX_NPTL */
    130  1.19.2.2      yamt 
    131       1.1      tron 	/*
    132       1.1      tron 	 * Note that Linux does not provide a portable way of specifying
    133       1.1      tron 	 * the stack area; the caller must know if the stack grows up
    134       1.1      tron 	 * or down.  So, we pass a stack size of 0, so that the code
    135       1.1      tron 	 * that makes this adjustment is a noop.
    136       1.1      tron 	 */
    137      1.19      manu 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    138      1.19      manu 	    NULL, NULL, retval, NULL)) != 0)
    139      1.19      manu 		return error;
    140      1.19      manu 
    141      1.19      manu 	return 0;
    142       1.1      tron }
    143       1.1      tron 
    144  1.19.2.8      yamt /*
    145  1.19.2.8      yamt  * linux realtime priority
    146  1.19.2.8      yamt  *
    147  1.19.2.8      yamt  * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
    148  1.19.2.8      yamt  *
    149  1.19.2.8      yamt  * - SCHED_OTHER tasks don't have realtime priorities.
    150  1.19.2.8      yamt  *   in particular, sched_param::sched_priority is always 0.
    151  1.19.2.8      yamt  */
    152  1.19.2.8      yamt 
    153  1.19.2.8      yamt #define	LINUX_SCHED_RTPRIO_MIN	1
    154  1.19.2.8      yamt #define	LINUX_SCHED_RTPRIO_MAX	99
    155  1.19.2.8      yamt 
    156  1.19.2.8      yamt static int
    157  1.19.2.8      yamt sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
    158  1.19.2.8      yamt     int *native_policy, struct sched_param *native_params)
    159  1.19.2.8      yamt {
    160  1.19.2.8      yamt 
    161  1.19.2.8      yamt 	switch (linux_policy) {
    162  1.19.2.8      yamt 	case LINUX_SCHED_OTHER:
    163  1.19.2.8      yamt 		if (native_policy != NULL) {
    164  1.19.2.8      yamt 			*native_policy = SCHED_OTHER;
    165  1.19.2.8      yamt 		}
    166  1.19.2.8      yamt 		break;
    167  1.19.2.8      yamt 
    168  1.19.2.8      yamt 	case LINUX_SCHED_FIFO:
    169  1.19.2.8      yamt 		if (native_policy != NULL) {
    170  1.19.2.8      yamt 			*native_policy = SCHED_FIFO;
    171  1.19.2.8      yamt 		}
    172  1.19.2.8      yamt 		break;
    173  1.19.2.8      yamt 
    174  1.19.2.8      yamt 	case LINUX_SCHED_RR:
    175  1.19.2.8      yamt 		if (native_policy != NULL) {
    176  1.19.2.8      yamt 			*native_policy = SCHED_RR;
    177  1.19.2.8      yamt 		}
    178  1.19.2.8      yamt 		break;
    179  1.19.2.8      yamt 
    180  1.19.2.8      yamt 	default:
    181  1.19.2.8      yamt 		return EINVAL;
    182  1.19.2.8      yamt 	}
    183  1.19.2.8      yamt 
    184  1.19.2.8      yamt 	if (linux_params != NULL) {
    185  1.19.2.8      yamt 		int prio = linux_params->sched_priority;
    186  1.19.2.8      yamt 
    187  1.19.2.8      yamt 		KASSERT(native_params != NULL);
    188  1.19.2.8      yamt 
    189  1.19.2.8      yamt 		if (linux_policy == LINUX_SCHED_OTHER) {
    190  1.19.2.8      yamt 			if (prio != 0) {
    191  1.19.2.8      yamt 				return EINVAL;
    192  1.19.2.8      yamt 			}
    193  1.19.2.8      yamt 			native_params->sched_priority = PRI_NONE; /* XXX */
    194  1.19.2.8      yamt 		} else {
    195  1.19.2.8      yamt 			if (prio < LINUX_SCHED_RTPRIO_MIN ||
    196  1.19.2.8      yamt 			    prio > LINUX_SCHED_RTPRIO_MAX) {
    197  1.19.2.8      yamt 				return EINVAL;
    198  1.19.2.8      yamt 			}
    199  1.19.2.8      yamt 			native_params->sched_priority =
    200  1.19.2.8      yamt 			    (prio - LINUX_SCHED_RTPRIO_MIN)
    201  1.19.2.8      yamt 			    * (SCHED_PRI_MAX - SCHED_PRI_MIN)
    202  1.19.2.8      yamt 			    / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    203  1.19.2.8      yamt 			    + SCHED_PRI_MIN;
    204  1.19.2.8      yamt 		}
    205  1.19.2.8      yamt 	}
    206  1.19.2.8      yamt 
    207  1.19.2.8      yamt 	return 0;
    208  1.19.2.8      yamt }
    209  1.19.2.8      yamt 
    210  1.19.2.8      yamt static int
    211  1.19.2.8      yamt sched_native2linux(int native_policy, struct sched_param *native_params,
    212  1.19.2.8      yamt     int *linux_policy, struct linux_sched_param *linux_params)
    213  1.19.2.8      yamt {
    214  1.19.2.8      yamt 
    215  1.19.2.8      yamt 	switch (native_policy) {
    216  1.19.2.8      yamt 	case SCHED_OTHER:
    217  1.19.2.8      yamt 		if (linux_policy != NULL) {
    218  1.19.2.8      yamt 			*linux_policy = LINUX_SCHED_OTHER;
    219  1.19.2.8      yamt 		}
    220  1.19.2.8      yamt 		break;
    221  1.19.2.8      yamt 
    222  1.19.2.8      yamt 	case SCHED_FIFO:
    223  1.19.2.8      yamt 		if (linux_policy != NULL) {
    224  1.19.2.8      yamt 			*linux_policy = LINUX_SCHED_FIFO;
    225  1.19.2.8      yamt 		}
    226  1.19.2.8      yamt 		break;
    227  1.19.2.8      yamt 
    228  1.19.2.8      yamt 	case SCHED_RR:
    229  1.19.2.8      yamt 		if (linux_policy != NULL) {
    230  1.19.2.8      yamt 			*linux_policy = LINUX_SCHED_RR;
    231  1.19.2.8      yamt 		}
    232  1.19.2.8      yamt 		break;
    233  1.19.2.8      yamt 
    234  1.19.2.8      yamt 	default:
    235  1.19.2.8      yamt 		panic("%s: unknown policy %d\n", __func__, native_policy);
    236  1.19.2.8      yamt 	}
    237  1.19.2.8      yamt 
    238  1.19.2.8      yamt 	if (native_params != NULL) {
    239  1.19.2.8      yamt 		int prio = native_params->sched_priority;
    240  1.19.2.8      yamt 
    241  1.19.2.8      yamt 		KASSERT(prio >= SCHED_PRI_MIN);
    242  1.19.2.8      yamt 		KASSERT(prio <= SCHED_PRI_MAX);
    243  1.19.2.8      yamt 		KASSERT(linux_params != NULL);
    244  1.19.2.8      yamt 
    245  1.19.2.8      yamt 		if (native_policy == SCHED_OTHER) {
    246  1.19.2.8      yamt 			linux_params->sched_priority = 0;
    247  1.19.2.8      yamt 		} else {
    248  1.19.2.8      yamt 			linux_params->sched_priority =
    249  1.19.2.8      yamt 			    (prio - SCHED_PRI_MIN)
    250  1.19.2.8      yamt 			    * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
    251  1.19.2.8      yamt 			    / (SCHED_PRI_MAX - SCHED_PRI_MIN)
    252  1.19.2.8      yamt 			    + LINUX_SCHED_RTPRIO_MIN;
    253  1.19.2.8      yamt 		}
    254  1.19.2.8      yamt 	}
    255  1.19.2.8      yamt 
    256  1.19.2.8      yamt 	return 0;
    257  1.19.2.8      yamt }
    258  1.19.2.8      yamt 
    259       1.1      tron int
    260  1.19.2.5      yamt linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
    261       1.1      tron {
    262  1.19.2.5      yamt 	/* {
    263       1.1      tron 		syscallarg(linux_pid_t) pid;
    264       1.1      tron 		syscallarg(const struct linux_sched_param *) sp;
    265  1.19.2.5      yamt 	} */
    266  1.19.2.8      yamt 	int error, policy;
    267       1.1      tron 	struct linux_sched_param lp;
    268  1.19.2.8      yamt 	struct sched_param sp;
    269       1.1      tron 
    270  1.19.2.8      yamt 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    271  1.19.2.8      yamt 		error = EINVAL;
    272  1.19.2.8      yamt 		goto out;
    273  1.19.2.8      yamt 	}
    274       1.1      tron 
    275       1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    276       1.1      tron 	if (error)
    277  1.19.2.8      yamt 		goto out;
    278       1.1      tron 
    279  1.19.2.8      yamt 	/* We need the current policy in Linux terms. */
    280  1.19.2.8      yamt 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    281  1.19.2.8      yamt 	if (error)
    282  1.19.2.8      yamt 		goto out;
    283  1.19.2.8      yamt 	error = sched_native2linux(policy, NULL, &policy, NULL);
    284  1.19.2.8      yamt 	if (error)
    285  1.19.2.8      yamt 		goto out;
    286       1.1      tron 
    287  1.19.2.8      yamt 	error = sched_linux2native(policy, &lp, &policy, &sp);
    288  1.19.2.8      yamt 	if (error)
    289  1.19.2.8      yamt 		goto out;
    290  1.19.2.8      yamt 
    291  1.19.2.8      yamt 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    292  1.19.2.8      yamt 	if (error)
    293  1.19.2.8      yamt 		goto out;
    294  1.19.2.8      yamt 
    295  1.19.2.8      yamt  out:
    296  1.19.2.8      yamt 	return error;
    297       1.1      tron }
    298       1.1      tron 
    299       1.1      tron int
    300  1.19.2.5      yamt linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
    301       1.1      tron {
    302  1.19.2.5      yamt 	/* {
    303       1.1      tron 		syscallarg(linux_pid_t) pid;
    304       1.1      tron 		syscallarg(struct linux_sched_param *) sp;
    305  1.19.2.5      yamt 	} */
    306       1.1      tron 	struct linux_sched_param lp;
    307  1.19.2.8      yamt 	struct sched_param sp;
    308  1.19.2.8      yamt 	int error, policy;
    309       1.1      tron 
    310  1.19.2.8      yamt 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    311  1.19.2.8      yamt 		error = EINVAL;
    312  1.19.2.8      yamt 		goto out;
    313  1.19.2.8      yamt 	}
    314       1.1      tron 
    315  1.19.2.8      yamt 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, &sp);
    316  1.19.2.8      yamt 	if (error)
    317  1.19.2.8      yamt 		goto out;
    318  1.19.2.6      yamt 
    319  1.19.2.8      yamt 	error = sched_native2linux(policy, &sp, NULL, &lp);
    320  1.19.2.8      yamt 	if (error)
    321  1.19.2.8      yamt 		goto out;
    322       1.1      tron 
    323  1.19.2.8      yamt 	error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
    324  1.19.2.8      yamt 	if (error)
    325  1.19.2.8      yamt 		goto out;
    326  1.19.2.8      yamt 
    327  1.19.2.8      yamt  out:
    328  1.19.2.8      yamt 	return error;
    329       1.1      tron }
    330       1.1      tron 
    331       1.1      tron int
    332  1.19.2.5      yamt linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
    333       1.1      tron {
    334  1.19.2.5      yamt 	/* {
    335       1.1      tron 		syscallarg(linux_pid_t) pid;
    336       1.1      tron 		syscallarg(int) policy;
    337       1.1      tron 		syscallarg(cont struct linux_sched_scheduler *) sp;
    338  1.19.2.5      yamt 	} */
    339  1.19.2.8      yamt 	int error, policy;
    340       1.1      tron 	struct linux_sched_param lp;
    341  1.19.2.8      yamt 	struct sched_param sp;
    342       1.1      tron 
    343  1.19.2.8      yamt 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
    344  1.19.2.8      yamt 		error = EINVAL;
    345  1.19.2.8      yamt 		goto out;
    346  1.19.2.8      yamt 	}
    347       1.1      tron 
    348       1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    349       1.1      tron 	if (error)
    350  1.19.2.8      yamt 		goto out;
    351       1.1      tron 
    352  1.19.2.8      yamt 	error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
    353  1.19.2.8      yamt 	if (error)
    354  1.19.2.8      yamt 		goto out;
    355       1.1      tron 
    356  1.19.2.8      yamt 	error = do_sched_setparam(SCARG(uap, pid), 0, policy, &sp);
    357  1.19.2.8      yamt 	if (error)
    358  1.19.2.8      yamt 		goto out;
    359       1.1      tron 
    360  1.19.2.8      yamt  out:
    361  1.19.2.8      yamt 	return error;
    362       1.1      tron }
    363       1.1      tron 
    364       1.1      tron int
    365  1.19.2.5      yamt linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
    366       1.1      tron {
    367  1.19.2.5      yamt 	/* {
    368       1.1      tron 		syscallarg(linux_pid_t) pid;
    369  1.19.2.5      yamt 	} */
    370  1.19.2.8      yamt 	int error, policy;
    371       1.1      tron 
    372       1.1      tron 	*retval = -1;
    373       1.1      tron 
    374  1.19.2.8      yamt 	error = do_sched_getparam(SCARG(uap, pid), 0, &policy, NULL);
    375  1.19.2.8      yamt 	if (error)
    376  1.19.2.8      yamt 		goto out;
    377       1.1      tron 
    378  1.19.2.8      yamt 	error = sched_native2linux(policy, NULL, &policy, NULL);
    379  1.19.2.8      yamt 	if (error)
    380  1.19.2.8      yamt 		goto out;
    381  1.19.2.8      yamt 
    382  1.19.2.8      yamt 	*retval = policy;
    383  1.19.2.8      yamt 
    384  1.19.2.8      yamt  out:
    385  1.19.2.8      yamt 	return error;
    386       1.1      tron }
    387       1.1      tron 
    388       1.1      tron int
    389  1.19.2.5      yamt linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
    390       1.1      tron {
    391      1.11  gmcgarry 
    392      1.11  gmcgarry 	yield();
    393       1.1      tron 	return 0;
    394       1.1      tron }
    395       1.1      tron 
    396       1.1      tron int
    397  1.19.2.5      yamt linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval)
    398       1.1      tron {
    399  1.19.2.5      yamt 	/* {
    400       1.1      tron 		syscallarg(int) policy;
    401  1.19.2.5      yamt 	} */
    402       1.1      tron 
    403       1.1      tron /*
    404       1.1      tron  * We can't emulate anything put the default scheduling policy.
    405       1.1      tron  */
    406       1.1      tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    407       1.1      tron 		*retval = -1;
    408       1.1      tron 		return EINVAL;
    409       1.1      tron 	}
    410       1.1      tron 
    411       1.1      tron 	*retval = 0;
    412       1.1      tron 	return 0;
    413       1.1      tron }
    414       1.1      tron 
    415       1.1      tron int
    416  1.19.2.5      yamt linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval)
    417       1.1      tron {
    418  1.19.2.5      yamt 	/* {
    419       1.1      tron 		syscallarg(int) policy;
    420  1.19.2.5      yamt 	} */
    421       1.1      tron 
    422       1.1      tron /*
    423       1.1      tron  * We can't emulate anything put the default scheduling policy.
    424       1.1      tron  */
    425       1.1      tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    426       1.1      tron 		*retval = -1;
    427       1.1      tron 		return EINVAL;
    428       1.1      tron 	}
    429       1.1      tron 
    430       1.1      tron 	*retval = 0;
    431       1.1      tron 	return 0;
    432       1.1      tron }
    433      1.14  jdolecek 
    434      1.14  jdolecek #ifndef __m68k__
    435      1.14  jdolecek /* Present on everything but m68k */
    436      1.14  jdolecek int
    437  1.19.2.5      yamt linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
    438      1.14  jdolecek {
    439  1.19.2.2      yamt #ifdef LINUX_NPTL
    440  1.19.2.5      yamt 	/* {
    441      1.14  jdolecek 		syscallarg(int) error_code;
    442  1.19.2.5      yamt 	} */
    443  1.19.2.2      yamt 	struct proc *p = l->l_proc;
    444  1.19.2.2      yamt 	struct linux_emuldata *led = p->p_emuldata;
    445  1.19.2.2      yamt 	struct linux_emuldata *e;
    446      1.14  jdolecek 
    447  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    448  1.19.2.3      yamt 
    449  1.19.2.2      yamt #ifdef DEBUG_LINUX
    450  1.19.2.3      yamt 		printf("%s:%d, led->s->refs = %d\n", __func__, __LINE__,
    451  1.19.2.3      yamt 		    led->s->refs);
    452  1.19.2.2      yamt #endif
    453  1.19.2.3      yamt 
    454  1.19.2.3      yamt 		/*
    455  1.19.2.3      yamt 		 * The calling thread is supposed to kill all threads
    456  1.19.2.3      yamt 		 * in the same thread group (i.e. all threads created
    457  1.19.2.3      yamt 		 * via clone(2) with CLONE_THREAD flag set).
    458  1.19.2.3      yamt 		 *
    459  1.19.2.3      yamt 		 * If there is only one thread, things are quite simple
    460  1.19.2.3      yamt 		 */
    461  1.19.2.3      yamt 		if (led->s->refs == 1)
    462  1.19.2.5      yamt 			return sys_exit(l, (const void *)uap, retval);
    463  1.19.2.2      yamt 
    464  1.19.2.2      yamt #ifdef DEBUG_LINUX
    465  1.19.2.3      yamt 		printf("%s:%d\n", __func__, __LINE__);
    466  1.19.2.2      yamt #endif
    467  1.19.2.2      yamt 
    468  1.19.2.3      yamt 		led->s->flags |= LINUX_LES_INEXITGROUP;
    469  1.19.2.3      yamt 		led->s->xstat = W_EXITCODE(SCARG(uap, error_code), 0);
    470  1.19.2.2      yamt 
    471  1.19.2.3      yamt 		/*
    472  1.19.2.3      yamt 		 * Kill all threads in the group. The emulation exit hook takes
    473  1.19.2.3      yamt 		 * care of hiding the zombies and reporting the exit code
    474  1.19.2.3      yamt 		 * properly.
    475  1.19.2.3      yamt 		 */
    476  1.19.2.3      yamt 		mutex_enter(&proclist_mutex);
    477  1.19.2.3      yamt       		LIST_FOREACH(e, &led->s->threads, threads) {
    478  1.19.2.3      yamt 			if (e->proc == p)
    479  1.19.2.3      yamt 				continue;
    480      1.14  jdolecek 
    481  1.19.2.2      yamt #ifdef DEBUG_LINUX
    482  1.19.2.3      yamt 			printf("%s: kill PID %d\n", __func__, e->proc->p_pid);
    483  1.19.2.2      yamt #endif
    484  1.19.2.3      yamt 			psignal(e->proc, SIGKILL);
    485  1.19.2.3      yamt 		}
    486  1.19.2.3      yamt 
    487  1.19.2.3      yamt 		/* Now, kill ourselves */
    488  1.19.2.3      yamt 		psignal(p, SIGKILL);
    489  1.19.2.3      yamt 		mutex_exit(&proclist_mutex);
    490  1.19.2.3      yamt 
    491  1.19.2.3      yamt 		return 0;
    492  1.19.2.3      yamt 
    493  1.19.2.2      yamt 	}
    494  1.19.2.3      yamt #endif /* LINUX_NPTL */
    495  1.19.2.2      yamt 
    496  1.19.2.5      yamt 	return sys_exit(l, (const void *)uap, retval);
    497      1.14  jdolecek }
    498      1.14  jdolecek #endif /* !__m68k__ */
    499      1.19      manu 
    500  1.19.2.1      yamt #ifdef LINUX_NPTL
    501      1.19      manu int
    502  1.19.2.5      yamt linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
    503      1.19      manu {
    504  1.19.2.5      yamt 	/* {
    505      1.19      manu 		syscallarg(int *) tidptr;
    506  1.19.2.5      yamt 	} */
    507      1.19      manu 	struct linux_emuldata *led;
    508      1.19      manu 
    509      1.19      manu 	led = (struct linux_emuldata *)l->l_proc->p_emuldata;
    510      1.19      manu 	led->clear_tid = SCARG(uap, tid);
    511      1.19      manu 
    512  1.19.2.3      yamt 	led->s->flags |= LINUX_LES_USE_NPTL;
    513  1.19.2.3      yamt 
    514      1.19      manu 	*retval = l->l_proc->p_pid;
    515      1.19      manu 
    516      1.19      manu 	return 0;
    517      1.19      manu }
    518  1.19.2.1      yamt 
    519  1.19.2.1      yamt /* ARGUSED1 */
    520  1.19.2.1      yamt int
    521  1.19.2.5      yamt linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
    522  1.19.2.1      yamt {
    523  1.19.2.2      yamt 	/* The Linux kernel does it exactly that way */
    524  1.19.2.1      yamt 	*retval = l->l_proc->p_pid;
    525  1.19.2.1      yamt 	return 0;
    526  1.19.2.1      yamt }
    527  1.19.2.1      yamt 
    528  1.19.2.2      yamt #ifdef LINUX_NPTL
    529  1.19.2.2      yamt /* ARGUSED1 */
    530  1.19.2.2      yamt int
    531  1.19.2.5      yamt linux_sys_getpid(struct lwp *l, const void *v, register_t *retval)
    532  1.19.2.2      yamt {
    533  1.19.2.3      yamt 	struct linux_emuldata *led = l->l_proc->p_emuldata;
    534  1.19.2.2      yamt 
    535  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    536  1.19.2.3      yamt 		/* The Linux kernel does it exactly that way */
    537  1.19.2.3      yamt 		*retval = led->s->group_pid;
    538  1.19.2.3      yamt 	} else {
    539  1.19.2.3      yamt 		*retval = l->l_proc->p_pid;
    540  1.19.2.3      yamt 	}
    541  1.19.2.2      yamt 
    542  1.19.2.2      yamt 	return 0;
    543  1.19.2.2      yamt }
    544  1.19.2.2      yamt 
    545  1.19.2.2      yamt /* ARGUSED1 */
    546  1.19.2.2      yamt int
    547  1.19.2.5      yamt linux_sys_getppid(struct lwp *l, const void *v, register_t *retval)
    548  1.19.2.2      yamt {
    549  1.19.2.2      yamt 	struct proc *p = l->l_proc;
    550  1.19.2.2      yamt 	struct linux_emuldata *led = p->p_emuldata;
    551  1.19.2.2      yamt 	struct proc *glp;
    552  1.19.2.2      yamt 	struct proc *pp;
    553  1.19.2.2      yamt 
    554  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    555  1.19.2.2      yamt 
    556  1.19.2.3      yamt 		/* Find the thread group leader's parent */
    557  1.19.2.3      yamt 		if ((glp = pfind(led->s->group_pid)) == NULL) {
    558  1.19.2.3      yamt 			/* Maybe panic... */
    559  1.19.2.3      yamt 			printf("linux_sys_getppid: missing group leader PID"
    560  1.19.2.3      yamt 			    " %d\n", led->s->group_pid);
    561  1.19.2.3      yamt 			return -1;
    562  1.19.2.3      yamt 		}
    563  1.19.2.3      yamt 		pp = glp->p_pptr;
    564  1.19.2.3      yamt 
    565  1.19.2.3      yamt 		/* If this is a Linux process too, return thread group PID */
    566  1.19.2.3      yamt 		if (pp->p_emul == p->p_emul) {
    567  1.19.2.3      yamt 			struct linux_emuldata *pled;
    568  1.19.2.3      yamt 
    569  1.19.2.3      yamt 			pled = pp->p_emuldata;
    570  1.19.2.3      yamt 			*retval = pled->s->group_pid;
    571  1.19.2.3      yamt 		} else {
    572  1.19.2.3      yamt 			*retval = pp->p_pid;
    573  1.19.2.3      yamt 		}
    574  1.19.2.2      yamt 
    575  1.19.2.2      yamt 	} else {
    576  1.19.2.3      yamt 		*retval = p->p_pptr->p_pid;
    577  1.19.2.2      yamt 	}
    578  1.19.2.2      yamt 
    579  1.19.2.2      yamt 	return 0;
    580  1.19.2.2      yamt }
    581  1.19.2.2      yamt #endif /* LINUX_NPTL */
    582  1.19.2.2      yamt 
    583  1.19.2.1      yamt int
    584  1.19.2.5      yamt linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
    585  1.19.2.1      yamt {
    586  1.19.2.5      yamt 	/* {
    587  1.19.2.1      yamt 		syscallarg(pid_t) pid;
    588  1.19.2.1      yamt 		syscallarg(unsigned int) len;
    589  1.19.2.1      yamt 		syscallarg(unsigned long *) mask;
    590  1.19.2.5      yamt 	} */
    591  1.19.2.1      yamt 	int error;
    592  1.19.2.1      yamt 	int ret;
    593  1.19.2.1      yamt 	char *data;
    594  1.19.2.1      yamt 	int *retp;
    595  1.19.2.1      yamt 
    596  1.19.2.1      yamt 	if (SCARG(uap, mask) == NULL)
    597  1.19.2.1      yamt 		return EINVAL;
    598  1.19.2.1      yamt 
    599  1.19.2.1      yamt 	if (SCARG(uap, len) < sizeof(int))
    600  1.19.2.1      yamt 		return EINVAL;
    601  1.19.2.1      yamt 
    602  1.19.2.1      yamt 	if (pfind(SCARG(uap, pid)) == NULL)
    603  1.19.2.1      yamt 		return ESRCH;
    604  1.19.2.1      yamt 
    605  1.19.2.1      yamt 	/*
    606  1.19.2.1      yamt 	 * return the actual number of CPU, tag all of them as available
    607  1.19.2.1      yamt 	 * The result is a mask, the first CPU being in the least significant
    608  1.19.2.1      yamt 	 * bit.
    609  1.19.2.1      yamt 	 */
    610  1.19.2.1      yamt 	ret = (1 << ncpu) - 1;
    611  1.19.2.1      yamt 	data = malloc(SCARG(uap, len), M_TEMP, M_WAITOK|M_ZERO);
    612  1.19.2.1      yamt 	retp = (int *)&data[SCARG(uap, len) - sizeof(ret)];
    613  1.19.2.1      yamt 	*retp = ret;
    614  1.19.2.1      yamt 
    615  1.19.2.1      yamt 	if ((error = copyout(data, SCARG(uap, mask), SCARG(uap, len))) != 0)
    616  1.19.2.1      yamt 		return error;
    617  1.19.2.1      yamt 
    618  1.19.2.1      yamt 	free(data, M_TEMP);
    619  1.19.2.1      yamt 
    620  1.19.2.1      yamt 	return 0;
    621  1.19.2.1      yamt 
    622  1.19.2.1      yamt }
    623  1.19.2.1      yamt 
    624  1.19.2.1      yamt int
    625  1.19.2.5      yamt linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
    626  1.19.2.1      yamt {
    627  1.19.2.5      yamt 	/* {
    628  1.19.2.1      yamt 		syscallarg(pid_t) pid;
    629  1.19.2.1      yamt 		syscallarg(unsigned int) len;
    630  1.19.2.1      yamt 		syscallarg(unsigned long *) mask;
    631  1.19.2.5      yamt 	} */
    632  1.19.2.1      yamt 
    633  1.19.2.1      yamt 	if (pfind(SCARG(uap, pid)) == NULL)
    634  1.19.2.1      yamt 		return ESRCH;
    635  1.19.2.1      yamt 
    636  1.19.2.1      yamt 	/* Let's ignore it */
    637  1.19.2.1      yamt #ifdef DEBUG_LINUX
    638  1.19.2.1      yamt 	printf("linux_sys_sched_setaffinity\n");
    639  1.19.2.1      yamt #endif
    640  1.19.2.1      yamt 	return 0;
    641  1.19.2.1      yamt };
    642  1.19.2.1      yamt #endif /* LINUX_NPTL */
    643