emul.c revision 1.121.2.3 1 /* $NetBSD: emul.c,v 1.121.2.3 2010/10/22 07:22:48 uebayasi 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.121.2.3 2010/10/22 07:22:48 uebayasi 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/cpu.h>
46 #include <sys/kmem.h>
47 #include <sys/poll.h>
48 #include <sys/timetc.h>
49 #include <sys/tprintf.h>
50 #include <sys/module.h>
51 #include <sys/tty.h>
52 #include <sys/reboot.h>
53 #include <sys/syscallvar.h>
54 #include <sys/xcall.h>
55
56 #include <dev/cons.h>
57
58 #include <rump/rumpuser.h>
59
60 #include <uvm/uvm_map.h>
61
62 #include "rump_private.h"
63
64 struct lwp lwp0;
65 struct vnode *rootvp;
66 dev_t rootdev = NODEV;
67 int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
68 int nkmempages = 256*256/2; /* from le chapeau */
69 const int schedppq = 1;
70 int hardclock_ticks;
71 bool mp_online = false;
72 struct timeval boottime;
73 int cold = 1;
74 int boothowto = AB_SILENT;
75 struct tty *constty;
76
77 const struct bdevsw *bdevsw0[255];
78 const struct bdevsw **bdevsw = bdevsw0;
79 const int sys_cdevsws = 255;
80 int max_cdevsws = 255;
81
82 const struct cdevsw *cdevsw0[255];
83 const struct cdevsw **cdevsw = cdevsw0;
84 const int sys_bdevsws = 255;
85 int max_bdevsws = 255;
86
87 int mem_no = 2;
88
89 struct device *booted_device;
90 struct device *booted_wedge;
91 int booted_partition;
92
93 /* XXX: unused */
94 kmutex_t tty_lock;
95 krwlock_t exec_lock;
96
97 struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
98
99 /* sparc doesn't sport constant page size, pretend we have 4k pages */
100 #ifdef __sparc__
101 int nbpg = 4096;
102 int pgofset = 4096-1;
103 int pgshift = 12;
104 #endif
105
106 /* on sun3 VM_MAX_ADDRESS is a const variable */
107 /* XXX: should be moved into rump.c and initialize for sun3 and sun3x? */
108 #ifdef sun3
109 const vaddr_t kernbase = KERNBASE3;
110 #endif
111
112 struct loadavg averunnable = {
113 { 0 * FSCALE,
114 1 * FSCALE,
115 11 * FSCALE, },
116 FSCALE,
117 };
118
119 struct emul emul_netbsd = {
120 .e_name = "netbsd-rump",
121 .e_sysent = rump_sysent,
122 .e_vm_default_addr = uvm_default_mapaddr,
123 #ifdef __HAVE_SYSCALL_INTERN
124 .e_syscall_intern = syscall_intern,
125 #endif
126 };
127
128 u_int nprocs = 1;
129
130 int
131 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
132 {
133 extern int hz;
134 int rv, error;
135 uint64_t sec, nsec;
136
137 if (mtx)
138 mutex_exit(mtx);
139
140 sec = timeo / hz;
141 nsec = (timeo % hz) * (1000000000 / hz);
142 rv = rumpuser_nanosleep(&sec, &nsec, &error);
143
144 if (mtx)
145 mutex_enter(mtx);
146
147 if (rv)
148 return error;
149
150 return 0;
151 }
152
153 void
154 lwp_unsleep(lwp_t *l, bool cleanup)
155 {
156
157 KASSERT(mutex_owned(l->l_mutex));
158
159 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
160 }
161
162 void
163 lwp_update_creds(struct lwp *l)
164 {
165 struct proc *p;
166 kauth_cred_t oldcred;
167
168 p = l->l_proc;
169 oldcred = l->l_cred;
170 l->l_prflag &= ~LPR_CRMOD;
171
172 mutex_enter(p->p_lock);
173 kauth_cred_hold(p->p_cred);
174 l->l_cred = p->p_cred;
175 mutex_exit(p->p_lock);
176
177 if (oldcred != NULL)
178 kauth_cred_free(oldcred);
179 }
180
181 vaddr_t
182 calc_cache_size(struct vm_map *map, int pct, int va_pct)
183 {
184 paddr_t t;
185
186 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
187 if ((vaddr_t)t != t) {
188 panic("%s: needs tweak", __func__);
189 }
190 return t;
191 }
192
193 void
194 assert_sleepable(void)
195 {
196
197 /* always sleepable, although we should improve this */
198 }
199
200 void
201 module_init_md(void)
202 {
203
204 /*
205 * Nothing for now. However, we should load the librump
206 * symbol table.
207 */
208 }
209
210 /* us and them, after all we're only ordinary seconds */
211 static void
212 rump_delay(unsigned int us)
213 {
214 uint64_t sec, nsec;
215 int error;
216
217 sec = us / 1000000;
218 nsec = (us % 1000000) * 1000;
219
220 if (__predict_false(sec != 0))
221 printf("WARNING: over 1s delay\n");
222
223 rumpuser_nanosleep(&sec, &nsec, &error);
224 }
225 void (*delay_func)(unsigned int) = rump_delay;
226
227 /*
228 * Provide weak aliases for tty routines used by printf.
229 * They will be used unless the rumpkern_tty component is present.
230 */
231
232 int rump_ttycheckoutq(struct tty *, int);
233 int
234 rump_ttycheckoutq(struct tty *tp, int wait)
235 {
236
237 return 1;
238 }
239 __weak_alias(ttycheckoutq,rump_ttycheckoutq);
240
241 int rump_tputchar(int, int, struct tty *);
242 int
243 rump_tputchar(int c, int flags, struct tty *tp)
244 {
245
246 cnputc(c);
247 return 0;
248 }
249 __weak_alias(tputchar,rump_tputchar);
250
251 void
252 cnputc(int c)
253 {
254 int error;
255
256 rumpuser_putchar(c, &error);
257 }
258
259 void
260 cnflush(void)
261 {
262
263 /* done */
264 }
265
266 void
267 cpu_reboot(int howto, char *bootstr)
268 {
269
270 rump_reboot(howto);
271
272 /* this function is __dead, we must exit */
273 rumpuser_exit(0);
274 }
275
276 #ifdef __HAVE_SYSCALL_INTERN
277 void
278 syscall_intern(struct proc *p)
279 {
280
281 /* no you don't */
282 }
283 #endif
284
285 void
286 xc_send_ipi(struct cpu_info *ci)
287 {
288
289 /* I'll think about the implementation if this is ever used */
290 panic("not implemented");
291 }
292