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