Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.177
      1 /*	$NetBSD: rump.c,v 1.177 2010/06/09 14:08:17 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by Google Summer of Code.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.177 2010/06/09 14:08:17 pooka Exp $");
     32 
     33 #include <sys/systm.h>
     34 #define ELFSIZE ARCH_ELFSIZE
     35 
     36 #include <sys/param.h>
     37 #include <sys/atomic.h>
     38 #include <sys/buf.h>
     39 #include <sys/callout.h>
     40 #include <sys/conf.h>
     41 #include <sys/cpu.h>
     42 #include <sys/device.h>
     43 #include <sys/evcnt.h>
     44 #include <sys/event.h>
     45 #include <sys/exec_elf.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/iostat.h>
     48 #include <sys/kauth.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/kprintf.h>
     52 #include <sys/kthread.h>
     53 #include <sys/ksyms.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/module.h>
     56 #include <sys/once.h>
     57 #include <sys/percpu.h>
     58 #include <sys/pipe.h>
     59 #include <sys/pool.h>
     60 #include <sys/queue.h>
     61 #include <sys/reboot.h>
     62 #include <sys/resourcevar.h>
     63 #include <sys/select.h>
     64 #include <sys/sysctl.h>
     65 #include <sys/syscall.h>
     66 #include <sys/syscallvar.h>
     67 #include <sys/timetc.h>
     68 #include <sys/tty.h>
     69 #include <sys/uidinfo.h>
     70 #include <sys/vmem.h>
     71 #include <sys/xcall.h>
     72 
     73 #include <rump/rumpuser.h>
     74 
     75 #include <secmodel/suser/suser.h>
     76 
     77 #include <prop/proplib.h>
     78 
     79 #include <uvm/uvm_extern.h>
     80 #include <uvm/uvm_readahead.h>
     81 
     82 #include "rump_private.h"
     83 #include "rump_net_private.h"
     84 #include "rump_vfs_private.h"
     85 #include "rump_dev_private.h"
     86 
     87 struct proc proc0;
     88 struct session rump_session = {
     89 	.s_count = 1,
     90 	.s_flags = 0,
     91 	.s_leader = &proc0,
     92 	.s_login = "rumphobo",
     93 	.s_sid = 0,
     94 };
     95 struct pgrp rump_pgrp = {
     96 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     97 	.pg_session = &rump_session,
     98 	.pg_jobc = 1,
     99 };
    100 struct pstats rump_stats;
    101 struct plimit rump_limits;
    102 struct filedesc rump_filedesc0;
    103 struct proclist allproc;
    104 char machine[] = MACHINE;
    105 static kauth_cred_t rump_susercred;
    106 
    107 /* pretend the master rump proc is init */
    108 struct proc *initproc = &proc0;
    109 
    110 struct rumpuser_mtx *rump_giantlock;
    111 
    112 struct device rump_rootdev = {
    113 	.dv_class = DV_VIRTUAL
    114 };
    115 
    116 #ifdef RUMP_WITHOUT_THREADS
    117 int rump_threads = 0;
    118 #else
    119 int rump_threads = 1;
    120 #endif
    121 
    122 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    123 
    124 static void
    125 rump_aiodone_worker(struct work *wk, void *dummy)
    126 {
    127 	struct buf *bp = (struct buf *)wk;
    128 
    129 	KASSERT(&bp->b_work == wk);
    130 	bp->b_iodone(bp);
    131 }
    132 
    133 static int rump_inited;
    134 
    135 /*
    136  * Make sure pnbuf_cache is available even without vfs
    137  */
    138 struct pool_cache *pnbuf_cache;
    139 int rump_initpnbufpool(void);
    140 int rump_initpnbufpool(void)
    141 {
    142 
    143         pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    144 	    NULL, IPL_NONE, NULL, NULL, NULL);
    145 	return EOPNOTSUPP;
    146 }
    147 
    148 int rump__unavailable(void);
    149 int rump__unavailable() {return EOPNOTSUPP;}
    150 __weak_alias(rump_net_init,rump__unavailable);
    151 __weak_alias(rump_vfs_init,rump_initpnbufpool);
    152 __weak_alias(rump_dev_init,rump__unavailable);
    153 
    154 __weak_alias(rump_vfs_fini,rump__unavailable);
    155 
    156 __weak_alias(biodone,rump__unavailable);
    157 __weak_alias(sopoll,rump__unavailable);
    158 
    159 void rump__unavailable_vfs_panic(void);
    160 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    161 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    162 
    163 rump_proc_vfs_init_fn rump_proc_vfs_init;
    164 rump_proc_vfs_release_fn rump_proc_vfs_release;
    165 
    166 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    167 
    168 static void __noinline
    169 messthestack(void)
    170 {
    171 	volatile uint32_t mess[64];
    172 	uint64_t d1, d2;
    173 	int i, error;
    174 
    175 	for (i = 0; i < 64; i++) {
    176 		rumpuser_gettime(&d1, &d2, &error);
    177 		mess[i] = d2;
    178 	}
    179 }
    180 
    181 /*
    182  * Create kern.hostname.  why only this you ask.  well, init_sysctl
    183  * is a kitchen sink in need of some gardening.  but i want to use
    184  * kern.hostname today.
    185  */
    186 static void
    187 mksysctls(void)
    188 {
    189 
    190 	sysctl_createv(NULL, 0, NULL, NULL,
    191 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
    192 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
    193 
    194 	/* XXX: setting hostnamelen is missing */
    195 	sysctl_createv(NULL, 0, NULL, NULL,
    196 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
    197 	    SYSCTL_DESCR("System hostname"), NULL, 0,
    198 	    &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    199 }
    200 
    201 int
    202 rump__init(int rump_version)
    203 {
    204 	char buf[256];
    205 	struct timespec ts;
    206 	uint64_t sec, nsec;
    207 	struct proc *p;
    208 	struct lwp *l;
    209 	int i, numcpu;
    210 	int error;
    211 
    212 	/* not reentrant */
    213 	if (rump_inited)
    214 		return 0;
    215 	else if (rump_inited == -1)
    216 		panic("rump_init: host process restart required");
    217 	else
    218 		rump_inited = 1;
    219 
    220 	if (rumpuser_getversion() != RUMPUSER_VERSION) {
    221 		/* let's hope the ABI of rumpuser_dprintf is the same ;) */
    222 		rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
    223 		    rumpuser_getversion(), RUMPUSER_VERSION);
    224 		return EPROGMISMATCH;
    225 	}
    226 
    227 	if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
    228 		if (*buf != '0')
    229 			boothowto = AB_VERBOSE;
    230 	}
    231 
    232 	if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
    233 		error = 0;
    234 	/* non-x86 is missing CPU_INFO_FOREACH() support */
    235 #if defined(__i386__) || defined(__x86_64__)
    236 	if (error == 0) {
    237 		numcpu = strtoll(buf, NULL, 10);
    238 		if (numcpu < 1)
    239 			numcpu = 1;
    240 	} else {
    241 		numcpu = rumpuser_getnhostcpu();
    242 	}
    243 #else
    244 	if (error == 0)
    245 		printf("NCPU limited to 1 on this host\n");
    246 	numcpu = 1;
    247 #endif
    248 	rump_cpus_bootstrap(numcpu);
    249 
    250 	rumpuser_gettime(&sec, &nsec, &error);
    251 	boottime.tv_sec = sec;
    252 	boottime.tv_nsec = nsec;
    253 
    254 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    255 	aprint_verbose("%s%s", copyright, version);
    256 
    257 	/*
    258 	 * Seed arc4random() with a "reasonable" amount of randomness.
    259 	 * Yes, this is a quick kludge which depends on the arc4random
    260 	 * implementation.
    261 	 */
    262 	messthestack();
    263 	arc4random();
    264 
    265 	if (rump_version != RUMP_VERSION) {
    266 		printf("rump version mismatch, %d vs. %d\n",
    267 		    rump_version, RUMP_VERSION);
    268 		return EPROGMISMATCH;
    269 	}
    270 
    271 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    272 		rump_threads = *buf != '0';
    273 	}
    274 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    275 	    rump_threads);
    276 	rump_intr_init();
    277 	rump_tsleep_init();
    278 
    279 	/* init minimal lwp/cpu context */
    280 	l = &lwp0;
    281 	l->l_lid = 1;
    282 	l->l_cpu = l->l_target_cpu = rump_cpu;
    283 	rumpuser_set_curlwp(l);
    284 
    285 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    286 	rumpuser_mutex_recursive_init(&rump_giantlock);
    287 	ksyms_init();
    288 	uvm_init();
    289 	evcnt_init();
    290 
    291 	once_init();
    292 	prop_kern_init();
    293 
    294 	pool_subsystem_init();
    295 	kmem_init();
    296 
    297 	uvm_ra_init();
    298 
    299 	mutex_obj_init();
    300 	callout_startup();
    301 
    302 	kprintf_init();
    303 	loginit();
    304 
    305 	kauth_init();
    306 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    307 
    308 	/* init proc0 and rest of lwp0 now that we can allocate memory */
    309 	p = &proc0;
    310 	p->p_stats = &rump_stats;
    311 	p->p_limit = &rump_limits;
    312 	p->p_pgrp = &rump_pgrp;
    313 	p->p_pid = 0;
    314 	p->p_fd = &rump_filedesc0;
    315 	p->p_vmspace = &rump_vmspace;
    316 	p->p_emul = &emul_netbsd;
    317 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    318 	l->l_cred = rump_cred_suserget();
    319 	l->l_proc = p;
    320 	LIST_INIT(&allproc);
    321 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    322 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    323 
    324 	lwpinit_specificdata();
    325 	lwp_initspecific(&lwp0);
    326 
    327 	mutex_init(&rump_limits.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    328 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    329 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    330 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    331 	rump_limits.pl_corename = defcorename;
    332 
    333 	rump_scheduler_init();
    334 	/* revert temporary context and schedule a real context */
    335 	l->l_cpu = NULL;
    336 	rumpuser_set_curlwp(NULL);
    337 	rump_schedule();
    338 
    339 	percpu_init();
    340 	inittimecounter();
    341 	ntp_init();
    342 
    343 	rumpuser_gettime(&sec, &nsec, &error);
    344 	ts.tv_sec = sec;
    345 	ts.tv_nsec = nsec;
    346 	tc_setclock(&ts);
    347 
    348 	/* we are mostly go.  do per-cpu subsystem init */
    349 	for (i = 0; i < ncpu; i++) {
    350 		struct cpu_info *ci = cpu_lookup(i);
    351 
    352 		callout_init_cpu(ci);
    353 		softint_init(ci);
    354 		xc_init_cpu(ci);
    355 		pool_cache_cpu_init(ci);
    356 		selsysinit(ci);
    357 		percpu_init_cpu(ci);
    358 	}
    359 
    360 	sysctl_init();
    361 	kqueue_init();
    362 	iostat_init();
    363 	uid_init();
    364 	fd_sys_init();
    365 	module_init();
    366 	devsw_init();
    367 	pipe_init();
    368 	resource_init();
    369 
    370 	/* start page baroness */
    371 	if (rump_threads) {
    372 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    373 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    374 			panic("pagedaemon create failed");
    375 	} else
    376 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    377 
    378 	/* process dso's */
    379 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    380 
    381 	/* these do nothing if not present */
    382 	rump_vfs_init();
    383 	rump_net_init();
    384 	rump_dev_init();
    385 	cold = 0;
    386 
    387 	/* aieeeedondest */
    388 	if (rump_threads) {
    389 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    390 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    391 			panic("aiodoned");
    392 	}
    393 
    394 	mksysctls();
    395 	sysctl_finalize();
    396 
    397 	module_init_class(MODULE_CLASS_ANY);
    398 
    399 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    400 	hostnamelen = strlen(hostname);
    401 
    402 	sigemptyset(&sigcantmask);
    403 
    404 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    405 
    406 	if (rump_threads)
    407 		vmem_rehash_start();
    408 
    409 	rump_unschedule();
    410 
    411 	return 0;
    412 }
    413 
    414 /* maybe support sys_reboot some day for remote shutdown */
    415 void
    416 rump_reboot(int howto)
    417 {
    418 
    419 	/* dump means we really take the dive here */
    420 	if ((howto & RB_DUMP) || panicstr) {
    421 		rumpuser_exit(RUMPUSER_PANIC);
    422 		/*NOTREACHED*/
    423 	}
    424 
    425 	/* try to sync */
    426 	if (!((howto & RB_NOSYNC) || panicstr)) {
    427 		rump_vfs_fini();
    428 	}
    429 
    430 	/* your wish is my command */
    431 	if (howto & RB_HALT) {
    432 		for (;;) {
    433 			uint64_t sec = 5, nsec = 0;
    434 			int error;
    435 
    436 			rumpuser_nanosleep(&sec, &nsec, &error);
    437 		}
    438 	}
    439 	rump_inited = -1;
    440 }
    441 
    442 struct uio *
    443 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    444 {
    445 	struct uio *uio;
    446 	enum uio_rw uiorw;
    447 
    448 	switch (rw) {
    449 	case RUMPUIO_READ:
    450 		uiorw = UIO_READ;
    451 		break;
    452 	case RUMPUIO_WRITE:
    453 		uiorw = UIO_WRITE;
    454 		break;
    455 	default:
    456 		panic("%s: invalid rw %d", __func__, rw);
    457 	}
    458 
    459 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    460 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    461 
    462 	uio->uio_iov->iov_base = buf;
    463 	uio->uio_iov->iov_len = bufsize;
    464 
    465 	uio->uio_iovcnt = 1;
    466 	uio->uio_offset = offset;
    467 	uio->uio_resid = bufsize;
    468 	uio->uio_rw = uiorw;
    469 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    470 
    471 	return uio;
    472 }
    473 
    474 size_t
    475 rump_uio_getresid(struct uio *uio)
    476 {
    477 
    478 	return uio->uio_resid;
    479 }
    480 
    481 off_t
    482 rump_uio_getoff(struct uio *uio)
    483 {
    484 
    485 	return uio->uio_offset;
    486 }
    487 
    488 size_t
    489 rump_uio_free(struct uio *uio)
    490 {
    491 	size_t resid;
    492 
    493 	resid = uio->uio_resid;
    494 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    495 	kmem_free(uio, sizeof(*uio));
    496 
    497 	return resid;
    498 }
    499 
    500 static pid_t nextpid = 1;
    501 struct lwp *
    502 rump_newproc_switch()
    503 {
    504 	struct lwp *l;
    505 	pid_t mypid;
    506 
    507 	mypid = atomic_inc_uint_nv(&nextpid);
    508 	if (__predict_false(mypid == 0))
    509 		mypid = atomic_inc_uint_nv(&nextpid);
    510 
    511 	l = rump_lwp_alloc(mypid, 0);
    512 	rump_lwp_switch(l);
    513 
    514 	return l;
    515 }
    516 
    517 struct lwp *
    518 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
    519 {
    520 	struct lwp *l;
    521 
    522 	l = rump_lwp_alloc(pid, lid);
    523 	rump_lwp_switch(l);
    524 
    525 	return l;
    526 }
    527 
    528 struct lwp *
    529 rump_lwp_alloc(pid_t pid, lwpid_t lid)
    530 {
    531 	struct lwp *l;
    532 	struct proc *p;
    533 
    534 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    535 	if (pid != 0) {
    536 		p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    537 		if (rump_proc_vfs_init)
    538 			rump_proc_vfs_init(p);
    539 		p->p_stats = &rump_stats;
    540 		p->p_limit = lim_copy(&rump_limits);
    541 		p->p_pid = pid;
    542 		p->p_vmspace = &rump_vmspace;
    543 		p->p_emul = &emul_netbsd;
    544 		p->p_fd = fd_init(NULL);
    545 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    546 		p->p_pgrp = &rump_pgrp;
    547 		l->l_cred = rump_cred_suserget();
    548 	} else {
    549 		p = &proc0;
    550 		l->l_cred = rump_susercred;
    551 	}
    552 
    553 	l->l_proc = p;
    554 	l->l_lid = lid;
    555 	l->l_fd = p->p_fd;
    556 	l->l_cpu = NULL;
    557 	l->l_target_cpu = rump_cpu;
    558 	lwp_initspecific(l);
    559 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    560 
    561 	return l;
    562 }
    563 
    564 void
    565 rump_lwp_switch(struct lwp *newlwp)
    566 {
    567 	struct lwp *l = curlwp;
    568 
    569 	rumpuser_set_curlwp(NULL);
    570 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    571 	newlwp->l_mutex = l->l_mutex;
    572 	l->l_mutex = NULL;
    573 	l->l_cpu = NULL;
    574 	rumpuser_set_curlwp(newlwp);
    575 	if (l->l_flag & LW_WEXIT)
    576 		rump_lwp_free(l);
    577 }
    578 
    579 /* XXX: this has effect only on non-pid0 lwps */
    580 void
    581 rump_lwp_release(struct lwp *l)
    582 {
    583 	struct proc *p;
    584 
    585 	p = l->l_proc;
    586 	if (p->p_pid != 0) {
    587 		mutex_obj_free(p->p_lock);
    588 		fd_free();
    589 		if (rump_proc_vfs_release)
    590 			rump_proc_vfs_release(p);
    591 		rump_cred_put(l->l_cred);
    592 		limfree(p->p_limit);
    593 		kmem_free(p, sizeof(*p));
    594 	}
    595 	KASSERT((l->l_flag & LW_WEXIT) == 0);
    596 	l->l_flag |= LW_WEXIT;
    597 }
    598 
    599 void
    600 rump_lwp_free(struct lwp *l)
    601 {
    602 
    603 	KASSERT(l->l_flag & LW_WEXIT);
    604 	KASSERT(l->l_mutex == NULL);
    605 	if (l->l_name)
    606 		kmem_free(l->l_name, MAXCOMLEN);
    607 	lwp_finispecific(l);
    608 	LIST_REMOVE(l, l_list);
    609 	kmem_free(l, sizeof(*l));
    610 }
    611 
    612 struct lwp *
    613 rump_lwp_curlwp(void)
    614 {
    615 	struct lwp *l = curlwp;
    616 
    617 	if (l->l_flag & LW_WEXIT)
    618 		return NULL;
    619 	return l;
    620 }
    621 
    622 /* rump private.  NEEDS WORK! */
    623 void
    624 rump_set_vmspace(struct vmspace *vm)
    625 {
    626 	struct proc *p = curproc;
    627 
    628 	p->p_vmspace = vm;
    629 }
    630 
    631 kauth_cred_t
    632 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    633 {
    634 	kauth_cred_t cred;
    635 	int rv;
    636 
    637 	cred = kauth_cred_alloc();
    638 	kauth_cred_setuid(cred, uid);
    639 	kauth_cred_seteuid(cred, uid);
    640 	kauth_cred_setsvuid(cred, uid);
    641 	kauth_cred_setgid(cred, gid);
    642 	kauth_cred_setgid(cred, gid);
    643 	kauth_cred_setegid(cred, gid);
    644 	kauth_cred_setsvgid(cred, gid);
    645 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    646 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    647 	assert(rv == 0);
    648 
    649 	return cred;
    650 }
    651 
    652 void
    653 rump_cred_put(kauth_cred_t cred)
    654 {
    655 
    656 	kauth_cred_free(cred);
    657 }
    658 
    659 kauth_cred_t
    660 rump_cred_suserget(void)
    661 {
    662 
    663 	kauth_cred_hold(rump_susercred);
    664 	return rump_susercred;
    665 }
    666 
    667 /*
    668  * Return the next system lwpid
    669  */
    670 lwpid_t
    671 rump_nextlid(void)
    672 {
    673 	lwpid_t retid;
    674 
    675 	mutex_enter(proc0.p_lock);
    676 	/*
    677 	 * Take next one, don't return 0
    678 	 * XXX: most likely we'll have collisions in case this
    679 	 * wraps around.
    680 	 */
    681 	if (++proc0.p_nlwpid == 0)
    682 		++proc0.p_nlwpid;
    683 	retid = proc0.p_nlwpid;
    684 	mutex_exit(proc0.p_lock);
    685 
    686 	return retid;
    687 }
    688 
    689 static int compcounter[RUMP_COMPONENT_MAX];
    690 
    691 static void
    692 rump_component_init_cb(struct rump_component *rc, int type)
    693 {
    694 
    695 	KASSERT(type < RUMP_COMPONENT_MAX);
    696 	if (rc->rc_type == type) {
    697 		rc->rc_init();
    698 		compcounter[type]++;
    699 	}
    700 }
    701 
    702 int
    703 rump_component_count(enum rump_component_type type)
    704 {
    705 
    706 	KASSERT(type <= RUMP_COMPONENT_MAX);
    707 	return compcounter[type];
    708 }
    709 
    710 void
    711 rump_component_init(enum rump_component_type type)
    712 {
    713 
    714 	rumpuser_dl_component_init(type, rump_component_init_cb);
    715 }
    716 
    717 /*
    718  * Initialize a module which has already been loaded and linked
    719  * with dlopen(). This is fundamentally the same as a builtin module.
    720  */
    721 int
    722 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    723 {
    724 
    725 	return module_builtin_add(mip, nmodinfo, true);
    726 }
    727 
    728 /*
    729  * Finish module (flawless victory, fatality!).
    730  */
    731 int
    732 rump_module_fini(const struct modinfo *mi)
    733 {
    734 
    735 	return module_builtin_remove(mi, true);
    736 }
    737 
    738 /*
    739  * Add loaded and linked module to the builtin list.  It will
    740  * later be initialized with module_init_class().
    741  */
    742 
    743 static void
    744 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    745 {
    746 
    747 	module_builtin_add(mip, nmodinfo, false);
    748 }
    749 
    750 int
    751 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    752 	char *strtab, uint64_t strsize)
    753 {
    754 	static int inited = 0;
    755 	Elf64_Ehdr ehdr;
    756 
    757 	if (inited)
    758 		return EBUSY;
    759 	inited = 1;
    760 
    761 	/*
    762 	 * Use 64bit header since it's bigger.  Shouldn't make a
    763 	 * difference, since we're passing in all zeroes anyway.
    764 	 */
    765 	memset(&ehdr, 0, sizeof(ehdr));
    766 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    767 
    768 	return 0;
    769 }
    770 
    771 static int
    772 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    773 	register_t *retval)
    774 {
    775 	struct lwp *l;
    776 	struct sysent *callp;
    777 	int rv;
    778 
    779 	if (__predict_false(num >= SYS_NSYSENT))
    780 		return ENOSYS;
    781 
    782 	callp = rump_sysent + num;
    783 	rump_schedule();
    784 	l = curlwp;
    785 	rv = sy_call(callp, l, (void *)data, retval);
    786 	rump_unschedule();
    787 
    788 	return rv;
    789 }
    790 
    791 int
    792 rump_boot_gethowto()
    793 {
    794 
    795 	return boothowto;
    796 }
    797 
    798 void
    799 rump_boot_sethowto(int howto)
    800 {
    801 
    802 	boothowto = howto;
    803 }
    804 
    805 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    806 void *rump_sysproxy_arg;
    807 
    808 /*
    809  * This whole syscall-via-rpc is still taking form.  For example, it
    810  * may be necessary to set syscalls individually instead of lobbing
    811  * them all to the same place.  So don't think this interface is
    812  * set in stone.
    813  */
    814 int
    815 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    816 {
    817 
    818 	if (rump_sysproxy_arg)
    819 		return EBUSY;
    820 
    821 	rump_sysproxy_arg = arg;
    822 	rump_sysproxy = proxy;
    823 
    824 	return 0;
    825 }
    826 
    827 int
    828 rump_getversion(void)
    829 {
    830 
    831 	return __NetBSD_Version__;
    832 }
    833 
    834 /*
    835  * Note: may be called unscheduled.  Not fully safe since no locking
    836  * of allevents (currently that's not even available).
    837  */
    838 void
    839 rump_printevcnts()
    840 {
    841 	struct evcnt *ev;
    842 
    843 	TAILQ_FOREACH(ev, &allevents, ev_list)
    844 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    845 		    ev->ev_group, ev->ev_name, ev->ev_count);
    846 }
    847