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