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/>.
19 #include "qemu/osdep.h"
20 #include <machine/trap.h>
24 #include "qemu/path.h"
25 #include "qemu/help_option.h"
28 #include "exec/exec-all.h"
30 #include "qemu/timer.h"
31 #include "qemu/envlist.h"
35 unsigned long mmap_min_addr;
36 unsigned long guest_base;
38 unsigned long reserved_va;
40 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
41 const char *qemu_uname_release;
42 extern char **environ;
43 enum BSDType bsd_type;
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(CPUX86State *env)
66 /* These are no-ops because we are not threadsafe. */
67 static inline void cpu_exec_start(CPUArchState *env)
71 static inline void cpu_exec_end(CPUArchState *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_cpu);
94 void cpu_list_lock(void)
98 void cpu_list_unlock(void)
103 /***********************************************************/
104 /* CPUX86 core interface */
106 uint64_t cpu_get_tsc(CPUX86State *env)
108 return cpu_get_host_ticks();
111 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
116 e1 = (addr << 16) | (limit & 0xffff);
117 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
124 static uint64_t *idt_table;
126 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
127 uint64_t addr, unsigned int sel)
130 e1 = (addr & 0xffff) | (sel << 16);
131 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
135 p[2] = tswap32(addr >> 32);
138 /* only dpl matters as we do only user space emulation */
139 static void set_idt(int n, unsigned int dpl)
141 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
144 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
145 uint32_t addr, unsigned int sel)
148 e1 = (addr & 0xffff) | (sel << 16);
149 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
155 /* only dpl matters as we do only user space emulation */
156 static void set_idt(int n, unsigned int dpl)
158 set_gate(idt_table + n, 0, dpl, 0, 0);
162 void cpu_loop(CPUX86State *env)
164 X86CPU *cpu = x86_env_get_cpu(env);
165 CPUState *cs = CPU(cpu);
168 //target_siginfo_t info;
171 trapnr = cpu_x86_exec(cs);
174 /* syscall from int $0x80 */
175 if (bsd_type == target_freebsd) {
176 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
178 int32_t syscall_nr = env->regs[R_EAX];
179 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
181 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
182 get_user_s32(syscall_nr, params);
183 params += sizeof(int32_t);
184 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
185 get_user_s32(syscall_nr, params);
186 params += sizeof(int64_t);
188 get_user_s32(arg1, params);
189 params += sizeof(int32_t);
190 get_user_s32(arg2, params);
191 params += sizeof(int32_t);
192 get_user_s32(arg3, params);
193 params += sizeof(int32_t);
194 get_user_s32(arg4, params);
195 params += sizeof(int32_t);
196 get_user_s32(arg5, params);
197 params += sizeof(int32_t);
198 get_user_s32(arg6, params);
199 params += sizeof(int32_t);
200 get_user_s32(arg7, params);
201 params += sizeof(int32_t);
202 get_user_s32(arg8, params);
203 env->regs[R_EAX] = do_freebsd_syscall(env,
213 } else { //if (bsd_type == target_openbsd)
214 env->regs[R_EAX] = do_openbsd_syscall(env,
223 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
224 env->regs[R_EAX] = -env->regs[R_EAX];
227 env->eflags &= ~CC_C;
232 /* syscall from syscall instruction */
233 if (bsd_type == target_freebsd)
234 env->regs[R_EAX] = do_freebsd_syscall(env,
242 else { //if (bsd_type == target_openbsd)
243 env->regs[R_EAX] = do_openbsd_syscall(env,
252 env->eip = env->exception_next_eip;
253 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
254 env->regs[R_EAX] = -env->regs[R_EAX];
257 env->eflags &= ~CC_C;
264 info.si_signo = SIGBUS;
266 info.si_code = TARGET_SI_KERNEL;
267 info._sifields._sigfault._addr = 0;
268 queue_signal(env, info.si_signo, &info);
271 /* XXX: potential problem if ABI32 */
272 #ifndef TARGET_X86_64
273 if (env->eflags & VM_MASK) {
274 handle_vm86_fault(env);
278 info.si_signo = SIGSEGV;
280 info.si_code = TARGET_SI_KERNEL;
281 info._sifields._sigfault._addr = 0;
282 queue_signal(env, info.si_signo, &info);
286 info.si_signo = SIGSEGV;
288 if (!(env->error_code & 1))
289 info.si_code = TARGET_SEGV_MAPERR;
291 info.si_code = TARGET_SEGV_ACCERR;
292 info._sifields._sigfault._addr = env->cr[2];
293 queue_signal(env, info.si_signo, &info);
296 #ifndef TARGET_X86_64
297 if (env->eflags & VM_MASK) {
298 handle_vm86_trap(env, trapnr);
302 /* division by zero */
303 info.si_signo = SIGFPE;
305 info.si_code = TARGET_FPE_INTDIV;
306 info._sifields._sigfault._addr = env->eip;
307 queue_signal(env, info.si_signo, &info);
312 #ifndef TARGET_X86_64
313 if (env->eflags & VM_MASK) {
314 handle_vm86_trap(env, trapnr);
318 info.si_signo = SIGTRAP;
320 if (trapnr == EXCP01_DB) {
321 info.si_code = TARGET_TRAP_BRKPT;
322 info._sifields._sigfault._addr = env->eip;
324 info.si_code = TARGET_SI_KERNEL;
325 info._sifields._sigfault._addr = 0;
327 queue_signal(env, info.si_signo, &info);
332 #ifndef TARGET_X86_64
333 if (env->eflags & VM_MASK) {
334 handle_vm86_trap(env, trapnr);
338 info.si_signo = SIGSEGV;
340 info.si_code = TARGET_SI_KERNEL;
341 info._sifields._sigfault._addr = 0;
342 queue_signal(env, info.si_signo, &info);
346 info.si_signo = SIGILL;
348 info.si_code = TARGET_ILL_ILLOPN;
349 info._sifields._sigfault._addr = env->eip;
350 queue_signal(env, info.si_signo, &info);
354 /* just indicate that signals should be handled asap */
361 sig = gdb_handlesig (env, TARGET_SIGTRAP);
366 info.si_code = TARGET_TRAP_BRKPT;
367 queue_signal(env, info.si_signo, &info);
373 pc = env->segs[R_CS].base + env->eip;
374 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
378 process_pending_signals(env);
384 #define SPARC64_STACK_BIAS 2047
387 /* WARNING: dealing with register windows _is_ complicated. More info
388 can be found at http://www.sics.se/~psm/sparcstack.html */
389 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
391 index = (index + cwp * 16) % (16 * env->nwindows);
392 /* wrap handling : if cwp is on the last window, then we use the
393 registers 'after' the end */
394 if (index < 8 && env->cwp == env->nwindows - 1)
395 index += 16 * env->nwindows;
399 /* save the register window 'cwp1' */
400 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
405 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
406 #ifdef TARGET_SPARC64
408 sp_ptr += SPARC64_STACK_BIAS;
410 #if defined(DEBUG_WIN)
411 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
414 for(i = 0; i < 16; i++) {
415 /* FIXME - what to do if put_user() fails? */
416 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
417 sp_ptr += sizeof(abi_ulong);
421 static void save_window(CPUSPARCState *env)
423 #ifndef TARGET_SPARC64
424 unsigned int new_wim;
425 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
426 ((1LL << env->nwindows) - 1);
427 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
430 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
436 static void restore_window(CPUSPARCState *env)
438 #ifndef TARGET_SPARC64
439 unsigned int new_wim;
441 unsigned int i, cwp1;
444 #ifndef TARGET_SPARC64
445 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
446 ((1LL << env->nwindows) - 1);
449 /* restore the invalid window */
450 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
451 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
452 #ifdef TARGET_SPARC64
454 sp_ptr += SPARC64_STACK_BIAS;
456 #if defined(DEBUG_WIN)
457 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
460 for(i = 0; i < 16; i++) {
461 /* FIXME - what to do if get_user() fails? */
462 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
463 sp_ptr += sizeof(abi_ulong);
465 #ifdef TARGET_SPARC64
467 if (env->cleanwin < env->nwindows - 1)
475 static void flush_windows(CPUSPARCState *env)
481 /* if restore would invoke restore_window(), then we can stop */
482 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
483 #ifndef TARGET_SPARC64
484 if (env->wim & (1 << cwp1))
487 if (env->canrestore == 0)
492 save_window_offset(env, cwp1);
495 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
496 #ifndef TARGET_SPARC64
497 /* set wim so that restore will reload the registers */
498 env->wim = 1 << cwp1;
500 #if defined(DEBUG_WIN)
501 printf("flush_windows: nb=%d\n", offset - 1);
505 void cpu_loop(CPUSPARCState *env)
507 CPUState *cs = CPU(sparc_env_get_cpu(env));
508 int trapnr, ret, syscall_nr;
509 //target_siginfo_t info;
512 trapnr = cpu_sparc_exec(cs);
515 #ifndef TARGET_SPARC64
518 /* FreeBSD uses 0x141 for syscalls too */
520 if (bsd_type != target_freebsd)
524 syscall_nr = env->gregs[1];
525 if (bsd_type == target_freebsd)
526 ret = do_freebsd_syscall(env, syscall_nr,
527 env->regwptr[0], env->regwptr[1],
528 env->regwptr[2], env->regwptr[3],
529 env->regwptr[4], env->regwptr[5], 0, 0);
530 else if (bsd_type == target_netbsd)
531 ret = do_netbsd_syscall(env, syscall_nr,
532 env->regwptr[0], env->regwptr[1],
533 env->regwptr[2], env->regwptr[3],
534 env->regwptr[4], env->regwptr[5]);
535 else { //if (bsd_type == target_openbsd)
536 #if defined(TARGET_SPARC64)
537 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
538 TARGET_OPENBSD_SYSCALL_G2RFLAG);
540 ret = do_openbsd_syscall(env, syscall_nr,
541 env->regwptr[0], env->regwptr[1],
542 env->regwptr[2], env->regwptr[3],
543 env->regwptr[4], env->regwptr[5]);
545 if ((unsigned int)ret >= (unsigned int)(-515)) {
547 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
548 env->xcc |= PSR_CARRY;
550 env->psr |= PSR_CARRY;
553 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
554 env->xcc &= ~PSR_CARRY;
556 env->psr &= ~PSR_CARRY;
559 env->regwptr[0] = ret;
560 /* next instruction */
561 #if defined(TARGET_SPARC64)
562 if (bsd_type == target_openbsd &&
563 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
564 env->pc = env->gregs[2];
565 env->npc = env->pc + 4;
566 } else if (bsd_type == target_openbsd &&
567 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
568 env->pc = env->gregs[7];
569 env->npc = env->pc + 4;
572 env->npc = env->npc + 4;
576 env->npc = env->npc + 4;
579 case 0x83: /* flush windows */
584 /* next instruction */
586 env->npc = env->npc + 4;
588 #ifndef TARGET_SPARC64
589 case TT_WIN_OVF: /* window overflow */
592 case TT_WIN_UNF: /* window underflow */
599 info.si_signo = SIGSEGV;
601 /* XXX: check env->error_code */
602 info.si_code = TARGET_SEGV_MAPERR;
603 info._sifields._sigfault._addr = env->mmuregs[4];
604 queue_signal(env, info.si_signo, &info);
609 case TT_SPILL: /* window overflow */
612 case TT_FILL: /* window underflow */
619 info.si_signo = SIGSEGV;
621 /* XXX: check env->error_code */
622 info.si_code = TARGET_SEGV_MAPERR;
623 if (trapnr == TT_DFAULT)
624 info._sifields._sigfault._addr = env->dmmuregs[4];
626 info._sifields._sigfault._addr = env->tsptr->tpc;
627 //queue_signal(env, info.si_signo, &info);
633 /* just indicate that signals should be handled asap */
639 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
645 info.si_code = TARGET_TRAP_BRKPT;
646 //queue_signal(env, info.si_signo, &info);
652 #ifdef TARGET_SPARC64
655 printf ("Unhandled trap: 0x%x\n", trapnr);
656 cpu_dump_state(cs, stderr, fprintf, 0);
659 process_pending_signals (env);
665 static void usage(void)
667 printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
668 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
669 "BSD CPU emulator (compiled for %s emulation)\n"
671 "Standard options:\n"
672 "-h print this help\n"
673 "-g port wait gdb connection to port\n"
674 "-L path set the elf interpreter prefix (default=%s)\n"
675 "-s size set the stack size in bytes (default=%ld)\n"
676 "-cpu model select CPU (-cpu help for list)\n"
677 "-drop-ld-preload drop LD_PRELOAD for target process\n"
678 "-E var=value sets/modifies targets environment variable(s)\n"
679 "-U var unsets targets environment variable(s)\n"
680 "-B address set guest_base address to address\n"
681 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
684 "-d item1[,...] enable logging of specified items\n"
685 " (use '-d help' for a list of log items)\n"
686 "-D logfile write logs to 'logfile' (default stderr)\n"
687 "-p pagesize set the host page size to 'pagesize'\n"
688 "-singlestep always run in singlestep mode\n"
689 "-strace log system calls\n"
691 "Environment variables:\n"
692 "QEMU_STRACE Print system calls and arguments similar to the\n"
693 " 'strace' program. Enable by setting to any value.\n"
694 "You can use -E and -U options to set/unset environment variables\n"
695 "for target process. It is possible to provide several variables\n"
696 "by repeating the option. For example:\n"
697 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
698 "Note that if you provide several changes to single variable\n"
699 "last change will stay in effect.\n"
707 THREAD CPUState *thread_cpu;
709 /* Assumes contents are already zeroed. */
710 void init_task_state(TaskState *ts)
715 ts->first_free = ts->sigqueue_table;
716 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
717 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
719 ts->sigqueue_table[i].next = NULL;
722 int main(int argc, char **argv)
724 const char *filename;
725 const char *cpu_model;
726 const char *log_file = NULL;
727 const char *log_mask = NULL;
728 struct target_pt_regs regs1, *regs = ®s1;
729 struct image_info info1, *info = &info1;
730 TaskState ts1, *ts = &ts1;
735 int gdbstub_port = 0;
736 char **target_environ, **wrk;
737 envlist_t *envlist = NULL;
738 bsd_type = target_openbsd;
743 module_call_init(MODULE_INIT_QOM);
745 if ((envlist = envlist_create()) == NULL) {
746 (void) fprintf(stderr, "Unable to allocate envlist\n");
750 /* add current environment into the list */
751 for (wrk = environ; *wrk != NULL; wrk++) {
752 (void) envlist_setenv(envlist, *wrk);
766 if (!strcmp(r, "-")) {
768 } else if (!strcmp(r, "d")) {
769 if (optind >= argc) {
772 log_mask = argv[optind++];
773 } else if (!strcmp(r, "D")) {
774 if (optind >= argc) {
777 log_file = argv[optind++];
778 } else if (!strcmp(r, "E")) {
780 if (envlist_setenv(envlist, r) != 0)
782 } else if (!strcmp(r, "ignore-environment")) {
783 envlist_free(envlist);
784 if ((envlist = envlist_create()) == NULL) {
785 (void) fprintf(stderr, "Unable to allocate envlist\n");
788 } else if (!strcmp(r, "U")) {
790 if (envlist_unsetenv(envlist, r) != 0)
792 } else if (!strcmp(r, "s")) {
794 x86_stack_size = strtol(r, (char **)&r, 0);
795 if (x86_stack_size <= 0)
798 x86_stack_size *= 1024 * 1024;
799 else if (*r == 'k' || *r == 'K')
800 x86_stack_size *= 1024;
801 } else if (!strcmp(r, "L")) {
802 interp_prefix = argv[optind++];
803 } else if (!strcmp(r, "p")) {
804 qemu_host_page_size = atoi(argv[optind++]);
805 if (qemu_host_page_size == 0 ||
806 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
807 fprintf(stderr, "page size must be a power of two\n");
810 } else if (!strcmp(r, "g")) {
811 gdbstub_port = atoi(argv[optind++]);
812 } else if (!strcmp(r, "r")) {
813 qemu_uname_release = argv[optind++];
814 } else if (!strcmp(r, "cpu")) {
815 cpu_model = argv[optind++];
816 if (is_help_option(cpu_model)) {
817 /* XXX: implement xxx_cpu_list for targets that still miss it */
818 #if defined(cpu_list)
819 cpu_list(stdout, &fprintf);
823 } else if (!strcmp(r, "B")) {
824 guest_base = strtol(argv[optind++], NULL, 0);
826 } else if (!strcmp(r, "drop-ld-preload")) {
827 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
828 } else if (!strcmp(r, "bsd")) {
829 if (!strcasecmp(argv[optind], "freebsd")) {
830 bsd_type = target_freebsd;
831 } else if (!strcasecmp(argv[optind], "netbsd")) {
832 bsd_type = target_netbsd;
833 } else if (!strcasecmp(argv[optind], "openbsd")) {
834 bsd_type = target_openbsd;
839 } else if (!strcmp(r, "singlestep")) {
841 } else if (!strcmp(r, "strace")) {
850 qemu_log_needs_buffers();
851 qemu_set_log_filename(log_file);
855 mask = qemu_str_to_log_mask(log_mask);
857 qemu_print_log_usage(stdout);
863 if (optind >= argc) {
866 filename = argv[optind];
869 memset(regs, 0, sizeof(struct target_pt_regs));
871 /* Zero out image_info */
872 memset(info, 0, sizeof(struct image_info));
874 /* Scan interp_prefix dir for replacement files. */
875 init_paths(interp_prefix);
877 if (cpu_model == NULL) {
878 #if defined(TARGET_I386)
880 cpu_model = "qemu64";
882 cpu_model = "qemu32";
884 #elif defined(TARGET_SPARC)
885 #ifdef TARGET_SPARC64
886 cpu_model = "TI UltraSparc II";
888 cpu_model = "Fujitsu MB86904";
895 /* NOTE: we need to init the CPU at this stage to get
896 qemu_host_page_size */
897 cpu = cpu_init(cpu_model);
899 fprintf(stderr, "Unable to find CPU definition\n");
903 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
908 if (getenv("QEMU_STRACE")) {
912 target_environ = envlist_to_environ(envlist, NULL);
913 envlist_free(envlist);
916 * Now that page sizes are configured in cpu_init() we can do
917 * proper page alignment for guest_base.
919 guest_base = HOST_PAGE_ALIGN(guest_base);
922 * Read in mmap_min_addr kernel parameter. This value is used
923 * When loading the ELF image to determine whether guest_base
926 * When user has explicitly set the quest base, we skip this
929 if (!have_guest_base) {
932 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
934 if (fscanf(fp, "%lu", &tmp) == 1) {
936 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", mmap_min_addr);
942 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
943 printf("Error loading %s\n", filename);
947 for (wrk = target_environ; *wrk; wrk++) {
951 free(target_environ);
953 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
954 qemu_log("guest_base 0x%lx\n", guest_base);
957 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
958 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
959 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
961 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
963 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
964 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
966 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
967 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
970 target_set_brk(info->brk);
974 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
975 generating the prologue until now so that the prologue can take
976 the real value of GUEST_BASE into account. */
977 tcg_prologue_init(&tcg_ctx);
979 /* build Task State */
980 memset(ts, 0, sizeof(TaskState));
985 #if defined(TARGET_I386)
986 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
987 env->hflags |= HF_PE_MASK | HF_CPL_MASK;
988 if (env->features[FEAT_1_EDX] & CPUID_SSE) {
989 env->cr[4] |= CR4_OSFXSR_MASK;
990 env->hflags |= HF_OSFXSR_MASK;
993 /* enable 64 bit mode if possible */
994 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
995 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
998 env->cr[4] |= CR4_PAE_MASK;
999 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1000 env->hflags |= HF_LMA_MASK;
1003 /* flags setup : we activate the IRQs by default as in user mode */
1004 env->eflags |= IF_MASK;
1006 /* linux register setup */
1007 #ifndef TARGET_ABI32
1008 env->regs[R_EAX] = regs->rax;
1009 env->regs[R_EBX] = regs->rbx;
1010 env->regs[R_ECX] = regs->rcx;
1011 env->regs[R_EDX] = regs->rdx;
1012 env->regs[R_ESI] = regs->rsi;
1013 env->regs[R_EDI] = regs->rdi;
1014 env->regs[R_EBP] = regs->rbp;
1015 env->regs[R_ESP] = regs->rsp;
1016 env->eip = regs->rip;
1018 env->regs[R_EAX] = regs->eax;
1019 env->regs[R_EBX] = regs->ebx;
1020 env->regs[R_ECX] = regs->ecx;
1021 env->regs[R_EDX] = regs->edx;
1022 env->regs[R_ESI] = regs->esi;
1023 env->regs[R_EDI] = regs->edi;
1024 env->regs[R_EBP] = regs->ebp;
1025 env->regs[R_ESP] = regs->esp;
1026 env->eip = regs->eip;
1029 /* linux interrupt setup */
1030 #ifndef TARGET_ABI32
1031 env->idt.limit = 511;
1033 env->idt.limit = 255;
1035 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1036 PROT_READ|PROT_WRITE,
1037 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1038 idt_table = g2h(env->idt.base);
1061 /* linux segment setup */
1063 uint64_t *gdt_table;
1064 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1065 PROT_READ|PROT_WRITE,
1066 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1067 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1068 gdt_table = g2h(env->gdt.base);
1070 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1071 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1072 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1074 /* 64 bit code segment */
1075 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1076 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1078 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1080 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1081 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1082 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1085 cpu_x86_load_seg(env, R_CS, __USER_CS);
1086 cpu_x86_load_seg(env, R_SS, __USER_DS);
1088 cpu_x86_load_seg(env, R_DS, __USER_DS);
1089 cpu_x86_load_seg(env, R_ES, __USER_DS);
1090 cpu_x86_load_seg(env, R_FS, __USER_DS);
1091 cpu_x86_load_seg(env, R_GS, __USER_DS);
1092 /* This hack makes Wine work... */
1093 env->segs[R_FS].selector = 0;
1095 cpu_x86_load_seg(env, R_DS, 0);
1096 cpu_x86_load_seg(env, R_ES, 0);
1097 cpu_x86_load_seg(env, R_FS, 0);
1098 cpu_x86_load_seg(env, R_GS, 0);
1100 #elif defined(TARGET_SPARC)
1104 env->npc = regs->npc;
1106 for(i = 0; i < 8; i++)
1107 env->gregs[i] = regs->u_regs[i];
1108 for(i = 0; i < 8; i++)
1109 env->regwptr[i] = regs->u_regs[i + 8];
1112 #error unsupported target CPU
1116 gdbserver_start (gdbstub_port);
1117 gdb_handlesig(cpu, 0);