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