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