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