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