Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.13
      1 /*	$NetBSD: emul.c,v 1.13 2007/09/11 16:33:19 briggs 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/filedesc.h>
     44 #include <sys/kthread.h>
     45 
     46 #include <machine/cpu.h>
     47 #include <machine/stdarg.h>
     48 
     49 #include <uvm/uvm_map.h>
     50 
     51 #include "rump_private.h"
     52 #include "rumpuser.h"
     53 
     54 #ifdef __HAVE_TIMECOUNTER
     55 time_t time_second = 1;
     56 #else
     57 volatile struct timeval time = { 1, 0 };
     58 #endif
     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;
     68 
     69 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     70 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     71 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     72 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     73 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
     74 
     75 struct lwp *curlwp;
     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 
     88 void
     89 panic(const char *fmt, ...)
     90 {
     91 	va_list ap;
     92 
     93 	va_start(ap, fmt);
     94 	vprintf(fmt, ap);
     95 	va_end(ap);
     96 	printf("\n");
     97 	abort();
     98 }
     99 
    100 void
    101 log(int level, const char *fmt, ...)
    102 {
    103 	va_list ap;
    104 
    105 	va_start(ap, fmt);
    106 	vprintf(fmt, ap);
    107 	va_end(ap);
    108 }
    109 
    110 void
    111 uprintf(const char *fmt, ...)
    112 {
    113 	va_list ap;
    114 
    115 	va_start(ap, fmt);
    116 	vprintf(fmt, ap);
    117 	va_end(ap);
    118 }
    119 
    120 int
    121 copyin(const void *uaddr, void *kaddr, size_t len)
    122 {
    123 
    124 	memcpy(kaddr, uaddr, len);
    125 	return 0;
    126 }
    127 
    128 int
    129 copyout(const void *kaddr, void *uaddr, size_t len)
    130 {
    131 
    132 	memcpy(uaddr, kaddr, len);
    133 	return 0;
    134 }
    135 
    136 int
    137 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    138 {
    139 
    140 	return copyinstr(kfaddr, kdaddr, len, done);
    141 }
    142 
    143 int
    144 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    145 {
    146 
    147 	strlcpy(kaddr, uaddr, len);
    148 	*done = strlen(kaddr);
    149 	return 0;
    150 }
    151 
    152 int
    153 uiomove(void *buf, size_t n, struct uio *uio)
    154 {
    155 	struct iovec *iov;
    156 	uint8_t *b = buf;
    157 	size_t cnt;
    158 
    159 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    160 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    161 
    162 	if (buf == RUMP_UBC_MAGIC_WINDOW)
    163 		return rump_ubc_magic_uiomove(n, uio);
    164 
    165 	while (n && uio->uio_resid) {
    166 		iov = uio->uio_iov;
    167 		cnt = iov->iov_len;
    168 		if (cnt == 0) {
    169 			uio->uio_iov++;
    170 			uio->uio_iovcnt--;
    171 			continue;
    172 		}
    173 		if (cnt > n)
    174 			cnt = n;
    175 
    176 		if (uio->uio_rw == UIO_READ)
    177 			memcpy(iov->iov_base, b, cnt);
    178 		else
    179 			memcpy(b, iov->iov_base, cnt);
    180 
    181 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    182 		iov->iov_len -= cnt;
    183 		b += cnt;
    184 		uio->uio_resid -= cnt;
    185 		uio->uio_offset += cnt;
    186 		n -= cnt;
    187 	}
    188 
    189 	return 0;
    190 }
    191 
    192 void
    193 uio_setup_sysspace(struct uio *uio)
    194 {
    195 
    196 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    197 }
    198 
    199 const struct bdevsw *
    200 bdevsw_lookup(dev_t dev)
    201 {
    202 
    203 	return (const struct bdevsw *)1;
    204 }
    205 
    206 devclass_t
    207 device_class(device_t dev)
    208 {
    209 
    210 	if (dev != root_device)
    211 		panic("%s: dev != root_device not supported", __func__);
    212 
    213 	return DV_DISK;
    214 }
    215 
    216 void
    217 getmicrouptime(struct timeval *tvp)
    218 {
    219 	int error;
    220 
    221 	rumpuser_gettimeofday(tvp, &error);
    222 }
    223 
    224 int
    225 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
    226 	volatile struct simplelock *slock)
    227 {
    228 
    229 	panic("%s: not implemented", __func__);
    230 }
    231 
    232 void
    233 wakeup(wchan_t ident)
    234 {
    235 
    236 	printf("%s: not implemented\n", __func__);
    237 }
    238 
    239 void
    240 malloc_type_attach(struct malloc_type *type)
    241 {
    242 
    243 	return;
    244 }
    245 
    246 void
    247 malloc_type_detach(struct malloc_type *type)
    248 {
    249 
    250 	return;
    251 }
    252 
    253 void *
    254 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    255 {
    256 	void *rv;
    257 
    258 	rv = rumpuser_malloc(size, flags * (M_CANFAIL | M_NOWAIT));
    259 	if (rv && flags & M_ZERO)
    260 		memset(rv, 0, size);
    261 
    262 	return rv;
    263 }
    264 
    265 void
    266 nanotime(struct timespec *ts)
    267 {
    268 	struct timeval tv;
    269 	int error;
    270 
    271 	rumpuser_gettimeofday(&tv, &error);
    272 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    273 }
    274 
    275 /* hooray for mick, so what if I do */
    276 void
    277 getnanotime(struct timespec *ts)
    278 {
    279 
    280 	nanotime(ts);
    281 }
    282 
    283 void
    284 microtime(struct timeval *tv)
    285 {
    286 	int error;
    287 
    288 	rumpuser_gettimeofday(tv, &error);
    289 }
    290 
    291 void
    292 getmicrotime(struct timeval *tv)
    293 {
    294 	int error;
    295 
    296 	rumpuser_gettimeofday(tv, &error);
    297 }
    298 
    299 void
    300 bdev_strategy(struct buf *bp)
    301 {
    302 
    303 	panic("%s: not supported", __func__);
    304 }
    305 
    306 int
    307 bdev_type(dev_t dev)
    308 {
    309 
    310 	return D_DISK;
    311 }
    312 
    313 int
    314 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    315 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    316 {
    317 
    318 	return 0;
    319 }
    320 
    321 void
    322 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
    323 {
    324 
    325 }
    326 
    327 void
    328 callout_init(callout_t *c, u_int flags)
    329 {
    330 
    331 }
    332 
    333 void
    334 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
    335 {
    336 
    337 	panic("%s: not implemented", __func__);
    338 }
    339 
    340 bool
    341 callout_stop(callout_t *c)
    342 {
    343 
    344 	panic("%s: not implemented", __func__);
    345 }
    346