kern_lwp.c revision 1.55.2.9 1 /* $NetBSD: kern_lwp.c,v 1.55.2.9 2007/03/24 14:56:01 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006, 2007 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, and Andrew Doran.
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 /*
40 * Overview
41 *
42 * Lightweight processes (LWPs) are the basic unit (or thread) of
43 * execution within the kernel. The core state of an LWP is described
44 * by "struct lwp".
45 *
46 * Each LWP is contained within a process (described by "struct proc"),
47 * Every process contains at least one LWP, but may contain more. The
48 * process describes attributes shared among all of its LWPs such as a
49 * private address space, global execution state (stopped, active,
50 * zombie, ...), signal disposition and so on. On a multiprocessor
51 * machine, multiple LWPs be executing in kernel simultaneously.
52 *
53 * Note that LWPs differ from kernel threads (kthreads) in that kernel
54 * threads are distinct processes (system processes) with no user space
55 * component, which themselves may contain one or more LWPs.
56 *
57 * Execution states
58 *
59 * At any given time, an LWP has overall state that is described by
60 * lwp::l_stat. The states are broken into two sets below. The first
61 * set is guaranteed to represent the absolute, current state of the
62 * LWP:
63 *
64 * LSONPROC
65 *
66 * On processor: the LWP is executing on a CPU, either in the
67 * kernel or in user space.
68 *
69 * LSRUN
70 *
71 * Runnable: the LWP is parked on a run queue, and may soon be
72 * chosen to run by a idle processor, or by a processor that
73 * has been asked to preempt a currently runnning but lower
74 * priority LWP. If the LWP is not swapped in (L_INMEM == 0)
75 * then the LWP is not on a run queue, but may be soon.
76 *
77 * LSIDL
78 *
79 * Idle: the LWP has been created but has not yet executed.
80 * Whoever created the new LWP can be expected to set it to
81 * another state shortly.
82 *
83 * LSSUSPENDED:
84 *
85 * Suspended: the LWP has had its execution suspended by
86 * another LWP in the same process using the _lwp_suspend()
87 * system call. User-level LWPs also enter the suspended
88 * state when the system is shutting down.
89 *
90 * The second set represent a "statement of intent" on behalf of the
91 * LWP. The LWP may in fact be executing on a processor, may be
92 * sleeping, idle, or on a run queue. It is expected to take the
93 * necessary action to stop executing or become "running" again within
94 * a short timeframe.
95 *
96 * LSZOMB:
97 *
98 * Dead: the LWP has released most of its resources and is
99 * about to switch away into oblivion. When it switches away,
100 * its few remaining resources will be collected.
101 *
102 * LSSLEEP:
103 *
104 * Sleeping: the LWP has entered itself onto a sleep queue, and
105 * will switch away shortly to allow other LWPs to run on the
106 * CPU.
107 *
108 * LSSTOP:
109 *
110 * Stopped: the LWP has been stopped as a result of a job
111 * control signal, or as a result of the ptrace() interface.
112 * Stopped LWPs may run briefly within the kernel to handle
113 * signals that they receive, but will not return to user space
114 * until their process' state is changed away from stopped.
115 * Single LWPs within a process can not be set stopped
116 * selectively: all actions that can stop or continue LWPs
117 * occur at the process level.
118 *
119 * State transitions
120 *
121 * Note that the LSSTOP and LSSUSPENDED states may only be set
122 * when returning to user space in userret(), or when sleeping
123 * interruptably. Before setting those states, we try to ensure
124 * that the LWPs will release all kernel locks that they hold,
125 * and at a minimum try to ensure that the LWP can be set runnable
126 * again by a signal.
127 *
128 * LWPs may transition states in the following ways:
129 *
130 * RUN -------> ONPROC ONPROC -----> RUN
131 * > STOPPED > SLEEP
132 * > SUSPENDED > STOPPED
133 * > SUSPENDED
134 * > ZOMB
135 *
136 * STOPPED ---> RUN SUSPENDED --> RUN
137 * > SLEEP > SLEEP
138 *
139 * SLEEP -----> ONPROC IDL --------> RUN
140 * > RUN > SUSPENDED
141 * > STOPPED > STOPPED
142 * > SUSPENDED
143 *
144 * Locking
145 *
146 * The majority of fields in 'struct lwp' are covered by a single,
147 * general spin mutex pointed to by lwp::l_mutex. The locks covering
148 * each field are documented in sys/lwp.h.
149 *
150 * State transitions must be made with the LWP's general lock held. In
151 * a multiprocessor kernel, state transitions may cause the LWP's lock
152 * pointer to change. On uniprocessor kernels, most scheduler and
153 * synchronisation objects such as sleep queues and LWPs are protected
154 * by only one mutex (spc_mutex on single CPU). In this case, LWPs' lock
155 * pointers will never change and will always reference spc_mutex.
156 * Please note that in a multiprocessor kernel each CPU has own spc_mutex.
157 *
158 * Where spc_mutex is noted, it refers to l->l_cpu->ci_schedstate.spc_mutex
159
160 *
161 * Manipulation of the general lock is not performed directly, but
162 * through calls to lwp_lock(), lwp_relock() and similar.
163 *
164 * States and their associated locks:
165 *
166 * LSIDL, LSZOMB
167 *
168 * Always covered by spc_mutex.
169 *
170 * LSONPROC, LSRUN:
171 *
172 * Always covered by spc_mutex, which protects the run queues
173 * and other miscellaneous items. If the scheduler is changed
174 * to use per-CPU run queues, this may become a per-CPU mutex.
175 *
176 * LSSLEEP:
177 *
178 * Covered by a mutex associated with the sleep queue that the
179 * LWP resides on, indirectly referenced by l_sleepq->sq_mutex.
180 *
181 * LSSTOP, LSSUSPENDED:
182 *
183 * If the LWP was previously sleeping (l_wchan != NULL), then
184 * l_mutex references the sleep queue mutex. If the LWP was
185 * runnable or on the CPU when halted, or has been removed from
186 * the sleep queue since halted, then the mutex is spc_mutex.
187 *
188 * The lock order is as follows:
189 *
190 * sleepq_t::sq_mutex |---> spc_mutex
191 * tschain_t::tc_mutex |
192 *
193 * Each process has an scheduler state mutex (proc::p_smutex), and a
194 * number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
195 * so on. When an LWP is to be entered into or removed from one of the
196 * following states, p_mutex must be held and the process wide counters
197 * adjusted:
198 *
199 * LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
200 *
201 * Note that an LWP is considered running or likely to run soon if in
202 * one of the following states. This affects the value of p_nrlwps:
203 *
204 * LSRUN, LSONPROC, LSSLEEP
205 *
206 * p_smutex does not need to be held when transitioning among these
207 * three states.
208 */
209
210 #include <sys/cdefs.h>
211 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.55.2.9 2007/03/24 14:56:01 yamt Exp $");
212
213 #include "opt_multiprocessor.h"
214 #include "opt_lockdebug.h"
215
216 #define _LWP_API_PRIVATE
217
218 #include <sys/param.h>
219 #include <sys/systm.h>
220 #include <sys/cpu.h>
221 #include <sys/pool.h>
222 #include <sys/proc.h>
223 #include <sys/syscallargs.h>
224 #include <sys/syscall_stats.h>
225 #include <sys/kauth.h>
226 #include <sys/sleepq.h>
227 #include <sys/lockdebug.h>
228 #include <sys/kmem.h>
229
230 #include <uvm/uvm_extern.h>
231
232 struct lwplist alllwp;
233
234 POOL_INIT(lwp_pool, sizeof(struct lwp), MIN_LWP_ALIGNMENT, 0, 0, "lwppl",
235 &pool_allocator_nointr, IPL_NONE);
236 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
237 &pool_allocator_nointr, IPL_NONE);
238
239 static specificdata_domain_t lwp_specificdata_domain;
240
241 #define LWP_DEBUG
242
243 #ifdef LWP_DEBUG
244 int lwp_debug = 0;
245 #define DPRINTF(x) if (lwp_debug) printf x
246 #else
247 #define DPRINTF(x)
248 #endif
249
250 void
251 lwpinit(void)
252 {
253
254 lwp_specificdata_domain = specificdata_domain_create();
255 KASSERT(lwp_specificdata_domain != NULL);
256 lwp_sys_init();
257 }
258
259 /*
260 * Set an suspended.
261 *
262 * Must be called with p_smutex held, and the LWP locked. Will unlock the
263 * LWP before return.
264 */
265 int
266 lwp_suspend(struct lwp *curl, struct lwp *t)
267 {
268 int error;
269
270 KASSERT(mutex_owned(&t->l_proc->p_smutex));
271 KASSERT(lwp_locked(t, NULL));
272
273 KASSERT(curl != t || curl->l_stat == LSONPROC);
274
275 /*
276 * If the current LWP has been told to exit, we must not suspend anyone
277 * else or deadlock could occur. We won't return to userspace.
278 */
279 if ((curl->l_stat & (LW_WEXIT | LW_WCORE)) != 0) {
280 lwp_unlock(t);
281 return (EDEADLK);
282 }
283
284 error = 0;
285
286 switch (t->l_stat) {
287 case LSRUN:
288 case LSONPROC:
289 t->l_flag |= LW_WSUSPEND;
290 lwp_need_userret(t);
291 lwp_unlock(t);
292 break;
293
294 case LSSLEEP:
295 t->l_flag |= LW_WSUSPEND;
296
297 /*
298 * Kick the LWP and try to get it to the kernel boundary
299 * so that it will release any locks that it holds.
300 * setrunnable() will release the lock.
301 */
302 if ((t->l_flag & LW_SINTR) != 0)
303 setrunnable(t);
304 else
305 lwp_unlock(t);
306 break;
307
308 case LSSUSPENDED:
309 lwp_unlock(t);
310 break;
311
312 case LSSTOP:
313 t->l_flag |= LW_WSUSPEND;
314 setrunnable(t);
315 break;
316
317 case LSIDL:
318 case LSZOMB:
319 error = EINTR; /* It's what Solaris does..... */
320 lwp_unlock(t);
321 break;
322 }
323
324 /*
325 * XXXLWP Wait for:
326 *
327 * o process exiting
328 * o target LWP suspended
329 * o target LWP not suspended and L_WSUSPEND clear
330 * o target LWP exited
331 */
332
333 return (error);
334 }
335
336 /*
337 * Restart a suspended LWP.
338 *
339 * Must be called with p_smutex held, and the LWP locked. Will unlock the
340 * LWP before return.
341 */
342 void
343 lwp_continue(struct lwp *l)
344 {
345
346 KASSERT(mutex_owned(&l->l_proc->p_smutex));
347 KASSERT(lwp_locked(l, NULL));
348
349 DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
350 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
351 l->l_wchan));
352
353 /* If rebooting or not suspended, then just bail out. */
354 if ((l->l_flag & LW_WREBOOT) != 0) {
355 lwp_unlock(l);
356 return;
357 }
358
359 l->l_flag &= ~LW_WSUSPEND;
360
361 if (l->l_stat != LSSUSPENDED) {
362 lwp_unlock(l);
363 return;
364 }
365
366 /* setrunnable() will release the lock. */
367 setrunnable(l);
368 }
369
370 /*
371 * Wait for an LWP within the current process to exit. If 'lid' is
372 * non-zero, we are waiting for a specific LWP.
373 *
374 * Must be called with p->p_smutex held.
375 */
376 int
377 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
378 {
379 struct proc *p = l->l_proc;
380 struct lwp *l2;
381 int nfound, error;
382 lwpid_t curlid;
383 bool exiting;
384
385 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
386 p->p_pid, l->l_lid, lid));
387
388 KASSERT(mutex_owned(&p->p_smutex));
389
390 p->p_nlwpwait++;
391 l->l_waitingfor = lid;
392 curlid = l->l_lid;
393 exiting = ((flags & LWPWAIT_EXITCONTROL) != 0);
394
395 for (;;) {
396 /*
397 * Avoid a race between exit1() and sigexit(): if the
398 * process is dumping core, then we need to bail out: call
399 * into lwp_userret() where we will be suspended until the
400 * deed is done.
401 */
402 if ((p->p_sflag & PS_WCORE) != 0) {
403 mutex_exit(&p->p_smutex);
404 lwp_userret(l);
405 #ifdef DIAGNOSTIC
406 panic("lwp_wait1");
407 #endif
408 /* NOTREACHED */
409 }
410
411 /*
412 * First off, drain any detached LWP that is waiting to be
413 * reaped.
414 */
415 while ((l2 = p->p_zomblwp) != NULL) {
416 p->p_zomblwp = NULL;
417 lwp_free(l2, false, false);/* releases proc mutex */
418 mutex_enter(&p->p_smutex);
419 }
420
421 /*
422 * Now look for an LWP to collect. If the whole process is
423 * exiting, count detached LWPs as eligible to be collected,
424 * but don't drain them here.
425 */
426 nfound = 0;
427 error = 0;
428 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
429 /*
430 * If a specific wait and the target is waiting on
431 * us, then avoid deadlock. This also traps LWPs
432 * that try to wait on themselves.
433 *
434 * Note that this does not handle more complicated
435 * cycles, like: t1 -> t2 -> t3 -> t1. The process
436 * can still be killed so it is not a major problem.
437 */
438 if (l2->l_lid == lid && l2->l_waitingfor == curlid) {
439 error = EDEADLK;
440 break;
441 }
442 if (l2 == l)
443 continue;
444 if ((l2->l_prflag & LPR_DETACHED) != 0) {
445 nfound += exiting;
446 continue;
447 }
448 if (lid != 0) {
449 if (l2->l_lid != lid)
450 continue;
451 /*
452 * Mark this LWP as the first waiter, if there
453 * is no other.
454 */
455 if (l2->l_waiter == 0)
456 l2->l_waiter = curlid;
457 } else if (l2->l_waiter != 0) {
458 /*
459 * It already has a waiter - so don't
460 * collect it. If the waiter doesn't
461 * grab it we'll get another chance
462 * later.
463 */
464 nfound++;
465 continue;
466 }
467 nfound++;
468
469 /* No need to lock the LWP in order to see LSZOMB. */
470 if (l2->l_stat != LSZOMB)
471 continue;
472
473 /*
474 * We're no longer waiting. Reset the "first waiter"
475 * pointer on the target, in case it was us.
476 */
477 l->l_waitingfor = 0;
478 l2->l_waiter = 0;
479 p->p_nlwpwait--;
480 if (departed)
481 *departed = l2->l_lid;
482
483 /* lwp_free() releases the proc lock. */
484 lwp_free(l2, false, false);
485 mutex_enter(&p->p_smutex);
486 return 0;
487 }
488
489 if (error != 0)
490 break;
491 if (nfound == 0) {
492 error = ESRCH;
493 break;
494 }
495
496 /*
497 * The kernel is careful to ensure that it can not deadlock
498 * when exiting - just keep waiting.
499 */
500 if (exiting) {
501 KASSERT(p->p_nlwps > 1);
502 cv_wait(&p->p_lwpcv, &p->p_smutex);
503 continue;
504 }
505
506 /*
507 * If all other LWPs are waiting for exits or suspends
508 * and the supply of zombies and potential zombies is
509 * exhausted, then we are about to deadlock.
510 *
511 * If the process is exiting (and this LWP is not the one
512 * that is coordinating the exit) then bail out now.
513 */
514 if ((p->p_sflag & PS_WEXIT) != 0 ||
515 p->p_nrlwps + p->p_nzlwps - p->p_ndlwps <= p->p_nlwpwait) {
516 error = EDEADLK;
517 break;
518 }
519
520 /*
521 * Sit around and wait for something to happen. We'll be
522 * awoken if any of the conditions examined change: if an
523 * LWP exits, is collected, or is detached.
524 */
525 if ((error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex)) != 0)
526 break;
527 }
528
529 /*
530 * We didn't find any LWPs to collect, we may have received a
531 * signal, or some other condition has caused us to bail out.
532 *
533 * If waiting on a specific LWP, clear the waiters marker: some
534 * other LWP may want it. Then, kick all the remaining waiters
535 * so that they can re-check for zombies and for deadlock.
536 */
537 if (lid != 0) {
538 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
539 if (l2->l_lid == lid) {
540 if (l2->l_waiter == curlid)
541 l2->l_waiter = 0;
542 break;
543 }
544 }
545 }
546 p->p_nlwpwait--;
547 l->l_waitingfor = 0;
548 cv_broadcast(&p->p_lwpcv);
549
550 return error;
551 }
552
553 /*
554 * Create a new LWP within process 'p2', using LWP 'l1' as a template.
555 * The new LWP is created in state LSIDL and must be set running,
556 * suspended, or stopped by the caller.
557 */
558 int
559 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, bool inmem,
560 int flags, void *stack, size_t stacksize,
561 void (*func)(void *), void *arg, struct lwp **rnewlwpp)
562 {
563 struct lwp *l2, *isfree;
564 turnstile_t *ts;
565
566 /*
567 * First off, reap any detached LWP waiting to be collected.
568 * We can re-use its LWP structure and turnstile.
569 */
570 isfree = NULL;
571 if (p2->p_zomblwp != NULL) {
572 mutex_enter(&p2->p_smutex);
573 if ((isfree = p2->p_zomblwp) != NULL) {
574 p2->p_zomblwp = NULL;
575 lwp_free(isfree, true, false);/* releases proc mutex */
576 } else
577 mutex_exit(&p2->p_smutex);
578 }
579 if (isfree == NULL) {
580 l2 = pool_get(&lwp_pool, PR_WAITOK);
581 memset(l2, 0, sizeof(*l2));
582 l2->l_ts = pool_cache_get(&turnstile_cache, PR_WAITOK);
583 SLIST_INIT(&l2->l_pi_lenders);
584 } else {
585 l2 = isfree;
586 ts = l2->l_ts;
587 KASSERT(l2->l_inheritedprio == MAXPRI);
588 KASSERT(SLIST_EMPTY(&l2->l_pi_lenders));
589 memset(l2, 0, sizeof(*l2));
590 l2->l_ts = ts;
591 }
592
593 l2->l_stat = LSIDL;
594 l2->l_proc = p2;
595 l2->l_refcnt = 1;
596 l2->l_priority = l1->l_priority;
597 l2->l_usrpri = l1->l_usrpri;
598 l2->l_inheritedprio = MAXPRI;
599 l2->l_mutex = l1->l_cpu->ci_schedstate.spc_mutex;
600 l2->l_cpu = l1->l_cpu;
601 l2->l_flag = inmem ? LW_INMEM : 0;
602 lwp_initspecific(l2);
603 sched_lwp_fork(l2);
604
605 if (p2->p_flag & PK_SYSTEM) {
606 /*
607 * Mark it as a system process and not a candidate for
608 * swapping.
609 */
610 l2->l_flag |= LW_SYSTEM;
611 }
612
613 lwp_update_creds(l2);
614 callout_init(&l2->l_tsleep_ch);
615 cv_init(&l2->l_sigcv, "sigwait");
616 l2->l_syncobj = &sched_syncobj;
617
618 if (rnewlwpp != NULL)
619 *rnewlwpp = l2;
620
621 l2->l_addr = UAREA_TO_USER(uaddr);
622 uvm_lwp_fork(l1, l2, stack, stacksize, func,
623 (arg != NULL) ? arg : l2);
624
625 mutex_enter(&p2->p_smutex);
626
627 if ((flags & LWP_DETACHED) != 0) {
628 l2->l_prflag = LPR_DETACHED;
629 p2->p_ndlwps++;
630 } else
631 l2->l_prflag = 0;
632
633 l2->l_sigmask = l1->l_sigmask;
634 CIRCLEQ_INIT(&l2->l_sigpend.sp_info);
635 sigemptyset(&l2->l_sigpend.sp_set);
636
637 p2->p_nlwpid++;
638 if (p2->p_nlwpid == 0)
639 p2->p_nlwpid++;
640 l2->l_lid = p2->p_nlwpid;
641 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
642 p2->p_nlwps++;
643
644 mutex_exit(&p2->p_smutex);
645
646 mutex_enter(&proclist_mutex);
647 LIST_INSERT_HEAD(&alllwp, l2, l_list);
648 mutex_exit(&proclist_mutex);
649
650 SYSCALL_TIME_LWP_INIT(l2);
651
652 if (p2->p_emul->e_lwp_fork)
653 (*p2->p_emul->e_lwp_fork)(l1, l2);
654
655 return (0);
656 }
657
658 /*
659 * Quit the process.
660 * this can only be used meaningfully if you're willing to switch away.
661 * Calling with l != curlwp would be weird.
662 */
663 void
664 lwp_exit(struct lwp *l)
665 {
666 struct proc *p = l->l_proc;
667 struct lwp *l2;
668
669 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
670 DPRINTF((" nlwps: %d nzlwps: %d\n", p->p_nlwps, p->p_nzlwps));
671
672 /*
673 * Verify that we hold no locks other than the kernel lock.
674 */
675 #ifdef MULTIPROCESSOR
676 LOCKDEBUG_BARRIER(&kernel_lock, 0);
677 #else
678 LOCKDEBUG_BARRIER(NULL, 0);
679 #endif
680
681 /*
682 * If we are the last live LWP in a process, we need to exit the
683 * entire process. We do so with an exit status of zero, because
684 * it's a "controlled" exit, and because that's what Solaris does.
685 *
686 * We are not quite a zombie yet, but for accounting purposes we
687 * must increment the count of zombies here.
688 *
689 * Note: the last LWP's specificdata will be deleted here.
690 */
691 mutex_enter(&p->p_smutex);
692 if (p->p_nlwps - p->p_nzlwps == 1) {
693 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
694 p->p_pid, l->l_lid));
695 exit1(l, 0);
696 /* NOTREACHED */
697 }
698 p->p_nzlwps++;
699 mutex_exit(&p->p_smutex);
700
701 if (p->p_emul->e_lwp_exit)
702 (*p->p_emul->e_lwp_exit)(l);
703
704 /* Delete the specificdata while it's still safe to sleep. */
705 specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
706
707 /*
708 * Release our cached credentials.
709 */
710 kauth_cred_free(l->l_cred);
711
712 /*
713 * Remove the LWP from the global list.
714 */
715 mutex_enter(&proclist_mutex);
716 LIST_REMOVE(l, l_list);
717 mutex_exit(&proclist_mutex);
718
719 /*
720 * Get rid of all references to the LWP that others (e.g. procfs)
721 * may have, and mark the LWP as a zombie. If the LWP is detached,
722 * mark it waiting for collection in the proc structure. Note that
723 * before we can do that, we need to free any other dead, deatched
724 * LWP waiting to meet its maker.
725 *
726 * XXXSMP disable preemption.
727 */
728 mutex_enter(&p->p_smutex);
729 lwp_drainrefs(l);
730
731 if ((l->l_prflag & LPR_DETACHED) != 0) {
732 while ((l2 = p->p_zomblwp) != NULL) {
733 p->p_zomblwp = NULL;
734 lwp_free(l2, false, false);/* releases proc mutex */
735 mutex_enter(&p->p_smutex);
736 }
737 p->p_zomblwp = l;
738 }
739
740 /*
741 * If we find a pending signal for the process and we have been
742 * asked to check for signals, then we loose: arrange to have
743 * all other LWPs in the process check for signals.
744 */
745 if ((l->l_flag & LW_PENDSIG) != 0 &&
746 firstsig(&p->p_sigpend.sp_set) != 0) {
747 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
748 lwp_lock(l2);
749 l2->l_flag |= LW_PENDSIG;
750 lwp_unlock(l2);
751 }
752 }
753
754 lwp_lock(l);
755 l->l_stat = LSZOMB;
756 lwp_unlock(l);
757 p->p_nrlwps--;
758 cv_broadcast(&p->p_lwpcv);
759 mutex_exit(&p->p_smutex);
760
761 /*
762 * We can no longer block. At this point, lwp_free() may already
763 * be gunning for us. On a multi-CPU system, we may be off p_lwps.
764 *
765 * Free MD LWP resources.
766 */
767 #ifndef __NO_CPU_LWP_FREE
768 cpu_lwp_free(l, 0);
769 #endif
770 pmap_deactivate(l);
771
772 /*
773 * Release the kernel lock, signal another LWP to collect us,
774 * and switch away into oblivion.
775 */
776 #ifdef notyet
777 /* XXXSMP hold in lwp_userret() */
778 KERNEL_UNLOCK_LAST(l);
779 #else
780 KERNEL_UNLOCK_ALL(l, NULL);
781 #endif
782
783 lwp_exit_switchaway(l);
784 }
785
786 void
787 lwp_exit_switchaway(struct lwp *l)
788 {
789 struct cpu_info *ci;
790 struct lwp *idlelwp;
791
792 uvmexp.swtch++; /* XXXSMP unlocked */
793
794 ci = curcpu();
795 idlelwp = ci->ci_data.cpu_idlelwp;
796
797 idlelwp->l_stat = LSONPROC;
798 cpu_switchto(NULL, idlelwp);
799 }
800
801 /*
802 * Free a dead LWP's remaining resources.
803 *
804 * XXXLWP limits.
805 */
806 void
807 lwp_free(struct lwp *l, bool recycle, bool last)
808 {
809 struct proc *p = l->l_proc;
810 ksiginfoq_t kq;
811
812 /*
813 * If this was not the last LWP in the process, then adjust
814 * counters and unlock.
815 */
816 if (!last) {
817 /*
818 * Add the LWP's run time to the process' base value.
819 * This needs to co-incide with coming off p_lwps.
820 */
821 timeradd(&l->l_rtime, &p->p_rtime, &p->p_rtime);
822 LIST_REMOVE(l, l_sibling);
823 p->p_nlwps--;
824 p->p_nzlwps--;
825 if ((l->l_prflag & LPR_DETACHED) != 0)
826 p->p_ndlwps--;
827
828 /*
829 * Have any LWPs sleeping in lwp_wait() recheck for
830 * deadlock.
831 */
832 cv_broadcast(&p->p_lwpcv);
833 mutex_exit(&p->p_smutex);
834 }
835
836 #ifdef MULTIPROCESSOR
837 /*
838 * In the unlikely event that the LWP is still on the CPU,
839 * then spin until it has switched away. We need to release
840 * all locks to avoid deadlock against interrupt handlers on
841 * the target CPU.
842 */
843 if (l->l_cpu->ci_curlwp == l) {
844 int count;
845 KERNEL_UNLOCK_ALL(curlwp, &count);
846 while (l->l_cpu->ci_curlwp == l)
847 SPINLOCK_BACKOFF_HOOK;
848 KERNEL_LOCK(count, curlwp);
849 }
850 #endif
851
852 /*
853 * Destroy the LWP's remaining signal information.
854 */
855 ksiginfo_queue_init(&kq);
856 sigclear(&l->l_sigpend, NULL, &kq);
857 ksiginfo_queue_drain(&kq);
858 cv_destroy(&l->l_sigcv);
859
860 /*
861 * Free the LWP's turnstile and the LWP structure itself unless the
862 * caller wants to recycle them. Also, free the scheduler specific data.
863 *
864 * We can't return turnstile0 to the pool (it didn't come from it),
865 * so if it comes up just drop it quietly and move on.
866 *
867 * We don't recycle the VM resources at this time.
868 */
869 KERNEL_LOCK(1, curlwp); /* XXXSMP */
870
871 sched_lwp_exit(l);
872
873 if (!recycle && l->l_ts != &turnstile0)
874 pool_cache_put(&turnstile_cache, l->l_ts);
875 #ifndef __NO_CPU_LWP_FREE
876 cpu_lwp_free2(l);
877 #endif
878 uvm_lwp_exit(l);
879 KASSERT(SLIST_EMPTY(&l->l_pi_lenders));
880 KASSERT(l->l_inheritedprio == MAXPRI);
881 if (!recycle)
882 pool_put(&lwp_pool, l);
883 KERNEL_UNLOCK_ONE(curlwp); /* XXXSMP */
884 }
885
886 /*
887 * Pick a LWP to represent the process for those operations which
888 * want information about a "process" that is actually associated
889 * with a LWP.
890 *
891 * If 'locking' is false, no locking or lock checks are performed.
892 * This is intended for use by DDB.
893 *
894 * We don't bother locking the LWP here, since code that uses this
895 * interface is broken by design and an exact match is not required.
896 */
897 struct lwp *
898 proc_representative_lwp(struct proc *p, int *nrlwps, int locking)
899 {
900 struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
901 struct lwp *signalled;
902 int cnt;
903
904 if (locking) {
905 KASSERT(mutex_owned(&p->p_smutex));
906 }
907
908 /* Trivial case: only one LWP */
909 if (p->p_nlwps == 1) {
910 l = LIST_FIRST(&p->p_lwps);
911 if (nrlwps)
912 *nrlwps = (l->l_stat == LSONPROC || LSRUN);
913 return l;
914 }
915
916 cnt = 0;
917 switch (p->p_stat) {
918 case SSTOP:
919 case SACTIVE:
920 /* Pick the most live LWP */
921 onproc = running = sleeping = stopped = suspended = NULL;
922 signalled = NULL;
923 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
924 if ((l->l_flag & LW_IDLE) != 0) {
925 continue;
926 }
927 if (l->l_lid == p->p_sigctx.ps_lwp)
928 signalled = l;
929 switch (l->l_stat) {
930 case LSONPROC:
931 onproc = l;
932 cnt++;
933 break;
934 case LSRUN:
935 running = l;
936 cnt++;
937 break;
938 case LSSLEEP:
939 sleeping = l;
940 break;
941 case LSSTOP:
942 stopped = l;
943 break;
944 case LSSUSPENDED:
945 suspended = l;
946 break;
947 }
948 }
949 if (nrlwps)
950 *nrlwps = cnt;
951 if (signalled)
952 l = signalled;
953 else if (onproc)
954 l = onproc;
955 else if (running)
956 l = running;
957 else if (sleeping)
958 l = sleeping;
959 else if (stopped)
960 l = stopped;
961 else if (suspended)
962 l = suspended;
963 else
964 break;
965 return l;
966 if (nrlwps)
967 *nrlwps = 0;
968 l = LIST_FIRST(&p->p_lwps);
969 return l;
970 #ifdef DIAGNOSTIC
971 case SIDL:
972 case SZOMB:
973 case SDYING:
974 case SDEAD:
975 if (locking)
976 mutex_exit(&p->p_smutex);
977 /* We have more than one LWP and we're in SIDL?
978 * How'd that happen?
979 */
980 panic("Too many LWPs in idle/dying process %d (%s) stat = %d",
981 p->p_pid, p->p_comm, p->p_stat);
982 break;
983 default:
984 if (locking)
985 mutex_exit(&p->p_smutex);
986 panic("Process %d (%s) in unknown state %d",
987 p->p_pid, p->p_comm, p->p_stat);
988 #endif
989 }
990
991 if (locking)
992 mutex_exit(&p->p_smutex);
993 panic("proc_representative_lwp: couldn't find a lwp for process"
994 " %d (%s)", p->p_pid, p->p_comm);
995 /* NOTREACHED */
996 return NULL;
997 }
998
999 /*
1000 * Look up a live LWP within the speicifed process, and return it locked.
1001 *
1002 * Must be called with p->p_smutex held.
1003 */
1004 struct lwp *
1005 lwp_find(struct proc *p, int id)
1006 {
1007 struct lwp *l;
1008
1009 KASSERT(mutex_owned(&p->p_smutex));
1010
1011 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
1012 if (l->l_lid == id)
1013 break;
1014 }
1015
1016 /*
1017 * No need to lock - all of these conditions will
1018 * be visible with the process level mutex held.
1019 */
1020 if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
1021 l = NULL;
1022
1023 return l;
1024 }
1025
1026 /*
1027 * Update an LWP's cached credentials to mirror the process' master copy.
1028 *
1029 * This happens early in the syscall path, on user trap, and on LWP
1030 * creation. A long-running LWP can also voluntarily choose to update
1031 * it's credentials by calling this routine. This may be called from
1032 * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
1033 */
1034 void
1035 lwp_update_creds(struct lwp *l)
1036 {
1037 kauth_cred_t oc;
1038 struct proc *p;
1039
1040 p = l->l_proc;
1041 oc = l->l_cred;
1042
1043 mutex_enter(&p->p_mutex);
1044 kauth_cred_hold(p->p_cred);
1045 l->l_cred = p->p_cred;
1046 mutex_exit(&p->p_mutex);
1047 if (oc != NULL) {
1048 KERNEL_LOCK(1, l); /* XXXSMP */
1049 kauth_cred_free(oc);
1050 KERNEL_UNLOCK_ONE(l); /* XXXSMP */
1051 }
1052 }
1053
1054 /*
1055 * Verify that an LWP is locked, and optionally verify that the lock matches
1056 * one we specify.
1057 */
1058 int
1059 lwp_locked(struct lwp *l, kmutex_t *mtx)
1060 {
1061 kmutex_t *cur = l->l_mutex;
1062
1063 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1064 return mutex_owned(cur) && (mtx == cur || mtx == NULL);
1065 #else
1066 return mutex_owned(cur);
1067 #endif
1068 }
1069
1070 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1071 /*
1072 * Lock an LWP.
1073 */
1074 void
1075 lwp_lock_retry(struct lwp *l, kmutex_t *old)
1076 {
1077
1078 /*
1079 * XXXgcc ignoring kmutex_t * volatile on i386
1080 *
1081 * gcc version 4.1.2 20061021 prerelease (NetBSD nb1 20061021)
1082 */
1083 #if 1
1084 while (l->l_mutex != old) {
1085 #else
1086 for (;;) {
1087 #endif
1088 mutex_spin_exit(old);
1089 old = l->l_mutex;
1090 mutex_spin_enter(old);
1091
1092 /*
1093 * mutex_enter() will have posted a read barrier. Re-test
1094 * l->l_mutex. If it has changed, we need to try again.
1095 */
1096 #if 1
1097 }
1098 #else
1099 } while (__predict_false(l->l_mutex != old));
1100 #endif
1101 }
1102 #endif
1103
1104 /*
1105 * Lend a new mutex to an LWP. The old mutex must be held.
1106 */
1107 void
1108 lwp_setlock(struct lwp *l, kmutex_t *new)
1109 {
1110
1111 KASSERT(mutex_owned(l->l_mutex));
1112
1113 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1114 mb_write();
1115 l->l_mutex = new;
1116 #else
1117 (void)new;
1118 #endif
1119 }
1120
1121 /*
1122 * Lend a new mutex to an LWP, and release the old mutex. The old mutex
1123 * must be held.
1124 */
1125 void
1126 lwp_unlock_to(struct lwp *l, kmutex_t *new)
1127 {
1128 kmutex_t *old;
1129
1130 KASSERT(mutex_owned(l->l_mutex));
1131
1132 old = l->l_mutex;
1133 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1134 mb_write();
1135 l->l_mutex = new;
1136 #else
1137 (void)new;
1138 #endif
1139 mutex_spin_exit(old);
1140 }
1141
1142 /*
1143 * Acquire a new mutex, and donate it to an LWP. The LWP must already be
1144 * locked.
1145 */
1146 void
1147 lwp_relock(struct lwp *l, kmutex_t *new)
1148 {
1149 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1150 kmutex_t *old;
1151 #endif
1152
1153 KASSERT(mutex_owned(l->l_mutex));
1154
1155 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1156 old = l->l_mutex;
1157 if (old != new) {
1158 mutex_spin_enter(new);
1159 l->l_mutex = new;
1160 mutex_spin_exit(old);
1161 }
1162 #else
1163 (void)new;
1164 #endif
1165 }
1166
1167 int
1168 lwp_trylock(struct lwp *l)
1169 {
1170 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1171 kmutex_t *old;
1172
1173 for (;;) {
1174 if (!mutex_tryenter(old = l->l_mutex))
1175 return 0;
1176 if (__predict_true(l->l_mutex == old))
1177 return 1;
1178 mutex_spin_exit(old);
1179 }
1180 #else
1181 return mutex_tryenter(l->l_mutex);
1182 #endif
1183 }
1184
1185 /*
1186 * Handle exceptions for mi_userret(). Called if a member of LW_USERRET is
1187 * set.
1188 */
1189 void
1190 lwp_userret(struct lwp *l)
1191 {
1192 struct proc *p;
1193 void (*hook)(void);
1194 int sig;
1195
1196 p = l->l_proc;
1197
1198 /*
1199 * It should be safe to do this read unlocked on a multiprocessor
1200 * system..
1201 */
1202 while ((l->l_flag & LW_USERRET) != 0) {
1203 /*
1204 * Process pending signals first, unless the process
1205 * is dumping core or exiting, where we will instead
1206 * enter the L_WSUSPEND case below.
1207 */
1208 if ((l->l_flag & (LW_PENDSIG | LW_WCORE | LW_WEXIT)) ==
1209 LW_PENDSIG) {
1210 KERNEL_LOCK(1, l); /* XXXSMP pool_put() below */
1211 mutex_enter(&p->p_smutex);
1212 while ((sig = issignal(l)) != 0)
1213 postsig(sig);
1214 mutex_exit(&p->p_smutex);
1215 KERNEL_UNLOCK_LAST(l); /* XXXSMP */
1216 }
1217
1218 /*
1219 * Core-dump or suspend pending.
1220 *
1221 * In case of core dump, suspend ourselves, so that the
1222 * kernel stack and therefore the userland registers saved
1223 * in the trapframe are around for coredump() to write them
1224 * out. We issue a wakeup on p->p_lwpcv so that sigexit()
1225 * will write the core file out once all other LWPs are
1226 * suspended.
1227 */
1228 if ((l->l_flag & LW_WSUSPEND) != 0) {
1229 mutex_enter(&p->p_smutex);
1230 p->p_nrlwps--;
1231 cv_broadcast(&p->p_lwpcv);
1232 lwp_lock(l);
1233 l->l_stat = LSSUSPENDED;
1234 mutex_exit(&p->p_smutex);
1235 mi_switch(l);
1236 }
1237
1238 /* Process is exiting. */
1239 if ((l->l_flag & LW_WEXIT) != 0) {
1240 KERNEL_LOCK(1, l);
1241 lwp_exit(l);
1242 KASSERT(0);
1243 /* NOTREACHED */
1244 }
1245
1246 /* Call userret hook; used by Linux emulation. */
1247 if ((l->l_flag & LW_WUSERRET) != 0) {
1248 lwp_lock(l);
1249 l->l_flag &= ~LW_WUSERRET;
1250 lwp_unlock(l);
1251 hook = p->p_userret;
1252 p->p_userret = NULL;
1253 (*hook)();
1254 }
1255 }
1256 }
1257
1258 /*
1259 * Force an LWP to enter the kernel, to take a trip through lwp_userret().
1260 */
1261 void
1262 lwp_need_userret(struct lwp *l)
1263 {
1264 KASSERT(lwp_locked(l, NULL));
1265
1266 /*
1267 * Since the tests in lwp_userret() are done unlocked, make sure
1268 * that the condition will be seen before forcing the LWP to enter
1269 * kernel mode.
1270 */
1271 mb_write();
1272 cpu_signotify(l);
1273 }
1274
1275 /*
1276 * Add one reference to an LWP. This will prevent the LWP from
1277 * exiting, thus keep the lwp structure and PCB around to inspect.
1278 */
1279 void
1280 lwp_addref(struct lwp *l)
1281 {
1282
1283 KASSERT(mutex_owned(&l->l_proc->p_smutex));
1284 KASSERT(l->l_stat != LSZOMB);
1285 KASSERT(l->l_refcnt != 0);
1286
1287 l->l_refcnt++;
1288 }
1289
1290 /*
1291 * Remove one reference to an LWP. If this is the last reference,
1292 * then we must finalize the LWP's death.
1293 */
1294 void
1295 lwp_delref(struct lwp *l)
1296 {
1297 struct proc *p = l->l_proc;
1298
1299 mutex_enter(&p->p_smutex);
1300 if (--l->l_refcnt == 0)
1301 cv_broadcast(&p->p_refcv);
1302 mutex_exit(&p->p_smutex);
1303 }
1304
1305 /*
1306 * Drain all references to the current LWP.
1307 */
1308 void
1309 lwp_drainrefs(struct lwp *l)
1310 {
1311 struct proc *p = l->l_proc;
1312
1313 KASSERT(mutex_owned(&p->p_smutex));
1314 KASSERT(l->l_refcnt != 0);
1315
1316 l->l_refcnt--;
1317 while (l->l_refcnt != 0)
1318 cv_wait(&p->p_refcv, &p->p_smutex);
1319 }
1320
1321 /*
1322 * lwp_specific_key_create --
1323 * Create a key for subsystem lwp-specific data.
1324 */
1325 int
1326 lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
1327 {
1328
1329 return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
1330 }
1331
1332 /*
1333 * lwp_specific_key_delete --
1334 * Delete a key for subsystem lwp-specific data.
1335 */
1336 void
1337 lwp_specific_key_delete(specificdata_key_t key)
1338 {
1339
1340 specificdata_key_delete(lwp_specificdata_domain, key);
1341 }
1342
1343 /*
1344 * lwp_initspecific --
1345 * Initialize an LWP's specificdata container.
1346 */
1347 void
1348 lwp_initspecific(struct lwp *l)
1349 {
1350 int error;
1351
1352 error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
1353 KASSERT(error == 0);
1354 }
1355
1356 /*
1357 * lwp_finispecific --
1358 * Finalize an LWP's specificdata container.
1359 */
1360 void
1361 lwp_finispecific(struct lwp *l)
1362 {
1363
1364 specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
1365 }
1366
1367 /*
1368 * lwp_getspecific --
1369 * Return lwp-specific data corresponding to the specified key.
1370 *
1371 * Note: LWP specific data is NOT INTERLOCKED. An LWP should access
1372 * only its OWN SPECIFIC DATA. If it is necessary to access another
1373 * LWP's specifc data, care must be taken to ensure that doing so
1374 * would not cause internal data structure inconsistency (i.e. caller
1375 * can guarantee that the target LWP is not inside an lwp_getspecific()
1376 * or lwp_setspecific() call).
1377 */
1378 void *
1379 lwp_getspecific(specificdata_key_t key)
1380 {
1381
1382 return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
1383 &curlwp->l_specdataref, key));
1384 }
1385
1386 void *
1387 _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
1388 {
1389
1390 return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
1391 &l->l_specdataref, key));
1392 }
1393
1394 /*
1395 * lwp_setspecific --
1396 * Set lwp-specific data corresponding to the specified key.
1397 */
1398 void
1399 lwp_setspecific(specificdata_key_t key, void *data)
1400 {
1401
1402 specificdata_setspecific(lwp_specificdata_domain,
1403 &curlwp->l_specdataref, key, data);
1404 }
1405