Home | History | Annotate | Line # | Download | only in rumpkern
      1 /*      $NetBSD: lwproc.c,v 1.60 2026/11/06 17:24:26 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.60 2026/11/06 17:24:26 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 	p->p_pgrp = parent->p_pgrp;
    279 	LIST_INSERT_AFTER(parent, p, p_pglist);
    280 	mutex_exit(&proc_lock);
    281 
    282 	return p;
    283 }
    284 
    285 static void
    286 lwproc_freelwp(struct lwp *l)
    287 {
    288 	struct proc *p;
    289 
    290 	p = l->l_proc;
    291 	mutex_enter(p->p_lock);
    292 
    293 	KASSERT(l->l_flag & LW_WEXIT);
    294 	KASSERT(l->l_refcnt == 0);
    295 
    296 	LIST_REMOVE(l, l_sibling);
    297 	KASSERT(p->p_nlwps >= 1);
    298 	if (--p->p_nlwps == 0) {
    299 		KASSERT(p != &proc0);
    300 		p->p_stat = SDEAD;
    301 	} else {
    302 		chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
    303 	}
    304 	cv_broadcast(&p->p_lwpcv); /* nobody sleeps on this in a rump kernel? */
    305 	kauth_cred_free(l->l_cred);
    306 	l->l_stat = LSIDL;
    307 	mutex_exit(p->p_lock);
    308 
    309 	mutex_enter(&proc_lock);
    310 	proc_free_lwpid(p, l->l_lid);
    311 	LIST_REMOVE(l, l_list);
    312 	mutex_exit(&proc_lock);
    313 
    314 	if (l->l_name)
    315 		kmem_free(l->l_name, MAXCOMLEN);
    316 	fstrans_lwp_dtor(l);
    317 	lwp_finispecific(l);
    318 
    319 	lwproc_curlwpop(RUMPUSER_LWP_DESTROY, l);
    320 	kmem_free(l, sizeof(*l));
    321 
    322 	if (p->p_stat == SDEAD)
    323 		lwproc_proc_free(p);
    324 }
    325 
    326 extern kmutex_t unruntime_lock;
    327 
    328 static struct lwp *
    329 lwproc_makelwp(struct proc *p, bool doswitch, bool procmake)
    330 {
    331 	struct lwp *l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    332 
    333 	l->l_refcnt = 1;
    334 	l->l_proc = p;
    335 	l->l_stat = LSIDL;
    336 	l->l_mutex = &unruntime_lock;
    337 
    338 	proc_alloc_lwpid(p, l);
    339 
    340 	mutex_enter(p->p_lock);
    341 	/*
    342 	 * Account the new lwp to the owner of the process.
    343 	 * For some reason, NetBSD doesn't count the first lwp
    344 	 * in a process as a lwp, so skip that.
    345 	 */
    346 	if (p->p_nlwps++) {
    347 		chglwpcnt(kauth_cred_getuid(p->p_cred), 1);
    348 	}
    349 
    350 	KASSERT((p->p_sflag & PS_RUMP_LWPEXIT) == 0);
    351 	LIST_INSERT_HEAD(&p->p_lwps, l, l_sibling);
    352 
    353 	l->l_fd = p->p_fd;
    354 	l->l_cpu = &rump_bootcpu;
    355 	l->l_target_cpu = &rump_bootcpu; /* Initial target CPU always same */
    356 	l->l_stat = LSRUN;
    357 	TAILQ_INIT(&l->l_ld_locks);
    358 	mutex_exit(p->p_lock);
    359 
    360 	l->l_cred = kauth_cred_hold(p->p_cred);
    361 	lwp_initspecific(l);
    362 	PSREF_DEBUG_INIT_LWP(l);
    363 
    364 	lwproc_curlwpop(RUMPUSER_LWP_CREATE, l);
    365 	if (doswitch) {
    366 		rump_lwproc_switch(l);
    367 	}
    368 
    369 	/* filedesc already has refcount 1 when process is created */
    370 	if (!procmake) {
    371 		fd_hold(l);
    372 	}
    373 
    374 	mutex_enter(&proc_lock);
    375 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    376 	mutex_exit(&proc_lock);
    377 
    378 	return l;
    379 }
    380 
    381 struct lwp *
    382 rump__lwproc_alloclwp(struct proc *p)
    383 {
    384 	bool newproc = false;
    385 
    386 	if (p == NULL) {
    387 		p = lwproc_newproc(&proc0, rump_vmspace_local, RUMP_RFCFDG);
    388 		newproc = true;
    389 	}
    390 
    391 	return lwproc_makelwp(p, false, newproc);
    392 }
    393 
    394 int
    395 rump_lwproc_newlwp(pid_t pid)
    396 {
    397 	struct proc *p;
    398 
    399 	mutex_enter(&proc_lock);
    400 	p = proc_find_raw(pid);
    401 	if (p == NULL) {
    402 		mutex_exit(&proc_lock);
    403 		return ESRCH;
    404 	}
    405 	mutex_enter(p->p_lock);
    406 	if (p->p_sflag & PS_RUMP_LWPEXIT) {
    407 		mutex_exit(&proc_lock);
    408 		mutex_exit(p->p_lock);
    409 		return EBUSY;
    410 	}
    411 	mutex_exit(p->p_lock);
    412 	mutex_exit(&proc_lock);
    413 
    414 	/* XXX what holds proc? */
    415 
    416 	lwproc_makelwp(p, true, false);
    417 
    418 	return 0;
    419 }
    420 
    421 int
    422 rump_lwproc_rfork_vmspace(struct vmspace *vm, int flags)
    423 {
    424 	struct proc *p;
    425 
    426 	if (flags & ~(RUMP_RFFDG|RUMP_RFCFDG) ||
    427 	    (~flags & (RUMP_RFFDG|RUMP_RFCFDG)) == 0)
    428 		return EINVAL;
    429 
    430 	p = lwproc_newproc(curproc, vm, flags);
    431 	lwproc_makelwp(p, true, true);
    432 
    433 	return 0;
    434 }
    435 
    436 int
    437 rump_lwproc_rfork(int flags)
    438 {
    439 
    440 	return rump_lwproc_rfork_vmspace(rump_vmspace_local, flags);
    441 }
    442 
    443 /*
    444  * Switch to a new process/thread.  Release previous one if
    445  * deemed to be exiting.  This is considered a slow path for
    446  * rump kernel entry.
    447  */
    448 void
    449 rump_lwproc_switch(struct lwp *newlwp)
    450 {
    451 	struct lwp *l = curlwp;
    452 	int nlocks;
    453 
    454 	KASSERT(!(l->l_flag & LW_WEXIT) || newlwp);
    455 
    456 	if (__predict_false(newlwp && (newlwp->l_pflag & LP_RUNNING)))
    457 		panic("lwp %p (%d:%d) already running",
    458 		    newlwp, newlwp->l_proc->p_pid, newlwp->l_lid);
    459 
    460 	if (newlwp == NULL) {
    461 		l->l_pflag &= ~LP_RUNNING;
    462 		l->l_flag |= LW_RUMP_CLEAR;
    463 		return;
    464 	}
    465 
    466 	/* fd_free() must be called from curlwp context.  talk about ugh */
    467 	if (l->l_flag & LW_WEXIT) {
    468 		fd_free();
    469 	}
    470 
    471 	KERNEL_UNLOCK_ALL(NULL, &nlocks);
    472 	lwproc_curlwpop(RUMPUSER_LWP_CLEAR, l);
    473 
    474 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    475 	newlwp->l_mutex = l->l_mutex;
    476 	newlwp->l_pflag |= LP_RUNNING;
    477 
    478 	lwproc_curlwpop(RUMPUSER_LWP_SET, newlwp);
    479 	curcpu()->ci_curlwp = newlwp;
    480 	KERNEL_LOCK(nlocks, NULL);
    481 
    482 	/*
    483 	 * Check if the thread should get a signal.  This is
    484 	 * mostly to satisfy the "record" rump sigmodel.
    485 	 */
    486 	mutex_enter(newlwp->l_proc->p_lock);
    487 	if (sigispending(newlwp, 0)) {
    488 		newlwp->l_flag |= LW_PENDSIG;
    489 	}
    490 	mutex_exit(newlwp->l_proc->p_lock);
    491 
    492 	l->l_mutex = &unruntime_lock;
    493 	l->l_pflag &= ~LP_RUNNING;
    494 	l->l_flag &= ~LW_PENDSIG;
    495 	l->l_stat = LSRUN;
    496 	l->l_ru.ru_nvcsw++;
    497 
    498 	if (l->l_flag & LW_WEXIT) {
    499 		l->l_stat = LSIDL;
    500 		lwproc_freelwp(l);
    501 	}
    502 }
    503 
    504 /*
    505  * Mark the current thread to be released upon return from
    506  * kernel.
    507  */
    508 void
    509 rump_lwproc_releaselwp(void)
    510 {
    511 	struct lwp *l = curlwp;
    512 
    513 	if (l->l_refcnt == 0 || l->l_flag & LW_WEXIT)
    514 		panic("releasing non-pertinent lwp");
    515 
    516 	rump__lwproc_lwprele();
    517 	KASSERT(l->l_refcnt == 0 && (l->l_flag & LW_WEXIT));
    518 }
    519 
    520 /*
    521  * In-kernel routines used to add and remove references for the
    522  * current thread.  The main purpose is to make it possible for
    523  * implicit threads to persist over scheduling operations in
    524  * rump kernel drivers.  Note that we don't need p_lock in a
    525  * rump kernel, since we do refcounting only for curlwp.
    526  */
    527 void
    528 rump__lwproc_lwphold(void)
    529 {
    530 	struct lwp *l = curlwp;
    531 
    532 	l->l_refcnt++;
    533 	l->l_flag &= ~LW_WEXIT;
    534 }
    535 
    536 void
    537 rump__lwproc_lwprele(void)
    538 {
    539 	struct lwp *l = curlwp;
    540 
    541 	l->l_refcnt--;
    542 	if (l->l_refcnt == 0)
    543 		l->l_flag |= LW_WEXIT;
    544 }
    545 
    546 struct lwp *
    547 rump_lwproc_curlwp(void)
    548 {
    549 	struct lwp *l = curlwp;
    550 
    551 	if (l->l_flag & LW_WEXIT)
    552 		return NULL;
    553 	return l;
    554 }
    555 
    556 /* this interface is under construction (like the proverbial 90's web page) */
    557 int rump_i_know_what_i_am_doing_with_sysents = 0;
    558 void
    559 rump_lwproc_sysent_usenative()
    560 {
    561 
    562 	if (!rump_i_know_what_i_am_doing_with_sysents)
    563 		panic("don't use rump_lwproc_sysent_usenative()");
    564 	curproc->p_emul = &emul_netbsd;
    565 }
    566 
    567 long
    568 lwp_pctr(void)
    569 {
    570 
    571 	return curlwp->l_ru.ru_nvcsw;
    572 }
    573