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