emul.c revision 1.46 1 /* $NetBSD: emul.c,v 1.46 2008/08/04 15:02:16 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 #define malloc(a,b,c) __wrap_malloc(a,b,c)
31
32 #include <sys/param.h>
33 #include <sys/malloc.h>
34 #include <sys/null.h>
35 #include <sys/vnode.h>
36 #include <sys/stat.h>
37 #include <sys/select.h>
38 #include <sys/syslog.h>
39 #include <sys/namei.h>
40 #include <sys/kauth.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/filedesc.h>
46 #include <sys/kthread.h>
47 #include <sys/cpu.h>
48 #include <sys/kmem.h>
49 #include <sys/poll.h>
50
51 #include <machine/stdarg.h>
52
53 #include <rump/rumpuser.h>
54
55 #include <uvm/uvm_map.h>
56
57 #include "rump_private.h"
58
59 time_t time_second = 1;
60
61 kmutex_t *proc_lock;
62 struct lwp lwp0;
63 struct vnode *rootvp;
64 struct device *root_device;
65 dev_t rootdev;
66 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
67 int doing_shutdown;
68 int ncpu = 1;
69 const int schedppq = 1;
70 int hardclock_ticks;
71
72 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
73 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
74 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
75 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
76
77 char hostname[MAXHOSTNAMELEN];
78 size_t hostnamelen;
79
80 u_long bufmem_valimit;
81 u_long bufmem_hiwater;
82 u_long bufmem_lowater;
83 u_long bufmem;
84 u_int nbuf;
85
86 const char *panicstr;
87 const char ostype[] = "NetBSD";
88 const char osrelease[] = "999"; /* paradroid 4evah */
89 const char kernel_ident[] = "RUMP-ROAST";
90 const char *domainname;
91 int domainnamelen;
92
93 const struct filterops seltrue_filtops;
94
95 void
96 panic(const char *fmt, ...)
97 {
98 va_list ap;
99
100 va_start(ap, fmt);
101 printf("panic: ");
102 vprintf(fmt, ap);
103 va_end(ap);
104 printf("\n");
105 abort();
106 }
107
108 void
109 log(int level, const char *fmt, ...)
110 {
111 va_list ap;
112
113 va_start(ap, fmt);
114 vprintf(fmt, ap);
115 va_end(ap);
116 }
117
118 void
119 vlog(int level, const char *fmt, va_list ap)
120 {
121
122 vprintf(fmt, ap);
123 }
124
125 void
126 uprintf(const char *fmt, ...)
127 {
128 va_list ap;
129
130 va_start(ap, fmt);
131 vprintf(fmt, ap);
132 va_end(ap);
133 }
134
135 void
136 printf_nolog(const char *fmt, ...)
137 {
138 va_list ap;
139
140 va_start(ap, fmt);
141 vprintf(fmt, ap);
142 va_end(ap);
143 }
144
145 void
146 aprint_normal(const char *fmt, ...)
147 {
148 va_list ap;
149
150 va_start(ap, fmt);
151 vprintf(fmt, ap);
152 va_end(ap);
153 }
154
155 int
156 copyin(const void *uaddr, void *kaddr, size_t len)
157 {
158
159 memcpy(kaddr, uaddr, len);
160 return 0;
161 }
162
163 int
164 copyout(const void *kaddr, void *uaddr, size_t len)
165 {
166
167 memcpy(uaddr, kaddr, len);
168 return 0;
169 }
170
171 int
172 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
173 {
174
175 return copyinstr(kfaddr, kdaddr, len, done);
176 }
177
178 int
179 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
180 {
181
182 strlcpy(kaddr, uaddr, len);
183 if (done)
184 *done = strlen(kaddr)+1; /* includes termination */
185 return 0;
186 }
187
188 int
189 kcopy(const void *src, void *dst, size_t len)
190 {
191
192 memcpy(dst, src, len);
193 return 0;
194 }
195
196 int
197 uiomove(void *buf, size_t n, struct uio *uio)
198 {
199 struct iovec *iov;
200 uint8_t *b = buf;
201 size_t cnt;
202 int rv;
203
204 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
205 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
206
207 /*
208 * See if rump ubc code claims the offset. This is of course
209 * a blatant violation of abstraction levels, but let's keep
210 * me simple & stupid for now.
211 */
212 if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
213 return rv;
214
215 while (n && uio->uio_resid) {
216 iov = uio->uio_iov;
217 cnt = iov->iov_len;
218 if (cnt == 0) {
219 uio->uio_iov++;
220 uio->uio_iovcnt--;
221 continue;
222 }
223 if (cnt > n)
224 cnt = n;
225
226 if (uio->uio_rw == UIO_READ)
227 memcpy(iov->iov_base, b, cnt);
228 else
229 memcpy(b, iov->iov_base, cnt);
230
231 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
232 iov->iov_len -= cnt;
233 b += cnt;
234 uio->uio_resid -= cnt;
235 uio->uio_offset += cnt;
236 n -= cnt;
237 }
238
239 return 0;
240 }
241
242 void
243 uio_setup_sysspace(struct uio *uio)
244 {
245
246 uio->uio_vmspace = UIO_VMSPACE_SYS;
247 }
248
249 const struct bdevsw *
250 bdevsw_lookup(dev_t dev)
251 {
252
253 return (const struct bdevsw *)1;
254 }
255
256 devclass_t
257 device_class(device_t dev)
258 {
259
260 if (dev != root_device)
261 panic("%s: dev != root_device not supported", __func__);
262
263 return DV_DISK;
264 }
265
266 void
267 getmicrouptime(struct timeval *tvp)
268 {
269 int error;
270
271 rumpuser_gettimeofday(tvp, &error);
272 }
273
274 void
275 malloc_type_attach(struct malloc_type *type)
276 {
277
278 return;
279 }
280
281 void
282 malloc_type_detach(struct malloc_type *type)
283 {
284
285 return;
286 }
287
288 void *
289 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
290 {
291 void *rv;
292
293 rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
294 if (rv && flags & M_ZERO)
295 memset(rv, 0, size);
296
297 return rv;
298 }
299
300 void
301 nanotime(struct timespec *ts)
302 {
303 struct timeval tv;
304 int error;
305
306 rumpuser_gettimeofday(&tv, &error);
307 TIMEVAL_TO_TIMESPEC(&tv, ts);
308 }
309
310 /* hooray for mick, so what if I do */
311 void
312 getnanotime(struct timespec *ts)
313 {
314
315 nanotime(ts);
316 }
317
318 void
319 microtime(struct timeval *tv)
320 {
321 int error;
322
323 rumpuser_gettimeofday(tv, &error);
324 }
325
326 void
327 getmicrotime(struct timeval *tv)
328 {
329 int error;
330
331 rumpuser_gettimeofday(tv, &error);
332 }
333
334 void
335 bdev_strategy(struct buf *bp)
336 {
337
338 panic("%s: not supported", __func__);
339 }
340
341 int
342 bdev_type(dev_t dev)
343 {
344
345 return D_DISK;
346 }
347
348 struct kthdesc {
349 void (*f)(void *);
350 void *arg;
351 struct lwp *mylwp;
352 };
353
354 static lwpid_t curlid = 2;
355
356 static void *
357 threadbouncer(void *arg)
358 {
359 struct kthdesc *k = arg;
360 void (*f)(void *);
361 void *thrarg;
362
363 f = k->f;
364 thrarg = k->arg;
365 rumpuser_set_curlwp(k->mylwp);
366 kmem_free(k, sizeof(struct kthdesc));
367
368 f(thrarg);
369 panic("unreachable, should kthread_exit()");
370 }
371
372 int
373 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
374 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
375 {
376 struct kthdesc *k;
377 struct lwp *l;
378 int rv;
379
380 #ifdef RUMP_WITHOUT_THREADS
381 /* fake them */
382 if (strcmp(fmt, "vrele") == 0) {
383 printf("rump warning: threads not enabled, not starting "
384 "vrele thread\n");
385 return 0;
386 } else if (strcmp(fmt, "cachegc") == 0) {
387 printf("rump warning: threads not enabled, not starting "
388 "namecache g/c thread\n");
389 return 0;
390 } else
391 panic("threads not available, undef RUMP_WITHOUT_THREADS");
392 #endif
393
394 KASSERT(fmt != NULL);
395 if (ci != NULL)
396 panic("%s: bounded threads not supported", __func__);
397
398 k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
399 k->f = func;
400 k->arg = arg;
401 k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
402 rv = rumpuser_thread_create(threadbouncer, k);
403 if (rv)
404 return rv;
405
406 if (newlp)
407 *newlp = l;
408 return 0;
409 }
410
411 void
412 kthread_exit(int ecode)
413 {
414
415 rumpuser_thread_exit();
416 }
417
418 void
419 callout_init(callout_t *c, u_int flags)
420 {
421
422 panic("%s: not implemented", __func__);
423 }
424
425 void
426 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
427 {
428
429 panic("%s: not implemented", __func__);
430 }
431
432 bool
433 callout_stop(callout_t *c)
434 {
435
436 panic("%s: not implemented", __func__);
437 }
438
439 void
440 callout_schedule(callout_t *c, int ticks)
441 {
442
443 panic("%s: not implemented", __func__);
444 }
445
446 void
447 callout_setfunc(callout_t *c, void (*func)(void *), void *arg)
448 {
449
450 panic("%s: not implemented", __func__);
451 }
452
453 struct proc *
454 p_find(pid_t pid, uint flags)
455 {
456
457 panic("%s: not implemented", __func__);
458 }
459
460 struct pgrp *
461 pg_find(pid_t pid, uint flags)
462 {
463
464 panic("%s: not implemented", __func__);
465 }
466
467 void
468 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
469 {
470
471 panic("%s: not implemented", __func__);
472 }
473
474 void
475 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
476 {
477
478 panic("%s: not implemented", __func__);
479 }
480
481 int
482 pgid_in_session(struct proc *p, pid_t pg_id)
483 {
484
485 panic("%s: not implemented", __func__);
486 }
487
488 int
489 sigispending(struct lwp *l, int signo)
490 {
491
492 return 0;
493 }
494
495 void
496 knote_fdclose(int fd)
497 {
498
499 /* since we don't add knotes, we don't have to remove them */
500 }
501
502 int
503 seltrue_kqfilter(dev_t dev, struct knote *kn)
504 {
505
506 panic("%s: not implemented", __func__);
507 }
508
509 int
510 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
511 {
512 extern int hz;
513 int rv, error;
514 struct timespec time;
515
516 if (mtx)
517 mutex_exit(mtx);
518
519 time.tv_sec = timeo / hz;
520 time.tv_nsec = (timeo % hz) * (1000000000 / hz);
521
522 rv = rumpuser_nanosleep(&time, NULL, &error);
523
524 if (mtx)
525 mutex_enter(mtx);
526
527 if (rv)
528 return error;
529
530 return 0;
531 }
532
533 void
534 suspendsched()
535 {
536
537 panic("%s: not implemented", __func__);
538 }
539
540 void
541 yield(void)
542 {
543
544 rumpuser_yield();
545 }
546
547
548 u_int
549 lwp_unsleep(lwp_t *l, bool cleanup)
550 {
551
552 KASSERT(mutex_owned(l->l_mutex));
553
554 return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
555 }
556
557 vaddr_t
558 calc_cache_size(struct vm_map *map, int pct, int va_pct)
559 {
560 paddr_t t;
561
562 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
563 if ((vaddr_t)t != t) {
564 panic("%s: needs tweak", __func__);
565 }
566 return t;
567 }
568
569 int
570 seltrue(dev_t dev, int events, struct lwp *l)
571 {
572 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
573 }
574
575 void
576 selrecord(lwp_t *selector, struct selinfo *sip)
577 {
578 }
579
580 void
581 selinit(struct selinfo *sip)
582 {
583 }
584
585 void
586 selnotify(struct selinfo *sip, int events, long knhint)
587 {
588 }
589
590 void
591 seldestroy(struct selinfo *sip)
592 {
593 }
594
595 const char *
596 device_xname(device_t dv)
597 {
598 return "bogus0";
599 }
600
601 void
602 assert_sleepable(void)
603 {
604
605 /* always sleepable, although we should improve this */
606 }
607
608 int
609 devsw_attach(const char *devname, const struct bdevsw *bdev, int *bmajor,
610 const struct cdevsw *cdev, int *cmajor)
611 {
612
613 panic("%s: not implemented", __func__);
614 }
615
616 int
617 devsw_detach(const struct bdevsw *bdev, const struct cdevsw *cdev)
618 {
619
620 panic("%s: not implemented", __func__);
621 }
622