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