Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.182
      1 /*	$NetBSD: rump.c,v 1.182 2010/08/26 19:56:07 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.182 2010/08/26 19:56:07 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/kthread.h>
     53 #include <sys/ksyms.h>
     54 #include <sys/msgbuf.h>
     55 #include <sys/module.h>
     56 #include <sys/once.h>
     57 #include <sys/percpu.h>
     58 #include <sys/pipe.h>
     59 #include <sys/pool.h>
     60 #include <sys/queue.h>
     61 #include <sys/reboot.h>
     62 #include <sys/resourcevar.h>
     63 #include <sys/select.h>
     64 #include <sys/sysctl.h>
     65 #include <sys/syscall.h>
     66 #include <sys/syscallvar.h>
     67 #include <sys/timetc.h>
     68 #include <sys/tty.h>
     69 #include <sys/uidinfo.h>
     70 #include <sys/vmem.h>
     71 #include <sys/xcall.h>
     72 
     73 #include <rump/rumpuser.h>
     74 
     75 #include <secmodel/suser/suser.h>
     76 
     77 #include <prop/proplib.h>
     78 
     79 #include <uvm/uvm_extern.h>
     80 #include <uvm/uvm_readahead.h>
     81 
     82 #include "rump_private.h"
     83 #include "rump_net_private.h"
     84 #include "rump_vfs_private.h"
     85 #include "rump_dev_private.h"
     86 
     87 /* is this still necessary or use kern_proc stuff? */
     88 struct session rump_session = {
     89 	.s_count = 1,
     90 	.s_flags = 0,
     91 	.s_leader = &proc0,
     92 	.s_login = "rumphobo",
     93 	.s_sid = 0,
     94 };
     95 struct pgrp rump_pgrp = {
     96 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     97 	.pg_session = &rump_session,
     98 	.pg_jobc = 1,
     99 };
    100 
    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 lwp *l;
    205 	int i, numcpu;
    206 	int error;
    207 
    208 	/* not reentrant */
    209 	if (rump_inited)
    210 		return 0;
    211 	else if (rump_inited == -1)
    212 		panic("rump_init: host process restart required");
    213 	else
    214 		rump_inited = 1;
    215 
    216 	if (rumpuser_getversion() != RUMPUSER_VERSION) {
    217 		/* let's hope the ABI of rumpuser_dprintf is the same ;) */
    218 		rumpuser_dprintf("rumpuser version mismatch: %d vs. %d\n",
    219 		    rumpuser_getversion(), RUMPUSER_VERSION);
    220 		return EPROGMISMATCH;
    221 	}
    222 
    223 	if (rumpuser_getenv("RUMP_VERBOSE", buf, sizeof(buf), &error) == 0) {
    224 		if (*buf != '0')
    225 			boothowto = AB_VERBOSE;
    226 	}
    227 
    228 	if (rumpuser_getenv("RUMP_NCPU", buf, sizeof(buf), &error) == 0)
    229 		error = 0;
    230 	/* non-x86 is missing CPU_INFO_FOREACH() support */
    231 #if defined(__i386__) || defined(__x86_64__)
    232 	if (error == 0) {
    233 		numcpu = strtoll(buf, NULL, 10);
    234 		if (numcpu < 1)
    235 			numcpu = 1;
    236 	} else {
    237 		numcpu = rumpuser_getnhostcpu();
    238 	}
    239 #else
    240 	if (error == 0)
    241 		printf("NCPU limited to 1 on this host\n");
    242 	numcpu = 1;
    243 #endif
    244 	rump_cpus_bootstrap(numcpu);
    245 
    246 	rumpuser_gettime(&sec, &nsec, &error);
    247 	boottime.tv_sec = sec;
    248 	boottime.tv_nsec = nsec;
    249 
    250 	initmsgbuf(rump_msgbuf, sizeof(rump_msgbuf));
    251 	aprint_verbose("%s%s", copyright, version);
    252 
    253 	/*
    254 	 * Seed arc4random() with a "reasonable" amount of randomness.
    255 	 * Yes, this is a quick kludge which depends on the arc4random
    256 	 * implementation.
    257 	 */
    258 	messthestack();
    259 	arc4random();
    260 
    261 	if (rump_version != RUMP_VERSION) {
    262 		printf("rump version mismatch, %d vs. %d\n",
    263 		    rump_version, RUMP_VERSION);
    264 		return EPROGMISMATCH;
    265 	}
    266 
    267 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    268 		rump_threads = *buf != '0';
    269 	}
    270 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    271 	    rump_threads);
    272 	rump_intr_init();
    273 	rump_tsleep_init();
    274 
    275 	/* init minimal lwp/cpu context */
    276 	l = &lwp0;
    277 	l->l_lid = 1;
    278 	l->l_cpu = l->l_target_cpu = rump_cpu;
    279 	l->l_fd = &filedesc0;
    280 	rumpuser_set_curlwp(l);
    281 
    282 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    283 	rumpuser_mutex_recursive_init(&rump_giantlock);
    284 	ksyms_init();
    285 	uvm_init();
    286 	evcnt_init();
    287 
    288 	once_init();
    289 	kernconfig_lock_init();
    290 	prop_kern_init();
    291 
    292 	pool_subsystem_init();
    293 	kmem_init();
    294 
    295 	uvm_ra_init();
    296 
    297 	mutex_obj_init();
    298 	callout_startup();
    299 
    300 	kprintf_init();
    301 	loginit();
    302 
    303 	kauth_init();
    304 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    305 
    306 	l->l_cred = rump_cred_suserget();
    307 	l->l_proc = &proc0;
    308 
    309 	procinit();
    310 	proc0_init();
    311 	lwpinit_specificdata();
    312 	lwp_initspecific(&lwp0);
    313 
    314 	rump_scheduler_init();
    315 	/* revert temporary context and schedule a real context */
    316 	l->l_cpu = NULL;
    317 	rumpuser_set_curlwp(NULL);
    318 	rump_schedule();
    319 
    320 	percpu_init();
    321 	inittimecounter();
    322 	ntp_init();
    323 
    324 	rumpuser_gettime(&sec, &nsec, &error);
    325 	ts.tv_sec = sec;
    326 	ts.tv_nsec = nsec;
    327 	tc_setclock(&ts);
    328 
    329 	/* we are mostly go.  do per-cpu subsystem init */
    330 	for (i = 0; i < ncpu; i++) {
    331 		struct cpu_info *ci = cpu_lookup(i);
    332 
    333 		callout_init_cpu(ci);
    334 		softint_init(ci);
    335 		xc_init_cpu(ci);
    336 		pool_cache_cpu_init(ci);
    337 		selsysinit(ci);
    338 		percpu_init_cpu(ci);
    339 
    340 		aprint_verbose("cpu%d at thinair0: rump virtual cpu\n", i);
    341 	}
    342 
    343 	sysctl_init();
    344 	kqueue_init();
    345 	iostat_init();
    346 	uid_init();
    347 	fd_sys_init();
    348 	module_init();
    349 	devsw_init();
    350 	pipe_init();
    351 	resource_init();
    352 
    353 	/* start page baroness */
    354 	if (rump_threads) {
    355 		if (kthread_create(PRI_PGDAEMON, KTHREAD_MPSAFE, NULL,
    356 		    uvm_pageout, NULL, &uvm.pagedaemon_lwp, "pdaemon") != 0)
    357 			panic("pagedaemon create failed");
    358 	} else
    359 		uvm.pagedaemon_lwp = NULL; /* doesn't match curlwp */
    360 
    361 	/* process dso's */
    362 	rumpuser_dl_bootstrap(add_linkedin_modules, rump_kernelfsym_load);
    363 
    364 	rump_component_init(RUMP_COMPONENT_KERN);
    365 
    366 	/* these do nothing if not present */
    367 	rump_vfs_init();
    368 	rump_net_init();
    369 	rump_dev_init();
    370 
    371 	rump_component_init(RUMP_COMPONENT_KERN_VFS);
    372 
    373 	cold = 0;
    374 
    375 	/* aieeeedondest */
    376 	if (rump_threads) {
    377 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    378 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    379 			panic("aiodoned");
    380 	}
    381 
    382 	mksysctls();
    383 	sysctl_finalize();
    384 
    385 	module_init_class(MODULE_CLASS_ANY);
    386 
    387 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    388 	hostnamelen = strlen(hostname);
    389 
    390 	sigemptyset(&sigcantmask);
    391 
    392 	if (rump_threads)
    393 		vmem_rehash_start();
    394 
    395 	rump_unschedule();
    396 
    397 	return 0;
    398 }
    399 
    400 /* maybe support sys_reboot some day for remote shutdown */
    401 void
    402 rump_reboot(int howto)
    403 {
    404 
    405 	/* dump means we really take the dive here */
    406 	if ((howto & RB_DUMP) || panicstr) {
    407 		rumpuser_exit(RUMPUSER_PANIC);
    408 		/*NOTREACHED*/
    409 	}
    410 
    411 	/* try to sync */
    412 	if (!((howto & RB_NOSYNC) || panicstr)) {
    413 		rump_vfs_fini();
    414 	}
    415 
    416 	/* your wish is my command */
    417 	if (howto & RB_HALT) {
    418 		for (;;) {
    419 			uint64_t sec = 5, nsec = 0;
    420 			int error;
    421 
    422 			rumpuser_nanosleep(&sec, &nsec, &error);
    423 		}
    424 	}
    425 	rump_inited = -1;
    426 }
    427 
    428 struct uio *
    429 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    430 {
    431 	struct uio *uio;
    432 	enum uio_rw uiorw;
    433 
    434 	switch (rw) {
    435 	case RUMPUIO_READ:
    436 		uiorw = UIO_READ;
    437 		break;
    438 	case RUMPUIO_WRITE:
    439 		uiorw = UIO_WRITE;
    440 		break;
    441 	default:
    442 		panic("%s: invalid rw %d", __func__, rw);
    443 	}
    444 
    445 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    446 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    447 
    448 	uio->uio_iov->iov_base = buf;
    449 	uio->uio_iov->iov_len = bufsize;
    450 
    451 	uio->uio_iovcnt = 1;
    452 	uio->uio_offset = offset;
    453 	uio->uio_resid = bufsize;
    454 	uio->uio_rw = uiorw;
    455 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    456 
    457 	return uio;
    458 }
    459 
    460 size_t
    461 rump_uio_getresid(struct uio *uio)
    462 {
    463 
    464 	return uio->uio_resid;
    465 }
    466 
    467 off_t
    468 rump_uio_getoff(struct uio *uio)
    469 {
    470 
    471 	return uio->uio_offset;
    472 }
    473 
    474 size_t
    475 rump_uio_free(struct uio *uio)
    476 {
    477 	size_t resid;
    478 
    479 	resid = uio->uio_resid;
    480 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    481 	kmem_free(uio, sizeof(*uio));
    482 
    483 	return resid;
    484 }
    485 
    486 static pid_t nextpid = 1;
    487 struct lwp *
    488 rump_newproc_switch()
    489 {
    490 	struct lwp *l;
    491 	pid_t mypid;
    492 
    493 	mypid = atomic_inc_uint_nv(&nextpid);
    494 	if (__predict_false(mypid == 0))
    495 		mypid = atomic_inc_uint_nv(&nextpid);
    496 
    497 	l = rump_lwp_alloc(mypid, 0);
    498 	rump_lwp_switch(l);
    499 
    500 	return l;
    501 }
    502 
    503 struct lwp *
    504 rump_lwp_alloc_and_switch(pid_t pid, lwpid_t lid)
    505 {
    506 	struct lwp *l;
    507 
    508 	l = rump_lwp_alloc(pid, lid);
    509 	rump_lwp_switch(l);
    510 
    511 	return l;
    512 }
    513 
    514 struct lwp *
    515 rump_lwp_alloc(pid_t pid, lwpid_t lid)
    516 {
    517 	struct lwp *l;
    518 	struct proc *p;
    519 
    520 	l = kmem_zalloc(sizeof(*l), KM_SLEEP);
    521 	if (pid != 0) {
    522 		p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    523 		if (rump_proc_vfs_init)
    524 			rump_proc_vfs_init(p);
    525 		p->p_stats = proc0.p_stats; /* XXX */
    526 		p->p_limit = lim_copy(proc0.p_limit);
    527 		p->p_pid = pid;
    528 		p->p_vmspace = &vmspace0;
    529 		p->p_emul = &emul_netbsd;
    530 		p->p_fd = fd_init(NULL);
    531 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    532 		p->p_pgrp = &rump_pgrp;
    533 		l->l_cred = rump_cred_suserget();
    534 
    535 		atomic_inc_uint(&nprocs);
    536 	} else {
    537 		p = &proc0;
    538 		l->l_cred = rump_susercred;
    539 	}
    540 
    541 	l->l_proc = p;
    542 	l->l_lid = lid;
    543 	l->l_fd = p->p_fd;
    544 	if (pid == 0)
    545 		fd_hold(l);
    546 	l->l_cpu = NULL;
    547 	l->l_target_cpu = rump_cpu;
    548 	lwp_initspecific(l);
    549 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    550 
    551 	return l;
    552 }
    553 
    554 void
    555 rump_lwp_switch(struct lwp *newlwp)
    556 {
    557 	struct lwp *l = curlwp;
    558 
    559 	rumpuser_set_curlwp(NULL);
    560 	newlwp->l_cpu = newlwp->l_target_cpu = l->l_cpu;
    561 	newlwp->l_mutex = l->l_mutex;
    562 	l->l_mutex = NULL;
    563 	l->l_cpu = NULL;
    564 	rumpuser_set_curlwp(newlwp);
    565 	if (l->l_flag & LW_WEXIT)
    566 		rump_lwp_free(l);
    567 }
    568 
    569 /* XXX: this has effect only on non-pid0 lwps */
    570 void
    571 rump_lwp_release(struct lwp *l)
    572 {
    573 	struct proc *p;
    574 
    575 	p = l->l_proc;
    576 	if (p->p_pid != 0) {
    577 		mutex_obj_free(p->p_lock);
    578 		fd_free();
    579 		if (rump_proc_vfs_release)
    580 			rump_proc_vfs_release(p);
    581 		rump_cred_put(l->l_cred);
    582 		limfree(p->p_limit);
    583 		kmem_free(p, sizeof(*p));
    584 
    585 		atomic_dec_uint(&nprocs);
    586 	} else {
    587 		fd_free();
    588 	}
    589 	KASSERT((l->l_flag & LW_WEXIT) == 0);
    590 	l->l_flag |= LW_WEXIT;
    591 }
    592 
    593 void
    594 rump_lwp_free(struct lwp *l)
    595 {
    596 
    597 	KASSERT(l->l_flag & LW_WEXIT);
    598 	KASSERT(l->l_mutex == NULL);
    599 	if (l->l_name)
    600 		kmem_free(l->l_name, MAXCOMLEN);
    601 	lwp_finispecific(l);
    602 	LIST_REMOVE(l, l_list);
    603 	kmem_free(l, sizeof(*l));
    604 }
    605 
    606 struct lwp *
    607 rump_lwp_curlwp(void)
    608 {
    609 	struct lwp *l = curlwp;
    610 
    611 	if (l->l_flag & LW_WEXIT)
    612 		return NULL;
    613 	return l;
    614 }
    615 
    616 /* rump private.  NEEDS WORK! */
    617 void
    618 rump_set_vmspace(struct vmspace *vm)
    619 {
    620 	struct proc *p = curproc;
    621 
    622 	p->p_vmspace = vm;
    623 }
    624 
    625 kauth_cred_t
    626 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    627 {
    628 	kauth_cred_t cred;
    629 	int rv;
    630 
    631 	cred = kauth_cred_alloc();
    632 	kauth_cred_setuid(cred, uid);
    633 	kauth_cred_seteuid(cred, uid);
    634 	kauth_cred_setsvuid(cred, uid);
    635 	kauth_cred_setgid(cred, gid);
    636 	kauth_cred_setgid(cred, gid);
    637 	kauth_cred_setegid(cred, gid);
    638 	kauth_cred_setsvgid(cred, gid);
    639 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    640 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    641 	assert(rv == 0);
    642 
    643 	return cred;
    644 }
    645 
    646 void
    647 rump_cred_put(kauth_cred_t cred)
    648 {
    649 
    650 	kauth_cred_free(cred);
    651 }
    652 
    653 kauth_cred_t
    654 rump_cred_suserget(void)
    655 {
    656 
    657 	kauth_cred_hold(rump_susercred);
    658 	return rump_susercred;
    659 }
    660 
    661 /*
    662  * Return the next system lwpid
    663  */
    664 lwpid_t
    665 rump_nextlid(void)
    666 {
    667 	lwpid_t retid;
    668 
    669 	mutex_enter(proc0.p_lock);
    670 	/*
    671 	 * Take next one, don't return 0
    672 	 * XXX: most likely we'll have collisions in case this
    673 	 * wraps around.
    674 	 */
    675 	if (++proc0.p_nlwpid == 0)
    676 		++proc0.p_nlwpid;
    677 	retid = proc0.p_nlwpid;
    678 	mutex_exit(proc0.p_lock);
    679 
    680 	return retid;
    681 }
    682 
    683 static int compcounter[RUMP_COMPONENT_MAX];
    684 
    685 static void
    686 rump_component_init_cb(struct rump_component *rc, int type)
    687 {
    688 
    689 	KASSERT(type < RUMP_COMPONENT_MAX);
    690 	if (rc->rc_type == type) {
    691 		rc->rc_init();
    692 		compcounter[type]++;
    693 	}
    694 }
    695 
    696 int
    697 rump_component_count(enum rump_component_type type)
    698 {
    699 
    700 	KASSERT(type <= RUMP_COMPONENT_MAX);
    701 	return compcounter[type];
    702 }
    703 
    704 void
    705 rump_component_init(enum rump_component_type type)
    706 {
    707 
    708 	rumpuser_dl_component_init(type, rump_component_init_cb);
    709 }
    710 
    711 /*
    712  * Initialize a module which has already been loaded and linked
    713  * with dlopen(). This is fundamentally the same as a builtin module.
    714  */
    715 int
    716 rump_module_init(const struct modinfo * const *mip, size_t nmodinfo)
    717 {
    718 
    719 	return module_builtin_add(mip, nmodinfo, true);
    720 }
    721 
    722 /*
    723  * Finish module (flawless victory, fatality!).
    724  */
    725 int
    726 rump_module_fini(const struct modinfo *mi)
    727 {
    728 
    729 	return module_builtin_remove(mi, true);
    730 }
    731 
    732 /*
    733  * Add loaded and linked module to the builtin list.  It will
    734  * later be initialized with module_init_class().
    735  */
    736 
    737 static void
    738 add_linkedin_modules(const struct modinfo * const *mip, size_t nmodinfo)
    739 {
    740 
    741 	module_builtin_add(mip, nmodinfo, false);
    742 }
    743 
    744 int
    745 rump_kernelfsym_load(void *symtab, uint64_t symsize,
    746 	char *strtab, uint64_t strsize)
    747 {
    748 	static int inited = 0;
    749 	Elf64_Ehdr ehdr;
    750 
    751 	if (inited)
    752 		return EBUSY;
    753 	inited = 1;
    754 
    755 	/*
    756 	 * Use 64bit header since it's bigger.  Shouldn't make a
    757 	 * difference, since we're passing in all zeroes anyway.
    758 	 */
    759 	memset(&ehdr, 0, sizeof(ehdr));
    760 	ksyms_addsyms_explicit(&ehdr, symtab, symsize, strtab, strsize);
    761 
    762 	return 0;
    763 }
    764 
    765 static int
    766 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    767 	register_t *retval)
    768 {
    769 	struct lwp *l;
    770 	struct sysent *callp;
    771 	int rv;
    772 
    773 	if (__predict_false(num >= SYS_NSYSENT))
    774 		return ENOSYS;
    775 
    776 	callp = rump_sysent + num;
    777 	rump_schedule();
    778 	l = curlwp;
    779 	rv = sy_call(callp, l, (void *)data, retval);
    780 	rump_unschedule();
    781 
    782 	return rv;
    783 }
    784 
    785 int
    786 rump_boot_gethowto()
    787 {
    788 
    789 	return boothowto;
    790 }
    791 
    792 void
    793 rump_boot_sethowto(int howto)
    794 {
    795 
    796 	boothowto = howto;
    797 }
    798 
    799 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    800 void *rump_sysproxy_arg;
    801 
    802 /*
    803  * This whole syscall-via-rpc is still taking form.  For example, it
    804  * may be necessary to set syscalls individually instead of lobbing
    805  * them all to the same place.  So don't think this interface is
    806  * set in stone.
    807  */
    808 int
    809 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    810 {
    811 
    812 	if (rump_sysproxy_arg)
    813 		return EBUSY;
    814 
    815 	rump_sysproxy_arg = arg;
    816 	rump_sysproxy = proxy;
    817 
    818 	return 0;
    819 }
    820 
    821 int
    822 rump_getversion(void)
    823 {
    824 
    825 	return __NetBSD_Version__;
    826 }
    827 
    828 /*
    829  * Note: may be called unscheduled.  Not fully safe since no locking
    830  * of allevents (currently that's not even available).
    831  */
    832 void
    833 rump_printevcnts()
    834 {
    835 	struct evcnt *ev;
    836 
    837 	TAILQ_FOREACH(ev, &allevents, ev_list)
    838 		rumpuser_dprintf("%s / %s: %" PRIu64 "\n",
    839 		    ev->ev_group, ev->ev_name, ev->ev_count);
    840 }
    841