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