Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.11.2.2
      1 /*	$NetBSD: emul.c,v 1.11.2.2 2008/01/09 01:57:59 matt 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 #ifdef __HAVE_TIMECOUNTER
     58 time_t time_second = 1;
     59 #else
     60 volatile struct timeval time = { 1, 0 };
     61 #endif
     62 
     63 kmutex_t proclist_mutex;
     64 kmutex_t proclist_lock;
     65 struct lwp lwp0;
     66 struct vnode *rootvp;
     67 struct device *root_device;
     68 dev_t rootdev;
     69 struct vm_map *kernel_map;
     70 int physmem;
     71 int doing_shutdown;
     72 int ncpu = 1;
     73 const int schedppq = 1;
     74 
     75 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     76 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     77 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     78 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     79 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
     80 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
     81 
     82 char hostname[MAXHOSTNAMELEN];
     83 size_t hostnamelen;
     84 
     85 u_long	bufmem_valimit;
     86 u_long	bufmem_hiwater;
     87 u_long	bufmem_lowater;
     88 u_long	bufmem;
     89 u_int	nbuf;
     90 
     91 const char *panicstr;
     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 int
    139 copyin(const void *uaddr, void *kaddr, size_t len)
    140 {
    141 
    142 	memcpy(kaddr, uaddr, len);
    143 	return 0;
    144 }
    145 
    146 int
    147 copyout(const void *kaddr, void *uaddr, size_t len)
    148 {
    149 
    150 	memcpy(uaddr, kaddr, len);
    151 	return 0;
    152 }
    153 
    154 int
    155 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    156 {
    157 
    158 	return copyinstr(kfaddr, kdaddr, len, done);
    159 }
    160 
    161 int
    162 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    163 {
    164 
    165 	strlcpy(kaddr, uaddr, len);
    166 	*done = strlen(kaddr);
    167 	return 0;
    168 }
    169 
    170 int
    171 uiomove(void *buf, size_t n, struct uio *uio)
    172 {
    173 	struct iovec *iov;
    174 	uint8_t *b = buf;
    175 	size_t cnt;
    176 	int rv;
    177 
    178 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    179 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    180 
    181 	/*
    182 	 * See if rump ubc code claims the offset.  This is of course
    183 	 * a blatant violation of abstraction levels, but let's keep
    184 	 * me simple & stupid for now.
    185 	 */
    186 	if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
    187 		return rv;
    188 
    189 	while (n && uio->uio_resid) {
    190 		iov = uio->uio_iov;
    191 		cnt = iov->iov_len;
    192 		if (cnt == 0) {
    193 			uio->uio_iov++;
    194 			uio->uio_iovcnt--;
    195 			continue;
    196 		}
    197 		if (cnt > n)
    198 			cnt = n;
    199 
    200 		if (uio->uio_rw == UIO_READ)
    201 			memcpy(iov->iov_base, b, cnt);
    202 		else
    203 			memcpy(b, iov->iov_base, cnt);
    204 
    205 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    206 		iov->iov_len -= cnt;
    207 		b += cnt;
    208 		uio->uio_resid -= cnt;
    209 		uio->uio_offset += cnt;
    210 		n -= cnt;
    211 	}
    212 
    213 	return 0;
    214 }
    215 
    216 void
    217 uio_setup_sysspace(struct uio *uio)
    218 {
    219 
    220 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    221 }
    222 
    223 const struct bdevsw *
    224 bdevsw_lookup(dev_t dev)
    225 {
    226 
    227 	return (const struct bdevsw *)1;
    228 }
    229 
    230 devclass_t
    231 device_class(device_t dev)
    232 {
    233 
    234 	if (dev != root_device)
    235 		panic("%s: dev != root_device not supported", __func__);
    236 
    237 	return DV_DISK;
    238 }
    239 
    240 void
    241 getmicrouptime(struct timeval *tvp)
    242 {
    243 	int error;
    244 
    245 	rumpuser_gettimeofday(tvp, &error);
    246 }
    247 
    248 void
    249 malloc_type_attach(struct malloc_type *type)
    250 {
    251 
    252 	return;
    253 }
    254 
    255 void
    256 malloc_type_detach(struct malloc_type *type)
    257 {
    258 
    259 	return;
    260 }
    261 
    262 void *
    263 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    264 {
    265 	void *rv;
    266 
    267 	rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
    268 	if (rv && flags & M_ZERO)
    269 		memset(rv, 0, size);
    270 
    271 	return rv;
    272 }
    273 
    274 void
    275 nanotime(struct timespec *ts)
    276 {
    277 	struct timeval tv;
    278 	int error;
    279 
    280 	rumpuser_gettimeofday(&tv, &error);
    281 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    282 }
    283 
    284 /* hooray for mick, so what if I do */
    285 void
    286 getnanotime(struct timespec *ts)
    287 {
    288 
    289 	nanotime(ts);
    290 }
    291 
    292 void
    293 microtime(struct timeval *tv)
    294 {
    295 	int error;
    296 
    297 	rumpuser_gettimeofday(tv, &error);
    298 }
    299 
    300 void
    301 getmicrotime(struct timeval *tv)
    302 {
    303 	int error;
    304 
    305 	rumpuser_gettimeofday(tv, &error);
    306 }
    307 
    308 void
    309 bdev_strategy(struct buf *bp)
    310 {
    311 
    312 	panic("%s: not supported", __func__);
    313 }
    314 
    315 int
    316 bdev_type(dev_t dev)
    317 {
    318 
    319 	return D_DISK;
    320 }
    321 
    322 struct kthdesc {
    323 	void (*f)(void *);
    324 	void *arg;
    325 	struct lwp *mylwp;
    326 };
    327 
    328 static lwpid_t curlid = 2;
    329 
    330 static void *
    331 threadbouncer(void *arg)
    332 {
    333 	struct kthdesc *k = arg;
    334 	void (*f)(void *);
    335 	void *thrarg;
    336 
    337 	f = k->f;
    338 	thrarg = k->arg;
    339 	rumpuser_set_curlwp(k->mylwp);
    340 	kmem_free(k, sizeof(struct kthdesc));
    341 
    342 	f(thrarg);
    343 	panic("unreachable, should kthread_exit()");
    344 }
    345 
    346 int
    347 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    348 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    349 {
    350 	struct kthdesc *k;
    351 	struct lwp *l;
    352 	int rv;
    353 
    354 	KASSERT(fmt != NULL);
    355 	if (ci != NULL)
    356 		panic("%s: bounded threads not supported", __func__);
    357 
    358 	k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
    359 	k->f = func;
    360 	k->arg = arg;
    361 	k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
    362 	rv = rumpuser_thread_create(threadbouncer, k);
    363 	if (rv)
    364 		return rv;
    365 
    366 	if (newlp)
    367 		*newlp = l;
    368 	return 0;
    369 }
    370 
    371 void
    372 kthread_exit(int ecode)
    373 {
    374 
    375 	rumpuser_thread_exit();
    376 }
    377 
    378 void
    379 callout_init(callout_t *c, u_int flags)
    380 {
    381 
    382 	panic("%s: not implemented", __func__);
    383 }
    384 
    385 void
    386 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
    387 {
    388 
    389 	panic("%s: not implemented", __func__);
    390 }
    391 
    392 bool
    393 callout_stop(callout_t *c)
    394 {
    395 
    396 	panic("%s: not implemented", __func__);
    397 }
    398 
    399 struct proc *
    400 p_find(pid_t pid, uint flags)
    401 {
    402 
    403 	panic("%s: not implemented", __func__);
    404 }
    405 
    406 struct pgrp *
    407 pg_find(pid_t pid, uint flags)
    408 {
    409 
    410 	panic("%s: not implemented", __func__);
    411 }
    412 
    413 void
    414 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
    415 {
    416 
    417 	panic("%s: not implemented", __func__);
    418 }
    419 
    420 void
    421 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
    422 {
    423 
    424 	panic("%s: not implemented", __func__);
    425 }
    426 
    427 int
    428 pgid_in_session(struct proc *p, pid_t pg_id)
    429 {
    430 
    431 	panic("%s: not implemented", __func__);
    432 }
    433 
    434 int
    435 sigispending(struct lwp *l, int signo)
    436 {
    437 
    438 	return 0;
    439 }
    440 
    441 void
    442 knote_fdclose(struct lwp *l, int fd)
    443 {
    444 
    445 	/* since we don't add knotes, we don't have to remove them */
    446 }
    447 
    448 int
    449 seltrue_kqfilter(dev_t dev, struct knote *kn)
    450 {
    451 
    452 	panic("%s: not implemented", __func__);
    453 }
    454 
    455 int
    456 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    457 {
    458 	extern int hz;
    459 	int rv, error;
    460 
    461 	if (mtx)
    462 		mutex_exit(mtx);
    463 	rv = rumpuser_usleep(timeo * (1000000 / hz), &error);
    464 	if (mtx)
    465 		mutex_enter(mtx);
    466 
    467 	if (rv)
    468 		return error;
    469 
    470 	return 0;
    471 }
    472