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