emul.c revision 1.108 1 /* $NetBSD: emul.c,v 1.108 2009/11/04 19:17:53 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.108 2009/11/04 19:17:53 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/filedesc.h>
46 #include <sys/cpu.h>
47 #include <sys/kmem.h>
48 #include <sys/poll.h>
49 #include <sys/timetc.h>
50 #include <sys/tprintf.h>
51 #include <sys/module.h>
52 #include <sys/tty.h>
53 #include <sys/reboot.h>
54
55 #include <dev/cons.h>
56
57 #include <rump/rumpuser.h>
58
59 #include <uvm/uvm_map.h>
60
61 #include "rump_private.h"
62
63 time_t time_second = 1;
64
65 kmutex_t *proc_lock;
66 struct lwp lwp0;
67 struct vnode *rootvp;
68 struct device *root_device;
69 dev_t rootdev;
70 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
71 int doing_shutdown;
72 const int schedppq = 1;
73 int hardclock_ticks;
74 bool mp_online = false;
75 struct vm_map *mb_map;
76 struct timeval boottime;
77 struct emul emul_netbsd;
78 int cold = 1;
79 int boothowto = AB_SILENT;
80 struct tty *constty;
81
82 char hostname[MAXHOSTNAMELEN];
83 size_t hostnamelen;
84
85 const char *panicstr;
86 const char ostype[] = "NetBSD";
87 const char osrelease[] = "999"; /* paradroid 4evah */
88 const char kernel_ident[] = "RUMP-ROAST";
89 const char *domainname;
90 int domainnamelen;
91
92 const struct filterops sig_filtops;
93
94 #define DEVSW_SIZE 255
95 const struct bdevsw *bdevsw0[DEVSW_SIZE]; /* XXX storage size */
96 const struct bdevsw **bdevsw = bdevsw0;
97 const int sys_cdevsws = DEVSW_SIZE;
98 int max_cdevsws = DEVSW_SIZE;
99
100 const struct cdevsw *cdevsw0[DEVSW_SIZE]; /* XXX storage size */
101 const struct cdevsw **cdevsw = cdevsw0;
102 const int sys_bdevsws = DEVSW_SIZE;
103 int max_bdevsws = DEVSW_SIZE;
104
105 struct devsw_conv devsw_conv0;
106 struct devsw_conv *devsw_conv = &devsw_conv0;
107 int max_devsw_convs = 0;
108 int mem_no = 2;
109
110 struct device *booted_device;
111 struct device *booted_wedge;
112 int booted_partition;
113
114 kmutex_t tty_lock;
115
116 devclass_t
117 device_class(device_t dev)
118 {
119
120 if (dev != root_device)
121 panic("%s: dev != root_device not supported", __func__);
122
123 return DV_DISK;
124 }
125
126 void
127 getnanouptime(struct timespec *ts)
128 {
129
130 rump_getuptime(ts);
131 }
132
133 void
134 getmicrouptime(struct timeval *tv)
135 {
136 struct timespec ts;
137
138 getnanouptime(&ts);
139 TIMESPEC_TO_TIMEVAL(tv, &ts);
140 }
141
142 static void
143 gettime(struct timespec *ts)
144 {
145 uint64_t sec, nsec;
146 int error;
147
148 rumpuser_gettime(&sec, &nsec, &error);
149 ts->tv_sec = sec;
150 ts->tv_nsec = nsec;
151 }
152
153 void
154 nanotime(struct timespec *ts)
155 {
156
157 if (rump_threads) {
158 rump_gettime(ts);
159 } else {
160 gettime(ts);
161 }
162 }
163
164 /* hooray for mick, so what if I do */
165 void
166 getnanotime(struct timespec *ts)
167 {
168
169 nanotime(ts);
170 }
171
172 void
173 microtime(struct timeval *tv)
174 {
175 struct timespec ts;
176
177 if (rump_threads) {
178 rump_gettime(&ts);
179 TIMESPEC_TO_TIMEVAL(tv, &ts);
180 } else {
181 gettime(&ts);
182 TIMESPEC_TO_TIMEVAL(tv, &ts);
183 }
184 }
185
186 void
187 getmicrotime(struct timeval *tv)
188 {
189
190 microtime(tv);
191 }
192
193 struct proc *
194 p_find(pid_t pid, uint flags)
195 {
196
197 panic("%s: not implemented", __func__);
198 }
199
200 struct pgrp *
201 pg_find(pid_t pid, uint flags)
202 {
203
204 panic("%s: not implemented", __func__);
205 }
206
207 void
208 psignal(struct proc *p, int signo)
209 {
210
211 switch (signo) {
212 case SIGSYS:
213 break;
214 default:
215 panic("unhandled signal %d\n", signo);
216 }
217 }
218
219 void
220 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
221 {
222
223 panic("%s: not implemented", __func__);
224 }
225
226 void
227 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
228 {
229
230 panic("%s: not implemented", __func__);
231 }
232
233 int
234 pgid_in_session(struct proc *p, pid_t pg_id)
235 {
236
237 panic("%s: not implemented", __func__);
238 }
239
240 int
241 sigispending(struct lwp *l, int signo)
242 {
243
244 return 0;
245 }
246
247 void
248 sigpending1(struct lwp *l, sigset_t *ss)
249 {
250
251 panic("%s: not implemented", __func__);
252 }
253
254 int
255 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
256 {
257 extern int hz;
258 int rv, error;
259 uint64_t sec, nsec;
260
261 if (mtx)
262 mutex_exit(mtx);
263
264 sec = timeo / hz;
265 nsec = (timeo % hz) * (1000000000 / hz);
266 rv = rumpuser_nanosleep(&sec, &nsec, &error);
267
268 if (mtx)
269 mutex_enter(mtx);
270
271 if (rv)
272 return error;
273
274 return 0;
275 }
276
277 void
278 suspendsched(void)
279 {
280
281 /* we don't control scheduling currently, can't do anything now */
282 }
283
284 void
285 lwp_unsleep(lwp_t *l, bool cleanup)
286 {
287
288 KASSERT(mutex_owned(l->l_mutex));
289
290 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
291 }
292
293 vaddr_t
294 calc_cache_size(struct vm_map *map, int pct, int va_pct)
295 {
296 paddr_t t;
297
298 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
299 if ((vaddr_t)t != t) {
300 panic("%s: needs tweak", __func__);
301 }
302 return t;
303 }
304
305 const char *
306 device_xname(device_t dv)
307 {
308 return "bogus0";
309 }
310
311 void
312 assert_sleepable(void)
313 {
314
315 /* always sleepable, although we should improve this */
316 }
317
318 void
319 tc_setclock(const struct timespec *ts)
320 {
321
322 panic("%s: not implemented", __func__);
323 }
324
325 int
326 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
327 {
328
329 panic("%s: not implemented", __func__);
330 }
331
332 void
333 proc_crmod_enter(void)
334 {
335
336 panic("%s: not implemented", __func__);
337 }
338
339 void
340 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
341 {
342
343 panic("%s: not implemented", __func__);
344 }
345
346 void
347 module_init_md(void)
348 {
349
350 /*
351 * Nothing for now. However, we should load the librump
352 * symbol table.
353 */
354 }
355
356 /* us and them, after all we're only ordinary seconds */
357 static void
358 rump_delay(unsigned int us)
359 {
360 uint64_t sec, nsec;
361 int error;
362
363 sec = us / 1000000;
364 nsec = (us % 1000000) * 1000;
365
366 if (__predict_false(sec != 0))
367 printf("WARNING: over 1s delay\n");
368
369 rumpuser_nanosleep(&sec, &nsec, &error);
370 }
371 void (*delay_func)(unsigned int) = rump_delay;
372
373 void
374 kpreempt_disable(void)
375 {
376
377 /* XXX: see below */
378 KPREEMPT_DISABLE(curlwp);
379 }
380
381 void
382 kpreempt_enable(void)
383 {
384
385 /* try to make sure kpreempt_disable() is only used from panic() */
386 panic("kpreempt not supported");
387 }
388
389 void
390 proc_sesshold(struct session *ss)
391 {
392
393 panic("proc_sesshold() impossible, session %p", ss);
394 }
395
396 void
397 proc_sessrele(struct session *ss)
398 {
399
400 panic("proc_sessrele() impossible, session %p", ss);
401 }
402
403 int
404 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
405 {
406
407 /* XXX */
408 *vm = p->p_vmspace;
409 return 0;
410 }
411
412 int
413 ttycheckoutq(struct tty *tp, int wait)
414 {
415
416 return 1;
417 }
418
419 void
420 cnputc(int c)
421 {
422 int error;
423
424 rumpuser_putchar(c, &error);
425 }
426
427 void
428 cnflush(void)
429 {
430
431 /* done */
432 }
433
434 int
435 tputchar(int c, int flags, struct tty *tp)
436 {
437
438 cnputc(c);
439 return 0;
440 }
441
442 void
443 cpu_reboot(int howto, char *bootstr)
444 {
445
446 rump_reboot(howto);
447
448 /* this function is __dead, we must exit */
449 rumpuser_exit(0);
450 }
451
452 bool
453 pmf_device_register1(struct device *dev,
454 bool (*suspend)(device_t PMF_FN_PROTO),
455 bool (*resume)(device_t PMF_FN_PROTO),
456 bool (*shutdown)(device_t, int))
457 {
458
459 return true;
460 }
461
462 void
463 pmf_device_deregister(struct device *dev)
464 {
465
466 /* nada */
467 }
468