Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.294
      1 /*	$NetBSD: rump.c,v 1.294 2014/04/09 23:53:36 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.294 2014/04/09 23:53:36 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/cprng.h>
     74 #include <sys/ktrace.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_hyp_syscall(int, void *, long *);
    105 static int rump_hyp_rfork(void *, int, const char *);
    106 static void rump_hyp_lwpexit(void);
    107 static void rump_hyp_execnotify(const char *);
    108 
    109 static void rump_component_addlocal(void);
    110 static void rump_component_load(const struct rump_component *);
    111 static struct lwp *bootlwp;
    112 
    113 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    114 
    115 #ifdef LOCKDEBUG
    116 const int rump_lockdebug = 1;
    117 #else
    118 const int rump_lockdebug = 0;
    119 #endif
    120 bool rump_ttycomponent = false;
    121 
    122 static void
    123 rump_aiodone_worker(struct work *wk, void *dummy)
    124 {
    125 	struct buf *bp = (struct buf *)wk;
    126 
    127 	KASSERT(&bp->b_work == wk);
    128 	bp->b_iodone(bp);
    129 }
    130 
    131 static int rump_inited;
    132 
    133 void (*rump_vfs_drainbufs)(int);
    134 void (*rump_vfs_fini)(void);
    135 int  (*rump_vfs_makeonedevnode)(dev_t, const char *,
    136 				devmajor_t, devminor_t) = (void *)nullop;
    137 int  (*rump_vfs_makedevnodes)(dev_t, const char *, char,
    138 			      devmajor_t, devminor_t, int) = (void *)nullop;
    139 
    140 int rump__unavailable(void);
    141 int rump__unavailable() {return EOPNOTSUPP;}
    142 
    143 __weak_alias(biodone,rump__unavailable);
    144 __weak_alias(sopoll,rump__unavailable);
    145 
    146 void rump__unavailable_vfs_panic(void);
    147 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    148 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    149 
    150 /* easier to write vfs-less clients */
    151 __weak_alias(rump_pub_etfs_register,rump__unavailable);
    152 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
    153 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
    154 
    155 rump_proc_vfs_init_fn rump_proc_vfs_init;
    156 rump_proc_vfs_release_fn rump_proc_vfs_release;
    157 
    158 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    159 
    160 /*
    161  * Create some sysctl nodes.  why only this you ask.  well, init_sysctl
    162  * is a kitchen sink in need of some gardening.  but i want to use
    163  * others today.  Furthermore, creating a whole kitchen sink full of
    164  * sysctl nodes is a waste of cycles for rump kernel bootstrap.
    165  */
    166 static void
    167 mksysctls(void)
    168 {
    169 
    170 	/* hw.pagesize */
    171 	sysctl_createv(NULL, 0, NULL, NULL,
    172 	    CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    173 	    CTLTYPE_INT, "pagesize",
    174 	    SYSCTL_DESCR("Software page size"),
    175 	    NULL, PAGE_SIZE, NULL, 0,
    176 	    CTL_HW, HW_PAGESIZE, CTL_EOL);
    177 }
    178 
    179 /* there's no convenient kernel entry point for this, so just craft out own */
    180 static pid_t
    181 spgetpid(void)
    182 {
    183 
    184 	return curproc->p_pid;
    185 }
    186 
    187 static const struct rumpuser_hyperup hyp = {
    188 	.hyp_schedule		= rump_schedule,
    189 	.hyp_unschedule		= rump_unschedule,
    190 	.hyp_backend_unschedule	= rump_user_unschedule,
    191 	.hyp_backend_schedule	= rump_user_schedule,
    192 	.hyp_lwproc_switch	= rump_lwproc_switch,
    193 	.hyp_lwproc_release	= rump_lwproc_releaselwp,
    194 	.hyp_lwproc_rfork	= rump_hyp_rfork,
    195 	.hyp_lwproc_newlwp	= rump_lwproc_newlwp,
    196 	.hyp_lwproc_curlwp	= rump_lwproc_curlwp,
    197 	.hyp_lwpexit		= rump_hyp_lwpexit,
    198 	.hyp_syscall		= rump_hyp_syscall,
    199 	.hyp_execnotify		= rump_hyp_execnotify,
    200 	.hyp_getpid		= spgetpid,
    201 };
    202 
    203 int
    204 rump_daemonize_begin(void)
    205 {
    206 
    207 	if (rump_inited)
    208 		return EALREADY;
    209 
    210 	return rumpuser_daemonize_begin();
    211 }
    212 
    213 int
    214 rump_daemonize_done(int error)
    215 {
    216 
    217 	return rumpuser_daemonize_done(error);
    218 }
    219 
    220 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
    221 {
    222 	__link_set_decl(rump_components, struct rump_component);
    223 
    224 	/*
    225 	 * Trick compiler into generating references so that statically
    226 	 * linked rump kernels are generated with the link set symbols.
    227 	 */
    228 	asm("" :: "r"(__start_link_set_rump_components));
    229 	asm("" :: "r"(__stop_link_set_rump_components));
    230 }
    231 
    232 int
    233 rump_init(void)
    234 {
    235 	char buf[256];
    236 	struct timespec ts;
    237 	int64_t sec;
    238 	long nsec;
    239 	struct lwp *l, *initlwp;
    240 	int i, numcpu;
    241 
    242 	/* not reentrant */
    243 	if (rump_inited)
    244 		return 0;
    245 	else if (rump_inited == -1)
    246 		panic("rump_init: host process restart required");
    247 	else
    248 		rump_inited = 1;
    249 
    250 	/* initialize hypervisor */
    251 	if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
    252 		rumpuser_dprintf("rumpuser init failed\n");
    253 		return EINVAL;
    254 	}
    255 
    256 	/* init minimal lwp/cpu context */
    257 	rump_lwproc_init();
    258 	l = &lwp0;
    259 	l->l_cpu = l->l_target_cpu = rump_cpu;
    260 	rump_lwproc_curlwp_set(l);
    261 
    262 	/* retrieve env vars which affect the early stage of bootstrap */
    263 	if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
    264 		rump_threads = *buf != '0';
    265 	}
    266 	if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
    267 		if (*buf != '0')
    268 			boothowto = AB_VERBOSE;
    269 	}
    270 
    271 	if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
    272 		panic("mandatory hypervisor configuration (NCPU) missing");
    273 	numcpu = strtoll(buf, NULL, 10);
    274 	if (numcpu < 1) {
    275 		panic("rump kernels are not lightweight enough for \"%d\" CPUs",
    276 		    numcpu);
    277 	}
    278 
    279 	rump_thread_init();
    280 	rump_cpus_bootstrap(&numcpu);
    281 
    282 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
    283 	boottime.tv_sec = sec;
    284 	boottime.tv_nsec = nsec;
    285 
    286 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    287 	aprint_verbose("%s%s", copyright, version);
    288 
    289 	rump_intr_init(numcpu);
    290 
    291 	rump_tsleep_init();
    292 
    293 	rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
    294 	ksyms_init();
    295 	uvm_init();
    296 	evcnt_init();
    297 
    298 	kcpuset_sysinit();
    299 	once_init();
    300 	kernconfig_lock_init();
    301 	prop_kern_init();
    302 
    303 	kmem_init();
    304 	kmeminit();
    305 
    306 	uvm_ra_init();
    307 	uao_init();
    308 
    309 	mutex_obj_init();
    310 	callout_startup();
    311 
    312 	kprintf_init();
    313 	pserialize_init();
    314 	loginit();
    315 
    316 	kauth_init();
    317 
    318 	secmodel_init();
    319 	sysctl_init();
    320 
    321 	rnd_init();
    322 	cprng_init();
    323 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
    324 	    CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    325 	rump_hyperentropy_init();
    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_biglock_init();
    339 
    340 	rump_scheduler_init(numcpu);
    341 	/* revert temporary context and schedule a semireal context */
    342 	rump_lwproc_curlwp_clear(l);
    343 	initproc = &proc0; /* borrow proc0 before we get initproc started */
    344 	rump_schedule();
    345 	bootlwp = curlwp;
    346 
    347 	percpu_init();
    348 	inittimecounter();
    349 	ntp_init();
    350 
    351 #ifdef KTRACE
    352 	ktrinit();
    353 #endif
    354 
    355 	ts = boottime;
    356 	tc_setclock(&ts);
    357 
    358 	extern krwlock_t exec_lock;
    359 	rw_init(&exec_lock);
    360 
    361 	/* we are mostly go.  do per-cpu subsystem init */
    362 	for (i = 0; i < numcpu; i++) {
    363 		struct cpu_info *ci = cpu_lookup(i);
    364 
    365 		/* attach non-bootstrap CPUs */
    366 		if (i > 0) {
    367 			rump_cpu_attach(ci);
    368 			ncpu++;
    369 		}
    370 
    371 		callout_init_cpu(ci);
    372 		softint_init(ci);
    373 		xc_init_cpu(ci);
    374 		pool_cache_cpu_init(ci);
    375 		selsysinit(ci);
    376 		percpu_init_cpu(ci);
    377 
    378 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
    379 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
    380 
    381 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
    382 	}
    383 
    384 	/* CPUs are up.  allow kernel threads to run */
    385 	rump_thread_allow(NULL);
    386 
    387 	rnd_init_softint();
    388 
    389 	mksysctls();
    390 	kqueue_init();
    391 	iostat_init();
    392 	fd_sys_init();
    393 	module_init();
    394 	devsw_init();
    395 	pipe_init();
    396 	resource_init();
    397 	procinit_sysctl();
    398 	time_init();
    399 	time_init2();
    400 
    401 	/* start page baroness */
    402 	if (rump_threads) {
    403 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    404 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    405 			panic("pagedaemon create failed");
    406 	} else
    407 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    408 
    409 	/* process dso's */
    410 	rumpuser_dl_bootstrap(add_linkedin_modules,
    411 	    rump_kernelfsym_load, rump_component_load);
    412 
    413 	rump_component_addlocal();
    414 	rump_component_init(RUMP_COMPONENT_KERN);
    415 
    416 	/* initialize factions, if present */
    417 	rump_component_init(RUMP__FACTION_VFS);
    418 	/* pnbuf_cache is used even without vfs */
    419 	if (rump_component_count(RUMP__FACTION_VFS) == 0) {
    420 		pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    421 		    NULL, IPL_NONE, NULL, NULL, NULL);
    422 	}
    423 	rump_component_init(RUMP__FACTION_NET);
    424 	rump_component_init(RUMP__FACTION_DEV);
    425 	KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
    426 	    && rump_component_count(RUMP__FACTION_NET) <= 1
    427 	    && rump_component_count(RUMP__FACTION_DEV) <= 1);
    428 
    429 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
    430 
    431 	/*
    432 	 * if we initialized the tty component above, the tyttymtx is
    433 	 * now initialized.  otherwise, we need to initialize it.
    434 	 */
    435 	if (!rump_ttycomponent)
    436 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
    437 
    438 	cold = 0;
    439 
    440 	/* aieeeedondest */
    441 	if (rump_threads) {
    442 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    443 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    444 			panic("aiodoned");
    445 	}
    446 
    447 	sysctl_finalize();
    448 
    449 	module_init_class(MODULE_CLASS_ANY);
    450 
    451 	if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
    452 	    hostname, MAXHOSTNAMELEN) != 0) {
    453 		panic("mandatory hypervisor configuration (HOSTNAME) missing");
    454 	}
    455 	hostnamelen = strlen(hostname);
    456 
    457 	sigemptyset(&sigcantmask);
    458 
    459 	if (rump_threads)
    460 		vmem_rehash_start();
    461 
    462 	/*
    463 	 * Create init (proc 1), used to attach implicit threads in rump.
    464 	 * (note: must be done after vfsinit to get cwdi)
    465 	 */
    466 	initlwp = rump__lwproc_alloclwp(NULL);
    467 	mutex_enter(proc_lock);
    468 	initproc = proc_find_raw(1);
    469 	mutex_exit(proc_lock);
    470 	if (initproc == NULL)
    471 		panic("where in the world is initproc?");
    472 
    473 	/*
    474 	 * Adjust syscall vector in case factions were dlopen()'d
    475 	 * before calling rump_init().
    476 	 * (modules will handle dynamic syscalls the usual way)
    477 	 *
    478 	 * Note: this will adjust the function vectors of
    479 	 * syscalls which use a funcalias (getpid etc.), but
    480 	 * it makes no difference.
    481 	 */
    482 	for (i = 0; i < SYS_NSYSENT; i++) {
    483 		void *sym;
    484 
    485 		if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
    486 		    *syscallnames[i] == '#' ||
    487 		    rump_sysent[i].sy_call == sys_nomodule)
    488 			continue;
    489 
    490 		/*
    491 		 * deal with compat wrappers.  makesyscalls.sh should
    492 		 * generate the necessary info instead of this hack,
    493 		 * though.  ugly, fix it later.
    494 		 */
    495 #define CPFX "compat_"
    496 #define CPFXLEN (sizeof(CPFX)-1)
    497 		if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
    498 			const char *p = syscallnames[i] + CPFXLEN;
    499 			size_t namelen;
    500 
    501 			/* skip version number */
    502 			while (*p >= '0' && *p <= '9')
    503 				p++;
    504 			if (p == syscallnames[i] + CPFXLEN || *p != '_')
    505 				panic("invalid syscall name %s\n",
    506 				    syscallnames[i]);
    507 
    508 			/* skip over the next underscore */
    509 			p++;
    510 			namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
    511 
    512 			strcpy(buf, "rumpns_");
    513 			strcat(buf, syscallnames[i]);
    514 			/* XXX: no strncat in the kernel */
    515 			strcpy(buf+namelen, "sys_");
    516 			strcat(buf, p);
    517 #undef CPFX
    518 #undef CPFXLEN
    519 		} else {
    520 			snprintf(buf, sizeof(buf), "rumpns_sys_%s",
    521 			    syscallnames[i]);
    522 		}
    523 		if ((sym = rumpuser_dl_globalsym(buf)) != NULL
    524 		    && sym != rump_sysent[i].sy_call) {
    525 #if 0
    526 			rumpuser_dprintf("adjusting %s: %p (old %p)\n",
    527 			    syscallnames[i], sym, rump_sysent[i].sy_call);
    528 #endif
    529 			rump_sysent[i].sy_call = sym;
    530 		}
    531 	}
    532 
    533 	rump_component_init(RUMP_COMPONENT_POSTINIT);
    534 
    535 	/* component inits done */
    536 	bootlwp = NULL;
    537 
    538 	/* open 0/1/2 for init */
    539 	KASSERT(rump_lwproc_curlwp() == NULL);
    540 	rump_lwproc_switch(initlwp);
    541 	rump_consdev_init();
    542 	rump_lwproc_switch(NULL);
    543 
    544 	/* release cpu */
    545 	rump_unschedule();
    546 
    547 	return 0;
    548 }
    549 /* historic compat */
    550 __strong_alias(rump__init,rump_init);
    551 
    552 int
    553 rump_init_server(const char *url)
    554 {
    555 
    556 	return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
    557 }
    558 
    559 void
    560 cpu_reboot(int howto, char *bootstr)
    561 {
    562 	int ruhow = 0;
    563 	void *finiarg;
    564 
    565 	printf("rump kernel halting...\n");
    566 
    567 	if (!RUMP_LOCALPROC_P(curproc))
    568 		finiarg = curproc->p_vmspace->vm_map.pmap;
    569 	else
    570 		finiarg = NULL;
    571 
    572 	/* dump means we really take the dive here */
    573 	if ((howto & RB_DUMP) || panicstr) {
    574 		ruhow = RUMPUSER_PANIC;
    575 		goto out;
    576 	}
    577 
    578 	/* try to sync */
    579 	if (!((howto & RB_NOSYNC) || panicstr)) {
    580 		if (rump_vfs_fini)
    581 			rump_vfs_fini();
    582 	}
    583 
    584 	doshutdownhooks();
    585 
    586 	/* your wish is my command */
    587 	if (howto & RB_HALT) {
    588 		printf("rump kernel halted\n");
    589 		rumpuser_sp_fini(finiarg);
    590 		for (;;) {
    591 			rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
    592 		}
    593 	}
    594 
    595 	/* this function is __dead, we must exit */
    596  out:
    597 	printf("halted\n");
    598 	rumpuser_sp_fini(finiarg);
    599 	rumpuser_exit(ruhow);
    600 }
    601 
    602 struct uio *
    603 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    604 {
    605 	struct uio *uio;
    606 	enum uio_rw uiorw;
    607 
    608 	switch (rw) {
    609 	case RUMPUIO_READ:
    610 		uiorw = UIO_READ;
    611 		break;
    612 	case RUMPUIO_WRITE:
    613 		uiorw = UIO_WRITE;
    614 		break;
    615 	default:
    616 		panic("%s: invalid rw %d", __func__, rw);
    617 	}
    618 
    619 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    620 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    621 
    622 	uio->uio_iov->iov_base = buf;
    623 	uio->uio_iov->iov_len = bufsize;
    624 
    625 	uio->uio_iovcnt = 1;
    626 	uio->uio_offset = offset;
    627 	uio->uio_resid = bufsize;
    628 	uio->uio_rw = uiorw;
    629 	UIO_SETUP_SYSSPACE(uio);
    630 
    631 	return uio;
    632 }
    633 
    634 size_t
    635 rump_uio_getresid(struct uio *uio)
    636 {
    637 
    638 	return uio->uio_resid;
    639 }
    640 
    641 off_t
    642 rump_uio_getoff(struct uio *uio)
    643 {
    644 
    645 	return uio->uio_offset;
    646 }
    647 
    648 size_t
    649 rump_uio_free(struct uio *uio)
    650 {
    651 	size_t resid;
    652 
    653 	resid = uio->uio_resid;
    654 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    655 	kmem_free(uio, sizeof(*uio));
    656 
    657 	return resid;
    658 }
    659 
    660 kauth_cred_t
    661 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    662 {
    663 	kauth_cred_t cred;
    664 	int rv;
    665 
    666 	cred = kauth_cred_alloc();
    667 	kauth_cred_setuid(cred, uid);
    668 	kauth_cred_seteuid(cred, uid);
    669 	kauth_cred_setsvuid(cred, uid);
    670 	kauth_cred_setgid(cred, gid);
    671 	kauth_cred_setgid(cred, gid);
    672 	kauth_cred_setegid(cred, gid);
    673 	kauth_cred_setsvgid(cred, gid);
    674 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    675 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    676 	assert(rv == 0);
    677 
    678 	return cred;
    679 }
    680 
    681 void
    682 rump_cred_put(kauth_cred_t cred)
    683 {
    684 
    685 	kauth_cred_free(cred);
    686 }
    687 
    688 static int compcounter[RUMP_COMPONENT_MAX];
    689 static int compinited[RUMP_COMPONENT_MAX];
    690 
    691 /*
    692  * Yea, this is O(n^2), but we're only looking at a handful of components.
    693  * Components are always initialized from the thread that called rump_init().
    694  * Could also free these when done with them, but prolly not worth it.
    695  */
    696 struct compstore {
    697 	const struct rump_component *cs_rc;
    698 	LIST_ENTRY(compstore) cs_entries;
    699 };
    700 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
    701 
    702 /*
    703  * add components which are visible from the current object.
    704  */
    705 static void
    706 rump_component_addlocal(void)
    707 {
    708 	__link_set_decl(rump_components, struct rump_component);
    709 	struct rump_component *const *rc;
    710 
    711 	__link_set_foreach(rc, rump_components) {
    712 		rump_component_load(*rc);
    713 	}
    714 }
    715 
    716 static void
    717 rump_component_load(const struct rump_component *rc)
    718 {
    719 	struct compstore *cs;
    720 
    721 	KASSERT(curlwp == bootlwp);
    722 
    723 	LIST_FOREACH(cs, &cshead, cs_entries) {
    724 		if (rc == cs->cs_rc)
    725 			return;
    726 	}
    727 
    728 	cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
    729 	cs->cs_rc = rc;
    730 	LIST_INSERT_HEAD(&cshead, cs, cs_entries);
    731 	KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
    732 	compcounter[rc->rc_type]++;
    733 }
    734 
    735 int
    736 rump_component_count(enum rump_component_type type)
    737 {
    738 
    739 	KASSERT(curlwp == bootlwp);
    740 	KASSERT(type < RUMP_COMPONENT_MAX);
    741 	return compcounter[type];
    742 }
    743 
    744 void
    745 rump_component_init(enum rump_component_type type)
    746 {
    747 	struct compstore *cs;
    748 	const struct rump_component *rc;
    749 
    750 	KASSERT(curlwp == bootlwp);
    751 	KASSERT(!compinited[type]);
    752 	LIST_FOREACH(cs, &cshead, cs_entries) {
    753 		rc = cs->cs_rc;
    754 		if (rc->rc_type == type)
    755 			rc->rc_init();
    756 	}
    757 	compinited[type] = 1;
    758 }
    759 
    760 /*
    761  * Initialize a module which has already been loaded and linked
    762  * with dlopen(). This is fundamentally the same as a builtin module.
    763  */
    764 int
    765 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    766 {
    767 
    768 	return module_builtin_add(mip, nmodinfo, true);
    769 }
    770 
    771 /*
    772  * Finish module (flawless victory, fatality!).
    773  */
    774 int
    775 rump_module_fini(const struct modinfo *mi)
    776 {
    777 
    778 	return module_builtin_remove(mi, true);
    779 }
    780 
    781 /*
    782  * Add loaded and linked module to the builtin list.  It will
    783  * later be initialized with module_init_class().
    784  */
    785 
    786 static void
    787 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    788 {
    789 
    790 	module_builtin_add(mip, nmodinfo, false);
    791 }
    792 
    793 int
    794 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    795 	char *strtab, uint64_t strsize)
    796 {
    797 	static int inited = 0;
    798 	Elf64_Ehdr ehdr;
    799 
    800 	if (inited)
    801 		return EBUSY;
    802 	inited = 1;
    803 
    804 	/*
    805 	 * Use 64bit header since it's bigger.  Shouldn't make a
    806 	 * difference, since we're passing in all zeroes anyway.
    807 	 */
    808 	memset(&ehdr, 0, sizeof(ehdr));
    809 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    810 
    811 	return 0;
    812 }
    813 
    814 static int
    815 rump_hyp_syscall(int num, void *arg, long *retval)
    816 {
    817 	register_t regrv[2] = {0, 0};
    818 	struct lwp *l;
    819 	struct sysent *callp;
    820 	int rv;
    821 
    822 	if (__predict_false(num >= SYS_NSYSENT))
    823 		return ENOSYS;
    824 
    825 	/* XXX: always uses native syscall vector */
    826 	callp = rump_sysent + num;
    827 	l = curlwp;
    828 	rv = sy_invoke(callp, l, (void *)arg, regrv, num);
    829 	retval[0] = regrv[0];
    830 	retval[1] = regrv[1];
    831 
    832 	return rv;
    833 }
    834 
    835 static int
    836 rump_hyp_rfork(void *priv, int flags, const char *comm)
    837 {
    838 	struct vmspace *newspace;
    839 	struct proc *p;
    840 	int error;
    841 
    842 	if ((error = rump_lwproc_rfork(flags)) != 0)
    843 		return error;
    844 
    845 	/*
    846 	 * Since it's a proxy proc, adjust the vmspace.
    847 	 * Refcount will eternally be 1.
    848 	 */
    849 	p = curproc;
    850 	newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
    851 	newspace->vm_refcnt = 1;
    852 	newspace->vm_map.pmap = priv;
    853 	KASSERT(p->p_vmspace == vmspace_kernel());
    854 	p->p_vmspace = newspace;
    855 	if (comm)
    856 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    857 
    858 	return 0;
    859 }
    860 
    861 /*
    862  * Order all lwps in a process to exit.  does *not* wait for them to drain.
    863  */
    864 static void
    865 rump_hyp_lwpexit(void)
    866 {
    867 	struct proc *p = curproc;
    868 	uint64_t where;
    869 	struct lwp *l;
    870 
    871 	mutex_enter(p->p_lock);
    872 	/*
    873 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
    874 	 * so that they know they should exit.
    875 	 */
    876 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    877 		if (l == curlwp)
    878 			continue;
    879 		l->l_flag |= LW_RUMP_QEXIT;
    880 	}
    881 	mutex_exit(p->p_lock);
    882 
    883 	/*
    884 	 * Next, make sure everyone on all CPUs sees our status
    885 	 * update.  This keeps threads inside cv_wait() and makes
    886 	 * sure we don't access a stale cv pointer later when
    887 	 * we wake up the threads.
    888 	 */
    889 
    890 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    891 	xc_wait(where);
    892 
    893 	/*
    894 	 * Ok, all lwps are either:
    895 	 *  1) not in the cv code
    896 	 *  2) sleeping on l->l_private
    897 	 *  3) sleeping on p->p_waitcv
    898 	 *
    899 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
    900 	 * in p->p_sflag.
    901 	 */
    902 
    903 	mutex_enter(p->p_lock);
    904 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    905 		if (l->l_private)
    906 			cv_broadcast(l->l_private);
    907 	}
    908 	p->p_sflag |= PS_RUMP_LWPEXIT;
    909 	cv_broadcast(&p->p_waitcv);
    910 	mutex_exit(p->p_lock);
    911 }
    912 
    913 /*
    914  * Notify process that all threads have been drained and exec is complete.
    915  */
    916 static void
    917 rump_hyp_execnotify(const char *comm)
    918 {
    919 	struct proc *p = curproc;
    920 
    921 	fd_closeexec();
    922 	mutex_enter(p->p_lock);
    923 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
    924 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
    925 	mutex_exit(p->p_lock);
    926 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    927 }
    928 
    929 int
    930 rump_boot_gethowto()
    931 {
    932 
    933 	return boothowto;
    934 }
    935 
    936 void
    937 rump_boot_sethowto(int howto)
    938 {
    939 
    940 	boothowto = howto;
    941 }
    942 
    943 int
    944 rump_getversion(void)
    945 {
    946 
    947 	return __NetBSD_Version__;
    948 }
    949 /* compat */
    950 __strong_alias(rump_pub_getversion,rump_getversion);
    951 
    952 int
    953 rump_nativeabi_p(void)
    954 {
    955 
    956 #ifdef _RUMP_NATIVE_ABI
    957 	return 1;
    958 #else
    959 	return 0;
    960 #endif
    961 }
    962 
    963 /*
    964  * Note: may be called unscheduled.  Not fully safe since no locking
    965  * of allevents (currently that's not even available).
    966  */
    967 void
    968 rump_printevcnts()
    969 {
    970 	struct evcnt *ev;
    971 
    972 	TAILQ_FOREACH(ev, &allevents, ev_list)
    973 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    974 		    ev->ev_group, ev->ev_name, ev->ev_count);
    975 }
    976 
    977 /*
    978  * If you use this interface ... well ... all bets are off.
    979  * The original purpose is for the p2k fs server library to be
    980  * able to use the same pid/lid for VOPs as the host kernel.
    981  */
    982 void
    983 rump_allbetsareoff_setid(pid_t pid, int lid)
    984 {
    985 	struct lwp *l = curlwp;
    986 	struct proc *p = l->l_proc;
    987 
    988 	l->l_lid = lid;
    989 	p->p_pid = pid;
    990 }
    991 
    992 #include <sys/pserialize.h>
    993 
    994 static void
    995 ipiemu(void *a1, void *a2)
    996 {
    997 
    998 	xc__highpri_intr(NULL);
    999 	pserialize_switchpoint();
   1000 }
   1001 
   1002 void
   1003 rump_xc_highpri(struct cpu_info *ci)
   1004 {
   1005 
   1006 	if (ci)
   1007 		xc_unicast(0, ipiemu, NULL, NULL, ci);
   1008 	else
   1009 		xc_broadcast(0, ipiemu, NULL, NULL);
   1010 }
   1011 
   1012 int
   1013 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
   1014 {
   1015 	struct proc *p;
   1016 	struct emul *e;
   1017 	struct sysent *callp;
   1018 	const int *etrans = NULL;
   1019 	int rv;
   1020 
   1021 	rump_schedule();
   1022 	p = curproc;
   1023 	e = p->p_emul;
   1024 #ifndef __HAVE_MINIMAL_EMUL
   1025 	KASSERT(num > 0 && num < e->e_nsysent);
   1026 #endif
   1027 	callp = e->e_sysent + num;
   1028 
   1029 	rv = sy_invoke(callp, curlwp, data, retval, num);
   1030 
   1031 	/*
   1032 	 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
   1033 	 * an invariant ...
   1034 	 */
   1035 #if !defined(__HAVE_MINIMAL_EMUL)
   1036 	etrans = e->e_errno;
   1037 #elif defined(__HAVE_SYSCALL_INTERN)
   1038 	etrans = p->p_emuldata;
   1039 #endif
   1040 
   1041 	if (etrans) {
   1042 		rv = etrans[rv];
   1043 		/*
   1044 		 * XXX: small hack since Linux etrans vectors on some
   1045 		 * archs contain negative errnos, but rump_syscalls
   1046 		 * uses the -1 + errno ABI.  Note that these
   1047 		 * negative values are always the result of translation,
   1048 		 * otherwise the above translation method would not
   1049 		 * work very well.
   1050 		 */
   1051 		if (rv < 0)
   1052 			rv = -rv;
   1053 	}
   1054 	rump_unschedule();
   1055 
   1056 	return rv;
   1057 }
   1058 
   1059 /*
   1060  * Temporary notification that rumpkern_time is obsolete.  This is to
   1061  * be removed along with obsoleting rumpkern_time in a few months.
   1062  */
   1063 #define RUMPKERN_TIME_WARN "rumpkern_time is obsolete, functionality in librump"
   1064 __warn_references(rumpkern_time_is_obsolete,RUMPKERN_TIME_WARN)
   1065 void rumpkern_time_is_obsolete(void);
   1066 void
   1067 rumpkern_time_is_obsolete(void)
   1068 {
   1069 	printf("WARNING: %s\n", RUMPKERN_TIME_WARN);
   1070 }
   1071