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