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