Home | History | Annotate | Line # | Download | only in rumpkern
lwproc.c revision 1.35
      1  1.35     pooka /*      $NetBSD: lwproc.c,v 1.35 2015/04/18 15:49:18 pooka Exp $	*/
      2   1.1     pooka 
      3   1.1     pooka /*
      4   1.7     pooka  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
      5   1.1     pooka  *
      6   1.1     pooka  * Redistribution and use in source and binary forms, with or without
      7   1.1     pooka  * modification, are permitted provided that the following conditions
      8   1.1     pooka  * are met:
      9   1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     10   1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     11   1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     13   1.1     pooka  *    documentation and/or other materials provided with the distribution.
     14   1.1     pooka  *
     15   1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.1     pooka  * SUCH DAMAGE.
     26   1.1     pooka  */
     27   1.1     pooka 
     28  1.28     pooka #define RUMP__CURLWP_PRIVATE
     29  1.27     pooka 
     30   1.1     pooka #include <sys/cdefs.h>
     31  1.35     pooka __KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.35 2015/04/18 15:49:18 pooka Exp $");
     32   1.1     pooka 
     33   1.1     pooka #include <sys/param.h>
     34   1.1     pooka #include <sys/atomic.h>
     35   1.1     pooka #include <sys/filedesc.h>
     36   1.1     pooka #include <sys/kauth.h>
     37   1.1     pooka #include <sys/kmem.h>
     38   1.1     pooka #include <sys/lwp.h>
     39  1.25     pooka #include <sys/ktrace.h>
     40   1.1     pooka #include <sys/pool.h>
     41   1.1     pooka #include <sys/proc.h>
     42   1.1     pooka #include <sys/queue.h>
     43   1.1     pooka #include <sys/resourcevar.h>
     44   1.1     pooka #include <sys/uidinfo.h>
     45   1.1     pooka 
     46   1.1     pooka #include <rump/rumpuser.h>
     47   1.1     pooka #include "rump_private.h"
     48  1.27     pooka #include "rump_curlwp.h"
     49   1.1     pooka 
     50  1.20     pooka struct emul *emul_default = &emul_netbsd;
     51  1.20     pooka 
     52  1.27     pooka void
     53  1.27     pooka rump_lwproc_init(void)
     54  1.27     pooka {
     55  1.27     pooka 
     56  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, &lwp0);
     57  1.27     pooka }
     58  1.27     pooka 
     59  1.27     pooka struct lwp *
     60  1.27     pooka rump_lwproc_curlwp_hypercall(void)
     61  1.27     pooka {
     62  1.27     pooka 
     63  1.27     pooka 	return rumpuser_curlwp();
     64  1.27     pooka }
     65  1.27     pooka 
     66  1.27     pooka void
     67  1.27     pooka rump_lwproc_curlwp_set(struct lwp *l)
     68  1.27     pooka {
     69  1.27     pooka 
     70  1.27     pooka 	KASSERT(curlwp == NULL);
     71  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_SET, l);
     72  1.27     pooka }
     73  1.27     pooka 
     74  1.27     pooka void
     75  1.27     pooka rump_lwproc_curlwp_clear(struct lwp *l)
     76  1.27     pooka {
     77  1.27     pooka 
     78  1.27     pooka 	KASSERT(l == curlwp);
     79  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
     80  1.27     pooka }
     81  1.27     pooka 
     82   1.1     pooka static void
     83   1.1     pooka lwproc_proc_free(struct proc *p)
     84   1.1     pooka {
     85   1.1     pooka 	kauth_cred_t cred;
     86  1.30     pooka 	struct proc *child;
     87   1.1     pooka 
     88  1.25     pooka 	KASSERT(p->p_stat == SDYING || p->p_stat == SDEAD);
     89  1.25     pooka 
     90  1.25     pooka #ifdef KTRACE
     91  1.25     pooka 	if (p->p_tracep) {
     92  1.25     pooka 		mutex_enter(&ktrace_lock);
     93  1.25     pooka 		ktrderef(p);
     94  1.25     pooka 		mutex_exit(&ktrace_lock);
     95  1.25     pooka 	}
     96  1.25     pooka #endif
     97  1.25     pooka 
     98   1.1     pooka 	mutex_enter(proc_lock);
     99   1.1     pooka 
    100  1.30     pooka 	/* childranee eunt initus */
    101  1.30     pooka 	while ((child = LIST_FIRST(&p->p_children)) != NULL) {
    102  1.30     pooka 		LIST_REMOVE(child, p_sibling);
    103  1.30     pooka 		child->p_pptr = initproc;
    104  1.30     pooka 		child->p_ppid = 1;
    105  1.30     pooka 		LIST_INSERT_HEAD(&initproc->p_children, child, p_sibling);
    106  1.30     pooka 	}
    107  1.30     pooka 
    108   1.1     pooka 	KASSERT(p->p_nlwps == 0);
    109   1.1     pooka 	KASSERT(LIST_EMPTY(&p->p_lwps));
    110   1.1     pooka 
    111   1.1     pooka 	LIST_REMOVE(p, p_list);
    112   1.1     pooka 	LIST_REMOVE(p, p_sibling);
    113   1.1     pooka 	proc_free_pid(p->p_pid); /* decrements nprocs */
    114   1.1     pooka 	proc_leavepgrp(p); /* releases proc_lock */
    115   1.1     pooka 
    116   1.1     pooka 	cred = p->p_cred;
    117   1.1     pooka 	chgproccnt(kauth_cred_getuid(cred), -1);
    118  1.31     pooka 	rump_proc_vfs_release(p);
    119   1.1     pooka 
    120  1.29     pooka 	doexithooks(p);
    121  1.18  pgoyette 	lim_free(p->p_limit);
    122   1.1     pooka 	pstatsfree(p->p_stats);
    123   1.1     pooka 	kauth_cred_free(p->p_cred);
    124   1.1     pooka 	proc_finispecific(p);
    125   1.1     pooka 
    126   1.1     pooka 	mutex_obj_free(p->p_lock);
    127   1.1     pooka 	mutex_destroy(&p->p_stmutex);
    128   1.1     pooka 	mutex_destroy(&p->p_auxlock);
    129   1.1     pooka 	rw_destroy(&p->p_reflock);
    130   1.1     pooka 	cv_destroy(&p->p_waitcv);
    131   1.1     pooka 	cv_destroy(&p->p_lwpcv);
    132   1.1     pooka 
    133  1.33     pooka 	/* non-local vmspaces are not shared */
    134  1.10     pooka 	if (!RUMP_LOCALPROC_P(p)) {
    135  1.35     pooka 		struct rump_spctl *ctl = (struct rump_spctl *)p->p_vmspace;
    136   1.6     pooka 		KASSERT(p->p_vmspace->vm_refcnt == 1);
    137  1.35     pooka 		kmem_free(ctl, sizeof(*ctl));
    138   1.6     pooka 	}
    139   1.6     pooka 
    140   1.1     pooka 	proc_free_mem(p);
    141   1.1     pooka }
    142   1.1     pooka 
    143   1.1     pooka /*
    144   1.1     pooka  * Allocate a new process.  Mostly mimic fork by
    145   1.1     pooka  * copying the properties of the parent.  However, there are some
    146  1.20     pooka  * differences.
    147   1.1     pooka  *
    148   1.1     pooka  * Switch to the new lwp and return a pointer to it.
    149   1.1     pooka  */
    150   1.1     pooka static struct proc *
    151  1.33     pooka lwproc_newproc(struct proc *parent, struct vmspace *vm, int flags)
    152   1.1     pooka {
    153   1.1     pooka 	uid_t uid = kauth_cred_getuid(parent->p_cred);
    154   1.1     pooka 	struct proc *p;
    155   1.1     pooka 
    156   1.1     pooka 	/* maxproc not enforced */
    157   1.1     pooka 	atomic_inc_uint(&nprocs);
    158   1.1     pooka 
    159   1.1     pooka 	/* allocate process */
    160   1.1     pooka 	p = proc_alloc();
    161   1.1     pooka 	memset(&p->p_startzero, 0,
    162   1.1     pooka 	    offsetof(struct proc, p_endzero)
    163   1.1     pooka 	      - offsetof(struct proc, p_startzero));
    164   1.1     pooka 	memcpy(&p->p_startcopy, &parent->p_startcopy,
    165   1.1     pooka 	    offsetof(struct proc, p_endcopy)
    166   1.1     pooka 	      - offsetof(struct proc, p_startcopy));
    167   1.1     pooka 
    168  1.15     pooka 	/* some other garbage we need to zero */
    169  1.15     pooka 	p->p_sigacts = NULL;
    170  1.15     pooka 	p->p_aio = NULL;
    171  1.15     pooka 	p->p_dtrace = NULL;
    172  1.15     pooka 	p->p_mqueue_cnt = p->p_exitsig = 0;
    173  1.15     pooka 	p->p_flag = p->p_sflag = p->p_slflag = p->p_lflag = p->p_stflag = 0;
    174  1.15     pooka 	p->p_trace_enabled = 0;
    175  1.15     pooka 	p->p_xstat = p->p_acflag = 0;
    176  1.15     pooka 	p->p_stackbase = 0;
    177  1.15     pooka 
    178   1.1     pooka 	p->p_stats = pstatscopy(parent->p_stats);
    179   1.1     pooka 
    180  1.33     pooka 	p->p_vmspace = vm;
    181  1.20     pooka 	p->p_emul = emul_default;
    182  1.26     pooka #ifdef __HAVE_SYSCALL_INTERN
    183  1.26     pooka 	p->p_emul->e_syscall_intern(p);
    184  1.26     pooka #endif
    185  1.14     pooka 	if (*parent->p_comm)
    186  1.14     pooka 		strcpy(p->p_comm, parent->p_comm);
    187  1.14     pooka 	else
    188  1.14     pooka 		strcpy(p->p_comm, "rumproc");
    189   1.7     pooka 
    190   1.7     pooka 	if ((flags & RUMP_RFCFDG) == 0)
    191   1.7     pooka 		KASSERT(parent == curproc);
    192   1.7     pooka 	if (flags & RUMP_RFFDG)
    193   1.7     pooka 		p->p_fd = fd_copy();
    194   1.7     pooka 	else if (flags & RUMP_RFCFDG)
    195   1.7     pooka 		p->p_fd = fd_init(NULL);
    196   1.7     pooka 	else
    197   1.7     pooka 		fd_share(p);
    198   1.7     pooka 
    199   1.1     pooka 	lim_addref(parent->p_limit);
    200   1.1     pooka 	p->p_limit = parent->p_limit;
    201   1.1     pooka 
    202   1.1     pooka 	LIST_INIT(&p->p_lwps);
    203   1.1     pooka 	LIST_INIT(&p->p_children);
    204   1.1     pooka 
    205   1.1     pooka 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    206  1.21     pooka 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    207   1.1     pooka 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    208   1.1     pooka 	rw_init(&p->p_reflock);
    209   1.1     pooka 	cv_init(&p->p_waitcv, "pwait");
    210   1.1     pooka 	cv_init(&p->p_lwpcv, "plwp");
    211   1.1     pooka 
    212   1.1     pooka 	p->p_pptr = parent;
    213   1.1     pooka 	p->p_ppid = parent->p_pid;
    214  1.12     pooka 	p->p_stat = SACTIVE;
    215   1.1     pooka 
    216   1.1     pooka 	kauth_proc_fork(parent, p);
    217   1.1     pooka 
    218   1.1     pooka 	/* initialize cwd in rump kernels with vfs */
    219  1.31     pooka 	rump_proc_vfs_init(p);
    220   1.1     pooka 
    221   1.1     pooka 	chgproccnt(uid, 1); /* not enforced */
    222   1.1     pooka 
    223   1.1     pooka 	/* publish proc various proc lists */
    224   1.1     pooka 	mutex_enter(proc_lock);
    225   1.1     pooka 	LIST_INSERT_HEAD(&allproc, p, p_list);
    226   1.1     pooka 	LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
    227   1.1     pooka 	LIST_INSERT_AFTER(parent, p, p_pglist);
    228   1.1     pooka 	mutex_exit(proc_lock);
    229   1.1     pooka 
    230   1.1     pooka 	return p;
    231   1.1     pooka }
    232   1.1     pooka 
    233   1.1     pooka static void
    234   1.1     pooka lwproc_freelwp(struct lwp *l)
    235   1.1     pooka {
    236   1.1     pooka 	struct proc *p;
    237   1.1     pooka 
    238   1.1     pooka 	p = l->l_proc;
    239   1.1     pooka 	mutex_enter(p->p_lock);
    240   1.1     pooka 
    241   1.1     pooka 	KASSERT(l->l_flag & LW_WEXIT);
    242   1.1     pooka 	KASSERT(l->l_refcnt == 0);
    243   1.1     pooka 
    244   1.1     pooka 	/* ok, zero references, continue with nuke */
    245   1.1     pooka 	LIST_REMOVE(l, l_sibling);
    246   1.1     pooka 	KASSERT(p->p_nlwps >= 1);
    247   1.1     pooka 	if (--p->p_nlwps == 0) {
    248   1.1     pooka 		KASSERT(p != &proc0);
    249   1.1     pooka 		p->p_stat = SDEAD;
    250  1.32     pooka 	} else {
    251  1.32     pooka 		chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
    252   1.1     pooka 	}
    253  1.22     pooka 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */
    254   1.1     pooka 	kauth_cred_free(l->l_cred);
    255   1.1     pooka 	mutex_exit(p->p_lock);
    256   1.1     pooka 
    257   1.1     pooka 	mutex_enter(proc_lock);
    258   1.1     pooka 	LIST_REMOVE(l, l_list);
    259   1.1     pooka 	mutex_exit(proc_lock);
    260   1.1     pooka 
    261   1.1     pooka 	if (l->l_name)
    262   1.1     pooka 		kmem_free(l->l_name, MAXCOMLEN);
    263   1.1     pooka 	lwp_finispecific(l);
    264   1.1     pooka 
    265  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_DESTROY, l);
    266  1.22     pooka 	membar_exit();
    267   1.1     pooka 	kmem_free(l, sizeof(*l));
    268   1.1     pooka 
    269   1.1     pooka 	if (p->p_stat == SDEAD)
    270   1.1     pooka 		lwproc_proc_free(p);
    271   1.1     pooka }
    272   1.1     pooka 
    273  1.12     pooka extern kmutex_t unruntime_lock;
    274  1.12     pooka 
    275   1.1     pooka /*
    276   1.1     pooka  * called with p_lock held, releases lock before return
    277   1.1     pooka  */
    278   1.1     pooka static void
    279   1.1     pooka lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
    280   1.1     pooka {
    281   1.1     pooka 
    282  1.32     pooka 	/*
    283  1.32     pooka 	 * Account the new lwp to the owner of the process.
    284  1.32     pooka 	 * For some reason, NetBSD doesn't count the first lwp
    285  1.32     pooka 	 * in a process as a lwp, so skip that.
    286  1.32     pooka 	 */
    287  1.32     pooka 	if (p->p_nlwps++) {
    288  1.32     pooka 		chglwpcnt(kauth_cred_getuid(p->p_cred), 1);
    289  1.32     pooka 	}
    290  1.32     pooka 
    291   1.1     pooka 	l->l_refcnt = 1;
    292   1.1     pooka 	l->l_proc = p;
    293   1.1     pooka 
    294   1.1     pooka 	l->l_lid = p->p_nlwpid++;
    295   1.1     pooka 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    296   1.1     pooka 
    297   1.1     pooka 	l->l_fd = p->p_fd;
    298  1.12     pooka 	l->l_cpu = rump_cpu;
    299   1.1     pooka 	l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
    300   1.9     pooka 	l->l_stat = LSRUN;
    301  1.12     pooka 	l->l_mutex = &unruntime_lock;
    302   1.8     pooka 	TAILQ_INIT(&l->l_ld_locks);
    303  1.16     pooka 	mutex_exit(p->p_lock);
    304   1.1     pooka 
    305  1.16     pooka 	lwp_update_creds(l);
    306   1.1     pooka 	lwp_initspecific(l);
    307   1.1     pooka 
    308  1.22     pooka 	membar_enter();
    309  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
    310   1.1     pooka 	if (doswitch) {
    311   1.1     pooka 		rump_lwproc_switch(l);
    312   1.1     pooka 	}
    313   1.1     pooka 
    314   1.1     pooka 	/* filedesc already has refcount 1 when process is created */
    315   1.1     pooka 	if (!procmake) {
    316   1.1     pooka 		fd_hold(l);
    317   1.1     pooka 	}
    318   1.1     pooka 
    319   1.1     pooka 	mutex_enter(proc_lock);
    320   1.1     pooka 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    321   1.1     pooka 	mutex_exit(proc_lock);
    322   1.1     pooka }
    323   1.1     pooka 
    324   1.1     pooka struct lwp *
    325   1.3     pooka rump__lwproc_alloclwp(struct proc *p)
    326   1.1     pooka {
    327   1.1     pooka 	struct lwp *l;
    328   1.3     pooka 	bool newproc = false;
    329   1.3     pooka 
    330   1.3     pooka 	if (p == NULL) {
    331  1.34     pooka 		p = lwproc_newproc(&proc0, rump_vmspace_local, RUMP_RFCFDG);
    332   1.3     pooka 		newproc = true;
    333   1.3     pooka 	}
    334   1.1     pooka 
    335   1.1     pooka 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    336   1.1     pooka 
    337   1.1     pooka 	mutex_enter(p->p_lock);
    338  1.17     pooka 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    339   1.3     pooka 	lwproc_makelwp(p, l, false, newproc);
    340   1.1     pooka 
    341   1.1     pooka 	return l;
    342   1.1     pooka }
    343   1.1     pooka 
    344   1.1     pooka int
    345   1.1     pooka rump_lwproc_newlwp(pid_t pid)
    346   1.1     pooka {
    347   1.1     pooka 	struct proc *p;
    348   1.1     pooka 	struct lwp *l;
    349   1.1     pooka 
    350   1.1     pooka 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    351   1.1     pooka 	mutex_enter(proc_lock);
    352   1.1     pooka 	p = proc_find_raw(pid);
    353   1.1     pooka 	if (p == NULL) {
    354   1.1     pooka 		mutex_exit(proc_lock);
    355   1.1     pooka 		kmem_free(l, sizeof(*l));
    356   1.1     pooka 		return ESRCH;
    357   1.1     pooka 	}
    358   1.1     pooka 	mutex_enter(p->p_lock);
    359  1.17     pooka 	if (p->p_sflag & PS_RUMP_LWPEXIT) {
    360  1.17     pooka 		mutex_exit(proc_lock);
    361  1.17     pooka 		mutex_exit(p->p_lock);
    362  1.17     pooka 		kmem_free(l, sizeof(*l));
    363  1.17     pooka 		return EBUSY;
    364  1.17     pooka 	}
    365   1.1     pooka 	mutex_exit(proc_lock);
    366   1.1     pooka 	lwproc_makelwp(p, l, true, false);
    367   1.1     pooka 
    368   1.1     pooka 	return 0;
    369   1.1     pooka }
    370   1.1     pooka 
    371   1.1     pooka int
    372  1.33     pooka rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
    373   1.1     pooka {
    374   1.1     pooka 	struct proc *p;
    375   1.1     pooka 	struct lwp *l;
    376   1.1     pooka 
    377   1.7     pooka 	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
    378   1.7     pooka 	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
    379   1.7     pooka 		return EINVAL;
    380   1.7     pooka 
    381  1.33     pooka 	p = lwproc_newproc(curproc, vm, flags);
    382   1.1     pooka 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    383   1.1     pooka 	mutex_enter(p->p_lock);
    384  1.17     pooka 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    385   1.1     pooka 	lwproc_makelwp(p, l, true, true);
    386   1.1     pooka 
    387   1.1     pooka 	return 0;
    388   1.1     pooka }
    389   1.1     pooka 
    390  1.33     pooka int
    391  1.33     pooka rump_lwproc_rfork(int flags)
    392  1.33     pooka {
    393  1.33     pooka 
    394  1.33     pooka 	return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
    395  1.33     pooka }
    396  1.33     pooka 
    397   1.1     pooka /*
    398   1.1     pooka  * Switch to a new process/thread.  Release previous one if
    399   1.4     pooka  * deemed to be exiting.  This is considered a slow path for
    400   1.4     pooka  * rump kernel entry.
    401   1.1     pooka  */
    402   1.1     pooka void
    403   1.1     pooka rump_lwproc_switch(struct lwp *newlwp)
    404   1.1     pooka {
    405   1.1     pooka 	struct lwp *l = curlwp;
    406   1.1     pooka 
    407   1.1     pooka 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    408   1.1     pooka 
    409   1.1     pooka 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    410   1.1     pooka 		panic("lwp %p (%d:%d) already running",
    411   1.1     pooka 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    412   1.1     pooka 
    413   1.1     pooka 	if (newlwp == NULL) {
    414   1.1     pooka 		l->l_pflag &= ~LP_RUNNING;
    415   1.1     pooka 		l->l_flag |= LW_RUMP_CLEAR;
    416   1.1     pooka 		return;
    417   1.1     pooka 	}
    418   1.1     pooka 
    419   1.1     pooka 	/* fd_free() must be called from curlwp context.  talk about ugh */
    420   1.1     pooka 	if (l->l_flag & LW_WEXIT) {
    421   1.1     pooka 		fd_free();
    422   1.1     pooka 	}
    423   1.1     pooka 
    424  1.24     pooka 	KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks);
    425  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
    426   1.1     pooka 
    427   1.1     pooka 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    428   1.1     pooka 	newlwp->l_mutex = l->l_mutex;
    429   1.1     pooka 	newlwp->l_pflag |= LP_RUNNING;
    430   1.1     pooka 
    431  1.27     pooka 	lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
    432  1.24     pooka 	curcpu()->ci_curlwp = newlwp;
    433  1.24     pooka 	KERNEL_LOCK(newlwp->l_biglocks, NULL);
    434   1.1     pooka 
    435   1.4     pooka 	/*
    436   1.4     pooka 	 * Check if the thread should get a signal.  This is
    437   1.4     pooka 	 * mostly to satisfy the "record" rump sigmodel.
    438   1.4     pooka 	 */
    439   1.4     pooka 	mutex_enter(newlwp->l_proc->p_lock);
    440   1.4     pooka 	if (sigispending(newlwp, 0)) {
    441   1.4     pooka 		newlwp->l_flag |= LW_PENDSIG;
    442   1.4     pooka 	}
    443   1.4     pooka 	mutex_exit(newlwp->l_proc->p_lock);
    444   1.4     pooka 
    445  1.12     pooka 	l->l_mutex = &unruntime_lock;
    446   1.1     pooka 	l->l_pflag &= ~LP_RUNNING;
    447   1.4     pooka 	l->l_flag &= ~LW_PENDSIG;
    448  1.13     pooka 	l->l_stat = LSRUN;
    449   1.1     pooka 
    450   1.1     pooka 	if (l->l_flag & LW_WEXIT) {
    451   1.1     pooka 		lwproc_freelwp(l);
    452   1.1     pooka 	}
    453   1.1     pooka }
    454   1.1     pooka 
    455  1.24     pooka /*
    456  1.24     pooka  * Mark the current thread to be released upon return from
    457  1.24     pooka  * kernel.
    458  1.24     pooka  */
    459   1.1     pooka void
    460   1.1     pooka rump_lwproc_releaselwp(void)
    461   1.1     pooka {
    462   1.1     pooka 	struct lwp *l = curlwp;
    463   1.1     pooka 
    464  1.24     pooka 	if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
    465   1.2     pooka 		panic("releasing non-pertinent lwp");
    466   1.2     pooka 
    467  1.24     pooka 	rump__lwproc_lwprele();
    468  1.24     pooka 	KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
    469  1.24     pooka }
    470  1.24     pooka 
    471  1.24     pooka /*
    472  1.24     pooka  * In-kernel routines used to add and remove references for the
    473  1.24     pooka  * current thread.  The main purpose is to make it possible for
    474  1.24     pooka  * implicit threads to persist over scheduling operations in
    475  1.24     pooka  * rump kernel drivers.  Note that we don't need p_lock in a
    476  1.24     pooka  * rump kernel, since we do refcounting only for curlwp.
    477  1.24     pooka  */
    478  1.24     pooka void
    479  1.24     pooka rump__lwproc_lwphold(void)
    480  1.24     pooka {
    481  1.24     pooka 	struct lwp *l = curlwp;
    482  1.24     pooka 
    483  1.24     pooka 	l->l_refcnt++;
    484  1.24     pooka 	l->l_flag &= ~LW_WEXIT;
    485  1.24     pooka }
    486  1.24     pooka 
    487  1.24     pooka void
    488  1.24     pooka rump__lwproc_lwprele(void)
    489  1.24     pooka {
    490  1.24     pooka 	struct lwp *l = curlwp;
    491  1.24     pooka 
    492   1.1     pooka 	l->l_refcnt--;
    493  1.24     pooka 	if (l->l_refcnt == 0)
    494  1.24     pooka 		l->l_flag |= LW_WEXIT;
    495   1.1     pooka }
    496   1.1     pooka 
    497   1.1     pooka struct lwp *
    498   1.1     pooka rump_lwproc_curlwp(void)
    499   1.1     pooka {
    500   1.1     pooka 	struct lwp *l = curlwp;
    501   1.1     pooka 
    502   1.1     pooka 	if (l->l_flag & LW_WEXIT)
    503   1.1     pooka 		return NULL;
    504   1.1     pooka 	return l;
    505   1.1     pooka }
    506  1.20     pooka 
    507  1.20     pooka /* this interface is under construction (like the proverbial 90's web page) */
    508  1.20     pooka int rump_i_know_what_i_am_doing_with_sysents = 0;
    509  1.20     pooka void
    510  1.20     pooka rump_lwproc_sysent_usenative()
    511  1.20     pooka {
    512  1.20     pooka 
    513  1.20     pooka 	if (!rump_i_know_what_i_am_doing_with_sysents)
    514  1.20     pooka 		panic("don't use rump_lwproc_sysent_usenative()");
    515  1.20     pooka 	curproc->p_emul = &emul_netbsd;
    516  1.20     pooka }
    517