linux_sched.c revision 1.61.2.1 1 /* $NetBSD: linux_sched.c,v 1.61.2.1 2010/08/17 06:45:49 uebayasi 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Linux compatibility module. Try to deal with scheduler related syscalls.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: linux_sched.c,v 1.61.2.1 2010/08/17 06:45:49 uebayasi Exp $");
39
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/proc.h>
43 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/malloc.h>
46 #include <sys/syscallargs.h>
47 #include <sys/wait.h>
48 #include <sys/kauth.h>
49 #include <sys/ptrace.h>
50 #include <sys/atomic.h>
51
52 #include <sys/cpu.h>
53
54 #include <compat/linux/common/linux_types.h>
55 #include <compat/linux/common/linux_signal.h>
56 #include <compat/linux/common/linux_emuldata.h>
57 #include <compat/linux/common/linux_ipc.h>
58 #include <compat/linux/common/linux_sem.h>
59 #include <compat/linux/common/linux_exec.h>
60 #include <compat/linux/common/linux_machdep.h>
61
62 #include <compat/linux/linux_syscallargs.h>
63
64 #include <compat/linux/common/linux_sched.h>
65
66 static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *, register_t *);
67
68 static void
69 linux_child_return(void *arg)
70 {
71 struct lwp *l = arg;
72 struct proc *p = l->l_proc;
73 struct linux_emuldata *led = l->l_emuldata;
74 void *ctp = led->led_child_tidptr;
75
76 if (ctp) {
77 if (copyout(&p->p_pid, ctp, sizeof(p->p_pid)) != 0)
78 printf("%s: LINUX_CLONE_CHILD_SETTID "
79 "failed (child_tidptr = %p, tid = %d)\n",
80 __func__, ctp, p->p_pid);
81 }
82 child_return(arg);
83 }
84
85 int
86 linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
87 {
88 /* {
89 syscallarg(int) flags;
90 syscallarg(void *) stack;
91 syscallarg(void *) parent_tidptr;
92 syscallarg(void *) tls;
93 syscallarg(void *) child_tidptr;
94 } */
95 struct proc *p;
96 struct linux_emuldata *led;
97 int flags, sig, error;
98
99 /*
100 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
101 */
102 if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
103 return (EINVAL);
104
105 /*
106 * Thread group implies shared signals. Shared signals
107 * imply shared VM. This matches what Linux kernel does.
108 */
109 if (SCARG(uap, flags) & LINUX_CLONE_THREAD
110 && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0)
111 return (EINVAL);
112 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND
113 && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0)
114 return (EINVAL);
115
116 /*
117 * The thread group flavor is implemented totally differently.
118 */
119 if (SCARG(uap, flags) & LINUX_CLONE_THREAD) {
120 return linux_clone_nptl(l, uap, retval);
121 }
122
123 flags = 0;
124 if (SCARG(uap, flags) & LINUX_CLONE_VM)
125 flags |= FORK_SHAREVM;
126 if (SCARG(uap, flags) & LINUX_CLONE_FS)
127 flags |= FORK_SHARECWD;
128 if (SCARG(uap, flags) & LINUX_CLONE_FILES)
129 flags |= FORK_SHAREFILES;
130 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
131 flags |= FORK_SHARESIGS;
132 if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
133 flags |= FORK_PPWAIT;
134
135 sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
136 if (sig < 0 || sig >= LINUX__NSIG)
137 return (EINVAL);
138 sig = linux_to_native_signo[sig];
139
140 if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) {
141 led = l->l_emuldata;
142 led->led_child_tidptr = SCARG(uap, child_tidptr);
143 }
144
145 /*
146 * Note that Linux does not provide a portable way of specifying
147 * the stack area; the caller must know if the stack grows up
148 * or down. So, we pass a stack size of 0, so that the code
149 * that makes this adjustment is a noop.
150 */
151 if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0,
152 linux_child_return, NULL, retval, &p)) != 0)
153 return error;
154
155 return 0;
156 }
157
158 static int
159 linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval)
160 {
161 /* {
162 syscallarg(int) flags;
163 syscallarg(void *) stack;
164 syscallarg(void *) parent_tidptr;
165 syscallarg(void *) tls;
166 syscallarg(void *) child_tidptr;
167 } */
168 struct proc *p;
169 struct lwp *l2;
170 struct linux_emuldata *led;
171 void *parent_tidptr, *tls, *child_tidptr;
172 struct schedstate_percpu *spc;
173 vaddr_t uaddr;
174 lwpid_t lid;
175 int flags, tnprocs, error;
176
177 p = l->l_proc;
178 flags = SCARG(uap, flags);
179 parent_tidptr = SCARG(uap, parent_tidptr);
180 tls = SCARG(uap, tls);
181 child_tidptr = SCARG(uap, child_tidptr);
182
183 tnprocs = atomic_inc_uint_nv(&nprocs);
184 if (__predict_false(tnprocs >= maxproc) ||
185 kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p,
186 KAUTH_ARG(tnprocs), NULL, NULL) != 0) {
187 atomic_dec_uint(&nprocs);
188 return EAGAIN;
189 }
190
191 uaddr = uvm_uarea_alloc();
192 if (__predict_false(uaddr == 0)) {
193 atomic_dec_uint(&nprocs);
194 return ENOMEM;
195 }
196
197 error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID,
198 SCARG(uap, stack), 0, child_return, NULL, &l2,
199 l->l_class);
200 if (__predict_false(error)) {
201 atomic_dec_uint(&nprocs);
202 uvm_uarea_free(uaddr);
203 return error;
204 }
205 lid = l2->l_lid;
206
207 /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */
208 if (flags & LINUX_CLONE_CHILD_CLEARTID) {
209 led = l2->l_emuldata;
210 led->led_clear_tid = child_tidptr;
211 }
212
213 /* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */
214 if (flags & LINUX_CLONE_PARENT_SETTID) {
215 if (copyout(&lid, parent_tidptr, sizeof(lid)) != 0)
216 printf("%s: LINUX_CLONE_PARENT_SETTID "
217 "failed (parent_tidptr = %p tid = %d)\n",
218 __func__, parent_tidptr, lid);
219 }
220
221 /* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory */
222 if (flags & LINUX_CLONE_CHILD_SETTID) {
223 if (copyout(&lid, child_tidptr, sizeof(lid)) != 0)
224 printf("%s: LINUX_CLONE_CHILD_SETTID "
225 "failed (child_tidptr = %p, tid = %d)\n",
226 __func__, child_tidptr, lid);
227 }
228
229 if (flags & LINUX_CLONE_SETTLS) {
230 error = LINUX_LWP_SETPRIVATE(l2, tls);
231 if (error) {
232 lwp_exit(l2);
233 return error;
234 }
235 }
236
237 /*
238 * Set the new LWP running, unless the process is stopping,
239 * then the LWP is created stopped.
240 */
241 mutex_enter(p->p_lock);
242 lwp_lock(l2);
243 spc = &l2->l_cpu->ci_schedstate;
244 if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
245 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
246 KASSERT(l2->l_wchan == NULL);
247 l2->l_stat = LSSTOP;
248 p->p_nrlwps--;
249 lwp_unlock_to(l2, spc->spc_lwplock);
250 } else {
251 KASSERT(lwp_locked(l2, spc->spc_mutex));
252 l2->l_stat = LSRUN;
253 sched_enqueue(l2, false);
254 lwp_unlock(l2);
255 }
256 } else {
257 l2->l_stat = LSSUSPENDED;
258 p->p_nrlwps--;
259 lwp_unlock_to(l2, spc->spc_lwplock);
260 }
261 mutex_exit(p->p_lock);
262
263 retval[0] = lid;
264 retval[1] = 0;
265 return 0;
266 }
267
268 /*
269 * linux realtime priority
270 *
271 * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99].
272 *
273 * - SCHED_OTHER tasks don't have realtime priorities.
274 * in particular, sched_param::sched_priority is always 0.
275 */
276
277 #define LINUX_SCHED_RTPRIO_MIN 1
278 #define LINUX_SCHED_RTPRIO_MAX 99
279
280 static int
281 sched_linux2native(int linux_policy, struct linux_sched_param *linux_params,
282 int *native_policy, struct sched_param *native_params)
283 {
284
285 switch (linux_policy) {
286 case LINUX_SCHED_OTHER:
287 if (native_policy != NULL) {
288 *native_policy = SCHED_OTHER;
289 }
290 break;
291
292 case LINUX_SCHED_FIFO:
293 if (native_policy != NULL) {
294 *native_policy = SCHED_FIFO;
295 }
296 break;
297
298 case LINUX_SCHED_RR:
299 if (native_policy != NULL) {
300 *native_policy = SCHED_RR;
301 }
302 break;
303
304 default:
305 return EINVAL;
306 }
307
308 if (linux_params != NULL) {
309 int prio = linux_params->sched_priority;
310
311 KASSERT(native_params != NULL);
312
313 if (linux_policy == LINUX_SCHED_OTHER) {
314 if (prio != 0) {
315 return EINVAL;
316 }
317 native_params->sched_priority = PRI_NONE; /* XXX */
318 } else {
319 if (prio < LINUX_SCHED_RTPRIO_MIN ||
320 prio > LINUX_SCHED_RTPRIO_MAX) {
321 return EINVAL;
322 }
323 native_params->sched_priority =
324 (prio - LINUX_SCHED_RTPRIO_MIN)
325 * (SCHED_PRI_MAX - SCHED_PRI_MIN)
326 / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
327 + SCHED_PRI_MIN;
328 }
329 }
330
331 return 0;
332 }
333
334 static int
335 sched_native2linux(int native_policy, struct sched_param *native_params,
336 int *linux_policy, struct linux_sched_param *linux_params)
337 {
338
339 switch (native_policy) {
340 case SCHED_OTHER:
341 if (linux_policy != NULL) {
342 *linux_policy = LINUX_SCHED_OTHER;
343 }
344 break;
345
346 case SCHED_FIFO:
347 if (linux_policy != NULL) {
348 *linux_policy = LINUX_SCHED_FIFO;
349 }
350 break;
351
352 case SCHED_RR:
353 if (linux_policy != NULL) {
354 *linux_policy = LINUX_SCHED_RR;
355 }
356 break;
357
358 default:
359 panic("%s: unknown policy %d\n", __func__, native_policy);
360 }
361
362 if (native_params != NULL) {
363 int prio = native_params->sched_priority;
364
365 KASSERT(prio >= SCHED_PRI_MIN);
366 KASSERT(prio <= SCHED_PRI_MAX);
367 KASSERT(linux_params != NULL);
368
369 #ifdef DEBUG_LINUX
370 printf("native2linux: native: policy %d, priority %d\n",
371 native_policy, prio);
372 #endif
373
374 if (native_policy == SCHED_OTHER) {
375 linux_params->sched_priority = 0;
376 } else {
377 linux_params->sched_priority =
378 (prio - SCHED_PRI_MIN)
379 * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN)
380 / (SCHED_PRI_MAX - SCHED_PRI_MIN)
381 + LINUX_SCHED_RTPRIO_MIN;
382 }
383 #ifdef DEBUG_LINUX
384 printf("native2linux: linux: policy %d, priority %d\n",
385 -1, linux_params->sched_priority);
386 #endif
387 }
388
389 return 0;
390 }
391
392 int
393 linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval)
394 {
395 /* {
396 syscallarg(linux_pid_t) pid;
397 syscallarg(const struct linux_sched_param *) sp;
398 } */
399 int error, policy;
400 struct linux_sched_param lp;
401 struct sched_param sp;
402
403 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
404 error = EINVAL;
405 goto out;
406 }
407
408 error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
409 if (error)
410 goto out;
411
412 /* We need the current policy in Linux terms. */
413 error = do_sched_getparam(0, SCARG(uap, pid), &policy, NULL);
414 if (error)
415 goto out;
416 error = sched_native2linux(policy, NULL, &policy, NULL);
417 if (error)
418 goto out;
419
420 error = sched_linux2native(policy, &lp, &policy, &sp);
421 if (error)
422 goto out;
423
424 error = do_sched_setparam(0, SCARG(uap, pid), policy, &sp);
425 if (error)
426 goto out;
427
428 out:
429 return error;
430 }
431
432 int
433 linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval)
434 {
435 /* {
436 syscallarg(linux_pid_t) pid;
437 syscallarg(struct linux_sched_param *) sp;
438 } */
439 struct linux_sched_param lp;
440 struct sched_param sp;
441 int error, policy;
442
443 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
444 error = EINVAL;
445 goto out;
446 }
447
448 error = do_sched_getparam(0, SCARG(uap, pid), &policy, &sp);
449 if (error)
450 goto out;
451 #ifdef DEBUG_LINUX
452 printf("getparam: native: policy %d, priority %d\n",
453 policy, sp.sched_priority);
454 #endif
455
456 error = sched_native2linux(policy, &sp, NULL, &lp);
457 if (error)
458 goto out;
459 #ifdef DEBUG_LINUX
460 printf("getparam: linux: policy %d, priority %d\n",
461 policy, lp.sched_priority);
462 #endif
463
464 error = copyout(&lp, SCARG(uap, sp), sizeof(lp));
465 if (error)
466 goto out;
467
468 out:
469 return error;
470 }
471
472 int
473 linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval)
474 {
475 /* {
476 syscallarg(linux_pid_t) pid;
477 syscallarg(int) policy;
478 syscallarg(cont struct linux_sched_param *) sp;
479 } */
480 int error, policy;
481 struct linux_sched_param lp;
482 struct sched_param sp;
483
484 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) {
485 error = EINVAL;
486 goto out;
487 }
488
489 error = copyin(SCARG(uap, sp), &lp, sizeof(lp));
490 if (error)
491 goto out;
492 #ifdef DEBUG_LINUX
493 printf("setscheduler: linux: policy %d, priority %d\n",
494 SCARG(uap, policy), lp.sched_priority);
495 #endif
496
497 error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp);
498 if (error)
499 goto out;
500 #ifdef DEBUG_LINUX
501 printf("setscheduler: native: policy %d, priority %d\n",
502 policy, sp.sched_priority);
503 #endif
504
505 error = do_sched_setparam(0, SCARG(uap, pid), policy, &sp);
506 if (error)
507 goto out;
508
509 out:
510 return error;
511 }
512
513 int
514 linux_sys_sched_getscheduler(struct lwp *l, const struct linux_sys_sched_getscheduler_args *uap, register_t *retval)
515 {
516 /* {
517 syscallarg(linux_pid_t) pid;
518 } */
519 int error, policy;
520
521 *retval = -1;
522
523 error = do_sched_getparam(0, SCARG(uap, pid), &policy, NULL);
524 if (error)
525 goto out;
526
527 error = sched_native2linux(policy, NULL, &policy, NULL);
528 if (error)
529 goto out;
530
531 *retval = policy;
532
533 out:
534 return error;
535 }
536
537 int
538 linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval)
539 {
540
541 yield();
542 return 0;
543 }
544
545 int
546 linux_sys_sched_get_priority_max(struct lwp *l, const struct linux_sys_sched_get_priority_max_args *uap, register_t *retval)
547 {
548 /* {
549 syscallarg(int) policy;
550 } */
551
552 switch (SCARG(uap, policy)) {
553 case LINUX_SCHED_OTHER:
554 *retval = 0;
555 break;
556 case LINUX_SCHED_FIFO:
557 case LINUX_SCHED_RR:
558 *retval = LINUX_SCHED_RTPRIO_MAX;
559 break;
560 default:
561 return EINVAL;
562 }
563
564 return 0;
565 }
566
567 int
568 linux_sys_sched_get_priority_min(struct lwp *l, const struct linux_sys_sched_get_priority_min_args *uap, register_t *retval)
569 {
570 /* {
571 syscallarg(int) policy;
572 } */
573
574 switch (SCARG(uap, policy)) {
575 case LINUX_SCHED_OTHER:
576 *retval = 0;
577 break;
578 case LINUX_SCHED_FIFO:
579 case LINUX_SCHED_RR:
580 *retval = LINUX_SCHED_RTPRIO_MIN;
581 break;
582 default:
583 return EINVAL;
584 }
585
586 return 0;
587 }
588
589 int
590 linux_sys_exit(struct lwp *l, const struct linux_sys_exit_args *uap, register_t *retval)
591 {
592
593 lwp_exit(l);
594 return 0;
595 }
596
597 #ifndef __m68k__
598 /* Present on everything but m68k */
599 int
600 linux_sys_exit_group(struct lwp *l, const struct linux_sys_exit_group_args *uap, register_t *retval)
601 {
602
603 return sys_exit(l, (const void *)uap, retval);
604 }
605 #endif /* !__m68k__ */
606
607 int
608 linux_sys_set_tid_address(struct lwp *l, const struct linux_sys_set_tid_address_args *uap, register_t *retval)
609 {
610 /* {
611 syscallarg(int *) tidptr;
612 } */
613 struct linux_emuldata *led;
614
615 led = (struct linux_emuldata *)l->l_emuldata;
616 led->led_clear_tid = SCARG(uap, tid);
617 *retval = l->l_lid;
618
619 return 0;
620 }
621
622 /* ARGUSED1 */
623 int
624 linux_sys_gettid(struct lwp *l, const void *v, register_t *retval)
625 {
626
627 *retval = l->l_lid;
628 return 0;
629 }
630
631 int
632 linux_sys_sched_getaffinity(struct lwp *l, const struct linux_sys_sched_getaffinity_args *uap, register_t *retval)
633 {
634 /* {
635 syscallarg(linux_pid_t) pid;
636 syscallarg(unsigned int) len;
637 syscallarg(unsigned long *) mask;
638 } */
639 proc_t *p;
640 unsigned long *lp, *data;
641 int error, size, nb = ncpu;
642
643 /* Unlike Linux, dynamically calculate cpu mask size */
644 size = sizeof(long) * ((ncpu + LONG_BIT - 1) / LONG_BIT);
645 if (SCARG(uap, len) < size)
646 return EINVAL;
647
648 /* XXX: Pointless check. TODO: Actually implement this. */
649 mutex_enter(proc_lock);
650 p = proc_find(SCARG(uap, pid));
651 mutex_exit(proc_lock);
652 if (p == NULL) {
653 return ESRCH;
654 }
655
656 /*
657 * return the actual number of CPU, tag all of them as available
658 * The result is a mask, the first CPU being in the least significant
659 * bit.
660 */
661 data = kmem_zalloc(size, KM_SLEEP);
662 lp = data;
663 while (nb > LONG_BIT) {
664 *lp++ = ~0UL;
665 nb -= LONG_BIT;
666 }
667 if (nb)
668 *lp = (1 << ncpu) - 1;
669
670 error = copyout(data, SCARG(uap, mask), size);
671 kmem_free(data, size);
672 *retval = size;
673 return error;
674 }
675
676 int
677 linux_sys_sched_setaffinity(struct lwp *l, const struct linux_sys_sched_setaffinity_args *uap, register_t *retval)
678 {
679 /* {
680 syscallarg(linux_pid_t) pid;
681 syscallarg(unsigned int) len;
682 syscallarg(unsigned long *) mask;
683 } */
684 proc_t *p;
685
686 /* XXX: Pointless check. TODO: Actually implement this. */
687 mutex_enter(proc_lock);
688 p = proc_find(SCARG(uap, pid));
689 mutex_exit(proc_lock);
690 if (p == NULL) {
691 return ESRCH;
692 }
693
694 /* Let's ignore it */
695 #ifdef DEBUG_LINUX
696 printf("linux_sys_sched_setaffinity\n");
697 #endif
698 return 0;
699 };
700