emul.c revision 1.167 1 /* $NetBSD: emul.c,v 1.167 2014/11/18 13:05:33 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.167 2014/11/18 13:05:33 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 static void
239 rump_delay(unsigned int us)
240 {
241 uint64_t sec, nsec;
242
243 #ifdef __mac68k__
244 sec = us / 1000;
245 nsec = (us % 1000) * 1000000;
246 #else
247 sec = us / 1000000;
248 nsec = (us % 1000000) * 1000;
249 #endif
250
251 if (__predict_false(sec != 0))
252 printf("WARNING: over 1s delay\n");
253
254 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
255 }
256 void (*delay_func)(unsigned int) = rump_delay;
257 __strong_alias(delay,rump_delay);
258 __strong_alias(_delay,rump_delay);
259
260 /*
261 * Provide weak aliases for tty routines used by printf.
262 * They will be used unless the rumpkern_tty component is present.
263 */
264
265 int rump_ttycheckoutq(struct tty *, int);
266 int
267 rump_ttycheckoutq(struct tty *tp, int wait)
268 {
269
270 return 1;
271 }
272 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
273
274 int rump_tputchar(int, int, struct tty *);
275 int
276 rump_tputchar(int c, int flags, struct tty *tp)
277 {
278
279 cnputc(c);
280 return 0;
281 }
282 __weak_alias(tputchar,rump_tputchar);
283
284 void
285 cnputc(int c)
286 {
287
288 rumpuser_putchar(c);
289 }
290
291 void
292 cnflush(void)
293 {
294
295 /* done */
296 }
297
298 void
299 resettodr(void)
300 {
301
302 /* setting clocks is not in the jurisdiction of rump kernels */
303 }
304
305 #ifdef __HAVE_SYSCALL_INTERN
306 void
307 syscall_intern(struct proc *p)
308 {
309
310 p->p_emuldata = NULL;
311 }
312 #endif
313
314 #ifdef LOCKDEBUG
315 void
316 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
317 {
318
319 /* nada */
320 }
321 #endif
322
323 void
324 cpu_reboot(int howto, char *bootstr)
325 {
326 int ruhow = 0;
327 void *finiarg;
328
329 printf("rump kernel halting...\n");
330
331 if (!RUMP_LOCALPROC_P(curproc))
332 finiarg = curproc->p_vmspace->vm_map.pmap;
333 else
334 finiarg = NULL;
335
336 /* dump means we really take the dive here */
337 if ((howto & RB_DUMP) || panicstr) {
338 ruhow = RUMPUSER_PANIC;
339 goto out;
340 }
341
342 /* try to sync */
343 if (!((howto & RB_NOSYNC) || panicstr)) {
344 rump_vfs_fini();
345 }
346
347 doshutdownhooks();
348
349 /* your wish is my command */
350 if (howto & RB_HALT) {
351 printf("rump kernel halted\n");
352 rumpuser_sp_fini(finiarg);
353 for (;;) {
354 rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, 10, 0);
355 }
356 }
357
358 /* this function is __dead, we must exit */
359 out:
360 printf("halted\n");
361 rumpuser_sp_fini(finiarg);
362 rumpuser_exit(ruhow);
363 }
364