Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.53.2.3
      1 /*	$NetBSD: emul.c,v 1.53.2.3 2009/04/28 07:37:50 skrll 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.53.2.3 2009/04/28 07:37:50 skrll 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 getnanouptime(struct timespec *ts)
    245 {
    246 
    247 	rump_getuptime(ts);
    248 }
    249 
    250 void
    251 getmicrouptime(struct timeval *tv)
    252 {
    253 	struct timespec ts;
    254 
    255 	getnanouptime(&ts);
    256 	TIMESPEC_TO_TIMEVAL(tv, &ts);
    257 }
    258 
    259 void
    260 malloc_type_attach(struct malloc_type *type)
    261 {
    262 
    263 	return;
    264 }
    265 
    266 void
    267 malloc_type_detach(struct malloc_type *type)
    268 {
    269 
    270 	return;
    271 }
    272 
    273 void *
    274 kern_malloc(unsigned long size, struct malloc_type *type, int flags)
    275 {
    276 	void *rv;
    277 
    278 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    279 	if (rv && flags & M_ZERO)
    280 		memset(rv, 0, size);
    281 
    282 	return rv;
    283 }
    284 
    285 void *
    286 kern_realloc(void *ptr, unsigned long size, struct malloc_type *type, int flags)
    287 {
    288 
    289 	return rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    290 }
    291 
    292 void
    293 kern_free(void *ptr, struct malloc_type *type)
    294 {
    295 
    296 	rumpuser_free(ptr);
    297 }
    298 
    299 static void
    300 gettime(struct timespec *ts)
    301 {
    302 	uint64_t sec, nsec;
    303 	int error;
    304 
    305 	rumpuser_gettime(&sec, &nsec, &error);
    306 	ts->tv_sec = sec;
    307 	ts->tv_nsec = nsec;
    308 }
    309 
    310 void
    311 nanotime(struct timespec *ts)
    312 {
    313 
    314 	if (rump_threads) {
    315 		rump_gettime(ts);
    316 	} else {
    317 		gettime(ts);
    318 	}
    319 }
    320 
    321 /* hooray for mick, so what if I do */
    322 void
    323 getnanotime(struct timespec *ts)
    324 {
    325 
    326 	nanotime(ts);
    327 }
    328 
    329 void
    330 microtime(struct timeval *tv)
    331 {
    332 	struct timespec ts;
    333 
    334 	if (rump_threads) {
    335 		rump_gettime(&ts);
    336 		TIMESPEC_TO_TIMEVAL(tv, &ts);
    337 	} else {
    338 		gettime(&ts);
    339 		TIMESPEC_TO_TIMEVAL(tv, &ts);
    340 	}
    341 }
    342 
    343 void
    344 getmicrotime(struct timeval *tv)
    345 {
    346 
    347 	microtime(tv);
    348 }
    349 
    350 struct kthdesc {
    351 	void (*f)(void *);
    352 	void *arg;
    353 	struct lwp *mylwp;
    354 };
    355 
    356 static void *
    357 threadbouncer(void *arg)
    358 {
    359 	struct kthdesc *k = arg;
    360 	void (*f)(void *);
    361 	void *thrarg;
    362 
    363 	f = k->f;
    364 	thrarg = k->arg;
    365 	rumpuser_set_curlwp(k->mylwp);
    366 	kmem_free(k, sizeof(struct kthdesc));
    367 
    368 	if ((curlwp->l_pflag & LP_MPSAFE) == 0)
    369 		KERNEL_LOCK(1, NULL);
    370 	f(thrarg);
    371 	panic("unreachable, should kthread_exit()");
    372 }
    373 
    374 int
    375 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    376 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    377 {
    378 	char thrstore[MAXCOMLEN];
    379 	const char *thrname = NULL;
    380 	va_list ap;
    381 	struct kthdesc *k;
    382 	struct lwp *l;
    383 	int rv;
    384 
    385 	thrstore[0] = '\0';
    386 	if (fmt) {
    387 		va_start(ap, fmt);
    388 		vsnprintf(thrstore, sizeof(thrstore), fmt, ap);
    389 		va_end(ap);
    390 		thrname = thrstore;
    391 	}
    392 
    393 	/*
    394 	 * We don't want a module unload thread.
    395 	 * (XXX: yes, this is a kludge too, and the kernel should
    396 	 * have a more flexible method for configuring which threads
    397 	 * we want).
    398 	 */
    399 	if (strcmp(thrstore, "modunload") == 0) {
    400 		return 0;
    401 	}
    402 
    403 	if (!rump_threads) {
    404 		/* fake them */
    405 		if (strcmp(thrstore, "vrele") == 0) {
    406 			printf("rump warning: threads not enabled, not starting"
    407 			   " vrele thread\n");
    408 			return 0;
    409 		} else if (strcmp(thrstore, "cachegc") == 0) {
    410 			printf("rump warning: threads not enabled, not starting"
    411 			   " namecache g/c thread\n");
    412 			return 0;
    413 		} else if (strcmp(thrstore, "nfssilly") == 0) {
    414 			printf("rump warning: threads not enabled, not enabling"
    415 			   " nfs silly rename\n");
    416 			return 0;
    417 		} else
    418 			panic("threads not available, setenv RUMP_THREADS 1");
    419 	}
    420 
    421 	KASSERT(fmt != NULL);
    422 	if (ci != NULL)
    423 		panic("%s: bounded threads not supported", __func__);
    424 
    425 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    426 	k->f = func;
    427 	k->arg = arg;
    428 	k->mylwp = l = rump_setup_curlwp(0, rump_nextlid(), 0);
    429 	if (flags & KTHREAD_MPSAFE)
    430 		l->l_pflag |= LP_MPSAFE;
    431 	rv = rumpuser_thread_create(threadbouncer, k, thrname);
    432 	if (rv)
    433 		return rv;
    434 
    435 	if (newlp)
    436 		*newlp = l;
    437 	return 0;
    438 }
    439 
    440 void
    441 kthread_exit(int ecode)
    442 {
    443 
    444 	if ((curlwp->l_pflag & LP_MPSAFE) == 0)
    445 		KERNEL_UNLOCK_ONE(NULL);
    446 	rump_clear_curlwp();
    447 	rumpuser_thread_exit();
    448 }
    449 
    450 struct proc *
    451 p_find(pid_t pid, uint flags)
    452 {
    453 
    454 	panic("%s: not implemented", __func__);
    455 }
    456 
    457 struct pgrp *
    458 pg_find(pid_t pid, uint flags)
    459 {
    460 
    461 	panic("%s: not implemented", __func__);
    462 }
    463 
    464 void
    465 psignal(struct proc *p, int signo)
    466 {
    467 
    468 	switch (signo) {
    469 	case SIGSYS:
    470 		break;
    471 	default:
    472 		panic("unhandled signal %d", signo);
    473 	}
    474 }
    475 
    476 void
    477 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    478 {
    479 
    480 	panic("%s: not implemented", __func__);
    481 }
    482 
    483 void
    484 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    485 {
    486 
    487 	panic("%s: not implemented", __func__);
    488 }
    489 
    490 int
    491 pgid_in_session(struct proc *p, pid_t pg_id)
    492 {
    493 
    494 	panic("%s: not implemented", __func__);
    495 }
    496 
    497 int
    498 sigispending(struct lwp *l, int signo)
    499 {
    500 
    501 	return 0;
    502 }
    503 
    504 void
    505 sigpending1(struct lwp *l, sigset_t *ss)
    506 {
    507 
    508 	panic("%s: not implemented", __func__);
    509 }
    510 
    511 int
    512 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    513 {
    514 	extern int hz;
    515 	int rv, error;
    516 	uint64_t sec, nsec;
    517 
    518 	if (mtx)
    519 		mutex_exit(mtx);
    520 
    521 	sec = timeo / hz;
    522 	nsec = (timeo % hz) * (1000000000 / hz);
    523 	rv = rumpuser_nanosleep(&sec, &nsec, &error);
    524 
    525 	if (mtx)
    526 		mutex_enter(mtx);
    527 
    528 	if (rv)
    529 		return error;
    530 
    531 	return 0;
    532 }
    533 
    534 void
    535 suspendsched(void)
    536 {
    537 
    538 	panic("%s: not implemented", __func__);
    539 }
    540 
    541 u_int
    542 lwp_unsleep(lwp_t *l, bool cleanup)
    543 {
    544 
    545 	KASSERT(mutex_owned(l->l_mutex));
    546 
    547 	return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
    548 }
    549 
    550 vaddr_t
    551 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    552 {
    553 	paddr_t t;
    554 
    555 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    556 	if ((vaddr_t)t != t) {
    557 		panic("%s: needs tweak", __func__);
    558 	}
    559 	return t;
    560 }
    561 
    562 int
    563 seltrue(dev_t dev, int events, struct lwp *l)
    564 {
    565         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    566 }
    567 
    568 void
    569 selrecord(lwp_t *selector, struct selinfo *sip)
    570 {
    571 }
    572 
    573 void
    574 selinit(struct selinfo *sip)
    575 {
    576 }
    577 
    578 void
    579 selnotify(struct selinfo *sip, int events, long knhint)
    580 {
    581 }
    582 
    583 void
    584 seldestroy(struct selinfo *sip)
    585 {
    586 }
    587 
    588 const char *
    589 device_xname(device_t dv)
    590 {
    591 	return "bogus0";
    592 }
    593 
    594 void
    595 assert_sleepable(void)
    596 {
    597 
    598 	/* always sleepable, although we should improve this */
    599 }
    600 
    601 void
    602 tc_setclock(const struct timespec *ts)
    603 {
    604 
    605 	panic("%s: not implemented", __func__);
    606 }
    607 
    608 void
    609 proc_crmod_enter(void)
    610 {
    611 
    612 	panic("%s: not implemented", __func__);
    613 }
    614 
    615 void
    616 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
    617 {
    618 
    619 	panic("%s: not implemented", __func__);
    620 }
    621 
    622 void
    623 module_init_md(void)
    624 {
    625 
    626 	/*
    627 	 * Nothing for now.  However, we should load the librump
    628 	 * symbol table.
    629 	 */
    630 }
    631 
    632 /* us and them, after all we're only ordinary seconds */
    633 static void
    634 rump_delay(unsigned int us)
    635 {
    636 	uint64_t sec, nsec;
    637 	int error;
    638 
    639 	sec = us / 1000000;
    640 	nsec = (us % 1000000) * 1000;
    641 
    642 	if (__predict_false(sec != 0))
    643 		printf("WARNING: over 1s delay\n");
    644 
    645 	rumpuser_nanosleep(&sec, &nsec, &error);
    646 }
    647 void (*delay_func)(unsigned int) = rump_delay;
    648 
    649 void
    650 kpreempt_disable(void)
    651 {
    652 
    653 	/* XXX: see below */
    654 	KPREEMPT_DISABLE(curlwp);
    655 }
    656 
    657 void
    658 kpreempt_enable(void)
    659 {
    660 
    661 	/* try to make sure kpreempt_disable() is only used from panic() */
    662 	panic("kpreempt not supported");
    663 }
    664 
    665 void
    666 proc_sesshold(struct session *ss)
    667 {
    668 
    669 	panic("proc_sesshold() impossible, session %p", ss);
    670 }
    671 
    672 void
    673 proc_sessrele(struct session *ss)
    674 {
    675 
    676 	panic("proc_sessrele() impossible, session %p", ss);
    677 }
    678 
    679 int
    680 ttycheckoutq(struct tty *tp, int wait)
    681 {
    682 
    683 	return 1;
    684 }
    685 
    686 void
    687 cnputc(int c)
    688 {
    689 	int error;
    690 
    691 	rumpuser_putchar(c, &error);
    692 }
    693 
    694 void
    695 cnflush(void)
    696 {
    697 
    698 	/* done */
    699 }
    700 
    701 int
    702 tputchar(int c, int flags, struct tty *tp)
    703 {
    704 
    705 	cnputc(c);
    706 	return 0;
    707 }
    708 
    709 void
    710 cpu_reboot(int howto, char *bootstr)
    711 {
    712 
    713 	rumpuser_panic();
    714 }
    715 
    716 /* XXX: static, but not used except to make spcopy.S link */
    717 #ifdef __hppa__
    718 #undef curlwp
    719 struct lwp *curlwp = &lwp0;
    720 #endif
    721 
    722 /*
    723  * XXX: from sys_select.c, see that file for license.
    724  * (these will go away really soon in favour of the real sys_select.c)
    725  * ((really, the select code just needs cleanup))
    726  * (((seriously)))
    727  */
    728 int
    729 inittimeleft(struct timespec *ts, struct timespec *sleepts)
    730 {
    731 	if (itimespecfix(ts))
    732 		return -1;
    733 	getnanouptime(sleepts);
    734 	return 0;
    735 }
    736 
    737 int
    738 gettimeleft(struct timespec *ts, struct timespec *sleepts)
    739 {
    740 	/*
    741 	 * We have to recalculate the timeout on every retry.
    742 	 */
    743 	struct timespec sleptts;
    744 	/*
    745 	 * reduce ts by elapsed time
    746 	 * based on monotonic time scale
    747 	 */
    748 	getnanouptime(&sleptts);
    749 	timespecadd(ts, sleepts, ts);
    750 	timespecsub(ts, &sleptts, ts);
    751 	*sleepts = sleptts;
    752 	return tstohz(ts);
    753 }
    754