Home | History | Annotate | Line # | Download | only in rumpkern
lwproc.c revision 1.20
      1 /*      $NetBSD: lwproc.c,v 1.20 2013/03/07 18:49:13 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, 2011 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.20 2013/03/07 18:49:13 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/atomic.h>
     33 #include <sys/filedesc.h>
     34 #include <sys/kauth.h>
     35 #include <sys/kmem.h>
     36 #include <sys/lwp.h>
     37 #include <sys/pool.h>
     38 #include <sys/proc.h>
     39 #include <sys/queue.h>
     40 #include <sys/resourcevar.h>
     41 #include <sys/uidinfo.h>
     42 
     43 #include <rump/rumpuser.h>
     44 
     45 #include "rump_private.h"
     46 
     47 struct emul *emul_default = &emul_netbsd;
     48 
     49 static void
     50 lwproc_proc_free(struct proc *p)
     51 {
     52 	kauth_cred_t cred;
     53 
     54 	mutex_enter(proc_lock);
     55 
     56 	KASSERT(p->p_nlwps == 0);
     57 	KASSERT(LIST_EMPTY(&p->p_lwps));
     58 	KASSERT(p->p_stat == SACTIVE || p->p_stat == SDYING ||
     59 	    p->p_stat == SDEAD);
     60 
     61 	LIST_REMOVE(p, p_list);
     62 	LIST_REMOVE(p, p_sibling);
     63 	proc_free_pid(p->p_pid); /* decrements nprocs */
     64 	proc_leavepgrp(p); /* releases proc_lock */
     65 
     66 	cred = p->p_cred;
     67 	chgproccnt(kauth_cred_getuid(cred), -1);
     68 	if (rump_proc_vfs_release)
     69 		rump_proc_vfs_release(p);
     70 
     71 	lim_free(p->p_limit);
     72 	pstatsfree(p->p_stats);
     73 	kauth_cred_free(p->p_cred);
     74 	proc_finispecific(p);
     75 
     76 	mutex_obj_free(p->p_lock);
     77 	mutex_destroy(&p->p_stmutex);
     78 	mutex_destroy(&p->p_auxlock);
     79 	rw_destroy(&p->p_reflock);
     80 	cv_destroy(&p->p_waitcv);
     81 	cv_destroy(&p->p_lwpcv);
     82 
     83 	/* non-kernel vmspaces are not shared */
     84 	if (!RUMP_LOCALPROC_P(p)) {
     85 		KASSERT(p->p_vmspace->vm_refcnt == 1);
     86 		kmem_free(p->p_vmspace, sizeof(*p->p_vmspace));
     87 	}
     88 
     89 	proc_free_mem(p);
     90 }
     91 
     92 /*
     93  * Allocate a new process.  Mostly mimic fork by
     94  * copying the properties of the parent.  However, there are some
     95  * differences.
     96  *
     97  * Switch to the new lwp and return a pointer to it.
     98  */
     99 static struct proc *
    100 lwproc_newproc(struct proc *parent, int flags)
    101 {
    102 	uid_t uid = kauth_cred_getuid(parent->p_cred);
    103 	struct proc *p;
    104 
    105 	/* maxproc not enforced */
    106 	atomic_inc_uint(&nprocs);
    107 
    108 	/* allocate process */
    109 	p = proc_alloc();
    110 	memset(&p->p_startzero, 0,
    111 	    offsetof(struct proc, p_endzero)
    112 	      - offsetof(struct proc, p_startzero));
    113 	memcpy(&p->p_startcopy, &parent->p_startcopy,
    114 	    offsetof(struct proc, p_endcopy)
    115 	      - offsetof(struct proc, p_startcopy));
    116 
    117 	/* some other garbage we need to zero */
    118 	p->p_sigacts = NULL;
    119 	p->p_aio = NULL;
    120 	p->p_dtrace = NULL;
    121 	p->p_mqueue_cnt = p->p_exitsig = 0;
    122 	p->p_flag = p->p_sflag = p->p_slflag = p->p_lflag = p->p_stflag = 0;
    123 	p->p_trace_enabled = 0;
    124 	p->p_xstat = p->p_acflag = 0;
    125 	p->p_stackbase = 0;
    126 
    127 	p->p_stats = pstatscopy(parent->p_stats);
    128 
    129 	p->p_vmspace = vmspace_kernel();
    130 	p->p_emul = emul_default;
    131 	if (*parent->p_comm)
    132 		strcpy(p->p_comm, parent->p_comm);
    133 	else
    134 		strcpy(p->p_comm, "rumproc");
    135 
    136 	if ((flags & RUMP_RFCFDG) == 0)
    137 		KASSERT(parent == curproc);
    138 	if (flags & RUMP_RFFDG)
    139 		p->p_fd = fd_copy();
    140 	else if (flags & RUMP_RFCFDG)
    141 		p->p_fd = fd_init(NULL);
    142 	else
    143 		fd_share(p);
    144 
    145 	lim_addref(parent->p_limit);
    146 	p->p_limit = parent->p_limit;
    147 
    148 	LIST_INIT(&p->p_lwps);
    149 	LIST_INIT(&p->p_children);
    150 
    151 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    152 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_NONE);
    153 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    154 	rw_init(&p->p_reflock);
    155 	cv_init(&p->p_waitcv, "pwait");
    156 	cv_init(&p->p_lwpcv, "plwp");
    157 
    158 	p->p_pptr = parent;
    159 	p->p_ppid = parent->p_pid;
    160 	p->p_stat = SACTIVE;
    161 
    162 	kauth_proc_fork(parent, p);
    163 
    164 	/* initialize cwd in rump kernels with vfs */
    165 	if (rump_proc_vfs_init)
    166 		rump_proc_vfs_init(p);
    167 
    168 	chgproccnt(uid, 1); /* not enforced */
    169 
    170 	/* publish proc various proc lists */
    171 	mutex_enter(proc_lock);
    172 	LIST_INSERT_HEAD(&allproc, p, p_list);
    173 	LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
    174 	LIST_INSERT_AFTER(parent, p, p_pglist);
    175 	mutex_exit(proc_lock);
    176 
    177 	return p;
    178 }
    179 
    180 static void
    181 lwproc_freelwp(struct lwp *l)
    182 {
    183 	struct proc *p;
    184 
    185 	p = l->l_proc;
    186 	mutex_enter(p->p_lock);
    187 
    188 	/* XXX: l_refcnt */
    189 	KASSERT(l->l_flag & LW_WEXIT);
    190 	KASSERT(l->l_refcnt == 0);
    191 
    192 	/* ok, zero references, continue with nuke */
    193 	LIST_REMOVE(l, l_sibling);
    194 	KASSERT(p->p_nlwps >= 1);
    195 	if (--p->p_nlwps == 0) {
    196 		KASSERT(p != &proc0);
    197 		p->p_stat = SDEAD;
    198 	}
    199 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in rump? */
    200 	kauth_cred_free(l->l_cred);
    201 	mutex_exit(p->p_lock);
    202 
    203 	mutex_enter(proc_lock);
    204 	LIST_REMOVE(l, l_list);
    205 	mutex_exit(proc_lock);
    206 
    207 	if (l->l_name)
    208 		kmem_free(l->l_name, MAXCOMLEN);
    209 	lwp_finispecific(l);
    210 
    211 	kmem_free(l, sizeof(*l));
    212 
    213 	if (p->p_stat == SDEAD)
    214 		lwproc_proc_free(p);
    215 }
    216 
    217 extern kmutex_t unruntime_lock;
    218 
    219 /*
    220  * called with p_lock held, releases lock before return
    221  */
    222 static void
    223 lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
    224 {
    225 
    226 	p->p_nlwps++;
    227 	l->l_refcnt = 1;
    228 	l->l_proc = p;
    229 
    230 	l->l_lid = p->p_nlwpid++;
    231 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    232 
    233 	l->l_fd = p->p_fd;
    234 	l->l_cpu = rump_cpu;
    235 	l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
    236 	l->l_stat = LSRUN;
    237 	l->l_mutex = &unruntime_lock;
    238 	TAILQ_INIT(&l->l_ld_locks);
    239 	mutex_exit(p->p_lock);
    240 
    241 	lwp_update_creds(l);
    242 	lwp_initspecific(l);
    243 
    244 	if (doswitch) {
    245 		rump_lwproc_switch(l);
    246 	}
    247 
    248 	/* filedesc already has refcount 1 when process is created */
    249 	if (!procmake) {
    250 		fd_hold(l);
    251 	}
    252 
    253 	mutex_enter(proc_lock);
    254 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    255 	mutex_exit(proc_lock);
    256 }
    257 
    258 struct lwp *
    259 rump__lwproc_alloclwp(struct proc *p)
    260 {
    261 	struct lwp *l;
    262 	bool newproc = false;
    263 
    264 	if (p == NULL) {
    265 		p = lwproc_newproc(&proc0, 0);
    266 		newproc = true;
    267 	}
    268 
    269 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    270 
    271 	mutex_enter(p->p_lock);
    272 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    273 	lwproc_makelwp(p, l, false, newproc);
    274 
    275 	return l;
    276 }
    277 
    278 int
    279 rump_lwproc_newlwp(pid_t pid)
    280 {
    281 	struct proc *p;
    282 	struct lwp *l;
    283 
    284 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    285 	mutex_enter(proc_lock);
    286 	p = proc_find_raw(pid);
    287 	if (p == NULL) {
    288 		mutex_exit(proc_lock);
    289 		kmem_free(l, sizeof(*l));
    290 		return ESRCH;
    291 	}
    292 	mutex_enter(p->p_lock);
    293 	if (p->p_sflag & PS_RUMP_LWPEXIT) {
    294 		mutex_exit(proc_lock);
    295 		mutex_exit(p->p_lock);
    296 		kmem_free(l, sizeof(*l));
    297 		return EBUSY;
    298 	}
    299 	mutex_exit(proc_lock);
    300 	lwproc_makelwp(p, l, true, false);
    301 
    302 	return 0;
    303 }
    304 
    305 int
    306 rump_lwproc_rfork(int flags)
    307 {
    308 	struct proc *p;
    309 	struct lwp *l;
    310 
    311 	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
    312 	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
    313 		return EINVAL;
    314 
    315 	p = lwproc_newproc(curproc, flags);
    316 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    317 	mutex_enter(p->p_lock);
    318 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    319 	lwproc_makelwp(p, l, true, true);
    320 
    321 	return 0;
    322 }
    323 
    324 /*
    325  * Switch to a new process/thread.  Release previous one if
    326  * deemed to be exiting.  This is considered a slow path for
    327  * rump kernel entry.
    328  */
    329 void
    330 rump_lwproc_switch(struct lwp *newlwp)
    331 {
    332 	struct lwp *l = curlwp;
    333 
    334 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    335 
    336 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    337 		panic("lwp %p (%d:%d) already running",
    338 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    339 
    340 	if (newlwp == NULL) {
    341 		l->l_pflag &= ~LP_RUNNING;
    342 		l->l_flag |= LW_RUMP_CLEAR;
    343 		return;
    344 	}
    345 
    346 	/* fd_free() must be called from curlwp context.  talk about ugh */
    347 	if (l->l_flag & LW_WEXIT) {
    348 		fd_free();
    349 	}
    350 
    351 	rumpuser_set_curlwp(NULL);
    352 
    353 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    354 	newlwp->l_mutex = l->l_mutex;
    355 	newlwp->l_pflag |= LP_RUNNING;
    356 
    357 	rumpuser_set_curlwp(newlwp);
    358 
    359 	/*
    360 	 * Check if the thread should get a signal.  This is
    361 	 * mostly to satisfy the "record" rump sigmodel.
    362 	 */
    363 	mutex_enter(newlwp->l_proc->p_lock);
    364 	if (sigispending(newlwp, 0)) {
    365 		newlwp->l_flag |= LW_PENDSIG;
    366 	}
    367 	mutex_exit(newlwp->l_proc->p_lock);
    368 
    369 	l->l_mutex = &unruntime_lock;
    370 	l->l_pflag &= ~LP_RUNNING;
    371 	l->l_flag &= ~LW_PENDSIG;
    372 	l->l_stat = LSRUN;
    373 
    374 	if (l->l_flag & LW_WEXIT) {
    375 		lwproc_freelwp(l);
    376 	}
    377 }
    378 
    379 void
    380 rump_lwproc_releaselwp(void)
    381 {
    382 	struct proc *p;
    383 	struct lwp *l = curlwp;
    384 
    385 	if (l->l_refcnt == 0 && l->l_flag & LW_WEXIT)
    386 		panic("releasing non-pertinent lwp");
    387 
    388 	p = l->l_proc;
    389 	mutex_enter(p->p_lock);
    390 	KASSERT(l->l_refcnt != 0);
    391 	l->l_refcnt--;
    392 	mutex_exit(p->p_lock);
    393 	l->l_flag |= LW_WEXIT; /* will be released when unscheduled */
    394 }
    395 
    396 struct lwp *
    397 rump_lwproc_curlwp(void)
    398 {
    399 	struct lwp *l = curlwp;
    400 
    401 	if (l->l_flag & LW_WEXIT)
    402 		return NULL;
    403 	return l;
    404 }
    405 
    406 /* this interface is under construction (like the proverbial 90's web page) */
    407 int rump_i_know_what_i_am_doing_with_sysents = 0;
    408 void
    409 rump_lwproc_sysent_usenative()
    410 {
    411 
    412 	if (!rump_i_know_what_i_am_doing_with_sysents)
    413 		panic("don't use rump_lwproc_sysent_usenative()");
    414 	curproc->p_emul = &emul_netbsd;
    415 }
    416