Home | History | Annotate | Line # | Download | only in rumpkern
lwproc.c revision 1.46
      1 /*      $NetBSD: lwproc.c,v 1.46 2020/04/24 03:56:12 thorpej 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 #define RUMP__CURLWP_PRIVATE
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: lwproc.c,v 1.46 2020/04/24 03:56:12 thorpej Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/atomic.h>
     35 #include <sys/filedesc.h>
     36 #include <sys/fstrans.h>
     37 #include <sys/kauth.h>
     38 #include <sys/kmem.h>
     39 #include <sys/lwp.h>
     40 #include <sys/ktrace.h>
     41 #include <sys/pool.h>
     42 #include <sys/proc.h>
     43 #include <sys/queue.h>
     44 #include <sys/resourcevar.h>
     45 #include <sys/uidinfo.h>
     46 #include <sys/psref.h>
     47 
     48 #include <rump-sys/kern.h>
     49 
     50 #include <rump/rumpuser.h>
     51 
     52 #include "rump_curlwp.h"
     53 
     54 struct lwp lwp0 = {
     55 	.l_lid = 1,
     56 	.l_proc = &proc0,
     57 	.l_fd = &filedesc0,
     58 };
     59 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
     60 
     61 u_int nprocs = 1;
     62 
     63 struct emul *emul_default = &emul_netbsd;
     64 
     65 void
     66 lwp_unsleep(lwp_t *l, bool cleanup)
     67 {
     68 
     69 	KASSERT(mutex_owned(l->l_mutex));
     70 
     71 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
     72 }
     73 
     74 /*
     75  * Look up a live LWP within the specified process.
     76  *
     77  * Must be called with p->p_lock held.
     78  */
     79 struct lwp *
     80 lwp_find(struct proc *p, lwpid_t id)
     81 {
     82 	struct lwp *l;
     83 
     84 	KASSERT(mutex_owned(p->p_lock));
     85 
     86 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
     87 		if (l->l_lid == id)
     88 			break;
     89 	}
     90 
     91 	/*
     92 	 * No need to lock - all of these conditions will
     93 	 * be visible with the process level mutex held.
     94 	 */
     95 	if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
     96 		l = NULL;
     97 
     98 	return l;
     99 }
    100 
    101 void
    102 lwp_update_creds(struct lwp *l)
    103 {
    104 	struct proc *p;
    105 	kauth_cred_t oldcred;
    106 
    107 	p = l->l_proc;
    108 	oldcred = l->l_cred;
    109 	l->l_prflag &= ~LPR_CRMOD;
    110 
    111 	mutex_enter(p->p_lock);
    112 	kauth_cred_hold(p->p_cred);
    113 	l->l_cred = p->p_cred;
    114 	mutex_exit(p->p_lock);
    115 
    116 	if (oldcred != NULL)
    117 		kauth_cred_free(oldcred);
    118 }
    119 
    120 void
    121 rump_lwproc_init(void)
    122 {
    123 
    124 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, &lwp0);
    125 }
    126 
    127 struct lwp *
    128 rump_lwproc_curlwp_hypercall(void)
    129 {
    130 
    131 	return rumpuser_curlwp();
    132 }
    133 
    134 void
    135 rump_lwproc_curlwp_set(struct lwp *l)
    136 {
    137 
    138 	KASSERT(curlwp == NULL);
    139 	lwproc_curlwpop(RUMPUSER_LWP_SET, l);
    140 }
    141 
    142 void
    143 rump_lwproc_curlwp_clear(struct lwp *l)
    144 {
    145 
    146 	KASSERT(l == curlwp);
    147 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
    148 }
    149 
    150 static void
    151 lwproc_proc_free(struct proc *p)
    152 {
    153 	kauth_cred_t cred;
    154 	struct proc *child;
    155 
    156 	KASSERT(p->p_stat == SDYING || p->p_stat == SDEAD);
    157 
    158 #ifdef KTRACE
    159 	if (p->p_tracep) {
    160 		mutex_enter(&ktrace_lock);
    161 		ktrderef(p);
    162 		mutex_exit(&ktrace_lock);
    163 	}
    164 #endif
    165 
    166 	mutex_enter(proc_lock);
    167 
    168 	/* childranee eunt initus */
    169 	while ((child = LIST_FIRST(&p->p_children)) != NULL) {
    170 		LIST_REMOVE(child, p_sibling);
    171 		child->p_pptr = initproc;
    172 		child->p_ppid = 1;
    173 		LIST_INSERT_HEAD(&initproc->p_children, child, p_sibling);
    174 	}
    175 
    176 	KASSERT(p->p_nlwps == 0);
    177 	KASSERT(LIST_EMPTY(&p->p_lwps));
    178 
    179 	LIST_REMOVE(p, p_list);
    180 	LIST_REMOVE(p, p_sibling);
    181 	proc_free_pid(p->p_pid);
    182 	atomic_dec_uint(&nprocs);
    183 	proc_leavepgrp(p); /* releases proc_lock */
    184 
    185 	cred = p->p_cred;
    186 	chgproccnt(kauth_cred_getuid(cred), -1);
    187 	rump_proc_vfs_release(p);
    188 
    189 	doexithooks(p);
    190 	lim_free(p->p_limit);
    191 	pstatsfree(p->p_stats);
    192 	kauth_cred_free(p->p_cred);
    193 	proc_finispecific(p);
    194 
    195 	mutex_obj_free(p->p_lock);
    196 	mutex_destroy(&p->p_stmutex);
    197 	mutex_destroy(&p->p_auxlock);
    198 	rw_destroy(&p->p_reflock);
    199 	cv_destroy(&p->p_waitcv);
    200 	cv_destroy(&p->p_lwpcv);
    201 
    202 	/* non-local vmspaces are not shared */
    203 	if (!RUMP_LOCALPROC_P(p)) {
    204 		struct rump_spctl *ctl = (struct rump_spctl *)p->p_vmspace;
    205 		KASSERT(p->p_vmspace->vm_refcnt == 1);
    206 		kmem_free(ctl, sizeof(*ctl));
    207 	}
    208 
    209 	proc_free_mem(p);
    210 }
    211 
    212 /*
    213  * Allocate a new process.  Mostly mimic fork by
    214  * copying the properties of the parent.  However, there are some
    215  * differences.
    216  *
    217  * Switch to the new lwp and return a pointer to it.
    218  */
    219 static struct proc *
    220 lwproc_newproc(struct proc *parent, struct vmspace *vm, int flags)
    221 {
    222 	uid_t uid = kauth_cred_getuid(parent->p_cred);
    223 	struct proc *p;
    224 
    225 	/* maxproc not enforced */
    226 	atomic_inc_uint(&nprocs);
    227 
    228 	/* allocate process */
    229 	p = proc_alloc();
    230 	memset(&p->p_startzero, 0,
    231 	    offsetof(struct proc, p_endzero)
    232 	      - offsetof(struct proc, p_startzero));
    233 	memcpy(&p->p_startcopy, &parent->p_startcopy,
    234 	    offsetof(struct proc, p_endcopy)
    235 	      - offsetof(struct proc, p_startcopy));
    236 
    237 	/* some other garbage we need to zero */
    238 	p->p_sigacts = NULL;
    239 	p->p_aio = NULL;
    240 	p->p_dtrace = NULL;
    241 	p->p_mqueue_cnt = p->p_exitsig = 0;
    242 	p->p_flag = p->p_sflag = p->p_slflag = p->p_lflag = p->p_stflag = 0;
    243 	p->p_trace_enabled = 0;
    244 	p->p_xsig = p->p_xexit = p->p_acflag = 0;
    245 	p->p_stackbase = 0;
    246 
    247 	p->p_stats = pstatscopy(parent->p_stats);
    248 
    249 	p->p_vmspace = vm;
    250 	p->p_emul = emul_default;
    251 #ifdef __HAVE_SYSCALL_INTERN
    252 	p->p_emul->e_syscall_intern(p);
    253 #endif
    254 	if (*parent->p_comm)
    255 		strcpy(p->p_comm, parent->p_comm);
    256 	else
    257 		strcpy(p->p_comm, "rumproc");
    258 
    259 	if ((flags & RUMP_RFCFDG) == 0)
    260 		KASSERT(parent == curproc);
    261 	if (flags & RUMP_RFFDG)
    262 		p->p_fd = fd_copy();
    263 	else if (flags & RUMP_RFCFDG)
    264 		p->p_fd = fd_init(NULL);
    265 	else
    266 		fd_share(p);
    267 
    268 	lim_addref(parent->p_limit);
    269 	p->p_limit = parent->p_limit;
    270 
    271 	LIST_INIT(&p->p_lwps);
    272 	LIST_INIT(&p->p_children);
    273 
    274 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    275 	mutex_init(&p->p_stmutex, MUTEX_DEFAULT, IPL_HIGH);
    276 	mutex_init(&p->p_auxlock, MUTEX_DEFAULT, IPL_NONE);
    277 	rw_init(&p->p_reflock);
    278 	cv_init(&p->p_waitcv, "pwait");
    279 	cv_init(&p->p_lwpcv, "plwp");
    280 
    281 	p->p_pptr = parent;
    282 	p->p_ppid = parent->p_pid;
    283 	p->p_stat = SACTIVE;
    284 
    285 	kauth_proc_fork(parent, p);
    286 
    287 	/* initialize cwd in rump kernels with vfs */
    288 	rump_proc_vfs_init(p);
    289 
    290 	chgproccnt(uid, 1); /* not enforced */
    291 
    292 	/* publish proc various proc lists */
    293 	mutex_enter(proc_lock);
    294 	LIST_INSERT_HEAD(&allproc, p, p_list);
    295 	LIST_INSERT_HEAD(&parent->p_children, p, p_sibling);
    296 	LIST_INSERT_AFTER(parent, p, p_pglist);
    297 	mutex_exit(proc_lock);
    298 
    299 	return p;
    300 }
    301 
    302 static void
    303 lwproc_freelwp(struct lwp *l)
    304 {
    305 	struct proc *p;
    306 
    307 	p = l->l_proc;
    308 	mutex_enter(p->p_lock);
    309 
    310 	KASSERT(l->l_flag & LW_WEXIT);
    311 	KASSERT(l->l_refcnt == 0);
    312 
    313 	/* ok, zero references, continue with nuke */
    314 	proc_free_lwpid(p, l->l_lid);
    315 	LIST_REMOVE(l, l_sibling);
    316 	KASSERT(p->p_nlwps >= 1);
    317 	if (--p->p_nlwps == 0) {
    318 		KASSERT(p != &proc0);
    319 		p->p_stat = SDEAD;
    320 	} else {
    321 		chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
    322 	}
    323 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */
    324 	kauth_cred_free(l->l_cred);
    325 	mutex_exit(p->p_lock);
    326 
    327 	mutex_enter(proc_lock);
    328 	LIST_REMOVE(l, l_list);
    329 	mutex_exit(proc_lock);
    330 
    331 	if (l->l_name)
    332 		kmem_free(l->l_name, MAXCOMLEN);
    333 	fstrans_lwp_dtor(l);
    334 	lwp_finispecific(l);
    335 
    336 	lwproc_curlwpop(RUMPUSER_LWP_DESTROY, l);
    337 	membar_exit();
    338 	kmem_free(l, sizeof(*l));
    339 
    340 	if (p->p_stat == SDEAD)
    341 		lwproc_proc_free(p);
    342 }
    343 
    344 extern kmutex_t unruntime_lock;
    345 
    346 /*
    347  * called with p_lock held, releases lock before return
    348  */
    349 static void
    350 lwproc_makelwp(struct proc *p, struct lwp *l, bool doswitch, bool procmake)
    351 {
    352 
    353 	/*
    354 	 * Account the new lwp to the owner of the process.
    355 	 * For some reason, NetBSD doesn't count the first lwp
    356 	 * in a process as a lwp, so skip that.
    357 	 */
    358 	if (p->p_nlwps++) {
    359 		chglwpcnt(kauth_cred_getuid(p->p_cred), 1);
    360 	}
    361 
    362 	l->l_refcnt = 1;
    363 	l->l_proc = p;
    364 
    365 	proc_alloc_lwpid(p, l);
    366 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    367 
    368 	l->l_fd = p->p_fd;
    369 	l->l_cpu = &rump_bootcpu;
    370 	l->l_target_cpu = &rump_bootcpu; /* Initial target CPU always same */
    371 	l->l_stat = LSRUN;
    372 	l->l_mutex = &unruntime_lock;
    373 	TAILQ_INIT(&l->l_ld_locks);
    374 	mutex_exit(p->p_lock);
    375 
    376 	lwp_update_creds(l);
    377 	lwp_initspecific(l);
    378 	PSREF_DEBUG_INIT_LWP(l);
    379 
    380 	membar_enter();
    381 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
    382 	if (doswitch) {
    383 		rump_lwproc_switch(l);
    384 	}
    385 
    386 	/* filedesc already has refcount 1 when process is created */
    387 	if (!procmake) {
    388 		fd_hold(l);
    389 	}
    390 
    391 	mutex_enter(proc_lock);
    392 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    393 	mutex_exit(proc_lock);
    394 }
    395 
    396 struct lwp *
    397 rump__lwproc_alloclwp(struct proc *p)
    398 {
    399 	struct lwp *l;
    400 	bool newproc = false;
    401 
    402 	if (p == NULL) {
    403 		p = lwproc_newproc(&proc0, rump_vmspace_local, RUMP_RFCFDG);
    404 		newproc = true;
    405 	}
    406 
    407 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    408 
    409 	mutex_enter(p->p_lock);
    410 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    411 	lwproc_makelwp(p, l, false, newproc);
    412 
    413 	return l;
    414 }
    415 
    416 int
    417 rump_lwproc_newlwp(pid_t pid)
    418 {
    419 	struct proc *p;
    420 	struct lwp *l;
    421 
    422 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    423 	mutex_enter(proc_lock);
    424 	p = proc_find_raw(pid);
    425 	if (p == NULL) {
    426 		mutex_exit(proc_lock);
    427 		kmem_free(l, sizeof(*l));
    428 		return ESRCH;
    429 	}
    430 	mutex_enter(p->p_lock);
    431 	if (p->p_sflag & PS_RUMP_LWPEXIT) {
    432 		mutex_exit(proc_lock);
    433 		mutex_exit(p->p_lock);
    434 		kmem_free(l, sizeof(*l));
    435 		return EBUSY;
    436 	}
    437 	mutex_exit(proc_lock);
    438 	lwproc_makelwp(p, l, true, false);
    439 
    440 	return 0;
    441 }
    442 
    443 int
    444 rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
    445 {
    446 	struct proc *p;
    447 	struct lwp *l;
    448 
    449 	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
    450 	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
    451 		return EINVAL;
    452 
    453 	p = lwproc_newproc(curproc, vm, flags);
    454 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    455 	mutex_enter(p->p_lock);
    456 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    457 	lwproc_makelwp(p, l, true, true);
    458 
    459 	return 0;
    460 }
    461 
    462 int
    463 rump_lwproc_rfork(int flags)
    464 {
    465 
    466 	return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
    467 }
    468 
    469 /*
    470  * Switch to a new process/thread.  Release previous one if
    471  * deemed to be exiting.  This is considered a slow path for
    472  * rump kernel entry.
    473  */
    474 void
    475 rump_lwproc_switch(struct lwp *newlwp)
    476 {
    477 	struct lwp *l = curlwp;
    478 
    479 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    480 
    481 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    482 		panic("lwp %p (%d:%d) already running",
    483 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    484 
    485 	if (newlwp == NULL) {
    486 		l->l_pflag &= ~LP_RUNNING;
    487 		l->l_flag |= LW_RUMP_CLEAR;
    488 		return;
    489 	}
    490 
    491 	/* fd_free() must be called from curlwp context.  talk about ugh */
    492 	if (l->l_flag & LW_WEXIT) {
    493 		fd_free();
    494 	}
    495 
    496 	KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks);
    497 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
    498 
    499 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    500 	newlwp->l_mutex = l->l_mutex;
    501 	newlwp->l_pflag |= LP_RUNNING;
    502 
    503 	lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
    504 	curcpu()->ci_curlwp = newlwp;
    505 	KERNEL_LOCK(newlwp->l_biglocks, NULL);
    506 
    507 	/*
    508 	 * Check if the thread should get a signal.  This is
    509 	 * mostly to satisfy the "record" rump sigmodel.
    510 	 */
    511 	mutex_enter(newlwp->l_proc->p_lock);
    512 	if (sigispending(newlwp, 0)) {
    513 		newlwp->l_flag |= LW_PENDSIG;
    514 	}
    515 	mutex_exit(newlwp->l_proc->p_lock);
    516 
    517 	l->l_mutex = &unruntime_lock;
    518 	l->l_pflag &= ~LP_RUNNING;
    519 	l->l_flag &= ~LW_PENDSIG;
    520 	l->l_stat = LSRUN;
    521 
    522 	if (l->l_flag & LW_WEXIT) {
    523 		lwproc_freelwp(l);
    524 	}
    525 }
    526 
    527 /*
    528  * Mark the current thread to be released upon return from
    529  * kernel.
    530  */
    531 void
    532 rump_lwproc_releaselwp(void)
    533 {
    534 	struct lwp *l = curlwp;
    535 
    536 	if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
    537 		panic("releasing non-pertinent lwp");
    538 
    539 	rump__lwproc_lwprele();
    540 	KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
    541 }
    542 
    543 /*
    544  * In-kernel routines used to add and remove references for the
    545  * current thread.  The main purpose is to make it possible for
    546  * implicit threads to persist over scheduling operations in
    547  * rump kernel drivers.  Note that we don't need p_lock in a
    548  * rump kernel, since we do refcounting only for curlwp.
    549  */
    550 void
    551 rump__lwproc_lwphold(void)
    552 {
    553 	struct lwp *l = curlwp;
    554 
    555 	l->l_refcnt++;
    556 	l->l_flag &= ~LW_WEXIT;
    557 }
    558 
    559 void
    560 rump__lwproc_lwprele(void)
    561 {
    562 	struct lwp *l = curlwp;
    563 
    564 	l->l_refcnt--;
    565 	if (l->l_refcnt == 0)
    566 		l->l_flag |= LW_WEXIT;
    567 }
    568 
    569 struct lwp *
    570 rump_lwproc_curlwp(void)
    571 {
    572 	struct lwp *l = curlwp;
    573 
    574 	if (l->l_flag & LW_WEXIT)
    575 		return NULL;
    576 	return l;
    577 }
    578 
    579 /* this interface is under construction (like the proverbial 90's web page) */
    580 int rump_i_know_what_i_am_doing_with_sysents = 0;
    581 void
    582 rump_lwproc_sysent_usenative()
    583 {
    584 
    585 	if (!rump_i_know_what_i_am_doing_with_sysents)
    586 		panic("don't use rump_lwproc_sysent_usenative()");
    587 	curproc->p_emul = &emul_netbsd;
    588 }
    589