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