Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.331
      1 /*	$NetBSD: rump.c,v 1.331 2018/01/09 04:55:43 msaitoh 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.331 2018/01/09 04:55:43 msaitoh 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/rnd.h>
     75 #include <sys/ktrace.h>
     76 
     77 #include <rump-sys/kern.h>
     78 #include <rump-sys/dev.h>
     79 #include <rump-sys/net.h>
     80 #include <rump-sys/vfs.h>
     81 
     82 #include <rump/rumpuser.h>
     83 
     84 #include <secmodel/suser/suser.h>
     85 
     86 #include <prop/proplib.h>
     87 
     88 #include <uvm/uvm_extern.h>
     89 #include <uvm/uvm_readahead.h>
     90 
     91 char machine[] = MACHINE;
     92 char machine_arch[] = MACHINE_ARCH;
     93 
     94 struct proc *initproc;
     95 
     96 struct device rump_rootdev = {
     97 	.dv_class = DV_VIRTUAL
     98 };
     99 
    100 #ifdef RUMP_WITHOUT_THREADS
    101 int rump_threads = 0;
    102 #else
    103 int rump_threads = 1;
    104 #endif
    105 
    106 static void rump_component_addlocal(void);
    107 static struct lwp *bootlwp;
    108 
    109 /* 16k should be enough for std rump needs */
    110 static  char rump_msgbuf[16*1024] __aligned(256);
    111 
    112 bool rump_ttycomponent = false;
    113 
    114 static void
    115 rump_aiodone_worker(struct work *wk, void *dummy)
    116 {
    117 	struct buf *bp = (struct buf *)wk;
    118 
    119 	KASSERT(&bp->b_work == wk);
    120 	bp->b_iodone(bp);
    121 }
    122 
    123 static int rump_inited;
    124 
    125 void (*rump_vfs_drainbufs)(int) = (void *)nullop;
    126 int  (*rump_vfs_makeonedevnode)(dev_t, const char *,
    127 				devmajor_t, devminor_t) = (void *)nullop;
    128 int  (*rump_vfs_makedevnodes)(dev_t, const char *, char,
    129 			      devmajor_t, devminor_t, int) = (void *)nullop;
    130 int  (*rump_vfs_makesymlink)(const char *, const char *) = (void *)nullop;
    131 
    132 rump_proc_vfs_init_fn rump_proc_vfs_init = (void *)nullop;
    133 rump_proc_vfs_release_fn rump_proc_vfs_release = (void *)nullop;
    134 
    135 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    136 
    137 static pid_t rspo_wrap_getpid(void) {
    138 	return rump_sysproxy_hyp_getpid();
    139 }
    140 static int rspo_wrap_syscall(int num, void *arg, long *retval) {
    141 	return rump_sysproxy_hyp_syscall(num, arg, retval);
    142 }
    143 static int rspo_wrap_rfork(void *priv, int flag, const char *comm) {
    144 	return rump_sysproxy_hyp_rfork(priv, flag, comm);
    145 }
    146 static void rspo_wrap_lwpexit(void) {
    147 	rump_sysproxy_hyp_lwpexit();
    148 }
    149 static void rspo_wrap_execnotify(const char *comm) {
    150 	rump_sysproxy_hyp_execnotify(comm);
    151 }
    152 static const struct rumpuser_hyperup hyp = {
    153 	.hyp_schedule		= rump_schedule,
    154 	.hyp_unschedule		= rump_unschedule,
    155 	.hyp_backend_unschedule	= rump_user_unschedule,
    156 	.hyp_backend_schedule	= rump_user_schedule,
    157 	.hyp_lwproc_switch	= rump_lwproc_switch,
    158 	.hyp_lwproc_release	= rump_lwproc_releaselwp,
    159 	.hyp_lwproc_newlwp	= rump_lwproc_newlwp,
    160 	.hyp_lwproc_curlwp	= rump_lwproc_curlwp,
    161 
    162 	.hyp_getpid		= rspo_wrap_getpid,
    163 	.hyp_syscall		= rspo_wrap_syscall,
    164 	.hyp_lwproc_rfork	= rspo_wrap_rfork,
    165 	.hyp_lwpexit		= rspo_wrap_lwpexit,
    166 	.hyp_execnotify		= rspo_wrap_execnotify,
    167 };
    168 struct rump_sysproxy_ops rump_sysproxy_ops = {
    169 	.rspo_copyin		= (void *)enxio,
    170 	.rspo_copyinstr 	= (void *)enxio,
    171 	.rspo_copyout	 	= (void *)enxio,
    172 	.rspo_copyoutstr 	= (void *)enxio,
    173 	.rspo_anonmmap 		= (void *)enxio,
    174 	.rspo_raise 		= (void *)enxio,
    175 	.rspo_fini 		= (void *)enxio,
    176 	.rspo_hyp_getpid 	= (void *)enxio,
    177 	.rspo_hyp_syscall 	= (void *)enxio,
    178 	.rspo_hyp_rfork 	= (void *)enxio,
    179 	.rspo_hyp_lwpexit 	= (void *)enxio,
    180 	.rspo_hyp_execnotify 	= (void *)enxio,
    181 };
    182 
    183 int
    184 rump_daemonize_begin(void)
    185 {
    186 
    187 	if (rump_inited)
    188 		return EALREADY;
    189 
    190 	return rumpuser_daemonize_begin();
    191 }
    192 
    193 int
    194 rump_daemonize_done(int error)
    195 {
    196 
    197 	return rumpuser_daemonize_done(error);
    198 }
    199 
    200 #ifdef RUMP_USE_CTOR
    201 
    202 /* sysctl bootstrap handling */
    203 struct sysctl_boot_chain sysctl_boot_chain \
    204     = LIST_HEAD_INITIALIZER(sysctl_boot_chain);
    205 __link_set_add_text(sysctl_funcs,voidop); /* ensure linkset is non-empty */
    206 
    207 #else /* RUMP_USE_CTOR */
    208 
    209 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
    210 {
    211 	__link_set_decl(rump_components, struct rump_component);
    212 
    213 	/*
    214 	 * Trick compiler into generating references so that statically
    215 	 * linked rump kernels are generated with the link set symbols.
    216 	 */
    217 	asm("" :: "r"(__start_link_set_rump_components));
    218 	asm("" :: "r"(__stop_link_set_rump_components));
    219 }
    220 
    221 #endif /* RUMP_USE_CTOR */
    222 
    223 int
    224 rump_init(void)
    225 {
    226 	char buf[256];
    227 	struct timespec ts;
    228 	int64_t sec;
    229 	long nsec;
    230 	struct lwp *l, *initlwp;
    231 	int i, numcpu;
    232 
    233 	/* not reentrant */
    234 	if (rump_inited)
    235 		return 0;
    236 	else if (rump_inited == -1)
    237 		panic("rump_init: host process restart required");
    238 	else
    239 		rump_inited = 1;
    240 
    241 	/* initialize hypervisor */
    242 	if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
    243 		rumpuser_dprintf("rumpuser init failed\n");
    244 		return EINVAL;
    245 	}
    246 
    247 	/* init minimal lwp/cpu context */
    248 	rump_lwproc_init();
    249 	l = &lwp0;
    250 	l->l_cpu = l->l_target_cpu = &rump_bootcpu;
    251 	rump_lwproc_curlwp_set(l);
    252 
    253 	/* retrieve env vars which affect the early stage of bootstrap */
    254 	if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
    255 		rump_threads = *buf != '0';
    256 	}
    257 	if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
    258 		if (*buf != '0')
    259 			boothowto = AB_VERBOSE;
    260 	}
    261 
    262 	if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
    263 		panic("mandatory hypervisor configuration (NCPU) missing");
    264 	numcpu = strtoll(buf, NULL, 10);
    265 	if (numcpu < 1) {
    266 		panic("rump kernels are not lightweight enough for \"%d\" CPUs",
    267 		    numcpu);
    268 	}
    269 
    270 	rump_thread_init();
    271 	rump_cpus_bootstrap(&numcpu);
    272 
    273 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
    274 	boottime.tv_sec = sec;
    275 	boottime.tv_nsec = nsec;
    276 
    277 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    278 	aprint_verbose("%s%s", copyright, version);
    279 
    280 	rump_intr_init(numcpu);
    281 
    282 	rump_tsleep_init();
    283 
    284 	rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
    285 	ksyms_init();
    286 	uvm_init();
    287 	evcnt_init();
    288 
    289 	kcpuset_sysinit();
    290 	once_init();
    291 	kernconfig_lock_init();
    292 	prop_kern_init();
    293 
    294 	kmem_init();
    295 
    296 	uvm_ra_init();
    297 	uao_init();
    298 
    299 	mutex_obj_init();
    300 	rw_obj_init();
    301 	callout_startup();
    302 
    303 	kprintf_init();
    304 	percpu_init();
    305 	pserialize_init();
    306 
    307 	kauth_init();
    308 
    309 	secmodel_init();
    310 	sysctl_init();
    311 	/*
    312 	 * The above call to sysctl_init() only initializes sysctl nodes
    313 	 * from link sets.  Initialize sysctls in case we used ctors.
    314 	 */
    315 #ifdef RUMP_USE_CTOR
    316 	{
    317 		struct sysctl_setup_chain *ssc;
    318 
    319 		while ((ssc = LIST_FIRST(&sysctl_boot_chain)) != NULL) {
    320 			LIST_REMOVE(ssc, ssc_entries);
    321 			ssc->ssc_func(NULL);
    322 		}
    323 	}
    324 #endif /* RUMP_USE_CTOR */
    325 
    326 	rnd_init();
    327 	cprng_init();
    328 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
    329 	    CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    330 
    331 	rump_hyperentropy_init();
    332 
    333 	procinit();
    334 	proc0_init();
    335 	uid_init();
    336 	chgproccnt(0, 1);
    337 
    338 	l->l_proc = &proc0;
    339 	lwp_update_creds(l);
    340 
    341 	lwpinit_specificdata();
    342 	lwp_initspecific(&lwp0);
    343 
    344 	loginit();
    345 
    346 	rump_biglock_init();
    347 
    348 	rump_scheduler_init(numcpu);
    349 	/* revert temporary context and schedule a semireal context */
    350 	rump_lwproc_curlwp_clear(l);
    351 	initproc = &proc0; /* borrow proc0 before we get initproc started */
    352 	rump_schedule();
    353 	bootlwp = curlwp;
    354 
    355 	inittimecounter();
    356 	ntp_init();
    357 
    358 #ifdef KTRACE
    359 	ktrinit();
    360 #endif
    361 
    362 	ts = boottime;
    363 	tc_setclock(&ts);
    364 
    365 	extern krwlock_t exec_lock;
    366 	rw_init(&exec_lock);
    367 
    368 	/* we are mostly go.  do per-cpu subsystem init */
    369 	for (i = 0; i < numcpu; i++) {
    370 		struct cpu_info *ci = cpu_lookup(i);
    371 
    372 		/* attach non-bootstrap CPUs */
    373 		if (i > 0) {
    374 			rump_cpu_attach(ci);
    375 			ncpu++;
    376 		}
    377 
    378 		callout_init_cpu(ci);
    379 		softint_init(ci);
    380 		xc_init_cpu(ci);
    381 		pool_cache_cpu_init(ci);
    382 		selsysinit(ci);
    383 		percpu_init_cpu(ci);
    384 
    385 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
    386 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
    387 
    388 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
    389 	}
    390 	ncpuonline = ncpu;
    391 
    392 	/* Once all CPUs are detected, initialize the per-CPU cprng_fast.  */
    393 	cprng_fast_init();
    394 
    395 	mp_online = true;
    396 
    397 	/* CPUs are up.  allow kernel threads to run */
    398 	rump_thread_allow(NULL);
    399 
    400 	rnd_init_softint();
    401 
    402 	kqueue_init();
    403 	iostat_init();
    404 	fd_sys_init();
    405 	module_init();
    406 	devsw_init();
    407 	pipe_init();
    408 	resource_init();
    409 	procinit_sysctl();
    410 	time_init();
    411 	time_init2();
    412 
    413 	/* start page baroness */
    414 	if (rump_threads) {
    415 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    416 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    417 			panic("pagedaemon create failed");
    418 	} else
    419 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    420 
    421 	/* process dso's */
    422 	rumpuser_dl_bootstrap(add_linkedin_modules,
    423 	    rump_kernelfsym_load, rump_component_load);
    424 
    425 	rump_component_addlocal();
    426 	rump_component_init(RUMP_COMPONENT_KERN);
    427 
    428 	/* initialize factions, if present */
    429 	rump_component_init(RUMP__FACTION_VFS);
    430 	/* pnbuf_cache is used even without vfs */
    431 	if (rump_component_count(RUMP__FACTION_VFS) == 0) {
    432 		pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    433 		    NULL, IPL_NONE, NULL, NULL, NULL);
    434 	}
    435 	rump_component_init(RUMP__FACTION_NET);
    436 	rump_component_init(RUMP__FACTION_DEV);
    437 	KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
    438 	    && rump_component_count(RUMP__FACTION_NET) <= 1
    439 	    && rump_component_count(RUMP__FACTION_DEV) <= 1);
    440 
    441 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
    442 
    443 	/*
    444 	 * if we initialized the tty component above, the tyttymtx is
    445 	 * now initialized.  otherwise, we need to initialize it.
    446 	 */
    447 	if (!rump_ttycomponent)
    448 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
    449 
    450 	cold = 0;
    451 
    452 	/* aieeeedondest */
    453 	if (rump_threads) {
    454 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    455 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    456 			panic("aiodoned");
    457 	}
    458 
    459 	sysctl_finalize();
    460 
    461 	module_init_class(MODULE_CLASS_ANY);
    462 
    463 	if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
    464 	    hostname, MAXHOSTNAMELEN) != 0) {
    465 		panic("mandatory hypervisor configuration (HOSTNAME) missing");
    466 	}
    467 	hostnamelen = strlen(hostname);
    468 
    469 	sigemptyset(&sigcantmask);
    470 
    471 	if (rump_threads)
    472 		vmem_rehash_start();
    473 
    474 	/*
    475 	 * Create init (proc 1), used to attach implicit threads in rump.
    476 	 * (note: must be done after vfsinit to get cwdi)
    477 	 */
    478 	initlwp = rump__lwproc_alloclwp(NULL);
    479 	mutex_enter(proc_lock);
    480 	initproc = proc_find_raw(1);
    481 	mutex_exit(proc_lock);
    482 	if (initproc == NULL)
    483 		panic("where in the world is initproc?");
    484 	strlcpy(initproc->p_comm, "rumplocal", sizeof(initproc->p_comm));
    485 
    486 	rump_component_init(RUMP_COMPONENT_POSTINIT);
    487 
    488 	/* load syscalls */
    489 	rump_component_init(RUMP_COMPONENT_SYSCALL);
    490 
    491 	/* component inits done */
    492 	bootlwp = NULL;
    493 
    494 	/* open 0/1/2 for init */
    495 	KASSERT(rump_lwproc_curlwp() == NULL);
    496 	rump_lwproc_switch(initlwp);
    497 	rump_consdev_init();
    498 	rump_lwproc_switch(NULL);
    499 
    500 	/* release cpu */
    501 	rump_unschedule();
    502 
    503 	return 0;
    504 }
    505 /* historic compat */
    506 __strong_alias(rump__init,rump_init);
    507 
    508 static int compcounter[RUMP_COMPONENT_MAX];
    509 static int compinited[RUMP_COMPONENT_MAX];
    510 
    511 /*
    512  * Yea, this is O(n^2), but we're only looking at a handful of components.
    513  * Components are always initialized from the thread that called rump_init().
    514  */
    515 static LIST_HEAD(, rump_component) rchead = LIST_HEAD_INITIALIZER(rchead);
    516 
    517 #ifdef RUMP_USE_CTOR
    518 struct modinfo_boot_chain modinfo_boot_chain \
    519     = LIST_HEAD_INITIALIZER(modinfo_boot_chain);
    520 
    521 static void
    522 rump_component_addlocal(void)
    523 {
    524 	struct modinfo_chain *mc;
    525 
    526 	while ((mc = LIST_FIRST(&modinfo_boot_chain)) != NULL) {
    527 		LIST_REMOVE(mc, mc_entries);
    528 		module_builtin_add(&mc->mc_info, 1, false);
    529 	}
    530 }
    531 
    532 #else /* RUMP_USE_CTOR */
    533 
    534 static void
    535 rump_component_addlocal(void)
    536 {
    537 	__link_set_decl(rump_components, struct rump_component);
    538 	struct rump_component *const *rc;
    539 
    540 	__link_set_foreach(rc, rump_components) {
    541 		rump_component_load(*rc);
    542 	}
    543 }
    544 #endif /* RUMP_USE_CTOR */
    545 
    546 void
    547 rump_component_load(const struct rump_component *rc_const)
    548 {
    549 	struct rump_component *rc, *rc_iter;
    550 
    551 	/* time for rump component loading and unloading has passed */
    552 	if (!cold)
    553 		return;
    554 
    555 	/*
    556 	 * XXX: this is ok since the "const" was removed from the
    557 	 * definition of RUMP_COMPONENT().
    558 	 *
    559 	 * However, to preserve the hypercall interface, the const
    560 	 * remains here.  This can be fixed in the next hypercall revision.
    561 	 */
    562 	rc = __UNCONST(rc_const);
    563 
    564 	KASSERT(!rump_inited || curlwp == bootlwp);
    565 
    566 	LIST_FOREACH(rc_iter, &rchead, rc_entries) {
    567 		if (rc_iter == rc)
    568 			return;
    569 	}
    570 
    571 	LIST_INSERT_HEAD(&rchead, rc, rc_entries);
    572 	KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
    573 	compcounter[rc->rc_type]++;
    574 }
    575 
    576 void
    577 rump_component_unload(struct rump_component *rc)
    578 {
    579 
    580 	/*
    581 	 * Checking for cold is enough because rump_init() both
    582 	 * flips it and handles component loading.
    583 	 */
    584 	if (!cold)
    585 		return;
    586 
    587 	LIST_REMOVE(rc, rc_entries);
    588 }
    589 
    590 int
    591 rump_component_count(enum rump_component_type type)
    592 {
    593 
    594 	KASSERT(curlwp == bootlwp);
    595 	KASSERT(type < RUMP_COMPONENT_MAX);
    596 	return compcounter[type];
    597 }
    598 
    599 void
    600 rump_component_init(enum rump_component_type type)
    601 {
    602 	const struct rump_component *rc, *rc_safe;
    603 
    604 	KASSERT(curlwp == bootlwp);
    605 	KASSERT(!compinited[type]);
    606 	LIST_FOREACH_SAFE(rc, &rchead, rc_entries, rc_safe) {
    607 		if (rc->rc_type == type) {
    608 			rc->rc_init();
    609 			LIST_REMOVE(rc, rc_entries);
    610 		}
    611 	}
    612 	compinited[type] = 1;
    613 }
    614 
    615 /*
    616  * Initialize a module which has already been loaded and linked
    617  * with dlopen(). This is fundamentally the same as a builtin module.
    618  *
    619  * XXX: this interface does not really work in the RUMP_USE_CTOR case,
    620  * but I'm not sure it's anything to cry about.  In feeling blue,
    621  * things could somehow be handled via modinfo_boot_chain.
    622  */
    623 int
    624 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    625 {
    626 
    627 	return module_builtin_add(mip, nmodinfo, true);
    628 }
    629 
    630 /*
    631  * Finish module (flawless victory, fatality!).
    632  */
    633 int
    634 rump_module_fini(const struct modinfo *mi)
    635 {
    636 
    637 	return module_builtin_remove(mi, true);
    638 }
    639 
    640 /*
    641  * Add loaded and linked module to the builtin list.  It will
    642  * later be initialized with module_init_class().
    643  */
    644 
    645 static void
    646 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    647 {
    648 
    649 	module_builtin_add(mip, nmodinfo, false);
    650 }
    651 
    652 int
    653 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    654 	char *strtab, uint64_t strsize)
    655 {
    656 	static int inited = 0;
    657 	Elf64_Ehdr ehdr;
    658 
    659 	if (inited)
    660 		return EBUSY;
    661 	inited = 1;
    662 
    663 	/*
    664 	 * Use 64bit header since it's bigger.  Shouldn't make a
    665 	 * difference, since we're passing in all zeroes anyway.
    666 	 */
    667 	memset(&ehdr, 0, sizeof(ehdr));
    668 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    669 
    670 	return 0;
    671 }
    672 
    673 int
    674 rump_boot_gethowto()
    675 {
    676 
    677 	return boothowto;
    678 }
    679 
    680 void
    681 rump_boot_sethowto(int howto)
    682 {
    683 
    684 	boothowto = howto;
    685 }
    686 
    687 int
    688 rump_getversion(void)
    689 {
    690 
    691 	return __NetBSD_Version__;
    692 }
    693 /* compat */
    694 __strong_alias(rump_pub_getversion,rump_getversion);
    695 
    696 /*
    697  * Note: may be called unscheduled.  Not fully safe since no locking
    698  * of allevents (currently that's not even available).
    699  */
    700 void
    701 rump_printevcnts()
    702 {
    703 	struct evcnt *ev;
    704 
    705 	TAILQ_FOREACH(ev, &allevents, ev_list)
    706 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    707 		    ev->ev_group, ev->ev_name, ev->ev_count);
    708 }
    709 
    710 /*
    711  * If you use this interface ... well ... all bets are off.
    712  * The original purpose is for the p2k fs server library to be
    713  * able to use the same pid/lid for VOPs as the host kernel.
    714  */
    715 void
    716 rump_allbetsareoff_setid(pid_t pid, int lid)
    717 {
    718 	struct lwp *l = curlwp;
    719 	struct proc *p = l->l_proc;
    720 
    721 	l->l_lid = lid;
    722 	p->p_pid = pid;
    723 }
    724 
    725 #include <sys/pserialize.h>
    726 
    727 static void
    728 ipiemu(void *a1, void *a2)
    729 {
    730 
    731 	xc__highpri_intr(NULL);
    732 	pserialize_switchpoint();
    733 }
    734 
    735 void
    736 rump_xc_highpri(struct cpu_info *ci)
    737 {
    738 
    739 	if (ci)
    740 		xc_unicast(0, ipiemu, NULL, NULL, ci);
    741 	else
    742 		xc_broadcast(0, ipiemu, NULL, NULL);
    743 }
    744 
    745 int
    746 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
    747 {
    748 	struct proc *p;
    749 	struct emul *e;
    750 	struct sysent *callp;
    751 	const int *etrans = NULL;
    752 	int rv;
    753 
    754 	rump_schedule();
    755 	p = curproc;
    756 	e = p->p_emul;
    757 #ifndef __HAVE_MINIMAL_EMUL
    758 	KASSERT(num > 0 && num < e->e_nsysent);
    759 #endif
    760 	callp = e->e_sysent + num;
    761 
    762 	rv = sy_invoke(callp, curlwp, data, retval, num);
    763 
    764 	/*
    765 	 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
    766 	 * an invariant ...
    767 	 */
    768 #if !defined(__HAVE_MINIMAL_EMUL)
    769 	etrans = e->e_errno;
    770 #elif defined(__HAVE_SYSCALL_INTERN)
    771 	etrans = p->p_emuldata;
    772 #endif
    773 
    774 	if (etrans) {
    775 		rv = etrans[rv];
    776 		/*
    777 		 * XXX: small hack since Linux etrans vectors on some
    778 		 * archs contain negative errnos, but rump_syscalls
    779 		 * uses the -1 + errno ABI.  Note that these
    780 		 * negative values are always the result of translation,
    781 		 * otherwise the above translation method would not
    782 		 * work very well.
    783 		 */
    784 		if (rv < 0)
    785 			rv = -rv;
    786 	}
    787 	rump_unschedule();
    788 
    789 	return rv;
    790 }
    791 
    792 void
    793 rump_syscall_boot_establish(const struct rump_onesyscall *calls, size_t ncall)
    794 {
    795 	struct sysent *callp;
    796 	size_t i;
    797 
    798 	for (i = 0; i < ncall; i++) {
    799 		callp = rump_sysent + calls[i].ros_num;
    800 		KASSERT(bootlwp != NULL
    801 		    && callp->sy_call == (sy_call_t *)enosys);
    802 		callp->sy_call = calls[i].ros_handler;
    803 	}
    804 }
    805 
    806 struct rump_boot_etfs *ebstart;
    807 void
    808 rump_boot_etfs_register(struct rump_boot_etfs *eb)
    809 {
    810 
    811 	/*
    812 	 * Could use atomics, but, since caller would need to synchronize
    813 	 * against calling rump_init() anyway, easier to just specify the
    814 	 * interface as "caller serializes".  This solve-by-specification
    815 	 * approach avoids the grey area of using atomics before rump_init()
    816 	 * runs.
    817 	 */
    818 	eb->_eb_next = ebstart;
    819 	eb->eb_status = -1;
    820 	ebstart = eb;
    821 }
    822