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