emul.c revision 1.11.2.2 1 1.11.2.2 matt /* $NetBSD: emul.c,v 1.11.2.2 2008/01/09 01:57:59 matt Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by Google Summer of Code.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.9 pooka #define malloc(a,b,c) __wrap_malloc(a,b,c)
31 1.9 pooka
32 1.1 pooka #include <sys/param.h>
33 1.1 pooka #include <sys/malloc.h>
34 1.1 pooka #include <sys/null.h>
35 1.1 pooka #include <sys/vnode.h>
36 1.1 pooka #include <sys/stat.h>
37 1.1 pooka #include <sys/syslog.h>
38 1.1 pooka #include <sys/namei.h>
39 1.1 pooka #include <sys/kauth.h>
40 1.1 pooka #include <sys/conf.h>
41 1.1 pooka #include <sys/device.h>
42 1.1 pooka #include <sys/queue.h>
43 1.11.2.2 matt #include <sys/file.h>
44 1.1 pooka #include <sys/filedesc.h>
45 1.5 pooka #include <sys/kthread.h>
46 1.11.2.1 matt #include <sys/cpu.h>
47 1.11.2.1 matt #include <sys/kmem.h>
48 1.11.2.2 matt #include <sys/poll.h>
49 1.1 pooka
50 1.1 pooka #include <machine/stdarg.h>
51 1.1 pooka
52 1.8 pooka #include <uvm/uvm_map.h>
53 1.8 pooka
54 1.10 pooka #include "rump_private.h"
55 1.1 pooka #include "rumpuser.h"
56 1.1 pooka
57 1.11.2.1 matt #ifdef __HAVE_TIMECOUNTER
58 1.1 pooka time_t time_second = 1;
59 1.11.2.1 matt #else
60 1.11.2.1 matt volatile struct timeval time = { 1, 0 };
61 1.11.2.1 matt #endif
62 1.1 pooka
63 1.1 pooka kmutex_t proclist_mutex;
64 1.7 pooka kmutex_t proclist_lock;
65 1.1 pooka struct lwp lwp0;
66 1.1 pooka struct vnode *rootvp;
67 1.1 pooka struct device *root_device;
68 1.5 pooka dev_t rootdev;
69 1.8 pooka struct vm_map *kernel_map;
70 1.8 pooka int physmem;
71 1.11.2.1 matt int doing_shutdown;
72 1.11.2.1 matt int ncpu = 1;
73 1.11.2.2 matt const int schedppq = 1;
74 1.1 pooka
75 1.1 pooka MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
76 1.1 pooka MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
77 1.1 pooka MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
78 1.1 pooka MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
79 1.1 pooka MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
80 1.11.2.2 matt MALLOC_DEFINE(M_KEVENT, "kevent", "kevents/knotes");
81 1.1 pooka
82 1.1 pooka char hostname[MAXHOSTNAMELEN];
83 1.1 pooka size_t hostnamelen;
84 1.1 pooka
85 1.5 pooka u_long bufmem_valimit;
86 1.5 pooka u_long bufmem_hiwater;
87 1.5 pooka u_long bufmem_lowater;
88 1.5 pooka u_long bufmem;
89 1.5 pooka u_int nbuf;
90 1.5 pooka
91 1.6 pooka const char *panicstr;
92 1.6 pooka
93 1.11.2.2 matt const struct filterops seltrue_filtops;
94 1.11.2.2 matt
95 1.1 pooka void
96 1.1 pooka panic(const char *fmt, ...)
97 1.1 pooka {
98 1.1 pooka va_list ap;
99 1.1 pooka
100 1.1 pooka va_start(ap, fmt);
101 1.11.2.2 matt printf("panic: ");
102 1.1 pooka vprintf(fmt, ap);
103 1.1 pooka va_end(ap);
104 1.1 pooka printf("\n");
105 1.1 pooka abort();
106 1.1 pooka }
107 1.1 pooka
108 1.1 pooka void
109 1.1 pooka log(int level, const char *fmt, ...)
110 1.1 pooka {
111 1.1 pooka va_list ap;
112 1.1 pooka
113 1.1 pooka va_start(ap, fmt);
114 1.1 pooka vprintf(fmt, ap);
115 1.1 pooka va_end(ap);
116 1.1 pooka }
117 1.1 pooka
118 1.1 pooka void
119 1.1 pooka uprintf(const char *fmt, ...)
120 1.1 pooka {
121 1.1 pooka va_list ap;
122 1.1 pooka
123 1.1 pooka va_start(ap, fmt);
124 1.1 pooka vprintf(fmt, ap);
125 1.1 pooka va_end(ap);
126 1.1 pooka }
127 1.1 pooka
128 1.11.2.1 matt void
129 1.11.2.1 matt printf_nolog(const char *fmt, ...)
130 1.11.2.1 matt {
131 1.11.2.1 matt va_list ap;
132 1.11.2.1 matt
133 1.11.2.1 matt va_start(ap, fmt);
134 1.11.2.1 matt vprintf(fmt, ap);
135 1.11.2.1 matt va_end(ap);
136 1.11.2.1 matt }
137 1.11.2.1 matt
138 1.1 pooka int
139 1.1 pooka copyin(const void *uaddr, void *kaddr, size_t len)
140 1.1 pooka {
141 1.1 pooka
142 1.1 pooka memcpy(kaddr, uaddr, len);
143 1.1 pooka return 0;
144 1.1 pooka }
145 1.1 pooka
146 1.1 pooka int
147 1.1 pooka copyout(const void *kaddr, void *uaddr, size_t len)
148 1.1 pooka {
149 1.1 pooka
150 1.1 pooka memcpy(uaddr, kaddr, len);
151 1.1 pooka return 0;
152 1.1 pooka }
153 1.1 pooka
154 1.1 pooka int
155 1.1 pooka copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
156 1.1 pooka {
157 1.1 pooka
158 1.1 pooka return copyinstr(kfaddr, kdaddr, len, done);
159 1.1 pooka }
160 1.1 pooka
161 1.1 pooka int
162 1.1 pooka copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
163 1.1 pooka {
164 1.1 pooka
165 1.1 pooka strlcpy(kaddr, uaddr, len);
166 1.1 pooka *done = strlen(kaddr);
167 1.1 pooka return 0;
168 1.1 pooka }
169 1.1 pooka
170 1.1 pooka int
171 1.1 pooka uiomove(void *buf, size_t n, struct uio *uio)
172 1.1 pooka {
173 1.1 pooka struct iovec *iov;
174 1.1 pooka uint8_t *b = buf;
175 1.1 pooka size_t cnt;
176 1.11.2.1 matt int rv;
177 1.1 pooka
178 1.1 pooka if (uio->uio_vmspace != UIO_VMSPACE_SYS)
179 1.1 pooka panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
180 1.1 pooka
181 1.11.2.1 matt /*
182 1.11.2.1 matt * See if rump ubc code claims the offset. This is of course
183 1.11.2.1 matt * a blatant violation of abstraction levels, but let's keep
184 1.11.2.1 matt * me simple & stupid for now.
185 1.11.2.1 matt */
186 1.11.2.1 matt if (rump_ubc_magic_uiomove(buf, n, uio, &rv, NULL))
187 1.11.2.1 matt return rv;
188 1.1 pooka
189 1.1 pooka while (n && uio->uio_resid) {
190 1.1 pooka iov = uio->uio_iov;
191 1.1 pooka cnt = iov->iov_len;
192 1.1 pooka if (cnt == 0) {
193 1.1 pooka uio->uio_iov++;
194 1.1 pooka uio->uio_iovcnt--;
195 1.1 pooka continue;
196 1.1 pooka }
197 1.1 pooka if (cnt > n)
198 1.1 pooka cnt = n;
199 1.1 pooka
200 1.1 pooka if (uio->uio_rw == UIO_READ)
201 1.1 pooka memcpy(iov->iov_base, b, cnt);
202 1.1 pooka else
203 1.1 pooka memcpy(b, iov->iov_base, cnt);
204 1.1 pooka
205 1.1 pooka iov->iov_base = (uint8_t *)iov->iov_base + cnt;
206 1.1 pooka iov->iov_len -= cnt;
207 1.1 pooka b += cnt;
208 1.1 pooka uio->uio_resid -= cnt;
209 1.1 pooka uio->uio_offset += cnt;
210 1.1 pooka n -= cnt;
211 1.1 pooka }
212 1.1 pooka
213 1.1 pooka return 0;
214 1.1 pooka }
215 1.1 pooka
216 1.1 pooka void
217 1.1 pooka uio_setup_sysspace(struct uio *uio)
218 1.1 pooka {
219 1.1 pooka
220 1.1 pooka uio->uio_vmspace = UIO_VMSPACE_SYS;
221 1.1 pooka }
222 1.1 pooka
223 1.1 pooka const struct bdevsw *
224 1.1 pooka bdevsw_lookup(dev_t dev)
225 1.1 pooka {
226 1.1 pooka
227 1.1 pooka return (const struct bdevsw *)1;
228 1.1 pooka }
229 1.1 pooka
230 1.1 pooka devclass_t
231 1.1 pooka device_class(device_t dev)
232 1.1 pooka {
233 1.1 pooka
234 1.1 pooka if (dev != root_device)
235 1.1 pooka panic("%s: dev != root_device not supported", __func__);
236 1.1 pooka
237 1.1 pooka return DV_DISK;
238 1.1 pooka }
239 1.1 pooka
240 1.1 pooka void
241 1.1 pooka getmicrouptime(struct timeval *tvp)
242 1.1 pooka {
243 1.11.2.1 matt int error;
244 1.1 pooka
245 1.11.2.1 matt rumpuser_gettimeofday(tvp, &error);
246 1.5 pooka }
247 1.5 pooka
248 1.5 pooka void
249 1.1 pooka malloc_type_attach(struct malloc_type *type)
250 1.1 pooka {
251 1.1 pooka
252 1.1 pooka return;
253 1.1 pooka }
254 1.1 pooka
255 1.1 pooka void
256 1.1 pooka malloc_type_detach(struct malloc_type *type)
257 1.1 pooka {
258 1.1 pooka
259 1.1 pooka return;
260 1.1 pooka }
261 1.1 pooka
262 1.9 pooka void *
263 1.9 pooka __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
264 1.9 pooka {
265 1.9 pooka void *rv;
266 1.9 pooka
267 1.11.2.1 matt rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
268 1.9 pooka if (rv && flags & M_ZERO)
269 1.9 pooka memset(rv, 0, size);
270 1.9 pooka
271 1.9 pooka return rv;
272 1.9 pooka }
273 1.9 pooka
274 1.1 pooka void
275 1.1 pooka nanotime(struct timespec *ts)
276 1.1 pooka {
277 1.1 pooka struct timeval tv;
278 1.11.2.1 matt int error;
279 1.1 pooka
280 1.11.2.1 matt rumpuser_gettimeofday(&tv, &error);
281 1.1 pooka TIMEVAL_TO_TIMESPEC(&tv, ts);
282 1.1 pooka }
283 1.1 pooka
284 1.1 pooka /* hooray for mick, so what if I do */
285 1.1 pooka void
286 1.1 pooka getnanotime(struct timespec *ts)
287 1.1 pooka {
288 1.1 pooka
289 1.1 pooka nanotime(ts);
290 1.1 pooka }
291 1.1 pooka
292 1.1 pooka void
293 1.2 pooka microtime(struct timeval *tv)
294 1.2 pooka {
295 1.11.2.1 matt int error;
296 1.2 pooka
297 1.11.2.1 matt rumpuser_gettimeofday(tv, &error);
298 1.2 pooka }
299 1.2 pooka
300 1.2 pooka void
301 1.5 pooka getmicrotime(struct timeval *tv)
302 1.5 pooka {
303 1.11.2.1 matt int error;
304 1.5 pooka
305 1.11.2.1 matt rumpuser_gettimeofday(tv, &error);
306 1.5 pooka }
307 1.5 pooka
308 1.5 pooka void
309 1.1 pooka bdev_strategy(struct buf *bp)
310 1.1 pooka {
311 1.1 pooka
312 1.1 pooka panic("%s: not supported", __func__);
313 1.1 pooka }
314 1.5 pooka
315 1.8 pooka int
316 1.8 pooka bdev_type(dev_t dev)
317 1.5 pooka {
318 1.5 pooka
319 1.8 pooka return D_DISK;
320 1.5 pooka }
321 1.5 pooka
322 1.11.2.1 matt struct kthdesc {
323 1.11.2.1 matt void (*f)(void *);
324 1.11.2.1 matt void *arg;
325 1.11.2.1 matt struct lwp *mylwp;
326 1.11.2.1 matt };
327 1.11.2.1 matt
328 1.11.2.1 matt static lwpid_t curlid = 2;
329 1.11.2.1 matt
330 1.11.2.1 matt static void *
331 1.11.2.1 matt threadbouncer(void *arg)
332 1.11.2.1 matt {
333 1.11.2.1 matt struct kthdesc *k = arg;
334 1.11.2.1 matt void (*f)(void *);
335 1.11.2.1 matt void *thrarg;
336 1.11.2.1 matt
337 1.11.2.1 matt f = k->f;
338 1.11.2.1 matt thrarg = k->arg;
339 1.11.2.1 matt rumpuser_set_curlwp(k->mylwp);
340 1.11.2.1 matt kmem_free(k, sizeof(struct kthdesc));
341 1.11.2.1 matt
342 1.11.2.1 matt f(thrarg);
343 1.11.2.1 matt panic("unreachable, should kthread_exit()");
344 1.11.2.1 matt }
345 1.11.2.1 matt
346 1.5 pooka int
347 1.5 pooka kthread_create(pri_t pri, int flags, struct cpu_info *ci,
348 1.5 pooka void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
349 1.5 pooka {
350 1.11.2.1 matt struct kthdesc *k;
351 1.11.2.1 matt struct lwp *l;
352 1.11.2.1 matt int rv;
353 1.11.2.1 matt
354 1.11.2.1 matt KASSERT(fmt != NULL);
355 1.11.2.1 matt if (ci != NULL)
356 1.11.2.1 matt panic("%s: bounded threads not supported", __func__);
357 1.11.2.1 matt
358 1.11.2.1 matt k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
359 1.11.2.1 matt k->f = func;
360 1.11.2.1 matt k->arg = arg;
361 1.11.2.1 matt k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
362 1.11.2.1 matt rv = rumpuser_thread_create(threadbouncer, k);
363 1.11.2.1 matt if (rv)
364 1.11.2.1 matt return rv;
365 1.5 pooka
366 1.11.2.1 matt if (newlp)
367 1.11.2.1 matt *newlp = l;
368 1.5 pooka return 0;
369 1.5 pooka }
370 1.5 pooka
371 1.5 pooka void
372 1.11.2.1 matt kthread_exit(int ecode)
373 1.5 pooka {
374 1.5 pooka
375 1.11.2.1 matt rumpuser_thread_exit();
376 1.5 pooka }
377 1.11 pooka
378 1.11 pooka void
379 1.11 pooka callout_init(callout_t *c, u_int flags)
380 1.11 pooka {
381 1.11 pooka
382 1.11.2.1 matt panic("%s: not implemented", __func__);
383 1.11 pooka }
384 1.11 pooka
385 1.11 pooka void
386 1.11 pooka callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
387 1.11 pooka {
388 1.11 pooka
389 1.11 pooka panic("%s: not implemented", __func__);
390 1.11 pooka }
391 1.11 pooka
392 1.11 pooka bool
393 1.11 pooka callout_stop(callout_t *c)
394 1.11 pooka {
395 1.11 pooka
396 1.11 pooka panic("%s: not implemented", __func__);
397 1.11 pooka }
398 1.11.2.2 matt
399 1.11.2.2 matt struct proc *
400 1.11.2.2 matt p_find(pid_t pid, uint flags)
401 1.11.2.2 matt {
402 1.11.2.2 matt
403 1.11.2.2 matt panic("%s: not implemented", __func__);
404 1.11.2.2 matt }
405 1.11.2.2 matt
406 1.11.2.2 matt struct pgrp *
407 1.11.2.2 matt pg_find(pid_t pid, uint flags)
408 1.11.2.2 matt {
409 1.11.2.2 matt
410 1.11.2.2 matt panic("%s: not implemented", __func__);
411 1.11.2.2 matt }
412 1.11.2.2 matt
413 1.11.2.2 matt void
414 1.11.2.2 matt kpsignal(struct proc *p, ksiginfo_t *ksi, void *data)
415 1.11.2.2 matt {
416 1.11.2.2 matt
417 1.11.2.2 matt panic("%s: not implemented", __func__);
418 1.11.2.2 matt }
419 1.11.2.2 matt
420 1.11.2.2 matt void
421 1.11.2.2 matt kpgsignal(struct pgrp *pgrp, ksiginfo_t *ksi, void *data, int checkctty)
422 1.11.2.2 matt {
423 1.11.2.2 matt
424 1.11.2.2 matt panic("%s: not implemented", __func__);
425 1.11.2.2 matt }
426 1.11.2.2 matt
427 1.11.2.2 matt int
428 1.11.2.2 matt pgid_in_session(struct proc *p, pid_t pg_id)
429 1.11.2.2 matt {
430 1.11.2.2 matt
431 1.11.2.2 matt panic("%s: not implemented", __func__);
432 1.11.2.2 matt }
433 1.11.2.2 matt
434 1.11.2.2 matt int
435 1.11.2.2 matt sigispending(struct lwp *l, int signo)
436 1.11.2.2 matt {
437 1.11.2.2 matt
438 1.11.2.2 matt return 0;
439 1.11.2.2 matt }
440 1.11.2.2 matt
441 1.11.2.2 matt void
442 1.11.2.2 matt knote_fdclose(struct lwp *l, int fd)
443 1.11.2.2 matt {
444 1.11.2.2 matt
445 1.11.2.2 matt /* since we don't add knotes, we don't have to remove them */
446 1.11.2.2 matt }
447 1.11.2.2 matt
448 1.11.2.2 matt int
449 1.11.2.2 matt seltrue_kqfilter(dev_t dev, struct knote *kn)
450 1.11.2.2 matt {
451 1.11.2.2 matt
452 1.11.2.2 matt panic("%s: not implemented", __func__);
453 1.11.2.2 matt }
454 1.11.2.2 matt
455 1.11.2.2 matt int
456 1.11.2.2 matt kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
457 1.11.2.2 matt {
458 1.11.2.2 matt extern int hz;
459 1.11.2.2 matt int rv, error;
460 1.11.2.2 matt
461 1.11.2.2 matt if (mtx)
462 1.11.2.2 matt mutex_exit(mtx);
463 1.11.2.2 matt rv = rumpuser_usleep(timeo * (1000000 / hz), &error);
464 1.11.2.2 matt if (mtx)
465 1.11.2.2 matt mutex_enter(mtx);
466 1.11.2.2 matt
467 1.11.2.2 matt if (rv)
468 1.11.2.2 matt return error;
469 1.11.2.2 matt
470 1.11.2.2 matt return 0;
471 1.11.2.2 matt }
472