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