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