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 "qemu-version.h"
21 #include <machine/trap.h>
23 #include "qapi/error.h"
25 #include "qemu/config-file.h"
26 #include "qemu/path.h"
27 #include "qemu/help_option.h"
30 #include "exec/exec-all.h"
32 #include "qemu/timer.h"
33 #include "qemu/envlist.h"
35 #include "trace/control.h"
36 #include "glib-compat.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
)
74 void fork_end(int child
)
77 gdbserver_fork(thread_cpu
);
82 /***********************************************************/
83 /* CPUX86 core interface */
85 uint64_t cpu_get_tsc(CPUX86State
*env
)
87 return cpu_get_host_ticks();
90 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
95 e1
= (addr
<< 16) | (limit
& 0xffff);
96 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
103 static uint64_t *idt_table
;
105 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
106 uint64_t addr
, unsigned int sel
)
109 e1
= (addr
& 0xffff) | (sel
<< 16);
110 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
114 p
[2] = tswap32(addr
>> 32);
117 /* only dpl matters as we do only user space emulation */
118 static void set_idt(int n
, unsigned int dpl
)
120 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
123 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
124 uint32_t addr
, unsigned int sel
)
127 e1
= (addr
& 0xffff) | (sel
<< 16);
128 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
134 /* only dpl matters as we do only user space emulation */
135 static void set_idt(int n
, unsigned int dpl
)
137 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
141 void cpu_loop(CPUX86State
*env
)
143 X86CPU
*cpu
= x86_env_get_cpu(env
);
144 CPUState
*cs
= CPU(cpu
);
147 //target_siginfo_t info;
151 trapnr
= cpu_exec(cs
);
153 process_queued_cpu_work(cs
);
157 /* syscall from int $0x80 */
158 if (bsd_type
== target_freebsd
) {
159 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
161 int32_t syscall_nr
= env
->regs
[R_EAX
];
162 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
164 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
165 get_user_s32(syscall_nr
, params
);
166 params
+= sizeof(int32_t);
167 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
168 get_user_s32(syscall_nr
, params
);
169 params
+= sizeof(int64_t);
171 get_user_s32(arg1
, params
);
172 params
+= sizeof(int32_t);
173 get_user_s32(arg2
, params
);
174 params
+= sizeof(int32_t);
175 get_user_s32(arg3
, params
);
176 params
+= sizeof(int32_t);
177 get_user_s32(arg4
, params
);
178 params
+= sizeof(int32_t);
179 get_user_s32(arg5
, params
);
180 params
+= sizeof(int32_t);
181 get_user_s32(arg6
, params
);
182 params
+= sizeof(int32_t);
183 get_user_s32(arg7
, params
);
184 params
+= sizeof(int32_t);
185 get_user_s32(arg8
, params
);
186 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
196 } else { //if (bsd_type == target_openbsd)
197 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
206 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
207 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
210 env
->eflags
&= ~CC_C
;
215 /* syscall from syscall instruction */
216 if (bsd_type
== target_freebsd
)
217 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
225 else { //if (bsd_type == target_openbsd)
226 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
235 env
->eip
= env
->exception_next_eip
;
236 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
237 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
240 env
->eflags
&= ~CC_C
;
247 info
.si_signo
= SIGBUS
;
249 info
.si_code
= TARGET_SI_KERNEL
;
250 info
._sifields
._sigfault
._addr
= 0;
251 queue_signal(env
, info
.si_signo
, &info
);
254 /* XXX: potential problem if ABI32 */
255 #ifndef TARGET_X86_64
256 if (env
->eflags
& VM_MASK
) {
257 handle_vm86_fault(env
);
261 info
.si_signo
= SIGSEGV
;
263 info
.si_code
= TARGET_SI_KERNEL
;
264 info
._sifields
._sigfault
._addr
= 0;
265 queue_signal(env
, info
.si_signo
, &info
);
269 info
.si_signo
= SIGSEGV
;
271 if (!(env
->error_code
& 1))
272 info
.si_code
= TARGET_SEGV_MAPERR
;
274 info
.si_code
= TARGET_SEGV_ACCERR
;
275 info
._sifields
._sigfault
._addr
= env
->cr
[2];
276 queue_signal(env
, info
.si_signo
, &info
);
279 #ifndef TARGET_X86_64
280 if (env
->eflags
& VM_MASK
) {
281 handle_vm86_trap(env
, trapnr
);
285 /* division by zero */
286 info
.si_signo
= SIGFPE
;
288 info
.si_code
= TARGET_FPE_INTDIV
;
289 info
._sifields
._sigfault
._addr
= env
->eip
;
290 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 info
.si_signo
= SIGTRAP
;
303 if (trapnr
== EXCP01_DB
) {
304 info
.si_code
= TARGET_TRAP_BRKPT
;
305 info
._sifields
._sigfault
._addr
= env
->eip
;
307 info
.si_code
= TARGET_SI_KERNEL
;
308 info
._sifields
._sigfault
._addr
= 0;
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
= SIGSEGV
;
323 info
.si_code
= TARGET_SI_KERNEL
;
324 info
._sifields
._sigfault
._addr
= 0;
325 queue_signal(env
, info
.si_signo
, &info
);
329 info
.si_signo
= SIGILL
;
331 info
.si_code
= TARGET_ILL_ILLOPN
;
332 info
._sifields
._sigfault
._addr
= env
->eip
;
333 queue_signal(env
, info
.si_signo
, &info
);
337 /* just indicate that signals should be handled asap */
344 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
349 info
.si_code
= TARGET_TRAP_BRKPT
;
350 queue_signal(env
, info
.si_signo
, &info
);
356 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
357 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
361 process_pending_signals(env
);
367 #define SPARC64_STACK_BIAS 2047
370 /* WARNING: dealing with register windows _is_ complicated. More info
371 can be found at http://www.sics.se/~psm/sparcstack.html */
372 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
374 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
375 /* wrap handling : if cwp is on the last window, then we use the
376 registers 'after' the end */
377 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
378 index
+= 16 * env
->nwindows
;
382 /* save the register window 'cwp1' */
383 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
388 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
389 #ifdef TARGET_SPARC64
391 sp_ptr
+= SPARC64_STACK_BIAS
;
393 #if defined(DEBUG_WIN)
394 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
397 for(i
= 0; i
< 16; i
++) {
398 /* FIXME - what to do if put_user() fails? */
399 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
400 sp_ptr
+= sizeof(abi_ulong
);
404 static void save_window(CPUSPARCState
*env
)
406 #ifndef TARGET_SPARC64
407 unsigned int new_wim
;
408 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
409 ((1LL << env
->nwindows
) - 1);
410 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
413 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
419 static void restore_window(CPUSPARCState
*env
)
421 #ifndef TARGET_SPARC64
422 unsigned int new_wim
;
424 unsigned int i
, cwp1
;
427 #ifndef TARGET_SPARC64
428 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
429 ((1LL << env
->nwindows
) - 1);
432 /* restore the invalid window */
433 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
434 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
435 #ifdef TARGET_SPARC64
437 sp_ptr
+= SPARC64_STACK_BIAS
;
439 #if defined(DEBUG_WIN)
440 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
443 for(i
= 0; i
< 16; i
++) {
444 /* FIXME - what to do if get_user() fails? */
445 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
446 sp_ptr
+= sizeof(abi_ulong
);
448 #ifdef TARGET_SPARC64
450 if (env
->cleanwin
< env
->nwindows
- 1)
458 static void flush_windows(CPUSPARCState
*env
)
464 /* if restore would invoke restore_window(), then we can stop */
465 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
466 #ifndef TARGET_SPARC64
467 if (env
->wim
& (1 << cwp1
))
470 if (env
->canrestore
== 0)
475 save_window_offset(env
, cwp1
);
478 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
479 #ifndef TARGET_SPARC64
480 /* set wim so that restore will reload the registers */
481 env
->wim
= 1 << cwp1
;
483 #if defined(DEBUG_WIN)
484 printf("flush_windows: nb=%d\n", offset
- 1);
488 void cpu_loop(CPUSPARCState
*env
)
490 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
491 int trapnr
, ret
, syscall_nr
;
492 //target_siginfo_t info;
496 trapnr
= cpu_exec(cs
);
498 process_queued_cpu_work(cs
);
501 #ifndef TARGET_SPARC64
504 /* FreeBSD uses 0x141 for syscalls too */
506 if (bsd_type
!= target_freebsd
)
510 syscall_nr
= env
->gregs
[1];
511 if (bsd_type
== target_freebsd
)
512 ret
= do_freebsd_syscall(env
, syscall_nr
,
513 env
->regwptr
[0], env
->regwptr
[1],
514 env
->regwptr
[2], env
->regwptr
[3],
515 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
516 else if (bsd_type
== target_netbsd
)
517 ret
= do_netbsd_syscall(env
, syscall_nr
,
518 env
->regwptr
[0], env
->regwptr
[1],
519 env
->regwptr
[2], env
->regwptr
[3],
520 env
->regwptr
[4], env
->regwptr
[5]);
521 else { //if (bsd_type == target_openbsd)
522 #if defined(TARGET_SPARC64)
523 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
524 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
526 ret
= do_openbsd_syscall(env
, syscall_nr
,
527 env
->regwptr
[0], env
->regwptr
[1],
528 env
->regwptr
[2], env
->regwptr
[3],
529 env
->regwptr
[4], env
->regwptr
[5]);
531 if ((unsigned int)ret
>= (unsigned int)(-515)) {
533 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
534 env
->xcc
|= PSR_CARRY
;
536 env
->psr
|= PSR_CARRY
;
539 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
540 env
->xcc
&= ~PSR_CARRY
;
542 env
->psr
&= ~PSR_CARRY
;
545 env
->regwptr
[0] = ret
;
546 /* next instruction */
547 #if defined(TARGET_SPARC64)
548 if (bsd_type
== target_openbsd
&&
549 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
550 env
->pc
= env
->gregs
[2];
551 env
->npc
= env
->pc
+ 4;
552 } else if (bsd_type
== target_openbsd
&&
553 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
554 env
->pc
= env
->gregs
[7];
555 env
->npc
= env
->pc
+ 4;
558 env
->npc
= env
->npc
+ 4;
562 env
->npc
= env
->npc
+ 4;
565 case 0x83: /* flush windows */
570 /* next instruction */
572 env
->npc
= env
->npc
+ 4;
574 #ifndef TARGET_SPARC64
575 case TT_WIN_OVF
: /* window overflow */
578 case TT_WIN_UNF
: /* window underflow */
585 info
.si_signo
= SIGSEGV
;
587 /* XXX: check env->error_code */
588 info
.si_code
= TARGET_SEGV_MAPERR
;
589 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
590 queue_signal(env
, info
.si_signo
, &info
);
595 case TT_SPILL
: /* window overflow */
598 case TT_FILL
: /* window underflow */
605 info
.si_signo
= SIGSEGV
;
607 /* XXX: check env->error_code */
608 info
.si_code
= TARGET_SEGV_MAPERR
;
609 if (trapnr
== TT_DFAULT
)
610 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
612 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
613 //queue_signal(env, info.si_signo, &info);
619 /* just indicate that signals should be handled asap */
625 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
631 info
.si_code
= TARGET_TRAP_BRKPT
;
632 //queue_signal(env, info.si_signo, &info);
638 #ifdef TARGET_SPARC64
641 printf ("Unhandled trap: 0x%x\n", trapnr
);
642 cpu_dump_state(cs
, stderr
, fprintf
, 0);
645 process_pending_signals (env
);
651 static void usage(void)
653 printf("qemu-" TARGET_NAME
" version " QEMU_VERSION QEMU_PKGVERSION
654 "\n" QEMU_COPYRIGHT
"\n"
655 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
656 "BSD CPU emulator (compiled for %s emulation)\n"
658 "Standard options:\n"
659 "-h print this help\n"
660 "-g port wait gdb connection to port\n"
661 "-L path set the elf interpreter prefix (default=%s)\n"
662 "-s size set the stack size in bytes (default=%ld)\n"
663 "-cpu model select CPU (-cpu help for list)\n"
664 "-drop-ld-preload drop LD_PRELOAD for target process\n"
665 "-E var=value sets/modifies targets environment variable(s)\n"
666 "-U var unsets targets environment variable(s)\n"
667 "-B address set guest_base address to address\n"
668 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
671 "-d item1[,...] enable logging of specified items\n"
672 " (use '-d help' for a list of log items)\n"
673 "-D logfile write logs to 'logfile' (default stderr)\n"
674 "-p pagesize set the host page size to 'pagesize'\n"
675 "-singlestep always run in singlestep mode\n"
676 "-strace log system calls\n"
677 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
678 " specify tracing options\n"
680 "Environment variables:\n"
681 "QEMU_STRACE Print system calls and arguments similar to the\n"
682 " 'strace' program. Enable by setting to any value.\n"
683 "You can use -E and -U options to set/unset environment variables\n"
684 "for target process. It is possible to provide several variables\n"
685 "by repeating the option. For example:\n"
686 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
687 "Note that if you provide several changes to single variable\n"
688 "last change will stay in effect.\n"
696 THREAD CPUState
*thread_cpu
;
698 bool qemu_cpu_is_self(CPUState
*cpu
)
700 return thread_cpu
== cpu
;
703 void qemu_cpu_kick(CPUState
*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 char *trace_file
= NULL
;
738 bsd_type
= target_openbsd
;
743 module_call_init(MODULE_INIT_TRACE
);
744 qemu_init_cpu_list();
745 module_call_init(MODULE_INIT_QOM
);
747 if ((envlist
= envlist_create()) == NULL
) {
748 (void) fprintf(stderr
, "Unable to allocate envlist\n");
752 /* add current environment into the list */
753 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
754 (void) envlist_setenv(envlist
, *wrk
);
759 qemu_add_opts(&qemu_trace_opts
);
770 if (!strcmp(r
, "-")) {
772 } else if (!strcmp(r
, "d")) {
773 if (optind
>= argc
) {
776 log_mask
= argv
[optind
++];
777 } else if (!strcmp(r
, "D")) {
778 if (optind
>= argc
) {
781 log_file
= argv
[optind
++];
782 } else if (!strcmp(r
, "E")) {
784 if (envlist_setenv(envlist
, r
) != 0)
786 } else if (!strcmp(r
, "ignore-environment")) {
787 envlist_free(envlist
);
788 if ((envlist
= envlist_create()) == NULL
) {
789 (void) fprintf(stderr
, "Unable to allocate envlist\n");
792 } else if (!strcmp(r
, "U")) {
794 if (envlist_unsetenv(envlist
, r
) != 0)
796 } else if (!strcmp(r
, "s")) {
798 x86_stack_size
= strtol(r
, (char **)&r
, 0);
799 if (x86_stack_size
<= 0)
802 x86_stack_size
*= 1024 * 1024;
803 else if (*r
== 'k' || *r
== 'K')
804 x86_stack_size
*= 1024;
805 } else if (!strcmp(r
, "L")) {
806 interp_prefix
= argv
[optind
++];
807 } else if (!strcmp(r
, "p")) {
808 qemu_host_page_size
= atoi(argv
[optind
++]);
809 if (qemu_host_page_size
== 0 ||
810 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
811 fprintf(stderr
, "page size must be a power of two\n");
814 } else if (!strcmp(r
, "g")) {
815 gdbstub_port
= atoi(argv
[optind
++]);
816 } else if (!strcmp(r
, "r")) {
817 qemu_uname_release
= argv
[optind
++];
818 } else if (!strcmp(r
, "cpu")) {
819 cpu_model
= argv
[optind
++];
820 if (is_help_option(cpu_model
)) {
821 /* XXX: implement xxx_cpu_list for targets that still miss it */
822 #if defined(cpu_list)
823 cpu_list(stdout
, &fprintf
);
827 } else if (!strcmp(r
, "B")) {
828 guest_base
= strtol(argv
[optind
++], NULL
, 0);
830 } else if (!strcmp(r
, "drop-ld-preload")) {
831 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
832 } else if (!strcmp(r
, "bsd")) {
833 if (!strcasecmp(argv
[optind
], "freebsd")) {
834 bsd_type
= target_freebsd
;
835 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
836 bsd_type
= target_netbsd
;
837 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
838 bsd_type
= target_openbsd
;
843 } else if (!strcmp(r
, "singlestep")) {
845 } else if (!strcmp(r
, "strace")) {
847 } else if (!strcmp(r
, "trace")) {
849 trace_file
= trace_opt_parse(optarg
);
856 qemu_log_needs_buffers();
857 qemu_set_log_filename(log_file
, &error_fatal
);
861 mask
= qemu_str_to_log_mask(log_mask
);
863 qemu_print_log_usage(stdout
);
869 if (optind
>= argc
) {
872 filename
= argv
[optind
];
874 if (!trace_init_backends()) {
877 trace_init_file(trace_file
);
880 memset(regs
, 0, sizeof(struct target_pt_regs
));
882 /* Zero out image_info */
883 memset(info
, 0, sizeof(struct image_info
));
885 /* Scan interp_prefix dir for replacement files. */
886 init_paths(interp_prefix
);
888 if (cpu_model
== NULL
) {
889 #if defined(TARGET_I386)
891 cpu_model
= "qemu64";
893 cpu_model
= "qemu32";
895 #elif defined(TARGET_SPARC)
896 #ifdef TARGET_SPARC64
897 cpu_model
= "TI UltraSparc II";
899 cpu_model
= "Fujitsu MB86904";
906 /* NOTE: we need to init the CPU at this stage to get
907 qemu_host_page_size */
908 cpu
= cpu_init(cpu_model
);
910 fprintf(stderr
, "Unable to find CPU definition\n");
914 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
919 if (getenv("QEMU_STRACE")) {
923 target_environ
= envlist_to_environ(envlist
, NULL
);
924 envlist_free(envlist
);
927 * Now that page sizes are configured in cpu_init() we can do
928 * proper page alignment for guest_base.
930 guest_base
= HOST_PAGE_ALIGN(guest_base
);
933 * Read in mmap_min_addr kernel parameter. This value is used
934 * When loading the ELF image to determine whether guest_base
937 * When user has explicitly set the quest base, we skip this
940 if (!have_guest_base
) {
943 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
945 if (fscanf(fp
, "%lu", &tmp
) == 1) {
947 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
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_loglevel_mask(CPU_LOG_PAGE
)) {
965 qemu_log("guest_base 0x%lx\n", guest_base
);
968 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
969 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
970 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
972 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
974 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
975 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
977 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
978 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
981 target_set_brk(info
->brk
);
985 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
986 generating the prologue until now so that the prologue can take
987 the real value of GUEST_BASE into account. */
988 tcg_prologue_init(&tcg_ctx
);
990 /* build Task State */
991 memset(ts
, 0, sizeof(TaskState
));
996 #if defined(TARGET_I386)
997 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
998 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
999 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
1000 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1001 env
->hflags
|= HF_OSFXSR_MASK
;
1003 #ifndef TARGET_ABI32
1004 /* enable 64 bit mode if possible */
1005 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1006 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1009 env
->cr
[4] |= CR4_PAE_MASK
;
1010 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1011 env
->hflags
|= HF_LMA_MASK
;
1014 /* flags setup : we activate the IRQs by default as in user mode */
1015 env
->eflags
|= IF_MASK
;
1017 /* linux register setup */
1018 #ifndef TARGET_ABI32
1019 env
->regs
[R_EAX
] = regs
->rax
;
1020 env
->regs
[R_EBX
] = regs
->rbx
;
1021 env
->regs
[R_ECX
] = regs
->rcx
;
1022 env
->regs
[R_EDX
] = regs
->rdx
;
1023 env
->regs
[R_ESI
] = regs
->rsi
;
1024 env
->regs
[R_EDI
] = regs
->rdi
;
1025 env
->regs
[R_EBP
] = regs
->rbp
;
1026 env
->regs
[R_ESP
] = regs
->rsp
;
1027 env
->eip
= regs
->rip
;
1029 env
->regs
[R_EAX
] = regs
->eax
;
1030 env
->regs
[R_EBX
] = regs
->ebx
;
1031 env
->regs
[R_ECX
] = regs
->ecx
;
1032 env
->regs
[R_EDX
] = regs
->edx
;
1033 env
->regs
[R_ESI
] = regs
->esi
;
1034 env
->regs
[R_EDI
] = regs
->edi
;
1035 env
->regs
[R_EBP
] = regs
->ebp
;
1036 env
->regs
[R_ESP
] = regs
->esp
;
1037 env
->eip
= regs
->eip
;
1040 /* linux interrupt setup */
1041 #ifndef TARGET_ABI32
1042 env
->idt
.limit
= 511;
1044 env
->idt
.limit
= 255;
1046 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1047 PROT_READ
|PROT_WRITE
,
1048 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1049 idt_table
= g2h(env
->idt
.base
);
1072 /* linux segment setup */
1074 uint64_t *gdt_table
;
1075 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1076 PROT_READ
|PROT_WRITE
,
1077 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1078 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1079 gdt_table
= g2h(env
->gdt
.base
);
1081 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1082 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1083 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1085 /* 64 bit code segment */
1086 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1087 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1089 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1091 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1092 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1093 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1096 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1097 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1099 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1100 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1101 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1102 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1103 /* This hack makes Wine work... */
1104 env
->segs
[R_FS
].selector
= 0;
1106 cpu_x86_load_seg(env
, R_DS
, 0);
1107 cpu_x86_load_seg(env
, R_ES
, 0);
1108 cpu_x86_load_seg(env
, R_FS
, 0);
1109 cpu_x86_load_seg(env
, R_GS
, 0);
1111 #elif defined(TARGET_SPARC)
1115 env
->npc
= regs
->npc
;
1117 for(i
= 0; i
< 8; i
++)
1118 env
->gregs
[i
] = regs
->u_regs
[i
];
1119 for(i
= 0; i
< 8; i
++)
1120 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1123 #error unsupported target CPU
1127 gdbserver_start (gdbstub_port
);
1128 gdb_handlesig(cpu
, 0);