Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.44
      1 /*	$NetBSD: emul.c,v 1.44 2008/07/29 13:17:47 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 
     51 #include <machine/stdarg.h>
     52 
     53 #include <rump/rumpuser.h>
     54 
     55 #include <uvm/uvm_map.h>
     56 
     57 #include "rump_private.h"
     58 
     59 time_t time_second = 1;
     60 
     61 kmutex_t *proc_lock;
     62 struct lwp lwp0;
     63 struct vnode *rootvp;
     64 struct device *root_device;
     65 dev_t rootdev;
     66 struct vm_map *kernel_map;
     67 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
     68 int doing_shutdown;
     69 int ncpu = 1;
     70 const int schedppq = 1;
     71 int hardclock_ticks;
     72 
     73 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     74 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     75 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     76 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
     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 void
    137 printf_nolog(const char *fmt, ...)
    138 {
    139 	va_list ap;
    140 
    141 	va_start(ap, fmt);
    142 	vprintf(fmt, ap);
    143 	va_end(ap);
    144 }
    145 
    146 void
    147 aprint_normal(const char *fmt, ...)
    148 {
    149 	va_list ap;
    150 
    151 	va_start(ap, fmt);
    152 	vprintf(fmt, ap);
    153 	va_end(ap);
    154 }
    155 
    156 int
    157 copyin(const void *uaddr, void *kaddr, size_t len)
    158 {
    159 
    160 	memcpy(kaddr, uaddr, len);
    161 	return 0;
    162 }
    163 
    164 int
    165 copyout(const void *kaddr, void *uaddr, size_t len)
    166 {
    167 
    168 	memcpy(uaddr, kaddr, len);
    169 	return 0;
    170 }
    171 
    172 int
    173 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    174 {
    175 
    176 	return copyinstr(kfaddr, kdaddr, len, done);
    177 }
    178 
    179 int
    180 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    181 {
    182 
    183 	strlcpy(kaddr, uaddr, len);
    184 	if (done)
    185 		*done = strlen(kaddr)+1; /* includes termination */
    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 	int rv;
    196 
    197 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    198 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    199 
    200 	/*
    201 	 * See if rump ubc code claims the offset.  This is of course
    202 	 * a blatant violation of abstraction levels, but let's keep
    203 	 * me simple & stupid for now.
    204 	 */
    205 	if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
    206 		return rv;
    207 
    208 	while (n && uio->uio_resid) {
    209 		iov = uio->uio_iov;
    210 		cnt = iov->iov_len;
    211 		if (cnt == 0) {
    212 			uio->uio_iov++;
    213 			uio->uio_iovcnt--;
    214 			continue;
    215 		}
    216 		if (cnt > n)
    217 			cnt = n;
    218 
    219 		if (uio->uio_rw == UIO_READ)
    220 			memcpy(iov->iov_base, b, cnt);
    221 		else
    222 			memcpy(b, iov->iov_base, cnt);
    223 
    224 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    225 		iov->iov_len -= cnt;
    226 		b += cnt;
    227 		uio->uio_resid -= cnt;
    228 		uio->uio_offset += cnt;
    229 		n -= cnt;
    230 	}
    231 
    232 	return 0;
    233 }
    234 
    235 void
    236 uio_setup_sysspace(struct uio *uio)
    237 {
    238 
    239 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    240 }
    241 
    242 const struct bdevsw *
    243 bdevsw_lookup(dev_t dev)
    244 {
    245 
    246 	return (const struct bdevsw *)1;
    247 }
    248 
    249 devclass_t
    250 device_class(device_t dev)
    251 {
    252 
    253 	if (dev != root_device)
    254 		panic("%s: dev != root_device not supported", __func__);
    255 
    256 	return DV_DISK;
    257 }
    258 
    259 void
    260 getmicrouptime(struct timeval *tvp)
    261 {
    262 	int error;
    263 
    264 	rumpuser_gettimeofday(tvp, &error);
    265 }
    266 
    267 void
    268 malloc_type_attach(struct malloc_type *type)
    269 {
    270 
    271 	return;
    272 }
    273 
    274 void
    275 malloc_type_detach(struct malloc_type *type)
    276 {
    277 
    278 	return;
    279 }
    280 
    281 void *
    282 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    283 {
    284 	void *rv;
    285 
    286 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    287 	if (rv && flags & M_ZERO)
    288 		memset(rv, 0, size);
    289 
    290 	return rv;
    291 }
    292 
    293 void
    294 nanotime(struct timespec *ts)
    295 {
    296 	struct timeval tv;
    297 	int error;
    298 
    299 	rumpuser_gettimeofday(&tv, &error);
    300 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    301 }
    302 
    303 /* hooray for mick, so what if I do */
    304 void
    305 getnanotime(struct timespec *ts)
    306 {
    307 
    308 	nanotime(ts);
    309 }
    310 
    311 void
    312 microtime(struct timeval *tv)
    313 {
    314 	int error;
    315 
    316 	rumpuser_gettimeofday(tv, &error);
    317 }
    318 
    319 void
    320 getmicrotime(struct timeval *tv)
    321 {
    322 	int error;
    323 
    324 	rumpuser_gettimeofday(tv, &error);
    325 }
    326 
    327 void
    328 bdev_strategy(struct buf *bp)
    329 {
    330 
    331 	panic("%s: not supported", __func__);
    332 }
    333 
    334 int
    335 bdev_type(dev_t dev)
    336 {
    337 
    338 	return D_DISK;
    339 }
    340 
    341 struct kthdesc {
    342 	void (*f)(void *);
    343 	void *arg;
    344 	struct lwp *mylwp;
    345 };
    346 
    347 static lwpid_t curlid = 2;
    348 
    349 static void *
    350 threadbouncer(void *arg)
    351 {
    352 	struct kthdesc *k = arg;
    353 	void (*f)(void *);
    354 	void *thrarg;
    355 
    356 	f = k->f;
    357 	thrarg = k->arg;
    358 	rumpuser_set_curlwp(k->mylwp);
    359 	kmem_free(k, sizeof(struct kthdesc));
    360 
    361 	f(thrarg);
    362 	panic("unreachable, should kthread_exit()");
    363 }
    364 
    365 int
    366 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    367 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    368 {
    369 	struct kthdesc *k;
    370 	struct lwp *l;
    371 	int rv;
    372 
    373 #ifdef RUMP_WITHOUT_THREADS
    374 	/* fake them */
    375 	if (strcmp(fmt, "vrele") == 0) {
    376 		printf("rump warning: threads not enabled, not starting "
    377 		   "vrele thread\n");
    378 		return 0;
    379 	} else if (strcmp(fmt, "cachegc") == 0) {
    380 		printf("rump warning: threads not enabled, not starting "
    381 		   "namecache g/c thread\n");
    382 		return 0;
    383 	} else
    384 		panic("threads not available, undef RUMP_WITHOUT_THREADS");
    385 #endif
    386 
    387 	KASSERT(fmt != NULL);
    388 	if (ci != NULL)
    389 		panic("%s: bounded threads not supported", __func__);
    390 
    391 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    392 	k->f = func;
    393 	k->arg = arg;
    394 	k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
    395 	rv = rumpuser_thread_create(threadbouncer, k);
    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 	rumpuser_thread_exit();
    409 }
    410 
    411 void
    412 callout_init(callout_t *c, u_int flags)
    413 {
    414 
    415 	panic("%s: not implemented", __func__);
    416 }
    417 
    418 void
    419 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
    420 {
    421 
    422 	panic("%s: not implemented", __func__);
    423 }
    424 
    425 bool
    426 callout_stop(callout_t *c)
    427 {
    428 
    429 	panic("%s: not implemented", __func__);
    430 }
    431 
    432 struct proc *
    433 p_find(pid_t pid, uint flags)
    434 {
    435 
    436 	panic("%s: not implemented", __func__);
    437 }
    438 
    439 struct pgrp *
    440 pg_find(pid_t pid, uint flags)
    441 {
    442 
    443 	panic("%s: not implemented", __func__);
    444 }
    445 
    446 void
    447 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    448 {
    449 
    450 	panic("%s: not implemented", __func__);
    451 }
    452 
    453 void
    454 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    455 {
    456 
    457 	panic("%s: not implemented", __func__);
    458 }
    459 
    460 int
    461 pgid_in_session(struct proc *p, pid_t pg_id)
    462 {
    463 
    464 	panic("%s: not implemented", __func__);
    465 }
    466 
    467 int
    468 sigispending(struct lwp *l, int signo)
    469 {
    470 
    471 	return 0;
    472 }
    473 
    474 void
    475 knote_fdclose(int fd)
    476 {
    477 
    478 	/* since we don't add knotes, we don't have to remove them */
    479 }
    480 
    481 int
    482 seltrue_kqfilter(dev_t dev, struct knote *kn)
    483 {
    484 
    485 	panic("%s: not implemented", __func__);
    486 }
    487 
    488 int
    489 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    490 {
    491 	extern int hz;
    492 	int rv, error;
    493 	struct timespec time;
    494 
    495 	if (mtx)
    496 		mutex_exit(mtx);
    497 
    498 	time.tv_sec = timeo / hz;
    499 	time.tv_nsec = (timeo % hz) * (1000000000 / hz);
    500 
    501 	rv = rumpuser_nanosleep(&time, NULL, &error);
    502 
    503 	if (mtx)
    504 		mutex_enter(mtx);
    505 
    506 	if (rv)
    507 		return error;
    508 
    509 	return 0;
    510 }
    511 
    512 void
    513 suspendsched()
    514 {
    515 
    516 	panic("%s: not implemented", __func__);
    517 }
    518 
    519 void
    520 yield(void)
    521 {
    522 
    523 	rumpuser_yield();
    524 }
    525 
    526 
    527 u_int
    528 lwp_unsleep(lwp_t *l, bool cleanup)
    529 {
    530 
    531 	KASSERT(mutex_owned(l->l_mutex));
    532 
    533 	return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
    534 }
    535 
    536 vaddr_t
    537 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    538 {
    539 	paddr_t t;
    540 
    541 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    542 	if ((vaddr_t)t != t) {
    543 		panic("%s: needs tweak", __func__);
    544 	}
    545 	return t;
    546 }
    547 
    548 int
    549 seltrue(dev_t dev, int events, struct lwp *l)
    550 {
    551         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    552 }
    553 
    554 void
    555 selrecord(lwp_t *selector, struct selinfo *sip)
    556 {
    557 }
    558 
    559 void
    560 selinit(struct selinfo *sip)
    561 {
    562 }
    563 
    564 void
    565 selnotify(struct selinfo *sip, int events, long knhint)
    566 {
    567 }
    568 
    569 void
    570 seldestroy(struct selinfo *sip)
    571 {
    572 }
    573 
    574 const char *
    575 device_xname(device_t dv)
    576 {
    577 	return "bogus0";
    578 }
    579 
    580 void
    581 assert_sleepable(void)
    582 {
    583 
    584 	/* always sleepable, although we should improve this */
    585 }
    586 
    587 int
    588 devsw_attach(const char *devname, const struct bdevsw *bdev, int *bmajor,
    589 	const struct cdevsw *cdev, int *cmajor)
    590 {
    591 
    592 	panic("%s: not implemented", __func__);
    593 }
    594 
    595 int
    596 devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
    597 {
    598 
    599 	panic("%s: not implemented", __func__);
    600 }
    601