]> git.proxmox.com Git - mirror_qemu.git/blame - linux-user/main.c
irq statistics code (initial patch by Jocelyn Mayer)
[mirror_qemu.git] / linux-user / main.c
CommitLineData
31e31b8a 1/*
93ac68bc 2 * qemu user main
31e31b8a
FB
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>
04369ff2 23#include <string.h>
31e31b8a 24#include <errno.h>
0ecfa993 25#include <unistd.h>
31e31b8a 26
3ef693a0 27#include "qemu.h"
31e31b8a 28
3ef693a0 29#define DEBUG_LOGFILE "/tmp/qemu.log"
586314f2 30
74cd30b8 31static const char *interp_prefix = CONFIG_QEMU_PREFIX;
586314f2 32
3a4739d6 33#if defined(__i386__) && !defined(CONFIG_STATIC)
f801f97e
FB
34/* Force usage of an ELF interpreter even if it is an ELF shared
35 object ! */
36const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
4304763b 37#endif
74cd30b8 38
93ac68bc 39/* for recent libc, we add these dummy symbols which are not declared
74cd30b8
FB
40 when generating a linked object (bug in ld ?) */
41#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)
c6981055
FB
42long __preinit_array_start[0];
43long __preinit_array_end[0];
74cd30b8
FB
44long __init_array_start[0];
45long __init_array_end[0];
46long __fini_array_start[0];
47long __fini_array_end[0];
48#endif
49
9de5e440
FB
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 */
53unsigned long x86_stack_size = 512 * 1024;
31e31b8a
FB
54
55void 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
61190b14 64void cpu_outb(CPUState *env, int addr, int val)
367e86e8
FB
65{
66 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
67}
68
61190b14 69void cpu_outw(CPUState *env, int addr, int val)
367e86e8
FB
70{
71 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
72}
73
61190b14 74void cpu_outl(CPUState *env, int addr, int val)
367e86e8
FB
75{
76 fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
77}
78
61190b14 79int cpu_inb(CPUState *env, int addr)
367e86e8
FB
80{
81 fprintf(stderr, "inb: port=0x%04x\n", addr);
82 return 0;
83}
84
61190b14 85int cpu_inw(CPUState *env, int addr)
367e86e8
FB
86{
87 fprintf(stderr, "inw: port=0x%04x\n", addr);
88 return 0;
89}
90
61190b14 91int cpu_inl(CPUState *env, int addr)
367e86e8
FB
92{
93 fprintf(stderr, "inl: port=0x%04x\n", addr);
94 return 0;
95}
96
a541f297 97int cpu_get_pic_interrupt(CPUState *env)
92ccca6a
FB
98{
99 return -1;
100}
101
28ab0e2e
FB
102/* timers for rdtsc */
103
104#if defined(__i386__)
105
106int64_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
115int64_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
128static uint64_t emu_time;
129
130int64_t cpu_get_real_ticks(void)
131{
132 return emu_time++;
133}
134
135#endif
136
a541f297
FB
137#ifdef TARGET_I386
138/***********************************************************/
139/* CPUX86 core interface */
140
28ab0e2e
FB
141uint64_t cpu_get_tsc(CPUX86State *env)
142{
143 return cpu_get_real_ticks();
144}
145
f4beb510
FB
146static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
147 int flags)
6dbad63e 148{
f4beb510 149 unsigned int e1, e2;
6dbad63e
FB
150 e1 = (addr << 16) | (limit & 0xffff);
151 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
f4beb510
FB
152 e2 |= flags;
153 stl((uint8_t *)ptr, e1);
154 stl((uint8_t *)ptr + 4, e2);
155}
156
157static 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);
6dbad63e
FB
163 stl((uint8_t *)ptr, e1);
164 stl((uint8_t *)ptr + 4, e2);
165}
166
167uint64_t gdt_table[6];
f4beb510
FB
168uint64_t idt_table[256];
169
170/* only dpl matters as we do only user space emulation */
171static void set_idt(int n, unsigned int dpl)
172{
173 set_gate(idt_table + n, 0, dpl, 0, 0);
174}
31e31b8a 175
89e957e7 176void cpu_loop(CPUX86State *env)
1b6b029e 177{
bc8a22cc 178 int trapnr;
9de5e440
FB
179 uint8_t *pc;
180 target_siginfo_t info;
851e67a1 181
1b6b029e 182 for(;;) {
bc8a22cc 183 trapnr = cpu_x86_exec(env);
bc8a22cc 184 switch(trapnr) {
f4beb510
FB
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;
1b6b029e 204 case EXCP0D_GPF:
851e67a1 205 if (env->eflags & VM_MASK) {
89e957e7 206 handle_vm86_fault(env);
1b6b029e 207 } else {
f4beb510
FB
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);
1b6b029e
FB
213 }
214 break;
b689bc57
FB
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;
970a87a6 222 info._sifields._sigfault._addr = env->cr[2];
b689bc57
FB
223 queue_signal(info.si_signo, &info);
224 break;
9de5e440 225 case EXCP00_DIVZ:
bc8a22cc 226 if (env->eflags & VM_MASK) {
447db213 227 handle_vm86_trap(env, trapnr);
bc8a22cc
FB
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 }
9de5e440 236 break;
447db213
FB
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;
9de5e440
FB
254 case EXCP04_INTO:
255 case EXCP05_BOUND:
bc8a22cc 256 if (env->eflags & VM_MASK) {
447db213 257 handle_vm86_trap(env, trapnr);
bc8a22cc
FB
258 } else {
259 info.si_signo = SIGSEGV;
260 info.si_errno = 0;
b689bc57 261 info.si_code = TARGET_SI_KERNEL;
bc8a22cc
FB
262 info._sifields._sigfault._addr = 0;
263 queue_signal(info.si_signo, &info);
264 }
9de5e440
FB
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;
1b6b029e 276 default:
970a87a6 277 pc = env->segs[R_CS].base + env->eip;
bc8a22cc
FB
278 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
279 (long)pc, trapnr);
1b6b029e
FB
280 abort();
281 }
66fb9763 282 process_pending_signals(env);
1b6b029e
FB
283 }
284}
b346ff46
FB
285#endif
286
287#ifdef TARGET_ARM
288
6f1f31c0
FB
289/* XXX: find a better solution */
290extern void tb_invalidate_page_range(target_ulong start, target_ulong end);
291
292static 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
b346ff46
FB
310void 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:
c6981055
FB
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 }
b346ff46
FB
339 break;
340 case EXCP_SWI:
341 {
342 /* system call */
343 insn = ldl((void *)(env->regs[15] - 4));
344 n = insn & 0xffffff;
6f1f31c0
FB
345 if (n == ARM_NR_cacheflush) {
346 arm_cache_flush(env->regs[0], env->regs[1]);
347 } else if (n >= ARM_SYSCALL_BASE) {
b346ff46
FB
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;
43fff238
FB
363 case EXCP_INTERRUPT:
364 /* just indicate that signals should be handled asap */
365 break;
b346ff46
FB
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
1b6b029e 378
93ac68bc
FB
379#ifdef TARGET_SPARC
380
060366c5
FB
381//#define DEBUG_WIN
382
383/* WARNING: dealing with register windows _is_ complicated */
384static 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
394static 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
413static void save_window(CPUSPARCState *env)
414{
415 save_window_offset(env, 2);
416}
417
418static 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
438static 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
93ac68bc
FB
458void cpu_loop (CPUSPARCState *env)
459{
060366c5
FB
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 }
93ac68bc
FB
502}
503
504#endif
505
67867308 506#ifdef TARGET_PPC
67867308
FB
507void cpu_loop(CPUPPCState *env)
508{
67867308 509 target_siginfo_t info;
61190b14
FB
510 int trapnr;
511 uint32_t ret;
67867308
FB
512
513 for(;;) {
514 trapnr = cpu_ppc_exec(env);
61190b14
FB
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 }
67867308
FB
521 switch(trapnr) {
522 case EXCP_NONE:
67867308 523 break;
61190b14
FB
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 */
67867308 530#if 0
61190b14
FB
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]);
67867308 533#endif
61190b14
FB
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");
67867308 552 abort();
61190b14
FB
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;
67867308 558 info.si_errno = 0;
61190b14
FB
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 }
67867308
FB
603 info._sifields._sigfault._addr = env->nip;
604 queue_signal(info.si_signo, &info);
605 break;
61190b14 606 case EXCP_ISI:
67867308 607 fprintf(stderr, "Invalid instruction fetch\n");
61190b14
FB
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;
67867308 613 info.si_errno = 0;
61190b14
FB
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;
67867308
FB
641 queue_signal(info.si_signo, &info);
642 break;
61190b14
FB
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");
67867308 648 abort();
61190b14
FB
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;
67867308 654 info.si_errno = 0;
61190b14
FB
655 info.si_code = TARGET_BUS_ADRALN;
656 info._sifields._sigfault._addr = env->nip - 4;
67867308
FB
657 queue_signal(info.si_signo, &info);
658 break;
61190b14
FB
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;
67867308 708 case EXCP_INVAL:
61190b14
FB
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:
67867308 719 info.si_code = TARGET_ILL_ILLOPN;
61190b14
FB
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;
67867308
FB
774 queue_signal(info.si_signo, &info);
775 break;
61190b14
FB
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;
67867308 781 info.si_errno = 0;
61190b14
FB
782 info.si_code = TARGET_ILL_COPROC;
783 info._sifields._sigfault._addr = env->nip - 4;
67867308
FB
784 queue_signal(info.si_signo, &info);
785 break;
61190b14
FB
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();
67867308 792 case EXCP_RESA: /* Implementation specific */
61190b14
FB
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();
67867308 798 case EXCP_RESB: /* Implementation specific */
61190b14
FB
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");
67867308 803 abort();
61190b14
FB
804 case EXCP_TRACE:
805 /* Do nothing: we use this to trace execution */
67867308 806 break;
61190b14
FB
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();
67867308 820 }
61190b14
FB
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;
a541f297
FB
834 case EXCP_DEBUG:
835 break;
67867308 836 default:
67867308
FB
837 fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
838 trapnr);
61190b14
FB
839 if (loglevel) {
840 fprintf(logfile, "qemu: unhandled CPU exception 0x%02x - "
841 "0x%02x - aborting\n", trapnr, env->error_code);
842 }
67867308
FB
843 abort();
844 }
61190b14
FB
845 if (trapnr < EXCP_PPC_MAX)
846 env->exceptions &= ~(1 << trapnr);
67867308 847 process_pending_signals(env);
61190b14
FB
848 if (env->exceptions != 0) {
849 check_exception_state(env);
850 }
67867308
FB
851 }
852}
853#endif
854
31e31b8a
FB
855void usage(void)
856{
4606bb3f 857 printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
6f1f31c0 858 "usage: qemu-" TARGET_ARCH " [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
b346ff46 859 "Linux CPU emulator (compiled for %s emulation)\n"
d691f669 860 "\n"
54936004 861 "-h print this help\n"
b346ff46
FB
862 "-L path set the elf interpreter prefix (default=%s)\n"
863 "-s size set the stack size in bytes (default=%ld)\n"
54936004
FB
864 "\n"
865 "debug options:\n"
c6981055
FB
866#ifdef USE_CODE_COPY
867 "-no-code-copy disable code copy acceleration\n"
868#endif
6f1f31c0 869 "-d options activate log (logfile=%s)\n"
54936004 870 "-p pagesize set the host page size to 'pagesize'\n",
b346ff46 871 TARGET_ARCH,
d691f669 872 interp_prefix,
54936004
FB
873 x86_stack_size,
874 DEBUG_LOGFILE);
74cd30b8 875 _exit(1);
31e31b8a
FB
876}
877
9de5e440 878/* XXX: currently only used for async signals (see signal.c) */
b346ff46 879CPUState *global_env;
59faf6d6
FB
880/* used only if single thread */
881CPUState *cpu_single_env = NULL;
882
851e67a1
FB
883/* used to free thread contexts */
884TaskState *first_task_state;
9de5e440 885
31e31b8a
FB
886int main(int argc, char **argv)
887{
888 const char *filename;
01ffc75b 889 struct target_pt_regs regs1, *regs = &regs1;
31e31b8a 890 struct image_info info1, *info = &info1;
851e67a1 891 TaskState ts1, *ts = &ts1;
b346ff46 892 CPUState *env;
586314f2 893 int optind;
d691f669
FB
894 const char *r;
895
31e31b8a
FB
896 if (argc <= 1)
897 usage();
f801f97e 898
cc38b844
FB
899 /* init debug */
900 cpu_set_log_filename(DEBUG_LOGFILE);
901
586314f2 902 optind = 1;
d691f669
FB
903 for(;;) {
904 if (optind >= argc)
905 break;
906 r = argv[optind];
907 if (r[0] != '-')
908 break;
586314f2 909 optind++;
d691f669
FB
910 r++;
911 if (!strcmp(r, "-")) {
912 break;
913 } else if (!strcmp(r, "d")) {
e19e89a5
FB
914 int mask;
915 CPULogItem *item;
6f1f31c0
FB
916
917 if (optind >= argc)
918 break;
e19e89a5 919
6f1f31c0
FB
920 r = argv[optind++];
921 mask = cpu_str_to_log_mask(r);
e19e89a5
FB
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);
d691f669
FB
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++];
54936004
FB
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 }
c6981055
FB
948 } else
949#ifdef USE_CODE_COPY
950 if (!strcmp(r, "no-code-copy")) {
951 code_copy_enabled = 0;
952 } else
953#endif
954 {
d691f669
FB
955 usage();
956 }
586314f2 957 }
d691f669
FB
958 if (optind >= argc)
959 usage();
586314f2
FB
960 filename = argv[optind];
961
31e31b8a 962 /* Zero out regs */
01ffc75b 963 memset(regs, 0, sizeof(struct target_pt_regs));
31e31b8a
FB
964
965 /* Zero out image_info */
966 memset(info, 0, sizeof(struct image_info));
967
74cd30b8
FB
968 /* Scan interp_prefix dir for replacement files. */
969 init_paths(interp_prefix);
970
54936004
FB
971 /* NOTE: we need to init the CPU at this stage to get the
972 host_page_size */
b346ff46 973 env = cpu_init();
92ccca6a 974
74cd30b8 975 if (elf_exec(filename, argv+optind, environ, regs, info) != 0) {
31e31b8a 976 printf("Error loading %s\n", filename);
74cd30b8 977 _exit(1);
31e31b8a
FB
978 }
979
4b74fe1f 980 if (loglevel) {
54936004
FB
981 page_dump(logfile);
982
4b74fe1f
FB
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);
b346ff46 989 fprintf(logfile, "entry 0x%08lx\n" , info->entry);
4b74fe1f 990 }
31e31b8a
FB
991
992 target_set_brk((char *)info->brk);
993 syscall_init();
66fb9763 994 signal_init();
31e31b8a 995
9de5e440 996 global_env = env;
0ecfa993 997
851e67a1
FB
998 /* build Task State */
999 memset(ts, 0, sizeof(TaskState));
1000 env->opaque = ts;
1001 ts->used = 1;
59faf6d6 1002 env->user_mode_only = 1;
851e67a1 1003
b346ff46 1004#if defined(TARGET_I386)
2e255c6b
FB
1005 cpu_x86_set_cpl(env, 3);
1006
3802ce26 1007 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
75c6215f 1008 env->hflags |= HF_PE_MASK;
3802ce26 1009
415e561f
FB
1010 /* flags setup : we activate the IRQs by default as in user mode */
1011 env->eflags |= IF_MASK;
1012
6dbad63e 1013 /* linux register setup */
0ecfa993
FB
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;
dab2ed99 1022 env->eip = regs->eip;
31e31b8a 1023
f4beb510
FB
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
6dbad63e
FB
1049 /* linux segment setup */
1050 env->gdt.base = (void *)gdt_table;
1051 env->gdt.limit = sizeof(gdt_table) - 1;
f4beb510
FB
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));
6dbad63e
FB
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);
92ccca6a 1064
b346ff46
FB
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 }
93ac68bc 1073#elif defined(TARGET_SPARC)
060366c5
FB
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 }
67867308
FB
1084#elif defined(TARGET_PPC)
1085 {
1086 int i;
61190b14
FB
1087 for (i = 0; i < 32; i++) {
1088 if (i != 12 && i != 6)
1089 env->msr[i] = (regs->msr >> i) & 1;
1090 }
67867308
FB
1091 env->nip = regs->nip;
1092 for(i = 0; i < 32; i++) {
1093 env->gpr[i] = regs->gpr[i];
1094 }
1095 }
b346ff46
FB
1096#else
1097#error unsupported target CPU
1098#endif
31e31b8a 1099
1b6b029e
FB
1100 cpu_loop(env);
1101 /* never exits */
31e31b8a
FB
1102 return 0;
1103}