emul.c revision 1.5 1 /* $NetBSD: emul.c,v 1.5 2007/08/09 08:56: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 #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 void
76 panic(const char *fmt, ...)
77 {
78 va_list ap;
79
80 va_start(ap, fmt);
81 vprintf(fmt, ap);
82 va_end(ap);
83 printf("\n");
84 abort();
85 }
86
87 void
88 log(int level, const char *fmt, ...)
89 {
90 va_list ap;
91
92 va_start(ap, fmt);
93 vprintf(fmt, ap);
94 va_end(ap);
95 }
96
97 void
98 uprintf(const char *fmt, ...)
99 {
100 va_list ap;
101
102 va_start(ap, fmt);
103 vprintf(fmt, ap);
104 va_end(ap);
105 }
106
107 int
108 copyin(const void *uaddr, void *kaddr, size_t len)
109 {
110
111 memcpy(kaddr, uaddr, len);
112 return 0;
113 }
114
115 int
116 copyout(const void *kaddr, void *uaddr, size_t len)
117 {
118
119 memcpy(uaddr, kaddr, len);
120 return 0;
121 }
122
123 int
124 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
125 {
126
127 return copyinstr(kfaddr, kdaddr, len, done);
128 }
129
130 int
131 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
132 {
133
134 strlcpy(kaddr, uaddr, len);
135 *done = strlen(kaddr);
136 return 0;
137 }
138
139 int
140 uiomove(void *buf, size_t n, struct uio *uio)
141 {
142 struct iovec *iov;
143 uint8_t *b = buf;
144 size_t cnt;
145
146 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
147 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
148
149 if (buf == RUMP_UBC_MAGIC_WINDOW)
150 return rump_ubc_magic_uiomove(n, uio);
151
152 while (n && uio->uio_resid) {
153 iov = uio->uio_iov;
154 cnt = iov->iov_len;
155 if (cnt == 0) {
156 uio->uio_iov++;
157 uio->uio_iovcnt--;
158 continue;
159 }
160 if (cnt > n)
161 cnt = n;
162
163 if (uio->uio_rw == UIO_READ)
164 memcpy(iov->iov_base, b, cnt);
165 else
166 memcpy(b, iov->iov_base, cnt);
167
168 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
169 iov->iov_len -= cnt;
170 b += cnt;
171 uio->uio_resid -= cnt;
172 uio->uio_offset += cnt;
173 n -= cnt;
174 }
175
176 return 0;
177 }
178
179 void
180 uio_setup_sysspace(struct uio *uio)
181 {
182
183 uio->uio_vmspace = UIO_VMSPACE_SYS;
184 }
185
186 const struct bdevsw *
187 bdevsw_lookup(dev_t dev)
188 {
189
190 return (const struct bdevsw *)1;
191 }
192
193 devclass_t
194 device_class(device_t dev)
195 {
196
197 if (dev != root_device)
198 panic("%s: dev != root_device not supported", __func__);
199
200 return DV_DISK;
201 }
202
203 void
204 getmicrouptime(struct timeval *tvp)
205 {
206
207 rumpuser_gettimeofday(tvp);
208 }
209
210 int
211 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
212 volatile struct simplelock *slock)
213 {
214
215 panic("%s: not implemented", __func__);
216 }
217
218 void
219 wakeup(wchan_t ident)
220 {
221
222 printf("%s: not implemented\n", __func__);
223 }
224
225 void
226 malloc_type_attach(struct malloc_type *type)
227 {
228
229 return;
230 }
231
232 void
233 malloc_type_detach(struct malloc_type *type)
234 {
235
236 return;
237 }
238
239 void
240 nanotime(struct timespec *ts)
241 {
242 struct timeval tv;
243
244 rumpuser_gettimeofday(&tv);
245 TIMEVAL_TO_TIMESPEC(&tv, ts);
246 }
247
248 /* hooray for mick, so what if I do */
249 void
250 getnanotime(struct timespec *ts)
251 {
252
253 nanotime(ts);
254 }
255
256 void
257 microtime(struct timeval *tv)
258 {
259
260 rumpuser_gettimeofday(tv);
261 }
262
263 void
264 getmicrotime(struct timeval *tv)
265 {
266
267 rumpuser_gettimeofday(tv);
268 }
269
270 void
271 bdev_strategy(struct buf *bp)
272 {
273
274 panic("%s: not supported", __func__);
275 }
276
277 void
278 vn_syncer_remove_from_worklist(struct vnode *vp)
279 {
280
281 /* nada */
282 }
283
284 int
285 kthread_create(pri_t pri, int flags, struct cpu_info *ci,
286 void (*func)(void *), void *arg, lwp_t **newlp, const char *fmt, ...)
287 {
288
289 return 0;
290 }
291
292 void
293 workqueue_enqueue(struct workqueue *wq, struct work *wk0, struct cpu_info *ci)
294 {
295
296 }
297