emul.c revision 1.155 1 /* $NetBSD: emul.c,v 1.155 2013/03/18 13:36:23 para 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.155 2013/03/18 13:36:23 para 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/cpu.h>
44 #include <sys/kmem.h>
45 #include <sys/poll.h>
46 #include <sys/timetc.h>
47 #include <sys/tprintf.h>
48 #include <sys/module.h>
49 #include <sys/tty.h>
50 #include <sys/reboot.h>
51 #include <sys/syscall.h>
52 #include <sys/syscallvar.h>
53 #include <sys/xcall.h>
54 #include <sys/sleepq.h>
55
56 #include <dev/cons.h>
57
58 #include <rump/rumpuser.h>
59
60 #include <uvm/uvm_map.h>
61
62 #include "rump_private.h"
63
64 /*
65 * physmem is largely unused (except for nmbcluster calculations),
66 * so pick a default value which suits ZFS. if an application wants
67 * a very small memory footprint, it can still adjust this before
68 * calling rump_init()
69 */
70 #define PHYSMEM 512*256
71 int physmem = PHYSMEM;
72 int nkmempages = PHYSMEM/2; /* from le chapeau */
73 #undef PHYSMEM
74
75 struct lwp lwp0;
76 struct vnode *rootvp;
77 dev_t rootdev = NODEV;
78
79 const int schedppq = 1;
80 int hardclock_ticks;
81 bool mp_online = false;
82 struct timeval boottime;
83 int cold = 1;
84 int boothowto = AB_SILENT;
85 struct tty *constty;
86
87 const struct bdevsw *bdevsw0[255];
88 const struct bdevsw **bdevsw = bdevsw0;
89 const int sys_cdevsws = 255;
90 int max_cdevsws = 255;
91
92 const struct cdevsw *cdevsw0[255];
93 const struct cdevsw **cdevsw = cdevsw0;
94 const int sys_bdevsws = 255;
95 int max_bdevsws = 255;
96
97 int mem_no = 2;
98
99 device_t booted_device;
100 device_t booted_wedge;
101 int booted_partition;
102
103 /* XXX: unused */
104 kmutex_t tty_lock;
105 krwlock_t exec_lock;
106
107 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
108
109 /* sparc doesn't sport constant page size, pretend we have 4k pages */
110 #ifdef __sparc__
111 int nbpg = 4096;
112 int pgofset = 4096-1;
113 int pgshift = 12;
114 #endif
115
116 /* on sun3 VM_MAX_ADDRESS is a const variable */
117 /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
118 #ifdef sun3
119 const vaddr_t kernbase = KERNBASE3;
120 #endif
121
122 struct loadavg averunnable = {
123 { 0 * FSCALE,
124 1 * FSCALE,
125 11 * FSCALE, },
126 FSCALE,
127 };
128
129 struct emul emul_netbsd = {
130 .e_name = "netbsd-rump",
131 .e_sysent = rump_sysent,
132 #ifndef __HAVE_MINIMAL_EMUL
133 .e_nsysent = SYS_NSYSENT,
134 #endif
135 .e_vm_default_addr = uvm_default_mapaddr,
136 #ifdef __HAVE_SYSCALL_INTERN
137 .e_syscall_intern = syscall_intern,
138 #endif
139 };
140
141 u_int nprocs = 1;
142
143 int
144 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
145 {
146 extern int hz;
147 int rv, error;
148 uint64_t sec, nsec;
149
150 if (mtx)
151 mutex_exit(mtx);
152
153 sec = timeo / hz;
154 nsec = (timeo % hz) * (1000000000 / hz);
155 rv = rumpuser_nanosleep(&sec, &nsec, &error);
156
157 if (mtx)
158 mutex_enter(mtx);
159
160 if (rv)
161 return error;
162
163 return 0;
164 }
165
166 void
167 lwp_unsleep(lwp_t *l, bool cleanup)
168 {
169
170 KASSERT(mutex_owned(l->l_mutex));
171
172 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
173 }
174
175 void
176 lwp_update_creds(struct lwp *l)
177 {
178 struct proc *p;
179 kauth_cred_t oldcred;
180
181 p = l->l_proc;
182 oldcred = l->l_cred;
183 l->l_prflag &= ~LPR_CRMOD;
184
185 mutex_enter(p->p_lock);
186 kauth_cred_hold(p->p_cred);
187 l->l_cred = p->p_cred;
188 mutex_exit(p->p_lock);
189
190 if (oldcred != NULL)
191 kauth_cred_free(oldcred);
192 }
193
194 vaddr_t
195 calc_cache_size(vsize_t vasz, int pct, int va_pct)
196 {
197 paddr_t t;
198
199 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
200 if ((vaddr_t)t != t) {
201 panic("%s: needs tweak", __func__);
202 }
203 return t;
204 }
205
206 void
207 assert_sleepable(void)
208 {
209
210 /* always sleepable, although we should improve this */
211 }
212
213 void
214 module_init_md(void)
215 {
216
217 /*
218 * Nothing for now. However, we should load the librump
219 * symbol table.
220 */
221 }
222
223 /* us and them, after all we're only ordinary seconds */
224 static void
225 rump_delay(unsigned int us)
226 {
227 uint64_t sec, nsec;
228 int error;
229
230 sec = us / 1000000;
231 nsec = (us % 1000000) * 1000;
232
233 if (__predict_false(sec != 0))
234 printf("WARNING: over 1s delay\n");
235
236 rumpuser_nanosleep(&sec, &nsec, &error);
237 }
238 void (*delay_func)(unsigned int) = rump_delay;
239
240 /*
241 * Provide weak aliases for tty routines used by printf.
242 * They will be used unless the rumpkern_tty component is present.
243 */
244
245 int rump_ttycheckoutq(struct tty *, int);
246 int
247 rump_ttycheckoutq(struct tty *tp, int wait)
248 {
249
250 return 1;
251 }
252 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
253
254 int rump_tputchar(int, int, struct tty *);
255 int
256 rump_tputchar(int c, int flags, struct tty *tp)
257 {
258
259 cnputc(c);
260 return 0;
261 }
262 __weak_alias(tputchar,rump_tputchar);
263
264 void
265 cnputc(int c)
266 {
267 int error;
268
269 rumpuser_putchar(c, &error);
270 }
271
272 void
273 cnflush(void)
274 {
275
276 /* done */
277 }
278
279 #ifdef __HAVE_SYSCALL_INTERN
280 void
281 syscall_intern(struct proc *p)
282 {
283
284 /* no you don't */
285 }
286 #endif
287
288 int
289 trace_enter(register_t code, const register_t *args, int narg)
290 {
291
292 return 0;
293 }
294
295 void
296 trace_exit(register_t code, register_t rval[], int error)
297 {
298
299 /* nada */
300 }
301
302 #ifdef LOCKDEBUG
303 void
304 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
305 {
306
307 /* nada */
308 }
309 #endif
310