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