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