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