xref: /netbsd-src/sys/rump/librump/rumpkern/rump.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: rump.c,v 1.307 2014/06/29 11:36:52 justin Exp $	*/
2 
3 /*
4  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.307 2014/06/29 11:36:52 justin Exp $");
30 
31 #include <sys/systm.h>
32 #define ELFSIZE ARCH_ELFSIZE
33 
34 #include <sys/param.h>
35 #include <sys/atomic.h>
36 #include <sys/buf.h>
37 #include <sys/callout.h>
38 #include <sys/conf.h>
39 #include <sys/cpu.h>
40 #include <sys/device.h>
41 #include <sys/evcnt.h>
42 #include <sys/event.h>
43 #include <sys/exec_elf.h>
44 #include <sys/filedesc.h>
45 #include <sys/iostat.h>
46 #include <sys/kauth.h>
47 #include <sys/kcpuset.h>
48 #include <sys/kernel.h>
49 #include <sys/kmem.h>
50 #include <sys/kprintf.h>
51 #include <sys/kthread.h>
52 #include <sys/ksyms.h>
53 #include <sys/msgbuf.h>
54 #include <sys/module.h>
55 #include <sys/namei.h>
56 #include <sys/once.h>
57 #include <sys/percpu.h>
58 #include <sys/pipe.h>
59 #include <sys/pool.h>
60 #include <sys/pserialize.h>
61 #include <sys/queue.h>
62 #include <sys/reboot.h>
63 #include <sys/resourcevar.h>
64 #include <sys/select.h>
65 #include <sys/sysctl.h>
66 #include <sys/syscall.h>
67 #include <sys/syscallvar.h>
68 #include <sys/timetc.h>
69 #include <sys/tty.h>
70 #include <sys/uidinfo.h>
71 #include <sys/vmem.h>
72 #include <sys/xcall.h>
73 #include <sys/cprng.h>
74 #include <sys/ktrace.h>
75 
76 #include <rump/rumpuser.h>
77 
78 #include <secmodel/suser/suser.h>
79 
80 #include <prop/proplib.h>
81 
82 #include <uvm/uvm_extern.h>
83 #include <uvm/uvm_readahead.h>
84 
85 #include "rump_private.h"
86 #include "rump_net_private.h"
87 #include "rump_vfs_private.h"
88 #include "rump_dev_private.h"
89 
90 char machine[] = MACHINE;
91 
92 struct proc *initproc;
93 
94 struct device rump_rootdev = {
95 	.dv_class = DV_VIRTUAL
96 };
97 
98 #ifdef RUMP_WITHOUT_THREADS
99 int rump_threads = 0;
100 #else
101 int rump_threads = 1;
102 #endif
103 
104 static int rump_hyp_syscall(int, void *, long *);
105 static int rump_hyp_rfork(void *, int, const char *);
106 static void rump_hyp_lwpexit(void);
107 static void rump_hyp_execnotify(const char *);
108 
109 static void rump_component_addlocal(void);
110 static struct lwp *bootlwp;
111 
112 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
113 
114 bool rump_ttycomponent = false;
115 
116 static void
117 rump_aiodone_worker(struct work *wk, void *dummy)
118 {
119 	struct buf *bp = (struct buf *)wk;
120 
121 	KASSERT(&bp->b_work == wk);
122 	bp->b_iodone(bp);
123 }
124 
125 static int rump_inited;
126 
127 void (*rump_vfs_drainbufs)(int) = (void *)nullop;
128 int  (*rump_vfs_makeonedevnode)(dev_t, const char *,
129 				devmajor_t, devminor_t) = (void *)nullop;
130 int  (*rump_vfs_makedevnodes)(dev_t, const char *, char,
131 			      devmajor_t, devminor_t, int) = (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 /*
139  * Create some sysctl nodes.  why only this you ask.  well, init_sysctl
140  * is a kitchen sink in need of some gardening.  but i want to use
141  * others today.  Furthermore, creating a whole kitchen sink full of
142  * sysctl nodes is a waste of cycles for rump kernel bootstrap.
143  */
144 static void
145 mksysctls(void)
146 {
147 
148 	/* hw.pagesize */
149 	sysctl_createv(NULL, 0, NULL, NULL,
150 	    CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
151 	    CTLTYPE_INT, "pagesize",
152 	    SYSCTL_DESCR("Software page size"),
153 	    NULL, PAGE_SIZE, NULL, 0,
154 	    CTL_HW, HW_PAGESIZE, CTL_EOL);
155 }
156 
157 /* there's no convenient kernel entry point for this, so just craft out own */
158 static pid_t
159 spgetpid(void)
160 {
161 
162 	return curproc->p_pid;
163 }
164 
165 static const struct rumpuser_hyperup hyp = {
166 	.hyp_schedule		= rump_schedule,
167 	.hyp_unschedule		= rump_unschedule,
168 	.hyp_backend_unschedule	= rump_user_unschedule,
169 	.hyp_backend_schedule	= rump_user_schedule,
170 	.hyp_lwproc_switch	= rump_lwproc_switch,
171 	.hyp_lwproc_release	= rump_lwproc_releaselwp,
172 	.hyp_lwproc_rfork	= rump_hyp_rfork,
173 	.hyp_lwproc_newlwp	= rump_lwproc_newlwp,
174 	.hyp_lwproc_curlwp	= rump_lwproc_curlwp,
175 	.hyp_lwpexit		= rump_hyp_lwpexit,
176 	.hyp_syscall		= rump_hyp_syscall,
177 	.hyp_execnotify		= rump_hyp_execnotify,
178 	.hyp_getpid		= spgetpid,
179 };
180 
181 int
182 rump_daemonize_begin(void)
183 {
184 
185 	if (rump_inited)
186 		return EALREADY;
187 
188 	return rumpuser_daemonize_begin();
189 }
190 
191 int
192 rump_daemonize_done(int error)
193 {
194 
195 	return rumpuser_daemonize_done(error);
196 }
197 
198 #ifndef RUMP_USE_CTOR
199 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
200 {
201 	__link_set_decl(rump_components, struct rump_component);
202 
203 	/*
204 	 * Trick compiler into generating references so that statically
205 	 * linked rump kernels are generated with the link set symbols.
206 	 */
207 	asm("" :: "r"(__start_link_set_rump_components));
208 	asm("" :: "r"(__stop_link_set_rump_components));
209 }
210 #endif
211 
212 int
213 rump_init(void)
214 {
215 	char buf[256];
216 	struct timespec ts;
217 	int64_t sec;
218 	long nsec;
219 	struct lwp *l, *initlwp;
220 	int i, numcpu;
221 
222 	/* not reentrant */
223 	if (rump_inited)
224 		return 0;
225 	else if (rump_inited == -1)
226 		panic("rump_init: host process restart required");
227 	else
228 		rump_inited = 1;
229 
230 	/* initialize hypervisor */
231 	if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
232 		rumpuser_dprintf("rumpuser init failed\n");
233 		return EINVAL;
234 	}
235 
236 	/* init minimal lwp/cpu context */
237 	rump_lwproc_init();
238 	l = &lwp0;
239 	l->l_cpu = l->l_target_cpu = rump_cpu;
240 	rump_lwproc_curlwp_set(l);
241 
242 	/* retrieve env vars which affect the early stage of bootstrap */
243 	if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
244 		rump_threads = *buf != '0';
245 	}
246 	if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
247 		if (*buf != '0')
248 			boothowto = AB_VERBOSE;
249 	}
250 
251 	if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
252 		panic("mandatory hypervisor configuration (NCPU) missing");
253 	numcpu = strtoll(buf, NULL, 10);
254 	if (numcpu < 1) {
255 		panic("rump kernels are not lightweight enough for \"%d\" CPUs",
256 		    numcpu);
257 	}
258 
259 	rump_thread_init();
260 	rump_cpus_bootstrap(&numcpu);
261 
262 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
263 	boottime.tv_sec = sec;
264 	boottime.tv_nsec = nsec;
265 
266 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
267 	aprint_verbose("%s%s", copyright, version);
268 
269 	rump_intr_init(numcpu);
270 
271 	rump_tsleep_init();
272 
273 	rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
274 	ksyms_init();
275 	uvm_init();
276 	evcnt_init();
277 
278 	kcpuset_sysinit();
279 	once_init();
280 	kernconfig_lock_init();
281 	prop_kern_init();
282 
283 	kmem_init();
284 	kmeminit();
285 
286 	uvm_ra_init();
287 	uao_init();
288 
289 	mutex_obj_init();
290 	callout_startup();
291 
292 	kprintf_init();
293 	pserialize_init();
294 	loginit();
295 
296 	kauth_init();
297 
298 	secmodel_init();
299 	sysctl_init();
300 
301 	rnd_init();
302 	cprng_init();
303 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
304 	    CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
305 	rump_hyperentropy_init();
306 
307 	procinit();
308 	proc0_init();
309 	uid_init();
310 	chgproccnt(0, 1);
311 
312 	l->l_proc = &proc0;
313 	lwp_update_creds(l);
314 
315 	lwpinit_specificdata();
316 	lwp_initspecific(&lwp0);
317 
318 	rump_biglock_init();
319 
320 	rump_scheduler_init(numcpu);
321 	/* revert temporary context and schedule a semireal context */
322 	rump_lwproc_curlwp_clear(l);
323 	initproc = &proc0; /* borrow proc0 before we get initproc started */
324 	rump_schedule();
325 	bootlwp = curlwp;
326 
327 	percpu_init();
328 	inittimecounter();
329 	ntp_init();
330 
331 #ifdef KTRACE
332 	ktrinit();
333 #endif
334 
335 	ts = boottime;
336 	tc_setclock(&ts);
337 
338 	extern krwlock_t exec_lock;
339 	rw_init(&exec_lock);
340 
341 	/* we are mostly go.  do per-cpu subsystem init */
342 	for (i = 0; i < numcpu; i++) {
343 		struct cpu_info *ci = cpu_lookup(i);
344 
345 		/* attach non-bootstrap CPUs */
346 		if (i > 0) {
347 			rump_cpu_attach(ci);
348 			ncpu++;
349 		}
350 
351 		callout_init_cpu(ci);
352 		softint_init(ci);
353 		xc_init_cpu(ci);
354 		pool_cache_cpu_init(ci);
355 		selsysinit(ci);
356 		percpu_init_cpu(ci);
357 
358 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
359 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
360 
361 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
362 	}
363 
364 	/* CPUs are up.  allow kernel threads to run */
365 	rump_thread_allow(NULL);
366 
367 	rnd_init_softint();
368 
369 	mksysctls();
370 	kqueue_init();
371 	iostat_init();
372 	fd_sys_init();
373 	module_init();
374 	devsw_init();
375 	pipe_init();
376 	resource_init();
377 	procinit_sysctl();
378 	time_init();
379 	time_init2();
380 
381 	/* start page baroness */
382 	if (rump_threads) {
383 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
384 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
385 			panic("pagedaemon create failed");
386 	} else
387 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
388 
389 	/* process dso's */
390 	rumpuser_dl_bootstrap(add_linkedin_modules,
391 	    rump_kernelfsym_load, rump_component_load);
392 
393 	rump_component_addlocal();
394 	rump_component_init(RUMP_COMPONENT_KERN);
395 
396 	/* initialize factions, if present */
397 	rump_component_init(RUMP__FACTION_VFS);
398 	/* pnbuf_cache is used even without vfs */
399 	if (rump_component_count(RUMP__FACTION_VFS) == 0) {
400 		pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
401 		    NULL, IPL_NONE, NULL, NULL, NULL);
402 	}
403 	rump_component_init(RUMP__FACTION_NET);
404 	rump_component_init(RUMP__FACTION_DEV);
405 	KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
406 	    && rump_component_count(RUMP__FACTION_NET) <= 1
407 	    && rump_component_count(RUMP__FACTION_DEV) <= 1);
408 
409 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
410 
411 	/*
412 	 * if we initialized the tty component above, the tyttymtx is
413 	 * now initialized.  otherwise, we need to initialize it.
414 	 */
415 	if (!rump_ttycomponent)
416 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
417 
418 	cold = 0;
419 
420 	/* aieeeedondest */
421 	if (rump_threads) {
422 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
423 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
424 			panic("aiodoned");
425 	}
426 
427 	sysctl_finalize();
428 
429 	module_init_class(MODULE_CLASS_ANY);
430 
431 	if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
432 	    hostname, MAXHOSTNAMELEN) != 0) {
433 		panic("mandatory hypervisor configuration (HOSTNAME) missing");
434 	}
435 	hostnamelen = strlen(hostname);
436 
437 	sigemptyset(&sigcantmask);
438 
439 	if (rump_threads)
440 		vmem_rehash_start();
441 
442 	/*
443 	 * Create init (proc 1), used to attach implicit threads in rump.
444 	 * (note: must be done after vfsinit to get cwdi)
445 	 */
446 	initlwp = rump__lwproc_alloclwp(NULL);
447 	mutex_enter(proc_lock);
448 	initproc = proc_find_raw(1);
449 	mutex_exit(proc_lock);
450 	if (initproc == NULL)
451 		panic("where in the world is initproc?");
452 
453 	rump_component_init(RUMP_COMPONENT_POSTINIT);
454 
455 	/* load syscalls */
456 	rump_component_init(RUMP_COMPONENT_SYSCALL);
457 
458 	/* component inits done */
459 	bootlwp = NULL;
460 
461 	/* open 0/1/2 for init */
462 	KASSERT(rump_lwproc_curlwp() == NULL);
463 	rump_lwproc_switch(initlwp);
464 	rump_consdev_init();
465 	rump_lwproc_switch(NULL);
466 
467 	/* release cpu */
468 	rump_unschedule();
469 
470 	return 0;
471 }
472 /* historic compat */
473 __strong_alias(rump__init,rump_init);
474 
475 int
476 rump_init_server(const char *url)
477 {
478 
479 	return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
480 }
481 
482 static int compcounter[RUMP_COMPONENT_MAX];
483 static int compinited[RUMP_COMPONENT_MAX];
484 
485 /*
486  * Yea, this is O(n^2), but we're only looking at a handful of components.
487  * Components are always initialized from the thread that called rump_init().
488  */
489 static LIST_HEAD(, rump_component) rchead = LIST_HEAD_INITIALIZER(rchead);
490 
491 #ifdef RUMP_USE_CTOR
492 struct modinfo_boot_chain modinfo_boot_chain \
493     = LIST_HEAD_INITIALIZER(modinfo_boot_chain);
494 
495 static void
496 rump_component_addlocal(void)
497 {
498 	struct modinfo_chain *mc;
499 
500 	while ((mc = LIST_FIRST(&modinfo_boot_chain)) != NULL) {
501 		LIST_REMOVE(mc, mc_entries);
502 		module_builtin_add(&mc->mc_info, 1, false);
503 	}
504 }
505 
506 #else /* RUMP_USE_CTOR */
507 
508 static void
509 rump_component_addlocal(void)
510 {
511 	__link_set_decl(rump_components, struct rump_component);
512 	struct rump_component *const *rc;
513 
514 	__link_set_foreach(rc, rump_components) {
515 		rump_component_load(*rc);
516 	}
517 }
518 #endif /* RUMP_USE_CTOR */
519 
520 void
521 rump_component_load(const struct rump_component *rc_const)
522 {
523 	struct rump_component *rc, *rc_iter;
524 
525 	/*
526 	 * XXX: this is ok since the "const" was removed from the
527 	 * definition of RUMP_COMPONENT().
528 	 *
529 	 * However, to preserve the hypercall interface, the const
530 	 * remains here.  This can be fixed in the next hypercall revision.
531 	 */
532 	rc = __UNCONST(rc_const);
533 
534 	KASSERT(!rump_inited || curlwp == bootlwp);
535 
536 	LIST_FOREACH(rc_iter, &rchead, rc_entries) {
537 		if (rc_iter == rc)
538 			return;
539 	}
540 
541 	LIST_INSERT_HEAD(&rchead, rc, rc_entries);
542 	KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
543 	compcounter[rc->rc_type]++;
544 }
545 
546 int
547 rump_component_count(enum rump_component_type type)
548 {
549 
550 	KASSERT(curlwp == bootlwp);
551 	KASSERT(type < RUMP_COMPONENT_MAX);
552 	return compcounter[type];
553 }
554 
555 void
556 rump_component_init(enum rump_component_type type)
557 {
558 	const struct rump_component *rc, *rc_safe;
559 
560 	KASSERT(curlwp == bootlwp);
561 	KASSERT(!compinited[type]);
562 	LIST_FOREACH_SAFE(rc, &rchead, rc_entries, rc_safe) {
563 		if (rc->rc_type == type) {
564 			rc->rc_init();
565 			LIST_REMOVE(rc, rc_entries);
566 		}
567 	}
568 	compinited[type] = 1;
569 }
570 
571 /*
572  * Initialize a module which has already been loaded and linked
573  * with dlopen(). This is fundamentally the same as a builtin module.
574  *
575  * XXX: this interface does not really work in the RUMP_USE_CTOR case,
576  * but I'm not sure it's anything to cry about.  In feeling blue,
577  * things could somehow be handled via modinfo_boot_chain.
578  */
579 int
580 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
581 {
582 
583 	return module_builtin_add(mip, nmodinfo, true);
584 }
585 
586 /*
587  * Finish module (flawless victory, fatality!).
588  */
589 int
590 rump_module_fini(const struct modinfo *mi)
591 {
592 
593 	return module_builtin_remove(mi, true);
594 }
595 
596 /*
597  * Add loaded and linked module to the builtin list.  It will
598  * later be initialized with module_init_class().
599  */
600 
601 static void
602 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
603 {
604 
605 	module_builtin_add(mip, nmodinfo, false);
606 }
607 
608 int
609 rump_kernelfsym_load(void *symtab, uint64_t symsize,
610 	char *strtab, uint64_t strsize)
611 {
612 	static int inited = 0;
613 	Elf64_Ehdr ehdr;
614 
615 	if (inited)
616 		return EBUSY;
617 	inited = 1;
618 
619 	/*
620 	 * Use 64bit header since it's bigger.  Shouldn't make a
621 	 * difference, since we're passing in all zeroes anyway.
622 	 */
623 	memset(&ehdr, 0, sizeof(ehdr));
624 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
625 
626 	return 0;
627 }
628 
629 static int
630 rump_hyp_syscall(int num, void *arg, long *retval)
631 {
632 	register_t regrv[2] = {0, 0};
633 	struct lwp *l;
634 	struct sysent *callp;
635 	int rv;
636 
637 	if (__predict_false(num >= SYS_NSYSENT))
638 		return ENOSYS;
639 
640 	/* XXX: always uses native syscall vector */
641 	callp = rump_sysent + num;
642 	l = curlwp;
643 	rv = sy_invoke(callp, l, (void *)arg, regrv, num);
644 	retval[0] = regrv[0];
645 	retval[1] = regrv[1];
646 
647 	return rv;
648 }
649 
650 static int
651 rump_hyp_rfork(void *priv, int flags, const char *comm)
652 {
653 	struct vmspace *newspace;
654 	struct proc *p;
655 	int error;
656 
657 	if ((error = rump_lwproc_rfork(flags)) != 0)
658 		return error;
659 
660 	/*
661 	 * Since it's a proxy proc, adjust the vmspace.
662 	 * Refcount will eternally be 1.
663 	 */
664 	p = curproc;
665 	newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
666 	newspace->vm_refcnt = 1;
667 	newspace->vm_map.pmap = priv;
668 	KASSERT(p->p_vmspace == vmspace_kernel());
669 	p->p_vmspace = newspace;
670 	if (comm)
671 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
672 
673 	return 0;
674 }
675 
676 /*
677  * Order all lwps in a process to exit.  does *not* wait for them to drain.
678  */
679 static void
680 rump_hyp_lwpexit(void)
681 {
682 	struct proc *p = curproc;
683 	uint64_t where;
684 	struct lwp *l;
685 
686 	mutex_enter(p->p_lock);
687 	/*
688 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
689 	 * so that they know they should exit.
690 	 */
691 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
692 		if (l == curlwp)
693 			continue;
694 		l->l_flag |= LW_RUMP_QEXIT;
695 	}
696 	mutex_exit(p->p_lock);
697 
698 	/*
699 	 * Next, make sure everyone on all CPUs sees our status
700 	 * update.  This keeps threads inside cv_wait() and makes
701 	 * sure we don't access a stale cv pointer later when
702 	 * we wake up the threads.
703 	 */
704 
705 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
706 	xc_wait(where);
707 
708 	/*
709 	 * Ok, all lwps are either:
710 	 *  1) not in the cv code
711 	 *  2) sleeping on l->l_private
712 	 *  3) sleeping on p->p_waitcv
713 	 *
714 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
715 	 * in p->p_sflag.
716 	 */
717 
718 	mutex_enter(p->p_lock);
719 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
720 		if (l->l_private)
721 			cv_broadcast(l->l_private);
722 	}
723 	p->p_sflag |= PS_RUMP_LWPEXIT;
724 	cv_broadcast(&p->p_waitcv);
725 	mutex_exit(p->p_lock);
726 }
727 
728 /*
729  * Notify process that all threads have been drained and exec is complete.
730  */
731 static void
732 rump_hyp_execnotify(const char *comm)
733 {
734 	struct proc *p = curproc;
735 
736 	fd_closeexec();
737 	mutex_enter(p->p_lock);
738 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
739 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
740 	mutex_exit(p->p_lock);
741 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
742 }
743 
744 int
745 rump_boot_gethowto()
746 {
747 
748 	return boothowto;
749 }
750 
751 void
752 rump_boot_sethowto(int howto)
753 {
754 
755 	boothowto = howto;
756 }
757 
758 int
759 rump_getversion(void)
760 {
761 
762 	return __NetBSD_Version__;
763 }
764 /* compat */
765 __strong_alias(rump_pub_getversion,rump_getversion);
766 
767 int
768 rump_nativeabi_p(void)
769 {
770 
771 #ifdef _RUMP_NATIVE_ABI
772 	return 1;
773 #else
774 	return 0;
775 #endif
776 }
777 
778 /*
779  * Note: may be called unscheduled.  Not fully safe since no locking
780  * of allevents (currently that's not even available).
781  */
782 void
783 rump_printevcnts()
784 {
785 	struct evcnt *ev;
786 
787 	TAILQ_FOREACH(ev, &allevents, ev_list)
788 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
789 		    ev->ev_group, ev->ev_name, ev->ev_count);
790 }
791 
792 /*
793  * If you use this interface ... well ... all bets are off.
794  * The original purpose is for the p2k fs server library to be
795  * able to use the same pid/lid for VOPs as the host kernel.
796  */
797 void
798 rump_allbetsareoff_setid(pid_t pid, int lid)
799 {
800 	struct lwp *l = curlwp;
801 	struct proc *p = l->l_proc;
802 
803 	l->l_lid = lid;
804 	p->p_pid = pid;
805 }
806 
807 #include <sys/pserialize.h>
808 
809 static void
810 ipiemu(void *a1, void *a2)
811 {
812 
813 	xc__highpri_intr(NULL);
814 	pserialize_switchpoint();
815 }
816 
817 void
818 rump_xc_highpri(struct cpu_info *ci)
819 {
820 
821 	if (ci)
822 		xc_unicast(0, ipiemu, NULL, NULL, ci);
823 	else
824 		xc_broadcast(0, ipiemu, NULL, NULL);
825 }
826 
827 int
828 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
829 {
830 	struct proc *p;
831 	struct emul *e;
832 	struct sysent *callp;
833 	const int *etrans = NULL;
834 	int rv;
835 
836 	rump_schedule();
837 	p = curproc;
838 	e = p->p_emul;
839 #ifndef __HAVE_MINIMAL_EMUL
840 	KASSERT(num > 0 && num < e->e_nsysent);
841 #endif
842 	callp = e->e_sysent + num;
843 
844 	rv = sy_invoke(callp, curlwp, data, retval, num);
845 
846 	/*
847 	 * I hope that (!__HAVE_MINIMAL_EMUL || __HAVE_SYSCALL_INTERN) is
848 	 * an invariant ...
849 	 */
850 #if !defined(__HAVE_MINIMAL_EMUL)
851 	etrans = e->e_errno;
852 #elif defined(__HAVE_SYSCALL_INTERN)
853 	etrans = p->p_emuldata;
854 #endif
855 
856 	if (etrans) {
857 		rv = etrans[rv];
858 		/*
859 		 * XXX: small hack since Linux etrans vectors on some
860 		 * archs contain negative errnos, but rump_syscalls
861 		 * uses the -1 + errno ABI.  Note that these
862 		 * negative values are always the result of translation,
863 		 * otherwise the above translation method would not
864 		 * work very well.
865 		 */
866 		if (rv < 0)
867 			rv = -rv;
868 	}
869 	rump_unschedule();
870 
871 	return rv;
872 }
873 
874 void
875 rump_syscall_boot_establish(const struct rump_onesyscall *calls, size_t ncall)
876 {
877 	struct sysent *callp;
878 	size_t i;
879 
880 	for (i = 0; i < ncall; i++) {
881 		callp = rump_sysent + calls[i].ros_num;
882 		KASSERT(bootlwp != NULL
883 		    && callp->sy_call == (sy_call_t *)enosys);
884 		callp->sy_call = calls[i].ros_handler;
885 	}
886 }
887 
888 struct rump_boot_etfs *ebstart;
889 void
890 rump_boot_etfs_register(struct rump_boot_etfs *eb)
891 {
892 
893 	/*
894 	 * Could use atomics, but, since caller would need to synchronize
895 	 * against calling rump_init() anyway, easier to just specify the
896 	 * interface as "caller serializes".  This solve-by-specification
897 	 * approach avoids the grey area of using atomics before rump_init()
898 	 * runs.
899 	 */
900 	eb->_eb_next = ebstart;
901 	eb->eb_status = -1;
902 	ebstart = eb;
903 }
904 
905 /*
906  * Temporary notification that rumpkern_time is obsolete.  This is to
907  * be removed along with obsoleting rumpkern_time in a few months.
908  */
909 #define RUMPKERN_TIME_WARN "rumpkern_time is obsolete, functionality in librump"
910 __warn_references(rumpkern_time_is_obsolete,RUMPKERN_TIME_WARN)
911 void rumpkern_time_is_obsolete(void);
912 void
913 rumpkern_time_is_obsolete(void)
914 {
915 	printf("WARNING: %s\n", RUMPKERN_TIME_WARN);
916 }
917