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