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