Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.2.2.1
      1  1.2.2.1   bouyer /*	$NetBSD: linux_sched.c,v 1.2.2.1 2000/11/20 18:08:25 bouyer 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.1     tron 
     44      1.1     tron #include <sys/types.h>
     45      1.1     tron #include <sys/param.h>
     46      1.1     tron #include <sys/mount.h>
     47      1.1     tron #include <sys/proc.h>
     48      1.1     tron #include <sys/systm.h>
     49      1.1     tron #include <sys/syscallargs.h>
     50      1.1     tron 
     51  1.2.2.1   bouyer #include <machine/cpu.h>
     52  1.2.2.1   bouyer 
     53      1.1     tron #include <compat/linux/common/linux_types.h>
     54      1.1     tron #include <compat/linux/common/linux_signal.h>
     55      1.1     tron 
     56      1.1     tron #include <compat/linux/linux_syscallargs.h>
     57      1.1     tron 
     58      1.1     tron #include <compat/linux/common/linux_sched.h>
     59      1.1     tron 
     60      1.1     tron int
     61      1.1     tron linux_sys_clone(p, v, retval)
     62      1.1     tron 	struct proc *p;
     63      1.1     tron 	void *v;
     64      1.1     tron 	register_t *retval;
     65      1.1     tron {
     66      1.1     tron 	struct linux_sys_clone_args /* {
     67      1.1     tron 		syscallarg(int) flags;
     68      1.1     tron 		syscallarg(void *) stack;
     69      1.1     tron 	} */ *uap = v;
     70      1.1     tron 	int flags, sig;
     71      1.1     tron 
     72      1.1     tron 	/*
     73      1.1     tron 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
     74      1.1     tron 	 */
     75      1.1     tron 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
     76      1.1     tron 		return (EINVAL);
     77      1.1     tron 
     78      1.1     tron 	flags = 0;
     79      1.1     tron 
     80      1.1     tron 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
     81      1.1     tron 		flags |= FORK_SHAREVM;
     82      1.1     tron 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
     83      1.1     tron 		flags |= FORK_SHARECWD;
     84      1.1     tron 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
     85      1.1     tron 		flags |= FORK_SHAREFILES;
     86      1.1     tron 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
     87      1.1     tron 		flags |= FORK_SHARESIGS;
     88      1.1     tron 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
     89      1.1     tron 		flags |= FORK_PPWAIT;
     90      1.1     tron 
     91      1.1     tron 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
     92  1.2.2.1   bouyer 	if (sig < 0 || sig >= LINUX__NSIG)
     93      1.1     tron 		return (EINVAL);
     94      1.1     tron 	sig = linux_to_native_sig[sig];
     95      1.1     tron 
     96      1.1     tron 	/*
     97      1.1     tron 	 * Note that Linux does not provide a portable way of specifying
     98      1.1     tron 	 * the stack area; the caller must know if the stack grows up
     99      1.1     tron 	 * or down.  So, we pass a stack size of 0, so that the code
    100      1.1     tron 	 * that makes this adjustment is a noop.
    101      1.1     tron 	 */
    102  1.2.2.1   bouyer 	return (fork1(p, flags, sig, SCARG(uap, stack), 0,
    103  1.2.2.1   bouyer 	    NULL, NULL, retval, NULL));
    104      1.1     tron }
    105      1.1     tron 
    106      1.1     tron int
    107      1.1     tron linux_sys_sched_setparam(cp, v, retval)
    108      1.1     tron 	struct proc *cp;
    109      1.1     tron 	void *v;
    110      1.1     tron 	register_t *retval;
    111      1.1     tron {
    112      1.1     tron 	struct linux_sys_sched_setparam_args /* {
    113      1.1     tron 		syscallarg(linux_pid_t) pid;
    114      1.1     tron 		syscallarg(const struct linux_sched_param *) sp;
    115      1.1     tron 	} */ *uap = v;
    116      1.1     tron 	int error;
    117      1.1     tron 	struct linux_sched_param lp;
    118  1.2.2.1   bouyer 	struct proc *p;
    119      1.1     tron 
    120      1.1     tron /*
    121      1.1     tron  * We only check for valid parameters and return afterwards.
    122      1.1     tron  */
    123      1.1     tron 
    124      1.1     tron 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    125      1.1     tron 		return EINVAL;
    126      1.1     tron 
    127      1.1     tron 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    128      1.1     tron 	if (error)
    129      1.1     tron 		return error;
    130      1.1     tron 
    131      1.1     tron 	if (SCARG(uap, pid) != 0) {
    132  1.2.2.1   bouyer 		struct pcred *pc = cp->p_cred;
    133      1.1     tron 
    134      1.1     tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    135      1.1     tron 			return ESRCH;
    136      1.1     tron 		if (!(cp == p ||
    137      1.1     tron 		      pc->pc_ucred->cr_uid == 0 ||
    138      1.1     tron 		      pc->p_ruid == p->p_cred->p_ruid ||
    139      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    140      1.1     tron 		      pc->p_ruid == p->p_ucred->cr_uid ||
    141      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    142      1.1     tron 			return EPERM;
    143      1.1     tron 	}
    144      1.1     tron 
    145      1.1     tron 	return 0;
    146      1.1     tron }
    147      1.1     tron 
    148      1.1     tron int
    149      1.1     tron linux_sys_sched_getparam(cp, v, retval)
    150      1.1     tron 	struct proc *cp;
    151      1.1     tron 	void *v;
    152      1.1     tron 	register_t *retval;
    153      1.1     tron {
    154      1.1     tron 	struct linux_sys_sched_getparam_args /* {
    155      1.1     tron 		syscallarg(linux_pid_t) pid;
    156      1.1     tron 		syscallarg(struct linux_sched_param *) sp;
    157      1.1     tron 	} */ *uap = v;
    158  1.2.2.1   bouyer 	struct proc *p;
    159      1.1     tron 	struct linux_sched_param lp;
    160      1.1     tron 
    161      1.1     tron /*
    162      1.1     tron  * We only check for valid parameters and return a dummy priority afterwards.
    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 	if (SCARG(uap, pid) != 0) {
    168  1.2.2.1   bouyer 		struct pcred *pc = cp->p_cred;
    169      1.1     tron 
    170      1.1     tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    171      1.1     tron 			return ESRCH;
    172      1.1     tron 		if (!(cp == p ||
    173      1.1     tron 		      pc->pc_ucred->cr_uid == 0 ||
    174      1.1     tron 		      pc->p_ruid == p->p_cred->p_ruid ||
    175      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    176      1.1     tron 		      pc->p_ruid == p->p_ucred->cr_uid ||
    177      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    178      1.1     tron 			return EPERM;
    179      1.1     tron 	}
    180      1.1     tron 
    181      1.1     tron 	lp.sched_priority = 0;
    182      1.1     tron 	return copyout(&lp, SCARG(uap, sp), sizeof(lp));
    183      1.1     tron }
    184      1.1     tron 
    185      1.1     tron int
    186      1.1     tron linux_sys_sched_setscheduler(cp, v, retval)
    187      1.1     tron 	struct proc *cp;
    188      1.1     tron 	void *v;
    189      1.1     tron 	register_t *retval;
    190      1.1     tron {
    191      1.1     tron 	struct linux_sys_sched_setscheduler_args /* {
    192      1.1     tron 		syscallarg(linux_pid_t) pid;
    193      1.1     tron 		syscallarg(int) policy;
    194      1.1     tron 		syscallarg(cont struct linux_sched_scheduler *) sp;
    195      1.1     tron 	} */ *uap = v;
    196      1.1     tron 	int error;
    197      1.1     tron 	struct linux_sched_param lp;
    198  1.2.2.1   bouyer 	struct proc *p;
    199      1.1     tron 
    200      1.1     tron /*
    201      1.1     tron  * We only check for valid parameters and return afterwards.
    202      1.1     tron  */
    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 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    208      1.1     tron 	if (error)
    209      1.1     tron 		return error;
    210      1.1     tron 
    211      1.1     tron 	if (SCARG(uap, pid) != 0) {
    212  1.2.2.1   bouyer 		struct pcred *pc = cp->p_cred;
    213      1.1     tron 
    214      1.1     tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    215      1.1     tron 			return ESRCH;
    216      1.1     tron 		if (!(cp == p ||
    217      1.1     tron 		      pc->pc_ucred->cr_uid == 0 ||
    218      1.1     tron 		      pc->p_ruid == p->p_cred->p_ruid ||
    219      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    220      1.1     tron 		      pc->p_ruid == p->p_ucred->cr_uid ||
    221      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    222      1.1     tron 			return EPERM;
    223      1.1     tron 	}
    224      1.1     tron 
    225      1.1     tron /*
    226      1.1     tron  * We can't emulate anything put the default scheduling policy.
    227      1.1     tron  */
    228      1.1     tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
    229      1.1     tron 		return EINVAL;
    230      1.1     tron 
    231      1.1     tron 	return 0;
    232      1.1     tron }
    233      1.1     tron 
    234      1.1     tron int
    235      1.1     tron linux_sys_sched_getscheduler(cp, v, retval)
    236      1.1     tron 	struct proc *cp;
    237      1.1     tron 	void *v;
    238      1.1     tron 	register_t *retval;
    239      1.1     tron {
    240      1.1     tron 	struct linux_sys_sched_getscheduler_args /* {
    241      1.1     tron 		syscallarg(linux_pid_t) pid;
    242      1.1     tron 	} */ *uap = v;
    243  1.2.2.1   bouyer 	struct proc *p;
    244      1.1     tron 
    245      1.1     tron 	*retval = -1;
    246      1.1     tron /*
    247      1.1     tron  * We only check for valid parameters and return afterwards.
    248      1.1     tron  */
    249      1.1     tron 
    250      1.1     tron 	if (SCARG(uap, pid) != 0) {
    251  1.2.2.1   bouyer 		struct pcred *pc = cp->p_cred;
    252      1.1     tron 
    253      1.1     tron 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    254      1.1     tron 			return ESRCH;
    255      1.1     tron 		if (!(cp == p ||
    256      1.1     tron 		      pc->pc_ucred->cr_uid == 0 ||
    257      1.1     tron 		      pc->p_ruid == p->p_cred->p_ruid ||
    258      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    259      1.1     tron 		      pc->p_ruid == p->p_ucred->cr_uid ||
    260      1.1     tron 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    261      1.1     tron 			return EPERM;
    262      1.1     tron 	}
    263      1.1     tron 
    264      1.1     tron /*
    265      1.1     tron  * We can't emulate anything put the default scheduling policy.
    266      1.1     tron  */
    267      1.1     tron 	*retval = LINUX_SCHED_OTHER;
    268      1.1     tron 	return 0;
    269      1.1     tron }
    270      1.1     tron 
    271      1.1     tron int
    272      1.1     tron linux_sys_sched_yield(cp, v, retval)
    273      1.1     tron 	struct proc *cp;
    274      1.1     tron 	void *v;
    275      1.1     tron 	register_t *retval;
    276      1.1     tron {
    277  1.2.2.1   bouyer 	need_resched(curcpu());
    278      1.1     tron 	return 0;
    279      1.1     tron }
    280      1.1     tron 
    281      1.1     tron int
    282      1.1     tron linux_sys_sched_get_priority_max(cp, v, retval)
    283      1.1     tron 	struct proc *cp;
    284      1.1     tron 	void *v;
    285      1.1     tron 	register_t *retval;
    286      1.1     tron {
    287      1.1     tron 	struct linux_sys_sched_get_priority_max_args /* {
    288      1.1     tron 		syscallarg(int) policy;
    289      1.1     tron 	} */ *uap = v;
    290      1.1     tron 
    291      1.1     tron /*
    292      1.1     tron  * We can't emulate anything put the default scheduling policy.
    293      1.1     tron  */
    294      1.1     tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    295      1.1     tron 		*retval = -1;
    296      1.1     tron 		return EINVAL;
    297      1.1     tron 	}
    298      1.1     tron 
    299      1.1     tron 	*retval = 0;
    300      1.1     tron 	return 0;
    301      1.1     tron }
    302      1.1     tron 
    303      1.1     tron int
    304      1.1     tron linux_sys_sched_get_priority_min(cp, v, retval)
    305      1.1     tron 	struct proc *cp;
    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_get_priority_min_args /* {
    310      1.1     tron 		syscallarg(int) policy;
    311      1.1     tron 	} */ *uap = v;
    312      1.1     tron 
    313      1.1     tron /*
    314      1.1     tron  * We can't emulate anything put the default scheduling policy.
    315      1.1     tron  */
    316      1.1     tron 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    317      1.1     tron 		*retval = -1;
    318      1.1     tron 		return EINVAL;
    319      1.1     tron 	}
    320      1.1     tron 
    321      1.1     tron 	*retval = 0;
    322      1.1     tron 	return 0;
    323      1.1     tron }
    324