emul.c revision 1.70 1 /* $NetBSD: emul.c,v 1.70 2009/01/04 20:30:21 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.70 2009/01/04 20:30:21 pooka Exp $");
32
33 #define malloc(a,b,c) __wrap_malloc(a,b,c)
34
35 #include <sys/param.h>
36 #include <sys/malloc.h>
37 #include <sys/null.h>
38 #include <sys/vnode.h>
39 #include <sys/stat.h>
40 #include <sys/select.h>
41 #include <sys/syslog.h>
42 #include <sys/namei.h>
43 #include <sys/kauth.h>
44 #include <sys/conf.h>
45 #include <sys/device.h>
46 #include <sys/queue.h>
47 #include <sys/file.h>
48 #include <sys/filedesc.h>
49 #include <sys/kthread.h>
50 #include <sys/cpu.h>
51 #include <sys/kmem.h>
52 #include <sys/poll.h>
53 #include <sys/timetc.h>
54 #include <sys/tprintf.h>
55 #include <sys/module.h>
56 #include <sys/tty.h>
57 #include <sys/reboot.h>
58
59 #include <dev/cons.h>
60
61 #include <machine/stdarg.h>
62
63 #include <rump/rumpuser.h>
64
65 #include <uvm/uvm_map.h>
66
67 #include "rump_private.h"
68
69 time_t time_second = 1;
70
71 kmutex_t *proc_lock;
72 struct lwp lwp0;
73 struct vnode *rootvp;
74 struct device *root_device;
75 dev_t rootdev;
76 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
77 int doing_shutdown;
78 int ncpu = 1;
79 const int schedppq = 1;
80 int hardclock_ticks;
81 bool mp_online = false;
82 struct vm_map *mb_map;
83 struct timeval boottime;
84 struct emul emul_netbsd;
85 int cold = 1;
86 int boothowto;
87 struct tty *constty;
88
89 char hostname[MAXHOSTNAMELEN];
90 size_t hostnamelen;
91
92 u_long bufmem_valimit;
93 u_long bufmem_hiwater;
94 u_long bufmem_lowater;
95 u_long bufmem;
96 u_int nbuf;
97
98 const char *panicstr;
99 const char ostype[] = "NetBSD";
100 const char osrelease[] = "999"; /* paradroid 4evah */
101 const char kernel_ident[] = "RUMP-ROAST";
102 const char *domainname;
103 int domainnamelen;
104
105 const struct filterops seltrue_filtops;
106
107 #define DEVSW_SIZE 255
108 const struct bdevsw *bdevsw0[DEVSW_SIZE]; /* XXX storage size */
109 const struct bdevsw **bdevsw = bdevsw0;
110 const int sys_cdevsws = DEVSW_SIZE;
111 int max_cdevsws = DEVSW_SIZE;
112
113 const struct cdevsw *cdevsw0[DEVSW_SIZE]; /* XXX storage size */
114 const struct cdevsw **cdevsw = cdevsw0;
115 const int sys_bdevsws = DEVSW_SIZE;
116 int max_bdevsws = DEVSW_SIZE;
117
118 struct devsw_conv devsw_conv0;
119 struct devsw_conv *devsw_conv = &devsw_conv0;
120 int max_devsw_convs = 0;
121
122
123 int
124 copyin(const void *uaddr, void *kaddr, size_t len)
125 {
126
127 memcpy(kaddr, uaddr, len);
128 return 0;
129 }
130
131 int
132 copyout(const void *kaddr, void *uaddr, size_t len)
133 {
134
135 memcpy(uaddr, kaddr, len);
136 return 0;
137 }
138
139 int
140 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
141 {
142
143 return copyinstr(kfaddr, kdaddr, len, done);
144 }
145
146 int
147 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
148 {
149
150 strlcpy(kaddr, uaddr, len);
151 if (done)
152 *done = strlen(kaddr)+1; /* includes termination */
153 return 0;
154 }
155
156 int
157 copyin_vmspace(struct vmspace *vm, const void *uaddr, void *kaddr, size_t len)
158 {
159
160 return copyin(uaddr, kaddr, len);
161 }
162
163 int
164 copyout_vmspace(struct vmspace *vm, const void *kaddr, void *uaddr, size_t len)
165 {
166
167 return copyout(kaddr, uaddr, len);
168 }
169
170 int
171 kcopy(const void *src, void *dst, size_t len)
172 {
173
174 memcpy(dst, src, len);
175 return 0;
176 }
177
178 int
179 uiomove(void *buf, size_t n, struct uio *uio)
180 {
181 struct iovec *iov;
182 uint8_t *b = buf;
183 size_t cnt;
184
185 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
186 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
187
188 while (n && uio->uio_resid) {
189 iov = uio->uio_iov;
190 cnt = iov->iov_len;
191 if (cnt == 0) {
192 uio->uio_iov++;
193 uio->uio_iovcnt--;
194 continue;
195 }
196 if (cnt > n)
197 cnt = n;
198
199 if (uio->uio_rw == UIO_READ)
200 memcpy(iov->iov_base, b, cnt);
201 else
202 memcpy(b, iov->iov_base, cnt);
203
204 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
205 iov->iov_len -= cnt;
206 b += cnt;
207 uio->uio_resid -= cnt;
208 uio->uio_offset += cnt;
209 n -= cnt;
210 }
211
212 return 0;
213 }
214
215 void
216 uio_setup_sysspace(struct uio *uio)
217 {
218
219 uio->uio_vmspace = UIO_VMSPACE_SYS;
220 }
221
222 devclass_t
223 device_class(device_t dev)
224 {
225
226 if (dev != root_device)
227 panic("%s: dev != root_device not supported", __func__);
228
229 return DV_DISK;
230 }
231
232 void
233 getmicrouptime(struct timeval *tvp)
234 {
235 int error;
236
237 rumpuser_gettimeofday(tvp, &error);
238 }
239
240 void
241 malloc_type_attach(struct malloc_type *type)
242 {
243
244 return;
245 }
246
247 void
248 malloc_type_detach(struct malloc_type *type)
249 {
250
251 return;
252 }
253
254 void *
255 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
256 {
257 void *rv;
258
259 rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
260 if (rv && flags & M_ZERO)
261 memset(rv, 0, size);
262
263 return rv;
264 }
265
266 void
267 nanotime(struct timespec *ts)
268 {
269 struct timeval tv;
270 int error;
271
272 rumpuser_gettimeofday(&tv, &error);
273 TIMEVAL_TO_TIMESPEC(&tv, ts);
274 }
275
276 /* hooray for mick, so what if I do */
277 void
278 getnanotime(struct timespec *ts)
279 {
280
281 nanotime(ts);
282 }
283
284 void
285 microtime(struct timeval *tv)
286 {
287 int error;
288
289 rumpuser_gettimeofday(tv, &error);
290 }
291
292 void
293 getmicrotime(struct timeval *tv)
294 {
295 int error;
296
297 rumpuser_gettimeofday(tv, &error);
298 }
299
300 struct kthdesc {
301 void (*f)(void *);
302 void *arg;
303 struct lwp *mylwp;
304 };
305
306 static void *
307 threadbouncer(void *arg)
308 {
309 struct kthdesc *k = arg;
310 void (*f)(void *);
311 void *thrarg;
312
313 f = k->f;
314 thrarg = k->arg;
315 rumpuser_set_curlwp(k->mylwp);
316 kmem_free(k, sizeof(struct kthdesc));
317
318 if ((curlwp->l_pflag & LP_MPSAFE) == 0)
319 KERNEL_LOCK(1, NULL);
320 f(thrarg);
321 panic("unreachable, should kthread_exit()");
322 }
323
324 int
325 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
326 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
327 {
328 char thrstore[MAXCOMLEN];
329 const char *thrname = NULL;
330 va_list ap;
331 struct kthdesc *k;
332 struct lwp *l;
333 int rv;
334
335 /*
336 * We don't want a module unload thread.
337 * (XXX: yes, this is a kludge too, and the kernel should
338 * have a more flexible method for configuring which threads
339 * we want).
340 */
341 if (strcmp(fmt, "modunload") == 0) {
342 return 0;
343 }
344
345 if (!rump_threads) {
346 /* fake them */
347 if (strcmp(fmt, "vrele") == 0) {
348 printf("rump warning: threads not enabled, not starting"
349 " vrele thread\n");
350 return 0;
351 } else if (strcmp(fmt, "cachegc") == 0) {
352 printf("rump warning: threads not enabled, not starting"
353 " namecache g/c thread\n");
354 return 0;
355 } else
356 panic("threads not available, setenv RUMP_THREADS 1");
357 }
358
359 KASSERT(fmt != NULL);
360 if (ci != NULL)
361 panic("%s: bounded threads not supported", __func__);
362
363 k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
364 k->f = func;
365 k->arg = arg;
366 k->mylwp = l = rump_setup_curlwp(0, rump_nextlid(), 0);
367 if (flags & KTHREAD_MPSAFE)
368 l->l_pflag |= LP_MPSAFE;
369 if (fmt) {
370 va_start(ap, fmt);
371 vsnprintf(thrstore, sizeof(thrname), fmt, ap);
372 va_end(ap);
373 thrname = thrstore;
374 }
375 rv = rumpuser_thread_create(threadbouncer, k, thrname);
376 if (rv)
377 return rv;
378
379 if (newlp)
380 *newlp = l;
381 return 0;
382 }
383
384 void
385 kthread_exit(int ecode)
386 {
387
388 if ((curlwp->l_pflag & LP_MPSAFE) == 0)
389 KERNEL_UNLOCK_ONE(NULL);
390 rump_clear_curlwp();
391 rumpuser_thread_exit();
392 }
393
394 struct proc *
395 p_find(pid_t pid, uint flags)
396 {
397
398 panic("%s: not implemented", __func__);
399 }
400
401 struct pgrp *
402 pg_find(pid_t pid, uint flags)
403 {
404
405 panic("%s: not implemented", __func__);
406 }
407
408 void
409 psignal(struct proc *p, int signo)
410 {
411
412 switch (signo) {
413 case SIGSYS:
414 break;
415 default:
416 panic("unhandled signal %d", signo);
417 }
418 }
419
420 void
421 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
422 {
423
424 panic("%s: not implemented", __func__);
425 }
426
427 void
428 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
429 {
430
431 panic("%s: not implemented", __func__);
432 }
433
434 int
435 pgid_in_session(struct proc *p, pid_t pg_id)
436 {
437
438 panic("%s: not implemented", __func__);
439 }
440
441 int
442 sigispending(struct lwp *l, int signo)
443 {
444
445 return 0;
446 }
447
448 void
449 sigpending1(struct lwp *l, sigset_t *ss)
450 {
451
452 panic("%s: not implemented", __func__);
453 }
454
455 void
456 knote_fdclose(int fd)
457 {
458
459 /* since we don't add knotes, we don't have to remove them */
460 }
461
462 int
463 seltrue_kqfilter(dev_t dev, struct knote *kn)
464 {
465
466 panic("%s: not implemented", __func__);
467 }
468
469 int
470 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
471 {
472 extern int hz;
473 int rv, error;
474 struct timespec time;
475
476 if (mtx)
477 mutex_exit(mtx);
478
479 time.tv_sec = timeo / hz;
480 time.tv_nsec = (timeo % hz) * (1000000000 / hz);
481
482 rv = rumpuser_nanosleep(&time, NULL, &error);
483
484 if (mtx)
485 mutex_enter(mtx);
486
487 if (rv)
488 return error;
489
490 return 0;
491 }
492
493 void
494 suspendsched()
495 {
496
497 panic("%s: not implemented", __func__);
498 }
499
500 u_int
501 lwp_unsleep(lwp_t *l, bool cleanup)
502 {
503
504 KASSERT(mutex_owned(l->l_mutex));
505
506 return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
507 }
508
509 vaddr_t
510 calc_cache_size(struct vm_map *map, int pct, int va_pct)
511 {
512 paddr_t t;
513
514 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
515 if ((vaddr_t)t != t) {
516 panic("%s: needs tweak", __func__);
517 }
518 return t;
519 }
520
521 int
522 seltrue(dev_t dev, int events, struct lwp *l)
523 {
524 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
525 }
526
527 void
528 selrecord(lwp_t *selector, struct selinfo *sip)
529 {
530 }
531
532 void
533 selinit(struct selinfo *sip)
534 {
535 }
536
537 void
538 selnotify(struct selinfo *sip, int events, long knhint)
539 {
540 }
541
542 void
543 seldestroy(struct selinfo *sip)
544 {
545 }
546
547 const char *
548 device_xname(device_t dv)
549 {
550 return "bogus0";
551 }
552
553 void
554 assert_sleepable(void)
555 {
556
557 /* always sleepable, although we should improve this */
558 }
559
560 void
561 tc_setclock(struct timespec *ts)
562 {
563
564 panic("%s: not implemented", __func__);
565 }
566
567 void
568 proc_crmod_enter()
569 {
570
571 panic("%s: not implemented", __func__);
572 }
573
574 void
575 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
576 {
577
578 panic("%s: not implemented", __func__);
579 }
580
581 void
582 module_init_md()
583 {
584
585 /*
586 * Nothing for now. However, we should load the librump
587 * symbol table.
588 */
589 }
590
591 /* us and them, after all we're only ordinary seconds */
592 static void
593 rump_delay(unsigned int us)
594 {
595 struct timespec ts;
596 int error;
597
598 ts.tv_sec = us / 1000000;
599 ts.tv_nsec = (us % 1000000) * 1000;
600
601 if (__predict_false(ts.tv_sec != 0))
602 printf("WARNING: over 1s delay\n");
603
604 rumpuser_nanosleep(&ts, NULL, &error);
605 }
606 void (*delay_func)(unsigned int) = rump_delay;
607
608 void
609 kpreempt_disable()
610 {
611
612 /* XXX: see below */
613 KPREEMPT_DISABLE(curlwp);
614 }
615
616 void
617 kpreempt_enable()
618 {
619
620 /* try to make sure kpreempt_disable() is only used from panic() */
621 panic("kpreempt not supported");
622 }
623
624 void
625 sessdelete(struct session *ss)
626 {
627
628 panic("sessdelete() impossible, session %p", ss);
629 }
630
631 int
632 ttycheckoutq(struct tty *tp, int wait)
633 {
634
635 return 1;
636 }
637
638 void
639 cnputc(int c)
640 {
641 int error;
642
643 rumpuser_putchar(c, &error);
644 }
645
646 void
647 cnflush()
648 {
649
650 /* done */
651 }
652
653 int
654 tputchar(int c, int flags, struct tty *tp)
655 {
656
657 cnputc(c);
658 return 0;
659 }
660
661 void
662 cpu_reboot(int howto, char *bootstr)
663 {
664
665 rumpuser_panic();
666 }
667