Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.74
      1 /*	$NetBSD: emul.c,v 1.74 2009/01/11 20:41: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: emul.c,v 1.74 2009/01/11 20:41:18 pooka Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/malloc.h>
     35 #include <sys/null.h>
     36 #include <sys/vnode.h>
     37 #include <sys/stat.h>
     38 #include <sys/select.h>
     39 #include <sys/syslog.h>
     40 #include <sys/namei.h>
     41 #include <sys/kauth.h>
     42 #include <sys/conf.h>
     43 #include <sys/device.h>
     44 #include <sys/queue.h>
     45 #include <sys/file.h>
     46 #include <sys/filedesc.h>
     47 #include <sys/kthread.h>
     48 #include <sys/cpu.h>
     49 #include <sys/kmem.h>
     50 #include <sys/poll.h>
     51 #include <sys/timetc.h>
     52 #include <sys/tprintf.h>
     53 #include <sys/module.h>
     54 #include <sys/tty.h>
     55 #include <sys/reboot.h>
     56 
     57 #include <dev/cons.h>
     58 
     59 #include <machine/stdarg.h>
     60 
     61 #include <rump/rumpuser.h>
     62 
     63 #include <uvm/uvm_map.h>
     64 
     65 #include "rump_private.h"
     66 
     67 time_t time_second = 1;
     68 
     69 kmutex_t *proc_lock;
     70 struct lwp lwp0;
     71 struct vnode *rootvp;
     72 struct device *root_device;
     73 dev_t rootdev;
     74 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
     75 int doing_shutdown;
     76 int ncpu = 1;
     77 const int schedppq = 1;
     78 int hardclock_ticks;
     79 bool mp_online = false;
     80 struct vm_map *mb_map;
     81 struct timeval boottime;
     82 struct emul emul_netbsd;
     83 int cold = 1;
     84 int boothowto;
     85 struct tty *constty;
     86 
     87 char hostname[MAXHOSTNAMELEN];
     88 size_t hostnamelen;
     89 
     90 u_long	bufmem_valimit;
     91 u_long	bufmem_hiwater;
     92 u_long	bufmem_lowater;
     93 u_long	bufmem;
     94 u_int	nbuf;
     95 
     96 const char *panicstr;
     97 const char ostype[] = "NetBSD";
     98 const char osrelease[] = "999"; /* paradroid 4evah */
     99 const char kernel_ident[] = "RUMP-ROAST";
    100 const char *domainname;
    101 int domainnamelen;
    102 
    103 const struct filterops seltrue_filtops;
    104 
    105 #define DEVSW_SIZE 255
    106 const struct bdevsw *bdevsw0[DEVSW_SIZE]; /* XXX storage size */
    107 const struct bdevsw **bdevsw = bdevsw0;
    108 const int sys_cdevsws = DEVSW_SIZE;
    109 int max_cdevsws = DEVSW_SIZE;
    110 
    111 const struct cdevsw *cdevsw0[DEVSW_SIZE]; /* XXX storage size */
    112 const struct cdevsw **cdevsw = cdevsw0;
    113 const int sys_bdevsws = DEVSW_SIZE;
    114 int max_bdevsws = DEVSW_SIZE;
    115 
    116 struct devsw_conv devsw_conv0;
    117 struct devsw_conv *devsw_conv = &devsw_conv0;
    118 int max_devsw_convs = 0;
    119 
    120 
    121 int
    122 copyin(const void *uaddr, void *kaddr, size_t len)
    123 {
    124 
    125 	memcpy(kaddr, uaddr, len);
    126 	return 0;
    127 }
    128 
    129 int
    130 copyout(const void *kaddr, void *uaddr, size_t len)
    131 {
    132 
    133 	memcpy(uaddr, kaddr, len);
    134 	return 0;
    135 }
    136 
    137 int
    138 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    139 {
    140 
    141 	return copyinstr(kfaddr, kdaddr, len, done);
    142 }
    143 
    144 int
    145 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    146 {
    147 
    148 	strlcpy(kaddr, uaddr, len);
    149 	if (done)
    150 		*done = strlen(kaddr)+1; /* includes termination */
    151 	return 0;
    152 }
    153 
    154 int
    155 copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
    156 {
    157 
    158 	return copyin(uaddr, kaddr, len);
    159 }
    160 
    161 int
    162 copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
    163 {
    164 
    165 	return copyout(kaddr, uaddr, len);
    166 }
    167 
    168 int
    169 kcopy(const void *src, void *dst, size_t len)
    170 {
    171 
    172 	memcpy(dst, src, len);
    173 	return 0;
    174 }
    175 
    176 int
    177 uiomove(void *buf, size_t n, struct uio *uio)
    178 {
    179 	struct iovec *iov;
    180 	uint8_t *b = buf;
    181 	size_t cnt;
    182 
    183 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    184 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    185 
    186 	while (n && uio->uio_resid) {
    187 		iov = uio->uio_iov;
    188 		cnt = iov->iov_len;
    189 		if (cnt == 0) {
    190 			uio->uio_iov++;
    191 			uio->uio_iovcnt--;
    192 			continue;
    193 		}
    194 		if (cnt > n)
    195 			cnt = n;
    196 
    197 		if (uio->uio_rw == UIO_READ)
    198 			memcpy(iov->iov_base, b, cnt);
    199 		else
    200 			memcpy(b, iov->iov_base, cnt);
    201 
    202 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    203 		iov->iov_len -= cnt;
    204 		b += cnt;
    205 		uio->uio_resid -= cnt;
    206 		uio->uio_offset += cnt;
    207 		n -= cnt;
    208 	}
    209 
    210 	return 0;
    211 }
    212 
    213 void
    214 uio_setup_sysspace(struct uio *uio)
    215 {
    216 
    217 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    218 }
    219 
    220 devclass_t
    221 device_class(device_t dev)
    222 {
    223 
    224 	if (dev != root_device)
    225 		panic("%s: dev != root_device not supported", __func__);
    226 
    227 	return DV_DISK;
    228 }
    229 
    230 void
    231 getmicrouptime(struct timeval *tvp)
    232 {
    233 	int error;
    234 
    235 	rumpuser_gettimeofday(tvp, &error);
    236 }
    237 
    238 void
    239 malloc_type_attach(struct malloc_type *type)
    240 {
    241 
    242 	return;
    243 }
    244 
    245 void
    246 malloc_type_detach(struct malloc_type *type)
    247 {
    248 
    249 	return;
    250 }
    251 
    252 void *
    253 kern_malloc(unsigned long size, struct malloc_type *type, int flags)
    254 {
    255 	void *rv;
    256 
    257 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    258 	if (rv && flags & M_ZERO)
    259 		memset(rv, 0, size);
    260 
    261 	return rv;
    262 }
    263 
    264 void *
    265 kern_realloc(void *ptr, unsigned long size, struct malloc_type *type, int flags)
    266 {
    267 
    268 	return rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    269 }
    270 
    271 void
    272 kern_free(void *ptr, struct malloc_type *type)
    273 {
    274 
    275 	rumpuser_free(ptr);
    276 }
    277 
    278 void
    279 nanotime(struct timespec *ts)
    280 {
    281 	struct timeval tv;
    282 	int error;
    283 
    284 	rumpuser_gettimeofday(&tv, &error);
    285 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    286 }
    287 
    288 /* hooray for mick, so what if I do */
    289 void
    290 getnanotime(struct timespec *ts)
    291 {
    292 
    293 	nanotime(ts);
    294 }
    295 
    296 void
    297 microtime(struct timeval *tv)
    298 {
    299 	int error;
    300 
    301 	rumpuser_gettimeofday(tv, &error);
    302 }
    303 
    304 void
    305 getmicrotime(struct timeval *tv)
    306 {
    307 	int error;
    308 
    309 	rumpuser_gettimeofday(tv, &error);
    310 }
    311 
    312 struct kthdesc {
    313 	void (*f)(void *);
    314 	void *arg;
    315 	struct lwp *mylwp;
    316 };
    317 
    318 static void *
    319 threadbouncer(void *arg)
    320 {
    321 	struct kthdesc *k = arg;
    322 	void (*f)(void *);
    323 	void *thrarg;
    324 
    325 	f = k->f;
    326 	thrarg = k->arg;
    327 	rumpuser_set_curlwp(k->mylwp);
    328 	kmem_free(k, sizeof(struct kthdesc));
    329 
    330 	if ((curlwp->l_pflag & LP_MPSAFE) == 0)
    331 		KERNEL_LOCK(1, NULL);
    332 	f(thrarg);
    333 	panic("unreachable, should kthread_exit()");
    334 }
    335 
    336 int
    337 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    338 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    339 {
    340 	char thrstore[MAXCOMLEN];
    341 	const char *thrname = NULL;
    342 	va_list ap;
    343 	struct kthdesc *k;
    344 	struct lwp *l;
    345 	int rv;
    346 
    347 	thrstore[0] = '\0';
    348 	if (fmt) {
    349 		va_start(ap, fmt);
    350 		vsnprintf(thrstore, sizeof(thrstore), fmt, ap);
    351 		va_end(ap);
    352 		thrname = thrstore;
    353 	}
    354 
    355 	/*
    356 	 * We don't want a module unload thread.
    357 	 * (XXX: yes, this is a kludge too, and the kernel should
    358 	 * have a more flexible method for configuring which threads
    359 	 * we want).
    360 	 */
    361 	if (strcmp(thrstore, "modunload") == 0) {
    362 		return 0;
    363 	}
    364 
    365 	if (!rump_threads) {
    366 		/* fake them */
    367 		if (strcmp(thrstore, "vrele") == 0) {
    368 			printf("rump warning: threads not enabled, not starting"
    369 			   " vrele thread\n");
    370 			return 0;
    371 		} else if (strcmp(thrstore, "cachegc") == 0) {
    372 			printf("rump warning: threads not enabled, not starting"
    373 			   " namecache g/c thread\n");
    374 			return 0;
    375 		} else if (strcmp(thrstore, "nfssilly") == 0) {
    376 			printf("rump warning: threads not enabled, not enabling"
    377 			   " nfs silly rename\n");
    378 			return 0;
    379 		} else
    380 			panic("threads not available, setenv RUMP_THREADS 1");
    381 	}
    382 
    383 	KASSERT(fmt != NULL);
    384 	if (ci != NULL)
    385 		panic("%s: bounded threads not supported", __func__);
    386 
    387 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    388 	k->f = func;
    389 	k->arg = arg;
    390 	k->mylwp = l = rump_setup_curlwp(0, rump_nextlid(), 0);
    391 	if (flags & KTHREAD_MPSAFE)
    392 		l->l_pflag |= LP_MPSAFE;
    393 	rv = rumpuser_thread_create(threadbouncer, k, thrname);
    394 	if (rv)
    395 		return rv;
    396 
    397 	if (newlp)
    398 		*newlp = l;
    399 	return 0;
    400 }
    401 
    402 void
    403 kthread_exit(int ecode)
    404 {
    405 
    406 	if ((curlwp->l_pflag & LP_MPSAFE) == 0)
    407 		KERNEL_UNLOCK_ONE(NULL);
    408 	rump_clear_curlwp();
    409 	rumpuser_thread_exit();
    410 }
    411 
    412 struct proc *
    413 p_find(pid_t pid, uint flags)
    414 {
    415 
    416 	panic("%s: not implemented", __func__);
    417 }
    418 
    419 struct pgrp *
    420 pg_find(pid_t pid, uint flags)
    421 {
    422 
    423 	panic("%s: not implemented", __func__);
    424 }
    425 
    426 void
    427 psignal(struct proc *p, int signo)
    428 {
    429 
    430 	switch (signo) {
    431 	case SIGSYS:
    432 		break;
    433 	default:
    434 		panic("unhandled signal %d", signo);
    435 	}
    436 }
    437 
    438 void
    439 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    440 {
    441 
    442 	panic("%s: not implemented", __func__);
    443 }
    444 
    445 void
    446 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    447 {
    448 
    449 	panic("%s: not implemented", __func__);
    450 }
    451 
    452 int
    453 pgid_in_session(struct proc *p, pid_t pg_id)
    454 {
    455 
    456 	panic("%s: not implemented", __func__);
    457 }
    458 
    459 int
    460 sigispending(struct lwp *l, int signo)
    461 {
    462 
    463 	return 0;
    464 }
    465 
    466 void
    467 sigpending1(struct lwp *l, sigset_t *ss)
    468 {
    469 
    470 	panic("%s: not implemented", __func__);
    471 }
    472 
    473 void
    474 knote_fdclose(int fd)
    475 {
    476 
    477 	/* since we don't add knotes, we don't have to remove them */
    478 }
    479 
    480 int
    481 seltrue_kqfilter(dev_t dev, struct knote *kn)
    482 {
    483 
    484 	panic("%s: not implemented", __func__);
    485 }
    486 
    487 int
    488 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    489 {
    490 	extern int hz;
    491 	int rv, error;
    492 	struct timespec time;
    493 
    494 	if (mtx)
    495 		mutex_exit(mtx);
    496 
    497 	time.tv_sec = timeo / hz;
    498 	time.tv_nsec = (timeo % hz) * (1000000000 / hz);
    499 
    500 	rv = rumpuser_nanosleep(&time, NULL, &error);
    501 
    502 	if (mtx)
    503 		mutex_enter(mtx);
    504 
    505 	if (rv)
    506 		return error;
    507 
    508 	return 0;
    509 }
    510 
    511 void
    512 suspendsched()
    513 {
    514 
    515 	panic("%s: not implemented", __func__);
    516 }
    517 
    518 u_int
    519 lwp_unsleep(lwp_t *l, bool cleanup)
    520 {
    521 
    522 	KASSERT(mutex_owned(l->l_mutex));
    523 
    524 	return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
    525 }
    526 
    527 vaddr_t
    528 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    529 {
    530 	paddr_t t;
    531 
    532 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    533 	if ((vaddr_t)t != t) {
    534 		panic("%s: needs tweak", __func__);
    535 	}
    536 	return t;
    537 }
    538 
    539 int
    540 seltrue(dev_t dev, int events, struct lwp *l)
    541 {
    542         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    543 }
    544 
    545 void
    546 selrecord(lwp_t *selector, struct selinfo *sip)
    547 {
    548 }
    549 
    550 void
    551 selinit(struct selinfo *sip)
    552 {
    553 }
    554 
    555 void
    556 selnotify(struct selinfo *sip, int events, long knhint)
    557 {
    558 }
    559 
    560 void
    561 seldestroy(struct selinfo *sip)
    562 {
    563 }
    564 
    565 const char *
    566 device_xname(device_t dv)
    567 {
    568 	return "bogus0";
    569 }
    570 
    571 void
    572 assert_sleepable(void)
    573 {
    574 
    575 	/* always sleepable, although we should improve this */
    576 }
    577 
    578 void
    579 tc_setclock(const struct timespec *ts)
    580 {
    581 
    582 	panic("%s: not implemented", __func__);
    583 }
    584 
    585 void
    586 proc_crmod_enter()
    587 {
    588 
    589 	panic("%s: not implemented", __func__);
    590 }
    591 
    592 void
    593 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
    594 {
    595 
    596 	panic("%s: not implemented", __func__);
    597 }
    598 
    599 void
    600 module_init_md()
    601 {
    602 
    603 	/*
    604 	 * Nothing for now.  However, we should load the librump
    605 	 * symbol table.
    606 	 */
    607 }
    608 
    609 /* us and them, after all we're only ordinary seconds */
    610 static void
    611 rump_delay(unsigned int us)
    612 {
    613 	struct timespec ts;
    614 	int error;
    615 
    616 	ts.tv_sec = us / 1000000;
    617 	ts.tv_nsec = (us % 1000000) * 1000;
    618 
    619 	if (__predict_false(ts.tv_sec != 0))
    620 		printf("WARNING: over 1s delay\n");
    621 
    622 	rumpuser_nanosleep(&ts, NULL, &error);
    623 }
    624 void (*delay_func)(unsigned int) = rump_delay;
    625 
    626 void
    627 kpreempt_disable()
    628 {
    629 
    630 	/* XXX: see below */
    631 	KPREEMPT_DISABLE(curlwp);
    632 }
    633 
    634 void
    635 kpreempt_enable()
    636 {
    637 
    638 	/* try to make sure kpreempt_disable() is only used from panic() */
    639 	panic("kpreempt not supported");
    640 }
    641 
    642 void
    643 sessdelete(struct session *ss)
    644 {
    645 
    646 	panic("sessdelete() impossible, session %p", ss);
    647 }
    648 
    649 int
    650 ttycheckoutq(struct tty *tp, int wait)
    651 {
    652 
    653 	return 1;
    654 }
    655 
    656 void
    657 cnputc(int c)
    658 {
    659 	int error;
    660 
    661 	rumpuser_putchar(c, &error);
    662 }
    663 
    664 void
    665 cnflush()
    666 {
    667 
    668 	/* done */
    669 }
    670 
    671 int
    672 tputchar(int c, int flags, struct tty *tp)
    673 {
    674 
    675 	cnputc(c);
    676 	return 0;
    677 }
    678 
    679 void
    680 cpu_reboot(int howto, char *bootstr)
    681 {
    682 
    683 	rumpuser_panic();
    684 }
    685