Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.116
      1 /*	$NetBSD: rump.c,v 1.116 2009/09/24 21:30:42 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.116 2009/09/24 21:30:42 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 	rumpuser_dl_module_bootstrap();
    288 
    289 	return 0;
    290 }
    291 
    292 struct uio *
    293 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    294 {
    295 	struct uio *uio;
    296 	enum uio_rw uiorw;
    297 
    298 	switch (rw) {
    299 	case RUMPUIO_READ:
    300 		uiorw = UIO_READ;
    301 		break;
    302 	case RUMPUIO_WRITE:
    303 		uiorw = UIO_WRITE;
    304 		break;
    305 	default:
    306 		panic("%s: invalid rw %d", __func__, rw);
    307 	}
    308 
    309 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    310 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    311 
    312 	uio->uio_iov->iov_base = buf;
    313 	uio->uio_iov->iov_len = bufsize;
    314 
    315 	uio->uio_iovcnt = 1;
    316 	uio->uio_offset = offset;
    317 	uio->uio_resid = bufsize;
    318 	uio->uio_rw = uiorw;
    319 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    320 
    321 	return uio;
    322 }
    323 
    324 size_t
    325 rump_uio_getresid(struct uio *uio)
    326 {
    327 
    328 	return uio->uio_resid;
    329 }
    330 
    331 off_t
    332 rump_uio_getoff(struct uio *uio)
    333 {
    334 
    335 	return uio->uio_offset;
    336 }
    337 
    338 size_t
    339 rump_uio_free(struct uio *uio)
    340 {
    341 	size_t resid;
    342 
    343 	resid = uio->uio_resid;
    344 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    345 	kmem_free(uio, sizeof(*uio));
    346 
    347 	return resid;
    348 }
    349 
    350 /* public interface */
    351 static pid_t nextpid = 1;
    352 struct lwp *
    353 rump_newproc_switch()
    354 {
    355 	struct lwp *oldlwp = curlwp;
    356 	pid_t mypid;
    357 
    358 	mypid = atomic_inc_uint_nv(&nextpid);
    359 	if (__predict_false(mypid == 0))
    360 		mypid = atomic_inc_uint_nv(&nextpid);
    361 
    362 	rumpuser_set_curlwp(NULL);
    363 	rump_setup_curlwp(mypid, 0, 1);
    364 
    365 	return oldlwp;
    366 }
    367 
    368 /* rump private */
    369 struct lwp *
    370 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    371 {
    372 	struct lwp *l;
    373 	struct proc *p;
    374 
    375 	l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
    376 	if (pid != 0) {
    377 		p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
    378 		rump_proc_vfs_init(p);
    379 		p->p_stats = &rump_stats;
    380 		p->p_limit = &rump_limits;
    381 		p->p_pid = pid;
    382 		p->p_vmspace = &rump_vmspace;
    383 		p->p_fd = fd_init(NULL);
    384 		p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    385 	} else {
    386 		p = &proc0;
    387 	}
    388 
    389 	l->l_cred = rump_cred_suserget();
    390 	l->l_proc = p;
    391 	l->l_lid = lid;
    392 	l->l_fd = p->p_fd;
    393 	l->l_mutex = RUMP_LMUTEX_MAGIC;
    394 	l->l_cpu = &rump_cpu;
    395 
    396 	if (set)
    397 		rumpuser_set_curlwp(l);
    398 
    399 	return l;
    400 }
    401 
    402 /* rump private.  NEEDS WORK! */
    403 void
    404 rump_set_vmspace(struct vmspace *vm)
    405 {
    406 	struct proc *p = curproc;
    407 
    408 	p->p_vmspace = vm;
    409 }
    410 
    411 void
    412 rump_clear_curlwp(void)
    413 {
    414 	struct lwp *l;
    415 	struct proc *p;
    416 
    417 	l = rumpuser_get_curlwp();
    418 	p = l->l_proc;
    419 	if (p->p_pid != 0) {
    420 		mutex_obj_free(p->p_lock);
    421 		fd_free();
    422 		rump_proc_vfs_release(p);
    423 		rump_cred_put(l->l_cred);
    424 		kmem_free(p, sizeof(*p));
    425 	}
    426 	kmem_free(l, sizeof(*l));
    427 	rumpuser_set_curlwp(NULL);
    428 }
    429 
    430 struct lwp *
    431 rump_get_curlwp(void)
    432 {
    433 	struct lwp *l;
    434 
    435 	l = rumpuser_get_curlwp();
    436 	if (l == NULL)
    437 		l = &lwp0;
    438 
    439 	return l;
    440 }
    441 
    442 void
    443 rump_set_curlwp(struct lwp *l)
    444 {
    445 
    446 	/* clear current */
    447 	rumpuser_set_curlwp(NULL);
    448 	/* set new */
    449 	rumpuser_set_curlwp(l);
    450 }
    451 
    452 kauth_cred_t
    453 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    454 {
    455 	kauth_cred_t cred;
    456 	int rv;
    457 
    458 	cred = kauth_cred_alloc();
    459 	kauth_cred_setuid(cred, uid);
    460 	kauth_cred_seteuid(cred, uid);
    461 	kauth_cred_setsvuid(cred, uid);
    462 	kauth_cred_setgid(cred, gid);
    463 	kauth_cred_setgid(cred, gid);
    464 	kauth_cred_setegid(cred, gid);
    465 	kauth_cred_setsvgid(cred, gid);
    466 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    467 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    468 	assert(rv == 0);
    469 
    470 	return cred;
    471 }
    472 
    473 void
    474 rump_cred_put(kauth_cred_t cred)
    475 {
    476 
    477 	kauth_cred_free(cred);
    478 }
    479 
    480 kauth_cred_t
    481 rump_cred_suserget(void)
    482 {
    483 
    484 	kauth_cred_hold(rump_susercred);
    485 	return rump_susercred;
    486 }
    487 
    488 /*
    489  * Return the next system lwpid
    490  */
    491 lwpid_t
    492 rump_nextlid(void)
    493 {
    494 	lwpid_t retid;
    495 
    496 	mutex_enter(proc0.p_lock);
    497 	/*
    498 	 * Take next one, don't return 0
    499 	 * XXX: most likely we'll have collisions in case this
    500 	 * wraps around.
    501 	 */
    502 	if (++proc0.p_nlwpid == 0)
    503 		++proc0.p_nlwpid;
    504 	retid = proc0.p_nlwpid;
    505 	mutex_exit(proc0.p_lock);
    506 
    507 	return retid;
    508 }
    509 
    510 int
    511 rump_module_init(struct modinfo *mi, prop_dictionary_t props)
    512 {
    513 
    514 	if (!module_compatible(mi->mi_version, __NetBSD_Version__))
    515 		return EPROGMISMATCH;
    516 
    517 	return mi->mi_modcmd(MODULE_CMD_INIT, props);
    518 }
    519 
    520 int
    521 rump_module_fini(struct modinfo *mi)
    522 {
    523 
    524 	return mi->mi_modcmd(MODULE_CMD_FINI, NULL);
    525 }
    526 
    527 int _syspuffs_stub(int, int *);
    528 int
    529 _syspuffs_stub(int fd, int *newfd)
    530 {
    531 
    532 	return ENODEV;
    533 }
    534 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
    535 
    536 static int
    537 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    538 	register_t *retval)
    539 {
    540 	struct lwp *l;
    541 	struct sysent *callp;
    542 
    543 	if (__predict_false(num >= SYS_NSYSENT))
    544 		return ENOSYS;
    545 
    546 	l = curlwp;
    547 	callp = rump_sysent + num;
    548 	return callp->sy_call(l, (void *)data, retval);
    549 }
    550 
    551 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    552 void *rump_sysproxy_arg;
    553 
    554 /*
    555  * This whole syscall-via-rpc is still taking form.  For example, it
    556  * may be necessary to set syscalls individually instead of lobbing
    557  * them all to the same place.  So don't think this interface is
    558  * set in stone.
    559  */
    560 int
    561 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    562 {
    563 
    564 	if (rump_sysproxy_arg)
    565 		return EBUSY;
    566 
    567 	rump_sysproxy_arg = arg;
    568 	rump_sysproxy = proxy;
    569 
    570 	return 0;
    571 }
    572 
    573 int
    574 rump_getversion()
    575 {
    576 
    577 	return __NetBSD_Version__;
    578 }
    579