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