1 /* $NetBSD: kern_lwp.c,v 1.6 2003/03/19 11:36:33 dsl Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nathan J. Williams. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/pool.h> 42 #include <sys/lock.h> 43 #include <sys/proc.h> 44 #include <sys/sa.h> 45 #include <sys/savar.h> 46 #include <sys/types.h> 47 #include <sys/ucontext.h> 48 #include <sys/resourcevar.h> 49 #include <sys/mount.h> 50 #include <sys/syscallargs.h> 51 52 #include <uvm/uvm_extern.h> 53 54 struct lwplist alllwp; 55 struct lwplist deadlwp; 56 struct lwplist zomblwp; 57 58 #define LWP_DEBUG 59 60 #ifdef LWP_DEBUG 61 int lwp_debug = 0; 62 #define DPRINTF(x) if (lwp_debug) printf x 63 #else 64 #define DPRINTF(x) 65 #endif 66 /* ARGSUSED */ 67 int 68 sys__lwp_create(struct lwp *l, void *v, register_t *retval) 69 { 70 struct sys__lwp_create_args /* { 71 syscallarg(const ucontext_t *) ucp; 72 syscallarg(u_long) flags; 73 syscallarg(lwpid_t *) new_lwp; 74 } */ *uap = v; 75 struct proc *p = l->l_proc; 76 struct lwp *l2; 77 vaddr_t uaddr; 78 boolean_t inmem; 79 ucontext_t *newuc; 80 int s, error; 81 82 newuc = pool_get(&lwp_uc_pool, PR_WAITOK); 83 84 error = copyin(SCARG(uap, ucp), newuc, sizeof(*newuc)); 85 if (error) 86 return (error); 87 88 /* XXX check against resource limits */ 89 90 inmem = uvm_uarea_alloc(&uaddr); 91 if (__predict_false(uaddr == 0)) { 92 return (ENOMEM); 93 } 94 95 /* XXX flags: 96 * __LWP_ASLWP is probably needed for Solaris compat. 97 */ 98 99 newlwp(l, p, uaddr, inmem, 100 SCARG(uap, flags) & LWP_DETACHED, 101 NULL, NULL, startlwp, newuc, &l2); 102 103 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) { 104 SCHED_LOCK(s); 105 l2->l_stat = LSRUN; 106 setrunqueue(l2); 107 SCHED_UNLOCK(s); 108 simple_lock(&p->p_lwplock); 109 p->p_nrlwps++; 110 simple_unlock(&p->p_lwplock); 111 } else { 112 l2->l_stat = LSSUSPENDED; 113 } 114 115 error = copyout(&l2->l_lid, SCARG(uap, new_lwp), 116 sizeof(l2->l_lid)); 117 if (error) 118 return (error); 119 120 return (0); 121 } 122 123 124 int 125 sys__lwp_exit(struct lwp *l, void *v, register_t *retval) 126 { 127 128 lwp_exit(l); 129 /* NOTREACHED */ 130 return (0); 131 } 132 133 134 int 135 sys__lwp_self(struct lwp *l, void *v, register_t *retval) 136 { 137 138 *retval = l->l_lid; 139 140 return (0); 141 } 142 143 144 int 145 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval) 146 { 147 148 *retval = (uintptr_t) l->l_private; 149 150 return (0); 151 } 152 153 154 int 155 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval) 156 { 157 struct sys__lwp_setprivate_args /* { 158 syscallarg(void *) ptr; 159 } */ *uap = v; 160 161 l->l_private = SCARG(uap, ptr); 162 163 return (0); 164 } 165 166 167 int 168 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval) 169 { 170 struct sys__lwp_suspend_args /* { 171 syscallarg(lwpid_t) target; 172 } */ *uap = v; 173 int target_lid; 174 struct proc *p = l->l_proc; 175 struct lwp *t, *t2; 176 int s; 177 178 target_lid = SCARG(uap, target); 179 180 LIST_FOREACH(t, &p->p_lwps, l_sibling) 181 if (t->l_lid == target_lid) 182 break; 183 184 if (t == NULL) 185 return (ESRCH); 186 187 if (t == l) { 188 /* 189 * Check for deadlock, which is only possible 190 * when we're suspending ourself. 191 */ 192 LIST_FOREACH(t2, &p->p_lwps, l_sibling) { 193 if ((t2 != l) && (t2->l_stat != LSSUSPENDED)) 194 break; 195 } 196 197 if (t2 == NULL) /* All other LWPs are suspended */ 198 return (EDEADLK); 199 200 SCHED_LOCK(s); 201 l->l_stat = LSSUSPENDED; 202 /* XXX NJWLWP check if this makes sense here: */ 203 l->l_proc->p_stats->p_ru.ru_nvcsw++; 204 mi_switch(l, NULL); 205 SCHED_ASSERT_UNLOCKED(); 206 splx(s); 207 } else { 208 switch (t->l_stat) { 209 case LSSUSPENDED: 210 return (0); /* _lwp_suspend() is idempotent */ 211 case LSRUN: 212 SCHED_LOCK(s); 213 remrunqueue(t); 214 t->l_stat = LSSUSPENDED; 215 SCHED_UNLOCK(s); 216 simple_lock(&p->p_lwplock); 217 p->p_nrlwps--; 218 simple_unlock(&p->p_lwplock); 219 break; 220 case LSSLEEP: 221 t->l_stat = LSSUSPENDED; 222 break; 223 case LSIDL: 224 case LSDEAD: 225 case LSZOMB: 226 return (EINTR); /* It's what Solaris does..... */ 227 case LSSTOP: 228 panic("_lwp_suspend: Stopped LWP in running process!"); 229 break; 230 case LSONPROC: 231 panic("XXX multiprocessor LWPs? Implement me!"); 232 break; 233 } 234 } 235 236 return (0); 237 } 238 239 240 int 241 sys__lwp_continue(struct lwp *l, void *v, register_t *retval) 242 { 243 struct sys__lwp_continue_args /* { 244 syscallarg(lwpid_t) target; 245 } */ *uap = v; 246 int target_lid; 247 struct proc *p = l->l_proc; 248 struct lwp *t; 249 250 target_lid = SCARG(uap, target); 251 252 LIST_FOREACH(t, &p->p_lwps, l_sibling) 253 if (t->l_lid == target_lid) 254 break; 255 256 if (t == NULL) 257 return (ESRCH); 258 259 lwp_continue(t); 260 261 return (0); 262 } 263 264 void 265 lwp_continue(struct lwp *l) 266 { 267 int s; 268 269 DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n", 270 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat, 271 l->l_wchan)); 272 273 if (l->l_stat != LSSUSPENDED) 274 return; 275 276 if (l->l_wchan == 0) { 277 /* LWP was runnable before being suspended. */ 278 SCHED_LOCK(s); 279 setrunnable(l); 280 SCHED_UNLOCK(s); 281 } else { 282 /* LWP was sleeping before being suspended. */ 283 l->l_stat = LSSLEEP; 284 } 285 } 286 287 int 288 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval) 289 { 290 struct sys__lwp_wakeup_args /* { 291 syscallarg(lwpid_t) wakeup; 292 } */ *uap = v; 293 lwpid_t target_lid; 294 struct lwp *t; 295 struct proc *p; 296 297 p = l->l_proc; 298 target_lid = SCARG(uap, target); 299 300 LIST_FOREACH(t, &p->p_lwps, l_sibling) 301 if (t->l_lid == target_lid) 302 break; 303 304 if (t == NULL) 305 return (ESRCH); 306 307 if (t->l_stat != LSSLEEP) 308 return (ENODEV); 309 310 if ((t->l_flag & L_SINTR) == 0) 311 return (EBUSY); 312 313 setrunnable(t); 314 315 return 0; 316 } 317 318 int 319 sys__lwp_wait(struct lwp *l, void *v, register_t *retval) 320 { 321 struct sys__lwp_wait_args /* { 322 syscallarg(lwpid_t) wait_for; 323 syscallarg(lwpid_t *) departed; 324 } */ *uap = v; 325 int error; 326 lwpid_t dep; 327 328 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0); 329 if (error) 330 return (error); 331 332 if (SCARG(uap, departed)) { 333 error = copyout(&dep, SCARG(uap, departed), 334 sizeof(dep)); 335 if (error) 336 return (error); 337 } 338 339 return (0); 340 } 341 342 343 int 344 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags) 345 { 346 347 struct proc *p = l->l_proc; 348 struct lwp *l2, *l3; 349 int nfound, error, s, wpri; 350 static char waitstr1[] = "lwpwait"; 351 static char waitstr2[] = "lwpwait2"; 352 353 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n", 354 p->p_pid, l->l_lid, lid)); 355 356 if (lid == l->l_lid) 357 return (EDEADLK); /* Waiting for ourselves makes no sense. */ 358 359 wpri = PWAIT | 360 ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : PCATCH); 361 loop: 362 nfound = 0; 363 LIST_FOREACH(l2, &p->p_lwps, l_sibling) { 364 if ((l2 == l) || (l2->l_flag & L_DETACHED) || 365 ((lid != 0) && (lid != l2->l_lid))) 366 continue; 367 368 nfound++; 369 if (l2->l_stat == LSZOMB) { 370 if (departed) 371 *departed = l2->l_lid; 372 373 s = proclist_lock_write(); 374 LIST_REMOVE(l2, l_zlist); /* off zomblwp */ 375 proclist_unlock_write(s); 376 377 simple_lock(&p->p_lwplock); 378 LIST_REMOVE(l2, l_sibling); 379 p->p_nlwps--; 380 p->p_nzlwps--; 381 simple_unlock(&p->p_lwplock); 382 /* XXX decrement limits */ 383 384 pool_put(&lwp_pool, l2); 385 386 return (0); 387 } else if (l2->l_stat == LSSLEEP || 388 l2->l_stat == LSSUSPENDED) { 389 /* Deadlock checks. 390 * 1. If all other LWPs are waiting for exits 391 * or suspended, we would deadlock. 392 */ 393 394 LIST_FOREACH(l3, &p->p_lwps, l_sibling) { 395 if (l3 != l && (l3->l_stat != LSSUSPENDED) && 396 !(l3->l_stat == LSSLEEP && 397 l3->l_wchan == (caddr_t) &p->p_nlwps)) 398 break; 399 } 400 if (l3 == NULL) /* Everyone else is waiting. */ 401 return (EDEADLK); 402 403 /* XXX we'd like to check for a cycle of waiting 404 * LWPs (specific LID waits, not any-LWP waits) 405 * and detect that sort of deadlock, but we don't 406 * have a good place to store the lwp that is 407 * being waited for. wchan is already filled with 408 * &p->p_nlwps, and putting the lwp address in 409 * there for deadlock tracing would require 410 * exiting LWPs to call wakeup on both their 411 * own address and &p->p_nlwps, to get threads 412 * sleeping on any LWP exiting. 413 * 414 * Revisit later. Maybe another auxillary 415 * storage location associated with sleeping 416 * is in order. 417 */ 418 } 419 } 420 421 if (nfound == 0) 422 return (ESRCH); 423 424 if ((error = tsleep((caddr_t) &p->p_nlwps, wpri, 425 (lid != 0) ? waitstr1 : waitstr2, 0)) != 0) 426 return (error); 427 428 goto loop; 429 } 430 431 432 int 433 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem, 434 int flags, void *stack, size_t stacksize, 435 void (*func)(void *), void *arg, struct lwp **rnewlwpp) 436 { 437 struct lwp *l2; 438 int s; 439 440 l2 = pool_get(&lwp_pool, PR_WAITOK); 441 442 l2->l_stat = LSIDL; 443 l2->l_forw = l2->l_back = NULL; 444 l2->l_proc = p2; 445 446 447 memset(&l2->l_startzero, 0, 448 (unsigned) ((caddr_t)&l2->l_endzero - 449 (caddr_t)&l2->l_startzero)); 450 memcpy(&l2->l_startcopy, &l1->l_startcopy, 451 (unsigned) ((caddr_t)&l2->l_endcopy - 452 (caddr_t)&l2->l_startcopy)); 453 454 #if !defined(MULTIPROCESSOR) 455 /* 456 * In the single-processor case, all processes will always run 457 * on the same CPU. So, initialize the child's CPU to the parent's 458 * now. In the multiprocessor case, the child's CPU will be 459 * initialized in the low-level context switch code when the 460 * process runs. 461 */ 462 KASSERT(l1->l_cpu != NULL); 463 l2->l_cpu = l1->l_cpu; 464 #else 465 /* 466 * zero child's cpu pointer so we don't get trash. 467 */ 468 l2->l_cpu = NULL; 469 #endif /* ! MULTIPROCESSOR */ 470 471 l2->l_flag = inmem ? L_INMEM : 0; 472 l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0; 473 474 callout_init(&l2->l_tsleep_ch); 475 476 if (rnewlwpp != NULL) 477 *rnewlwpp = l2; 478 479 l2->l_addr = (struct user *)uaddr; 480 uvm_lwp_fork(l1, l2, stack, stacksize, func, 481 (arg != NULL) ? arg : l2); 482 483 484 simple_lock(&p2->p_lwplock); 485 l2->l_lid = ++p2->p_nlwpid; 486 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling); 487 p2->p_nlwps++; 488 simple_unlock(&p2->p_lwplock); 489 490 /* XXX should be locked differently... */ 491 s = proclist_lock_write(); 492 LIST_INSERT_HEAD(&alllwp, l2, l_list); 493 proclist_unlock_write(s); 494 495 return (0); 496 } 497 498 499 /* 500 * Quit the process. This will call cpu_exit, which will call cpu_switch, 501 * so this can only be used meaningfully if you're willing to switch away. 502 * Calling with l!=curlwp would be weird. 503 */ 504 void 505 lwp_exit(struct lwp *l) 506 { 507 struct proc *p = l->l_proc; 508 int s; 509 510 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid)); 511 DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n", 512 p->p_nlwps, p->p_nrlwps, p->p_nzlwps)); 513 514 /* 515 * If we are the last live LWP in a process, we need to exit 516 * the entire process (if that's not already going on). We do 517 * so with an exit status of zero, because it's a "controlled" 518 * exit, and because that's what Solaris does. 519 */ 520 if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) { 521 DPRINTF(("lwp_exit: %d.%d calling exit1()\n", 522 p->p_pid, l->l_lid)); 523 exit1(l, 0); 524 } 525 526 s = proclist_lock_write(); 527 LIST_REMOVE(l, l_list); 528 if ((l->l_flag & L_DETACHED) == 0) { 529 DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid, 530 l->l_lid)); 531 LIST_INSERT_HEAD(&zomblwp, l, l_zlist); 532 } 533 proclist_unlock_write(s); 534 535 simple_lock(&p->p_lwplock); 536 p->p_nrlwps--; 537 simple_unlock(&p->p_lwplock); 538 539 l->l_stat = LSDEAD; 540 541 /* This LWP no longer needs to hold the kernel lock. */ 542 KERNEL_PROC_UNLOCK(l); 543 544 /* cpu_exit() will not return */ 545 cpu_exit(l, 0); 546 547 } 548 549 550 void 551 lwp_exit2(struct lwp *l) 552 { 553 554 simple_lock(&deadproc_slock); 555 LIST_INSERT_HEAD(&deadlwp, l, l_list); 556 simple_unlock(&deadproc_slock); 557 558 wakeup(&deadprocs); 559 } 560 561 /* 562 * Pick a LWP to represent the process for those operations which 563 * want information about a "process" that is actually associated 564 * with a LWP. 565 */ 566 struct lwp * 567 proc_representative_lwp(p) 568 struct proc *p; 569 { 570 struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended; 571 572 /* Trivial case: only one LWP */ 573 if (p->p_nlwps == 1) 574 return (LIST_FIRST(&p->p_lwps)); 575 576 switch (p->p_stat) { 577 case SSTOP: 578 case SACTIVE: 579 /* Pick the most live LWP */ 580 onproc = running = sleeping = stopped = suspended = NULL; 581 LIST_FOREACH(l, &p->p_lwps, l_sibling) { 582 switch (l->l_stat) { 583 case LSONPROC: 584 onproc = l; 585 break; 586 case LSRUN: 587 running = l; 588 break; 589 case LSSLEEP: 590 sleeping = l; 591 break; 592 case LSSTOP: 593 stopped = l; 594 break; 595 case LSSUSPENDED: 596 suspended = l; 597 break; 598 } 599 } 600 if (onproc) 601 return onproc; 602 if (running) 603 return running; 604 if (sleeping) 605 return sleeping; 606 if (stopped) 607 return stopped; 608 if (suspended) 609 return suspended; 610 break; 611 case SDEAD: 612 case SZOMB: 613 /* Doesn't really matter... */ 614 return (LIST_FIRST(&p->p_lwps)); 615 break; 616 #ifdef DIAGNOSTIC 617 case SIDL: 618 /* We have more than one LWP and we're in SIDL? 619 * How'd that happen? 620 */ 621 panic("Too many LWPs (%d) in SIDL process %d (%s)", 622 p->p_nrlwps, p->p_pid, p->p_comm); 623 default: 624 panic("Process %d (%s) in unknown state %d", 625 p->p_pid, p->p_comm, p->p_stat); 626 #endif 627 } 628 629 panic("proc_representative_lwp: couldn't find a lwp for process" 630 " %d (%s)", p->p_pid, p->p_comm); 631 /* NOTREACHED */ 632 return NULL; 633 } 634