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