Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.1.2.2
      1  1.1.2.2  ad /*	$NetBSD: sys_lwp.c,v 1.1.2.2 2006/10/24 21:10:21 ad Exp $	*/
      2  1.1.2.1  ad 
      3  1.1.2.1  ad /*-
      4  1.1.2.1  ad  * Copyright (c) 2001, 2006 The NetBSD Foundation, Inc.
      5  1.1.2.1  ad  * All rights reserved.
      6  1.1.2.1  ad  *
      7  1.1.2.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  ad  * by Nathan J. Williams, and Andrew Doran.
      9  1.1.2.1  ad  *
     10  1.1.2.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  ad  * modification, are permitted provided that the following conditions
     12  1.1.2.1  ad  * are met:
     13  1.1.2.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.1  ad  *    must display the following acknowledgement:
     20  1.1.2.1  ad  *        This product includes software developed by the NetBSD
     21  1.1.2.1  ad  *        Foundation, Inc. and its contributors.
     22  1.1.2.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1.2.1  ad  *    from this software without specific prior written permission.
     25  1.1.2.1  ad  *
     26  1.1.2.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.1  ad  */
     38  1.1.2.1  ad 
     39  1.1.2.1  ad #include <sys/cdefs.h>
     40  1.1.2.2  ad __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.1.2.2 2006/10/24 21:10:21 ad Exp $");
     41  1.1.2.1  ad 
     42  1.1.2.1  ad #include <sys/param.h>
     43  1.1.2.1  ad #include <sys/systm.h>
     44  1.1.2.1  ad #include <sys/pool.h>
     45  1.1.2.1  ad #include <sys/proc.h>
     46  1.1.2.1  ad #include <sys/sa.h>
     47  1.1.2.1  ad #include <sys/savar.h>
     48  1.1.2.1  ad #include <sys/types.h>
     49  1.1.2.1  ad #include <sys/syscallargs.h>
     50  1.1.2.2  ad #include <sys/kauth.h>
     51  1.1.2.1  ad 
     52  1.1.2.1  ad #include <uvm/uvm_extern.h>
     53  1.1.2.1  ad 
     54  1.1.2.1  ad /* ARGSUSED */
     55  1.1.2.1  ad int
     56  1.1.2.1  ad sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     57  1.1.2.1  ad {
     58  1.1.2.1  ad 	struct sys__lwp_create_args /* {
     59  1.1.2.1  ad 		syscallarg(const ucontext_t *) ucp;
     60  1.1.2.1  ad 		syscallarg(u_long) flags;
     61  1.1.2.1  ad 		syscallarg(lwpid_t *) new_lwp;
     62  1.1.2.1  ad 	} */ *uap = v;
     63  1.1.2.1  ad 	struct proc *p = l->l_proc;
     64  1.1.2.1  ad 	struct lwp *l2;
     65  1.1.2.1  ad 	vaddr_t uaddr;
     66  1.1.2.1  ad 	boolean_t inmem;
     67  1.1.2.1  ad 	ucontext_t *newuc;
     68  1.1.2.1  ad 	int error, lid;
     69  1.1.2.1  ad 
     70  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
     71  1.1.2.1  ad 	if (p->p_flag & (P_SA | P_WEXIT)) {
     72  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
     73  1.1.2.1  ad 		return EINVAL;
     74  1.1.2.1  ad 	}
     75  1.1.2.1  ad 	/* XXXAD p->p_flag |= P_NOSA; */
     76  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
     77  1.1.2.1  ad 
     78  1.1.2.1  ad 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     79  1.1.2.1  ad 
     80  1.1.2.1  ad 	error = copyin(SCARG(uap, ucp), newuc,
     81  1.1.2.1  ad 	    l->l_proc->p_emul->e_sa->sae_ucsize);
     82  1.1.2.1  ad 	if (error) {
     83  1.1.2.1  ad 		pool_put(&lwp_uc_pool, newuc);
     84  1.1.2.1  ad 		return (error);
     85  1.1.2.1  ad 	}
     86  1.1.2.1  ad 
     87  1.1.2.1  ad 	/* XXX check against resource limits */
     88  1.1.2.1  ad 
     89  1.1.2.1  ad 	inmem = uvm_uarea_alloc(&uaddr);
     90  1.1.2.1  ad 	if (__predict_false(uaddr == 0)) {
     91  1.1.2.1  ad 		pool_put(&lwp_uc_pool, newuc);
     92  1.1.2.1  ad 		return (ENOMEM);
     93  1.1.2.1  ad 	}
     94  1.1.2.1  ad 
     95  1.1.2.1  ad 	/* XXX flags:
     96  1.1.2.1  ad 	 * __LWP_ASLWP is probably needed for Solaris compat.
     97  1.1.2.1  ad 	 */
     98  1.1.2.1  ad 
     99  1.1.2.1  ad 	newlwp(l, p, uaddr, inmem,
    100  1.1.2.1  ad 	    SCARG(uap, flags) & LWP_DETACHED,
    101  1.1.2.1  ad 	    NULL, 0, startlwp, newuc, &l2);
    102  1.1.2.1  ad 
    103  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    104  1.1.2.1  ad 	lwp_lock(l2);
    105  1.1.2.1  ad 	lid = l2->l_lid;
    106  1.1.2.1  ad 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0) {
    107  1.1.2.1  ad 		p->p_nrlwps++;
    108  1.1.2.2  ad 		lwp_relock(l2, &sched_mutex);
    109  1.1.2.1  ad 		l2->l_stat = LSRUN;
    110  1.1.2.1  ad 		setrunqueue(l2);
    111  1.1.2.1  ad 	} else
    112  1.1.2.1  ad 		l2->l_stat = LSSUSPENDED;
    113  1.1.2.1  ad 	lwp_unlock(l2);
    114  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    115  1.1.2.1  ad 
    116  1.1.2.1  ad 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    117  1.1.2.1  ad 	if (error) {
    118  1.1.2.1  ad 		/* XXX We should destroy the LWP. */
    119  1.1.2.1  ad 		return (error);
    120  1.1.2.1  ad 	}
    121  1.1.2.1  ad 
    122  1.1.2.1  ad 	return (0);
    123  1.1.2.1  ad }
    124  1.1.2.1  ad 
    125  1.1.2.1  ad int
    126  1.1.2.1  ad sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    127  1.1.2.1  ad {
    128  1.1.2.1  ad 
    129  1.1.2.1  ad 	lwp_exit(l);
    130  1.1.2.1  ad 	/* NOTREACHED */
    131  1.1.2.1  ad 	return (0);
    132  1.1.2.1  ad }
    133  1.1.2.1  ad 
    134  1.1.2.1  ad int
    135  1.1.2.1  ad sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    136  1.1.2.1  ad {
    137  1.1.2.1  ad 
    138  1.1.2.1  ad 	*retval = l->l_lid;
    139  1.1.2.1  ad 
    140  1.1.2.1  ad 	return (0);
    141  1.1.2.1  ad }
    142  1.1.2.1  ad 
    143  1.1.2.1  ad int
    144  1.1.2.1  ad sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    145  1.1.2.1  ad {
    146  1.1.2.1  ad 
    147  1.1.2.1  ad 	mb_read();
    148  1.1.2.1  ad 	*retval = (uintptr_t) l->l_private;
    149  1.1.2.1  ad 
    150  1.1.2.1  ad 	return (0);
    151  1.1.2.1  ad }
    152  1.1.2.1  ad 
    153  1.1.2.1  ad int
    154  1.1.2.1  ad sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    155  1.1.2.1  ad {
    156  1.1.2.1  ad 	struct sys__lwp_setprivate_args /* {
    157  1.1.2.1  ad 		syscallarg(void *) ptr;
    158  1.1.2.1  ad 	} */ *uap = v;
    159  1.1.2.1  ad 
    160  1.1.2.1  ad 	l->l_private = SCARG(uap, ptr);
    161  1.1.2.1  ad 	mb_write();
    162  1.1.2.1  ad 
    163  1.1.2.1  ad 	return (0);
    164  1.1.2.1  ad }
    165  1.1.2.1  ad 
    166  1.1.2.1  ad int
    167  1.1.2.1  ad sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    168  1.1.2.1  ad {
    169  1.1.2.1  ad 	struct sys__lwp_suspend_args /* {
    170  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    171  1.1.2.1  ad 	} */ *uap = v;
    172  1.1.2.1  ad 	struct proc *p = l->l_proc;
    173  1.1.2.1  ad 	struct lwp *t;
    174  1.1.2.1  ad 	int error;
    175  1.1.2.1  ad 
    176  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    177  1.1.2.1  ad 
    178  1.1.2.1  ad 	if (p->p_flag & P_SA) {
    179  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    180  1.1.2.1  ad 		return EINVAL;
    181  1.1.2.1  ad 	}
    182  1.1.2.1  ad 
    183  1.1.2.1  ad 	if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
    184  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    185  1.1.2.1  ad 		return (ESRCH);
    186  1.1.2.1  ad 	}
    187  1.1.2.1  ad 
    188  1.1.2.1  ad 	/*
    189  1.1.2.1  ad 	 * Check for deadlock, which is only possible when we're suspending
    190  1.1.2.1  ad 	 * ourself.
    191  1.1.2.1  ad 	 */
    192  1.1.2.1  ad 	if (t == l && l->l_stat == LSONPROC && p->p_nrlwps == 1) {
    193  1.1.2.1  ad 		lwp_unlock(l);
    194  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    195  1.1.2.1  ad 		return (EDEADLK);
    196  1.1.2.1  ad 	}
    197  1.1.2.1  ad 
    198  1.1.2.1  ad 	/*
    199  1.1.2.1  ad 	 * Suspend the LWP.  If it's on a different CPU, we need to wait for
    200  1.1.2.1  ad 	 * it to be preempted, where it will put itself to sleep.  If not
    201  1.1.2.1  ad 	 * suspending ourselves, the LWP will be returned unlocked.
    202  1.1.2.1  ad 	 */
    203  1.1.2.1  ad 	error = lwp_halt(l, t, LSSUSPENDED);
    204  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    205  1.1.2.1  ad 
    206  1.1.2.1  ad 	/*
    207  1.1.2.1  ad 	 * If we suspended ourself, we need to sleep now.
    208  1.1.2.1  ad 	 */
    209  1.1.2.1  ad 	if (t == l && !error) {
    210  1.1.2.1  ad 		lwp_lock(l);
    211  1.1.2.1  ad 		l->l_nvcsw++;
    212  1.1.2.1  ad 		mi_switch(t, NULL);
    213  1.1.2.1  ad 	}
    214  1.1.2.1  ad 
    215  1.1.2.1  ad 	return (error);
    216  1.1.2.1  ad }
    217  1.1.2.1  ad 
    218  1.1.2.1  ad int
    219  1.1.2.1  ad sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    220  1.1.2.1  ad {
    221  1.1.2.1  ad 	struct sys__lwp_continue_args /* {
    222  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    223  1.1.2.1  ad 	} */ *uap = v;
    224  1.1.2.1  ad 	int error;
    225  1.1.2.1  ad 	struct proc *p = l->l_proc;
    226  1.1.2.1  ad 	struct lwp *t;
    227  1.1.2.1  ad 
    228  1.1.2.1  ad 	error = 0;
    229  1.1.2.1  ad 
    230  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    231  1.1.2.1  ad 
    232  1.1.2.1  ad 	if (p->p_flag & P_SA)
    233  1.1.2.1  ad 		error = EINVAL;
    234  1.1.2.1  ad 	else if ((t = lwp_byid(p, SCARG(uap, target))) == NULL)
    235  1.1.2.1  ad 		error = ESRCH;
    236  1.1.2.1  ad 	else if (t == l || t->l_stat != LSSUSPENDED)
    237  1.1.2.1  ad 		lwp_unlock(t);
    238  1.1.2.1  ad 	else
    239  1.1.2.1  ad 		lwp_continue(t);
    240  1.1.2.1  ad 
    241  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    242  1.1.2.1  ad 
    243  1.1.2.1  ad 	return (error);
    244  1.1.2.1  ad }
    245  1.1.2.1  ad 
    246  1.1.2.1  ad int
    247  1.1.2.1  ad sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    248  1.1.2.1  ad {
    249  1.1.2.1  ad 	struct sys__lwp_wakeup_args /* {
    250  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    251  1.1.2.1  ad 	} */ *uap = v;
    252  1.1.2.1  ad 	struct lwp *t;
    253  1.1.2.1  ad 	struct proc *p;
    254  1.1.2.1  ad 	int error;
    255  1.1.2.1  ad 
    256  1.1.2.1  ad 	p = l->l_proc;
    257  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    258  1.1.2.1  ad 
    259  1.1.2.1  ad 	if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
    260  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    261  1.1.2.1  ad 		return ESRCH;
    262  1.1.2.1  ad 	}
    263  1.1.2.1  ad 
    264  1.1.2.1  ad 	if (t->l_stat != LSSLEEP) {
    265  1.1.2.1  ad 		error = ENODEV;
    266  1.1.2.1  ad 		goto bad;
    267  1.1.2.1  ad 	}
    268  1.1.2.1  ad 
    269  1.1.2.1  ad 	if ((t->l_flag & L_SINTR) == 0) {
    270  1.1.2.1  ad 		error = EBUSY;
    271  1.1.2.1  ad 		goto bad;
    272  1.1.2.1  ad 	}
    273  1.1.2.1  ad 
    274  1.1.2.1  ad 	/*
    275  1.1.2.1  ad 	 * Tell ltsleep to wakeup.  setrunnable() will release the mutex.
    276  1.1.2.1  ad 	 */
    277  1.1.2.1  ad 	t->l_flag |= L_CANCELLED;
    278  1.1.2.1  ad 	setrunnable(t);
    279  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    280  1.1.2.1  ad 	return 0;
    281  1.1.2.1  ad 
    282  1.1.2.1  ad  bad:
    283  1.1.2.1  ad  	lwp_unlock(l);
    284  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    285  1.1.2.1  ad 	return error;
    286  1.1.2.1  ad }
    287  1.1.2.1  ad 
    288  1.1.2.1  ad int
    289  1.1.2.1  ad sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    290  1.1.2.1  ad {
    291  1.1.2.1  ad 	struct sys__lwp_wait_args /* {
    292  1.1.2.1  ad 		syscallarg(lwpid_t) wait_for;
    293  1.1.2.1  ad 		syscallarg(lwpid_t *) departed;
    294  1.1.2.1  ad 	} */ *uap = v;
    295  1.1.2.1  ad 	struct proc *p = l->l_proc;
    296  1.1.2.1  ad 	int error;
    297  1.1.2.1  ad 	lwpid_t dep;
    298  1.1.2.1  ad 
    299  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    300  1.1.2.1  ad 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    301  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    302  1.1.2.1  ad 	if (error)
    303  1.1.2.1  ad 		return (error);
    304  1.1.2.1  ad 
    305  1.1.2.1  ad 	if (SCARG(uap, departed)) {
    306  1.1.2.1  ad 		error = copyout(&dep, SCARG(uap, departed),
    307  1.1.2.1  ad 		    sizeof(dep));
    308  1.1.2.1  ad 		if (error)
    309  1.1.2.1  ad 			return (error);
    310  1.1.2.1  ad 	}
    311  1.1.2.1  ad 
    312  1.1.2.1  ad 	return (0);
    313  1.1.2.1  ad }
    314  1.1.2.2  ad 
    315  1.1.2.2  ad /* ARGSUSED */
    316  1.1.2.2  ad int
    317  1.1.2.2  ad sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    318  1.1.2.2  ad {
    319  1.1.2.2  ad 	struct sys__lwp_kill_args /* {
    320  1.1.2.2  ad 		syscallarg(lwpid_t)	target;
    321  1.1.2.2  ad 		syscallarg(int)		signo;
    322  1.1.2.2  ad 	} */ *uap = v;
    323  1.1.2.2  ad 	struct proc *p = l->l_proc;
    324  1.1.2.2  ad 	struct lwp *t;
    325  1.1.2.2  ad 	ksiginfo_t ksi;
    326  1.1.2.2  ad 	int signo = SCARG(uap, signo);
    327  1.1.2.2  ad 	int error;
    328  1.1.2.2  ad 
    329  1.1.2.2  ad 	if ((u_int)signo >= NSIG)
    330  1.1.2.2  ad 		return (EINVAL);
    331  1.1.2.2  ad 
    332  1.1.2.2  ad 	KSI_INIT(&ksi);
    333  1.1.2.2  ad 	ksi.ksi_signo = signo;
    334  1.1.2.2  ad 	ksi.ksi_code = SI_USER;
    335  1.1.2.2  ad 	ksi.ksi_pid = p->p_pid;
    336  1.1.2.2  ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    337  1.1.2.2  ad 	ksi.ksi_lid = SCARG(uap, target);
    338  1.1.2.2  ad 
    339  1.1.2.2  ad 	rw_enter(&proclist_lock, RW_READER);
    340  1.1.2.2  ad 	mutex_enter(&p->p_smutex);
    341  1.1.2.2  ad 	if ((t = lwp_byid(p, ksi.ksi_lid)) == NULL)
    342  1.1.2.2  ad 		error = ESRCH;
    343  1.1.2.2  ad 	else {
    344  1.1.2.2  ad 		lwp_unlock(t);
    345  1.1.2.2  ad 		kpsignal2(p, &ksi);
    346  1.1.2.2  ad 		error = 0;
    347  1.1.2.2  ad 	}
    348  1.1.2.2  ad 	mutex_exit(&p->p_smutex);
    349  1.1.2.2  ad 	rw_exit(&proclist_lock);
    350  1.1.2.2  ad 
    351  1.1.2.2  ad 	return (0);
    352  1.1.2.2  ad }
    353