emul.c revision 1.15 1 /* $NetBSD: emul.c,v 1.15 2007/10/19 12:16:47 ad 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
69 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
70 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
71 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
72 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
73 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
74
75 struct lwp *curlwp;
76
77 char hostname[MAXHOSTNAMELEN];
78 size_t hostnamelen;
79
80 u_long bufmem_valimit;
81 u_long bufmem_hiwater;
82 u_long bufmem_lowater;
83 u_long bufmem;
84 u_int nbuf;
85
86 const char *panicstr;
87
88 void
89 panic(const char *fmt, ...)
90 {
91 va_list ap;
92
93 va_start(ap, fmt);
94 vprintf(fmt, ap);
95 va_end(ap);
96 printf("\n");
97 abort();
98 }
99
100 void
101 log(int level, const char *fmt, ...)
102 {
103 va_list ap;
104
105 va_start(ap, fmt);
106 vprintf(fmt, ap);
107 va_end(ap);
108 }
109
110 void
111 uprintf(const char *fmt, ...)
112 {
113 va_list ap;
114
115 va_start(ap, fmt);
116 vprintf(fmt, ap);
117 va_end(ap);
118 }
119
120 int
121 copyin(const void *uaddr, void *kaddr, size_t len)
122 {
123
124 memcpy(kaddr, uaddr, len);
125 return 0;
126 }
127
128 int
129 copyout(const void *kaddr, void *uaddr, size_t len)
130 {
131
132 memcpy(uaddr, kaddr, len);
133 return 0;
134 }
135
136 int
137 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
138 {
139
140 return copyinstr(kfaddr, kdaddr, len, done);
141 }
142
143 int
144 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
145 {
146
147 strlcpy(kaddr, uaddr, len);
148 *done = strlen(kaddr);
149 return 0;
150 }
151
152 int
153 uiomove(void *buf, size_t n, struct uio *uio)
154 {
155 struct iovec *iov;
156 uint8_t *b = buf;
157 size_t cnt;
158
159 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
160 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
161
162 if (buf == RUMP_UBC_MAGIC_WINDOW)
163 return rump_ubc_magic_uiomove(n, uio);
164
165 while (n && uio->uio_resid) {
166 iov = uio->uio_iov;
167 cnt = iov->iov_len;
168 if (cnt == 0) {
169 uio->uio_iov++;
170 uio->uio_iovcnt--;
171 continue;
172 }
173 if (cnt > n)
174 cnt = n;
175
176 if (uio->uio_rw == UIO_READ)
177 memcpy(iov->iov_base, b, cnt);
178 else
179 memcpy(b, iov->iov_base, cnt);
180
181 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
182 iov->iov_len -= cnt;
183 b += cnt;
184 uio->uio_resid -= cnt;
185 uio->uio_offset += cnt;
186 n -= cnt;
187 }
188
189 return 0;
190 }
191
192 void
193 uio_setup_sysspace(struct uio *uio)
194 {
195
196 uio->uio_vmspace = UIO_VMSPACE_SYS;
197 }
198
199 const struct bdevsw *
200 bdevsw_lookup(dev_t dev)
201 {
202
203 return (const struct bdevsw *)1;
204 }
205
206 devclass_t
207 device_class(device_t dev)
208 {
209
210 if (dev != root_device)
211 panic("%s: dev != root_device not supported", __func__);
212
213 return DV_DISK;
214 }
215
216 void
217 getmicrouptime(struct timeval *tvp)
218 {
219 int error;
220
221 rumpuser_gettimeofday(tvp, &error);
222 }
223
224 int
225 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
226 volatile struct simplelock *slock)
227 {
228
229 panic("%s: not implemented", __func__);
230 }
231
232 void
233 wakeup(wchan_t ident)
234 {
235
236 }
237
238 void
239 malloc_type_attach(struct malloc_type *type)
240 {
241
242 return;
243 }
244
245 void
246 malloc_type_detach(struct malloc_type *type)
247 {
248
249 return;
250 }
251
252 void *
253 __wrap_malloc(unsigned long size, struct malloc_type *type, int flags)
254 {
255 void *rv;
256
257 rv = rumpuser_malloc(size, flags * (M_CANFAIL | M_NOWAIT));
258 if (rv && flags & M_ZERO)
259 memset(rv, 0, size);
260
261 return rv;
262 }
263
264 void
265 nanotime(struct timespec *ts)
266 {
267 struct timeval tv;
268 int error;
269
270 rumpuser_gettimeofday(&tv, &error);
271 TIMEVAL_TO_TIMESPEC(&tv, ts);
272 }
273
274 /* hooray for mick, so what if I do */
275 void
276 getnanotime(struct timespec *ts)
277 {
278
279 nanotime(ts);
280 }
281
282 void
283 microtime(struct timeval *tv)
284 {
285 int error;
286
287 rumpuser_gettimeofday(tv, &error);
288 }
289
290 void
291 getmicrotime(struct timeval *tv)
292 {
293 int error;
294
295 rumpuser_gettimeofday(tv, &error);
296 }
297
298 void
299 bdev_strategy(struct buf *bp)
300 {
301
302 panic("%s: not supported", __func__);
303 }
304
305 int
306 bdev_type(dev_t dev)
307 {
308
309 return D_DISK;
310 }
311
312 int
313 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
314 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
315 {
316
317 return 0;
318 }
319
320 void
321 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
322 {
323
324 }
325
326 void
327 callout_init(callout_t *c, u_int flags)
328 {
329
330 }
331
332 void
333 callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
334 {
335
336 panic("%s: not implemented", __func__);
337 }
338
339 bool
340 callout_stop(callout_t *c)
341 {
342
343 panic("%s: not implemented", __func__);
344 }
345