]>
git.proxmox.com Git - mirror_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
)
103 /***********************************************************/
104 /* CPUX86 core interface */
106 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
110 e1
= (addr
<< 16) | (limit
& 0xffff);
111 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
113 stl((uint8_t *)ptr
, e1
);
114 stl((uint8_t *)ptr
+ 4, e2
);
117 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
118 unsigned long addr
, unsigned int sel
)
121 e1
= (addr
& 0xffff) | (sel
<< 16);
122 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
123 stl((uint8_t *)ptr
, e1
);
124 stl((uint8_t *)ptr
+ 4, e2
);
127 uint64_t gdt_table
[6];
128 uint64_t idt_table
[256];
130 /* only dpl matters as we do only user space emulation */
131 static void set_idt(int n
, unsigned int dpl
)
133 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
136 void cpu_loop(CPUX86State
*env
)
140 target_siginfo_t info
;
143 trapnr
= cpu_x86_exec(env
);
147 env
->regs
[R_EAX
] = do_syscall(env
,
158 info
.si_signo
= SIGBUS
;
160 info
.si_code
= TARGET_SI_KERNEL
;
161 info
._sifields
._sigfault
._addr
= 0;
162 queue_signal(info
.si_signo
, &info
);
165 if (env
->eflags
& VM_MASK
) {
166 handle_vm86_fault(env
);
168 info
.si_signo
= SIGSEGV
;
170 info
.si_code
= TARGET_SI_KERNEL
;
171 info
._sifields
._sigfault
._addr
= 0;
172 queue_signal(info
.si_signo
, &info
);
176 info
.si_signo
= SIGSEGV
;
178 if (!(env
->error_code
& 1))
179 info
.si_code
= TARGET_SEGV_MAPERR
;
181 info
.si_code
= TARGET_SEGV_ACCERR
;
182 info
._sifields
._sigfault
._addr
= env
->cr
[2];
183 queue_signal(info
.si_signo
, &info
);
186 if (env
->eflags
& VM_MASK
) {
187 handle_vm86_trap(env
, trapnr
);
189 /* division by zero */
190 info
.si_signo
= SIGFPE
;
192 info
.si_code
= TARGET_FPE_INTDIV
;
193 info
._sifields
._sigfault
._addr
= env
->eip
;
194 queue_signal(info
.si_signo
, &info
);
199 if (env
->eflags
& VM_MASK
) {
200 handle_vm86_trap(env
, trapnr
);
202 info
.si_signo
= SIGTRAP
;
204 if (trapnr
== EXCP01_SSTP
) {
205 info
.si_code
= TARGET_TRAP_BRKPT
;
206 info
._sifields
._sigfault
._addr
= env
->eip
;
208 info
.si_code
= TARGET_SI_KERNEL
;
209 info
._sifields
._sigfault
._addr
= 0;
211 queue_signal(info
.si_signo
, &info
);
216 if (env
->eflags
& VM_MASK
) {
217 handle_vm86_trap(env
, trapnr
);
219 info
.si_signo
= SIGSEGV
;
221 info
.si_code
= TARGET_SI_KERNEL
;
222 info
._sifields
._sigfault
._addr
= 0;
223 queue_signal(info
.si_signo
, &info
);
227 info
.si_signo
= SIGILL
;
229 info
.si_code
= TARGET_ILL_ILLOPN
;
230 info
._sifields
._sigfault
._addr
= env
->eip
;
231 queue_signal(info
.si_signo
, &info
);
234 /* just indicate that signals should be handled asap */
237 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
238 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
242 process_pending_signals(env
);
249 /* XXX: find a better solution */
250 extern void tb_invalidate_page_range(target_ulong start
, target_ulong end
);
252 static void arm_cache_flush(target_ulong start
, target_ulong last
)
254 target_ulong addr
, last1
;
260 last1
= ((addr
+ TARGET_PAGE_SIZE
) & TARGET_PAGE_MASK
) - 1;
263 tb_invalidate_page_range(addr
, last1
+ 1);
270 void cpu_loop(CPUARMState
*env
)
273 unsigned int n
, insn
;
274 target_siginfo_t info
;
277 trapnr
= cpu_arm_exec(env
);
281 TaskState
*ts
= env
->opaque
;
284 /* we handle the FPU emulation here, as Linux */
285 /* we get the opcode */
286 opcode
= ldl_raw((uint8_t *)env
->regs
[15]);
288 if (EmulateAll(opcode
, &ts
->fpa
, env
->regs
) == 0) {
289 info
.si_signo
= SIGILL
;
291 info
.si_code
= TARGET_ILL_ILLOPN
;
292 info
._sifields
._sigfault
._addr
= env
->regs
[15];
293 queue_signal(info
.si_signo
, &info
);
303 insn
= ldl((void *)(env
->regs
[15] - 4));
305 if (n
== ARM_NR_cacheflush
) {
306 arm_cache_flush(env
->regs
[0], env
->regs
[1]);
307 } else if (n
>= ARM_SYSCALL_BASE
) {
309 n
-= ARM_SYSCALL_BASE
;
310 env
->regs
[0] = do_syscall(env
,
324 /* just indicate that signals should be handled asap */
328 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
330 cpu_arm_dump_state(env
, stderr
, 0);
333 process_pending_signals(env
);
343 /* WARNING: dealing with register windows _is_ complicated */
344 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
346 index
= (index
+ cwp
* 16) & (16 * NWINDOWS
- 1);
347 /* wrap handling : if cwp is on the last window, then we use the
348 registers 'after' the end */
349 if (index
< 8 && env
->cwp
== (NWINDOWS
- 1))
350 index
+= (16 * NWINDOWS
);
354 static inline void save_window_offset(CPUSPARCState
*env
, int offset
)
356 unsigned int new_wim
, i
, cwp1
;
359 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (NWINDOWS
- 1))) &
360 ((1LL << NWINDOWS
) - 1);
361 /* save the window */
362 cwp1
= (env
->cwp
+ offset
) & (NWINDOWS
- 1);
363 sp_ptr
= (uint32_t *)(env
->regbase
[get_reg_index(env
, cwp1
, 6)]);
364 #if defined(DEBUG_WIN)
365 printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n",
368 for(i
= 0; i
< 16; i
++)
369 stl_raw(sp_ptr
+ i
, env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)]);
373 static void save_window(CPUSPARCState
*env
)
375 save_window_offset(env
, 2);
378 static void restore_window(CPUSPARCState
*env
)
380 unsigned int new_wim
, i
, cwp1
;
383 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (NWINDOWS
- 1))) &
384 ((1LL << NWINDOWS
) - 1);
386 /* restore the invalid window */
387 cwp1
= (env
->cwp
+ 1) & (NWINDOWS
- 1);
388 sp_ptr
= (uint32_t *)(env
->regbase
[get_reg_index(env
, cwp1
, 6)]);
389 #if defined(DEBUG_WIN)
390 printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n",
393 for(i
= 0; i
< 16; i
++)
394 env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)] = ldl_raw(sp_ptr
+ i
);
398 static void flush_windows(CPUSPARCState
*env
)
401 #if defined(DEBUG_WIN)
402 printf("flush_windows:\n");
406 /* if restore would invoke restore_window(), then we can stop */
407 cwp1
= (env
->cwp
+ 1) & (NWINDOWS
- 1);
408 if (env
->wim
& (1 << cwp1
))
410 #if defined(DEBUG_WIN)
411 printf("offset=%d: ", offset
);
413 save_window_offset(env
, offset
);
418 void cpu_loop (CPUSPARCState
*env
)
423 trapnr
= cpu_sparc_exec (env
);
428 ret
= do_syscall (env
, env
->gregs
[1],
429 env
->regwptr
[0], env
->regwptr
[1],
430 env
->regwptr
[2], env
->regwptr
[3],
431 env
->regwptr
[4], env
->regwptr
[5]);
432 if ((unsigned int)ret
>= (unsigned int)(-515)) {
433 env
->psr
|= PSR_CARRY
;
436 env
->psr
&= ~PSR_CARRY
;
438 env
->regwptr
[0] = ret
;
439 /* next instruction */
441 env
->npc
= env
->npc
+ 4;
443 case 0x83: /* flush windows */
444 // flush_windows(env);
445 /* next instruction */
447 env
->npc
= env
->npc
+ 4;
449 case TT_WIN_OVF
: /* window overflow */
452 case TT_WIN_UNF
: /* window underflow */
456 printf ("Unhandled trap: 0x%x\n", trapnr
);
457 cpu_sparc_dump_state(env
, stderr
, 0);
460 process_pending_signals (env
);
467 void cpu_loop(CPUPPCState
*env
)
469 target_siginfo_t info
;
474 trapnr
= cpu_ppc_exec(env
);
475 if (trapnr
!= EXCP_SYSCALL_USER
&& trapnr
!= EXCP_BRANCH
&&
476 trapnr
!= EXCP_TRACE
) {
478 cpu_ppc_dump_state(env
, logfile
, 0);
484 case EXCP_SYSCALL_USER
:
487 * PPC ABI uses overflow flag in cr0 to signal an error
491 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env
->gpr
[0],
492 env
->gpr
[3], env
->gpr
[4], env
->gpr
[5], env
->gpr
[6]);
495 ret
= do_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
496 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
498 if (ret
> (uint32_t)(-515)) {
504 printf("syscall returned 0x%08x (%d)\n", ret
, ret
);
508 /* Should not happen ! */
509 fprintf(stderr
, "RESET asked... Stop emulation\n");
511 fprintf(logfile
, "RESET asked... Stop emulation\n");
513 case EXCP_MACHINE_CHECK
:
514 fprintf(stderr
, "Machine check exeption... Stop emulation\n");
516 fprintf(logfile
, "RESET asked... Stop emulation\n");
517 info
.si_signo
= TARGET_SIGBUS
;
519 info
.si_code
= TARGET_BUS_OBJERR
;
520 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
521 queue_signal(info
.si_signo
, &info
);
523 fprintf(stderr
, "Invalid data memory access: 0x%08x\n", env
->spr
[DAR
]);
525 fprintf(logfile
, "Invalid data memory access: 0x%08x\n",
528 switch (env
->error_code
& 0xF) {
529 case EXCP_DSI_TRANSLATE
:
530 info
.si_signo
= TARGET_SIGSEGV
;
532 info
.si_code
= TARGET_SEGV_MAPERR
;
534 case EXCP_DSI_NOTSUP
:
535 case EXCP_DSI_EXTERNAL
:
536 info
.si_signo
= TARGET_SIGILL
;
538 info
.si_code
= TARGET_ILL_ILLADR
;
541 info
.si_signo
= TARGET_SIGSEGV
;
543 info
.si_code
= TARGET_SEGV_ACCERR
;
546 info
.si_signo
= TARGET_SIGTRAP
;
548 info
.si_code
= TARGET_TRAP_BRKPT
;
551 /* Let's send a regular segfault... */
552 fprintf(stderr
, "Invalid segfault errno (%02x)\n",
555 fprintf(logfile
, "Invalid segfault errno (%02x)\n",
558 info
.si_signo
= TARGET_SIGSEGV
;
560 info
.si_code
= TARGET_SEGV_MAPERR
;
563 info
._sifields
._sigfault
._addr
= env
->nip
;
564 queue_signal(info
.si_signo
, &info
);
567 fprintf(stderr
, "Invalid instruction fetch\n");
569 fprintf(logfile
, "Invalid instruction fetch\n");
570 switch (env
->error_code
) {
571 case EXCP_ISI_TRANSLATE
:
572 info
.si_signo
= TARGET_SIGSEGV
;
574 info
.si_code
= TARGET_SEGV_MAPERR
;
577 info
.si_signo
= TARGET_SIGILL
;
579 info
.si_code
= TARGET_ILL_ILLADR
;
581 case EXCP_ISI_NOEXEC
:
583 info
.si_signo
= TARGET_SIGSEGV
;
585 info
.si_code
= TARGET_SEGV_ACCERR
;
588 /* Let's send a regular segfault... */
589 fprintf(stderr
, "Invalid segfault errno (%02x)\n",
592 fprintf(logfile
, "Invalid segfault errno (%02x)\n",
595 info
.si_signo
= TARGET_SIGSEGV
;
597 info
.si_code
= TARGET_SEGV_MAPERR
;
600 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
601 queue_signal(info
.si_signo
, &info
);
604 /* Should not happen ! */
605 fprintf(stderr
, "External interruption... Stop emulation\n");
607 fprintf(logfile
, "External interruption... Stop emulation\n");
610 fprintf(stderr
, "Invalid unaligned memory access\n");
612 fprintf(logfile
, "Invalid unaligned memory access\n");
613 info
.si_signo
= TARGET_SIGBUS
;
615 info
.si_code
= TARGET_BUS_ADRALN
;
616 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
617 queue_signal(info
.si_signo
, &info
);
620 switch (env
->error_code
& ~0xF) {
622 fprintf(stderr
, "Program exception\n");
624 fprintf(logfile
, "Program exception\n");
626 env
->fpscr
[7] |= 0x8;
627 /* Finally, update FEX */
628 if ((((env
->fpscr
[7] & 0x3) << 3) | (env
->fpscr
[6] >> 1)) &
629 ((env
->fpscr
[1] << 1) | (env
->fpscr
[0] >> 3)))
630 env
->fpscr
[7] |= 0x4;
631 info
.si_signo
= TARGET_SIGFPE
;
633 switch (env
->error_code
& 0xF) {
635 info
.si_code
= TARGET_FPE_FLTOVF
;
638 info
.si_code
= TARGET_FPE_FLTUND
;
642 info
.si_code
= TARGET_FPE_FLTDIV
;
645 info
.si_code
= TARGET_FPE_FLTRES
;
648 info
.si_code
= TARGET_FPE_FLTINV
;
657 info
.si_code
= TARGET_FPE_FLTSUB
;
660 fprintf(stderr
, "Unknown floating point exception "
661 "(%02x)\n", env
->error_code
);
663 fprintf(logfile
, "Unknown floating point exception "
664 "(%02x)\n", env
->error_code
& 0xF);
669 fprintf(stderr
, "Invalid instruction\n");
671 fprintf(logfile
, "Invalid instruction\n");
672 info
.si_signo
= TARGET_SIGILL
;
674 switch (env
->error_code
& 0xF) {
675 case EXCP_INVAL_INVAL
:
676 info
.si_code
= TARGET_ILL_ILLOPC
;
678 case EXCP_INVAL_LSWX
:
679 info
.si_code
= TARGET_ILL_ILLOPN
;
682 info
.si_code
= TARGET_ILL_PRVREG
;
685 info
.si_code
= TARGET_ILL_COPROC
;
688 fprintf(stderr
, "Unknown invalid operation (%02x)\n",
689 env
->error_code
& 0xF);
691 fprintf(logfile
, "Unknown invalid operation (%02x)\n",
692 env
->error_code
& 0xF);
694 info
.si_code
= TARGET_ILL_ILLADR
;
699 fprintf(stderr
, "Privilege violation\n");
701 fprintf(logfile
, "Privilege violation\n");
702 info
.si_signo
= TARGET_SIGILL
;
704 switch (env
->error_code
& 0xF) {
706 info
.si_code
= TARGET_ILL_PRVOPC
;
709 info
.si_code
= TARGET_ILL_PRVREG
;
712 fprintf(stderr
, "Unknown privilege violation (%02x)\n",
713 env
->error_code
& 0xF);
714 info
.si_code
= TARGET_ILL_PRVOPC
;
719 fprintf(stderr
, "Tried to call a TRAP\n");
721 fprintf(logfile
, "Tried to call a TRAP\n");
724 /* Should not happen ! */
725 fprintf(stderr
, "Unknown program exception (%02x)\n",
728 fprintf(logfile
, "Unknwon program exception (%02x)\n",
733 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
734 queue_signal(info
.si_signo
, &info
);
737 fprintf(stderr
, "No floating point allowed\n");
739 fprintf(logfile
, "No floating point allowed\n");
740 info
.si_signo
= TARGET_SIGILL
;
742 info
.si_code
= TARGET_ILL_COPROC
;
743 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
744 queue_signal(info
.si_signo
, &info
);
747 /* Should not happen ! */
748 fprintf(stderr
, "Decrementer exception\n");
750 fprintf(logfile
, "Decrementer exception\n");
752 case EXCP_RESA
: /* Implementation specific */
753 /* Should not happen ! */
754 fprintf(stderr
, "RESA exception should never happen !\n");
756 fprintf(logfile
, "RESA exception should never happen !\n");
758 case EXCP_RESB
: /* Implementation specific */
759 /* Should not happen ! */
760 fprintf(stderr
, "RESB exception should never happen !\n");
762 fprintf(logfile
, "RESB exception should never happen !\n");
765 /* Do nothing: we use this to trace execution */
768 /* Should not happen ! */
769 fprintf(stderr
, "Floating point assist exception\n");
771 fprintf(logfile
, "Floating point assist exception\n");
774 /* We reloaded the msr, just go on */
776 fprintf(stderr
, "Tried to go into supervisor mode !\n");
778 fprintf(logfile
, "Tried to go into supervisor mode !\n");
783 /* We stopped because of a jump... */
786 /* Should not occur: we always are in user mode */
787 fprintf(stderr
, "Return from interrupt ?\n");
789 fprintf(logfile
, "Return from interrupt ?\n");
792 /* Don't know why this should ever happen... */
797 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
800 fprintf(logfile
, "qemu: unhandled CPU exception 0x%02x - "
801 "0x%02x - aborting\n", trapnr
, env
->error_code
);
805 if (trapnr
< EXCP_PPC_MAX
)
806 env
->exceptions
&= ~(1 << trapnr
);
807 process_pending_signals(env
);
808 if (env
->exceptions
!= 0) {
809 check_exception_state(env
);
817 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
818 "usage: qemu-" TARGET_ARCH
" [-h] [-d opts] [-L path] [-s size] program [arguments...]\n"
819 "Linux CPU emulator (compiled for %s emulation)\n"
821 "-h print this help\n"
822 "-L path set the elf interpreter prefix (default=%s)\n"
823 "-s size set the stack size in bytes (default=%ld)\n"
827 "-no-code-copy disable code copy acceleration\n"
829 "-d options activate log (logfile=%s)\n"
830 "-p pagesize set the host page size to 'pagesize'\n",
838 /* XXX: currently only used for async signals (see signal.c) */
839 CPUState
*global_env
;
840 /* used only if single thread */
841 CPUState
*cpu_single_env
= NULL
;
843 /* used to free thread contexts */
844 TaskState
*first_task_state
;
846 int main(int argc
, char **argv
)
848 const char *filename
;
849 struct target_pt_regs regs1
, *regs
= ®s1
;
850 struct image_info info1
, *info
= &info1
;
851 TaskState ts1
, *ts
= &ts1
;
860 cpu_set_log_filename(DEBUG_LOGFILE
);
871 if (!strcmp(r
, "-")) {
873 } else if (!strcmp(r
, "d")) {
881 mask
= cpu_str_to_log_mask(r
);
883 printf("Log items (comma separated):\n");
884 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
885 printf("%-10s %s\n", item
->name
, item
->help
);
890 } else if (!strcmp(r
, "s")) {
892 x86_stack_size
= strtol(r
, (char **)&r
, 0);
893 if (x86_stack_size
<= 0)
896 x86_stack_size
*= 1024 * 1024;
897 else if (*r
== 'k' || *r
== 'K')
898 x86_stack_size
*= 1024;
899 } else if (!strcmp(r
, "L")) {
900 interp_prefix
= argv
[optind
++];
901 } else if (!strcmp(r
, "p")) {
902 host_page_size
= atoi(argv
[optind
++]);
903 if (host_page_size
== 0 ||
904 (host_page_size
& (host_page_size
- 1)) != 0) {
905 fprintf(stderr
, "page size must be a power of two\n");
910 if (!strcmp(r
, "no-code-copy")) {
911 code_copy_enabled
= 0;
920 filename
= argv
[optind
];
923 memset(regs
, 0, sizeof(struct target_pt_regs
));
925 /* Zero out image_info */
926 memset(info
, 0, sizeof(struct image_info
));
928 /* Scan interp_prefix dir for replacement files. */
929 init_paths(interp_prefix
);
931 /* NOTE: we need to init the CPU at this stage to get the
935 if (elf_exec(filename
, argv
+optind
, environ
, regs
, info
) != 0) {
936 printf("Error loading %s\n", filename
);
943 fprintf(logfile
, "start_brk 0x%08lx\n" , info
->start_brk
);
944 fprintf(logfile
, "end_code 0x%08lx\n" , info
->end_code
);
945 fprintf(logfile
, "start_code 0x%08lx\n" , info
->start_code
);
946 fprintf(logfile
, "end_data 0x%08lx\n" , info
->end_data
);
947 fprintf(logfile
, "start_stack 0x%08lx\n" , info
->start_stack
);
948 fprintf(logfile
, "brk 0x%08lx\n" , info
->brk
);
949 fprintf(logfile
, "entry 0x%08lx\n" , info
->entry
);
952 target_set_brk((char *)info
->brk
);
958 /* build Task State */
959 memset(ts
, 0, sizeof(TaskState
));
962 env
->user_mode_only
= 1;
964 #if defined(TARGET_I386)
965 cpu_x86_set_cpl(env
, 3);
967 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
968 env
->hflags
|= HF_PE_MASK
;
970 /* flags setup : we activate the IRQs by default as in user mode */
971 env
->eflags
|= IF_MASK
;
973 /* linux register setup */
974 env
->regs
[R_EAX
] = regs
->eax
;
975 env
->regs
[R_EBX
] = regs
->ebx
;
976 env
->regs
[R_ECX
] = regs
->ecx
;
977 env
->regs
[R_EDX
] = regs
->edx
;
978 env
->regs
[R_ESI
] = regs
->esi
;
979 env
->regs
[R_EDI
] = regs
->edi
;
980 env
->regs
[R_EBP
] = regs
->ebp
;
981 env
->regs
[R_ESP
] = regs
->esp
;
982 env
->eip
= regs
->eip
;
984 /* linux interrupt setup */
985 env
->idt
.base
= (void *)idt_table
;
986 env
->idt
.limit
= sizeof(idt_table
) - 1;
1009 /* linux segment setup */
1010 env
->gdt
.base
= (void *)gdt_table
;
1011 env
->gdt
.limit
= sizeof(gdt_table
) - 1;
1012 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1013 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1014 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1015 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1016 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1017 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1018 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1019 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1020 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1021 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1022 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1023 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1025 #elif defined(TARGET_ARM)
1028 for(i
= 0; i
< 16; i
++) {
1029 env
->regs
[i
] = regs
->uregs
[i
];
1031 env
->cpsr
= regs
->uregs
[16];
1033 #elif defined(TARGET_SPARC)
1037 env
->npc
= regs
->npc
;
1039 for(i
= 0; i
< 8; i
++)
1040 env
->gregs
[i
] = regs
->u_regs
[i
];
1041 for(i
= 0; i
< 8; i
++)
1042 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1044 #elif defined(TARGET_PPC)
1047 for (i
= 0; i
< 32; i
++) {
1048 if (i
!= 12 && i
!= 6)
1049 env
->msr
[i
] = (regs
->msr
>> i
) & 1;
1051 env
->nip
= regs
->nip
;
1052 for(i
= 0; i
< 32; i
++) {
1053 env
->gpr
[i
] = regs
->gpr
[i
];
1057 #error unsupported target CPU