Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.138
      1 /*	$NetBSD: emul.c,v 1.138 2010/06/10 21:40:42 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/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.138 2010/06/10 21:40:42 pooka Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/null.h>
     35 #include <sys/vnode.h>
     36 #include <sys/stat.h>
     37 #include <sys/select.h>
     38 #include <sys/syslog.h>
     39 #include <sys/namei.h>
     40 #include <sys/kauth.h>
     41 #include <sys/conf.h>
     42 #include <sys/device.h>
     43 #include <sys/queue.h>
     44 #include <sys/file.h>
     45 #include <sys/cpu.h>
     46 #include <sys/kmem.h>
     47 #include <sys/poll.h>
     48 #include <sys/timetc.h>
     49 #include <sys/tprintf.h>
     50 #include <sys/module.h>
     51 #include <sys/tty.h>
     52 #include <sys/reboot.h>
     53 #include <sys/syscallvar.h>
     54 #include <sys/xcall.h>
     55 
     56 #include <dev/cons.h>
     57 
     58 #include <rump/rumpuser.h>
     59 
     60 #include <uvm/uvm_map.h>
     61 
     62 #include "rump_private.h"
     63 
     64 struct lwp lwp0;
     65 struct vnode *rootvp;
     66 dev_t rootdev = NODEV;
     67 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
     68 int nkmempages = 256*256/2; /* from le chapeau */
     69 const int schedppq = 1;
     70 int hardclock_ticks;
     71 bool mp_online = false;
     72 struct timeval boottime;
     73 int cold = 1;
     74 int boothowto = AB_SILENT;
     75 struct tty *constty;
     76 
     77 const struct bdevsw *bdevsw0[255];
     78 const struct bdevsw **bdevsw = bdevsw0;
     79 const int sys_cdevsws = 255;
     80 int max_cdevsws = 255;
     81 
     82 const struct cdevsw *cdevsw0[255];
     83 const struct cdevsw **cdevsw = cdevsw0;
     84 const int sys_bdevsws = 255;
     85 int max_bdevsws = 255;
     86 
     87 int mem_no = 2;
     88 
     89 struct device *booted_device;
     90 struct device *booted_wedge;
     91 int booted_partition;
     92 
     93 /* XXX: unused */
     94 kmutex_t tty_lock;
     95 krwlock_t exec_lock;
     96 
     97 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
     98 
     99 /* sparc doesn't sport constant page size */
    100 #ifdef __sparc__
    101 int nbpg = 4096;
    102 #endif
    103 
    104 struct loadavg averunnable = {
    105 	{ 0 * FSCALE,
    106 	  1 * FSCALE,
    107 	  11 * FSCALE, },
    108 	FSCALE,
    109 };
    110 
    111 struct emul emul_netbsd = {
    112 	.e_name = "netbsd-rump",
    113 	.e_sysent = rump_sysent,
    114 	.e_vm_default_addr = uvm_default_mapaddr,
    115 #ifdef __HAVE_SYSCALL_INTERN
    116 	.e_syscall_intern = syscall_intern,
    117 #endif
    118 };
    119 
    120 u_int nprocs = 1;
    121 
    122 int
    123 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    124 {
    125 	extern int hz;
    126 	int rv, error;
    127 	uint64_t sec, nsec;
    128 
    129 	if (mtx)
    130 		mutex_exit(mtx);
    131 
    132 	sec = timeo / hz;
    133 	nsec = (timeo % hz) * (1000000000 / hz);
    134 	rv = rumpuser_nanosleep(&sec, &nsec, &error);
    135 
    136 	if (mtx)
    137 		mutex_enter(mtx);
    138 
    139 	if (rv)
    140 		return error;
    141 
    142 	return 0;
    143 }
    144 
    145 void
    146 lwp_unsleep(lwp_t *l, bool cleanup)
    147 {
    148 
    149 	KASSERT(mutex_owned(l->l_mutex));
    150 
    151 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
    152 }
    153 
    154 vaddr_t
    155 calc_cache_size(struct vm_map *map, int pct, int va_pct)
    156 {
    157 	paddr_t t;
    158 
    159 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    160 	if ((vaddr_t)t != t) {
    161 		panic("%s: needs tweak", __func__);
    162 	}
    163 	return t;
    164 }
    165 
    166 void
    167 assert_sleepable(void)
    168 {
    169 
    170 	/* always sleepable, although we should improve this */
    171 }
    172 
    173 void
    174 module_init_md(void)
    175 {
    176 
    177 	/*
    178 	 * Nothing for now.  However, we should load the librump
    179 	 * symbol table.
    180 	 */
    181 }
    182 
    183 /* us and them, after all we're only ordinary seconds */
    184 static void
    185 rump_delay(unsigned int us)
    186 {
    187 	uint64_t sec, nsec;
    188 	int error;
    189 
    190 	sec = us / 1000000;
    191 	nsec = (us % 1000000) * 1000;
    192 
    193 	if (__predict_false(sec != 0))
    194 		printf("WARNING: over 1s delay\n");
    195 
    196 	rumpuser_nanosleep(&sec, &nsec, &error);
    197 }
    198 void (*delay_func)(unsigned int) = rump_delay;
    199 
    200 int
    201 ttycheckoutq(struct tty *tp, int wait)
    202 {
    203 
    204 	return 1;
    205 }
    206 
    207 void
    208 cnputc(int c)
    209 {
    210 	int error;
    211 
    212 	rumpuser_putchar(c, &error);
    213 }
    214 
    215 void
    216 cnflush(void)
    217 {
    218 
    219 	/* done */
    220 }
    221 
    222 int
    223 tputchar(int c, int flags, struct tty *tp)
    224 {
    225 
    226 	cnputc(c);
    227 	return 0;
    228 }
    229 
    230 void
    231 cpu_reboot(int howto, char *bootstr)
    232 {
    233 
    234 	rump_reboot(howto);
    235 
    236 	/* this function is __dead, we must exit */
    237 	rumpuser_exit(0);
    238 }
    239 
    240 #ifdef __HAVE_SYSCALL_INTERN
    241 void
    242 syscall_intern(struct proc *p)
    243 {
    244 
    245 	/* no you don't */
    246 }
    247 #endif
    248