1/* $NetBSD: linux_sched.c,v 1.64 2011/06/05 08:42:59 dsl 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.64 2011/06/05 08:42:59 dsl 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 66static int linux_clone_nptl(struct lwp *, const struct linux_sys_clone_args *, 67 register_t *); 68 69#if DEBUG_LINUX 70#define DPRINTF(x) uprintf x 71#else 72#define DPRINTF(x) 73#endif 74 75static void 76linux_child_return(void *arg) 77{ 78 struct lwp *l = arg; 79 struct proc *p = l->l_proc; 80 struct linux_emuldata *led = l->l_emuldata; 81 void *ctp = led->led_child_tidptr; 82 int error; 83 84 if (ctp) { 85 if ((error = copyout(&p->p_pid, ctp, sizeof(p->p_pid))) != 0) 86 printf("%s: LINUX_CLONE_CHILD_SETTID " 87 "failed (child_tidptr = %p, tid = %d error =%d)\n", 88 __func__, ctp, p->p_pid, error); 89 } 90 child_return(arg); 91} 92 93int 94linux_sys_clone(struct lwp *l, const struct linux_sys_clone_args *uap, 95 register_t *retval) 96{ 97 /* { 98 syscallarg(int) flags; 99 syscallarg(void *) stack; 100 syscallarg(void *) parent_tidptr; 101 syscallarg(void *) tls; 102 syscallarg(void *) child_tidptr; 103 } */ 104 struct proc *p; 105 struct linux_emuldata *led; 106 int flags, sig, error; 107 108 /* 109 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags. 110 */ 111 if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE)) 112 return EINVAL; 113 114 /* 115 * Thread group implies shared signals. Shared signals 116 * imply shared VM. This matches what Linux kernel does. 117 */ 118 if (SCARG(uap, flags) & LINUX_CLONE_THREAD 119 && (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) == 0) 120 return EINVAL; 121 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND 122 && (SCARG(uap, flags) & LINUX_CLONE_VM) == 0) 123 return EINVAL; 124 125 /* 126 * The thread group flavor is implemented totally differently. 127 */ 128 if (SCARG(uap, flags) & LINUX_CLONE_THREAD) 129 return linux_clone_nptl(l, uap, retval); 130 131 flags = 0; 132 if (SCARG(uap, flags) & LINUX_CLONE_VM) 133 flags |= FORK_SHAREVM; 134 if (SCARG(uap, flags) & LINUX_CLONE_FS) 135 flags |= FORK_SHARECWD; 136 if (SCARG(uap, flags) & LINUX_CLONE_FILES) 137 flags |= FORK_SHAREFILES; 138 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND) 139 flags |= FORK_SHARESIGS; 140 if (SCARG(uap, flags) & LINUX_CLONE_VFORK) 141 flags |= FORK_PPWAIT; 142 143 sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL; 144 if (sig < 0 || sig >= LINUX__NSIG) 145 return EINVAL; 146 sig = linux_to_native_signo[sig]; 147 148 if (SCARG(uap, flags) & LINUX_CLONE_CHILD_SETTID) { 149 led = l->l_emuldata; 150 led->led_child_tidptr = SCARG(uap, child_tidptr); 151 } 152 153 /* 154 * Note that Linux does not provide a portable way of specifying 155 * the stack area; the caller must know if the stack grows up 156 * or down. So, we pass a stack size of 0, so that the code 157 * that makes this adjustment is a noop. 158 */ 159 if ((error = fork1(l, flags, sig, SCARG(uap, stack), 0, 160 linux_child_return, NULL, retval, &p)) != 0) { 161 DPRINTF(("%s: fork1: error %d\n", __func__, error)); 162 return error; 163 } 164 165 return 0; 166} 167 168static int 169linux_clone_nptl(struct lwp *l, const struct linux_sys_clone_args *uap, register_t *retval) 170{ 171 /* { 172 syscallarg(int) flags; 173 syscallarg(void *) stack; 174 syscallarg(void *) parent_tidptr; 175 syscallarg(void *) tls; 176 syscallarg(void *) child_tidptr; 177 } */ 178 struct proc *p; 179 struct lwp *l2; 180 struct linux_emuldata *led; 181 void *parent_tidptr, *tls, *child_tidptr; 182 struct schedstate_percpu *spc; 183 vaddr_t uaddr; 184 lwpid_t lid; 185 int flags, tnprocs, error; 186 187 p = l->l_proc; 188 flags = SCARG(uap, flags); 189 parent_tidptr = SCARG(uap, parent_tidptr); 190 tls = SCARG(uap, tls); 191 child_tidptr = SCARG(uap, child_tidptr); 192 193 tnprocs = atomic_inc_uint_nv(&nprocs); 194 if (__predict_false(tnprocs >= maxproc) || 195 kauth_authorize_process(l->l_cred, KAUTH_PROCESS_FORK, p, 196 KAUTH_ARG(tnprocs), NULL, NULL) != 0) { 197 atomic_dec_uint(&nprocs); 198 return EAGAIN; 199 } 200 201 uaddr = uvm_uarea_alloc(); 202 if (__predict_false(uaddr == 0)) { 203 atomic_dec_uint(&nprocs); 204 return ENOMEM; 205 } 206 207 error = lwp_create(l, p, uaddr, LWP_DETACHED | LWP_PIDLID, 208 SCARG(uap, stack), 0, child_return, NULL, &l2, l->l_class); 209 if (__predict_false(error)) { 210 DPRINTF(("%s: lwp_create error=%d\n", __func__, error)); 211 atomic_dec_uint(&nprocs); 212 uvm_uarea_free(uaddr); 213 return error; 214 } 215 lid = l2->l_lid; 216 217 /* LINUX_CLONE_CHILD_CLEARTID: clear TID in child's memory on exit() */ 218 if (flags & LINUX_CLONE_CHILD_CLEARTID) { 219 led = l2->l_emuldata; 220 led->led_clear_tid = child_tidptr; 221 } 222 223 /* LINUX_CLONE_PARENT_SETTID: store child's TID in parent's memory */ 224 if (flags & LINUX_CLONE_PARENT_SETTID) { 225 if ((error = copyout(&lid, parent_tidptr, sizeof(lid))) != 0) 226 printf("%s: LINUX_CLONE_PARENT_SETTID " 227 "failed (parent_tidptr = %p tid = %d error=%d)\n", 228 __func__, parent_tidptr, lid, error); 229 } 230 231 /* LINUX_CLONE_CHILD_SETTID: store child's TID in child's memory */ 232 if (flags & LINUX_CLONE_CHILD_SETTID) { 233 if ((error = copyout(&lid, child_tidptr, sizeof(lid))) != 0) 234 printf("%s: LINUX_CLONE_CHILD_SETTID " 235 "failed (child_tidptr = %p, tid = %d error=%d)\n", 236 __func__, child_tidptr, lid, error); 237 } 238 239 if (flags & LINUX_CLONE_SETTLS) { 240 error = LINUX_LWP_SETPRIVATE(l2, tls); 241 if (error) { 242 DPRINTF(("%s: LINUX_LWP_SETPRIVATE %d\n", __func__, 243 error)); 244 lwp_exit(l2); 245 return error; 246 } 247 } 248 249 /* 250 * Set the new LWP running, unless the process is stopping, 251 * then the LWP is created stopped. 252 */ 253 mutex_enter(p->p_lock); 254 lwp_lock(l2); 255 spc = &l2->l_cpu->ci_schedstate; 256 if ((l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) { 257 if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) { 258 KASSERT(l2->l_wchan == NULL); 259 l2->l_stat = LSSTOP; 260 p->p_nrlwps--; 261 lwp_unlock_to(l2, spc->spc_lwplock); 262 } else { 263 KASSERT(lwp_locked(l2, spc->spc_mutex)); 264 l2->l_stat = LSRUN; 265 sched_enqueue(l2, false); 266 lwp_unlock(l2); 267 } 268 } else { 269 l2->l_stat = LSSUSPENDED; 270 p->p_nrlwps--; 271 lwp_unlock_to(l2, spc->spc_lwplock); 272 } 273 mutex_exit(p->p_lock); 274 275 retval[0] = lid; 276 retval[1] = 0; 277 return 0; 278} 279 280/* 281 * linux realtime priority 282 * 283 * - SCHED_RR and SCHED_FIFO tasks have priorities [1,99]. 284 * 285 * - SCHED_OTHER tasks don't have realtime priorities. 286 * in particular, sched_param::sched_priority is always 0. 287 */ 288 289#define LINUX_SCHED_RTPRIO_MIN 1 290#define LINUX_SCHED_RTPRIO_MAX 99 291 292static int 293sched_linux2native(int linux_policy, struct linux_sched_param *linux_params, 294 int *native_policy, struct sched_param *native_params) 295{ 296 297 switch (linux_policy) { 298 case LINUX_SCHED_OTHER: 299 if (native_policy != NULL) { 300 *native_policy = SCHED_OTHER; 301 } 302 break; 303 304 case LINUX_SCHED_FIFO: 305 if (native_policy != NULL) { 306 *native_policy = SCHED_FIFO; 307 } 308 break; 309 310 case LINUX_SCHED_RR: 311 if (native_policy != NULL) { 312 *native_policy = SCHED_RR; 313 } 314 break; 315 316 default: 317 return EINVAL; 318 } 319 320 if (linux_params != NULL) { 321 int prio = linux_params->sched_priority; 322 323 KASSERT(native_params != NULL); 324 325 if (linux_policy == LINUX_SCHED_OTHER) { 326 if (prio != 0) { 327 return EINVAL; 328 } 329 native_params->sched_priority = PRI_NONE; /* XXX */ 330 } else { 331 if (prio < LINUX_SCHED_RTPRIO_MIN || 332 prio > LINUX_SCHED_RTPRIO_MAX) { 333 return EINVAL; 334 } 335 native_params->sched_priority = 336 (prio - LINUX_SCHED_RTPRIO_MIN) 337 * (SCHED_PRI_MAX - SCHED_PRI_MIN) 338 / (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN) 339 + SCHED_PRI_MIN; 340 } 341 } 342 343 return 0; 344} 345 346static int 347sched_native2linux(int native_policy, struct sched_param *native_params, 348 int *linux_policy, struct linux_sched_param *linux_params) 349{ 350 351 switch (native_policy) { 352 case SCHED_OTHER: 353 if (linux_policy != NULL) { 354 *linux_policy = LINUX_SCHED_OTHER; 355 } 356 break; 357 358 case SCHED_FIFO: 359 if (linux_policy != NULL) { 360 *linux_policy = LINUX_SCHED_FIFO; 361 } 362 break; 363 364 case SCHED_RR: 365 if (linux_policy != NULL) { 366 *linux_policy = LINUX_SCHED_RR; 367 } 368 break; 369 370 default: 371 panic("%s: unknown policy %d\n", __func__, native_policy); 372 } 373 374 if (native_params != NULL) { 375 int prio = native_params->sched_priority; 376 377 KASSERT(prio >= SCHED_PRI_MIN); 378 KASSERT(prio <= SCHED_PRI_MAX); 379 KASSERT(linux_params != NULL); 380 381 DPRINTF(("%s: native: policy %d, priority %d\n", 382 __func__, native_policy, prio)); 383 384 if (native_policy == SCHED_OTHER) { 385 linux_params->sched_priority = 0; 386 } else { 387 linux_params->sched_priority = 388 (prio - SCHED_PRI_MIN) 389 * (LINUX_SCHED_RTPRIO_MAX - LINUX_SCHED_RTPRIO_MIN) 390 / (SCHED_PRI_MAX - SCHED_PRI_MIN) 391 + LINUX_SCHED_RTPRIO_MIN; 392 } 393 DPRINTF(("%s: linux: policy %d, priority %d\n", 394 __func__, -1, linux_params->sched_priority)); 395 } 396 397 return 0; 398} 399 400int 401linux_sys_sched_setparam(struct lwp *l, const struct linux_sys_sched_setparam_args *uap, register_t *retval) 402{ 403 /* { 404 syscallarg(linux_pid_t) pid; 405 syscallarg(const struct linux_sched_param *) sp; 406 } */ 407 int error, policy; 408 struct linux_sched_param lp; 409 struct sched_param sp; 410 411 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 412 error = EINVAL; 413 goto out; 414 } 415 416 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 417 if (error) 418 goto out; 419 420 /* We need the current policy in Linux terms. */ 421 error = do_sched_getparam(0, SCARG(uap, pid), &policy, NULL); 422 if (error) 423 goto out; 424 error = sched_native2linux(policy, NULL, &policy, NULL); 425 if (error) 426 goto out; 427 428 error = sched_linux2native(policy, &lp, &policy, &sp); 429 if (error) 430 goto out; 431 432 error = do_sched_setparam(0, SCARG(uap, pid), policy, &sp); 433 if (error) 434 goto out; 435 436 out: 437 return error; 438} 439 440int 441linux_sys_sched_getparam(struct lwp *l, const struct linux_sys_sched_getparam_args *uap, register_t *retval) 442{ 443 /* { 444 syscallarg(linux_pid_t) pid; 445 syscallarg(struct linux_sched_param *) sp; 446 } */ 447 struct linux_sched_param lp; 448 struct sched_param sp; 449 int error, policy; 450 451 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 452 error = EINVAL; 453 goto out; 454 } 455 456 error = do_sched_getparam(0, SCARG(uap, pid), &policy, &sp); 457 if (error) 458 goto out; 459 DPRINTF(("%s: native: policy %d, priority %d\n", 460 __func__, policy, sp.sched_priority)); 461 462 error = sched_native2linux(policy, &sp, NULL, &lp); 463 if (error) 464 goto out; 465 DPRINTF(("%s: linux: policy %d, priority %d\n", 466 __func__, policy, lp.sched_priority)); 467 468 error = copyout(&lp, SCARG(uap, sp), sizeof(lp)); 469 if (error) 470 goto out; 471 472 out: 473 return error; 474} 475 476int 477linux_sys_sched_setscheduler(struct lwp *l, const struct linux_sys_sched_setscheduler_args *uap, register_t *retval) 478{ 479 /* { 480 syscallarg(linux_pid_t) pid; 481 syscallarg(int) policy; 482 syscallarg(cont struct linux_sched_param *) sp; 483 } */ 484 int error, policy; 485 struct linux_sched_param lp; 486 struct sched_param sp; 487 488 if (SCARG(uap, pid) < 0 || SCARG(uap, sp) == NULL) { 489 error = EINVAL; 490 goto out; 491 } 492 493 error = copyin(SCARG(uap, sp), &lp, sizeof(lp)); 494 if (error) 495 goto out; 496 DPRINTF(("%s: linux: policy %d, priority %d\n", 497 __func__, SCARG(uap, policy), lp.sched_priority)); 498 499 error = sched_linux2native(SCARG(uap, policy), &lp, &policy, &sp); 500 if (error) 501 goto out; 502 DPRINTF(("%s: native: policy %d, priority %d\n", 503 __func__, policy, sp.sched_priority)); 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 513int 514linux_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 537int 538linux_sys_sched_yield(struct lwp *l, const void *v, register_t *retval) 539{ 540 541 yield(); 542 return 0; 543} 544 545int 546linux_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 567int 568linux_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 589int 590linux_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 */ 599int 600linux_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 607int 608linux_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 */ 623int 624linux_sys_gettid(struct lwp *l, const void *v, register_t *retval) 625{ 626 627 *retval = l->l_lid; 628 return 0; 629} 630 631int 632linux_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 676int 677linux_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 DPRINTF(("%s\n", __func__)); 696 return 0; 697} 698