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