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