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"
29 #include "qemu/timer.h"
30 #include "qemu/envlist.h"
34 unsigned long mmap_min_addr
;
35 unsigned long guest_base
;
37 unsigned long reserved_va
;
39 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
40 const char *qemu_uname_release
;
41 extern char **environ
;
42 enum BSDType bsd_type
;
44 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
45 we allocate a bigger stack. Need a better solution, for example
46 by remapping the process stack directly at the right place */
47 unsigned long x86_stack_size
= 512 * 1024;
49 void gemu_log(const char *fmt
, ...)
54 vfprintf(stderr
, fmt
, ap
);
58 #if defined(TARGET_I386)
59 int cpu_get_pic_interrupt(CPUX86State
*env
)
65 /* These are no-ops because we are not threadsafe. */
66 static inline void cpu_exec_start(CPUArchState
*env
)
70 static inline void cpu_exec_end(CPUArchState
*env
)
74 static inline void start_exclusive(void)
78 static inline void end_exclusive(void)
86 void fork_end(int child
)
89 gdbserver_fork(thread_cpu
);
93 void cpu_list_lock(void)
97 void cpu_list_unlock(void)
102 /***********************************************************/
103 /* CPUX86 core interface */
105 uint64_t cpu_get_tsc(CPUX86State
*env
)
107 return cpu_get_host_ticks();
110 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
115 e1
= (addr
<< 16) | (limit
& 0xffff);
116 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
123 static uint64_t *idt_table
;
125 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
126 uint64_t addr
, unsigned int sel
)
129 e1
= (addr
& 0xffff) | (sel
<< 16);
130 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
134 p
[2] = tswap32(addr
>> 32);
137 /* only dpl matters as we do only user space emulation */
138 static void set_idt(int n
, unsigned int dpl
)
140 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
143 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
144 uint32_t addr
, unsigned int sel
)
147 e1
= (addr
& 0xffff) | (sel
<< 16);
148 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
154 /* only dpl matters as we do only user space emulation */
155 static void set_idt(int n
, unsigned int dpl
)
157 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
161 void cpu_loop(CPUX86State
*env
)
163 X86CPU
*cpu
= x86_env_get_cpu(env
);
164 CPUState
*cs
= CPU(cpu
);
167 //target_siginfo_t info;
170 trapnr
= cpu_x86_exec(cs
);
173 /* syscall from int $0x80 */
174 if (bsd_type
== target_freebsd
) {
175 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
177 int32_t syscall_nr
= env
->regs
[R_EAX
];
178 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
180 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
181 get_user_s32(syscall_nr
, params
);
182 params
+= sizeof(int32_t);
183 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
184 get_user_s32(syscall_nr
, params
);
185 params
+= sizeof(int64_t);
187 get_user_s32(arg1
, params
);
188 params
+= sizeof(int32_t);
189 get_user_s32(arg2
, params
);
190 params
+= sizeof(int32_t);
191 get_user_s32(arg3
, params
);
192 params
+= sizeof(int32_t);
193 get_user_s32(arg4
, params
);
194 params
+= sizeof(int32_t);
195 get_user_s32(arg5
, params
);
196 params
+= sizeof(int32_t);
197 get_user_s32(arg6
, params
);
198 params
+= sizeof(int32_t);
199 get_user_s32(arg7
, params
);
200 params
+= sizeof(int32_t);
201 get_user_s32(arg8
, params
);
202 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
212 } else { //if (bsd_type == target_openbsd)
213 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
222 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
223 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
226 env
->eflags
&= ~CC_C
;
231 /* syscall from syscall instruction */
232 if (bsd_type
== target_freebsd
)
233 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
241 else { //if (bsd_type == target_openbsd)
242 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
251 env
->eip
= env
->exception_next_eip
;
252 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
253 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
256 env
->eflags
&= ~CC_C
;
263 info
.si_signo
= SIGBUS
;
265 info
.si_code
= TARGET_SI_KERNEL
;
266 info
._sifields
._sigfault
._addr
= 0;
267 queue_signal(env
, info
.si_signo
, &info
);
270 /* XXX: potential problem if ABI32 */
271 #ifndef TARGET_X86_64
272 if (env
->eflags
& VM_MASK
) {
273 handle_vm86_fault(env
);
277 info
.si_signo
= SIGSEGV
;
279 info
.si_code
= TARGET_SI_KERNEL
;
280 info
._sifields
._sigfault
._addr
= 0;
281 queue_signal(env
, info
.si_signo
, &info
);
285 info
.si_signo
= SIGSEGV
;
287 if (!(env
->error_code
& 1))
288 info
.si_code
= TARGET_SEGV_MAPERR
;
290 info
.si_code
= TARGET_SEGV_ACCERR
;
291 info
._sifields
._sigfault
._addr
= env
->cr
[2];
292 queue_signal(env
, info
.si_signo
, &info
);
295 #ifndef TARGET_X86_64
296 if (env
->eflags
& VM_MASK
) {
297 handle_vm86_trap(env
, trapnr
);
301 /* division by zero */
302 info
.si_signo
= SIGFPE
;
304 info
.si_code
= TARGET_FPE_INTDIV
;
305 info
._sifields
._sigfault
._addr
= env
->eip
;
306 queue_signal(env
, info
.si_signo
, &info
);
311 #ifndef TARGET_X86_64
312 if (env
->eflags
& VM_MASK
) {
313 handle_vm86_trap(env
, trapnr
);
317 info
.si_signo
= SIGTRAP
;
319 if (trapnr
== EXCP01_DB
) {
320 info
.si_code
= TARGET_TRAP_BRKPT
;
321 info
._sifields
._sigfault
._addr
= env
->eip
;
323 info
.si_code
= TARGET_SI_KERNEL
;
324 info
._sifields
._sigfault
._addr
= 0;
326 queue_signal(env
, info
.si_signo
, &info
);
331 #ifndef TARGET_X86_64
332 if (env
->eflags
& VM_MASK
) {
333 handle_vm86_trap(env
, trapnr
);
337 info
.si_signo
= SIGSEGV
;
339 info
.si_code
= TARGET_SI_KERNEL
;
340 info
._sifields
._sigfault
._addr
= 0;
341 queue_signal(env
, info
.si_signo
, &info
);
345 info
.si_signo
= SIGILL
;
347 info
.si_code
= TARGET_ILL_ILLOPN
;
348 info
._sifields
._sigfault
._addr
= env
->eip
;
349 queue_signal(env
, info
.si_signo
, &info
);
353 /* just indicate that signals should be handled asap */
360 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
365 info
.si_code
= TARGET_TRAP_BRKPT
;
366 queue_signal(env
, info
.si_signo
, &info
);
372 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
373 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
377 process_pending_signals(env
);
383 #define SPARC64_STACK_BIAS 2047
386 /* WARNING: dealing with register windows _is_ complicated. More info
387 can be found at http://www.sics.se/~psm/sparcstack.html */
388 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
390 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
391 /* wrap handling : if cwp is on the last window, then we use the
392 registers 'after' the end */
393 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
394 index
+= 16 * env
->nwindows
;
398 /* save the register window 'cwp1' */
399 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
404 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
405 #ifdef TARGET_SPARC64
407 sp_ptr
+= SPARC64_STACK_BIAS
;
409 #if defined(DEBUG_WIN)
410 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
413 for(i
= 0; i
< 16; i
++) {
414 /* FIXME - what to do if put_user() fails? */
415 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
416 sp_ptr
+= sizeof(abi_ulong
);
420 static void save_window(CPUSPARCState
*env
)
422 #ifndef TARGET_SPARC64
423 unsigned int new_wim
;
424 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
425 ((1LL << env
->nwindows
) - 1);
426 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
429 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
435 static void restore_window(CPUSPARCState
*env
)
437 #ifndef TARGET_SPARC64
438 unsigned int new_wim
;
440 unsigned int i
, cwp1
;
443 #ifndef TARGET_SPARC64
444 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
445 ((1LL << env
->nwindows
) - 1);
448 /* restore the invalid window */
449 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
450 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
451 #ifdef TARGET_SPARC64
453 sp_ptr
+= SPARC64_STACK_BIAS
;
455 #if defined(DEBUG_WIN)
456 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
459 for(i
= 0; i
< 16; i
++) {
460 /* FIXME - what to do if get_user() fails? */
461 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
462 sp_ptr
+= sizeof(abi_ulong
);
464 #ifdef TARGET_SPARC64
466 if (env
->cleanwin
< env
->nwindows
- 1)
474 static void flush_windows(CPUSPARCState
*env
)
480 /* if restore would invoke restore_window(), then we can stop */
481 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
482 #ifndef TARGET_SPARC64
483 if (env
->wim
& (1 << cwp1
))
486 if (env
->canrestore
== 0)
491 save_window_offset(env
, cwp1
);
494 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
495 #ifndef TARGET_SPARC64
496 /* set wim so that restore will reload the registers */
497 env
->wim
= 1 << cwp1
;
499 #if defined(DEBUG_WIN)
500 printf("flush_windows: nb=%d\n", offset
- 1);
504 void cpu_loop(CPUSPARCState
*env
)
506 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
507 int trapnr
, ret
, syscall_nr
;
508 //target_siginfo_t info;
511 trapnr
= cpu_sparc_exec(cs
);
514 #ifndef TARGET_SPARC64
517 /* FreeBSD uses 0x141 for syscalls too */
519 if (bsd_type
!= target_freebsd
)
523 syscall_nr
= env
->gregs
[1];
524 if (bsd_type
== target_freebsd
)
525 ret
= do_freebsd_syscall(env
, syscall_nr
,
526 env
->regwptr
[0], env
->regwptr
[1],
527 env
->regwptr
[2], env
->regwptr
[3],
528 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
529 else if (bsd_type
== target_netbsd
)
530 ret
= do_netbsd_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]);
534 else { //if (bsd_type == target_openbsd)
535 #if defined(TARGET_SPARC64)
536 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
537 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
539 ret
= do_openbsd_syscall(env
, syscall_nr
,
540 env
->regwptr
[0], env
->regwptr
[1],
541 env
->regwptr
[2], env
->regwptr
[3],
542 env
->regwptr
[4], env
->regwptr
[5]);
544 if ((unsigned int)ret
>= (unsigned int)(-515)) {
546 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
547 env
->xcc
|= PSR_CARRY
;
549 env
->psr
|= PSR_CARRY
;
552 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
553 env
->xcc
&= ~PSR_CARRY
;
555 env
->psr
&= ~PSR_CARRY
;
558 env
->regwptr
[0] = ret
;
559 /* next instruction */
560 #if defined(TARGET_SPARC64)
561 if (bsd_type
== target_openbsd
&&
562 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
563 env
->pc
= env
->gregs
[2];
564 env
->npc
= env
->pc
+ 4;
565 } else if (bsd_type
== target_openbsd
&&
566 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
567 env
->pc
= env
->gregs
[7];
568 env
->npc
= env
->pc
+ 4;
571 env
->npc
= env
->npc
+ 4;
575 env
->npc
= env
->npc
+ 4;
578 case 0x83: /* flush windows */
583 /* next instruction */
585 env
->npc
= env
->npc
+ 4;
587 #ifndef TARGET_SPARC64
588 case TT_WIN_OVF
: /* window overflow */
591 case TT_WIN_UNF
: /* window underflow */
598 info
.si_signo
= SIGSEGV
;
600 /* XXX: check env->error_code */
601 info
.si_code
= TARGET_SEGV_MAPERR
;
602 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
603 queue_signal(env
, info
.si_signo
, &info
);
608 case TT_SPILL
: /* window overflow */
611 case TT_FILL
: /* window underflow */
618 info
.si_signo
= SIGSEGV
;
620 /* XXX: check env->error_code */
621 info
.si_code
= TARGET_SEGV_MAPERR
;
622 if (trapnr
== TT_DFAULT
)
623 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
625 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
626 //queue_signal(env, info.si_signo, &info);
632 /* just indicate that signals should be handled asap */
638 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
644 info
.si_code
= TARGET_TRAP_BRKPT
;
645 //queue_signal(env, info.si_signo, &info);
651 #ifdef TARGET_SPARC64
654 printf ("Unhandled trap: 0x%x\n", trapnr
);
655 cpu_dump_state(cs
, stderr
, fprintf
, 0);
658 process_pending_signals (env
);
664 static void usage(void)
666 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
667 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
668 "BSD CPU emulator (compiled for %s emulation)\n"
670 "Standard options:\n"
671 "-h print this help\n"
672 "-g port wait gdb connection to port\n"
673 "-L path set the elf interpreter prefix (default=%s)\n"
674 "-s size set the stack size in bytes (default=%ld)\n"
675 "-cpu model select CPU (-cpu help for list)\n"
676 "-drop-ld-preload drop LD_PRELOAD for target process\n"
677 "-E var=value sets/modifies targets environment variable(s)\n"
678 "-U var unsets targets environment variable(s)\n"
679 "-B address set guest_base address to address\n"
680 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
683 "-d item1[,...] enable logging of specified items\n"
684 " (use '-d help' for a list of log items)\n"
685 "-D logfile write logs to 'logfile' (default stderr)\n"
686 "-p pagesize set the host page size to 'pagesize'\n"
687 "-singlestep always run in singlestep mode\n"
688 "-strace log system calls\n"
690 "Environment variables:\n"
691 "QEMU_STRACE Print system calls and arguments similar to the\n"
692 " 'strace' program. Enable by setting to any value.\n"
693 "You can use -E and -U options to set/unset environment variables\n"
694 "for target process. It is possible to provide several variables\n"
695 "by repeating the option. For example:\n"
696 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
697 "Note that if you provide several changes to single variable\n"
698 "last change will stay in effect.\n"
706 THREAD CPUState
*thread_cpu
;
708 /* Assumes contents are already zeroed. */
709 void init_task_state(TaskState
*ts
)
714 ts
->first_free
= ts
->sigqueue_table
;
715 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
716 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
718 ts
->sigqueue_table
[i
].next
= NULL
;
721 int main(int argc
, char **argv
)
723 const char *filename
;
724 const char *cpu_model
;
725 const char *log_file
= NULL
;
726 const char *log_mask
= NULL
;
727 struct target_pt_regs regs1
, *regs
= ®s1
;
728 struct image_info info1
, *info
= &info1
;
729 TaskState ts1
, *ts
= &ts1
;
734 int gdbstub_port
= 0;
735 char **target_environ
, **wrk
;
736 envlist_t
*envlist
= NULL
;
737 bsd_type
= target_openbsd
;
742 module_call_init(MODULE_INIT_QOM
);
744 if ((envlist
= envlist_create()) == NULL
) {
745 (void) fprintf(stderr
, "Unable to allocate envlist\n");
749 /* add current environment into the list */
750 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
751 (void) envlist_setenv(envlist
, *wrk
);
755 #if defined(cpudef_setup)
756 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
768 if (!strcmp(r
, "-")) {
770 } else if (!strcmp(r
, "d")) {
771 if (optind
>= argc
) {
774 log_mask
= argv
[optind
++];
775 } else if (!strcmp(r
, "D")) {
776 if (optind
>= argc
) {
779 log_file
= argv
[optind
++];
780 } else if (!strcmp(r
, "E")) {
782 if (envlist_setenv(envlist
, r
) != 0)
784 } else if (!strcmp(r
, "ignore-environment")) {
785 envlist_free(envlist
);
786 if ((envlist
= envlist_create()) == NULL
) {
787 (void) fprintf(stderr
, "Unable to allocate envlist\n");
790 } else if (!strcmp(r
, "U")) {
792 if (envlist_unsetenv(envlist
, r
) != 0)
794 } else if (!strcmp(r
, "s")) {
796 x86_stack_size
= strtol(r
, (char **)&r
, 0);
797 if (x86_stack_size
<= 0)
800 x86_stack_size
*= 1024 * 1024;
801 else if (*r
== 'k' || *r
== 'K')
802 x86_stack_size
*= 1024;
803 } else if (!strcmp(r
, "L")) {
804 interp_prefix
= argv
[optind
++];
805 } else if (!strcmp(r
, "p")) {
806 qemu_host_page_size
= atoi(argv
[optind
++]);
807 if (qemu_host_page_size
== 0 ||
808 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
809 fprintf(stderr
, "page size must be a power of two\n");
812 } else if (!strcmp(r
, "g")) {
813 gdbstub_port
= atoi(argv
[optind
++]);
814 } else if (!strcmp(r
, "r")) {
815 qemu_uname_release
= argv
[optind
++];
816 } else if (!strcmp(r
, "cpu")) {
817 cpu_model
= argv
[optind
++];
818 if (is_help_option(cpu_model
)) {
819 /* XXX: implement xxx_cpu_list for targets that still miss it */
820 #if defined(cpu_list)
821 cpu_list(stdout
, &fprintf
);
825 } else if (!strcmp(r
, "B")) {
826 guest_base
= strtol(argv
[optind
++], NULL
, 0);
828 } else if (!strcmp(r
, "drop-ld-preload")) {
829 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
830 } else if (!strcmp(r
, "bsd")) {
831 if (!strcasecmp(argv
[optind
], "freebsd")) {
832 bsd_type
= target_freebsd
;
833 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
834 bsd_type
= target_netbsd
;
835 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
836 bsd_type
= target_openbsd
;
841 } else if (!strcmp(r
, "singlestep")) {
843 } else if (!strcmp(r
, "strace")) {
852 qemu_set_log_filename(log_file
);
856 mask
= qemu_str_to_log_mask(log_mask
);
858 qemu_print_log_usage(stdout
);
864 if (optind
>= argc
) {
867 filename
= argv
[optind
];
870 memset(regs
, 0, sizeof(struct target_pt_regs
));
872 /* Zero out image_info */
873 memset(info
, 0, sizeof(struct image_info
));
875 /* Scan interp_prefix dir for replacement files. */
876 init_paths(interp_prefix
);
878 if (cpu_model
== NULL
) {
879 #if defined(TARGET_I386)
881 cpu_model
= "qemu64";
883 cpu_model
= "qemu32";
885 #elif defined(TARGET_SPARC)
886 #ifdef TARGET_SPARC64
887 cpu_model
= "TI UltraSparc II";
889 cpu_model
= "Fujitsu MB86904";
896 /* NOTE: we need to init the CPU at this stage to get
897 qemu_host_page_size */
898 cpu
= cpu_init(cpu_model
);
900 fprintf(stderr
, "Unable to find CPU definition\n");
904 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
909 if (getenv("QEMU_STRACE")) {
913 target_environ
= envlist_to_environ(envlist
, NULL
);
914 envlist_free(envlist
);
917 * Now that page sizes are configured in cpu_init() we can do
918 * proper page alignment for guest_base.
920 guest_base
= HOST_PAGE_ALIGN(guest_base
);
923 * Read in mmap_min_addr kernel parameter. This value is used
924 * When loading the ELF image to determine whether guest_base
927 * When user has explicitly set the quest base, we skip this
930 if (!have_guest_base
) {
933 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
935 if (fscanf(fp
, "%lu", &tmp
) == 1) {
937 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
943 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
944 printf("Error loading %s\n", filename
);
948 for (wrk
= target_environ
; *wrk
; wrk
++) {
952 free(target_environ
);
954 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
955 qemu_log("guest_base 0x%lx\n", guest_base
);
958 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
959 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
960 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
962 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
964 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
965 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
967 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
968 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
971 target_set_brk(info
->brk
);
975 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
976 generating the prologue until now so that the prologue can take
977 the real value of GUEST_BASE into account. */
978 tcg_prologue_init(&tcg_ctx
);
980 /* build Task State */
981 memset(ts
, 0, sizeof(TaskState
));
986 #if defined(TARGET_I386)
987 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
988 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
989 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
990 env
->cr
[4] |= CR4_OSFXSR_MASK
;
991 env
->hflags
|= HF_OSFXSR_MASK
;
994 /* enable 64 bit mode if possible */
995 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
996 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
999 env
->cr
[4] |= CR4_PAE_MASK
;
1000 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1001 env
->hflags
|= HF_LMA_MASK
;
1004 /* flags setup : we activate the IRQs by default as in user mode */
1005 env
->eflags
|= IF_MASK
;
1007 /* linux register setup */
1008 #ifndef TARGET_ABI32
1009 env
->regs
[R_EAX
] = regs
->rax
;
1010 env
->regs
[R_EBX
] = regs
->rbx
;
1011 env
->regs
[R_ECX
] = regs
->rcx
;
1012 env
->regs
[R_EDX
] = regs
->rdx
;
1013 env
->regs
[R_ESI
] = regs
->rsi
;
1014 env
->regs
[R_EDI
] = regs
->rdi
;
1015 env
->regs
[R_EBP
] = regs
->rbp
;
1016 env
->regs
[R_ESP
] = regs
->rsp
;
1017 env
->eip
= regs
->rip
;
1019 env
->regs
[R_EAX
] = regs
->eax
;
1020 env
->regs
[R_EBX
] = regs
->ebx
;
1021 env
->regs
[R_ECX
] = regs
->ecx
;
1022 env
->regs
[R_EDX
] = regs
->edx
;
1023 env
->regs
[R_ESI
] = regs
->esi
;
1024 env
->regs
[R_EDI
] = regs
->edi
;
1025 env
->regs
[R_EBP
] = regs
->ebp
;
1026 env
->regs
[R_ESP
] = regs
->esp
;
1027 env
->eip
= regs
->eip
;
1030 /* linux interrupt setup */
1031 #ifndef TARGET_ABI32
1032 env
->idt
.limit
= 511;
1034 env
->idt
.limit
= 255;
1036 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1037 PROT_READ
|PROT_WRITE
,
1038 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1039 idt_table
= g2h(env
->idt
.base
);
1062 /* linux segment setup */
1064 uint64_t *gdt_table
;
1065 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1066 PROT_READ
|PROT_WRITE
,
1067 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1068 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1069 gdt_table
= g2h(env
->gdt
.base
);
1071 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1072 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1073 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1075 /* 64 bit code segment */
1076 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1077 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1079 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1081 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1082 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1083 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1086 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1087 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1089 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1090 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1091 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1092 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1093 /* This hack makes Wine work... */
1094 env
->segs
[R_FS
].selector
= 0;
1096 cpu_x86_load_seg(env
, R_DS
, 0);
1097 cpu_x86_load_seg(env
, R_ES
, 0);
1098 cpu_x86_load_seg(env
, R_FS
, 0);
1099 cpu_x86_load_seg(env
, R_GS
, 0);
1101 #elif defined(TARGET_SPARC)
1105 env
->npc
= regs
->npc
;
1107 for(i
= 0; i
< 8; i
++)
1108 env
->gregs
[i
] = regs
->u_regs
[i
];
1109 for(i
= 0; i
< 8; i
++)
1110 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1113 #error unsupported target CPU
1117 gdbserver_start (gdbstub_port
);
1118 gdb_handlesig(cpu
, 0);