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"
38 #if defined(CONFIG_USE_GUEST_BASE)
39 unsigned long mmap_min_addr
;
40 unsigned long guest_base
;
42 unsigned long reserved_va
;
45 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
46 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
47 extern char **environ
;
48 enum BSDType bsd_type
;
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 #if defined(TARGET_I386)
65 int cpu_get_pic_interrupt(CPUX86State
*env
)
71 /* These are no-ops because we are not threadsafe. */
72 static inline void cpu_exec_start(CPUArchState
*env
)
76 static inline void cpu_exec_end(CPUArchState
*env
)
80 static inline void start_exclusive(void)
84 static inline void end_exclusive(void)
92 void fork_end(int child
)
95 gdbserver_fork(thread_env
);
99 void cpu_list_lock(void)
103 void cpu_list_unlock(void)
108 /***********************************************************/
109 /* CPUX86 core interface */
111 void cpu_smm_update(CPUX86State
*env
)
115 uint64_t cpu_get_tsc(CPUX86State
*env
)
117 return cpu_get_real_ticks();
120 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
125 e1
= (addr
<< 16) | (limit
& 0xffff);
126 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
133 static uint64_t *idt_table
;
135 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
136 uint64_t addr
, unsigned int sel
)
139 e1
= (addr
& 0xffff) | (sel
<< 16);
140 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
144 p
[2] = tswap32(addr
>> 32);
147 /* only dpl matters as we do only user space emulation */
148 static void set_idt(int n
, unsigned int dpl
)
150 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
153 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
154 uint32_t addr
, unsigned int sel
)
157 e1
= (addr
& 0xffff) | (sel
<< 16);
158 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
164 /* only dpl matters as we do only user space emulation */
165 static void set_idt(int n
, unsigned int dpl
)
167 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
171 void cpu_loop(CPUX86State
*env
)
175 //target_siginfo_t info;
178 trapnr
= cpu_x86_exec(env
);
181 /* syscall from int $0x80 */
182 if (bsd_type
== target_freebsd
) {
183 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
185 int32_t syscall_nr
= env
->regs
[R_EAX
];
186 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
188 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
189 get_user_s32(syscall_nr
, params
);
190 params
+= sizeof(int32_t);
191 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
192 get_user_s32(syscall_nr
, params
);
193 params
+= sizeof(int64_t);
195 get_user_s32(arg1
, params
);
196 params
+= sizeof(int32_t);
197 get_user_s32(arg2
, params
);
198 params
+= sizeof(int32_t);
199 get_user_s32(arg3
, params
);
200 params
+= sizeof(int32_t);
201 get_user_s32(arg4
, params
);
202 params
+= sizeof(int32_t);
203 get_user_s32(arg5
, params
);
204 params
+= sizeof(int32_t);
205 get_user_s32(arg6
, params
);
206 params
+= sizeof(int32_t);
207 get_user_s32(arg7
, params
);
208 params
+= sizeof(int32_t);
209 get_user_s32(arg8
, params
);
210 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
220 } else { //if (bsd_type == target_openbsd)
221 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
230 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
231 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
234 env
->eflags
&= ~CC_C
;
239 /* syscall from syscall instruction */
240 if (bsd_type
== target_freebsd
)
241 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
249 else { //if (bsd_type == target_openbsd)
250 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
259 env
->eip
= env
->exception_next_eip
;
260 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
261 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
264 env
->eflags
&= ~CC_C
;
271 info
.si_signo
= SIGBUS
;
273 info
.si_code
= TARGET_SI_KERNEL
;
274 info
._sifields
._sigfault
._addr
= 0;
275 queue_signal(env
, info
.si_signo
, &info
);
278 /* XXX: potential problem if ABI32 */
279 #ifndef TARGET_X86_64
280 if (env
->eflags
& VM_MASK
) {
281 handle_vm86_fault(env
);
285 info
.si_signo
= SIGSEGV
;
287 info
.si_code
= TARGET_SI_KERNEL
;
288 info
._sifields
._sigfault
._addr
= 0;
289 queue_signal(env
, info
.si_signo
, &info
);
293 info
.si_signo
= SIGSEGV
;
295 if (!(env
->error_code
& 1))
296 info
.si_code
= TARGET_SEGV_MAPERR
;
298 info
.si_code
= TARGET_SEGV_ACCERR
;
299 info
._sifields
._sigfault
._addr
= env
->cr
[2];
300 queue_signal(env
, info
.si_signo
, &info
);
303 #ifndef TARGET_X86_64
304 if (env
->eflags
& VM_MASK
) {
305 handle_vm86_trap(env
, trapnr
);
309 /* division by zero */
310 info
.si_signo
= SIGFPE
;
312 info
.si_code
= TARGET_FPE_INTDIV
;
313 info
._sifields
._sigfault
._addr
= env
->eip
;
314 queue_signal(env
, info
.si_signo
, &info
);
319 #ifndef TARGET_X86_64
320 if (env
->eflags
& VM_MASK
) {
321 handle_vm86_trap(env
, trapnr
);
325 info
.si_signo
= SIGTRAP
;
327 if (trapnr
== EXCP01_DB
) {
328 info
.si_code
= TARGET_TRAP_BRKPT
;
329 info
._sifields
._sigfault
._addr
= env
->eip
;
331 info
.si_code
= TARGET_SI_KERNEL
;
332 info
._sifields
._sigfault
._addr
= 0;
334 queue_signal(env
, info
.si_signo
, &info
);
339 #ifndef TARGET_X86_64
340 if (env
->eflags
& VM_MASK
) {
341 handle_vm86_trap(env
, trapnr
);
345 info
.si_signo
= SIGSEGV
;
347 info
.si_code
= TARGET_SI_KERNEL
;
348 info
._sifields
._sigfault
._addr
= 0;
349 queue_signal(env
, info
.si_signo
, &info
);
353 info
.si_signo
= SIGILL
;
355 info
.si_code
= TARGET_ILL_ILLOPN
;
356 info
._sifields
._sigfault
._addr
= env
->eip
;
357 queue_signal(env
, info
.si_signo
, &info
);
361 /* just indicate that signals should be handled asap */
368 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
373 info
.si_code
= TARGET_TRAP_BRKPT
;
374 queue_signal(env
, info
.si_signo
, &info
);
380 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
381 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
385 process_pending_signals(env
);
391 #define SPARC64_STACK_BIAS 2047
394 /* WARNING: dealing with register windows _is_ complicated. More info
395 can be found at http://www.sics.se/~psm/sparcstack.html */
396 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
398 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
399 /* wrap handling : if cwp is on the last window, then we use the
400 registers 'after' the end */
401 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
402 index
+= 16 * env
->nwindows
;
406 /* save the register window 'cwp1' */
407 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
412 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
413 #ifdef TARGET_SPARC64
415 sp_ptr
+= SPARC64_STACK_BIAS
;
417 #if defined(DEBUG_WIN)
418 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
421 for(i
= 0; i
< 16; i
++) {
422 /* FIXME - what to do if put_user() fails? */
423 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
424 sp_ptr
+= sizeof(abi_ulong
);
428 static void save_window(CPUSPARCState
*env
)
430 #ifndef TARGET_SPARC64
431 unsigned int new_wim
;
432 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
433 ((1LL << env
->nwindows
) - 1);
434 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
437 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
443 static void restore_window(CPUSPARCState
*env
)
445 #ifndef TARGET_SPARC64
446 unsigned int new_wim
;
448 unsigned int i
, cwp1
;
451 #ifndef TARGET_SPARC64
452 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
453 ((1LL << env
->nwindows
) - 1);
456 /* restore the invalid window */
457 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
458 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
459 #ifdef TARGET_SPARC64
461 sp_ptr
+= SPARC64_STACK_BIAS
;
463 #if defined(DEBUG_WIN)
464 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
467 for(i
= 0; i
< 16; i
++) {
468 /* FIXME - what to do if get_user() fails? */
469 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
470 sp_ptr
+= sizeof(abi_ulong
);
472 #ifdef TARGET_SPARC64
474 if (env
->cleanwin
< env
->nwindows
- 1)
482 static void flush_windows(CPUSPARCState
*env
)
488 /* if restore would invoke restore_window(), then we can stop */
489 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
490 #ifndef TARGET_SPARC64
491 if (env
->wim
& (1 << cwp1
))
494 if (env
->canrestore
== 0)
499 save_window_offset(env
, cwp1
);
502 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
503 #ifndef TARGET_SPARC64
504 /* set wim so that restore will reload the registers */
505 env
->wim
= 1 << cwp1
;
507 #if defined(DEBUG_WIN)
508 printf("flush_windows: nb=%d\n", offset
- 1);
512 void cpu_loop(CPUSPARCState
*env
)
514 int trapnr
, ret
, syscall_nr
;
515 //target_siginfo_t info;
518 trapnr
= cpu_sparc_exec (env
);
521 #ifndef TARGET_SPARC64
524 /* FreeBSD uses 0x141 for syscalls too */
526 if (bsd_type
!= target_freebsd
)
530 syscall_nr
= env
->gregs
[1];
531 if (bsd_type
== target_freebsd
)
532 ret
= do_freebsd_syscall(env
, syscall_nr
,
533 env
->regwptr
[0], env
->regwptr
[1],
534 env
->regwptr
[2], env
->regwptr
[3],
535 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
536 else if (bsd_type
== target_netbsd
)
537 ret
= do_netbsd_syscall(env
, syscall_nr
,
538 env
->regwptr
[0], env
->regwptr
[1],
539 env
->regwptr
[2], env
->regwptr
[3],
540 env
->regwptr
[4], env
->regwptr
[5]);
541 else { //if (bsd_type == target_openbsd)
542 #if defined(TARGET_SPARC64)
543 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
544 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
546 ret
= do_openbsd_syscall(env
, syscall_nr
,
547 env
->regwptr
[0], env
->regwptr
[1],
548 env
->regwptr
[2], env
->regwptr
[3],
549 env
->regwptr
[4], env
->regwptr
[5]);
551 if ((unsigned int)ret
>= (unsigned int)(-515)) {
553 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
554 env
->xcc
|= PSR_CARRY
;
556 env
->psr
|= PSR_CARRY
;
559 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
560 env
->xcc
&= ~PSR_CARRY
;
562 env
->psr
&= ~PSR_CARRY
;
565 env
->regwptr
[0] = ret
;
566 /* next instruction */
567 #if defined(TARGET_SPARC64)
568 if (bsd_type
== target_openbsd
&&
569 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
570 env
->pc
= env
->gregs
[2];
571 env
->npc
= env
->pc
+ 4;
572 } else if (bsd_type
== target_openbsd
&&
573 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
574 env
->pc
= env
->gregs
[7];
575 env
->npc
= env
->pc
+ 4;
578 env
->npc
= env
->npc
+ 4;
582 env
->npc
= env
->npc
+ 4;
585 case 0x83: /* flush windows */
590 /* next instruction */
592 env
->npc
= env
->npc
+ 4;
594 #ifndef TARGET_SPARC64
595 case TT_WIN_OVF
: /* window overflow */
598 case TT_WIN_UNF
: /* window underflow */
605 info
.si_signo
= SIGSEGV
;
607 /* XXX: check env->error_code */
608 info
.si_code
= TARGET_SEGV_MAPERR
;
609 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
610 queue_signal(env
, info
.si_signo
, &info
);
615 case TT_SPILL
: /* window overflow */
618 case TT_FILL
: /* window underflow */
625 info
.si_signo
= SIGSEGV
;
627 /* XXX: check env->error_code */
628 info
.si_code
= TARGET_SEGV_MAPERR
;
629 if (trapnr
== TT_DFAULT
)
630 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
632 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
633 //queue_signal(env, info.si_signo, &info);
639 /* just indicate that signals should be handled asap */
645 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
651 info
.si_code
= TARGET_TRAP_BRKPT
;
652 //queue_signal(env, info.si_signo, &info);
658 #ifdef TARGET_SPARC64
661 printf ("Unhandled trap: 0x%x\n", trapnr
);
662 cpu_dump_state(env
, stderr
, fprintf
, 0);
665 process_pending_signals (env
);
671 static void usage(void)
673 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
674 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
675 "BSD CPU emulator (compiled for %s emulation)\n"
677 "Standard options:\n"
678 "-h print this help\n"
679 "-g port wait gdb connection to port\n"
680 "-L path set the elf interpreter prefix (default=%s)\n"
681 "-s size set the stack size in bytes (default=%ld)\n"
682 "-cpu model select CPU (-cpu help for list)\n"
683 "-drop-ld-preload drop LD_PRELOAD for target process\n"
684 "-E var=value sets/modifies targets environment variable(s)\n"
685 "-U var unsets targets environment variable(s)\n"
686 #if defined(CONFIG_USE_GUEST_BASE)
687 "-B address set guest_base address to address\n"
689 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
692 "-d item1[,...] enable logging of specified items\n"
693 " (use '-d help' for a list of log items)\n"
694 "-D logfile write logs to 'logfile' (default stderr)\n"
695 "-p pagesize set the host page size to 'pagesize'\n"
696 "-singlestep always run in singlestep mode\n"
697 "-strace log system calls\n"
699 "Environment variables:\n"
700 "QEMU_STRACE Print system calls and arguments similar to the\n"
701 " 'strace' program. Enable by setting to any value.\n"
702 "You can use -E and -U options to set/unset environment variables\n"
703 "for target process. It is possible to provide several variables\n"
704 "by repeating the option. For example:\n"
705 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
706 "Note that if you provide several changes to single variable\n"
707 "last change will stay in effect.\n"
715 THREAD CPUArchState
*thread_env
;
717 /* Assumes contents are already zeroed. */
718 void init_task_state(TaskState
*ts
)
723 ts
->first_free
= ts
->sigqueue_table
;
724 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
725 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
727 ts
->sigqueue_table
[i
].next
= NULL
;
730 int main(int argc
, char **argv
)
732 const char *filename
;
733 const char *cpu_model
;
734 const char *log_file
= NULL
;
735 const char *log_mask
= NULL
;
736 struct target_pt_regs regs1
, *regs
= ®s1
;
737 struct image_info info1
, *info
= &info1
;
738 TaskState ts1
, *ts
= &ts1
;
742 int gdbstub_port
= 0;
743 char **target_environ
, **wrk
;
744 envlist_t
*envlist
= NULL
;
745 bsd_type
= target_openbsd
;
750 module_call_init(MODULE_INIT_QOM
);
752 if ((envlist
= envlist_create()) == NULL
) {
753 (void) fprintf(stderr
, "Unable to allocate envlist\n");
757 /* add current environment into the list */
758 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
759 (void) envlist_setenv(envlist
, *wrk
);
763 #if defined(cpudef_setup)
764 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
776 if (!strcmp(r
, "-")) {
778 } else if (!strcmp(r
, "d")) {
779 if (optind
>= argc
) {
782 log_mask
= argv
[optind
++];
783 } else if (!strcmp(r
, "D")) {
784 if (optind
>= argc
) {
787 log_file
= argv
[optind
++];
788 } else if (!strcmp(r
, "E")) {
790 if (envlist_setenv(envlist
, r
) != 0)
792 } else if (!strcmp(r
, "ignore-environment")) {
793 envlist_free(envlist
);
794 if ((envlist
= envlist_create()) == NULL
) {
795 (void) fprintf(stderr
, "Unable to allocate envlist\n");
798 } else if (!strcmp(r
, "U")) {
800 if (envlist_unsetenv(envlist
, r
) != 0)
802 } else if (!strcmp(r
, "s")) {
804 x86_stack_size
= strtol(r
, (char **)&r
, 0);
805 if (x86_stack_size
<= 0)
808 x86_stack_size
*= 1024 * 1024;
809 else if (*r
== 'k' || *r
== 'K')
810 x86_stack_size
*= 1024;
811 } else if (!strcmp(r
, "L")) {
812 interp_prefix
= argv
[optind
++];
813 } else if (!strcmp(r
, "p")) {
814 qemu_host_page_size
= atoi(argv
[optind
++]);
815 if (qemu_host_page_size
== 0 ||
816 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
817 fprintf(stderr
, "page size must be a power of two\n");
820 } else if (!strcmp(r
, "g")) {
821 gdbstub_port
= atoi(argv
[optind
++]);
822 } else if (!strcmp(r
, "r")) {
823 qemu_uname_release
= argv
[optind
++];
824 } else if (!strcmp(r
, "cpu")) {
825 cpu_model
= argv
[optind
++];
826 if (is_help_option(cpu_model
)) {
827 /* XXX: implement xxx_cpu_list for targets that still miss it */
828 #if defined(cpu_list)
829 cpu_list(stdout
, &fprintf
);
833 #if defined(CONFIG_USE_GUEST_BASE)
834 } else if (!strcmp(r
, "B")) {
835 guest_base
= strtol(argv
[optind
++], NULL
, 0);
838 } else if (!strcmp(r
, "drop-ld-preload")) {
839 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
840 } else if (!strcmp(r
, "bsd")) {
841 if (!strcasecmp(argv
[optind
], "freebsd")) {
842 bsd_type
= target_freebsd
;
843 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
844 bsd_type
= target_netbsd
;
845 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
846 bsd_type
= target_openbsd
;
851 } else if (!strcmp(r
, "singlestep")) {
853 } else if (!strcmp(r
, "strace")) {
862 qemu_set_log_filename(log_file
);
866 mask
= qemu_str_to_log_mask(log_mask
);
868 qemu_print_log_usage(stdout
);
874 if (optind
>= argc
) {
877 filename
= argv
[optind
];
880 memset(regs
, 0, sizeof(struct target_pt_regs
));
882 /* Zero out image_info */
883 memset(info
, 0, sizeof(struct image_info
));
885 /* Scan interp_prefix dir for replacement files. */
886 init_paths(interp_prefix
);
888 if (cpu_model
== NULL
) {
889 #if defined(TARGET_I386)
891 cpu_model
= "qemu64";
893 cpu_model
= "qemu32";
895 #elif defined(TARGET_SPARC)
896 #ifdef TARGET_SPARC64
897 cpu_model
= "TI UltraSparc II";
899 cpu_model
= "Fujitsu MB86904";
907 /* NOTE: we need to init the CPU at this stage to get
908 qemu_host_page_size */
909 env
= cpu_init(cpu_model
);
911 fprintf(stderr
, "Unable to find CPU definition\n");
914 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
915 cpu_reset(ENV_GET_CPU(env
));
919 if (getenv("QEMU_STRACE")) {
923 target_environ
= envlist_to_environ(envlist
, NULL
);
924 envlist_free(envlist
);
926 #if defined(CONFIG_USE_GUEST_BASE)
928 * Now that page sizes are configured in cpu_init() we can do
929 * proper page alignment for guest_base.
931 guest_base
= HOST_PAGE_ALIGN(guest_base
);
934 * Read in mmap_min_addr kernel parameter. This value is used
935 * When loading the ELF image to determine whether guest_base
938 * When user has explicitly set the quest base, we skip this
941 if (!have_guest_base
) {
944 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
946 if (fscanf(fp
, "%lu", &tmp
) == 1) {
948 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr
);
953 #endif /* CONFIG_USE_GUEST_BASE */
955 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
956 printf("Error loading %s\n", filename
);
960 for (wrk
= target_environ
; *wrk
; wrk
++) {
964 free(target_environ
);
966 if (qemu_log_enabled()) {
967 #if defined(CONFIG_USE_GUEST_BASE)
968 qemu_log("guest_base 0x%lx\n", guest_base
);
972 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
973 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
974 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
976 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
978 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
979 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
981 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
982 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
985 target_set_brk(info
->brk
);
989 #if defined(CONFIG_USE_GUEST_BASE)
990 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
991 generating the prologue until now so that the prologue can take
992 the real value of GUEST_BASE into account. */
993 tcg_prologue_init(&tcg_ctx
);
996 /* build Task State */
997 memset(ts
, 0, sizeof(TaskState
));
1002 #if defined(TARGET_I386)
1003 cpu_x86_set_cpl(env
, 3);
1005 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
1006 env
->hflags
|= HF_PE_MASK
;
1007 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
1008 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1009 env
->hflags
|= HF_OSFXSR_MASK
;
1011 #ifndef TARGET_ABI32
1012 /* enable 64 bit mode if possible */
1013 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1014 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1017 env
->cr
[4] |= CR4_PAE_MASK
;
1018 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1019 env
->hflags
|= HF_LMA_MASK
;
1022 /* flags setup : we activate the IRQs by default as in user mode */
1023 env
->eflags
|= IF_MASK
;
1025 /* linux register setup */
1026 #ifndef TARGET_ABI32
1027 env
->regs
[R_EAX
] = regs
->rax
;
1028 env
->regs
[R_EBX
] = regs
->rbx
;
1029 env
->regs
[R_ECX
] = regs
->rcx
;
1030 env
->regs
[R_EDX
] = regs
->rdx
;
1031 env
->regs
[R_ESI
] = regs
->rsi
;
1032 env
->regs
[R_EDI
] = regs
->rdi
;
1033 env
->regs
[R_EBP
] = regs
->rbp
;
1034 env
->regs
[R_ESP
] = regs
->rsp
;
1035 env
->eip
= regs
->rip
;
1037 env
->regs
[R_EAX
] = regs
->eax
;
1038 env
->regs
[R_EBX
] = regs
->ebx
;
1039 env
->regs
[R_ECX
] = regs
->ecx
;
1040 env
->regs
[R_EDX
] = regs
->edx
;
1041 env
->regs
[R_ESI
] = regs
->esi
;
1042 env
->regs
[R_EDI
] = regs
->edi
;
1043 env
->regs
[R_EBP
] = regs
->ebp
;
1044 env
->regs
[R_ESP
] = regs
->esp
;
1045 env
->eip
= regs
->eip
;
1048 /* linux interrupt setup */
1049 #ifndef TARGET_ABI32
1050 env
->idt
.limit
= 511;
1052 env
->idt
.limit
= 255;
1054 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1055 PROT_READ
|PROT_WRITE
,
1056 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1057 idt_table
= g2h(env
->idt
.base
);
1080 /* linux segment setup */
1082 uint64_t *gdt_table
;
1083 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1084 PROT_READ
|PROT_WRITE
,
1085 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1086 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1087 gdt_table
= g2h(env
->gdt
.base
);
1089 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1090 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1091 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1093 /* 64 bit code segment */
1094 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1095 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1097 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1099 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1100 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1101 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1104 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1105 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1107 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1108 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1109 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1110 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1111 /* This hack makes Wine work... */
1112 env
->segs
[R_FS
].selector
= 0;
1114 cpu_x86_load_seg(env
, R_DS
, 0);
1115 cpu_x86_load_seg(env
, R_ES
, 0);
1116 cpu_x86_load_seg(env
, R_FS
, 0);
1117 cpu_x86_load_seg(env
, R_GS
, 0);
1119 #elif defined(TARGET_SPARC)
1123 env
->npc
= regs
->npc
;
1125 for(i
= 0; i
< 8; i
++)
1126 env
->gregs
[i
] = regs
->u_regs
[i
];
1127 for(i
= 0; i
< 8; i
++)
1128 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1131 #error unsupported target CPU
1135 gdbserver_start (gdbstub_port
);
1136 gdb_handlesig(env
, 0);