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