Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.240
      1 /*	$NetBSD: rump.c,v 1.240 2012/02/04 10:02:25 njoly Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007-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 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.240 2012/02/04 10:02:25 njoly Exp $");
     30 
     31 #include <sys/systm.h>
     32 #define ELFSIZE ARCH_ELFSIZE
     33 
     34 #include <sys/param.h>
     35 #include <sys/atomic.h>
     36 #include <sys/buf.h>
     37 #include <sys/callout.h>
     38 #include <sys/conf.h>
     39 #include <sys/cpu.h>
     40 #include <sys/device.h>
     41 #include <sys/evcnt.h>
     42 #include <sys/event.h>
     43 #include <sys/exec_elf.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/iostat.h>
     46 #include <sys/kauth.h>
     47 #include <sys/kernel.h>
     48 #include <sys/kmem.h>
     49 #include <sys/kprintf.h>
     50 #include <sys/kthread.h>
     51 #include <sys/ksyms.h>
     52 #include <sys/msgbuf.h>
     53 #include <sys/module.h>
     54 #include <sys/namei.h>
     55 #include <sys/once.h>
     56 #include <sys/percpu.h>
     57 #include <sys/pipe.h>
     58 #include <sys/pool.h>
     59 #include <sys/queue.h>
     60 #include <sys/reboot.h>
     61 #include <sys/resourcevar.h>
     62 #include <sys/select.h>
     63 #include <sys/sysctl.h>
     64 #include <sys/syscall.h>
     65 #include <sys/syscallvar.h>
     66 #include <sys/timetc.h>
     67 #include <sys/tty.h>
     68 #include <sys/uidinfo.h>
     69 #include <sys/vmem.h>
     70 #include <sys/xcall.h>
     71 #include <sys/simplelock.h>
     72 #include <sys/cprng.h>
     73 
     74 #include <rump/rumpuser.h>
     75 
     76 #include <secmodel/suser/suser.h>
     77 
     78 #include <prop/proplib.h>
     79 
     80 #include <uvm/uvm_extern.h>
     81 #include <uvm/uvm_readahead.h>
     82 
     83 #include "rump_private.h"
     84 #include "rump_net_private.h"
     85 #include "rump_vfs_private.h"
     86 #include "rump_dev_private.h"
     87 
     88 char machine[] = MACHINE;
     89 
     90 struct proc *initproc;
     91 
     92 struct device rump_rootdev = {
     93 	.dv_class = DV_VIRTUAL
     94 };
     95 
     96 #ifdef RUMP_WITHOUT_THREADS
     97 int rump_threads = 0;
     98 #else
     99 int rump_threads = 1;
    100 #endif
    101 
    102 static int rump_proxy_syscall(int, void *, register_t *);
    103 static int rump_proxy_rfork(void *, int, const char *);
    104 static void rump_proxy_lwpexit(void);
    105 static void rump_proxy_execnotify(const char *);
    106 
    107 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    108 
    109 #ifdef LOCKDEBUG
    110 const int rump_lockdebug = 1;
    111 #else
    112 const int rump_lockdebug = 0;
    113 #endif
    114 bool rump_ttycomponent = false;
    115 
    116 static void
    117 rump_aiodone_worker(struct work *wk, void *dummy)
    118 {
    119 	struct buf *bp = (struct buf *)wk;
    120 
    121 	KASSERT(&bp->b_work == wk);
    122 	bp->b_iodone(bp);
    123 }
    124 
    125 static int rump_inited;
    126 
    127 /*
    128  * Make sure pnbuf_cache is available even without vfs
    129  */
    130 int rump_initpnbufpool(void);
    131 int rump_initpnbufpool(void)
    132 {
    133 
    134         pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    135 	    NULL, IPL_NONE, NULL, NULL, NULL);
    136 	return EOPNOTSUPP;
    137 }
    138 
    139 int rump__unavailable(void);
    140 int rump__unavailable() {return EOPNOTSUPP;}
    141 __weak_alias(rump_net_init,rump__unavailable);
    142 __weak_alias(rump_vfs_init,rump_initpnbufpool);
    143 __weak_alias(rump_dev_init,rump__unavailable);
    144 
    145 __weak_alias(rump_vfs_fini,rump__unavailable);
    146 
    147 __weak_alias(biodone,rump__unavailable);
    148 __weak_alias(sopoll,rump__unavailable);
    149 
    150 __weak_alias(rump_vfs_drainbufs,rump__unavailable);
    151 
    152 void rump__unavailable_vfs_panic(void);
    153 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    154 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    155 
    156 /* easier to write vfs-less clients */
    157 __weak_alias(rump_pub_etfs_register,rump__unavailable);
    158 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
    159 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
    160 
    161 rump_proc_vfs_init_fn rump_proc_vfs_init;
    162 rump_proc_vfs_release_fn rump_proc_vfs_release;
    163 
    164 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    165 
    166 /*
    167  * Create kern.hostname.  why only this you ask.  well, init_sysctl
    168  * is a kitchen sink in need of some gardening.  but i want to use
    169  * kern.hostname today.
    170  */
    171 static void
    172 mksysctls(void)
    173 {
    174 
    175 	sysctl_createv(NULL, 0, NULL, NULL,
    176 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
    177 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
    178 
    179 	/* XXX: setting hostnamelen is missing */
    180 	sysctl_createv(NULL, 0, NULL, NULL,
    181 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
    182 	    SYSCTL_DESCR("System hostname"), NULL, 0,
    183 	    &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    184 }
    185 
    186 /* there's no convenient kernel entry point for this, so just craft out own */
    187 static pid_t
    188 spgetpid(void)
    189 {
    190 
    191 	return curproc->p_pid;
    192 }
    193 
    194 static const struct rumpuser_sp_ops spops = {
    195 	.spop_schedule		= rump_schedule,
    196 	.spop_unschedule	= rump_unschedule,
    197 	.spop_lwproc_switch	= rump_lwproc_switch,
    198 	.spop_lwproc_release	= rump_lwproc_releaselwp,
    199 	.spop_lwproc_rfork	= rump_proxy_rfork,
    200 	.spop_lwproc_newlwp	= rump_lwproc_newlwp,
    201 	.spop_lwproc_curlwp	= rump_lwproc_curlwp,
    202 	.spop_lwpexit		= rump_proxy_lwpexit,
    203 	.spop_syscall		= rump_proxy_syscall,
    204 	.spop_execnotify	= rump_proxy_execnotify,
    205 	.spop_getpid		= spgetpid,
    206 };
    207 
    208 int
    209 rump_daemonize_begin(void)
    210 {
    211 
    212 	if (rump_inited)
    213 		return EALREADY;
    214 
    215 	return rumpuser_daemonize_begin();
    216 }
    217 
    218 int
    219 rump_daemonize_done(int error)
    220 {
    221 
    222 	return rumpuser_daemonize_done(error);
    223 }
    224 
    225 int
    226 rump__init(int rump_version)
    227 {
    228 	char buf[256];
    229 	struct timespec ts;
    230 	uint64_t sec, nsec;
    231 	struct lwp *l;
    232 	int i, numcpu;
    233 	int error;
    234 
    235 	/* not reentrant */
    236 	if (rump_inited)
    237 		return 0;
    238 	else if (rump_inited == -1)
    239 		panic("rump_init: host process restart required");
    240 	else
    241 		rump_inited = 1;
    242 
    243 	if (rumpuser_getversion() != RUMPUSER_VERSION) {
    244 		/* let's hope the ABI of rumpuser_dprintf is the same ;) */
    245 		rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
    246 		    rumpuser_getversion(), RUMPUSER_VERSION);
    247 		return EPROGMISMATCH;
    248 	}
    249 
    250 	if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
    251 		if (*buf != '0')
    252 			boothowto = AB_VERBOSE;
    253 	}
    254 
    255 	if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
    256 		error = 0;
    257 	if (error == 0) {
    258 		numcpu = strtoll(buf, NULL, 10);
    259 		if (numcpu < 1)
    260 			numcpu = 1;
    261 	} else {
    262 		numcpu = rumpuser_getnhostcpu();
    263 	}
    264 	rump_cpus_bootstrap(&numcpu);
    265 
    266 	rumpuser_gettime(&sec, &nsec, &error);
    267 	boottime.tv_sec = sec;
    268 	boottime.tv_nsec = nsec;
    269 
    270 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    271 	aprint_verbose("%s%s", copyright, version);
    272 
    273 	if (rump_version != RUMP_VERSION) {
    274 		printf("rump version mismatch, %d vs. %d\n",
    275 		    rump_version, RUMP_VERSION);
    276 		return EPROGMISMATCH;
    277 	}
    278 
    279 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    280 		rump_threads = *buf != '0';
    281 	}
    282 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    283 	    rump_threads);
    284 	rump_intr_init(numcpu);
    285 	rump_tsleep_init();
    286 
    287 	/* init minimal lwp/cpu context */
    288 	l = &lwp0;
    289 	l->l_lid = 1;
    290 	l->l_cpu = l->l_target_cpu = rump_cpu;
    291 	l->l_fd = &filedesc0;
    292 	rumpuser_set_curlwp(l);
    293 
    294 	rumpuser_mutex_init(&rump_giantlock);
    295 	ksyms_init();
    296 	uvm_init();
    297 	evcnt_init();
    298 
    299 	once_init();
    300 	kernconfig_lock_init();
    301 	prop_kern_init();
    302 
    303 	kmem_init();
    304 
    305 	uvm_ra_init();
    306 	uao_init();
    307 
    308 	mutex_obj_init();
    309 	callout_startup();
    310 
    311 	kprintf_init();
    312 	loginit();
    313 
    314 	kauth_init();
    315 
    316 	secmodel_init();
    317 
    318 	rnd_init();
    319 
    320 	/*
    321 	 * Create the kernel cprng.  Yes, it's currently stubbed out
    322 	 * to arc4random() for RUMP, but this won't always be so.
    323 	 */
    324 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
    325 					 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    326 
    327 	procinit();
    328 	proc0_init();
    329 	uid_init();
    330 	chgproccnt(0, 1);
    331 
    332 	l->l_proc = &proc0;
    333 	lwp_update_creds(l);
    334 
    335 	lwpinit_specificdata();
    336 	lwp_initspecific(&lwp0);
    337 
    338 	rump_scheduler_init(numcpu);
    339 	/* revert temporary context and schedule a semireal context */
    340 	rumpuser_set_curlwp(NULL);
    341 	initproc = &proc0; /* borrow proc0 before we get initproc started */
    342 	rump_schedule();
    343 
    344 	percpu_init();
    345 	inittimecounter();
    346 	ntp_init();
    347 
    348 	rumpuser_gettime(&sec, &nsec, &error);
    349 	ts.tv_sec = sec;
    350 	ts.tv_nsec = nsec;
    351 	tc_setclock(&ts);
    352 
    353 	/* we are mostly go.  do per-cpu subsystem init */
    354 	for (i = 0; i < numcpu; i++) {
    355 		struct cpu_info *ci = cpu_lookup(i);
    356 
    357 		/* attach non-bootstrap CPUs */
    358 		if (i > 0) {
    359 			rump_cpu_attach(ci);
    360 			ncpu++;
    361 		}
    362 
    363 		callout_init_cpu(ci);
    364 		softint_init(ci);
    365 		xc_init_cpu(ci);
    366 		pool_cache_cpu_init(ci);
    367 		selsysinit(ci);
    368 		percpu_init_cpu(ci);
    369 
    370 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
    371 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
    372 
    373 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
    374 	}
    375 
    376 	sysctl_init();
    377 	mksysctls();
    378 	kqueue_init();
    379 	iostat_init();
    380 	fd_sys_init();
    381 	module_init();
    382 	devsw_init();
    383 	pipe_init();
    384 	resource_init();
    385 	procinit_sysctl();
    386 
    387 	/* start page baroness */
    388 	if (rump_threads) {
    389 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    390 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    391 			panic("pagedaemon create failed");
    392 	} else
    393 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    394 
    395 	/* process dso's */
    396 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    397 
    398 	rump_component_init(RUMP_COMPONENT_KERN);
    399 
    400 	/* these do nothing if not present */
    401 	rump_vfs_init();
    402 	rump_net_init();
    403 	rump_dev_init();
    404 
    405 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
    406 
    407 	/*
    408 	 * if we initialized the tty component above, the tyttymtx is
    409 	 * now initialized.  otherwise, we need to initialize it.
    410 	 */
    411 	if (!rump_ttycomponent)
    412 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
    413 
    414 	cold = 0;
    415 
    416 	/* aieeeedondest */
    417 	if (rump_threads) {
    418 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    419 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    420 			panic("aiodoned");
    421 	}
    422 
    423 	sysctl_finalize();
    424 
    425 	module_init_class(MODULE_CLASS_ANY);
    426 
    427 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    428 	hostnamelen = strlen(hostname);
    429 
    430 	sigemptyset(&sigcantmask);
    431 
    432 	if (rump_threads)
    433 		vmem_rehash_start();
    434 
    435 	/*
    436 	 * Create init, used to attach implicit threads in rump.
    437 	 * (note: must be done after vfsinit to get cwdi)
    438 	 */
    439 	(void)rump__lwproc_alloclwp(NULL); /* dummy thread for initproc */
    440 	mutex_enter(proc_lock);
    441 	initproc = proc_find_raw(1);
    442 	mutex_exit(proc_lock);
    443 	if (initproc == NULL)
    444 		panic("where in the world is initproc?");
    445 
    446 	/*
    447 	 * Adjust syscall vector in case factions were dlopen()'d
    448 	 * before calling rump_init().
    449 	 * (modules will handle dynamic syscalls the usual way)
    450 	 *
    451 	 * Note: this will adjust the function vectors of
    452 	 * syscalls which use a funcalias (getpid etc.), but
    453 	 * it makes no difference.
    454 	 */
    455 	for (i = 0; i < SYS_NSYSENT; i++) {
    456 		void *sym;
    457 
    458 		if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
    459 		    *syscallnames[i] == '#' ||
    460 		    rump_sysent[i].sy_call == sys_nomodule)
    461 			continue;
    462 
    463 		/*
    464 		 * deal with compat wrappers.  makesyscalls.sh should
    465 		 * generate the necessary info instead of this hack,
    466 		 * though.  ugly, fix it later.
    467 		 */
    468 #define CPFX "compat_"
    469 #define CPFXLEN (sizeof(CPFX)-1)
    470 		if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
    471 			const char *p = syscallnames[i] + CPFXLEN;
    472 			size_t namelen;
    473 
    474 			/* skip version number */
    475 			while (*p >= '0' && *p <= '9')
    476 				p++;
    477 			if (p == syscallnames[i] + CPFXLEN || *p != '_')
    478 				panic("invalid syscall name %s\n",
    479 				    syscallnames[i]);
    480 
    481 			/* skip over the next underscore */
    482 			p++;
    483 			namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
    484 
    485 			strcpy(buf, "rumpns_");
    486 			strcat(buf, syscallnames[i]);
    487 			/* XXX: no strncat in the kernel */
    488 			strcpy(buf+namelen, "sys_");
    489 			strcat(buf, p);
    490 #undef CPFX
    491 #undef CPFXLEN
    492 		} else {
    493 			sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
    494 		}
    495 		if ((sym = rumpuser_dl_globalsym(buf)) != NULL
    496 		    && sym != rump_sysent[i].sy_call) {
    497 #if 0
    498 			rumpuser_dprintf("adjusting %s: %p (old %p)\n",
    499 			    syscallnames[i], sym, rump_sysent[i].sy_call);
    500 #endif
    501 			rump_sysent[i].sy_call = sym;
    502 		}
    503 	}
    504 
    505 	/* release cpu */
    506 	rump_unschedule();
    507 
    508 	return 0;
    509 }
    510 
    511 int
    512 rump_init_server(const char *url)
    513 {
    514 
    515 	return rumpuser_sp_init(url, &spops, ostype, osrelease, MACHINE);
    516 }
    517 
    518 void
    519 cpu_reboot(int howto, char *bootstr)
    520 {
    521 	int ruhow = 0;
    522 	void *finiarg;
    523 
    524 	printf("rump kernel halting...\n");
    525 
    526 	if (!RUMP_LOCALPROC_P(curproc))
    527 		finiarg = curproc->p_vmspace->vm_map.pmap;
    528 	else
    529 		finiarg = NULL;
    530 
    531 	/* dump means we really take the dive here */
    532 	if ((howto & RB_DUMP) || panicstr) {
    533 		ruhow = RUMPUSER_PANIC;
    534 		goto out;
    535 	}
    536 
    537 	/* try to sync */
    538 	if (!((howto & RB_NOSYNC) || panicstr)) {
    539 		rump_vfs_fini();
    540 	}
    541 
    542 	/* your wish is my command */
    543 	if (howto & RB_HALT) {
    544 		printf("rump kernel halted\n");
    545 		rumpuser_sp_fini(finiarg);
    546 		for (;;) {
    547 			uint64_t sec = 5, nsec = 0;
    548 			int error;
    549 
    550 			rumpuser_nanosleep(&sec, &nsec, &error);
    551 		}
    552 	}
    553 
    554 	/* this function is __dead, we must exit */
    555  out:
    556 	printf("halted\n");
    557 	rumpuser_sp_fini(finiarg);
    558 	rumpuser_exit(ruhow);
    559 }
    560 
    561 struct uio *
    562 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    563 {
    564 	struct uio *uio;
    565 	enum uio_rw uiorw;
    566 
    567 	switch (rw) {
    568 	case RUMPUIO_READ:
    569 		uiorw = UIO_READ;
    570 		break;
    571 	case RUMPUIO_WRITE:
    572 		uiorw = UIO_WRITE;
    573 		break;
    574 	default:
    575 		panic("%s: invalid rw %d", __func__, rw);
    576 	}
    577 
    578 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    579 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    580 
    581 	uio->uio_iov->iov_base = buf;
    582 	uio->uio_iov->iov_len = bufsize;
    583 
    584 	uio->uio_iovcnt = 1;
    585 	uio->uio_offset = offset;
    586 	uio->uio_resid = bufsize;
    587 	uio->uio_rw = uiorw;
    588 	UIO_SETUP_SYSSPACE(uio);
    589 
    590 	return uio;
    591 }
    592 
    593 size_t
    594 rump_uio_getresid(struct uio *uio)
    595 {
    596 
    597 	return uio->uio_resid;
    598 }
    599 
    600 off_t
    601 rump_uio_getoff(struct uio *uio)
    602 {
    603 
    604 	return uio->uio_offset;
    605 }
    606 
    607 size_t
    608 rump_uio_free(struct uio *uio)
    609 {
    610 	size_t resid;
    611 
    612 	resid = uio->uio_resid;
    613 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    614 	kmem_free(uio, sizeof(*uio));
    615 
    616 	return resid;
    617 }
    618 
    619 kauth_cred_t
    620 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    621 {
    622 	kauth_cred_t cred;
    623 	int rv;
    624 
    625 	cred = kauth_cred_alloc();
    626 	kauth_cred_setuid(cred, uid);
    627 	kauth_cred_seteuid(cred, uid);
    628 	kauth_cred_setsvuid(cred, uid);
    629 	kauth_cred_setgid(cred, gid);
    630 	kauth_cred_setgid(cred, gid);
    631 	kauth_cred_setegid(cred, gid);
    632 	kauth_cred_setsvgid(cred, gid);
    633 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    634 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    635 	assert(rv == 0);
    636 
    637 	return cred;
    638 }
    639 
    640 void
    641 rump_cred_put(kauth_cred_t cred)
    642 {
    643 
    644 	kauth_cred_free(cred);
    645 }
    646 
    647 static int compcounter[RUMP_COMPONENT_MAX];
    648 
    649 static void
    650 rump_component_init_cb(struct rump_component *rc, int type)
    651 {
    652 
    653 	KASSERT(type < RUMP_COMPONENT_MAX);
    654 	if (rc->rc_type == type) {
    655 		rc->rc_init();
    656 		compcounter[type]++;
    657 	}
    658 }
    659 
    660 int
    661 rump_component_count(enum rump_component_type type)
    662 {
    663 
    664 	KASSERT(type <= RUMP_COMPONENT_MAX);
    665 	return compcounter[type];
    666 }
    667 
    668 void
    669 rump_component_init(enum rump_component_type type)
    670 {
    671 
    672 	rumpuser_dl_component_init(type, rump_component_init_cb);
    673 }
    674 
    675 /*
    676  * Initialize a module which has already been loaded and linked
    677  * with dlopen(). This is fundamentally the same as a builtin module.
    678  */
    679 int
    680 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    681 {
    682 
    683 	return module_builtin_add(mip, nmodinfo, true);
    684 }
    685 
    686 /*
    687  * Finish module (flawless victory, fatality!).
    688  */
    689 int
    690 rump_module_fini(const struct modinfo *mi)
    691 {
    692 
    693 	return module_builtin_remove(mi, true);
    694 }
    695 
    696 /*
    697  * Add loaded and linked module to the builtin list.  It will
    698  * later be initialized with module_init_class().
    699  */
    700 
    701 static void
    702 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    703 {
    704 
    705 	module_builtin_add(mip, nmodinfo, false);
    706 }
    707 
    708 int
    709 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    710 	char *strtab, uint64_t strsize)
    711 {
    712 	static int inited = 0;
    713 	Elf64_Ehdr ehdr;
    714 
    715 	if (inited)
    716 		return EBUSY;
    717 	inited = 1;
    718 
    719 	/*
    720 	 * Use 64bit header since it's bigger.  Shouldn't make a
    721 	 * difference, since we're passing in all zeroes anyway.
    722 	 */
    723 	memset(&ehdr, 0, sizeof(ehdr));
    724 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    725 
    726 	return 0;
    727 }
    728 
    729 static int
    730 rump_proxy_syscall(int num, void *arg, register_t *retval)
    731 {
    732 	struct lwp *l;
    733 	struct sysent *callp;
    734 	int rv;
    735 
    736 	if (__predict_false(num >= SYS_NSYSENT))
    737 		return ENOSYS;
    738 
    739 	callp = rump_sysent + num;
    740 	l = curlwp;
    741 	rv = sy_call(callp, l, (void *)arg, retval);
    742 
    743 	return rv;
    744 }
    745 
    746 static int
    747 rump_proxy_rfork(void *priv, int flags, const char *comm)
    748 {
    749 	struct vmspace *newspace;
    750 	struct proc *p;
    751 	int error;
    752 
    753 	if ((error = rump_lwproc_rfork(flags)) != 0)
    754 		return error;
    755 
    756 	/*
    757 	 * Since it's a proxy proc, adjust the vmspace.
    758 	 * Refcount will eternally be 1.
    759 	 */
    760 	p = curproc;
    761 	newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
    762 	newspace->vm_refcnt = 1;
    763 	newspace->vm_map.pmap = priv;
    764 	KASSERT(p->p_vmspace == vmspace_kernel());
    765 	p->p_vmspace = newspace;
    766 	if (comm)
    767 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    768 
    769 	return 0;
    770 }
    771 
    772 /*
    773  * Order all lwps in a process to exit.  does *not* wait for them to drain.
    774  */
    775 static void
    776 rump_proxy_lwpexit(void)
    777 {
    778 	struct proc *p = curproc;
    779 	uint64_t where;
    780 	struct lwp *l;
    781 
    782 	mutex_enter(p->p_lock);
    783 	/*
    784 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
    785 	 * so that they know they should exit.
    786 	 */
    787 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    788 		if (l == curlwp)
    789 			continue;
    790 		l->l_flag |= LW_RUMP_QEXIT;
    791 	}
    792 	mutex_exit(p->p_lock);
    793 
    794 	/*
    795 	 * Next, make sure everyone on all CPUs sees our status
    796 	 * update.  This keeps threads inside cv_wait() and makes
    797 	 * sure we don't access a stale cv pointer later when
    798 	 * we wake up the threads.
    799 	 */
    800 
    801 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    802 	xc_wait(where);
    803 
    804 	/*
    805 	 * Ok, all lwps are either:
    806 	 *  1) not in the cv code
    807 	 *  2) sleeping on l->l_private
    808 	 *  3) sleeping on p->p_waitcv
    809 	 *
    810 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
    811 	 * in p->p_sflag.
    812 	 */
    813 
    814 	mutex_enter(p->p_lock);
    815 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    816 		if (l->l_private)
    817 			cv_broadcast(l->l_private);
    818 	}
    819 	p->p_sflag |= PS_RUMP_LWPEXIT;
    820 	cv_broadcast(&p->p_waitcv);
    821 	mutex_exit(p->p_lock);
    822 }
    823 
    824 /*
    825  * Notify process that all threads have been drained and exec is complete.
    826  */
    827 static void
    828 rump_proxy_execnotify(const char *comm)
    829 {
    830 	struct proc *p = curproc;
    831 
    832 	fd_closeexec();
    833 	mutex_enter(p->p_lock);
    834 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
    835 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
    836 	mutex_exit(p->p_lock);
    837 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    838 }
    839 
    840 int
    841 rump_boot_gethowto()
    842 {
    843 
    844 	return boothowto;
    845 }
    846 
    847 void
    848 rump_boot_sethowto(int howto)
    849 {
    850 
    851 	boothowto = howto;
    852 }
    853 
    854 int
    855 rump_getversion(void)
    856 {
    857 
    858 	return __NetBSD_Version__;
    859 }
    860 
    861 /*
    862  * Note: may be called unscheduled.  Not fully safe since no locking
    863  * of allevents (currently that's not even available).
    864  */
    865 void
    866 rump_printevcnts()
    867 {
    868 	struct evcnt *ev;
    869 
    870 	TAILQ_FOREACH(ev, &allevents, ev_list)
    871 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    872 		    ev->ev_group, ev->ev_name, ev->ev_count);
    873 }
    874 
    875 /*
    876  * If you use this interface ... well ... all bets are off.
    877  * The original purpose is for the p2k fs server library to be
    878  * able to use the same pid/lid for VOPs as the host kernel.
    879  */
    880 void
    881 rump_allbetsareoff_setid(pid_t pid, int lid)
    882 {
    883 	struct lwp *l = curlwp;
    884 	struct proc *p = l->l_proc;
    885 
    886 	l->l_lid = lid;
    887 	p->p_pid = pid;
    888 }
    889