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