emul.c revision 1.4 1 /* $NetBSD: emul.c,v 1.4 2007/08/08 13:12:08 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
43 #include <machine/cpu.h>
44 #include <machine/stdarg.h>
45
46 #include "rump.h"
47 #include "rumpuser.h"
48
49 time_t time_second = 1;
50
51 kmutex_t proclist_mutex;
52 struct lwp lwp0;
53 struct vnode *rootvp;
54 struct device *root_device;
55
56 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount struct");
57 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
58 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
59 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
60 MALLOC_DEFINE(M_VNODE, "vnodes", "Dynamically allocated vnodes");
61
62 struct lwp *curlwp;
63
64 char hostname[MAXHOSTNAMELEN];
65 size_t hostnamelen;
66
67 void
68 panic(const char *fmt, ...)
69 {
70 va_list ap;
71
72 va_start(ap, fmt);
73 vprintf(fmt, ap);
74 va_end(ap);
75 printf("\n");
76 abort();
77 }
78
79 void
80 log(int level, const char *fmt, ...)
81 {
82 va_list ap;
83
84 va_start(ap, fmt);
85 vprintf(fmt, ap);
86 va_end(ap);
87 }
88
89 void
90 uprintf(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 int
100 copyin(const void *uaddr, void *kaddr, size_t len)
101 {
102
103 memcpy(kaddr, uaddr, len);
104 return 0;
105 }
106
107 int
108 copyout(const void *kaddr, void *uaddr, size_t len)
109 {
110
111 memcpy(uaddr, kaddr, len);
112 return 0;
113 }
114
115 int
116 copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done)
117 {
118
119 return copyinstr(kfaddr, kdaddr, len, done);
120 }
121
122 int
123 copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done)
124 {
125
126 strlcpy(kaddr, uaddr, len);
127 *done = strlen(kaddr);
128 return 0;
129 }
130
131 int
132 uiomove(void *buf, size_t n, struct uio *uio)
133 {
134 struct iovec *iov;
135 uint8_t *b = buf;
136 size_t cnt;
137
138 if (uio->uio_vmspace != UIO_VMSPACE_SYS)
139 panic("%s: vmspace != UIO_VMSPACE_SYS", __func__);
140
141 if (buf == RUMP_UBC_MAGIC_WINDOW)
142 return rump_ubc_magic_uiomove(n, uio);
143
144 while (n && uio->uio_resid) {
145 iov = uio->uio_iov;
146 cnt = iov->iov_len;
147 if (cnt == 0) {
148 uio->uio_iov++;
149 uio->uio_iovcnt--;
150 continue;
151 }
152 if (cnt > n)
153 cnt = n;
154
155 if (uio->uio_rw == UIO_READ)
156 memcpy(iov->iov_base, b, cnt);
157 else
158 memcpy(b, iov->iov_base, cnt);
159
160 iov->iov_base = (uint8_t *)iov->iov_base + cnt;
161 iov->iov_len -= cnt;
162 b += cnt;
163 uio->uio_resid -= cnt;
164 uio->uio_offset += cnt;
165 n -= cnt;
166 }
167
168 return 0;
169 }
170
171 void
172 uio_setup_sysspace(struct uio *uio)
173 {
174
175 uio->uio_vmspace = UIO_VMSPACE_SYS;
176 }
177
178 const struct bdevsw *
179 bdevsw_lookup(dev_t dev)
180 {
181
182 return (const struct bdevsw *)1;
183 }
184
185 devclass_t
186 device_class(device_t dev)
187 {
188
189 if (dev != root_device)
190 panic("%s: dev != root_device not supported", __func__);
191
192 return DV_DISK;
193 }
194
195 void
196 getmicrouptime(struct timeval *tvp)
197 {
198
199 rumpuser_gettimeofday(tvp);
200 }
201
202 int
203 ltsleep(wchan_t ident, pri_t prio, const char *wmesg, int timo,
204 volatile struct simplelock *slock)
205 {
206
207 panic("%s: not implemented", __func__);
208 }
209
210 void
211 malloc_type_attach(struct malloc_type *type)
212 {
213
214 return;
215 }
216
217 void
218 malloc_type_detach(struct malloc_type *type)
219 {
220
221 return;
222 }
223
224 void
225 nanotime(struct timespec *ts)
226 {
227 struct timeval tv;
228
229 rumpuser_gettimeofday(&tv);
230 TIMEVAL_TO_TIMESPEC(&tv, ts);
231 }
232
233 /* hooray for mick, so what if I do */
234 void
235 getnanotime(struct timespec *ts)
236 {
237
238 nanotime(ts);
239 }
240
241 void
242 microtime(struct timeval *tv)
243 {
244
245 rumpuser_gettimeofday(tv);
246 }
247
248 void
249 bdev_strategy(struct buf *bp)
250 {
251
252 panic("%s: not supported", __func__);
253 }
254