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