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