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/>.
25 #include <machine/trap.h>
26 #include <sys/types.h>
30 #include "qemu-common.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
40 #if defined(CONFIG_USE_GUEST_BASE)
41 unsigned long mmap_min_addr
;
42 unsigned long guest_base
;
46 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
47 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
48 extern char **environ
;
50 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
51 we allocate a bigger stack. Need a better solution, for example
52 by remapping the process stack directly at the right place */
53 unsigned long x86_stack_size
= 512 * 1024;
55 void gemu_log(const char *fmt
, ...)
60 vfprintf(stderr
, fmt
, ap
);
64 #if defined(TARGET_I386)
65 int cpu_get_pic_interrupt(CPUState
*env
)
71 /* These are no-ops because we are not threadsafe. */
72 static inline void cpu_exec_start(CPUState
*env
)
76 static inline void cpu_exec_end(CPUState
*env
)
80 static inline void start_exclusive(void)
84 static inline void end_exclusive(void)
92 void fork_end(int child
)
95 gdbserver_fork(thread_env
);
99 void cpu_list_lock(void)
103 void cpu_list_unlock(void)
108 /***********************************************************/
109 /* CPUX86 core interface */
111 void cpu_smm_update(CPUState
*env
)
115 uint64_t cpu_get_tsc(CPUX86State
*env
)
117 return cpu_get_real_ticks();
120 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
125 e1
= (addr
<< 16) | (limit
& 0xffff);
126 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
133 static uint64_t *idt_table
;
135 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
136 uint64_t addr
, unsigned int sel
)
139 e1
= (addr
& 0xffff) | (sel
<< 16);
140 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
144 p
[2] = tswap32(addr
>> 32);
147 /* only dpl matters as we do only user space emulation */
148 static void set_idt(int n
, unsigned int dpl
)
150 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
153 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
154 uint32_t addr
, unsigned int sel
)
157 e1
= (addr
& 0xffff) | (sel
<< 16);
158 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
164 /* only dpl matters as we do only user space emulation */
165 static void set_idt(int n
, unsigned int dpl
)
167 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
171 void cpu_loop(CPUX86State
*env
, enum BSDType bsd_type
)
175 //target_siginfo_t info;
178 trapnr
= cpu_x86_exec(env
);
181 /* syscall from int $0x80 */
182 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
193 /* linux syscall from syscall intruction */
194 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
202 env
->eip
= env
->exception_next_eip
;
208 info
.si_signo
= SIGBUS
;
210 info
.si_code
= TARGET_SI_KERNEL
;
211 info
._sifields
._sigfault
._addr
= 0;
212 queue_signal(env
, info
.si_signo
, &info
);
215 /* XXX: potential problem if ABI32 */
216 #ifndef TARGET_X86_64
217 if (env
->eflags
& VM_MASK
) {
218 handle_vm86_fault(env
);
222 info
.si_signo
= SIGSEGV
;
224 info
.si_code
= TARGET_SI_KERNEL
;
225 info
._sifields
._sigfault
._addr
= 0;
226 queue_signal(env
, info
.si_signo
, &info
);
230 info
.si_signo
= SIGSEGV
;
232 if (!(env
->error_code
& 1))
233 info
.si_code
= TARGET_SEGV_MAPERR
;
235 info
.si_code
= TARGET_SEGV_ACCERR
;
236 info
._sifields
._sigfault
._addr
= env
->cr
[2];
237 queue_signal(env
, info
.si_signo
, &info
);
240 #ifndef TARGET_X86_64
241 if (env
->eflags
& VM_MASK
) {
242 handle_vm86_trap(env
, trapnr
);
246 /* division by zero */
247 info
.si_signo
= SIGFPE
;
249 info
.si_code
= TARGET_FPE_INTDIV
;
250 info
._sifields
._sigfault
._addr
= env
->eip
;
251 queue_signal(env
, info
.si_signo
, &info
);
256 #ifndef TARGET_X86_64
257 if (env
->eflags
& VM_MASK
) {
258 handle_vm86_trap(env
, trapnr
);
262 info
.si_signo
= SIGTRAP
;
264 if (trapnr
== EXCP01_DB
) {
265 info
.si_code
= TARGET_TRAP_BRKPT
;
266 info
._sifields
._sigfault
._addr
= env
->eip
;
268 info
.si_code
= TARGET_SI_KERNEL
;
269 info
._sifields
._sigfault
._addr
= 0;
271 queue_signal(env
, info
.si_signo
, &info
);
276 #ifndef TARGET_X86_64
277 if (env
->eflags
& VM_MASK
) {
278 handle_vm86_trap(env
, trapnr
);
282 info
.si_signo
= SIGSEGV
;
284 info
.si_code
= TARGET_SI_KERNEL
;
285 info
._sifields
._sigfault
._addr
= 0;
286 queue_signal(env
, info
.si_signo
, &info
);
290 info
.si_signo
= SIGILL
;
292 info
.si_code
= TARGET_ILL_ILLOPN
;
293 info
._sifields
._sigfault
._addr
= env
->eip
;
294 queue_signal(env
, info
.si_signo
, &info
);
298 /* just indicate that signals should be handled asap */
305 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
310 info
.si_code
= TARGET_TRAP_BRKPT
;
311 queue_signal(env
, info
.si_signo
, &info
);
317 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
318 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
322 process_pending_signals(env
);
328 #define SPARC64_STACK_BIAS 2047
331 /* WARNING: dealing with register windows _is_ complicated. More info
332 can be found at http://www.sics.se/~psm/sparcstack.html */
333 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
335 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
336 /* wrap handling : if cwp is on the last window, then we use the
337 registers 'after' the end */
338 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
339 index
+= 16 * env
->nwindows
;
343 /* save the register window 'cwp1' */
344 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
349 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
350 #ifdef TARGET_SPARC64
352 sp_ptr
+= SPARC64_STACK_BIAS
;
354 #if defined(DEBUG_WIN)
355 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
358 for(i
= 0; i
< 16; i
++) {
359 /* FIXME - what to do if put_user() fails? */
360 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
361 sp_ptr
+= sizeof(abi_ulong
);
365 static void save_window(CPUSPARCState
*env
)
367 #ifndef TARGET_SPARC64
368 unsigned int new_wim
;
369 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
370 ((1LL << env
->nwindows
) - 1);
371 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
374 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
380 static void restore_window(CPUSPARCState
*env
)
382 #ifndef TARGET_SPARC64
383 unsigned int new_wim
;
385 unsigned int i
, cwp1
;
388 #ifndef TARGET_SPARC64
389 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
390 ((1LL << env
->nwindows
) - 1);
393 /* restore the invalid window */
394 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
395 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
396 #ifdef TARGET_SPARC64
398 sp_ptr
+= SPARC64_STACK_BIAS
;
400 #if defined(DEBUG_WIN)
401 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
404 for(i
= 0; i
< 16; i
++) {
405 /* FIXME - what to do if get_user() fails? */
406 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
407 sp_ptr
+= sizeof(abi_ulong
);
409 #ifdef TARGET_SPARC64
411 if (env
->cleanwin
< env
->nwindows
- 1)
419 static void flush_windows(CPUSPARCState
*env
)
425 /* if restore would invoke restore_window(), then we can stop */
426 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
427 #ifndef TARGET_SPARC64
428 if (env
->wim
& (1 << cwp1
))
431 if (env
->canrestore
== 0)
436 save_window_offset(env
, cwp1
);
439 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
440 #ifndef TARGET_SPARC64
441 /* set wim so that restore will reload the registers */
442 env
->wim
= 1 << cwp1
;
444 #if defined(DEBUG_WIN)
445 printf("flush_windows: nb=%d\n", offset
- 1);
449 void cpu_loop(CPUSPARCState
*env
, enum BSDType bsd_type
)
451 int trapnr
, ret
, syscall_nr
;
452 //target_siginfo_t info;
455 trapnr
= cpu_sparc_exec (env
);
458 #ifndef TARGET_SPARC64
463 syscall_nr
= env
->gregs
[1];
464 if (bsd_type
== target_freebsd
)
465 ret
= do_freebsd_syscall(env
, syscall_nr
,
466 env
->regwptr
[0], env
->regwptr
[1],
467 env
->regwptr
[2], env
->regwptr
[3],
468 env
->regwptr
[4], env
->regwptr
[5]);
469 else if (bsd_type
== target_netbsd
)
470 ret
= do_netbsd_syscall(env
, syscall_nr
,
471 env
->regwptr
[0], env
->regwptr
[1],
472 env
->regwptr
[2], env
->regwptr
[3],
473 env
->regwptr
[4], env
->regwptr
[5]);
474 else { //if (bsd_type == target_openbsd)
475 #if defined(TARGET_SPARC64)
476 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
477 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
479 ret
= do_openbsd_syscall(env
, syscall_nr
,
480 env
->regwptr
[0], env
->regwptr
[1],
481 env
->regwptr
[2], env
->regwptr
[3],
482 env
->regwptr
[4], env
->regwptr
[5]);
484 if ((unsigned int)ret
>= (unsigned int)(-515)) {
485 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
486 env
->xcc
|= PSR_CARRY
;
488 env
->psr
|= PSR_CARRY
;
491 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
492 env
->xcc
&= ~PSR_CARRY
;
494 env
->psr
&= ~PSR_CARRY
;
497 env
->regwptr
[0] = ret
;
498 /* next instruction */
499 #if defined(TARGET_SPARC64)
500 if (bsd_type
== target_openbsd
&&
501 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
502 env
->pc
= env
->gregs
[2];
503 env
->npc
= env
->pc
+ 4;
504 } else if (bsd_type
== target_openbsd
&&
505 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
506 env
->pc
= env
->gregs
[7];
507 env
->npc
= env
->pc
+ 4;
510 env
->npc
= env
->npc
+ 4;
514 env
->npc
= env
->npc
+ 4;
517 case 0x83: /* flush windows */
522 /* next instruction */
524 env
->npc
= env
->npc
+ 4;
526 #ifndef TARGET_SPARC64
527 case TT_WIN_OVF
: /* window overflow */
530 case TT_WIN_UNF
: /* window underflow */
537 info
.si_signo
= SIGSEGV
;
539 /* XXX: check env->error_code */
540 info
.si_code
= TARGET_SEGV_MAPERR
;
541 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
542 queue_signal(env
, info
.si_signo
, &info
);
547 case TT_SPILL
: /* window overflow */
550 case TT_FILL
: /* window underflow */
557 info
.si_signo
= SIGSEGV
;
559 /* XXX: check env->error_code */
560 info
.si_code
= TARGET_SEGV_MAPERR
;
561 if (trapnr
== TT_DFAULT
)
562 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
564 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
565 //queue_signal(env, info.si_signo, &info);
571 /* just indicate that signals should be handled asap */
577 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
583 info
.si_code
= TARGET_TRAP_BRKPT
;
584 //queue_signal(env, info.si_signo, &info);
590 printf ("Unhandled trap: 0x%x\n", trapnr
);
591 cpu_dump_state(env
, stderr
, fprintf
, 0);
594 process_pending_signals (env
);
600 static void usage(void)
602 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
603 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
604 "BSD CPU emulator (compiled for %s emulation)\n"
606 "Standard options:\n"
607 "-h print this help\n"
608 "-g port wait gdb connection to port\n"
609 "-L path set the elf interpreter prefix (default=%s)\n"
610 "-s size set the stack size in bytes (default=%ld)\n"
611 "-cpu model select CPU (-cpu ? for list)\n"
612 "-drop-ld-preload drop LD_PRELOAD for target process\n"
613 "-E var=value sets/modifies targets environment variable(s)\n"
614 "-U var unsets targets environment variable(s)\n"
615 #if defined(CONFIG_USE_GUEST_BASE)
616 "-B address set guest_base address to address\n"
618 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
621 "-d options activate log (logfile=%s)\n"
622 "-p pagesize set the host page size to 'pagesize'\n"
623 "-singlestep always run in singlestep mode\n"
624 "-strace log system calls\n"
626 "Environment variables:\n"
627 "QEMU_STRACE Print system calls and arguments similar to the\n"
628 " 'strace' program. Enable by setting to any value.\n"
629 "You can use -E and -U options to set/unset environment variables\n"
630 "for target process. It is possible to provide several variables\n"
631 "by repeating the option. For example:\n"
632 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
633 "Note that if you provide several changes to single variable\n"
634 "last change will stay in effect.\n"
643 THREAD CPUState
*thread_env
;
645 /* Assumes contents are already zeroed. */
646 void init_task_state(TaskState
*ts
)
651 ts
->first_free
= ts
->sigqueue_table
;
652 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
653 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
655 ts
->sigqueue_table
[i
].next
= NULL
;
658 int main(int argc
, char **argv
)
660 const char *filename
;
661 const char *cpu_model
;
662 struct target_pt_regs regs1
, *regs
= ®s1
;
663 struct image_info info1
, *info
= &info1
;
664 TaskState ts1
, *ts
= &ts1
;
668 int gdbstub_port
= 0;
669 char **target_environ
, **wrk
;
670 envlist_t
*envlist
= NULL
;
671 enum BSDType bsd_type
= target_openbsd
;
677 cpu_set_log_filename(DEBUG_LOGFILE
);
679 if ((envlist
= envlist_create()) == NULL
) {
680 (void) fprintf(stderr
, "Unable to allocate envlist\n");
684 /* add current environment into the list */
685 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
686 (void) envlist_setenv(envlist
, *wrk
);
699 if (!strcmp(r
, "-")) {
701 } else if (!strcmp(r
, "d")) {
703 const CPULogItem
*item
;
709 mask
= cpu_str_to_log_mask(r
);
711 printf("Log items (comma separated):\n");
712 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
713 printf("%-10s %s\n", item
->name
, item
->help
);
718 } else if (!strcmp(r
, "E")) {
720 if (envlist_setenv(envlist
, r
) != 0)
722 } else if (!strcmp(r
, "U")) {
724 if (envlist_unsetenv(envlist
, r
) != 0)
726 } else if (!strcmp(r
, "s")) {
728 x86_stack_size
= strtol(r
, (char **)&r
, 0);
729 if (x86_stack_size
<= 0)
732 x86_stack_size
*= 1024 * 1024;
733 else if (*r
== 'k' || *r
== 'K')
734 x86_stack_size
*= 1024;
735 } else if (!strcmp(r
, "L")) {
736 interp_prefix
= argv
[optind
++];
737 } else if (!strcmp(r
, "p")) {
738 qemu_host_page_size
= atoi(argv
[optind
++]);
739 if (qemu_host_page_size
== 0 ||
740 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
741 fprintf(stderr
, "page size must be a power of two\n");
744 } else if (!strcmp(r
, "g")) {
745 gdbstub_port
= atoi(argv
[optind
++]);
746 } else if (!strcmp(r
, "r")) {
747 qemu_uname_release
= argv
[optind
++];
748 } else if (!strcmp(r
, "cpu")) {
749 cpu_model
= argv
[optind
++];
750 if (strcmp(cpu_model
, "?") == 0) {
751 /* XXX: implement xxx_cpu_list for targets that still miss it */
752 #if defined(cpu_list)
753 cpu_list(stdout
, &fprintf
);
757 #if defined(CONFIG_USE_GUEST_BASE)
758 } else if (!strcmp(r
, "B")) {
759 guest_base
= strtol(argv
[optind
++], NULL
, 0);
762 } else if (!strcmp(r
, "drop-ld-preload")) {
763 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
764 } else if (!strcmp(r
, "bsd")) {
765 if (!strcasecmp(argv
[optind
], "freebsd")) {
766 bsd_type
= target_freebsd
;
767 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
768 bsd_type
= target_netbsd
;
769 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
770 bsd_type
= target_openbsd
;
775 } else if (!strcmp(r
, "singlestep")) {
777 } else if (!strcmp(r
, "strace")) {
786 filename
= argv
[optind
];
789 memset(regs
, 0, sizeof(struct target_pt_regs
));
791 /* Zero out image_info */
792 memset(info
, 0, sizeof(struct image_info
));
794 /* Scan interp_prefix dir for replacement files. */
795 init_paths(interp_prefix
);
797 if (cpu_model
== NULL
) {
798 #if defined(TARGET_I386)
800 cpu_model
= "qemu64";
802 cpu_model
= "qemu32";
804 #elif defined(TARGET_SPARC)
805 #ifdef TARGET_SPARC64
806 cpu_model
= "TI UltraSparc II";
808 cpu_model
= "Fujitsu MB86904";
814 cpu_exec_init_all(0);
815 /* NOTE: we need to init the CPU at this stage to get
816 qemu_host_page_size */
817 env
= cpu_init(cpu_model
);
819 fprintf(stderr
, "Unable to find CPU definition\n");
824 if (getenv("QEMU_STRACE")) {
828 target_environ
= envlist_to_environ(envlist
, NULL
);
829 envlist_free(envlist
);
831 #if defined(CONFIG_USE_GUEST_BASE)
833 * Now that page sizes are configured in cpu_init() we can do
834 * proper page alignment for guest_base.
836 guest_base
= HOST_PAGE_ALIGN(guest_base
);
839 * Read in mmap_min_addr kernel parameter. This value is used
840 * When loading the ELF image to determine whether guest_base
843 * When user has explicitly set the quest base, we skip this
846 if (!have_guest_base
) {
849 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
851 if (fscanf(fp
, "%lu", &tmp
) == 1) {
853 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr
);
858 #endif /* CONFIG_USE_GUEST_BASE */
860 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
861 printf("Error loading %s\n", filename
);
865 for (wrk
= target_environ
; *wrk
; wrk
++) {
869 free(target_environ
);
871 if (qemu_log_enabled()) {
872 #if defined(CONFIG_USE_GUEST_BASE)
873 qemu_log("guest_base 0x%lx\n", guest_base
);
877 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
878 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
879 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
881 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
883 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
884 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
886 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
887 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
890 target_set_brk(info
->brk
);
894 /* build Task State */
895 memset(ts
, 0, sizeof(TaskState
));
900 #if defined(TARGET_I386)
901 cpu_x86_set_cpl(env
, 3);
903 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
904 env
->hflags
|= HF_PE_MASK
;
905 if (env
->cpuid_features
& CPUID_SSE
) {
906 env
->cr
[4] |= CR4_OSFXSR_MASK
;
907 env
->hflags
|= HF_OSFXSR_MASK
;
910 /* enable 64 bit mode if possible */
911 if (!(env
->cpuid_ext2_features
& CPUID_EXT2_LM
)) {
912 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
915 env
->cr
[4] |= CR4_PAE_MASK
;
916 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
917 env
->hflags
|= HF_LMA_MASK
;
920 /* flags setup : we activate the IRQs by default as in user mode */
921 env
->eflags
|= IF_MASK
;
923 /* linux register setup */
925 env
->regs
[R_EAX
] = regs
->rax
;
926 env
->regs
[R_EBX
] = regs
->rbx
;
927 env
->regs
[R_ECX
] = regs
->rcx
;
928 env
->regs
[R_EDX
] = regs
->rdx
;
929 env
->regs
[R_ESI
] = regs
->rsi
;
930 env
->regs
[R_EDI
] = regs
->rdi
;
931 env
->regs
[R_EBP
] = regs
->rbp
;
932 env
->regs
[R_ESP
] = regs
->rsp
;
933 env
->eip
= regs
->rip
;
935 env
->regs
[R_EAX
] = regs
->eax
;
936 env
->regs
[R_EBX
] = regs
->ebx
;
937 env
->regs
[R_ECX
] = regs
->ecx
;
938 env
->regs
[R_EDX
] = regs
->edx
;
939 env
->regs
[R_ESI
] = regs
->esi
;
940 env
->regs
[R_EDI
] = regs
->edi
;
941 env
->regs
[R_EBP
] = regs
->ebp
;
942 env
->regs
[R_ESP
] = regs
->esp
;
943 env
->eip
= regs
->eip
;
946 /* linux interrupt setup */
948 env
->idt
.limit
= 511;
950 env
->idt
.limit
= 255;
952 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
953 PROT_READ
|PROT_WRITE
,
954 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
955 idt_table
= g2h(env
->idt
.base
);
978 /* linux segment setup */
981 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
982 PROT_READ
|PROT_WRITE
,
983 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
984 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
985 gdt_table
= g2h(env
->gdt
.base
);
987 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
988 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
989 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
991 /* 64 bit code segment */
992 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
993 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
995 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
997 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
998 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
999 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1002 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1003 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1005 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1006 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1007 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1008 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1009 /* This hack makes Wine work... */
1010 env
->segs
[R_FS
].selector
= 0;
1012 cpu_x86_load_seg(env
, R_DS
, 0);
1013 cpu_x86_load_seg(env
, R_ES
, 0);
1014 cpu_x86_load_seg(env
, R_FS
, 0);
1015 cpu_x86_load_seg(env
, R_GS
, 0);
1017 #elif defined(TARGET_SPARC)
1021 env
->npc
= regs
->npc
;
1023 for(i
= 0; i
< 8; i
++)
1024 env
->gregs
[i
] = regs
->u_regs
[i
];
1025 for(i
= 0; i
< 8; i
++)
1026 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1029 #error unsupported target CPU
1033 gdbserver_start (gdbstub_port
);
1034 gdb_handlesig(env
, 0);
1036 cpu_loop(env
, bsd_type
);