Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.30
      1 /*	$NetBSD: emul.c,v 1.30 2008/03/11 10:50:16 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/syslog.h>
     38 #include <sys/namei.h>
     39 #include <sys/kauth.h>
     40 #include <sys/conf.h>
     41 #include <sys/device.h>
     42 #include <sys/queue.h>
     43 #include <sys/file.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kthread.h>
     46 #include <sys/cpu.h>
     47 #include <sys/kmem.h>
     48 #include <sys/poll.h>
     49 
     50 #include <machine/stdarg.h>
     51 
     52 #include <uvm/uvm_map.h>
     53 
     54 #include "rump_private.h"
     55 #include "rumpuser.h"
     56 
     57 time_t time_second = 1;
     58 
     59 kmutex_t proclist_mutex;
     60 kmutex_t proclist_lock;
     61 struct lwp lwp0;
     62 struct vnode *rootvp;
     63 struct device *root_device;
     64 dev_t rootdev;
     65 struct vm_map *kernel_map;
     66 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
     67 int doing_shutdown;
     68 int ncpu = 1;
     69 const int schedppq = 1;
     70 int hardclock_ticks;
     71 
     72 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     73 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     74 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     75 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
     76 
     77 char hostname[MAXHOSTNAMELEN];
     78 size_t hostnamelen;
     79 
     80 u_long	bufmem_valimit;
     81 u_long	bufmem_hiwater;
     82 u_long	bufmem_lowater;
     83 u_long	bufmem;
     84 u_int	nbuf;
     85 
     86 const char *panicstr;
     87 const char ostype[] = "NetBSD";
     88 const char osrelease[] = "999"; /* paradroid 4evah */
     89 const char kernel_ident[] = "RUMP-ROAST";
     90 const char *domainname;
     91 int domainnamelen;
     92 
     93 const struct filterops seltrue_filtops;
     94 
     95 void
     96 panic(const char *fmt, ...)
     97 {
     98 	va_list ap;
     99 
    100 	va_start(ap, fmt);
    101 	printf("panic: ");
    102 	vprintf(fmt, ap);
    103 	va_end(ap);
    104 	printf("\n");
    105 	abort();
    106 }
    107 
    108 void
    109 log(int level, const char *fmt, ...)
    110 {
    111 	va_list ap;
    112 
    113 	va_start(ap, fmt);
    114 	vprintf(fmt, ap);
    115 	va_end(ap);
    116 }
    117 
    118 void
    119 uprintf(const char *fmt, ...)
    120 {
    121 	va_list ap;
    122 
    123 	va_start(ap, fmt);
    124 	vprintf(fmt, ap);
    125 	va_end(ap);
    126 }
    127 
    128 void
    129 printf_nolog(const char *fmt, ...)
    130 {
    131 	va_list ap;
    132 
    133 	va_start(ap, fmt);
    134 	vprintf(fmt, ap);
    135 	va_end(ap);
    136 }
    137 
    138 void
    139 aprint_normal(const char *fmt, ...)
    140 {
    141 	va_list ap;
    142 
    143 	va_start(ap, fmt);
    144 	vprintf(fmt, ap);
    145 	va_end(ap);
    146 }
    147 
    148 int
    149 copyin(const void *uaddr, void *kaddr, size_t len)
    150 {
    151 
    152 	memcpy(kaddr, uaddr, len);
    153 	return 0;
    154 }
    155 
    156 int
    157 copyout(const void *kaddr, void *uaddr, size_t len)
    158 {
    159 
    160 	memcpy(uaddr, kaddr, len);
    161 	return 0;
    162 }
    163 
    164 int
    165 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    166 {
    167 
    168 	return copyinstr(kfaddr, kdaddr, len, done);
    169 }
    170 
    171 int
    172 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    173 {
    174 
    175 	strlcpy(kaddr, uaddr, len);
    176 	*done = strlen(kaddr)+1; /* includes termination */
    177 	return 0;
    178 }
    179 
    180 int
    181 uiomove(void *buf, size_t n, struct uio *uio)
    182 {
    183 	struct iovec *iov;
    184 	uint8_t *b = buf;
    185 	size_t cnt;
    186 	int rv;
    187 
    188 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    189 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    190 
    191 	/*
    192 	 * See if rump ubc code claims the offset.  This is of course
    193 	 * a blatant violation of abstraction levels, but let's keep
    194 	 * me simple & stupid for now.
    195 	 */
    196 	if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
    197 		return rv;
    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 const struct bdevsw *
    234 bdevsw_lookup(dev_t dev)
    235 {
    236 
    237 	return (const struct bdevsw *)1;
    238 }
    239 
    240 devclass_t
    241 device_class(device_t dev)
    242 {
    243 
    244 	if (dev != root_device)
    245 		panic("%s: dev != root_device not supported", __func__);
    246 
    247 	return DV_DISK;
    248 }
    249 
    250 void
    251 getmicrouptime(struct timeval *tvp)
    252 {
    253 	int error;
    254 
    255 	rumpuser_gettimeofday(tvp, &error);
    256 }
    257 
    258 void
    259 malloc_type_attach(struct malloc_type *type)
    260 {
    261 
    262 	return;
    263 }
    264 
    265 void
    266 malloc_type_detach(struct malloc_type *type)
    267 {
    268 
    269 	return;
    270 }
    271 
    272 void *
    273 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    274 {
    275 	void *rv;
    276 
    277 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    278 	if (rv && flags & M_ZERO)
    279 		memset(rv, 0, size);
    280 
    281 	return rv;
    282 }
    283 
    284 void
    285 nanotime(struct timespec *ts)
    286 {
    287 	struct timeval tv;
    288 	int error;
    289 
    290 	rumpuser_gettimeofday(&tv, &error);
    291 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    292 }
    293 
    294 /* hooray for mick, so what if I do */
    295 void
    296 getnanotime(struct timespec *ts)
    297 {
    298 
    299 	nanotime(ts);
    300 }
    301 
    302 void
    303 microtime(struct timeval *tv)
    304 {
    305 	int error;
    306 
    307 	rumpuser_gettimeofday(tv, &error);
    308 }
    309 
    310 void
    311 getmicrotime(struct timeval *tv)
    312 {
    313 	int error;
    314 
    315 	rumpuser_gettimeofday(tv, &error);
    316 }
    317 
    318 void
    319 bdev_strategy(struct buf *bp)
    320 {
    321 
    322 	panic("%s: not supported", __func__);
    323 }
    324 
    325 int
    326 bdev_type(dev_t dev)
    327 {
    328 
    329 	return D_DISK;
    330 }
    331 
    332 struct kthdesc {
    333 	void (*f)(void *);
    334 	void *arg;
    335 	struct lwp *mylwp;
    336 };
    337 
    338 static lwpid_t curlid = 2;
    339 
    340 static void *
    341 threadbouncer(void *arg)
    342 {
    343 	struct kthdesc *k = arg;
    344 	void (*f)(void *);
    345 	void *thrarg;
    346 
    347 	f = k->f;
    348 	thrarg = k->arg;
    349 	rumpuser_set_curlwp(k->mylwp);
    350 	kmem_free(k, sizeof(struct kthdesc));
    351 
    352 	f(thrarg);
    353 	panic("unreachable, should kthread_exit()");
    354 }
    355 
    356 int
    357 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    358 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    359 {
    360 	struct kthdesc *k;
    361 	struct lwp *l;
    362 	int rv;
    363 
    364 #ifdef RUMP_WITHOUT_THREADS
    365 	/* XXX: fake it */
    366 	if (strcmp(fmt, "vrele") == 0)
    367 		return 0;
    368 	else
    369 		panic("threads not available, undef RUMP_WITHOUT_THREADS");
    370 #endif
    371 
    372 	KASSERT(fmt != NULL);
    373 	if (ci != NULL)
    374 		panic("%s: bounded threads not supported", __func__);
    375 
    376 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    377 	k->f = func;
    378 	k->arg = arg;
    379 	k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
    380 	rv = rumpuser_thread_create(threadbouncer, k);
    381 	if (rv)
    382 		return rv;
    383 
    384 	if (newlp)
    385 		*newlp = l;
    386 	return 0;
    387 }
    388 
    389 void
    390 kthread_exit(int ecode)
    391 {
    392 
    393 	rumpuser_thread_exit();
    394 }
    395 
    396 void
    397 callout_init(callout_t *c, u_int flags)
    398 {
    399 
    400 	panic("%s: not implemented", __func__);
    401 }
    402 
    403 void
    404 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
    405 {
    406 
    407 	panic("%s: not implemented", __func__);
    408 }
    409 
    410 bool
    411 callout_stop(callout_t *c)
    412 {
    413 
    414 	panic("%s: not implemented", __func__);
    415 }
    416 
    417 struct proc *
    418 p_find(pid_t pid, uint flags)
    419 {
    420 
    421 	panic("%s: not implemented", __func__);
    422 }
    423 
    424 struct pgrp *
    425 pg_find(pid_t pid, uint flags)
    426 {
    427 
    428 	panic("%s: not implemented", __func__);
    429 }
    430 
    431 void
    432 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    433 {
    434 
    435 	panic("%s: not implemented", __func__);
    436 }
    437 
    438 void
    439 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    440 {
    441 
    442 	panic("%s: not implemented", __func__);
    443 }
    444 
    445 int
    446 pgid_in_session(struct proc *p, pid_t pg_id)
    447 {
    448 
    449 	panic("%s: not implemented", __func__);
    450 }
    451 
    452 int
    453 sigispending(struct lwp *l, int signo)
    454 {
    455 
    456 	return 0;
    457 }
    458 
    459 void
    460 knote_fdclose(struct lwp *l, int fd)
    461 {
    462 
    463 	/* since we don't add knotes, we don't have to remove them */
    464 }
    465 
    466 int
    467 seltrue_kqfilter(dev_t dev, struct knote *kn)
    468 {
    469 
    470 	panic("%s: not implemented", __func__);
    471 }
    472 
    473 int
    474 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    475 {
    476 	extern int hz;
    477 	int rv, error;
    478 
    479 	if (mtx)
    480 		mutex_exit(mtx);
    481 	rv = rumpuser_usleep(timeo * (1000000 / hz), &error);
    482 	if (mtx)
    483 		mutex_enter(mtx);
    484 
    485 	if (rv)
    486 		return error;
    487 
    488 	return 0;
    489 }
    490 
    491 void
    492 suspendsched()
    493 {
    494 
    495 	panic("%s: not implemented", __func__);
    496 }
    497 
    498 void
    499 yield(void)
    500 {
    501 
    502 	rumpuser_yield();
    503 }
    504