Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.124
      1 /*	$NetBSD: rump.c,v 1.124 2009/10/15 00:28:46 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.124 2009/10/15 00:28:46 pooka Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/atomic.h>
     35 #include <sys/buf.h>
     36 #include <sys/callout.h>
     37 #include <sys/conf.h>
     38 #include <sys/cpu.h>
     39 #include <sys/evcnt.h>
     40 #include <sys/event.h>
     41 #include <sys/filedesc.h>
     42 #include <sys/iostat.h>
     43 #include <sys/kauth.h>
     44 #include <sys/kernel.h>
     45 #include <sys/kmem.h>
     46 #include <sys/kprintf.h>
     47 #include <sys/ksyms.h>
     48 #include <sys/msgbuf.h>
     49 #include <sys/module.h>
     50 #include <sys/once.h>
     51 #include <sys/percpu.h>
     52 #include <sys/queue.h>
     53 #include <sys/reboot.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/select.h>
     56 #include <sys/sysctl.h>
     57 #include <sys/syscall.h>
     58 #include <sys/tty.h>
     59 #include <sys/uidinfo.h>
     60 #include <sys/vmem.h>
     61 
     62 #include <rump/rumpuser.h>
     63 
     64 #include <secmodel/suser/suser.h>
     65 
     66 #include <prop/proplib.h>
     67 
     68 #include "rump_private.h"
     69 #include "rump_net_private.h"
     70 #include "rump_vfs_private.h"
     71 #include "rump_dev_private.h"
     72 
     73 struct proc proc0;
     74 struct session rump_session = {
     75 	.s_count = 1,
     76 	.s_flags = 0,
     77 	.s_leader = &proc0,
     78 	.s_login = "rumphobo",
     79 	.s_sid = 0,
     80 };
     81 struct pgrp rump_pgrp = {
     82 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     83 	.pg_session = &rump_session,
     84 	.pg_jobc = 1,
     85 };
     86 struct pstats rump_stats;
     87 struct plimit rump_limits;
     88 struct filedesc rump_filedesc0;
     89 struct proclist allproc;
     90 char machine[] = "rump";
     91 static kauth_cred_t rump_susercred;
     92 
     93 /* pretend the master rump proc is init */
     94 struct proc *initproc = &proc0;
     95 
     96 struct rumpuser_mtx *rump_giantlock;
     97 
     98 sigset_t sigcantmask;
     99 
    100 #ifdef RUMP_WITHOUT_THREADS
    101 int rump_threads = 0;
    102 #else
    103 int rump_threads = 1;
    104 #endif
    105 
    106 static void
    107 rump_aiodone_worker(struct work *wk, void *dummy)
    108 {
    109 	struct buf *bp = (struct buf *)wk;
    110 
    111 	KASSERT(&bp->b_work == wk);
    112 	bp->b_iodone(bp);
    113 }
    114 
    115 static int rump_inited;
    116 static struct emul emul_rump;
    117 
    118 void rump__unavailable(void);
    119 void rump__unavailable() {}
    120 __weak_alias(rump_net_init,rump__unavailable);
    121 __weak_alias(rump_vfs_init,rump__unavailable);
    122 __weak_alias(rump_dev_init,rump__unavailable);
    123 
    124 __weak_alias(rump_vfs_fini,rump__unavailable);
    125 
    126 __weak_alias(biodone,rump__unavailable);
    127 
    128 void rump__unavailable_vfs_panic(void);
    129 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    130 __weak_alias(vn_open,rump__unavailable_vfs_panic);
    131 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
    132 __weak_alias(vn_stat,rump__unavailable_vfs_panic);
    133 __weak_alias(vn_close,rump__unavailable_vfs_panic);
    134 __weak_alias(namei,rump__unavailable_vfs_panic);
    135 
    136 static void
    137 pvfsinit_nop(struct proc *p)
    138 {
    139 
    140 	return;
    141 }
    142 
    143 static void
    144 pvfsrele_nop(struct proc *p)
    145 {
    146 
    147 	return;
    148 }
    149 
    150 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
    151 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
    152 
    153 /*
    154  * Stir up the stack a bit.  These are exported functions to help
    155  * convince the compiler that we don't want these routines completely
    156  * optimized out or inlined.  Is there an easier way to do this?
    157  */
    158 void nullfn(uint32_t *);
    159 void nullfn(uint32_t *arg){}
    160 void messthestack(void);
    161 void
    162 messthestack(void)
    163 {
    164 	uint32_t mess[64];
    165 	uint64_t d1, d2;
    166 	int i, error;
    167 
    168 	for (i = 0; i < 64; i++) {
    169 		rumpuser_gettime(&d1, &d2, &error);
    170 		mess[i] = d2;
    171 	}
    172 	nullfn(mess);
    173 }
    174 
    175 int
    176 rump__init(int rump_version)
    177 {
    178 	char buf[256];
    179 	struct proc *p;
    180 	struct lwp *l;
    181 	int error;
    182 
    183 	/* not reentrant */
    184 	if (rump_inited)
    185 		return 0;
    186 	else if (rump_inited == -1)
    187 		panic("rump_init: host process restart required");
    188 	else
    189 		rump_inited = 1;
    190 
    191 	/*
    192 	 * Seed arc4random() with a "reasonable" amount of randomness.
    193 	 * Yes, this is a quick kludge which depends on the arc4random
    194 	 * implementation.
    195 	 */
    196 	messthestack();
    197 	arc4random();
    198 
    199 	if (rump_version != RUMP_VERSION) {
    200 		printf("rump version mismatch, %d vs. %d\n",
    201 		    rump_version, RUMP_VERSION);
    202 		return EPROGMISMATCH;
    203 	}
    204 
    205 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    206 		rump_threads = *buf != '0';
    207 	}
    208 	rumpuser_thrinit(rump_user_schedule, rump_user_unschedule,
    209 	    rump_threads);
    210 
    211 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    212 	rumpuser_mutex_recursive_init(&rump_giantlock);
    213 	ksyms_init();
    214 	rumpvm_init();
    215 	evcnt_init();
    216 
    217 	once_init();
    218 	prop_kern_init();
    219 
    220 	rump_sleepers_init();
    221 
    222 	pool_subsystem_init();
    223 	kmem_init();
    224 
    225 	kprintf_init();
    226 	loginit();
    227 
    228 	kauth_init();
    229 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    230 
    231 	l = &lwp0;
    232 	p = &proc0;
    233 	p->p_stats = &rump_stats;
    234 	p->p_limit = &rump_limits;
    235 	p->p_pgrp = &rump_pgrp;
    236 	p->p_pid = 0;
    237 	p->p_fd = &rump_filedesc0;
    238 	p->p_vmspace = &rump_vmspace;
    239 	p->p_emul = &emul_rump;
    240 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    241 	l->l_cred = rump_cred_suserget();
    242 	l->l_proc = p;
    243 	l->l_lid = 1;
    244 	LIST_INIT(&allproc);
    245 	LIST_INSERT_HEAD(&allproc, &proc0, p_list);
    246 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    247 
    248 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    249 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    250 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    251 
    252 	rump_scheduler_init();
    253 	rump_schedule();
    254 
    255 	callout_startup();
    256 	callout_init_cpu(rump_cpu);
    257 
    258 	sysctl_init();
    259 	kqueue_init();
    260 	iostat_init();
    261 	uid_init();
    262 	percpu_init();
    263 	fd_sys_init();
    264 	module_init();
    265 	softint_init(rump_cpu);
    266 	devsw_init();
    267 
    268 	/* these do nothing if not present */
    269 	rump_vfs_init();
    270 	rump_net_init();
    271 	rump_dev_init();
    272 	cold = 0;
    273 
    274 	/* aieeeedondest */
    275 	if (rump_threads) {
    276 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    277 		    rump_aiodone_worker, NULL, 0, 0, WQ_MPSAFE))
    278 			panic("aiodoned");
    279 	}
    280 
    281 	sysctl_finalize();
    282 
    283 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    284 	hostnamelen = strlen(hostname);
    285 
    286 	sigemptyset(&sigcantmask);
    287 
    288 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    289 
    290 #ifdef RUMP_USE_REAL_ALLOCATORS
    291 	if (rump_threads)
    292 		vmem_rehash_start();
    293 #endif
    294 
    295 	/*
    296 	 * Module bootstrap makes calls back into rump, so
    297 	 * unschedule before calling.
    298 	 */
    299 	rump_unschedule();
    300 	rumpuser_dl_module_bootstrap();
    301 
    302 	return 0;
    303 }
    304 
    305 /* maybe support sys_reboot some day for remote shutdown */
    306 void
    307 rump_reboot(int howto)
    308 {
    309 
    310 	/* dump means we really take the dive here */
    311 	if ((howto & RB_DUMP) || panicstr) {
    312 		rumpuser_exit(RUMPUSER_PANIC);
    313 		/*NOTREACHED*/
    314 	}
    315 
    316 	/* try to sync */
    317 	if (!((howto & RB_NOSYNC) || panicstr)) {
    318 		rump_vfs_fini();
    319 	}
    320 
    321 	/* your wish is my command */
    322 	if (howto & RB_HALT) {
    323 		for (;;) {
    324 			uint64_t sec = 5, nsec = 0;
    325 			int error;
    326 
    327 			rumpuser_nanosleep(&sec, &nsec, &error);
    328 		}
    329 	}
    330 	rump_inited = -1;
    331 }
    332 
    333 struct uio *
    334 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    335 {
    336 	struct uio *uio;
    337 	enum uio_rw uiorw;
    338 
    339 	switch (rw) {
    340 	case RUMPUIO_READ:
    341 		uiorw = UIO_READ;
    342 		break;
    343 	case RUMPUIO_WRITE:
    344 		uiorw = UIO_WRITE;
    345 		break;
    346 	default:
    347 		panic("%s: invalid rw %d", __func__, rw);
    348 	}
    349 
    350 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    351 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    352 
    353 	uio->uio_iov->iov_base = buf;
    354 	uio->uio_iov->iov_len = bufsize;
    355 
    356 	uio->uio_iovcnt = 1;
    357 	uio->uio_offset = offset;
    358 	uio->uio_resid = bufsize;
    359 	uio->uio_rw = uiorw;
    360 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    361 
    362 	return uio;
    363 }
    364 
    365 size_t
    366 rump_uio_getresid(struct uio *uio)
    367 {
    368 
    369 	return uio->uio_resid;
    370 }
    371 
    372 off_t
    373 rump_uio_getoff(struct uio *uio)
    374 {
    375 
    376 	return uio->uio_offset;
    377 }
    378 
    379 size_t
    380 rump_uio_free(struct uio *uio)
    381 {
    382 	size_t resid;
    383 
    384 	resid = uio->uio_resid;
    385 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    386 	kmem_free(uio, sizeof(*uio));
    387 
    388 	return resid;
    389 }
    390 
    391 /* public interface */
    392 static pid_t nextpid = 1;
    393 struct lwp *
    394 rump_newproc_switch()
    395 {
    396 	struct lwp *oldlwp = curlwp;
    397 	pid_t mypid;
    398 
    399 	mypid = atomic_inc_uint_nv(&nextpid);
    400 	if (__predict_false(mypid == 0))
    401 		mypid = atomic_inc_uint_nv(&nextpid);
    402 
    403 	rumpuser_set_curlwp(NULL);
    404 	rump_setup_curlwp(mypid, 0, 1);
    405 
    406 	return oldlwp;
    407 }
    408 
    409 /* rump private */
    410 struct lwp *
    411 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    412 {
    413 	struct lwp *l;
    414 	struct proc *p;
    415 
    416 	l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
    417 	if (pid != 0) {
    418 		p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
    419 		rump_proc_vfs_init(p);
    420 		p->p_stats = &rump_stats;
    421 		p->p_limit = &rump_limits;
    422 		p->p_pid = pid;
    423 		p->p_vmspace = &rump_vmspace;
    424 		p->p_fd = fd_init(NULL);
    425 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    426 	} else {
    427 		p = &proc0;
    428 	}
    429 
    430 	l->l_cred = rump_cred_suserget();
    431 	l->l_proc = p;
    432 	l->l_lid = lid;
    433 	l->l_fd = p->p_fd;
    434 	l->l_mutex = RUMP_LMUTEX_MAGIC;
    435 	l->l_cpu = NULL;
    436 
    437 	/* give current cpu to new lwp */
    438 	if (set) {
    439 		struct cpu_info *saveci = curlwp->l_cpu;
    440 
    441 		curlwp->l_cpu = NULL;
    442 		rumpuser_set_curlwp(l);
    443 		curlwp->l_cpu = saveci;
    444 	}
    445 
    446 	return l;
    447 }
    448 
    449 /* rump private.  NEEDS WORK! */
    450 void
    451 rump_set_vmspace(struct vmspace *vm)
    452 {
    453 	struct proc *p = curproc;
    454 
    455 	p->p_vmspace = vm;
    456 }
    457 
    458 void
    459 rump_clear_curlwp(void)
    460 {
    461 	struct cpu_info *ci;
    462 	struct lwp *l;
    463 	struct proc *p;
    464 
    465 	l = rumpuser_get_curlwp();
    466 	ci = l->l_cpu;
    467 	KASSERT(ci);
    468 	p = l->l_proc;
    469 	if (p->p_pid != 0) {
    470 		mutex_obj_free(p->p_lock);
    471 		fd_free();
    472 		rump_proc_vfs_release(p);
    473 		rump_cred_put(l->l_cred);
    474 		kmem_free(p, sizeof(*p));
    475 	}
    476 	kmem_free(l, sizeof(*l));
    477 
    478 	/* lwp0 inherits current cpu.  quite XXX */
    479 	rumpuser_set_curlwp(NULL);
    480 	lwp0.l_cpu = ci;
    481 }
    482 
    483 struct lwp *
    484 rump_get_curlwp(void)
    485 {
    486 	struct lwp *l;
    487 
    488 	l = rumpuser_get_curlwp();
    489 	if (l == NULL)
    490 		l = &lwp0;
    491 
    492 	return l;
    493 }
    494 
    495 void
    496 rump_set_curlwp(struct lwp *l)
    497 {
    498 
    499 	/* clear current */
    500 	rumpuser_set_curlwp(NULL);
    501 	/* set new */
    502 	rumpuser_set_curlwp(l);
    503 }
    504 
    505 kauth_cred_t
    506 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    507 {
    508 	kauth_cred_t cred;
    509 	int rv;
    510 
    511 	cred = kauth_cred_alloc();
    512 	kauth_cred_setuid(cred, uid);
    513 	kauth_cred_seteuid(cred, uid);
    514 	kauth_cred_setsvuid(cred, uid);
    515 	kauth_cred_setgid(cred, gid);
    516 	kauth_cred_setgid(cred, gid);
    517 	kauth_cred_setegid(cred, gid);
    518 	kauth_cred_setsvgid(cred, gid);
    519 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    520 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    521 	assert(rv == 0);
    522 
    523 	return cred;
    524 }
    525 
    526 void
    527 rump_cred_put(kauth_cred_t cred)
    528 {
    529 
    530 	kauth_cred_free(cred);
    531 }
    532 
    533 kauth_cred_t
    534 rump_cred_suserget(void)
    535 {
    536 
    537 	kauth_cred_hold(rump_susercred);
    538 	return rump_susercred;
    539 }
    540 
    541 /*
    542  * Return the next system lwpid
    543  */
    544 lwpid_t
    545 rump_nextlid(void)
    546 {
    547 	lwpid_t retid;
    548 
    549 	mutex_enter(proc0.p_lock);
    550 	/*
    551 	 * Take next one, don't return 0
    552 	 * XXX: most likely we'll have collisions in case this
    553 	 * wraps around.
    554 	 */
    555 	if (++proc0.p_nlwpid == 0)
    556 		++proc0.p_nlwpid;
    557 	retid = proc0.p_nlwpid;
    558 	mutex_exit(proc0.p_lock);
    559 
    560 	return retid;
    561 }
    562 
    563 int
    564 rump_module_init(struct modinfo *mi, prop_dictionary_t props)
    565 {
    566 	int rv;
    567 
    568 	if (!module_compatible(mi->mi_version, __NetBSD_Version__))
    569 		return EPROGMISMATCH;
    570 
    571 	rv = mi->mi_modcmd(MODULE_CMD_INIT, props);
    572 	if (rv == 0 && mi->mi_class == MODULE_CLASS_SECMODEL)
    573 		secmodel_register();
    574 
    575 	return rv;
    576 }
    577 
    578 int
    579 rump_module_fini(struct modinfo *mi)
    580 {
    581 	int rv;
    582 
    583 	rv = mi->mi_modcmd(MODULE_CMD_FINI, NULL);
    584 	if (rv == 0 && mi->mi_class == MODULE_CLASS_SECMODEL)
    585 		secmodel_deregister();
    586 
    587 	return rv;
    588 }
    589 
    590 static int
    591 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    592 	register_t *retval)
    593 {
    594 	struct lwp *l;
    595 	struct sysent *callp;
    596 	int rv;
    597 
    598 	if (__predict_false(num >= SYS_NSYSENT))
    599 		return ENOSYS;
    600 
    601 	l = curlwp;
    602 	callp = rump_sysent + num;
    603 	rump_schedule();
    604 	rv = callp->sy_call(l, (void *)data, retval);
    605 	rump_unschedule();
    606 
    607 	return rv;
    608 }
    609 
    610 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    611 void *rump_sysproxy_arg;
    612 
    613 /*
    614  * This whole syscall-via-rpc is still taking form.  For example, it
    615  * may be necessary to set syscalls individually instead of lobbing
    616  * them all to the same place.  So don't think this interface is
    617  * set in stone.
    618  */
    619 int
    620 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    621 {
    622 
    623 	if (rump_sysproxy_arg)
    624 		return EBUSY;
    625 
    626 	rump_sysproxy_arg = arg;
    627 	rump_sysproxy = proxy;
    628 
    629 	return 0;
    630 }
    631 
    632 int
    633 rump_getversion(void)
    634 {
    635 
    636 	return __NetBSD_Version__;
    637 }
    638