Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.36
      1 /*	$NetBSD: emul.c,v 1.36 2008/03/25 23:21:42 yamt 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 <uvm/uvm_map.h>
     54 
     55 #include "rump_private.h"
     56 #include "rumpuser.h"
     57 
     58 time_t time_second = 1;
     59 
     60 kmutex_t proclist_mutex;
     61 kmutex_t proclist_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 uprintf(const char *fmt, ...)
    121 {
    122 	va_list ap;
    123 
    124 	va_start(ap, fmt);
    125 	vprintf(fmt, ap);
    126 	va_end(ap);
    127 }
    128 
    129 void
    130 printf_nolog(const char *fmt, ...)
    131 {
    132 	va_list ap;
    133 
    134 	va_start(ap, fmt);
    135 	vprintf(fmt, ap);
    136 	va_end(ap);
    137 }
    138 
    139 void
    140 aprint_normal(const char *fmt, ...)
    141 {
    142 	va_list ap;
    143 
    144 	va_start(ap, fmt);
    145 	vprintf(fmt, ap);
    146 	va_end(ap);
    147 }
    148 
    149 int
    150 copyin(const void *uaddr, void *kaddr, size_t len)
    151 {
    152 
    153 	memcpy(kaddr, uaddr, len);
    154 	return 0;
    155 }
    156 
    157 int
    158 copyout(const void *kaddr, void *uaddr, size_t len)
    159 {
    160 
    161 	memcpy(uaddr, kaddr, len);
    162 	return 0;
    163 }
    164 
    165 int
    166 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    167 {
    168 
    169 	return copyinstr(kfaddr, kdaddr, len, done);
    170 }
    171 
    172 int
    173 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    174 {
    175 
    176 	strlcpy(kaddr, uaddr, len);
    177 	if (done)
    178 		*done = strlen(kaddr)+1; /* includes termination */
    179 	return 0;
    180 }
    181 
    182 int
    183 uiomove(void *buf, size_t n, struct uio *uio)
    184 {
    185 	struct iovec *iov;
    186 	uint8_t *b = buf;
    187 	size_t cnt;
    188 	int rv;
    189 
    190 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    191 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    192 
    193 	/*
    194 	 * See if rump ubc code claims the offset.  This is of course
    195 	 * a blatant violation of abstraction levels, but let's keep
    196 	 * me simple & stupid for now.
    197 	 */
    198 	if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
    199 		return rv;
    200 
    201 	while (n && uio->uio_resid) {
    202 		iov = uio->uio_iov;
    203 		cnt = iov->iov_len;
    204 		if (cnt == 0) {
    205 			uio->uio_iov++;
    206 			uio->uio_iovcnt--;
    207 			continue;
    208 		}
    209 		if (cnt > n)
    210 			cnt = n;
    211 
    212 		if (uio->uio_rw == UIO_READ)
    213 			memcpy(iov->iov_base, b, cnt);
    214 		else
    215 			memcpy(b, iov->iov_base, cnt);
    216 
    217 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    218 		iov->iov_len -= cnt;
    219 		b += cnt;
    220 		uio->uio_resid -= cnt;
    221 		uio->uio_offset += cnt;
    222 		n -= cnt;
    223 	}
    224 
    225 	return 0;
    226 }
    227 
    228 void
    229 uio_setup_sysspace(struct uio *uio)
    230 {
    231 
    232 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    233 }
    234 
    235 const struct bdevsw *
    236 bdevsw_lookup(dev_t dev)
    237 {
    238 
    239 	return (const struct bdevsw *)1;
    240 }
    241 
    242 devclass_t
    243 device_class(device_t dev)
    244 {
    245 
    246 	if (dev != root_device)
    247 		panic("%s: dev != root_device not supported", __func__);
    248 
    249 	return DV_DISK;
    250 }
    251 
    252 void
    253 getmicrouptime(struct timeval *tvp)
    254 {
    255 	int error;
    256 
    257 	rumpuser_gettimeofday(tvp, &error);
    258 }
    259 
    260 void
    261 malloc_type_attach(struct malloc_type *type)
    262 {
    263 
    264 	return;
    265 }
    266 
    267 void
    268 malloc_type_detach(struct malloc_type *type)
    269 {
    270 
    271 	return;
    272 }
    273 
    274 void *
    275 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    276 {
    277 	void *rv;
    278 
    279 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    280 	if (rv && flags & M_ZERO)
    281 		memset(rv, 0, size);
    282 
    283 	return rv;
    284 }
    285 
    286 void
    287 nanotime(struct timespec *ts)
    288 {
    289 	struct timeval tv;
    290 	int error;
    291 
    292 	rumpuser_gettimeofday(&tv, &error);
    293 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    294 }
    295 
    296 /* hooray for mick, so what if I do */
    297 void
    298 getnanotime(struct timespec *ts)
    299 {
    300 
    301 	nanotime(ts);
    302 }
    303 
    304 void
    305 microtime(struct timeval *tv)
    306 {
    307 	int error;
    308 
    309 	rumpuser_gettimeofday(tv, &error);
    310 }
    311 
    312 void
    313 getmicrotime(struct timeval *tv)
    314 {
    315 	int error;
    316 
    317 	rumpuser_gettimeofday(tv, &error);
    318 }
    319 
    320 void
    321 bdev_strategy(struct buf *bp)
    322 {
    323 
    324 	panic("%s: not supported", __func__);
    325 }
    326 
    327 int
    328 bdev_type(dev_t dev)
    329 {
    330 
    331 	return D_DISK;
    332 }
    333 
    334 struct kthdesc {
    335 	void (*f)(void *);
    336 	void *arg;
    337 	struct lwp *mylwp;
    338 };
    339 
    340 static lwpid_t curlid = 2;
    341 
    342 static void *
    343 threadbouncer(void *arg)
    344 {
    345 	struct kthdesc *k = arg;
    346 	void (*f)(void *);
    347 	void *thrarg;
    348 
    349 	f = k->f;
    350 	thrarg = k->arg;
    351 	rumpuser_set_curlwp(k->mylwp);
    352 	kmem_free(k, sizeof(struct kthdesc));
    353 
    354 	f(thrarg);
    355 	panic("unreachable, should kthread_exit()");
    356 }
    357 
    358 int
    359 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    360 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    361 {
    362 	struct kthdesc *k;
    363 	struct lwp *l;
    364 	int rv;
    365 
    366 #ifdef RUMP_WITHOUT_THREADS
    367 	/* XXX: fake it */
    368 	if (strcmp(fmt, "vrele") == 0)
    369 		return 0;
    370 	else
    371 		panic("threads not available, undef RUMP_WITHOUT_THREADS");
    372 #endif
    373 
    374 	KASSERT(fmt != NULL);
    375 	if (ci != NULL)
    376 		panic("%s: bounded threads not supported", __func__);
    377 
    378 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    379 	k->f = func;
    380 	k->arg = arg;
    381 	k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
    382 	rv = rumpuser_thread_create(threadbouncer, k);
    383 	if (rv)
    384 		return rv;
    385 
    386 	if (newlp)
    387 		*newlp = l;
    388 	return 0;
    389 }
    390 
    391 void
    392 kthread_exit(int ecode)
    393 {
    394 
    395 	rumpuser_thread_exit();
    396 }
    397 
    398 void
    399 callout_init(callout_t *c, u_int flags)
    400 {
    401 
    402 	panic("%s: not implemented", __func__);
    403 }
    404 
    405 void
    406 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
    407 {
    408 
    409 	panic("%s: not implemented", __func__);
    410 }
    411 
    412 bool
    413 callout_stop(callout_t *c)
    414 {
    415 
    416 	panic("%s: not implemented", __func__);
    417 }
    418 
    419 struct proc *
    420 p_find(pid_t pid, uint flags)
    421 {
    422 
    423 	panic("%s: not implemented", __func__);
    424 }
    425 
    426 struct pgrp *
    427 pg_find(pid_t pid, uint flags)
    428 {
    429 
    430 	panic("%s: not implemented", __func__);
    431 }
    432 
    433 void
    434 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    435 {
    436 
    437 	panic("%s: not implemented", __func__);
    438 }
    439 
    440 void
    441 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    442 {
    443 
    444 	panic("%s: not implemented", __func__);
    445 }
    446 
    447 int
    448 pgid_in_session(struct proc *p, pid_t pg_id)
    449 {
    450 
    451 	panic("%s: not implemented", __func__);
    452 }
    453 
    454 int
    455 sigispending(struct lwp *l, int signo)
    456 {
    457 
    458 	return 0;
    459 }
    460 
    461 void
    462 knote_fdclose(int fd)
    463 {
    464 
    465 	/* since we don't add knotes, we don't have to remove them */
    466 }
    467 
    468 int
    469 seltrue_kqfilter(dev_t dev, struct knote *kn)
    470 {
    471 
    472 	panic("%s: not implemented", __func__);
    473 }
    474 
    475 int
    476 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    477 {
    478 	extern int hz;
    479 	int rv, error;
    480 
    481 	if (mtx)
    482 		mutex_exit(mtx);
    483 	rv = rumpuser_usleep(timeo * (1000000 / hz), &error);
    484 	if (mtx)
    485 		mutex_enter(mtx);
    486 
    487 	if (rv)
    488 		return error;
    489 
    490 	return 0;
    491 }
    492 
    493 void
    494 suspendsched()
    495 {
    496 
    497 	panic("%s: not implemented", __func__);
    498 }
    499 
    500 void
    501 yield(void)
    502 {
    503 
    504 	rumpuser_yield();
    505 }
    506 
    507 
    508 u_int
    509 lwp_unsleep(lwp_t *l, bool cleanup)
    510 {
    511 
    512 	KASSERT(mutex_owned(l->l_mutex));
    513 
    514 	return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
    515 }
    516 
    517 vaddr_t
    518 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    519 {
    520 	paddr_t t;
    521 
    522 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    523 	if ((vaddr_t)t != t) {
    524 		panic("%s: needs tweak", __func__);
    525 	}
    526 	return t;
    527 }
    528 
    529 int
    530 seltrue(dev_t dev, int events, struct lwp *l)
    531 {
    532         return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    533 }
    534 
    535 void
    536 selrecord(lwp_t *selector, struct selinfo *sip)
    537 {
    538 }
    539 
    540 void
    541 selinit(struct selinfo *sip)
    542 {
    543 }
    544 
    545 void
    546 selnotify(struct selinfo *sip, int events, long knhint)
    547 {
    548 }
    549 
    550 void
    551 seldestroy(struct selinfo *sip)
    552 {
    553 }
    554