emul.c revision 1.38.2.7 1 1.38.2.7 yamt /* $NetBSD: emul.c,v 1.38.2.7 2010/08/11 22:55:06 yamt Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2007 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by Google Summer of Code.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.38.2.1 yamt #include <sys/cdefs.h>
31 1.38.2.7 yamt __KERNEL_RCSID(0, "$NetBSD: emul.c,v 1.38.2.7 2010/08/11 22:55:06 yamt Exp $");
32 1.9 pooka
33 1.1 pooka #include <sys/param.h>
34 1.1 pooka #include <sys/null.h>
35 1.1 pooka #include <sys/vnode.h>
36 1.1 pooka #include <sys/stat.h>
37 1.35 martin #include <sys/select.h>
38 1.1 pooka #include <sys/syslog.h>
39 1.1 pooka #include <sys/namei.h>
40 1.1 pooka #include <sys/kauth.h>
41 1.1 pooka #include <sys/conf.h>
42 1.1 pooka #include <sys/device.h>
43 1.1 pooka #include <sys/queue.h>
44 1.20 pooka #include <sys/file.h>
45 1.15 ad #include <sys/cpu.h>
46 1.17 pooka #include <sys/kmem.h>
47 1.20 pooka #include <sys/poll.h>
48 1.38.2.1 yamt #include <sys/timetc.h>
49 1.38.2.1 yamt #include <sys/tprintf.h>
50 1.38.2.1 yamt #include <sys/module.h>
51 1.38.2.1 yamt #include <sys/tty.h>
52 1.38.2.1 yamt #include <sys/reboot.h>
53 1.38.2.6 yamt #include <sys/syscallvar.h>
54 1.38.2.6 yamt #include <sys/xcall.h>
55 1.38.2.1 yamt
56 1.38.2.1 yamt #include <dev/cons.h>
57 1.1 pooka
58 1.38.2.1 yamt #include <rump/rumpuser.h>
59 1.38.2.1 yamt
60 1.8 pooka #include <uvm/uvm_map.h>
61 1.8 pooka
62 1.10 pooka #include "rump_private.h"
63 1.1 pooka
64 1.1 pooka struct lwp lwp0;
65 1.1 pooka struct vnode *rootvp;
66 1.38.2.7 yamt dev_t rootdev = NODEV;
67 1.25 pooka int physmem = 256*256; /* 256 * 1024*1024 / 4k, PAGE_SIZE not always set */
68 1.38.2.7 yamt int nkmempages = 256*256/2; /* from le chapeau */
69 1.19 pooka const int schedppq = 1;
70 1.28 ad int hardclock_ticks;
71 1.38.2.1 yamt bool mp_online = false;
72 1.38.2.1 yamt struct timeval boottime;
73 1.38.2.1 yamt int cold = 1;
74 1.38.2.6 yamt int boothowto = AB_SILENT;
75 1.38.2.1 yamt struct tty *constty;
76 1.1 pooka
77 1.38.2.7 yamt const struct bdevsw *bdevsw0[255];
78 1.38.2.1 yamt const struct bdevsw **bdevsw = bdevsw0;
79 1.38.2.7 yamt const int sys_cdevsws = 255;
80 1.38.2.7 yamt int max_cdevsws = 255;
81 1.38.2.1 yamt
82 1.38.2.7 yamt const struct cdevsw *cdevsw0[255];
83 1.38.2.1 yamt const struct cdevsw **cdevsw = cdevsw0;
84 1.38.2.7 yamt const int sys_bdevsws = 255;
85 1.38.2.7 yamt int max_bdevsws = 255;
86 1.38.2.1 yamt
87 1.38.2.1 yamt int mem_no = 2;
88 1.1 pooka
89 1.38.2.5 yamt struct device *booted_device;
90 1.38.2.5 yamt struct device *booted_wedge;
91 1.38.2.5 yamt int booted_partition;
92 1.38.2.5 yamt
93 1.38.2.6 yamt /* XXX: unused */
94 1.38.2.1 yamt kmutex_t tty_lock;
95 1.38.2.6 yamt krwlock_t exec_lock;
96 1.1 pooka
97 1.38.2.7 yamt struct lwplist alllwp = LIST_HEAD_INITIALIZER(alllwp);
98 1.38.2.7 yamt
99 1.38.2.7 yamt /* sparc doesn't sport constant page size, pretend we have 4k pages */
100 1.38.2.6 yamt #ifdef __sparc__
101 1.38.2.6 yamt int nbpg = 4096;
102 1.38.2.7 yamt int pgofset = 4096-1;
103 1.38.2.7 yamt int pgshift = 12;
104 1.38.2.7 yamt #endif
105 1.38.2.7 yamt
106 1.38.2.7 yamt /* sun3 is sun3 with broken kernel modules */
107 1.38.2.7 yamt #ifdef sun3
108 1.38.2.7 yamt char KERNBASE[1]; /* this is completely random ... */
109 1.38.2.6 yamt #endif
110 1.1 pooka
111 1.38.2.6 yamt struct loadavg averunnable = {
112 1.38.2.6 yamt { 0 * FSCALE,
113 1.38.2.6 yamt 1 * FSCALE,
114 1.38.2.6 yamt 11 * FSCALE, },
115 1.38.2.6 yamt FSCALE,
116 1.38.2.6 yamt };
117 1.1 pooka
118 1.38.2.7 yamt struct emul emul_netbsd = {
119 1.38.2.7 yamt .e_name = "netbsd-rump",
120 1.38.2.7 yamt .e_sysent = rump_sysent,
121 1.38.2.7 yamt .e_vm_default_addr = uvm_default_mapaddr,
122 1.38.2.7 yamt #ifdef __HAVE_SYSCALL_INTERN
123 1.38.2.7 yamt .e_syscall_intern = syscall_intern,
124 1.38.2.7 yamt #endif
125 1.38.2.7 yamt };
126 1.20 pooka
127 1.38.2.7 yamt u_int nprocs = 1;
128 1.20 pooka
129 1.20 pooka int
130 1.20 pooka kpause(const char *wmesg, bool intr, int timeo, kmutex_t *mtx)
131 1.20 pooka {
132 1.20 pooka extern int hz;
133 1.20 pooka int rv, error;
134 1.38.2.1 yamt uint64_t sec, nsec;
135 1.38.2.7 yamt
136 1.20 pooka if (mtx)
137 1.20 pooka mutex_exit(mtx);
138 1.38.2.1 yamt
139 1.38.2.1 yamt sec = timeo / hz;
140 1.38.2.1 yamt nsec = (timeo % hz) * (1000000000 / hz);
141 1.38.2.1 yamt rv = rumpuser_nanosleep(&sec, &nsec, &error);
142 1.38.2.1 yamt
143 1.20 pooka if (mtx)
144 1.20 pooka mutex_enter(mtx);
145 1.20 pooka
146 1.20 pooka if (rv)
147 1.20 pooka return error;
148 1.20 pooka
149 1.20 pooka return 0;
150 1.20 pooka }
151 1.27 pooka
152 1.27 pooka void
153 1.32 bjs lwp_unsleep(lwp_t *l, bool cleanup)
154 1.32 bjs {
155 1.32 bjs
156 1.32 bjs KASSERT(mutex_owned(l->l_mutex));
157 1.32 bjs
158 1.38.2.6 yamt (*l->l_syncobj->sobj_unsleep)(l, cleanup);
159 1.32 bjs }
160 1.34 yamt
161 1.34 yamt vaddr_t
162 1.36 yamt calc_cache_size(struct vm_map *map, int pct, int va_pct)
163 1.34 yamt {
164 1.34 yamt paddr_t t;
165 1.34 yamt
166 1.34 yamt t = (paddr_t)physmem * pct / 100 * PAGE_SIZE;
167 1.34 yamt if ((vaddr_t)t != t) {
168 1.34 yamt panic("%s: needs tweak", __func__);
169 1.34 yamt }
170 1.34 yamt return t;
171 1.34 yamt }
172 1.35 martin
173 1.35 martin void
174 1.38.2.6 yamt assert_sleepable(void)
175 1.35 martin {
176 1.37 matt
177 1.38.2.6 yamt /* always sleepable, although we should improve this */
178 1.37 matt }
179 1.38.2.1 yamt
180 1.38.2.1 yamt void
181 1.38.2.1 yamt module_init_md(void)
182 1.38.2.1 yamt {
183 1.38.2.1 yamt
184 1.38.2.1 yamt /*
185 1.38.2.1 yamt * Nothing for now. However, we should load the librump
186 1.38.2.1 yamt * symbol table.
187 1.38.2.1 yamt */
188 1.38.2.1 yamt }
189 1.38.2.1 yamt
190 1.38.2.1 yamt /* us and them, after all we're only ordinary seconds */
191 1.38.2.1 yamt static void
192 1.38.2.1 yamt rump_delay(unsigned int us)
193 1.38.2.1 yamt {
194 1.38.2.1 yamt uint64_t sec, nsec;
195 1.38.2.1 yamt int error;
196 1.38.2.1 yamt
197 1.38.2.1 yamt sec = us / 1000000;
198 1.38.2.1 yamt nsec = (us % 1000000) * 1000;
199 1.38.2.1 yamt
200 1.38.2.1 yamt if (__predict_false(sec != 0))
201 1.38.2.1 yamt printf("WARNING: over 1s delay\n");
202 1.38.2.1 yamt
203 1.38.2.1 yamt rumpuser_nanosleep(&sec, &nsec, &error);
204 1.38.2.1 yamt }
205 1.38.2.1 yamt void (*delay_func)(unsigned int) = rump_delay;
206 1.38.2.1 yamt
207 1.38.2.6 yamt /*
208 1.38.2.7 yamt * Provide weak aliases for tty routines used by printf.
209 1.38.2.7 yamt * They will be used unless the rumpkern_tty component is present.
210 1.38.2.6 yamt */
211 1.38.2.1 yamt
212 1.38.2.7 yamt int rump_ttycheckoutq(struct tty *, int);
213 1.38.2.1 yamt int
214 1.38.2.7 yamt rump_ttycheckoutq(struct tty *tp, int wait)
215 1.38.2.6 yamt {
216 1.38.2.6 yamt
217 1.38.2.7 yamt return 1;
218 1.38.2.6 yamt }
219 1.38.2.7 yamt __weak_alias(ttycheckoutq,rump_ttycheckoutq);
220 1.38.2.6 yamt
221 1.38.2.7 yamt int rump_tputchar(int, int, struct tty *);
222 1.38.2.6 yamt int
223 1.38.2.7 yamt rump_tputchar(int c, int flags, struct tty *tp)
224 1.38.2.1 yamt {
225 1.38.2.1 yamt
226 1.38.2.7 yamt cnputc(c);
227 1.38.2.7 yamt return 0;
228 1.38.2.1 yamt }
229 1.38.2.7 yamt __weak_alias(tputchar,rump_tputchar);
230 1.38.2.1 yamt
231 1.38.2.1 yamt void
232 1.38.2.1 yamt cnputc(int c)
233 1.38.2.1 yamt {
234 1.38.2.1 yamt int error;
235 1.38.2.1 yamt
236 1.38.2.1 yamt rumpuser_putchar(c, &error);
237 1.38.2.1 yamt }
238 1.38.2.1 yamt
239 1.38.2.1 yamt void
240 1.38.2.1 yamt cnflush(void)
241 1.38.2.1 yamt {
242 1.38.2.1 yamt
243 1.38.2.1 yamt /* done */
244 1.38.2.1 yamt }
245 1.38.2.1 yamt
246 1.38.2.1 yamt void
247 1.38.2.1 yamt cpu_reboot(int howto, char *bootstr)
248 1.38.2.1 yamt {
249 1.38.2.1 yamt
250 1.38.2.6 yamt rump_reboot(howto);
251 1.38.2.6 yamt
252 1.38.2.6 yamt /* this function is __dead, we must exit */
253 1.38.2.6 yamt rumpuser_exit(0);
254 1.38.2.1 yamt }
255 1.38.2.1 yamt
256 1.38.2.7 yamt #ifdef __HAVE_SYSCALL_INTERN
257 1.38.2.6 yamt void
258 1.38.2.7 yamt syscall_intern(struct proc *p)
259 1.38.2.6 yamt {
260 1.38.2.5 yamt
261 1.38.2.7 yamt /* no you don't */
262 1.38.2.6 yamt }
263 1.38.2.7 yamt #endif
264 1.38.2.5 yamt
265 1.38.2.6 yamt void
266 1.38.2.7 yamt xc_send_ipi(struct cpu_info *ci)
267 1.38.2.6 yamt {
268 1.38.2.6 yamt
269 1.38.2.7 yamt /* I'll think about the implementation if this is ever used */
270 1.38.2.7 yamt panic("not implemented");
271 1.38.2.6 yamt }
272