1 /* $NetBSD: emul.c,v 1.183 2017/11/09 12:46:55 christos 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.183 2017/11/09 12:46:55 christos 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 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 #ifndef __HAVE_MINIMAL_EMUL 122 .e_nsysent = SYS_NSYSENT, 123 #endif 124 .e_vm_default_addr = uvm_default_mapaddr, 125 #ifdef __HAVE_SYSCALL_INTERN 126 .e_syscall_intern = syscall_intern, 127 #endif 128 .e_sc_autoload = netbsd_syscalls_autoload, 129 }; 130 131 cprng_strong_t *kern_cprng; 132 133 /* not used, but need the symbols for pointer comparisons */ 134 syncobj_t mutex_syncobj, rw_syncobj; 135 136 int 137 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx) 138 { 139 extern int hz; 140 int rv __diagused; 141 uint64_t sec, nsec; 142 143 if (mtx) 144 mutex_exit(mtx); 145 146 sec = timeo / hz; 147 nsec = (timeo % hz) * (1000000000 / hz); 148 rv = rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec); 149 KASSERT(rv == 0); 150 151 if (mtx) 152 mutex_enter(mtx); 153 154 return 0; 155 } 156 157 vaddr_t 158 calc_cache_size(vsize_t vasz, int pct, int va_pct) 159 { 160 paddr_t t; 161 162 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE; 163 if ((vaddr_t)t != t) { 164 panic("%s: needs tweak", __func__); 165 } 166 return t; 167 } 168 169 void 170 assert_sleepable(void) 171 { 172 173 /* always sleepable, although we should improve this */ 174 } 175 176 void 177 module_init_md(void) 178 { 179 180 /* 181 * Nothing for now. However, we should load the librump 182 * symbol table. 183 */ 184 } 185 186 /* 187 * Try to emulate all the MD definitions of DELAY() / delay(). 188 * Would be nice to fix the #defines in MD headers, but this quicker. 189 * 190 * XXX: we'd need a rumpuser_clock_sleep_nowrap() here. Since we 191 * don't have it in the current hypercall revision, busyloop. 192 * Note that rather than calibrate a loop delay and work with that, 193 * get call gettime (which does not block) in a loop to make sure 194 * we didn't get virtual ghosttime. That might be slightly inaccurate 195 * for very small delays ... 196 * 197 * The other option would be to run a thread in the hypervisor which 198 * sleeps for us and we can wait for it using rumpuser_cv_wait_nowrap() 199 * Probably too fussy. Better just wait for hypercall rev 18 ;) 200 */ 201 static void 202 rump_delay(unsigned int us) 203 { 204 struct timespec target, tmp; 205 uint64_t sec, sec_ini, sec_now; 206 long nsec, nsec_ini, nsec_now; 207 int loops; 208 209 rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec_ini, &nsec_ini); 210 211 #ifdef __mac68k__ 212 sec = us / 1000; 213 nsec = (us % 1000) * 1000000; 214 #else 215 sec = us / 1000000; 216 nsec = (us % 1000000) * 1000; 217 #endif 218 219 target.tv_sec = sec_ini; 220 tmp.tv_sec = sec; 221 target.tv_nsec = nsec_ini; 222 tmp.tv_nsec = nsec; 223 timespecadd(&target, &tmp, &target); 224 225 if (__predict_false(sec != 0)) 226 printf("WARNING: over 1s delay\n"); 227 228 for (loops = 0; loops < 1000*1000*100; loops++) { 229 struct timespec cur; 230 231 rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, 232 &sec_now, &nsec_now); 233 cur.tv_sec = sec_now; 234 cur.tv_nsec = nsec_now; 235 if (timespeccmp(&cur, &target, >=)) { 236 return; 237 } 238 } 239 printf("WARNING: DELAY ESCAPED\n"); 240 } 241 void (*delay_func)(unsigned int) = rump_delay; 242 __strong_alias(delay,rump_delay); 243 __strong_alias(_delay,rump_delay); 244 245 /* Weak aliases for fstrans to be used unless librumpvfs is present. */ 246 247 void rump_fstrans_start(struct mount *); 248 void 249 rump_fstrans_start(struct mount *mp) 250 { 251 252 } 253 __weak_alias(fstrans_start,rump_fstrans_start); 254 255 int rump_fstrans_start_nowait(struct mount *); 256 int 257 rump_fstrans_start_nowait(struct mount *mp) 258 { 259 260 return 0; 261 } 262 __weak_alias(fstrans_start_nowait,rump_fstrans_start_nowait); 263 264 void rump_fstrans_done(struct mount *); 265 void 266 rump_fstrans_done(struct mount *mp) 267 { 268 269 } 270 __weak_alias(fstrans_done,rump_fstrans_done); 271 272 /* 273 * Provide weak aliases for tty routines used by printf. 274 * They will be used unless the rumpkern_tty component is present. 275 */ 276 277 int rump_ttycheckoutq(struct tty *, int); 278 int 279 rump_ttycheckoutq(struct tty *tp, int wait) 280 { 281 282 return 1; 283 } 284 __weak_alias(ttycheckoutq,rump_ttycheckoutq); 285 286 int rump_tputchar(int, int, struct tty *); 287 int 288 rump_tputchar(int c, int flags, struct tty *tp) 289 { 290 291 cnputc(c); 292 return 0; 293 } 294 __weak_alias(tputchar,rump_tputchar); 295 296 void 297 cnputc(int c) 298 { 299 300 rumpuser_putchar(c); 301 } 302 303 void 304 cnflush(void) 305 { 306 307 /* done */ 308 } 309 310 void 311 resettodr(void) 312 { 313 314 /* setting clocks is not in the jurisdiction of rump kernels */ 315 } 316 317 #ifdef __HAVE_SYSCALL_INTERN 318 void 319 syscall_intern(struct proc *p) 320 { 321 322 p->p_emuldata = NULL; 323 } 324 #endif 325 326 #ifdef LOCKDEBUG 327 void 328 turnstile_print(volatile void *obj, void (*pr)(const char *, ...)) 329 { 330 331 /* nada */ 332 } 333 #endif 334 335 void 336 cpu_reboot(int howto, char *bootstr) 337 { 338 int ruhow = 0; 339 void *finiarg; 340 341 printf("rump kernel halting...\n"); 342 343 if (!RUMP_LOCALPROC_P(curproc)) 344 finiarg = RUMP_SPVM2CTL(curproc->p_vmspace); 345 else 346 finiarg = NULL; 347 348 /* dump means we really take the dive here */ 349 if ((howto & RB_DUMP) || panicstr) { 350 ruhow = RUMPUSER_PANIC; 351 goto out; 352 } 353 354 /* try to sync */ 355 if (!((howto & RB_NOSYNC) || panicstr)) { 356 rump_vfs_fini(); 357 } 358 359 doshutdownhooks(); 360 361 /* your wish is my command */ 362 if (howto & RB_HALT) { 363 printf("rump kernel halted (with RB_HALT, not exiting)\n"); 364 rump_sysproxy_fini(finiarg); 365 for (;;) { 366 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0); 367 } 368 } 369 370 /* this function is __dead, we must exit */ 371 out: 372 rump_sysproxy_fini(finiarg); 373 rumpuser_exit(ruhow); 374 } 375 376 const char * 377 cpu_getmodel(void) 378 { 379 380 return "rumpcore (virtual)"; 381 } 382