Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.10.2.2
      1  1.10.2.2  ad /*	$NetBSD: emul.c,v 1.10.2.2 2007/08/20 22:07:26 ad Exp $	*/
      2  1.10.2.2  ad 
      3  1.10.2.2  ad /*
      4  1.10.2.2  ad  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
      5  1.10.2.2  ad  *
      6  1.10.2.2  ad  * Development of this software was supported by Google Summer of Code.
      7  1.10.2.2  ad  *
      8  1.10.2.2  ad  * Redistribution and use in source and binary forms, with or without
      9  1.10.2.2  ad  * modification, are permitted provided that the following conditions
     10  1.10.2.2  ad  * are met:
     11  1.10.2.2  ad  * 1. Redistributions of source code must retain the above copyright
     12  1.10.2.2  ad  *    notice, this list of conditions and the following disclaimer.
     13  1.10.2.2  ad  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.10.2.2  ad  *    notice, this list of conditions and the following disclaimer in the
     15  1.10.2.2  ad  *    documentation and/or other materials provided with the distribution.
     16  1.10.2.2  ad  *
     17  1.10.2.2  ad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     18  1.10.2.2  ad  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  1.10.2.2  ad  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  1.10.2.2  ad  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.10.2.2  ad  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.10.2.2  ad  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     23  1.10.2.2  ad  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.10.2.2  ad  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.10.2.2  ad  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.10.2.2  ad  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.10.2.2  ad  * SUCH DAMAGE.
     28  1.10.2.2  ad  */
     29  1.10.2.2  ad 
     30  1.10.2.2  ad #define malloc(a,b,c) __wrap_malloc(a,b,c)
     31  1.10.2.2  ad 
     32  1.10.2.2  ad #include <sys/param.h>
     33  1.10.2.2  ad #include <sys/malloc.h>
     34  1.10.2.2  ad #include <sys/null.h>
     35  1.10.2.2  ad #include <sys/vnode.h>
     36  1.10.2.2  ad #include <sys/stat.h>
     37  1.10.2.2  ad #include <sys/syslog.h>
     38  1.10.2.2  ad #include <sys/namei.h>
     39  1.10.2.2  ad #include <sys/kauth.h>
     40  1.10.2.2  ad #include <sys/conf.h>
     41  1.10.2.2  ad #include <sys/device.h>
     42  1.10.2.2  ad #include <sys/queue.h>
     43  1.10.2.2  ad #include <sys/filedesc.h>
     44  1.10.2.2  ad #include <sys/kthread.h>
     45  1.10.2.2  ad 
     46  1.10.2.2  ad #include <machine/cpu.h>
     47  1.10.2.2  ad #include <machine/stdarg.h>
     48  1.10.2.2  ad 
     49  1.10.2.2  ad #include <uvm/uvm_map.h>
     50  1.10.2.2  ad 
     51  1.10.2.2  ad #include "rump.h"
     52  1.10.2.2  ad #include "rumpuser.h"
     53  1.10.2.2  ad 
     54  1.10.2.2  ad time_t time_second = 1;
     55  1.10.2.2  ad 
     56  1.10.2.2  ad kmutex_t proclist_mutex;
     57  1.10.2.2  ad kmutex_t proclist_lock;
     58  1.10.2.2  ad struct lwp lwp0;
     59  1.10.2.2  ad struct vnode *rootvp;
     60  1.10.2.2  ad struct device *root_device;
     61  1.10.2.2  ad dev_t rootdev;
     62  1.10.2.2  ad struct vm_map *kernel_map;
     63  1.10.2.2  ad int physmem;
     64  1.10.2.2  ad 
     65  1.10.2.2  ad MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
     66  1.10.2.2  ad MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
     67  1.10.2.2  ad MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
     68  1.10.2.2  ad MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
     69  1.10.2.2  ad MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
     70  1.10.2.2  ad 
     71  1.10.2.2  ad struct lwp *curlwp;
     72  1.10.2.2  ad 
     73  1.10.2.2  ad char hostname[MAXHOSTNAMELEN];
     74  1.10.2.2  ad size_t hostnamelen;
     75  1.10.2.2  ad 
     76  1.10.2.2  ad u_long	bufmem_valimit;
     77  1.10.2.2  ad u_long	bufmem_hiwater;
     78  1.10.2.2  ad u_long	bufmem_lowater;
     79  1.10.2.2  ad u_long	bufmem;
     80  1.10.2.2  ad u_int	nbuf;
     81  1.10.2.2  ad 
     82  1.10.2.2  ad const char *panicstr;
     83  1.10.2.2  ad 
     84  1.10.2.2  ad void
     85  1.10.2.2  ad panic(const char *fmt, ...)
     86  1.10.2.2  ad {
     87  1.10.2.2  ad 	va_list ap;
     88  1.10.2.2  ad 
     89  1.10.2.2  ad 	va_start(ap, fmt);
     90  1.10.2.2  ad 	vprintf(fmt, ap);
     91  1.10.2.2  ad 	va_end(ap);
     92  1.10.2.2  ad 	printf("\n");
     93  1.10.2.2  ad 	abort();
     94  1.10.2.2  ad }
     95  1.10.2.2  ad 
     96  1.10.2.2  ad void
     97  1.10.2.2  ad log(int level, const char *fmt, ...)
     98  1.10.2.2  ad {
     99  1.10.2.2  ad 	va_list ap;
    100  1.10.2.2  ad 
    101  1.10.2.2  ad 	va_start(ap, fmt);
    102  1.10.2.2  ad 	vprintf(fmt, ap);
    103  1.10.2.2  ad 	va_end(ap);
    104  1.10.2.2  ad }
    105  1.10.2.2  ad 
    106  1.10.2.2  ad void
    107  1.10.2.2  ad uprintf(const char *fmt, ...)
    108  1.10.2.2  ad {
    109  1.10.2.2  ad 	va_list ap;
    110  1.10.2.2  ad 
    111  1.10.2.2  ad 	va_start(ap, fmt);
    112  1.10.2.2  ad 	vprintf(fmt, ap);
    113  1.10.2.2  ad 	va_end(ap);
    114  1.10.2.2  ad }
    115  1.10.2.2  ad 
    116  1.10.2.2  ad int
    117  1.10.2.2  ad copyin(const void *uaddr, void *kaddr, size_t len)
    118  1.10.2.2  ad {
    119  1.10.2.2  ad 
    120  1.10.2.2  ad 	memcpy(kaddr, uaddr, len);
    121  1.10.2.2  ad 	return 0;
    122  1.10.2.2  ad }
    123  1.10.2.2  ad 
    124  1.10.2.2  ad int
    125  1.10.2.2  ad copyout(const void *kaddr, void *uaddr, size_t len)
    126  1.10.2.2  ad {
    127  1.10.2.2  ad 
    128  1.10.2.2  ad 	memcpy(uaddr, kaddr, len);
    129  1.10.2.2  ad 	return 0;
    130  1.10.2.2  ad }
    131  1.10.2.2  ad 
    132  1.10.2.2  ad int
    133  1.10.2.2  ad copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
    134  1.10.2.2  ad {
    135  1.10.2.2  ad 
    136  1.10.2.2  ad 	return copyinstr(kfaddr, kdaddr, len, done);
    137  1.10.2.2  ad }
    138  1.10.2.2  ad 
    139  1.10.2.2  ad int
    140  1.10.2.2  ad copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
    141  1.10.2.2  ad {
    142  1.10.2.2  ad 
    143  1.10.2.2  ad 	strlcpy(kaddr, uaddr, len);
    144  1.10.2.2  ad 	*done = strlen(kaddr);
    145  1.10.2.2  ad 	return 0;
    146  1.10.2.2  ad }
    147  1.10.2.2  ad 
    148  1.10.2.2  ad int
    149  1.10.2.2  ad uiomove(void *buf, size_t n, struct uio *uio)
    150  1.10.2.2  ad {
    151  1.10.2.2  ad 	struct iovec *iov;
    152  1.10.2.2  ad 	uint8_t *b = buf;
    153  1.10.2.2  ad 	size_t cnt;
    154  1.10.2.2  ad 
    155  1.10.2.2  ad 	if (uio->uio_vmspace != UIO_VMSPACE_SYS)
    156  1.10.2.2  ad 		panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
    157  1.10.2.2  ad 
    158  1.10.2.2  ad 	if (buf == RUMP_UBC_MAGIC_WINDOW)
    159  1.10.2.2  ad 		return rump_ubc_magic_uiomove(n, uio);
    160  1.10.2.2  ad 
    161  1.10.2.2  ad 	while (n && uio->uio_resid) {
    162  1.10.2.2  ad 		iov = uio->uio_iov;
    163  1.10.2.2  ad 		cnt = iov->iov_len;
    164  1.10.2.2  ad 		if (cnt == 0) {
    165  1.10.2.2  ad 			uio->uio_iov++;
    166  1.10.2.2  ad 			uio->uio_iovcnt--;
    167  1.10.2.2  ad 			continue;
    168  1.10.2.2  ad 		}
    169  1.10.2.2  ad 		if (cnt > n)
    170  1.10.2.2  ad 			cnt = n;
    171  1.10.2.2  ad 
    172  1.10.2.2  ad 		if (uio->uio_rw == UIO_READ)
    173  1.10.2.2  ad 			memcpy(iov->iov_base, b, cnt);
    174  1.10.2.2  ad 		else
    175  1.10.2.2  ad 			memcpy(b, iov->iov_base, cnt);
    176  1.10.2.2  ad 
    177  1.10.2.2  ad 		iov->iov_base = (uint8_t *)iov->iov_base + cnt;
    178  1.10.2.2  ad 		iov->iov_len -= cnt;
    179  1.10.2.2  ad 		b += cnt;
    180  1.10.2.2  ad 		uio->uio_resid -= cnt;
    181  1.10.2.2  ad 		uio->uio_offset += cnt;
    182  1.10.2.2  ad 		n -= cnt;
    183  1.10.2.2  ad 	}
    184  1.10.2.2  ad 
    185  1.10.2.2  ad 	return 0;
    186  1.10.2.2  ad }
    187  1.10.2.2  ad 
    188  1.10.2.2  ad void
    189  1.10.2.2  ad uio_setup_sysspace(struct uio *uio)
    190  1.10.2.2  ad {
    191  1.10.2.2  ad 
    192  1.10.2.2  ad 	uio->uio_vmspace = UIO_VMSPACE_SYS;
    193  1.10.2.2  ad }
    194  1.10.2.2  ad 
    195  1.10.2.2  ad const struct bdevsw *
    196  1.10.2.2  ad bdevsw_lookup(dev_t dev)
    197  1.10.2.2  ad {
    198  1.10.2.2  ad 
    199  1.10.2.2  ad 	return (const struct bdevsw *)1;
    200  1.10.2.2  ad }
    201  1.10.2.2  ad 
    202  1.10.2.2  ad devclass_t
    203  1.10.2.2  ad device_class(device_t dev)
    204  1.10.2.2  ad {
    205  1.10.2.2  ad 
    206  1.10.2.2  ad 	if (dev != root_device)
    207  1.10.2.2  ad 		panic("%s: dev != root_device not supported", __func__);
    208  1.10.2.2  ad 
    209  1.10.2.2  ad 	return DV_DISK;
    210  1.10.2.2  ad }
    211  1.10.2.2  ad 
    212  1.10.2.2  ad void
    213  1.10.2.2  ad getmicrouptime(struct timeval *tvp)
    214  1.10.2.2  ad {
    215  1.10.2.2  ad 
    216  1.10.2.2  ad 	rumpuser_gettimeofday(tvp);
    217  1.10.2.2  ad }
    218  1.10.2.2  ad 
    219  1.10.2.2  ad int
    220  1.10.2.2  ad ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
    221  1.10.2.2  ad 	volatile struct simplelock *slock)
    222  1.10.2.2  ad {
    223  1.10.2.2  ad 
    224  1.10.2.2  ad 	panic("%s: not implemented", __func__);
    225  1.10.2.2  ad }
    226  1.10.2.2  ad 
    227  1.10.2.2  ad void
    228  1.10.2.2  ad wakeup(wchan_t ident)
    229  1.10.2.2  ad {
    230  1.10.2.2  ad 
    231  1.10.2.2  ad 	printf("%s: not implemented\n", __func__);
    232  1.10.2.2  ad }
    233  1.10.2.2  ad 
    234  1.10.2.2  ad void
    235  1.10.2.2  ad malloc_type_attach(struct malloc_type *type)
    236  1.10.2.2  ad {
    237  1.10.2.2  ad 
    238  1.10.2.2  ad 	return;
    239  1.10.2.2  ad }
    240  1.10.2.2  ad 
    241  1.10.2.2  ad void
    242  1.10.2.2  ad malloc_type_detach(struct malloc_type *type)
    243  1.10.2.2  ad {
    244  1.10.2.2  ad 
    245  1.10.2.2  ad 	return;
    246  1.10.2.2  ad }
    247  1.10.2.2  ad 
    248  1.10.2.2  ad void *
    249  1.10.2.2  ad __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
    250  1.10.2.2  ad {
    251  1.10.2.2  ad 	void *rv;
    252  1.10.2.2  ad 
    253  1.10.2.2  ad 	rv = rumpuser_malloc(size, flags * (M_CANFAIL | M_NOWAIT));
    254  1.10.2.2  ad 	if (rv && flags & M_ZERO)
    255  1.10.2.2  ad 		memset(rv, 0, size);
    256  1.10.2.2  ad 
    257  1.10.2.2  ad 	return rv;
    258  1.10.2.2  ad }
    259  1.10.2.2  ad 
    260  1.10.2.2  ad void
    261  1.10.2.2  ad nanotime(struct timespec *ts)
    262  1.10.2.2  ad {
    263  1.10.2.2  ad 	struct timeval tv;
    264  1.10.2.2  ad 
    265  1.10.2.2  ad 	rumpuser_gettimeofday(&tv);
    266  1.10.2.2  ad 	TIMEVAL_TO_TIMESPEC(&tv, ts);
    267  1.10.2.2  ad }
    268  1.10.2.2  ad 
    269  1.10.2.2  ad /* hooray for mick, so what if I do */
    270  1.10.2.2  ad void
    271  1.10.2.2  ad getnanotime(struct timespec *ts)
    272  1.10.2.2  ad {
    273  1.10.2.2  ad 
    274  1.10.2.2  ad 	nanotime(ts);
    275  1.10.2.2  ad }
    276  1.10.2.2  ad 
    277  1.10.2.2  ad void
    278  1.10.2.2  ad microtime(struct timeval *tv)
    279  1.10.2.2  ad {
    280  1.10.2.2  ad 
    281  1.10.2.2  ad 	rumpuser_gettimeofday(tv);
    282  1.10.2.2  ad }
    283  1.10.2.2  ad 
    284  1.10.2.2  ad void
    285  1.10.2.2  ad getmicrotime(struct timeval *tv)
    286  1.10.2.2  ad {
    287  1.10.2.2  ad 
    288  1.10.2.2  ad 	rumpuser_gettimeofday(tv);
    289  1.10.2.2  ad }
    290  1.10.2.2  ad 
    291  1.10.2.2  ad void
    292  1.10.2.2  ad bdev_strategy(struct buf *bp)
    293  1.10.2.2  ad {
    294  1.10.2.2  ad 
    295  1.10.2.2  ad 	panic("%s: not supported", __func__);
    296  1.10.2.2  ad }
    297  1.10.2.2  ad 
    298  1.10.2.2  ad int
    299  1.10.2.2  ad bdev_type(dev_t dev)
    300  1.10.2.2  ad {
    301  1.10.2.2  ad 
    302  1.10.2.2  ad 	return D_DISK;
    303  1.10.2.2  ad }
    304  1.10.2.2  ad 
    305  1.10.2.2  ad int
    306  1.10.2.2  ad kthread_create(pri_t pri, int flags, struct cpu_info *ci,
    307  1.10.2.2  ad 	void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
    308  1.10.2.2  ad {
    309  1.10.2.2  ad 
    310  1.10.2.2  ad 	return 0;
    311  1.10.2.2  ad }
    312  1.10.2.2  ad 
    313  1.10.2.2  ad void
    314  1.10.2.2  ad workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
    315  1.10.2.2  ad {
    316  1.10.2.2  ad 
    317  1.10.2.2  ad }
    318