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