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