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