emul.c revision 1.139 1 /* $NetBSD: emul.c,v 1.139 2010/06/13 11:05:58 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.139 2010/06/13 11:05:58 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/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 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 #ifdef __HAVE_SYSCALL_INTERN
118 .e_syscall_intern = syscall_intern,
119 #endif
120 };
121
122 u_int nprocs = 1;
123
124 int
125 kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
126 {
127 extern int hz;
128 int rv, error;
129 uint64_t sec, nsec;
130
131 if (mtx)
132 mutex_exit(mtx);
133
134 sec = timeo / hz;
135 nsec = (timeo % hz) * (1000000000 / hz);
136 rv = rumpuser_nanosleep(&sec, &nsec, &error);
137
138 if (mtx)
139 mutex_enter(mtx);
140
141 if (rv)
142 return error;
143
144 return 0;
145 }
146
147 void
148 lwp_unsleep(lwp_t *l, bool cleanup)
149 {
150
151 KASSERT(mutex_owned(l->l_mutex));
152
153 (*l->l_syncobj->sobj_unsleep)(l, cleanup);
154 }
155
156 vaddr_t
157 calc_cache_size(struct vm_map *map, int pct, int va_pct)
158 {
159 paddr_t t;
160
161 t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
162 if ((vaddr_t)t != t) {
163 panic("%s: needs tweak", __func__);
164 }
165 return t;
166 }
167
168 void
169 assert_sleepable(void)
170 {
171
172 /* always sleepable, although we should improve this */
173 }
174
175 void
176 module_init_md(void)
177 {
178
179 /*
180 * Nothing for now. However, we should load the librump
181 * symbol table.
182 */
183 }
184
185 /* us and them, after all we're only ordinary seconds */
186 static void
187 rump_delay(unsigned int us)
188 {
189 uint64_t sec, nsec;
190 int error;
191
192 sec = us / 1000000;
193 nsec = (us % 1000000) * 1000;
194
195 if (__predict_false(sec != 0))
196 printf("WARNING: over 1s delay\n");
197
198 rumpuser_nanosleep(&sec, &nsec, &error);
199 }
200 void (*delay_func)(unsigned int) = rump_delay;
201
202 int
203 ttycheckoutq(struct tty *tp, int wait)
204 {
205
206 return 1;
207 }
208
209 void
210 cnputc(int c)
211 {
212 int error;
213
214 rumpuser_putchar(c, &error);
215 }
216
217 void
218 cnflush(void)
219 {
220
221 /* done */
222 }
223
224 int
225 tputchar(int c, int flags, struct tty *tp)
226 {
227
228 cnputc(c);
229 return 0;
230 }
231
232 void
233 cpu_reboot(int howto, char *bootstr)
234 {
235
236 rump_reboot(howto);
237
238 /* this function is __dead, we must exit */
239 rumpuser_exit(0);
240 }
241
242 #ifdef __HAVE_SYSCALL_INTERN
243 void
244 syscall_intern(struct proc *p)
245 {
246
247 /* no you don't */
248 }
249 #endif
250