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