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