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