Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.11
      1 /*	$NetBSD: kern_lwp.c,v 1.11 2003/07/17 19:05:30 fvdl 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.11 2003/07/17 19:05:30 fvdl 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 	int error;
    302 	int s;
    303 
    304 	p = l->l_proc;
    305 	target_lid = SCARG(uap, target);
    306 
    307 	SCHED_LOCK(s);
    308 
    309 
    310 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    311 		if (t->l_lid == target_lid)
    312 			break;
    313 
    314 	if (t == NULL) {
    315 		error = ESRCH;
    316 		goto exit;
    317 	}
    318 
    319 	if (t->l_stat != LSSLEEP) {
    320 		error = ENODEV;
    321 		goto exit;
    322 	}
    323 
    324 	if ((t->l_flag & L_SINTR) == 0) {
    325 		error = EBUSY;
    326 		goto exit;
    327 	}
    328 
    329 	setrunnable(t);
    330 	error = 0;
    331 exit:
    332 	SCHED_UNLOCK(s);
    333 
    334 
    335 	return error;
    336 }
    337 
    338 int
    339 sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    340 {
    341 	struct sys__lwp_wait_args /* {
    342 		syscallarg(lwpid_t) wait_for;
    343 		syscallarg(lwpid_t *) departed;
    344 	} */ *uap = v;
    345 	int error;
    346 	lwpid_t dep;
    347 
    348 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    349 	if (error)
    350 		return (error);
    351 
    352 	if (SCARG(uap, departed)) {
    353 		error = copyout(&dep, SCARG(uap, departed),
    354 		    sizeof(dep));
    355 		if (error)
    356 			return (error);
    357 	}
    358 
    359 	return (0);
    360 }
    361 
    362 
    363 int
    364 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    365 {
    366 
    367 	struct proc *p = l->l_proc;
    368 	struct lwp *l2, *l3;
    369 	int nfound, error, s, wpri;
    370 	static char waitstr1[] = "lwpwait";
    371 	static char waitstr2[] = "lwpwait2";
    372 
    373 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    374 	    p->p_pid, l->l_lid, lid));
    375 
    376 	if (lid == l->l_lid)
    377 		return (EDEADLK); /* Waiting for ourselves makes no sense. */
    378 
    379 	wpri = PWAIT |
    380 	    ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : PCATCH);
    381  loop:
    382 	nfound = 0;
    383 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    384 		if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
    385 		    ((lid != 0) && (lid != l2->l_lid)))
    386 			continue;
    387 
    388 		nfound++;
    389 		if (l2->l_stat == LSZOMB) {
    390 			if (departed)
    391 				*departed = l2->l_lid;
    392 
    393 			s = proclist_lock_write();
    394 			LIST_REMOVE(l2, l_zlist); /* off zomblwp */
    395 			proclist_unlock_write(s);
    396 
    397 			simple_lock(&p->p_lwplock);
    398 			LIST_REMOVE(l2, l_sibling);
    399 			p->p_nlwps--;
    400 			p->p_nzlwps--;
    401 			simple_unlock(&p->p_lwplock);
    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 
    467 	memset(&l2->l_startzero, 0,
    468 	       (unsigned) ((caddr_t)&l2->l_endzero -
    469 			   (caddr_t)&l2->l_startzero));
    470 	memcpy(&l2->l_startcopy, &l1->l_startcopy,
    471 	       (unsigned) ((caddr_t)&l2->l_endcopy -
    472 			   (caddr_t)&l2->l_startcopy));
    473 
    474 #if !defined(MULTIPROCESSOR)
    475 	/*
    476 	 * In the single-processor case, all processes will always run
    477 	 * on the same CPU.  So, initialize the child's CPU to the parent's
    478 	 * now.  In the multiprocessor case, the child's CPU will be
    479 	 * initialized in the low-level context switch code when the
    480 	 * process runs.
    481 	 */
    482 	KASSERT(l1->l_cpu != NULL);
    483 	l2->l_cpu = l1->l_cpu;
    484 #else
    485 	/*
    486 	 * zero child's cpu pointer so we don't get trash.
    487 	 */
    488 	l2->l_cpu = NULL;
    489 #endif /* ! MULTIPROCESSOR */
    490 
    491 	l2->l_flag = inmem ? L_INMEM : 0;
    492 	l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
    493 
    494 	callout_init(&l2->l_tsleep_ch);
    495 
    496 	if (rnewlwpp != NULL)
    497 		*rnewlwpp = l2;
    498 
    499 	l2->l_addr = (struct user *)uaddr;
    500 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    501 	    (arg != NULL) ? arg : l2);
    502 
    503 
    504 	simple_lock(&p2->p_lwplock);
    505 	l2->l_lid = ++p2->p_nlwpid;
    506 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    507 	p2->p_nlwps++;
    508 	simple_unlock(&p2->p_lwplock);
    509 
    510 	/* XXX should be locked differently... */
    511 	s = proclist_lock_write();
    512 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    513 	proclist_unlock_write(s);
    514 
    515 	return (0);
    516 }
    517 
    518 
    519 /*
    520  * Quit the process. This will call cpu_exit, which will call cpu_switch,
    521  * so this can only be used meaningfully if you're willing to switch away.
    522  * Calling with l!=curlwp would be weird.
    523  */
    524 void
    525 lwp_exit(struct lwp *l)
    526 {
    527 	struct proc *p = l->l_proc;
    528 	int s;
    529 
    530 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    531 	DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
    532 	    p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
    533 
    534 	/*
    535 	 * If we are the last live LWP in a process, we need to exit
    536 	 * the entire process (if that's not already going on). We do
    537 	 * so with an exit status of zero, because it's a "controlled"
    538 	 * exit, and because that's what Solaris does.
    539 	 */
    540 	if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
    541 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    542 		    p->p_pid, l->l_lid));
    543 		exit1(l, 0);
    544 	}
    545 
    546 	s = proclist_lock_write();
    547 	LIST_REMOVE(l, l_list);
    548 	if ((l->l_flag & L_DETACHED) == 0) {
    549 		DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
    550 		    l->l_lid));
    551 		LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
    552 	}
    553 	proclist_unlock_write(s);
    554 
    555 	simple_lock(&p->p_lwplock);
    556 	p->p_nrlwps--;
    557 	simple_unlock(&p->p_lwplock);
    558 
    559 	l->l_stat = LSDEAD;
    560 
    561 	/* This LWP no longer needs to hold the kernel lock. */
    562 	KERNEL_PROC_UNLOCK(l);
    563 
    564 	/* cpu_exit() will not return */
    565 	cpu_exit(l, 0);
    566 
    567 }
    568 
    569 
    570 void
    571 lwp_exit2(struct lwp *l)
    572 {
    573 
    574 	simple_lock(&deadproc_slock);
    575 	LIST_INSERT_HEAD(&deadlwp, l, l_list);
    576 	simple_unlock(&deadproc_slock);
    577 
    578 	wakeup(&deadprocs);
    579 }
    580 
    581 /*
    582  * Pick a LWP to represent the process for those operations which
    583  * want information about a "process" that is actually associated
    584  * with a LWP.
    585  */
    586 struct lwp *
    587 proc_representative_lwp(p)
    588 	struct proc *p;
    589 {
    590 	struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
    591 
    592 	/* Trivial case: only one LWP */
    593 	if (p->p_nlwps == 1)
    594 		return (LIST_FIRST(&p->p_lwps));
    595 
    596 	switch (p->p_stat) {
    597 	case SSTOP:
    598 	case SACTIVE:
    599 		/* Pick the most live LWP */
    600 		onproc = running = sleeping = stopped = suspended = NULL;
    601 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    602 			switch (l->l_stat) {
    603 			case LSONPROC:
    604 				onproc = l;
    605 				break;
    606 			case LSRUN:
    607 				running = l;
    608 				break;
    609 			case LSSLEEP:
    610 				sleeping = l;
    611 				break;
    612 			case LSSTOP:
    613 				stopped = l;
    614 				break;
    615 			case LSSUSPENDED:
    616 				suspended = l;
    617 				break;
    618 			}
    619 		}
    620 		if (onproc)
    621 			return onproc;
    622 		if (running)
    623 			return running;
    624 		if (sleeping)
    625 			return sleeping;
    626 		if (stopped)
    627 			return stopped;
    628 		if (suspended)
    629 			return suspended;
    630 		break;
    631 	case SDEAD:
    632 	case SZOMB:
    633 		/* Doesn't really matter... */
    634 		return (LIST_FIRST(&p->p_lwps));
    635 		break;
    636 #ifdef DIAGNOSTIC
    637 	case SIDL:
    638 		/* We have more than one LWP and we're in SIDL?
    639 		 * How'd that happen?
    640 		 */
    641 		panic("Too many LWPs (%d) in SIDL process %d (%s)",
    642 		    p->p_nrlwps, p->p_pid, p->p_comm);
    643 	default:
    644 		panic("Process %d (%s) in unknown state %d",
    645 		    p->p_pid, p->p_comm, p->p_stat);
    646 #endif
    647 	}
    648 
    649 	panic("proc_representative_lwp: couldn't find a lwp for process"
    650 		" %d (%s)", p->p_pid, p->p_comm);
    651 	/* NOTREACHED */
    652 	return NULL;
    653 }
    654