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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qemu/units.h"
23 #include "sysemu/tcg.h"
24 #include "qemu-version.h"
25 #include <machine/trap.h>
27 #include "qapi/error.h"
29 #include "qemu/config-file.h"
30 #include "qemu/error-report.h"
31 #include "qemu/path.h"
32 #include "qemu/help_option.h"
33 #include "qemu/module.h"
35 #include "exec/exec-all.h"
37 #include "qemu/timer.h"
38 #include "qemu/envlist.h"
40 #include "trace/control.h"
43 unsigned long mmap_min_addr
;
44 unsigned long guest_base
;
46 unsigned long reserved_va
;
48 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
49 const char *qemu_uname_release
;
50 extern char **environ
;
51 enum BSDType bsd_type
;
53 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
54 we allocate a bigger stack. Need a better solution, for example
55 by remapping the process stack directly at the right place */
56 unsigned long x86_stack_size
= 512 * 1024;
58 void gemu_log(const char *fmt
, ...)
63 vfprintf(stderr
, fmt
, ap
);
67 #if defined(TARGET_I386)
68 int cpu_get_pic_interrupt(CPUX86State
*env
)
78 void fork_end(int child
)
81 gdbserver_fork(thread_cpu
);
86 /***********************************************************/
87 /* CPUX86 core interface */
89 uint64_t cpu_get_tsc(CPUX86State
*env
)
91 return cpu_get_host_ticks();
94 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
99 e1
= (addr
<< 16) | (limit
& 0xffff);
100 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
107 static uint64_t *idt_table
;
109 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
110 uint64_t addr
, unsigned int sel
)
113 e1
= (addr
& 0xffff) | (sel
<< 16);
114 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
118 p
[2] = tswap32(addr
>> 32);
121 /* only dpl matters as we do only user space emulation */
122 static void set_idt(int n
, unsigned int dpl
)
124 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
127 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
128 uint32_t addr
, unsigned int sel
)
131 e1
= (addr
& 0xffff) | (sel
<< 16);
132 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
138 /* only dpl matters as we do only user space emulation */
139 static void set_idt(int n
, unsigned int dpl
)
141 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
145 void cpu_loop(CPUX86State
*env
)
147 CPUState
*cs
= env_cpu(env
);
150 //target_siginfo_t info;
154 trapnr
= cpu_exec(cs
);
156 process_queued_cpu_work(cs
);
160 /* syscall from int $0x80 */
161 if (bsd_type
== target_freebsd
) {
162 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
164 int32_t syscall_nr
= env
->regs
[R_EAX
];
165 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
167 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
168 get_user_s32(syscall_nr
, params
);
169 params
+= sizeof(int32_t);
170 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
171 get_user_s32(syscall_nr
, params
);
172 params
+= sizeof(int64_t);
174 get_user_s32(arg1
, params
);
175 params
+= sizeof(int32_t);
176 get_user_s32(arg2
, params
);
177 params
+= sizeof(int32_t);
178 get_user_s32(arg3
, params
);
179 params
+= sizeof(int32_t);
180 get_user_s32(arg4
, params
);
181 params
+= sizeof(int32_t);
182 get_user_s32(arg5
, params
);
183 params
+= sizeof(int32_t);
184 get_user_s32(arg6
, params
);
185 params
+= sizeof(int32_t);
186 get_user_s32(arg7
, params
);
187 params
+= sizeof(int32_t);
188 get_user_s32(arg8
, params
);
189 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
199 } else { //if (bsd_type == target_openbsd)
200 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
209 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
210 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
213 env
->eflags
&= ~CC_C
;
218 /* syscall from syscall instruction */
219 if (bsd_type
== target_freebsd
)
220 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
228 else { //if (bsd_type == target_openbsd)
229 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
238 env
->eip
= env
->exception_next_eip
;
239 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
240 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
243 env
->eflags
&= ~CC_C
;
250 info
.si_signo
= SIGBUS
;
252 info
.si_code
= TARGET_SI_KERNEL
;
253 info
._sifields
._sigfault
._addr
= 0;
254 queue_signal(env
, info
.si_signo
, &info
);
257 /* XXX: potential problem if ABI32 */
258 #ifndef TARGET_X86_64
259 if (env
->eflags
& VM_MASK
) {
260 handle_vm86_fault(env
);
264 info
.si_signo
= SIGSEGV
;
266 info
.si_code
= TARGET_SI_KERNEL
;
267 info
._sifields
._sigfault
._addr
= 0;
268 queue_signal(env
, info
.si_signo
, &info
);
272 info
.si_signo
= SIGSEGV
;
274 if (!(env
->error_code
& 1))
275 info
.si_code
= TARGET_SEGV_MAPERR
;
277 info
.si_code
= TARGET_SEGV_ACCERR
;
278 info
._sifields
._sigfault
._addr
= env
->cr
[2];
279 queue_signal(env
, info
.si_signo
, &info
);
282 #ifndef TARGET_X86_64
283 if (env
->eflags
& VM_MASK
) {
284 handle_vm86_trap(env
, trapnr
);
288 /* division by zero */
289 info
.si_signo
= SIGFPE
;
291 info
.si_code
= TARGET_FPE_INTDIV
;
292 info
._sifields
._sigfault
._addr
= env
->eip
;
293 queue_signal(env
, info
.si_signo
, &info
);
298 #ifndef TARGET_X86_64
299 if (env
->eflags
& VM_MASK
) {
300 handle_vm86_trap(env
, trapnr
);
304 info
.si_signo
= SIGTRAP
;
306 if (trapnr
== EXCP01_DB
) {
307 info
.si_code
= TARGET_TRAP_BRKPT
;
308 info
._sifields
._sigfault
._addr
= env
->eip
;
310 info
.si_code
= TARGET_SI_KERNEL
;
311 info
._sifields
._sigfault
._addr
= 0;
313 queue_signal(env
, info
.si_signo
, &info
);
318 #ifndef TARGET_X86_64
319 if (env
->eflags
& VM_MASK
) {
320 handle_vm86_trap(env
, trapnr
);
324 info
.si_signo
= SIGSEGV
;
326 info
.si_code
= TARGET_SI_KERNEL
;
327 info
._sifields
._sigfault
._addr
= 0;
328 queue_signal(env
, info
.si_signo
, &info
);
332 info
.si_signo
= SIGILL
;
334 info
.si_code
= TARGET_ILL_ILLOPN
;
335 info
._sifields
._sigfault
._addr
= env
->eip
;
336 queue_signal(env
, info
.si_signo
, &info
);
340 /* just indicate that signals should be handled asap */
347 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
352 info
.si_code
= TARGET_TRAP_BRKPT
;
353 queue_signal(env
, info
.si_signo
, &info
);
359 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
360 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
364 process_pending_signals(env
);
370 #define SPARC64_STACK_BIAS 2047
373 /* WARNING: dealing with register windows _is_ complicated. More info
374 can be found at http://www.sics.se/~psm/sparcstack.html */
375 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
377 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
378 /* wrap handling : if cwp is on the last window, then we use the
379 registers 'after' the end */
380 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
381 index
+= 16 * env
->nwindows
;
385 /* save the register window 'cwp1' */
386 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
391 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
392 #ifdef TARGET_SPARC64
394 sp_ptr
+= SPARC64_STACK_BIAS
;
396 #if defined(DEBUG_WIN)
397 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
400 for(i
= 0; i
< 16; i
++) {
401 /* FIXME - what to do if put_user() fails? */
402 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
403 sp_ptr
+= sizeof(abi_ulong
);
407 static void save_window(CPUSPARCState
*env
)
409 #ifndef TARGET_SPARC64
410 unsigned int new_wim
;
411 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
412 ((1LL << env
->nwindows
) - 1);
413 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
417 * cansave is zero if the spill trap handler is triggered by `save` and
418 * nonzero if triggered by a `flushw`
420 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- env
->cansave
- 2));
426 static void restore_window(CPUSPARCState
*env
)
428 #ifndef TARGET_SPARC64
429 unsigned int new_wim
;
431 unsigned int i
, cwp1
;
434 #ifndef TARGET_SPARC64
435 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
436 ((1LL << env
->nwindows
) - 1);
439 /* restore the invalid window */
440 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
441 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
442 #ifdef TARGET_SPARC64
444 sp_ptr
+= SPARC64_STACK_BIAS
;
446 #if defined(DEBUG_WIN)
447 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
450 for(i
= 0; i
< 16; i
++) {
451 /* FIXME - what to do if get_user() fails? */
452 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
453 sp_ptr
+= sizeof(abi_ulong
);
455 #ifdef TARGET_SPARC64
457 if (env
->cleanwin
< env
->nwindows
- 1)
465 static void flush_windows(CPUSPARCState
*env
)
471 /* if restore would invoke restore_window(), then we can stop */
472 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
473 #ifndef TARGET_SPARC64
474 if (env
->wim
& (1 << cwp1
))
477 if (env
->canrestore
== 0)
482 save_window_offset(env
, cwp1
);
485 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
486 #ifndef TARGET_SPARC64
487 /* set wim so that restore will reload the registers */
488 env
->wim
= 1 << cwp1
;
490 #if defined(DEBUG_WIN)
491 printf("flush_windows: nb=%d\n", offset
- 1);
495 void cpu_loop(CPUSPARCState
*env
)
497 CPUState
*cs
= env_cpu(env
);
498 int trapnr
, ret
, syscall_nr
;
499 //target_siginfo_t info;
503 trapnr
= cpu_exec(cs
);
505 process_queued_cpu_work(cs
);
508 #ifndef TARGET_SPARC64
511 /* FreeBSD uses 0x141 for syscalls too */
513 if (bsd_type
!= target_freebsd
)
517 syscall_nr
= env
->gregs
[1];
518 if (bsd_type
== target_freebsd
)
519 ret
= do_freebsd_syscall(env
, syscall_nr
,
520 env
->regwptr
[0], env
->regwptr
[1],
521 env
->regwptr
[2], env
->regwptr
[3],
522 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
523 else if (bsd_type
== target_netbsd
)
524 ret
= do_netbsd_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]);
528 else { //if (bsd_type == target_openbsd)
529 #if defined(TARGET_SPARC64)
530 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
531 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
533 ret
= do_openbsd_syscall(env
, syscall_nr
,
534 env
->regwptr
[0], env
->regwptr
[1],
535 env
->regwptr
[2], env
->regwptr
[3],
536 env
->regwptr
[4], env
->regwptr
[5]);
538 if ((unsigned int)ret
>= (unsigned int)(-515)) {
540 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
541 env
->xcc
|= PSR_CARRY
;
543 env
->psr
|= PSR_CARRY
;
546 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
547 env
->xcc
&= ~PSR_CARRY
;
549 env
->psr
&= ~PSR_CARRY
;
552 env
->regwptr
[0] = ret
;
553 /* next instruction */
554 #if defined(TARGET_SPARC64)
555 if (bsd_type
== target_openbsd
&&
556 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
557 env
->pc
= env
->gregs
[2];
558 env
->npc
= env
->pc
+ 4;
559 } else if (bsd_type
== target_openbsd
&&
560 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
561 env
->pc
= env
->gregs
[7];
562 env
->npc
= env
->pc
+ 4;
565 env
->npc
= env
->npc
+ 4;
569 env
->npc
= env
->npc
+ 4;
572 case 0x83: /* flush windows */
577 /* next instruction */
579 env
->npc
= env
->npc
+ 4;
581 #ifndef TARGET_SPARC64
582 case TT_WIN_OVF
: /* window overflow */
585 case TT_WIN_UNF
: /* window underflow */
592 info
.si_signo
= SIGSEGV
;
594 /* XXX: check env->error_code */
595 info
.si_code
= TARGET_SEGV_MAPERR
;
596 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
597 queue_signal(env
, info
.si_signo
, &info
);
602 case TT_SPILL
: /* window overflow */
605 case TT_FILL
: /* window underflow */
612 info
.si_signo
= SIGSEGV
;
614 /* XXX: check env->error_code */
615 info
.si_code
= TARGET_SEGV_MAPERR
;
616 if (trapnr
== TT_DFAULT
)
617 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
619 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
620 //queue_signal(env, info.si_signo, &info);
626 /* just indicate that signals should be handled asap */
633 gdb_handlesig(cs
, TARGET_SIGTRAP
);
639 info
.si_code
= TARGET_TRAP_BRKPT
;
640 //queue_signal(env, info.si_signo, &info);
646 #ifdef TARGET_SPARC64
649 printf ("Unhandled trap: 0x%x\n", trapnr
);
650 cpu_dump_state(cs
, stderr
, 0);
653 process_pending_signals (env
);
659 static void usage(void)
661 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
662 "\n" QEMU_COPYRIGHT
"\n"
663 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
664 "BSD CPU emulator (compiled for %s emulation)\n"
666 "Standard options:\n"
667 "-h print this help\n"
668 "-g port wait gdb connection to port\n"
669 "-L path set the elf interpreter prefix (default=%s)\n"
670 "-s size set the stack size in bytes (default=%ld)\n"
671 "-cpu model select CPU (-cpu help for list)\n"
672 "-drop-ld-preload drop LD_PRELOAD for target process\n"
673 "-E var=value sets/modifies targets environment variable(s)\n"
674 "-U var unsets targets environment variable(s)\n"
675 "-B address set guest_base address to address\n"
676 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
679 "-d item1[,...] enable logging of specified items\n"
680 " (use '-d help' for a list of log items)\n"
681 "-D logfile write logs to 'logfile' (default stderr)\n"
682 "-p pagesize set the host page size to 'pagesize'\n"
683 "-singlestep always run in singlestep mode\n"
684 "-strace log system calls\n"
685 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
686 " specify tracing options\n"
688 "Environment variables:\n"
689 "QEMU_STRACE Print system calls and arguments similar to the\n"
690 " 'strace' program. Enable by setting to any value.\n"
691 "You can use -E and -U options to set/unset environment variables\n"
692 "for target process. It is possible to provide several variables\n"
693 "by repeating the option. For example:\n"
694 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
695 "Note that if you provide several changes to single variable\n"
696 "last change will stay in effect.\n"
698 QEMU_HELP_BOTTOM
"\n"
706 THREAD CPUState
*thread_cpu
;
708 bool qemu_cpu_is_self(CPUState
*cpu
)
710 return thread_cpu
== cpu
;
713 void qemu_cpu_kick(CPUState
*cpu
)
718 /* Assumes contents are already zeroed. */
719 void init_task_state(TaskState
*ts
)
724 ts
->first_free
= ts
->sigqueue_table
;
725 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
726 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
728 ts
->sigqueue_table
[i
].next
= NULL
;
731 int main(int argc
, char **argv
)
733 const char *filename
;
734 const char *cpu_model
;
735 const char *cpu_type
;
736 const char *log_file
= NULL
;
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
;
745 const char *gdbstub
= NULL
;
746 char **target_environ
, **wrk
;
747 envlist_t
*envlist
= NULL
;
748 char *trace_file
= NULL
;
749 bsd_type
= target_openbsd
;
755 module_call_init(MODULE_INIT_TRACE
);
756 qemu_init_cpu_list();
757 module_call_init(MODULE_INIT_QOM
);
759 envlist
= envlist_create();
761 /* add current environment into the list */
762 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
763 (void) envlist_setenv(envlist
, *wrk
);
768 qemu_add_opts(&qemu_trace_opts
);
779 if (!strcmp(r
, "-")) {
781 } else if (!strcmp(r
, "d")) {
782 if (optind
>= argc
) {
785 log_mask
= argv
[optind
++];
786 } else if (!strcmp(r
, "D")) {
787 if (optind
>= argc
) {
790 log_file
= argv
[optind
++];
791 } else if (!strcmp(r
, "E")) {
793 if (envlist_setenv(envlist
, r
) != 0)
795 } else if (!strcmp(r
, "ignore-environment")) {
796 envlist_free(envlist
);
797 envlist
= envlist_create();
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
*= MiB
;
809 else if (*r
== 'k' || *r
== 'K')
810 x86_stack_size
*= KiB
;
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
= g_strdup(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)
833 } else if (!strcmp(r
, "B")) {
834 guest_base
= strtol(argv
[optind
++], NULL
, 0);
835 have_guest_base
= true;
836 } else if (!strcmp(r
, "drop-ld-preload")) {
837 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
838 } else if (!strcmp(r
, "bsd")) {
839 if (!strcasecmp(argv
[optind
], "freebsd")) {
840 bsd_type
= target_freebsd
;
841 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
842 bsd_type
= target_netbsd
;
843 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
844 bsd_type
= target_openbsd
;
849 } else if (!strcmp(r
, "singlestep")) {
851 } else if (!strcmp(r
, "strace")) {
853 } else if (!strcmp(r
, "trace")) {
855 trace_file
= trace_opt_parse(optarg
);
862 qemu_log_needs_buffers();
863 qemu_set_log_filename(log_file
, &error_fatal
);
867 mask
= qemu_str_to_log_mask(log_mask
);
869 qemu_print_log_usage(stdout
);
875 if (optind
>= argc
) {
878 filename
= argv
[optind
];
880 if (!trace_init_backends()) {
883 trace_init_file(trace_file
);
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";
912 /* init tcg before creating CPUs and to get qemu_host_page_size */
915 cpu_type
= parse_cpu_option(cpu_model
);
916 cpu
= cpu_create(cpu_type
);
918 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
923 if (getenv("QEMU_STRACE")) {
927 target_environ
= envlist_to_environ(envlist
, NULL
);
928 envlist_free(envlist
);
931 * Now that page sizes are configured in tcg_exec_init() we can do
932 * proper page alignment for guest_base.
934 guest_base
= HOST_PAGE_ALIGN(guest_base
);
937 * Read in mmap_min_addr kernel parameter. This value is used
938 * When loading the ELF image to determine whether guest_base
941 * When user has explicitly set the quest base, we skip this
944 if (!have_guest_base
) {
947 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
949 if (fscanf(fp
, "%lu", &tmp
) == 1) {
951 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
957 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
958 printf("Error loading %s\n", filename
);
962 for (wrk
= target_environ
; *wrk
; wrk
++) {
966 g_free(target_environ
);
968 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
969 qemu_log("guest_base 0x%lx\n", guest_base
);
970 log_page_dump("binary load");
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 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
990 generating the prologue until now so that the prologue can take
991 the real value of GUEST_BASE into account. */
992 tcg_prologue_init(tcg_ctx
);
995 /* build Task State */
996 memset(ts
, 0, sizeof(TaskState
));
1001 #if defined(TARGET_I386)
1002 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
1003 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
1004 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
1005 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1006 env
->hflags
|= HF_OSFXSR_MASK
;
1008 #ifndef TARGET_ABI32
1009 /* enable 64 bit mode if possible */
1010 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1011 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1014 env
->cr
[4] |= CR4_PAE_MASK
;
1015 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1016 env
->hflags
|= HF_LMA_MASK
;
1019 /* flags setup : we activate the IRQs by default as in user mode */
1020 env
->eflags
|= IF_MASK
;
1022 /* linux register setup */
1023 #ifndef TARGET_ABI32
1024 env
->regs
[R_EAX
] = regs
->rax
;
1025 env
->regs
[R_EBX
] = regs
->rbx
;
1026 env
->regs
[R_ECX
] = regs
->rcx
;
1027 env
->regs
[R_EDX
] = regs
->rdx
;
1028 env
->regs
[R_ESI
] = regs
->rsi
;
1029 env
->regs
[R_EDI
] = regs
->rdi
;
1030 env
->regs
[R_EBP
] = regs
->rbp
;
1031 env
->regs
[R_ESP
] = regs
->rsp
;
1032 env
->eip
= regs
->rip
;
1034 env
->regs
[R_EAX
] = regs
->eax
;
1035 env
->regs
[R_EBX
] = regs
->ebx
;
1036 env
->regs
[R_ECX
] = regs
->ecx
;
1037 env
->regs
[R_EDX
] = regs
->edx
;
1038 env
->regs
[R_ESI
] = regs
->esi
;
1039 env
->regs
[R_EDI
] = regs
->edi
;
1040 env
->regs
[R_EBP
] = regs
->ebp
;
1041 env
->regs
[R_ESP
] = regs
->esp
;
1042 env
->eip
= regs
->eip
;
1045 /* linux interrupt setup */
1046 #ifndef TARGET_ABI32
1047 env
->idt
.limit
= 511;
1049 env
->idt
.limit
= 255;
1051 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1052 PROT_READ
|PROT_WRITE
,
1053 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1054 idt_table
= g2h(env
->idt
.base
);
1077 /* linux segment setup */
1079 uint64_t *gdt_table
;
1080 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1081 PROT_READ
|PROT_WRITE
,
1082 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1083 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1084 gdt_table
= g2h(env
->gdt
.base
);
1086 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1087 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1088 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1090 /* 64 bit code segment */
1091 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1092 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1094 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1096 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1097 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1098 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1101 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1102 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1104 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1105 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1106 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1107 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1108 /* This hack makes Wine work... */
1109 env
->segs
[R_FS
].selector
= 0;
1111 cpu_x86_load_seg(env
, R_DS
, 0);
1112 cpu_x86_load_seg(env
, R_ES
, 0);
1113 cpu_x86_load_seg(env
, R_FS
, 0);
1114 cpu_x86_load_seg(env
, R_GS
, 0);
1116 #elif defined(TARGET_SPARC)
1120 env
->npc
= regs
->npc
;
1122 for(i
= 0; i
< 8; i
++)
1123 env
->gregs
[i
] = regs
->u_regs
[i
];
1124 for(i
= 0; i
< 8; i
++)
1125 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1128 #error unsupported target CPU
1132 gdbserver_start(gdbstub
);
1133 gdb_handlesig(cpu
, 0);