emul.c revision 1.169 1 1.169 pooka /* $NetBSD: emul.c,v 1.169 2015/01/03 17:23:51 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.150 pooka * Copyright (c) 2007-2011 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.61 pooka #include <sys/cdefs.h>
29 1.169 pooka __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.169 2015/01/03 17:23:51 pooka Exp $");
30 1.9 pooka
31 1.1 pooka #include <sys/param.h>
32 1.1 pooka #include <sys/null.h>
33 1.1 pooka #include <sys/vnode.h>
34 1.1 pooka #include <sys/stat.h>
35 1.35 martin #include <sys/select.h>
36 1.1 pooka #include <sys/syslog.h>
37 1.1 pooka #include <sys/namei.h>
38 1.1 pooka #include <sys/kauth.h>
39 1.1 pooka #include <sys/conf.h>
40 1.1 pooka #include <sys/device.h>
41 1.1 pooka #include <sys/queue.h>
42 1.20 pooka #include <sys/file.h>
43 1.162 pooka #include <sys/filedesc.h>
44 1.15 ad #include <sys/cpu.h>
45 1.17 pooka #include <sys/kmem.h>
46 1.20 pooka #include <sys/poll.h>
47 1.66 pooka #include <sys/timetc.h>
48 1.53 pooka #include <sys/tprintf.h>
49 1.66 pooka #include <sys/module.h>
50 1.67 pooka #include <sys/tty.h>
51 1.67 pooka #include <sys/reboot.h>
52 1.154 pooka #include <sys/syscall.h>
53 1.109 pooka #include <sys/syscallvar.h>
54 1.109 pooka #include <sys/xcall.h>
55 1.148 pooka #include <sys/sleepq.h>
56 1.161 pooka #include <sys/cprng.h>
57 1.67 pooka
58 1.67 pooka #include <dev/cons.h>
59 1.1 pooka
60 1.44 pooka #include <rump/rumpuser.h>
61 1.44 pooka
62 1.8 pooka #include <uvm/uvm_map.h>
63 1.8 pooka
64 1.10 pooka #include "rump_private.h"
65 1.1 pooka
66 1.165 pooka void (*rump_vfs_fini)(void) = (void *)nullop;
67 1.165 pooka
68 1.149 pooka /*
69 1.149 pooka * physmem is largely unused (except for nmbcluster calculations),
70 1.149 pooka * so pick a default value which suits ZFS. if an application wants
71 1.149 pooka * a very small memory footprint, it can still adjust this before
72 1.149 pooka * calling rump_init()
73 1.149 pooka */
74 1.149 pooka #define PHYSMEM 512*256
75 1.149 pooka int physmem = PHYSMEM;
76 1.149 pooka int nkmempages = PHYSMEM/2; /* from le chapeau */
77 1.149 pooka #undef PHYSMEM
78 1.149 pooka
79 1.162 pooka struct lwp lwp0 = {
80 1.162 pooka .l_lid = 1,
81 1.162 pooka .l_proc = &proc0,
82 1.162 pooka .l_fd = &filedesc0,
83 1.162 pooka };
84 1.1 pooka struct vnode *rootvp;
85 1.124 pooka dev_t rootdev = NODEV;
86 1.149 pooka
87 1.19 pooka const int schedppq = 1;
88 1.28 ad int hardclock_ticks;
89 1.53 pooka bool mp_online = false;
90 1.56 pooka struct timeval boottime;
91 1.66 pooka int cold = 1;
92 1.95 pooka int boothowto = AB_SILENT;
93 1.67 pooka struct tty *constty;
94 1.1 pooka
95 1.135 pooka const struct bdevsw *bdevsw0[255];
96 1.64 pooka const struct bdevsw **bdevsw = bdevsw0;
97 1.135 pooka const int sys_cdevsws = 255;
98 1.135 pooka int max_cdevsws = 255;
99 1.64 pooka
100 1.135 pooka const struct cdevsw *cdevsw0[255];
101 1.64 pooka const struct cdevsw **cdevsw = cdevsw0;
102 1.135 pooka const int sys_bdevsws = 255;
103 1.135 pooka int max_bdevsws = 255;
104 1.64 pooka
105 1.76 pooka int mem_no = 2;
106 1.76 pooka
107 1.151 chs device_t booted_device;
108 1.151 chs device_t booted_wedge;
109 1.92 pooka int booted_partition;
110 1.92 pooka
111 1.120 pooka /* XXX: unused */
112 1.76 pooka kmutex_t tty_lock;
113 1.120 pooka krwlock_t exec_lock;
114 1.64 pooka
115 1.125 pooka struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
116 1.125 pooka
117 1.139 pooka /* sparc doesn't sport constant page size, pretend we have 4k pages */
118 1.109 pooka #ifdef __sparc__
119 1.109 pooka int nbpg = 4096;
120 1.139 pooka int pgofset = 4096-1;
121 1.139 pooka int pgshift = 12;
122 1.109 pooka #endif
123 1.109 pooka
124 1.146 tsutsui /* on sun3 VM_MAX_ADDRESS is a const variable */
125 1.146 tsutsui /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
126 1.141 pooka #ifdef sun3
127 1.146 tsutsui const vaddr_t kernbase = KERNBASE3;
128 1.140 pooka #endif
129 1.140 pooka
130 1.117 pooka struct loadavg averunnable = {
131 1.117 pooka { 0 * FSCALE,
132 1.117 pooka 1 * FSCALE,
133 1.117 pooka 11 * FSCALE, },
134 1.117 pooka FSCALE,
135 1.117 pooka };
136 1.117 pooka
137 1.126 pooka struct emul emul_netbsd = {
138 1.126 pooka .e_name = "netbsd-rump",
139 1.126 pooka .e_sysent = rump_sysent,
140 1.154 pooka #ifndef __HAVE_MINIMAL_EMUL
141 1.154 pooka .e_nsysent = SYS_NSYSENT,
142 1.154 pooka #endif
143 1.128 pooka .e_vm_default_addr = uvm_default_mapaddr,
144 1.138 pooka #ifdef __HAVE_SYSCALL_INTERN
145 1.138 pooka .e_syscall_intern = syscall_intern,
146 1.138 pooka #endif
147 1.126 pooka };
148 1.126 pooka
149 1.138 pooka u_int nprocs = 1;
150 1.20 pooka
151 1.161 pooka cprng_strong_t *kern_cprng;
152 1.161 pooka
153 1.164 pooka /* not used, but need the symbols for pointer comparisons */
154 1.164 pooka syncobj_t mutex_syncobj, rw_syncobj;
155 1.164 pooka
156 1.20 pooka int
157 1.20 pooka kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
158 1.20 pooka {
159 1.20 pooka extern int hz;
160 1.166 justin int rv __diagused;
161 1.78 pooka uint64_t sec, nsec;
162 1.127 pooka
163 1.20 pooka if (mtx)
164 1.20 pooka mutex_exit(mtx);
165 1.40 pooka
166 1.78 pooka sec = timeo / hz;
167 1.78 pooka nsec = (timeo % hz) * (1000000000 / hz);
168 1.158 pooka rv = rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
169 1.156 pooka KASSERT(rv == 0);
170 1.156 pooka
171 1.20 pooka if (mtx)
172 1.20 pooka mutex_enter(mtx);
173 1.20 pooka
174 1.20 pooka return 0;
175 1.20 pooka }
176 1.27 pooka
177 1.27 pooka void
178 1.32 bjs lwp_unsleep(lwp_t *l, bool cleanup)
179 1.32 bjs {
180 1.32 bjs
181 1.32 bjs KASSERT(mutex_owned(l->l_mutex));
182 1.32 bjs
183 1.104 rmind (*l->l_syncobj->sobj_unsleep)(l, cleanup);
184 1.32 bjs }
185 1.34 yamt
186 1.145 pooka void
187 1.145 pooka lwp_update_creds(struct lwp *l)
188 1.145 pooka {
189 1.145 pooka struct proc *p;
190 1.145 pooka kauth_cred_t oldcred;
191 1.145 pooka
192 1.145 pooka p = l->l_proc;
193 1.145 pooka oldcred = l->l_cred;
194 1.145 pooka l->l_prflag &= ~LPR_CRMOD;
195 1.145 pooka
196 1.145 pooka mutex_enter(p->p_lock);
197 1.145 pooka kauth_cred_hold(p->p_cred);
198 1.145 pooka l->l_cred = p->p_cred;
199 1.145 pooka mutex_exit(p->p_lock);
200 1.145 pooka
201 1.145 pooka if (oldcred != NULL)
202 1.145 pooka kauth_cred_free(oldcred);
203 1.145 pooka }
204 1.145 pooka
205 1.34 yamt vaddr_t
206 1.155 para calc_cache_size(vsize_t vasz, int pct, int va_pct)
207 1.34 yamt {
208 1.34 yamt paddr_t t;
209 1.34 yamt
210 1.34 yamt t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
211 1.34 yamt if ((vaddr_t)t != t) {
212 1.34 yamt panic("%s: needs tweak", __func__);
213 1.34 yamt }
214 1.34 yamt return t;
215 1.34 yamt }
216 1.35 martin
217 1.41 pooka void
218 1.41 pooka assert_sleepable(void)
219 1.41 pooka {
220 1.41 pooka
221 1.41 pooka /* always sleepable, although we should improve this */
222 1.41 pooka }
223 1.43 pooka
224 1.66 pooka void
225 1.80 cegger module_init_md(void)
226 1.66 pooka {
227 1.66 pooka
228 1.66 pooka /*
229 1.66 pooka * Nothing for now. However, we should load the librump
230 1.66 pooka * symbol table.
231 1.66 pooka */
232 1.66 pooka }
233 1.67 pooka
234 1.167 pooka /*
235 1.167 pooka * Try to emulate all the MD definitions of DELAY() / delay().
236 1.167 pooka * Would be nice to fix the #defines in MD headers, but this quicker.
237 1.168 pooka *
238 1.168 pooka * XXX: we'd need a rumpuser_clock_sleep_nowrap() here. Since we
239 1.168 pooka * don't have it in the current hypercall revision, busyloop.
240 1.168 pooka * Note that rather than calibrate a loop delay and work with that,
241 1.168 pooka * get call gettime (which does not block) in a loop to make sure
242 1.168 pooka * we didn't get virtual ghosttime. That might be slightly inaccurate
243 1.168 pooka * for very small delays ...
244 1.168 pooka *
245 1.168 pooka * The other option would be to run a thread in the hypervisor which
246 1.168 pooka * sleeps for us and we can wait for it using rumpuser_cv_wait_nowrap()
247 1.168 pooka * Probably too fussy. Better just wait for hypercall rev 18 ;)
248 1.167 pooka */
249 1.69 pooka static void
250 1.69 pooka rump_delay(unsigned int us)
251 1.67 pooka {
252 1.168 pooka struct timespec target, tmp;
253 1.168 pooka uint64_t sec, sec_ini, sec_now;
254 1.168 pooka long nsec, nsec_ini, nsec_now;
255 1.168 pooka int loops;
256 1.168 pooka
257 1.168 pooka rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec_ini, &nsec_ini);
258 1.67 pooka
259 1.167 pooka #ifdef __mac68k__
260 1.167 pooka sec = us / 1000;
261 1.167 pooka nsec = (us % 1000) * 1000000;
262 1.167 pooka #else
263 1.78 pooka sec = us / 1000000;
264 1.78 pooka nsec = (us % 1000000) * 1000;
265 1.167 pooka #endif
266 1.67 pooka
267 1.168 pooka target.tv_sec = sec_ini;
268 1.168 pooka tmp.tv_sec = sec;
269 1.168 pooka target.tv_nsec = nsec_ini;
270 1.168 pooka tmp.tv_nsec = nsec;
271 1.168 pooka timespecadd(&target, &tmp, &target);
272 1.168 pooka
273 1.78 pooka if (__predict_false(sec != 0))
274 1.67 pooka printf("WARNING: over 1s delay\n");
275 1.67 pooka
276 1.168 pooka for (loops = 0; loops < 1000*1000*100; loops++) {
277 1.168 pooka struct timespec cur;
278 1.168 pooka
279 1.168 pooka rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO,
280 1.168 pooka &sec_now, &nsec_now);
281 1.168 pooka cur.tv_sec = sec_now;
282 1.168 pooka cur.tv_nsec = nsec_now;
283 1.168 pooka if (timespeccmp(&cur, &target, >=)) {
284 1.168 pooka return;
285 1.168 pooka }
286 1.168 pooka }
287 1.168 pooka printf("WARNING: DELAY ESCAPED\n");
288 1.67 pooka }
289 1.69 pooka void (*delay_func)(unsigned int) = rump_delay;
290 1.167 pooka __strong_alias(delay,rump_delay);
291 1.167 pooka __strong_alias(_delay,rump_delay);
292 1.67 pooka
293 1.142 pooka /*
294 1.142 pooka * Provide weak aliases for tty routines used by printf.
295 1.142 pooka * They will be used unless the rumpkern_tty component is present.
296 1.142 pooka */
297 1.142 pooka
298 1.142 pooka int rump_ttycheckoutq(struct tty *, int);
299 1.105 pooka int
300 1.142 pooka rump_ttycheckoutq(struct tty *tp, int wait)
301 1.67 pooka {
302 1.67 pooka
303 1.67 pooka return 1;
304 1.67 pooka }
305 1.142 pooka __weak_alias(ttycheckoutq,rump_ttycheckoutq);
306 1.67 pooka
307 1.142 pooka int rump_tputchar(int, int, struct tty *);
308 1.142 pooka int
309 1.142 pooka rump_tputchar(int c, int flags, struct tty *tp)
310 1.142 pooka {
311 1.142 pooka
312 1.142 pooka cnputc(c);
313 1.142 pooka return 0;
314 1.142 pooka }
315 1.142 pooka __weak_alias(tputchar,rump_tputchar);
316 1.142 pooka
317 1.67 pooka void
318 1.67 pooka cnputc(int c)
319 1.67 pooka {
320 1.67 pooka
321 1.157 pooka rumpuser_putchar(c);
322 1.67 pooka }
323 1.67 pooka
324 1.67 pooka void
325 1.80 cegger cnflush(void)
326 1.67 pooka {
327 1.67 pooka
328 1.67 pooka /* done */
329 1.67 pooka }
330 1.67 pooka
331 1.163 pooka void
332 1.163 pooka resettodr(void)
333 1.163 pooka {
334 1.163 pooka
335 1.163 pooka /* setting clocks is not in the jurisdiction of rump kernels */
336 1.163 pooka }
337 1.163 pooka
338 1.138 pooka #ifdef __HAVE_SYSCALL_INTERN
339 1.138 pooka void
340 1.138 pooka syscall_intern(struct proc *p)
341 1.138 pooka {
342 1.138 pooka
343 1.160 pooka p->p_emuldata = NULL;
344 1.138 pooka }
345 1.138 pooka #endif
346 1.144 pooka
347 1.148 pooka #ifdef LOCKDEBUG
348 1.148 pooka void
349 1.148 pooka turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
350 1.148 pooka {
351 1.148 pooka
352 1.148 pooka /* nada */
353 1.148 pooka }
354 1.148 pooka #endif
355 1.165 pooka
356 1.165 pooka void
357 1.165 pooka cpu_reboot(int howto, char *bootstr)
358 1.165 pooka {
359 1.165 pooka int ruhow = 0;
360 1.165 pooka void *finiarg;
361 1.165 pooka
362 1.165 pooka printf("rump kernel halting...\n");
363 1.165 pooka
364 1.165 pooka if (!RUMP_LOCALPROC_P(curproc))
365 1.165 pooka finiarg = curproc->p_vmspace->vm_map.pmap;
366 1.165 pooka else
367 1.165 pooka finiarg = NULL;
368 1.165 pooka
369 1.165 pooka /* dump means we really take the dive here */
370 1.165 pooka if ((howto & RB_DUMP) || panicstr) {
371 1.165 pooka ruhow = RUMPUSER_PANIC;
372 1.165 pooka goto out;
373 1.165 pooka }
374 1.165 pooka
375 1.165 pooka /* try to sync */
376 1.165 pooka if (!((howto & RB_NOSYNC) || panicstr)) {
377 1.165 pooka rump_vfs_fini();
378 1.165 pooka }
379 1.165 pooka
380 1.165 pooka doshutdownhooks();
381 1.165 pooka
382 1.165 pooka /* your wish is my command */
383 1.165 pooka if (howto & RB_HALT) {
384 1.165 pooka printf("rump kernel halted\n");
385 1.169 pooka rump_sysproxy_fini(finiarg);
386 1.165 pooka for (;;) {
387 1.165 pooka rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
388 1.165 pooka }
389 1.165 pooka }
390 1.165 pooka
391 1.165 pooka /* this function is __dead, we must exit */
392 1.165 pooka out:
393 1.165 pooka printf("halted\n");
394 1.169 pooka rump_sysproxy_fini(finiarg);
395 1.165 pooka rumpuser_exit(ruhow);
396 1.165 pooka }
397