linux_sched.c revision 1.20 1 1.20 manu /* $NetBSD: linux_sched.c,v 1.20 2005/11/04 16:54:11 manu 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.20 manu __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.20 2005/11/04 16:54:11 manu 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.12 thorpej #include <sys/sa.h>
52 1.1 tron #include <sys/syscallargs.h>
53 1.14 jdolecek #include <sys/wait.h>
54 1.3 itohy
55 1.3 itohy #include <machine/cpu.h>
56 1.1 tron
57 1.1 tron #include <compat/linux/common/linux_types.h>
58 1.1 tron #include <compat/linux/common/linux_signal.h>
59 1.19 manu #include <compat/linux/common/linux_emuldata.h>
60 1.1 tron
61 1.1 tron #include <compat/linux/linux_syscallargs.h>
62 1.1 tron
63 1.1 tron #include <compat/linux/common/linux_sched.h>
64 1.1 tron
65 1.1 tron int
66 1.12 thorpej linux_sys_clone(l, v, retval)
67 1.12 thorpej struct lwp *l;
68 1.1 tron void *v;
69 1.1 tron register_t *retval;
70 1.1 tron {
71 1.1 tron struct linux_sys_clone_args /* {
72 1.1 tron syscallarg(int) flags;
73 1.1 tron syscallarg(void *) stack;
74 1.19 manu #ifdef __amd64__
75 1.19 manu syscallarg(void *) parent_tidptr;
76 1.19 manu syscallarg(void *) child_tidptr;
77 1.19 manu #endif
78 1.1 tron } */ *uap = v;
79 1.1 tron int flags, sig;
80 1.19 manu int error;
81 1.19 manu #ifdef __amd64__
82 1.19 manu struct linux_emuldata *led;
83 1.19 manu #endif
84 1.1 tron
85 1.1 tron /*
86 1.1 tron * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
87 1.1 tron */
88 1.1 tron if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
89 1.1 tron return (EINVAL);
90 1.1 tron
91 1.13 jdolecek /*
92 1.13 jdolecek * Thread group implies shared signals. Shared signals
93 1.13 jdolecek * imply shared VM. This matches what Linux kernel does.
94 1.13 jdolecek */
95 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_THREAD
96 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
97 1.13 jdolecek return (EINVAL);
98 1.13 jdolecek if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
99 1.13 jdolecek && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
100 1.13 jdolecek return (EINVAL);
101 1.13 jdolecek
102 1.1 tron flags = 0;
103 1.1 tron
104 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VM)
105 1.1 tron flags |= FORK_SHAREVM;
106 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FS)
107 1.1 tron flags |= FORK_SHARECWD;
108 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_FILES)
109 1.1 tron flags |= FORK_SHAREFILES;
110 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
111 1.1 tron flags |= FORK_SHARESIGS;
112 1.1 tron if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
113 1.1 tron flags |= FORK_PPWAIT;
114 1.1 tron
115 1.1 tron sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
116 1.4 tron if (sig < 0 || sig >= LINUX__NSIG)
117 1.1 tron return (EINVAL);
118 1.10 christos sig = linux_to_native_signo[sig];
119 1.1 tron
120 1.19 manu #ifdef __amd64__
121 1.19 manu led = (struct linux_emuldata *)l->l_proc->p_emuldata;
122 1.19 manu
123 1.19 manu if (SCARG(uap, flags) & LINUX_CLONE_PARENT_SETTID) {
124 1.19 manu if (SCARG(uap, parent_tidptr) == NULL) {
125 1.19 manu printf("linux_sys_clone: NULL parent_tidptr\n");
126 1.19 manu return EINVAL;
127 1.19 manu }
128 1.19 manu
129 1.19 manu if ((error = copyout(&l->l_proc->p_pid,
130 1.19 manu SCARG(uap, parent_tidptr),
131 1.19 manu sizeof(l->l_proc->p_pid))) != 0)
132 1.19 manu return error;
133 1.19 manu }
134 1.19 manu
135 1.19 manu /* CLONE_CHILD_CLEARTID: TID clear in the child on exit() */
136 1.19 manu if (SCARG(uap, flags) & LINUX_CLONE_CHILD_CLEARTID)
137 1.19 manu led->child_clear_tid = SCARG(uap, child_tidptr);
138 1.19 manu else
139 1.19 manu led->child_clear_tid = NULL;
140 1.19 manu
141 1.19 manu /* CLONE_CHILD_SETTID: TID set in the child on clone() */
142 1.19 manu if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID)
143 1.19 manu led->child_set_tid = SCARG(uap, child_tidptr);
144 1.19 manu else
145 1.19 manu led->child_set_tid = NULL;
146 1.19 manu #endif
147 1.1 tron /*
148 1.1 tron * Note that Linux does not provide a portable way of specifying
149 1.1 tron * the stack area; the caller must know if the stack grows up
150 1.1 tron * or down. So, we pass a stack size of 0, so that the code
151 1.1 tron * that makes this adjustment is a noop.
152 1.1 tron */
153 1.19 manu if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
154 1.19 manu NULL, NULL, retval, NULL)) != 0)
155 1.19 manu return error;
156 1.19 manu
157 1.19 manu return 0;
158 1.1 tron }
159 1.1 tron
160 1.1 tron int
161 1.12 thorpej linux_sys_sched_setparam(cl, v, retval)
162 1.12 thorpej struct lwp *cl;
163 1.1 tron void *v;
164 1.1 tron register_t *retval;
165 1.1 tron {
166 1.1 tron struct linux_sys_sched_setparam_args /* {
167 1.1 tron syscallarg(linux_pid_t) pid;
168 1.1 tron syscallarg(const struct linux_sched_param *) sp;
169 1.1 tron } */ *uap = v;
170 1.12 thorpej struct proc *cp = cl->l_proc;
171 1.1 tron int error;
172 1.1 tron struct linux_sched_param lp;
173 1.5 augustss struct proc *p;
174 1.1 tron
175 1.1 tron /*
176 1.1 tron * We only check for valid parameters and return afterwards.
177 1.1 tron */
178 1.1 tron
179 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
180 1.1 tron return EINVAL;
181 1.1 tron
182 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
183 1.1 tron if (error)
184 1.1 tron return error;
185 1.1 tron
186 1.1 tron if (SCARG(uap, pid) != 0) {
187 1.5 augustss struct pcred *pc = cp->p_cred;
188 1.1 tron
189 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
190 1.1 tron return ESRCH;
191 1.1 tron if (!(cp == p ||
192 1.1 tron pc->pc_ucred->cr_uid == 0 ||
193 1.1 tron pc->p_ruid == p->p_cred->p_ruid ||
194 1.1 tron pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
195 1.1 tron pc->p_ruid == p->p_ucred->cr_uid ||
196 1.1 tron pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
197 1.1 tron return EPERM;
198 1.1 tron }
199 1.1 tron
200 1.1 tron return 0;
201 1.1 tron }
202 1.1 tron
203 1.1 tron int
204 1.12 thorpej linux_sys_sched_getparam(cl, v, retval)
205 1.12 thorpej struct lwp *cl;
206 1.1 tron void *v;
207 1.1 tron register_t *retval;
208 1.1 tron {
209 1.1 tron struct linux_sys_sched_getparam_args /* {
210 1.1 tron syscallarg(linux_pid_t) pid;
211 1.1 tron syscallarg(struct linux_sched_param *) sp;
212 1.1 tron } */ *uap = v;
213 1.12 thorpej struct proc *cp = cl->l_proc;
214 1.5 augustss struct proc *p;
215 1.1 tron struct linux_sched_param lp;
216 1.1 tron
217 1.1 tron /*
218 1.1 tron * We only check for valid parameters and return a dummy priority afterwards.
219 1.1 tron */
220 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
221 1.1 tron return EINVAL;
222 1.1 tron
223 1.1 tron if (SCARG(uap, pid) != 0) {
224 1.5 augustss struct pcred *pc = cp->p_cred;
225 1.1 tron
226 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
227 1.1 tron return ESRCH;
228 1.1 tron if (!(cp == p ||
229 1.1 tron pc->pc_ucred->cr_uid == 0 ||
230 1.1 tron pc->p_ruid == p->p_cred->p_ruid ||
231 1.1 tron pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
232 1.1 tron pc->p_ruid == p->p_ucred->cr_uid ||
233 1.1 tron pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
234 1.1 tron return EPERM;
235 1.1 tron }
236 1.1 tron
237 1.1 tron lp.sched_priority = 0;
238 1.1 tron return copyout(&lp, SCARG(uap, sp), sizeof(lp));
239 1.1 tron }
240 1.1 tron
241 1.1 tron int
242 1.12 thorpej linux_sys_sched_setscheduler(cl, v, retval)
243 1.12 thorpej struct lwp *cl;
244 1.1 tron void *v;
245 1.1 tron register_t *retval;
246 1.1 tron {
247 1.1 tron struct linux_sys_sched_setscheduler_args /* {
248 1.1 tron syscallarg(linux_pid_t) pid;
249 1.1 tron syscallarg(int) policy;
250 1.1 tron syscallarg(cont struct linux_sched_scheduler *) sp;
251 1.1 tron } */ *uap = v;
252 1.12 thorpej struct proc *cp = cl->l_proc;
253 1.1 tron int error;
254 1.1 tron struct linux_sched_param lp;
255 1.5 augustss struct proc *p;
256 1.1 tron
257 1.1 tron /*
258 1.1 tron * We only check for valid parameters and return afterwards.
259 1.1 tron */
260 1.1 tron
261 1.1 tron if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL)
262 1.1 tron return EINVAL;
263 1.1 tron
264 1.1 tron error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
265 1.1 tron if (error)
266 1.1 tron return error;
267 1.1 tron
268 1.1 tron if (SCARG(uap, pid) != 0) {
269 1.5 augustss struct pcred *pc = cp->p_cred;
270 1.1 tron
271 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
272 1.1 tron return ESRCH;
273 1.1 tron if (!(cp == p ||
274 1.1 tron pc->pc_ucred->cr_uid == 0 ||
275 1.1 tron pc->p_ruid == p->p_cred->p_ruid ||
276 1.1 tron pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
277 1.1 tron pc->p_ruid == p->p_ucred->cr_uid ||
278 1.1 tron pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
279 1.1 tron return EPERM;
280 1.1 tron }
281 1.1 tron
282 1.1 tron /*
283 1.1 tron * We can't emulate anything put the default scheduling policy.
284 1.1 tron */
285 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER || lp.sched_priority != 0)
286 1.1 tron return EINVAL;
287 1.1 tron
288 1.1 tron return 0;
289 1.1 tron }
290 1.1 tron
291 1.1 tron int
292 1.12 thorpej linux_sys_sched_getscheduler(cl, v, retval)
293 1.12 thorpej struct lwp *cl;
294 1.1 tron void *v;
295 1.1 tron register_t *retval;
296 1.1 tron {
297 1.1 tron struct linux_sys_sched_getscheduler_args /* {
298 1.1 tron syscallarg(linux_pid_t) pid;
299 1.1 tron } */ *uap = v;
300 1.12 thorpej struct proc *cp = cl->l_proc;
301 1.5 augustss struct proc *p;
302 1.1 tron
303 1.1 tron *retval = -1;
304 1.1 tron /*
305 1.1 tron * We only check for valid parameters and return afterwards.
306 1.1 tron */
307 1.1 tron
308 1.1 tron if (SCARG(uap, pid) != 0) {
309 1.5 augustss struct pcred *pc = cp->p_cred;
310 1.1 tron
311 1.1 tron if ((p = pfind(SCARG(uap, pid))) == NULL)
312 1.1 tron return ESRCH;
313 1.1 tron if (!(cp == p ||
314 1.1 tron pc->pc_ucred->cr_uid == 0 ||
315 1.1 tron pc->p_ruid == p->p_cred->p_ruid ||
316 1.1 tron pc->pc_ucred->cr_uid == p->p_cred->p_ruid ||
317 1.1 tron pc->p_ruid == p->p_ucred->cr_uid ||
318 1.1 tron pc->pc_ucred->cr_uid == p->p_ucred->cr_uid))
319 1.1 tron return EPERM;
320 1.1 tron }
321 1.1 tron
322 1.1 tron /*
323 1.1 tron * We can't emulate anything put the default scheduling policy.
324 1.1 tron */
325 1.1 tron *retval = LINUX_SCHED_OTHER;
326 1.1 tron return 0;
327 1.1 tron }
328 1.1 tron
329 1.1 tron int
330 1.12 thorpej linux_sys_sched_yield(cl, v, retval)
331 1.12 thorpej struct lwp *cl;
332 1.1 tron void *v;
333 1.1 tron register_t *retval;
334 1.1 tron {
335 1.11 gmcgarry
336 1.11 gmcgarry yield();
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_get_priority_max(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.1 tron struct linux_sys_sched_get_priority_max_args /* {
347 1.1 tron syscallarg(int) policy;
348 1.1 tron } */ *uap = v;
349 1.1 tron
350 1.1 tron /*
351 1.1 tron * We can't emulate anything put the default scheduling policy.
352 1.1 tron */
353 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
354 1.1 tron *retval = -1;
355 1.1 tron return EINVAL;
356 1.1 tron }
357 1.1 tron
358 1.1 tron *retval = 0;
359 1.1 tron return 0;
360 1.1 tron }
361 1.1 tron
362 1.1 tron int
363 1.12 thorpej linux_sys_sched_get_priority_min(cl, v, retval)
364 1.12 thorpej struct lwp *cl;
365 1.1 tron void *v;
366 1.1 tron register_t *retval;
367 1.1 tron {
368 1.1 tron struct linux_sys_sched_get_priority_min_args /* {
369 1.1 tron syscallarg(int) policy;
370 1.1 tron } */ *uap = v;
371 1.1 tron
372 1.1 tron /*
373 1.1 tron * We can't emulate anything put the default scheduling policy.
374 1.1 tron */
375 1.1 tron if (SCARG(uap, policy) != LINUX_SCHED_OTHER) {
376 1.1 tron *retval = -1;
377 1.1 tron return EINVAL;
378 1.1 tron }
379 1.1 tron
380 1.1 tron *retval = 0;
381 1.1 tron return 0;
382 1.1 tron }
383 1.14 jdolecek
384 1.14 jdolecek #ifndef __m68k__
385 1.14 jdolecek /* Present on everything but m68k */
386 1.14 jdolecek int
387 1.14 jdolecek linux_sys_exit_group(l, v, retval)
388 1.14 jdolecek struct lwp *l;
389 1.14 jdolecek void *v;
390 1.14 jdolecek register_t *retval;
391 1.14 jdolecek {
392 1.14 jdolecek struct linux_sys_exit_group_args /* {
393 1.14 jdolecek syscallarg(int) error_code;
394 1.14 jdolecek } */ *uap = v;
395 1.14 jdolecek
396 1.14 jdolecek /*
397 1.14 jdolecek * XXX The calling thread is supposed to kill all threads
398 1.14 jdolecek * in the same thread group (i.e. all threads created
399 1.14 jdolecek * via clone(2) with CLONE_THREAD flag set). This appears
400 1.14 jdolecek * to not be used yet, so the thread group handling
401 1.18 wiz * is currently not implemented.
402 1.14 jdolecek */
403 1.14 jdolecek
404 1.14 jdolecek exit1(l, W_EXITCODE(SCARG(uap, error_code), 0));
405 1.14 jdolecek /* NOTREACHED */
406 1.15 tron return 0;
407 1.14 jdolecek }
408 1.14 jdolecek #endif /* !__m68k__ */
409 1.19 manu
410 1.19 manu #ifdef __amd64__
411 1.19 manu int
412 1.19 manu linux_sys_set_tid_address(l, v, retval)
413 1.19 manu struct lwp *l;
414 1.19 manu void *v;
415 1.19 manu register_t *retval;
416 1.19 manu {
417 1.19 manu struct linux_sys_set_tid_address_args /* {
418 1.19 manu syscallarg(int *) tidptr;
419 1.19 manu } */ *uap = v;
420 1.19 manu struct linux_emuldata *led;
421 1.19 manu
422 1.19 manu led = (struct linux_emuldata *)l->l_proc->p_emuldata;
423 1.19 manu led->clear_tid = SCARG(uap, tid);
424 1.19 manu
425 1.19 manu *retval = l->l_proc->p_pid;
426 1.19 manu
427 1.19 manu return 0;
428 1.19 manu }
429 1.20 manu
430 1.20 manu /* ARGUSED1 */
431 1.20 manu int
432 1.20 manu linux_sys_gettid(l, v, retval)
433 1.20 manu struct lwp *l;
434 1.20 manu void *v;
435 1.20 manu register_t *retval;
436 1.20 manu {
437 1.20 manu *retval = l->l_proc->p_pid;
438 1.20 manu return 0;
439 1.20 manu }
440 1.19 manu #endif /* __amd64__ */
441