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