Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.1.2.2
      1  1.1.2.2  nathanw /*	$NetBSD: kern_lwp.c,v 1.1.2.2 2001/07/09 22:32:28 nathanw Exp $	*/
      2  1.1.2.1  nathanw 
      3  1.1.2.1  nathanw #include <sys/param.h>
      4  1.1.2.1  nathanw #include <sys/systm.h>
      5  1.1.2.1  nathanw #include <sys/pool.h>
      6  1.1.2.1  nathanw #include <sys/lock.h>
      7  1.1.2.1  nathanw #include <sys/lwp.h>
      8  1.1.2.1  nathanw #include <sys/proc.h>
      9  1.1.2.1  nathanw #include <sys/types.h>
     10  1.1.2.1  nathanw #include <sys/ucontext.h>
     11  1.1.2.1  nathanw #include <sys/resourcevar.h>
     12  1.1.2.1  nathanw #include <sys/mount.h>
     13  1.1.2.1  nathanw #include <sys/syscallargs.h>
     14  1.1.2.1  nathanw 
     15  1.1.2.1  nathanw #include <uvm/uvm_extern.h>
     16  1.1.2.1  nathanw 
     17  1.1.2.1  nathanw struct lwplist alllwp;
     18  1.1.2.1  nathanw struct lwplist deadlwp;
     19  1.1.2.1  nathanw struct lwplist zomblwp;
     20  1.1.2.1  nathanw 
     21  1.1.2.1  nathanw #define DPRINTF(x)
     22  1.1.2.1  nathanw 
     23  1.1.2.1  nathanw /* ARGSUSED */
     24  1.1.2.1  nathanw int
     25  1.1.2.1  nathanw sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     26  1.1.2.1  nathanw {
     27  1.1.2.1  nathanw 	struct sys__lwp_create_args /* {
     28  1.1.2.1  nathanw 	syscallarg(const ucontext_t *) ucp;
     29  1.1.2.1  nathanw 	syscallarg(u_long) flags;
     30  1.1.2.1  nathanw 	syscallarg(lwpid_t *) new_lwp;
     31  1.1.2.1  nathanw 	} */ *uap = v;
     32  1.1.2.1  nathanw 	struct proc *p = l->l_proc;
     33  1.1.2.1  nathanw 	struct lwp *l2;
     34  1.1.2.1  nathanw 	vaddr_t uaddr;
     35  1.1.2.1  nathanw 	ucontext_t *newuc;
     36  1.1.2.1  nathanw 	int s, error;
     37  1.1.2.1  nathanw 
     38  1.1.2.1  nathanw 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     39  1.1.2.1  nathanw 
     40  1.1.2.1  nathanw 	error = copyin(SCARG(uap, ucp), newuc, sizeof(*newuc));
     41  1.1.2.1  nathanw 	if (error)
     42  1.1.2.1  nathanw 		return (error);
     43  1.1.2.1  nathanw 
     44  1.1.2.1  nathanw 	/* XXX check against resource limits */
     45  1.1.2.1  nathanw 
     46  1.1.2.1  nathanw 	uaddr = uvm_km_valloc(kernel_map, USPACE);
     47  1.1.2.1  nathanw 	if (__predict_false(uaddr == 0)) {
     48  1.1.2.1  nathanw 		return (ENOMEM);
     49  1.1.2.1  nathanw 	}
     50  1.1.2.1  nathanw 
     51  1.1.2.1  nathanw 	/* XXX flags:
     52  1.1.2.1  nathanw 	 * __LWP_ASLWP is probably needed for Solaris compat.
     53  1.1.2.1  nathanw 	 */
     54  1.1.2.1  nathanw 
     55  1.1.2.1  nathanw 	newlwp(l, p, uaddr,
     56  1.1.2.1  nathanw 	    SCARG(uap, flags) & LWP_DETACHED,
     57  1.1.2.1  nathanw 	    NULL, NULL, startlwp, newuc, &l2);
     58  1.1.2.1  nathanw 
     59  1.1.2.1  nathanw 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
     60  1.1.2.1  nathanw 		SCHED_LOCK(s);
     61  1.1.2.1  nathanw 		l2->l_stat = LSRUN;
     62  1.1.2.1  nathanw 		setrunqueue(l2);
     63  1.1.2.1  nathanw 		SCHED_UNLOCK(s);
     64  1.1.2.1  nathanw 		simple_lock(&p->p_lwplock);
     65  1.1.2.1  nathanw 		p->p_nrlwps++;
     66  1.1.2.1  nathanw 		simple_unlock(&p->p_lwplock);
     67  1.1.2.1  nathanw 	} else {
     68  1.1.2.1  nathanw 		l2->l_stat = LSSUSPENDED;
     69  1.1.2.1  nathanw 	}
     70  1.1.2.1  nathanw 
     71  1.1.2.1  nathanw 	error = copyout(&l2->l_lid, SCARG(uap, new_lwp),
     72  1.1.2.1  nathanw 	    sizeof(l2->l_lid));
     73  1.1.2.1  nathanw 	if (error)
     74  1.1.2.1  nathanw 		return (error);
     75  1.1.2.1  nathanw 
     76  1.1.2.1  nathanw 	return (0);
     77  1.1.2.1  nathanw }
     78  1.1.2.1  nathanw 
     79  1.1.2.1  nathanw 
     80  1.1.2.1  nathanw int
     81  1.1.2.1  nathanw sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
     82  1.1.2.1  nathanw {
     83  1.1.2.1  nathanw 
     84  1.1.2.1  nathanw 	lwp_exit(l);
     85  1.1.2.1  nathanw 	/* NOTREACHED */
     86  1.1.2.1  nathanw 	return (0);
     87  1.1.2.1  nathanw }
     88  1.1.2.1  nathanw 
     89  1.1.2.1  nathanw 
     90  1.1.2.1  nathanw int
     91  1.1.2.1  nathanw sys__lwp_self(struct lwp *l, void *v, register_t *retval)
     92  1.1.2.1  nathanw {
     93  1.1.2.1  nathanw 
     94  1.1.2.1  nathanw 	*retval = l->l_lid;
     95  1.1.2.1  nathanw 
     96  1.1.2.1  nathanw 	return (0);
     97  1.1.2.1  nathanw }
     98  1.1.2.1  nathanw 
     99  1.1.2.1  nathanw 
    100  1.1.2.1  nathanw int
    101  1.1.2.1  nathanw sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    102  1.1.2.1  nathanw {
    103  1.1.2.1  nathanw 	struct sys__lwp_suspend_args /* {
    104  1.1.2.1  nathanw 	syscallarg(lwpid_t) target;
    105  1.1.2.1  nathanw 	} */ *uap = v;
    106  1.1.2.1  nathanw 	int target_lid;
    107  1.1.2.1  nathanw 	struct proc *p = l->l_proc;
    108  1.1.2.1  nathanw 	struct lwp *t, *t2;
    109  1.1.2.1  nathanw 	int s;
    110  1.1.2.1  nathanw 
    111  1.1.2.1  nathanw 	target_lid = SCARG(uap, target);
    112  1.1.2.1  nathanw 
    113  1.1.2.1  nathanw 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    114  1.1.2.1  nathanw 		if (t->l_lid == target_lid)
    115  1.1.2.1  nathanw 			break;
    116  1.1.2.1  nathanw 
    117  1.1.2.1  nathanw 	if (t == NULL)
    118  1.1.2.1  nathanw 		return (ESRCH);
    119  1.1.2.1  nathanw 
    120  1.1.2.1  nathanw 	if (t == l) {
    121  1.1.2.1  nathanw 		/*
    122  1.1.2.1  nathanw 		 * Check for deadlock, which is only possible
    123  1.1.2.1  nathanw 		 * when we're suspending ourself.
    124  1.1.2.1  nathanw 		 */
    125  1.1.2.1  nathanw 		LIST_FOREACH(t2, &p->p_lwps, l_sibling) {
    126  1.1.2.1  nathanw 			if ((t2 != l) && (t2->l_stat != LSSUSPENDED))
    127  1.1.2.1  nathanw 				break;
    128  1.1.2.1  nathanw 		}
    129  1.1.2.1  nathanw 
    130  1.1.2.1  nathanw 		if (t2 == NULL) /* All other LWPs are suspended */
    131  1.1.2.1  nathanw 			return (EDEADLK);
    132  1.1.2.1  nathanw 
    133  1.1.2.1  nathanw 		SCHED_LOCK(s);
    134  1.1.2.1  nathanw 		l->l_stat = LSSUSPENDED;
    135  1.1.2.1  nathanw 		/* XXX NJWLWP check if this makes sense here: */
    136  1.1.2.1  nathanw 		l->l_proc->p_stats->p_ru.ru_nvcsw++;
    137  1.1.2.1  nathanw 		mi_switch(l, NULL);
    138  1.1.2.1  nathanw 		SCHED_ASSERT_UNLOCKED();
    139  1.1.2.1  nathanw 	} else {
    140  1.1.2.1  nathanw 		switch (t->l_stat) {
    141  1.1.2.1  nathanw 		case LSSUSPENDED:
    142  1.1.2.1  nathanw 			return (0); /* _lwp_suspend() is idempotent */
    143  1.1.2.1  nathanw 		case LSRUN:
    144  1.1.2.1  nathanw 			SCHED_LOCK(s);
    145  1.1.2.1  nathanw 			remrunqueue(t);
    146  1.1.2.1  nathanw 			t->l_stat = LSSUSPENDED;
    147  1.1.2.2  nathanw 			SCHED_UNLOCK(s);
    148  1.1.2.1  nathanw 			simple_lock(&p->p_lwplock);
    149  1.1.2.1  nathanw 			p->p_nrlwps--;
    150  1.1.2.1  nathanw 			simple_unlock(&p->p_lwplock);
    151  1.1.2.1  nathanw 			break;
    152  1.1.2.1  nathanw 		case LSSLEEP:
    153  1.1.2.1  nathanw 			t->l_stat = LSSUSPENDED;
    154  1.1.2.1  nathanw 			break;
    155  1.1.2.1  nathanw 		case LSIDL:
    156  1.1.2.1  nathanw 		case LSDEAD:
    157  1.1.2.1  nathanw 		case LSZOMB:
    158  1.1.2.1  nathanw 			return (EINTR); /* It's what Solaris does..... */
    159  1.1.2.1  nathanw 		case LSSTOP:
    160  1.1.2.1  nathanw 			panic("_lwp_suspend: Stopped LWP in running process!");
    161  1.1.2.1  nathanw 			break;
    162  1.1.2.1  nathanw 		case LSONPROC:
    163  1.1.2.1  nathanw 			panic("XXX multiprocessor LWPs? Implement me!");
    164  1.1.2.1  nathanw 			break;
    165  1.1.2.1  nathanw 		}
    166  1.1.2.1  nathanw 	}
    167  1.1.2.1  nathanw 
    168  1.1.2.1  nathanw 	return (0);
    169  1.1.2.1  nathanw }
    170  1.1.2.1  nathanw 
    171  1.1.2.1  nathanw 
    172  1.1.2.1  nathanw int
    173  1.1.2.1  nathanw sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    174  1.1.2.1  nathanw {
    175  1.1.2.1  nathanw 	struct sys__lwp_continue_args /* {
    176  1.1.2.1  nathanw 	syscallarg(lwpid_t) target;
    177  1.1.2.1  nathanw 	} */ *uap = v;
    178  1.1.2.1  nathanw 	int target_lid;
    179  1.1.2.1  nathanw 	struct proc *p = l->l_proc;
    180  1.1.2.1  nathanw 	struct lwp *t;
    181  1.1.2.1  nathanw 	int s;
    182  1.1.2.1  nathanw 
    183  1.1.2.1  nathanw 	target_lid = SCARG(uap, target);
    184  1.1.2.1  nathanw 
    185  1.1.2.1  nathanw 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    186  1.1.2.1  nathanw 		if (t->l_lid == target_lid)
    187  1.1.2.1  nathanw 			break;
    188  1.1.2.1  nathanw 
    189  1.1.2.1  nathanw 	if (t == NULL)
    190  1.1.2.1  nathanw 		return (ESRCH);
    191  1.1.2.1  nathanw 
    192  1.1.2.1  nathanw 	if (t->l_stat != LSSUSPENDED)
    193  1.1.2.1  nathanw 		return (0);
    194  1.1.2.1  nathanw 
    195  1.1.2.1  nathanw 	if (t->l_wchan == 0) {
    196  1.1.2.1  nathanw 		/* LWP was runnable before being suspended. */
    197  1.1.2.1  nathanw 		SCHED_LOCK(s);
    198  1.1.2.1  nathanw 		setrunnable(t);
    199  1.1.2.1  nathanw 		simple_lock(&p->p_lwplock);
    200  1.1.2.1  nathanw 		p->p_nrlwps++;
    201  1.1.2.1  nathanw 		simple_unlock(&p->p_lwplock);
    202  1.1.2.1  nathanw 		SCHED_UNLOCK(s);
    203  1.1.2.1  nathanw 	} else {
    204  1.1.2.1  nathanw 		/* LWP was sleeping before being suspended */
    205  1.1.2.1  nathanw 		t->l_stat = LSSLEEP;
    206  1.1.2.1  nathanw 	}
    207  1.1.2.1  nathanw 
    208  1.1.2.1  nathanw 	return (0);
    209  1.1.2.1  nathanw }
    210  1.1.2.1  nathanw 
    211  1.1.2.1  nathanw 
    212  1.1.2.1  nathanw int
    213  1.1.2.1  nathanw sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    214  1.1.2.1  nathanw {
    215  1.1.2.1  nathanw 	struct sys__lwp_wait_args /* {
    216  1.1.2.1  nathanw 	syscallarg(lwpid_t) wait_for;
    217  1.1.2.1  nathanw 	syscallarg(lwpid_t *) departed;
    218  1.1.2.1  nathanw 	} */ *uap = v;
    219  1.1.2.1  nathanw 	int error;
    220  1.1.2.1  nathanw 	lwpid_t dep;
    221  1.1.2.1  nathanw 
    222  1.1.2.1  nathanw 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    223  1.1.2.1  nathanw 	if (error)
    224  1.1.2.1  nathanw 		return (error);
    225  1.1.2.1  nathanw 
    226  1.1.2.1  nathanw 	if (SCARG(uap, departed)) {
    227  1.1.2.1  nathanw 		error = copyout(&dep, SCARG(uap, departed),
    228  1.1.2.1  nathanw 		    sizeof(dep));
    229  1.1.2.1  nathanw 		if (error)
    230  1.1.2.1  nathanw 			return (error);
    231  1.1.2.1  nathanw 	}
    232  1.1.2.1  nathanw 
    233  1.1.2.1  nathanw 	return (0);
    234  1.1.2.1  nathanw }
    235  1.1.2.1  nathanw 
    236  1.1.2.1  nathanw 
    237  1.1.2.1  nathanw int
    238  1.1.2.1  nathanw lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    239  1.1.2.1  nathanw {
    240  1.1.2.1  nathanw 
    241  1.1.2.1  nathanw 	struct proc *p = l->l_proc;
    242  1.1.2.1  nathanw 	struct lwp *l2, *l3;
    243  1.1.2.1  nathanw 	int nfound, error, s, wpri;
    244  1.1.2.1  nathanw 	static char waitstr1[] = "lwpwait";
    245  1.1.2.1  nathanw 	static char waitstr2[] = "lwpwait2";
    246  1.1.2.1  nathanw 
    247  1.1.2.1  nathanw 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    248  1.1.2.1  nathanw 	    p->p_pid, l->l_lid, lid));
    249  1.1.2.1  nathanw 
    250  1.1.2.1  nathanw 	if (lid == l->l_lid)
    251  1.1.2.1  nathanw 		return (EDEADLK); /* Waiting for ourselves makes no sense. */
    252  1.1.2.1  nathanw 
    253  1.1.2.1  nathanw 	wpri = PWAIT | PCATCH |
    254  1.1.2.1  nathanw 	    ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : 0);
    255  1.1.2.1  nathanw  loop:
    256  1.1.2.1  nathanw 	nfound = 0;
    257  1.1.2.1  nathanw 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    258  1.1.2.1  nathanw 		if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
    259  1.1.2.1  nathanw 		    ((lid != 0) && (lid != l2->l_lid)))
    260  1.1.2.1  nathanw 			continue;
    261  1.1.2.1  nathanw 
    262  1.1.2.1  nathanw 		nfound++;
    263  1.1.2.1  nathanw 		if (l2->l_stat == LSZOMB) {
    264  1.1.2.1  nathanw 			if (departed)
    265  1.1.2.1  nathanw 				*departed = l2->l_lid;
    266  1.1.2.1  nathanw 
    267  1.1.2.1  nathanw 			s = proclist_lock_write();
    268  1.1.2.1  nathanw 			LIST_REMOVE(l2, l_zlist); /* off zomblwp */
    269  1.1.2.1  nathanw 			proclist_unlock_write(s);
    270  1.1.2.1  nathanw 
    271  1.1.2.1  nathanw 			simple_lock(&p->p_lwplock);
    272  1.1.2.1  nathanw 			LIST_REMOVE(l2, l_sibling);
    273  1.1.2.1  nathanw 			p->p_nlwps--;
    274  1.1.2.1  nathanw 			p->p_nzlwps--;
    275  1.1.2.1  nathanw 			simple_unlock(&p->p_lwplock);
    276  1.1.2.1  nathanw 			/* XXX decrement limits */
    277  1.1.2.1  nathanw 
    278  1.1.2.1  nathanw 			pool_put(&lwp_pool, l2);
    279  1.1.2.1  nathanw 
    280  1.1.2.1  nathanw 			return (0);
    281  1.1.2.1  nathanw 		} else if (l2->l_stat == LSSLEEP ||
    282  1.1.2.1  nathanw 		           l2->l_stat == LSSUSPENDED) {
    283  1.1.2.1  nathanw 			/* Deadlock checks.
    284  1.1.2.1  nathanw 			 * 1. If all other LWPs are waiting for exits
    285  1.1.2.1  nathanw 			 *    or suspended, we would deadlock.
    286  1.1.2.1  nathanw 			 */
    287  1.1.2.1  nathanw 
    288  1.1.2.1  nathanw 			LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
    289  1.1.2.1  nathanw 				if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
    290  1.1.2.1  nathanw 				    !(l3->l_stat == LSSLEEP &&
    291  1.1.2.1  nathanw 					l3->l_wchan == (caddr_t) &p->p_nlwps))
    292  1.1.2.1  nathanw 					break;
    293  1.1.2.1  nathanw 			}
    294  1.1.2.1  nathanw 			if (l3 == NULL) /* Everyone else is waiting. */
    295  1.1.2.1  nathanw 				return (EDEADLK);
    296  1.1.2.1  nathanw 
    297  1.1.2.1  nathanw 			/* XXX we'd like to check for a cycle of waiting
    298  1.1.2.1  nathanw 			 * LWPs (specific LID waits, not any-LWP waits)
    299  1.1.2.1  nathanw 			 * and detect that sort of deadlock, but we don't
    300  1.1.2.1  nathanw 			 * have a good place to store the lwp that is
    301  1.1.2.1  nathanw 			 * being waited for. wchan is already filled with
    302  1.1.2.1  nathanw 			 * &p->p_nlwps, and putting the lwp address in
    303  1.1.2.1  nathanw 			 * there for deadlock tracing would require
    304  1.1.2.1  nathanw 			 * exiting LWPs to call wakeup on both their
    305  1.1.2.1  nathanw 			 * own address and &p->p_nlwps, to get threads
    306  1.1.2.1  nathanw 			 * sleeping on any LWP exiting.
    307  1.1.2.1  nathanw 			 *
    308  1.1.2.1  nathanw 			 * Revisit later. Maybe another auxillary
    309  1.1.2.1  nathanw 			 * storage location associated with sleeping
    310  1.1.2.1  nathanw 			 * is in order.
    311  1.1.2.1  nathanw 			 */
    312  1.1.2.1  nathanw 		}
    313  1.1.2.1  nathanw 	}
    314  1.1.2.1  nathanw 
    315  1.1.2.1  nathanw 	if (nfound == 0)
    316  1.1.2.1  nathanw 		return (ESRCH);
    317  1.1.2.1  nathanw 
    318  1.1.2.1  nathanw 	if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
    319  1.1.2.1  nathanw 	    (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
    320  1.1.2.1  nathanw 		return (error);
    321  1.1.2.1  nathanw 
    322  1.1.2.1  nathanw 	goto loop;
    323  1.1.2.1  nathanw }
    324  1.1.2.1  nathanw 
    325  1.1.2.1  nathanw 
    326  1.1.2.1  nathanw int
    327  1.1.2.1  nathanw newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr,
    328  1.1.2.1  nathanw     int flags, void *stack, size_t stacksize,
    329  1.1.2.1  nathanw     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    330  1.1.2.1  nathanw {
    331  1.1.2.1  nathanw 	struct lwp *l2;
    332  1.1.2.1  nathanw 	int s;
    333  1.1.2.1  nathanw 
    334  1.1.2.1  nathanw 	l2 = pool_get(&lwp_pool, PR_WAITOK);
    335  1.1.2.1  nathanw 
    336  1.1.2.1  nathanw 	l2->l_stat = LSIDL;
    337  1.1.2.1  nathanw 	l2->l_forw = l2->l_back = NULL;
    338  1.1.2.1  nathanw 	l2->l_proc = p2;
    339  1.1.2.1  nathanw 
    340  1.1.2.1  nathanw 
    341  1.1.2.1  nathanw 	memset(&l2->l_startzero, 0,
    342  1.1.2.1  nathanw 	       (unsigned) ((caddr_t)&l2->l_endzero -
    343  1.1.2.1  nathanw 			   (caddr_t)&l2->l_startzero));
    344  1.1.2.1  nathanw 	memcpy(&l2->l_startcopy, &l1->l_startcopy,
    345  1.1.2.1  nathanw 	       (unsigned) ((caddr_t)&l2->l_endcopy -
    346  1.1.2.1  nathanw 			   (caddr_t)&l2->l_startcopy));
    347  1.1.2.1  nathanw 
    348  1.1.2.1  nathanw #if !defined(MULTIPROCESSOR)
    349  1.1.2.1  nathanw 	/*
    350  1.1.2.1  nathanw 	 * In the single-processor case, all processes will always run
    351  1.1.2.1  nathanw 	 * on the same CPU.  So, initialize the child's CPU to the parent's
    352  1.1.2.1  nathanw 	 * now.  In the multiprocessor case, the child's CPU will be
    353  1.1.2.1  nathanw 	 * initialized in the low-level context switch code when the
    354  1.1.2.1  nathanw 	 * process runs.
    355  1.1.2.1  nathanw 	 */
    356  1.1.2.1  nathanw 	l2->l_cpu = l1->l_cpu;
    357  1.1.2.1  nathanw #else
    358  1.1.2.1  nathanw 	/*
    359  1.1.2.1  nathanw 	 * zero child's cpu pointer so we don't get trash.
    360  1.1.2.1  nathanw 	 */
    361  1.1.2.1  nathanw 	l2->l_cpu = NULL;
    362  1.1.2.1  nathanw #endif /* ! MULTIPROCESSOR */
    363  1.1.2.1  nathanw 
    364  1.1.2.1  nathanw 	l2->l_flag = L_INMEM;
    365  1.1.2.1  nathanw 	l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
    366  1.1.2.1  nathanw 
    367  1.1.2.1  nathanw 	callout_init(&l2->l_tsleep_ch);
    368  1.1.2.1  nathanw 
    369  1.1.2.1  nathanw 	if (rnewlwpp != NULL)
    370  1.1.2.1  nathanw 		*rnewlwpp = l2;
    371  1.1.2.1  nathanw 
    372  1.1.2.1  nathanw 	l2->l_addr = (struct user *)uaddr;
    373  1.1.2.1  nathanw 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    374  1.1.2.1  nathanw 	    (arg != NULL) ? arg : l2);
    375  1.1.2.1  nathanw 
    376  1.1.2.1  nathanw 
    377  1.1.2.1  nathanw 	simple_lock(&p2->p_lwplock);
    378  1.1.2.1  nathanw 	l2->l_lid = ++p2->p_nlwpid;
    379  1.1.2.1  nathanw 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    380  1.1.2.1  nathanw 	p2->p_nlwps++;
    381  1.1.2.1  nathanw 	simple_unlock(&p2->p_lwplock);
    382  1.1.2.1  nathanw 
    383  1.1.2.1  nathanw 	/* XXX should be locked differently... */
    384  1.1.2.1  nathanw 	s = proclist_lock_write();
    385  1.1.2.1  nathanw 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    386  1.1.2.1  nathanw 	proclist_unlock_write(s);
    387  1.1.2.1  nathanw 
    388  1.1.2.1  nathanw 	return (0);
    389  1.1.2.1  nathanw }
    390  1.1.2.1  nathanw 
    391  1.1.2.1  nathanw 
    392  1.1.2.1  nathanw /*
    393  1.1.2.1  nathanw  * Quit the process. This will call cpu_exit, which will call cpu_switch,
    394  1.1.2.1  nathanw  * so this can only be used meaningfully if you're willing to switch away.
    395  1.1.2.1  nathanw  * Calling with l!=curproc would be weird.
    396  1.1.2.1  nathanw  */
    397  1.1.2.1  nathanw void
    398  1.1.2.1  nathanw lwp_exit(struct lwp *l)
    399  1.1.2.1  nathanw {
    400  1.1.2.1  nathanw 	struct proc *p = l->l_proc;
    401  1.1.2.1  nathanw 	int s;
    402  1.1.2.1  nathanw 
    403  1.1.2.1  nathanw 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    404  1.1.2.1  nathanw 	/*
    405  1.1.2.1  nathanw 	 * If we are the last live LWP in a process, we need to exit
    406  1.1.2.1  nathanw 	 * the entire process (if that's not already going on). We do
    407  1.1.2.1  nathanw 	 * so with an exit status of zero, because it's a "controlled"
    408  1.1.2.1  nathanw 	 * exit, and because that's what Solaris does.
    409  1.1.2.1  nathanw 	 */
    410  1.1.2.1  nathanw 	if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
    411  1.1.2.1  nathanw 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    412  1.1.2.1  nathanw 		    p->p_pid, l->l_lid));
    413  1.1.2.1  nathanw 		exit1(l, 0);
    414  1.1.2.1  nathanw 	}
    415  1.1.2.1  nathanw 
    416  1.1.2.1  nathanw 	s = proclist_lock_write();
    417  1.1.2.1  nathanw 	LIST_REMOVE(l, l_list);
    418  1.1.2.1  nathanw 	if ((l->l_flag & L_DETACHED) == 0) {
    419  1.1.2.1  nathanw 		DPRINTF(("lwp_exit: %d.%d going on zombie list\n", p->p_pid,
    420  1.1.2.1  nathanw 		    l->l_lid));
    421  1.1.2.1  nathanw 		LIST_INSERT_HEAD(&zomblwp, l, l_zlist);
    422  1.1.2.1  nathanw 	}
    423  1.1.2.1  nathanw 	proclist_unlock_write(s);
    424  1.1.2.1  nathanw 
    425  1.1.2.1  nathanw 	simple_lock(&p->p_lwplock);
    426  1.1.2.1  nathanw 	p->p_nrlwps--;
    427  1.1.2.1  nathanw 	simple_unlock(&p->p_lwplock);
    428  1.1.2.1  nathanw 
    429  1.1.2.1  nathanw 	l->l_stat = LSDEAD;
    430  1.1.2.1  nathanw 
    431  1.1.2.1  nathanw 	/* cpu_exit() will not return */
    432  1.1.2.1  nathanw 	cpu_exit(l, 0);
    433  1.1.2.1  nathanw 
    434  1.1.2.1  nathanw }
    435  1.1.2.1  nathanw 
    436  1.1.2.1  nathanw 
    437  1.1.2.1  nathanw void
    438  1.1.2.1  nathanw lwp_exit2(struct lwp *l)
    439  1.1.2.1  nathanw {
    440  1.1.2.1  nathanw 
    441  1.1.2.1  nathanw 	simple_lock(&deadproc_slock);
    442  1.1.2.1  nathanw 	LIST_INSERT_HEAD(&deadlwp, l, l_list);
    443  1.1.2.1  nathanw 	simple_unlock(&deadproc_slock);
    444  1.1.2.1  nathanw 
    445  1.1.2.1  nathanw 	wakeup(&deadproc);
    446  1.1.2.1  nathanw }
    447