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