emul.c revision 1.161 1 /* $NetBSD: emul.c,v 1.161 2014/01/17 01:32:53 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.161 2014/01/17 01:32:53 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/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 #include <sys/cprng.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 device_t booted_device;
101 device_t 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 #ifndef __HAVE_MINIMAL_EMUL
134 .e_nsysent = SYS_NSYSENT,
135 #endif
136 .e_vm_default_addr = uvm_default_mapaddr,
137 #ifdef __HAVE_SYSCALL_INTERN
138 .e_syscall_intern = syscall_intern,
139 #endif
140 };
141
142 u_int nprocs = 1;
143
144 cprng_strong_t *kern_cprng;
145
146 int
147 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
148 {
149 extern int hz;
150 int rv;
151 uint64_t sec, nsec;
152
153 if (mtx)
154 mutex_exit(mtx);
155
156 sec = timeo / hz;
157 nsec = (timeo % hz) * (1000000000 / hz);
158 rv = rumpuser_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
159 KASSERT(rv == 0);
160
161 if (mtx)
162 mutex_enter(mtx);
163
164 return 0;
165 }
166
167 void
168 lwp_unsleep(lwp_t *l, bool cleanup)
169 {
170
171 KASSERT(mutex_owned(l->l_mutex));
172
173 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
174 }
175
176 void
177 lwp_update_creds(struct lwp *l)
178 {
179 struct proc *p;
180 kauth_cred_t oldcred;
181
182 p = l->l_proc;
183 oldcred = l->l_cred;
184 l->l_prflag &= ~LPR_CRMOD;
185
186 mutex_enter(p->p_lock);
187 kauth_cred_hold(p->p_cred);
188 l->l_cred = p->p_cred;
189 mutex_exit(p->p_lock);
190
191 if (oldcred != NULL)
192 kauth_cred_free(oldcred);
193 }
194
195 vaddr_t
196 calc_cache_size(vsize_t vasz, int pct, int va_pct)
197 {
198 paddr_t t;
199
200 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
201 if ((vaddr_t)t != t) {
202 panic("%s: needs tweak", __func__);
203 }
204 return t;
205 }
206
207 void
208 assert_sleepable(void)
209 {
210
211 /* always sleepable, although we should improve this */
212 }
213
214 void
215 module_init_md(void)
216 {
217
218 /*
219 * Nothing for now. However, we should load the librump
220 * symbol table.
221 */
222 }
223
224 /* us and them, after all we're only ordinary seconds */
225 static void
226 rump_delay(unsigned int us)
227 {
228 uint64_t sec, nsec;
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_clock_sleep(RUMPUSER_CLOCK_RELWALL, sec, nsec);
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
268 rumpuser_putchar(c);
269 }
270
271 void
272 cnflush(void)
273 {
274
275 /* done */
276 }
277
278 #ifdef __HAVE_SYSCALL_INTERN
279 void
280 syscall_intern(struct proc *p)
281 {
282
283 p->p_emuldata = NULL;
284 }
285 #endif
286
287 #ifdef LOCKDEBUG
288 void
289 turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
290 {
291
292 /* nada */
293 }
294 #endif
295