]>
git.proxmox.com Git - qemu.git/blob - linux-user/main.c
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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.
29 #define DEBUG_LOGFILE "/tmp/qemu.log"
31 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
33 #if defined(__i386__) && !defined(CONFIG_STATIC)
34 /* Force usage of an ELF interpreter even if it is an ELF shared
36 const char interp
[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
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];
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;
55 void gemu_log(const char *fmt
, ...)
60 vfprintf(stderr
, fmt
, ap
);
64 void cpu_outb(CPUState
*env
, int addr
, int val
)
66 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", addr
, val
);
69 void cpu_outw(CPUState
*env
, int addr
, int val
)
71 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", addr
, val
);
74 void cpu_outl(CPUState
*env
, int addr
, int val
)
76 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", addr
, val
);
79 int cpu_inb(CPUState
*env
, int addr
)
81 fprintf(stderr
, "inb: port=0x%04x\n", addr
);
85 int cpu_inw(CPUState
*env
, int addr
)
87 fprintf(stderr
, "inw: port=0x%04x\n", addr
);
91 int cpu_inl(CPUState
*env
, int addr
)
93 fprintf(stderr
, "inl: port=0x%04x\n", addr
);
97 int cpu_get_pic_interrupt(CPUState
*env
)
102 /* timers for rdtsc */
104 #if defined(__i386__)
106 int64_t cpu_get_real_ticks(void)
109 asm volatile ("rdtsc" : "=A" (val
));
113 #elif defined(__x86_64__)
115 int64_t cpu_get_real_ticks(void)
119 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
128 static uint64_t emu_time
;
130 int64_t cpu_get_real_ticks(void)
138 /***********************************************************/
139 /* CPUX86 core interface */
141 uint64_t cpu_get_tsc(CPUX86State
*env
)
143 return cpu_get_real_ticks();
146 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
150 e1
= (addr
<< 16) | (limit
& 0xffff);
151 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
153 stl((uint8_t *)ptr
, e1
);
154 stl((uint8_t *)ptr
+ 4, e2
);
157 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
158 unsigned long addr
, unsigned int sel
)
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
);
167 uint64_t gdt_table
[6];
168 uint64_t idt_table
[256];
170 /* only dpl matters as we do only user space emulation */
171 static void set_idt(int n
, unsigned int dpl
)
173 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
176 void cpu_loop(CPUX86State
*env
)
180 target_siginfo_t info
;
183 trapnr
= cpu_x86_exec(env
);
187 env
->regs
[R_EAX
] = do_syscall(env
,
198 info
.si_signo
= SIGBUS
;
200 info
.si_code
= TARGET_SI_KERNEL
;
201 info
._sifields
._sigfault
._addr
= 0;
202 queue_signal(info
.si_signo
, &info
);
205 if (env
->eflags
& VM_MASK
) {
206 handle_vm86_fault(env
);
208 info
.si_signo
= SIGSEGV
;
210 info
.si_code
= TARGET_SI_KERNEL
;
211 info
._sifields
._sigfault
._addr
= 0;
212 queue_signal(info
.si_signo
, &info
);
216 info
.si_signo
= SIGSEGV
;
218 if (!(env
->error_code
& 1))
219 info
.si_code
= TARGET_SEGV_MAPERR
;
221 info
.si_code
= TARGET_SEGV_ACCERR
;
222 info
._sifields
._sigfault
._addr
= env
->cr
[2];
223 queue_signal(info
.si_signo
, &info
);
226 if (env
->eflags
& VM_MASK
) {
227 handle_vm86_trap(env
, trapnr
);
229 /* division by zero */
230 info
.si_signo
= SIGFPE
;
232 info
.si_code
= TARGET_FPE_INTDIV
;
233 info
._sifields
._sigfault
._addr
= env
->eip
;
234 queue_signal(info
.si_signo
, &info
);
239 if (env
->eflags
& VM_MASK
) {
240 handle_vm86_trap(env
, trapnr
);
242 info
.si_signo
= SIGTRAP
;
244 if (trapnr
== EXCP01_SSTP
) {
245 info
.si_code
= TARGET_TRAP_BRKPT
;
246 info
._sifields
._sigfault
._addr
= env
->eip
;
248 info
.si_code
= TARGET_SI_KERNEL
;
249 info
._sifields
._sigfault
._addr
= 0;
251 queue_signal(info
.si_signo
, &info
);
256 if (env
->eflags
& VM_MASK
) {
257 handle_vm86_trap(env
, trapnr
);
259 info
.si_signo
= SIGSEGV
;
261 info
.si_code
= TARGET_SI_KERNEL
;
262 info
._sifields
._sigfault
._addr
= 0;
263 queue_signal(info
.si_signo
, &info
);
267 info
.si_signo
= SIGILL
;
269 info
.si_code
= TARGET_ILL_ILLOPN
;
270 info
._sifields
._sigfault
._addr
= env
->eip
;
271 queue_signal(info
.si_signo
, &info
);
274 /* just indicate that signals should be handled asap */
277 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
278 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
282 process_pending_signals(env
);
289 /* XXX: find a better solution */
290 extern void tb_invalidate_page_range(target_ulong start
, target_ulong end
);
292 static void arm_cache_flush(target_ulong start
, target_ulong last
)
294 target_ulong addr
, last1
;
300 last1
= ((addr
+ TARGET_PAGE_SIZE
) & TARGET_PAGE_MASK
) - 1;
303 tb_invalidate_page_range(addr
, last1
+ 1);
310 void cpu_loop(CPUARMState
*env
)
313 unsigned int n
, insn
;
314 target_siginfo_t info
;
317 trapnr
= cpu_arm_exec(env
);
321 TaskState
*ts
= env
->opaque
;
324 /* we handle the FPU emulation here, as Linux */
325 /* we get the opcode */
326 opcode
= ldl_raw((uint8_t *)env
->regs
[15]);
328 if (EmulateAll(opcode
, &ts
->fpa
, env
->regs
) == 0) {
329 info
.si_signo
= SIGILL
;
331 info
.si_code
= TARGET_ILL_ILLOPN
;
332 info
._sifields
._sigfault
._addr
= env
->regs
[15];
333 queue_signal(info
.si_signo
, &info
);
343 insn
= ldl((void *)(env
->regs
[15] - 4));
345 if (n
== ARM_NR_cacheflush
) {
346 arm_cache_flush(env
->regs
[0], env
->regs
[1]);
347 } else if (n
>= ARM_SYSCALL_BASE
) {
349 n
-= ARM_SYSCALL_BASE
;
350 env
->regs
[0] = do_syscall(env
,
364 /* just indicate that signals should be handled asap */
368 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
370 cpu_arm_dump_state(env
, stderr
, 0);
373 process_pending_signals(env
);
383 /* WARNING: dealing with register windows _is_ complicated */
384 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
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
);
394 static inline void save_window_offset(CPUSPARCState
*env
, int offset
)
396 unsigned int new_wim
, i
, cwp1
;
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",
408 for(i
= 0; i
< 16; i
++)
409 stl_raw(sp_ptr
+ i
, env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)]);
413 static void save_window(CPUSPARCState
*env
)
415 save_window_offset(env
, 2);
418 static void restore_window(CPUSPARCState
*env
)
420 unsigned int new_wim
, i
, cwp1
;
423 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (NWINDOWS
- 1))) &
424 ((1LL << NWINDOWS
) - 1);
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",
433 for(i
= 0; i
< 16; i
++)
434 env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)] = ldl_raw(sp_ptr
+ i
);
438 static void flush_windows(CPUSPARCState
*env
)
441 #if defined(DEBUG_WIN)
442 printf("flush_windows:\n");
446 /* if restore would invoke restore_window(), then we can stop */
447 cwp1
= (env
->cwp
+ 1) & (NWINDOWS
- 1);
448 if (env
->wim
& (1 << cwp1
))
450 #if defined(DEBUG_WIN)
451 printf("offset=%d: ", offset
);
453 save_window_offset(env
, offset
);
458 void cpu_loop (CPUSPARCState
*env
)
463 trapnr
= cpu_sparc_exec (env
);
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
;
476 env
->psr
&= ~PSR_CARRY
;
478 env
->regwptr
[0] = ret
;
479 /* next instruction */
481 env
->npc
= env
->npc
+ 4;
483 case 0x83: /* flush windows */
484 // flush_windows(env);
485 /* next instruction */
487 env
->npc
= env
->npc
+ 4;
489 case TT_WIN_OVF
: /* window overflow */
492 case TT_WIN_UNF
: /* window underflow */
496 printf ("Unhandled trap: 0x%x\n", trapnr
);
497 cpu_sparc_dump_state(env
, stderr
, 0);
500 process_pending_signals (env
);
508 static inline uint64_t cpu_ppc_get_tb (CPUState
*env
)
514 uint32_t cpu_ppc_load_tbl (CPUState
*env
)
516 return cpu_ppc_get_tb(env
) & 0xFFFFFFFF;
519 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
521 return cpu_ppc_get_tb(env
) >> 32;
524 static void cpu_ppc_store_tb (CPUState
*env
, uint64_t value
)
529 void cpu_ppc_store_tbu (CPUState
*env
, uint32_t value
)
531 cpu_ppc_store_tb(env
, ((uint64_t)value
<< 32) | cpu_ppc_load_tbl(env
));
534 void cpu_ppc_store_tbl (CPUState
*env
, uint32_t value
)
536 cpu_ppc_store_tb(env
, ((uint64_t)cpu_ppc_load_tbl(env
) << 32) | value
);
539 uint32_t cpu_ppc_load_decr (CPUState
*env
)
545 void cpu_ppc_store_decr (CPUState
*env
, uint32_t value
)
550 void cpu_loop(CPUPPCState
*env
)
552 target_siginfo_t info
;
557 trapnr
= cpu_ppc_exec(env
);
558 if (trapnr
!= EXCP_SYSCALL_USER
&& trapnr
!= EXCP_BRANCH
&&
559 trapnr
!= EXCP_TRACE
) {
561 cpu_ppc_dump_state(env
, logfile
, 0);
567 case EXCP_SYSCALL_USER
:
570 * PPC ABI uses overflow flag in cr0 to signal an error
574 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env
->gpr
[0],
575 env
->gpr
[3], env
->gpr
[4], env
->gpr
[5], env
->gpr
[6]);
578 ret
= do_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
579 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
581 if (ret
> (uint32_t)(-515)) {
587 printf("syscall returned 0x%08x (%d)\n", ret
, ret
);
591 /* Should not happen ! */
592 fprintf(stderr
, "RESET asked... Stop emulation\n");
594 fprintf(logfile
, "RESET asked... Stop emulation\n");
596 case EXCP_MACHINE_CHECK
:
597 fprintf(stderr
, "Machine check exeption... Stop emulation\n");
599 fprintf(logfile
, "RESET asked... Stop emulation\n");
600 info
.si_signo
= TARGET_SIGBUS
;
602 info
.si_code
= TARGET_BUS_OBJERR
;
603 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
604 queue_signal(info
.si_signo
, &info
);
606 fprintf(stderr
, "Invalid data memory access: 0x%08x\n", env
->spr
[DAR
]);
608 fprintf(logfile
, "Invalid data memory access: 0x%08x\n",
611 switch (env
->error_code
& 0xF) {
612 case EXCP_DSI_TRANSLATE
:
613 info
.si_signo
= TARGET_SIGSEGV
;
615 info
.si_code
= TARGET_SEGV_MAPERR
;
617 case EXCP_DSI_NOTSUP
:
618 case EXCP_DSI_EXTERNAL
:
619 info
.si_signo
= TARGET_SIGILL
;
621 info
.si_code
= TARGET_ILL_ILLADR
;
624 info
.si_signo
= TARGET_SIGSEGV
;
626 info
.si_code
= TARGET_SEGV_ACCERR
;
629 info
.si_signo
= TARGET_SIGTRAP
;
631 info
.si_code
= TARGET_TRAP_BRKPT
;
634 /* Let's send a regular segfault... */
635 fprintf(stderr
, "Invalid segfault errno (%02x)\n",
638 fprintf(logfile
, "Invalid segfault errno (%02x)\n",
641 info
.si_signo
= TARGET_SIGSEGV
;
643 info
.si_code
= TARGET_SEGV_MAPERR
;
646 info
._sifields
._sigfault
._addr
= env
->nip
;
647 queue_signal(info
.si_signo
, &info
);
650 fprintf(stderr
, "Invalid instruction fetch\n");
652 fprintf(logfile
, "Invalid instruction fetch\n");
653 switch (env
->error_code
) {
654 case EXCP_ISI_TRANSLATE
:
655 info
.si_signo
= TARGET_SIGSEGV
;
657 info
.si_code
= TARGET_SEGV_MAPERR
;
660 info
.si_signo
= TARGET_SIGILL
;
662 info
.si_code
= TARGET_ILL_ILLADR
;
664 case EXCP_ISI_NOEXEC
:
666 info
.si_signo
= TARGET_SIGSEGV
;
668 info
.si_code
= TARGET_SEGV_ACCERR
;
671 /* Let's send a regular segfault... */
672 fprintf(stderr
, "Invalid segfault errno (%02x)\n",
675 fprintf(logfile
, "Invalid segfault errno (%02x)\n",
678 info
.si_signo
= TARGET_SIGSEGV
;
680 info
.si_code
= TARGET_SEGV_MAPERR
;
683 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
684 queue_signal(info
.si_signo
, &info
);
687 /* Should not happen ! */
688 fprintf(stderr
, "External interruption... Stop emulation\n");
690 fprintf(logfile
, "External interruption... Stop emulation\n");
693 fprintf(stderr
, "Invalid unaligned memory access\n");
695 fprintf(logfile
, "Invalid unaligned memory access\n");
696 info
.si_signo
= TARGET_SIGBUS
;
698 info
.si_code
= TARGET_BUS_ADRALN
;
699 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
700 queue_signal(info
.si_signo
, &info
);
703 switch (env
->error_code
& ~0xF) {
705 fprintf(stderr
, "Program exception\n");
707 fprintf(logfile
, "Program exception\n");
709 env
->fpscr
[7] |= 0x8;
710 /* Finally, update FEX */
711 if ((((env
->fpscr
[7] & 0x3) << 3) | (env
->fpscr
[6] >> 1)) &
712 ((env
->fpscr
[1] << 1) | (env
->fpscr
[0] >> 3)))
713 env
->fpscr
[7] |= 0x4;
714 info
.si_signo
= TARGET_SIGFPE
;
716 switch (env
->error_code
& 0xF) {
718 info
.si_code
= TARGET_FPE_FLTOVF
;
721 info
.si_code
= TARGET_FPE_FLTUND
;
725 info
.si_code
= TARGET_FPE_FLTDIV
;
728 info
.si_code
= TARGET_FPE_FLTRES
;
731 info
.si_code
= TARGET_FPE_FLTINV
;
740 info
.si_code
= TARGET_FPE_FLTSUB
;
743 fprintf(stderr
, "Unknown floating point exception "
744 "(%02x)\n", env
->error_code
);
746 fprintf(logfile
, "Unknown floating point exception "
747 "(%02x)\n", env
->error_code
& 0xF);
752 fprintf(stderr
, "Invalid instruction\n");
754 fprintf(logfile
, "Invalid instruction\n");
755 info
.si_signo
= TARGET_SIGILL
;
757 switch (env
->error_code
& 0xF) {
758 case EXCP_INVAL_INVAL
:
759 info
.si_code
= TARGET_ILL_ILLOPC
;
761 case EXCP_INVAL_LSWX
:
762 info
.si_code
= TARGET_ILL_ILLOPN
;
765 info
.si_code
= TARGET_ILL_PRVREG
;
768 info
.si_code
= TARGET_ILL_COPROC
;
771 fprintf(stderr
, "Unknown invalid operation (%02x)\n",
772 env
->error_code
& 0xF);
774 fprintf(logfile
, "Unknown invalid operation (%02x)\n",
775 env
->error_code
& 0xF);
777 info
.si_code
= TARGET_ILL_ILLADR
;
782 fprintf(stderr
, "Privilege violation\n");
784 fprintf(logfile
, "Privilege violation\n");
785 info
.si_signo
= TARGET_SIGILL
;
787 switch (env
->error_code
& 0xF) {
789 info
.si_code
= TARGET_ILL_PRVOPC
;
792 info
.si_code
= TARGET_ILL_PRVREG
;
795 fprintf(stderr
, "Unknown privilege violation (%02x)\n",
796 env
->error_code
& 0xF);
797 info
.si_code
= TARGET_ILL_PRVOPC
;
802 fprintf(stderr
, "Tried to call a TRAP\n");
804 fprintf(logfile
, "Tried to call a TRAP\n");
807 /* Should not happen ! */
808 fprintf(stderr
, "Unknown program exception (%02x)\n",
811 fprintf(logfile
, "Unknwon program exception (%02x)\n",
816 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
817 queue_signal(info
.si_signo
, &info
);
820 fprintf(stderr
, "No floating point allowed\n");
822 fprintf(logfile
, "No floating point allowed\n");
823 info
.si_signo
= TARGET_SIGILL
;
825 info
.si_code
= TARGET_ILL_COPROC
;
826 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
827 queue_signal(info
.si_signo
, &info
);
830 /* Should not happen ! */
831 fprintf(stderr
, "Decrementer exception\n");
833 fprintf(logfile
, "Decrementer exception\n");
835 case EXCP_RESA
: /* Implementation specific */
836 /* Should not happen ! */
837 fprintf(stderr
, "RESA exception should never happen !\n");
839 fprintf(logfile
, "RESA exception should never happen !\n");
841 case EXCP_RESB
: /* Implementation specific */
842 /* Should not happen ! */
843 fprintf(stderr
, "RESB exception should never happen !\n");
845 fprintf(logfile
, "RESB exception should never happen !\n");
848 /* Do nothing: we use this to trace execution */
851 /* Should not happen ! */
852 fprintf(stderr
, "Floating point assist exception\n");
854 fprintf(logfile
, "Floating point assist exception\n");
857 /* We reloaded the msr, just go on */
859 fprintf(stderr
, "Tried to go into supervisor mode !\n");
861 fprintf(logfile
, "Tried to go into supervisor mode !\n");
866 /* We stopped because of a jump... */
869 /* Should not occur: we always are in user mode */
870 fprintf(stderr
, "Return from interrupt ?\n");
872 fprintf(logfile
, "Return from interrupt ?\n");
875 /* Don't know why this should ever happen... */
880 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
883 fprintf(logfile
, "qemu: unhandled CPU exception 0x%02x - "
884 "0x%02x - aborting\n", trapnr
, env
->error_code
);
888 process_pending_signals(env
);
895 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
896 "usage: qemu-" TARGET_ARCH
" [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
897 "Linux CPU emulator (compiled for %s emulation)\n"
899 "-h print this help\n"
900 "-L path set the elf interpreter prefix (default=%s)\n"
901 "-s size set the stack size in bytes (default=%ld)\n"
905 "-no-code-copy disable code copy acceleration\n"
907 "-d options activate log (logfile=%s)\n"
908 "-p pagesize set the host page size to 'pagesize'\n",
916 /* XXX: currently only used for async signals (see signal.c) */
917 CPUState
*global_env
;
918 /* used only if single thread */
919 CPUState
*cpu_single_env
= NULL
;
921 /* used to free thread contexts */
922 TaskState
*first_task_state
;
924 int main(int argc
, char **argv
)
926 const char *filename
;
927 struct target_pt_regs regs1
, *regs
= ®s1
;
928 struct image_info info1
, *info
= &info1
;
929 TaskState ts1
, *ts
= &ts1
;
938 cpu_set_log_filename(DEBUG_LOGFILE
);
949 if (!strcmp(r
, "-")) {
951 } else if (!strcmp(r
, "d")) {
959 mask
= cpu_str_to_log_mask(r
);
961 printf("Log items (comma separated):\n");
962 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
963 printf("%-10s %s\n", item
->name
, item
->help
);
968 } else if (!strcmp(r
, "s")) {
970 x86_stack_size
= strtol(r
, (char **)&r
, 0);
971 if (x86_stack_size
<= 0)
974 x86_stack_size
*= 1024 * 1024;
975 else if (*r
== 'k' || *r
== 'K')
976 x86_stack_size
*= 1024;
977 } else if (!strcmp(r
, "L")) {
978 interp_prefix
= argv
[optind
++];
979 } else if (!strcmp(r
, "p")) {
980 host_page_size
= atoi(argv
[optind
++]);
981 if (host_page_size
== 0 ||
982 (host_page_size
& (host_page_size
- 1)) != 0) {
983 fprintf(stderr
, "page size must be a power of two\n");
988 if (!strcmp(r
, "no-code-copy")) {
989 code_copy_enabled
= 0;
998 filename
= argv
[optind
];
1001 memset(regs
, 0, sizeof(struct target_pt_regs
));
1003 /* Zero out image_info */
1004 memset(info
, 0, sizeof(struct image_info
));
1006 /* Scan interp_prefix dir for replacement files. */
1007 init_paths(interp_prefix
);
1009 /* NOTE: we need to init the CPU at this stage to get the
1013 if (elf_exec(filename
, argv
+optind
, environ
, regs
, info
) != 0) {
1014 printf("Error loading %s\n", filename
);
1021 fprintf(logfile
, "start_brk 0x%08lx\n" , info
->start_brk
);
1022 fprintf(logfile
, "end_code 0x%08lx\n" , info
->end_code
);
1023 fprintf(logfile
, "start_code 0x%08lx\n" , info
->start_code
);
1024 fprintf(logfile
, "end_data 0x%08lx\n" , info
->end_data
);
1025 fprintf(logfile
, "start_stack 0x%08lx\n" , info
->start_stack
);
1026 fprintf(logfile
, "brk 0x%08lx\n" , info
->brk
);
1027 fprintf(logfile
, "entry 0x%08lx\n" , info
->entry
);
1030 target_set_brk((char *)info
->brk
);
1036 /* build Task State */
1037 memset(ts
, 0, sizeof(TaskState
));
1040 env
->user_mode_only
= 1;
1042 #if defined(TARGET_I386)
1043 cpu_x86_set_cpl(env
, 3);
1045 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
1046 env
->hflags
|= HF_PE_MASK
;
1048 /* flags setup : we activate the IRQs by default as in user mode */
1049 env
->eflags
|= IF_MASK
;
1051 /* linux register setup */
1052 env
->regs
[R_EAX
] = regs
->eax
;
1053 env
->regs
[R_EBX
] = regs
->ebx
;
1054 env
->regs
[R_ECX
] = regs
->ecx
;
1055 env
->regs
[R_EDX
] = regs
->edx
;
1056 env
->regs
[R_ESI
] = regs
->esi
;
1057 env
->regs
[R_EDI
] = regs
->edi
;
1058 env
->regs
[R_EBP
] = regs
->ebp
;
1059 env
->regs
[R_ESP
] = regs
->esp
;
1060 env
->eip
= regs
->eip
;
1062 /* linux interrupt setup */
1063 env
->idt
.base
= (void *)idt_table
;
1064 env
->idt
.limit
= sizeof(idt_table
) - 1;
1087 /* linux segment setup */
1088 env
->gdt
.base
= (void *)gdt_table
;
1089 env
->gdt
.limit
= sizeof(gdt_table
) - 1;
1090 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1091 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1092 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1093 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1094 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1095 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1096 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1097 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1098 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1099 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1100 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1101 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1103 #elif defined(TARGET_ARM)
1106 for(i
= 0; i
< 16; i
++) {
1107 env
->regs
[i
] = regs
->uregs
[i
];
1109 env
->cpsr
= regs
->uregs
[16];
1111 #elif defined(TARGET_SPARC)
1115 env
->npc
= regs
->npc
;
1117 for(i
= 0; i
< 8; i
++)
1118 env
->gregs
[i
] = regs
->u_regs
[i
];
1119 for(i
= 0; i
< 8; i
++)
1120 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1122 #elif defined(TARGET_PPC)
1125 for (i
= 0; i
< 32; i
++) {
1126 if (i
!= 12 && i
!= 6)
1127 env
->msr
[i
] = (regs
->msr
>> i
) & 1;
1129 env
->nip
= regs
->nip
;
1130 for(i
= 0; i
< 32; i
++) {
1131 env
->gpr
[i
] = regs
->gpr
[i
];
1135 #error unsupported target CPU