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