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"
40 #include "trace/control.h"
43 unsigned long mmap_min_addr
;
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
;
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
;
254 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
258 process_pending_signals(env
);
264 #define SPARC64_STACK_BIAS 2047
266 /* #define DEBUG_WIN */
268 * WARNING: dealing with register windows _is_ complicated. More info
269 * can be found at http://www.sics.se/~psm/sparcstack.html
271 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
273 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
275 * wrap handling : if cwp is on the last window, then we use the
276 * registers 'after' the end
278 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
279 index
+= 16 * env
->nwindows
;
283 /* save the register window 'cwp1' */
284 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
289 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
290 #ifdef TARGET_SPARC64
292 sp_ptr
+= SPARC64_STACK_BIAS
;
294 #if defined(DEBUG_WIN)
295 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
298 for (i
= 0; i
< 16; i
++) {
299 /* FIXME - what to do if put_user() fails? */
300 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
301 sp_ptr
+= sizeof(abi_ulong
);
305 static void save_window(CPUSPARCState
*env
)
307 #ifndef TARGET_SPARC64
308 unsigned int new_wim
;
309 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
310 ((1LL << env
->nwindows
) - 1);
311 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
315 * cansave is zero if the spill trap handler is triggered by `save` and
316 * nonzero if triggered by a `flushw`
318 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- env
->cansave
- 2));
324 static void restore_window(CPUSPARCState
*env
)
326 #ifndef TARGET_SPARC64
327 unsigned int new_wim
;
329 unsigned int i
, cwp1
;
332 #ifndef TARGET_SPARC64
333 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
334 ((1LL << env
->nwindows
) - 1);
337 /* restore the invalid window */
338 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
339 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
340 #ifdef TARGET_SPARC64
342 sp_ptr
+= SPARC64_STACK_BIAS
;
344 #if defined(DEBUG_WIN)
345 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
348 for (i
= 0; i
< 16; i
++) {
349 /* FIXME - what to do if get_user() fails? */
350 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
351 sp_ptr
+= sizeof(abi_ulong
);
353 #ifdef TARGET_SPARC64
355 if (env
->cleanwin
< env
->nwindows
- 1)
363 static void flush_windows(CPUSPARCState
*env
)
369 /* if restore would invoke restore_window(), then we can stop */
370 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
371 #ifndef TARGET_SPARC64
372 if (env
->wim
& (1 << cwp1
))
375 if (env
->canrestore
== 0)
380 save_window_offset(env
, cwp1
);
383 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
384 #ifndef TARGET_SPARC64
385 /* set wim so that restore will reload the registers */
386 env
->wim
= 1 << cwp1
;
388 #if defined(DEBUG_WIN)
389 printf("flush_windows: nb=%d\n", offset
- 1);
393 void cpu_loop(CPUSPARCState
*env
)
395 CPUState
*cs
= env_cpu(env
);
396 int trapnr
, ret
, syscall_nr
;
397 /* target_siginfo_t info; */
401 trapnr
= cpu_exec(cs
);
403 process_queued_cpu_work(cs
);
406 #ifndef TARGET_SPARC64
409 /* FreeBSD uses 0x141 for syscalls too */
411 if (bsd_type
!= target_freebsd
)
416 syscall_nr
= env
->gregs
[1];
417 if (bsd_type
== target_freebsd
)
418 ret
= do_freebsd_syscall(env
, syscall_nr
,
419 env
->regwptr
[0], env
->regwptr
[1],
420 env
->regwptr
[2], env
->regwptr
[3],
421 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
422 else if (bsd_type
== target_netbsd
)
423 ret
= do_netbsd_syscall(env
, syscall_nr
,
424 env
->regwptr
[0], env
->regwptr
[1],
425 env
->regwptr
[2], env
->regwptr
[3],
426 env
->regwptr
[4], env
->regwptr
[5]);
427 else { /* if (bsd_type == target_openbsd) */
428 #if defined(TARGET_SPARC64)
429 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
430 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
432 ret
= do_openbsd_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]);
437 if ((unsigned int)ret
>= (unsigned int)(-515)) {
439 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
440 env
->xcc
|= PSR_CARRY
;
442 env
->psr
|= PSR_CARRY
;
445 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
446 env
->xcc
&= ~PSR_CARRY
;
448 env
->psr
&= ~PSR_CARRY
;
451 env
->regwptr
[0] = ret
;
452 /* next instruction */
453 #if defined(TARGET_SPARC64)
454 if (bsd_type
== target_openbsd
&&
455 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
456 env
->pc
= env
->gregs
[2];
457 env
->npc
= env
->pc
+ 4;
458 } else if (bsd_type
== target_openbsd
&&
459 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
460 env
->pc
= env
->gregs
[7];
461 env
->npc
= env
->pc
+ 4;
464 env
->npc
= env
->npc
+ 4;
468 env
->npc
= env
->npc
+ 4;
471 case 0x83: /* flush windows */
476 /* next instruction */
478 env
->npc
= env
->npc
+ 4;
480 #ifndef TARGET_SPARC64
481 case TT_WIN_OVF
: /* window overflow */
484 case TT_WIN_UNF
: /* window underflow */
491 case TT_SPILL
: /* window overflow */
494 case TT_FILL
: /* window underflow */
502 /* just indicate that signals should be handled asap */
506 gdb_handlesig(cs
, TARGET_SIGTRAP
);
510 #ifdef TARGET_SPARC64
513 printf("Unhandled trap: 0x%x\n", trapnr
);
514 cpu_dump_state(cs
, stderr
, 0);
517 process_pending_signals(env
);
523 static void usage(void)
525 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
526 "\n" QEMU_COPYRIGHT
"\n"
527 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
528 "BSD CPU emulator (compiled for %s emulation)\n"
530 "Standard options:\n"
531 "-h print this help\n"
532 "-g port wait gdb connection to port\n"
533 "-L path set the elf interpreter prefix (default=%s)\n"
534 "-s size set the stack size in bytes (default=%ld)\n"
535 "-cpu model select CPU (-cpu help for list)\n"
536 "-drop-ld-preload drop LD_PRELOAD for target process\n"
537 "-E var=value sets/modifies targets environment variable(s)\n"
538 "-U var unsets targets environment variable(s)\n"
539 "-B address set guest_base address to address\n"
540 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
543 "-d item1[,...] enable logging of specified items\n"
544 " (use '-d help' for a list of log items)\n"
545 "-D logfile write logs to 'logfile' (default stderr)\n"
546 "-p pagesize set the host page size to 'pagesize'\n"
547 "-singlestep always run in singlestep mode\n"
548 "-strace log system calls\n"
549 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
550 " specify tracing options\n"
552 "Environment variables:\n"
553 "QEMU_STRACE Print system calls and arguments similar to the\n"
554 " 'strace' program. Enable by setting to any value.\n"
555 "You can use -E and -U options to set/unset environment variables\n"
556 "for target process. It is possible to provide several variables\n"
557 "by repeating the option. For example:\n"
558 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
559 "Note that if you provide several changes to single variable\n"
560 "last change will stay in effect.\n"
562 QEMU_HELP_BOTTOM
"\n"
570 THREAD CPUState
*thread_cpu
;
572 bool qemu_cpu_is_self(CPUState
*cpu
)
574 return thread_cpu
== cpu
;
577 void qemu_cpu_kick(CPUState
*cpu
)
582 /* Assumes contents are already zeroed. */
583 void init_task_state(TaskState
*ts
)
588 ts
->first_free
= ts
->sigqueue_table
;
589 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
590 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
592 ts
->sigqueue_table
[i
].next
= NULL
;
595 int main(int argc
, char **argv
)
597 const char *filename
;
598 const char *cpu_model
;
599 const char *cpu_type
;
600 const char *log_file
= NULL
;
601 const char *log_mask
= NULL
;
602 struct target_pt_regs regs1
, *regs
= ®s1
;
603 struct image_info info1
, *info
= &info1
;
604 TaskState ts1
, *ts
= &ts1
;
609 const char *gdbstub
= NULL
;
610 char **target_environ
, **wrk
;
611 envlist_t
*envlist
= NULL
;
612 bsd_type
= target_openbsd
;
618 module_call_init(MODULE_INIT_TRACE
);
619 qemu_init_cpu_list();
620 module_call_init(MODULE_INIT_QOM
);
622 envlist
= envlist_create();
624 /* add current environment into the list */
625 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
626 (void) envlist_setenv(envlist
, *wrk
);
631 qemu_add_opts(&qemu_trace_opts
);
642 if (!strcmp(r
, "-")) {
644 } else if (!strcmp(r
, "d")) {
645 if (optind
>= argc
) {
648 log_mask
= argv
[optind
++];
649 } else if (!strcmp(r
, "D")) {
650 if (optind
>= argc
) {
653 log_file
= argv
[optind
++];
654 } else if (!strcmp(r
, "E")) {
656 if (envlist_setenv(envlist
, r
) != 0)
658 } else if (!strcmp(r
, "ignore-environment")) {
659 envlist_free(envlist
);
660 envlist
= envlist_create();
661 } else if (!strcmp(r
, "U")) {
663 if (envlist_unsetenv(envlist
, r
) != 0)
665 } else if (!strcmp(r
, "s")) {
667 x86_stack_size
= strtol(r
, (char **)&r
, 0);
668 if (x86_stack_size
<= 0)
671 x86_stack_size
*= MiB
;
672 else if (*r
== 'k' || *r
== 'K')
673 x86_stack_size
*= KiB
;
674 } else if (!strcmp(r
, "L")) {
675 interp_prefix
= argv
[optind
++];
676 } else if (!strcmp(r
, "p")) {
677 qemu_host_page_size
= atoi(argv
[optind
++]);
678 if (qemu_host_page_size
== 0 ||
679 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
680 fprintf(stderr
, "page size must be a power of two\n");
683 } else if (!strcmp(r
, "g")) {
684 gdbstub
= g_strdup(argv
[optind
++]);
685 } else if (!strcmp(r
, "r")) {
686 qemu_uname_release
= argv
[optind
++];
687 } else if (!strcmp(r
, "cpu")) {
688 cpu_model
= argv
[optind
++];
689 if (is_help_option(cpu_model
)) {
690 /* XXX: implement xxx_cpu_list for targets that still miss it */
691 #if defined(cpu_list)
696 } else if (!strcmp(r
, "B")) {
697 guest_base
= strtol(argv
[optind
++], NULL
, 0);
698 have_guest_base
= true;
699 } else if (!strcmp(r
, "drop-ld-preload")) {
700 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
701 } else if (!strcmp(r
, "bsd")) {
702 if (!strcasecmp(argv
[optind
], "freebsd")) {
703 bsd_type
= target_freebsd
;
704 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
705 bsd_type
= target_netbsd
;
706 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
707 bsd_type
= target_openbsd
;
712 } else if (!strcmp(r
, "singlestep")) {
714 } else if (!strcmp(r
, "strace")) {
716 } else if (!strcmp(r
, "trace")) {
717 trace_opt_parse(optarg
);
724 qemu_log_needs_buffers();
725 qemu_set_log_filename(log_file
, &error_fatal
);
729 mask
= qemu_str_to_log_mask(log_mask
);
731 qemu_print_log_usage(stdout
);
737 if (optind
>= argc
) {
740 filename
= argv
[optind
];
742 if (!trace_init_backends()) {
748 memset(regs
, 0, sizeof(struct target_pt_regs
));
750 /* Zero out image_info */
751 memset(info
, 0, sizeof(struct image_info
));
753 /* Scan interp_prefix dir for replacement files. */
754 init_paths(interp_prefix
);
756 if (cpu_model
== NULL
) {
757 #if defined(TARGET_I386)
759 cpu_model
= "qemu64";
761 cpu_model
= "qemu32";
763 #elif defined(TARGET_SPARC)
764 #ifdef TARGET_SPARC64
765 cpu_model
= "TI UltraSparc II";
767 cpu_model
= "Fujitsu MB86904";
774 cpu_type
= parse_cpu_option(cpu_model
);
775 /* init tcg before creating CPUs and to get qemu_host_page_size */
777 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
779 ac
->init_machine(NULL
);
780 accel_init_interfaces(ac
);
782 cpu
= cpu_create(cpu_type
);
784 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
789 if (getenv("QEMU_STRACE")) {
793 target_environ
= envlist_to_environ(envlist
, NULL
);
794 envlist_free(envlist
);
797 * Now that page sizes are configured in tcg_exec_init() we can do
798 * proper page alignment for guest_base.
800 guest_base
= HOST_PAGE_ALIGN(guest_base
);
803 * Read in mmap_min_addr kernel parameter. This value is used
804 * When loading the ELF image to determine whether guest_base
807 * When user has explicitly set the quest base, we skip this
810 if (!have_guest_base
) {
813 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
815 if (fscanf(fp
, "%lu", &tmp
) == 1) {
817 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
823 if (loader_exec(filename
, argv
+ optind
, target_environ
, regs
, info
) != 0) {
824 printf("Error loading %s\n", filename
);
828 for (wrk
= target_environ
; *wrk
; wrk
++) {
832 g_free(target_environ
);
834 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
835 qemu_log("guest_base %p\n", (void *)guest_base
);
836 log_page_dump("binary load");
838 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
839 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
840 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
842 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
844 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
845 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
847 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
848 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
851 target_set_brk(info
->brk
);
856 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
857 * generating the prologue until now so that the prologue can take
858 * the real value of GUEST_BASE into account.
860 tcg_prologue_init(tcg_ctx
);
863 /* build Task State */
864 memset(ts
, 0, sizeof(TaskState
));
869 #if defined(TARGET_I386)
870 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
871 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
872 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
873 env
->cr
[4] |= CR4_OSFXSR_MASK
;
874 env
->hflags
|= HF_OSFXSR_MASK
;
877 /* enable 64 bit mode if possible */
878 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
879 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
882 env
->cr
[4] |= CR4_PAE_MASK
;
883 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
884 env
->hflags
|= HF_LMA_MASK
;
887 /* flags setup : we activate the IRQs by default as in user mode */
888 env
->eflags
|= IF_MASK
;
890 /* linux register setup */
892 env
->regs
[R_EAX
] = regs
->rax
;
893 env
->regs
[R_EBX
] = regs
->rbx
;
894 env
->regs
[R_ECX
] = regs
->rcx
;
895 env
->regs
[R_EDX
] = regs
->rdx
;
896 env
->regs
[R_ESI
] = regs
->rsi
;
897 env
->regs
[R_EDI
] = regs
->rdi
;
898 env
->regs
[R_EBP
] = regs
->rbp
;
899 env
->regs
[R_ESP
] = regs
->rsp
;
900 env
->eip
= regs
->rip
;
902 env
->regs
[R_EAX
] = regs
->eax
;
903 env
->regs
[R_EBX
] = regs
->ebx
;
904 env
->regs
[R_ECX
] = regs
->ecx
;
905 env
->regs
[R_EDX
] = regs
->edx
;
906 env
->regs
[R_ESI
] = regs
->esi
;
907 env
->regs
[R_EDI
] = regs
->edi
;
908 env
->regs
[R_EBP
] = regs
->ebp
;
909 env
->regs
[R_ESP
] = regs
->esp
;
910 env
->eip
= regs
->eip
;
913 /* linux interrupt setup */
915 env
->idt
.limit
= 511;
917 env
->idt
.limit
= 255;
919 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
920 PROT_READ
| PROT_WRITE
,
921 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
922 idt_table
= g2h_untagged(env
->idt
.base
);
945 /* linux segment setup */
948 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
949 PROT_READ
| PROT_WRITE
,
950 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
951 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
952 gdt_table
= g2h_untagged(env
->gdt
.base
);
954 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
955 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
956 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
958 /* 64 bit code segment */
959 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
960 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
962 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
964 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
965 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
966 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
969 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
970 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
972 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
973 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
974 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
975 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
976 /* This hack makes Wine work... */
977 env
->segs
[R_FS
].selector
= 0;
979 cpu_x86_load_seg(env
, R_DS
, 0);
980 cpu_x86_load_seg(env
, R_ES
, 0);
981 cpu_x86_load_seg(env
, R_FS
, 0);
982 cpu_x86_load_seg(env
, R_GS
, 0);
984 #elif defined(TARGET_SPARC)
988 env
->npc
= regs
->npc
;
990 for (i
= 0; i
< 8; i
++)
991 env
->gregs
[i
] = regs
->u_regs
[i
];
992 for (i
= 0; i
< 8; i
++)
993 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
996 #error unsupported target CPU
1000 gdbserver_start(gdbstub
);
1001 gdb_handlesig(cpu
, 0);