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