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/>.
19 #include "qemu/osdep.h"
20 #include "qemu-version.h"
21 #include <machine/trap.h>
23 #include "qapi/error.h"
25 #include "qemu/config-file.h"
26 #include "qemu/path.h"
27 #include "qemu/help_option.h"
29 #include "exec/exec-all.h"
31 #include "qemu/timer.h"
32 #include "qemu/envlist.h"
34 #include "trace/control.h"
37 unsigned long mmap_min_addr
;
38 unsigned long guest_base
;
40 unsigned long reserved_va
;
42 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
43 const char *qemu_uname_release
;
44 extern char **environ
;
45 enum BSDType bsd_type
;
47 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
48 we allocate a bigger stack. Need a better solution, for example
49 by remapping the process stack directly at the right place */
50 unsigned long x86_stack_size
= 512 * 1024;
52 void gemu_log(const char *fmt
, ...)
57 vfprintf(stderr
, fmt
, ap
);
61 #if defined(TARGET_I386)
62 int cpu_get_pic_interrupt(CPUX86State
*env
)
72 void fork_end(int child
)
75 gdbserver_fork(thread_cpu
);
80 /***********************************************************/
81 /* CPUX86 core interface */
83 uint64_t cpu_get_tsc(CPUX86State
*env
)
85 return cpu_get_host_ticks();
88 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
93 e1
= (addr
<< 16) | (limit
& 0xffff);
94 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
101 static uint64_t *idt_table
;
103 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
104 uint64_t addr
, unsigned int sel
)
107 e1
= (addr
& 0xffff) | (sel
<< 16);
108 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
112 p
[2] = tswap32(addr
>> 32);
115 /* only dpl matters as we do only user space emulation */
116 static void set_idt(int n
, unsigned int dpl
)
118 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
121 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
122 uint32_t addr
, unsigned int sel
)
125 e1
= (addr
& 0xffff) | (sel
<< 16);
126 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
132 /* only dpl matters as we do only user space emulation */
133 static void set_idt(int n
, unsigned int dpl
)
135 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
139 void cpu_loop(CPUX86State
*env
)
141 X86CPU
*cpu
= x86_env_get_cpu(env
);
142 CPUState
*cs
= CPU(cpu
);
145 //target_siginfo_t info;
149 trapnr
= cpu_exec(cs
);
151 process_queued_cpu_work(cs
);
155 /* syscall from int $0x80 */
156 if (bsd_type
== target_freebsd
) {
157 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
159 int32_t syscall_nr
= env
->regs
[R_EAX
];
160 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
162 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
163 get_user_s32(syscall_nr
, params
);
164 params
+= sizeof(int32_t);
165 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
166 get_user_s32(syscall_nr
, params
);
167 params
+= sizeof(int64_t);
169 get_user_s32(arg1
, params
);
170 params
+= sizeof(int32_t);
171 get_user_s32(arg2
, params
);
172 params
+= sizeof(int32_t);
173 get_user_s32(arg3
, params
);
174 params
+= sizeof(int32_t);
175 get_user_s32(arg4
, params
);
176 params
+= sizeof(int32_t);
177 get_user_s32(arg5
, params
);
178 params
+= sizeof(int32_t);
179 get_user_s32(arg6
, params
);
180 params
+= sizeof(int32_t);
181 get_user_s32(arg7
, params
);
182 params
+= sizeof(int32_t);
183 get_user_s32(arg8
, params
);
184 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
194 } else { //if (bsd_type == target_openbsd)
195 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
204 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
205 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
208 env
->eflags
&= ~CC_C
;
213 /* syscall from syscall instruction */
214 if (bsd_type
== target_freebsd
)
215 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
223 else { //if (bsd_type == target_openbsd)
224 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
233 env
->eip
= env
->exception_next_eip
;
234 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
235 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
238 env
->eflags
&= ~CC_C
;
245 info
.si_signo
= SIGBUS
;
247 info
.si_code
= TARGET_SI_KERNEL
;
248 info
._sifields
._sigfault
._addr
= 0;
249 queue_signal(env
, info
.si_signo
, &info
);
252 /* XXX: potential problem if ABI32 */
253 #ifndef TARGET_X86_64
254 if (env
->eflags
& VM_MASK
) {
255 handle_vm86_fault(env
);
259 info
.si_signo
= SIGSEGV
;
261 info
.si_code
= TARGET_SI_KERNEL
;
262 info
._sifields
._sigfault
._addr
= 0;
263 queue_signal(env
, info
.si_signo
, &info
);
267 info
.si_signo
= SIGSEGV
;
269 if (!(env
->error_code
& 1))
270 info
.si_code
= TARGET_SEGV_MAPERR
;
272 info
.si_code
= TARGET_SEGV_ACCERR
;
273 info
._sifields
._sigfault
._addr
= env
->cr
[2];
274 queue_signal(env
, info
.si_signo
, &info
);
277 #ifndef TARGET_X86_64
278 if (env
->eflags
& VM_MASK
) {
279 handle_vm86_trap(env
, trapnr
);
283 /* division by zero */
284 info
.si_signo
= SIGFPE
;
286 info
.si_code
= TARGET_FPE_INTDIV
;
287 info
._sifields
._sigfault
._addr
= env
->eip
;
288 queue_signal(env
, info
.si_signo
, &info
);
293 #ifndef TARGET_X86_64
294 if (env
->eflags
& VM_MASK
) {
295 handle_vm86_trap(env
, trapnr
);
299 info
.si_signo
= SIGTRAP
;
301 if (trapnr
== EXCP01_DB
) {
302 info
.si_code
= TARGET_TRAP_BRKPT
;
303 info
._sifields
._sigfault
._addr
= env
->eip
;
305 info
.si_code
= TARGET_SI_KERNEL
;
306 info
._sifields
._sigfault
._addr
= 0;
308 queue_signal(env
, info
.si_signo
, &info
);
313 #ifndef TARGET_X86_64
314 if (env
->eflags
& VM_MASK
) {
315 handle_vm86_trap(env
, trapnr
);
319 info
.si_signo
= SIGSEGV
;
321 info
.si_code
= TARGET_SI_KERNEL
;
322 info
._sifields
._sigfault
._addr
= 0;
323 queue_signal(env
, info
.si_signo
, &info
);
327 info
.si_signo
= SIGILL
;
329 info
.si_code
= TARGET_ILL_ILLOPN
;
330 info
._sifields
._sigfault
._addr
= env
->eip
;
331 queue_signal(env
, info
.si_signo
, &info
);
335 /* just indicate that signals should be handled asap */
342 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
347 info
.si_code
= TARGET_TRAP_BRKPT
;
348 queue_signal(env
, info
.si_signo
, &info
);
354 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
355 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
359 process_pending_signals(env
);
365 #define SPARC64_STACK_BIAS 2047
368 /* WARNING: dealing with register windows _is_ complicated. More info
369 can be found at http://www.sics.se/~psm/sparcstack.html */
370 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
372 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
373 /* wrap handling : if cwp is on the last window, then we use the
374 registers 'after' the end */
375 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
376 index
+= 16 * env
->nwindows
;
380 /* save the register window 'cwp1' */
381 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
386 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
387 #ifdef TARGET_SPARC64
389 sp_ptr
+= SPARC64_STACK_BIAS
;
391 #if defined(DEBUG_WIN)
392 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
395 for(i
= 0; i
< 16; i
++) {
396 /* FIXME - what to do if put_user() fails? */
397 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
398 sp_ptr
+= sizeof(abi_ulong
);
402 static void save_window(CPUSPARCState
*env
)
404 #ifndef TARGET_SPARC64
405 unsigned int new_wim
;
406 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
407 ((1LL << env
->nwindows
) - 1);
408 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
411 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
417 static void restore_window(CPUSPARCState
*env
)
419 #ifndef TARGET_SPARC64
420 unsigned int new_wim
;
422 unsigned int i
, cwp1
;
425 #ifndef TARGET_SPARC64
426 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
427 ((1LL << env
->nwindows
) - 1);
430 /* restore the invalid window */
431 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
432 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
433 #ifdef TARGET_SPARC64
435 sp_ptr
+= SPARC64_STACK_BIAS
;
437 #if defined(DEBUG_WIN)
438 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
441 for(i
= 0; i
< 16; i
++) {
442 /* FIXME - what to do if get_user() fails? */
443 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
444 sp_ptr
+= sizeof(abi_ulong
);
446 #ifdef TARGET_SPARC64
448 if (env
->cleanwin
< env
->nwindows
- 1)
456 static void flush_windows(CPUSPARCState
*env
)
462 /* if restore would invoke restore_window(), then we can stop */
463 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
464 #ifndef TARGET_SPARC64
465 if (env
->wim
& (1 << cwp1
))
468 if (env
->canrestore
== 0)
473 save_window_offset(env
, cwp1
);
476 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
477 #ifndef TARGET_SPARC64
478 /* set wim so that restore will reload the registers */
479 env
->wim
= 1 << cwp1
;
481 #if defined(DEBUG_WIN)
482 printf("flush_windows: nb=%d\n", offset
- 1);
486 void cpu_loop(CPUSPARCState
*env
)
488 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
489 int trapnr
, ret
, syscall_nr
;
490 //target_siginfo_t info;
494 trapnr
= cpu_exec(cs
);
496 process_queued_cpu_work(cs
);
499 #ifndef TARGET_SPARC64
502 /* FreeBSD uses 0x141 for syscalls too */
504 if (bsd_type
!= target_freebsd
)
508 syscall_nr
= env
->gregs
[1];
509 if (bsd_type
== target_freebsd
)
510 ret
= do_freebsd_syscall(env
, syscall_nr
,
511 env
->regwptr
[0], env
->regwptr
[1],
512 env
->regwptr
[2], env
->regwptr
[3],
513 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
514 else if (bsd_type
== target_netbsd
)
515 ret
= do_netbsd_syscall(env
, syscall_nr
,
516 env
->regwptr
[0], env
->regwptr
[1],
517 env
->regwptr
[2], env
->regwptr
[3],
518 env
->regwptr
[4], env
->regwptr
[5]);
519 else { //if (bsd_type == target_openbsd)
520 #if defined(TARGET_SPARC64)
521 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
522 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
524 ret
= do_openbsd_syscall(env
, syscall_nr
,
525 env
->regwptr
[0], env
->regwptr
[1],
526 env
->regwptr
[2], env
->regwptr
[3],
527 env
->regwptr
[4], env
->regwptr
[5]);
529 if ((unsigned int)ret
>= (unsigned int)(-515)) {
531 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
532 env
->xcc
|= PSR_CARRY
;
534 env
->psr
|= PSR_CARRY
;
537 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
538 env
->xcc
&= ~PSR_CARRY
;
540 env
->psr
&= ~PSR_CARRY
;
543 env
->regwptr
[0] = ret
;
544 /* next instruction */
545 #if defined(TARGET_SPARC64)
546 if (bsd_type
== target_openbsd
&&
547 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
548 env
->pc
= env
->gregs
[2];
549 env
->npc
= env
->pc
+ 4;
550 } else if (bsd_type
== target_openbsd
&&
551 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
552 env
->pc
= env
->gregs
[7];
553 env
->npc
= env
->pc
+ 4;
556 env
->npc
= env
->npc
+ 4;
560 env
->npc
= env
->npc
+ 4;
563 case 0x83: /* flush windows */
568 /* next instruction */
570 env
->npc
= env
->npc
+ 4;
572 #ifndef TARGET_SPARC64
573 case TT_WIN_OVF
: /* window overflow */
576 case TT_WIN_UNF
: /* window underflow */
583 info
.si_signo
= SIGSEGV
;
585 /* XXX: check env->error_code */
586 info
.si_code
= TARGET_SEGV_MAPERR
;
587 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
588 queue_signal(env
, info
.si_signo
, &info
);
593 case TT_SPILL
: /* window overflow */
596 case TT_FILL
: /* window underflow */
603 info
.si_signo
= SIGSEGV
;
605 /* XXX: check env->error_code */
606 info
.si_code
= TARGET_SEGV_MAPERR
;
607 if (trapnr
== TT_DFAULT
)
608 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
610 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
611 //queue_signal(env, info.si_signo, &info);
617 /* just indicate that signals should be handled asap */
624 gdb_handlesig(cs
, TARGET_SIGTRAP
);
630 info
.si_code
= TARGET_TRAP_BRKPT
;
631 //queue_signal(env, info.si_signo, &info);
637 #ifdef TARGET_SPARC64
640 printf ("Unhandled trap: 0x%x\n", trapnr
);
641 cpu_dump_state(cs
, stderr
, fprintf
, 0);
644 process_pending_signals (env
);
650 static void usage(void)
652 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION QEMU_PKGVERSION
653 "\n" QEMU_COPYRIGHT
"\n"
654 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
655 "BSD CPU emulator (compiled for %s emulation)\n"
657 "Standard options:\n"
658 "-h print this help\n"
659 "-g port wait gdb connection to port\n"
660 "-L path set the elf interpreter prefix (default=%s)\n"
661 "-s size set the stack size in bytes (default=%ld)\n"
662 "-cpu model select CPU (-cpu help for list)\n"
663 "-drop-ld-preload drop LD_PRELOAD for target process\n"
664 "-E var=value sets/modifies targets environment variable(s)\n"
665 "-U var unsets targets environment variable(s)\n"
666 "-B address set guest_base address to address\n"
667 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
670 "-d item1[,...] enable logging of specified items\n"
671 " (use '-d help' for a list of log items)\n"
672 "-D logfile write logs to 'logfile' (default stderr)\n"
673 "-p pagesize set the host page size to 'pagesize'\n"
674 "-singlestep always run in singlestep mode\n"
675 "-strace log system calls\n"
676 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
677 " specify tracing options\n"
679 "Environment variables:\n"
680 "QEMU_STRACE Print system calls and arguments similar to the\n"
681 " 'strace' program. Enable by setting to any value.\n"
682 "You can use -E and -U options to set/unset environment variables\n"
683 "for target process. It is possible to provide several variables\n"
684 "by repeating the option. For example:\n"
685 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
686 "Note that if you provide several changes to single variable\n"
687 "last change will stay in effect.\n"
689 QEMU_HELP_BOTTOM
"\n"
697 THREAD CPUState
*thread_cpu
;
699 bool qemu_cpu_is_self(CPUState
*cpu
)
701 return thread_cpu
== cpu
;
704 void qemu_cpu_kick(CPUState
*cpu
)
709 /* Assumes contents are already zeroed. */
710 void init_task_state(TaskState
*ts
)
715 ts
->first_free
= ts
->sigqueue_table
;
716 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
717 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
719 ts
->sigqueue_table
[i
].next
= NULL
;
722 int main(int argc
, char **argv
)
724 const char *filename
;
725 const char *cpu_model
;
726 const char *log_file
= NULL
;
727 const char *log_mask
= NULL
;
728 struct target_pt_regs regs1
, *regs
= ®s1
;
729 struct image_info info1
, *info
= &info1
;
730 TaskState ts1
, *ts
= &ts1
;
735 int gdbstub_port
= 0;
736 char **target_environ
, **wrk
;
737 envlist_t
*envlist
= NULL
;
738 char *trace_file
= NULL
;
739 bsd_type
= target_openbsd
;
744 module_call_init(MODULE_INIT_TRACE
);
745 qemu_init_cpu_list();
746 module_call_init(MODULE_INIT_QOM
);
748 envlist
= envlist_create();
750 /* add current environment into the list */
751 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
752 (void) envlist_setenv(envlist
, *wrk
);
757 qemu_add_opts(&qemu_trace_opts
);
768 if (!strcmp(r
, "-")) {
770 } else if (!strcmp(r
, "d")) {
771 if (optind
>= argc
) {
774 log_mask
= argv
[optind
++];
775 } else if (!strcmp(r
, "D")) {
776 if (optind
>= argc
) {
779 log_file
= argv
[optind
++];
780 } else if (!strcmp(r
, "E")) {
782 if (envlist_setenv(envlist
, r
) != 0)
784 } else if (!strcmp(r
, "ignore-environment")) {
785 envlist_free(envlist
);
786 envlist
= envlist_create();
787 } else if (!strcmp(r
, "U")) {
789 if (envlist_unsetenv(envlist
, r
) != 0)
791 } else if (!strcmp(r
, "s")) {
793 x86_stack_size
= strtol(r
, (char **)&r
, 0);
794 if (x86_stack_size
<= 0)
797 x86_stack_size
*= 1024 * 1024;
798 else if (*r
== 'k' || *r
== 'K')
799 x86_stack_size
*= 1024;
800 } else if (!strcmp(r
, "L")) {
801 interp_prefix
= argv
[optind
++];
802 } else if (!strcmp(r
, "p")) {
803 qemu_host_page_size
= atoi(argv
[optind
++]);
804 if (qemu_host_page_size
== 0 ||
805 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
806 fprintf(stderr
, "page size must be a power of two\n");
809 } else if (!strcmp(r
, "g")) {
810 gdbstub_port
= atoi(argv
[optind
++]);
811 } else if (!strcmp(r
, "r")) {
812 qemu_uname_release
= argv
[optind
++];
813 } else if (!strcmp(r
, "cpu")) {
814 cpu_model
= argv
[optind
++];
815 if (is_help_option(cpu_model
)) {
816 /* XXX: implement xxx_cpu_list for targets that still miss it */
817 #if defined(cpu_list)
818 cpu_list(stdout
, &fprintf
);
822 } else if (!strcmp(r
, "B")) {
823 guest_base
= strtol(argv
[optind
++], NULL
, 0);
825 } else if (!strcmp(r
, "drop-ld-preload")) {
826 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
827 } else if (!strcmp(r
, "bsd")) {
828 if (!strcasecmp(argv
[optind
], "freebsd")) {
829 bsd_type
= target_freebsd
;
830 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
831 bsd_type
= target_netbsd
;
832 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
833 bsd_type
= target_openbsd
;
838 } else if (!strcmp(r
, "singlestep")) {
840 } else if (!strcmp(r
, "strace")) {
842 } else if (!strcmp(r
, "trace")) {
844 trace_file
= trace_opt_parse(optarg
);
851 qemu_log_needs_buffers();
852 qemu_set_log_filename(log_file
, &error_fatal
);
856 mask
= qemu_str_to_log_mask(log_mask
);
858 qemu_print_log_usage(stdout
);
864 if (optind
>= argc
) {
867 filename
= argv
[optind
];
869 if (!trace_init_backends()) {
872 trace_init_file(trace_file
);
875 memset(regs
, 0, sizeof(struct target_pt_regs
));
877 /* Zero out image_info */
878 memset(info
, 0, sizeof(struct image_info
));
880 /* Scan interp_prefix dir for replacement files. */
881 init_paths(interp_prefix
);
883 if (cpu_model
== NULL
) {
884 #if defined(TARGET_I386)
886 cpu_model
= "qemu64";
888 cpu_model
= "qemu32";
890 #elif defined(TARGET_SPARC)
891 #ifdef TARGET_SPARC64
892 cpu_model
= "TI UltraSparc II";
894 cpu_model
= "Fujitsu MB86904";
901 /* NOTE: we need to init the CPU at this stage to get
902 qemu_host_page_size */
903 cpu
= cpu_init(cpu_model
);
905 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
910 if (getenv("QEMU_STRACE")) {
914 target_environ
= envlist_to_environ(envlist
, NULL
);
915 envlist_free(envlist
);
918 * Now that page sizes are configured in cpu_init() we can do
919 * proper page alignment for guest_base.
921 guest_base
= HOST_PAGE_ALIGN(guest_base
);
924 * Read in mmap_min_addr kernel parameter. This value is used
925 * When loading the ELF image to determine whether guest_base
928 * When user has explicitly set the quest base, we skip this
931 if (!have_guest_base
) {
934 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
936 if (fscanf(fp
, "%lu", &tmp
) == 1) {
938 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
944 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
945 printf("Error loading %s\n", filename
);
949 for (wrk
= target_environ
; *wrk
; wrk
++) {
953 g_free(target_environ
);
955 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
956 qemu_log("guest_base 0x%lx\n", guest_base
);
959 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
960 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
961 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
963 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
965 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
966 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
968 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
969 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
972 target_set_brk(info
->brk
);
976 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
977 generating the prologue until now so that the prologue can take
978 the real value of GUEST_BASE into account. */
979 tcg_prologue_init(tcg_ctx
);
982 /* build Task State */
983 memset(ts
, 0, sizeof(TaskState
));
988 #if defined(TARGET_I386)
989 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
990 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
991 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
992 env
->cr
[4] |= CR4_OSFXSR_MASK
;
993 env
->hflags
|= HF_OSFXSR_MASK
;
996 /* enable 64 bit mode if possible */
997 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
998 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1001 env
->cr
[4] |= CR4_PAE_MASK
;
1002 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1003 env
->hflags
|= HF_LMA_MASK
;
1006 /* flags setup : we activate the IRQs by default as in user mode */
1007 env
->eflags
|= IF_MASK
;
1009 /* linux register setup */
1010 #ifndef TARGET_ABI32
1011 env
->regs
[R_EAX
] = regs
->rax
;
1012 env
->regs
[R_EBX
] = regs
->rbx
;
1013 env
->regs
[R_ECX
] = regs
->rcx
;
1014 env
->regs
[R_EDX
] = regs
->rdx
;
1015 env
->regs
[R_ESI
] = regs
->rsi
;
1016 env
->regs
[R_EDI
] = regs
->rdi
;
1017 env
->regs
[R_EBP
] = regs
->rbp
;
1018 env
->regs
[R_ESP
] = regs
->rsp
;
1019 env
->eip
= regs
->rip
;
1021 env
->regs
[R_EAX
] = regs
->eax
;
1022 env
->regs
[R_EBX
] = regs
->ebx
;
1023 env
->regs
[R_ECX
] = regs
->ecx
;
1024 env
->regs
[R_EDX
] = regs
->edx
;
1025 env
->regs
[R_ESI
] = regs
->esi
;
1026 env
->regs
[R_EDI
] = regs
->edi
;
1027 env
->regs
[R_EBP
] = regs
->ebp
;
1028 env
->regs
[R_ESP
] = regs
->esp
;
1029 env
->eip
= regs
->eip
;
1032 /* linux interrupt setup */
1033 #ifndef TARGET_ABI32
1034 env
->idt
.limit
= 511;
1036 env
->idt
.limit
= 255;
1038 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1039 PROT_READ
|PROT_WRITE
,
1040 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1041 idt_table
= g2h(env
->idt
.base
);
1064 /* linux segment setup */
1066 uint64_t *gdt_table
;
1067 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1068 PROT_READ
|PROT_WRITE
,
1069 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1070 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1071 gdt_table
= g2h(env
->gdt
.base
);
1073 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1074 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1075 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1077 /* 64 bit code segment */
1078 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1079 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1081 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1083 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1084 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1085 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1088 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1089 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1091 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1092 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1093 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1094 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1095 /* This hack makes Wine work... */
1096 env
->segs
[R_FS
].selector
= 0;
1098 cpu_x86_load_seg(env
, R_DS
, 0);
1099 cpu_x86_load_seg(env
, R_ES
, 0);
1100 cpu_x86_load_seg(env
, R_FS
, 0);
1101 cpu_x86_load_seg(env
, R_GS
, 0);
1103 #elif defined(TARGET_SPARC)
1107 env
->npc
= regs
->npc
;
1109 for(i
= 0; i
< 8; i
++)
1110 env
->gregs
[i
] = regs
->u_regs
[i
];
1111 for(i
= 0; i
< 8; i
++)
1112 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1115 #error unsupported target CPU
1119 gdbserver_start (gdbstub_port
);
1120 gdb_handlesig(cpu
, 0);