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