emul.c revision 1.11.4.3 1 /* $NetBSD: emul.c,v 1.11.4.3 2007/10/27 11:36:21 yamt 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
47 #include <machine/stdarg.h>
48
49 #include <uvm/uvm_map.h>
50
51 #include "rump_private.h"
52 #include "rumpuser.h"
53
54 #ifdef __HAVE_TIMECOUNTER
55 time_t time_second = 1;
56 #else
57 volatile struct timeval time = { 1, 0 };
58 #endif
59
60 kmutex_t proclist_mutex;
61 kmutex_t proclist_lock;
62 struct lwp lwp0;
63 struct vnode *rootvp;
64 struct device *root_device;
65 dev_t rootdev;
66 struct vm_map *kernel_map;
67 int physmem;
68 int doing_shutdown;
69
70 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
71 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
72 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
73 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
74 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
75
76 struct lwp *curlwp;
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
170 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
171 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
172
173 if (buf == RUMP_UBC_MAGIC_WINDOW)
174 return rump_ubc_magic_uiomove(n, uio);
175
176 while (n && uio->uio_resid) {
177 iov = uio->uio_iov;
178 cnt = iov->iov_len;
179 if (cnt == 0) {
180 uio->uio_iov++;
181 uio->uio_iovcnt--;
182 continue;
183 }
184 if (cnt > n)
185 cnt = n;
186
187 if (uio->uio_rw == UIO_READ)
188 memcpy(iov->iov_base, b, cnt);
189 else
190 memcpy(b, iov->iov_base, cnt);
191
192 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
193 iov->iov_len -= cnt;
194 b += cnt;
195 uio->uio_resid -= cnt;
196 uio->uio_offset += cnt;
197 n -= cnt;
198 }
199
200 return 0;
201 }
202
203 void
204 uio_setup_sysspace(struct uio *uio)
205 {
206
207 uio->uio_vmspace = UIO_VMSPACE_SYS;
208 }
209
210 const struct bdevsw *
211 bdevsw_lookup(dev_t dev)
212 {
213
214 return (const struct bdevsw *)1;
215 }
216
217 devclass_t
218 device_class(device_t dev)
219 {
220
221 if (dev != root_device)
222 panic("%s: dev != root_device not supported", __func__);
223
224 return DV_DISK;
225 }
226
227 void
228 getmicrouptime(struct timeval *tvp)
229 {
230 int error;
231
232 rumpuser_gettimeofday(tvp, &error);
233 }
234
235 int
236 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
237 volatile struct simplelock *slock)
238 {
239
240 panic("%s: not implemented", __func__);
241 }
242
243 void
244 wakeup(wchan_t ident)
245 {
246
247 }
248
249 void
250 malloc_type_attach(struct malloc_type *type)
251 {
252
253 return;
254 }
255
256 void
257 malloc_type_detach(struct malloc_type *type)
258 {
259
260 return;
261 }
262
263 void *
264 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
265 {
266 void *rv;
267
268 rv = rumpuser_malloc(size, (flags & (M_CANFAIL | M_NOWAIT)) != 0);
269 if (rv && flags & M_ZERO)
270 memset(rv, 0, size);
271
272 return rv;
273 }
274
275 void
276 nanotime(struct timespec *ts)
277 {
278 struct timeval tv;
279 int error;
280
281 rumpuser_gettimeofday(&tv, &error);
282 TIMEVAL_TO_TIMESPEC(&tv, ts);
283 }
284
285 /* hooray for mick, so what if I do */
286 void
287 getnanotime(struct timespec *ts)
288 {
289
290 nanotime(ts);
291 }
292
293 void
294 microtime(struct timeval *tv)
295 {
296 int error;
297
298 rumpuser_gettimeofday(tv, &error);
299 }
300
301 void
302 getmicrotime(struct timeval *tv)
303 {
304 int error;
305
306 rumpuser_gettimeofday(tv, &error);
307 }
308
309 void
310 bdev_strategy(struct buf *bp)
311 {
312
313 panic("%s: not supported", __func__);
314 }
315
316 int
317 bdev_type(dev_t dev)
318 {
319
320 return D_DISK;
321 }
322
323 int
324 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
325 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
326 {
327
328 return 0;
329 }
330
331 void
332 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
333 {
334
335 }
336
337 void
338 callout_init(callout_t *c, u_int flags)
339 {
340
341 }
342
343 void
344 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
345 {
346
347 panic("%s: not implemented", __func__);
348 }
349
350 bool
351 callout_stop(callout_t *c)
352 {
353
354 panic("%s: not implemented", __func__);
355 }
356