Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.1.2.3
      1  1.1.2.3  ad /*	$NetBSD: sys_lwp.c,v 1.1.2.3 2006/11/17 16:34:37 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.3  ad /*
     40  1.1.2.3  ad  * Lightweight process (LWP) system calls.  See kern_lwp.c for a description
     41  1.1.2.3  ad  * of LWPs.
     42  1.1.2.3  ad  */
     43  1.1.2.3  ad 
     44  1.1.2.1  ad #include <sys/cdefs.h>
     45  1.1.2.3  ad __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.1.2.3 2006/11/17 16:34:37 ad Exp $");
     46  1.1.2.1  ad 
     47  1.1.2.1  ad #include <sys/param.h>
     48  1.1.2.1  ad #include <sys/systm.h>
     49  1.1.2.1  ad #include <sys/pool.h>
     50  1.1.2.1  ad #include <sys/proc.h>
     51  1.1.2.1  ad #include <sys/sa.h>
     52  1.1.2.1  ad #include <sys/savar.h>
     53  1.1.2.1  ad #include <sys/types.h>
     54  1.1.2.1  ad #include <sys/syscallargs.h>
     55  1.1.2.2  ad #include <sys/kauth.h>
     56  1.1.2.1  ad 
     57  1.1.2.1  ad #include <uvm/uvm_extern.h>
     58  1.1.2.1  ad 
     59  1.1.2.1  ad /* ARGSUSED */
     60  1.1.2.1  ad int
     61  1.1.2.1  ad sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     62  1.1.2.1  ad {
     63  1.1.2.1  ad 	struct sys__lwp_create_args /* {
     64  1.1.2.1  ad 		syscallarg(const ucontext_t *) ucp;
     65  1.1.2.1  ad 		syscallarg(u_long) flags;
     66  1.1.2.1  ad 		syscallarg(lwpid_t *) new_lwp;
     67  1.1.2.1  ad 	} */ *uap = v;
     68  1.1.2.1  ad 	struct proc *p = l->l_proc;
     69  1.1.2.1  ad 	struct lwp *l2;
     70  1.1.2.1  ad 	vaddr_t uaddr;
     71  1.1.2.1  ad 	boolean_t inmem;
     72  1.1.2.1  ad 	ucontext_t *newuc;
     73  1.1.2.1  ad 	int error, lid;
     74  1.1.2.1  ad 
     75  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
     76  1.1.2.3  ad 	if ((p->p_sflag & (PS_SA | PS_WEXIT)) != 0 || p->p_sa != NULL) {
     77  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
     78  1.1.2.1  ad 		return EINVAL;
     79  1.1.2.1  ad 	}
     80  1.1.2.3  ad 	p->p_sflag |= PS_NOSA;
     81  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
     82  1.1.2.1  ad 
     83  1.1.2.1  ad 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     84  1.1.2.1  ad 
     85  1.1.2.1  ad 	error = copyin(SCARG(uap, ucp), newuc,
     86  1.1.2.1  ad 	    l->l_proc->p_emul->e_sa->sae_ucsize);
     87  1.1.2.1  ad 	if (error) {
     88  1.1.2.1  ad 		pool_put(&lwp_uc_pool, newuc);
     89  1.1.2.1  ad 		return (error);
     90  1.1.2.1  ad 	}
     91  1.1.2.1  ad 
     92  1.1.2.1  ad 	/* XXX check against resource limits */
     93  1.1.2.1  ad 
     94  1.1.2.1  ad 	inmem = uvm_uarea_alloc(&uaddr);
     95  1.1.2.1  ad 	if (__predict_false(uaddr == 0)) {
     96  1.1.2.1  ad 		pool_put(&lwp_uc_pool, newuc);
     97  1.1.2.1  ad 		return (ENOMEM);
     98  1.1.2.1  ad 	}
     99  1.1.2.1  ad 
    100  1.1.2.1  ad 	/* XXX flags:
    101  1.1.2.1  ad 	 * __LWP_ASLWP is probably needed for Solaris compat.
    102  1.1.2.1  ad 	 */
    103  1.1.2.1  ad 
    104  1.1.2.1  ad 	newlwp(l, p, uaddr, inmem,
    105  1.1.2.1  ad 	    SCARG(uap, flags) & LWP_DETACHED,
    106  1.1.2.1  ad 	    NULL, 0, startlwp, newuc, &l2);
    107  1.1.2.1  ad 
    108  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    109  1.1.2.1  ad 	lwp_lock(l2);
    110  1.1.2.1  ad 	lid = l2->l_lid;
    111  1.1.2.3  ad 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    112  1.1.2.3  ad 	    (l->l_flag & L_WREBOOT) == 0) {
    113  1.1.2.1  ad 		p->p_nrlwps++;
    114  1.1.2.2  ad 		lwp_relock(l2, &sched_mutex);
    115  1.1.2.1  ad 		l2->l_stat = LSRUN;
    116  1.1.2.1  ad 		setrunqueue(l2);
    117  1.1.2.1  ad 	} else
    118  1.1.2.1  ad 		l2->l_stat = LSSUSPENDED;
    119  1.1.2.1  ad 	lwp_unlock(l2);
    120  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    121  1.1.2.1  ad 
    122  1.1.2.1  ad 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    123  1.1.2.1  ad 	if (error) {
    124  1.1.2.1  ad 		/* XXX We should destroy the LWP. */
    125  1.1.2.1  ad 		return (error);
    126  1.1.2.1  ad 	}
    127  1.1.2.1  ad 
    128  1.1.2.1  ad 	return (0);
    129  1.1.2.1  ad }
    130  1.1.2.1  ad 
    131  1.1.2.1  ad int
    132  1.1.2.1  ad sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    133  1.1.2.1  ad {
    134  1.1.2.1  ad 
    135  1.1.2.3  ad 	return lwp_exit(l, 1);
    136  1.1.2.1  ad }
    137  1.1.2.1  ad 
    138  1.1.2.1  ad int
    139  1.1.2.1  ad sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    140  1.1.2.1  ad {
    141  1.1.2.1  ad 
    142  1.1.2.1  ad 	*retval = l->l_lid;
    143  1.1.2.1  ad 
    144  1.1.2.1  ad 	return (0);
    145  1.1.2.1  ad }
    146  1.1.2.1  ad 
    147  1.1.2.1  ad int
    148  1.1.2.1  ad sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    149  1.1.2.1  ad {
    150  1.1.2.1  ad 
    151  1.1.2.1  ad 	mb_read();
    152  1.1.2.1  ad 	*retval = (uintptr_t) l->l_private;
    153  1.1.2.1  ad 
    154  1.1.2.1  ad 	return (0);
    155  1.1.2.1  ad }
    156  1.1.2.1  ad 
    157  1.1.2.1  ad int
    158  1.1.2.1  ad sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    159  1.1.2.1  ad {
    160  1.1.2.1  ad 	struct sys__lwp_setprivate_args /* {
    161  1.1.2.1  ad 		syscallarg(void *) ptr;
    162  1.1.2.1  ad 	} */ *uap = v;
    163  1.1.2.1  ad 
    164  1.1.2.1  ad 	l->l_private = SCARG(uap, ptr);
    165  1.1.2.1  ad 	mb_write();
    166  1.1.2.1  ad 
    167  1.1.2.1  ad 	return (0);
    168  1.1.2.1  ad }
    169  1.1.2.1  ad 
    170  1.1.2.1  ad int
    171  1.1.2.1  ad sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    172  1.1.2.1  ad {
    173  1.1.2.1  ad 	struct sys__lwp_suspend_args /* {
    174  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    175  1.1.2.1  ad 	} */ *uap = v;
    176  1.1.2.1  ad 	struct proc *p = l->l_proc;
    177  1.1.2.1  ad 	struct lwp *t;
    178  1.1.2.1  ad 	int error;
    179  1.1.2.1  ad 
    180  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    181  1.1.2.1  ad 
    182  1.1.2.3  ad 	if ((p->p_flag & P_SA) != 0 || p->p_sa != NULL) {
    183  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    184  1.1.2.1  ad 		return EINVAL;
    185  1.1.2.1  ad 	}
    186  1.1.2.1  ad 
    187  1.1.2.1  ad 	if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
    188  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    189  1.1.2.1  ad 		return (ESRCH);
    190  1.1.2.1  ad 	}
    191  1.1.2.1  ad 
    192  1.1.2.1  ad 	/*
    193  1.1.2.1  ad 	 * Check for deadlock, which is only possible when we're suspending
    194  1.1.2.1  ad 	 * ourself.
    195  1.1.2.1  ad 	 */
    196  1.1.2.1  ad 	if (t == l && l->l_stat == LSONPROC && p->p_nrlwps == 1) {
    197  1.1.2.1  ad 		lwp_unlock(l);
    198  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    199  1.1.2.1  ad 		return (EDEADLK);
    200  1.1.2.1  ad 	}
    201  1.1.2.1  ad 
    202  1.1.2.1  ad 	/*
    203  1.1.2.1  ad 	 * Suspend the LWP.  If it's on a different CPU, we need to wait for
    204  1.1.2.1  ad 	 * it to be preempted, where it will put itself to sleep.  If not
    205  1.1.2.1  ad 	 * suspending ourselves, the LWP will be returned unlocked.
    206  1.1.2.1  ad 	 */
    207  1.1.2.1  ad 	error = lwp_halt(l, t, LSSUSPENDED);
    208  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    209  1.1.2.1  ad 
    210  1.1.2.1  ad 	/*
    211  1.1.2.1  ad 	 * If we suspended ourself, we need to sleep now.
    212  1.1.2.1  ad 	 */
    213  1.1.2.1  ad 	if (t == l && !error) {
    214  1.1.2.1  ad 		lwp_lock(l);
    215  1.1.2.1  ad 		l->l_nvcsw++;
    216  1.1.2.1  ad 		mi_switch(t, NULL);
    217  1.1.2.1  ad 	}
    218  1.1.2.1  ad 
    219  1.1.2.1  ad 	return (error);
    220  1.1.2.1  ad }
    221  1.1.2.1  ad 
    222  1.1.2.1  ad int
    223  1.1.2.1  ad sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    224  1.1.2.1  ad {
    225  1.1.2.1  ad 	struct sys__lwp_continue_args /* {
    226  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    227  1.1.2.1  ad 	} */ *uap = v;
    228  1.1.2.1  ad 	int error;
    229  1.1.2.1  ad 	struct proc *p = l->l_proc;
    230  1.1.2.1  ad 	struct lwp *t;
    231  1.1.2.1  ad 
    232  1.1.2.1  ad 	error = 0;
    233  1.1.2.1  ad 
    234  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    235  1.1.2.1  ad 
    236  1.1.2.3  ad 	if ((p->p_flag & P_SA) != 0 || p->p_sa != NULL)
    237  1.1.2.1  ad 		error = EINVAL;
    238  1.1.2.1  ad 	else if ((t = lwp_byid(p, SCARG(uap, target))) == NULL)
    239  1.1.2.1  ad 		error = ESRCH;
    240  1.1.2.1  ad 	else if (t == l || t->l_stat != LSSUSPENDED)
    241  1.1.2.1  ad 		lwp_unlock(t);
    242  1.1.2.1  ad 	else
    243  1.1.2.1  ad 		lwp_continue(t);
    244  1.1.2.1  ad 
    245  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    246  1.1.2.1  ad 
    247  1.1.2.1  ad 	return (error);
    248  1.1.2.1  ad }
    249  1.1.2.1  ad 
    250  1.1.2.1  ad int
    251  1.1.2.1  ad sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    252  1.1.2.1  ad {
    253  1.1.2.1  ad 	struct sys__lwp_wakeup_args /* {
    254  1.1.2.1  ad 		syscallarg(lwpid_t) target;
    255  1.1.2.1  ad 	} */ *uap = v;
    256  1.1.2.1  ad 	struct lwp *t;
    257  1.1.2.1  ad 	struct proc *p;
    258  1.1.2.1  ad 	int error;
    259  1.1.2.1  ad 
    260  1.1.2.1  ad 	p = l->l_proc;
    261  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    262  1.1.2.1  ad 
    263  1.1.2.1  ad 	if ((t = lwp_byid(p, SCARG(uap, target))) == NULL) {
    264  1.1.2.1  ad 		mutex_exit(&p->p_smutex);
    265  1.1.2.1  ad 		return ESRCH;
    266  1.1.2.1  ad 	}
    267  1.1.2.1  ad 
    268  1.1.2.1  ad 	if (t->l_stat != LSSLEEP) {
    269  1.1.2.1  ad 		error = ENODEV;
    270  1.1.2.1  ad 		goto bad;
    271  1.1.2.1  ad 	}
    272  1.1.2.1  ad 
    273  1.1.2.1  ad 	if ((t->l_flag & L_SINTR) == 0) {
    274  1.1.2.1  ad 		error = EBUSY;
    275  1.1.2.1  ad 		goto bad;
    276  1.1.2.1  ad 	}
    277  1.1.2.1  ad 
    278  1.1.2.3  ad 	/* wake it up  setrunnable() will release the LWP lock. */
    279  1.1.2.1  ad 	t->l_flag |= L_CANCELLED;
    280  1.1.2.1  ad 	setrunnable(t);
    281  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    282  1.1.2.1  ad 	return 0;
    283  1.1.2.1  ad 
    284  1.1.2.1  ad  bad:
    285  1.1.2.1  ad  	lwp_unlock(l);
    286  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    287  1.1.2.1  ad 	return error;
    288  1.1.2.1  ad }
    289  1.1.2.1  ad 
    290  1.1.2.1  ad int
    291  1.1.2.1  ad sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    292  1.1.2.1  ad {
    293  1.1.2.1  ad 	struct sys__lwp_wait_args /* {
    294  1.1.2.1  ad 		syscallarg(lwpid_t) wait_for;
    295  1.1.2.1  ad 		syscallarg(lwpid_t *) departed;
    296  1.1.2.1  ad 	} */ *uap = v;
    297  1.1.2.1  ad 	struct proc *p = l->l_proc;
    298  1.1.2.1  ad 	int error;
    299  1.1.2.1  ad 	lwpid_t dep;
    300  1.1.2.1  ad 
    301  1.1.2.1  ad 	mutex_enter(&p->p_smutex);
    302  1.1.2.1  ad 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    303  1.1.2.1  ad 	mutex_exit(&p->p_smutex);
    304  1.1.2.1  ad 	if (error)
    305  1.1.2.1  ad 		return (error);
    306  1.1.2.1  ad 
    307  1.1.2.1  ad 	if (SCARG(uap, departed)) {
    308  1.1.2.1  ad 		error = copyout(&dep, SCARG(uap, departed),
    309  1.1.2.1  ad 		    sizeof(dep));
    310  1.1.2.1  ad 		if (error)
    311  1.1.2.1  ad 			return (error);
    312  1.1.2.1  ad 	}
    313  1.1.2.1  ad 
    314  1.1.2.1  ad 	return (0);
    315  1.1.2.1  ad }
    316  1.1.2.2  ad 
    317  1.1.2.2  ad /* ARGSUSED */
    318  1.1.2.2  ad int
    319  1.1.2.2  ad sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    320  1.1.2.2  ad {
    321  1.1.2.2  ad 	struct sys__lwp_kill_args /* {
    322  1.1.2.2  ad 		syscallarg(lwpid_t)	target;
    323  1.1.2.2  ad 		syscallarg(int)		signo;
    324  1.1.2.2  ad 	} */ *uap = v;
    325  1.1.2.2  ad 	struct proc *p = l->l_proc;
    326  1.1.2.2  ad 	struct lwp *t;
    327  1.1.2.2  ad 	ksiginfo_t ksi;
    328  1.1.2.2  ad 	int signo = SCARG(uap, signo);
    329  1.1.2.2  ad 	int error;
    330  1.1.2.2  ad 
    331  1.1.2.2  ad 	if ((u_int)signo >= NSIG)
    332  1.1.2.2  ad 		return (EINVAL);
    333  1.1.2.2  ad 
    334  1.1.2.2  ad 	KSI_INIT(&ksi);
    335  1.1.2.2  ad 	ksi.ksi_signo = signo;
    336  1.1.2.2  ad 	ksi.ksi_code = SI_USER;
    337  1.1.2.2  ad 	ksi.ksi_pid = p->p_pid;
    338  1.1.2.2  ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    339  1.1.2.2  ad 	ksi.ksi_lid = SCARG(uap, target);
    340  1.1.2.2  ad 
    341  1.1.2.2  ad 	rw_enter(&proclist_lock, RW_READER);
    342  1.1.2.2  ad 	mutex_enter(&p->p_smutex);
    343  1.1.2.2  ad 	if ((t = lwp_byid(p, ksi.ksi_lid)) == NULL)
    344  1.1.2.2  ad 		error = ESRCH;
    345  1.1.2.2  ad 	else {
    346  1.1.2.2  ad 		lwp_unlock(t);
    347  1.1.2.2  ad 		kpsignal2(p, &ksi);
    348  1.1.2.2  ad 		error = 0;
    349  1.1.2.2  ad 	}
    350  1.1.2.2  ad 	mutex_exit(&p->p_smutex);
    351  1.1.2.2  ad 	rw_exit(&proclist_lock);
    352  1.1.2.2  ad 
    353  1.1.2.3  ad 	return (error);
    354  1.1.2.2  ad }
    355