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