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