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