Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.19.2.4
      1  1.19.2.4      yamt /*	$NetBSD: linux_sched.c,v 1.19.2.4 2007/10/27 11:29:39 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.4      yamt __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.19.2.4 2007/10/27 11:29:39 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.12   thorpej linux_sys_clone(l, v, retval)
     73      1.12   thorpej 	struct lwp *l;
     74       1.1      tron 	void *v;
     75       1.1      tron 	register_t *retval;
     76       1.1      tron {
     77       1.1      tron 	struct linux_sys_clone_args /* {
     78       1.1      tron 		syscallarg(int) flags;
     79       1.1      tron 		syscallarg(void *) stack;
     80  1.19.2.1      yamt #ifdef LINUX_NPTL
     81      1.19      manu 		syscallarg(void *) parent_tidptr;
     82      1.19      manu 		syscallarg(void *) child_tidptr;
     83      1.19      manu #endif
     84       1.1      tron 	} */ *uap = v;
     85       1.1      tron 	int flags, sig;
     86      1.19      manu 	int error;
     87  1.19.2.1      yamt #ifdef LINUX_NPTL
     88      1.19      manu 	struct linux_emuldata *led;
     89      1.19      manu #endif
     90       1.1      tron 
     91       1.1      tron 	/*
     92       1.1      tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
     93       1.1      tron 	 */
     94       1.1      tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
     95       1.1      tron 		return (EINVAL);
     96       1.1      tron 
     97      1.13  jdolecek 	/*
     98      1.13  jdolecek 	 * Thread group implies shared signals. Shared signals
     99      1.13  jdolecek 	 * imply shared VM. This matches what Linux kernel does.
    100      1.13  jdolecek 	 */
    101      1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_THREAD
    102      1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
    103      1.13  jdolecek 		return (EINVAL);
    104      1.13  jdolecek 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
    105      1.13  jdolecek 	    && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
    106      1.13  jdolecek 		return (EINVAL);
    107      1.13  jdolecek 
    108       1.1      tron 	flags = 0;
    109       1.1      tron 
    110       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
    111       1.1      tron 		flags |= FORK_SHAREVM;
    112       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
    113       1.1      tron 		flags |= FORK_SHARECWD;
    114       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
    115       1.1      tron 		flags |= FORK_SHAREFILES;
    116       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
    117       1.1      tron 		flags |= FORK_SHARESIGS;
    118       1.1      tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
    119       1.1      tron 		flags |= FORK_PPWAIT;
    120       1.1      tron 
    121  1.19.2.2      yamt 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
    122  1.19.2.2      yamt 	if (sig < 0 || sig >= LINUX__NSIG)
    123  1.19.2.2      yamt 		return (EINVAL);
    124  1.19.2.2      yamt 	sig = linux_to_native_signo[sig];
    125       1.1      tron 
    126  1.19.2.1      yamt #ifdef LINUX_NPTL
    127      1.19      manu 	led = (struct linux_emuldata *)l->l_proc->p_emuldata;
    128      1.19      manu 
    129  1.19.2.2      yamt 	led->parent_tidptr = SCARG(uap, parent_tidptr);
    130  1.19.2.2      yamt 	led->child_tidptr = SCARG(uap, child_tidptr);
    131  1.19.2.2      yamt 	led->clone_flags = SCARG(uap, flags);
    132  1.19.2.1      yamt #endif /* LINUX_NPTL */
    133  1.19.2.2      yamt 
    134       1.1      tron 	/*
    135       1.1      tron 	 * Note that Linux does not provide a portable way of specifying
    136       1.1      tron 	 * the stack area; the caller must know if the stack grows up
    137       1.1      tron 	 * or down.  So, we pass a stack size of 0, so that the code
    138       1.1      tron 	 * that makes this adjustment is a noop.
    139       1.1      tron 	 */
    140      1.19      manu 	if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
    141      1.19      manu 	    NULL, NULL, retval, NULL)) != 0)
    142      1.19      manu 		return error;
    143      1.19      manu 
    144      1.19      manu 	return 0;
    145       1.1      tron }
    146       1.1      tron 
    147       1.1      tron int
    148  1.19.2.2      yamt linux_sys_sched_setparam(struct lwp *cl, void *v, register_t *retval)
    149       1.1      tron {
    150       1.1      tron 	struct linux_sys_sched_setparam_args /* {
    151       1.1      tron 		syscallarg(linux_pid_t) pid;
    152       1.1      tron 		syscallarg(const struct linux_sched_param *) sp;
    153       1.1      tron 	} */ *uap = v;
    154       1.1      tron 	int error;
    155       1.1      tron 	struct linux_sched_param lp;
    156       1.5  augustss 	struct proc *p;
    157       1.1      tron 
    158       1.1      tron /*
    159       1.1      tron  * We only check for valid parameters and return afterwards.
    160       1.1      tron  */
    161       1.1      tron 
    162       1.1      tron 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    163       1.1      tron 		return EINVAL;
    164       1.1      tron 
    165       1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    166       1.1      tron 	if (error)
    167       1.1      tron 		return error;
    168       1.1      tron 
    169       1.1      tron 	if (SCARG(uap, pid) != 0) {
    170  1.19.2.2      yamt 		kauth_cred_t pc = cl->l_cred;
    171       1.1      tron 
    172       1.1      tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    173       1.1      tron 			return ESRCH;
    174  1.19.2.2      yamt 		if (!(cl->l_proc == p ||
    175  1.19.2.3      yamt 		      kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
    176  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
    177  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
    178  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
    179  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
    180       1.1      tron 			return EPERM;
    181       1.1      tron 	}
    182       1.1      tron 
    183       1.1      tron 	return 0;
    184       1.1      tron }
    185       1.1      tron 
    186       1.1      tron int
    187  1.19.2.2      yamt linux_sys_sched_getparam(struct lwp *cl, void *v, register_t *retval)
    188       1.1      tron {
    189       1.1      tron 	struct linux_sys_sched_getparam_args /* {
    190       1.1      tron 		syscallarg(linux_pid_t) pid;
    191       1.1      tron 		syscallarg(struct linux_sched_param *) sp;
    192       1.1      tron 	} */ *uap = v;
    193       1.5  augustss 	struct proc *p;
    194       1.1      tron 	struct linux_sched_param lp;
    195       1.1      tron 
    196       1.1      tron /*
    197       1.1      tron  * We only check for valid parameters and return a dummy priority afterwards.
    198       1.1      tron  */
    199       1.1      tron 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    200       1.1      tron 		return EINVAL;
    201       1.1      tron 
    202       1.1      tron 	if (SCARG(uap, pid) != 0) {
    203  1.19.2.2      yamt 		kauth_cred_t pc = cl->l_cred;
    204       1.1      tron 
    205       1.1      tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    206       1.1      tron 			return ESRCH;
    207  1.19.2.2      yamt 		if (!(cl->l_proc == p ||
    208  1.19.2.3      yamt 		      kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
    209  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
    210  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
    211  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
    212  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
    213       1.1      tron 			return EPERM;
    214       1.1      tron 	}
    215       1.1      tron 
    216       1.1      tron 	lp.sched_priority = 0;
    217       1.1      tron 	return copyout(&lp, SCARG(uap, sp), sizeof(lp));
    218       1.1      tron }
    219       1.1      tron 
    220       1.1      tron int
    221  1.19.2.2      yamt linux_sys_sched_setscheduler(struct lwp *cl, void *v,
    222  1.19.2.2      yamt     register_t *retval)
    223       1.1      tron {
    224       1.1      tron 	struct linux_sys_sched_setscheduler_args /* {
    225       1.1      tron 		syscallarg(linux_pid_t) pid;
    226       1.1      tron 		syscallarg(int) policy;
    227       1.1      tron 		syscallarg(cont struct linux_sched_scheduler *) sp;
    228       1.1      tron 	} */ *uap = v;
    229       1.1      tron 	int error;
    230       1.1      tron 	struct linux_sched_param lp;
    231       1.5  augustss 	struct proc *p;
    232       1.1      tron 
    233       1.1      tron /*
    234       1.1      tron  * We only check for valid parameters and return afterwards.
    235       1.1      tron  */
    236       1.1      tron 
    237       1.1      tron 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    238       1.1      tron 		return EINVAL;
    239       1.1      tron 
    240       1.1      tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    241       1.1      tron 	if (error)
    242       1.1      tron 		return error;
    243       1.1      tron 
    244       1.1      tron 	if (SCARG(uap, pid) != 0) {
    245  1.19.2.2      yamt 		kauth_cred_t pc = cl->l_cred;
    246       1.1      tron 
    247       1.1      tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    248       1.1      tron 			return ESRCH;
    249  1.19.2.2      yamt 		if (!(cl->l_proc == p ||
    250  1.19.2.3      yamt 		      kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
    251  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
    252  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
    253  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
    254  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
    255       1.1      tron 			return EPERM;
    256       1.1      tron 	}
    257       1.1      tron 
    258  1.19.2.2      yamt 	return 0;
    259       1.1      tron /*
    260       1.1      tron  * We can't emulate anything put the default scheduling policy.
    261       1.1      tron  */
    262       1.1      tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
    263       1.1      tron 		return EINVAL;
    264       1.1      tron 
    265       1.1      tron 	return 0;
    266       1.1      tron }
    267       1.1      tron 
    268       1.1      tron int
    269      1.12   thorpej linux_sys_sched_getscheduler(cl, v, retval)
    270      1.12   thorpej 	struct lwp *cl;
    271       1.1      tron 	void *v;
    272       1.1      tron 	register_t *retval;
    273       1.1      tron {
    274       1.1      tron 	struct linux_sys_sched_getscheduler_args /* {
    275       1.1      tron 		syscallarg(linux_pid_t) pid;
    276       1.1      tron 	} */ *uap = v;
    277       1.5  augustss 	struct proc *p;
    278       1.1      tron 
    279       1.1      tron 	*retval = -1;
    280       1.1      tron /*
    281       1.1      tron  * We only check for valid parameters and return afterwards.
    282       1.1      tron  */
    283       1.1      tron 
    284       1.1      tron 	if (SCARG(uap, pid) != 0) {
    285  1.19.2.2      yamt 		kauth_cred_t pc = cl->l_cred;
    286       1.1      tron 
    287       1.1      tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    288       1.1      tron 			return ESRCH;
    289  1.19.2.2      yamt 		if (!(cl->l_proc == p ||
    290  1.19.2.3      yamt 		      kauth_authorize_generic(pc, KAUTH_GENERIC_ISSUSER, NULL) == 0 ||
    291  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) ||
    292  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) ||
    293  1.19.2.1      yamt 		      kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) ||
    294  1.19.2.1      yamt 		      kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred)))
    295       1.1      tron 			return EPERM;
    296       1.1      tron 	}
    297       1.1      tron 
    298       1.1      tron /*
    299       1.1      tron  * We can't emulate anything put the default scheduling policy.
    300       1.1      tron  */
    301       1.1      tron 	*retval = LINUX_SCHED_OTHER;
    302       1.1      tron 	return 0;
    303       1.1      tron }
    304       1.1      tron 
    305       1.1      tron int
    306  1.19.2.2      yamt linux_sys_sched_yield(struct lwp *cl, void *v,
    307  1.19.2.2      yamt     register_t *retval)
    308       1.1      tron {
    309      1.11  gmcgarry 
    310      1.11  gmcgarry 	yield();
    311       1.1      tron 	return 0;
    312       1.1      tron }
    313       1.1      tron 
    314       1.1      tron int
    315  1.19.2.2      yamt linux_sys_sched_get_priority_max(struct lwp *cl, void *v,
    316  1.19.2.2      yamt     register_t *retval)
    317       1.1      tron {
    318       1.1      tron 	struct linux_sys_sched_get_priority_max_args /* {
    319       1.1      tron 		syscallarg(int) policy;
    320       1.1      tron 	} */ *uap = v;
    321       1.1      tron 
    322       1.1      tron /*
    323       1.1      tron  * We can't emulate anything put the default scheduling policy.
    324       1.1      tron  */
    325       1.1      tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    326       1.1      tron 		*retval = -1;
    327       1.1      tron 		return EINVAL;
    328       1.1      tron 	}
    329       1.1      tron 
    330       1.1      tron 	*retval = 0;
    331       1.1      tron 	return 0;
    332       1.1      tron }
    333       1.1      tron 
    334       1.1      tron int
    335  1.19.2.2      yamt linux_sys_sched_get_priority_min(struct lwp *cl, void *v,
    336  1.19.2.2      yamt     register_t *retval)
    337       1.1      tron {
    338       1.1      tron 	struct linux_sys_sched_get_priority_min_args /* {
    339       1.1      tron 		syscallarg(int) policy;
    340       1.1      tron 	} */ *uap = v;
    341       1.1      tron 
    342       1.1      tron /*
    343       1.1      tron  * We can't emulate anything put the default scheduling policy.
    344       1.1      tron  */
    345       1.1      tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    346       1.1      tron 		*retval = -1;
    347       1.1      tron 		return EINVAL;
    348       1.1      tron 	}
    349       1.1      tron 
    350       1.1      tron 	*retval = 0;
    351       1.1      tron 	return 0;
    352       1.1      tron }
    353      1.14  jdolecek 
    354      1.14  jdolecek #ifndef __m68k__
    355      1.14  jdolecek /* Present on everything but m68k */
    356      1.14  jdolecek int
    357      1.14  jdolecek linux_sys_exit_group(l, v, retval)
    358      1.14  jdolecek 	struct lwp *l;
    359      1.14  jdolecek 	void *v;
    360      1.14  jdolecek 	register_t *retval;
    361      1.14  jdolecek {
    362  1.19.2.2      yamt #ifdef LINUX_NPTL
    363      1.14  jdolecek 	struct linux_sys_exit_group_args /* {
    364      1.14  jdolecek 		syscallarg(int) error_code;
    365      1.14  jdolecek 	} */ *uap = v;
    366  1.19.2.2      yamt 	struct proc *p = l->l_proc;
    367  1.19.2.2      yamt 	struct linux_emuldata *led = p->p_emuldata;
    368  1.19.2.2      yamt 	struct linux_emuldata *e;
    369      1.14  jdolecek 
    370  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    371  1.19.2.3      yamt 
    372  1.19.2.2      yamt #ifdef DEBUG_LINUX
    373  1.19.2.3      yamt 		printf("%s:%d, led->s->refs = %d\n", __func__, __LINE__,
    374  1.19.2.3      yamt 		    led->s->refs);
    375  1.19.2.2      yamt #endif
    376  1.19.2.3      yamt 
    377  1.19.2.3      yamt 		/*
    378  1.19.2.3      yamt 		 * The calling thread is supposed to kill all threads
    379  1.19.2.3      yamt 		 * in the same thread group (i.e. all threads created
    380  1.19.2.3      yamt 		 * via clone(2) with CLONE_THREAD flag set).
    381  1.19.2.3      yamt 		 *
    382  1.19.2.3      yamt 		 * If there is only one thread, things are quite simple
    383  1.19.2.3      yamt 		 */
    384  1.19.2.3      yamt 		if (led->s->refs == 1)
    385  1.19.2.3      yamt 			return sys_exit(l, v, retval);
    386  1.19.2.2      yamt 
    387  1.19.2.2      yamt #ifdef DEBUG_LINUX
    388  1.19.2.3      yamt 		printf("%s:%d\n", __func__, __LINE__);
    389  1.19.2.2      yamt #endif
    390  1.19.2.2      yamt 
    391  1.19.2.3      yamt 		led->s->flags |= LINUX_LES_INEXITGROUP;
    392  1.19.2.3      yamt 		led->s->xstat = W_EXITCODE(SCARG(uap, error_code), 0);
    393  1.19.2.2      yamt 
    394  1.19.2.3      yamt 		/*
    395  1.19.2.3      yamt 		 * Kill all threads in the group. The emulation exit hook takes
    396  1.19.2.3      yamt 		 * care of hiding the zombies and reporting the exit code
    397  1.19.2.3      yamt 		 * properly.
    398  1.19.2.3      yamt 		 */
    399  1.19.2.3      yamt 		mutex_enter(&proclist_mutex);
    400  1.19.2.3      yamt       		LIST_FOREACH(e, &led->s->threads, threads) {
    401  1.19.2.3      yamt 			if (e->proc == p)
    402  1.19.2.3      yamt 				continue;
    403      1.14  jdolecek 
    404  1.19.2.2      yamt #ifdef DEBUG_LINUX
    405  1.19.2.3      yamt 			printf("%s: kill PID %d\n", __func__, e->proc->p_pid);
    406  1.19.2.2      yamt #endif
    407  1.19.2.3      yamt 			psignal(e->proc, SIGKILL);
    408  1.19.2.3      yamt 		}
    409  1.19.2.3      yamt 
    410  1.19.2.3      yamt 		/* Now, kill ourselves */
    411  1.19.2.3      yamt 		psignal(p, SIGKILL);
    412  1.19.2.3      yamt 		mutex_exit(&proclist_mutex);
    413  1.19.2.3      yamt 
    414  1.19.2.3      yamt 		return 0;
    415  1.19.2.3      yamt 
    416  1.19.2.2      yamt 	}
    417  1.19.2.3      yamt #endif /* LINUX_NPTL */
    418  1.19.2.2      yamt 
    419  1.19.2.2      yamt 	return sys_exit(l, v, retval);
    420      1.14  jdolecek }
    421      1.14  jdolecek #endif /* !__m68k__ */
    422      1.19      manu 
    423  1.19.2.1      yamt #ifdef LINUX_NPTL
    424      1.19      manu int
    425      1.19      manu linux_sys_set_tid_address(l, v, retval)
    426      1.19      manu 	struct lwp *l;
    427      1.19      manu 	void *v;
    428      1.19      manu 	register_t *retval;
    429      1.19      manu {
    430      1.19      manu 	struct linux_sys_set_tid_address_args /* {
    431      1.19      manu 		syscallarg(int *) tidptr;
    432      1.19      manu 	} */ *uap = v;
    433      1.19      manu 	struct linux_emuldata *led;
    434      1.19      manu 
    435      1.19      manu 	led = (struct linux_emuldata *)l->l_proc->p_emuldata;
    436      1.19      manu 	led->clear_tid = SCARG(uap, tid);
    437      1.19      manu 
    438  1.19.2.3      yamt 	led->s->flags |= LINUX_LES_USE_NPTL;
    439  1.19.2.3      yamt 
    440      1.19      manu 	*retval = l->l_proc->p_pid;
    441      1.19      manu 
    442      1.19      manu 	return 0;
    443      1.19      manu }
    444  1.19.2.1      yamt 
    445  1.19.2.1      yamt /* ARGUSED1 */
    446  1.19.2.1      yamt int
    447  1.19.2.1      yamt linux_sys_gettid(l, v, retval)
    448  1.19.2.1      yamt 	struct lwp *l;
    449  1.19.2.1      yamt 	void *v;
    450  1.19.2.1      yamt 	register_t *retval;
    451  1.19.2.1      yamt {
    452  1.19.2.2      yamt 	/* The Linux kernel does it exactly that way */
    453  1.19.2.1      yamt 	*retval = l->l_proc->p_pid;
    454  1.19.2.1      yamt 	return 0;
    455  1.19.2.1      yamt }
    456  1.19.2.1      yamt 
    457  1.19.2.2      yamt #ifdef LINUX_NPTL
    458  1.19.2.2      yamt /* ARGUSED1 */
    459  1.19.2.2      yamt int
    460  1.19.2.2      yamt linux_sys_getpid(l, v, retval)
    461  1.19.2.2      yamt 	struct lwp *l;
    462  1.19.2.2      yamt 	void *v;
    463  1.19.2.2      yamt 	register_t *retval;
    464  1.19.2.2      yamt {
    465  1.19.2.3      yamt 	struct linux_emuldata *led = l->l_proc->p_emuldata;
    466  1.19.2.2      yamt 
    467  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    468  1.19.2.3      yamt 		/* The Linux kernel does it exactly that way */
    469  1.19.2.3      yamt 		*retval = led->s->group_pid;
    470  1.19.2.3      yamt 	} else {
    471  1.19.2.3      yamt 		*retval = l->l_proc->p_pid;
    472  1.19.2.3      yamt 	}
    473  1.19.2.2      yamt 
    474  1.19.2.2      yamt 	return 0;
    475  1.19.2.2      yamt }
    476  1.19.2.2      yamt 
    477  1.19.2.2      yamt /* ARGUSED1 */
    478  1.19.2.2      yamt int
    479  1.19.2.2      yamt linux_sys_getppid(l, v, retval)
    480  1.19.2.2      yamt 	struct lwp *l;
    481  1.19.2.2      yamt 	void *v;
    482  1.19.2.2      yamt 	register_t *retval;
    483  1.19.2.2      yamt {
    484  1.19.2.2      yamt 	struct proc *p = l->l_proc;
    485  1.19.2.2      yamt 	struct linux_emuldata *led = p->p_emuldata;
    486  1.19.2.2      yamt 	struct proc *glp;
    487  1.19.2.2      yamt 	struct proc *pp;
    488  1.19.2.2      yamt 
    489  1.19.2.3      yamt 	if (led->s->flags & LINUX_LES_USE_NPTL) {
    490  1.19.2.2      yamt 
    491  1.19.2.3      yamt 		/* Find the thread group leader's parent */
    492  1.19.2.3      yamt 		if ((glp = pfind(led->s->group_pid)) == NULL) {
    493  1.19.2.3      yamt 			/* Maybe panic... */
    494  1.19.2.3      yamt 			printf("linux_sys_getppid: missing group leader PID"
    495  1.19.2.3      yamt 			    " %d\n", led->s->group_pid);
    496  1.19.2.3      yamt 			return -1;
    497  1.19.2.3      yamt 		}
    498  1.19.2.3      yamt 		pp = glp->p_pptr;
    499  1.19.2.3      yamt 
    500  1.19.2.3      yamt 		/* If this is a Linux process too, return thread group PID */
    501  1.19.2.3      yamt 		if (pp->p_emul == p->p_emul) {
    502  1.19.2.3      yamt 			struct linux_emuldata *pled;
    503  1.19.2.3      yamt 
    504  1.19.2.3      yamt 			pled = pp->p_emuldata;
    505  1.19.2.3      yamt 			*retval = pled->s->group_pid;
    506  1.19.2.3      yamt 		} else {
    507  1.19.2.3      yamt 			*retval = pp->p_pid;
    508  1.19.2.3      yamt 		}
    509  1.19.2.2      yamt 
    510  1.19.2.2      yamt 	} else {
    511  1.19.2.3      yamt 		*retval = p->p_pptr->p_pid;
    512  1.19.2.2      yamt 	}
    513  1.19.2.2      yamt 
    514  1.19.2.2      yamt 	return 0;
    515  1.19.2.2      yamt }
    516  1.19.2.2      yamt #endif /* LINUX_NPTL */
    517  1.19.2.2      yamt 
    518  1.19.2.1      yamt int
    519  1.19.2.1      yamt linux_sys_sched_getaffinity(l, v, retval)
    520  1.19.2.1      yamt 	struct lwp *l;
    521  1.19.2.1      yamt 	void *v;
    522  1.19.2.1      yamt 	register_t *retval;
    523  1.19.2.1      yamt {
    524  1.19.2.1      yamt 	struct linux_sys_sched_getaffinity_args /* {
    525  1.19.2.1      yamt 		syscallarg(pid_t) pid;
    526  1.19.2.1      yamt 		syscallarg(unsigned int) len;
    527  1.19.2.1      yamt 		syscallarg(unsigned long *) mask;
    528  1.19.2.1      yamt 	} */ *uap = v;
    529  1.19.2.1      yamt 	int error;
    530  1.19.2.1      yamt 	int ret;
    531  1.19.2.1      yamt 	char *data;
    532  1.19.2.1      yamt 	int *retp;
    533  1.19.2.1      yamt 
    534  1.19.2.1      yamt 	if (SCARG(uap, mask) == NULL)
    535  1.19.2.1      yamt 		return EINVAL;
    536  1.19.2.1      yamt 
    537  1.19.2.1      yamt 	if (SCARG(uap, len) < sizeof(int))
    538  1.19.2.1      yamt 		return EINVAL;
    539  1.19.2.1      yamt 
    540  1.19.2.1      yamt 	if (pfind(SCARG(uap, pid)) == NULL)
    541  1.19.2.1      yamt 		return ESRCH;
    542  1.19.2.1      yamt 
    543  1.19.2.1      yamt 	/*
    544  1.19.2.1      yamt 	 * return the actual number of CPU, tag all of them as available
    545  1.19.2.1      yamt 	 * The result is a mask, the first CPU being in the least significant
    546  1.19.2.1      yamt 	 * bit.
    547  1.19.2.1      yamt 	 */
    548  1.19.2.1      yamt 	ret = (1 << ncpu) - 1;
    549  1.19.2.1      yamt 	data = malloc(SCARG(uap, len), M_TEMP, M_WAITOK|M_ZERO);
    550  1.19.2.1      yamt 	retp = (int *)&data[SCARG(uap, len) - sizeof(ret)];
    551  1.19.2.1      yamt 	*retp = ret;
    552  1.19.2.1      yamt 
    553  1.19.2.1      yamt 	if ((error = copyout(data, SCARG(uap, mask), SCARG(uap, len))) != 0)
    554  1.19.2.1      yamt 		return error;
    555  1.19.2.1      yamt 
    556  1.19.2.1      yamt 	free(data, M_TEMP);
    557  1.19.2.1      yamt 
    558  1.19.2.1      yamt 	return 0;
    559  1.19.2.1      yamt 
    560  1.19.2.1      yamt }
    561  1.19.2.1      yamt 
    562  1.19.2.1      yamt int
    563  1.19.2.1      yamt linux_sys_sched_setaffinity(l, v, retval)
    564  1.19.2.1      yamt 	struct lwp *l;
    565  1.19.2.1      yamt 	void *v;
    566  1.19.2.1      yamt 	register_t *retval;
    567  1.19.2.1      yamt {
    568  1.19.2.1      yamt 	struct linux_sys_sched_setaffinity_args /* {
    569  1.19.2.1      yamt 		syscallarg(pid_t) pid;
    570  1.19.2.1      yamt 		syscallarg(unsigned int) len;
    571  1.19.2.1      yamt 		syscallarg(unsigned long *) mask;
    572  1.19.2.1      yamt 	} */ *uap = v;
    573  1.19.2.1      yamt 
    574  1.19.2.1      yamt 	if (pfind(SCARG(uap, pid)) == NULL)
    575  1.19.2.1      yamt 		return ESRCH;
    576  1.19.2.1      yamt 
    577  1.19.2.1      yamt 	/* Let's ignore it */
    578  1.19.2.1      yamt #ifdef DEBUG_LINUX
    579  1.19.2.1      yamt 	printf("linux_sys_sched_setaffinity\n");
    580  1.19.2.1      yamt #endif
    581  1.19.2.1      yamt 	return 0;
    582  1.19.2.1      yamt };
    583  1.19.2.1      yamt #endif /* LINUX_NPTL */
    584