Home | History | Annotate | Line # | Download | only in rumpkern
emul.c revision 1.182
      1 /*	$NetBSD: emul.c,v 1.182 2017/06/04 08:05:42 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2007-2011 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.182 2017/06/04 08:05:42 hannken Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/cprng.h>
     33 #include <sys/filedesc.h>
     34 #include <sys/fstrans.h>
     35 #include <sys/kauth.h>
     36 #include <sys/module.h>
     37 #include <sys/reboot.h>
     38 #include <sys/syscall.h>
     39 #ifdef LOCKDEBUG
     40 #include <sys/sleepq.h>
     41 #endif
     42 
     43 #include <dev/cons.h>
     44 
     45 #include <rump-sys/kern.h>
     46 
     47 #include <rump/rumpuser.h>
     48 
     49 void (*rump_vfs_fini)(void) = (void *)nullop;
     50 
     51 /*
     52  * physmem is largely unused (except for nmbcluster calculations),
     53  * so pick a default value which suits ZFS.  if an application wants
     54  * a very small memory footprint, it can still adjust this before
     55  * calling rump_init()
     56  */
     57 #define PHYSMEM 512*256
     58 psize_t physmem = PHYSMEM;
     59 int nkmempages = PHYSMEM/2; /* from le chapeau */
     60 #undef PHYSMEM
     61 
     62 struct vnode *rootvp;
     63 dev_t rootdev = NODEV;
     64 
     65 const int schedppq = 1;
     66 bool mp_online = false;
     67 struct timespec boottime;
     68 int cold = 1;
     69 int boothowto = AB_SILENT;
     70 struct tty *constty;
     71 
     72 const struct bdevsw *bdevsw0[255];
     73 const struct bdevsw **bdevsw = bdevsw0;
     74 const int sys_cdevsws = 255;
     75 int max_cdevsws = 255;
     76 
     77 const struct cdevsw *cdevsw0[255];
     78 const struct cdevsw **cdevsw = cdevsw0;
     79 const int sys_bdevsws = 255;
     80 int max_bdevsws = 255;
     81 
     82 int mem_no = 2;
     83 
     84 device_t booted_device;
     85 device_t booted_wedge;
     86 int booted_partition;
     87 
     88 /* XXX: unused */
     89 kmutex_t tty_lock;
     90 krwlock_t exec_lock;
     91 
     92 /* sparc doesn't sport constant page size, pretend we have 4k pages */
     93 #ifdef __sparc__
     94 int nbpg = 4096;
     95 int pgofset = 4096-1;
     96 int pgshift = 12;
     97 #endif
     98 
     99 /* on sun3 VM_MAX_ADDRESS is a const variable */
    100 /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
    101 #ifdef sun3
    102 const vaddr_t kernbase = KERNBASE3;
    103 #endif
    104 
    105 struct loadavg averunnable = {
    106 	{ 0 * FSCALE,
    107 	  1 * FSCALE,
    108 	  11 * FSCALE, },
    109 	FSCALE,
    110 };
    111 
    112 /*
    113  * Include the autogenerated list of auto-loadable syscalls
    114  */
    115 #include <kern/syscalls_autoload.c>
    116 
    117 struct emul emul_netbsd = {
    118 	.e_name = "netbsd-rump",
    119 	.e_sysent = rump_sysent,
    120 #ifndef __HAVE_MINIMAL_EMUL
    121 	.e_nsysent = SYS_NSYSENT,
    122 #endif
    123 	.e_vm_default_addr = uvm_default_mapaddr,
    124 #ifdef __HAVE_SYSCALL_INTERN
    125 	.e_syscall_intern = syscall_intern,
    126 #endif
    127 	.e_sc_autoload = netbsd_syscalls_autoload,
    128 };
    129 
    130 cprng_strong_t *kern_cprng;
    131 
    132 /* not used, but need the symbols for pointer comparisons */
    133 syncobj_t mutex_syncobj, rw_syncobj;
    134 
    135 int
    136 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
    137 {
    138 	extern int hz;
    139 	int rv __diagused;
    140 	uint64_t sec, nsec;
    141 
    142 	if (mtx)
    143 		mutex_exit(mtx);
    144 
    145 	sec = timeo / hz;
    146 	nsec = (timeo % hz) * (1000000000 / hz);
    147 	rv = rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
    148 	KASSERT(rv == 0);
    149 
    150 	if (mtx)
    151 		mutex_enter(mtx);
    152 
    153 	return 0;
    154 }
    155 
    156 vaddr_t
    157 calc_cache_size(vsize_t vasz, int pct, int va_pct)
    158 {
    159 	paddr_t t;
    160 
    161 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
    162 	if ((vaddr_t)t != t) {
    163 		panic("%s: needs tweak", __func__);
    164 	}
    165 	return t;
    166 }
    167 
    168 void
    169 assert_sleepable(void)
    170 {
    171 
    172 	/* always sleepable, although we should improve this */
    173 }
    174 
    175 void
    176 module_init_md(void)
    177 {
    178 
    179 	/*
    180 	 * Nothing for now.  However, we should load the librump
    181 	 * symbol table.
    182 	 */
    183 }
    184 
    185 /*
    186  * Try to emulate all the MD definitions of DELAY() / delay().
    187  * Would be nice to fix the #defines in MD headers, but this quicker.
    188  *
    189  * XXX: we'd need a rumpuser_clock_sleep_nowrap() here.  Since we
    190  * don't have it in the current hypercall revision, busyloop.
    191  * Note that rather than calibrate a loop delay and work with that,
    192  * get call gettime (which does not block) in a loop to make sure
    193  * we didn't get virtual ghosttime.  That might be slightly inaccurate
    194  * for very small delays ...
    195  *
    196  * The other option would be to run a thread in the hypervisor which
    197  * sleeps for us and we can wait for it using rumpuser_cv_wait_nowrap()
    198  * Probably too fussy.  Better just wait for hypercall rev 18 ;)
    199  */
    200 static void
    201 rump_delay(unsigned int us)
    202 {
    203 	struct timespec target, tmp;
    204 	uint64_t sec, sec_ini, sec_now;
    205 	long nsec, nsec_ini, nsec_now;
    206 	int loops;
    207 
    208 	rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec_ini, &nsec_ini);
    209 
    210 #ifdef __mac68k__
    211 	sec = us / 1000;
    212 	nsec = (us % 1000) * 1000000;
    213 #else
    214 	sec = us / 1000000;
    215 	nsec = (us % 1000000) * 1000;
    216 #endif
    217 
    218 	target.tv_sec = sec_ini;
    219 	tmp.tv_sec = sec;
    220 	target.tv_nsec = nsec_ini;
    221 	tmp.tv_nsec = nsec;
    222 	timespecadd(&target, &tmp, &target);
    223 
    224 	if (__predict_false(sec != 0))
    225 		printf("WARNING: over 1s delay\n");
    226 
    227 	for (loops = 0; loops < 1000*1000*100; loops++) {
    228 		struct timespec cur;
    229 
    230 		rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO,
    231 		    &sec_now, &nsec_now);
    232 		cur.tv_sec = sec_now;
    233 		cur.tv_nsec = nsec_now;
    234 		if (timespeccmp(&cur, &target, >=)) {
    235 			return;
    236 		}
    237 	}
    238 	printf("WARNING: DELAY ESCAPED\n");
    239 }
    240 void (*delay_func)(unsigned int) = rump_delay;
    241 __strong_alias(delay,rump_delay);
    242 __strong_alias(_delay,rump_delay);
    243 
    244 /* Weak aliases for fstrans to be used unless librumpvfs is present. */
    245 
    246 void rump_fstrans_start(struct mount *);
    247 void
    248 rump_fstrans_start(struct mount *mp)
    249 {
    250 
    251 }
    252 __weak_alias(fstrans_start,rump_fstrans_start);
    253 
    254 int rump_fstrans_start_nowait(struct mount *);
    255 int
    256 rump_fstrans_start_nowait(struct mount *mp)
    257 {
    258 
    259 	return 0;
    260 }
    261 __weak_alias(fstrans_start_nowait,rump_fstrans_start_nowait);
    262 
    263 void rump_fstrans_done(struct mount *);
    264 void
    265 rump_fstrans_done(struct mount *mp)
    266 {
    267 
    268 }
    269 __weak_alias(fstrans_done,rump_fstrans_done);
    270 
    271 /*
    272  * Provide weak aliases for tty routines used by printf.
    273  * They will be used unless the rumpkern_tty component is present.
    274  */
    275 
    276 int rump_ttycheckoutq(struct tty *, int);
    277 int
    278 rump_ttycheckoutq(struct tty *tp, int wait)
    279 {
    280 
    281 	return 1;
    282 }
    283 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
    284 
    285 int rump_tputchar(int, int, struct tty *);
    286 int
    287 rump_tputchar(int c, int flags, struct tty *tp)
    288 {
    289 
    290 	cnputc(c);
    291 	return 0;
    292 }
    293 __weak_alias(tputchar,rump_tputchar);
    294 
    295 void
    296 cnputc(int c)
    297 {
    298 
    299 	rumpuser_putchar(c);
    300 }
    301 
    302 void
    303 cnflush(void)
    304 {
    305 
    306 	/* done */
    307 }
    308 
    309 void
    310 resettodr(void)
    311 {
    312 
    313 	/* setting clocks is not in the jurisdiction of rump kernels */
    314 }
    315 
    316 #ifdef __HAVE_SYSCALL_INTERN
    317 void
    318 syscall_intern(struct proc *p)
    319 {
    320 
    321 	p->p_emuldata = NULL;
    322 }
    323 #endif
    324 
    325 #ifdef LOCKDEBUG
    326 void
    327 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
    328 {
    329 
    330 	/* nada */
    331 }
    332 #endif
    333 
    334 void
    335 cpu_reboot(int howto, char *bootstr)
    336 {
    337 	int ruhow = 0;
    338 	void *finiarg;
    339 
    340 	printf("rump kernel halting...\n");
    341 
    342 	if (!RUMP_LOCALPROC_P(curproc))
    343 		finiarg = RUMP_SPVM2CTL(curproc->p_vmspace);
    344 	else
    345 		finiarg = NULL;
    346 
    347 	/* dump means we really take the dive here */
    348 	if ((howto & RB_DUMP) || panicstr) {
    349 		ruhow = RUMPUSER_PANIC;
    350 		goto out;
    351 	}
    352 
    353 	/* try to sync */
    354 	if (!((howto & RB_NOSYNC) || panicstr)) {
    355 		rump_vfs_fini();
    356 	}
    357 
    358 	doshutdownhooks();
    359 
    360 	/* your wish is my command */
    361 	if (howto & RB_HALT) {
    362 		printf("rump kernel halted (with RB_HALT, not exiting)\n");
    363 		rump_sysproxy_fini(finiarg);
    364 		for (;;) {
    365 			rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
    366 		}
    367 	}
    368 
    369 	/* this function is __dead, we must exit */
    370  out:
    371 	rump_sysproxy_fini(finiarg);
    372 	rumpuser_exit(ruhow);
    373 }
    374 
    375 const char *
    376 cpu_getmodel(void)
    377 {
    378 
    379 	return "rumpcore (virtual)";
    380 }
    381