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-common.h"
28 #include "qemu/timer.h"
29 #include "qemu/envlist.h"
33 unsigned long mmap_min_addr
;
34 unsigned long guest_base
;
36 unsigned long reserved_va
;
38 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
39 const char *qemu_uname_release
;
40 extern char **environ
;
41 enum BSDType bsd_type
;
43 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
44 we allocate a bigger stack. Need a better solution, for example
45 by remapping the process stack directly at the right place */
46 unsigned long x86_stack_size
= 512 * 1024;
48 void gemu_log(const char *fmt
, ...)
53 vfprintf(stderr
, fmt
, ap
);
57 #if defined(TARGET_I386)
58 int cpu_get_pic_interrupt(CPUX86State
*env
)
64 /* These are no-ops because we are not threadsafe. */
65 static inline void cpu_exec_start(CPUArchState
*env
)
69 static inline void cpu_exec_end(CPUArchState
*env
)
73 static inline void start_exclusive(void)
77 static inline void end_exclusive(void)
85 void fork_end(int child
)
88 gdbserver_fork(thread_cpu
);
92 void cpu_list_lock(void)
96 void cpu_list_unlock(void)
101 /***********************************************************/
102 /* CPUX86 core interface */
104 uint64_t cpu_get_tsc(CPUX86State
*env
)
106 return cpu_get_host_ticks();
109 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
114 e1
= (addr
<< 16) | (limit
& 0xffff);
115 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
122 static uint64_t *idt_table
;
124 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
125 uint64_t addr
, unsigned int sel
)
128 e1
= (addr
& 0xffff) | (sel
<< 16);
129 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
133 p
[2] = tswap32(addr
>> 32);
136 /* only dpl matters as we do only user space emulation */
137 static void set_idt(int n
, unsigned int dpl
)
139 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
142 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
143 uint32_t addr
, unsigned int sel
)
146 e1
= (addr
& 0xffff) | (sel
<< 16);
147 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
153 /* only dpl matters as we do only user space emulation */
154 static void set_idt(int n
, unsigned int dpl
)
156 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
160 void cpu_loop(CPUX86State
*env
)
162 X86CPU
*cpu
= x86_env_get_cpu(env
);
163 CPUState
*cs
= CPU(cpu
);
166 //target_siginfo_t info;
169 trapnr
= cpu_x86_exec(cs
);
172 /* syscall from int $0x80 */
173 if (bsd_type
== target_freebsd
) {
174 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
176 int32_t syscall_nr
= env
->regs
[R_EAX
];
177 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
179 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
180 get_user_s32(syscall_nr
, params
);
181 params
+= sizeof(int32_t);
182 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
183 get_user_s32(syscall_nr
, params
);
184 params
+= sizeof(int64_t);
186 get_user_s32(arg1
, params
);
187 params
+= sizeof(int32_t);
188 get_user_s32(arg2
, params
);
189 params
+= sizeof(int32_t);
190 get_user_s32(arg3
, params
);
191 params
+= sizeof(int32_t);
192 get_user_s32(arg4
, params
);
193 params
+= sizeof(int32_t);
194 get_user_s32(arg5
, params
);
195 params
+= sizeof(int32_t);
196 get_user_s32(arg6
, params
);
197 params
+= sizeof(int32_t);
198 get_user_s32(arg7
, params
);
199 params
+= sizeof(int32_t);
200 get_user_s32(arg8
, params
);
201 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
211 } else { //if (bsd_type == target_openbsd)
212 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
221 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
222 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
225 env
->eflags
&= ~CC_C
;
230 /* syscall from syscall instruction */
231 if (bsd_type
== target_freebsd
)
232 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
240 else { //if (bsd_type == target_openbsd)
241 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
250 env
->eip
= env
->exception_next_eip
;
251 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
252 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
255 env
->eflags
&= ~CC_C
;
262 info
.si_signo
= SIGBUS
;
264 info
.si_code
= TARGET_SI_KERNEL
;
265 info
._sifields
._sigfault
._addr
= 0;
266 queue_signal(env
, info
.si_signo
, &info
);
269 /* XXX: potential problem if ABI32 */
270 #ifndef TARGET_X86_64
271 if (env
->eflags
& VM_MASK
) {
272 handle_vm86_fault(env
);
276 info
.si_signo
= SIGSEGV
;
278 info
.si_code
= TARGET_SI_KERNEL
;
279 info
._sifields
._sigfault
._addr
= 0;
280 queue_signal(env
, info
.si_signo
, &info
);
284 info
.si_signo
= SIGSEGV
;
286 if (!(env
->error_code
& 1))
287 info
.si_code
= TARGET_SEGV_MAPERR
;
289 info
.si_code
= TARGET_SEGV_ACCERR
;
290 info
._sifields
._sigfault
._addr
= env
->cr
[2];
291 queue_signal(env
, info
.si_signo
, &info
);
294 #ifndef TARGET_X86_64
295 if (env
->eflags
& VM_MASK
) {
296 handle_vm86_trap(env
, trapnr
);
300 /* division by zero */
301 info
.si_signo
= SIGFPE
;
303 info
.si_code
= TARGET_FPE_INTDIV
;
304 info
._sifields
._sigfault
._addr
= env
->eip
;
305 queue_signal(env
, info
.si_signo
, &info
);
310 #ifndef TARGET_X86_64
311 if (env
->eflags
& VM_MASK
) {
312 handle_vm86_trap(env
, trapnr
);
316 info
.si_signo
= SIGTRAP
;
318 if (trapnr
== EXCP01_DB
) {
319 info
.si_code
= TARGET_TRAP_BRKPT
;
320 info
._sifields
._sigfault
._addr
= env
->eip
;
322 info
.si_code
= TARGET_SI_KERNEL
;
323 info
._sifields
._sigfault
._addr
= 0;
325 queue_signal(env
, info
.si_signo
, &info
);
330 #ifndef TARGET_X86_64
331 if (env
->eflags
& VM_MASK
) {
332 handle_vm86_trap(env
, trapnr
);
336 info
.si_signo
= SIGSEGV
;
338 info
.si_code
= TARGET_SI_KERNEL
;
339 info
._sifields
._sigfault
._addr
= 0;
340 queue_signal(env
, info
.si_signo
, &info
);
344 info
.si_signo
= SIGILL
;
346 info
.si_code
= TARGET_ILL_ILLOPN
;
347 info
._sifields
._sigfault
._addr
= env
->eip
;
348 queue_signal(env
, info
.si_signo
, &info
);
352 /* just indicate that signals should be handled asap */
359 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
364 info
.si_code
= TARGET_TRAP_BRKPT
;
365 queue_signal(env
, info
.si_signo
, &info
);
371 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
372 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
376 process_pending_signals(env
);
382 #define SPARC64_STACK_BIAS 2047
385 /* WARNING: dealing with register windows _is_ complicated. More info
386 can be found at http://www.sics.se/~psm/sparcstack.html */
387 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
389 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
390 /* wrap handling : if cwp is on the last window, then we use the
391 registers 'after' the end */
392 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
393 index
+= 16 * env
->nwindows
;
397 /* save the register window 'cwp1' */
398 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
403 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
404 #ifdef TARGET_SPARC64
406 sp_ptr
+= SPARC64_STACK_BIAS
;
408 #if defined(DEBUG_WIN)
409 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
412 for(i
= 0; i
< 16; i
++) {
413 /* FIXME - what to do if put_user() fails? */
414 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
415 sp_ptr
+= sizeof(abi_ulong
);
419 static void save_window(CPUSPARCState
*env
)
421 #ifndef TARGET_SPARC64
422 unsigned int new_wim
;
423 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
424 ((1LL << env
->nwindows
) - 1);
425 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
428 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
434 static void restore_window(CPUSPARCState
*env
)
436 #ifndef TARGET_SPARC64
437 unsigned int new_wim
;
439 unsigned int i
, cwp1
;
442 #ifndef TARGET_SPARC64
443 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
444 ((1LL << env
->nwindows
) - 1);
447 /* restore the invalid window */
448 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
449 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
450 #ifdef TARGET_SPARC64
452 sp_ptr
+= SPARC64_STACK_BIAS
;
454 #if defined(DEBUG_WIN)
455 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
458 for(i
= 0; i
< 16; i
++) {
459 /* FIXME - what to do if get_user() fails? */
460 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
461 sp_ptr
+= sizeof(abi_ulong
);
463 #ifdef TARGET_SPARC64
465 if (env
->cleanwin
< env
->nwindows
- 1)
473 static void flush_windows(CPUSPARCState
*env
)
479 /* if restore would invoke restore_window(), then we can stop */
480 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
481 #ifndef TARGET_SPARC64
482 if (env
->wim
& (1 << cwp1
))
485 if (env
->canrestore
== 0)
490 save_window_offset(env
, cwp1
);
493 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
494 #ifndef TARGET_SPARC64
495 /* set wim so that restore will reload the registers */
496 env
->wim
= 1 << cwp1
;
498 #if defined(DEBUG_WIN)
499 printf("flush_windows: nb=%d\n", offset
- 1);
503 void cpu_loop(CPUSPARCState
*env
)
505 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
506 int trapnr
, ret
, syscall_nr
;
507 //target_siginfo_t info;
510 trapnr
= cpu_sparc_exec(cs
);
513 #ifndef TARGET_SPARC64
516 /* FreeBSD uses 0x141 for syscalls too */
518 if (bsd_type
!= target_freebsd
)
522 syscall_nr
= env
->gregs
[1];
523 if (bsd_type
== target_freebsd
)
524 ret
= do_freebsd_syscall(env
, syscall_nr
,
525 env
->regwptr
[0], env
->regwptr
[1],
526 env
->regwptr
[2], env
->regwptr
[3],
527 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
528 else if (bsd_type
== target_netbsd
)
529 ret
= do_netbsd_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]);
533 else { //if (bsd_type == target_openbsd)
534 #if defined(TARGET_SPARC64)
535 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
536 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
538 ret
= do_openbsd_syscall(env
, syscall_nr
,
539 env
->regwptr
[0], env
->regwptr
[1],
540 env
->regwptr
[2], env
->regwptr
[3],
541 env
->regwptr
[4], env
->regwptr
[5]);
543 if ((unsigned int)ret
>= (unsigned int)(-515)) {
545 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
546 env
->xcc
|= PSR_CARRY
;
548 env
->psr
|= PSR_CARRY
;
551 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
552 env
->xcc
&= ~PSR_CARRY
;
554 env
->psr
&= ~PSR_CARRY
;
557 env
->regwptr
[0] = ret
;
558 /* next instruction */
559 #if defined(TARGET_SPARC64)
560 if (bsd_type
== target_openbsd
&&
561 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
562 env
->pc
= env
->gregs
[2];
563 env
->npc
= env
->pc
+ 4;
564 } else if (bsd_type
== target_openbsd
&&
565 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
566 env
->pc
= env
->gregs
[7];
567 env
->npc
= env
->pc
+ 4;
570 env
->npc
= env
->npc
+ 4;
574 env
->npc
= env
->npc
+ 4;
577 case 0x83: /* flush windows */
582 /* next instruction */
584 env
->npc
= env
->npc
+ 4;
586 #ifndef TARGET_SPARC64
587 case TT_WIN_OVF
: /* window overflow */
590 case TT_WIN_UNF
: /* window underflow */
597 info
.si_signo
= SIGSEGV
;
599 /* XXX: check env->error_code */
600 info
.si_code
= TARGET_SEGV_MAPERR
;
601 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
602 queue_signal(env
, info
.si_signo
, &info
);
607 case TT_SPILL
: /* window overflow */
610 case TT_FILL
: /* window underflow */
617 info
.si_signo
= SIGSEGV
;
619 /* XXX: check env->error_code */
620 info
.si_code
= TARGET_SEGV_MAPERR
;
621 if (trapnr
== TT_DFAULT
)
622 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
624 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
625 //queue_signal(env, info.si_signo, &info);
631 /* just indicate that signals should be handled asap */
637 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
643 info
.si_code
= TARGET_TRAP_BRKPT
;
644 //queue_signal(env, info.si_signo, &info);
650 #ifdef TARGET_SPARC64
653 printf ("Unhandled trap: 0x%x\n", trapnr
);
654 cpu_dump_state(cs
, stderr
, fprintf
, 0);
657 process_pending_signals (env
);
663 static void usage(void)
665 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
666 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
667 "BSD CPU emulator (compiled for %s emulation)\n"
669 "Standard options:\n"
670 "-h print this help\n"
671 "-g port wait gdb connection to port\n"
672 "-L path set the elf interpreter prefix (default=%s)\n"
673 "-s size set the stack size in bytes (default=%ld)\n"
674 "-cpu model select CPU (-cpu help for list)\n"
675 "-drop-ld-preload drop LD_PRELOAD for target process\n"
676 "-E var=value sets/modifies targets environment variable(s)\n"
677 "-U var unsets targets environment variable(s)\n"
678 "-B address set guest_base address to address\n"
679 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
682 "-d item1[,...] enable logging of specified items\n"
683 " (use '-d help' for a list of log items)\n"
684 "-D logfile write logs to 'logfile' (default stderr)\n"
685 "-p pagesize set the host page size to 'pagesize'\n"
686 "-singlestep always run in singlestep mode\n"
687 "-strace log system calls\n"
689 "Environment variables:\n"
690 "QEMU_STRACE Print system calls and arguments similar to the\n"
691 " 'strace' program. Enable by setting to any value.\n"
692 "You can use -E and -U options to set/unset environment variables\n"
693 "for target process. It is possible to provide several variables\n"
694 "by repeating the option. For example:\n"
695 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
696 "Note that if you provide several changes to single variable\n"
697 "last change will stay in effect.\n"
705 THREAD CPUState
*thread_cpu
;
707 /* Assumes contents are already zeroed. */
708 void init_task_state(TaskState
*ts
)
713 ts
->first_free
= ts
->sigqueue_table
;
714 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
715 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
717 ts
->sigqueue_table
[i
].next
= NULL
;
720 int main(int argc
, char **argv
)
722 const char *filename
;
723 const char *cpu_model
;
724 const char *log_file
= NULL
;
725 const char *log_mask
= NULL
;
726 struct target_pt_regs regs1
, *regs
= ®s1
;
727 struct image_info info1
, *info
= &info1
;
728 TaskState ts1
, *ts
= &ts1
;
733 int gdbstub_port
= 0;
734 char **target_environ
, **wrk
;
735 envlist_t
*envlist
= NULL
;
736 bsd_type
= target_openbsd
;
741 module_call_init(MODULE_INIT_QOM
);
743 if ((envlist
= envlist_create()) == NULL
) {
744 (void) fprintf(stderr
, "Unable to allocate envlist\n");
748 /* add current environment into the list */
749 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
750 (void) envlist_setenv(envlist
, *wrk
);
754 #if defined(cpudef_setup)
755 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
767 if (!strcmp(r
, "-")) {
769 } else if (!strcmp(r
, "d")) {
770 if (optind
>= argc
) {
773 log_mask
= argv
[optind
++];
774 } else if (!strcmp(r
, "D")) {
775 if (optind
>= argc
) {
778 log_file
= argv
[optind
++];
779 } else if (!strcmp(r
, "E")) {
781 if (envlist_setenv(envlist
, r
) != 0)
783 } else if (!strcmp(r
, "ignore-environment")) {
784 envlist_free(envlist
);
785 if ((envlist
= envlist_create()) == NULL
) {
786 (void) fprintf(stderr
, "Unable to allocate envlist\n");
789 } else if (!strcmp(r
, "U")) {
791 if (envlist_unsetenv(envlist
, r
) != 0)
793 } else if (!strcmp(r
, "s")) {
795 x86_stack_size
= strtol(r
, (char **)&r
, 0);
796 if (x86_stack_size
<= 0)
799 x86_stack_size
*= 1024 * 1024;
800 else if (*r
== 'k' || *r
== 'K')
801 x86_stack_size
*= 1024;
802 } else if (!strcmp(r
, "L")) {
803 interp_prefix
= argv
[optind
++];
804 } else if (!strcmp(r
, "p")) {
805 qemu_host_page_size
= atoi(argv
[optind
++]);
806 if (qemu_host_page_size
== 0 ||
807 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
808 fprintf(stderr
, "page size must be a power of two\n");
811 } else if (!strcmp(r
, "g")) {
812 gdbstub_port
= atoi(argv
[optind
++]);
813 } else if (!strcmp(r
, "r")) {
814 qemu_uname_release
= argv
[optind
++];
815 } else if (!strcmp(r
, "cpu")) {
816 cpu_model
= argv
[optind
++];
817 if (is_help_option(cpu_model
)) {
818 /* XXX: implement xxx_cpu_list for targets that still miss it */
819 #if defined(cpu_list)
820 cpu_list(stdout
, &fprintf
);
824 } else if (!strcmp(r
, "B")) {
825 guest_base
= strtol(argv
[optind
++], NULL
, 0);
827 } else if (!strcmp(r
, "drop-ld-preload")) {
828 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
829 } else if (!strcmp(r
, "bsd")) {
830 if (!strcasecmp(argv
[optind
], "freebsd")) {
831 bsd_type
= target_freebsd
;
832 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
833 bsd_type
= target_netbsd
;
834 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
835 bsd_type
= target_openbsd
;
840 } else if (!strcmp(r
, "singlestep")) {
842 } else if (!strcmp(r
, "strace")) {
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);