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