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