Home | History | Annotate | Line # | Download | only in rumpkern
lwproc.c revision 1.4
      1 /*      $NetBSD: lwproc.c,v 1.4 2010/11/15 20:37:22 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010 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.4 2010/11/15 20:37:22 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 static void
     48 lwproc_proc_free(struct proc *p)
     49 {
     50 	kauth_cred_t cred;
     51 
     52 	mutex_enter(proc_lock);
     53 
     54 	KASSERT(p->p_nlwps == 0);
     55 	KASSERT(LIST_EMPTY(&p->p_lwps));
     56 	KASSERT(p->p_stat == SIDL || p->p_stat == SDEAD);
     57 
     58 	LIST_REMOVE(p, p_list);
     59 	LIST_REMOVE(p, p_sibling);
     60 	proc_free_pid(p->p_pid); /* decrements nprocs */
     61 	proc_leavepgrp(p); /* releases proc_lock */
     62 
     63 	cred = p->p_cred;
     64 	chgproccnt(kauth_cred_getuid(cred), -1);
     65 	if (rump_proc_vfs_release)
     66 		rump_proc_vfs_release(p);
     67 
     68 	limfree(p->p_limit);
     69 	pstatsfree(p->p_stats);
     70 	kauth_cred_free(p->p_cred);
     71 	proc_finispecific(p);
     72 
     73 	mutex_obj_free(p->p_lock);
     74 	mutex_destroy(&p->p_stmutex);
     75 	mutex_destroy(&p->p_auxlock);
     76 	rw_destroy(&p->p_reflock);
     77 	cv_destroy(&p->p_waitcv);
     78 	cv_destroy(&p->p_lwpcv);
     79 
     80 	proc_free_mem(p);
     81 }
     82 
     83 /*
     84  * Allocate a new process.  Mostly mimic fork by
     85  * copying the properties of the parent.  However, there are some
     86  * differences.  For example, we never share the fd table.
     87  *
     88  * Switch to the new lwp and return a pointer to it.
     89  */
     90 static struct proc *
     91 lwproc_newproc(struct proc *parent)
     92 {
     93 	uid_t uid = kauth_cred_getuid(parent->p_cred);
     94 	struct proc *p;
     95 
     96 	/* maxproc not enforced */
     97 	atomic_inc_uint(&nprocs);
     98 
     99 	/* allocate process */
    100 	p = proc_alloc();
    101 	memset(&p->p_startzero, 0,
    102 	    offsetof(struct proc, p_endzero)
    103 	      - offsetof(struct proc, p_startzero));
    104 	memcpy(&p->p_startcopy, &parent->p_startcopy,
    105 	    offsetof(struct proc, p_endcopy)
    106 	      - offsetof(struct proc, p_startcopy));
    107 
    108 	p->p_stats = pstatscopy(parent->p_stats);
    109 
    110 	/* not based on parent */
    111 	p->p_vmspace = &vmspace0;
    112 	p->p_emul = &emul_netbsd;
    113 	p->p_fd = fd_init(NULL);
    114 	lim_addref(parent->p_limit);
    115 	p->p_limit = parent->p_limit;
    116 
    117 	LIST_INIT(&p->p_lwps);
    118 	LIST_INIT(&p->p_children);
    119 
    120 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    121 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_NONE);
    122 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    123 	rw_init(&p->p_reflock);
    124 	cv_init(&p->p_waitcv, "pwait");
    125 	cv_init(&p->p_lwpcv, "plwp");
    126 
    127 	p->p_pptr = parent;
    128 	p->p_ppid = parent->p_pid;
    129 
    130 	kauth_proc_fork(parent, p);
    131 
    132 	/* initialize cwd in rump kernels with vfs */
    133 	if (rump_proc_vfs_init)
    134 		rump_proc_vfs_init(p);
    135 
    136 	chgproccnt(uid, 1); /* not enforced */
    137 
    138 	/* publish proc various proc lists */
    139 	mutex_enter(proc_lock);
    140 	LIST_INSERT_HEAD(&allproc, p, p_list);
    141 	LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
    142 	LIST_INSERT_AFTER(parent, p, p_pglist);
    143 	mutex_exit(proc_lock);
    144 
    145 	return p;
    146 }
    147 
    148 static void
    149 lwproc_freelwp(struct lwp *l)
    150 {
    151 	struct proc *p;
    152 	bool freeproc;
    153 
    154 	p = l->l_proc;
    155 	mutex_enter(p->p_lock);
    156 
    157 	/* XXX: l_refcnt */
    158 	KASSERT(l->l_flag & LW_WEXIT);
    159 	KASSERT(l->l_refcnt == 0);
    160 
    161 	/* ok, zero references, continue with nuke */
    162 	LIST_REMOVE(l, l_sibling);
    163 	KASSERT(p->p_nlwps >= 1);
    164 	if (--p->p_nlwps == 0) {
    165 		KASSERT(p != &proc0);
    166 		p->p_stat = SDEAD;
    167 	}
    168 	freeproc = p->p_nlwps == 0;
    169 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in rump? */
    170 	kauth_cred_free(l->l_cred);
    171 	mutex_exit(p->p_lock);
    172 
    173 	mutex_enter(proc_lock);
    174 	LIST_REMOVE(l, l_list);
    175 	mutex_exit(proc_lock);
    176 
    177 	if (l->l_name)
    178 		kmem_free(l->l_name, MAXCOMLEN);
    179 	lwp_finispecific(l);
    180 
    181 	kmem_free(l, sizeof(*l));
    182 
    183 	if (p->p_stat == SDEAD)
    184 		lwproc_proc_free(p);
    185 }
    186 
    187 /*
    188  * called with p_lock held, releases lock before return
    189  */
    190 static void
    191 lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
    192 {
    193 
    194 	p->p_nlwps++;
    195 	l->l_refcnt = 1;
    196 	l->l_proc = p;
    197 
    198 	l->l_lid = p->p_nlwpid++;
    199 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    200 	mutex_exit(p->p_lock);
    201 
    202 	lwp_update_creds(l);
    203 
    204 	l->l_fd = p->p_fd;
    205 	l->l_cpu = NULL;
    206 	l->l_target_cpu = rump_cpu; /* Initial target CPU always the same */
    207 
    208 	lwp_initspecific(l);
    209 
    210 	if (doswitch) {
    211 		rump_lwproc_switch(l);
    212 	}
    213 
    214 	/* filedesc already has refcount 1 when process is created */
    215 	if (!procmake) {
    216 		fd_hold(l);
    217 	}
    218 
    219 	mutex_enter(proc_lock);
    220 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    221 	mutex_exit(proc_lock);
    222 }
    223 
    224 struct lwp *
    225 rump__lwproc_alloclwp(struct proc *p)
    226 {
    227 	struct lwp *l;
    228 	bool newproc = false;
    229 
    230 	if (p == NULL) {
    231 		p = lwproc_newproc(&proc0);
    232 		newproc = true;
    233 	}
    234 
    235 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    236 
    237 	mutex_enter(p->p_lock);
    238 	lwproc_makelwp(p, l, false, newproc);
    239 
    240 	return l;
    241 }
    242 
    243 int
    244 rump_lwproc_newlwp(pid_t pid)
    245 {
    246 	struct proc *p;
    247 	struct lwp *l;
    248 
    249 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    250 	mutex_enter(proc_lock);
    251 	p = proc_find_raw(pid);
    252 	if (p == NULL) {
    253 		mutex_exit(proc_lock);
    254 		kmem_free(l, sizeof(*l));
    255 		return ESRCH;
    256 	}
    257 	mutex_enter(p->p_lock);
    258 	mutex_exit(proc_lock);
    259 	lwproc_makelwp(p, l, true, false);
    260 
    261 	return 0;
    262 }
    263 
    264 int
    265 rump_lwproc_newproc(void)
    266 {
    267 	struct proc *p;
    268 	struct lwp *l;
    269 
    270 	p = lwproc_newproc(curproc);
    271 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    272 	mutex_enter(p->p_lock);
    273 	lwproc_makelwp(p, l, true, true);
    274 
    275 	return 0;
    276 }
    277 
    278 /*
    279  * Switch to a new process/thread.  Release previous one if
    280  * deemed to be exiting.  This is considered a slow path for
    281  * rump kernel entry.
    282  */
    283 void
    284 rump_lwproc_switch(struct lwp *newlwp)
    285 {
    286 	struct lwp *l = curlwp;
    287 
    288 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    289 
    290 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    291 		panic("lwp %p (%d:%d) already running",
    292 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    293 
    294 	if (newlwp == NULL) {
    295 		l->l_pflag &= ~LP_RUNNING;
    296 		l->l_flag |= LW_RUMP_CLEAR;
    297 		return;
    298 	}
    299 
    300 	/* fd_free() must be called from curlwp context.  talk about ugh */
    301 	if (l->l_flag & LW_WEXIT) {
    302 		fd_free();
    303 	}
    304 
    305 	rumpuser_set_curlwp(NULL);
    306 
    307 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    308 	newlwp->l_mutex = l->l_mutex;
    309 	newlwp->l_pflag |= LP_RUNNING;
    310 
    311 	rumpuser_set_curlwp(newlwp);
    312 
    313 	/*
    314 	 * Check if the thread should get a signal.  This is
    315 	 * mostly to satisfy the "record" rump sigmodel.
    316 	 */
    317 	mutex_enter(newlwp->l_proc->p_lock);
    318 	if (sigispending(newlwp, 0)) {
    319 		newlwp->l_flag |= LW_PENDSIG;
    320 	}
    321 	mutex_exit(newlwp->l_proc->p_lock);
    322 
    323 	l->l_mutex = NULL;
    324 	l->l_cpu = NULL;
    325 	l->l_pflag &= ~LP_RUNNING;
    326 	l->l_flag &= ~LW_PENDSIG;
    327 
    328 	if (l->l_flag & LW_WEXIT) {
    329 		lwproc_freelwp(l);
    330 	}
    331 }
    332 
    333 void
    334 rump_lwproc_releaselwp(void)
    335 {
    336 	struct proc *p;
    337 	struct lwp *l = curlwp;
    338 
    339 	if (l->l_refcnt == 0 && l->l_flag & LW_WEXIT)
    340 		panic("releasing non-pertinent lwp");
    341 
    342 	p = l->l_proc;
    343 	mutex_enter(p->p_lock);
    344 	KASSERT(l->l_refcnt != 0);
    345 	l->l_refcnt--;
    346 	mutex_exit(p->p_lock);
    347 	l->l_flag |= LW_WEXIT; /* will be released when unscheduled */
    348 }
    349 
    350 struct lwp *
    351 rump_lwproc_curlwp(void)
    352 {
    353 	struct lwp *l = curlwp;
    354 
    355 	if (l->l_flag & LW_WEXIT)
    356 		return NULL;
    357 	return l;
    358 }
    359