emul.c revision 1.132 1 /* $NetBSD: emul.c,v 1.132 2010/04/21 20:07:02 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/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.132 2010/04/21 20:07:02 pooka Exp $");
32
33 #include <sys/param.h>
34 #include <sys/null.h>
35 #include <sys/vnode.h>
36 #include <sys/stat.h>
37 #include <sys/select.h>
38 #include <sys/syslog.h>
39 #include <sys/namei.h>
40 #include <sys/kauth.h>
41 #include <sys/conf.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/file.h>
45 #include <sys/filedesc.h>
46 #include <sys/cpu.h>
47 #include <sys/kmem.h>
48 #include <sys/poll.h>
49 #include <sys/timetc.h>
50 #include <sys/tprintf.h>
51 #include <sys/module.h>
52 #include <sys/tty.h>
53 #include <sys/reboot.h>
54 #include <sys/syscallvar.h>
55 #include <sys/xcall.h>
56
57 #include <dev/cons.h>
58
59 #include <rump/rumpuser.h>
60
61 #include <uvm/uvm_map.h>
62
63 #include "rump_private.h"
64
65 kmutex_t *proc_lock;
66 struct lwp lwp0;
67 struct vnode *rootvp;
68 dev_t rootdev = NODEV;
69 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
70 const int schedppq = 1;
71 int hardclock_ticks;
72 bool mp_online = false;
73 struct timeval boottime;
74 int cold = 1;
75 int boothowto = AB_SILENT;
76 struct tty *constty;
77
78 #define DEVSW_SIZE 255
79 const struct bdevsw *bdevsw0[DEVSW_SIZE]; /* XXX storage size */
80 const struct bdevsw **bdevsw = bdevsw0;
81 const int sys_cdevsws = DEVSW_SIZE;
82 int max_cdevsws = DEVSW_SIZE;
83
84 const struct cdevsw *cdevsw0[DEVSW_SIZE]; /* XXX storage size */
85 const struct cdevsw **cdevsw = cdevsw0;
86 const int sys_bdevsws = DEVSW_SIZE;
87 int max_bdevsws = DEVSW_SIZE;
88
89 struct devsw_conv devsw_conv0;
90 struct devsw_conv *devsw_conv = &devsw_conv0;
91 int max_devsw_convs = 0;
92 int mem_no = 2;
93
94 struct device *booted_device;
95 struct device *booted_wedge;
96 int booted_partition;
97
98 /* XXX: unused */
99 kmutex_t tty_lock;
100 krwlock_t exec_lock;
101
102 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
103
104 /* sparc doesn't sport constant page size */
105 #ifdef __sparc__
106 int nbpg = 4096;
107 #endif
108
109 struct loadavg averunnable = {
110 { 0 * FSCALE,
111 1 * FSCALE,
112 11 * FSCALE, },
113 FSCALE,
114 };
115
116 struct emul emul_netbsd = {
117 .e_name = "netbsd-rump",
118 .e_sysent = rump_sysent,
119 .e_vm_default_addr = uvm_default_mapaddr,
120 };
121
122 struct proc *
123 p_find(pid_t pid, uint flags)
124 {
125
126 panic("%s: not implemented", __func__);
127 }
128
129 struct pgrp *
130 pg_find(pid_t pid, uint flags)
131 {
132
133 panic("%s: not implemented", __func__);
134 }
135
136 int
137 pgid_in_session(struct proc *p, pid_t pg_id)
138 {
139
140 panic("%s: not implemented", __func__);
141 }
142
143 int
144 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
145 {
146 extern int hz;
147 int rv, error;
148 uint64_t sec, nsec;
149
150 if (mtx)
151 mutex_exit(mtx);
152
153 sec = timeo / hz;
154 nsec = (timeo % hz) * (1000000000 / hz);
155 rv = rumpuser_nanosleep(&sec, &nsec, &error);
156
157 if (mtx)
158 mutex_enter(mtx);
159
160 if (rv)
161 return error;
162
163 return 0;
164 }
165
166 void
167 lwp_unsleep(lwp_t *l, bool cleanup)
168 {
169
170 KASSERT(mutex_owned(l->l_mutex));
171
172 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
173 }
174
175 vaddr_t
176 calc_cache_size(struct vm_map *map, int pct, int va_pct)
177 {
178 paddr_t t;
179
180 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
181 if ((vaddr_t)t != t) {
182 panic("%s: needs tweak", __func__);
183 }
184 return t;
185 }
186
187 void
188 assert_sleepable(void)
189 {
190
191 /* always sleepable, although we should improve this */
192 }
193
194 int
195 proc_uidmatch(kauth_cred_t cred, kauth_cred_t target)
196 {
197
198 panic("%s: not implemented", __func__);
199 }
200
201 void
202 proc_crmod_enter(void)
203 {
204
205 panic("%s: not implemented", __func__);
206 }
207
208 void
209 proc_crmod_leave(kauth_cred_t c1, kauth_cred_t c2, bool sugid)
210 {
211
212 panic("%s: not implemented", __func__);
213 }
214
215 void
216 module_init_md(void)
217 {
218
219 /*
220 * Nothing for now. However, we should load the librump
221 * symbol table.
222 */
223 }
224
225 /* us and them, after all we're only ordinary seconds */
226 static void
227 rump_delay(unsigned int us)
228 {
229 uint64_t sec, nsec;
230 int error;
231
232 sec = us / 1000000;
233 nsec = (us % 1000000) * 1000;
234
235 if (__predict_false(sec != 0))
236 printf("WARNING: over 1s delay\n");
237
238 rumpuser_nanosleep(&sec, &nsec, &error);
239 }
240 void (*delay_func)(unsigned int) = rump_delay;
241
242 void
243 proc_sesshold(struct session *ss)
244 {
245
246 panic("proc_sesshold() impossible, session %p", ss);
247 }
248
249 void
250 proc_sessrele(struct session *ss)
251 {
252
253 panic("proc_sessrele() impossible, session %p", ss);
254 }
255
256 int
257 proc_vmspace_getref(struct proc *p, struct vmspace **vm)
258 {
259
260 /* XXX */
261 *vm = p->p_vmspace;
262 return 0;
263 }
264
265 int
266 ttycheckoutq(struct tty *tp, int wait)
267 {
268
269 return 1;
270 }
271
272 void
273 cnputc(int c)
274 {
275 int error;
276
277 rumpuser_putchar(c, &error);
278 }
279
280 void
281 cnflush(void)
282 {
283
284 /* done */
285 }
286
287 int
288 tputchar(int c, int flags, struct tty *tp)
289 {
290
291 cnputc(c);
292 return 0;
293 }
294
295 void
296 cpu_reboot(int howto, char *bootstr)
297 {
298
299 rump_reboot(howto);
300
301 /* this function is __dead, we must exit */
302 rumpuser_exit(0);
303 }
304