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