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