emul.c revision 1.17 1 /* $NetBSD: emul.c,v 1.17 2007/10/31 15:57:20 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/filedesc.h>
44 #include <sys/kthread.h>
45 #include <sys/cpu.h>
46 #include <sys/kmem.h>
47
48 #include <machine/stdarg.h>
49
50 #include <uvm/uvm_map.h>
51
52 #include "rump_private.h"
53 #include "rumpuser.h"
54
55 #ifdef __HAVE_TIMECOUNTER
56 time_t time_second = 1;
57 #else
58 volatile struct timeval time = { 1, 0 };
59 #endif
60
61 kmutex_t proclist_mutex;
62 kmutex_t proclist_lock;
63 struct lwp lwp0;
64 struct vnode *rootvp;
65 struct device *root_device;
66 dev_t rootdev;
67 struct vm_map *kernel_map;
68 int physmem;
69 int doing_shutdown;
70 int ncpu = 1;
71
72 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
73 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
74 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
75 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
76 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
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 void
90 panic(const char *fmt, ...)
91 {
92 va_list ap;
93
94 va_start(ap, fmt);
95 vprintf(fmt, ap);
96 va_end(ap);
97 printf("\n");
98 abort();
99 }
100
101 void
102 log(int level, const char *fmt, ...)
103 {
104 va_list ap;
105
106 va_start(ap, fmt);
107 vprintf(fmt, ap);
108 va_end(ap);
109 }
110
111 void
112 uprintf(const char *fmt, ...)
113 {
114 va_list ap;
115
116 va_start(ap, fmt);
117 vprintf(fmt, ap);
118 va_end(ap);
119 }
120
121 void
122 printf_nolog(const char *fmt, ...)
123 {
124 va_list ap;
125
126 va_start(ap, fmt);
127 vprintf(fmt, ap);
128 va_end(ap);
129 }
130
131 int
132 copyin(const void *uaddr, void *kaddr, size_t len)
133 {
134
135 memcpy(kaddr, uaddr, len);
136 return 0;
137 }
138
139 int
140 copyout(const void *kaddr, void *uaddr, size_t len)
141 {
142
143 memcpy(uaddr, kaddr, len);
144 return 0;
145 }
146
147 int
148 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
149 {
150
151 return copyinstr(kfaddr, kdaddr, len, done);
152 }
153
154 int
155 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
156 {
157
158 strlcpy(kaddr, uaddr, len);
159 *done = strlen(kaddr);
160 return 0;
161 }
162
163 int
164 uiomove(void *buf, size_t n, struct uio *uio)
165 {
166 struct iovec *iov;
167 uint8_t *b = buf;
168 size_t cnt;
169 int rv;
170
171 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
172 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
173
174 /*
175 * See if rump ubc code claims the offset. This is of course
176 * a blatant violation of abstraction levels, but let's keep
177 * me simple & stupid for now.
178 */
179 if (rump_ubc_magic_uiomove(buf, n, uio, &rv))
180 return rv;
181
182 while (n && uio->uio_resid) {
183 iov = uio->uio_iov;
184 cnt = iov->iov_len;
185 if (cnt == 0) {
186 uio->uio_iov++;
187 uio->uio_iovcnt--;
188 continue;
189 }
190 if (cnt > n)
191 cnt = n;
192
193 if (uio->uio_rw == UIO_READ)
194 memcpy(iov->iov_base, b, cnt);
195 else
196 memcpy(b, iov->iov_base, cnt);
197
198 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
199 iov->iov_len -= cnt;
200 b += cnt;
201 uio->uio_resid -= cnt;
202 uio->uio_offset += cnt;
203 n -= cnt;
204 }
205
206 return 0;
207 }
208
209 void
210 uio_setup_sysspace(struct uio *uio)
211 {
212
213 uio->uio_vmspace = UIO_VMSPACE_SYS;
214 }
215
216 const struct bdevsw *
217 bdevsw_lookup(dev_t dev)
218 {
219
220 return (const struct bdevsw *)1;
221 }
222
223 devclass_t
224 device_class(device_t dev)
225 {
226
227 if (dev != root_device)
228 panic("%s: dev != root_device not supported", __func__);
229
230 return DV_DISK;
231 }
232
233 void
234 getmicrouptime(struct timeval *tvp)
235 {
236 int error;
237
238 rumpuser_gettimeofday(tvp, &error);
239 }
240
241 void
242 malloc_type_attach(struct malloc_type *type)
243 {
244
245 return;
246 }
247
248 void
249 malloc_type_detach(struct malloc_type *type)
250 {
251
252 return;
253 }
254
255 void *
256 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
257 {
258 void *rv;
259
260 rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
261 if (rv && flags & M_ZERO)
262 memset(rv, 0, size);
263
264 return rv;
265 }
266
267 void
268 nanotime(struct timespec *ts)
269 {
270 struct timeval tv;
271 int error;
272
273 rumpuser_gettimeofday(&tv, &error);
274 TIMEVAL_TO_TIMESPEC(&tv, ts);
275 }
276
277 /* hooray for mick, so what if I do */
278 void
279 getnanotime(struct timespec *ts)
280 {
281
282 nanotime(ts);
283 }
284
285 void
286 microtime(struct timeval *tv)
287 {
288 int error;
289
290 rumpuser_gettimeofday(tv, &error);
291 }
292
293 void
294 getmicrotime(struct timeval *tv)
295 {
296 int error;
297
298 rumpuser_gettimeofday(tv, &error);
299 }
300
301 void
302 bdev_strategy(struct buf *bp)
303 {
304
305 panic("%s: not supported", __func__);
306 }
307
308 int
309 bdev_type(dev_t dev)
310 {
311
312 return D_DISK;
313 }
314
315 struct kthdesc {
316 void (*f)(void *);
317 void *arg;
318 struct lwp *mylwp;
319 };
320
321 static lwpid_t curlid = 2;
322
323 static void *
324 threadbouncer(void *arg)
325 {
326 struct kthdesc *k = arg;
327 void (*f)(void *);
328 void *thrarg;
329
330 f = k->f;
331 thrarg = k->arg;
332 rumpuser_set_curlwp(k->mylwp);
333 kmem_free(k, sizeof(struct kthdesc));
334
335 f(thrarg);
336 panic("unreachable, should kthread_exit()");
337 }
338
339 int
340 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
341 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
342 {
343 struct kthdesc *k;
344 struct lwp *l;
345 int rv;
346
347 KASSERT(fmt != NULL);
348 if (ci != NULL)
349 panic("%s: bounded threads not supported", __func__);
350
351 k = kmem_alloc(sizeof(struct kthdesc), KM_SLEEP);
352 k->f = func;
353 k->arg = arg;
354 k->mylwp = l = rump_setup_curlwp(0, curlid++, 0);
355 rv = rumpuser_thread_create(threadbouncer, k);
356 if (rv)
357 return rv;
358
359 if (newlp)
360 *newlp = l;
361 return 0;
362 }
363
364 void
365 kthread_exit(int ecode)
366 {
367
368 rumpuser_thread_exit();
369 }
370
371 void
372 callout_init(callout_t *c, u_int flags)
373 {
374
375 panic("%s: not implemented", __func__);
376 }
377
378 void
379 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
380 {
381
382 panic("%s: not implemented", __func__);
383 }
384
385 bool
386 callout_stop(callout_t *c)
387 {
388
389 panic("%s: not implemented", __func__);
390 }
391