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/units.h"
22 #include "sysemu/tcg.h"
23 #include "qemu-version.h"
24 #include <machine/trap.h>
26 #include "qapi/error.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qemu/path.h"
31 #include "qemu/help_option.h"
33 #include "exec/exec-all.h"
35 #include "qemu/timer.h"
36 #include "qemu/envlist.h"
38 #include "trace/control.h"
41 unsigned long mmap_min_addr
;
42 unsigned long guest_base
;
44 unsigned long reserved_va
;
46 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
47 const char *qemu_uname_release
;
48 extern char **environ
;
49 enum BSDType bsd_type
;
51 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
52 we allocate a bigger stack. Need a better solution, for example
53 by remapping the process stack directly at the right place */
54 unsigned long x86_stack_size
= 512 * 1024;
56 void gemu_log(const char *fmt
, ...)
61 vfprintf(stderr
, fmt
, ap
);
65 #if defined(TARGET_I386)
66 int cpu_get_pic_interrupt(CPUX86State
*env
)
76 void fork_end(int child
)
79 gdbserver_fork(thread_cpu
);
84 /***********************************************************/
85 /* CPUX86 core interface */
87 uint64_t cpu_get_tsc(CPUX86State
*env
)
89 return cpu_get_host_ticks();
92 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
97 e1
= (addr
<< 16) | (limit
& 0xffff);
98 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
105 static uint64_t *idt_table
;
107 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
108 uint64_t addr
, unsigned int sel
)
111 e1
= (addr
& 0xffff) | (sel
<< 16);
112 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
116 p
[2] = tswap32(addr
>> 32);
119 /* only dpl matters as we do only user space emulation */
120 static void set_idt(int n
, unsigned int dpl
)
122 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
125 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
126 uint32_t addr
, unsigned int sel
)
129 e1
= (addr
& 0xffff) | (sel
<< 16);
130 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
136 /* only dpl matters as we do only user space emulation */
137 static void set_idt(int n
, unsigned int dpl
)
139 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
143 void cpu_loop(CPUX86State
*env
)
145 CPUState
*cs
= env_cpu(env
);
148 //target_siginfo_t info;
152 trapnr
= cpu_exec(cs
);
154 process_queued_cpu_work(cs
);
158 /* syscall from int $0x80 */
159 if (bsd_type
== target_freebsd
) {
160 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
162 int32_t syscall_nr
= env
->regs
[R_EAX
];
163 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
165 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
166 get_user_s32(syscall_nr
, params
);
167 params
+= sizeof(int32_t);
168 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
169 get_user_s32(syscall_nr
, params
);
170 params
+= sizeof(int64_t);
172 get_user_s32(arg1
, params
);
173 params
+= sizeof(int32_t);
174 get_user_s32(arg2
, params
);
175 params
+= sizeof(int32_t);
176 get_user_s32(arg3
, params
);
177 params
+= sizeof(int32_t);
178 get_user_s32(arg4
, params
);
179 params
+= sizeof(int32_t);
180 get_user_s32(arg5
, params
);
181 params
+= sizeof(int32_t);
182 get_user_s32(arg6
, params
);
183 params
+= sizeof(int32_t);
184 get_user_s32(arg7
, params
);
185 params
+= sizeof(int32_t);
186 get_user_s32(arg8
, params
);
187 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
197 } else { //if (bsd_type == target_openbsd)
198 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
207 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
208 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
211 env
->eflags
&= ~CC_C
;
216 /* syscall from syscall instruction */
217 if (bsd_type
== target_freebsd
)
218 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
226 else { //if (bsd_type == target_openbsd)
227 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
236 env
->eip
= env
->exception_next_eip
;
237 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
238 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
241 env
->eflags
&= ~CC_C
;
248 info
.si_signo
= SIGBUS
;
250 info
.si_code
= TARGET_SI_KERNEL
;
251 info
._sifields
._sigfault
._addr
= 0;
252 queue_signal(env
, info
.si_signo
, &info
);
255 /* XXX: potential problem if ABI32 */
256 #ifndef TARGET_X86_64
257 if (env
->eflags
& VM_MASK
) {
258 handle_vm86_fault(env
);
262 info
.si_signo
= SIGSEGV
;
264 info
.si_code
= TARGET_SI_KERNEL
;
265 info
._sifields
._sigfault
._addr
= 0;
266 queue_signal(env
, info
.si_signo
, &info
);
270 info
.si_signo
= SIGSEGV
;
272 if (!(env
->error_code
& 1))
273 info
.si_code
= TARGET_SEGV_MAPERR
;
275 info
.si_code
= TARGET_SEGV_ACCERR
;
276 info
._sifields
._sigfault
._addr
= env
->cr
[2];
277 queue_signal(env
, info
.si_signo
, &info
);
280 #ifndef TARGET_X86_64
281 if (env
->eflags
& VM_MASK
) {
282 handle_vm86_trap(env
, trapnr
);
286 /* division by zero */
287 info
.si_signo
= SIGFPE
;
289 info
.si_code
= TARGET_FPE_INTDIV
;
290 info
._sifields
._sigfault
._addr
= env
->eip
;
291 queue_signal(env
, info
.si_signo
, &info
);
296 #ifndef TARGET_X86_64
297 if (env
->eflags
& VM_MASK
) {
298 handle_vm86_trap(env
, trapnr
);
302 info
.si_signo
= SIGTRAP
;
304 if (trapnr
== EXCP01_DB
) {
305 info
.si_code
= TARGET_TRAP_BRKPT
;
306 info
._sifields
._sigfault
._addr
= env
->eip
;
308 info
.si_code
= TARGET_SI_KERNEL
;
309 info
._sifields
._sigfault
._addr
= 0;
311 queue_signal(env
, info
.si_signo
, &info
);
316 #ifndef TARGET_X86_64
317 if (env
->eflags
& VM_MASK
) {
318 handle_vm86_trap(env
, trapnr
);
322 info
.si_signo
= SIGSEGV
;
324 info
.si_code
= TARGET_SI_KERNEL
;
325 info
._sifields
._sigfault
._addr
= 0;
326 queue_signal(env
, info
.si_signo
, &info
);
330 info
.si_signo
= SIGILL
;
332 info
.si_code
= TARGET_ILL_ILLOPN
;
333 info
._sifields
._sigfault
._addr
= env
->eip
;
334 queue_signal(env
, info
.si_signo
, &info
);
338 /* just indicate that signals should be handled asap */
345 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
350 info
.si_code
= TARGET_TRAP_BRKPT
;
351 queue_signal(env
, info
.si_signo
, &info
);
357 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
358 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
362 process_pending_signals(env
);
368 #define SPARC64_STACK_BIAS 2047
371 /* WARNING: dealing with register windows _is_ complicated. More info
372 can be found at http://www.sics.se/~psm/sparcstack.html */
373 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
375 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
376 /* wrap handling : if cwp is on the last window, then we use the
377 registers 'after' the end */
378 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
379 index
+= 16 * env
->nwindows
;
383 /* save the register window 'cwp1' */
384 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
389 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
390 #ifdef TARGET_SPARC64
392 sp_ptr
+= SPARC64_STACK_BIAS
;
394 #if defined(DEBUG_WIN)
395 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
398 for(i
= 0; i
< 16; i
++) {
399 /* FIXME - what to do if put_user() fails? */
400 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
401 sp_ptr
+= sizeof(abi_ulong
);
405 static void save_window(CPUSPARCState
*env
)
407 #ifndef TARGET_SPARC64
408 unsigned int new_wim
;
409 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
410 ((1LL << env
->nwindows
) - 1);
411 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
414 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
420 static void restore_window(CPUSPARCState
*env
)
422 #ifndef TARGET_SPARC64
423 unsigned int new_wim
;
425 unsigned int i
, cwp1
;
428 #ifndef TARGET_SPARC64
429 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
430 ((1LL << env
->nwindows
) - 1);
433 /* restore the invalid window */
434 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
435 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
436 #ifdef TARGET_SPARC64
438 sp_ptr
+= SPARC64_STACK_BIAS
;
440 #if defined(DEBUG_WIN)
441 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
444 for(i
= 0; i
< 16; i
++) {
445 /* FIXME - what to do if get_user() fails? */
446 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
447 sp_ptr
+= sizeof(abi_ulong
);
449 #ifdef TARGET_SPARC64
451 if (env
->cleanwin
< env
->nwindows
- 1)
459 static void flush_windows(CPUSPARCState
*env
)
465 /* if restore would invoke restore_window(), then we can stop */
466 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
467 #ifndef TARGET_SPARC64
468 if (env
->wim
& (1 << cwp1
))
471 if (env
->canrestore
== 0)
476 save_window_offset(env
, cwp1
);
479 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
480 #ifndef TARGET_SPARC64
481 /* set wim so that restore will reload the registers */
482 env
->wim
= 1 << cwp1
;
484 #if defined(DEBUG_WIN)
485 printf("flush_windows: nb=%d\n", offset
- 1);
489 void cpu_loop(CPUSPARCState
*env
)
491 CPUState
*cs
= env_cpu(env
);
492 int trapnr
, ret
, syscall_nr
;
493 //target_siginfo_t info;
497 trapnr
= cpu_exec(cs
);
499 process_queued_cpu_work(cs
);
502 #ifndef TARGET_SPARC64
505 /* FreeBSD uses 0x141 for syscalls too */
507 if (bsd_type
!= target_freebsd
)
511 syscall_nr
= env
->gregs
[1];
512 if (bsd_type
== target_freebsd
)
513 ret
= do_freebsd_syscall(env
, syscall_nr
,
514 env
->regwptr
[0], env
->regwptr
[1],
515 env
->regwptr
[2], env
->regwptr
[3],
516 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
517 else if (bsd_type
== target_netbsd
)
518 ret
= do_netbsd_syscall(env
, syscall_nr
,
519 env
->regwptr
[0], env
->regwptr
[1],
520 env
->regwptr
[2], env
->regwptr
[3],
521 env
->regwptr
[4], env
->regwptr
[5]);
522 else { //if (bsd_type == target_openbsd)
523 #if defined(TARGET_SPARC64)
524 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
525 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
527 ret
= do_openbsd_syscall(env
, syscall_nr
,
528 env
->regwptr
[0], env
->regwptr
[1],
529 env
->regwptr
[2], env
->regwptr
[3],
530 env
->regwptr
[4], env
->regwptr
[5]);
532 if ((unsigned int)ret
>= (unsigned int)(-515)) {
534 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
535 env
->xcc
|= PSR_CARRY
;
537 env
->psr
|= PSR_CARRY
;
540 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
541 env
->xcc
&= ~PSR_CARRY
;
543 env
->psr
&= ~PSR_CARRY
;
546 env
->regwptr
[0] = ret
;
547 /* next instruction */
548 #if defined(TARGET_SPARC64)
549 if (bsd_type
== target_openbsd
&&
550 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
551 env
->pc
= env
->gregs
[2];
552 env
->npc
= env
->pc
+ 4;
553 } else if (bsd_type
== target_openbsd
&&
554 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
555 env
->pc
= env
->gregs
[7];
556 env
->npc
= env
->pc
+ 4;
559 env
->npc
= env
->npc
+ 4;
563 env
->npc
= env
->npc
+ 4;
566 case 0x83: /* flush windows */
571 /* next instruction */
573 env
->npc
= env
->npc
+ 4;
575 #ifndef TARGET_SPARC64
576 case TT_WIN_OVF
: /* window overflow */
579 case TT_WIN_UNF
: /* window underflow */
586 info
.si_signo
= SIGSEGV
;
588 /* XXX: check env->error_code */
589 info
.si_code
= TARGET_SEGV_MAPERR
;
590 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
591 queue_signal(env
, info
.si_signo
, &info
);
596 case TT_SPILL
: /* window overflow */
599 case TT_FILL
: /* window underflow */
606 info
.si_signo
= SIGSEGV
;
608 /* XXX: check env->error_code */
609 info
.si_code
= TARGET_SEGV_MAPERR
;
610 if (trapnr
== TT_DFAULT
)
611 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
613 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
614 //queue_signal(env, info.si_signo, &info);
620 /* just indicate that signals should be handled asap */
627 gdb_handlesig(cs
, TARGET_SIGTRAP
);
633 info
.si_code
= TARGET_TRAP_BRKPT
;
634 //queue_signal(env, info.si_signo, &info);
640 #ifdef TARGET_SPARC64
643 printf ("Unhandled trap: 0x%x\n", trapnr
);
644 cpu_dump_state(cs
, stderr
, 0);
647 process_pending_signals (env
);
653 static void usage(void)
655 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
656 "\n" QEMU_COPYRIGHT
"\n"
657 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
658 "BSD CPU emulator (compiled for %s emulation)\n"
660 "Standard options:\n"
661 "-h print this help\n"
662 "-g port wait gdb connection to port\n"
663 "-L path set the elf interpreter prefix (default=%s)\n"
664 "-s size set the stack size in bytes (default=%ld)\n"
665 "-cpu model select CPU (-cpu help for list)\n"
666 "-drop-ld-preload drop LD_PRELOAD for target process\n"
667 "-E var=value sets/modifies targets environment variable(s)\n"
668 "-U var unsets targets environment variable(s)\n"
669 "-B address set guest_base address to address\n"
670 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
673 "-d item1[,...] enable logging of specified items\n"
674 " (use '-d help' for a list of log items)\n"
675 "-D logfile write logs to 'logfile' (default stderr)\n"
676 "-p pagesize set the host page size to 'pagesize'\n"
677 "-singlestep always run in singlestep mode\n"
678 "-strace log system calls\n"
679 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
680 " specify tracing options\n"
682 "Environment variables:\n"
683 "QEMU_STRACE Print system calls and arguments similar to the\n"
684 " 'strace' program. Enable by setting to any value.\n"
685 "You can use -E and -U options to set/unset environment variables\n"
686 "for target process. It is possible to provide several variables\n"
687 "by repeating the option. For example:\n"
688 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
689 "Note that if you provide several changes to single variable\n"
690 "last change will stay in effect.\n"
692 QEMU_HELP_BOTTOM
"\n"
700 THREAD CPUState
*thread_cpu
;
702 bool qemu_cpu_is_self(CPUState
*cpu
)
704 return thread_cpu
== cpu
;
707 void qemu_cpu_kick(CPUState
*cpu
)
712 /* Assumes contents are already zeroed. */
713 void init_task_state(TaskState
*ts
)
718 ts
->first_free
= ts
->sigqueue_table
;
719 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
720 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
722 ts
->sigqueue_table
[i
].next
= NULL
;
725 int main(int argc
, char **argv
)
727 const char *filename
;
728 const char *cpu_model
;
729 const char *cpu_type
;
730 const char *log_file
= NULL
;
731 const char *log_mask
= NULL
;
732 struct target_pt_regs regs1
, *regs
= ®s1
;
733 struct image_info info1
, *info
= &info1
;
734 TaskState ts1
, *ts
= &ts1
;
739 int gdbstub_port
= 0;
740 char **target_environ
, **wrk
;
741 envlist_t
*envlist
= NULL
;
742 char *trace_file
= NULL
;
743 bsd_type
= target_openbsd
;
749 module_call_init(MODULE_INIT_TRACE
);
750 qemu_init_cpu_list();
751 module_call_init(MODULE_INIT_QOM
);
753 envlist
= envlist_create();
755 /* add current environment into the list */
756 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
757 (void) envlist_setenv(envlist
, *wrk
);
762 qemu_add_opts(&qemu_trace_opts
);
773 if (!strcmp(r
, "-")) {
775 } else if (!strcmp(r
, "d")) {
776 if (optind
>= argc
) {
779 log_mask
= argv
[optind
++];
780 } else if (!strcmp(r
, "D")) {
781 if (optind
>= argc
) {
784 log_file
= argv
[optind
++];
785 } else if (!strcmp(r
, "E")) {
787 if (envlist_setenv(envlist
, r
) != 0)
789 } else if (!strcmp(r
, "ignore-environment")) {
790 envlist_free(envlist
);
791 envlist
= envlist_create();
792 } else if (!strcmp(r
, "U")) {
794 if (envlist_unsetenv(envlist
, r
) != 0)
796 } else if (!strcmp(r
, "s")) {
798 x86_stack_size
= strtol(r
, (char **)&r
, 0);
799 if (x86_stack_size
<= 0)
802 x86_stack_size
*= MiB
;
803 else if (*r
== 'k' || *r
== 'K')
804 x86_stack_size
*= KiB
;
805 } else if (!strcmp(r
, "L")) {
806 interp_prefix
= argv
[optind
++];
807 } else if (!strcmp(r
, "p")) {
808 qemu_host_page_size
= atoi(argv
[optind
++]);
809 if (qemu_host_page_size
== 0 ||
810 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
811 fprintf(stderr
, "page size must be a power of two\n");
814 } else if (!strcmp(r
, "g")) {
815 gdbstub_port
= atoi(argv
[optind
++]);
816 } else if (!strcmp(r
, "r")) {
817 qemu_uname_release
= argv
[optind
++];
818 } else if (!strcmp(r
, "cpu")) {
819 cpu_model
= argv
[optind
++];
820 if (is_help_option(cpu_model
)) {
821 /* XXX: implement xxx_cpu_list for targets that still miss it */
822 #if defined(cpu_list)
827 } else if (!strcmp(r
, "B")) {
828 guest_base
= strtol(argv
[optind
++], NULL
, 0);
830 } else if (!strcmp(r
, "drop-ld-preload")) {
831 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
832 } else if (!strcmp(r
, "bsd")) {
833 if (!strcasecmp(argv
[optind
], "freebsd")) {
834 bsd_type
= target_freebsd
;
835 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
836 bsd_type
= target_netbsd
;
837 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
838 bsd_type
= target_openbsd
;
843 } else if (!strcmp(r
, "singlestep")) {
845 } else if (!strcmp(r
, "strace")) {
847 } else if (!strcmp(r
, "trace")) {
849 trace_file
= trace_opt_parse(optarg
);
856 qemu_log_needs_buffers();
857 qemu_set_log_filename(log_file
, &error_fatal
);
861 mask
= qemu_str_to_log_mask(log_mask
);
863 qemu_print_log_usage(stdout
);
869 if (optind
>= argc
) {
872 filename
= argv
[optind
];
874 if (!trace_init_backends()) {
877 trace_init_file(trace_file
);
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";
906 /* init tcg before creating CPUs and to get qemu_host_page_size */
909 cpu_type
= parse_cpu_option(cpu_model
);
910 cpu
= cpu_create(cpu_type
);
912 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
917 if (getenv("QEMU_STRACE")) {
921 target_environ
= envlist_to_environ(envlist
, NULL
);
922 envlist_free(envlist
);
925 * Now that page sizes are configured in tcg_exec_init() we can do
926 * proper page alignment for guest_base.
928 guest_base
= HOST_PAGE_ALIGN(guest_base
);
931 * Read in mmap_min_addr kernel parameter. This value is used
932 * When loading the ELF image to determine whether guest_base
935 * When user has explicitly set the quest base, we skip this
938 if (!have_guest_base
) {
941 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
943 if (fscanf(fp
, "%lu", &tmp
) == 1) {
945 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
951 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
952 printf("Error loading %s\n", filename
);
956 for (wrk
= target_environ
; *wrk
; wrk
++) {
960 g_free(target_environ
);
962 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
963 qemu_log("guest_base 0x%lx\n", guest_base
);
966 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
967 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
968 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
970 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
972 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
973 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
975 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
976 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
979 target_set_brk(info
->brk
);
983 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
984 generating the prologue until now so that the prologue can take
985 the real value of GUEST_BASE into account. */
986 tcg_prologue_init(tcg_ctx
);
989 /* build Task State */
990 memset(ts
, 0, sizeof(TaskState
));
995 #if defined(TARGET_I386)
996 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
997 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
998 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
999 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1000 env
->hflags
|= HF_OSFXSR_MASK
;
1002 #ifndef TARGET_ABI32
1003 /* enable 64 bit mode if possible */
1004 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1005 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1008 env
->cr
[4] |= CR4_PAE_MASK
;
1009 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1010 env
->hflags
|= HF_LMA_MASK
;
1013 /* flags setup : we activate the IRQs by default as in user mode */
1014 env
->eflags
|= IF_MASK
;
1016 /* linux register setup */
1017 #ifndef TARGET_ABI32
1018 env
->regs
[R_EAX
] = regs
->rax
;
1019 env
->regs
[R_EBX
] = regs
->rbx
;
1020 env
->regs
[R_ECX
] = regs
->rcx
;
1021 env
->regs
[R_EDX
] = regs
->rdx
;
1022 env
->regs
[R_ESI
] = regs
->rsi
;
1023 env
->regs
[R_EDI
] = regs
->rdi
;
1024 env
->regs
[R_EBP
] = regs
->rbp
;
1025 env
->regs
[R_ESP
] = regs
->rsp
;
1026 env
->eip
= regs
->rip
;
1028 env
->regs
[R_EAX
] = regs
->eax
;
1029 env
->regs
[R_EBX
] = regs
->ebx
;
1030 env
->regs
[R_ECX
] = regs
->ecx
;
1031 env
->regs
[R_EDX
] = regs
->edx
;
1032 env
->regs
[R_ESI
] = regs
->esi
;
1033 env
->regs
[R_EDI
] = regs
->edi
;
1034 env
->regs
[R_EBP
] = regs
->ebp
;
1035 env
->regs
[R_ESP
] = regs
->esp
;
1036 env
->eip
= regs
->eip
;
1039 /* linux interrupt setup */
1040 #ifndef TARGET_ABI32
1041 env
->idt
.limit
= 511;
1043 env
->idt
.limit
= 255;
1045 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1046 PROT_READ
|PROT_WRITE
,
1047 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1048 idt_table
= g2h(env
->idt
.base
);
1071 /* linux segment setup */
1073 uint64_t *gdt_table
;
1074 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1075 PROT_READ
|PROT_WRITE
,
1076 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1077 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1078 gdt_table
= g2h(env
->gdt
.base
);
1080 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1081 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1082 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1084 /* 64 bit code segment */
1085 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1086 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1088 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1090 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1091 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1092 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1095 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1096 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1098 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1099 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1100 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1101 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1102 /* This hack makes Wine work... */
1103 env
->segs
[R_FS
].selector
= 0;
1105 cpu_x86_load_seg(env
, R_DS
, 0);
1106 cpu_x86_load_seg(env
, R_ES
, 0);
1107 cpu_x86_load_seg(env
, R_FS
, 0);
1108 cpu_x86_load_seg(env
, R_GS
, 0);
1110 #elif defined(TARGET_SPARC)
1114 env
->npc
= regs
->npc
;
1116 for(i
= 0; i
< 8; i
++)
1117 env
->gregs
[i
] = regs
->u_regs
[i
];
1118 for(i
= 0; i
< 8; i
++)
1119 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1122 #error unsupported target CPU
1126 gdbserver_start (gdbstub_port
);
1127 gdb_handlesig(cpu
, 0);