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