Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.47
      1  1.47   hannken /*	$NetBSD: kern_lwp.c,v 1.47 2006/10/24 10:05:45 hannken 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.47   hannken __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.47 2006/10/24 10:05:45 hannken Exp $");
     41   1.8    martin 
     42   1.8    martin #include "opt_multiprocessor.h"
     43   1.2   thorpej 
     44  1.47   hannken #define _LWP_API_PRIVATE
     45  1.47   hannken 
     46   1.2   thorpej #include <sys/param.h>
     47   1.2   thorpej #include <sys/systm.h>
     48   1.2   thorpej #include <sys/pool.h>
     49   1.2   thorpej #include <sys/lock.h>
     50   1.2   thorpej #include <sys/proc.h>
     51   1.2   thorpej #include <sys/sa.h>
     52   1.2   thorpej #include <sys/savar.h>
     53   1.2   thorpej #include <sys/types.h>
     54   1.2   thorpej #include <sys/ucontext.h>
     55   1.2   thorpej #include <sys/resourcevar.h>
     56   1.2   thorpej #include <sys/mount.h>
     57   1.2   thorpej #include <sys/syscallargs.h>
     58  1.37        ad #include <sys/kauth.h>
     59   1.2   thorpej 
     60   1.2   thorpej #include <uvm/uvm_extern.h>
     61   1.2   thorpej 
     62  1.41   thorpej POOL_INIT(lwp_pool, sizeof(struct lwp), 0, 0, 0, "lwppl",
     63  1.41   thorpej     &pool_allocator_nointr);
     64  1.41   thorpej POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
     65  1.41   thorpej     &pool_allocator_nointr);
     66  1.41   thorpej 
     67  1.41   thorpej static specificdata_domain_t lwp_specificdata_domain;
     68  1.41   thorpej 
     69   1.2   thorpej struct lwplist alllwp;
     70   1.2   thorpej 
     71   1.2   thorpej #define LWP_DEBUG
     72   1.2   thorpej 
     73   1.2   thorpej #ifdef LWP_DEBUG
     74   1.2   thorpej int lwp_debug = 0;
     75   1.2   thorpej #define DPRINTF(x) if (lwp_debug) printf x
     76   1.2   thorpej #else
     77   1.2   thorpej #define DPRINTF(x)
     78   1.2   thorpej #endif
     79  1.41   thorpej 
     80  1.41   thorpej void
     81  1.41   thorpej lwpinit(void)
     82  1.41   thorpej {
     83  1.41   thorpej 
     84  1.41   thorpej 	lwp_specificdata_domain = specificdata_domain_create();
     85  1.41   thorpej 	KASSERT(lwp_specificdata_domain != NULL);
     86  1.41   thorpej }
     87  1.41   thorpej 
     88   1.2   thorpej /* ARGSUSED */
     89   1.2   thorpej int
     90  1.46  christos sys__lwp_create(struct lwp *l, void *v, register_t *retval __unused)
     91   1.2   thorpej {
     92   1.2   thorpej 	struct sys__lwp_create_args /* {
     93   1.2   thorpej 		syscallarg(const ucontext_t *) ucp;
     94   1.2   thorpej 		syscallarg(u_long) flags;
     95   1.2   thorpej 		syscallarg(lwpid_t *) new_lwp;
     96   1.2   thorpej 	} */ *uap = v;
     97   1.2   thorpej 	struct proc *p = l->l_proc;
     98   1.2   thorpej 	struct lwp *l2;
     99   1.2   thorpej 	vaddr_t uaddr;
    100   1.2   thorpej 	boolean_t inmem;
    101   1.2   thorpej 	ucontext_t *newuc;
    102   1.2   thorpej 	int s, error;
    103   1.2   thorpej 
    104  1.33       chs 	if (p->p_flag & P_SA)
    105  1.33       chs 		return EINVAL;
    106  1.33       chs 
    107   1.2   thorpej 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
    108   1.2   thorpej 
    109  1.34      cube 	error = copyin(SCARG(uap, ucp), newuc,
    110  1.34      cube 	    l->l_proc->p_emul->e_sa->sae_ucsize);
    111  1.40        ad 	if (error) {
    112  1.40        ad 		pool_put(&lwp_uc_pool, newuc);
    113   1.2   thorpej 		return (error);
    114  1.40        ad 	}
    115   1.2   thorpej 
    116   1.2   thorpej 	/* XXX check against resource limits */
    117   1.2   thorpej 
    118   1.2   thorpej 	inmem = uvm_uarea_alloc(&uaddr);
    119   1.2   thorpej 	if (__predict_false(uaddr == 0)) {
    120  1.40        ad 		pool_put(&lwp_uc_pool, newuc);
    121   1.2   thorpej 		return (ENOMEM);
    122   1.2   thorpej 	}
    123   1.2   thorpej 
    124   1.2   thorpej 	/* XXX flags:
    125   1.2   thorpej 	 * __LWP_ASLWP is probably needed for Solaris compat.
    126   1.2   thorpej 	 */
    127   1.2   thorpej 
    128   1.2   thorpej 	newlwp(l, p, uaddr, inmem,
    129   1.2   thorpej 	    SCARG(uap, flags) & LWP_DETACHED,
    130   1.7  kristerw 	    NULL, 0, startlwp, newuc, &l2);
    131   1.2   thorpej 
    132   1.2   thorpej 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
    133   1.2   thorpej 		SCHED_LOCK(s);
    134   1.2   thorpej 		l2->l_stat = LSRUN;
    135   1.2   thorpej 		setrunqueue(l2);
    136  1.30      yamt 		p->p_nrlwps++;
    137   1.2   thorpej 		SCHED_UNLOCK(s);
    138   1.2   thorpej 	} else {
    139   1.2   thorpej 		l2->l_stat = LSSUSPENDED;
    140   1.2   thorpej 	}
    141   1.2   thorpej 
    142   1.2   thorpej 	error = copyout(&l2->l_lid, SCARG(uap, new_lwp),
    143   1.2   thorpej 	    sizeof(l2->l_lid));
    144  1.40        ad 	if (error) {
    145  1.40        ad 		/* XXX We should destroy the LWP. */
    146   1.2   thorpej 		return (error);
    147  1.40        ad 	}
    148   1.2   thorpej 
    149   1.2   thorpej 	return (0);
    150   1.2   thorpej }
    151   1.2   thorpej 
    152   1.2   thorpej 
    153   1.2   thorpej int
    154  1.46  christos sys__lwp_exit(struct lwp *l, void *v __unused, register_t *retval __unused)
    155   1.2   thorpej {
    156   1.2   thorpej 
    157   1.2   thorpej 	lwp_exit(l);
    158   1.2   thorpej 	/* NOTREACHED */
    159   1.2   thorpej 	return (0);
    160   1.2   thorpej }
    161   1.2   thorpej 
    162   1.2   thorpej 
    163   1.2   thorpej int
    164  1.46  christos sys__lwp_self(struct lwp *l, void *v __unused, register_t *retval)
    165   1.2   thorpej {
    166   1.2   thorpej 
    167   1.2   thorpej 	*retval = l->l_lid;
    168   1.2   thorpej 
    169   1.2   thorpej 	return (0);
    170   1.2   thorpej }
    171   1.2   thorpej 
    172   1.2   thorpej 
    173   1.2   thorpej int
    174  1.46  christos sys__lwp_getprivate(struct lwp *l, void *v __unused, register_t *retval)
    175   1.2   thorpej {
    176   1.2   thorpej 
    177   1.2   thorpej 	*retval = (uintptr_t) l->l_private;
    178   1.2   thorpej 
    179   1.2   thorpej 	return (0);
    180   1.2   thorpej }
    181   1.2   thorpej 
    182   1.2   thorpej 
    183   1.2   thorpej int
    184  1.46  christos sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval __unused)
    185   1.2   thorpej {
    186   1.2   thorpej 	struct sys__lwp_setprivate_args /* {
    187   1.2   thorpej 		syscallarg(void *) ptr;
    188   1.2   thorpej 	} */ *uap = v;
    189   1.2   thorpej 
    190   1.2   thorpej 	l->l_private = SCARG(uap, ptr);
    191   1.2   thorpej 
    192   1.2   thorpej 	return (0);
    193   1.2   thorpej }
    194   1.2   thorpej 
    195   1.2   thorpej 
    196   1.2   thorpej int
    197  1.46  christos sys__lwp_suspend(struct lwp *l, void *v, register_t *retval __unused)
    198   1.2   thorpej {
    199   1.2   thorpej 	struct sys__lwp_suspend_args /* {
    200   1.2   thorpej 		syscallarg(lwpid_t) target;
    201   1.2   thorpej 	} */ *uap = v;
    202   1.2   thorpej 	int target_lid;
    203   1.2   thorpej 	struct proc *p = l->l_proc;
    204  1.17      manu 	struct lwp *t;
    205  1.17      manu 	struct lwp *t2;
    206   1.2   thorpej 
    207  1.33       chs 	if (p->p_flag & P_SA)
    208  1.33       chs 		return EINVAL;
    209  1.33       chs 
    210   1.2   thorpej 	target_lid = SCARG(uap, target);
    211   1.2   thorpej 
    212   1.2   thorpej 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    213   1.2   thorpej 		if (t->l_lid == target_lid)
    214   1.2   thorpej 			break;
    215   1.2   thorpej 
    216   1.2   thorpej 	if (t == NULL)
    217   1.2   thorpej 		return (ESRCH);
    218   1.2   thorpej 
    219   1.2   thorpej 	if (t == l) {
    220   1.2   thorpej 		/*
    221   1.2   thorpej 		 * Check for deadlock, which is only possible
    222   1.2   thorpej 		 * when we're suspending ourself.
    223   1.2   thorpej 		 */
    224   1.2   thorpej 		LIST_FOREACH(t2, &p->p_lwps, l_sibling) {
    225   1.2   thorpej 			if ((t2 != l) && (t2->l_stat != LSSUSPENDED))
    226   1.2   thorpej 				break;
    227   1.2   thorpej 		}
    228   1.2   thorpej 
    229   1.2   thorpej 		if (t2 == NULL) /* All other LWPs are suspended */
    230   1.2   thorpej 			return (EDEADLK);
    231  1.17      manu 	}
    232  1.17      manu 
    233  1.17      manu 	return lwp_suspend(l, t);
    234  1.17      manu }
    235   1.2   thorpej 
    236  1.17      manu inline int
    237  1.21  junyoung lwp_suspend(struct lwp *l, struct lwp *t)
    238  1.17      manu {
    239  1.17      manu 	struct proc *p = t->l_proc;
    240  1.17      manu 	int s;
    241  1.17      manu 
    242  1.17      manu 	if (t == l) {
    243   1.2   thorpej 		SCHED_LOCK(s);
    244  1.35      yamt 		KASSERT(l->l_stat == LSONPROC);
    245   1.2   thorpej 		l->l_stat = LSSUSPENDED;
    246  1.35      yamt 		p->p_nrlwps--;
    247   1.2   thorpej 		/* XXX NJWLWP check if this makes sense here: */
    248  1.17      manu 		p->p_stats->p_ru.ru_nvcsw++;
    249   1.2   thorpej 		mi_switch(l, NULL);
    250   1.2   thorpej 		SCHED_ASSERT_UNLOCKED();
    251   1.2   thorpej 		splx(s);
    252   1.2   thorpej 	} else {
    253   1.2   thorpej 		switch (t->l_stat) {
    254   1.2   thorpej 		case LSSUSPENDED:
    255   1.2   thorpej 			return (0); /* _lwp_suspend() is idempotent */
    256   1.2   thorpej 		case LSRUN:
    257   1.2   thorpej 			SCHED_LOCK(s);
    258   1.2   thorpej 			remrunqueue(t);
    259   1.2   thorpej 			t->l_stat = LSSUSPENDED;
    260  1.30      yamt 			p->p_nrlwps--;
    261   1.2   thorpej 			SCHED_UNLOCK(s);
    262   1.2   thorpej 			break;
    263   1.2   thorpej 		case LSSLEEP:
    264   1.2   thorpej 			t->l_stat = LSSUSPENDED;
    265   1.2   thorpej 			break;
    266   1.2   thorpej 		case LSIDL:
    267   1.2   thorpej 		case LSZOMB:
    268   1.2   thorpej 			return (EINTR); /* It's what Solaris does..... */
    269   1.2   thorpej 		case LSSTOP:
    270   1.2   thorpej 			panic("_lwp_suspend: Stopped LWP in running process!");
    271   1.2   thorpej 			break;
    272   1.2   thorpej 		case LSONPROC:
    273  1.29      fvdl 			/* XXX multiprocessor LWPs? Implement me! */
    274  1.29      fvdl 			return (EINVAL);
    275   1.2   thorpej 		}
    276   1.2   thorpej 	}
    277   1.2   thorpej 
    278   1.2   thorpej 	return (0);
    279   1.2   thorpej }
    280   1.2   thorpej 
    281   1.2   thorpej 
    282   1.2   thorpej int
    283  1.46  christos sys__lwp_continue(struct lwp *l, void *v, register_t *retval __unused)
    284   1.2   thorpej {
    285   1.2   thorpej 	struct sys__lwp_continue_args /* {
    286   1.2   thorpej 		syscallarg(lwpid_t) target;
    287   1.2   thorpej 	} */ *uap = v;
    288  1.14        cl 	int s, target_lid;
    289   1.2   thorpej 	struct proc *p = l->l_proc;
    290   1.2   thorpej 	struct lwp *t;
    291   1.2   thorpej 
    292  1.33       chs 	if (p->p_flag & P_SA)
    293  1.33       chs 		return EINVAL;
    294  1.33       chs 
    295   1.2   thorpej 	target_lid = SCARG(uap, target);
    296   1.2   thorpej 
    297   1.2   thorpej 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    298   1.2   thorpej 		if (t->l_lid == target_lid)
    299   1.2   thorpej 			break;
    300   1.2   thorpej 
    301   1.2   thorpej 	if (t == NULL)
    302   1.2   thorpej 		return (ESRCH);
    303   1.2   thorpej 
    304  1.14        cl 	SCHED_LOCK(s);
    305   1.2   thorpej 	lwp_continue(t);
    306  1.14        cl 	SCHED_UNLOCK(s);
    307   1.2   thorpej 
    308   1.2   thorpej 	return (0);
    309   1.2   thorpej }
    310   1.2   thorpej 
    311   1.2   thorpej void
    312   1.2   thorpej lwp_continue(struct lwp *l)
    313   1.2   thorpej {
    314   1.2   thorpej 
    315   1.2   thorpej 	DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
    316   1.2   thorpej 	    l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
    317   1.2   thorpej 	    l->l_wchan));
    318   1.2   thorpej 
    319   1.2   thorpej 	if (l->l_stat != LSSUSPENDED)
    320   1.2   thorpej 		return;
    321   1.2   thorpej 
    322   1.2   thorpej 	if (l->l_wchan == 0) {
    323   1.2   thorpej 		/* LWP was runnable before being suspended. */
    324   1.2   thorpej 		setrunnable(l);
    325   1.2   thorpej 	} else {
    326   1.2   thorpej 		/* LWP was sleeping before being suspended. */
    327   1.2   thorpej 		l->l_stat = LSSLEEP;
    328   1.2   thorpej 	}
    329   1.2   thorpej }
    330   1.2   thorpej 
    331   1.2   thorpej int
    332  1.46  christos sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval __unused)
    333   1.2   thorpej {
    334   1.2   thorpej 	struct sys__lwp_wakeup_args /* {
    335  1.28     skrll 		syscallarg(lwpid_t) target;
    336   1.2   thorpej 	} */ *uap = v;
    337   1.2   thorpej 	lwpid_t target_lid;
    338   1.2   thorpej 	struct lwp *t;
    339   1.2   thorpej 	struct proc *p;
    340  1.10      fvdl 	int error;
    341  1.10      fvdl 	int s;
    342   1.2   thorpej 
    343   1.2   thorpej 	p = l->l_proc;
    344   1.2   thorpej 	target_lid = SCARG(uap, target);
    345   1.2   thorpej 
    346  1.10      fvdl 	SCHED_LOCK(s);
    347  1.10      fvdl 
    348   1.2   thorpej 	LIST_FOREACH(t, &p->p_lwps, l_sibling)
    349   1.2   thorpej 		if (t->l_lid == target_lid)
    350   1.2   thorpej 			break;
    351   1.2   thorpej 
    352  1.10      fvdl 	if (t == NULL) {
    353  1.10      fvdl 		error = ESRCH;
    354  1.10      fvdl 		goto exit;
    355  1.10      fvdl 	}
    356   1.2   thorpej 
    357  1.10      fvdl 	if (t->l_stat != LSSLEEP) {
    358  1.10      fvdl 		error = ENODEV;
    359  1.10      fvdl 		goto exit;
    360  1.10      fvdl 	}
    361   1.2   thorpej 
    362  1.10      fvdl 	if ((t->l_flag & L_SINTR) == 0) {
    363  1.10      fvdl 		error = EBUSY;
    364  1.10      fvdl 		goto exit;
    365  1.10      fvdl 	}
    366  1.12      matt 	/*
    367  1.12      matt 	 * Tell ltsleep to wakeup.
    368  1.12      matt 	 */
    369  1.12      matt 	t->l_flag |= L_CANCELLED;
    370   1.2   thorpej 
    371   1.4   nathanw 	setrunnable(t);
    372  1.10      fvdl 	error = 0;
    373  1.10      fvdl exit:
    374  1.10      fvdl 	SCHED_UNLOCK(s);
    375  1.10      fvdl 
    376  1.11      fvdl 	return error;
    377   1.2   thorpej }
    378   1.2   thorpej 
    379   1.2   thorpej int
    380  1.46  christos sys__lwp_wait(struct lwp *l, void *v, register_t *retval __unused)
    381   1.2   thorpej {
    382   1.2   thorpej 	struct sys__lwp_wait_args /* {
    383   1.2   thorpej 		syscallarg(lwpid_t) wait_for;
    384   1.2   thorpej 		syscallarg(lwpid_t *) departed;
    385   1.2   thorpej 	} */ *uap = v;
    386   1.2   thorpej 	int error;
    387   1.2   thorpej 	lwpid_t dep;
    388   1.2   thorpej 
    389   1.2   thorpej 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    390   1.2   thorpej 	if (error)
    391   1.2   thorpej 		return (error);
    392   1.2   thorpej 
    393   1.2   thorpej 	if (SCARG(uap, departed)) {
    394   1.2   thorpej 		error = copyout(&dep, SCARG(uap, departed),
    395   1.2   thorpej 		    sizeof(dep));
    396   1.2   thorpej 		if (error)
    397   1.2   thorpej 			return (error);
    398   1.2   thorpej 	}
    399   1.2   thorpej 
    400   1.2   thorpej 	return (0);
    401   1.2   thorpej }
    402   1.2   thorpej 
    403   1.2   thorpej 
    404   1.2   thorpej int
    405   1.2   thorpej lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    406   1.2   thorpej {
    407   1.2   thorpej 	struct proc *p = l->l_proc;
    408   1.2   thorpej 	struct lwp *l2, *l3;
    409  1.19  jdolecek 	int nfound, error, wpri;
    410  1.18  jdolecek 	static const char waitstr1[] = "lwpwait";
    411  1.18  jdolecek 	static const char waitstr2[] = "lwpwait2";
    412   1.2   thorpej 
    413   1.2   thorpej 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    414   1.2   thorpej 	    p->p_pid, l->l_lid, lid));
    415   1.2   thorpej 
    416   1.2   thorpej 	if (lid == l->l_lid)
    417   1.2   thorpej 		return (EDEADLK); /* Waiting for ourselves makes no sense. */
    418   1.2   thorpej 
    419   1.2   thorpej 	wpri = PWAIT |
    420   1.2   thorpej 	    ((flags & LWPWAIT_EXITCONTROL) ? PNOEXITERR : PCATCH);
    421   1.2   thorpej  loop:
    422   1.2   thorpej 	nfound = 0;
    423   1.2   thorpej 	LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    424   1.2   thorpej 		if ((l2 == l) || (l2->l_flag & L_DETACHED) ||
    425   1.2   thorpej 		    ((lid != 0) && (lid != l2->l_lid)))
    426   1.2   thorpej 			continue;
    427   1.2   thorpej 
    428   1.2   thorpej 		nfound++;
    429   1.2   thorpej 		if (l2->l_stat == LSZOMB) {
    430   1.2   thorpej 			if (departed)
    431   1.2   thorpej 				*departed = l2->l_lid;
    432   1.2   thorpej 
    433  1.15       dsl 			simple_lock(&p->p_lock);
    434   1.2   thorpej 			LIST_REMOVE(l2, l_sibling);
    435   1.2   thorpej 			p->p_nlwps--;
    436   1.2   thorpej 			p->p_nzlwps--;
    437  1.15       dsl 			simple_unlock(&p->p_lock);
    438   1.2   thorpej 			/* XXX decrement limits */
    439   1.2   thorpej 
    440   1.2   thorpej 			pool_put(&lwp_pool, l2);
    441   1.2   thorpej 
    442   1.2   thorpej 			return (0);
    443   1.2   thorpej 		} else if (l2->l_stat == LSSLEEP ||
    444   1.2   thorpej 		           l2->l_stat == LSSUSPENDED) {
    445   1.2   thorpej 			/* Deadlock checks.
    446   1.2   thorpej 			 * 1. If all other LWPs are waiting for exits
    447   1.2   thorpej 			 *    or suspended, we would deadlock.
    448   1.2   thorpej 			 */
    449   1.2   thorpej 
    450   1.2   thorpej 			LIST_FOREACH(l3, &p->p_lwps, l_sibling) {
    451   1.2   thorpej 				if (l3 != l && (l3->l_stat != LSSUSPENDED) &&
    452   1.2   thorpej 				    !(l3->l_stat == LSSLEEP &&
    453   1.2   thorpej 					l3->l_wchan == (caddr_t) &p->p_nlwps))
    454   1.2   thorpej 					break;
    455   1.2   thorpej 			}
    456   1.2   thorpej 			if (l3 == NULL) /* Everyone else is waiting. */
    457   1.2   thorpej 				return (EDEADLK);
    458   1.2   thorpej 
    459   1.2   thorpej 			/* XXX we'd like to check for a cycle of waiting
    460   1.2   thorpej 			 * LWPs (specific LID waits, not any-LWP waits)
    461   1.2   thorpej 			 * and detect that sort of deadlock, but we don't
    462   1.2   thorpej 			 * have a good place to store the lwp that is
    463   1.2   thorpej 			 * being waited for. wchan is already filled with
    464   1.2   thorpej 			 * &p->p_nlwps, and putting the lwp address in
    465   1.2   thorpej 			 * there for deadlock tracing would require
    466   1.2   thorpej 			 * exiting LWPs to call wakeup on both their
    467   1.2   thorpej 			 * own address and &p->p_nlwps, to get threads
    468   1.2   thorpej 			 * sleeping on any LWP exiting.
    469   1.2   thorpej 			 *
    470   1.2   thorpej 			 * Revisit later. Maybe another auxillary
    471   1.2   thorpej 			 * storage location associated with sleeping
    472   1.2   thorpej 			 * is in order.
    473   1.2   thorpej 			 */
    474   1.2   thorpej 		}
    475   1.2   thorpej 	}
    476   1.2   thorpej 
    477   1.2   thorpej 	if (nfound == 0)
    478   1.2   thorpej 		return (ESRCH);
    479   1.2   thorpej 
    480   1.2   thorpej 	if ((error = tsleep((caddr_t) &p->p_nlwps, wpri,
    481   1.2   thorpej 	    (lid != 0) ? waitstr1 : waitstr2, 0)) != 0)
    482   1.2   thorpej 		return (error);
    483   1.2   thorpej 
    484   1.2   thorpej 	goto loop;
    485   1.2   thorpej }
    486   1.2   thorpej 
    487   1.2   thorpej 
    488   1.2   thorpej int
    489   1.2   thorpej newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem,
    490   1.2   thorpej     int flags, void *stack, size_t stacksize,
    491   1.2   thorpej     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    492   1.2   thorpej {
    493   1.2   thorpej 	struct lwp *l2;
    494  1.43    martin 	int s;
    495   1.2   thorpej 
    496   1.2   thorpej 	l2 = pool_get(&lwp_pool, PR_WAITOK);
    497   1.2   thorpej 
    498   1.2   thorpej 	l2->l_stat = LSIDL;
    499   1.2   thorpej 	l2->l_forw = l2->l_back = NULL;
    500   1.2   thorpej 	l2->l_proc = p2;
    501   1.2   thorpej 
    502  1.42  christos 	lwp_initspecific(l2);
    503  1.41   thorpej 
    504   1.2   thorpej 	memset(&l2->l_startzero, 0,
    505   1.2   thorpej 	       (unsigned) ((caddr_t)&l2->l_endzero -
    506   1.2   thorpej 			   (caddr_t)&l2->l_startzero));
    507   1.2   thorpej 	memcpy(&l2->l_startcopy, &l1->l_startcopy,
    508   1.2   thorpej 	       (unsigned) ((caddr_t)&l2->l_endcopy -
    509   1.2   thorpej 			   (caddr_t)&l2->l_startcopy));
    510   1.2   thorpej 
    511   1.2   thorpej #if !defined(MULTIPROCESSOR)
    512   1.2   thorpej 	/*
    513   1.2   thorpej 	 * In the single-processor case, all processes will always run
    514   1.2   thorpej 	 * on the same CPU.  So, initialize the child's CPU to the parent's
    515   1.2   thorpej 	 * now.  In the multiprocessor case, the child's CPU will be
    516   1.2   thorpej 	 * initialized in the low-level context switch code when the
    517   1.2   thorpej 	 * process runs.
    518   1.2   thorpej 	 */
    519   1.5      matt 	KASSERT(l1->l_cpu != NULL);
    520   1.2   thorpej 	l2->l_cpu = l1->l_cpu;
    521   1.2   thorpej #else
    522   1.2   thorpej 	/*
    523  1.24       wiz 	 * zero child's CPU pointer so we don't get trash.
    524   1.2   thorpej 	 */
    525   1.2   thorpej 	l2->l_cpu = NULL;
    526   1.2   thorpej #endif /* ! MULTIPROCESSOR */
    527   1.2   thorpej 
    528   1.2   thorpej 	l2->l_flag = inmem ? L_INMEM : 0;
    529   1.2   thorpej 	l2->l_flag |= (flags & LWP_DETACHED) ? L_DETACHED : 0;
    530   1.2   thorpej 
    531  1.37        ad 	lwp_update_creds(l2);
    532   1.2   thorpej 	callout_init(&l2->l_tsleep_ch);
    533   1.2   thorpej 
    534   1.2   thorpej 	if (rnewlwpp != NULL)
    535   1.2   thorpej 		*rnewlwpp = l2;
    536   1.2   thorpej 
    537  1.36      yamt 	l2->l_addr = UAREA_TO_USER(uaddr);
    538   1.2   thorpej 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    539   1.2   thorpej 	    (arg != NULL) ? arg : l2);
    540   1.2   thorpej 
    541  1.15       dsl 	simple_lock(&p2->p_lock);
    542   1.2   thorpej 	l2->l_lid = ++p2->p_nlwpid;
    543   1.2   thorpej 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    544   1.2   thorpej 	p2->p_nlwps++;
    545  1.15       dsl 	simple_unlock(&p2->p_lock);
    546   1.2   thorpej 
    547   1.2   thorpej 	/* XXX should be locked differently... */
    548   1.2   thorpej 	s = proclist_lock_write();
    549   1.2   thorpej 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    550   1.2   thorpej 	proclist_unlock_write(s);
    551   1.2   thorpej 
    552  1.16      manu 	if (p2->p_emul->e_lwp_fork)
    553  1.16      manu 		(*p2->p_emul->e_lwp_fork)(l1, l2);
    554  1.16      manu 
    555   1.2   thorpej 	return (0);
    556   1.2   thorpej }
    557   1.2   thorpej 
    558   1.2   thorpej 
    559   1.2   thorpej /*
    560   1.2   thorpej  * Quit the process. This will call cpu_exit, which will call cpu_switch,
    561   1.2   thorpej  * so this can only be used meaningfully if you're willing to switch away.
    562   1.2   thorpej  * Calling with l!=curlwp would be weird.
    563   1.2   thorpej  */
    564   1.2   thorpej void
    565   1.2   thorpej lwp_exit(struct lwp *l)
    566   1.2   thorpej {
    567   1.2   thorpej 	struct proc *p = l->l_proc;
    568   1.2   thorpej 	int s;
    569   1.2   thorpej 
    570   1.2   thorpej 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    571   1.2   thorpej 	DPRINTF((" nlwps: %d nrlwps %d nzlwps: %d\n",
    572   1.2   thorpej 	    p->p_nlwps, p->p_nrlwps, p->p_nzlwps));
    573   1.2   thorpej 
    574  1.16      manu 	if (p->p_emul->e_lwp_exit)
    575  1.16      manu 		(*p->p_emul->e_lwp_exit)(l);
    576  1.16      manu 
    577   1.2   thorpej 	/*
    578   1.2   thorpej 	 * If we are the last live LWP in a process, we need to exit
    579   1.2   thorpej 	 * the entire process (if that's not already going on). We do
    580   1.2   thorpej 	 * so with an exit status of zero, because it's a "controlled"
    581   1.2   thorpej 	 * exit, and because that's what Solaris does.
    582  1.45   thorpej 	 *
    583  1.45   thorpej 	 * Note: the last LWP's specificdata will be deleted here.
    584   1.2   thorpej 	 */
    585   1.2   thorpej 	if (((p->p_nlwps - p->p_nzlwps) == 1) && ((p->p_flag & P_WEXIT) == 0)) {
    586   1.2   thorpej 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    587   1.2   thorpej 		    p->p_pid, l->l_lid));
    588   1.2   thorpej 		exit1(l, 0);
    589  1.19  jdolecek 		/* NOTREACHED */
    590   1.2   thorpej 	}
    591   1.2   thorpej 
    592  1.45   thorpej 	/* Delete the specificdata while it's still safe to sleep. */
    593  1.45   thorpej 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
    594  1.45   thorpej 
    595   1.2   thorpej 	s = proclist_lock_write();
    596   1.2   thorpej 	LIST_REMOVE(l, l_list);
    597   1.2   thorpej 	proclist_unlock_write(s);
    598   1.2   thorpej 
    599  1.37        ad 	/*
    600  1.37        ad 	 * Release our cached credentials, and collate accounting flags.
    601  1.37        ad 	 */
    602  1.37        ad 	kauth_cred_free(l->l_cred);
    603  1.37        ad 	simple_lock(&p->p_lock);
    604  1.37        ad 	p->p_acflag |= l->l_acflag;
    605  1.37        ad 	simple_unlock(&p->p_lock);
    606  1.37        ad 
    607  1.19  jdolecek 	/* Free MD LWP resources */
    608  1.19  jdolecek #ifndef __NO_CPU_LWP_FREE
    609  1.19  jdolecek 	cpu_lwp_free(l, 0);
    610  1.19  jdolecek #endif
    611  1.19  jdolecek 
    612  1.31      yamt 	pmap_deactivate(l);
    613  1.31      yamt 
    614  1.31      yamt 	if (l->l_flag & L_DETACHED) {
    615  1.31      yamt 		simple_lock(&p->p_lock);
    616  1.31      yamt 		LIST_REMOVE(l, l_sibling);
    617  1.31      yamt 		p->p_nlwps--;
    618  1.31      yamt 		simple_unlock(&p->p_lock);
    619  1.31      yamt 
    620  1.31      yamt 		curlwp = NULL;
    621  1.31      yamt 		l->l_proc = NULL;
    622  1.31      yamt 	}
    623  1.31      yamt 
    624  1.30      yamt 	SCHED_LOCK(s);
    625   1.2   thorpej 	p->p_nrlwps--;
    626   1.2   thorpej 	l->l_stat = LSDEAD;
    627  1.30      yamt 	SCHED_UNLOCK(s);
    628   1.2   thorpej 
    629   1.2   thorpej 	/* This LWP no longer needs to hold the kernel lock. */
    630   1.2   thorpej 	KERNEL_PROC_UNLOCK(l);
    631   1.2   thorpej 
    632   1.2   thorpej 	/* cpu_exit() will not return */
    633  1.19  jdolecek 	cpu_exit(l);
    634   1.2   thorpej }
    635   1.2   thorpej 
    636  1.19  jdolecek /*
    637  1.19  jdolecek  * We are called from cpu_exit() once it is safe to schedule the
    638  1.19  jdolecek  * dead process's resources to be freed (i.e., once we've switched to
    639  1.19  jdolecek  * the idle PCB for the current CPU).
    640  1.19  jdolecek  *
    641  1.19  jdolecek  * NOTE: One must be careful with locking in this routine.  It's
    642  1.19  jdolecek  * called from a critical section in machine-dependent code, so
    643  1.19  jdolecek  * we should refrain from changing any interrupt state.
    644  1.19  jdolecek  */
    645   1.2   thorpej void
    646   1.2   thorpej lwp_exit2(struct lwp *l)
    647   1.2   thorpej {
    648  1.19  jdolecek 	struct proc *p;
    649   1.2   thorpej 
    650  1.22      yamt 	KERNEL_LOCK(LK_EXCLUSIVE);
    651  1.19  jdolecek 	/*
    652  1.19  jdolecek 	 * Free the VM resources we're still holding on to.
    653  1.19  jdolecek 	 */
    654  1.19  jdolecek 	uvm_lwp_exit(l);
    655  1.19  jdolecek 
    656  1.19  jdolecek 	if (l->l_flag & L_DETACHED) {
    657  1.19  jdolecek 		/* Nobody waits for detached LWPs. */
    658  1.19  jdolecek 		pool_put(&lwp_pool, l);
    659  1.22      yamt 		KERNEL_UNLOCK();
    660  1.19  jdolecek 	} else {
    661  1.26  junyoung 		l->l_stat = LSZOMB;
    662  1.19  jdolecek 		p = l->l_proc;
    663  1.19  jdolecek 		p->p_nzlwps++;
    664  1.22      yamt 		KERNEL_UNLOCK();
    665  1.19  jdolecek 		wakeup(&p->p_nlwps);
    666  1.19  jdolecek 	}
    667   1.2   thorpej }
    668   1.2   thorpej 
    669   1.2   thorpej /*
    670   1.2   thorpej  * Pick a LWP to represent the process for those operations which
    671   1.2   thorpej  * want information about a "process" that is actually associated
    672   1.2   thorpej  * with a LWP.
    673   1.2   thorpej  */
    674   1.2   thorpej struct lwp *
    675  1.21  junyoung proc_representative_lwp(struct proc *p)
    676   1.2   thorpej {
    677   1.2   thorpej 	struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
    678  1.27      matt 	struct lwp *signalled;
    679   1.2   thorpej 
    680   1.2   thorpej 	/* Trivial case: only one LWP */
    681   1.2   thorpej 	if (p->p_nlwps == 1)
    682   1.2   thorpej 		return (LIST_FIRST(&p->p_lwps));
    683   1.2   thorpej 
    684   1.2   thorpej 	switch (p->p_stat) {
    685   1.2   thorpej 	case SSTOP:
    686   1.2   thorpej 	case SACTIVE:
    687   1.2   thorpej 		/* Pick the most live LWP */
    688   1.2   thorpej 		onproc = running = sleeping = stopped = suspended = NULL;
    689  1.27      matt 		signalled = NULL;
    690   1.2   thorpej 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    691  1.27      matt 			if (l->l_lid == p->p_sigctx.ps_lwp)
    692  1.27      matt 				signalled = l;
    693   1.2   thorpej 			switch (l->l_stat) {
    694   1.2   thorpej 			case LSONPROC:
    695   1.2   thorpej 				onproc = l;
    696   1.2   thorpej 				break;
    697   1.2   thorpej 			case LSRUN:
    698   1.2   thorpej 				running = l;
    699   1.2   thorpej 				break;
    700   1.2   thorpej 			case LSSLEEP:
    701   1.2   thorpej 				sleeping = l;
    702   1.2   thorpej 				break;
    703   1.2   thorpej 			case LSSTOP:
    704   1.2   thorpej 				stopped = l;
    705   1.2   thorpej 				break;
    706   1.2   thorpej 			case LSSUSPENDED:
    707   1.2   thorpej 				suspended = l;
    708   1.2   thorpej 				break;
    709   1.2   thorpej 			}
    710   1.2   thorpej 		}
    711  1.27      matt 		if (signalled)
    712  1.27      matt 			return signalled;
    713   1.3   nathanw 		if (onproc)
    714   1.3   nathanw 			return onproc;
    715   1.3   nathanw 		if (running)
    716   1.3   nathanw 			return running;
    717   1.3   nathanw 		if (sleeping)
    718   1.3   nathanw 			return sleeping;
    719   1.3   nathanw 		if (stopped)
    720   1.3   nathanw 			return stopped;
    721   1.3   nathanw 		if (suspended)
    722   1.3   nathanw 			return suspended;
    723   1.2   thorpej 		break;
    724   1.2   thorpej 	case SZOMB:
    725   1.2   thorpej 		/* Doesn't really matter... */
    726   1.2   thorpej 		return (LIST_FIRST(&p->p_lwps));
    727   1.2   thorpej #ifdef DIAGNOSTIC
    728   1.2   thorpej 	case SIDL:
    729   1.2   thorpej 		/* We have more than one LWP and we're in SIDL?
    730   1.2   thorpej 		 * How'd that happen?
    731   1.2   thorpej 		 */
    732   1.2   thorpej 		panic("Too many LWPs (%d) in SIDL process %d (%s)",
    733   1.2   thorpej 		    p->p_nrlwps, p->p_pid, p->p_comm);
    734   1.2   thorpej 	default:
    735   1.2   thorpej 		panic("Process %d (%s) in unknown state %d",
    736   1.2   thorpej 		    p->p_pid, p->p_comm, p->p_stat);
    737   1.2   thorpej #endif
    738   1.2   thorpej 	}
    739   1.2   thorpej 
    740   1.2   thorpej 	panic("proc_representative_lwp: couldn't find a lwp for process"
    741   1.2   thorpej 		" %d (%s)", p->p_pid, p->p_comm);
    742   1.2   thorpej 	/* NOTREACHED */
    743   1.2   thorpej 	return NULL;
    744   1.2   thorpej }
    745  1.37        ad 
    746  1.37        ad /*
    747  1.37        ad  * Update an LWP's cached credentials to mirror the process' master copy.
    748  1.37        ad  *
    749  1.37        ad  * This happens early in the syscall path, on user trap, and on LWP
    750  1.37        ad  * creation.  A long-running LWP can also voluntarily choose to update
    751  1.37        ad  * it's credentials by calling this routine.  This may be called from
    752  1.37        ad  * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
    753  1.37        ad  */
    754  1.37        ad void
    755  1.37        ad lwp_update_creds(struct lwp *l)
    756  1.37        ad {
    757  1.37        ad 	kauth_cred_t oc;
    758  1.37        ad 	struct proc *p;
    759  1.37        ad 
    760  1.37        ad 	p = l->l_proc;
    761  1.37        ad 	oc = l->l_cred;
    762  1.37        ad 
    763  1.37        ad 	simple_lock(&p->p_lock);
    764  1.37        ad 	kauth_cred_hold(p->p_cred);
    765  1.37        ad 	l->l_cred = p->p_cred;
    766  1.37        ad 	simple_unlock(&p->p_lock);
    767  1.37        ad 	if (oc != NULL)
    768  1.37        ad 		kauth_cred_free(oc);
    769  1.37        ad }
    770  1.41   thorpej 
    771  1.41   thorpej /*
    772  1.41   thorpej  * lwp_specific_key_create --
    773  1.41   thorpej  *	Create a key for subsystem lwp-specific data.
    774  1.41   thorpej  */
    775  1.41   thorpej int
    776  1.41   thorpej lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
    777  1.41   thorpej {
    778  1.41   thorpej 
    779  1.45   thorpej 	return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
    780  1.41   thorpej }
    781  1.41   thorpej 
    782  1.41   thorpej /*
    783  1.41   thorpej  * lwp_specific_key_delete --
    784  1.41   thorpej  *	Delete a key for subsystem lwp-specific data.
    785  1.41   thorpej  */
    786  1.41   thorpej void
    787  1.41   thorpej lwp_specific_key_delete(specificdata_key_t key)
    788  1.41   thorpej {
    789  1.41   thorpej 
    790  1.41   thorpej 	specificdata_key_delete(lwp_specificdata_domain, key);
    791  1.41   thorpej }
    792  1.41   thorpej 
    793  1.45   thorpej /*
    794  1.45   thorpej  * lwp_initspecific --
    795  1.45   thorpej  *	Initialize an LWP's specificdata container.
    796  1.45   thorpej  */
    797  1.42  christos void
    798  1.42  christos lwp_initspecific(struct lwp *l)
    799  1.42  christos {
    800  1.42  christos 	int error;
    801  1.45   thorpej 
    802  1.42  christos 	error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
    803  1.42  christos 	KASSERT(error == 0);
    804  1.42  christos }
    805  1.42  christos 
    806  1.41   thorpej /*
    807  1.45   thorpej  * lwp_finispecific --
    808  1.45   thorpej  *	Finalize an LWP's specificdata container.
    809  1.45   thorpej  */
    810  1.45   thorpej void
    811  1.45   thorpej lwp_finispecific(struct lwp *l)
    812  1.45   thorpej {
    813  1.45   thorpej 
    814  1.45   thorpej 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
    815  1.45   thorpej }
    816  1.45   thorpej 
    817  1.45   thorpej /*
    818  1.41   thorpej  * lwp_getspecific --
    819  1.41   thorpej  *	Return lwp-specific data corresponding to the specified key.
    820  1.41   thorpej  *
    821  1.41   thorpej  *	Note: LWP specific data is NOT INTERLOCKED.  An LWP should access
    822  1.41   thorpej  *	only its OWN SPECIFIC DATA.  If it is necessary to access another
    823  1.41   thorpej  *	LWP's specifc data, care must be taken to ensure that doing so
    824  1.41   thorpej  *	would not cause internal data structure inconsistency (i.e. caller
    825  1.41   thorpej  *	can guarantee that the target LWP is not inside an lwp_getspecific()
    826  1.41   thorpej  *	or lwp_setspecific() call).
    827  1.41   thorpej  */
    828  1.41   thorpej void *
    829  1.44   thorpej lwp_getspecific(specificdata_key_t key)
    830  1.41   thorpej {
    831  1.41   thorpej 
    832  1.41   thorpej 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
    833  1.44   thorpej 						  &curlwp->l_specdataref, key));
    834  1.41   thorpej }
    835  1.41   thorpej 
    836  1.47   hannken void *
    837  1.47   hannken _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
    838  1.47   hannken {
    839  1.47   hannken 
    840  1.47   hannken 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
    841  1.47   hannken 						  &l->l_specdataref, key));
    842  1.47   hannken }
    843  1.47   hannken 
    844  1.41   thorpej /*
    845  1.41   thorpej  * lwp_setspecific --
    846  1.41   thorpej  *	Set lwp-specific data corresponding to the specified key.
    847  1.41   thorpej  */
    848  1.41   thorpej void
    849  1.45   thorpej lwp_setspecific(specificdata_key_t key, void *data)
    850  1.41   thorpej {
    851  1.41   thorpej 
    852  1.41   thorpej 	specificdata_setspecific(lwp_specificdata_domain,
    853  1.44   thorpej 				 &curlwp->l_specdataref, key, data);
    854  1.41   thorpej }
    855