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