xref: /netbsd-src/sys/rump/librump/rumpkern/emul.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: emul.c,v 1.149 2011/01/21 13:11:03 pooka Exp $	*/
2 
3 /*
4  * Copyright (c) 2007 Antti Kantee.  All Rights Reserved.
5  *
6  * Development of this software was supported by Google Summer of Code.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.149 2011/01/21 13:11:03 pooka Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/null.h>
35 #include <sys/vnode.h>
36 #include <sys/stat.h>
37 #include <sys/select.h>
38 #include <sys/syslog.h>
39 #include <sys/namei.h>
40 #include <sys/kauth.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/cpu.h>
46 #include <sys/kmem.h>
47 #include <sys/poll.h>
48 #include <sys/timetc.h>
49 #include <sys/tprintf.h>
50 #include <sys/module.h>
51 #include <sys/tty.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallvar.h>
54 #include <sys/xcall.h>
55 #include <sys/sleepq.h>
56 
57 #include <dev/cons.h>
58 
59 #include <rump/rumpuser.h>
60 
61 #include <uvm/uvm_map.h>
62 
63 #include "rump_private.h"
64 
65 /*
66  * physmem is largely unused (except for nmbcluster calculations),
67  * so pick a default value which suits ZFS.  if an application wants
68  * a very small memory footprint, it can still adjust this before
69  * calling rump_init()
70  */
71 #define PHYSMEM 512*256
72 int physmem = PHYSMEM;
73 int nkmempages = PHYSMEM/2; /* from le chapeau */
74 #undef PHYSMEM
75 
76 struct lwp lwp0;
77 struct vnode *rootvp;
78 dev_t rootdev = NODEV;
79 
80 const int schedppq = 1;
81 int hardclock_ticks;
82 bool mp_online = false;
83 struct timeval boottime;
84 int cold = 1;
85 int boothowto = AB_SILENT;
86 struct tty *constty;
87 
88 const struct bdevsw *bdevsw0[255];
89 const struct bdevsw **bdevsw = bdevsw0;
90 const int sys_cdevsws = 255;
91 int max_cdevsws = 255;
92 
93 const struct cdevsw *cdevsw0[255];
94 const struct cdevsw **cdevsw = cdevsw0;
95 const int sys_bdevsws = 255;
96 int max_bdevsws = 255;
97 
98 int mem_no = 2;
99 
100 struct device *booted_device;
101 struct device *booted_wedge;
102 int booted_partition;
103 
104 /* XXX: unused */
105 kmutex_t tty_lock;
106 krwlock_t exec_lock;
107 
108 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
109 
110 /* sparc doesn't sport constant page size, pretend we have 4k pages */
111 #ifdef __sparc__
112 int nbpg = 4096;
113 int pgofset = 4096-1;
114 int pgshift = 12;
115 #endif
116 
117 /* on sun3 VM_MAX_ADDRESS is a const variable */
118 /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
119 #ifdef sun3
120 const vaddr_t kernbase = KERNBASE3;
121 #endif
122 
123 struct loadavg averunnable = {
124 	{ 0 * FSCALE,
125 	  1 * FSCALE,
126 	  11 * FSCALE, },
127 	FSCALE,
128 };
129 
130 struct emul emul_netbsd = {
131 	.e_name = "netbsd-rump",
132 	.e_sysent = rump_sysent,
133 	.e_vm_default_addr = uvm_default_mapaddr,
134 #ifdef __HAVE_SYSCALL_INTERN
135 	.e_syscall_intern = syscall_intern,
136 #endif
137 };
138 
139 u_int nprocs = 1;
140 
141 int
142 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
143 {
144 	extern int hz;
145 	int rv, error;
146 	uint64_t sec, nsec;
147 
148 	if (mtx)
149 		mutex_exit(mtx);
150 
151 	sec = timeo / hz;
152 	nsec = (timeo % hz) * (1000000000 / hz);
153 	rv = rumpuser_nanosleep(&sec, &nsec, &error);
154 
155 	if (mtx)
156 		mutex_enter(mtx);
157 
158 	if (rv)
159 		return error;
160 
161 	return 0;
162 }
163 
164 void
165 lwp_unsleep(lwp_t *l, bool cleanup)
166 {
167 
168 	KASSERT(mutex_owned(l->l_mutex));
169 
170 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
171 }
172 
173 void
174 lwp_update_creds(struct lwp *l)
175 {
176 	struct proc *p;
177 	kauth_cred_t oldcred;
178 
179 	p = l->l_proc;
180 	oldcred = l->l_cred;
181 	l->l_prflag &= ~LPR_CRMOD;
182 
183 	mutex_enter(p->p_lock);
184 	kauth_cred_hold(p->p_cred);
185 	l->l_cred = p->p_cred;
186 	mutex_exit(p->p_lock);
187 
188 	if (oldcred != NULL)
189 		kauth_cred_free(oldcred);
190 }
191 
192 vaddr_t
193 calc_cache_size(struct vm_map *map, int pct, int va_pct)
194 {
195 	paddr_t t;
196 
197 	t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
198 	if ((vaddr_t)t != t) {
199 		panic("%s: needs tweak", __func__);
200 	}
201 	return t;
202 }
203 
204 void
205 assert_sleepable(void)
206 {
207 
208 	/* always sleepable, although we should improve this */
209 }
210 
211 void
212 module_init_md(void)
213 {
214 
215 	/*
216 	 * Nothing for now.  However, we should load the librump
217 	 * symbol table.
218 	 */
219 }
220 
221 /* us and them, after all we're only ordinary seconds */
222 static void
223 rump_delay(unsigned int us)
224 {
225 	uint64_t sec, nsec;
226 	int error;
227 
228 	sec = us / 1000000;
229 	nsec = (us % 1000000) * 1000;
230 
231 	if (__predict_false(sec != 0))
232 		printf("WARNING: over 1s delay\n");
233 
234 	rumpuser_nanosleep(&sec, &nsec, &error);
235 }
236 void (*delay_func)(unsigned int) = rump_delay;
237 
238 /*
239  * Provide weak aliases for tty routines used by printf.
240  * They will be used unless the rumpkern_tty component is present.
241  */
242 
243 int rump_ttycheckoutq(struct tty *, int);
244 int
245 rump_ttycheckoutq(struct tty *tp, int wait)
246 {
247 
248 	return 1;
249 }
250 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
251 
252 int rump_tputchar(int, int, struct tty *);
253 int
254 rump_tputchar(int c, int flags, struct tty *tp)
255 {
256 
257 	cnputc(c);
258 	return 0;
259 }
260 __weak_alias(tputchar,rump_tputchar);
261 
262 void
263 cnputc(int c)
264 {
265 	int error;
266 
267 	rumpuser_putchar(c, &error);
268 }
269 
270 void
271 cnflush(void)
272 {
273 
274 	/* done */
275 }
276 
277 #ifdef __HAVE_SYSCALL_INTERN
278 void
279 syscall_intern(struct proc *p)
280 {
281 
282 	/* no you don't */
283 }
284 #endif
285 
286 void
287 xc_send_ipi(struct cpu_info *ci)
288 {
289 
290 	/* I'll think about the implementation if this is ever used */
291 	panic("not implemented");
292 }
293 
294 int
295 trace_enter(register_t code, const register_t *args, int narg)
296 {
297 
298 	return 0;
299 }
300 
301 void
302 trace_exit(register_t code, register_t rval[], int error)
303 {
304 
305 	/* nada */
306 }
307 
308 #ifdef LOCKDEBUG
309 void
310 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
311 {
312 
313 	/* nada */
314 }
315 #endif
316