1 /* $NetBSD: freebsd_sched.c,v 1.3 2006/05/14 21:24:49 elad 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 * FreeBSD compatibility module. Try to deal with scheduler related syscalls. 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: freebsd_sched.c,v 1.3 2006/05/14 21:24:49 elad Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/mount.h> 49 #include <sys/proc.h> 50 #include <sys/systm.h> 51 #include <sys/syscallargs.h> 52 #include <sys/kauth.h> 53 54 #include <machine/cpu.h> 55 56 #include <compat/freebsd/freebsd_syscallargs.h> 57 #include <compat/freebsd/freebsd_sched.h> 58 59 int 60 freebsd_sys_yield(l, v, retval) 61 struct lwp *l; 62 void *v; 63 register_t *retval; 64 { 65 66 yield(); 67 return 0; 68 } 69 70 int 71 freebsd_sys_sched_setparam(l, v, retval) 72 struct lwp *l; 73 void *v; 74 register_t *retval; 75 { 76 struct freebsd_sys_sched_setparam_args /* { 77 syscallarg(pid_t) pid; 78 syscallarg(const struct freebsd_sched_param *) sp; 79 } */ *uap = v; 80 int error; 81 struct freebsd_sched_param lp; 82 struct proc *p; 83 84 /* 85 * We only check for valid parameters and return afterwards. 86 */ 87 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) 88 return EINVAL; 89 90 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 91 if (error) 92 return error; 93 94 if (SCARG(uap, pid) != 0) { 95 kauth_cred_t pc = l->l_proc->p_cred; 96 97 if ((p = pfind(SCARG(uap, pid))) == NULL) 98 return ESRCH; 99 if (!(l->l_proc == p || 100 kauth_cred_geteuid(pc) == 0 || 101 kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) || 102 kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) || 103 kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) || 104 kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred))) 105 return EPERM; 106 } 107 108 return 0; 109 } 110 111 int 112 freebsd_sys_sched_getparam(l, v, retval) 113 struct lwp *l; 114 void *v; 115 register_t *retval; 116 { 117 struct freebsd_sys_sched_getparam_args /* { 118 syscallarg(pid_t) pid; 119 syscallarg(struct freebsd_sched_param *) sp; 120 } */ *uap = v; 121 struct proc *p; 122 struct freebsd_sched_param lp; 123 124 /* 125 * We only check for valid parameters and return a dummy 126 * priority afterwards. 127 */ 128 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) 129 return EINVAL; 130 131 if (SCARG(uap, pid) != 0) { 132 kauth_cred_t pc = l->l_proc->p_cred; 133 134 if ((p = pfind(SCARG(uap, pid))) == NULL) 135 return ESRCH; 136 if (!(l->l_proc == p || 137 kauth_cred_geteuid(pc) == 0 || 138 kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) || 139 kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) || 140 kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) || 141 kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred))) 142 return EPERM; 143 } 144 145 lp.sched_priority = 0; 146 return copyout(&lp, SCARG(uap, sp), sizeof(lp)); 147 } 148 149 int 150 freebsd_sys_sched_setscheduler(l, v, retval) 151 struct lwp *l; 152 void *v; 153 register_t *retval; 154 { 155 struct freebsd_sys_sched_setscheduler_args /* { 156 syscallarg(pid_t) pid; 157 syscallarg(int) policy; 158 syscallarg(cont struct freebsd_sched_scheduler *) sp; 159 } */ *uap = v; 160 int error; 161 struct freebsd_sched_param lp; 162 struct proc *p; 163 164 /* 165 * We only check for valid parameters and return afterwards. 166 */ 167 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) 168 return EINVAL; 169 170 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 171 if (error) 172 return error; 173 174 if (SCARG(uap, pid) != 0) { 175 kauth_cred_t pc = l->l_proc->p_cred; 176 177 if ((p = pfind(SCARG(uap, pid))) == NULL) 178 return ESRCH; 179 if (!(l->l_proc == p || 180 kauth_cred_geteuid(pc) == 0 || 181 kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) || 182 kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) || 183 kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) || 184 kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred))) 185 return EPERM; 186 } 187 188 /* 189 * We can't emulate anything put the default scheduling policy. 190 */ 191 if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER || lp.sched_priority != 0) 192 return EINVAL; 193 194 return 0; 195 } 196 197 int 198 freebsd_sys_sched_getscheduler(l, v, retval) 199 struct lwp *l; 200 void *v; 201 register_t *retval; 202 { 203 struct freebsd_sys_sched_getscheduler_args /* { 204 syscallarg(pid_t) pid; 205 } */ *uap = v; 206 struct proc *p; 207 208 *retval = -1; 209 210 /* 211 * We only check for valid parameters and return afterwards. 212 */ 213 if (SCARG(uap, pid) != 0) { 214 kauth_cred_t pc = l->l_proc->p_cred; 215 216 if ((p = pfind(SCARG(uap, pid))) == NULL) 217 return ESRCH; 218 if (!(l->l_proc == p || 219 kauth_cred_geteuid(pc) == 0 || 220 kauth_cred_getuid(pc) == kauth_cred_getuid(p->p_cred) || 221 kauth_cred_geteuid(pc) == kauth_cred_getuid(p->p_cred) || 222 kauth_cred_getuid(pc) == kauth_cred_geteuid(p->p_cred) || 223 kauth_cred_geteuid(pc) == kauth_cred_geteuid(p->p_cred))) 224 return EPERM; 225 } 226 227 /* 228 * We can't emulate anything put the default scheduling policy. 229 */ 230 *retval = FREEBSD_SCHED_OTHER; 231 return 0; 232 } 233 234 int 235 freebsd_sys_sched_yield(l, v, retval) 236 struct lwp *l; 237 void *v; 238 register_t *retval; 239 { 240 241 yield(); 242 return 0; 243 } 244 245 int 246 freebsd_sys_sched_get_priority_max(l, v, retval) 247 struct lwp *l; 248 void *v; 249 register_t *retval; 250 { 251 struct freebsd_sys_sched_get_priority_max_args /* { 252 syscallarg(int) policy; 253 } */ *uap = v; 254 255 /* 256 * We can't emulate anything put the default scheduling policy. 257 */ 258 if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) { 259 *retval = -1; 260 return EINVAL; 261 } 262 263 *retval = 0; 264 return 0; 265 } 266 267 int 268 freebsd_sys_sched_get_priority_min(l, v, retval) 269 struct lwp *l; 270 void *v; 271 register_t *retval; 272 { 273 struct freebsd_sys_sched_get_priority_min_args /* { 274 syscallarg(int) policy; 275 } */ *uap = v; 276 277 /* 278 * We can't emulate anything put the default scheduling policy. 279 */ 280 if (SCARG(uap, policy) != FREEBSD_SCHED_OTHER) { 281 *retval = -1; 282 return EINVAL; 283 } 284 285 *retval = 0; 286 return 0; 287 } 288