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 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
57 unsigned long x86_stack_size
= 512 * 1024;
59 void gemu_log(const char *fmt
, ...)
64 vfprintf(stderr
, fmt
, ap
);
68 #if defined(TARGET_I386)
69 int cpu_get_pic_interrupt(CPUX86State
*env
)
79 void fork_end(int child
)
82 gdbserver_fork(thread_cpu
);
87 /***********************************************************/
88 /* CPUX86 core interface */
90 uint64_t cpu_get_tsc(CPUX86State
*env
)
92 return cpu_get_host_ticks();
95 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
100 e1
= (addr
<< 16) | (limit
& 0xffff);
101 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
108 static uint64_t *idt_table
;
110 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
111 uint64_t addr
, unsigned int sel
)
114 e1
= (addr
& 0xffff) | (sel
<< 16);
115 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
119 p
[2] = tswap32(addr
>> 32);
122 /* only dpl matters as we do only user space emulation */
123 static void set_idt(int n
, unsigned int dpl
)
125 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
128 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
129 uint32_t addr
, unsigned int sel
)
132 e1
= (addr
& 0xffff) | (sel
<< 16);
133 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
139 /* only dpl matters as we do only user space emulation */
140 static void set_idt(int n
, unsigned int dpl
)
142 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
146 void cpu_loop(CPUX86State
*env
)
148 CPUState
*cs
= env_cpu(env
);
151 /* target_siginfo_t info; */
155 trapnr
= cpu_exec(cs
);
157 process_queued_cpu_work(cs
);
161 /* syscall from int $0x80 */
162 if (bsd_type
== target_freebsd
) {
163 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
165 int32_t syscall_nr
= env
->regs
[R_EAX
];
166 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
168 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
169 get_user_s32(syscall_nr
, params
);
170 params
+= sizeof(int32_t);
171 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
172 get_user_s32(syscall_nr
, params
);
173 params
+= sizeof(int64_t);
175 get_user_s32(arg1
, params
);
176 params
+= sizeof(int32_t);
177 get_user_s32(arg2
, params
);
178 params
+= sizeof(int32_t);
179 get_user_s32(arg3
, params
);
180 params
+= sizeof(int32_t);
181 get_user_s32(arg4
, params
);
182 params
+= sizeof(int32_t);
183 get_user_s32(arg5
, params
);
184 params
+= sizeof(int32_t);
185 get_user_s32(arg6
, params
);
186 params
+= sizeof(int32_t);
187 get_user_s32(arg7
, params
);
188 params
+= sizeof(int32_t);
189 get_user_s32(arg8
, params
);
190 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
200 } else { /* if (bsd_type == target_openbsd) */
201 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
210 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
211 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
214 env
->eflags
&= ~CC_C
;
219 /* syscall from syscall instruction */
220 if (bsd_type
== target_freebsd
) {
221 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
229 } else { /* if (bsd_type == target_openbsd) */
230 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
239 env
->eip
= env
->exception_next_eip
;
240 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
241 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
244 env
->eflags
&= ~CC_C
;
249 /* just indicate that signals should be handled asap */
252 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
254 "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
;
284 /* save the register window 'cwp1' */
285 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
290 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
291 #ifdef TARGET_SPARC64
293 sp_ptr
+= SPARC64_STACK_BIAS
;
296 #if defined(DEBUG_WIN)
297 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
300 for (i
= 0; i
< 16; i
++) {
301 /* FIXME - what to do if put_user() fails? */
302 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
303 sp_ptr
+= sizeof(abi_ulong
);
307 static void save_window(CPUSPARCState
*env
)
309 #ifndef TARGET_SPARC64
310 unsigned int new_wim
;
311 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
312 ((1LL << env
->nwindows
) - 1);
313 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
317 * cansave is zero if the spill trap handler is triggered by `save` and
318 * nonzero if triggered by a `flushw`
320 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- env
->cansave
- 2));
326 static void restore_window(CPUSPARCState
*env
)
328 #ifndef TARGET_SPARC64
329 unsigned int new_wim
;
331 unsigned int i
, cwp1
;
334 #ifndef TARGET_SPARC64
335 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
336 ((1LL << env
->nwindows
) - 1);
339 /* restore the invalid window */
340 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
341 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
342 #ifdef TARGET_SPARC64
344 sp_ptr
+= SPARC64_STACK_BIAS
;
347 #if defined(DEBUG_WIN)
348 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
351 for (i
= 0; i
< 16; i
++) {
352 /* FIXME - what to do if get_user() fails? */
353 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
354 sp_ptr
+= sizeof(abi_ulong
);
356 #ifdef TARGET_SPARC64
358 if (env
->cleanwin
< env
->nwindows
- 1) {
367 static void flush_windows(CPUSPARCState
*env
)
373 /* if restore would invoke restore_window(), then we can stop */
374 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
375 #ifndef TARGET_SPARC64
376 if (env
->wim
& (1 << cwp1
)) {
380 if (env
->canrestore
== 0) {
386 save_window_offset(env
, cwp1
);
389 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
390 #ifndef TARGET_SPARC64
391 /* set wim so that restore will reload the registers */
392 env
->wim
= 1 << cwp1
;
394 #if defined(DEBUG_WIN)
395 printf("flush_windows: nb=%d\n", offset
- 1);
399 void cpu_loop(CPUSPARCState
*env
)
401 CPUState
*cs
= env_cpu(env
);
402 int trapnr
, ret
, syscall_nr
;
403 /* target_siginfo_t info; */
407 trapnr
= cpu_exec(cs
);
409 process_queued_cpu_work(cs
);
412 #ifndef TARGET_SPARC64
415 /* FreeBSD uses 0x141 for syscalls too */
417 if (bsd_type
!= target_freebsd
) {
423 syscall_nr
= env
->gregs
[1];
424 if (bsd_type
== target_freebsd
)
425 ret
= do_freebsd_syscall(env
, syscall_nr
,
426 env
->regwptr
[0], env
->regwptr
[1],
427 env
->regwptr
[2], env
->regwptr
[3],
428 env
->regwptr
[4], env
->regwptr
[5],
430 else if (bsd_type
== target_netbsd
)
431 ret
= do_netbsd_syscall(env
, syscall_nr
,
432 env
->regwptr
[0], env
->regwptr
[1],
433 env
->regwptr
[2], env
->regwptr
[3],
434 env
->regwptr
[4], env
->regwptr
[5]);
435 else { /* if (bsd_type == target_openbsd) */
436 #if defined(TARGET_SPARC64)
437 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
438 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
440 ret
= do_openbsd_syscall(env
, syscall_nr
,
441 env
->regwptr
[0], env
->regwptr
[1],
442 env
->regwptr
[2], env
->regwptr
[3],
443 env
->regwptr
[4], env
->regwptr
[5]);
445 if ((unsigned int)ret
>= (unsigned int)(-515)) {
447 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
448 env
->xcc
|= PSR_CARRY
;
450 env
->psr
|= PSR_CARRY
;
453 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
454 env
->xcc
&= ~PSR_CARRY
;
456 env
->psr
&= ~PSR_CARRY
;
459 env
->regwptr
[0] = ret
;
460 /* next instruction */
461 #if defined(TARGET_SPARC64)
462 if (bsd_type
== target_openbsd
&&
463 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
464 env
->pc
= env
->gregs
[2];
465 env
->npc
= env
->pc
+ 4;
466 } else if (bsd_type
== target_openbsd
&&
467 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
468 env
->pc
= env
->gregs
[7];
469 env
->npc
= env
->pc
+ 4;
472 env
->npc
= env
->npc
+ 4;
476 env
->npc
= env
->npc
+ 4;
479 case 0x83: /* flush windows */
484 /* next instruction */
486 env
->npc
= env
->npc
+ 4;
488 #ifndef TARGET_SPARC64
489 case TT_WIN_OVF
: /* window overflow */
492 case TT_WIN_UNF
: /* window underflow */
499 case TT_SPILL
: /* window overflow */
502 case TT_FILL
: /* window underflow */
510 /* just indicate that signals should be handled asap */
514 gdb_handlesig(cs
, TARGET_SIGTRAP
);
518 #ifdef TARGET_SPARC64
521 printf("Unhandled trap: 0x%x\n", trapnr
);
522 cpu_dump_state(cs
, stderr
, 0);
525 process_pending_signals(env
);
531 static void usage(void)
533 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
534 "\n" QEMU_COPYRIGHT
"\n"
535 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
536 "BSD CPU emulator (compiled for %s emulation)\n"
538 "Standard options:\n"
539 "-h print this help\n"
540 "-g port wait gdb connection to port\n"
541 "-L path set the elf interpreter prefix (default=%s)\n"
542 "-s size set the stack size in bytes (default=%ld)\n"
543 "-cpu model select CPU (-cpu help for list)\n"
544 "-drop-ld-preload drop LD_PRELOAD for target process\n"
545 "-E var=value sets/modifies targets environment variable(s)\n"
546 "-U var unsets targets environment variable(s)\n"
547 "-B address set guest_base address to address\n"
548 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
551 "-d item1[,...] enable logging of specified items\n"
552 " (use '-d help' for a list of log items)\n"
553 "-D logfile write logs to 'logfile' (default stderr)\n"
554 "-p pagesize set the host page size to 'pagesize'\n"
555 "-singlestep always run in singlestep mode\n"
556 "-strace log system calls\n"
557 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
558 " specify tracing options\n"
560 "Environment variables:\n"
561 "QEMU_STRACE Print system calls and arguments similar to the\n"
562 " 'strace' program. Enable by setting to any value.\n"
563 "You can use -E and -U options to set/unset environment variables\n"
564 "for target process. It is possible to provide several variables\n"
565 "by repeating the option. For example:\n"
566 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
567 "Note that if you provide several changes to single variable\n"
568 "last change will stay in effect.\n"
570 QEMU_HELP_BOTTOM
"\n"
578 THREAD CPUState
*thread_cpu
;
580 bool qemu_cpu_is_self(CPUState
*cpu
)
582 return thread_cpu
== cpu
;
585 void qemu_cpu_kick(CPUState
*cpu
)
590 /* Assumes contents are already zeroed. */
591 void init_task_state(TaskState
*ts
)
596 ts
->first_free
= ts
->sigqueue_table
;
597 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
598 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
600 ts
->sigqueue_table
[i
].next
= NULL
;
603 int main(int argc
, char **argv
)
605 const char *filename
;
606 const char *cpu_model
;
607 const char *cpu_type
;
608 const char *log_file
= NULL
;
609 const char *log_mask
= NULL
;
610 struct target_pt_regs regs1
, *regs
= ®s1
;
611 struct image_info info1
, *info
= &info1
;
612 TaskState ts1
, *ts
= &ts1
;
617 const char *gdbstub
= NULL
;
618 char **target_environ
, **wrk
;
619 envlist_t
*envlist
= NULL
;
620 bsd_type
= target_openbsd
;
627 module_call_init(MODULE_INIT_TRACE
);
628 qemu_init_cpu_list();
629 module_call_init(MODULE_INIT_QOM
);
631 envlist
= envlist_create();
633 /* add current environment into the list */
634 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
635 (void) envlist_setenv(envlist
, *wrk
);
640 qemu_add_opts(&qemu_trace_opts
);
644 if (optind
>= argc
) {
653 if (!strcmp(r
, "-")) {
655 } else if (!strcmp(r
, "d")) {
656 if (optind
>= argc
) {
659 log_mask
= argv
[optind
++];
660 } else if (!strcmp(r
, "D")) {
661 if (optind
>= argc
) {
664 log_file
= argv
[optind
++];
665 } else if (!strcmp(r
, "E")) {
667 if (envlist_setenv(envlist
, r
) != 0) {
670 } else if (!strcmp(r
, "ignore-environment")) {
671 envlist_free(envlist
);
672 envlist
= envlist_create();
673 } else if (!strcmp(r
, "U")) {
675 if (envlist_unsetenv(envlist
, r
) != 0) {
678 } else if (!strcmp(r
, "s")) {
680 x86_stack_size
= strtol(r
, (char **)&r
, 0);
681 if (x86_stack_size
<= 0) {
685 x86_stack_size
*= MiB
;
686 } else if (*r
== 'k' || *r
== 'K') {
687 x86_stack_size
*= KiB
;
689 } else if (!strcmp(r
, "L")) {
690 interp_prefix
= argv
[optind
++];
691 } else if (!strcmp(r
, "p")) {
692 qemu_host_page_size
= atoi(argv
[optind
++]);
693 if (qemu_host_page_size
== 0 ||
694 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
695 fprintf(stderr
, "page size must be a power of two\n");
698 } else if (!strcmp(r
, "g")) {
699 gdbstub
= g_strdup(argv
[optind
++]);
700 } else if (!strcmp(r
, "r")) {
701 qemu_uname_release
= argv
[optind
++];
702 } else if (!strcmp(r
, "cpu")) {
703 cpu_model
= argv
[optind
++];
704 if (is_help_option(cpu_model
)) {
705 /* XXX: implement xxx_cpu_list for targets that still miss it */
706 #if defined(cpu_list)
711 } else if (!strcmp(r
, "B")) {
712 guest_base
= strtol(argv
[optind
++], NULL
, 0);
713 have_guest_base
= true;
714 } else if (!strcmp(r
, "drop-ld-preload")) {
715 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
716 } else if (!strcmp(r
, "bsd")) {
717 if (!strcasecmp(argv
[optind
], "freebsd")) {
718 bsd_type
= target_freebsd
;
719 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
720 bsd_type
= target_netbsd
;
721 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
722 bsd_type
= target_openbsd
;
727 } else if (!strcmp(r
, "singlestep")) {
729 } else if (!strcmp(r
, "strace")) {
731 } else if (!strcmp(r
, "trace")) {
732 trace_opt_parse(optarg
);
739 qemu_log_needs_buffers();
740 qemu_set_log_filename(log_file
, &error_fatal
);
744 mask
= qemu_str_to_log_mask(log_mask
);
746 qemu_print_log_usage(stdout
);
752 if (optind
>= argc
) {
755 filename
= argv
[optind
];
757 if (!trace_init_backends()) {
763 memset(regs
, 0, sizeof(struct target_pt_regs
));
765 /* Zero out image_info */
766 memset(info
, 0, sizeof(struct image_info
));
768 /* Scan interp_prefix dir for replacement files. */
769 init_paths(interp_prefix
);
771 if (cpu_model
== NULL
) {
772 #if defined(TARGET_I386)
774 cpu_model
= "qemu64";
776 cpu_model
= "qemu32";
778 #elif defined(TARGET_SPARC)
779 #ifdef TARGET_SPARC64
780 cpu_model
= "TI UltraSparc II";
782 cpu_model
= "Fujitsu MB86904";
789 cpu_type
= parse_cpu_option(cpu_model
);
790 /* init tcg before creating CPUs and to get qemu_host_page_size */
792 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
794 ac
->init_machine(NULL
);
795 accel_init_interfaces(ac
);
797 cpu
= cpu_create(cpu_type
);
799 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
804 if (getenv("QEMU_STRACE")) {
808 target_environ
= envlist_to_environ(envlist
, NULL
);
809 envlist_free(envlist
);
812 * Now that page sizes are configured in tcg_exec_init() we can do
813 * proper page alignment for guest_base.
815 guest_base
= HOST_PAGE_ALIGN(guest_base
);
818 * Read in mmap_min_addr kernel parameter. This value is used
819 * When loading the ELF image to determine whether guest_base
822 * When user has explicitly set the quest base, we skip this
825 if (!have_guest_base
) {
828 fp
= fopen("/proc/sys/vm/mmap_min_addr", "r");
831 if (fscanf(fp
, "%lu", &tmp
) == 1) {
833 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n",
840 if (loader_exec(filename
, argv
+ optind
, target_environ
, regs
, info
) != 0) {
841 printf("Error loading %s\n", filename
);
845 for (wrk
= target_environ
; *wrk
; wrk
++) {
849 g_free(target_environ
);
851 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
852 qemu_log("guest_base %p\n", (void *)guest_base
);
853 log_page_dump("binary load");
855 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
856 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
857 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
859 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
861 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
862 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
864 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
865 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
868 target_set_brk(info
->brk
);
873 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
874 * generating the prologue until now so that the prologue can take
875 * the real value of GUEST_BASE into account.
877 tcg_prologue_init(tcg_ctx
);
880 /* build Task State */
881 memset(ts
, 0, sizeof(TaskState
));
886 #if defined(TARGET_I386)
887 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
888 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
889 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
890 env
->cr
[4] |= CR4_OSFXSR_MASK
;
891 env
->hflags
|= HF_OSFXSR_MASK
;
894 /* enable 64 bit mode if possible */
895 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
896 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
899 env
->cr
[4] |= CR4_PAE_MASK
;
900 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
901 env
->hflags
|= HF_LMA_MASK
;
904 /* flags setup : we activate the IRQs by default as in user mode */
905 env
->eflags
|= IF_MASK
;
907 /* linux register setup */
909 env
->regs
[R_EAX
] = regs
->rax
;
910 env
->regs
[R_EBX
] = regs
->rbx
;
911 env
->regs
[R_ECX
] = regs
->rcx
;
912 env
->regs
[R_EDX
] = regs
->rdx
;
913 env
->regs
[R_ESI
] = regs
->rsi
;
914 env
->regs
[R_EDI
] = regs
->rdi
;
915 env
->regs
[R_EBP
] = regs
->rbp
;
916 env
->regs
[R_ESP
] = regs
->rsp
;
917 env
->eip
= regs
->rip
;
919 env
->regs
[R_EAX
] = regs
->eax
;
920 env
->regs
[R_EBX
] = regs
->ebx
;
921 env
->regs
[R_ECX
] = regs
->ecx
;
922 env
->regs
[R_EDX
] = regs
->edx
;
923 env
->regs
[R_ESI
] = regs
->esi
;
924 env
->regs
[R_EDI
] = regs
->edi
;
925 env
->regs
[R_EBP
] = regs
->ebp
;
926 env
->regs
[R_ESP
] = regs
->esp
;
927 env
->eip
= regs
->eip
;
930 /* linux interrupt setup */
932 env
->idt
.limit
= 511;
934 env
->idt
.limit
= 255;
936 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
937 PROT_READ
| PROT_WRITE
,
938 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
939 idt_table
= g2h_untagged(env
->idt
.base
);
962 /* linux segment setup */
965 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
966 PROT_READ
| PROT_WRITE
,
967 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
968 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
969 gdt_table
= g2h_untagged(env
->gdt
.base
);
971 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
972 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
973 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
975 /* 64 bit code segment */
976 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
977 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
979 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
981 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
982 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
983 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
986 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
987 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
989 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
990 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
991 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
992 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
993 /* This hack makes Wine work... */
994 env
->segs
[R_FS
].selector
= 0;
996 cpu_x86_load_seg(env
, R_DS
, 0);
997 cpu_x86_load_seg(env
, R_ES
, 0);
998 cpu_x86_load_seg(env
, R_FS
, 0);
999 cpu_x86_load_seg(env
, R_GS
, 0);
1001 #elif defined(TARGET_SPARC)
1005 env
->npc
= regs
->npc
;
1007 for (i
= 0; i
< 8; i
++) {
1008 env
->gregs
[i
] = regs
->u_regs
[i
];
1010 for (i
= 0; i
< 8; i
++) {
1011 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1015 #error unsupported target CPU
1019 gdbserver_start(gdbstub
);
1020 gdb_handlesig(cpu
, 0);