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"
41 static const char *interp_prefix
= CONFIG_QEMU_PREFIX
;
42 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
43 extern char **environ
;
45 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
46 we allocate a bigger stack. Need a better solution, for example
47 by remapping the process stack directly at the right place */
48 unsigned long x86_stack_size
= 512 * 1024;
50 void gemu_log(const char *fmt
, ...)
55 vfprintf(stderr
, fmt
, ap
);
59 #if defined(TARGET_I386)
60 int cpu_get_pic_interrupt(CPUState
*env
)
66 /* These are no-ops because we are not threadsafe. */
67 static inline void cpu_exec_start(CPUState
*env
)
71 static inline void cpu_exec_end(CPUState
*env
)
75 static inline void start_exclusive(void)
79 static inline void end_exclusive(void)
87 void fork_end(int child
)
90 gdbserver_fork(thread_env
);
94 void cpu_list_lock(void)
98 void cpu_list_unlock(void)
103 /***********************************************************/
104 /* CPUX86 core interface */
106 void cpu_smm_update(CPUState
*env
)
110 uint64_t cpu_get_tsc(CPUX86State
*env
)
112 return cpu_get_real_ticks();
115 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
120 e1
= (addr
<< 16) | (limit
& 0xffff);
121 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
128 static uint64_t *idt_table
;
130 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
131 uint64_t addr
, unsigned int sel
)
134 e1
= (addr
& 0xffff) | (sel
<< 16);
135 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
139 p
[2] = tswap32(addr
>> 32);
142 /* only dpl matters as we do only user space emulation */
143 static void set_idt(int n
, unsigned int dpl
)
145 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
148 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
149 uint32_t addr
, unsigned int sel
)
152 e1
= (addr
& 0xffff) | (sel
<< 16);
153 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
159 /* only dpl matters as we do only user space emulation */
160 static void set_idt(int n
, unsigned int dpl
)
162 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
166 void cpu_loop(CPUX86State
*env
, enum BSDType bsd_type
)
170 //target_siginfo_t info;
173 trapnr
= cpu_x86_exec(env
);
176 /* syscall from int $0x80 */
177 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
188 /* linux syscall from syscall intruction */
189 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
197 env
->eip
= env
->exception_next_eip
;
203 info
.si_signo
= SIGBUS
;
205 info
.si_code
= TARGET_SI_KERNEL
;
206 info
._sifields
._sigfault
._addr
= 0;
207 queue_signal(env
, info
.si_signo
, &info
);
210 /* XXX: potential problem if ABI32 */
211 #ifndef TARGET_X86_64
212 if (env
->eflags
& VM_MASK
) {
213 handle_vm86_fault(env
);
217 info
.si_signo
= SIGSEGV
;
219 info
.si_code
= TARGET_SI_KERNEL
;
220 info
._sifields
._sigfault
._addr
= 0;
221 queue_signal(env
, info
.si_signo
, &info
);
225 info
.si_signo
= SIGSEGV
;
227 if (!(env
->error_code
& 1))
228 info
.si_code
= TARGET_SEGV_MAPERR
;
230 info
.si_code
= TARGET_SEGV_ACCERR
;
231 info
._sifields
._sigfault
._addr
= env
->cr
[2];
232 queue_signal(env
, info
.si_signo
, &info
);
235 #ifndef TARGET_X86_64
236 if (env
->eflags
& VM_MASK
) {
237 handle_vm86_trap(env
, trapnr
);
241 /* division by zero */
242 info
.si_signo
= SIGFPE
;
244 info
.si_code
= TARGET_FPE_INTDIV
;
245 info
._sifields
._sigfault
._addr
= env
->eip
;
246 queue_signal(env
, info
.si_signo
, &info
);
251 #ifndef TARGET_X86_64
252 if (env
->eflags
& VM_MASK
) {
253 handle_vm86_trap(env
, trapnr
);
257 info
.si_signo
= SIGTRAP
;
259 if (trapnr
== EXCP01_DB
) {
260 info
.si_code
= TARGET_TRAP_BRKPT
;
261 info
._sifields
._sigfault
._addr
= env
->eip
;
263 info
.si_code
= TARGET_SI_KERNEL
;
264 info
._sifields
._sigfault
._addr
= 0;
266 queue_signal(env
, info
.si_signo
, &info
);
271 #ifndef TARGET_X86_64
272 if (env
->eflags
& VM_MASK
) {
273 handle_vm86_trap(env
, trapnr
);
277 info
.si_signo
= SIGSEGV
;
279 info
.si_code
= TARGET_SI_KERNEL
;
280 info
._sifields
._sigfault
._addr
= 0;
281 queue_signal(env
, info
.si_signo
, &info
);
285 info
.si_signo
= SIGILL
;
287 info
.si_code
= TARGET_ILL_ILLOPN
;
288 info
._sifields
._sigfault
._addr
= env
->eip
;
289 queue_signal(env
, info
.si_signo
, &info
);
293 /* just indicate that signals should be handled asap */
300 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
305 info
.si_code
= TARGET_TRAP_BRKPT
;
306 queue_signal(env
, info
.si_signo
, &info
);
312 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
313 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
317 process_pending_signals(env
);
323 #define SPARC64_STACK_BIAS 2047
326 /* WARNING: dealing with register windows _is_ complicated. More info
327 can be found at http://www.sics.se/~psm/sparcstack.html */
328 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
330 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
331 /* wrap handling : if cwp is on the last window, then we use the
332 registers 'after' the end */
333 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
334 index
+= 16 * env
->nwindows
;
338 /* save the register window 'cwp1' */
339 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
344 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
345 #ifdef TARGET_SPARC64
347 sp_ptr
+= SPARC64_STACK_BIAS
;
349 #if defined(DEBUG_WIN)
350 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
353 for(i
= 0; i
< 16; i
++) {
354 /* FIXME - what to do if put_user() fails? */
355 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
356 sp_ptr
+= sizeof(abi_ulong
);
360 static void save_window(CPUSPARCState
*env
)
362 #ifndef TARGET_SPARC64
363 unsigned int new_wim
;
364 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
365 ((1LL << env
->nwindows
) - 1);
366 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
369 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
375 static void restore_window(CPUSPARCState
*env
)
377 #ifndef TARGET_SPARC64
378 unsigned int new_wim
;
380 unsigned int i
, cwp1
;
383 #ifndef TARGET_SPARC64
384 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
385 ((1LL << env
->nwindows
) - 1);
388 /* restore the invalid window */
389 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
390 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
391 #ifdef TARGET_SPARC64
393 sp_ptr
+= SPARC64_STACK_BIAS
;
395 #if defined(DEBUG_WIN)
396 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
399 for(i
= 0; i
< 16; i
++) {
400 /* FIXME - what to do if get_user() fails? */
401 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
402 sp_ptr
+= sizeof(abi_ulong
);
404 #ifdef TARGET_SPARC64
406 if (env
->cleanwin
< env
->nwindows
- 1)
414 static void flush_windows(CPUSPARCState
*env
)
420 /* if restore would invoke restore_window(), then we can stop */
421 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
422 #ifndef TARGET_SPARC64
423 if (env
->wim
& (1 << cwp1
))
426 if (env
->canrestore
== 0)
431 save_window_offset(env
, cwp1
);
434 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
435 #ifndef TARGET_SPARC64
436 /* set wim so that restore will reload the registers */
437 env
->wim
= 1 << cwp1
;
439 #if defined(DEBUG_WIN)
440 printf("flush_windows: nb=%d\n", offset
- 1);
444 void cpu_loop(CPUSPARCState
*env
, enum BSDType bsd_type
)
446 int trapnr
, ret
, syscall_nr
;
447 //target_siginfo_t info;
450 trapnr
= cpu_sparc_exec (env
);
453 #ifndef TARGET_SPARC64
458 syscall_nr
= env
->gregs
[1];
459 if (bsd_type
== target_freebsd
)
460 ret
= do_freebsd_syscall(env
, syscall_nr
,
461 env
->regwptr
[0], env
->regwptr
[1],
462 env
->regwptr
[2], env
->regwptr
[3],
463 env
->regwptr
[4], env
->regwptr
[5]);
464 else if (bsd_type
== target_netbsd
)
465 ret
= do_netbsd_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_openbsd)
470 #if defined(TARGET_SPARC64)
471 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
472 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
474 ret
= do_openbsd_syscall(env
, syscall_nr
,
475 env
->regwptr
[0], env
->regwptr
[1],
476 env
->regwptr
[2], env
->regwptr
[3],
477 env
->regwptr
[4], env
->regwptr
[5]);
479 if ((unsigned int)ret
>= (unsigned int)(-515)) {
480 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
481 env
->xcc
|= PSR_CARRY
;
483 env
->psr
|= PSR_CARRY
;
486 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
487 env
->xcc
&= ~PSR_CARRY
;
489 env
->psr
&= ~PSR_CARRY
;
492 env
->regwptr
[0] = ret
;
493 /* next instruction */
494 #if defined(TARGET_SPARC64)
495 if (bsd_type
== target_openbsd
&&
496 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
497 env
->pc
= env
->gregs
[2];
498 env
->npc
= env
->pc
+ 4;
499 } else if (bsd_type
== target_openbsd
&&
500 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
501 env
->pc
= env
->gregs
[7];
502 env
->npc
= env
->pc
+ 4;
505 env
->npc
= env
->npc
+ 4;
509 env
->npc
= env
->npc
+ 4;
512 case 0x83: /* flush windows */
517 /* next instruction */
519 env
->npc
= env
->npc
+ 4;
521 #ifndef TARGET_SPARC64
522 case TT_WIN_OVF
: /* window overflow */
525 case TT_WIN_UNF
: /* window underflow */
532 info
.si_signo
= SIGSEGV
;
534 /* XXX: check env->error_code */
535 info
.si_code
= TARGET_SEGV_MAPERR
;
536 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
537 queue_signal(env
, info
.si_signo
, &info
);
542 case TT_SPILL
: /* window overflow */
545 case TT_FILL
: /* window underflow */
552 info
.si_signo
= SIGSEGV
;
554 /* XXX: check env->error_code */
555 info
.si_code
= TARGET_SEGV_MAPERR
;
556 if (trapnr
== TT_DFAULT
)
557 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
559 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
560 //queue_signal(env, info.si_signo, &info);
566 /* just indicate that signals should be handled asap */
572 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
578 info
.si_code
= TARGET_TRAP_BRKPT
;
579 //queue_signal(env, info.si_signo, &info);
585 printf ("Unhandled trap: 0x%x\n", trapnr
);
586 cpu_dump_state(env
, stderr
, fprintf
, 0);
589 process_pending_signals (env
);
595 static void usage(void)
597 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
598 "usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
599 "BSD CPU emulator (compiled for %s emulation)\n"
601 "Standard options:\n"
602 "-h print this help\n"
603 "-g port wait gdb connection to port\n"
604 "-L path set the elf interpreter prefix (default=%s)\n"
605 "-s size set the stack size in bytes (default=%ld)\n"
606 "-cpu model select CPU (-cpu ? for list)\n"
607 "-drop-ld-preload drop LD_PRELOAD for target process\n"
608 "-E var=value sets/modifies targets environment variable(s)\n"
609 "-U var unsets targets environment variable(s)\n"
610 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
613 "-d options activate log (logfile=%s)\n"
614 "-p pagesize set the host page size to 'pagesize'\n"
615 "-singlestep always run in singlestep mode\n"
616 "-strace log system calls\n"
618 "Environment variables:\n"
619 "QEMU_STRACE Print system calls and arguments similar to the\n"
620 " 'strace' program. Enable by setting to any value.\n"
621 "You can use -E and -U options to set/unset environment variables\n"
622 "for target process. It is possible to provide several variables\n"
623 "by repeating the option. For example:\n"
624 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
625 "Note that if you provide several changes to single variable\n"
626 "last change will stay in effect.\n"
635 THREAD CPUState
*thread_env
;
637 /* Assumes contents are already zeroed. */
638 void init_task_state(TaskState
*ts
)
643 ts
->first_free
= ts
->sigqueue_table
;
644 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
645 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
647 ts
->sigqueue_table
[i
].next
= NULL
;
650 int main(int argc
, char **argv
)
652 const char *filename
;
653 const char *cpu_model
;
654 struct target_pt_regs regs1
, *regs
= ®s1
;
655 struct image_info info1
, *info
= &info1
;
656 TaskState ts1
, *ts
= &ts1
;
660 int gdbstub_port
= 0;
661 char **target_environ
, **wrk
;
662 envlist_t
*envlist
= NULL
;
663 enum BSDType bsd_type
= target_openbsd
;
669 cpu_set_log_filename(DEBUG_LOGFILE
);
671 if ((envlist
= envlist_create()) == NULL
) {
672 (void) fprintf(stderr
, "Unable to allocate envlist\n");
676 /* add current environment into the list */
677 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
678 (void) envlist_setenv(envlist
, *wrk
);
691 if (!strcmp(r
, "-")) {
693 } else if (!strcmp(r
, "d")) {
695 const CPULogItem
*item
;
701 mask
= cpu_str_to_log_mask(r
);
703 printf("Log items (comma separated):\n");
704 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
705 printf("%-10s %s\n", item
->name
, item
->help
);
710 } else if (!strcmp(r
, "E")) {
712 if (envlist_setenv(envlist
, r
) != 0)
714 } else if (!strcmp(r
, "U")) {
716 if (envlist_unsetenv(envlist
, r
) != 0)
718 } else if (!strcmp(r
, "s")) {
720 x86_stack_size
= strtol(r
, (char **)&r
, 0);
721 if (x86_stack_size
<= 0)
724 x86_stack_size
*= 1024 * 1024;
725 else if (*r
== 'k' || *r
== 'K')
726 x86_stack_size
*= 1024;
727 } else if (!strcmp(r
, "L")) {
728 interp_prefix
= argv
[optind
++];
729 } else if (!strcmp(r
, "p")) {
730 qemu_host_page_size
= atoi(argv
[optind
++]);
731 if (qemu_host_page_size
== 0 ||
732 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
733 fprintf(stderr
, "page size must be a power of two\n");
736 } else if (!strcmp(r
, "g")) {
737 gdbstub_port
= atoi(argv
[optind
++]);
738 } else if (!strcmp(r
, "r")) {
739 qemu_uname_release
= argv
[optind
++];
740 } else if (!strcmp(r
, "cpu")) {
741 cpu_model
= argv
[optind
++];
742 if (strcmp(cpu_model
, "?") == 0) {
743 /* XXX: implement xxx_cpu_list for targets that still miss it */
744 #if defined(cpu_list)
745 cpu_list(stdout
, &fprintf
);
749 } else if (!strcmp(r
, "drop-ld-preload")) {
750 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
751 } else if (!strcmp(r
, "bsd")) {
752 if (!strcasecmp(argv
[optind
], "freebsd")) {
753 bsd_type
= target_freebsd
;
754 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
755 bsd_type
= target_netbsd
;
756 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
757 bsd_type
= target_openbsd
;
762 } else if (!strcmp(r
, "singlestep")) {
764 } else if (!strcmp(r
, "strace")) {
773 filename
= argv
[optind
];
776 memset(regs
, 0, sizeof(struct target_pt_regs
));
778 /* Zero out image_info */
779 memset(info
, 0, sizeof(struct image_info
));
781 /* Scan interp_prefix dir for replacement files. */
782 init_paths(interp_prefix
);
784 if (cpu_model
== NULL
) {
785 #if defined(TARGET_I386)
787 cpu_model
= "qemu64";
789 cpu_model
= "qemu32";
791 #elif defined(TARGET_SPARC)
792 #ifdef TARGET_SPARC64
793 cpu_model
= "TI UltraSparc II";
795 cpu_model
= "Fujitsu MB86904";
801 cpu_exec_init_all(0);
802 /* NOTE: we need to init the CPU at this stage to get
803 qemu_host_page_size */
804 env
= cpu_init(cpu_model
);
806 fprintf(stderr
, "Unable to find CPU definition\n");
811 if (getenv("QEMU_STRACE")) {
815 target_environ
= envlist_to_environ(envlist
, NULL
);
816 envlist_free(envlist
);
819 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
820 printf("Error loading %s\n", filename
);
824 for (wrk
= target_environ
; *wrk
; wrk
++) {
828 free(target_environ
);
830 if (qemu_log_enabled()) {
833 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
834 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
835 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
837 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
839 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
840 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
842 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
843 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
846 target_set_brk(info
->brk
);
850 /* build Task State */
851 memset(ts
, 0, sizeof(TaskState
));
856 #if defined(TARGET_I386)
857 cpu_x86_set_cpl(env
, 3);
859 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
860 env
->hflags
|= HF_PE_MASK
;
861 if (env
->cpuid_features
& CPUID_SSE
) {
862 env
->cr
[4] |= CR4_OSFXSR_MASK
;
863 env
->hflags
|= HF_OSFXSR_MASK
;
866 /* enable 64 bit mode if possible */
867 if (!(env
->cpuid_ext2_features
& CPUID_EXT2_LM
)) {
868 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
871 env
->cr
[4] |= CR4_PAE_MASK
;
872 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
873 env
->hflags
|= HF_LMA_MASK
;
876 /* flags setup : we activate the IRQs by default as in user mode */
877 env
->eflags
|= IF_MASK
;
879 /* linux register setup */
881 env
->regs
[R_EAX
] = regs
->rax
;
882 env
->regs
[R_EBX
] = regs
->rbx
;
883 env
->regs
[R_ECX
] = regs
->rcx
;
884 env
->regs
[R_EDX
] = regs
->rdx
;
885 env
->regs
[R_ESI
] = regs
->rsi
;
886 env
->regs
[R_EDI
] = regs
->rdi
;
887 env
->regs
[R_EBP
] = regs
->rbp
;
888 env
->regs
[R_ESP
] = regs
->rsp
;
889 env
->eip
= regs
->rip
;
891 env
->regs
[R_EAX
] = regs
->eax
;
892 env
->regs
[R_EBX
] = regs
->ebx
;
893 env
->regs
[R_ECX
] = regs
->ecx
;
894 env
->regs
[R_EDX
] = regs
->edx
;
895 env
->regs
[R_ESI
] = regs
->esi
;
896 env
->regs
[R_EDI
] = regs
->edi
;
897 env
->regs
[R_EBP
] = regs
->ebp
;
898 env
->regs
[R_ESP
] = regs
->esp
;
899 env
->eip
= regs
->eip
;
902 /* linux interrupt setup */
904 env
->idt
.limit
= 511;
906 env
->idt
.limit
= 255;
908 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
909 PROT_READ
|PROT_WRITE
,
910 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
911 idt_table
= g2h(env
->idt
.base
);
934 /* linux segment setup */
937 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
938 PROT_READ
|PROT_WRITE
,
939 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
940 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
941 gdt_table
= g2h(env
->gdt
.base
);
943 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
944 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
945 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
947 /* 64 bit code segment */
948 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
949 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
951 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
953 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
954 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
955 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
958 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
959 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
961 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
962 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
963 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
964 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
965 /* This hack makes Wine work... */
966 env
->segs
[R_FS
].selector
= 0;
968 cpu_x86_load_seg(env
, R_DS
, 0);
969 cpu_x86_load_seg(env
, R_ES
, 0);
970 cpu_x86_load_seg(env
, R_FS
, 0);
971 cpu_x86_load_seg(env
, R_GS
, 0);
973 #elif defined(TARGET_SPARC)
977 env
->npc
= regs
->npc
;
979 for(i
= 0; i
< 8; i
++)
980 env
->gregs
[i
] = regs
->u_regs
[i
];
981 for(i
= 0; i
< 8; i
++)
982 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
985 #error unsupported target CPU
989 gdbserver_start (gdbstub_port
);
990 gdb_handlesig(env
, 0);
992 cpu_loop(env
, bsd_type
);