root / linux-user / main.c @ 97eb5b14
History | View | Annotate | Download (33 kB)
1 | 31e31b8a | bellard | /*
|
---|---|---|---|
2 | 93ac68bc | bellard | * qemu user main
|
3 | 31e31b8a | bellard | *
|
4 | 31e31b8a | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 31e31b8a | bellard | *
|
6 | 31e31b8a | bellard | * This program is free software; you can redistribute it and/or modify
|
7 | 31e31b8a | bellard | * it under the terms of the GNU General Public License as published by
|
8 | 31e31b8a | bellard | * the Free Software Foundation; either version 2 of the License, or
|
9 | 31e31b8a | bellard | * (at your option) any later version.
|
10 | 31e31b8a | bellard | *
|
11 | 31e31b8a | bellard | * This program is distributed in the hope that it will be useful,
|
12 | 31e31b8a | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 31e31b8a | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | 31e31b8a | bellard | * GNU General Public License for more details.
|
15 | 31e31b8a | bellard | *
|
16 | 31e31b8a | bellard | * You should have received a copy of the GNU General Public License
|
17 | 31e31b8a | bellard | * along with this program; if not, write to the Free Software
|
18 | 31e31b8a | bellard | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
19 | 31e31b8a | bellard | */
|
20 | 31e31b8a | bellard | #include <stdlib.h> |
21 | 31e31b8a | bellard | #include <stdio.h> |
22 | 31e31b8a | bellard | #include <stdarg.h> |
23 | 04369ff2 | bellard | #include <string.h> |
24 | 31e31b8a | bellard | #include <errno.h> |
25 | 0ecfa993 | bellard | #include <unistd.h> |
26 | 31e31b8a | bellard | |
27 | 3ef693a0 | bellard | #include "qemu.h" |
28 | 31e31b8a | bellard | |
29 | 3ef693a0 | bellard | #define DEBUG_LOGFILE "/tmp/qemu.log" |
30 | 586314f2 | bellard | |
31 | 74cd30b8 | bellard | static const char *interp_prefix = CONFIG_QEMU_PREFIX; |
32 | 586314f2 | bellard | |
33 | 3a4739d6 | bellard | #if defined(__i386__) && !defined(CONFIG_STATIC)
|
34 | f801f97e | bellard | /* Force usage of an ELF interpreter even if it is an ELF shared
|
35 | f801f97e | bellard | object ! */
|
36 | f801f97e | bellard | const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2"; |
37 | 4304763b | bellard | #endif
|
38 | 74cd30b8 | bellard | |
39 | 93ac68bc | bellard | /* for recent libc, we add these dummy symbols which are not declared
|
40 | 74cd30b8 | bellard | when generating a linked object (bug in ld ?) */
|
41 | 74cd30b8 | bellard | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) |
42 | c6981055 | bellard | long __preinit_array_start[0]; |
43 | c6981055 | bellard | long __preinit_array_end[0]; |
44 | 74cd30b8 | bellard | long __init_array_start[0]; |
45 | 74cd30b8 | bellard | long __init_array_end[0]; |
46 | 74cd30b8 | bellard | long __fini_array_start[0]; |
47 | 74cd30b8 | bellard | long __fini_array_end[0]; |
48 | 74cd30b8 | bellard | #endif
|
49 | 74cd30b8 | bellard | |
50 | 9de5e440 | bellard | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
|
51 | 9de5e440 | bellard | we allocate a bigger stack. Need a better solution, for example
|
52 | 9de5e440 | bellard | by remapping the process stack directly at the right place */
|
53 | 9de5e440 | bellard | unsigned long x86_stack_size = 512 * 1024; |
54 | 31e31b8a | bellard | |
55 | 31e31b8a | bellard | void gemu_log(const char *fmt, ...) |
56 | 31e31b8a | bellard | { |
57 | 31e31b8a | bellard | va_list ap; |
58 | 31e31b8a | bellard | |
59 | 31e31b8a | bellard | va_start(ap, fmt); |
60 | 31e31b8a | bellard | vfprintf(stderr, fmt, ap); |
61 | 31e31b8a | bellard | va_end(ap); |
62 | 31e31b8a | bellard | } |
63 | 31e31b8a | bellard | |
64 | 61190b14 | bellard | void cpu_outb(CPUState *env, int addr, int val) |
65 | 367e86e8 | bellard | { |
66 | 367e86e8 | bellard | fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
|
67 | 367e86e8 | bellard | } |
68 | 367e86e8 | bellard | |
69 | 61190b14 | bellard | void cpu_outw(CPUState *env, int addr, int val) |
70 | 367e86e8 | bellard | { |
71 | 367e86e8 | bellard | fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
|
72 | 367e86e8 | bellard | } |
73 | 367e86e8 | bellard | |
74 | 61190b14 | bellard | void cpu_outl(CPUState *env, int addr, int val) |
75 | 367e86e8 | bellard | { |
76 | 367e86e8 | bellard | fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
|
77 | 367e86e8 | bellard | } |
78 | 367e86e8 | bellard | |
79 | 61190b14 | bellard | int cpu_inb(CPUState *env, int addr) |
80 | 367e86e8 | bellard | { |
81 | 367e86e8 | bellard | fprintf(stderr, "inb: port=0x%04x\n", addr);
|
82 | 367e86e8 | bellard | return 0; |
83 | 367e86e8 | bellard | } |
84 | 367e86e8 | bellard | |
85 | 61190b14 | bellard | int cpu_inw(CPUState *env, int addr) |
86 | 367e86e8 | bellard | { |
87 | 367e86e8 | bellard | fprintf(stderr, "inw: port=0x%04x\n", addr);
|
88 | 367e86e8 | bellard | return 0; |
89 | 367e86e8 | bellard | } |
90 | 367e86e8 | bellard | |
91 | 61190b14 | bellard | int cpu_inl(CPUState *env, int addr) |
92 | 367e86e8 | bellard | { |
93 | 367e86e8 | bellard | fprintf(stderr, "inl: port=0x%04x\n", addr);
|
94 | 367e86e8 | bellard | return 0; |
95 | 367e86e8 | bellard | } |
96 | 367e86e8 | bellard | |
97 | 61190b14 | bellard | #ifdef TARGET_I386
|
98 | 61190b14 | bellard | /***********************************************************/
|
99 | 61190b14 | bellard | /* CPUX86 core interface */
|
100 | 61190b14 | bellard | |
101 | 61190b14 | bellard | int cpu_x86_get_pic_interrupt(CPUState *env)
|
102 | 92ccca6a | bellard | { |
103 | 92ccca6a | bellard | return -1; |
104 | 92ccca6a | bellard | } |
105 | 92ccca6a | bellard | |
106 | f4beb510 | bellard | static void write_dt(void *ptr, unsigned long addr, unsigned long limit, |
107 | f4beb510 | bellard | int flags)
|
108 | 6dbad63e | bellard | { |
109 | f4beb510 | bellard | unsigned int e1, e2; |
110 | 6dbad63e | bellard | e1 = (addr << 16) | (limit & 0xffff); |
111 | 6dbad63e | bellard | e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000); |
112 | f4beb510 | bellard | e2 |= flags; |
113 | f4beb510 | bellard | stl((uint8_t *)ptr, e1); |
114 | f4beb510 | bellard | stl((uint8_t *)ptr + 4, e2);
|
115 | f4beb510 | bellard | } |
116 | f4beb510 | bellard | |
117 | f4beb510 | bellard | static void set_gate(void *ptr, unsigned int type, unsigned int dpl, |
118 | f4beb510 | bellard | unsigned long addr, unsigned int sel) |
119 | f4beb510 | bellard | { |
120 | f4beb510 | bellard | unsigned int e1, e2; |
121 | f4beb510 | bellard | e1 = (addr & 0xffff) | (sel << 16); |
122 | f4beb510 | bellard | e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8); |
123 | 6dbad63e | bellard | stl((uint8_t *)ptr, e1); |
124 | 6dbad63e | bellard | stl((uint8_t *)ptr + 4, e2);
|
125 | 6dbad63e | bellard | } |
126 | 6dbad63e | bellard | |
127 | 6dbad63e | bellard | uint64_t gdt_table[6];
|
128 | f4beb510 | bellard | uint64_t idt_table[256];
|
129 | f4beb510 | bellard | |
130 | f4beb510 | bellard | /* only dpl matters as we do only user space emulation */
|
131 | f4beb510 | bellard | static void set_idt(int n, unsigned int dpl) |
132 | f4beb510 | bellard | { |
133 | f4beb510 | bellard | set_gate(idt_table + n, 0, dpl, 0, 0); |
134 | f4beb510 | bellard | } |
135 | 31e31b8a | bellard | |
136 | 89e957e7 | bellard | void cpu_loop(CPUX86State *env)
|
137 | 1b6b029e | bellard | { |
138 | bc8a22cc | bellard | int trapnr;
|
139 | 9de5e440 | bellard | uint8_t *pc; |
140 | 9de5e440 | bellard | target_siginfo_t info; |
141 | 851e67a1 | bellard | |
142 | 1b6b029e | bellard | for(;;) {
|
143 | bc8a22cc | bellard | trapnr = cpu_x86_exec(env); |
144 | bc8a22cc | bellard | switch(trapnr) {
|
145 | f4beb510 | bellard | case 0x80: |
146 | f4beb510 | bellard | /* linux syscall */
|
147 | f4beb510 | bellard | env->regs[R_EAX] = do_syscall(env, |
148 | f4beb510 | bellard | env->regs[R_EAX], |
149 | f4beb510 | bellard | env->regs[R_EBX], |
150 | f4beb510 | bellard | env->regs[R_ECX], |
151 | f4beb510 | bellard | env->regs[R_EDX], |
152 | f4beb510 | bellard | env->regs[R_ESI], |
153 | f4beb510 | bellard | env->regs[R_EDI], |
154 | f4beb510 | bellard | env->regs[R_EBP]); |
155 | f4beb510 | bellard | break;
|
156 | f4beb510 | bellard | case EXCP0B_NOSEG:
|
157 | f4beb510 | bellard | case EXCP0C_STACK:
|
158 | f4beb510 | bellard | info.si_signo = SIGBUS; |
159 | f4beb510 | bellard | info.si_errno = 0;
|
160 | f4beb510 | bellard | info.si_code = TARGET_SI_KERNEL; |
161 | f4beb510 | bellard | info._sifields._sigfault._addr = 0;
|
162 | f4beb510 | bellard | queue_signal(info.si_signo, &info); |
163 | f4beb510 | bellard | break;
|
164 | 1b6b029e | bellard | case EXCP0D_GPF:
|
165 | 851e67a1 | bellard | if (env->eflags & VM_MASK) {
|
166 | 89e957e7 | bellard | handle_vm86_fault(env); |
167 | 1b6b029e | bellard | } else {
|
168 | f4beb510 | bellard | info.si_signo = SIGSEGV; |
169 | f4beb510 | bellard | info.si_errno = 0;
|
170 | f4beb510 | bellard | info.si_code = TARGET_SI_KERNEL; |
171 | f4beb510 | bellard | info._sifields._sigfault._addr = 0;
|
172 | f4beb510 | bellard | queue_signal(info.si_signo, &info); |
173 | 1b6b029e | bellard | } |
174 | 1b6b029e | bellard | break;
|
175 | b689bc57 | bellard | case EXCP0E_PAGE:
|
176 | b689bc57 | bellard | info.si_signo = SIGSEGV; |
177 | b689bc57 | bellard | info.si_errno = 0;
|
178 | b689bc57 | bellard | if (!(env->error_code & 1)) |
179 | b689bc57 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
180 | b689bc57 | bellard | else
|
181 | b689bc57 | bellard | info.si_code = TARGET_SEGV_ACCERR; |
182 | 970a87a6 | bellard | info._sifields._sigfault._addr = env->cr[2];
|
183 | b689bc57 | bellard | queue_signal(info.si_signo, &info); |
184 | b689bc57 | bellard | break;
|
185 | 9de5e440 | bellard | case EXCP00_DIVZ:
|
186 | bc8a22cc | bellard | if (env->eflags & VM_MASK) {
|
187 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
188 | bc8a22cc | bellard | } else {
|
189 | bc8a22cc | bellard | /* division by zero */
|
190 | bc8a22cc | bellard | info.si_signo = SIGFPE; |
191 | bc8a22cc | bellard | info.si_errno = 0;
|
192 | bc8a22cc | bellard | info.si_code = TARGET_FPE_INTDIV; |
193 | bc8a22cc | bellard | info._sifields._sigfault._addr = env->eip; |
194 | bc8a22cc | bellard | queue_signal(info.si_signo, &info); |
195 | bc8a22cc | bellard | } |
196 | 9de5e440 | bellard | break;
|
197 | 447db213 | bellard | case EXCP01_SSTP:
|
198 | 447db213 | bellard | case EXCP03_INT3:
|
199 | 447db213 | bellard | if (env->eflags & VM_MASK) {
|
200 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
201 | 447db213 | bellard | } else {
|
202 | 447db213 | bellard | info.si_signo = SIGTRAP; |
203 | 447db213 | bellard | info.si_errno = 0;
|
204 | 447db213 | bellard | if (trapnr == EXCP01_SSTP) {
|
205 | 447db213 | bellard | info.si_code = TARGET_TRAP_BRKPT; |
206 | 447db213 | bellard | info._sifields._sigfault._addr = env->eip; |
207 | 447db213 | bellard | } else {
|
208 | 447db213 | bellard | info.si_code = TARGET_SI_KERNEL; |
209 | 447db213 | bellard | info._sifields._sigfault._addr = 0;
|
210 | 447db213 | bellard | } |
211 | 447db213 | bellard | queue_signal(info.si_signo, &info); |
212 | 447db213 | bellard | } |
213 | 447db213 | bellard | break;
|
214 | 9de5e440 | bellard | case EXCP04_INTO:
|
215 | 9de5e440 | bellard | case EXCP05_BOUND:
|
216 | bc8a22cc | bellard | if (env->eflags & VM_MASK) {
|
217 | 447db213 | bellard | handle_vm86_trap(env, trapnr); |
218 | bc8a22cc | bellard | } else {
|
219 | bc8a22cc | bellard | info.si_signo = SIGSEGV; |
220 | bc8a22cc | bellard | info.si_errno = 0;
|
221 | b689bc57 | bellard | info.si_code = TARGET_SI_KERNEL; |
222 | bc8a22cc | bellard | info._sifields._sigfault._addr = 0;
|
223 | bc8a22cc | bellard | queue_signal(info.si_signo, &info); |
224 | bc8a22cc | bellard | } |
225 | 9de5e440 | bellard | break;
|
226 | 9de5e440 | bellard | case EXCP06_ILLOP:
|
227 | 9de5e440 | bellard | info.si_signo = SIGILL; |
228 | 9de5e440 | bellard | info.si_errno = 0;
|
229 | 9de5e440 | bellard | info.si_code = TARGET_ILL_ILLOPN; |
230 | 9de5e440 | bellard | info._sifields._sigfault._addr = env->eip; |
231 | 9de5e440 | bellard | queue_signal(info.si_signo, &info); |
232 | 9de5e440 | bellard | break;
|
233 | 9de5e440 | bellard | case EXCP_INTERRUPT:
|
234 | 9de5e440 | bellard | /* just indicate that signals should be handled asap */
|
235 | 9de5e440 | bellard | break;
|
236 | 1b6b029e | bellard | default:
|
237 | 970a87a6 | bellard | pc = env->segs[R_CS].base + env->eip; |
238 | bc8a22cc | bellard | fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
|
239 | bc8a22cc | bellard | (long)pc, trapnr);
|
240 | 1b6b029e | bellard | abort(); |
241 | 1b6b029e | bellard | } |
242 | 66fb9763 | bellard | process_pending_signals(env); |
243 | 1b6b029e | bellard | } |
244 | 1b6b029e | bellard | } |
245 | b346ff46 | bellard | #endif
|
246 | b346ff46 | bellard | |
247 | b346ff46 | bellard | #ifdef TARGET_ARM
|
248 | b346ff46 | bellard | |
249 | b346ff46 | bellard | void cpu_loop(CPUARMState *env)
|
250 | b346ff46 | bellard | { |
251 | b346ff46 | bellard | int trapnr;
|
252 | b346ff46 | bellard | unsigned int n, insn; |
253 | b346ff46 | bellard | target_siginfo_t info; |
254 | b346ff46 | bellard | |
255 | b346ff46 | bellard | for(;;) {
|
256 | b346ff46 | bellard | trapnr = cpu_arm_exec(env); |
257 | b346ff46 | bellard | switch(trapnr) {
|
258 | b346ff46 | bellard | case EXCP_UDEF:
|
259 | c6981055 | bellard | { |
260 | c6981055 | bellard | TaskState *ts = env->opaque; |
261 | c6981055 | bellard | uint32_t opcode; |
262 | c6981055 | bellard | |
263 | c6981055 | bellard | /* we handle the FPU emulation here, as Linux */
|
264 | c6981055 | bellard | /* we get the opcode */
|
265 | c6981055 | bellard | opcode = ldl_raw((uint8_t *)env->regs[15]);
|
266 | c6981055 | bellard | |
267 | c6981055 | bellard | if (EmulateAll(opcode, &ts->fpa, env->regs) == 0) { |
268 | c6981055 | bellard | info.si_signo = SIGILL; |
269 | c6981055 | bellard | info.si_errno = 0;
|
270 | c6981055 | bellard | info.si_code = TARGET_ILL_ILLOPN; |
271 | c6981055 | bellard | info._sifields._sigfault._addr = env->regs[15];
|
272 | c6981055 | bellard | queue_signal(info.si_signo, &info); |
273 | c6981055 | bellard | } else {
|
274 | c6981055 | bellard | /* increment PC */
|
275 | c6981055 | bellard | env->regs[15] += 4; |
276 | c6981055 | bellard | } |
277 | c6981055 | bellard | } |
278 | b346ff46 | bellard | break;
|
279 | b346ff46 | bellard | case EXCP_SWI:
|
280 | b346ff46 | bellard | { |
281 | b346ff46 | bellard | /* system call */
|
282 | b346ff46 | bellard | insn = ldl((void *)(env->regs[15] - 4)); |
283 | b346ff46 | bellard | n = insn & 0xffffff;
|
284 | b346ff46 | bellard | if (n >= ARM_SYSCALL_BASE) {
|
285 | b346ff46 | bellard | /* linux syscall */
|
286 | b346ff46 | bellard | n -= ARM_SYSCALL_BASE; |
287 | b346ff46 | bellard | env->regs[0] = do_syscall(env,
|
288 | b346ff46 | bellard | n, |
289 | b346ff46 | bellard | env->regs[0],
|
290 | b346ff46 | bellard | env->regs[1],
|
291 | b346ff46 | bellard | env->regs[2],
|
292 | b346ff46 | bellard | env->regs[3],
|
293 | b346ff46 | bellard | env->regs[4],
|
294 | b346ff46 | bellard | 0);
|
295 | b346ff46 | bellard | } else {
|
296 | b346ff46 | bellard | goto error;
|
297 | b346ff46 | bellard | } |
298 | b346ff46 | bellard | } |
299 | b346ff46 | bellard | break;
|
300 | 43fff238 | bellard | case EXCP_INTERRUPT:
|
301 | 43fff238 | bellard | /* just indicate that signals should be handled asap */
|
302 | 43fff238 | bellard | break;
|
303 | b346ff46 | bellard | default:
|
304 | b346ff46 | bellard | error:
|
305 | b346ff46 | bellard | fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
|
306 | b346ff46 | bellard | trapnr); |
307 | b346ff46 | bellard | cpu_arm_dump_state(env, stderr, 0);
|
308 | b346ff46 | bellard | abort(); |
309 | b346ff46 | bellard | } |
310 | b346ff46 | bellard | process_pending_signals(env); |
311 | b346ff46 | bellard | } |
312 | b346ff46 | bellard | } |
313 | b346ff46 | bellard | |
314 | b346ff46 | bellard | #endif
|
315 | 1b6b029e | bellard | |
316 | 93ac68bc | bellard | #ifdef TARGET_SPARC
|
317 | 93ac68bc | bellard | |
318 | 060366c5 | bellard | //#define DEBUG_WIN
|
319 | 060366c5 | bellard | |
320 | 060366c5 | bellard | /* WARNING: dealing with register windows _is_ complicated */
|
321 | 060366c5 | bellard | static inline int get_reg_index(CPUSPARCState *env, int cwp, int index) |
322 | 060366c5 | bellard | { |
323 | 060366c5 | bellard | index = (index + cwp * 16) & (16 * NWINDOWS - 1); |
324 | 060366c5 | bellard | /* wrap handling : if cwp is on the last window, then we use the
|
325 | 060366c5 | bellard | registers 'after' the end */
|
326 | 060366c5 | bellard | if (index < 8 && env->cwp == (NWINDOWS - 1)) |
327 | 060366c5 | bellard | index += (16 * NWINDOWS);
|
328 | 060366c5 | bellard | return index;
|
329 | 060366c5 | bellard | } |
330 | 060366c5 | bellard | |
331 | 060366c5 | bellard | static inline void save_window_offset(CPUSPARCState *env, int offset) |
332 | 060366c5 | bellard | { |
333 | 060366c5 | bellard | unsigned int new_wim, i, cwp1; |
334 | 060366c5 | bellard | uint32_t *sp_ptr; |
335 | 060366c5 | bellard | |
336 | 060366c5 | bellard | new_wim = ((env->wim >> 1) | (env->wim << (NWINDOWS - 1))) & |
337 | 060366c5 | bellard | ((1LL << NWINDOWS) - 1); |
338 | 060366c5 | bellard | /* save the window */
|
339 | 060366c5 | bellard | cwp1 = (env->cwp + offset) & (NWINDOWS - 1);
|
340 | 060366c5 | bellard | sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]);
|
341 | 060366c5 | bellard | #if defined(DEBUG_WIN)
|
342 | 060366c5 | bellard | printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n",
|
343 | 060366c5 | bellard | (int)sp_ptr, cwp1);
|
344 | 060366c5 | bellard | #endif
|
345 | 060366c5 | bellard | for(i = 0; i < 16; i++) |
346 | 060366c5 | bellard | stl_raw(sp_ptr + i, env->regbase[get_reg_index(env, cwp1, 8 + i)]);
|
347 | 060366c5 | bellard | env->wim = new_wim; |
348 | 060366c5 | bellard | } |
349 | 060366c5 | bellard | |
350 | 060366c5 | bellard | static void save_window(CPUSPARCState *env) |
351 | 060366c5 | bellard | { |
352 | 060366c5 | bellard | save_window_offset(env, 2);
|
353 | 060366c5 | bellard | } |
354 | 060366c5 | bellard | |
355 | 060366c5 | bellard | static void restore_window(CPUSPARCState *env) |
356 | 060366c5 | bellard | { |
357 | 060366c5 | bellard | unsigned int new_wim, i, cwp1; |
358 | 060366c5 | bellard | uint32_t *sp_ptr; |
359 | 060366c5 | bellard | |
360 | 060366c5 | bellard | new_wim = ((env->wim << 1) | (env->wim >> (NWINDOWS - 1))) & |
361 | 060366c5 | bellard | ((1LL << NWINDOWS) - 1); |
362 | 060366c5 | bellard | |
363 | 060366c5 | bellard | /* restore the invalid window */
|
364 | 060366c5 | bellard | cwp1 = (env->cwp + 1) & (NWINDOWS - 1); |
365 | 060366c5 | bellard | sp_ptr = (uint32_t *)(env->regbase[get_reg_index(env, cwp1, 6)]);
|
366 | 060366c5 | bellard | #if defined(DEBUG_WIN)
|
367 | 060366c5 | bellard | printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n",
|
368 | 060366c5 | bellard | (int)sp_ptr, cwp1);
|
369 | 060366c5 | bellard | #endif
|
370 | 060366c5 | bellard | for(i = 0; i < 16; i++) |
371 | 060366c5 | bellard | env->regbase[get_reg_index(env, cwp1, 8 + i)] = ldl_raw(sp_ptr + i);
|
372 | 060366c5 | bellard | env->wim = new_wim; |
373 | 060366c5 | bellard | } |
374 | 060366c5 | bellard | |
375 | 060366c5 | bellard | static void flush_windows(CPUSPARCState *env) |
376 | 060366c5 | bellard | { |
377 | 060366c5 | bellard | int offset, cwp1;
|
378 | 060366c5 | bellard | #if defined(DEBUG_WIN)
|
379 | 060366c5 | bellard | printf("flush_windows:\n");
|
380 | 060366c5 | bellard | #endif
|
381 | 060366c5 | bellard | offset = 2;
|
382 | 060366c5 | bellard | for(;;) {
|
383 | 060366c5 | bellard | /* if restore would invoke restore_window(), then we can stop */
|
384 | 060366c5 | bellard | cwp1 = (env->cwp + 1) & (NWINDOWS - 1); |
385 | 060366c5 | bellard | if (env->wim & (1 << cwp1)) |
386 | 060366c5 | bellard | break;
|
387 | 060366c5 | bellard | #if defined(DEBUG_WIN)
|
388 | 060366c5 | bellard | printf("offset=%d: ", offset);
|
389 | 060366c5 | bellard | #endif
|
390 | 060366c5 | bellard | save_window_offset(env, offset); |
391 | 060366c5 | bellard | offset++; |
392 | 060366c5 | bellard | } |
393 | 060366c5 | bellard | } |
394 | 060366c5 | bellard | |
395 | 93ac68bc | bellard | void cpu_loop (CPUSPARCState *env)
|
396 | 93ac68bc | bellard | { |
397 | 060366c5 | bellard | int trapnr, ret;
|
398 | 060366c5 | bellard | |
399 | 060366c5 | bellard | while (1) { |
400 | 060366c5 | bellard | trapnr = cpu_sparc_exec (env); |
401 | 060366c5 | bellard | |
402 | 060366c5 | bellard | switch (trapnr) {
|
403 | 060366c5 | bellard | case 0x88: |
404 | 060366c5 | bellard | case 0x90: |
405 | 060366c5 | bellard | ret = do_syscall (env, env->gregs[1],
|
406 | 060366c5 | bellard | env->regwptr[0], env->regwptr[1], |
407 | 060366c5 | bellard | env->regwptr[2], env->regwptr[3], |
408 | 060366c5 | bellard | env->regwptr[4], env->regwptr[5]); |
409 | 060366c5 | bellard | if ((unsigned int)ret >= (unsigned int)(-515)) { |
410 | 060366c5 | bellard | env->psr |= PSR_CARRY; |
411 | 060366c5 | bellard | ret = -ret; |
412 | 060366c5 | bellard | } else {
|
413 | 060366c5 | bellard | env->psr &= ~PSR_CARRY; |
414 | 060366c5 | bellard | } |
415 | 060366c5 | bellard | env->regwptr[0] = ret;
|
416 | 060366c5 | bellard | /* next instruction */
|
417 | 060366c5 | bellard | env->pc = env->npc; |
418 | 060366c5 | bellard | env->npc = env->npc + 4;
|
419 | 060366c5 | bellard | break;
|
420 | 060366c5 | bellard | case 0x83: /* flush windows */ |
421 | 060366c5 | bellard | // flush_windows(env);
|
422 | 060366c5 | bellard | /* next instruction */
|
423 | 060366c5 | bellard | env->pc = env->npc; |
424 | 060366c5 | bellard | env->npc = env->npc + 4;
|
425 | 060366c5 | bellard | break;
|
426 | 060366c5 | bellard | case TT_WIN_OVF: /* window overflow */ |
427 | 060366c5 | bellard | save_window(env); |
428 | 060366c5 | bellard | break;
|
429 | 060366c5 | bellard | case TT_WIN_UNF: /* window underflow */ |
430 | 060366c5 | bellard | restore_window(env); |
431 | 060366c5 | bellard | break;
|
432 | 060366c5 | bellard | default:
|
433 | 060366c5 | bellard | printf ("Unhandled trap: 0x%x\n", trapnr);
|
434 | 060366c5 | bellard | cpu_sparc_dump_state(env, stderr, 0);
|
435 | 060366c5 | bellard | exit (1);
|
436 | 060366c5 | bellard | } |
437 | 060366c5 | bellard | process_pending_signals (env); |
438 | 060366c5 | bellard | } |
439 | 93ac68bc | bellard | } |
440 | 93ac68bc | bellard | |
441 | 93ac68bc | bellard | #endif
|
442 | 93ac68bc | bellard | |
443 | 67867308 | bellard | #ifdef TARGET_PPC
|
444 | 67867308 | bellard | |
445 | 67867308 | bellard | void cpu_loop(CPUPPCState *env)
|
446 | 67867308 | bellard | { |
447 | 67867308 | bellard | target_siginfo_t info; |
448 | 61190b14 | bellard | int trapnr;
|
449 | 61190b14 | bellard | uint32_t ret; |
450 | 67867308 | bellard | |
451 | 67867308 | bellard | for(;;) {
|
452 | 67867308 | bellard | trapnr = cpu_ppc_exec(env); |
453 | 61190b14 | bellard | if (trapnr != EXCP_SYSCALL_USER && trapnr != EXCP_BRANCH &&
|
454 | 61190b14 | bellard | trapnr != EXCP_TRACE) { |
455 | 61190b14 | bellard | if (loglevel > 0) { |
456 | 61190b14 | bellard | cpu_ppc_dump_state(env, logfile, 0);
|
457 | 61190b14 | bellard | } |
458 | 61190b14 | bellard | } |
459 | 67867308 | bellard | switch(trapnr) {
|
460 | 67867308 | bellard | case EXCP_NONE:
|
461 | 67867308 | bellard | break;
|
462 | 61190b14 | bellard | case EXCP_SYSCALL_USER:
|
463 | 61190b14 | bellard | /* system call */
|
464 | 61190b14 | bellard | /* WARNING:
|
465 | 61190b14 | bellard | * PPC ABI uses overflow flag in cr0 to signal an error
|
466 | 61190b14 | bellard | * in syscalls.
|
467 | 61190b14 | bellard | */
|
468 | 67867308 | bellard | #if 0
|
469 | 61190b14 | bellard | printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
|
470 | 61190b14 | bellard | env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
|
471 | 67867308 | bellard | #endif
|
472 | 61190b14 | bellard | env->crf[0] &= ~0x1; |
473 | 61190b14 | bellard | ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4], |
474 | 61190b14 | bellard | env->gpr[5], env->gpr[6], env->gpr[7], |
475 | 61190b14 | bellard | env->gpr[8]);
|
476 | 61190b14 | bellard | if (ret > (uint32_t)(-515)) { |
477 | 61190b14 | bellard | env->crf[0] |= 0x1; |
478 | 61190b14 | bellard | ret = -ret; |
479 | 61190b14 | bellard | } |
480 | 61190b14 | bellard | env->gpr[3] = ret;
|
481 | 61190b14 | bellard | #if 0
|
482 | 61190b14 | bellard | printf("syscall returned 0x%08x (%d)\n", ret, ret);
|
483 | 61190b14 | bellard | #endif
|
484 | 61190b14 | bellard | break;
|
485 | 61190b14 | bellard | case EXCP_RESET:
|
486 | 61190b14 | bellard | /* Should not happen ! */
|
487 | 61190b14 | bellard | fprintf(stderr, "RESET asked... Stop emulation\n");
|
488 | 61190b14 | bellard | if (loglevel)
|
489 | 61190b14 | bellard | fprintf(logfile, "RESET asked... Stop emulation\n");
|
490 | 67867308 | bellard | abort(); |
491 | 61190b14 | bellard | case EXCP_MACHINE_CHECK:
|
492 | 61190b14 | bellard | fprintf(stderr, "Machine check exeption... Stop emulation\n");
|
493 | 61190b14 | bellard | if (loglevel)
|
494 | 61190b14 | bellard | fprintf(logfile, "RESET asked... Stop emulation\n");
|
495 | 61190b14 | bellard | info.si_signo = TARGET_SIGBUS; |
496 | 67867308 | bellard | info.si_errno = 0;
|
497 | 61190b14 | bellard | info.si_code = TARGET_BUS_OBJERR; |
498 | 61190b14 | bellard | info._sifields._sigfault._addr = env->nip - 4;
|
499 | 61190b14 | bellard | queue_signal(info.si_signo, &info); |
500 | 61190b14 | bellard | case EXCP_DSI:
|
501 | 61190b14 | bellard | fprintf(stderr, "Invalid data memory access: 0x%08x\n", env->spr[DAR]);
|
502 | 61190b14 | bellard | if (loglevel) {
|
503 | 61190b14 | bellard | fprintf(logfile, "Invalid data memory access: 0x%08x\n",
|
504 | 61190b14 | bellard | env->spr[DAR]); |
505 | 61190b14 | bellard | } |
506 | 61190b14 | bellard | switch (env->error_code & 0xF) { |
507 | 61190b14 | bellard | case EXCP_DSI_TRANSLATE:
|
508 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
509 | 61190b14 | bellard | info.si_errno = 0;
|
510 | 61190b14 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
511 | 61190b14 | bellard | break;
|
512 | 61190b14 | bellard | case EXCP_DSI_NOTSUP:
|
513 | 61190b14 | bellard | case EXCP_DSI_EXTERNAL:
|
514 | 61190b14 | bellard | info.si_signo = TARGET_SIGILL; |
515 | 61190b14 | bellard | info.si_errno = 0;
|
516 | 61190b14 | bellard | info.si_code = TARGET_ILL_ILLADR; |
517 | 61190b14 | bellard | break;
|
518 | 61190b14 | bellard | case EXCP_DSI_PROT:
|
519 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
520 | 61190b14 | bellard | info.si_errno = 0;
|
521 | 61190b14 | bellard | info.si_code = TARGET_SEGV_ACCERR; |
522 | 61190b14 | bellard | break;
|
523 | 61190b14 | bellard | case EXCP_DSI_DABR:
|
524 | 61190b14 | bellard | info.si_signo = TARGET_SIGTRAP; |
525 | 61190b14 | bellard | info.si_errno = 0;
|
526 | 61190b14 | bellard | info.si_code = TARGET_TRAP_BRKPT; |
527 | 61190b14 | bellard | break;
|
528 | 61190b14 | bellard | default:
|
529 | 61190b14 | bellard | /* Let's send a regular segfault... */
|
530 | 61190b14 | bellard | fprintf(stderr, "Invalid segfault errno (%02x)\n",
|
531 | 61190b14 | bellard | env->error_code); |
532 | 61190b14 | bellard | if (loglevel) {
|
533 | 61190b14 | bellard | fprintf(logfile, "Invalid segfault errno (%02x)\n",
|
534 | 61190b14 | bellard | env->error_code); |
535 | 61190b14 | bellard | } |
536 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
537 | 61190b14 | bellard | info.si_errno = 0;
|
538 | 61190b14 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
539 | 61190b14 | bellard | break;
|
540 | 61190b14 | bellard | } |
541 | 67867308 | bellard | info._sifields._sigfault._addr = env->nip; |
542 | 67867308 | bellard | queue_signal(info.si_signo, &info); |
543 | 67867308 | bellard | break;
|
544 | 61190b14 | bellard | case EXCP_ISI:
|
545 | 67867308 | bellard | fprintf(stderr, "Invalid instruction fetch\n");
|
546 | 61190b14 | bellard | if (loglevel)
|
547 | 61190b14 | bellard | fprintf(logfile, "Invalid instruction fetch\n");
|
548 | 61190b14 | bellard | switch (env->error_code) {
|
549 | 61190b14 | bellard | case EXCP_ISI_TRANSLATE:
|
550 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
551 | 67867308 | bellard | info.si_errno = 0;
|
552 | 61190b14 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
553 | 61190b14 | bellard | break;
|
554 | 61190b14 | bellard | case EXCP_ISI_GUARD:
|
555 | 61190b14 | bellard | info.si_signo = TARGET_SIGILL; |
556 | 61190b14 | bellard | info.si_errno = 0;
|
557 | 61190b14 | bellard | info.si_code = TARGET_ILL_ILLADR; |
558 | 61190b14 | bellard | break;
|
559 | 61190b14 | bellard | case EXCP_ISI_NOEXEC:
|
560 | 61190b14 | bellard | case EXCP_ISI_PROT:
|
561 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
562 | 61190b14 | bellard | info.si_errno = 0;
|
563 | 61190b14 | bellard | info.si_code = TARGET_SEGV_ACCERR; |
564 | 61190b14 | bellard | break;
|
565 | 61190b14 | bellard | default:
|
566 | 61190b14 | bellard | /* Let's send a regular segfault... */
|
567 | 61190b14 | bellard | fprintf(stderr, "Invalid segfault errno (%02x)\n",
|
568 | 61190b14 | bellard | env->error_code); |
569 | 61190b14 | bellard | if (loglevel) {
|
570 | 61190b14 | bellard | fprintf(logfile, "Invalid segfault errno (%02x)\n",
|
571 | 61190b14 | bellard | env->error_code); |
572 | 61190b14 | bellard | } |
573 | 61190b14 | bellard | info.si_signo = TARGET_SIGSEGV; |
574 | 61190b14 | bellard | info.si_errno = 0;
|
575 | 61190b14 | bellard | info.si_code = TARGET_SEGV_MAPERR; |
576 | 61190b14 | bellard | break;
|
577 | 61190b14 | bellard | } |
578 | 61190b14 | bellard | info._sifields._sigfault._addr = env->nip - 4;
|
579 | 67867308 | bellard | queue_signal(info.si_signo, &info); |
580 | 67867308 | bellard | break;
|
581 | 61190b14 | bellard | case EXCP_EXTERNAL:
|
582 | 61190b14 | bellard | /* Should not happen ! */
|
583 | 61190b14 | bellard | fprintf(stderr, "External interruption... Stop emulation\n");
|
584 | 61190b14 | bellard | if (loglevel)
|
585 | 61190b14 | bellard | fprintf(logfile, "External interruption... Stop emulation\n");
|
586 | 67867308 | bellard | abort(); |
587 | 61190b14 | bellard | case EXCP_ALIGN:
|
588 | 61190b14 | bellard | fprintf(stderr, "Invalid unaligned memory access\n");
|
589 | 61190b14 | bellard | if (loglevel)
|
590 | 61190b14 | bellard | fprintf(logfile, "Invalid unaligned memory access\n");
|
591 | 61190b14 | bellard | info.si_signo = TARGET_SIGBUS; |
592 | 67867308 | bellard | info.si_errno = 0;
|
593 | 61190b14 | bellard | info.si_code = TARGET_BUS_ADRALN; |
594 | 61190b14 | bellard | info._sifields._sigfault._addr = env->nip - 4;
|
595 | 67867308 | bellard | queue_signal(info.si_signo, &info); |
596 | 67867308 | bellard | break;
|
597 | 61190b14 | bellard | case EXCP_PROGRAM:
|
598 | 61190b14 | bellard | switch (env->error_code & ~0xF) { |
599 | 61190b14 | bellard | case EXCP_FP:
|
600 | 61190b14 | bellard | fprintf(stderr, "Program exception\n");
|
601 | 61190b14 | bellard | if (loglevel)
|
602 | 61190b14 | bellard | fprintf(logfile, "Program exception\n");
|
603 | 61190b14 | bellard | /* Set FX */
|
604 | 61190b14 | bellard | env->fpscr[7] |= 0x8; |
605 | 61190b14 | bellard | /* Finally, update FEX */
|
606 | 61190b14 | bellard | if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) & |
607 | 61190b14 | bellard | ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3))) |
608 | 61190b14 | bellard | env->fpscr[7] |= 0x4; |
609 | 61190b14 | bellard | info.si_signo = TARGET_SIGFPE; |
610 | 61190b14 | bellard | info.si_errno = 0;
|
611 | 61190b14 | bellard | switch (env->error_code & 0xF) { |
612 | 61190b14 | bellard | case EXCP_FP_OX:
|
613 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTOVF; |
614 | 61190b14 | bellard | break;
|
615 | 61190b14 | bellard | case EXCP_FP_UX:
|
616 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTUND; |
617 | 61190b14 | bellard | break;
|
618 | 61190b14 | bellard | case EXCP_FP_ZX:
|
619 | 61190b14 | bellard | case EXCP_FP_VXZDZ:
|
620 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTDIV; |
621 | 61190b14 | bellard | break;
|
622 | 61190b14 | bellard | case EXCP_FP_XX:
|
623 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTRES; |
624 | 61190b14 | bellard | break;
|
625 | 61190b14 | bellard | case EXCP_FP_VXSOFT:
|
626 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTINV; |
627 | 61190b14 | bellard | break;
|
628 | 61190b14 | bellard | case EXCP_FP_VXNAN:
|
629 | 61190b14 | bellard | case EXCP_FP_VXISI:
|
630 | 61190b14 | bellard | case EXCP_FP_VXIDI:
|
631 | 61190b14 | bellard | case EXCP_FP_VXIMZ:
|
632 | 61190b14 | bellard | case EXCP_FP_VXVC:
|
633 | 61190b14 | bellard | case EXCP_FP_VXSQRT:
|
634 | 61190b14 | bellard | case EXCP_FP_VXCVI:
|
635 | 61190b14 | bellard | info.si_code = TARGET_FPE_FLTSUB; |
636 | 61190b14 | bellard | break;
|
637 | 61190b14 | bellard | default:
|
638 | 61190b14 | bellard | fprintf(stderr, "Unknown floating point exception "
|
639 | 61190b14 | bellard | "(%02x)\n", env->error_code);
|
640 | 61190b14 | bellard | if (loglevel) {
|
641 | 61190b14 | bellard | fprintf(logfile, "Unknown floating point exception "
|
642 | 61190b14 | bellard | "(%02x)\n", env->error_code & 0xF); |
643 | 61190b14 | bellard | } |
644 | 61190b14 | bellard | } |
645 | 61190b14 | bellard | break;
|
646 | 67867308 | bellard | case EXCP_INVAL:
|
647 | 61190b14 | bellard | fprintf(stderr, "Invalid instruction\n");
|
648 | 61190b14 | bellard | if (loglevel)
|
649 | 61190b14 | bellard | fprintf(logfile, "Invalid instruction\n");
|
650 | 61190b14 | bellard | info.si_signo = TARGET_SIGILL; |
651 | 61190b14 | bellard | info.si_errno = 0;
|
652 | 61190b14 | bellard | switch (env->error_code & 0xF) { |
653 | 61190b14 | bellard | case EXCP_INVAL_INVAL:
|
654 | 61190b14 | bellard | info.si_code = TARGET_ILL_ILLOPC; |
655 | 61190b14 | bellard | break;
|
656 | 61190b14 | bellard | case EXCP_INVAL_LSWX:
|
657 | 67867308 | bellard | info.si_code = TARGET_ILL_ILLOPN; |
658 | 61190b14 | bellard | break;
|
659 | 61190b14 | bellard | case EXCP_INVAL_SPR:
|
660 | 61190b14 | bellard | info.si_code = TARGET_ILL_PRVREG; |
661 | 61190b14 | bellard | break;
|
662 | 61190b14 | bellard | case EXCP_INVAL_FP:
|
663 | 61190b14 | bellard | info.si_code = TARGET_ILL_COPROC; |
664 | 61190b14 | bellard | break;
|
665 | 61190b14 | bellard | default:
|
666 | 61190b14 | bellard | fprintf(stderr, "Unknown invalid operation (%02x)\n",
|
667 | 61190b14 | bellard | env->error_code & 0xF);
|
668 | 61190b14 | bellard | if (loglevel) {
|
669 | 61190b14 | bellard | fprintf(logfile, "Unknown invalid operation (%02x)\n",
|
670 | 61190b14 | bellard | env->error_code & 0xF);
|
671 | 61190b14 | bellard | } |
672 | 61190b14 | bellard | info.si_code = TARGET_ILL_ILLADR; |
673 | 61190b14 | bellard | break;
|
674 | 61190b14 | bellard | } |
675 | 61190b14 | bellard | break;
|
676 | 61190b14 | bellard | case EXCP_PRIV:
|
677 | 61190b14 | bellard | fprintf(stderr, "Privilege violation\n");
|
678 | 61190b14 | bellard | if (loglevel)
|
679 | 61190b14 | bellard | fprintf(logfile, "Privilege violation\n");
|
680 | 61190b14 | bellard | info.si_signo = TARGET_SIGILL; |
681 | 61190b14 | bellard | info.si_errno = 0;
|
682 | 61190b14 | bellard | switch (env->error_code & 0xF) { |
683 | 61190b14 | bellard | case EXCP_PRIV_OPC:
|
684 | 61190b14 | bellard | info.si_code = TARGET_ILL_PRVOPC; |
685 | 61190b14 | bellard | break;
|
686 | 61190b14 | bellard | case EXCP_PRIV_REG:
|
687 | 61190b14 | bellard | info.si_code = TARGET_ILL_PRVREG; |
688 | 61190b14 | bellard | break;
|
689 | 61190b14 | bellard | default:
|
690 | 61190b14 | bellard | fprintf(stderr, "Unknown privilege violation (%02x)\n",
|
691 | 61190b14 | bellard | env->error_code & 0xF);
|
692 | 61190b14 | bellard | info.si_code = TARGET_ILL_PRVOPC; |
693 | 61190b14 | bellard | break;
|
694 | 61190b14 | bellard | } |
695 | 61190b14 | bellard | break;
|
696 | 61190b14 | bellard | case EXCP_TRAP:
|
697 | 61190b14 | bellard | fprintf(stderr, "Tried to call a TRAP\n");
|
698 | 61190b14 | bellard | if (loglevel)
|
699 | 61190b14 | bellard | fprintf(logfile, "Tried to call a TRAP\n");
|
700 | 61190b14 | bellard | abort(); |
701 | 61190b14 | bellard | default:
|
702 | 61190b14 | bellard | /* Should not happen ! */
|
703 | 61190b14 | bellard | fprintf(stderr, "Unknown program exception (%02x)\n",
|
704 | 61190b14 | bellard | env->error_code); |
705 | 61190b14 | bellard | if (loglevel) {
|
706 | 61190b14 | bellard | fprintf(logfile, "Unknwon program exception (%02x)\n",
|
707 | 61190b14 | bellard | env->error_code); |
708 | 61190b14 | bellard | } |
709 | 61190b14 | bellard | abort(); |
710 | 61190b14 | bellard | } |
711 | 61190b14 | bellard | info._sifields._sigfault._addr = env->nip - 4;
|
712 | 67867308 | bellard | queue_signal(info.si_signo, &info); |
713 | 67867308 | bellard | break;
|
714 | 61190b14 | bellard | case EXCP_NO_FP:
|
715 | 61190b14 | bellard | fprintf(stderr, "No floating point allowed\n");
|
716 | 61190b14 | bellard | if (loglevel)
|
717 | 61190b14 | bellard | fprintf(logfile, "No floating point allowed\n");
|
718 | 61190b14 | bellard | info.si_signo = TARGET_SIGILL; |
719 | 67867308 | bellard | info.si_errno = 0;
|
720 | 61190b14 | bellard | info.si_code = TARGET_ILL_COPROC; |
721 | 61190b14 | bellard | info._sifields._sigfault._addr = env->nip - 4;
|
722 | 67867308 | bellard | queue_signal(info.si_signo, &info); |
723 | 67867308 | bellard | break;
|
724 | 61190b14 | bellard | case EXCP_DECR:
|
725 | 61190b14 | bellard | /* Should not happen ! */
|
726 | 61190b14 | bellard | fprintf(stderr, "Decrementer exception\n");
|
727 | 61190b14 | bellard | if (loglevel)
|
728 | 61190b14 | bellard | fprintf(logfile, "Decrementer exception\n");
|
729 | 61190b14 | bellard | abort(); |
730 | 67867308 | bellard | case EXCP_RESA: /* Implementation specific */ |
731 | 61190b14 | bellard | /* Should not happen ! */
|
732 | 61190b14 | bellard | fprintf(stderr, "RESA exception should never happen !\n");
|
733 | 61190b14 | bellard | if (loglevel)
|
734 | 61190b14 | bellard | fprintf(logfile, "RESA exception should never happen !\n");
|
735 | 61190b14 | bellard | abort(); |
736 | 67867308 | bellard | case EXCP_RESB: /* Implementation specific */ |
737 | 61190b14 | bellard | /* Should not happen ! */
|
738 | 61190b14 | bellard | fprintf(stderr, "RESB exception should never happen !\n");
|
739 | 61190b14 | bellard | if (loglevel)
|
740 | 61190b14 | bellard | fprintf(logfile, "RESB exception should never happen !\n");
|
741 | 67867308 | bellard | abort(); |
742 | 61190b14 | bellard | case EXCP_TRACE:
|
743 | 61190b14 | bellard | /* Do nothing: we use this to trace execution */
|
744 | 67867308 | bellard | break;
|
745 | 61190b14 | bellard | case EXCP_FP_ASSIST:
|
746 | 61190b14 | bellard | /* Should not happen ! */
|
747 | 61190b14 | bellard | fprintf(stderr, "Floating point assist exception\n");
|
748 | 61190b14 | bellard | if (loglevel)
|
749 | 61190b14 | bellard | fprintf(logfile, "Floating point assist exception\n");
|
750 | 61190b14 | bellard | abort(); |
751 | 61190b14 | bellard | case EXCP_MTMSR:
|
752 | 61190b14 | bellard | /* We reloaded the msr, just go on */
|
753 | 61190b14 | bellard | if (msr_pr) {
|
754 | 61190b14 | bellard | fprintf(stderr, "Tried to go into supervisor mode !\n");
|
755 | 61190b14 | bellard | if (loglevel)
|
756 | 61190b14 | bellard | fprintf(logfile, "Tried to go into supervisor mode !\n");
|
757 | 61190b14 | bellard | abort(); |
758 | 67867308 | bellard | } |
759 | 61190b14 | bellard | break;
|
760 | 61190b14 | bellard | case EXCP_BRANCH:
|
761 | 61190b14 | bellard | /* We stopped because of a jump... */
|
762 | 61190b14 | bellard | break;
|
763 | 61190b14 | bellard | case EXCP_RFI:
|
764 | 61190b14 | bellard | /* Should not occur: we always are in user mode */
|
765 | 61190b14 | bellard | fprintf(stderr, "Return from interrupt ?\n");
|
766 | 61190b14 | bellard | if (loglevel)
|
767 | 61190b14 | bellard | fprintf(logfile, "Return from interrupt ?\n");
|
768 | 61190b14 | bellard | abort(); |
769 | 61190b14 | bellard | case EXCP_INTERRUPT:
|
770 | 61190b14 | bellard | /* Don't know why this should ever happen... */
|
771 | 61190b14 | bellard | break;
|
772 | 67867308 | bellard | default:
|
773 | 67867308 | bellard | fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
|
774 | 67867308 | bellard | trapnr); |
775 | 61190b14 | bellard | if (loglevel) {
|
776 | 61190b14 | bellard | fprintf(logfile, "qemu: unhandled CPU exception 0x%02x - "
|
777 | 61190b14 | bellard | "0x%02x - aborting\n", trapnr, env->error_code);
|
778 | 61190b14 | bellard | } |
779 | 67867308 | bellard | abort(); |
780 | 67867308 | bellard | } |
781 | 61190b14 | bellard | if (trapnr < EXCP_PPC_MAX)
|
782 | 61190b14 | bellard | env->exceptions &= ~(1 << trapnr);
|
783 | 67867308 | bellard | process_pending_signals(env); |
784 | 61190b14 | bellard | if (env->exceptions != 0) { |
785 | 61190b14 | bellard | check_exception_state(env); |
786 | 61190b14 | bellard | } |
787 | 67867308 | bellard | } |
788 | 67867308 | bellard | } |
789 | 67867308 | bellard | #endif
|
790 | 67867308 | bellard | |
791 | 31e31b8a | bellard | void usage(void) |
792 | 31e31b8a | bellard | { |
793 | 93ac68bc | bellard | printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
794 | 93ac68bc | bellard | "usage: qemu-" TARGET_ARCH " [-h] [-d] [-L path] [-s size] program [arguments...]\n" |
795 | b346ff46 | bellard | "Linux CPU emulator (compiled for %s emulation)\n"
|
796 | d691f669 | bellard | "\n"
|
797 | 54936004 | bellard | "-h print this help\n"
|
798 | b346ff46 | bellard | "-L path set the elf interpreter prefix (default=%s)\n"
|
799 | b346ff46 | bellard | "-s size set the stack size in bytes (default=%ld)\n"
|
800 | 54936004 | bellard | "\n"
|
801 | 54936004 | bellard | "debug options:\n"
|
802 | c6981055 | bellard | #ifdef USE_CODE_COPY
|
803 | c6981055 | bellard | "-no-code-copy disable code copy acceleration\n"
|
804 | c6981055 | bellard | #endif
|
805 | 54936004 | bellard | "-d activate log (logfile=%s)\n"
|
806 | 54936004 | bellard | "-p pagesize set the host page size to 'pagesize'\n",
|
807 | b346ff46 | bellard | TARGET_ARCH, |
808 | d691f669 | bellard | interp_prefix, |
809 | 54936004 | bellard | x86_stack_size, |
810 | 54936004 | bellard | DEBUG_LOGFILE); |
811 | 74cd30b8 | bellard | _exit(1);
|
812 | 31e31b8a | bellard | } |
813 | 31e31b8a | bellard | |
814 | 9de5e440 | bellard | /* XXX: currently only used for async signals (see signal.c) */
|
815 | b346ff46 | bellard | CPUState *global_env; |
816 | 59faf6d6 | bellard | /* used only if single thread */
|
817 | 59faf6d6 | bellard | CPUState *cpu_single_env = NULL;
|
818 | 59faf6d6 | bellard | |
819 | 851e67a1 | bellard | /* used to free thread contexts */
|
820 | 851e67a1 | bellard | TaskState *first_task_state; |
821 | 9de5e440 | bellard | |
822 | 31e31b8a | bellard | int main(int argc, char **argv) |
823 | 31e31b8a | bellard | { |
824 | 31e31b8a | bellard | const char *filename; |
825 | 01ffc75b | bellard | struct target_pt_regs regs1, *regs = ®s1;
|
826 | 31e31b8a | bellard | struct image_info info1, *info = &info1;
|
827 | 851e67a1 | bellard | TaskState ts1, *ts = &ts1; |
828 | b346ff46 | bellard | CPUState *env; |
829 | 586314f2 | bellard | int optind;
|
830 | d691f669 | bellard | const char *r; |
831 | d691f669 | bellard | |
832 | 31e31b8a | bellard | if (argc <= 1) |
833 | 31e31b8a | bellard | usage(); |
834 | f801f97e | bellard | |
835 | cc38b844 | bellard | /* init debug */
|
836 | cc38b844 | bellard | cpu_set_log_filename(DEBUG_LOGFILE); |
837 | cc38b844 | bellard | |
838 | 586314f2 | bellard | optind = 1;
|
839 | d691f669 | bellard | for(;;) {
|
840 | d691f669 | bellard | if (optind >= argc)
|
841 | d691f669 | bellard | break;
|
842 | d691f669 | bellard | r = argv[optind]; |
843 | d691f669 | bellard | if (r[0] != '-') |
844 | d691f669 | bellard | break;
|
845 | 586314f2 | bellard | optind++; |
846 | d691f669 | bellard | r++; |
847 | d691f669 | bellard | if (!strcmp(r, "-")) { |
848 | d691f669 | bellard | break;
|
849 | d691f669 | bellard | } else if (!strcmp(r, "d")) { |
850 | cc38b844 | bellard | cpu_set_log(CPU_LOG_ALL); |
851 | d691f669 | bellard | } else if (!strcmp(r, "s")) { |
852 | d691f669 | bellard | r = argv[optind++]; |
853 | d691f669 | bellard | x86_stack_size = strtol(r, (char **)&r, 0); |
854 | d691f669 | bellard | if (x86_stack_size <= 0) |
855 | d691f669 | bellard | usage(); |
856 | d691f669 | bellard | if (*r == 'M') |
857 | d691f669 | bellard | x86_stack_size *= 1024 * 1024; |
858 | d691f669 | bellard | else if (*r == 'k' || *r == 'K') |
859 | d691f669 | bellard | x86_stack_size *= 1024;
|
860 | d691f669 | bellard | } else if (!strcmp(r, "L")) { |
861 | d691f669 | bellard | interp_prefix = argv[optind++]; |
862 | 54936004 | bellard | } else if (!strcmp(r, "p")) { |
863 | 54936004 | bellard | host_page_size = atoi(argv[optind++]); |
864 | 54936004 | bellard | if (host_page_size == 0 || |
865 | 54936004 | bellard | (host_page_size & (host_page_size - 1)) != 0) { |
866 | 54936004 | bellard | fprintf(stderr, "page size must be a power of two\n");
|
867 | 54936004 | bellard | exit(1);
|
868 | 54936004 | bellard | } |
869 | c6981055 | bellard | } else
|
870 | c6981055 | bellard | #ifdef USE_CODE_COPY
|
871 | c6981055 | bellard | if (!strcmp(r, "no-code-copy")) { |
872 | c6981055 | bellard | code_copy_enabled = 0;
|
873 | c6981055 | bellard | } else
|
874 | c6981055 | bellard | #endif
|
875 | c6981055 | bellard | { |
876 | d691f669 | bellard | usage(); |
877 | d691f669 | bellard | } |
878 | 586314f2 | bellard | } |
879 | d691f669 | bellard | if (optind >= argc)
|
880 | d691f669 | bellard | usage(); |
881 | 586314f2 | bellard | filename = argv[optind]; |
882 | 586314f2 | bellard | |
883 | 31e31b8a | bellard | /* Zero out regs */
|
884 | 01ffc75b | bellard | memset(regs, 0, sizeof(struct target_pt_regs)); |
885 | 31e31b8a | bellard | |
886 | 31e31b8a | bellard | /* Zero out image_info */
|
887 | 31e31b8a | bellard | memset(info, 0, sizeof(struct image_info)); |
888 | 31e31b8a | bellard | |
889 | 74cd30b8 | bellard | /* Scan interp_prefix dir for replacement files. */
|
890 | 74cd30b8 | bellard | init_paths(interp_prefix); |
891 | 74cd30b8 | bellard | |
892 | 54936004 | bellard | /* NOTE: we need to init the CPU at this stage to get the
|
893 | 54936004 | bellard | host_page_size */
|
894 | b346ff46 | bellard | env = cpu_init(); |
895 | 92ccca6a | bellard | |
896 | 74cd30b8 | bellard | if (elf_exec(filename, argv+optind, environ, regs, info) != 0) { |
897 | 31e31b8a | bellard | printf("Error loading %s\n", filename);
|
898 | 74cd30b8 | bellard | _exit(1);
|
899 | 31e31b8a | bellard | } |
900 | 31e31b8a | bellard | |
901 | 4b74fe1f | bellard | if (loglevel) {
|
902 | 54936004 | bellard | page_dump(logfile); |
903 | 54936004 | bellard | |
904 | 4b74fe1f | bellard | fprintf(logfile, "start_brk 0x%08lx\n" , info->start_brk);
|
905 | 4b74fe1f | bellard | fprintf(logfile, "end_code 0x%08lx\n" , info->end_code);
|
906 | 4b74fe1f | bellard | fprintf(logfile, "start_code 0x%08lx\n" , info->start_code);
|
907 | 4b74fe1f | bellard | fprintf(logfile, "end_data 0x%08lx\n" , info->end_data);
|
908 | 4b74fe1f | bellard | fprintf(logfile, "start_stack 0x%08lx\n" , info->start_stack);
|
909 | 4b74fe1f | bellard | fprintf(logfile, "brk 0x%08lx\n" , info->brk);
|
910 | b346ff46 | bellard | fprintf(logfile, "entry 0x%08lx\n" , info->entry);
|
911 | 4b74fe1f | bellard | } |
912 | 31e31b8a | bellard | |
913 | 31e31b8a | bellard | target_set_brk((char *)info->brk);
|
914 | 31e31b8a | bellard | syscall_init(); |
915 | 66fb9763 | bellard | signal_init(); |
916 | 31e31b8a | bellard | |
917 | 9de5e440 | bellard | global_env = env; |
918 | 0ecfa993 | bellard | |
919 | 851e67a1 | bellard | /* build Task State */
|
920 | 851e67a1 | bellard | memset(ts, 0, sizeof(TaskState)); |
921 | 851e67a1 | bellard | env->opaque = ts; |
922 | 851e67a1 | bellard | ts->used = 1;
|
923 | 59faf6d6 | bellard | env->user_mode_only = 1;
|
924 | 851e67a1 | bellard | |
925 | b346ff46 | bellard | #if defined(TARGET_I386)
|
926 | 2e255c6b | bellard | cpu_x86_set_cpl(env, 3);
|
927 | 2e255c6b | bellard | |
928 | 3802ce26 | bellard | env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
|
929 | 75c6215f | bellard | env->hflags |= HF_PE_MASK; |
930 | 3802ce26 | bellard | |
931 | 415e561f | bellard | /* flags setup : we activate the IRQs by default as in user mode */
|
932 | 415e561f | bellard | env->eflags |= IF_MASK; |
933 | 415e561f | bellard | |
934 | 6dbad63e | bellard | /* linux register setup */
|
935 | 0ecfa993 | bellard | env->regs[R_EAX] = regs->eax; |
936 | 0ecfa993 | bellard | env->regs[R_EBX] = regs->ebx; |
937 | 0ecfa993 | bellard | env->regs[R_ECX] = regs->ecx; |
938 | 0ecfa993 | bellard | env->regs[R_EDX] = regs->edx; |
939 | 0ecfa993 | bellard | env->regs[R_ESI] = regs->esi; |
940 | 0ecfa993 | bellard | env->regs[R_EDI] = regs->edi; |
941 | 0ecfa993 | bellard | env->regs[R_EBP] = regs->ebp; |
942 | 0ecfa993 | bellard | env->regs[R_ESP] = regs->esp; |
943 | dab2ed99 | bellard | env->eip = regs->eip; |
944 | 31e31b8a | bellard | |
945 | f4beb510 | bellard | /* linux interrupt setup */
|
946 | f4beb510 | bellard | env->idt.base = (void *)idt_table;
|
947 | f4beb510 | bellard | env->idt.limit = sizeof(idt_table) - 1; |
948 | f4beb510 | bellard | set_idt(0, 0); |
949 | f4beb510 | bellard | set_idt(1, 0); |
950 | f4beb510 | bellard | set_idt(2, 0); |
951 | f4beb510 | bellard | set_idt(3, 3); |
952 | f4beb510 | bellard | set_idt(4, 3); |
953 | f4beb510 | bellard | set_idt(5, 3); |
954 | f4beb510 | bellard | set_idt(6, 0); |
955 | f4beb510 | bellard | set_idt(7, 0); |
956 | f4beb510 | bellard | set_idt(8, 0); |
957 | f4beb510 | bellard | set_idt(9, 0); |
958 | f4beb510 | bellard | set_idt(10, 0); |
959 | f4beb510 | bellard | set_idt(11, 0); |
960 | f4beb510 | bellard | set_idt(12, 0); |
961 | f4beb510 | bellard | set_idt(13, 0); |
962 | f4beb510 | bellard | set_idt(14, 0); |
963 | f4beb510 | bellard | set_idt(15, 0); |
964 | f4beb510 | bellard | set_idt(16, 0); |
965 | f4beb510 | bellard | set_idt(17, 0); |
966 | f4beb510 | bellard | set_idt(18, 0); |
967 | f4beb510 | bellard | set_idt(19, 0); |
968 | f4beb510 | bellard | set_idt(0x80, 3); |
969 | f4beb510 | bellard | |
970 | 6dbad63e | bellard | /* linux segment setup */
|
971 | 6dbad63e | bellard | env->gdt.base = (void *)gdt_table;
|
972 | 6dbad63e | bellard | env->gdt.limit = sizeof(gdt_table) - 1; |
973 | f4beb510 | bellard | write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff, |
974 | f4beb510 | bellard | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
975 | f4beb510 | bellard | (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT)); |
976 | f4beb510 | bellard | write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff, |
977 | f4beb510 | bellard | DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK | |
978 | f4beb510 | bellard | (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT)); |
979 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_CS, __USER_CS); |
980 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_DS, __USER_DS); |
981 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_ES, __USER_DS); |
982 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_SS, __USER_DS); |
983 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_FS, __USER_DS); |
984 | 6dbad63e | bellard | cpu_x86_load_seg(env, R_GS, __USER_DS); |
985 | 92ccca6a | bellard | |
986 | b346ff46 | bellard | #elif defined(TARGET_ARM)
|
987 | b346ff46 | bellard | { |
988 | b346ff46 | bellard | int i;
|
989 | b346ff46 | bellard | for(i = 0; i < 16; i++) { |
990 | b346ff46 | bellard | env->regs[i] = regs->uregs[i]; |
991 | b346ff46 | bellard | } |
992 | b346ff46 | bellard | env->cpsr = regs->uregs[16];
|
993 | b346ff46 | bellard | } |
994 | 93ac68bc | bellard | #elif defined(TARGET_SPARC)
|
995 | 060366c5 | bellard | { |
996 | 060366c5 | bellard | int i;
|
997 | 060366c5 | bellard | env->pc = regs->pc; |
998 | 060366c5 | bellard | env->npc = regs->npc; |
999 | 060366c5 | bellard | env->y = regs->y; |
1000 | 060366c5 | bellard | for(i = 0; i < 8; i++) |
1001 | 060366c5 | bellard | env->gregs[i] = regs->u_regs[i]; |
1002 | 060366c5 | bellard | for(i = 0; i < 8; i++) |
1003 | 060366c5 | bellard | env->regwptr[i] = regs->u_regs[i + 8];
|
1004 | 060366c5 | bellard | } |
1005 | 67867308 | bellard | #elif defined(TARGET_PPC)
|
1006 | 67867308 | bellard | { |
1007 | 67867308 | bellard | int i;
|
1008 | 61190b14 | bellard | for (i = 0; i < 32; i++) { |
1009 | 61190b14 | bellard | if (i != 12 && i != 6) |
1010 | 61190b14 | bellard | env->msr[i] = (regs->msr >> i) & 1;
|
1011 | 61190b14 | bellard | } |
1012 | 67867308 | bellard | env->nip = regs->nip; |
1013 | 67867308 | bellard | for(i = 0; i < 32; i++) { |
1014 | 67867308 | bellard | env->gpr[i] = regs->gpr[i]; |
1015 | 67867308 | bellard | } |
1016 | 67867308 | bellard | } |
1017 | b346ff46 | bellard | #else
|
1018 | b346ff46 | bellard | #error unsupported target CPU
|
1019 | b346ff46 | bellard | #endif
|
1020 | 31e31b8a | bellard | |
1021 | 1b6b029e | bellard | cpu_loop(env); |
1022 | 1b6b029e | bellard | /* never exits */
|
1023 | 31e31b8a | bellard | return 0; |
1024 | 31e31b8a | bellard | } |