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"
39 unsigned long mmap_min_addr
;
40 unsigned long guest_base
;
42 unsigned long reserved_va
;
44 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
45 const char *qemu_uname_release
;
46 extern char **environ
;
47 enum BSDType bsd_type
;
49 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
50 we allocate a bigger stack. Need a better solution, for example
51 by remapping the process stack directly at the right place */
52 unsigned long x86_stack_size
= 512 * 1024;
54 void gemu_log(const char *fmt
, ...)
59 vfprintf(stderr
, fmt
, ap
);
63 #if defined(TARGET_I386)
64 int cpu_get_pic_interrupt(CPUX86State
*env
)
70 /* These are no-ops because we are not threadsafe. */
71 static inline void cpu_exec_start(CPUArchState
*env
)
75 static inline void cpu_exec_end(CPUArchState
*env
)
79 static inline void start_exclusive(void)
83 static inline void end_exclusive(void)
91 void fork_end(int child
)
94 gdbserver_fork(thread_cpu
);
98 void cpu_list_lock(void)
102 void cpu_list_unlock(void)
107 /***********************************************************/
108 /* CPUX86 core interface */
110 uint64_t cpu_get_tsc(CPUX86State
*env
)
112 return cpu_get_host_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
)
168 X86CPU
*cpu
= x86_env_get_cpu(env
);
169 CPUState
*cs
= CPU(cpu
);
172 //target_siginfo_t info;
175 trapnr
= cpu_x86_exec(cs
);
178 /* syscall from int $0x80 */
179 if (bsd_type
== target_freebsd
) {
180 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
182 int32_t syscall_nr
= env
->regs
[R_EAX
];
183 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
185 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
186 get_user_s32(syscall_nr
, params
);
187 params
+= sizeof(int32_t);
188 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
189 get_user_s32(syscall_nr
, params
);
190 params
+= sizeof(int64_t);
192 get_user_s32(arg1
, params
);
193 params
+= sizeof(int32_t);
194 get_user_s32(arg2
, params
);
195 params
+= sizeof(int32_t);
196 get_user_s32(arg3
, params
);
197 params
+= sizeof(int32_t);
198 get_user_s32(arg4
, params
);
199 params
+= sizeof(int32_t);
200 get_user_s32(arg5
, params
);
201 params
+= sizeof(int32_t);
202 get_user_s32(arg6
, params
);
203 params
+= sizeof(int32_t);
204 get_user_s32(arg7
, params
);
205 params
+= sizeof(int32_t);
206 get_user_s32(arg8
, params
);
207 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
217 } else { //if (bsd_type == target_openbsd)
218 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
227 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
228 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
231 env
->eflags
&= ~CC_C
;
236 /* syscall from syscall instruction */
237 if (bsd_type
== target_freebsd
)
238 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
246 else { //if (bsd_type == target_openbsd)
247 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
256 env
->eip
= env
->exception_next_eip
;
257 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
258 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
261 env
->eflags
&= ~CC_C
;
268 info
.si_signo
= SIGBUS
;
270 info
.si_code
= TARGET_SI_KERNEL
;
271 info
._sifields
._sigfault
._addr
= 0;
272 queue_signal(env
, info
.si_signo
, &info
);
275 /* XXX: potential problem if ABI32 */
276 #ifndef TARGET_X86_64
277 if (env
->eflags
& VM_MASK
) {
278 handle_vm86_fault(env
);
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
= SIGSEGV
;
292 if (!(env
->error_code
& 1))
293 info
.si_code
= TARGET_SEGV_MAPERR
;
295 info
.si_code
= TARGET_SEGV_ACCERR
;
296 info
._sifields
._sigfault
._addr
= env
->cr
[2];
297 queue_signal(env
, info
.si_signo
, &info
);
300 #ifndef TARGET_X86_64
301 if (env
->eflags
& VM_MASK
) {
302 handle_vm86_trap(env
, trapnr
);
306 /* division by zero */
307 info
.si_signo
= SIGFPE
;
309 info
.si_code
= TARGET_FPE_INTDIV
;
310 info
._sifields
._sigfault
._addr
= env
->eip
;
311 queue_signal(env
, info
.si_signo
, &info
);
316 #ifndef TARGET_X86_64
317 if (env
->eflags
& VM_MASK
) {
318 handle_vm86_trap(env
, trapnr
);
322 info
.si_signo
= SIGTRAP
;
324 if (trapnr
== EXCP01_DB
) {
325 info
.si_code
= TARGET_TRAP_BRKPT
;
326 info
._sifields
._sigfault
._addr
= env
->eip
;
328 info
.si_code
= TARGET_SI_KERNEL
;
329 info
._sifields
._sigfault
._addr
= 0;
331 queue_signal(env
, info
.si_signo
, &info
);
336 #ifndef TARGET_X86_64
337 if (env
->eflags
& VM_MASK
) {
338 handle_vm86_trap(env
, trapnr
);
342 info
.si_signo
= SIGSEGV
;
344 info
.si_code
= TARGET_SI_KERNEL
;
345 info
._sifields
._sigfault
._addr
= 0;
346 queue_signal(env
, info
.si_signo
, &info
);
350 info
.si_signo
= SIGILL
;
352 info
.si_code
= TARGET_ILL_ILLOPN
;
353 info
._sifields
._sigfault
._addr
= env
->eip
;
354 queue_signal(env
, info
.si_signo
, &info
);
358 /* just indicate that signals should be handled asap */
365 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
370 info
.si_code
= TARGET_TRAP_BRKPT
;
371 queue_signal(env
, info
.si_signo
, &info
);
377 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
378 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
382 process_pending_signals(env
);
388 #define SPARC64_STACK_BIAS 2047
391 /* WARNING: dealing with register windows _is_ complicated. More info
392 can be found at http://www.sics.se/~psm/sparcstack.html */
393 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
395 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
396 /* wrap handling : if cwp is on the last window, then we use the
397 registers 'after' the end */
398 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
399 index
+= 16 * env
->nwindows
;
403 /* save the register window 'cwp1' */
404 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
409 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
410 #ifdef TARGET_SPARC64
412 sp_ptr
+= SPARC64_STACK_BIAS
;
414 #if defined(DEBUG_WIN)
415 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
418 for(i
= 0; i
< 16; i
++) {
419 /* FIXME - what to do if put_user() fails? */
420 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
421 sp_ptr
+= sizeof(abi_ulong
);
425 static void save_window(CPUSPARCState
*env
)
427 #ifndef TARGET_SPARC64
428 unsigned int new_wim
;
429 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
430 ((1LL << env
->nwindows
) - 1);
431 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
434 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
440 static void restore_window(CPUSPARCState
*env
)
442 #ifndef TARGET_SPARC64
443 unsigned int new_wim
;
445 unsigned int i
, cwp1
;
448 #ifndef TARGET_SPARC64
449 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
450 ((1LL << env
->nwindows
) - 1);
453 /* restore the invalid window */
454 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
455 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
456 #ifdef TARGET_SPARC64
458 sp_ptr
+= SPARC64_STACK_BIAS
;
460 #if defined(DEBUG_WIN)
461 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
464 for(i
= 0; i
< 16; i
++) {
465 /* FIXME - what to do if get_user() fails? */
466 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
467 sp_ptr
+= sizeof(abi_ulong
);
469 #ifdef TARGET_SPARC64
471 if (env
->cleanwin
< env
->nwindows
- 1)
479 static void flush_windows(CPUSPARCState
*env
)
485 /* if restore would invoke restore_window(), then we can stop */
486 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
487 #ifndef TARGET_SPARC64
488 if (env
->wim
& (1 << cwp1
))
491 if (env
->canrestore
== 0)
496 save_window_offset(env
, cwp1
);
499 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
500 #ifndef TARGET_SPARC64
501 /* set wim so that restore will reload the registers */
502 env
->wim
= 1 << cwp1
;
504 #if defined(DEBUG_WIN)
505 printf("flush_windows: nb=%d\n", offset
- 1);
509 void cpu_loop(CPUSPARCState
*env
)
511 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
512 int trapnr
, ret
, syscall_nr
;
513 //target_siginfo_t info;
516 trapnr
= cpu_sparc_exec(cs
);
519 #ifndef TARGET_SPARC64
522 /* FreeBSD uses 0x141 for syscalls too */
524 if (bsd_type
!= target_freebsd
)
528 syscall_nr
= env
->gregs
[1];
529 if (bsd_type
== target_freebsd
)
530 ret
= do_freebsd_syscall(env
, syscall_nr
,
531 env
->regwptr
[0], env
->regwptr
[1],
532 env
->regwptr
[2], env
->regwptr
[3],
533 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
534 else if (bsd_type
== target_netbsd
)
535 ret
= do_netbsd_syscall(env
, syscall_nr
,
536 env
->regwptr
[0], env
->regwptr
[1],
537 env
->regwptr
[2], env
->regwptr
[3],
538 env
->regwptr
[4], env
->regwptr
[5]);
539 else { //if (bsd_type == target_openbsd)
540 #if defined(TARGET_SPARC64)
541 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
542 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
544 ret
= do_openbsd_syscall(env
, syscall_nr
,
545 env
->regwptr
[0], env
->regwptr
[1],
546 env
->regwptr
[2], env
->regwptr
[3],
547 env
->regwptr
[4], env
->regwptr
[5]);
549 if ((unsigned int)ret
>= (unsigned int)(-515)) {
551 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
552 env
->xcc
|= PSR_CARRY
;
554 env
->psr
|= PSR_CARRY
;
557 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
558 env
->xcc
&= ~PSR_CARRY
;
560 env
->psr
&= ~PSR_CARRY
;
563 env
->regwptr
[0] = ret
;
564 /* next instruction */
565 #if defined(TARGET_SPARC64)
566 if (bsd_type
== target_openbsd
&&
567 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
568 env
->pc
= env
->gregs
[2];
569 env
->npc
= env
->pc
+ 4;
570 } else if (bsd_type
== target_openbsd
&&
571 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
572 env
->pc
= env
->gregs
[7];
573 env
->npc
= env
->pc
+ 4;
576 env
->npc
= env
->npc
+ 4;
580 env
->npc
= env
->npc
+ 4;
583 case 0x83: /* flush windows */
588 /* next instruction */
590 env
->npc
= env
->npc
+ 4;
592 #ifndef TARGET_SPARC64
593 case TT_WIN_OVF
: /* window overflow */
596 case TT_WIN_UNF
: /* window underflow */
603 info
.si_signo
= SIGSEGV
;
605 /* XXX: check env->error_code */
606 info
.si_code
= TARGET_SEGV_MAPERR
;
607 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
608 queue_signal(env
, info
.si_signo
, &info
);
613 case TT_SPILL
: /* window overflow */
616 case TT_FILL
: /* window underflow */
623 info
.si_signo
= SIGSEGV
;
625 /* XXX: check env->error_code */
626 info
.si_code
= TARGET_SEGV_MAPERR
;
627 if (trapnr
== TT_DFAULT
)
628 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
630 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
631 //queue_signal(env, info.si_signo, &info);
637 /* just indicate that signals should be handled asap */
643 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
649 info
.si_code
= TARGET_TRAP_BRKPT
;
650 //queue_signal(env, info.si_signo, &info);
656 #ifdef TARGET_SPARC64
659 printf ("Unhandled trap: 0x%x\n", trapnr
);
660 cpu_dump_state(cs
, stderr
, fprintf
, 0);
663 process_pending_signals (env
);
669 static void usage(void)
671 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
672 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
673 "BSD CPU emulator (compiled for %s emulation)\n"
675 "Standard options:\n"
676 "-h print this help\n"
677 "-g port wait gdb connection to port\n"
678 "-L path set the elf interpreter prefix (default=%s)\n"
679 "-s size set the stack size in bytes (default=%ld)\n"
680 "-cpu model select CPU (-cpu help for list)\n"
681 "-drop-ld-preload drop LD_PRELOAD for target process\n"
682 "-E var=value sets/modifies targets environment variable(s)\n"
683 "-U var unsets targets environment variable(s)\n"
684 "-B address set guest_base address to address\n"
685 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
688 "-d item1[,...] enable logging of specified items\n"
689 " (use '-d help' for a list of log items)\n"
690 "-D logfile write logs to 'logfile' (default stderr)\n"
691 "-p pagesize set the host page size to 'pagesize'\n"
692 "-singlestep always run in singlestep mode\n"
693 "-strace log system calls\n"
695 "Environment variables:\n"
696 "QEMU_STRACE Print system calls and arguments similar to the\n"
697 " 'strace' program. Enable by setting to any value.\n"
698 "You can use -E and -U options to set/unset environment variables\n"
699 "for target process. It is possible to provide several variables\n"
700 "by repeating the option. For example:\n"
701 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
702 "Note that if you provide several changes to single variable\n"
703 "last change will stay in effect.\n"
711 THREAD CPUState
*thread_cpu
;
713 /* Assumes contents are already zeroed. */
714 void init_task_state(TaskState
*ts
)
719 ts
->first_free
= ts
->sigqueue_table
;
720 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
721 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
723 ts
->sigqueue_table
[i
].next
= NULL
;
726 int main(int argc
, char **argv
)
728 const char *filename
;
729 const char *cpu_model
;
730 const char *log_file
= NULL
;
731 const char *log_mask
= NULL
;
732 struct target_pt_regs regs1
, *regs
= ®s1
;
733 struct image_info info1
, *info
= &info1
;
734 TaskState ts1
, *ts
= &ts1
;
739 int gdbstub_port
= 0;
740 char **target_environ
, **wrk
;
741 envlist_t
*envlist
= NULL
;
742 bsd_type
= target_openbsd
;
747 module_call_init(MODULE_INIT_QOM
);
749 if ((envlist
= envlist_create()) == NULL
) {
750 (void) fprintf(stderr
, "Unable to allocate envlist\n");
754 /* add current environment into the list */
755 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
756 (void) envlist_setenv(envlist
, *wrk
);
760 #if defined(cpudef_setup)
761 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
773 if (!strcmp(r
, "-")) {
775 } else if (!strcmp(r
, "d")) {
776 if (optind
>= argc
) {
779 log_mask
= argv
[optind
++];
780 } else if (!strcmp(r
, "D")) {
781 if (optind
>= argc
) {
784 log_file
= argv
[optind
++];
785 } else if (!strcmp(r
, "E")) {
787 if (envlist_setenv(envlist
, r
) != 0)
789 } else if (!strcmp(r
, "ignore-environment")) {
790 envlist_free(envlist
);
791 if ((envlist
= envlist_create()) == NULL
) {
792 (void) fprintf(stderr
, "Unable to allocate envlist\n");
795 } else if (!strcmp(r
, "U")) {
797 if (envlist_unsetenv(envlist
, r
) != 0)
799 } else if (!strcmp(r
, "s")) {
801 x86_stack_size
= strtol(r
, (char **)&r
, 0);
802 if (x86_stack_size
<= 0)
805 x86_stack_size
*= 1024 * 1024;
806 else if (*r
== 'k' || *r
== 'K')
807 x86_stack_size
*= 1024;
808 } else if (!strcmp(r
, "L")) {
809 interp_prefix
= argv
[optind
++];
810 } else if (!strcmp(r
, "p")) {
811 qemu_host_page_size
= atoi(argv
[optind
++]);
812 if (qemu_host_page_size
== 0 ||
813 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
814 fprintf(stderr
, "page size must be a power of two\n");
817 } else if (!strcmp(r
, "g")) {
818 gdbstub_port
= atoi(argv
[optind
++]);
819 } else if (!strcmp(r
, "r")) {
820 qemu_uname_release
= argv
[optind
++];
821 } else if (!strcmp(r
, "cpu")) {
822 cpu_model
= argv
[optind
++];
823 if (is_help_option(cpu_model
)) {
824 /* XXX: implement xxx_cpu_list for targets that still miss it */
825 #if defined(cpu_list)
826 cpu_list(stdout
, &fprintf
);
830 } else if (!strcmp(r
, "B")) {
831 guest_base
= strtol(argv
[optind
++], NULL
, 0);
833 } else if (!strcmp(r
, "drop-ld-preload")) {
834 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
835 } else if (!strcmp(r
, "bsd")) {
836 if (!strcasecmp(argv
[optind
], "freebsd")) {
837 bsd_type
= target_freebsd
;
838 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
839 bsd_type
= target_netbsd
;
840 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
841 bsd_type
= target_openbsd
;
846 } else if (!strcmp(r
, "singlestep")) {
848 } else if (!strcmp(r
, "strace")) {
857 qemu_set_log_filename(log_file
);
861 mask
= qemu_str_to_log_mask(log_mask
);
863 qemu_print_log_usage(stdout
);
869 if (optind
>= argc
) {
872 filename
= argv
[optind
];
875 memset(regs
, 0, sizeof(struct target_pt_regs
));
877 /* Zero out image_info */
878 memset(info
, 0, sizeof(struct image_info
));
880 /* Scan interp_prefix dir for replacement files. */
881 init_paths(interp_prefix
);
883 if (cpu_model
== NULL
) {
884 #if defined(TARGET_I386)
886 cpu_model
= "qemu64";
888 cpu_model
= "qemu32";
890 #elif defined(TARGET_SPARC)
891 #ifdef TARGET_SPARC64
892 cpu_model
= "TI UltraSparc II";
894 cpu_model
= "Fujitsu MB86904";
901 /* NOTE: we need to init the CPU at this stage to get
902 qemu_host_page_size */
903 cpu
= cpu_init(cpu_model
);
905 fprintf(stderr
, "Unable to find CPU definition\n");
909 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
914 if (getenv("QEMU_STRACE")) {
918 target_environ
= envlist_to_environ(envlist
, NULL
);
919 envlist_free(envlist
);
922 * Now that page sizes are configured in cpu_init() we can do
923 * proper page alignment for guest_base.
925 guest_base
= HOST_PAGE_ALIGN(guest_base
);
928 * Read in mmap_min_addr kernel parameter. This value is used
929 * When loading the ELF image to determine whether guest_base
932 * When user has explicitly set the quest base, we skip this
935 if (!have_guest_base
) {
938 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
940 if (fscanf(fp
, "%lu", &tmp
) == 1) {
942 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
948 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
949 printf("Error loading %s\n", filename
);
953 for (wrk
= target_environ
; *wrk
; wrk
++) {
957 free(target_environ
);
959 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
960 qemu_log("guest_base 0x%lx\n", guest_base
);
963 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
964 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
965 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
967 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
969 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
970 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
972 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
973 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
976 target_set_brk(info
->brk
);
980 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
981 generating the prologue until now so that the prologue can take
982 the real value of GUEST_BASE into account. */
983 tcg_prologue_init(&tcg_ctx
);
985 /* build Task State */
986 memset(ts
, 0, sizeof(TaskState
));
991 #if defined(TARGET_I386)
992 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
993 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
994 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
995 env
->cr
[4] |= CR4_OSFXSR_MASK
;
996 env
->hflags
|= HF_OSFXSR_MASK
;
999 /* enable 64 bit mode if possible */
1000 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1001 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1004 env
->cr
[4] |= CR4_PAE_MASK
;
1005 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1006 env
->hflags
|= HF_LMA_MASK
;
1009 /* flags setup : we activate the IRQs by default as in user mode */
1010 env
->eflags
|= IF_MASK
;
1012 /* linux register setup */
1013 #ifndef TARGET_ABI32
1014 env
->regs
[R_EAX
] = regs
->rax
;
1015 env
->regs
[R_EBX
] = regs
->rbx
;
1016 env
->regs
[R_ECX
] = regs
->rcx
;
1017 env
->regs
[R_EDX
] = regs
->rdx
;
1018 env
->regs
[R_ESI
] = regs
->rsi
;
1019 env
->regs
[R_EDI
] = regs
->rdi
;
1020 env
->regs
[R_EBP
] = regs
->rbp
;
1021 env
->regs
[R_ESP
] = regs
->rsp
;
1022 env
->eip
= regs
->rip
;
1024 env
->regs
[R_EAX
] = regs
->eax
;
1025 env
->regs
[R_EBX
] = regs
->ebx
;
1026 env
->regs
[R_ECX
] = regs
->ecx
;
1027 env
->regs
[R_EDX
] = regs
->edx
;
1028 env
->regs
[R_ESI
] = regs
->esi
;
1029 env
->regs
[R_EDI
] = regs
->edi
;
1030 env
->regs
[R_EBP
] = regs
->ebp
;
1031 env
->regs
[R_ESP
] = regs
->esp
;
1032 env
->eip
= regs
->eip
;
1035 /* linux interrupt setup */
1036 #ifndef TARGET_ABI32
1037 env
->idt
.limit
= 511;
1039 env
->idt
.limit
= 255;
1041 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1042 PROT_READ
|PROT_WRITE
,
1043 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1044 idt_table
= g2h(env
->idt
.base
);
1067 /* linux segment setup */
1069 uint64_t *gdt_table
;
1070 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1071 PROT_READ
|PROT_WRITE
,
1072 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1073 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1074 gdt_table
= g2h(env
->gdt
.base
);
1076 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1077 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1078 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1080 /* 64 bit code segment */
1081 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1082 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1084 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1086 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1087 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1088 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1091 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1092 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1094 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1095 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1096 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1097 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1098 /* This hack makes Wine work... */
1099 env
->segs
[R_FS
].selector
= 0;
1101 cpu_x86_load_seg(env
, R_DS
, 0);
1102 cpu_x86_load_seg(env
, R_ES
, 0);
1103 cpu_x86_load_seg(env
, R_FS
, 0);
1104 cpu_x86_load_seg(env
, R_GS
, 0);
1106 #elif defined(TARGET_SPARC)
1110 env
->npc
= regs
->npc
;
1112 for(i
= 0; i
< 8; i
++)
1113 env
->gregs
[i
] = regs
->u_regs
[i
];
1114 for(i
= 0; i
< 8; i
++)
1115 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1118 #error unsupported target CPU
1122 gdbserver_start (gdbstub_port
);
1123 gdb_handlesig(cpu
, 0);