Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.3
      1 /*	$NetBSD: emul.c,v 1.3 2007/08/08 09:06:11 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 #include <sys/param.h>
     31 #include <sys/malloc.h>
     32 #include <sys/null.h>
     33 #include <sys/vnode.h>
     34 #include <sys/stat.h>
     35 #include <sys/syslog.h>
     36 #include <sys/namei.h>
     37 #include <sys/kauth.h>
     38 #include <sys/conf.h>
     39 #include <sys/device.h>
     40 #include <sys/queue.h>
     41 #include <sys/filedesc.h>
     42 
     43 #include <machine/cpu.h>
     44 #include <machine/stdarg.h>
     45 
     46 #include "rump.h"
     47 #include "rumpuser.h"
     48 
     49 time_t time_second = 1;
     50 
     51 kmutex_t proclist_mutex;
     52 struct lwp lwp0;
     53 struct vnode *rootvp;
     54 struct device *root_device;
     55 
     56 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     57 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     58 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     59 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     60 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
     61 
     62 struct lwp *curlwp;
     63 
     64 char hostname[MAXHOSTNAMELEN];
     65 size_t hostnamelen;
     66 
     67 void
     68 panic(const char *fmt, ...)
     69 {
     70 	va_list ap;
     71 
     72 	va_start(ap, fmt);
     73 	vprintf(fmt, ap);
     74 	va_end(ap);
     75 	printf("\n");
     76 	abort();
     77 }
     78 
     79 void
     80 log(int level, const char *fmt, ...)
     81 {
     82 	va_list ap;
     83 
     84 	va_start(ap, fmt);
     85 	vprintf(fmt, ap);
     86 	va_end(ap);
     87 }
     88 
     89 void
     90 uprintf(const char *fmt, ...)
     91 {
     92 	va_list ap;
     93 
     94 	va_start(ap, fmt);
     95 	vprintf(fmt, ap);
     96 	va_end(ap);
     97 }
     98 
     99 int
    100 copyin(const void *uaddr, void *kaddr, size_t len)
    101 {
    102 
    103 	memcpy(kaddr, uaddr, len);
    104 	return 0;
    105 }
    106 
    107 int
    108 copyout(const void *kaddr, void *uaddr, size_t len)
    109 {
    110 
    111 	memcpy(uaddr, kaddr, len);
    112 	return 0;
    113 }
    114 
    115 int
    116 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    117 {
    118 
    119 	return copyinstr(kfaddr, kdaddr, len, done);
    120 }
    121 
    122 int
    123 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    124 {
    125 
    126 	strlcpy(kaddr, uaddr, len);
    127 	*done = strlen(kaddr);
    128 	return 0;
    129 }
    130 
    131 int
    132 uiomove(void *buf, size_t n, struct uio *uio)
    133 {
    134 	struct iovec *iov;
    135 	uint8_t *b = buf;
    136 	size_t cnt;
    137 
    138 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    139 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    140 
    141 	if (buf == RUMP_UBC_MAGIC_WINDOW)
    142 		return rump_ubc_magic_uiomove(n, uio);
    143 
    144 	while (n && uio->uio_resid) {
    145 		iov = uio->uio_iov;
    146 		cnt = iov->iov_len;
    147 		if (cnt == 0) {
    148 			uio->uio_iov++;
    149 			uio->uio_iovcnt--;
    150 			continue;
    151 		}
    152 		if (cnt > n)
    153 			cnt = n;
    154 
    155 		if (uio->uio_rw == UIO_READ)
    156 			memcpy(iov->iov_base, b, cnt);
    157 		else
    158 			memcpy(b, iov->iov_base, cnt);
    159 
    160 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    161 		iov->iov_len -= cnt;
    162 		b += cnt;
    163 		uio->uio_resid -= cnt;
    164 		uio->uio_offset += cnt;
    165 		n -= cnt;
    166 	}
    167 
    168 	return 0;
    169 }
    170 
    171 void
    172 uio_setup_sysspace(struct uio *uio)
    173 {
    174 
    175 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    176 }
    177 
    178 const struct bdevsw *
    179 bdevsw_lookup(dev_t dev)
    180 {
    181 
    182 	return (const struct bdevsw *)1;
    183 }
    184 
    185 devclass_t
    186 device_class(device_t dev)
    187 {
    188 
    189 	if (dev != root_device)
    190 		panic("%s: dev != root_device not supported", __func__);
    191 
    192 	return DV_DISK;
    193 }
    194 
    195 void
    196 getmicrouptime(struct timeval *tvp)
    197 {
    198 
    199 	rumpuser_gettimeofday(tvp);
    200 }
    201 
    202 int
    203 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
    204 	volatile struct simplelock *slock)
    205 {
    206 
    207 	panic("%s: not implemented", __func__);
    208 }
    209 
    210 /*
    211  * XXX: ok, this is stupid
    212  */
    213 #undef bswap16
    214 uint16_t
    215 bswap16(uint16_t val)
    216 {
    217 
    218 	return rumpuser_bswap16(val);
    219 }
    220 
    221 #undef bswap32
    222 uint32_t
    223 bswap32(uint32_t value)
    224 {
    225 
    226 	return rumpuser_bswap32(value);
    227 }
    228 
    229 #undef bswap64
    230 uint64_t
    231 bswap64(uint64_t value)
    232 {
    233 
    234 	return rumpuser_bswap64(value);
    235 }
    236 
    237 void
    238 malloc_type_attach(struct malloc_type *type)
    239 {
    240 
    241 	return;
    242 }
    243 
    244 void
    245 malloc_type_detach(struct malloc_type *type)
    246 {
    247 
    248 	return;
    249 }
    250 
    251 void
    252 nanotime(struct timespec *ts)
    253 {
    254 	struct timeval tv;
    255 
    256 	rumpuser_gettimeofday(&tv);
    257 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    258 }
    259 
    260 /* hooray for mick, so what if I do */
    261 void
    262 getnanotime(struct timespec *ts)
    263 {
    264 
    265 	nanotime(ts);
    266 }
    267 
    268 void
    269 microtime(struct timeval *tv)
    270 {
    271 
    272 	rumpuser_gettimeofday(tv);
    273 }
    274 
    275 void
    276 bdev_strategy(struct buf *bp)
    277 {
    278 
    279 	panic("%s: not supported", __func__);
    280 }
    281