kern_lwp.c revision 1.33.10.3 1 /* $NetBSD: kern_lwp.c,v 1.33.10.3 2006/08/11 15:45:46 yamt 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.33.10.3 2006/08/11 15:45:46 yamt 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 lwp_update_creds(l2);
507 callout_init(&l2->l_tsleep_ch);
508
509 if (rnewlwpp != NULL)
510 *rnewlwpp = l2;
511
512 l2->l_addr = UAREA_TO_USER(uaddr);
513 uvm_lwp_fork(l1, l2, stack, stacksize, func,
514 (arg != NULL) ? arg : l2);
515
516 simple_lock(&p2->p_lock);
517 l2->l_lid = ++p2->p_nlwpid;
518 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
519 p2->p_nlwps++;
520 simple_unlock(&p2->p_lock);
521
522 /* XXX should be locked differently... */
523 s = proclist_lock_write();
524 LIST_INSERT_HEAD(&alllwp, l2, l_list);
525 proclist_unlock_write(s);
526
527 if (p2->p_emul->e_lwp_fork)
528 (*p2->p_emul->e_lwp_fork)(l1, l2);
529
530 return (0);
531 }
532
533
534 /*
535 * Quit the process. This will call cpu_exit, which will call cpu_switch,
536 * so this can only be used meaningfully if you're willing to switch away.
537 * Calling with l!=curlwp would be weird.
538 */
539 void
540 lwp_exit(struct lwp *l)
541 {
542 struct proc *p = l->l_proc;
543 int s;
544
545 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
546 DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
547 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
548
549 if (p->p_emul->e_lwp_exit)
550 (*p->p_emul->e_lwp_exit)(l);
551
552 /*
553 * If we are the last live LWP in a process, we need to exit
554 * the entire process (if that's not already going on). We do
555 * so with an exit status of zero, because it's a "controlled"
556 * exit, and because that's what Solaris does.
557 */
558 if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
559 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
560 p->p_pid, l->l_lid));
561 exit1(l, 0);
562 /* NOTREACHED */
563 }
564
565 s = proclist_lock_write();
566 LIST_REMOVE(l, l_list);
567 proclist_unlock_write(s);
568
569 /*
570 * Release our cached credentials, and collate accounting flags.
571 */
572 kauth_cred_free(l->l_cred);
573 simple_lock(&p->p_lock);
574 p->p_acflag |= l->l_acflag;
575 simple_unlock(&p->p_lock);
576
577 /* Free MD LWP resources */
578 #ifndef __NO_CPU_LWP_FREE
579 cpu_lwp_free(l, 0);
580 #endif
581
582 pmap_deactivate(l);
583
584 if (l->l_flag & L_DETACHED) {
585 simple_lock(&p->p_lock);
586 LIST_REMOVE(l, l_sibling);
587 p->p_nlwps--;
588 simple_unlock(&p->p_lock);
589
590 curlwp = NULL;
591 l->l_proc = NULL;
592 }
593
594 SCHED_LOCK(s);
595 p->p_nrlwps--;
596 l->l_stat = LSDEAD;
597 SCHED_UNLOCK(s);
598
599 /* This LWP no longer needs to hold the kernel lock. */
600 KERNEL_PROC_UNLOCK(l);
601
602 /* cpu_exit() will not return */
603 cpu_exit(l);
604 }
605
606 /*
607 * We are called from cpu_exit() once it is safe to schedule the
608 * dead process's resources to be freed (i.e., once we've switched to
609 * the idle PCB for the current CPU).
610 *
611 * NOTE: One must be careful with locking in this routine. It's
612 * called from a critical section in machine-dependent code, so
613 * we should refrain from changing any interrupt state.
614 */
615 void
616 lwp_exit2(struct lwp *l)
617 {
618 struct proc *p;
619
620 KERNEL_LOCK(LK_EXCLUSIVE);
621 /*
622 * Free the VM resources we're still holding on to.
623 */
624 uvm_lwp_exit(l);
625
626 if (l->l_flag & L_DETACHED) {
627 /* Nobody waits for detached LWPs. */
628 pool_put(&lwp_pool, l);
629 KERNEL_UNLOCK();
630 } else {
631 l->l_stat = LSZOMB;
632 p = l->l_proc;
633 p->p_nzlwps++;
634 KERNEL_UNLOCK();
635 wakeup(&p->p_nlwps);
636 }
637 }
638
639 /*
640 * Pick a LWP to represent the process for those operations which
641 * want information about a "process" that is actually associated
642 * with a LWP.
643 */
644 struct lwp *
645 proc_representative_lwp(struct proc *p)
646 {
647 struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
648 struct lwp *signalled;
649
650 /* Trivial case: only one LWP */
651 if (p->p_nlwps == 1)
652 return (LIST_FIRST(&p->p_lwps));
653
654 switch (p->p_stat) {
655 case SSTOP:
656 case SACTIVE:
657 /* Pick the most live LWP */
658 onproc = running = sleeping = stopped = suspended = NULL;
659 signalled = NULL;
660 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
661 if (l->l_lid == p->p_sigctx.ps_lwp)
662 signalled = l;
663 switch (l->l_stat) {
664 case LSONPROC:
665 onproc = l;
666 break;
667 case LSRUN:
668 running = l;
669 break;
670 case LSSLEEP:
671 sleeping = l;
672 break;
673 case LSSTOP:
674 stopped = l;
675 break;
676 case LSSUSPENDED:
677 suspended = l;
678 break;
679 }
680 }
681 if (signalled)
682 return signalled;
683 if (onproc)
684 return onproc;
685 if (running)
686 return running;
687 if (sleeping)
688 return sleeping;
689 if (stopped)
690 return stopped;
691 if (suspended)
692 return suspended;
693 break;
694 case SZOMB:
695 /* Doesn't really matter... */
696 return (LIST_FIRST(&p->p_lwps));
697 #ifdef DIAGNOSTIC
698 case SIDL:
699 /* We have more than one LWP and we're in SIDL?
700 * How'd that happen?
701 */
702 panic("Too many LWPs (%d) in SIDL process %d (%s)",
703 p->p_nrlwps, p->p_pid, p->p_comm);
704 default:
705 panic("Process %d (%s) in unknown state %d",
706 p->p_pid, p->p_comm, p->p_stat);
707 #endif
708 }
709
710 panic("proc_representative_lwp: couldn't find a lwp for process"
711 " %d (%s)", p->p_pid, p->p_comm);
712 /* NOTREACHED */
713 return NULL;
714 }
715
716 /*
717 * Update an LWP's cached credentials to mirror the process' master copy.
718 *
719 * This happens early in the syscall path, on user trap, and on LWP
720 * creation. A long-running LWP can also voluntarily choose to update
721 * it's credentials by calling this routine. This may be called from
722 * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
723 */
724 void
725 lwp_update_creds(struct lwp *l)
726 {
727 kauth_cred_t oc;
728 struct proc *p;
729
730 p = l->l_proc;
731 oc = l->l_cred;
732
733 simple_lock(&p->p_lock);
734 kauth_cred_hold(p->p_cred);
735 l->l_cred = p->p_cred;
736 simple_unlock(&p->p_lock);
737 if (oc != NULL)
738 kauth_cred_free(oc);
739 }
740