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