Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.173
      1 /*	$NetBSD: rump.c,v 1.173 2010/05/31 23:18:33 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.173 2010/05/31 23:18:33 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 	rump_tsleep_init();
    269 
    270 	/* init minimal lwp/cpu context */
    271 	l = &lwp0;
    272 	l->l_lid = 1;
    273 	l->l_cpu = l->l_target_cpu = rump_cpu;
    274 	rumpuser_set_curlwp(l);
    275 
    276 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    277 	rumpuser_mutex_recursive_init(&rump_giantlock);
    278 	ksyms_init();
    279 	rumpvm_init();
    280 	evcnt_init();
    281 
    282 	once_init();
    283 	prop_kern_init();
    284 
    285 	pool_subsystem_init();
    286 	kmem_init();
    287 
    288 	uvm_ra_init();
    289 
    290 	mutex_obj_init();
    291 	callout_startup();
    292 
    293 	kprintf_init();
    294 	loginit();
    295 
    296 	kauth_init();
    297 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    298 
    299 	/* init proc0 and rest of lwp0 now that we can allocate memory */
    300 	p = &proc0;
    301 	p->p_stats = &rump_stats;
    302 	p->p_limit = &rump_limits;
    303 	p->p_pgrp = &rump_pgrp;
    304 	p->p_pid = 0;
    305 	p->p_fd = &rump_filedesc0;
    306 	p->p_vmspace = &rump_vmspace;
    307 	p->p_emul = &emul_netbsd;
    308 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    309 	l->l_cred = rump_cred_suserget();
    310 	l->l_proc = p;
    311 	LIST_INIT(&allproc);
    312 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    313 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    314 	lwpinit_specificdata();
    315 
    316 	mutex_init(&rump_limits.pl_lock, MUTEX_DEFAULT, IPL_NONE);
    317 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    318 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    319 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    320 	rump_limits.pl_corename = defcorename;
    321 
    322 	rump_scheduler_init();
    323 	/* revert temporary context and schedule a real context */
    324 	l->l_cpu = NULL;
    325 	rumpuser_set_curlwp(NULL);
    326 	rump_schedule();
    327 
    328 	percpu_init();
    329 	inittimecounter();
    330 	ntp_init();
    331 
    332 	rumpuser_gettime(&sec, &nsec, &error);
    333 	ts.tv_sec = sec;
    334 	ts.tv_nsec = nsec;
    335 	tc_setclock(&ts);
    336 
    337 	/* we are mostly go.  do per-cpu subsystem init */
    338 	for (i = 0; i < ncpu; i++) {
    339 		struct cpu_info *ci = cpu_lookup(i);
    340 
    341 		callout_init_cpu(ci);
    342 		softint_init(ci);
    343 		xc_init_cpu(ci);
    344 		pool_cache_cpu_init(ci);
    345 		selsysinit(ci);
    346 		percpu_init_cpu(ci);
    347 	}
    348 
    349 	sysctl_init();
    350 	kqueue_init();
    351 	iostat_init();
    352 	uid_init();
    353 	fd_sys_init();
    354 	module_init();
    355 	devsw_init();
    356 	pipe_init();
    357 	resource_init();
    358 
    359 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    360 
    361 	/* these do nothing if not present */
    362 	rump_vfs_init();
    363 	rump_net_init();
    364 	rump_dev_init();
    365 	cold = 0;
    366 
    367 	/* aieeeedondest */
    368 	if (rump_threads) {
    369 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    370 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    371 			panic("aiodoned");
    372 	}
    373 
    374 	mksysctls();
    375 	sysctl_finalize();
    376 
    377 	module_init_class(MODULE_CLASS_ANY);
    378 
    379 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    380 	hostnamelen = strlen(hostname);
    381 
    382 	sigemptyset(&sigcantmask);
    383 
    384 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    385 
    386 	if (rump_threads)
    387 		vmem_rehash_start();
    388 
    389 	rump_unschedule();
    390 
    391 	return 0;
    392 }
    393 
    394 /* maybe support sys_reboot some day for remote shutdown */
    395 void
    396 rump_reboot(int howto)
    397 {
    398 
    399 	/* dump means we really take the dive here */
    400 	if ((howto & RB_DUMP) || panicstr) {
    401 		rumpuser_exit(RUMPUSER_PANIC);
    402 		/*NOTREACHED*/
    403 	}
    404 
    405 	/* try to sync */
    406 	if (!((howto & RB_NOSYNC) || panicstr)) {
    407 		rump_vfs_fini();
    408 	}
    409 
    410 	/* your wish is my command */
    411 	if (howto & RB_HALT) {
    412 		for (;;) {
    413 			uint64_t sec = 5, nsec = 0;
    414 			int error;
    415 
    416 			rumpuser_nanosleep(&sec, &nsec, &error);
    417 		}
    418 	}
    419 	rump_inited = -1;
    420 }
    421 
    422 struct uio *
    423 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    424 {
    425 	struct uio *uio;
    426 	enum uio_rw uiorw;
    427 
    428 	switch (rw) {
    429 	case RUMPUIO_READ:
    430 		uiorw = UIO_READ;
    431 		break;
    432 	case RUMPUIO_WRITE:
    433 		uiorw = UIO_WRITE;
    434 		break;
    435 	default:
    436 		panic("%s: invalid rw %d", __func__, rw);
    437 	}
    438 
    439 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    440 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    441 
    442 	uio->uio_iov->iov_base = buf;
    443 	uio->uio_iov->iov_len = bufsize;
    444 
    445 	uio->uio_iovcnt = 1;
    446 	uio->uio_offset = offset;
    447 	uio->uio_resid = bufsize;
    448 	uio->uio_rw = uiorw;
    449 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    450 
    451 	return uio;
    452 }
    453 
    454 size_t
    455 rump_uio_getresid(struct uio *uio)
    456 {
    457 
    458 	return uio->uio_resid;
    459 }
    460 
    461 off_t
    462 rump_uio_getoff(struct uio *uio)
    463 {
    464 
    465 	return uio->uio_offset;
    466 }
    467 
    468 size_t
    469 rump_uio_free(struct uio *uio)
    470 {
    471 	size_t resid;
    472 
    473 	resid = uio->uio_resid;
    474 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    475 	kmem_free(uio, sizeof(*uio));
    476 
    477 	return resid;
    478 }
    479 
    480 static pid_t nextpid = 1;
    481 struct lwp *
    482 rump_newproc_switch()
    483 {
    484 	struct lwp *l;
    485 	pid_t mypid;
    486 
    487 	mypid = atomic_inc_uint_nv(&nextpid);
    488 	if (__predict_false(mypid == 0))
    489 		mypid = atomic_inc_uint_nv(&nextpid);
    490 
    491 	l = rump_lwp_alloc(mypid, 0);
    492 	rump_lwp_switch(l);
    493 
    494 	return l;
    495 }
    496 
    497 struct lwp *
    498 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
    499 {
    500 	struct lwp *l;
    501 
    502 	l = rump_lwp_alloc(pid, lid);
    503 	rump_lwp_switch(l);
    504 
    505 	return l;
    506 }
    507 
    508 struct lwp *
    509 rump_lwp_alloc(pid_t pid, lwpid_t lid)
    510 {
    511 	struct lwp *l;
    512 	struct proc *p;
    513 
    514 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    515 	if (pid != 0) {
    516 		p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    517 		if (rump_proc_vfs_init)
    518 			rump_proc_vfs_init(p);
    519 		p->p_stats = &rump_stats;
    520 		p->p_limit = lim_copy(&rump_limits);
    521 		p->p_pid = pid;
    522 		p->p_vmspace = &rump_vmspace;
    523 		p->p_emul = &emul_netbsd;
    524 		p->p_fd = fd_init(NULL);
    525 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    526 		p->p_pgrp = &rump_pgrp;
    527 		l->l_cred = rump_cred_suserget();
    528 	} else {
    529 		p = &proc0;
    530 		l->l_cred = rump_susercred;
    531 	}
    532 
    533 	l->l_proc = p;
    534 	l->l_lid = lid;
    535 	l->l_fd = p->p_fd;
    536 	l->l_cpu = NULL;
    537 	l->l_target_cpu = rump_cpu;
    538 	lwp_initspecific(l);
    539 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    540 
    541 	return l;
    542 }
    543 
    544 void
    545 rump_lwp_switch(struct lwp *newlwp)
    546 {
    547 	struct lwp *l = curlwp;
    548 
    549 	rumpuser_set_curlwp(NULL);
    550 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    551 	newlwp->l_mutex = l->l_mutex;
    552 	l->l_mutex = NULL;
    553 	l->l_cpu = NULL;
    554 	rumpuser_set_curlwp(newlwp);
    555 	if (l->l_flag & LW_WEXIT)
    556 		rump_lwp_free(l);
    557 }
    558 
    559 /* XXX: this has effect only on non-pid0 lwps */
    560 void
    561 rump_lwp_release(struct lwp *l)
    562 {
    563 	struct proc *p;
    564 
    565 	p = l->l_proc;
    566 	if (p->p_pid != 0) {
    567 		mutex_obj_free(p->p_lock);
    568 		fd_free();
    569 		if (rump_proc_vfs_release)
    570 			rump_proc_vfs_release(p);
    571 		rump_cred_put(l->l_cred);
    572 		limfree(p->p_limit);
    573 		kmem_free(p, sizeof(*p));
    574 	}
    575 	KASSERT((l->l_flag & LW_WEXIT) == 0);
    576 	l->l_flag |= LW_WEXIT;
    577 }
    578 
    579 void
    580 rump_lwp_free(struct lwp *l)
    581 {
    582 
    583 	KASSERT(l->l_flag & LW_WEXIT);
    584 	KASSERT(l->l_mutex == NULL);
    585 	if (l->l_name)
    586 		kmem_free(l->l_name, MAXCOMLEN);
    587 	lwp_finispecific(l);
    588 	LIST_REMOVE(l, l_list);
    589 	kmem_free(l, sizeof(*l));
    590 }
    591 
    592 struct lwp *
    593 rump_lwp_curlwp(void)
    594 {
    595 	struct lwp *l = curlwp;
    596 
    597 	if (l->l_flag & LW_WEXIT)
    598 		return NULL;
    599 	return l;
    600 }
    601 
    602 /* rump private.  NEEDS WORK! */
    603 void
    604 rump_set_vmspace(struct vmspace *vm)
    605 {
    606 	struct proc *p = curproc;
    607 
    608 	p->p_vmspace = vm;
    609 }
    610 
    611 kauth_cred_t
    612 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    613 {
    614 	kauth_cred_t cred;
    615 	int rv;
    616 
    617 	cred = kauth_cred_alloc();
    618 	kauth_cred_setuid(cred, uid);
    619 	kauth_cred_seteuid(cred, uid);
    620 	kauth_cred_setsvuid(cred, uid);
    621 	kauth_cred_setgid(cred, gid);
    622 	kauth_cred_setgid(cred, gid);
    623 	kauth_cred_setegid(cred, gid);
    624 	kauth_cred_setsvgid(cred, gid);
    625 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    626 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    627 	assert(rv == 0);
    628 
    629 	return cred;
    630 }
    631 
    632 void
    633 rump_cred_put(kauth_cred_t cred)
    634 {
    635 
    636 	kauth_cred_free(cred);
    637 }
    638 
    639 kauth_cred_t
    640 rump_cred_suserget(void)
    641 {
    642 
    643 	kauth_cred_hold(rump_susercred);
    644 	return rump_susercred;
    645 }
    646 
    647 /*
    648  * Return the next system lwpid
    649  */
    650 lwpid_t
    651 rump_nextlid(void)
    652 {
    653 	lwpid_t retid;
    654 
    655 	mutex_enter(proc0.p_lock);
    656 	/*
    657 	 * Take next one, don't return 0
    658 	 * XXX: most likely we'll have collisions in case this
    659 	 * wraps around.
    660 	 */
    661 	if (++proc0.p_nlwpid == 0)
    662 		++proc0.p_nlwpid;
    663 	retid = proc0.p_nlwpid;
    664 	mutex_exit(proc0.p_lock);
    665 
    666 	return retid;
    667 }
    668 
    669 static int compcounter[RUMP_COMPONENT_MAX];
    670 
    671 static void
    672 rump_component_init_cb(struct rump_component *rc, int type)
    673 {
    674 
    675 	KASSERT(type < RUMP_COMPONENT_MAX);
    676 	if (rc->rc_type == type) {
    677 		rc->rc_init();
    678 		compcounter[type]++;
    679 	}
    680 }
    681 
    682 int
    683 rump_component_count(enum rump_component_type type)
    684 {
    685 
    686 	KASSERT(type <= RUMP_COMPONENT_MAX);
    687 	return compcounter[type];
    688 }
    689 
    690 void
    691 rump_component_init(enum rump_component_type type)
    692 {
    693 
    694 	rumpuser_dl_component_init(type, rump_component_init_cb);
    695 }
    696 
    697 /*
    698  * Initialize a module which has already been loaded and linked
    699  * with dlopen(). This is fundamentally the same as a builtin module.
    700  */
    701 int
    702 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    703 {
    704 
    705 	return module_builtin_add(mip, nmodinfo, true);
    706 }
    707 
    708 /*
    709  * Finish module (flawless victory, fatality!).
    710  */
    711 int
    712 rump_module_fini(const struct modinfo *mi)
    713 {
    714 
    715 	return module_builtin_remove(mi, true);
    716 }
    717 
    718 /*
    719  * Add loaded and linked module to the builtin list.  It will
    720  * later be initialized with module_init_class().
    721  */
    722 
    723 static void
    724 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    725 {
    726 
    727 	module_builtin_add(mip, nmodinfo, false);
    728 }
    729 
    730 int
    731 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    732 	char *strtab, uint64_t strsize)
    733 {
    734 	static int inited = 0;
    735 	Elf64_Ehdr ehdr;
    736 
    737 	if (inited)
    738 		return EBUSY;
    739 	inited = 1;
    740 
    741 	/*
    742 	 * Use 64bit header since it's bigger.  Shouldn't make a
    743 	 * difference, since we're passing in all zeroes anyway.
    744 	 */
    745 	memset(&ehdr, 0, sizeof(ehdr));
    746 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    747 
    748 	return 0;
    749 }
    750 
    751 static int
    752 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    753 	register_t *retval)
    754 {
    755 	struct lwp *l;
    756 	struct sysent *callp;
    757 	int rv;
    758 
    759 	if (__predict_false(num >= SYS_NSYSENT))
    760 		return ENOSYS;
    761 
    762 	callp = rump_sysent + num;
    763 	rump_schedule();
    764 	l = curlwp;
    765 	rv = sy_call(callp, l, (void *)data, retval);
    766 	rump_unschedule();
    767 
    768 	return rv;
    769 }
    770 
    771 int
    772 rump_boot_gethowto()
    773 {
    774 
    775 	return boothowto;
    776 }
    777 
    778 void
    779 rump_boot_sethowto(int howto)
    780 {
    781 
    782 	boothowto = howto;
    783 }
    784 
    785 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    786 void *rump_sysproxy_arg;
    787 
    788 /*
    789  * This whole syscall-via-rpc is still taking form.  For example, it
    790  * may be necessary to set syscalls individually instead of lobbing
    791  * them all to the same place.  So don't think this interface is
    792  * set in stone.
    793  */
    794 int
    795 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    796 {
    797 
    798 	if (rump_sysproxy_arg)
    799 		return EBUSY;
    800 
    801 	rump_sysproxy_arg = arg;
    802 	rump_sysproxy = proxy;
    803 
    804 	return 0;
    805 }
    806 
    807 int
    808 rump_getversion(void)
    809 {
    810 
    811 	return __NetBSD_Version__;
    812 }
    813 
    814 /*
    815  * Note: may be called unscheduled.  Not fully safe since no locking
    816  * of allevents (currently that's not even available).
    817  */
    818 void
    819 rump_printevcnts()
    820 {
    821 	struct evcnt *ev;
    822 
    823 	TAILQ_FOREACH(ev, &allevents, ev_list)
    824 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    825 		    ev->ev_group, ev->ev_name, ev->ev_count);
    826 }
    827