Home | History | Annotate | Line # | Download | only in rumpkern
rump.c revision 1.101
      1 /*	$NetBSD: rump.c,v 1.101 2009/04/16 14:07:18 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.101 2009/04/16 14:07:18 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 
     69 struct proc proc0;
     70 struct session rump_session = {
     71 	.s_count = 1,
     72 	.s_flags = 0,
     73 	.s_leader = &proc0,
     74 	.s_login = "rumphobo",
     75 	.s_sid = 0,
     76 };
     77 struct pgrp rump_pgrp = {
     78 	.pg_members = LIST_HEAD_INITIALIZER(pg_members),
     79 	.pg_session = &rump_session,
     80 	.pg_jobc = 1,
     81 };
     82 struct pstats rump_stats;
     83 struct plimit rump_limits;
     84 struct cpu_info rump_cpu;
     85 struct filedesc rump_filedesc0;
     86 struct proclist allproc;
     87 char machine[] = "rump";
     88 static kauth_cred_t rump_susercred;
     89 
     90 /* pretend the master rump proc is init */
     91 struct proc *initproc = &proc0;
     92 
     93 struct rumpuser_mtx *rump_giantlock;
     94 
     95 sigset_t sigcantmask;
     96 
     97 #ifdef RUMP_WITHOUT_THREADS
     98 int rump_threads = 0;
     99 #else
    100 int rump_threads = 1;
    101 #endif
    102 
    103 static void
    104 rump_aiodone_worker(struct work *wk, void *dummy)
    105 {
    106 	struct buf *bp = (struct buf *)wk;
    107 
    108 	KASSERT(&bp->b_work == wk);
    109 	bp->b_iodone(bp);
    110 }
    111 
    112 static int rump_inited;
    113 static struct emul emul_rump;
    114 
    115 void rump__unavailable(void);
    116 void rump__unavailable() {}
    117 __weak_alias(rump_net_init,rump__unavailable);
    118 __weak_alias(rump_vfs_init,rump__unavailable);
    119 
    120 void rump__unavailable_vfs_panic(void);
    121 void rump__unavailable_vfs_panic() {panic("vfs component not available");}
    122 __weak_alias(vn_open,rump__unavailable_vfs_panic);
    123 __weak_alias(vn_rdwr,rump__unavailable_vfs_panic);
    124 __weak_alias(vn_close,rump__unavailable_vfs_panic);
    125 
    126 static void
    127 pvfsinit_nop(struct proc *p)
    128 {
    129 
    130 	return;
    131 }
    132 
    133 static void
    134 pvfsrele_nop(struct proc *p)
    135 {
    136 
    137 	return;
    138 }
    139 
    140 rump_proc_vfs_init_fn rump_proc_vfs_init = pvfsinit_nop;
    141 rump_proc_vfs_release_fn rump_proc_vfs_release = pvfsrele_nop;
    142 
    143 int
    144 rump__init(int rump_version)
    145 {
    146 	char buf[256];
    147 	struct proc *p;
    148 	struct lwp *l;
    149 	int error;
    150 
    151 	/* XXX */
    152 	if (rump_inited)
    153 		return 0;
    154 	rump_inited = 1;
    155 
    156 	if (rump_version != RUMP_VERSION) {
    157 		printf("rump version mismatch, %d vs. %d\n",
    158 		    rump_version, RUMP_VERSION);
    159 		return EPROGMISMATCH;
    160 	}
    161 
    162 	if (rumpuser_getenv("RUMP_THREADS", buf, sizeof(buf), &error) == 0) {
    163 		rump_threads = *buf != '0';
    164 	}
    165 	rumpuser_thrinit(_kernel_lock, _kernel_unlock, rump_threads);
    166 
    167 	mutex_init(&tty_lock, MUTEX_DEFAULT, IPL_NONE);
    168 	rumpuser_mutex_recursive_init(&rump_giantlock);
    169 	ksyms_init();
    170 	rumpvm_init();
    171 	evcnt_init();
    172 
    173 	once_init();
    174 
    175 	rump_sleepers_init();
    176 #ifdef RUMP_USE_REAL_ALLOCATORS
    177 	pool_subsystem_init();
    178 	kmem_init();
    179 #endif
    180 	kprintf_init();
    181 	loginit();
    182 
    183 	kauth_init();
    184 	rump_susercred = rump_cred_create(0, 0, 0, NULL);
    185 
    186 	l = &lwp0;
    187 	p = &proc0;
    188 	p->p_stats = &rump_stats;
    189 	p->p_limit = &rump_limits;
    190 	p->p_pgrp = &rump_pgrp;
    191 	p->p_pid = 0;
    192 	p->p_fd = &rump_filedesc0;
    193 	p->p_vmspace = &rump_vmspace;
    194 	p->p_emul = &emul_rump;
    195 	p->p_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    196 	l->l_cred = rump_cred_suserget();
    197 	l->l_proc = p;
    198 	l->l_lid = 1;
    199 	LIST_INIT(&allproc);
    200 	proc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    201 
    202 	rump_limits.pl_rlimit[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
    203 	rump_limits.pl_rlimit[RLIMIT_NOFILE].rlim_cur = RLIM_INFINITY;
    204 	rump_limits.pl_rlimit[RLIMIT_SBSIZE].rlim_cur = RLIM_INFINITY;
    205 
    206 	callout_startup();
    207 	callout_init_cpu(&rump_cpu);
    208 
    209 	kqueue_init();
    210 	iostat_init();
    211 	uid_init();
    212 	percpu_init();
    213 	fd_sys_init();
    214 	module_init();
    215 	sysctl_init();
    216 	softint_init(&rump_cpu);
    217 	cold = 0;
    218 	devsw_init();
    219 	secmodel_start();
    220 
    221 	/* these do nothing if not present */
    222 	rump_vfs_init();
    223 	rump_net_init();
    224 
    225 	/* aieeeedondest */
    226 	if (rump_threads) {
    227 		if (workqueue_create(&uvm.aiodone_queue, "aiodoned",
    228 		    rump_aiodone_worker, NULL, 0, 0, 0))
    229 			panic("aiodoned");
    230 	}
    231 
    232 	rumpuser_gethostname(hostname, MAXHOSTNAMELEN, &error);
    233 	hostnamelen = strlen(hostname);
    234 
    235 	sigemptyset(&sigcantmask);
    236 
    237 	lwp0.l_fd = proc0.p_fd = fd_init(&rump_filedesc0);
    238 
    239 #ifdef RUMP_USE_REAL_ALLOCATORS
    240 	if (rump_threads)
    241 		vmem_rehash_start();
    242 #endif
    243 
    244 	return 0;
    245 }
    246 
    247 struct uio *
    248 rump_uio_setup(void *buf, size_t bufsize, off_t offset, enum rump_uiorw rw)
    249 {
    250 	struct uio *uio;
    251 	enum uio_rw uiorw;
    252 
    253 	switch (rw) {
    254 	case RUMPUIO_READ:
    255 		uiorw = UIO_READ;
    256 		break;
    257 	case RUMPUIO_WRITE:
    258 		uiorw = UIO_WRITE;
    259 		break;
    260 	default:
    261 		panic("%s: invalid rw %d", __func__, rw);
    262 	}
    263 
    264 	uio = kmem_alloc(sizeof(struct uio), KM_SLEEP);
    265 	uio->uio_iov = kmem_alloc(sizeof(struct iovec), KM_SLEEP);
    266 
    267 	uio->uio_iov->iov_base = buf;
    268 	uio->uio_iov->iov_len = bufsize;
    269 
    270 	uio->uio_iovcnt = 1;
    271 	uio->uio_offset = offset;
    272 	uio->uio_resid = bufsize;
    273 	uio->uio_rw = uiorw;
    274 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    275 
    276 	return uio;
    277 }
    278 
    279 size_t
    280 rump_uio_getresid(struct uio *uio)
    281 {
    282 
    283 	return uio->uio_resid;
    284 }
    285 
    286 off_t
    287 rump_uio_getoff(struct uio *uio)
    288 {
    289 
    290 	return uio->uio_offset;
    291 }
    292 
    293 size_t
    294 rump_uio_free(struct uio *uio)
    295 {
    296 	size_t resid;
    297 
    298 	resid = uio->uio_resid;
    299 	kmem_free(uio->uio_iov, sizeof(*uio->uio_iov));
    300 	kmem_free(uio, sizeof(*uio));
    301 
    302 	return resid;
    303 }
    304 
    305 struct lwp *
    306 rump_setup_curlwp(pid_t pid, lwpid_t lid, int set)
    307 {
    308 	struct lwp *l;
    309 	struct proc *p;
    310 
    311 	l = kmem_zalloc(sizeof(struct lwp), KM_SLEEP);
    312 	if (pid != 0) {
    313 		p = kmem_zalloc(sizeof(struct proc), KM_SLEEP);
    314 		rump_proc_vfs_init(p);
    315 		p->p_stats = &rump_stats;
    316 		p->p_limit = &rump_limits;
    317 		p->p_pid = pid;
    318 		p->p_vmspace = &rump_vmspace;
    319 		p->p_fd = fd_init(NULL);
    320 	} else {
    321 		p = &proc0;
    322 	}
    323 
    324 	l->l_cred = rump_cred_suserget();
    325 	l->l_proc = p;
    326 	l->l_lid = lid;
    327 	l->l_fd = p->p_fd;
    328 	l->l_mutex = RUMP_LMUTEX_MAGIC;
    329 	l->l_cpu = &rump_cpu;
    330 
    331 	if (set)
    332 		rumpuser_set_curlwp(l);
    333 
    334 	return l;
    335 }
    336 
    337 void
    338 rump_clear_curlwp(void)
    339 {
    340 	struct lwp *l;
    341 
    342 	l = rumpuser_get_curlwp();
    343 	if (l->l_proc->p_pid != 0) {
    344 		fd_free();
    345 		rump_proc_vfs_release(l->l_proc);
    346 		rump_cred_destroy(l->l_cred);
    347 		kmem_free(l->l_proc, sizeof(*l->l_proc));
    348 	}
    349 	kmem_free(l, sizeof(*l));
    350 	rumpuser_set_curlwp(NULL);
    351 }
    352 
    353 struct lwp *
    354 rump_get_curlwp(void)
    355 {
    356 	struct lwp *l;
    357 
    358 	l = rumpuser_get_curlwp();
    359 	if (l == NULL)
    360 		l = &lwp0;
    361 
    362 	return l;
    363 }
    364 
    365 kauth_cred_t
    366 rump_cred_create(uid_t uid, gid_t gid, size_t ngroups, gid_t *groups)
    367 {
    368 	kauth_cred_t cred;
    369 	int rv;
    370 
    371 	cred = kauth_cred_alloc();
    372 	kauth_cred_setuid(cred, uid);
    373 	kauth_cred_seteuid(cred, uid);
    374 	kauth_cred_setsvuid(cred, uid);
    375 	kauth_cred_setgid(cred, gid);
    376 	kauth_cred_setgid(cred, gid);
    377 	kauth_cred_setegid(cred, gid);
    378 	kauth_cred_setsvgid(cred, gid);
    379 	rv = kauth_cred_setgroups(cred, groups, ngroups, 0, UIO_SYSSPACE);
    380 	/* oh this is silly.  and by "this" I mean kauth_cred_setgroups() */
    381 	assert(rv == 0);
    382 
    383 	return cred;
    384 }
    385 
    386 void
    387 rump_cred_destroy(kauth_cred_t cred)
    388 {
    389 
    390 	kauth_cred_free(cred);
    391 }
    392 
    393 kauth_cred_t
    394 rump_cred_suserget(void)
    395 {
    396 
    397 	kauth_cred_hold(rump_susercred);
    398 	return rump_susercred;
    399 }
    400 
    401 /*
    402  * Return the next system lwpid
    403  */
    404 lwpid_t
    405 rump_nextlid(void)
    406 {
    407 	lwpid_t retid;
    408 
    409 	mutex_enter(proc0.p_lock);
    410 	/*
    411 	 * Take next one, don't return 0
    412 	 * XXX: most likely we'll have collisions in case this
    413 	 * wraps around.
    414 	 */
    415 	if (++proc0.p_nlwpid == 0)
    416 		++proc0.p_nlwpid;
    417 	retid = proc0.p_nlwpid;
    418 	mutex_exit(proc0.p_lock);
    419 
    420 	return retid;
    421 }
    422 
    423 int
    424 rump_module_load(struct modinfo **mi)
    425 {
    426 
    427 	if (!module_compatible((*mi)->mi_version, __NetBSD_Version__))
    428 		return EPROGMISMATCH;
    429 
    430 	return (*mi)->mi_modcmd(MODULE_CMD_INIT, NULL);
    431 }
    432 
    433 int _syspuffs_stub(int, int *);
    434 int
    435 _syspuffs_stub(int fd, int *newfd)
    436 {
    437 
    438 	return ENODEV;
    439 }
    440 __weak_alias(rump_syspuffs_glueinit,_syspuffs_stub);
    441 
    442 static int
    443 rump_sysproxy_local(int num, void *arg, uint8_t *data, size_t dlen,
    444 	register_t *retval)
    445 {
    446 	struct lwp *l;
    447 	struct sysent *callp;
    448 
    449 	if (__predict_false(num >= SYS_NSYSENT))
    450 		return ENOSYS;
    451 
    452 	l = curlwp;
    453 	callp = rump_sysent + num;
    454 	return callp->sy_call(l, (void *)data, retval);
    455 }
    456 
    457 rump_sysproxy_t rump_sysproxy = rump_sysproxy_local;
    458 void *rump_sysproxy_arg;
    459 
    460 /*
    461  * This whole syscall-via-rpc is still taking form.  For example, it
    462  * may be necessary to set syscalls individually instead of lobbing
    463  * them all to the same place.  So don't think this interface is
    464  * set in stone.
    465  */
    466 int
    467 rump_sysproxy_set(rump_sysproxy_t proxy, void *arg)
    468 {
    469 
    470 	if (rump_sysproxy_arg)
    471 		return EBUSY;
    472 
    473 	rump_sysproxy_arg = arg;
    474 	rump_sysproxy = proxy;
    475 
    476 	return 0;
    477 }
    478