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