kern_lwp.c revision 1.1.2.8 1 /* $NetBSD: kern_lwp.c,v 1.1.2.8 2002/02/06 19:48:35 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/lwp.h>
44 #include <sys/proc.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 simple_lock(&p->p_lwplock);
243 p->p_nrlwps++;
244 simple_unlock(&p->p_lwplock);
245 SCHED_UNLOCK(s);
246 } else {
247 /* LWP was sleeping before being suspended */
248 t->l_stat = LSSLEEP;
249 }
250
251 return (0);
252 }
253
254 int sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
255 {
256 struct sys__lwp_wakeup_args /* {
257 syscallarg(lwpid_t) wakeup;
258 } */ *uap = v;
259 lwpid_t target_lid;
260 struct lwp *t;
261 struct proc *p;
262
263 p = l->l_proc;
264 target_lid = SCARG(uap, target);
265
266 LIST_FOREACH(t, &p->p_lwps, l_sibling)
267 if (t->l_lid == target_lid)
268 break;
269
270 if (t == NULL)
271 return (ESRCH);
272
273 if (t->l_stat != LSSLEEP)
274 return (ENODEV);
275
276 if ((l->l_flag & L_SINTR) == 0)
277 return (EBUSY);
278
279 setrunnable(l);
280
281 return 0;
282 }
283
284 int
285 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
286 {
287 struct sys__lwp_wait_args /* {
288 syscallarg(lwpid_t) wait_for;
289 syscallarg(lwpid_t *) departed;
290 } */ *uap = v;
291 int error;
292 lwpid_t dep;
293
294 error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
295 if (error)
296 return (error);
297
298 if (SCARG(uap, departed)) {
299 error = copyout(&dep, SCARG(uap, departed),
300 sizeof(dep));
301 if (error)
302 return (error);
303 }
304
305 return (0);
306 }
307
308
309 int
310 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
311 {
312
313 struct proc *p = l->l_proc;
314 struct lwp *l2, *l3;
315 int nfound, error, s, wpri;
316 static char waitstr1[] = "lwpwait";
317 static char waitstr2[] = "lwpwait2";
318
319 DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
320 p->p_pid, l->l_lid, lid));
321
322 if (lid == l->l_lid)
323 return (EDEADLK); /* Waiting for ourselves makes no sense. */
324
325 wpri = PWAIT | PCATCH |
326 ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : 0);
327 loop:
328 nfound = 0;
329 LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
330 if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
331 ((lid != 0) && (lid != l2->l_lid)))
332 continue;
333
334 nfound++;
335 if (l2->l_stat == LSZOMB) {
336 if (departed)
337 *departed = l2->l_lid;
338
339 s = proclist_lock_write();
340 LIST_REMOVE(l2, l_zlist); /* off zomblwp */
341 proclist_unlock_write(s);
342
343 simple_lock(&p->p_lwplock);
344 LIST_REMOVE(l2, l_sibling);
345 p->p_nlwps--;
346 p->p_nzlwps--;
347 simple_unlock(&p->p_lwplock);
348 /* XXX decrement limits */
349
350 pool_put(&lwp_pool, l2);
351
352 return (0);
353 } else if (l2->l_stat == LSSLEEP ||
354 l2->l_stat == LSSUSPENDED) {
355 /* Deadlock checks.
356 * 1. If all other LWPs are waiting for exits
357 * or suspended, we would deadlock.
358 */
359
360 LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
361 if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
362 !(l3->l_stat == LSSLEEP &&
363 l3->l_wchan == (caddr_t) &p->p_nlwps))
364 break;
365 }
366 if (l3 == NULL) /* Everyone else is waiting. */
367 return (EDEADLK);
368
369 /* XXX we'd like to check for a cycle of waiting
370 * LWPs (specific LID waits, not any-LWP waits)
371 * and detect that sort of deadlock, but we don't
372 * have a good place to store the lwp that is
373 * being waited for. wchan is already filled with
374 * &p->p_nlwps, and putting the lwp address in
375 * there for deadlock tracing would require
376 * exiting LWPs to call wakeup on both their
377 * own address and &p->p_nlwps, to get threads
378 * sleeping on any LWP exiting.
379 *
380 * Revisit later. Maybe another auxillary
381 * storage location associated with sleeping
382 * is in order.
383 */
384 }
385 }
386
387 if (nfound == 0)
388 return (ESRCH);
389
390 if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
391 (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
392 return (error);
393
394 goto loop;
395 }
396
397
398 int
399 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr,
400 int flags, void *stack, size_t stacksize,
401 void (*func)(void *), void *arg, struct lwp **rnewlwpp)
402 {
403 struct lwp *l2;
404 int s;
405
406 l2 = pool_get(&lwp_pool, PR_WAITOK);
407
408 l2->l_stat = LSIDL;
409 l2->l_forw = l2->l_back = NULL;
410 l2->l_proc = p2;
411
412
413 memset(&l2->l_startzero, 0,
414 (unsigned) ((caddr_t)&l2->l_endzero -
415 (caddr_t)&l2->l_startzero));
416 memcpy(&l2->l_startcopy, &l1->l_startcopy,
417 (unsigned) ((caddr_t)&l2->l_endcopy -
418 (caddr_t)&l2->l_startcopy));
419
420 #if !defined(MULTIPROCESSOR)
421 /*
422 * In the single-processor case, all processes will always run
423 * on the same CPU. So, initialize the child's CPU to the parent's
424 * now. In the multiprocessor case, the child's CPU will be
425 * initialized in the low-level context switch code when the
426 * process runs.
427 */
428 l2->l_cpu = l1->l_cpu;
429 #else
430 /*
431 * zero child's cpu pointer so we don't get trash.
432 */
433 l2->l_cpu = NULL;
434 #endif /* ! MULTIPROCESSOR */
435
436 l2->l_flag = L_INMEM;
437 l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
438
439 callout_init(&l2->l_tsleep_ch);
440
441 if (rnewlwpp != NULL)
442 *rnewlwpp = l2;
443
444 l2->l_addr = (struct user *)uaddr;
445 uvm_lwp_fork(l1, l2, stack, stacksize, func,
446 (arg != NULL) ? arg : l2);
447
448
449 simple_lock(&p2->p_lwplock);
450 l2->l_lid = ++p2->p_nlwpid;
451 LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
452 p2->p_nlwps++;
453 simple_unlock(&p2->p_lwplock);
454
455 /* XXX should be locked differently... */
456 s = proclist_lock_write();
457 LIST_INSERT_HEAD(&alllwp, l2, l_list);
458 proclist_unlock_write(s);
459
460 return (0);
461 }
462
463
464 /*
465 * Quit the process. This will call cpu_exit, which will call cpu_switch,
466 * so this can only be used meaningfully if you're willing to switch away.
467 * Calling with l!=curproc would be weird.
468 */
469 void
470 lwp_exit(struct lwp *l)
471 {
472 struct proc *p = l->l_proc;
473 int s;
474
475 DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
476 DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
477 p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
478 /*
479 * If we are the last live LWP in a process, we need to exit
480 * the entire process (if that's not already going on). We do
481 * so with an exit status of zero, because it's a "controlled"
482 * exit, and because that's what Solaris does.
483 */
484 if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
485 DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
486 p->p_pid, l->l_lid));
487 exit1(l, 0);
488 }
489
490 if ((l->l_flag & L_SA) && ((p->p_flag & P_WEXIT) == 0)) {
491 /* Recycle, don't exit */
492 SCHED_LOCK(s);
493 p->p_nrlwps--;
494 sa_putcachelwp(p, l);
495 mi_switch(l, NULL);
496 /* This isn't quite a NOTREACHED; we may get here
497 * if the process exits before this LWP is reused. In
498 * that case, we want to run lwp_exit()... conveniently,
499 * we're already there!
500 */
501 KDASSERT(p->p_flag & P_WEXIT);
502 }
503
504 s = proclist_lock_write();
505 LIST_REMOVE(l, l_list);
506 if ((l->l_flag & L_DETACHED) == 0) {
507 DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
508 l->l_lid));
509 LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
510 }
511 proclist_unlock_write(s);
512
513 simple_lock(&p->p_lwplock);
514 p->p_nrlwps--;
515 simple_unlock(&p->p_lwplock);
516
517 l->l_stat = LSDEAD;
518
519 /* cpu_exit() will not return */
520 cpu_exit(l, 0);
521
522 }
523
524
525 void
526 lwp_exit2(struct lwp *l)
527 {
528
529 simple_lock(&deadproc_slock);
530 LIST_INSERT_HEAD(&deadlwp, l, l_list);
531 simple_unlock(&deadproc_slock);
532
533 wakeup(&deadproc);
534 }
535
536 /*
537 * Pick a LWP to represent the process for those operations which
538 * want information about a "process" that is actually associated
539 * with a LWP.
540 */
541 struct lwp *
542 proc_representative_lwp(p)
543 struct proc *p;
544 {
545 struct lwp *l = NULL;
546
547 /* Trivial case: only one LWP */
548 if (p->p_nrlwps == 1)
549 return (LIST_FIRST(&p->p_lwps));
550
551 switch (p->p_stat) {
552 case SSTOP:
553 /* Pick the first stopped LWP */
554 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
555 if (l->l_stat == LSSTOP)
556 return (l);
557 }
558 /* NOTREACHED */
559 break;
560 case SACTIVE:
561 /* Pick the first live LWP */
562 LIST_FOREACH(l, &p->p_lwps, l_sibling) {
563 if (l->l_stat == LSRUN ||
564 l->l_stat == LSSLEEP ||
565 l->l_stat == LSONPROC ||
566 l->l_stat == LSSUSPENDED)
567 return (l);
568 }
569 break;
570 case SDEAD:
571 case SZOMB:
572 /* Doesn't really matter... */
573 l = LIST_FIRST(&p->p_lwps);
574 break;
575 #ifdef DIAGNOSTIC
576 case SIDL:
577 /* We have more than one LWP and we're in SIDL?
578 * How'd that happen?
579 */
580 panic("Too many LWPs (%d) in SIDL process %d (%s)",
581 p->p_nrlwps, p->p_pid, p->p_comm);
582 default:
583 panic("Process %d (%s) in unknown state %d",
584 p->p_pid, p->p_comm, p->p_stat);
585 #endif
586 }
587
588 panic("proc_representative_lwp: couldn't find a lwp for process"
589 " %d (%s)", p->p_pid, p->p_comm);
590 /* NOTREACHED */
591 return NULL;
592 }
593