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