4 * Copyright (c) 2003-2008 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, see <http://www.gnu.org/licenses/>.
25 #include <machine/trap.h>
26 #include <sys/types.h>
30 #include "qemu-common.h"
34 #include "qemu/timer.h"
35 #include "qemu/envlist.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
40 #if defined(CONFIG_USE_GUEST_BASE)
41 unsigned long mmap_min_addr
;
42 unsigned long guest_base
;
44 unsigned long reserved_va
;
47 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
48 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
49 extern char **environ
;
50 enum BSDType bsd_type
;
52 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
53 we allocate a bigger stack. Need a better solution, for example
54 by remapping the process stack directly at the right place */
55 unsigned long x86_stack_size
= 512 * 1024;
57 void gemu_log(const char *fmt
, ...)
62 vfprintf(stderr
, fmt
, ap
);
66 #if defined(TARGET_I386)
67 int cpu_get_pic_interrupt(CPUX86State
*env
)
73 /* These are no-ops because we are not threadsafe. */
74 static inline void cpu_exec_start(CPUArchState
*env
)
78 static inline void cpu_exec_end(CPUArchState
*env
)
82 static inline void start_exclusive(void)
86 static inline void end_exclusive(void)
94 void fork_end(int child
)
97 gdbserver_fork(thread_env
);
101 void cpu_list_lock(void)
105 void cpu_list_unlock(void)
110 /***********************************************************/
111 /* CPUX86 core interface */
113 void cpu_smm_update(CPUX86State
*env
)
117 uint64_t cpu_get_tsc(CPUX86State
*env
)
119 return cpu_get_real_ticks();
122 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
127 e1
= (addr
<< 16) | (limit
& 0xffff);
128 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
135 static uint64_t *idt_table
;
137 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
138 uint64_t addr
, unsigned int sel
)
141 e1
= (addr
& 0xffff) | (sel
<< 16);
142 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
146 p
[2] = tswap32(addr
>> 32);
149 /* only dpl matters as we do only user space emulation */
150 static void set_idt(int n
, unsigned int dpl
)
152 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
155 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
156 uint32_t addr
, unsigned int sel
)
159 e1
= (addr
& 0xffff) | (sel
<< 16);
160 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
166 /* only dpl matters as we do only user space emulation */
167 static void set_idt(int n
, unsigned int dpl
)
169 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
173 void cpu_loop(CPUX86State
*env
)
177 //target_siginfo_t info;
180 trapnr
= cpu_x86_exec(env
);
183 /* syscall from int $0x80 */
184 if (bsd_type
== target_freebsd
) {
185 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
187 int32_t syscall_nr
= env
->regs
[R_EAX
];
188 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
190 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
191 get_user_s32(syscall_nr
, params
);
192 params
+= sizeof(int32_t);
193 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
194 get_user_s32(syscall_nr
, params
);
195 params
+= sizeof(int64_t);
197 get_user_s32(arg1
, params
);
198 params
+= sizeof(int32_t);
199 get_user_s32(arg2
, params
);
200 params
+= sizeof(int32_t);
201 get_user_s32(arg3
, params
);
202 params
+= sizeof(int32_t);
203 get_user_s32(arg4
, params
);
204 params
+= sizeof(int32_t);
205 get_user_s32(arg5
, params
);
206 params
+= sizeof(int32_t);
207 get_user_s32(arg6
, params
);
208 params
+= sizeof(int32_t);
209 get_user_s32(arg7
, params
);
210 params
+= sizeof(int32_t);
211 get_user_s32(arg8
, params
);
212 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
222 } else { //if (bsd_type == target_openbsd)
223 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
232 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
233 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
236 env
->eflags
&= ~CC_C
;
241 /* syscall from syscall instruction */
242 if (bsd_type
== target_freebsd
)
243 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
251 else { //if (bsd_type == target_openbsd)
252 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
261 env
->eip
= env
->exception_next_eip
;
262 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
263 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
266 env
->eflags
&= ~CC_C
;
273 info
.si_signo
= SIGBUS
;
275 info
.si_code
= TARGET_SI_KERNEL
;
276 info
._sifields
._sigfault
._addr
= 0;
277 queue_signal(env
, info
.si_signo
, &info
);
280 /* XXX: potential problem if ABI32 */
281 #ifndef TARGET_X86_64
282 if (env
->eflags
& VM_MASK
) {
283 handle_vm86_fault(env
);
287 info
.si_signo
= SIGSEGV
;
289 info
.si_code
= TARGET_SI_KERNEL
;
290 info
._sifields
._sigfault
._addr
= 0;
291 queue_signal(env
, info
.si_signo
, &info
);
295 info
.si_signo
= SIGSEGV
;
297 if (!(env
->error_code
& 1))
298 info
.si_code
= TARGET_SEGV_MAPERR
;
300 info
.si_code
= TARGET_SEGV_ACCERR
;
301 info
._sifields
._sigfault
._addr
= env
->cr
[2];
302 queue_signal(env
, info
.si_signo
, &info
);
305 #ifndef TARGET_X86_64
306 if (env
->eflags
& VM_MASK
) {
307 handle_vm86_trap(env
, trapnr
);
311 /* division by zero */
312 info
.si_signo
= SIGFPE
;
314 info
.si_code
= TARGET_FPE_INTDIV
;
315 info
._sifields
._sigfault
._addr
= env
->eip
;
316 queue_signal(env
, info
.si_signo
, &info
);
321 #ifndef TARGET_X86_64
322 if (env
->eflags
& VM_MASK
) {
323 handle_vm86_trap(env
, trapnr
);
327 info
.si_signo
= SIGTRAP
;
329 if (trapnr
== EXCP01_DB
) {
330 info
.si_code
= TARGET_TRAP_BRKPT
;
331 info
._sifields
._sigfault
._addr
= env
->eip
;
333 info
.si_code
= TARGET_SI_KERNEL
;
334 info
._sifields
._sigfault
._addr
= 0;
336 queue_signal(env
, info
.si_signo
, &info
);
341 #ifndef TARGET_X86_64
342 if (env
->eflags
& VM_MASK
) {
343 handle_vm86_trap(env
, trapnr
);
347 info
.si_signo
= SIGSEGV
;
349 info
.si_code
= TARGET_SI_KERNEL
;
350 info
._sifields
._sigfault
._addr
= 0;
351 queue_signal(env
, info
.si_signo
, &info
);
355 info
.si_signo
= SIGILL
;
357 info
.si_code
= TARGET_ILL_ILLOPN
;
358 info
._sifields
._sigfault
._addr
= env
->eip
;
359 queue_signal(env
, info
.si_signo
, &info
);
363 /* just indicate that signals should be handled asap */
370 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
375 info
.si_code
= TARGET_TRAP_BRKPT
;
376 queue_signal(env
, info
.si_signo
, &info
);
382 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
383 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
387 process_pending_signals(env
);
393 #define SPARC64_STACK_BIAS 2047
396 /* WARNING: dealing with register windows _is_ complicated. More info
397 can be found at http://www.sics.se/~psm/sparcstack.html */
398 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
400 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
401 /* wrap handling : if cwp is on the last window, then we use the
402 registers 'after' the end */
403 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
404 index
+= 16 * env
->nwindows
;
408 /* save the register window 'cwp1' */
409 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
414 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
415 #ifdef TARGET_SPARC64
417 sp_ptr
+= SPARC64_STACK_BIAS
;
419 #if defined(DEBUG_WIN)
420 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
423 for(i
= 0; i
< 16; i
++) {
424 /* FIXME - what to do if put_user() fails? */
425 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
426 sp_ptr
+= sizeof(abi_ulong
);
430 static void save_window(CPUSPARCState
*env
)
432 #ifndef TARGET_SPARC64
433 unsigned int new_wim
;
434 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
435 ((1LL << env
->nwindows
) - 1);
436 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
439 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
445 static void restore_window(CPUSPARCState
*env
)
447 #ifndef TARGET_SPARC64
448 unsigned int new_wim
;
450 unsigned int i
, cwp1
;
453 #ifndef TARGET_SPARC64
454 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
455 ((1LL << env
->nwindows
) - 1);
458 /* restore the invalid window */
459 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
460 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
461 #ifdef TARGET_SPARC64
463 sp_ptr
+= SPARC64_STACK_BIAS
;
465 #if defined(DEBUG_WIN)
466 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
469 for(i
= 0; i
< 16; i
++) {
470 /* FIXME - what to do if get_user() fails? */
471 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
472 sp_ptr
+= sizeof(abi_ulong
);
474 #ifdef TARGET_SPARC64
476 if (env
->cleanwin
< env
->nwindows
- 1)
484 static void flush_windows(CPUSPARCState
*env
)
490 /* if restore would invoke restore_window(), then we can stop */
491 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
492 #ifndef TARGET_SPARC64
493 if (env
->wim
& (1 << cwp1
))
496 if (env
->canrestore
== 0)
501 save_window_offset(env
, cwp1
);
504 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
505 #ifndef TARGET_SPARC64
506 /* set wim so that restore will reload the registers */
507 env
->wim
= 1 << cwp1
;
509 #if defined(DEBUG_WIN)
510 printf("flush_windows: nb=%d\n", offset
- 1);
514 void cpu_loop(CPUSPARCState
*env
)
516 int trapnr
, ret
, syscall_nr
;
517 //target_siginfo_t info;
520 trapnr
= cpu_sparc_exec (env
);
523 #ifndef TARGET_SPARC64
526 /* FreeBSD uses 0x141 for syscalls too */
528 if (bsd_type
!= target_freebsd
)
532 syscall_nr
= env
->gregs
[1];
533 if (bsd_type
== target_freebsd
)
534 ret
= do_freebsd_syscall(env
, syscall_nr
,
535 env
->regwptr
[0], env
->regwptr
[1],
536 env
->regwptr
[2], env
->regwptr
[3],
537 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
538 else if (bsd_type
== target_netbsd
)
539 ret
= do_netbsd_syscall(env
, syscall_nr
,
540 env
->regwptr
[0], env
->regwptr
[1],
541 env
->regwptr
[2], env
->regwptr
[3],
542 env
->regwptr
[4], env
->regwptr
[5]);
543 else { //if (bsd_type == target_openbsd)
544 #if defined(TARGET_SPARC64)
545 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
546 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
548 ret
= do_openbsd_syscall(env
, syscall_nr
,
549 env
->regwptr
[0], env
->regwptr
[1],
550 env
->regwptr
[2], env
->regwptr
[3],
551 env
->regwptr
[4], env
->regwptr
[5]);
553 if ((unsigned int)ret
>= (unsigned int)(-515)) {
555 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
556 env
->xcc
|= PSR_CARRY
;
558 env
->psr
|= PSR_CARRY
;
561 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
562 env
->xcc
&= ~PSR_CARRY
;
564 env
->psr
&= ~PSR_CARRY
;
567 env
->regwptr
[0] = ret
;
568 /* next instruction */
569 #if defined(TARGET_SPARC64)
570 if (bsd_type
== target_openbsd
&&
571 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
572 env
->pc
= env
->gregs
[2];
573 env
->npc
= env
->pc
+ 4;
574 } else if (bsd_type
== target_openbsd
&&
575 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
576 env
->pc
= env
->gregs
[7];
577 env
->npc
= env
->pc
+ 4;
580 env
->npc
= env
->npc
+ 4;
584 env
->npc
= env
->npc
+ 4;
587 case 0x83: /* flush windows */
592 /* next instruction */
594 env
->npc
= env
->npc
+ 4;
596 #ifndef TARGET_SPARC64
597 case TT_WIN_OVF
: /* window overflow */
600 case TT_WIN_UNF
: /* window underflow */
607 info
.si_signo
= SIGSEGV
;
609 /* XXX: check env->error_code */
610 info
.si_code
= TARGET_SEGV_MAPERR
;
611 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
612 queue_signal(env
, info
.si_signo
, &info
);
617 case TT_SPILL
: /* window overflow */
620 case TT_FILL
: /* window underflow */
627 info
.si_signo
= SIGSEGV
;
629 /* XXX: check env->error_code */
630 info
.si_code
= TARGET_SEGV_MAPERR
;
631 if (trapnr
== TT_DFAULT
)
632 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
634 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
635 //queue_signal(env, info.si_signo, &info);
641 /* just indicate that signals should be handled asap */
647 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
653 info
.si_code
= TARGET_TRAP_BRKPT
;
654 //queue_signal(env, info.si_signo, &info);
660 #ifdef TARGET_SPARC64
663 printf ("Unhandled trap: 0x%x\n", trapnr
);
664 cpu_dump_state(env
, stderr
, fprintf
, 0);
667 process_pending_signals (env
);
673 static void usage(void)
675 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
676 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
677 "BSD CPU emulator (compiled for %s emulation)\n"
679 "Standard options:\n"
680 "-h print this help\n"
681 "-g port wait gdb connection to port\n"
682 "-L path set the elf interpreter prefix (default=%s)\n"
683 "-s size set the stack size in bytes (default=%ld)\n"
684 "-cpu model select CPU (-cpu help for list)\n"
685 "-drop-ld-preload drop LD_PRELOAD for target process\n"
686 "-E var=value sets/modifies targets environment variable(s)\n"
687 "-U var unsets targets environment variable(s)\n"
688 #if defined(CONFIG_USE_GUEST_BASE)
689 "-B address set guest_base address to address\n"
691 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
694 "-d options activate log (default logfile=%s)\n"
695 "-D logfile override default logfile location\n"
696 "-p pagesize set the host page size to 'pagesize'\n"
697 "-singlestep always run in singlestep mode\n"
698 "-strace log system calls\n"
700 "Environment variables:\n"
701 "QEMU_STRACE Print system calls and arguments similar to the\n"
702 " 'strace' program. Enable by setting to any value.\n"
703 "You can use -E and -U options to set/unset environment variables\n"
704 "for target process. It is possible to provide several variables\n"
705 "by repeating the option. For example:\n"
706 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
707 "Note that if you provide several changes to single variable\n"
708 "last change will stay in effect.\n"
717 THREAD CPUArchState
*thread_env
;
719 /* Assumes contents are already zeroed. */
720 void init_task_state(TaskState
*ts
)
725 ts
->first_free
= ts
->sigqueue_table
;
726 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
727 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
729 ts
->sigqueue_table
[i
].next
= NULL
;
732 int main(int argc
, char **argv
)
734 const char *filename
;
735 const char *cpu_model
;
736 const char *log_file
= DEBUG_LOGFILE
;
737 const char *log_mask
= NULL
;
738 struct target_pt_regs regs1
, *regs
= ®s1
;
739 struct image_info info1
, *info
= &info1
;
740 TaskState ts1
, *ts
= &ts1
;
744 int gdbstub_port
= 0;
745 char **target_environ
, **wrk
;
746 envlist_t
*envlist
= NULL
;
747 bsd_type
= target_openbsd
;
752 module_call_init(MODULE_INIT_QOM
);
754 if ((envlist
= envlist_create()) == NULL
) {
755 (void) fprintf(stderr
, "Unable to allocate envlist\n");
759 /* add current environment into the list */
760 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
761 (void) envlist_setenv(envlist
, *wrk
);
765 #if defined(cpudef_setup)
766 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
778 if (!strcmp(r
, "-")) {
780 } else if (!strcmp(r
, "d")) {
781 if (optind
>= argc
) {
784 log_mask
= argv
[optind
++];
785 } else if (!strcmp(r
, "D")) {
786 if (optind
>= argc
) {
789 log_file
= argv
[optind
++];
790 } else if (!strcmp(r
, "E")) {
792 if (envlist_setenv(envlist
, r
) != 0)
794 } else if (!strcmp(r
, "ignore-environment")) {
795 envlist_free(envlist
);
796 if ((envlist
= envlist_create()) == NULL
) {
797 (void) fprintf(stderr
, "Unable to allocate envlist\n");
800 } else if (!strcmp(r
, "U")) {
802 if (envlist_unsetenv(envlist
, r
) != 0)
804 } else if (!strcmp(r
, "s")) {
806 x86_stack_size
= strtol(r
, (char **)&r
, 0);
807 if (x86_stack_size
<= 0)
810 x86_stack_size
*= 1024 * 1024;
811 else if (*r
== 'k' || *r
== 'K')
812 x86_stack_size
*= 1024;
813 } else if (!strcmp(r
, "L")) {
814 interp_prefix
= argv
[optind
++];
815 } else if (!strcmp(r
, "p")) {
816 qemu_host_page_size
= atoi(argv
[optind
++]);
817 if (qemu_host_page_size
== 0 ||
818 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
819 fprintf(stderr
, "page size must be a power of two\n");
822 } else if (!strcmp(r
, "g")) {
823 gdbstub_port
= atoi(argv
[optind
++]);
824 } else if (!strcmp(r
, "r")) {
825 qemu_uname_release
= argv
[optind
++];
826 } else if (!strcmp(r
, "cpu")) {
827 cpu_model
= argv
[optind
++];
828 if (is_help_option(cpu_model
)) {
829 /* XXX: implement xxx_cpu_list for targets that still miss it */
830 #if defined(cpu_list)
831 cpu_list(stdout
, &fprintf
);
835 #if defined(CONFIG_USE_GUEST_BASE)
836 } else if (!strcmp(r
, "B")) {
837 guest_base
= strtol(argv
[optind
++], NULL
, 0);
840 } else if (!strcmp(r
, "drop-ld-preload")) {
841 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
842 } else if (!strcmp(r
, "bsd")) {
843 if (!strcasecmp(argv
[optind
], "freebsd")) {
844 bsd_type
= target_freebsd
;
845 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
846 bsd_type
= target_netbsd
;
847 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
848 bsd_type
= target_openbsd
;
853 } else if (!strcmp(r
, "singlestep")) {
855 } else if (!strcmp(r
, "strace")) {
864 cpu_set_log_filename(log_file
);
867 const CPULogItem
*item
;
869 mask
= cpu_str_to_log_mask(log_mask
);
871 printf("Log items (comma separated):\n");
872 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
873 printf("%-10s %s\n", item
->name
, item
->help
);
880 if (optind
>= argc
) {
883 filename
= argv
[optind
];
886 memset(regs
, 0, sizeof(struct target_pt_regs
));
888 /* Zero out image_info */
889 memset(info
, 0, sizeof(struct image_info
));
891 /* Scan interp_prefix dir for replacement files. */
892 init_paths(interp_prefix
);
894 if (cpu_model
== NULL
) {
895 #if defined(TARGET_I386)
897 cpu_model
= "qemu64";
899 cpu_model
= "qemu32";
901 #elif defined(TARGET_SPARC)
902 #ifdef TARGET_SPARC64
903 cpu_model
= "TI UltraSparc II";
905 cpu_model
= "Fujitsu MB86904";
913 /* NOTE: we need to init the CPU at this stage to get
914 qemu_host_page_size */
915 env
= cpu_init(cpu_model
);
917 fprintf(stderr
, "Unable to find CPU definition\n");
920 #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
921 cpu_reset(ENV_GET_CPU(env
));
925 if (getenv("QEMU_STRACE")) {
929 target_environ
= envlist_to_environ(envlist
, NULL
);
930 envlist_free(envlist
);
932 #if defined(CONFIG_USE_GUEST_BASE)
934 * Now that page sizes are configured in cpu_init() we can do
935 * proper page alignment for guest_base.
937 guest_base
= HOST_PAGE_ALIGN(guest_base
);
940 * Read in mmap_min_addr kernel parameter. This value is used
941 * When loading the ELF image to determine whether guest_base
944 * When user has explicitly set the quest base, we skip this
947 if (!have_guest_base
) {
950 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
952 if (fscanf(fp
, "%lu", &tmp
) == 1) {
954 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr
);
959 #endif /* CONFIG_USE_GUEST_BASE */
961 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
962 printf("Error loading %s\n", filename
);
966 for (wrk
= target_environ
; *wrk
; wrk
++) {
970 free(target_environ
);
972 if (qemu_log_enabled()) {
973 #if defined(CONFIG_USE_GUEST_BASE)
974 qemu_log("guest_base 0x%lx\n", guest_base
);
978 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
979 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
980 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
982 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
984 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
985 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
987 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
988 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
991 target_set_brk(info
->brk
);
995 #if defined(CONFIG_USE_GUEST_BASE)
996 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
997 generating the prologue until now so that the prologue can take
998 the real value of GUEST_BASE into account. */
999 tcg_prologue_init(&tcg_ctx
);
1002 /* build Task State */
1003 memset(ts
, 0, sizeof(TaskState
));
1004 init_task_state(ts
);
1008 #if defined(TARGET_I386)
1009 cpu_x86_set_cpl(env
, 3);
1011 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
1012 env
->hflags
|= HF_PE_MASK
;
1013 if (env
->cpuid_features
& CPUID_SSE
) {
1014 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1015 env
->hflags
|= HF_OSFXSR_MASK
;
1017 #ifndef TARGET_ABI32
1018 /* enable 64 bit mode if possible */
1019 if (!(env
->cpuid_ext2_features
& CPUID_EXT2_LM
)) {
1020 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1023 env
->cr
[4] |= CR4_PAE_MASK
;
1024 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1025 env
->hflags
|= HF_LMA_MASK
;
1028 /* flags setup : we activate the IRQs by default as in user mode */
1029 env
->eflags
|= IF_MASK
;
1031 /* linux register setup */
1032 #ifndef TARGET_ABI32
1033 env
->regs
[R_EAX
] = regs
->rax
;
1034 env
->regs
[R_EBX
] = regs
->rbx
;
1035 env
->regs
[R_ECX
] = regs
->rcx
;
1036 env
->regs
[R_EDX
] = regs
->rdx
;
1037 env
->regs
[R_ESI
] = regs
->rsi
;
1038 env
->regs
[R_EDI
] = regs
->rdi
;
1039 env
->regs
[R_EBP
] = regs
->rbp
;
1040 env
->regs
[R_ESP
] = regs
->rsp
;
1041 env
->eip
= regs
->rip
;
1043 env
->regs
[R_EAX
] = regs
->eax
;
1044 env
->regs
[R_EBX
] = regs
->ebx
;
1045 env
->regs
[R_ECX
] = regs
->ecx
;
1046 env
->regs
[R_EDX
] = regs
->edx
;
1047 env
->regs
[R_ESI
] = regs
->esi
;
1048 env
->regs
[R_EDI
] = regs
->edi
;
1049 env
->regs
[R_EBP
] = regs
->ebp
;
1050 env
->regs
[R_ESP
] = regs
->esp
;
1051 env
->eip
= regs
->eip
;
1054 /* linux interrupt setup */
1055 #ifndef TARGET_ABI32
1056 env
->idt
.limit
= 511;
1058 env
->idt
.limit
= 255;
1060 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1061 PROT_READ
|PROT_WRITE
,
1062 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1063 idt_table
= g2h(env
->idt
.base
);
1086 /* linux segment setup */
1088 uint64_t *gdt_table
;
1089 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1090 PROT_READ
|PROT_WRITE
,
1091 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1092 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1093 gdt_table
= g2h(env
->gdt
.base
);
1095 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1096 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1097 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1099 /* 64 bit code segment */
1100 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1101 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1103 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1105 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1106 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1107 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1110 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1111 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1113 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1114 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1115 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1116 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1117 /* This hack makes Wine work... */
1118 env
->segs
[R_FS
].selector
= 0;
1120 cpu_x86_load_seg(env
, R_DS
, 0);
1121 cpu_x86_load_seg(env
, R_ES
, 0);
1122 cpu_x86_load_seg(env
, R_FS
, 0);
1123 cpu_x86_load_seg(env
, R_GS
, 0);
1125 #elif defined(TARGET_SPARC)
1129 env
->npc
= regs
->npc
;
1131 for(i
= 0; i
< 8; i
++)
1132 env
->gregs
[i
] = regs
->u_regs
[i
];
1133 for(i
= 0; i
< 8; i
++)
1134 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1137 #error unsupported target CPU
1141 gdbserver_start (gdbstub_port
);
1142 gdb_handlesig(env
, 0);