Home | History | Annotate | Line # | Download | only in common
linux_sched.c revision 1.7.2.1
      1 /*	$NetBSD: linux_sched.c,v 1.7.2.1 2001/03/05 22:49:27 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center; by Matthias Scheler.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Linux compatibility module. Try to deal with scheduler related syscalls.
     42  */
     43 
     44 #include <sys/types.h>
     45 #include <sys/param.h>
     46 #include <sys/mount.h>
     47 #include <sys/lwp.h>
     48 #include <sys/proc.h>
     49 #include <sys/systm.h>
     50 #include <sys/syscallargs.h>
     51 
     52 #include <machine/cpu.h>
     53 
     54 #include <compat/linux/common/linux_types.h>
     55 #include <compat/linux/common/linux_signal.h>
     56 
     57 #include <compat/linux/linux_syscallargs.h>
     58 
     59 #include <compat/linux/common/linux_sched.h>
     60 
     61 int
     62 linux_sys_clone(l, v, retval)
     63 	struct lwp *l;
     64 	void *v;
     65 	register_t *retval;
     66 {
     67 	struct linux_sys_clone_args /* {
     68 		syscallarg(int) flags;
     69 		syscallarg(void *) stack;
     70 	} */ *uap = v;
     71 	int flags, sig;
     72 
     73 	/*
     74 	 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
     75 	 */
     76 	if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
     77 		return (EINVAL);
     78 
     79 	flags = 0;
     80 
     81 	if (SCARG(uap, flags) & LINUX_CLONE_VM)
     82 		flags |= FORK_SHAREVM;
     83 	if (SCARG(uap, flags) & LINUX_CLONE_FS)
     84 		flags |= FORK_SHARECWD;
     85 	if (SCARG(uap, flags) & LINUX_CLONE_FILES)
     86 		flags |= FORK_SHAREFILES;
     87 	if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
     88 		flags |= FORK_SHARESIGS;
     89 	if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
     90 		flags |= FORK_PPWAIT;
     91 
     92 	sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
     93 	if (sig < 0 || sig >= LINUX__NSIG)
     94 		return (EINVAL);
     95 	sig = linux_to_native_sig[sig];
     96 
     97 	/*
     98 	 * Note that Linux does not provide a portable way of specifying
     99 	 * the stack area; the caller must know if the stack grows up
    100 	 * or down.  So, we pass a stack size of 0, so that the code
    101 	 * that makes this adjustment is a noop.
    102 	 */
    103 	return (fork1(l, flags, sig, SCARG(uap, stack), 0,
    104 	    NULL, NULL, retval, NULL));
    105 }
    106 
    107 int
    108 linux_sys_sched_setparam(cl, v, retval)
    109 	struct lwp *cl;
    110 	void *v;
    111 	register_t *retval;
    112 {
    113 	struct linux_sys_sched_setparam_args /* {
    114 		syscallarg(linux_pid_t) pid;
    115 		syscallarg(const struct linux_sched_param *) sp;
    116 	} */ *uap = v;
    117 	struct proc *cp = cl->l_proc;
    118 	int error;
    119 	struct linux_sched_param lp;
    120 	struct proc *p;
    121 
    122 /*
    123  * We only check for valid parameters and return afterwards.
    124  */
    125 
    126 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    127 		return EINVAL;
    128 
    129 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    130 	if (error)
    131 		return error;
    132 
    133 	if (SCARG(uap, pid) != 0) {
    134 		struct pcred *pc = cp->p_cred;
    135 
    136 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    137 			return ESRCH;
    138 		if (!(cp == p ||
    139 		      pc->pc_ucred->cr_uid == 0 ||
    140 		      pc->p_ruid == p->p_cred->p_ruid ||
    141 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    142 		      pc->p_ruid == p->p_ucred->cr_uid ||
    143 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    144 			return EPERM;
    145 	}
    146 
    147 	return 0;
    148 }
    149 
    150 int
    151 linux_sys_sched_getparam(cl, v, retval)
    152 	struct lwp *cl;
    153 	void *v;
    154 	register_t *retval;
    155 {
    156 	struct linux_sys_sched_getparam_args /* {
    157 		syscallarg(linux_pid_t) pid;
    158 		syscallarg(struct linux_sched_param *) sp;
    159 	} */ *uap = v;
    160 	struct proc *cp = cl->l_proc;
    161 	struct proc *p;
    162 	struct linux_sched_param lp;
    163 
    164 /*
    165  * We only check for valid parameters and return a dummy priority afterwards.
    166  */
    167 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    168 		return EINVAL;
    169 
    170 	if (SCARG(uap, pid) != 0) {
    171 		struct pcred *pc = cp->p_cred;
    172 
    173 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    174 			return ESRCH;
    175 		if (!(cp == p ||
    176 		      pc->pc_ucred->cr_uid == 0 ||
    177 		      pc->p_ruid == p->p_cred->p_ruid ||
    178 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    179 		      pc->p_ruid == p->p_ucred->cr_uid ||
    180 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    181 			return EPERM;
    182 	}
    183 
    184 	lp.sched_priority = 0;
    185 	return copyout(&lp, SCARG(uap, sp), sizeof(lp));
    186 }
    187 
    188 int
    189 linux_sys_sched_setscheduler(cl, v, retval)
    190 	struct lwp *cl;
    191 	void *v;
    192 	register_t *retval;
    193 {
    194 	struct linux_sys_sched_setscheduler_args /* {
    195 		syscallarg(linux_pid_t) pid;
    196 		syscallarg(int) policy;
    197 		syscallarg(cont struct linux_sched_scheduler *) sp;
    198 	} */ *uap = v;
    199 	struct proc *cp = cl->l_proc;
    200 	int error;
    201 	struct linux_sched_param lp;
    202 	struct proc *p;
    203 
    204 /*
    205  * We only check for valid parameters and return afterwards.
    206  */
    207 
    208 	if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
    209 		return EINVAL;
    210 
    211 	error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
    212 	if (error)
    213 		return error;
    214 
    215 	if (SCARG(uap, pid) != 0) {
    216 		struct pcred *pc = cp->p_cred;
    217 
    218 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    219 			return ESRCH;
    220 		if (!(cp == p ||
    221 		      pc->pc_ucred->cr_uid == 0 ||
    222 		      pc->p_ruid == p->p_cred->p_ruid ||
    223 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    224 		      pc->p_ruid == p->p_ucred->cr_uid ||
    225 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    226 			return EPERM;
    227 	}
    228 
    229 /*
    230  * We can't emulate anything put the default scheduling policy.
    231  */
    232 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
    233 		return EINVAL;
    234 
    235 	return 0;
    236 }
    237 
    238 int
    239 linux_sys_sched_getscheduler(cl, v, retval)
    240 	struct lwp *cl;
    241 	void *v;
    242 	register_t *retval;
    243 {
    244 	struct linux_sys_sched_getscheduler_args /* {
    245 		syscallarg(linux_pid_t) pid;
    246 	} */ *uap = v;
    247 	struct proc *cp = cl->l_proc;
    248 	struct proc *p;
    249 
    250 	*retval = -1;
    251 /*
    252  * We only check for valid parameters and return afterwards.
    253  */
    254 
    255 	if (SCARG(uap, pid) != 0) {
    256 		struct pcred *pc = cp->p_cred;
    257 
    258 		if ((p = pfind(SCARG(uap, pid))) == NULL)
    259 			return ESRCH;
    260 		if (!(cp == p ||
    261 		      pc->pc_ucred->cr_uid == 0 ||
    262 		      pc->p_ruid == p->p_cred->p_ruid ||
    263 		      pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
    264 		      pc->p_ruid == p->p_ucred->cr_uid ||
    265 		      pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
    266 			return EPERM;
    267 	}
    268 
    269 /*
    270  * We can't emulate anything put the default scheduling policy.
    271  */
    272 	*retval = LINUX_SCHED_OTHER;
    273 	return 0;
    274 }
    275 
    276 int
    277 linux_sys_sched_yield(cl, v, retval)
    278 	struct lwp *cl;
    279 	void *v;
    280 	register_t *retval;
    281 {
    282 	need_resched(curcpu());
    283 	return 0;
    284 }
    285 
    286 int
    287 linux_sys_sched_get_priority_max(cl, v, retval)
    288 	struct lwp *cl;
    289 	void *v;
    290 	register_t *retval;
    291 {
    292 	struct linux_sys_sched_get_priority_max_args /* {
    293 		syscallarg(int) policy;
    294 	} */ *uap = v;
    295 
    296 /*
    297  * We can't emulate anything put the default scheduling policy.
    298  */
    299 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    300 		*retval = -1;
    301 		return EINVAL;
    302 	}
    303 
    304 	*retval = 0;
    305 	return 0;
    306 }
    307 
    308 int
    309 linux_sys_sched_get_priority_min(cl, v, retval)
    310 	struct lwp *cl;
    311 	void *v;
    312 	register_t *retval;
    313 {
    314 	struct linux_sys_sched_get_priority_min_args /* {
    315 		syscallarg(int) policy;
    316 	} */ *uap = v;
    317 
    318 /*
    319  * We can't emulate anything put the default scheduling policy.
    320  */
    321 	if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
    322 		*retval = -1;
    323 		return EINVAL;
    324 	}
    325 
    326 	*retval = 0;
    327 	return 0;
    328 }
    329