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