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