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"
34 #include "qemu/timer.h"
35 #include "qemu/envlist.h"
38 #if defined(CONFIG_USE_GUEST_BASE)
39 unsigned long mmap_min_addr;
40 unsigned long guest_base;
42 unsigned long reserved_va;
45 static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
46 const char *qemu_uname_release;
47 extern char **environ;
48 enum BSDType bsd_type;
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(CPUX86State *env)
71 /* These are no-ops because we are not threadsafe. */
72 static inline void cpu_exec_start(CPUArchState *env)
76 static inline void cpu_exec_end(CPUArchState *env)
80 static inline void start_exclusive(void)
84 static inline void end_exclusive(void)
92 void fork_end(int child)
95 gdbserver_fork((CPUArchState *)thread_cpu->env_ptr);
99 void cpu_list_lock(void)
103 void cpu_list_unlock(void)
108 /***********************************************************/
109 /* CPUX86 core interface */
111 uint64_t cpu_get_tsc(CPUX86State *env)
113 return cpu_get_real_ticks();
116 static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
121 e1 = (addr << 16) | (limit & 0xffff);
122 e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
129 static uint64_t *idt_table;
131 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
132 uint64_t addr, unsigned int sel)
135 e1 = (addr & 0xffff) | (sel << 16);
136 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
140 p[2] = tswap32(addr >> 32);
143 /* only dpl matters as we do only user space emulation */
144 static void set_idt(int n, unsigned int dpl)
146 set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
149 static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
150 uint32_t addr, unsigned int sel)
153 e1 = (addr & 0xffff) | (sel << 16);
154 e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
160 /* only dpl matters as we do only user space emulation */
161 static void set_idt(int n, unsigned int dpl)
163 set_gate(idt_table + n, 0, dpl, 0, 0);
167 void cpu_loop(CPUX86State *env)
171 //target_siginfo_t info;
174 trapnr = cpu_x86_exec(env);
177 /* syscall from int $0x80 */
178 if (bsd_type == target_freebsd) {
179 abi_ulong params = (abi_ulong) env->regs[R_ESP] +
181 int32_t syscall_nr = env->regs[R_EAX];
182 int32_t arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8;
184 if (syscall_nr == TARGET_FREEBSD_NR_syscall) {
185 get_user_s32(syscall_nr, params);
186 params += sizeof(int32_t);
187 } else if (syscall_nr == TARGET_FREEBSD_NR___syscall) {
188 get_user_s32(syscall_nr, params);
189 params += sizeof(int64_t);
191 get_user_s32(arg1, params);
192 params += sizeof(int32_t);
193 get_user_s32(arg2, params);
194 params += sizeof(int32_t);
195 get_user_s32(arg3, params);
196 params += sizeof(int32_t);
197 get_user_s32(arg4, params);
198 params += sizeof(int32_t);
199 get_user_s32(arg5, params);
200 params += sizeof(int32_t);
201 get_user_s32(arg6, params);
202 params += sizeof(int32_t);
203 get_user_s32(arg7, params);
204 params += sizeof(int32_t);
205 get_user_s32(arg8, params);
206 env->regs[R_EAX] = do_freebsd_syscall(env,
216 } else { //if (bsd_type == target_openbsd)
217 env->regs[R_EAX] = do_openbsd_syscall(env,
226 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
227 env->regs[R_EAX] = -env->regs[R_EAX];
230 env->eflags &= ~CC_C;
235 /* syscall from syscall instruction */
236 if (bsd_type == target_freebsd)
237 env->regs[R_EAX] = do_freebsd_syscall(env,
245 else { //if (bsd_type == target_openbsd)
246 env->regs[R_EAX] = do_openbsd_syscall(env,
255 env->eip = env->exception_next_eip;
256 if (((abi_ulong)env->regs[R_EAX]) >= (abi_ulong)(-515)) {
257 env->regs[R_EAX] = -env->regs[R_EAX];
260 env->eflags &= ~CC_C;
267 info.si_signo = SIGBUS;
269 info.si_code = TARGET_SI_KERNEL;
270 info._sifields._sigfault._addr = 0;
271 queue_signal(env, info.si_signo, &info);
274 /* XXX: potential problem if ABI32 */
275 #ifndef TARGET_X86_64
276 if (env->eflags & VM_MASK) {
277 handle_vm86_fault(env);
281 info.si_signo = SIGSEGV;
283 info.si_code = TARGET_SI_KERNEL;
284 info._sifields._sigfault._addr = 0;
285 queue_signal(env, info.si_signo, &info);
289 info.si_signo = SIGSEGV;
291 if (!(env->error_code & 1))
292 info.si_code = TARGET_SEGV_MAPERR;
294 info.si_code = TARGET_SEGV_ACCERR;
295 info._sifields._sigfault._addr = env->cr[2];
296 queue_signal(env, info.si_signo, &info);
299 #ifndef TARGET_X86_64
300 if (env->eflags & VM_MASK) {
301 handle_vm86_trap(env, trapnr);
305 /* division by zero */
306 info.si_signo = SIGFPE;
308 info.si_code = TARGET_FPE_INTDIV;
309 info._sifields._sigfault._addr = env->eip;
310 queue_signal(env, info.si_signo, &info);
315 #ifndef TARGET_X86_64
316 if (env->eflags & VM_MASK) {
317 handle_vm86_trap(env, trapnr);
321 info.si_signo = SIGTRAP;
323 if (trapnr == EXCP01_DB) {
324 info.si_code = TARGET_TRAP_BRKPT;
325 info._sifields._sigfault._addr = env->eip;
327 info.si_code = TARGET_SI_KERNEL;
328 info._sifields._sigfault._addr = 0;
330 queue_signal(env, info.si_signo, &info);
335 #ifndef TARGET_X86_64
336 if (env->eflags & VM_MASK) {
337 handle_vm86_trap(env, trapnr);
341 info.si_signo = SIGSEGV;
343 info.si_code = TARGET_SI_KERNEL;
344 info._sifields._sigfault._addr = 0;
345 queue_signal(env, info.si_signo, &info);
349 info.si_signo = SIGILL;
351 info.si_code = TARGET_ILL_ILLOPN;
352 info._sifields._sigfault._addr = env->eip;
353 queue_signal(env, info.si_signo, &info);
357 /* just indicate that signals should be handled asap */
364 sig = gdb_handlesig (env, TARGET_SIGTRAP);
369 info.si_code = TARGET_TRAP_BRKPT;
370 queue_signal(env, info.si_signo, &info);
376 pc = env->segs[R_CS].base + env->eip;
377 fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
381 process_pending_signals(env);
387 #define SPARC64_STACK_BIAS 2047
390 /* WARNING: dealing with register windows _is_ complicated. More info
391 can be found at http://www.sics.se/~psm/sparcstack.html */
392 static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
394 index = (index + cwp * 16) % (16 * env->nwindows);
395 /* wrap handling : if cwp is on the last window, then we use the
396 registers 'after' the end */
397 if (index < 8 && env->cwp == env->nwindows - 1)
398 index += 16 * env->nwindows;
402 /* save the register window 'cwp1' */
403 static inline void save_window_offset(CPUSPARCState *env, int cwp1)
408 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
409 #ifdef TARGET_SPARC64
411 sp_ptr += SPARC64_STACK_BIAS;
413 #if defined(DEBUG_WIN)
414 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx " save_cwp=%d\n",
417 for(i = 0; i < 16; i++) {
418 /* FIXME - what to do if put_user() fails? */
419 put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
420 sp_ptr += sizeof(abi_ulong);
424 static void save_window(CPUSPARCState *env)
426 #ifndef TARGET_SPARC64
427 unsigned int new_wim;
428 new_wim = ((env->wim >> 1) | (env->wim << (env->nwindows - 1))) &
429 ((1LL << env->nwindows) - 1);
430 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
433 save_window_offset(env, cpu_cwp_dec(env, env->cwp - 2));
439 static void restore_window(CPUSPARCState *env)
441 #ifndef TARGET_SPARC64
442 unsigned int new_wim;
444 unsigned int i, cwp1;
447 #ifndef TARGET_SPARC64
448 new_wim = ((env->wim << 1) | (env->wim >> (env->nwindows - 1))) &
449 ((1LL << env->nwindows) - 1);
452 /* restore the invalid window */
453 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
454 sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
455 #ifdef TARGET_SPARC64
457 sp_ptr += SPARC64_STACK_BIAS;
459 #if defined(DEBUG_WIN)
460 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx " load_cwp=%d\n",
463 for(i = 0; i < 16; i++) {
464 /* FIXME - what to do if get_user() fails? */
465 get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
466 sp_ptr += sizeof(abi_ulong);
468 #ifdef TARGET_SPARC64
470 if (env->cleanwin < env->nwindows - 1)
478 static void flush_windows(CPUSPARCState *env)
484 /* if restore would invoke restore_window(), then we can stop */
485 cwp1 = cpu_cwp_inc(env, env->cwp + offset);
486 #ifndef TARGET_SPARC64
487 if (env->wim & (1 << cwp1))
490 if (env->canrestore == 0)
495 save_window_offset(env, cwp1);
498 cwp1 = cpu_cwp_inc(env, env->cwp + 1);
499 #ifndef TARGET_SPARC64
500 /* set wim so that restore will reload the registers */
501 env->wim = 1 << cwp1;
503 #if defined(DEBUG_WIN)
504 printf("flush_windows: nb=%d\n", offset - 1);
508 void cpu_loop(CPUSPARCState *env)
510 CPUState *cs = CPU(sparc_env_get_cpu(env));
511 int trapnr, ret, syscall_nr;
512 //target_siginfo_t info;
515 trapnr = cpu_sparc_exec (env);
518 #ifndef TARGET_SPARC64
521 /* FreeBSD uses 0x141 for syscalls too */
523 if (bsd_type != target_freebsd)
527 syscall_nr = env->gregs[1];
528 if (bsd_type == target_freebsd)
529 ret = do_freebsd_syscall(env, syscall_nr,
530 env->regwptr[0], env->regwptr[1],
531 env->regwptr[2], env->regwptr[3],
532 env->regwptr[4], env->regwptr[5], 0, 0);
533 else if (bsd_type == target_netbsd)
534 ret = do_netbsd_syscall(env, syscall_nr,
535 env->regwptr[0], env->regwptr[1],
536 env->regwptr[2], env->regwptr[3],
537 env->regwptr[4], env->regwptr[5]);
538 else { //if (bsd_type == target_openbsd)
539 #if defined(TARGET_SPARC64)
540 syscall_nr &= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG |
541 TARGET_OPENBSD_SYSCALL_G2RFLAG);
543 ret = do_openbsd_syscall(env, syscall_nr,
544 env->regwptr[0], env->regwptr[1],
545 env->regwptr[2], env->regwptr[3],
546 env->regwptr[4], env->regwptr[5]);
548 if ((unsigned int)ret >= (unsigned int)(-515)) {
550 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
551 env->xcc |= PSR_CARRY;
553 env->psr |= PSR_CARRY;
556 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
557 env->xcc &= ~PSR_CARRY;
559 env->psr &= ~PSR_CARRY;
562 env->regwptr[0] = ret;
563 /* next instruction */
564 #if defined(TARGET_SPARC64)
565 if (bsd_type == target_openbsd &&
566 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG) {
567 env->pc = env->gregs[2];
568 env->npc = env->pc + 4;
569 } else if (bsd_type == target_openbsd &&
570 env->gregs[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG) {
571 env->pc = env->gregs[7];
572 env->npc = env->pc + 4;
575 env->npc = env->npc + 4;
579 env->npc = env->npc + 4;
582 case 0x83: /* flush windows */
587 /* next instruction */
589 env->npc = env->npc + 4;
591 #ifndef TARGET_SPARC64
592 case TT_WIN_OVF: /* window overflow */
595 case TT_WIN_UNF: /* window underflow */
602 info.si_signo = SIGSEGV;
604 /* XXX: check env->error_code */
605 info.si_code = TARGET_SEGV_MAPERR;
606 info._sifields._sigfault._addr = env->mmuregs[4];
607 queue_signal(env, info.si_signo, &info);
612 case TT_SPILL: /* window overflow */
615 case TT_FILL: /* window underflow */
622 info.si_signo = SIGSEGV;
624 /* XXX: check env->error_code */
625 info.si_code = TARGET_SEGV_MAPERR;
626 if (trapnr == TT_DFAULT)
627 info._sifields._sigfault._addr = env->dmmuregs[4];
629 info._sifields._sigfault._addr = env->tsptr->tpc;
630 //queue_signal(env, info.si_signo, &info);
636 /* just indicate that signals should be handled asap */
642 sig = gdb_handlesig(cs, TARGET_SIGTRAP);
648 info.si_code = TARGET_TRAP_BRKPT;
649 //queue_signal(env, info.si_signo, &info);
655 #ifdef TARGET_SPARC64
658 printf ("Unhandled trap: 0x%x\n", trapnr);
659 cpu_dump_state(cs, stderr, fprintf, 0);
662 process_pending_signals (env);
668 static void usage(void)
670 printf("qemu-" TARGET_NAME " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
671 "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
672 "BSD CPU emulator (compiled for %s emulation)\n"
674 "Standard options:\n"
675 "-h print this help\n"
676 "-g port wait gdb connection to port\n"
677 "-L path set the elf interpreter prefix (default=%s)\n"
678 "-s size set the stack size in bytes (default=%ld)\n"
679 "-cpu model select CPU (-cpu help for list)\n"
680 "-drop-ld-preload drop LD_PRELOAD for target process\n"
681 "-E var=value sets/modifies targets environment variable(s)\n"
682 "-U var unsets targets environment variable(s)\n"
683 #if defined(CONFIG_USE_GUEST_BASE)
684 "-B address set guest_base address to address\n"
686 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
689 "-d item1[,...] enable logging of specified items\n"
690 " (use '-d help' for a list of log items)\n"
691 "-D logfile write logs to 'logfile' (default stderr)\n"
692 "-p pagesize set the host page size to 'pagesize'\n"
693 "-singlestep always run in singlestep mode\n"
694 "-strace log system calls\n"
696 "Environment variables:\n"
697 "QEMU_STRACE Print system calls and arguments similar to the\n"
698 " 'strace' program. Enable by setting to any value.\n"
699 "You can use -E and -U options to set/unset environment variables\n"
700 "for target process. It is possible to provide several variables\n"
701 "by repeating the option. For example:\n"
702 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
703 "Note that if you provide several changes to single variable\n"
704 "last change will stay in effect.\n"
712 THREAD CPUState *thread_cpu;
714 /* Assumes contents are already zeroed. */
715 void init_task_state(TaskState *ts)
720 ts->first_free = ts->sigqueue_table;
721 for (i = 0; i < MAX_SIGQUEUE_SIZE - 1; i++) {
722 ts->sigqueue_table[i].next = &ts->sigqueue_table[i + 1];
724 ts->sigqueue_table[i].next = NULL;
727 int main(int argc, char **argv)
729 const char *filename;
730 const char *cpu_model;
731 const char *log_file = NULL;
732 const char *log_mask = NULL;
733 struct target_pt_regs regs1, *regs = ®s1;
734 struct image_info info1, *info = &info1;
735 TaskState ts1, *ts = &ts1;
740 int gdbstub_port = 0;
741 char **target_environ, **wrk;
742 envlist_t *envlist = NULL;
743 bsd_type = target_openbsd;
748 module_call_init(MODULE_INIT_QOM);
750 if ((envlist = envlist_create()) == NULL) {
751 (void) fprintf(stderr, "Unable to allocate envlist\n");
755 /* add current environment into the list */
756 for (wrk = environ; *wrk != NULL; wrk++) {
757 (void) envlist_setenv(envlist, *wrk);
761 #if defined(cpudef_setup)
762 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
774 if (!strcmp(r, "-")) {
776 } else if (!strcmp(r, "d")) {
777 if (optind >= argc) {
780 log_mask = argv[optind++];
781 } else if (!strcmp(r, "D")) {
782 if (optind >= argc) {
785 log_file = argv[optind++];
786 } else if (!strcmp(r, "E")) {
788 if (envlist_setenv(envlist, r) != 0)
790 } else if (!strcmp(r, "ignore-environment")) {
791 envlist_free(envlist);
792 if ((envlist = envlist_create()) == NULL) {
793 (void) fprintf(stderr, "Unable to allocate envlist\n");
796 } else if (!strcmp(r, "U")) {
798 if (envlist_unsetenv(envlist, r) != 0)
800 } else if (!strcmp(r, "s")) {
802 x86_stack_size = strtol(r, (char **)&r, 0);
803 if (x86_stack_size <= 0)
806 x86_stack_size *= 1024 * 1024;
807 else if (*r == 'k' || *r == 'K')
808 x86_stack_size *= 1024;
809 } else if (!strcmp(r, "L")) {
810 interp_prefix = argv[optind++];
811 } else if (!strcmp(r, "p")) {
812 qemu_host_page_size = atoi(argv[optind++]);
813 if (qemu_host_page_size == 0 ||
814 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
815 fprintf(stderr, "page size must be a power of two\n");
818 } else if (!strcmp(r, "g")) {
819 gdbstub_port = atoi(argv[optind++]);
820 } else if (!strcmp(r, "r")) {
821 qemu_uname_release = argv[optind++];
822 } else if (!strcmp(r, "cpu")) {
823 cpu_model = argv[optind++];
824 if (is_help_option(cpu_model)) {
825 /* XXX: implement xxx_cpu_list for targets that still miss it */
826 #if defined(cpu_list)
827 cpu_list(stdout, &fprintf);
831 #if defined(CONFIG_USE_GUEST_BASE)
832 } else if (!strcmp(r, "B")) {
833 guest_base = strtol(argv[optind++], NULL, 0);
836 } else if (!strcmp(r, "drop-ld-preload")) {
837 (void) envlist_unsetenv(envlist, "LD_PRELOAD");
838 } else if (!strcmp(r, "bsd")) {
839 if (!strcasecmp(argv[optind], "freebsd")) {
840 bsd_type = target_freebsd;
841 } else if (!strcasecmp(argv[optind], "netbsd")) {
842 bsd_type = target_netbsd;
843 } else if (!strcasecmp(argv[optind], "openbsd")) {
844 bsd_type = target_openbsd;
849 } else if (!strcmp(r, "singlestep")) {
851 } else if (!strcmp(r, "strace")) {
860 qemu_set_log_filename(log_file);
864 mask = qemu_str_to_log_mask(log_mask);
866 qemu_print_log_usage(stdout);
872 if (optind >= argc) {
875 filename = argv[optind];
878 memset(regs, 0, sizeof(struct target_pt_regs));
880 /* Zero out image_info */
881 memset(info, 0, sizeof(struct image_info));
883 /* Scan interp_prefix dir for replacement files. */
884 init_paths(interp_prefix);
886 if (cpu_model == NULL) {
887 #if defined(TARGET_I386)
889 cpu_model = "qemu64";
891 cpu_model = "qemu32";
893 #elif defined(TARGET_SPARC)
894 #ifdef TARGET_SPARC64
895 cpu_model = "TI UltraSparc II";
897 cpu_model = "Fujitsu MB86904";
904 /* NOTE: we need to init the CPU at this stage to get
905 qemu_host_page_size */
906 cpu = cpu_init(cpu_model);
908 fprintf(stderr, "Unable to find CPU definition\n");
912 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
917 if (getenv("QEMU_STRACE")) {
921 target_environ = envlist_to_environ(envlist, NULL);
922 envlist_free(envlist);
924 #if defined(CONFIG_USE_GUEST_BASE)
926 * Now that page sizes are configured in cpu_init() we can do
927 * proper page alignment for guest_base.
929 guest_base = HOST_PAGE_ALIGN(guest_base);
932 * Read in mmap_min_addr kernel parameter. This value is used
933 * When loading the ELF image to determine whether guest_base
936 * When user has explicitly set the quest base, we skip this
939 if (!have_guest_base) {
942 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
944 if (fscanf(fp, "%lu", &tmp) == 1) {
946 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
951 #endif /* CONFIG_USE_GUEST_BASE */
953 if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
954 printf("Error loading %s\n", filename);
958 for (wrk = target_environ; *wrk; wrk++) {
962 free(target_environ);
964 if (qemu_log_enabled()) {
965 #if defined(CONFIG_USE_GUEST_BASE)
966 qemu_log("guest_base 0x%lx\n", guest_base);
970 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
971 qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
972 qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n",
974 qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n",
976 qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
977 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n",
979 qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
980 qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
983 target_set_brk(info->brk);
987 #if defined(CONFIG_USE_GUEST_BASE)
988 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
989 generating the prologue until now so that the prologue can take
990 the real value of GUEST_BASE into account. */
991 tcg_prologue_init(&tcg_ctx);
994 /* build Task State */
995 memset(ts, 0, sizeof(TaskState));
1000 #if defined(TARGET_I386)
1001 env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
1002 env->hflags |= HF_PE_MASK | HF_CPL_MASK;
1003 if (env->features[FEAT_1_EDX] & CPUID_SSE) {
1004 env->cr[4] |= CR4_OSFXSR_MASK;
1005 env->hflags |= HF_OSFXSR_MASK;
1007 #ifndef TARGET_ABI32
1008 /* enable 64 bit mode if possible */
1009 if (!(env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM)) {
1010 fprintf(stderr, "The selected x86 CPU does not support 64 bit mode\n");
1013 env->cr[4] |= CR4_PAE_MASK;
1014 env->efer |= MSR_EFER_LMA | MSR_EFER_LME;
1015 env->hflags |= HF_LMA_MASK;
1018 /* flags setup : we activate the IRQs by default as in user mode */
1019 env->eflags |= IF_MASK;
1021 /* linux register setup */
1022 #ifndef TARGET_ABI32
1023 env->regs[R_EAX] = regs->rax;
1024 env->regs[R_EBX] = regs->rbx;
1025 env->regs[R_ECX] = regs->rcx;
1026 env->regs[R_EDX] = regs->rdx;
1027 env->regs[R_ESI] = regs->rsi;
1028 env->regs[R_EDI] = regs->rdi;
1029 env->regs[R_EBP] = regs->rbp;
1030 env->regs[R_ESP] = regs->rsp;
1031 env->eip = regs->rip;
1033 env->regs[R_EAX] = regs->eax;
1034 env->regs[R_EBX] = regs->ebx;
1035 env->regs[R_ECX] = regs->ecx;
1036 env->regs[R_EDX] = regs->edx;
1037 env->regs[R_ESI] = regs->esi;
1038 env->regs[R_EDI] = regs->edi;
1039 env->regs[R_EBP] = regs->ebp;
1040 env->regs[R_ESP] = regs->esp;
1041 env->eip = regs->eip;
1044 /* linux interrupt setup */
1045 #ifndef TARGET_ABI32
1046 env->idt.limit = 511;
1048 env->idt.limit = 255;
1050 env->idt.base = target_mmap(0, sizeof(uint64_t) * (env->idt.limit + 1),
1051 PROT_READ|PROT_WRITE,
1052 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1053 idt_table = g2h(env->idt.base);
1076 /* linux segment setup */
1078 uint64_t *gdt_table;
1079 env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
1080 PROT_READ|PROT_WRITE,
1081 MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
1082 env->gdt.limit = sizeof(uint64_t) * TARGET_GDT_ENTRIES - 1;
1083 gdt_table = g2h(env->gdt.base);
1085 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1086 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1087 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1089 /* 64 bit code segment */
1090 write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
1091 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1093 (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
1095 write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
1096 DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
1097 (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
1100 cpu_x86_load_seg(env, R_CS, __USER_CS);
1101 cpu_x86_load_seg(env, R_SS, __USER_DS);
1103 cpu_x86_load_seg(env, R_DS, __USER_DS);
1104 cpu_x86_load_seg(env, R_ES, __USER_DS);
1105 cpu_x86_load_seg(env, R_FS, __USER_DS);
1106 cpu_x86_load_seg(env, R_GS, __USER_DS);
1107 /* This hack makes Wine work... */
1108 env->segs[R_FS].selector = 0;
1110 cpu_x86_load_seg(env, R_DS, 0);
1111 cpu_x86_load_seg(env, R_ES, 0);
1112 cpu_x86_load_seg(env, R_FS, 0);
1113 cpu_x86_load_seg(env, R_GS, 0);
1115 #elif defined(TARGET_SPARC)
1119 env->npc = regs->npc;
1121 for(i = 0; i < 8; i++)
1122 env->gregs[i] = regs->u_regs[i];
1123 for(i = 0; i < 8; i++)
1124 env->regwptr[i] = regs->u_regs[i + 8];
1127 #error unsupported target CPU
1131 gdbserver_start (gdbstub_port);
1132 gdb_handlesig(cpu, 0);