Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.279
      1 /*	$NetBSD: rump.c,v 1.279 2013/12/09 16:56:11 pooka 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.279 2013/12/09 16:56:11 pooka 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/simplelock.h>
     74 #include <sys/cprng.h>
     75 #include <sys/ktrace.h>
     76 
     77 #include <rump/rumpuser.h>
     78 
     79 #include <secmodel/suser/suser.h>
     80 
     81 #include <prop/proplib.h>
     82 
     83 #include <uvm/uvm_extern.h>
     84 #include <uvm/uvm_readahead.h>
     85 
     86 #include "rump_private.h"
     87 #include "rump_net_private.h"
     88 #include "rump_vfs_private.h"
     89 #include "rump_dev_private.h"
     90 
     91 char machine[] = MACHINE;
     92 
     93 struct proc *initproc;
     94 
     95 struct device rump_rootdev = {
     96 	.dv_class = DV_VIRTUAL
     97 };
     98 
     99 #ifdef RUMP_WITHOUT_THREADS
    100 int rump_threads = 0;
    101 #else
    102 int rump_threads = 1;
    103 #endif
    104 
    105 static int rump_hyp_syscall(int, void *, long *);
    106 static int rump_hyp_rfork(void *, int, const char *);
    107 static void rump_hyp_lwpexit(void);
    108 static void rump_hyp_execnotify(const char *);
    109 
    110 static void rump_component_addlocal(void);
    111 static void rump_component_load(const struct rump_component *);
    112 static struct lwp *bootlwp;
    113 
    114 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    115 
    116 #ifdef LOCKDEBUG
    117 const int rump_lockdebug = 1;
    118 #else
    119 const int rump_lockdebug = 0;
    120 #endif
    121 bool rump_ttycomponent = false;
    122 
    123 static void
    124 rump_aiodone_worker(struct work *wk, void *dummy)
    125 {
    126 	struct buf *bp = (struct buf *)wk;
    127 
    128 	KASSERT(&bp->b_work == wk);
    129 	bp->b_iodone(bp);
    130 }
    131 
    132 static int rump_inited;
    133 
    134 void (*rump_vfs_drainbufs)(int);
    135 void (*rump_vfs_fini)(void);
    136 int  (*rump_vfs_makeonedevnode)(dev_t, const char *,
    137 				devmajor_t, devminor_t) = (void *)nullop;
    138 int  (*rump_vfs_makedevnodes)(dev_t, const char *, char,
    139 			      devmajor_t, devminor_t, int) = (void *)nullop;
    140 
    141 int rump__unavailable(void);
    142 int rump__unavailable() {return EOPNOTSUPP;}
    143 
    144 __weak_alias(biodone,rump__unavailable);
    145 __weak_alias(sopoll,rump__unavailable);
    146 
    147 void rump__unavailable_vfs_panic(void);
    148 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    149 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    150 
    151 /* easier to write vfs-less clients */
    152 __weak_alias(rump_pub_etfs_register,rump__unavailable);
    153 __weak_alias(rump_pub_etfs_register_withsize,rump__unavailable);
    154 __weak_alias(rump_pub_etfs_remove,rump__unavailable);
    155 
    156 rump_proc_vfs_init_fn rump_proc_vfs_init;
    157 rump_proc_vfs_release_fn rump_proc_vfs_release;
    158 
    159 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    160 
    161 /*
    162  * Create some sysctl nodes.  why only this you ask.  well, init_sysctl
    163  * is a kitchen sink in need of some gardening.  but i want to use
    164  * others today.  Furthermore, creating a whole kitchen sink full of
    165  * sysctl nodes is a waste of cycles for rump kernel bootstrap.
    166  */
    167 static void
    168 mksysctls(void)
    169 {
    170 
    171 	/* kern.hostname */
    172 	sysctl_createv(NULL, 0, NULL, NULL,
    173 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
    174 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
    175 	/* XXX: setting hostnamelen is missing */
    176 	sysctl_createv(NULL, 0, NULL, NULL,
    177 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
    178 	    SYSCTL_DESCR("System hostname"), NULL, 0,
    179 	    hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    180 
    181 	/* hw.pagesize */
    182 	sysctl_createv(NULL, 0, NULL, NULL,
    183 	    CTLFLAG_PERMANENT,
    184 	    CTLTYPE_NODE, "hw", NULL,
    185 	    NULL, 0, NULL, 0,
    186 	    CTL_HW, CTL_EOL);
    187 	sysctl_createv(NULL, 0, NULL, NULL,
    188 	    CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
    189 	    CTLTYPE_INT, "pagesize",
    190 	    SYSCTL_DESCR("Software page size"),
    191 	    NULL, PAGE_SIZE, NULL, 0,
    192 	    CTL_HW, HW_PAGESIZE, CTL_EOL);
    193 }
    194 
    195 /* there's no convenient kernel entry point for this, so just craft out own */
    196 static pid_t
    197 spgetpid(void)
    198 {
    199 
    200 	return curproc->p_pid;
    201 }
    202 
    203 static const struct rumpuser_hyperup hyp = {
    204 	.hyp_schedule		= rump_schedule,
    205 	.hyp_unschedule		= rump_unschedule,
    206 	.hyp_backend_unschedule	= rump_user_unschedule,
    207 	.hyp_backend_schedule	= rump_user_schedule,
    208 	.hyp_lwproc_switch	= rump_lwproc_switch,
    209 	.hyp_lwproc_release	= rump_lwproc_releaselwp,
    210 	.hyp_lwproc_rfork	= rump_hyp_rfork,
    211 	.hyp_lwproc_newlwp	= rump_lwproc_newlwp,
    212 	.hyp_lwproc_curlwp	= rump_lwproc_curlwp,
    213 	.hyp_lwpexit		= rump_hyp_lwpexit,
    214 	.hyp_syscall		= rump_hyp_syscall,
    215 	.hyp_execnotify		= rump_hyp_execnotify,
    216 	.hyp_getpid		= spgetpid,
    217 };
    218 
    219 int
    220 rump_daemonize_begin(void)
    221 {
    222 
    223 	if (rump_inited)
    224 		return EALREADY;
    225 
    226 	return rumpuser_daemonize_begin();
    227 }
    228 
    229 int
    230 rump_daemonize_done(int error)
    231 {
    232 
    233 	return rumpuser_daemonize_done(error);
    234 }
    235 
    236 RUMP_COMPONENT(RUMP_COMPONENT_POSTINIT)
    237 {
    238 	__link_set_decl(rump_components, struct rump_component);
    239 
    240 	/*
    241 	 * Trick compiler into generating references so that statically
    242 	 * linked rump kernels are generated with the link set symbols.
    243 	 */
    244 	asm("" :: "r"(__start_link_set_rump_components));
    245 	asm("" :: "r"(__stop_link_set_rump_components));
    246 }
    247 
    248 int
    249 rump_init(void)
    250 {
    251 	char buf[256];
    252 	struct timespec ts;
    253 	int64_t sec;
    254 	long nsec;
    255 	struct lwp *l, *initlwp;
    256 	int i, numcpu;
    257 
    258 	/* not reentrant */
    259 	if (rump_inited)
    260 		return 0;
    261 	else if (rump_inited == -1)
    262 		panic("rump_init: host process restart required");
    263 	else
    264 		rump_inited = 1;
    265 
    266 	/* initialize hypervisor */
    267 	if (rumpuser_init(RUMPUSER_VERSION, &hyp) != 0) {
    268 		rumpuser_dprintf("rumpuser init failed\n");
    269 		return EINVAL;
    270 	}
    271 
    272 	/* init minimal lwp/cpu context */
    273 	l = &lwp0;
    274 	l->l_lid = 1;
    275 	l->l_cpu = l->l_target_cpu = rump_cpu;
    276 	l->l_fd = &filedesc0;
    277 
    278 	/* lwp0 isn't created like other threads, so notify hypervisor here */
    279 	rumpuser_curlwpop(RUMPUSER_LWP_CREATE, l);
    280 	rumpuser_curlwpop(RUMPUSER_LWP_SET, l);
    281 
    282 	/* retrieve env vars which affect the early stage of bootstrap */
    283 	if (rumpuser_getparam("RUMP_THREADS", buf, sizeof(buf)) == 0) {
    284 		rump_threads = *buf != '0';
    285 	}
    286 	if (rumpuser_getparam("RUMP_VERBOSE", buf, sizeof(buf)) == 0) {
    287 		if (*buf != '0')
    288 			boothowto = AB_VERBOSE;
    289 	}
    290 
    291 	if (rumpuser_getparam(RUMPUSER_PARAM_NCPU, buf, sizeof(buf)) != 0)
    292 		panic("mandatory hypervisor configuration (NCPU) missing");
    293 	numcpu = strtoll(buf, NULL, 10);
    294 	if (numcpu < 1) {
    295 		panic("rump kernels are not lightweight enough for \"%d\" CPUs",
    296 		    numcpu);
    297 	}
    298 
    299 	rump_thread_init();
    300 	rump_cpus_bootstrap(&numcpu);
    301 
    302 	rumpuser_clock_gettime(RUMPUSER_CLOCK_RELWALL, &sec, &nsec);
    303 	boottime.tv_sec = sec;
    304 	boottime.tv_nsec = nsec;
    305 
    306 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    307 	aprint_verbose("%s%s", copyright, version);
    308 
    309 	rump_intr_init(numcpu);
    310 
    311 	rump_tsleep_init();
    312 
    313 	rumpuser_mutex_init(&rump_giantlock, RUMPUSER_MTX_SPIN);
    314 	ksyms_init();
    315 	uvm_init();
    316 	evcnt_init();
    317 
    318 	kcpuset_sysinit();
    319 	once_init();
    320 	kernconfig_lock_init();
    321 	prop_kern_init();
    322 
    323 	kmem_init();
    324 	kmeminit();
    325 
    326 	uvm_ra_init();
    327 	uao_init();
    328 
    329 	mutex_obj_init();
    330 	callout_startup();
    331 
    332 	kprintf_init();
    333 	pserialize_init();
    334 	loginit();
    335 
    336 	kauth_init();
    337 
    338 	secmodel_init();
    339 
    340 	rnd_init();
    341 
    342 	/*
    343 	 * Create the kernel cprng.  Yes, it's currently stubbed out
    344 	 * to arc4random() for RUMP, but this won't always be so.
    345 	 */
    346 	kern_cprng = cprng_strong_create("kernel", IPL_VM,
    347 					 CPRNG_INIT_ANY|CPRNG_REKEY_ANY);
    348 
    349 	procinit();
    350 	proc0_init();
    351 	sysctl_init();
    352 	uid_init();
    353 	chgproccnt(0, 1);
    354 
    355 	l->l_proc = &proc0;
    356 	lwp_update_creds(l);
    357 
    358 	lwpinit_specificdata();
    359 	lwp_initspecific(&lwp0);
    360 
    361 	rump_biglock_init();
    362 
    363 	rump_scheduler_init(numcpu);
    364 	/* revert temporary context and schedule a semireal context */
    365 	rumpuser_curlwpop(RUMPUSER_LWP_CLEAR, l);
    366 	initproc = &proc0; /* borrow proc0 before we get initproc started */
    367 	rump_schedule();
    368 	bootlwp = curlwp;
    369 
    370 	percpu_init();
    371 	inittimecounter();
    372 	ntp_init();
    373 
    374 	ktrinit();
    375 
    376 	ts = boottime;
    377 	tc_setclock(&ts);
    378 
    379 	/* we are mostly go.  do per-cpu subsystem init */
    380 	for (i = 0; i < numcpu; i++) {
    381 		struct cpu_info *ci = cpu_lookup(i);
    382 
    383 		/* attach non-bootstrap CPUs */
    384 		if (i > 0) {
    385 			rump_cpu_attach(ci);
    386 			ncpu++;
    387 		}
    388 
    389 		callout_init_cpu(ci);
    390 		softint_init(ci);
    391 		xc_init_cpu(ci);
    392 		pool_cache_cpu_init(ci);
    393 		selsysinit(ci);
    394 		percpu_init_cpu(ci);
    395 
    396 		TAILQ_INIT(&ci->ci_data.cpu_ld_locks);
    397 		__cpu_simple_lock_init(&ci->ci_data.cpu_ld_lock);
    398 
    399 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
    400 	}
    401 
    402 	/* CPUs are up.  allow kernel threads to run */
    403 	rump_thread_allow();
    404 
    405 	mksysctls();
    406 	kqueue_init();
    407 	iostat_init();
    408 	fd_sys_init();
    409 	module_init();
    410 	devsw_init();
    411 	pipe_init();
    412 	resource_init();
    413 	procinit_sysctl();
    414 
    415 	/* start page baroness */
    416 	if (rump_threads) {
    417 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    418 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    419 			panic("pagedaemon create failed");
    420 	} else
    421 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    422 
    423 	/* process dso's */
    424 	rumpuser_dl_bootstrap(add_linkedin_modules,
    425 	    rump_kernelfsym_load, rump_component_load);
    426 
    427 	rump_component_addlocal();
    428 	rump_component_init(RUMP_COMPONENT_KERN);
    429 
    430 	/* initialize factions, if present */
    431 	rump_component_init(RUMP__FACTION_VFS);
    432 	/* pnbuf_cache is used even without vfs */
    433 	if (rump_component_count(RUMP__FACTION_VFS) == 0) {
    434 		pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    435 		    NULL, IPL_NONE, NULL, NULL, NULL);
    436 	}
    437 	rump_component_init(RUMP__FACTION_NET);
    438 	rump_component_init(RUMP__FACTION_DEV);
    439 	KASSERT(rump_component_count(RUMP__FACTION_VFS) <= 1
    440 	    && rump_component_count(RUMP__FACTION_NET) <= 1
    441 	    && rump_component_count(RUMP__FACTION_DEV) <= 1);
    442 
    443 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
    444 
    445 	/*
    446 	 * if we initialized the tty component above, the tyttymtx is
    447 	 * now initialized.  otherwise, we need to initialize it.
    448 	 */
    449 	if (!rump_ttycomponent)
    450 		mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_VM);
    451 
    452 	cold = 0;
    453 
    454 	/* aieeeedondest */
    455 	if (rump_threads) {
    456 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    457 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    458 			panic("aiodoned");
    459 	}
    460 
    461 	sysctl_finalize();
    462 
    463 	module_init_class(MODULE_CLASS_ANY);
    464 
    465 	if (rumpuser_getparam(RUMPUSER_PARAM_HOSTNAME,
    466 	    hostname, MAXHOSTNAMELEN) != 0) {
    467 		panic("mandatory hypervisor configuration (HOSTNAME) missing");
    468 	}
    469 	hostnamelen = strlen(hostname);
    470 
    471 	sigemptyset(&sigcantmask);
    472 
    473 	if (rump_threads)
    474 		vmem_rehash_start();
    475 
    476 	/*
    477 	 * Create init (proc 1), used to attach implicit threads in rump.
    478 	 * (note: must be done after vfsinit to get cwdi)
    479 	 */
    480 	initlwp = rump__lwproc_alloclwp(NULL);
    481 	mutex_enter(proc_lock);
    482 	initproc = proc_find_raw(1);
    483 	mutex_exit(proc_lock);
    484 	if (initproc == NULL)
    485 		panic("where in the world is initproc?");
    486 
    487 	/*
    488 	 * Adjust syscall vector in case factions were dlopen()'d
    489 	 * before calling rump_init().
    490 	 * (modules will handle dynamic syscalls the usual way)
    491 	 *
    492 	 * Note: this will adjust the function vectors of
    493 	 * syscalls which use a funcalias (getpid etc.), but
    494 	 * it makes no difference.
    495 	 */
    496 	for (i = 0; i < SYS_NSYSENT; i++) {
    497 		void *sym;
    498 
    499 		if (rump_sysent[i].sy_flags & SYCALL_NOSYS ||
    500 		    *syscallnames[i] == '#' ||
    501 		    rump_sysent[i].sy_call == sys_nomodule)
    502 			continue;
    503 
    504 		/*
    505 		 * deal with compat wrappers.  makesyscalls.sh should
    506 		 * generate the necessary info instead of this hack,
    507 		 * though.  ugly, fix it later.
    508 		 */
    509 #define CPFX "compat_"
    510 #define CPFXLEN (sizeof(CPFX)-1)
    511 		if (strncmp(syscallnames[i], CPFX, CPFXLEN) == 0) {
    512 			const char *p = syscallnames[i] + CPFXLEN;
    513 			size_t namelen;
    514 
    515 			/* skip version number */
    516 			while (*p >= '0' && *p <= '9')
    517 				p++;
    518 			if (p == syscallnames[i] + CPFXLEN || *p != '_')
    519 				panic("invalid syscall name %s\n",
    520 				    syscallnames[i]);
    521 
    522 			/* skip over the next underscore */
    523 			p++;
    524 			namelen = p + (sizeof("rumpns_")-1) - syscallnames[i];
    525 
    526 			strcpy(buf, "rumpns_");
    527 			strcat(buf, syscallnames[i]);
    528 			/* XXX: no strncat in the kernel */
    529 			strcpy(buf+namelen, "sys_");
    530 			strcat(buf, p);
    531 #undef CPFX
    532 #undef CPFXLEN
    533 		} else {
    534 			sprintf(buf, "rumpns_sys_%s", syscallnames[i]);
    535 		}
    536 		if ((sym = rumpuser_dl_globalsym(buf)) != NULL
    537 		    && sym != rump_sysent[i].sy_call) {
    538 #if 0
    539 			rumpuser_dprintf("adjusting %s: %p (old %p)\n",
    540 			    syscallnames[i], sym, rump_sysent[i].sy_call);
    541 #endif
    542 			rump_sysent[i].sy_call = sym;
    543 		}
    544 	}
    545 
    546 	rump_component_init(RUMP_COMPONENT_POSTINIT);
    547 
    548 	/* component inits done */
    549 	bootlwp = NULL;
    550 
    551 	/* open 0/1/2 for init */
    552 	KASSERT(rump_lwproc_curlwp() == NULL);
    553 	rump_lwproc_switch(initlwp);
    554 	rump_consdev_init();
    555 	rump_lwproc_switch(NULL);
    556 
    557 	/* release cpu */
    558 	rump_unschedule();
    559 
    560 	return 0;
    561 }
    562 /* historic compat */
    563 __strong_alias(rump__init,rump_init);
    564 
    565 int
    566 rump_init_server(const char *url)
    567 {
    568 
    569 	return rumpuser_sp_init(url, ostype, osrelease, MACHINE);
    570 }
    571 
    572 void
    573 cpu_reboot(int howto, char *bootstr)
    574 {
    575 	int ruhow = 0;
    576 	void *finiarg;
    577 
    578 	printf("rump kernel halting...\n");
    579 
    580 	if (!RUMP_LOCALPROC_P(curproc))
    581 		finiarg = curproc->p_vmspace->vm_map.pmap;
    582 	else
    583 		finiarg = NULL;
    584 
    585 	/* dump means we really take the dive here */
    586 	if ((howto & RB_DUMP) || panicstr) {
    587 		ruhow = RUMPUSER_PANIC;
    588 		goto out;
    589 	}
    590 
    591 	/* try to sync */
    592 	if (!((howto & RB_NOSYNC) || panicstr)) {
    593 		if (rump_vfs_fini)
    594 			rump_vfs_fini();
    595 	}
    596 
    597 	doshutdownhooks();
    598 
    599 	/* your wish is my command */
    600 	if (howto & RB_HALT) {
    601 		printf("rump kernel halted\n");
    602 		rumpuser_sp_fini(finiarg);
    603 		for (;;) {
    604 			rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
    605 		}
    606 	}
    607 
    608 	/* this function is __dead, we must exit */
    609  out:
    610 	printf("halted\n");
    611 	rumpuser_sp_fini(finiarg);
    612 	rumpuser_exit(ruhow);
    613 }
    614 
    615 struct uio *
    616 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    617 {
    618 	struct uio *uio;
    619 	enum uio_rw uiorw;
    620 
    621 	switch (rw) {
    622 	case RUMPUIO_READ:
    623 		uiorw = UIO_READ;
    624 		break;
    625 	case RUMPUIO_WRITE:
    626 		uiorw = UIO_WRITE;
    627 		break;
    628 	default:
    629 		panic("%s: invalid rw %d", __func__, rw);
    630 	}
    631 
    632 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    633 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    634 
    635 	uio->uio_iov->iov_base = buf;
    636 	uio->uio_iov->iov_len = bufsize;
    637 
    638 	uio->uio_iovcnt = 1;
    639 	uio->uio_offset = offset;
    640 	uio->uio_resid = bufsize;
    641 	uio->uio_rw = uiorw;
    642 	UIO_SETUP_SYSSPACE(uio);
    643 
    644 	return uio;
    645 }
    646 
    647 size_t
    648 rump_uio_getresid(struct uio *uio)
    649 {
    650 
    651 	return uio->uio_resid;
    652 }
    653 
    654 off_t
    655 rump_uio_getoff(struct uio *uio)
    656 {
    657 
    658 	return uio->uio_offset;
    659 }
    660 
    661 size_t
    662 rump_uio_free(struct uio *uio)
    663 {
    664 	size_t resid;
    665 
    666 	resid = uio->uio_resid;
    667 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    668 	kmem_free(uio, sizeof(*uio));
    669 
    670 	return resid;
    671 }
    672 
    673 kauth_cred_t
    674 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    675 {
    676 	kauth_cred_t cred;
    677 	int rv;
    678 
    679 	cred = kauth_cred_alloc();
    680 	kauth_cred_setuid(cred, uid);
    681 	kauth_cred_seteuid(cred, uid);
    682 	kauth_cred_setsvuid(cred, uid);
    683 	kauth_cred_setgid(cred, gid);
    684 	kauth_cred_setgid(cred, gid);
    685 	kauth_cred_setegid(cred, gid);
    686 	kauth_cred_setsvgid(cred, gid);
    687 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    688 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    689 	assert(rv == 0);
    690 
    691 	return cred;
    692 }
    693 
    694 void
    695 rump_cred_put(kauth_cred_t cred)
    696 {
    697 
    698 	kauth_cred_free(cred);
    699 }
    700 
    701 static int compcounter[RUMP_COMPONENT_MAX];
    702 static int compinited[RUMP_COMPONENT_MAX];
    703 
    704 /*
    705  * Yea, this is O(n^2), but we're only looking at a handful of components.
    706  * Components are always initialized from the thread that called rump_init().
    707  * Could also free these when done with them, but prolly not worth it.
    708  */
    709 struct compstore {
    710 	const struct rump_component *cs_rc;
    711 	LIST_ENTRY(compstore) cs_entries;
    712 };
    713 static LIST_HEAD(, compstore) cshead = LIST_HEAD_INITIALIZER(cshead);
    714 
    715 /*
    716  * add components which are visible from the current object.
    717  */
    718 static void
    719 rump_component_addlocal(void)
    720 {
    721 	__link_set_decl(rump_components, struct rump_component);
    722 	struct rump_component *const *rc;
    723 
    724 	__link_set_foreach(rc, rump_components) {
    725 		rump_component_load(*rc);
    726 	}
    727 }
    728 
    729 static void
    730 rump_component_load(const struct rump_component *rc)
    731 {
    732 	struct compstore *cs;
    733 
    734 	KASSERT(curlwp == bootlwp);
    735 
    736 	LIST_FOREACH(cs, &cshead, cs_entries) {
    737 		if (rc == cs->cs_rc)
    738 			return;
    739 	}
    740 
    741 	cs = kmem_alloc(sizeof(*cs), KM_SLEEP);
    742 	cs->cs_rc = rc;
    743 	LIST_INSERT_HEAD(&cshead, cs, cs_entries);
    744 	KASSERT(rc->rc_type < RUMP_COMPONENT_MAX);
    745 	compcounter[rc->rc_type]++;
    746 }
    747 
    748 int
    749 rump_component_count(enum rump_component_type type)
    750 {
    751 
    752 	KASSERT(curlwp == bootlwp);
    753 	KASSERT(type < RUMP_COMPONENT_MAX);
    754 	return compcounter[type];
    755 }
    756 
    757 void
    758 rump_component_init(enum rump_component_type type)
    759 {
    760 	struct compstore *cs;
    761 	const struct rump_component *rc;
    762 
    763 	KASSERT(curlwp == bootlwp);
    764 	KASSERT(!compinited[type]);
    765 	LIST_FOREACH(cs, &cshead, cs_entries) {
    766 		rc = cs->cs_rc;
    767 		if (rc->rc_type == type)
    768 			rc->rc_init();
    769 	}
    770 	compinited[type] = 1;
    771 }
    772 
    773 /*
    774  * Initialize a module which has already been loaded and linked
    775  * with dlopen(). This is fundamentally the same as a builtin module.
    776  */
    777 int
    778 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    779 {
    780 
    781 	return module_builtin_add(mip, nmodinfo, true);
    782 }
    783 
    784 /*
    785  * Finish module (flawless victory, fatality!).
    786  */
    787 int
    788 rump_module_fini(const struct modinfo *mi)
    789 {
    790 
    791 	return module_builtin_remove(mi, true);
    792 }
    793 
    794 /*
    795  * Add loaded and linked module to the builtin list.  It will
    796  * later be initialized with module_init_class().
    797  */
    798 
    799 static void
    800 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    801 {
    802 
    803 	module_builtin_add(mip, nmodinfo, false);
    804 }
    805 
    806 int
    807 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    808 	char *strtab, uint64_t strsize)
    809 {
    810 	static int inited = 0;
    811 	Elf64_Ehdr ehdr;
    812 
    813 	if (inited)
    814 		return EBUSY;
    815 	inited = 1;
    816 
    817 	/*
    818 	 * Use 64bit header since it's bigger.  Shouldn't make a
    819 	 * difference, since we're passing in all zeroes anyway.
    820 	 */
    821 	memset(&ehdr, 0, sizeof(ehdr));
    822 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    823 
    824 	return 0;
    825 }
    826 
    827 static int
    828 rump_hyp_syscall(int num, void *arg, long *retval)
    829 {
    830 	register_t regrv[2] = {0, 0};
    831 	struct lwp *l;
    832 	struct sysent *callp;
    833 	int rv;
    834 
    835 	if (__predict_false(num >= SYS_NSYSENT))
    836 		return ENOSYS;
    837 
    838 	callp = rump_sysent + num;
    839 	l = curlwp;
    840 	rv = sy_invoke(callp, l, (void *)arg, regrv, num);
    841 	retval[0] = regrv[0];
    842 	retval[1] = regrv[1];
    843 
    844 	return rv;
    845 }
    846 
    847 static int
    848 rump_hyp_rfork(void *priv, int flags, const char *comm)
    849 {
    850 	struct vmspace *newspace;
    851 	struct proc *p;
    852 	int error;
    853 
    854 	if ((error = rump_lwproc_rfork(flags)) != 0)
    855 		return error;
    856 
    857 	/*
    858 	 * Since it's a proxy proc, adjust the vmspace.
    859 	 * Refcount will eternally be 1.
    860 	 */
    861 	p = curproc;
    862 	newspace = kmem_zalloc(sizeof(*newspace), KM_SLEEP);
    863 	newspace->vm_refcnt = 1;
    864 	newspace->vm_map.pmap = priv;
    865 	KASSERT(p->p_vmspace == vmspace_kernel());
    866 	p->p_vmspace = newspace;
    867 	if (comm)
    868 		strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    869 
    870 	return 0;
    871 }
    872 
    873 /*
    874  * Order all lwps in a process to exit.  does *not* wait for them to drain.
    875  */
    876 static void
    877 rump_hyp_lwpexit(void)
    878 {
    879 	struct proc *p = curproc;
    880 	uint64_t where;
    881 	struct lwp *l;
    882 
    883 	mutex_enter(p->p_lock);
    884 	/*
    885 	 * First pass: mark all lwps in the process with LW_RUMP_QEXIT
    886 	 * so that they know they should exit.
    887 	 */
    888 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    889 		if (l == curlwp)
    890 			continue;
    891 		l->l_flag |= LW_RUMP_QEXIT;
    892 	}
    893 	mutex_exit(p->p_lock);
    894 
    895 	/*
    896 	 * Next, make sure everyone on all CPUs sees our status
    897 	 * update.  This keeps threads inside cv_wait() and makes
    898 	 * sure we don't access a stale cv pointer later when
    899 	 * we wake up the threads.
    900 	 */
    901 
    902 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    903 	xc_wait(where);
    904 
    905 	/*
    906 	 * Ok, all lwps are either:
    907 	 *  1) not in the cv code
    908 	 *  2) sleeping on l->l_private
    909 	 *  3) sleeping on p->p_waitcv
    910 	 *
    911 	 * Either way, l_private is stable until we set PS_RUMP_LWPEXIT
    912 	 * in p->p_sflag.
    913 	 */
    914 
    915 	mutex_enter(p->p_lock);
    916 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    917 		if (l->l_private)
    918 			cv_broadcast(l->l_private);
    919 	}
    920 	p->p_sflag |= PS_RUMP_LWPEXIT;
    921 	cv_broadcast(&p->p_waitcv);
    922 	mutex_exit(p->p_lock);
    923 }
    924 
    925 /*
    926  * Notify process that all threads have been drained and exec is complete.
    927  */
    928 static void
    929 rump_hyp_execnotify(const char *comm)
    930 {
    931 	struct proc *p = curproc;
    932 
    933 	fd_closeexec();
    934 	mutex_enter(p->p_lock);
    935 	KASSERT(p->p_nlwps == 1 && p->p_sflag & PS_RUMP_LWPEXIT);
    936 	p->p_sflag &= ~PS_RUMP_LWPEXIT;
    937 	mutex_exit(p->p_lock);
    938 	strlcpy(p->p_comm, comm, sizeof(p->p_comm));
    939 }
    940 
    941 int
    942 rump_boot_gethowto()
    943 {
    944 
    945 	return boothowto;
    946 }
    947 
    948 void
    949 rump_boot_sethowto(int howto)
    950 {
    951 
    952 	boothowto = howto;
    953 }
    954 
    955 int
    956 rump_getversion(void)
    957 {
    958 
    959 	return __NetBSD_Version__;
    960 }
    961 
    962 /*
    963  * Note: may be called unscheduled.  Not fully safe since no locking
    964  * of allevents (currently that's not even available).
    965  */
    966 void
    967 rump_printevcnts()
    968 {
    969 	struct evcnt *ev;
    970 
    971 	TAILQ_FOREACH(ev, &allevents, ev_list)
    972 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    973 		    ev->ev_group, ev->ev_name, ev->ev_count);
    974 }
    975 
    976 /*
    977  * If you use this interface ... well ... all bets are off.
    978  * The original purpose is for the p2k fs server library to be
    979  * able to use the same pid/lid for VOPs as the host kernel.
    980  */
    981 void
    982 rump_allbetsareoff_setid(pid_t pid, int lid)
    983 {
    984 	struct lwp *l = curlwp;
    985 	struct proc *p = l->l_proc;
    986 
    987 	l->l_lid = lid;
    988 	p->p_pid = pid;
    989 }
    990 
    991 #include <sys/pserialize.h>
    992 
    993 static void
    994 ipiemu(void *a1, void *a2)
    995 {
    996 
    997 	xc__highpri_intr(NULL);
    998 	pserialize_switchpoint();
    999 }
   1000 
   1001 void
   1002 rump_xc_highpri(struct cpu_info *ci)
   1003 {
   1004 
   1005 	if (ci)
   1006 		xc_unicast(0, ipiemu, NULL, NULL, ci);
   1007 	else
   1008 		xc_broadcast(0, ipiemu, NULL, NULL);
   1009 }
   1010 
   1011 int
   1012 rump_syscall(int num, void *data, size_t dlen, register_t *retval)
   1013 {
   1014 	struct proc *p;
   1015 	struct emul *e;
   1016 	struct sysent *callp;
   1017 	int rv;
   1018 
   1019 	rump_schedule();
   1020 	p = curproc;
   1021 	e = p->p_emul;
   1022 #ifndef __HAVE_MINIMAL_EMUL
   1023 	KASSERT(num > 0 && num < e->e_nsysent);
   1024 #endif
   1025 	callp = e->e_sysent + num;
   1026 
   1027 	rv = sy_invoke(callp, curlwp, data, retval, num);
   1028 	rump_unschedule();
   1029 
   1030 	return rv;
   1031 }
   1032