kern_lwp.c revision 1.40 1 /* $NetBSD: kern_lwp.c,v 1.40 2006/08/14 14:11:21 ad 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/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.40 2006/08/14 14:11:21 ad Exp $");
41
42 #include "opt_multiprocessor.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/pool.h>
47 #include <sys/lock.h>
48 #include <sys/proc.h>
49 #include <sys/sa.h>
50 #include <sys/savar.h>
51 #include <sys/types.h>
52 #include <sys/ucontext.h>
53 #include <sys/resourcevar.h>
54 #include <sys/mount.h>
55 #include <sys/syscallargs.h>
56 #include <sys/kauth.h>
57
58 #include <uvm/uvm_extern.h>
59
60 struct lwplist alllwp;
61
62 #define LWP_DEBUG
63
64 #ifdef LWP_DEBUG
65 int lwp_debug = 0;
66 #define DPRINTF(x) if (lwp_debug) printf x
67 #else
68 #define DPRINTF(x)
69 #endif
70 /* ARGSUSED */
71 int
72 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
73 {
74 struct sys__lwp_create_args /* {
75 syscallarg(const ucontext_t *) ucp;
76 syscallarg(u_long) flags;
77 syscallarg(lwpid_t *) new_lwp;
78 } */ *uap = v;
79 struct proc *p = l->l_proc;
80 struct lwp *l2;
81 vaddr_t uaddr;
82 boolean_t inmem;
83 ucontext_t *newuc;
84 int s, error;
85
86 if (p->p_flag & P_SA)
87 return EINVAL;
88
89 newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
90
91 error = copyin(SCARG(uap, ucp), newuc,
92 l->l_proc->p_emul->e_sa->sae_ucsize);
93 if (error) {
94 pool_put(&lwp_uc_pool, newuc);
95 return (error);
96 }
97
98 /* XXX check against resource limits */
99
100 inmem = uvm_uarea_alloc(&uaddr);
101 if (__predict_false(uaddr == 0)) {
102 pool_put(&lwp_uc_pool, newuc);
103 return (ENOMEM);
104 }
105
106 /* XXX flags:
107 * __LWP_ASLWP is probably needed for Solaris compat.
108 */
109
110 newlwp(l, p, uaddr, inmem,
111 SCARG(uap, flags) & LWP_DETACHED,
112 NULL, 0, startlwp, newuc, &l2);
113
114 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
115 SCHED_LOCK(s);
116 l2->l_stat = LSRUN;
117 setrunqueue(l2);
118 p->p_nrlwps++;
119 SCHED_UNLOCK(s);
120 } else {
121 l2->l_stat = LSSUSPENDED;
122 }
123
124 error = copyout(&l2->l_lid, SCARG(uap, new_lwp),
125 sizeof(l2->l_lid));
126 if (error) {
127 /* XXX We should destroy the LWP. */
128 return (error);
129 }
130
131 return (0);
132 }
133
134
135 int
136 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
137 {
138
139 lwp_exit(l);
140 /* NOTREACHED */
141 return (0);
142 }
143
144
145 int
146 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
147 {
148
149 *retval = l->l_lid;
150
151 return (0);
152 }
153
154
155 int
156 sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
157 {
158
159 *retval = (uintptr_t) l->l_private;
160
161 return (0);
162 }
163
164
165 int
166 sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
167 {
168 struct sys__lwp_setprivate_args /* {
169 syscallarg(void *) ptr;
170 } */ *uap = v;
171
172 l->l_private = SCARG(uap, ptr);
173
174 return (0);
175 }
176
177
178 int
179 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
180 {
181 struct sys__lwp_suspend_args /* {
182 syscallarg(lwpid_t) target;
183 } */ *uap = v;
184 int target_lid;
185 struct proc *p = l->l_proc;
186 struct lwp *t;
187 struct lwp *t2;
188
189 if (p->p_flag & P_SA)
190 return EINVAL;
191
192 target_lid = SCARG(uap, target);
193
194 LIST_FOREACH(t, &p->p_lwps, l_sibling)
195 if (t->l_lid == target_lid)
196 break;
197
198 if (t == NULL)
199 return (ESRCH);
200
201 if (t == l) {
202 /*
203 * Check for deadlock, which is only possible
204 * when we're suspending ourself.
205 */
206 LIST_FOREACH(t2, &p->p_lwps, l_sibling) {
207 if ((t2 != l) && (t2->l_stat != LSSUSPENDED))
208 break;
209 }
210
211 if (t2 == NULL) /* All other LWPs are suspended */
212 return (EDEADLK);
213 }
214
215 return lwp_suspend(l, t);
216 }
217
218 inline int
219 lwp_suspend(struct lwp *l, struct lwp *t)
220 {
221 struct proc *p = t->l_proc;
222 int s;
223
224 if (t == l) {
225 SCHED_LOCK(s);
226 KASSERT(l->l_stat == LSONPROC);
227 l->l_stat = LSSUSPENDED;
228 p->p_nrlwps--;
229 /* XXX NJWLWP check if this makes sense here: */
230 p->p_stats->p_ru.ru_nvcsw++;
231 mi_switch(l, NULL);
232 SCHED_ASSERT_UNLOCKED();
233 splx(s);
234 } else {
235 switch (t->l_stat) {
236 case LSSUSPENDED:
237 return (0); /* _lwp_suspend() is idempotent */
238 case LSRUN:
239 SCHED_LOCK(s);
240 remrunqueue(t);
241 t->l_stat = LSSUSPENDED;
242 p->p_nrlwps--;
243 SCHED_UNLOCK(s);
244 break;
245 case LSSLEEP:
246 t->l_stat = LSSUSPENDED;
247 break;
248 case LSIDL:
249 case LSZOMB:
250 return (EINTR); /* It's what Solaris does..... */
251 case LSSTOP:
252 panic("_lwp_suspend: Stopped LWP in running process!");
253 break;
254 case LSONPROC:
255 /* XXX multiprocessor LWPs? Implement me! */
256 return (EINVAL);
257 }
258 }
259
260 return (0);
261 }
262
263
264 int
265 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
266 {
267 struct sys__lwp_continue_args /* {
268 syscallarg(lwpid_t) target;
269 } */ *uap = v;
270 int s, target_lid;
271 struct proc *p = l->l_proc;
272 struct lwp *t;
273
274 if (p->p_flag & P_SA)
275 return EINVAL;
276
277 target_lid = SCARG(uap, target);
278
279 LIST_FOREACH(t, &p->p_lwps, l_sibling)
280 if (t->l_lid == target_lid)
281 break;
282
283 if (t == NULL)
284 return (ESRCH);
285
286 SCHED_LOCK(s);
287 lwp_continue(t);
288 SCHED_UNLOCK(s);
289
290 return (0);
291 }
292
293 void
294 lwp_continue(struct lwp *l)
295 {
296
297 DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
298 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
299 l->l_wchan));
300
301 if (l->l_stat != LSSUSPENDED)
302 return;
303
304 if (l->l_wchan == 0) {
305 /* LWP was runnable before being suspended. */
306 setrunnable(l);
307 } else {
308 /* LWP was sleeping before being suspended. */
309 l->l_stat = LSSLEEP;
310 }
311 }
312
313 int
314 sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
315 {
316 struct sys__lwp_wakeup_args /* {
317 syscallarg(lwpid_t) target;
318 } */ *uap = v;
319 lwpid_t target_lid;
320 struct lwp *t;
321 struct proc *p;
322 int error;
323 int s;
324
325 p = l->l_proc;
326 target_lid = SCARG(uap, target);
327
328 SCHED_LOCK(s);
329
330 LIST_FOREACH(t, &p->p_lwps, l_sibling)
331 if (t->l_lid == target_lid)
332 break;
333
334 if (t == NULL) {
335 error = ESRCH;
336 goto exit;
337 }
338
339 if (t->l_stat != LSSLEEP) {
340 error = ENODEV;
341 goto exit;
342 }
343
344 if ((t->l_flag & L_SINTR) == 0) {
345 error = EBUSY;
346 goto exit;
347 }
348 /*
349 * Tell ltsleep to wakeup.
350 */
351 t->l_flag |= L_CANCELLED;
352
353 setrunnable(t);
354 error = 0;
355 exit:
356 SCHED_UNLOCK(s);
357
358 return error;
359 }
360
361 int
362 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
363 {
364 struct sys__lwp_wait_args /* {
365 syscallarg(lwpid_t) wait_for;
366 syscallarg(lwpid_t *) departed;
367 } */ *uap = v;
368 int error;
369 lwpid_t dep;
370
371 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
372 if (error)
373 return (error);
374
375 if (SCARG(uap, departed)) {
376 error = copyout(&dep, SCARG(uap, departed),
377 sizeof(dep));
378 if (error)
379 return (error);
380 }
381
382 return (0);
383 }
384
385
386 int
387 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
388 {
389 struct proc *p = l->l_proc;
390 struct lwp *l2, *l3;
391 int nfound, error, wpri;
392 static const char waitstr1[] = "lwpwait";
393 static const char waitstr2[] = "lwpwait2";
394
395 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
396 p->p_pid, l->l_lid, lid));
397
398 if (lid == l->l_lid)
399 return (EDEADLK); /* Waiting for ourselves makes no sense. */
400
401 wpri = PWAIT |
402 ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : PCATCH);
403 loop:
404 nfound = 0;
405 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
406 if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
407 ((lid != 0) && (lid != l2->l_lid)))
408 continue;
409
410 nfound++;
411 if (l2->l_stat == LSZOMB) {
412 if (departed)
413 *departed = l2->l_lid;
414
415 simple_lock(&p->p_lock);
416 LIST_REMOVE(l2, l_sibling);
417 p->p_nlwps--;
418 p->p_nzlwps--;
419 simple_unlock(&p->p_lock);
420 /* XXX decrement limits */
421
422 pool_put(&lwp_pool, l2);
423
424 return (0);
425 } else if (l2->l_stat == LSSLEEP ||
426 l2->l_stat == LSSUSPENDED) {
427 /* Deadlock checks.
428 * 1. If all other LWPs are waiting for exits
429 * or suspended, we would deadlock.
430 */
431
432 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
433 if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
434 !(l3->l_stat == LSSLEEP &&
435 l3->l_wchan == (caddr_t) &p->p_nlwps))
436 break;
437 }
438 if (l3 == NULL) /* Everyone else is waiting. */
439 return (EDEADLK);
440
441 /* XXX we'd like to check for a cycle of waiting
442 * LWPs (specific LID waits, not any-LWP waits)
443 * and detect that sort of deadlock, but we don't
444 * have a good place to store the lwp that is
445 * being waited for. wchan is already filled with
446 * &p->p_nlwps, and putting the lwp address in
447 * there for deadlock tracing would require
448 * exiting LWPs to call wakeup on both their
449 * own address and &p->p_nlwps, to get threads
450 * sleeping on any LWP exiting.
451 *
452 * Revisit later. Maybe another auxillary
453 * storage location associated with sleeping
454 * is in order.
455 */
456 }
457 }
458
459 if (nfound == 0)
460 return (ESRCH);
461
462 if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
463 (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
464 return (error);
465
466 goto loop;
467 }
468
469
470 int
471 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem,
472 int flags, void *stack, size_t stacksize,
473 void (*func)(void *), void *arg, struct lwp **rnewlwpp)
474 {
475 struct lwp *l2;
476 int s;
477
478 l2 = pool_get(&lwp_pool, PR_WAITOK);
479
480 l2->l_stat = LSIDL;
481 l2->l_forw = l2->l_back = NULL;
482 l2->l_proc = p2;
483
484 memset(&l2->l_startzero, 0,
485 (unsigned) ((caddr_t)&l2->l_endzero -
486 (caddr_t)&l2->l_startzero));
487 memcpy(&l2->l_startcopy, &l1->l_startcopy,
488 (unsigned) ((caddr_t)&l2->l_endcopy -
489 (caddr_t)&l2->l_startcopy));
490
491 #if !defined(MULTIPROCESSOR)
492 /*
493 * In the single-processor case, all processes will always run
494 * on the same CPU. So, initialize the child's CPU to the parent's
495 * now. In the multiprocessor case, the child's CPU will be
496 * initialized in the low-level context switch code when the
497 * process runs.
498 */
499 KASSERT(l1->l_cpu != NULL);
500 l2->l_cpu = l1->l_cpu;
501 #else
502 /*
503 * zero child's CPU pointer so we don't get trash.
504 */
505 l2->l_cpu = NULL;
506 #endif /* ! MULTIPROCESSOR */
507
508 l2->l_flag = inmem ? L_INMEM : 0;
509 l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
510
511 lwp_update_creds(l2);
512 callout_init(&l2->l_tsleep_ch);
513
514 if (rnewlwpp != NULL)
515 *rnewlwpp = l2;
516
517 l2->l_addr = UAREA_TO_USER(uaddr);
518 uvm_lwp_fork(l1, l2, stack, stacksize, func,
519 (arg != NULL) ? arg : l2);
520
521 simple_lock(&p2->p_lock);
522 l2->l_lid = ++p2->p_nlwpid;
523 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
524 p2->p_nlwps++;
525 simple_unlock(&p2->p_lock);
526
527 /* XXX should be locked differently... */
528 s = proclist_lock_write();
529 LIST_INSERT_HEAD(&alllwp, l2, l_list);
530 proclist_unlock_write(s);
531
532 if (p2->p_emul->e_lwp_fork)
533 (*p2->p_emul->e_lwp_fork)(l1, l2);
534
535 return (0);
536 }
537
538
539 /*
540 * Quit the process. This will call cpu_exit, which will call cpu_switch,
541 * so this can only be used meaningfully if you're willing to switch away.
542 * Calling with l!=curlwp would be weird.
543 */
544 void
545 lwp_exit(struct lwp *l)
546 {
547 struct proc *p = l->l_proc;
548 int s;
549
550 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
551 DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
552 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
553
554 if (p->p_emul->e_lwp_exit)
555 (*p->p_emul->e_lwp_exit)(l);
556
557 /*
558 * If we are the last live LWP in a process, we need to exit
559 * the entire process (if that's not already going on). We do
560 * so with an exit status of zero, because it's a "controlled"
561 * exit, and because that's what Solaris does.
562 */
563 if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
564 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
565 p->p_pid, l->l_lid));
566 exit1(l, 0);
567 /* NOTREACHED */
568 }
569
570 s = proclist_lock_write();
571 LIST_REMOVE(l, l_list);
572 proclist_unlock_write(s);
573
574 /*
575 * Release our cached credentials, and collate accounting flags.
576 */
577 kauth_cred_free(l->l_cred);
578 simple_lock(&p->p_lock);
579 p->p_acflag |= l->l_acflag;
580 simple_unlock(&p->p_lock);
581
582 /* Free MD LWP resources */
583 #ifndef __NO_CPU_LWP_FREE
584 cpu_lwp_free(l, 0);
585 #endif
586
587 pmap_deactivate(l);
588
589 if (l->l_flag & L_DETACHED) {
590 simple_lock(&p->p_lock);
591 LIST_REMOVE(l, l_sibling);
592 p->p_nlwps--;
593 simple_unlock(&p->p_lock);
594
595 curlwp = NULL;
596 l->l_proc = NULL;
597 }
598
599 SCHED_LOCK(s);
600 p->p_nrlwps--;
601 l->l_stat = LSDEAD;
602 SCHED_UNLOCK(s);
603
604 /* This LWP no longer needs to hold the kernel lock. */
605 KERNEL_PROC_UNLOCK(l);
606
607 /* cpu_exit() will not return */
608 cpu_exit(l);
609 }
610
611 /*
612 * We are called from cpu_exit() once it is safe to schedule the
613 * dead process's resources to be freed (i.e., once we've switched to
614 * the idle PCB for the current CPU).
615 *
616 * NOTE: One must be careful with locking in this routine. It's
617 * called from a critical section in machine-dependent code, so
618 * we should refrain from changing any interrupt state.
619 */
620 void
621 lwp_exit2(struct lwp *l)
622 {
623 struct proc *p;
624
625 KERNEL_LOCK(LK_EXCLUSIVE);
626 /*
627 * Free the VM resources we're still holding on to.
628 */
629 uvm_lwp_exit(l);
630
631 if (l->l_flag & L_DETACHED) {
632 /* Nobody waits for detached LWPs. */
633 pool_put(&lwp_pool, l);
634 KERNEL_UNLOCK();
635 } else {
636 l->l_stat = LSZOMB;
637 p = l->l_proc;
638 p->p_nzlwps++;
639 KERNEL_UNLOCK();
640 wakeup(&p->p_nlwps);
641 }
642 }
643
644 /*
645 * Pick a LWP to represent the process for those operations which
646 * want information about a "process" that is actually associated
647 * with a LWP.
648 */
649 struct lwp *
650 proc_representative_lwp(struct proc *p)
651 {
652 struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
653 struct lwp *signalled;
654
655 /* Trivial case: only one LWP */
656 if (p->p_nlwps == 1)
657 return (LIST_FIRST(&p->p_lwps));
658
659 switch (p->p_stat) {
660 case SSTOP:
661 case SACTIVE:
662 /* Pick the most live LWP */
663 onproc = running = sleeping = stopped = suspended = NULL;
664 signalled = NULL;
665 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
666 if (l->l_lid == p->p_sigctx.ps_lwp)
667 signalled = l;
668 switch (l->l_stat) {
669 case LSONPROC:
670 onproc = l;
671 break;
672 case LSRUN:
673 running = l;
674 break;
675 case LSSLEEP:
676 sleeping = l;
677 break;
678 case LSSTOP:
679 stopped = l;
680 break;
681 case LSSUSPENDED:
682 suspended = l;
683 break;
684 }
685 }
686 if (signalled)
687 return signalled;
688 if (onproc)
689 return onproc;
690 if (running)
691 return running;
692 if (sleeping)
693 return sleeping;
694 if (stopped)
695 return stopped;
696 if (suspended)
697 return suspended;
698 break;
699 case SZOMB:
700 /* Doesn't really matter... */
701 return (LIST_FIRST(&p->p_lwps));
702 #ifdef DIAGNOSTIC
703 case SIDL:
704 /* We have more than one LWP and we're in SIDL?
705 * How'd that happen?
706 */
707 panic("Too many LWPs (%d) in SIDL process %d (%s)",
708 p->p_nrlwps, p->p_pid, p->p_comm);
709 default:
710 panic("Process %d (%s) in unknown state %d",
711 p->p_pid, p->p_comm, p->p_stat);
712 #endif
713 }
714
715 panic("proc_representative_lwp: couldn't find a lwp for process"
716 " %d (%s)", p->p_pid, p->p_comm);
717 /* NOTREACHED */
718 return NULL;
719 }
720
721 /*
722 * Update an LWP's cached credentials to mirror the process' master copy.
723 *
724 * This happens early in the syscall path, on user trap, and on LWP
725 * creation. A long-running LWP can also voluntarily choose to update
726 * it's credentials by calling this routine. This may be called from
727 * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
728 */
729 void
730 lwp_update_creds(struct lwp *l)
731 {
732 kauth_cred_t oc;
733 struct proc *p;
734
735 p = l->l_proc;
736 oc = l->l_cred;
737
738 simple_lock(&p->p_lock);
739 kauth_cred_hold(p->p_cred);
740 l->l_cred = p->p_cred;
741 simple_unlock(&p->p_lock);
742 if (oc != NULL)
743 kauth_cred_free(oc);
744 }
745