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