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 "qemu/accel.h"
24 #include "sysemu/tcg.h"
25 #include "qemu-version.h"
26 #include <machine/trap.h>
28 #include "qapi/error.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/path.h"
33 #include "qemu/help_option.h"
34 #include "qemu/module.h"
35 #include "exec/exec-all.h"
37 #include "qemu/timer.h"
38 #include "qemu/envlist.h"
39 #include "qemu/cutils.h"
41 #include "trace/control.h"
44 unsigned long mmap_min_addr
;
47 unsigned long reserved_va
;
49 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
50 const char *qemu_uname_release
;
51 enum BSDType bsd_type
;
54 * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
55 * we allocate a bigger stack. Need a better solution, for example
56 * by remapping the process stack directly at the right place
58 unsigned long x86_stack_size
= 512 * 1024;
60 void gemu_log(const char *fmt
, ...)
65 vfprintf(stderr
, fmt
, ap
);
69 #if defined(TARGET_I386)
70 int cpu_get_pic_interrupt(CPUX86State
*env
)
80 void fork_end(int child
)
83 gdbserver_fork(thread_cpu
);
88 /***********************************************************/
89 /* CPUX86 core interface */
91 uint64_t cpu_get_tsc(CPUX86State
*env
)
93 return cpu_get_host_ticks();
96 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
101 e1
= (addr
<< 16) | (limit
& 0xffff);
102 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
109 static uint64_t *idt_table
;
111 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
112 uint64_t addr
, unsigned int sel
)
115 e1
= (addr
& 0xffff) | (sel
<< 16);
116 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
120 p
[2] = tswap32(addr
>> 32);
123 /* only dpl matters as we do only user space emulation */
124 static void set_idt(int n
, unsigned int dpl
)
126 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
129 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
130 uint32_t addr
, unsigned int sel
)
133 e1
= (addr
& 0xffff) | (sel
<< 16);
134 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
140 /* only dpl matters as we do only user space emulation */
141 static void set_idt(int n
, unsigned int dpl
)
143 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
147 void cpu_loop(CPUX86State
*env
)
149 CPUState
*cs
= env_cpu(env
);
152 /* target_siginfo_t info; */
156 trapnr
= cpu_exec(cs
);
158 process_queued_cpu_work(cs
);
162 /* syscall from int $0x80 */
163 if (bsd_type
== target_freebsd
) {
164 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
166 int32_t syscall_nr
= env
->regs
[R_EAX
];
167 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
169 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
170 get_user_s32(syscall_nr
, params
);
171 params
+= sizeof(int32_t);
172 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
173 get_user_s32(syscall_nr
, params
);
174 params
+= sizeof(int64_t);
176 get_user_s32(arg1
, params
);
177 params
+= sizeof(int32_t);
178 get_user_s32(arg2
, params
);
179 params
+= sizeof(int32_t);
180 get_user_s32(arg3
, params
);
181 params
+= sizeof(int32_t);
182 get_user_s32(arg4
, params
);
183 params
+= sizeof(int32_t);
184 get_user_s32(arg5
, params
);
185 params
+= sizeof(int32_t);
186 get_user_s32(arg6
, params
);
187 params
+= sizeof(int32_t);
188 get_user_s32(arg7
, params
);
189 params
+= sizeof(int32_t);
190 get_user_s32(arg8
, params
);
191 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
201 } else { /* if (bsd_type == target_openbsd) */
202 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
211 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
212 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
215 env
->eflags
&= ~CC_C
;
220 /* syscall from syscall instruction */
221 if (bsd_type
== target_freebsd
) {
222 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
230 } else { /* if (bsd_type == target_openbsd) */
231 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
240 env
->eip
= env
->exception_next_eip
;
241 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
242 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
245 env
->eflags
&= ~CC_C
;
250 /* just indicate that signals should be handled asap */
253 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
255 "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
259 process_pending_signals(env
);
265 #define SPARC64_STACK_BIAS 2047
267 /* #define DEBUG_WIN */
269 * WARNING: dealing with register windows _is_ complicated. More info
270 * can be found at http://www.sics.se/~psm/sparcstack.html
272 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
274 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
276 * wrap handling : if cwp is on the last window, then we use the
277 * registers 'after' the end
279 if (index
< 8 && env
->cwp
== env
->nwindows
- 1) {
280 index
+= 16 * env
->nwindows
;
285 /* save the register window 'cwp1' */
286 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
291 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
292 #ifdef TARGET_SPARC64
294 sp_ptr
+= SPARC64_STACK_BIAS
;
297 #if defined(DEBUG_WIN)
298 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
301 for (i
= 0; i
< 16; i
++) {
302 /* FIXME - what to do if put_user() fails? */
303 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
304 sp_ptr
+= sizeof(abi_ulong
);
308 static void save_window(CPUSPARCState
*env
)
310 #ifndef TARGET_SPARC64
311 unsigned int new_wim
;
312 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
313 ((1LL << env
->nwindows
) - 1);
314 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
318 * cansave is zero if the spill trap handler is triggered by `save` and
319 * nonzero if triggered by a `flushw`
321 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- env
->cansave
- 2));
327 static void restore_window(CPUSPARCState
*env
)
329 #ifndef TARGET_SPARC64
330 unsigned int new_wim
;
332 unsigned int i
, cwp1
;
335 #ifndef TARGET_SPARC64
336 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
337 ((1LL << env
->nwindows
) - 1);
340 /* restore the invalid window */
341 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
342 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
343 #ifdef TARGET_SPARC64
345 sp_ptr
+= SPARC64_STACK_BIAS
;
348 #if defined(DEBUG_WIN)
349 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
352 for (i
= 0; i
< 16; i
++) {
353 /* FIXME - what to do if get_user() fails? */
354 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
355 sp_ptr
+= sizeof(abi_ulong
);
357 #ifdef TARGET_SPARC64
359 if (env
->cleanwin
< env
->nwindows
- 1) {
368 static void flush_windows(CPUSPARCState
*env
)
374 /* if restore would invoke restore_window(), then we can stop */
375 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
376 #ifndef TARGET_SPARC64
377 if (env
->wim
& (1 << cwp1
)) {
381 if (env
->canrestore
== 0) {
387 save_window_offset(env
, cwp1
);
390 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
391 #ifndef TARGET_SPARC64
392 /* set wim so that restore will reload the registers */
393 env
->wim
= 1 << cwp1
;
395 #if defined(DEBUG_WIN)
396 printf("flush_windows: nb=%d\n", offset
- 1);
400 void cpu_loop(CPUSPARCState
*env
)
402 CPUState
*cs
= env_cpu(env
);
403 int trapnr
, ret
, syscall_nr
;
404 /* target_siginfo_t info; */
408 trapnr
= cpu_exec(cs
);
410 process_queued_cpu_work(cs
);
413 #ifndef TARGET_SPARC64
416 /* FreeBSD uses 0x141 for syscalls too */
418 if (bsd_type
!= target_freebsd
) {
424 syscall_nr
= env
->gregs
[1];
425 if (bsd_type
== target_freebsd
)
426 ret
= do_freebsd_syscall(env
, syscall_nr
,
427 env
->regwptr
[0], env
->regwptr
[1],
428 env
->regwptr
[2], env
->regwptr
[3],
429 env
->regwptr
[4], env
->regwptr
[5],
431 else if (bsd_type
== target_netbsd
)
432 ret
= do_netbsd_syscall(env
, syscall_nr
,
433 env
->regwptr
[0], env
->regwptr
[1],
434 env
->regwptr
[2], env
->regwptr
[3],
435 env
->regwptr
[4], env
->regwptr
[5]);
436 else { /* if (bsd_type == target_openbsd) */
437 #if defined(TARGET_SPARC64)
438 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
439 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
441 ret
= do_openbsd_syscall(env
, syscall_nr
,
442 env
->regwptr
[0], env
->regwptr
[1],
443 env
->regwptr
[2], env
->regwptr
[3],
444 env
->regwptr
[4], env
->regwptr
[5]);
446 if ((unsigned int)ret
>= (unsigned int)(-515)) {
448 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
449 env
->xcc
|= PSR_CARRY
;
451 env
->psr
|= PSR_CARRY
;
454 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
455 env
->xcc
&= ~PSR_CARRY
;
457 env
->psr
&= ~PSR_CARRY
;
460 env
->regwptr
[0] = ret
;
461 /* next instruction */
462 #if defined(TARGET_SPARC64)
463 if (bsd_type
== target_openbsd
&&
464 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
465 env
->pc
= env
->gregs
[2];
466 env
->npc
= env
->pc
+ 4;
467 } else if (bsd_type
== target_openbsd
&&
468 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
469 env
->pc
= env
->gregs
[7];
470 env
->npc
= env
->pc
+ 4;
473 env
->npc
= env
->npc
+ 4;
477 env
->npc
= env
->npc
+ 4;
480 case 0x83: /* flush windows */
485 /* next instruction */
487 env
->npc
= env
->npc
+ 4;
489 #ifndef TARGET_SPARC64
490 case TT_WIN_OVF
: /* window overflow */
493 case TT_WIN_UNF
: /* window underflow */
500 case TT_SPILL
: /* window overflow */
503 case TT_FILL
: /* window underflow */
511 /* just indicate that signals should be handled asap */
515 gdb_handlesig(cs
, TARGET_SIGTRAP
);
519 #ifdef TARGET_SPARC64
522 printf("Unhandled trap: 0x%x\n", trapnr
);
523 cpu_dump_state(cs
, stderr
, 0);
526 process_pending_signals(env
);
532 static void usage(void)
534 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
535 "\n" QEMU_COPYRIGHT
"\n"
536 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
537 "BSD CPU emulator (compiled for %s emulation)\n"
539 "Standard options:\n"
540 "-h print this help\n"
541 "-g port wait gdb connection to port\n"
542 "-L path set the elf interpreter prefix (default=%s)\n"
543 "-s size set the stack size in bytes (default=%ld)\n"
544 "-cpu model select CPU (-cpu help for list)\n"
545 "-drop-ld-preload drop LD_PRELOAD for target process\n"
546 "-E var=value sets/modifies targets environment variable(s)\n"
547 "-U var unsets targets environment variable(s)\n"
548 "-B address set guest_base address to address\n"
549 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
552 "-d item1[,...] enable logging of specified items\n"
553 " (use '-d help' for a list of log items)\n"
554 "-D logfile write logs to 'logfile' (default stderr)\n"
555 "-p pagesize set the host page size to 'pagesize'\n"
556 "-singlestep always run in singlestep mode\n"
557 "-strace log system calls\n"
558 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
559 " specify tracing options\n"
561 "Environment variables:\n"
562 "QEMU_STRACE Print system calls and arguments similar to the\n"
563 " 'strace' program. Enable by setting to any value.\n"
564 "You can use -E and -U options to set/unset environment variables\n"
565 "for target process. It is possible to provide several variables\n"
566 "by repeating the option. For example:\n"
567 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
568 "Note that if you provide several changes to single variable\n"
569 "last change will stay in effect.\n"
571 QEMU_HELP_BOTTOM
"\n"
579 THREAD CPUState
*thread_cpu
;
581 bool qemu_cpu_is_self(CPUState
*cpu
)
583 return thread_cpu
== cpu
;
586 void qemu_cpu_kick(CPUState
*cpu
)
591 /* Assumes contents are already zeroed. */
592 void init_task_state(TaskState
*ts
)
597 ts
->first_free
= ts
->sigqueue_table
;
598 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
599 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
601 ts
->sigqueue_table
[i
].next
= NULL
;
604 int main(int argc
, char **argv
)
606 const char *filename
;
607 const char *cpu_model
;
608 const char *cpu_type
;
609 const char *log_file
= NULL
;
610 const char *log_mask
= NULL
;
611 struct target_pt_regs regs1
, *regs
= ®s1
;
612 struct image_info info1
, *info
= &info1
;
613 TaskState ts1
, *ts
= &ts1
;
618 const char *gdbstub
= NULL
;
619 char **target_environ
, **wrk
;
620 envlist_t
*envlist
= NULL
;
621 bsd_type
= target_openbsd
;
628 module_call_init(MODULE_INIT_TRACE
);
629 qemu_init_cpu_list();
630 module_call_init(MODULE_INIT_QOM
);
632 envlist
= envlist_create();
634 /* add current environment into the list */
635 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
636 (void) envlist_setenv(envlist
, *wrk
);
641 qemu_add_opts(&qemu_trace_opts
);
645 if (optind
>= argc
) {
654 if (!strcmp(r
, "-")) {
656 } else if (!strcmp(r
, "d")) {
657 if (optind
>= argc
) {
660 log_mask
= argv
[optind
++];
661 } else if (!strcmp(r
, "D")) {
662 if (optind
>= argc
) {
665 log_file
= argv
[optind
++];
666 } else if (!strcmp(r
, "E")) {
668 if (envlist_setenv(envlist
, r
) != 0) {
671 } else if (!strcmp(r
, "ignore-environment")) {
672 envlist_free(envlist
);
673 envlist
= envlist_create();
674 } else if (!strcmp(r
, "U")) {
676 if (envlist_unsetenv(envlist
, r
) != 0) {
679 } else if (!strcmp(r
, "s")) {
681 rv
= qemu_strtoul(r
, &r
, 0, &x86_stack_size
);
682 if (rv
< 0 || x86_stack_size
<= 0) {
686 x86_stack_size
*= MiB
;
687 } else if (*r
== 'k' || *r
== 'K') {
688 x86_stack_size
*= KiB
;
690 } else if (!strcmp(r
, "L")) {
691 interp_prefix
= argv
[optind
++];
692 } else if (!strcmp(r
, "p")) {
693 qemu_host_page_size
= atoi(argv
[optind
++]);
694 if (qemu_host_page_size
== 0 ||
695 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
696 fprintf(stderr
, "page size must be a power of two\n");
699 } else if (!strcmp(r
, "g")) {
700 gdbstub
= g_strdup(argv
[optind
++]);
701 } else if (!strcmp(r
, "r")) {
702 qemu_uname_release
= argv
[optind
++];
703 } else if (!strcmp(r
, "cpu")) {
704 cpu_model
= argv
[optind
++];
705 if (is_help_option(cpu_model
)) {
706 /* XXX: implement xxx_cpu_list for targets that still miss it */
707 #if defined(cpu_list)
712 } else if (!strcmp(r
, "B")) {
713 rv
= qemu_strtoul(argv
[optind
++], NULL
, 0, &guest_base
);
717 have_guest_base
= true;
718 } else if (!strcmp(r
, "drop-ld-preload")) {
719 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
720 } else if (!strcmp(r
, "bsd")) {
721 if (!strcasecmp(argv
[optind
], "freebsd")) {
722 bsd_type
= target_freebsd
;
723 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
724 bsd_type
= target_netbsd
;
725 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
726 bsd_type
= target_openbsd
;
731 } else if (!strcmp(r
, "singlestep")) {
733 } else if (!strcmp(r
, "strace")) {
735 } else if (!strcmp(r
, "trace")) {
736 trace_opt_parse(optarg
);
743 qemu_log_needs_buffers();
744 qemu_set_log_filename(log_file
, &error_fatal
);
748 mask
= qemu_str_to_log_mask(log_mask
);
750 qemu_print_log_usage(stdout
);
756 if (optind
>= argc
) {
759 filename
= argv
[optind
];
761 if (!trace_init_backends()) {
767 memset(regs
, 0, sizeof(struct target_pt_regs
));
769 /* Zero out image_info */
770 memset(info
, 0, sizeof(struct image_info
));
772 /* Scan interp_prefix dir for replacement files. */
773 init_paths(interp_prefix
);
775 if (cpu_model
== NULL
) {
776 #if defined(TARGET_I386)
778 cpu_model
= "qemu64";
780 cpu_model
= "qemu32";
782 #elif defined(TARGET_SPARC)
783 #ifdef TARGET_SPARC64
784 cpu_model
= "TI UltraSparc II";
786 cpu_model
= "Fujitsu MB86904";
793 cpu_type
= parse_cpu_option(cpu_model
);
794 /* init tcg before creating CPUs and to get qemu_host_page_size */
796 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
798 accel_init_interfaces(ac
);
799 ac
->init_machine(NULL
);
801 cpu
= cpu_create(cpu_type
);
803 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
808 if (getenv("QEMU_STRACE")) {
812 target_environ
= envlist_to_environ(envlist
, NULL
);
813 envlist_free(envlist
);
816 * Now that page sizes are configured we can do
817 * proper page alignment for guest_base.
819 guest_base
= HOST_PAGE_ALIGN(guest_base
);
822 * Read in mmap_min_addr kernel parameter. This value is used
823 * When loading the ELF image to determine whether guest_base
826 * When user has explicitly set the quest base, we skip this
829 if (!have_guest_base
) {
832 fp
= fopen("/proc/sys/vm/mmap_min_addr", "r");
835 if (fscanf(fp
, "%lu", &tmp
) == 1) {
837 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n",
844 if (loader_exec(filename
, argv
+ optind
, target_environ
, regs
, info
) != 0) {
845 printf("Error loading %s\n", filename
);
849 for (wrk
= target_environ
; *wrk
; wrk
++) {
853 g_free(target_environ
);
855 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
856 qemu_log("guest_base %p\n", (void *)guest_base
);
857 log_page_dump("binary load");
859 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
860 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
861 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
863 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
865 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
866 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
868 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
869 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
872 target_set_brk(info
->brk
);
877 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
878 * generating the prologue until now so that the prologue can take
879 * the real value of GUEST_BASE into account.
881 tcg_prologue_init(tcg_ctx
);
883 /* build Task State */
884 memset(ts
, 0, sizeof(TaskState
));
889 #if defined(TARGET_I386)
890 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
891 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
892 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
893 env
->cr
[4] |= CR4_OSFXSR_MASK
;
894 env
->hflags
|= HF_OSFXSR_MASK
;
897 /* enable 64 bit mode if possible */
898 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
899 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
902 env
->cr
[4] |= CR4_PAE_MASK
;
903 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
904 env
->hflags
|= HF_LMA_MASK
;
907 /* flags setup : we activate the IRQs by default as in user mode */
908 env
->eflags
|= IF_MASK
;
910 /* linux register setup */
912 env
->regs
[R_EAX
] = regs
->rax
;
913 env
->regs
[R_EBX
] = regs
->rbx
;
914 env
->regs
[R_ECX
] = regs
->rcx
;
915 env
->regs
[R_EDX
] = regs
->rdx
;
916 env
->regs
[R_ESI
] = regs
->rsi
;
917 env
->regs
[R_EDI
] = regs
->rdi
;
918 env
->regs
[R_EBP
] = regs
->rbp
;
919 env
->regs
[R_ESP
] = regs
->rsp
;
920 env
->eip
= regs
->rip
;
922 env
->regs
[R_EAX
] = regs
->eax
;
923 env
->regs
[R_EBX
] = regs
->ebx
;
924 env
->regs
[R_ECX
] = regs
->ecx
;
925 env
->regs
[R_EDX
] = regs
->edx
;
926 env
->regs
[R_ESI
] = regs
->esi
;
927 env
->regs
[R_EDI
] = regs
->edi
;
928 env
->regs
[R_EBP
] = regs
->ebp
;
929 env
->regs
[R_ESP
] = regs
->esp
;
930 env
->eip
= regs
->eip
;
933 /* linux interrupt setup */
935 env
->idt
.limit
= 511;
937 env
->idt
.limit
= 255;
939 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
940 PROT_READ
| PROT_WRITE
,
941 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
942 idt_table
= g2h_untagged(env
->idt
.base
);
965 /* linux segment setup */
968 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
969 PROT_READ
| PROT_WRITE
,
970 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
971 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
972 gdt_table
= g2h_untagged(env
->gdt
.base
);
974 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
975 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
976 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
978 /* 64 bit code segment */
979 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
980 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
982 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
984 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
985 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
986 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
989 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
990 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
992 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
993 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
994 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
995 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
996 /* This hack makes Wine work... */
997 env
->segs
[R_FS
].selector
= 0;
999 cpu_x86_load_seg(env
, R_DS
, 0);
1000 cpu_x86_load_seg(env
, R_ES
, 0);
1001 cpu_x86_load_seg(env
, R_FS
, 0);
1002 cpu_x86_load_seg(env
, R_GS
, 0);
1004 #elif defined(TARGET_SPARC)
1008 env
->npc
= regs
->npc
;
1010 for (i
= 0; i
< 8; i
++) {
1011 env
->gregs
[i
] = regs
->u_regs
[i
];
1013 for (i
= 0; i
< 8; i
++) {
1014 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1018 #error unsupported target CPU
1022 gdbserver_start(gdbstub
);
1023 gdb_handlesig(cpu
, 0);