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