emul.c revision 1.28 1 /* $NetBSD: emul.c,v 1.28 2008/02/15 13:07:02 ad 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/syslog.h>
38 #include <sys/namei.h>
39 #include <sys/kauth.h>
40 #include <sys/conf.h>
41 #include <sys/device.h>
42 #include <sys/queue.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/kthread.h>
46 #include <sys/cpu.h>
47 #include <sys/kmem.h>
48 #include <sys/poll.h>
49
50 #include <machine/stdarg.h>
51
52 #include <uvm/uvm_map.h>
53
54 #include "rump_private.h"
55 #include "rumpuser.h"
56
57 time_t time_second = 1;
58
59 kmutex_t proclist_mutex;
60 kmutex_t proclist_lock;
61 struct lwp lwp0;
62 struct vnode *rootvp;
63 struct device *root_device;
64 dev_t rootdev;
65 struct vm_map *kernel_map;
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 dovfsusermount = 1;
71 int hardclock_ticks;
72
73 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
74 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
75 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
76 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
77 MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
78
79 char hostname[MAXHOSTNAMELEN];
80 size_t hostnamelen;
81
82 u_long bufmem_valimit;
83 u_long bufmem_hiwater;
84 u_long bufmem_lowater;
85 u_long bufmem;
86 u_int nbuf;
87
88 const char *panicstr;
89 const char ostype[] = "NetBSD";
90 const char osrelease[] = "999"; /* paradroid 4evah */
91 const char kernel_ident[] = "RUMP-ROAST";
92 const char *domainname;
93 int domainnamelen;
94
95 const struct filterops seltrue_filtops;
96
97 void
98 panic(const char *fmt, ...)
99 {
100 va_list ap;
101
102 va_start(ap, fmt);
103 printf("panic: ");
104 vprintf(fmt, ap);
105 va_end(ap);
106 printf("\n");
107 abort();
108 }
109
110 void
111 log(int level, const char *fmt, ...)
112 {
113 va_list ap;
114
115 va_start(ap, fmt);
116 vprintf(fmt, ap);
117 va_end(ap);
118 }
119
120 void
121 uprintf(const char *fmt, ...)
122 {
123 va_list ap;
124
125 va_start(ap, fmt);
126 vprintf(fmt, ap);
127 va_end(ap);
128 }
129
130 void
131 printf_nolog(const char *fmt, ...)
132 {
133 va_list ap;
134
135 va_start(ap, fmt);
136 vprintf(fmt, ap);
137 va_end(ap);
138 }
139
140 void
141 aprint_normal(const char *fmt, ...)
142 {
143 va_list ap;
144
145 va_start(ap, fmt);
146 vprintf(fmt, ap);
147 va_end(ap);
148 }
149
150 int
151 copyin(const void *uaddr, void *kaddr, size_t len)
152 {
153
154 memcpy(kaddr, uaddr, len);
155 return 0;
156 }
157
158 int
159 copyout(const void *kaddr, void *uaddr, size_t len)
160 {
161
162 memcpy(uaddr, kaddr, len);
163 return 0;
164 }
165
166 int
167 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
168 {
169
170 return copyinstr(kfaddr, kdaddr, len, done);
171 }
172
173 int
174 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
175 {
176
177 strlcpy(kaddr, uaddr, len);
178 *done = strlen(kaddr);
179 return 0;
180 }
181
182 int
183 uiomove(void *buf, size_t n, struct uio *uio)
184 {
185 struct iovec *iov;
186 uint8_t *b = buf;
187 size_t cnt;
188 int rv;
189
190 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
191 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
192
193 /*
194 * See if rump ubc code claims the offset. This is of course
195 * a blatant violation of abstraction levels, but let's keep
196 * me simple & stupid for now.
197 */
198 if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
199 return rv;
200
201 while (n && uio->uio_resid) {
202 iov = uio->uio_iov;
203 cnt = iov->iov_len;
204 if (cnt == 0) {
205 uio->uio_iov++;
206 uio->uio_iovcnt--;
207 continue;
208 }
209 if (cnt > n)
210 cnt = n;
211
212 if (uio->uio_rw == UIO_READ)
213 memcpy(iov->iov_base, b, cnt);
214 else
215 memcpy(b, iov->iov_base, cnt);
216
217 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
218 iov->iov_len -= cnt;
219 b += cnt;
220 uio->uio_resid -= cnt;
221 uio->uio_offset += cnt;
222 n -= cnt;
223 }
224
225 return 0;
226 }
227
228 void
229 uio_setup_sysspace(struct uio *uio)
230 {
231
232 uio->uio_vmspace = UIO_VMSPACE_SYS;
233 }
234
235 const struct bdevsw *
236 bdevsw_lookup(dev_t dev)
237 {
238
239 return (const struct bdevsw *)1;
240 }
241
242 devclass_t
243 device_class(device_t dev)
244 {
245
246 if (dev != root_device)
247 panic("%s: dev != root_device not supported", __func__);
248
249 return DV_DISK;
250 }
251
252 void
253 getmicrouptime(struct timeval *tvp)
254 {
255 int error;
256
257 rumpuser_gettimeofday(tvp, &error);
258 }
259
260 void
261 malloc_type_attach(struct malloc_type *type)
262 {
263
264 return;
265 }
266
267 void
268 malloc_type_detach(struct malloc_type *type)
269 {
270
271 return;
272 }
273
274 void *
275 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
276 {
277 void *rv;
278
279 rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
280 if (rv && flags & M_ZERO)
281 memset(rv, 0, size);
282
283 return rv;
284 }
285
286 void
287 nanotime(struct timespec *ts)
288 {
289 struct timeval tv;
290 int error;
291
292 rumpuser_gettimeofday(&tv, &error);
293 TIMEVAL_TO_TIMESPEC(&tv, ts);
294 }
295
296 /* hooray for mick, so what if I do */
297 void
298 getnanotime(struct timespec *ts)
299 {
300
301 nanotime(ts);
302 }
303
304 void
305 microtime(struct timeval *tv)
306 {
307 int error;
308
309 rumpuser_gettimeofday(tv, &error);
310 }
311
312 void
313 getmicrotime(struct timeval *tv)
314 {
315 int error;
316
317 rumpuser_gettimeofday(tv, &error);
318 }
319
320 void
321 bdev_strategy(struct buf *bp)
322 {
323
324 panic("%s: not supported", __func__);
325 }
326
327 int
328 bdev_type(dev_t dev)
329 {
330
331 return D_DISK;
332 }
333
334 struct kthdesc {
335 void (*f)(void *);
336 void *arg;
337 struct lwp *mylwp;
338 };
339
340 static lwpid_t curlid = 2;
341
342 static void *
343 threadbouncer(void *arg)
344 {
345 struct kthdesc *k = arg;
346 void (*f)(void *);
347 void *thrarg;
348
349 f = k->f;
350 thrarg = k->arg;
351 rumpuser_set_curlwp(k->mylwp);
352 kmem_free(k, sizeof(struct kthdesc));
353
354 f(thrarg);
355 panic("unreachable, should kthread_exit()");
356 }
357
358 int
359 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
360 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
361 {
362 struct kthdesc *k;
363 struct lwp *l;
364 int rv;
365
366 #ifdef RUMP_WITHOUT_THREADS
367 /* XXX: fake it */
368 if (strcmp(fmt, "vrele") == 0)
369 return 0;
370 else
371 panic("threads not available, undef RUMP_WITHOUT_THREADS");
372 #endif
373
374 KASSERT(fmt != NULL);
375 if (ci != NULL)
376 panic("%s: bounded threads not supported", __func__);
377
378 k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
379 k->f = func;
380 k->arg = arg;
381 k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
382 rv = rumpuser_thread_create(threadbouncer, k);
383 if (rv)
384 return rv;
385
386 if (newlp)
387 *newlp = l;
388 return 0;
389 }
390
391 void
392 kthread_exit(int ecode)
393 {
394
395 rumpuser_thread_exit();
396 }
397
398 void
399 callout_init(callout_t *c, u_int flags)
400 {
401
402 panic("%s: not implemented", __func__);
403 }
404
405 void
406 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
407 {
408
409 panic("%s: not implemented", __func__);
410 }
411
412 bool
413 callout_stop(callout_t *c)
414 {
415
416 panic("%s: not implemented", __func__);
417 }
418
419 struct proc *
420 p_find(pid_t pid, uint flags)
421 {
422
423 panic("%s: not implemented", __func__);
424 }
425
426 struct pgrp *
427 pg_find(pid_t pid, uint flags)
428 {
429
430 panic("%s: not implemented", __func__);
431 }
432
433 void
434 kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
435 {
436
437 panic("%s: not implemented", __func__);
438 }
439
440 void
441 kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
442 {
443
444 panic("%s: not implemented", __func__);
445 }
446
447 int
448 pgid_in_session(struct proc *p, pid_t pg_id)
449 {
450
451 panic("%s: not implemented", __func__);
452 }
453
454 int
455 sigispending(struct lwp *l, int signo)
456 {
457
458 return 0;
459 }
460
461 void
462 knote_fdclose(struct lwp *l, int fd)
463 {
464
465 /* since we don't add knotes, we don't have to remove them */
466 }
467
468 int
469 seltrue_kqfilter(dev_t dev, struct knote *kn)
470 {
471
472 panic("%s: not implemented", __func__);
473 }
474
475 int
476 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
477 {
478 extern int hz;
479 int rv, error;
480
481 if (mtx)
482 mutex_exit(mtx);
483 rv = rumpuser_usleep(timeo * (1000000 / hz), &error);
484 if (mtx)
485 mutex_enter(mtx);
486
487 if (rv)
488 return error;
489
490 return 0;
491 }
492
493 void
494 suspendsched()
495 {
496
497 panic("%s: not implemented", __func__);
498 }
499