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