kern_lwp.c revision 1.40.2.4 1 /* $NetBSD: kern_lwp.c,v 1.40.2.4 2006/11/17 16:34:36 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2006 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 * LSZOMB
84 *
85 * Zombie: the LWP has exited, released all of its resources
86 * and can execute no further. It will persist until 'reaped'
87 * by another LWP or process via the _lwp_wait() or wait()
88 * system calls.
89 *
90 * LSSUSPENDED:
91 *
92 * Suspended: the LWP has had its execution suspended by
93 * another LWP in the same process using the _lwp_suspend()
94 * system call. User-level LWPs also enter the suspended
95 * state when the system is shutting down.
96 *
97 * The second set represent a "statement of intent" on behalf of the
98 * LWP. The LWP may in fact be executing on a processor, may be
99 * sleeping, idle, or on a run queue. It is expected to take the
100 * necessary action to stop executing or become "running" again within
101 * a short timeframe.
102 *
103 * LSDEAD:
104 *
105 * Dead: the LWP has released most of its resources and is
106 * about to switch away into oblivion. When it switches away,
107 * its few remaining resources will be collected and the LWP
108 * will enter the LSZOMB (zombie) state.
109 *
110 * LSSLEEP:
111 *
112 * Sleeping: the LWP has entered itself onto a sleep queue, and
113 * will switch away shortly to allow other LWPs to run on the
114 * CPU.
115 *
116 * LSSTOP:
117 *
118 * Stopped: the LWP has been stopped as a result of a job
119 * control signal, or as a result of the ptrace() interface.
120 * Stopped LWPs may run briefly within the kernel to handle
121 * signals that they receive, but will not return to user space
122 * until their process' state is changed away from stopped.
123 * Single LWPs within a process can not be set stopped
124 * selectively: all actions that can stop or continue LWPs
125 * occur at the process level.
126 *
127 * State transitions
128 *
129 * Note that the LSSTOP and LSSUSPENDED states may only be set
130 * when returning to user space in userret(), or when sleeping
131 * interruptably. Before setting those states, we try to ensure
132 * that the LWPs will release all kernel locks that they hold,
133 * and at a minimum try to ensure that the LWP can be set runnable
134 * again by a signal.
135 *
136 * LWPs may transition states in the following ways:
137 *
138 * IDL -------> SUSPENDED DEAD -------> ZOMBIE
139 * > RUN
140 *
141 * RUN -------> ONPROC ONPROC -----> RUN
142 * > STOPPED > SLEEP
143 * > SUSPENDED > STOPPED
144 * > SUSPENDED
145 * > DEAD
146 *
147 * STOPPED ---> RUN SUSPENDED --> RUN
148 * > SLEEP > SLEEP
149 *
150 * SLEEP -----> ONPROC
151 * > RUN
152 * > STOPPED
153 * > SUSPENDED
154 *
155 * Locking
156 *
157 * The majority of fields in 'struct lwp' are covered by a single,
158 * general spin mutex pointed to by lwp::l_mutex. The locks covering
159 * each field are documented in sys/lwp.h.
160 *
161 * State transitions must be made with the LWP's general lock held. In
162 * a multiprocessor kernel, state transitions may cause the LWP's lock
163 * pointer to change. On uniprocessor kernels, most scheduler and
164 * synchronisation objects such as sleep queues and LWPs are protected
165 * by only one mutex (sched_mutex). In this case, LWPs' lock pointers
166 * will never change and will always reference sched_mutex.
167 *
168 * Manipulation of the general lock is not performed directly, but
169 * through calls to lwp_lock(), lwp_relock() and similar.
170 *
171 * States and their associated locks:
172 *
173 * LSIDL, LSDEAD, LSZOMB
174 *
175 * Always covered by lwp_mutex (the idle mutex).
176 *
177 * LSONPROC, LSRUN:
178 *
179 * Always covered by sched_mutex, which protects the run queues
180 * and other miscellaneous items. If the scheduler is changed
181 * to use per-CPU run queues, this may become a per-CPU mutex.
182 *
183 * LSSLEEP:
184 *
185 * Covered by a mutex associated with the sleep queue that the
186 * LWP resides on, indirectly referenced by l_sleepq->sq_mutex.
187 *
188 * LSSTOP, LSSUSPENDED:
189 *
190 * If the LWP was previously sleeping (l_wchan != NULL), then
191 * l_mutex references the sleep queue mutex. If the LWP was
192 * runnable or on the CPU when halted, or has been removed from
193 * the sleep queue since halted, then the mutex is lwp_mutex.
194 *
195 * The lock order for the various mutexes is as follows:
196 *
197 * sleepq_t::sq_mutex -> lwp_mutex -> sched_mutex
198 *
199 * Each process has an scheduler state mutex (proc::p_smutex), and a
200 * number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
201 * so on. When an LWP is to be entered into or removed from one of the
202 * following states, p_mutex must be held and the process wide counters
203 * adjusted:
204 *
205 * LSIDL, LSDEAD, LSZOMB, LSSTOP, LSSUSPENDED
206 *
207 * Note that an LWP is considered running or likely to run soon if in
208 * one of the following states. This affects the value of p_nrlwps:
209 *
210 * LSRUN, LSONPROC, LSSLEEP
211 *
212 * p_smutex does not need to be held when transitioning among these
213 * three states.
214 */
215
216 #include <sys/cdefs.h>
217 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.40.2.4 2006/11/17 16:34:36 ad Exp $");
218
219 #include "opt_multiprocessor.h"
220 #include "opt_lockdebug.h"
221
222 #include <sys/param.h>
223 #include <sys/systm.h>
224 #include <sys/pool.h>
225 #include <sys/proc.h>
226 #include <sys/sa.h>
227 #include <sys/syscallargs.h>
228 #include <sys/kauth.h>
229 #include <sys/sleepq.h>
230 #include <sys/lockdebug.h>
231
232 #include <uvm/uvm_extern.h>
233
234 struct lwplist alllwp;
235 kmutex_t alllwp_mutex;
236 kmutex_t lwp_mutex;
237
238 #define LWP_DEBUG
239
240 #ifdef LWP_DEBUG
241 int lwp_debug = 0;
242 #define DPRINTF(x) if (lwp_debug) printf x
243 #else
244 #define DPRINTF(x)
245 #endif
246
247 /*
248 * Set an LWP halted or suspended.
249 *
250 * Must be called with p_smutex held, and the LWP locked. Will unlock the
251 * LWP before return.
252 */
253 int
254 lwp_halt(struct lwp *curl, struct lwp *t, int state)
255 {
256 int error, want;
257
258 LOCK_ASSERT(mutex_owned(&t->l_proc->p_smutex)); /* XXXAD what now? */
259 LOCK_ASSERT(lwp_locked(t, NULL));
260
261 KASSERT(curl != t || curl->l_stat == LSONPROC);
262
263 /*
264 * If the current LWP has been told to exit, we must not suspend anyone
265 * else or deadlock could occur. We won't return to userspace.
266 */
267 if ((curl->l_stat & (L_WEXIT | L_WCORE)) != 0)
268 return (EDEADLK);
269
270 error = 0;
271
272 want = (state == LSSUSPENDED ? L_WSUSPEND : 0);
273
274 switch (t->l_stat) {
275 case LSRUN:
276 case LSONPROC:
277 t->l_flag |= want;
278 signotify(t);
279 break;
280
281 case LSSLEEP:
282 t->l_stat |= want;
283
284 /*
285 * Kick the LWP and try to get it to the kernel boundary
286 * so that it will release any locks that it holds.
287 * setrunnable() will release the lock.
288 */
289 signotify(t);
290 setrunnable(t);
291 return 0;
292
293 case LSSUSPENDED:
294 case LSSTOP:
295 t->l_flag |= want;
296 break;
297
298 case LSIDL:
299 case LSZOMB:
300 case LSDEAD:
301 error = EINTR; /* It's what Solaris does..... */
302 break;
303 }
304
305 lwp_unlock(t);
306
307 return (error);
308 }
309
310 /*
311 * Restart a suspended LWP.
312 *
313 * Must be called with p_smutex held, and the LWP locked. Will unlock the
314 * LWP before return.
315 */
316 void
317 lwp_continue(struct lwp *l)
318 {
319
320 LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
321 LOCK_ASSERT(lwp_locked(l, NULL));
322
323 DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
324 l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
325 l->l_wchan));
326
327 /* If rebooting or not suspended, then just bail out. */
328 if ((l->l_flag & L_WREBOOT) != 0) {
329 lwp_unlock(l);
330 return;
331 }
332
333 l->l_flag &= ~L_WSUSPEND;
334
335 if (l->l_stat != LSSUSPENDED) {
336 lwp_unlock(l);
337 return;
338 }
339
340 /* setrunnable() will release the lock. */
341 setrunnable(l);
342 }
343
344 /*
345 * Wait for an LWP within the current process to exit. If 'lid' is
346 * non-zero, we are waiting for a specific LWP.
347 *
348 * Must be called with p->p_smutex held.
349 */
350 int
351 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
352 {
353 struct proc *p = l->l_proc;
354 struct lwp *l2;
355 int nfound, error, wpri;
356 static const char waitstr1[] = "lwpwait";
357 static const char waitstr2[] = "lwpwait2";
358
359 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
360 p->p_pid, l->l_lid, lid));
361
362 LOCK_ASSERT(mutex_owned(&p->p_smutex));
363
364 /*
365 * Check for deadlock:
366 *
367 * 1) If all other LWPs are waiting for exits or suspended.
368 * 2) If we are trying to wait on ourself.
369 *
370 * XXX we'd like to check for a cycle of waiting LWPs (specific LID
371 * waits, not any-LWP waits) and detect that sort of deadlock, but
372 * we don't have a good place to store the lwp that is being waited
373 * for. wchan is already filled with &p->p_nlwps, and putting the
374 * lwp address in there for deadlock tracing would require exiting
375 * LWPs to call wakeup on both their own address and &p->p_nlwps, to
376 * get threads sleeping on any LWP exiting.
377 */
378 if (lwp_lastlive(p->p_nlwpwait) || lid == l->l_lid)
379 return (EDEADLK);
380
381 p->p_nlwpwait++;
382 wpri = PWAIT;
383 if ((flags & LWPWAIT_EXITCONTROL) == 0)
384 wpri |= PCATCH;
385 loop:
386 nfound = 0;
387 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
388 if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
389 ((lid != 0) && (lid != l2->l_lid)))
390 continue;
391 nfound++;
392 if (l2->l_stat != LSZOMB)
393 continue;
394
395 if (departed)
396 *departed = l2->l_lid;
397
398 LIST_REMOVE(l2, l_sibling);
399 p->p_nlwps--;
400 p->p_nzlwps--;
401 p->p_nlwpwait--;
402 /* XXX decrement limits */
403 pool_put(&lwp_pool, l2);
404 return (0);
405 }
406
407 if (nfound == 0) {
408 p->p_nlwpwait--;
409 return (ESRCH);
410 }
411
412 if ((error = mtsleep(&p->p_nlwps, wpri,
413 (lid != 0) ? waitstr1 : waitstr2, 0, &p->p_smutex)) != 0)
414 return (error);
415
416 goto loop;
417 }
418
419 /*
420 * Create a new LWP within process 'p2', using LWP 'l1' as a template.
421 * The new LWP is created in state LSIDL and must be set running,
422 * suspended, or stopped by the caller.
423 */
424 int
425 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem,
426 int flags, void *stack, size_t stacksize,
427 void (*func)(void *), void *arg, struct lwp **rnewlwpp)
428 {
429 struct lwp *l2;
430
431 l2 = pool_get(&lwp_pool, PR_WAITOK);
432
433 l2->l_stat = LSIDL;
434 l2->l_forw = l2->l_back = NULL;
435 l2->l_proc = p2;
436 l2->l_refcnt = 1;
437
438 memset(&l2->l_startzero, 0,
439 (unsigned) ((caddr_t)&l2->l_endzero -
440 (caddr_t)&l2->l_startzero));
441
442 /* The copy here is unlocked, but is unlikely to pose a problem. */
443 memcpy(&l2->l_startcopy, &l1->l_startcopy,
444 (unsigned) ((caddr_t)&l2->l_endcopy -
445 (caddr_t)&l2->l_startcopy));
446
447 #if !defined(MULTIPROCESSOR)
448 /*
449 * In the single-processor case, all processes will always run
450 * on the same CPU. So, initialize the child's CPU to the parent's
451 * now. In the multiprocessor case, the child's CPU will be
452 * initialized in the low-level context switch code when the
453 * process runs.
454 */
455 KASSERT(l1->l_cpu != NULL);
456 l2->l_cpu = l1->l_cpu;
457 #else
458 /*
459 * Zero child's CPU pointer so we don't get trash.
460 */
461 l2->l_cpu = NULL;
462 #endif /* ! MULTIPROCESSOR */
463
464 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
465 l2->l_mutex = &lwp_mutex;
466 #else
467 l2->l_mutex = &sched_mutex;
468 #endif
469
470 l2->l_flag = inmem ? L_INMEM : 0;
471 l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
472
473 if (p2->p_flag & P_SYSTEM) {
474 /*
475 * Mark it as a system process and not a candidate for
476 * swapping.
477 */
478 l2->l_flag |= L_SYSTEM | L_INMEM;
479 }
480
481 lwp_update_creds(l2);
482 callout_init(&l2->l_tsleep_ch);
483 l2->l_ts = pool_cache_get(&turnstile_cache, PR_WAITOK);
484 l2->l_syncobj = &sched_syncobj;
485
486 if (rnewlwpp != NULL)
487 *rnewlwpp = l2;
488
489 l2->l_addr = UAREA_TO_USER(uaddr);
490 uvm_lwp_fork(l1, l2, stack, stacksize, func,
491 (arg != NULL) ? arg : l2);
492
493 mutex_enter(&p2->p_smutex);
494
495 if ((p2->p_flag & P_SA) == 0) {
496 l2->l_sigpend = &l2->l_sigstore.ss_pend;
497 l2->l_sigmask = &l2->l_sigstore.ss_mask;
498 l2->l_sigstk = &l2->l_sigstore.ss_stk;
499 l2->l_sigmask = l1->l_sigmask;
500 CIRCLEQ_INIT(&l2->l_sigpend->sp_info);
501 sigemptyset(&l2->l_sigpend->sp_set);
502 } else {
503 l2->l_sigpend = &p2->p_sigstore.ss_pend;
504 l2->l_sigmask = &p2->p_sigstore.ss_mask;
505 l2->l_sigstk = &p2->p_sigstore.ss_stk;
506 }
507
508 l2->l_lid = ++p2->p_nlwpid;
509 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
510 p2->p_nlwps++;
511
512 mutex_exit(&p2->p_smutex);
513
514 mutex_enter(&alllwp_mutex);
515 LIST_INSERT_HEAD(&alllwp, l2, l_list);
516 mutex_exit(&alllwp_mutex);
517
518 if (p2->p_emul->e_lwp_fork)
519 (*p2->p_emul->e_lwp_fork)(l1, l2);
520
521 return (0);
522 }
523
524 /*
525 * Quit the process. This will call cpu_exit, which will call cpu_switch,
526 * so this can only be used meaningfully if you're willing to switch away.
527 * Calling with l!=curlwp would be weird.
528 */
529 int
530 lwp_exit(struct lwp *l, int checksigs)
531 {
532 struct proc *p = l->l_proc;
533
534 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
535 DPRINTF((" nlwps: %d nzlwps: %d\n", p->p_nlwps, p->p_nzlwps));
536
537 mutex_enter(&p->p_smutex);
538
539 /*
540 * If we've got pending signals that we haven't processed yet, make
541 * sure that we take them before exiting.
542 */
543 if (checksigs && sigispending(l)) {
544 mutex_exit(&p->p_smutex);
545 return ERESTART;
546 }
547
548 if (p->p_emul->e_lwp_exit)
549 (*p->p_emul->e_lwp_exit)(l);
550
551 /*
552 * If we are the last live LWP in a process, we need to exit the
553 * entire process. We do so with an exit status of zero, because
554 * it's a "controlled" exit, and because that's what Solaris does.
555 *
556 * We are not quite a zombie yet, but for accounting purposes we
557 * must increment the count of zombies here.
558 */
559 p->p_nzlwps++;
560 if (p->p_nlwps - p->p_nzlwps == 0) {
561 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
562 p->p_pid, l->l_lid));
563 exit1(l, 0);
564 /* NOTREACHED */
565 }
566
567 lwp_lock(l);
568 if ((l->l_flag & L_DETACHED) != 0) {
569 LIST_REMOVE(l, l_sibling);
570 p->p_nlwps--;
571 curlwp = NULL;
572 l->l_proc = NULL;
573 }
574 l->l_stat = LSDEAD;
575 lwp_unlock_to(l, &lwp_mutex);
576
577 if ((p->p_flag & P_SA) == 0) {
578 /*
579 * Clear any private, pending signals. XXX We may loose
580 * process-wide signals that we didn't want to take.
581 */
582 sigclear(l->l_sigpend, NULL);
583 }
584
585 mutex_exit(&p->p_smutex);
586
587 /*
588 * Remove the LWP from the global list and from the parent process.
589 * Once done, mark it as dead. Nothing should be able to find or
590 * update it past this point.
591 */
592 mutex_enter(&alllwp_mutex);
593 LIST_REMOVE(l, l_list);
594 mutex_exit(&alllwp_mutex);
595
596 /*
597 * Release our cached credentials and collate accounting flags.
598 */
599 kauth_cred_free(l->l_cred);
600 mutex_enter(&p->p_mutex);
601 p->p_acflag |= l->l_acflag;
602 mutex_exit(&p->p_mutex);
603
604 /*
605 * Verify that we hold no locks other than the kernel mutex, and
606 * release our turnstile. We should no longer sleep past this
607 * point.
608 */
609 LOCKDEBUG_BARRIER(&kernel_lock, 0);
610 pool_cache_put(&turnstile_cache, l->l_ts);
611
612 /*
613 * Free MD LWP resources
614 */
615 #ifndef __NO_CPU_LWP_FREE
616 cpu_lwp_free(l, 0);
617 #endif
618 pmap_deactivate(l);
619
620 /*
621 * Release the kernel lock, and switch away into oblivion.
622 */
623 (void)KERNEL_UNLOCK(0, l); /* XXXSMP assert count == 1 */
624 cpu_exit(l);
625
626 /* NOTREACHED */
627 return 0;
628 }
629
630 /*
631 * We are called from cpu_exit() once it is safe to schedule the dead LWP's
632 * resources to be freed (i.e., once we've switched to the idle PCB for the
633 * current CPU).
634 *
635 * NOTE: One must be careful with locking in this routine. It's called from
636 * a critical section in machine-dependent code.
637 */
638 void
639 lwp_exit2(struct lwp *l)
640 {
641 struct proc *p;
642 u_int refcnt;
643
644 /*
645 * If someone holds a reference on the LWP, let them clean us up.
646 */
647 lwp_lock(l);
648 refcnt = --l->l_refcnt;
649 lwp_unlock(l);
650 if (refcnt != 0)
651 return;
652
653 KASSERT(l->l_stat == LSDEAD);
654 KERNEL_LOCK(1, NULL);
655
656 /*
657 * Free the VM resources we're still holding on to.
658 */
659 uvm_lwp_exit(l);
660
661 p = l->l_proc;
662
663 if ((l->l_flag & L_DETACHED) != 0) {
664 /*
665 * Nobody waits for detached LWPs.
666 */
667 pool_put(&lwp_pool, l);
668 (void)KERNEL_UNLOCK(1, NULL);
669
670 /*
671 * If this is the last LWP in the process, wake up the
672 * parent so that it can reap us.
673 */
674 mb_read();
675 if (p->p_nlwps == 0) {
676 KASSERT(p->p_stat == SDEAD);
677 p->p_stat = SZOMB;
678 mb_write();
679
680 /* XXXSMP too much locking */
681 mutex_enter(&proclist_mutex);
682 mutex_enter(&proc_stop_mutex);
683 p = p->p_pptr;
684 p->p_nstopchild++;
685 cv_broadcast(&p->p_waitcv);
686 mutex_exit(&proc_stop_mutex);
687 mutex_exit(&proclist_mutex);
688 }
689 } else {
690 (void)KERNEL_UNLOCK(1, NULL);
691 l->l_stat = LSZOMB;
692 mb_write();
693 mutex_enter(&p->p_smutex);
694 wakeup(&p->p_nlwps);
695 mutex_exit(&p->p_smutex);
696 }
697 }
698
699 /*
700 * Pick a LWP to represent the process for those operations which
701 * want information about a "process" that is actually associated
702 * with a LWP.
703 *
704 * Must be called with p->p_smutex held, and will return the LWP locked.
705 * If 'locking' is false, no locking or lock checks are performed. This
706 * is intended for use by DDB.
707 */
708 struct lwp *
709 proc_representative_lwp(struct proc *p, int *nrlwps, int locking)
710 {
711 struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
712 struct lwp *signalled;
713 int cnt;
714
715 if (locking)
716 LOCK_ASSERT(mutex_owned(&p->p_smutex));
717
718 /* Trivial case: only one LWP */
719 if (p->p_nlwps == 1) {
720 l = LIST_FIRST(&p->p_lwps);
721 if (nrlwps)
722 *nrlwps = (l->l_stat == LSONPROC || LSRUN);
723 if (locking)
724 lwp_lock(l);
725 return l;
726 }
727
728 cnt = 0;
729 switch (p->p_stat) {
730 case SSTOP:
731 case SACTIVE:
732 /* Pick the most live LWP */
733 onproc = running = sleeping = stopped = suspended = NULL;
734 signalled = NULL;
735 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
736 if (locking)
737 lwp_lock(l);
738 if (l->l_lid == p->p_sigctx.ps_lwp)
739 signalled = l;
740 switch (l->l_stat) {
741 case LSONPROC:
742 onproc = l;
743 cnt++;
744 break;
745 case LSRUN:
746 running = l;
747 cnt++;
748 break;
749 case LSSLEEP:
750 sleeping = l;
751 break;
752 case LSSTOP:
753 stopped = l;
754 break;
755 case LSSUSPENDED:
756 suspended = l;
757 break;
758 }
759 if (locking)
760 lwp_unlock(l);
761 }
762 if (nrlwps)
763 *nrlwps = cnt;
764 if (signalled)
765 l = signalled;
766 else if (onproc)
767 l = onproc;
768 else if (running)
769 l = running;
770 else if (sleeping)
771 l = sleeping;
772 else if (stopped)
773 l = stopped;
774 else if (suspended)
775 l = suspended;
776 else
777 break;
778 if (locking)
779 lwp_lock(l);
780 return l;
781 case SZOMB:
782 /* Doesn't really matter... */
783 if (nrlwps)
784 *nrlwps = 0;
785 l = LIST_FIRST(&p->p_lwps);
786 if (locking)
787 lwp_lock(l);
788 return l;
789 #ifdef DIAGNOSTIC
790 case SIDL:
791 if (locking)
792 mutex_exit(&p->p_smutex);
793 /* We have more than one LWP and we're in SIDL?
794 * How'd that happen?
795 */
796 panic("Too many LWPs in SIDL process %d (%s)",
797 p->p_pid, p->p_comm);
798 default:
799 if (locking)
800 mutex_exit(&p->p_smutex);
801 panic("Process %d (%s) in unknown state %d",
802 p->p_pid, p->p_comm, p->p_stat);
803 #endif
804 }
805
806 if (locking)
807 mutex_exit(&p->p_smutex);
808 panic("proc_representative_lwp: couldn't find a lwp for process"
809 " %d (%s)", p->p_pid, p->p_comm);
810 /* NOTREACHED */
811 return NULL;
812 }
813
814 /*
815 * Look up a live LWP within the speicifed process, and return it locked.
816 *
817 * Must be called with p->p_smutex held.
818 */
819 struct lwp *
820 lwp_byid(struct proc *p, int id)
821 {
822 struct lwp *l;
823
824 LOCK_ASSERT(mutex_owned(&p->p_smutex));
825
826 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
827 if (l->l_lid == id)
828 break;
829 }
830
831 if (l != NULL) {
832 lwp_lock(l);
833 if (l->l_stat == LSIDL || l->l_stat == LSZOMB ||
834 l->l_stat == LSDEAD) {
835 lwp_unlock(l);
836 l = NULL;
837 }
838 }
839
840 return l;
841 }
842
843 /*
844 * Update an LWP's cached credentials to mirror the process' master copy.
845 *
846 * This happens early in the syscall path, on user trap, and on LWP
847 * creation. A long-running LWP can also voluntarily choose to update
848 * it's credentials by calling this routine. This may be called from
849 * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
850 */
851 void
852 lwp_update_creds(struct lwp *l)
853 {
854 kauth_cred_t oc;
855 struct proc *p;
856
857 p = l->l_proc;
858 oc = l->l_cred;
859
860 mutex_enter(&p->p_mutex);
861 kauth_cred_hold(p->p_cred);
862 l->l_cred = p->p_cred;
863 mutex_exit(&p->p_mutex);
864 if (oc != NULL)
865 kauth_cred_free(oc);
866 }
867
868 /*
869 * Verify that an LWP is locked, and optionally verify that the lock matches
870 * one we specify.
871 */
872 int
873 lwp_locked(struct lwp *l, kmutex_t *mtx)
874 {
875 kmutex_t *cur = l->l_mutex;
876
877 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
878 return mutex_owned(cur) && (mtx == cur || mtx == NULL);
879 #else
880 return mutex_owned(cur);
881 #endif
882 }
883
884 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
885 /*
886 * Lock an LWP.
887 */
888 void
889 lwp_lock_retry(struct lwp *l, kmutex_t *old)
890 {
891
892 for (;;) {
893 mutex_exit(old);
894 old = l->l_mutex;
895 mutex_enter(old);
896
897 /*
898 * mutex_enter() will have posted a read barrier. Re-test
899 * l->l_mutex. If it has changed, we need to try again.
900 */
901 } while (__predict_false(l->l_mutex != old));
902 }
903 #endif
904
905 /*
906 * Lend a new mutex to an LWP. The old mutex must be held.
907 */
908 void
909 lwp_setlock(struct lwp *l, kmutex_t *new)
910 {
911
912 LOCK_ASSERT(mutex_owned(l->l_mutex));
913
914 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
915 mb_write();
916 l->l_mutex = new;
917 #else
918 (void)new;
919 #endif
920 }
921
922 /*
923 * Lend a new mutex to an LWP, and release the old mutex. The old mutex
924 * must be held.
925 */
926 void
927 lwp_unlock_to(struct lwp *l, kmutex_t *new)
928 {
929 kmutex_t *old;
930
931 LOCK_ASSERT(mutex_owned(l->l_mutex));
932
933 old = l->l_mutex;
934 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
935 mb_write();
936 l->l_mutex = new;
937 #else
938 (void)new;
939 #endif
940 mutex_exit(old);
941 }
942
943 /*
944 * Acquire a new mutex, and dontate it to an LWP. The LWP must already be
945 * locked.
946 */
947 void
948 lwp_relock(struct lwp *l, kmutex_t *new)
949 {
950 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
951 kmutex_t *old;
952 #endif
953
954 LOCK_ASSERT(mutex_owned(l->l_mutex));
955
956 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
957 old = l->l_mutex;
958 if (old != new) {
959 mutex_enter(new);
960 l->l_mutex = new;
961 mutex_exit(old);
962 }
963 #else
964 (void)new;
965 #endif
966 }
967
968 /*
969 * Handle exceptions for mi_userret(). Called if L_USERRET is set.
970 */
971 void
972 lwp_userret(struct lwp *l)
973 {
974 struct proc *p;
975 int sig;
976
977 p = l->l_proc;
978
979 do {
980 /* Process pending signals first. */
981 if ((l->l_flag & L_PENDSIG) != 0) {
982 KERNEL_LOCK(1, l); /* XXXSMP pool_put() below */
983 mutex_enter(&p->p_smutex);
984 while ((sig = issignal(l)) != 0)
985 postsig(sig);
986 mutex_exit(&p->p_smutex);
987 (void)KERNEL_UNLOCK(0, l); /* XXXSMP */
988 }
989
990 /* Core-dump or suspend pending. */
991 if ((l->l_flag & L_WSUSPEND) != 0) {
992 /*
993 * Suspend ourselves, so that the kernel stack and
994 * therefore the userland registers saved in the
995 * trapframe are around for coredump() to write them
996 * out. We issue a wakeup() on p->p_nrlwps so that
997 * sigexit() will write the core file out once all
998 * other LWPs are suspended.
999 */
1000 mutex_enter(&p->p_smutex);
1001 lwp_lock(l);
1002 lwp_relock(l, &lwp_mutex);
1003 p->p_nrlwps--;
1004 wakeup(&p->p_nrlwps);
1005 l->l_stat = LSSUSPENDED;
1006 mutex_exit(&p->p_smutex);
1007 mi_switch(l, NULL);
1008 lwp_lock(l);
1009 }
1010
1011 /* Process is exiting. */
1012 if ((l->l_flag & L_WEXIT) != 0) {
1013 KERNEL_LOCK(1, l);
1014 (void)lwp_exit(l, 0);
1015 KASSERT(0);
1016 /* NOTREACHED */
1017 }
1018 } while ((l->l_flag & L_USERRET) != 0);
1019 }
1020
1021 /*
1022 * Return non-zero if this the last live LWP in the process. Called when
1023 * exiting, dumping core, waiting for other LWPs to exit, etc. Accepts a
1024 * 'bias' value for deadlock detection.
1025 *
1026 * Must be called with p->p_smutex held.
1027 */
1028 int
1029 lwp_lastlive(int bias)
1030 {
1031 struct lwp *l = curlwp;
1032 struct proc *p = l->l_proc;
1033
1034 LOCK_ASSERT(mutex_owned(&p->p_smutex));
1035 KASSERT(l->l_stat == LSONPROC || l->l_stat == LSSTOP);
1036
1037 return p->p_nrlwps - bias - (l->l_stat == LSONPROC) == 0;
1038 }
1039
1040 /*
1041 * Add one reference to an LWP. This will prevent the LWP from
1042 * transitioning from the LSDEAD state into LSZOMB, and thus keep
1043 * the lwp structure and PCB around to inspect.
1044 */
1045 void
1046 lwp_addref(struct lwp *l)
1047 {
1048
1049 LOCK_ASSERT(lwp_locked(l, NULL));
1050 KASSERT(l->l_stat != LSZOMB);
1051 KASSERT(l->l_refcnt != 0);
1052
1053 l->l_refcnt++;
1054 }
1055
1056 /*
1057 * Remove one reference to an LWP. If this is the last reference,
1058 * then we must finalize the LWP's death.
1059 */
1060 void
1061 lwp_delref(struct lwp *l)
1062 {
1063
1064 lwp_exit2(l);
1065 }
1066