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