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_FULL_VERSION
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 *cpu_type
;
727 const char *log_file
= NULL
;
728 const char *log_mask
= NULL
;
729 struct target_pt_regs regs1
, *regs
= ®s1
;
730 struct image_info info1
, *info
= &info1
;
731 TaskState ts1
, *ts
= &ts1
;
736 int gdbstub_port
= 0;
737 char **target_environ
, **wrk
;
738 envlist_t
*envlist
= NULL
;
739 char *trace_file
= NULL
;
740 bsd_type
= target_openbsd
;
745 module_call_init(MODULE_INIT_TRACE
);
746 qemu_init_cpu_list();
747 module_call_init(MODULE_INIT_QOM
);
749 envlist
= envlist_create();
751 /* add current environment into the list */
752 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
753 (void) envlist_setenv(envlist
, *wrk
);
758 qemu_add_opts(&qemu_trace_opts
);
769 if (!strcmp(r
, "-")) {
771 } else if (!strcmp(r
, "d")) {
772 if (optind
>= argc
) {
775 log_mask
= argv
[optind
++];
776 } else if (!strcmp(r
, "D")) {
777 if (optind
>= argc
) {
780 log_file
= argv
[optind
++];
781 } else if (!strcmp(r
, "E")) {
783 if (envlist_setenv(envlist
, r
) != 0)
785 } else if (!strcmp(r
, "ignore-environment")) {
786 envlist_free(envlist
);
787 envlist
= envlist_create();
788 } else if (!strcmp(r
, "U")) {
790 if (envlist_unsetenv(envlist
, r
) != 0)
792 } else if (!strcmp(r
, "s")) {
794 x86_stack_size
= strtol(r
, (char **)&r
, 0);
795 if (x86_stack_size
<= 0)
798 x86_stack_size
*= 1024 * 1024;
799 else if (*r
== 'k' || *r
== 'K')
800 x86_stack_size
*= 1024;
801 } else if (!strcmp(r
, "L")) {
802 interp_prefix
= argv
[optind
++];
803 } else if (!strcmp(r
, "p")) {
804 qemu_host_page_size
= atoi(argv
[optind
++]);
805 if (qemu_host_page_size
== 0 ||
806 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
807 fprintf(stderr
, "page size must be a power of two\n");
810 } else if (!strcmp(r
, "g")) {
811 gdbstub_port
= atoi(argv
[optind
++]);
812 } else if (!strcmp(r
, "r")) {
813 qemu_uname_release
= argv
[optind
++];
814 } else if (!strcmp(r
, "cpu")) {
815 cpu_model
= argv
[optind
++];
816 if (is_help_option(cpu_model
)) {
817 /* XXX: implement xxx_cpu_list for targets that still miss it */
818 #if defined(cpu_list)
819 cpu_list(stdout
, &fprintf
);
823 } else if (!strcmp(r
, "B")) {
824 guest_base
= strtol(argv
[optind
++], NULL
, 0);
826 } else if (!strcmp(r
, "drop-ld-preload")) {
827 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
828 } else if (!strcmp(r
, "bsd")) {
829 if (!strcasecmp(argv
[optind
], "freebsd")) {
830 bsd_type
= target_freebsd
;
831 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
832 bsd_type
= target_netbsd
;
833 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
834 bsd_type
= target_openbsd
;
839 } else if (!strcmp(r
, "singlestep")) {
841 } else if (!strcmp(r
, "strace")) {
843 } else if (!strcmp(r
, "trace")) {
845 trace_file
= trace_opt_parse(optarg
);
852 qemu_log_needs_buffers();
853 qemu_set_log_filename(log_file
, &error_fatal
);
857 mask
= qemu_str_to_log_mask(log_mask
);
859 qemu_print_log_usage(stdout
);
865 if (optind
>= argc
) {
868 filename
= argv
[optind
];
870 if (!trace_init_backends()) {
873 trace_init_file(trace_file
);
876 memset(regs
, 0, sizeof(struct target_pt_regs
));
878 /* Zero out image_info */
879 memset(info
, 0, sizeof(struct image_info
));
881 /* Scan interp_prefix dir for replacement files. */
882 init_paths(interp_prefix
);
884 if (cpu_model
== NULL
) {
885 #if defined(TARGET_I386)
887 cpu_model
= "qemu64";
889 cpu_model
= "qemu32";
891 #elif defined(TARGET_SPARC)
892 #ifdef TARGET_SPARC64
893 cpu_model
= "TI UltraSparc II";
895 cpu_model
= "Fujitsu MB86904";
902 /* NOTE: we need to init the CPU at this stage to get
903 qemu_host_page_size */
904 cpu_type
= parse_cpu_model(cpu_model
);
905 cpu
= cpu_create(cpu_type
);
907 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
912 if (getenv("QEMU_STRACE")) {
916 target_environ
= envlist_to_environ(envlist
, NULL
);
917 envlist_free(envlist
);
920 * Now that page sizes are configured in cpu_init() we can do
921 * proper page alignment for guest_base.
923 guest_base
= HOST_PAGE_ALIGN(guest_base
);
926 * Read in mmap_min_addr kernel parameter. This value is used
927 * When loading the ELF image to determine whether guest_base
930 * When user has explicitly set the quest base, we skip this
933 if (!have_guest_base
) {
936 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
938 if (fscanf(fp
, "%lu", &tmp
) == 1) {
940 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
946 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
947 printf("Error loading %s\n", filename
);
951 for (wrk
= target_environ
; *wrk
; wrk
++) {
955 g_free(target_environ
);
957 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
958 qemu_log("guest_base 0x%lx\n", guest_base
);
961 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
962 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
963 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
965 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
967 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
968 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
970 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
971 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
974 target_set_brk(info
->brk
);
978 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
979 generating the prologue until now so that the prologue can take
980 the real value of GUEST_BASE into account. */
981 tcg_prologue_init(tcg_ctx
);
984 /* build Task State */
985 memset(ts
, 0, sizeof(TaskState
));
990 #if defined(TARGET_I386)
991 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
992 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
993 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
994 env
->cr
[4] |= CR4_OSFXSR_MASK
;
995 env
->hflags
|= HF_OSFXSR_MASK
;
998 /* enable 64 bit mode if possible */
999 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1000 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1003 env
->cr
[4] |= CR4_PAE_MASK
;
1004 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1005 env
->hflags
|= HF_LMA_MASK
;
1008 /* flags setup : we activate the IRQs by default as in user mode */
1009 env
->eflags
|= IF_MASK
;
1011 /* linux register setup */
1012 #ifndef TARGET_ABI32
1013 env
->regs
[R_EAX
] = regs
->rax
;
1014 env
->regs
[R_EBX
] = regs
->rbx
;
1015 env
->regs
[R_ECX
] = regs
->rcx
;
1016 env
->regs
[R_EDX
] = regs
->rdx
;
1017 env
->regs
[R_ESI
] = regs
->rsi
;
1018 env
->regs
[R_EDI
] = regs
->rdi
;
1019 env
->regs
[R_EBP
] = regs
->rbp
;
1020 env
->regs
[R_ESP
] = regs
->rsp
;
1021 env
->eip
= regs
->rip
;
1023 env
->regs
[R_EAX
] = regs
->eax
;
1024 env
->regs
[R_EBX
] = regs
->ebx
;
1025 env
->regs
[R_ECX
] = regs
->ecx
;
1026 env
->regs
[R_EDX
] = regs
->edx
;
1027 env
->regs
[R_ESI
] = regs
->esi
;
1028 env
->regs
[R_EDI
] = regs
->edi
;
1029 env
->regs
[R_EBP
] = regs
->ebp
;
1030 env
->regs
[R_ESP
] = regs
->esp
;
1031 env
->eip
= regs
->eip
;
1034 /* linux interrupt setup */
1035 #ifndef TARGET_ABI32
1036 env
->idt
.limit
= 511;
1038 env
->idt
.limit
= 255;
1040 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1041 PROT_READ
|PROT_WRITE
,
1042 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1043 idt_table
= g2h(env
->idt
.base
);
1066 /* linux segment setup */
1068 uint64_t *gdt_table
;
1069 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1070 PROT_READ
|PROT_WRITE
,
1071 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1072 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1073 gdt_table
= g2h(env
->gdt
.base
);
1075 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1076 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1077 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1079 /* 64 bit code segment */
1080 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1081 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1083 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1085 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1086 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1087 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1090 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1091 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1093 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1094 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1095 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1096 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1097 /* This hack makes Wine work... */
1098 env
->segs
[R_FS
].selector
= 0;
1100 cpu_x86_load_seg(env
, R_DS
, 0);
1101 cpu_x86_load_seg(env
, R_ES
, 0);
1102 cpu_x86_load_seg(env
, R_FS
, 0);
1103 cpu_x86_load_seg(env
, R_GS
, 0);
1105 #elif defined(TARGET_SPARC)
1109 env
->npc
= regs
->npc
;
1111 for(i
= 0; i
< 8; i
++)
1112 env
->gregs
[i
] = regs
->u_regs
[i
];
1113 for(i
= 0; i
< 8; i
++)
1114 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1117 #error unsupported target CPU
1121 gdbserver_start (gdbstub_port
);
1122 gdb_handlesig(cpu
, 0);