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