Home | History | Annotate | Line # | Download | only in rumpkern
lwproc.c revision 1.50
      1 /*      $NetBSD: lwproc.c,v 1.50 2020/05/23 23:42:44 ad 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.50 2020/05/23 23:42:44 ad 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 = 0,
     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 	LIST_REMOVE(l, l_sibling);
    314 	KASSERT(p->p_nlwps >= 1);
    315 	if (--p->p_nlwps == 0) {
    316 		KASSERT(p != &proc0);
    317 		p->p_stat = SDEAD;
    318 	} else {
    319 		chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
    320 	}
    321 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */
    322 	kauth_cred_free(l->l_cred);
    323 	l->l_stat = LSIDL;
    324 	mutex_exit(p->p_lock);
    325 
    326 	mutex_enter(&proc_lock);
    327 	proc_free_lwpid(p, l->l_lid);
    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 	l->l_stat = LSIDL;
    365 	l->l_mutex = &unruntime_lock;
    366 
    367 	proc_alloc_lwpid(p, l);
    368 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    369 
    370 	l->l_fd = p->p_fd;
    371 	l->l_cpu = &rump_bootcpu;
    372 	l->l_target_cpu = &rump_bootcpu; /* Initial target CPU always same */
    373 	l->l_stat = LSRUN;
    374 	TAILQ_INIT(&l->l_ld_locks);
    375 	mutex_exit(p->p_lock);
    376 
    377 	lwp_update_creds(l);
    378 	lwp_initspecific(l);
    379 	PSREF_DEBUG_INIT_LWP(l);
    380 
    381 	membar_enter();
    382 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
    383 	if (doswitch) {
    384 		rump_lwproc_switch(l);
    385 	}
    386 
    387 	/* filedesc already has refcount 1 when process is created */
    388 	if (!procmake) {
    389 		fd_hold(l);
    390 	}
    391 
    392 	mutex_enter(&proc_lock);
    393 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    394 	mutex_exit(&proc_lock);
    395 }
    396 
    397 struct lwp *
    398 rump__lwproc_alloclwp(struct proc *p)
    399 {
    400 	struct lwp *l;
    401 	bool newproc = false;
    402 
    403 	if (p == NULL) {
    404 		p = lwproc_newproc(&proc0, rump_vmspace_local, RUMP_RFCFDG);
    405 		newproc = true;
    406 	}
    407 
    408 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    409 
    410 	mutex_enter(p->p_lock);
    411 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    412 	lwproc_makelwp(p, l, false, newproc);
    413 
    414 	return l;
    415 }
    416 
    417 int
    418 rump_lwproc_newlwp(pid_t pid)
    419 {
    420 	struct proc *p;
    421 	struct lwp *l;
    422 
    423 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    424 	mutex_enter(&proc_lock);
    425 	p = proc_find_raw(pid);
    426 	if (p == NULL) {
    427 		mutex_exit(&proc_lock);
    428 		kmem_free(l, sizeof(*l));
    429 		return ESRCH;
    430 	}
    431 	mutex_enter(p->p_lock);
    432 	if (p->p_sflag & PS_RUMP_LWPEXIT) {
    433 		mutex_exit(&proc_lock);
    434 		mutex_exit(p->p_lock);
    435 		kmem_free(l, sizeof(*l));
    436 		return EBUSY;
    437 	}
    438 	mutex_exit(&proc_lock);
    439 	lwproc_makelwp(p, l, true, false);
    440 
    441 	return 0;
    442 }
    443 
    444 int
    445 rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
    446 {
    447 	struct proc *p;
    448 	struct lwp *l;
    449 
    450 	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
    451 	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
    452 		return EINVAL;
    453 
    454 	p = lwproc_newproc(curproc, vm, flags);
    455 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    456 	mutex_enter(p->p_lock);
    457 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    458 	lwproc_makelwp(p, l, true, true);
    459 
    460 	return 0;
    461 }
    462 
    463 int
    464 rump_lwproc_rfork(int flags)
    465 {
    466 
    467 	return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
    468 }
    469 
    470 /*
    471  * Switch to a new process/thread.  Release previous one if
    472  * deemed to be exiting.  This is considered a slow path for
    473  * rump kernel entry.
    474  */
    475 void
    476 rump_lwproc_switch(struct lwp *newlwp)
    477 {
    478 	struct lwp *l = curlwp;
    479 
    480 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    481 
    482 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    483 		panic("lwp %p (%d:%d) already running",
    484 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    485 
    486 	if (newlwp == NULL) {
    487 		l->l_pflag &= ~LP_RUNNING;
    488 		l->l_flag |= LW_RUMP_CLEAR;
    489 		return;
    490 	}
    491 
    492 	/* fd_free() must be called from curlwp context.  talk about ugh */
    493 	if (l->l_flag & LW_WEXIT) {
    494 		fd_free();
    495 	}
    496 
    497 	KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks);
    498 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
    499 
    500 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    501 	newlwp->l_mutex = l->l_mutex;
    502 	newlwp->l_pflag |= LP_RUNNING;
    503 
    504 	lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
    505 	curcpu()->ci_curlwp = newlwp;
    506 	KERNEL_LOCK(newlwp->l_biglocks, NULL);
    507 
    508 	/*
    509 	 * Check if the thread should get a signal.  This is
    510 	 * mostly to satisfy the "record" rump sigmodel.
    511 	 */
    512 	mutex_enter(newlwp->l_proc->p_lock);
    513 	if (sigispending(newlwp, 0)) {
    514 		newlwp->l_flag |= LW_PENDSIG;
    515 	}
    516 	mutex_exit(newlwp->l_proc->p_lock);
    517 
    518 	l->l_mutex = &unruntime_lock;
    519 	l->l_pflag &= ~LP_RUNNING;
    520 	l->l_flag &= ~LW_PENDSIG;
    521 	l->l_stat = LSRUN;
    522 
    523 	if (l->l_flag & LW_WEXIT) {
    524 		l->l_stat = LSIDL;
    525 		lwproc_freelwp(l);
    526 	}
    527 }
    528 
    529 /*
    530  * Mark the current thread to be released upon return from
    531  * kernel.
    532  */
    533 void
    534 rump_lwproc_releaselwp(void)
    535 {
    536 	struct lwp *l = curlwp;
    537 
    538 	if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
    539 		panic("releasing non-pertinent lwp");
    540 
    541 	rump__lwproc_lwprele();
    542 	KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
    543 }
    544 
    545 /*
    546  * In-kernel routines used to add and remove references for the
    547  * current thread.  The main purpose is to make it possible for
    548  * implicit threads to persist over scheduling operations in
    549  * rump kernel drivers.  Note that we don't need p_lock in a
    550  * rump kernel, since we do refcounting only for curlwp.
    551  */
    552 void
    553 rump__lwproc_lwphold(void)
    554 {
    555 	struct lwp *l = curlwp;
    556 
    557 	l->l_refcnt++;
    558 	l->l_flag &= ~LW_WEXIT;
    559 }
    560 
    561 void
    562 rump__lwproc_lwprele(void)
    563 {
    564 	struct lwp *l = curlwp;
    565 
    566 	l->l_refcnt--;
    567 	if (l->l_refcnt == 0)
    568 		l->l_flag |= LW_WEXIT;
    569 }
    570 
    571 struct lwp *
    572 rump_lwproc_curlwp(void)
    573 {
    574 	struct lwp *l = curlwp;
    575 
    576 	if (l->l_flag & LW_WEXIT)
    577 		return NULL;
    578 	return l;
    579 }
    580 
    581 /* this interface is under construction (like the proverbial 90's web page) */
    582 int rump_i_know_what_i_am_doing_with_sysents = 0;
    583 void
    584 rump_lwproc_sysent_usenative()
    585 {
    586 
    587 	if (!rump_i_know_what_i_am_doing_with_sysents)
    588 		panic("don't use rump_lwproc_sysent_usenative()");
    589 	curproc->p_emul = &emul_netbsd;
    590 }
    591