Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.174
      1 /*	$NetBSD: rump.c,v 1.174 2010/06/02 10:55:18 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Development of this software was supported by Google Summer of Code.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: rump.c,v 1.174 2010/06/02 10:55:18 pooka Exp $");
     32 
     33 #include <sys/systm.h>
     34 #define ELFSIZE ARCH_ELFSIZE
     35 
     36 #include <sys/param.h>
     37 #include <sys/atomic.h>
     38 #include <sys/buf.h>
     39 #include <sys/callout.h>
     40 #include <sys/conf.h>
     41 #include <sys/cpu.h>
     42 #include <sys/device.h>
     43 #include <sys/evcnt.h>
     44 #include <sys/event.h>
     45 #include <sys/exec_elf.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/iostat.h>
     48 #include <sys/kauth.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/kprintf.h>
     52 #include <sys/ksyms.h>
     53 #include <sys/msgbuf.h>
     54 #include <sys/module.h>
     55 #include <sys/once.h>
     56 #include <sys/percpu.h>
     57 #include <sys/pipe.h>
     58 #include <sys/pool.h>
     59 #include <sys/queue.h>
     60 #include <sys/reboot.h>
     61 #include <sys/resourcevar.h>
     62 #include <sys/select.h>
     63 #include <sys/sysctl.h>
     64 #include <sys/syscall.h>
     65 #include <sys/syscallvar.h>
     66 #include <sys/timetc.h>
     67 #include <sys/tty.h>
     68 #include <sys/uidinfo.h>
     69 #include <sys/vmem.h>
     70 #include <sys/xcall.h>
     71 
     72 #include <rump/rumpuser.h>
     73 
     74 #include <secmodel/suser/suser.h>
     75 
     76 #include <prop/proplib.h>
     77 
     78 #include <uvm/uvm_extern.h>
     79 #include <uvm/uvm_readahead.h>
     80 
     81 #include "rump_private.h"
     82 #include "rump_net_private.h"
     83 #include "rump_vfs_private.h"
     84 #include "rump_dev_private.h"
     85 
     86 struct proc proc0;
     87 struct session rump_session = {
     88 	.s_count = 1,
     89 	.s_flags = 0,
     90 	.s_leader = &proc0,
     91 	.s_login = "rumphobo",
     92 	.s_sid = 0,
     93 };
     94 struct pgrp rump_pgrp = {
     95 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     96 	.pg_session = &rump_session,
     97 	.pg_jobc = 1,
     98 };
     99 struct pstats rump_stats;
    100 struct plimit rump_limits;
    101 struct filedesc rump_filedesc0;
    102 struct proclist allproc;
    103 char machine[] = MACHINE;
    104 static kauth_cred_t rump_susercred;
    105 
    106 /* pretend the master rump proc is init */
    107 struct proc *initproc = &proc0;
    108 
    109 struct rumpuser_mtx *rump_giantlock;
    110 
    111 struct device rump_rootdev = {
    112 	.dv_class = DV_VIRTUAL
    113 };
    114 
    115 #ifdef RUMP_WITHOUT_THREADS
    116 int rump_threads = 0;
    117 #else
    118 int rump_threads = 1;
    119 #endif
    120 
    121 static char rump_msgbuf[16*1024]; /* 16k should be enough for std rump needs */
    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 /*
    135  * Make sure pnbuf_cache is available even without vfs
    136  */
    137 struct pool_cache *pnbuf_cache;
    138 int rump_initpnbufpool(void);
    139 int rump_initpnbufpool(void)
    140 {
    141 
    142         pnbuf_cache = pool_cache_init(MAXPATHLEN, 0, 0, 0, "pnbufpl",
    143 	    NULL, IPL_NONE, NULL, NULL, NULL);
    144 	return EOPNOTSUPP;
    145 }
    146 
    147 int rump__unavailable(void);
    148 int rump__unavailable() {return EOPNOTSUPP;}
    149 __weak_alias(rump_net_init,rump__unavailable);
    150 __weak_alias(rump_vfs_init,rump_initpnbufpool);
    151 __weak_alias(rump_dev_init,rump__unavailable);
    152 
    153 __weak_alias(rump_vfs_fini,rump__unavailable);
    154 
    155 __weak_alias(biodone,rump__unavailable);
    156 __weak_alias(sopoll,rump__unavailable);
    157 
    158 void rump__unavailable_vfs_panic(void);
    159 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    160 __weak_alias(usermount_common_policy,rump__unavailable_vfs_panic);
    161 
    162 rump_proc_vfs_init_fn rump_proc_vfs_init;
    163 rump_proc_vfs_release_fn rump_proc_vfs_release;
    164 
    165 static void add_linkedin_modules(const struct modinfo *const *, size_t);
    166 
    167 static void __noinline
    168 messthestack(void)
    169 {
    170 	volatile uint32_t mess[64];
    171 	uint64_t d1, d2;
    172 	int i, error;
    173 
    174 	for (i = 0; i < 64; i++) {
    175 		rumpuser_gettime(&d1, &d2, &error);
    176 		mess[i] = d2;
    177 	}
    178 }
    179 
    180 /*
    181  * Create kern.hostname.  why only this you ask.  well, init_sysctl
    182  * is a kitchen sink in need of some gardening.  but i want to use
    183  * kern.hostname today.
    184  */
    185 static void
    186 mksysctls(void)
    187 {
    188 
    189 	sysctl_createv(NULL, 0, NULL, NULL,
    190 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "kern", NULL,
    191 	    NULL, 0, NULL, 0, CTL_KERN, CTL_EOL);
    192 
    193 	/* XXX: setting hostnamelen is missing */
    194 	sysctl_createv(NULL, 0, NULL, NULL,
    195 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_STRING, "hostname",
    196 	    SYSCTL_DESCR("System hostname"), NULL, 0,
    197 	    &hostname, MAXHOSTNAMELEN, CTL_KERN, KERN_HOSTNAME, CTL_EOL);
    198 }
    199 
    200 int
    201 rump__init(int rump_version)
    202 {
    203 	char buf[256];
    204 	struct timespec ts;
    205 	uint64_t sec, nsec;
    206 	struct proc *p;
    207 	struct lwp *l;
    208 	int i, numcpu;
    209 	int error;
    210 
    211 	/* not reentrant */
    212 	if (rump_inited)
    213 		return 0;
    214 	else if (rump_inited == -1)
    215 		panic("rump_init: host process restart required");
    216 	else
    217 		rump_inited = 1;
    218 
    219 	if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
    220 		if (*buf != '0')
    221 			boothowto = AB_VERBOSE;
    222 	}
    223 
    224 	if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
    225 		error = 0;
    226 	/* non-x86 is missing CPU_INFO_FOREACH() support */
    227 #if defined(__i386__) || defined(__x86_64__)
    228 	if (error == 0) {
    229 		numcpu = strtoll(buf, NULL, 10);
    230 		if (numcpu < 1)
    231 			numcpu = 1;
    232 	} else {
    233 		numcpu = rumpuser_getnhostcpu();
    234 	}
    235 #else
    236 	if (error == 0)
    237 		printf("NCPU limited to 1 on this host\n");
    238 	numcpu = 1;
    239 #endif
    240 	rump_cpus_bootstrap(numcpu);
    241 
    242 	rumpuser_gettime(&sec, &nsec, &error);
    243 	boottime.tv_sec = sec;
    244 	boottime.tv_nsec = nsec;
    245 
    246 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    247 	aprint_verbose("%s%s", copyright, version);
    248 
    249 	/*
    250 	 * Seed arc4random() with a "reasonable" amount of randomness.
    251 	 * Yes, this is a quick kludge which depends on the arc4random
    252 	 * implementation.
    253 	 */
    254 	messthestack();
    255 	arc4random();
    256 
    257 	if (rump_version != RUMP_VERSION) {
    258 		printf("rump version mismatch, %d vs. %d\n",
    259 		    rump_version, RUMP_VERSION);
    260 		return EPROGMISMATCH;
    261 	}
    262 
    263 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    264 		rump_threads = *buf != '0';
    265 	}
    266 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    267 	    rump_threads);
    268 	rump_intr_init();
    269 	rump_tsleep_init();
    270 
    271 	/* init minimal lwp/cpu context */
    272 	l = &lwp0;
    273 	l->l_lid = 1;
    274 	l->l_cpu = l->l_target_cpu = rump_cpu;
    275 	rumpuser_set_curlwp(l);
    276 
    277 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    278 	rumpuser_mutex_recursive_init(&rump_giantlock);
    279 	ksyms_init();
    280 	uvm_init();
    281 	evcnt_init();
    282 
    283 	once_init();
    284 	prop_kern_init();
    285 
    286 	pool_subsystem_init();
    287 	kmem_init();
    288 
    289 	uvm_ra_init();
    290 
    291 	mutex_obj_init();
    292 	callout_startup();
    293 
    294 	kprintf_init();
    295 	loginit();
    296 
    297 	kauth_init();
    298 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    299 
    300 	/* init proc0 and rest of lwp0 now that we can allocate memory */
    301 	p = &proc0;
    302 	p->p_stats = &rump_stats;
    303 	p->p_limit = &rump_limits;
    304 	p->p_pgrp = &rump_pgrp;
    305 	p->p_pid = 0;
    306 	p->p_fd = &rump_filedesc0;
    307 	p->p_vmspace = &rump_vmspace;
    308 	p->p_emul = &emul_netbsd;
    309 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    310 	l->l_cred = rump_cred_suserget();
    311 	l->l_proc = p;
    312 	LIST_INIT(&allproc);
    313 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    314 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    315 	lwpinit_specificdata();
    316 
    317 	mutex_init(&rump_limits.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    318 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    319 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    320 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    321 	rump_limits.pl_corename = defcorename;
    322 
    323 	rump_scheduler_init();
    324 	/* revert temporary context and schedule a real context */
    325 	l->l_cpu = NULL;
    326 	rumpuser_set_curlwp(NULL);
    327 	rump_schedule();
    328 
    329 	percpu_init();
    330 	inittimecounter();
    331 	ntp_init();
    332 
    333 	rumpuser_gettime(&sec, &nsec, &error);
    334 	ts.tv_sec = sec;
    335 	ts.tv_nsec = nsec;
    336 	tc_setclock(&ts);
    337 
    338 	/* we are mostly go.  do per-cpu subsystem init */
    339 	for (i = 0; i < ncpu; i++) {
    340 		struct cpu_info *ci = cpu_lookup(i);
    341 
    342 		callout_init_cpu(ci);
    343 		softint_init(ci);
    344 		xc_init_cpu(ci);
    345 		pool_cache_cpu_init(ci);
    346 		selsysinit(ci);
    347 		percpu_init_cpu(ci);
    348 	}
    349 
    350 	sysctl_init();
    351 	kqueue_init();
    352 	iostat_init();
    353 	uid_init();
    354 	fd_sys_init();
    355 	module_init();
    356 	devsw_init();
    357 	pipe_init();
    358 	resource_init();
    359 
    360 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    361 
    362 	/* these do nothing if not present */
    363 	rump_vfs_init();
    364 	rump_net_init();
    365 	rump_dev_init();
    366 	cold = 0;
    367 
    368 	/* aieeeedondest */
    369 	if (rump_threads) {
    370 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    371 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    372 			panic("aiodoned");
    373 	}
    374 
    375 	mksysctls();
    376 	sysctl_finalize();
    377 
    378 	module_init_class(MODULE_CLASS_ANY);
    379 
    380 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    381 	hostnamelen = strlen(hostname);
    382 
    383 	sigemptyset(&sigcantmask);
    384 
    385 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    386 
    387 	if (rump_threads)
    388 		vmem_rehash_start();
    389 
    390 	rump_unschedule();
    391 
    392 	return 0;
    393 }
    394 
    395 /* maybe support sys_reboot some day for remote shutdown */
    396 void
    397 rump_reboot(int howto)
    398 {
    399 
    400 	/* dump means we really take the dive here */
    401 	if ((howto & RB_DUMP) || panicstr) {
    402 		rumpuser_exit(RUMPUSER_PANIC);
    403 		/*NOTREACHED*/
    404 	}
    405 
    406 	/* try to sync */
    407 	if (!((howto & RB_NOSYNC) || panicstr)) {
    408 		rump_vfs_fini();
    409 	}
    410 
    411 	/* your wish is my command */
    412 	if (howto & RB_HALT) {
    413 		for (;;) {
    414 			uint64_t sec = 5, nsec = 0;
    415 			int error;
    416 
    417 			rumpuser_nanosleep(&sec, &nsec, &error);
    418 		}
    419 	}
    420 	rump_inited = -1;
    421 }
    422 
    423 struct uio *
    424 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    425 {
    426 	struct uio *uio;
    427 	enum uio_rw uiorw;
    428 
    429 	switch (rw) {
    430 	case RUMPUIO_READ:
    431 		uiorw = UIO_READ;
    432 		break;
    433 	case RUMPUIO_WRITE:
    434 		uiorw = UIO_WRITE;
    435 		break;
    436 	default:
    437 		panic("%s: invalid rw %d", __func__, rw);
    438 	}
    439 
    440 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    441 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    442 
    443 	uio->uio_iov->iov_base = buf;
    444 	uio->uio_iov->iov_len = bufsize;
    445 
    446 	uio->uio_iovcnt = 1;
    447 	uio->uio_offset = offset;
    448 	uio->uio_resid = bufsize;
    449 	uio->uio_rw = uiorw;
    450 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    451 
    452 	return uio;
    453 }
    454 
    455 size_t
    456 rump_uio_getresid(struct uio *uio)
    457 {
    458 
    459 	return uio->uio_resid;
    460 }
    461 
    462 off_t
    463 rump_uio_getoff(struct uio *uio)
    464 {
    465 
    466 	return uio->uio_offset;
    467 }
    468 
    469 size_t
    470 rump_uio_free(struct uio *uio)
    471 {
    472 	size_t resid;
    473 
    474 	resid = uio->uio_resid;
    475 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    476 	kmem_free(uio, sizeof(*uio));
    477 
    478 	return resid;
    479 }
    480 
    481 static pid_t nextpid = 1;
    482 struct lwp *
    483 rump_newproc_switch()
    484 {
    485 	struct lwp *l;
    486 	pid_t mypid;
    487 
    488 	mypid = atomic_inc_uint_nv(&nextpid);
    489 	if (__predict_false(mypid == 0))
    490 		mypid = atomic_inc_uint_nv(&nextpid);
    491 
    492 	l = rump_lwp_alloc(mypid, 0);
    493 	rump_lwp_switch(l);
    494 
    495 	return l;
    496 }
    497 
    498 struct lwp *
    499 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
    500 {
    501 	struct lwp *l;
    502 
    503 	l = rump_lwp_alloc(pid, lid);
    504 	rump_lwp_switch(l);
    505 
    506 	return l;
    507 }
    508 
    509 struct lwp *
    510 rump_lwp_alloc(pid_t pid, lwpid_t lid)
    511 {
    512 	struct lwp *l;
    513 	struct proc *p;
    514 
    515 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    516 	if (pid != 0) {
    517 		p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    518 		if (rump_proc_vfs_init)
    519 			rump_proc_vfs_init(p);
    520 		p->p_stats = &rump_stats;
    521 		p->p_limit = lim_copy(&rump_limits);
    522 		p->p_pid = pid;
    523 		p->p_vmspace = &rump_vmspace;
    524 		p->p_emul = &emul_netbsd;
    525 		p->p_fd = fd_init(NULL);
    526 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    527 		p->p_pgrp = &rump_pgrp;
    528 		l->l_cred = rump_cred_suserget();
    529 	} else {
    530 		p = &proc0;
    531 		l->l_cred = rump_susercred;
    532 	}
    533 
    534 	l->l_proc = p;
    535 	l->l_lid = lid;
    536 	l->l_fd = p->p_fd;
    537 	l->l_cpu = NULL;
    538 	l->l_target_cpu = rump_cpu;
    539 	lwp_initspecific(l);
    540 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    541 
    542 	return l;
    543 }
    544 
    545 void
    546 rump_lwp_switch(struct lwp *newlwp)
    547 {
    548 	struct lwp *l = curlwp;
    549 
    550 	rumpuser_set_curlwp(NULL);
    551 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    552 	newlwp->l_mutex = l->l_mutex;
    553 	l->l_mutex = NULL;
    554 	l->l_cpu = NULL;
    555 	rumpuser_set_curlwp(newlwp);
    556 	if (l->l_flag & LW_WEXIT)
    557 		rump_lwp_free(l);
    558 }
    559 
    560 /* XXX: this has effect only on non-pid0 lwps */
    561 void
    562 rump_lwp_release(struct lwp *l)
    563 {
    564 	struct proc *p;
    565 
    566 	p = l->l_proc;
    567 	if (p->p_pid != 0) {
    568 		mutex_obj_free(p->p_lock);
    569 		fd_free();
    570 		if (rump_proc_vfs_release)
    571 			rump_proc_vfs_release(p);
    572 		rump_cred_put(l->l_cred);
    573 		limfree(p->p_limit);
    574 		kmem_free(p, sizeof(*p));
    575 	}
    576 	KASSERT((l->l_flag & LW_WEXIT) == 0);
    577 	l->l_flag |= LW_WEXIT;
    578 }
    579 
    580 void
    581 rump_lwp_free(struct lwp *l)
    582 {
    583 
    584 	KASSERT(l->l_flag & LW_WEXIT);
    585 	KASSERT(l->l_mutex == NULL);
    586 	if (l->l_name)
    587 		kmem_free(l->l_name, MAXCOMLEN);
    588 	lwp_finispecific(l);
    589 	LIST_REMOVE(l, l_list);
    590 	kmem_free(l, sizeof(*l));
    591 }
    592 
    593 struct lwp *
    594 rump_lwp_curlwp(void)
    595 {
    596 	struct lwp *l = curlwp;
    597 
    598 	if (l->l_flag & LW_WEXIT)
    599 		return NULL;
    600 	return l;
    601 }
    602 
    603 /* rump private.  NEEDS WORK! */
    604 void
    605 rump_set_vmspace(struct vmspace *vm)
    606 {
    607 	struct proc *p = curproc;
    608 
    609 	p->p_vmspace = vm;
    610 }
    611 
    612 kauth_cred_t
    613 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    614 {
    615 	kauth_cred_t cred;
    616 	int rv;
    617 
    618 	cred = kauth_cred_alloc();
    619 	kauth_cred_setuid(cred, uid);
    620 	kauth_cred_seteuid(cred, uid);
    621 	kauth_cred_setsvuid(cred, uid);
    622 	kauth_cred_setgid(cred, gid);
    623 	kauth_cred_setgid(cred, gid);
    624 	kauth_cred_setegid(cred, gid);
    625 	kauth_cred_setsvgid(cred, gid);
    626 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    627 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    628 	assert(rv == 0);
    629 
    630 	return cred;
    631 }
    632 
    633 void
    634 rump_cred_put(kauth_cred_t cred)
    635 {
    636 
    637 	kauth_cred_free(cred);
    638 }
    639 
    640 kauth_cred_t
    641 rump_cred_suserget(void)
    642 {
    643 
    644 	kauth_cred_hold(rump_susercred);
    645 	return rump_susercred;
    646 }
    647 
    648 /*
    649  * Return the next system lwpid
    650  */
    651 lwpid_t
    652 rump_nextlid(void)
    653 {
    654 	lwpid_t retid;
    655 
    656 	mutex_enter(proc0.p_lock);
    657 	/*
    658 	 * Take next one, don't return 0
    659 	 * XXX: most likely we'll have collisions in case this
    660 	 * wraps around.
    661 	 */
    662 	if (++proc0.p_nlwpid == 0)
    663 		++proc0.p_nlwpid;
    664 	retid = proc0.p_nlwpid;
    665 	mutex_exit(proc0.p_lock);
    666 
    667 	return retid;
    668 }
    669 
    670 static int compcounter[RUMP_COMPONENT_MAX];
    671 
    672 static void
    673 rump_component_init_cb(struct rump_component *rc, int type)
    674 {
    675 
    676 	KASSERT(type < RUMP_COMPONENT_MAX);
    677 	if (rc->rc_type == type) {
    678 		rc->rc_init();
    679 		compcounter[type]++;
    680 	}
    681 }
    682 
    683 int
    684 rump_component_count(enum rump_component_type type)
    685 {
    686 
    687 	KASSERT(type <= RUMP_COMPONENT_MAX);
    688 	return compcounter[type];
    689 }
    690 
    691 void
    692 rump_component_init(enum rump_component_type type)
    693 {
    694 
    695 	rumpuser_dl_component_init(type, rump_component_init_cb);
    696 }
    697 
    698 /*
    699  * Initialize a module which has already been loaded and linked
    700  * with dlopen(). This is fundamentally the same as a builtin module.
    701  */
    702 int
    703 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    704 {
    705 
    706 	return module_builtin_add(mip, nmodinfo, true);
    707 }
    708 
    709 /*
    710  * Finish module (flawless victory, fatality!).
    711  */
    712 int
    713 rump_module_fini(const struct modinfo *mi)
    714 {
    715 
    716 	return module_builtin_remove(mi, true);
    717 }
    718 
    719 /*
    720  * Add loaded and linked module to the builtin list.  It will
    721  * later be initialized with module_init_class().
    722  */
    723 
    724 static void
    725 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    726 {
    727 
    728 	module_builtin_add(mip, nmodinfo, false);
    729 }
    730 
    731 int
    732 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    733 	char *strtab, uint64_t strsize)
    734 {
    735 	static int inited = 0;
    736 	Elf64_Ehdr ehdr;
    737 
    738 	if (inited)
    739 		return EBUSY;
    740 	inited = 1;
    741 
    742 	/*
    743 	 * Use 64bit header since it's bigger.  Shouldn't make a
    744 	 * difference, since we're passing in all zeroes anyway.
    745 	 */
    746 	memset(&ehdr, 0, sizeof(ehdr));
    747 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    748 
    749 	return 0;
    750 }
    751 
    752 static int
    753 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    754 	register_t *retval)
    755 {
    756 	struct lwp *l;
    757 	struct sysent *callp;
    758 	int rv;
    759 
    760 	if (__predict_false(num >= SYS_NSYSENT))
    761 		return ENOSYS;
    762 
    763 	callp = rump_sysent + num;
    764 	rump_schedule();
    765 	l = curlwp;
    766 	rv = sy_call(callp, l, (void *)data, retval);
    767 	rump_unschedule();
    768 
    769 	return rv;
    770 }
    771 
    772 int
    773 rump_boot_gethowto()
    774 {
    775 
    776 	return boothowto;
    777 }
    778 
    779 void
    780 rump_boot_sethowto(int howto)
    781 {
    782 
    783 	boothowto = howto;
    784 }
    785 
    786 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    787 void *rump_sysproxy_arg;
    788 
    789 /*
    790  * This whole syscall-via-rpc is still taking form.  For example, it
    791  * may be necessary to set syscalls individually instead of lobbing
    792  * them all to the same place.  So don't think this interface is
    793  * set in stone.
    794  */
    795 int
    796 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    797 {
    798 
    799 	if (rump_sysproxy_arg)
    800 		return EBUSY;
    801 
    802 	rump_sysproxy_arg = arg;
    803 	rump_sysproxy = proxy;
    804 
    805 	return 0;
    806 }
    807 
    808 int
    809 rump_getversion(void)
    810 {
    811 
    812 	return __NetBSD_Version__;
    813 }
    814 
    815 /*
    816  * Note: may be called unscheduled.  Not fully safe since no locking
    817  * of allevents (currently that's not even available).
    818  */
    819 void
    820 rump_printevcnts()
    821 {
    822 	struct evcnt *ev;
    823 
    824 	TAILQ_FOREACH(ev, &allevents, ev_list)
    825 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    826 		    ev->ev_group, ev->ev_name, ev->ev_count);
    827 }
    828