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