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