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