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