kern_lwp.c revision 1.1.2.12 1 /* $NetBSD: kern_lwp.c,v 1.1.2.12 2002/07/12 01:40:17 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nathan J. Williams.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/pool.h>
42 #include <sys/lock.h>
43 #include <sys/proc.h>
44 #include <sys/sa.h>
45 #include <sys/savar.h>
46 #include <sys/types.h>
47 #include <sys/ucontext.h>
48 #include <sys/resourcevar.h>
49 #include <sys/mount.h>
50 #include <sys/syscallargs.h>
51
52 #include <uvm/uvm_extern.h>
53
54 struct lwplist alllwp;
55 struct lwplist deadlwp;
56 struct lwplist zomblwp;
57
58 #define LWP_DEBUG
59
60 #ifdef LWP_DEBUG
61 int lwp_debug = 0;
62 #define DPRINTF(x) if (lwp_debug) printf x
63 #else
64 #define DPRINTF(x)
65 #endif
66 /* ARGSUSED */
67 int
68 sys__lwp_create(struct lwp *l, void *v, register_t *retval)
69 {
70 struct sys__lwp_create_args /* {
71 syscallarg(const ucontext_t *) ucp;
72 syscallarg(u_long) flags;
73 syscallarg(lwpid_t *) new_lwp;
74 } */ *uap = v;
75 struct proc *p = l->l_proc;
76 struct lwp *l2;
77 vaddr_t uaddr;
78 ucontext_t *newuc;
79 int s, error;
80
81 newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
82
83 error = copyin(SCARG(uap, ucp), newuc, sizeof(*newuc));
84 if (error)
85 return (error);
86
87 /* XXX check against resource limits */
88
89 uaddr = uvm_km_valloc(kernel_map, USPACE);
90 if (__predict_false(uaddr == 0)) {
91 return (ENOMEM);
92 }
93
94 /* XXX flags:
95 * __LWP_ASLWP is probably needed for Solaris compat.
96 */
97
98 newlwp(l, p, uaddr,
99 SCARG(uap, flags) & LWP_DETACHED,
100 NULL, NULL, startlwp, newuc, &l2);
101
102 if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
103 SCHED_LOCK(s);
104 l2->l_stat = LSRUN;
105 setrunqueue(l2);
106 SCHED_UNLOCK(s);
107 simple_lock(&p->p_lwplock);
108 p->p_nrlwps++;
109 simple_unlock(&p->p_lwplock);
110 } else {
111 l2->l_stat = LSSUSPENDED;
112 }
113
114 error = copyout(&l2->l_lid, SCARG(uap, new_lwp),
115 sizeof(l2->l_lid));
116 if (error)
117 return (error);
118
119 return (0);
120 }
121
122
123 int
124 sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
125 {
126
127 lwp_exit(l);
128 /* NOTREACHED */
129 return (0);
130 }
131
132
133 int
134 sys__lwp_self(struct lwp *l, void *v, register_t *retval)
135 {
136
137 *retval = l->l_lid;
138
139 return (0);
140 }
141
142
143 int
144 sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
145 {
146 struct sys__lwp_suspend_args /* {
147 syscallarg(lwpid_t) target;
148 } */ *uap = v;
149 int target_lid;
150 struct proc *p = l->l_proc;
151 struct lwp *t, *t2;
152 int s;
153
154 target_lid = SCARG(uap, target);
155
156 LIST_FOREACH(t, &p->p_lwps, l_sibling)
157 if (t->l_lid == target_lid)
158 break;
159
160 if (t == NULL)
161 return (ESRCH);
162
163 if (t == l) {
164 /*
165 * Check for deadlock, which is only possible
166 * when we're suspending ourself.
167 */
168 LIST_FOREACH(t2, &p->p_lwps, l_sibling) {
169 if ((t2 != l) && (t2->l_stat != LSSUSPENDED))
170 break;
171 }
172
173 if (t2 == NULL) /* All other LWPs are suspended */
174 return (EDEADLK);
175
176 SCHED_LOCK(s);
177 l->l_stat = LSSUSPENDED;
178 /* XXX NJWLWP check if this makes sense here: */
179 l->l_proc->p_stats->p_ru.ru_nvcsw++;
180 mi_switch(l, NULL);
181 SCHED_ASSERT_UNLOCKED();
182 } else {
183 switch (t->l_stat) {
184 case LSSUSPENDED:
185 return (0); /* _lwp_suspend() is idempotent */
186 case LSRUN:
187 SCHED_LOCK(s);
188 remrunqueue(t);
189 t->l_stat = LSSUSPENDED;
190 SCHED_UNLOCK(s);
191 simple_lock(&p->p_lwplock);
192 p->p_nrlwps--;
193 simple_unlock(&p->p_lwplock);
194 break;
195 case LSSLEEP:
196 t->l_stat = LSSUSPENDED;
197 break;
198 case LSIDL:
199 case LSDEAD:
200 case LSZOMB:
201 return (EINTR); /* It's what Solaris does..... */
202 case LSSTOP:
203 panic("_lwp_suspend: Stopped LWP in running process!");
204 break;
205 case LSONPROC:
206 panic("XXX multiprocessor LWPs? Implement me!");
207 break;
208 }
209 }
210
211 return (0);
212 }
213
214
215 int
216 sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
217 {
218 struct sys__lwp_continue_args /* {
219 syscallarg(lwpid_t) target;
220 } */ *uap = v;
221 int target_lid;
222 struct proc *p = l->l_proc;
223 struct lwp *t;
224 int s;
225
226 target_lid = SCARG(uap, target);
227
228 LIST_FOREACH(t, &p->p_lwps, l_sibling)
229 if (t->l_lid == target_lid)
230 break;
231
232 if (t == NULL)
233 return (ESRCH);
234
235 if (t->l_stat != LSSUSPENDED)
236 return (0);
237
238 if (t->l_wchan == 0) {
239 /* LWP was runnable before being suspended. */
240 SCHED_LOCK(s);
241 setrunnable(t);
242 SCHED_UNLOCK(s);
243 } else {
244 /* LWP was sleeping before being suspended */
245 t->l_stat = LSSLEEP;
246 }
247
248 return (0);
249 }
250
251 int sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
252 {
253 struct sys__lwp_wakeup_args /* {
254 syscallarg(lwpid_t) wakeup;
255 } */ *uap = v;
256 lwpid_t target_lid;
257 struct lwp *t;
258 struct proc *p;
259
260 p = l->l_proc;
261 target_lid = SCARG(uap, target);
262
263 LIST_FOREACH(t, &p->p_lwps, l_sibling)
264 if (t->l_lid == target_lid)
265 break;
266
267 if (t == NULL)
268 return (ESRCH);
269
270 if (t->l_stat != LSSLEEP)
271 return (ENODEV);
272
273 if ((l->l_flag & L_SINTR) == 0)
274 return (EBUSY);
275
276 setrunnable(l);
277
278 return 0;
279 }
280
281 int
282 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
283 {
284 struct sys__lwp_wait_args /* {
285 syscallarg(lwpid_t) wait_for;
286 syscallarg(lwpid_t *) departed;
287 } */ *uap = v;
288 int error;
289 lwpid_t dep;
290
291 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
292 if (error)
293 return (error);
294
295 if (SCARG(uap, departed)) {
296 error = copyout(&dep, SCARG(uap, departed),
297 sizeof(dep));
298 if (error)
299 return (error);
300 }
301
302 return (0);
303 }
304
305
306 int
307 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
308 {
309
310 struct proc *p = l->l_proc;
311 struct lwp *l2, *l3;
312 int nfound, error, s, wpri;
313 static char waitstr1[] = "lwpwait";
314 static char waitstr2[] = "lwpwait2";
315
316 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
317 p->p_pid, l->l_lid, lid));
318
319 if (lid == l->l_lid)
320 return (EDEADLK); /* Waiting for ourselves makes no sense. */
321
322 wpri = PWAIT | PCATCH |
323 ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : 0);
324 loop:
325 nfound = 0;
326 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
327 if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
328 ((lid != 0) && (lid != l2->l_lid)))
329 continue;
330
331 nfound++;
332 if (l2->l_stat == LSZOMB) {
333 if (departed)
334 *departed = l2->l_lid;
335
336 s = proclist_lock_write();
337 LIST_REMOVE(l2, l_zlist); /* off zomblwp */
338 proclist_unlock_write(s);
339
340 simple_lock(&p->p_lwplock);
341 LIST_REMOVE(l2, l_sibling);
342 p->p_nlwps--;
343 p->p_nzlwps--;
344 simple_unlock(&p->p_lwplock);
345 /* XXX decrement limits */
346
347 pool_put(&lwp_pool, l2);
348
349 return (0);
350 } else if (l2->l_stat == LSSLEEP ||
351 l2->l_stat == LSSUSPENDED) {
352 /* Deadlock checks.
353 * 1. If all other LWPs are waiting for exits
354 * or suspended, we would deadlock.
355 */
356
357 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
358 if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
359 !(l3->l_stat == LSSLEEP &&
360 l3->l_wchan == (caddr_t) &p->p_nlwps))
361 break;
362 }
363 if (l3 == NULL) /* Everyone else is waiting. */
364 return (EDEADLK);
365
366 /* XXX we'd like to check for a cycle of waiting
367 * LWPs (specific LID waits, not any-LWP waits)
368 * and detect that sort of deadlock, but we don't
369 * have a good place to store the lwp that is
370 * being waited for. wchan is already filled with
371 * &p->p_nlwps, and putting the lwp address in
372 * there for deadlock tracing would require
373 * exiting LWPs to call wakeup on both their
374 * own address and &p->p_nlwps, to get threads
375 * sleeping on any LWP exiting.
376 *
377 * Revisit later. Maybe another auxillary
378 * storage location associated with sleeping
379 * is in order.
380 */
381 }
382 }
383
384 if (nfound == 0)
385 return (ESRCH);
386
387 if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
388 (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
389 return (error);
390
391 goto loop;
392 }
393
394
395 int
396 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr,
397 int flags, void *stack, size_t stacksize,
398 void (*func)(void *), void *arg, struct lwp **rnewlwpp)
399 {
400 struct lwp *l2;
401 int s;
402
403 l2 = pool_get(&lwp_pool, PR_WAITOK);
404
405 l2->l_stat = LSIDL;
406 l2->l_forw = l2->l_back = NULL;
407 l2->l_proc = p2;
408
409
410 memset(&l2->l_startzero, 0,
411 (unsigned) ((caddr_t)&l2->l_endzero -
412 (caddr_t)&l2->l_startzero));
413 memcpy(&l2->l_startcopy, &l1->l_startcopy,
414 (unsigned) ((caddr_t)&l2->l_endcopy -
415 (caddr_t)&l2->l_startcopy));
416
417 #if !defined(MULTIPROCESSOR)
418 /*
419 * In the single-processor case, all processes will always run
420 * on the same CPU. So, initialize the child's CPU to the parent's
421 * now. In the multiprocessor case, the child's CPU will be
422 * initialized in the low-level context switch code when the
423 * process runs.
424 */
425 l2->l_cpu = l1->l_cpu;
426 #else
427 /*
428 * zero child's cpu pointer so we don't get trash.
429 */
430 l2->l_cpu = NULL;
431 #endif /* ! MULTIPROCESSOR */
432
433 l2->l_flag = L_INMEM;
434 l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
435
436 callout_init(&l2->l_tsleep_ch);
437
438 if (rnewlwpp != NULL)
439 *rnewlwpp = l2;
440
441 l2->l_addr = (struct user *)uaddr;
442 uvm_lwp_fork(l1, l2, stack, stacksize, func,
443 (arg != NULL) ? arg : l2);
444
445
446 simple_lock(&p2->p_lwplock);
447 l2->l_lid = ++p2->p_nlwpid;
448 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
449 p2->p_nlwps++;
450 simple_unlock(&p2->p_lwplock);
451
452 /* XXX should be locked differently... */
453 s = proclist_lock_write();
454 LIST_INSERT_HEAD(&alllwp, l2, l_list);
455 proclist_unlock_write(s);
456
457 return (0);
458 }
459
460
461 /*
462 * Quit the process. This will call cpu_exit, which will call cpu_switch,
463 * so this can only be used meaningfully if you're willing to switch away.
464 * Calling with l!=curlwp would be weird.
465 */
466 void
467 lwp_exit(struct lwp *l)
468 {
469 struct proc *p = l->l_proc;
470 int s;
471
472 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
473 DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
474 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
475 /*
476 * If we are the last live LWP in a process, we need to exit
477 * the entire process (if that's not already going on). We do
478 * so with an exit status of zero, because it's a "controlled"
479 * exit, and because that's what Solaris does.
480 */
481 if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
482 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
483 p->p_pid, l->l_lid));
484 exit1(l, 0);
485 }
486
487 if ((l->l_flag & L_SA) && ((p->p_flag & P_WEXIT) == 0)) {
488 /* Recycle, don't exit */
489 SCHED_LOCK(s);
490 p->p_nrlwps--;
491 sa_putcachelwp(p, l);
492 mi_switch(l, NULL);
493 /* This isn't quite a NOTREACHED; we may get here
494 * if the process exits before this LWP is reused. In
495 * that case, we want to run lwp_exit()... conveniently,
496 * we're already there!
497 */
498 KDASSERT(p->p_flag & P_WEXIT);
499 }
500
501 s = proclist_lock_write();
502 LIST_REMOVE(l, l_list);
503 if ((l->l_flag & L_DETACHED) == 0) {
504 DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
505 l->l_lid));
506 LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
507 }
508 proclist_unlock_write(s);
509
510 simple_lock(&p->p_lwplock);
511 p->p_nrlwps--;
512 simple_unlock(&p->p_lwplock);
513
514 l->l_stat = LSDEAD;
515
516 /* cpu_exit() will not return */
517 cpu_exit(l, 0);
518
519 }
520
521
522 void
523 lwp_exit2(struct lwp *l)
524 {
525
526 simple_lock(&deadproc_slock);
527 LIST_INSERT_HEAD(&deadlwp, l, l_list);
528 simple_unlock(&deadproc_slock);
529
530 wakeup(&deadproc);
531 }
532
533 /*
534 * Pick a LWP to represent the process for those operations which
535 * want information about a "process" that is actually associated
536 * with a LWP.
537 */
538 struct lwp *
539 proc_representative_lwp(p)
540 struct proc *p;
541 {
542 struct lwp *l = NULL;
543
544 /* Trivial case: only one LWP */
545 if (p->p_nrlwps == 1)
546 return (LIST_FIRST(&p->p_lwps));
547
548 switch (p->p_stat) {
549 case SSTOP:
550 /* Pick the first stopped LWP */
551 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
552 if (l->l_stat == LSSTOP)
553 return (l);
554 }
555 /* NOTREACHED */
556 break;
557 case SACTIVE:
558 /* Pick the first live LWP */
559 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
560 if (l->l_stat == LSRUN ||
561 l->l_stat == LSSLEEP ||
562 l->l_stat == LSONPROC ||
563 l->l_stat == LSSUSPENDED)
564 return (l);
565 }
566 break;
567 case SDEAD:
568 case SZOMB:
569 /* Doesn't really matter... */
570 l = LIST_FIRST(&p->p_lwps);
571 break;
572 #ifdef DIAGNOSTIC
573 case SIDL:
574 /* We have more than one LWP and we're in SIDL?
575 * How'd that happen?
576 */
577 panic("Too many LWPs (%d) in SIDL process %d (%s)",
578 p->p_nrlwps, p->p_pid, p->p_comm);
579 default:
580 panic("Process %d (%s) in unknown state %d",
581 p->p_pid, p->p_comm, p->p_stat);
582 #endif
583 }
584
585 panic("proc_representative_lwp: couldn't find a lwp for process"
586 " %d (%s)", p->p_pid, p->p_comm);
587 /* NOTREACHED */
588 return NULL;
589 }
590