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/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qemu/units.h"
23 #include "qemu/accel.h"
24 #include "sysemu/tcg.h"
25 #include "qemu-version.h"
26 #include <machine/trap.h>
28 #include "qapi/error.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/path.h"
33 #include "qemu/help_option.h"
34 #include "qemu/module.h"
36 #include "exec/exec-all.h"
38 #include "qemu/timer.h"
39 #include "qemu/envlist.h"
41 #include "trace/control.h"
44 unsigned long mmap_min_addr
;
47 unsigned long reserved_va
;
49 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
50 const char *qemu_uname_release
;
51 extern char **environ
;
52 enum BSDType bsd_type
;
54 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
55 we allocate a bigger stack. Need a better solution, for example
56 by remapping the process stack directly at the right place */
57 unsigned long x86_stack_size
= 512 * 1024;
59 void gemu_log(const char *fmt
, ...)
64 vfprintf(stderr
, fmt
, ap
);
68 #if defined(TARGET_I386)
69 int cpu_get_pic_interrupt(CPUX86State
*env
)
79 void fork_end(int child
)
82 gdbserver_fork(thread_cpu
);
87 /***********************************************************/
88 /* CPUX86 core interface */
90 uint64_t cpu_get_tsc(CPUX86State
*env
)
92 return cpu_get_host_ticks();
95 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
100 e1
= (addr
<< 16) | (limit
& 0xffff);
101 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
108 static uint64_t *idt_table
;
110 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
111 uint64_t addr
, unsigned int sel
)
114 e1
= (addr
& 0xffff) | (sel
<< 16);
115 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
119 p
[2] = tswap32(addr
>> 32);
122 /* only dpl matters as we do only user space emulation */
123 static void set_idt(int n
, unsigned int dpl
)
125 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
128 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
129 uint32_t addr
, unsigned int sel
)
132 e1
= (addr
& 0xffff) | (sel
<< 16);
133 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
139 /* only dpl matters as we do only user space emulation */
140 static void set_idt(int n
, unsigned int dpl
)
142 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
146 void cpu_loop(CPUX86State
*env
)
148 CPUState
*cs
= env_cpu(env
);
151 //target_siginfo_t info;
155 trapnr
= cpu_exec(cs
);
157 process_queued_cpu_work(cs
);
161 /* syscall from int $0x80 */
162 if (bsd_type
== target_freebsd
) {
163 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
165 int32_t syscall_nr
= env
->regs
[R_EAX
];
166 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
168 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
169 get_user_s32(syscall_nr
, params
);
170 params
+= sizeof(int32_t);
171 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
172 get_user_s32(syscall_nr
, params
);
173 params
+= sizeof(int64_t);
175 get_user_s32(arg1
, params
);
176 params
+= sizeof(int32_t);
177 get_user_s32(arg2
, params
);
178 params
+= sizeof(int32_t);
179 get_user_s32(arg3
, params
);
180 params
+= sizeof(int32_t);
181 get_user_s32(arg4
, params
);
182 params
+= sizeof(int32_t);
183 get_user_s32(arg5
, params
);
184 params
+= sizeof(int32_t);
185 get_user_s32(arg6
, params
);
186 params
+= sizeof(int32_t);
187 get_user_s32(arg7
, params
);
188 params
+= sizeof(int32_t);
189 get_user_s32(arg8
, params
);
190 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
200 } else { //if (bsd_type == target_openbsd)
201 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
210 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
211 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
214 env
->eflags
&= ~CC_C
;
219 /* syscall from syscall instruction */
220 if (bsd_type
== target_freebsd
)
221 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
229 else { //if (bsd_type == target_openbsd)
230 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
239 env
->eip
= env
->exception_next_eip
;
240 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
241 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
244 env
->eflags
&= ~CC_C
;
251 info
.si_signo
= SIGBUS
;
253 info
.si_code
= TARGET_SI_KERNEL
;
254 info
._sifields
._sigfault
._addr
= 0;
255 queue_signal(env
, info
.si_signo
, &info
);
258 /* XXX: potential problem if ABI32 */
259 #ifndef TARGET_X86_64
260 if (env
->eflags
& VM_MASK
) {
261 handle_vm86_fault(env
);
265 info
.si_signo
= SIGSEGV
;
267 info
.si_code
= TARGET_SI_KERNEL
;
268 info
._sifields
._sigfault
._addr
= 0;
269 queue_signal(env
, info
.si_signo
, &info
);
273 info
.si_signo
= SIGSEGV
;
275 if (!(env
->error_code
& 1))
276 info
.si_code
= TARGET_SEGV_MAPERR
;
278 info
.si_code
= TARGET_SEGV_ACCERR
;
279 info
._sifields
._sigfault
._addr
= env
->cr
[2];
280 queue_signal(env
, info
.si_signo
, &info
);
283 #ifndef TARGET_X86_64
284 if (env
->eflags
& VM_MASK
) {
285 handle_vm86_trap(env
, trapnr
);
289 /* division by zero */
290 info
.si_signo
= SIGFPE
;
292 info
.si_code
= TARGET_FPE_INTDIV
;
293 info
._sifields
._sigfault
._addr
= env
->eip
;
294 queue_signal(env
, info
.si_signo
, &info
);
299 #ifndef TARGET_X86_64
300 if (env
->eflags
& VM_MASK
) {
301 handle_vm86_trap(env
, trapnr
);
305 info
.si_signo
= SIGTRAP
;
307 if (trapnr
== EXCP01_DB
) {
308 info
.si_code
= TARGET_TRAP_BRKPT
;
309 info
._sifields
._sigfault
._addr
= env
->eip
;
311 info
.si_code
= TARGET_SI_KERNEL
;
312 info
._sifields
._sigfault
._addr
= 0;
314 queue_signal(env
, info
.si_signo
, &info
);
319 #ifndef TARGET_X86_64
320 if (env
->eflags
& VM_MASK
) {
321 handle_vm86_trap(env
, trapnr
);
325 info
.si_signo
= SIGSEGV
;
327 info
.si_code
= TARGET_SI_KERNEL
;
328 info
._sifields
._sigfault
._addr
= 0;
329 queue_signal(env
, info
.si_signo
, &info
);
333 info
.si_signo
= SIGILL
;
335 info
.si_code
= TARGET_ILL_ILLOPN
;
336 info
._sifields
._sigfault
._addr
= env
->eip
;
337 queue_signal(env
, info
.si_signo
, &info
);
341 /* just indicate that signals should be handled asap */
348 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
353 info
.si_code
= TARGET_TRAP_BRKPT
;
354 queue_signal(env
, info
.si_signo
, &info
);
360 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
361 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
365 process_pending_signals(env
);
371 #define SPARC64_STACK_BIAS 2047
374 /* WARNING: dealing with register windows _is_ complicated. More info
375 can be found at http://www.sics.se/~psm/sparcstack.html */
376 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
378 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
379 /* wrap handling : if cwp is on the last window, then we use the
380 registers 'after' the end */
381 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
382 index
+= 16 * env
->nwindows
;
386 /* save the register window 'cwp1' */
387 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
392 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
393 #ifdef TARGET_SPARC64
395 sp_ptr
+= SPARC64_STACK_BIAS
;
397 #if defined(DEBUG_WIN)
398 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
401 for(i
= 0; i
< 16; i
++) {
402 /* FIXME - what to do if put_user() fails? */
403 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
404 sp_ptr
+= sizeof(abi_ulong
);
408 static void save_window(CPUSPARCState
*env
)
410 #ifndef TARGET_SPARC64
411 unsigned int new_wim
;
412 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
413 ((1LL << env
->nwindows
) - 1);
414 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
418 * cansave is zero if the spill trap handler is triggered by `save` and
419 * nonzero if triggered by a `flushw`
421 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- env
->cansave
- 2));
427 static void restore_window(CPUSPARCState
*env
)
429 #ifndef TARGET_SPARC64
430 unsigned int new_wim
;
432 unsigned int i
, cwp1
;
435 #ifndef TARGET_SPARC64
436 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
437 ((1LL << env
->nwindows
) - 1);
440 /* restore the invalid window */
441 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
442 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
443 #ifdef TARGET_SPARC64
445 sp_ptr
+= SPARC64_STACK_BIAS
;
447 #if defined(DEBUG_WIN)
448 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
451 for(i
= 0; i
< 16; i
++) {
452 /* FIXME - what to do if get_user() fails? */
453 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
454 sp_ptr
+= sizeof(abi_ulong
);
456 #ifdef TARGET_SPARC64
458 if (env
->cleanwin
< env
->nwindows
- 1)
466 static void flush_windows(CPUSPARCState
*env
)
472 /* if restore would invoke restore_window(), then we can stop */
473 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
474 #ifndef TARGET_SPARC64
475 if (env
->wim
& (1 << cwp1
))
478 if (env
->canrestore
== 0)
483 save_window_offset(env
, cwp1
);
486 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
487 #ifndef TARGET_SPARC64
488 /* set wim so that restore will reload the registers */
489 env
->wim
= 1 << cwp1
;
491 #if defined(DEBUG_WIN)
492 printf("flush_windows: nb=%d\n", offset
- 1);
496 void cpu_loop(CPUSPARCState
*env
)
498 CPUState
*cs
= env_cpu(env
);
499 int trapnr
, ret
, syscall_nr
;
500 //target_siginfo_t info;
504 trapnr
= cpu_exec(cs
);
506 process_queued_cpu_work(cs
);
509 #ifndef TARGET_SPARC64
512 /* FreeBSD uses 0x141 for syscalls too */
514 if (bsd_type
!= target_freebsd
)
519 syscall_nr
= env
->gregs
[1];
520 if (bsd_type
== target_freebsd
)
521 ret
= do_freebsd_syscall(env
, syscall_nr
,
522 env
->regwptr
[0], env
->regwptr
[1],
523 env
->regwptr
[2], env
->regwptr
[3],
524 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
525 else if (bsd_type
== target_netbsd
)
526 ret
= do_netbsd_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]);
530 else { //if (bsd_type == target_openbsd)
531 #if defined(TARGET_SPARC64)
532 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
533 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
535 ret
= do_openbsd_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]);
540 if ((unsigned int)ret
>= (unsigned int)(-515)) {
542 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
543 env
->xcc
|= PSR_CARRY
;
545 env
->psr
|= PSR_CARRY
;
548 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
549 env
->xcc
&= ~PSR_CARRY
;
551 env
->psr
&= ~PSR_CARRY
;
554 env
->regwptr
[0] = ret
;
555 /* next instruction */
556 #if defined(TARGET_SPARC64)
557 if (bsd_type
== target_openbsd
&&
558 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
559 env
->pc
= env
->gregs
[2];
560 env
->npc
= env
->pc
+ 4;
561 } else if (bsd_type
== target_openbsd
&&
562 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
563 env
->pc
= env
->gregs
[7];
564 env
->npc
= env
->pc
+ 4;
567 env
->npc
= env
->npc
+ 4;
571 env
->npc
= env
->npc
+ 4;
574 case 0x83: /* flush windows */
579 /* next instruction */
581 env
->npc
= env
->npc
+ 4;
583 #ifndef TARGET_SPARC64
584 case TT_WIN_OVF
: /* window overflow */
587 case TT_WIN_UNF
: /* window underflow */
594 info
.si_signo
= SIGSEGV
;
596 /* XXX: check env->error_code */
597 info
.si_code
= TARGET_SEGV_MAPERR
;
598 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
599 queue_signal(env
, info
.si_signo
, &info
);
604 case TT_SPILL
: /* window overflow */
607 case TT_FILL
: /* window underflow */
614 info
.si_signo
= SIGSEGV
;
616 /* XXX: check env->error_code */
617 info
.si_code
= TARGET_SEGV_MAPERR
;
618 if (trapnr
== TT_DFAULT
)
619 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
621 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
622 //queue_signal(env, info.si_signo, &info);
628 /* just indicate that signals should be handled asap */
635 gdb_handlesig(cs
, TARGET_SIGTRAP
);
641 info
.si_code
= TARGET_TRAP_BRKPT
;
642 //queue_signal(env, info.si_signo, &info);
648 #ifdef TARGET_SPARC64
651 printf ("Unhandled trap: 0x%x\n", trapnr
);
652 cpu_dump_state(cs
, stderr
, 0);
655 process_pending_signals (env
);
661 static void usage(void)
663 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
664 "\n" QEMU_COPYRIGHT
"\n"
665 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
666 "BSD CPU emulator (compiled for %s emulation)\n"
668 "Standard options:\n"
669 "-h print this help\n"
670 "-g port wait gdb connection to port\n"
671 "-L path set the elf interpreter prefix (default=%s)\n"
672 "-s size set the stack size in bytes (default=%ld)\n"
673 "-cpu model select CPU (-cpu help for list)\n"
674 "-drop-ld-preload drop LD_PRELOAD for target process\n"
675 "-E var=value sets/modifies targets environment variable(s)\n"
676 "-U var unsets targets environment variable(s)\n"
677 "-B address set guest_base address to address\n"
678 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
681 "-d item1[,...] enable logging of specified items\n"
682 " (use '-d help' for a list of log items)\n"
683 "-D logfile write logs to 'logfile' (default stderr)\n"
684 "-p pagesize set the host page size to 'pagesize'\n"
685 "-singlestep always run in singlestep mode\n"
686 "-strace log system calls\n"
687 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
688 " specify tracing options\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"
700 QEMU_HELP_BOTTOM
"\n"
708 THREAD CPUState
*thread_cpu
;
710 bool qemu_cpu_is_self(CPUState
*cpu
)
712 return thread_cpu
== cpu
;
715 void qemu_cpu_kick(CPUState
*cpu
)
720 /* Assumes contents are already zeroed. */
721 void init_task_state(TaskState
*ts
)
726 ts
->first_free
= ts
->sigqueue_table
;
727 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
728 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
730 ts
->sigqueue_table
[i
].next
= NULL
;
733 int main(int argc
, char **argv
)
735 const char *filename
;
736 const char *cpu_model
;
737 const char *cpu_type
;
738 const char *log_file
= NULL
;
739 const char *log_mask
= NULL
;
740 struct target_pt_regs regs1
, *regs
= ®s1
;
741 struct image_info info1
, *info
= &info1
;
742 TaskState ts1
, *ts
= &ts1
;
747 const char *gdbstub
= NULL
;
748 char **target_environ
, **wrk
;
749 envlist_t
*envlist
= NULL
;
750 bsd_type
= target_openbsd
;
756 module_call_init(MODULE_INIT_TRACE
);
757 qemu_init_cpu_list();
758 module_call_init(MODULE_INIT_QOM
);
760 envlist
= envlist_create();
762 /* add current environment into the list */
763 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
764 (void) envlist_setenv(envlist
, *wrk
);
769 qemu_add_opts(&qemu_trace_opts
);
780 if (!strcmp(r
, "-")) {
782 } else if (!strcmp(r
, "d")) {
783 if (optind
>= argc
) {
786 log_mask
= argv
[optind
++];
787 } else if (!strcmp(r
, "D")) {
788 if (optind
>= argc
) {
791 log_file
= argv
[optind
++];
792 } else if (!strcmp(r
, "E")) {
794 if (envlist_setenv(envlist
, r
) != 0)
796 } else if (!strcmp(r
, "ignore-environment")) {
797 envlist_free(envlist
);
798 envlist
= envlist_create();
799 } else if (!strcmp(r
, "U")) {
801 if (envlist_unsetenv(envlist
, r
) != 0)
803 } else if (!strcmp(r
, "s")) {
805 x86_stack_size
= strtol(r
, (char **)&r
, 0);
806 if (x86_stack_size
<= 0)
809 x86_stack_size
*= MiB
;
810 else if (*r
== 'k' || *r
== 'K')
811 x86_stack_size
*= KiB
;
812 } else if (!strcmp(r
, "L")) {
813 interp_prefix
= argv
[optind
++];
814 } else if (!strcmp(r
, "p")) {
815 qemu_host_page_size
= atoi(argv
[optind
++]);
816 if (qemu_host_page_size
== 0 ||
817 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
818 fprintf(stderr
, "page size must be a power of two\n");
821 } else if (!strcmp(r
, "g")) {
822 gdbstub
= g_strdup(argv
[optind
++]);
823 } else if (!strcmp(r
, "r")) {
824 qemu_uname_release
= argv
[optind
++];
825 } else if (!strcmp(r
, "cpu")) {
826 cpu_model
= argv
[optind
++];
827 if (is_help_option(cpu_model
)) {
828 /* XXX: implement xxx_cpu_list for targets that still miss it */
829 #if defined(cpu_list)
834 } else if (!strcmp(r
, "B")) {
835 guest_base
= strtol(argv
[optind
++], NULL
, 0);
836 have_guest_base
= true;
837 } else if (!strcmp(r
, "drop-ld-preload")) {
838 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
839 } else if (!strcmp(r
, "bsd")) {
840 if (!strcasecmp(argv
[optind
], "freebsd")) {
841 bsd_type
= target_freebsd
;
842 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
843 bsd_type
= target_netbsd
;
844 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
845 bsd_type
= target_openbsd
;
850 } else if (!strcmp(r
, "singlestep")) {
852 } else if (!strcmp(r
, "strace")) {
854 } else if (!strcmp(r
, "trace")) {
855 trace_opt_parse(optarg
);
862 qemu_log_needs_buffers();
863 qemu_set_log_filename(log_file
, &error_fatal
);
867 mask
= qemu_str_to_log_mask(log_mask
);
869 qemu_print_log_usage(stdout
);
875 if (optind
>= argc
) {
878 filename
= argv
[optind
];
880 if (!trace_init_backends()) {
886 memset(regs
, 0, sizeof(struct target_pt_regs
));
888 /* Zero out image_info */
889 memset(info
, 0, sizeof(struct image_info
));
891 /* Scan interp_prefix dir for replacement files. */
892 init_paths(interp_prefix
);
894 if (cpu_model
== NULL
) {
895 #if defined(TARGET_I386)
897 cpu_model
= "qemu64";
899 cpu_model
= "qemu32";
901 #elif defined(TARGET_SPARC)
902 #ifdef TARGET_SPARC64
903 cpu_model
= "TI UltraSparc II";
905 cpu_model
= "Fujitsu MB86904";
912 cpu_type
= parse_cpu_option(cpu_model
);
913 /* init tcg before creating CPUs and to get qemu_host_page_size */
915 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
917 ac
->init_machine(NULL
);
918 accel_init_interfaces(ac
);
920 cpu
= cpu_create(cpu_type
);
922 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
927 if (getenv("QEMU_STRACE")) {
931 target_environ
= envlist_to_environ(envlist
, NULL
);
932 envlist_free(envlist
);
935 * Now that page sizes are configured in tcg_exec_init() we can do
936 * proper page alignment for guest_base.
938 guest_base
= HOST_PAGE_ALIGN(guest_base
);
941 * Read in mmap_min_addr kernel parameter. This value is used
942 * When loading the ELF image to determine whether guest_base
945 * When user has explicitly set the quest base, we skip this
948 if (!have_guest_base
) {
951 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
953 if (fscanf(fp
, "%lu", &tmp
) == 1) {
955 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
961 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
962 printf("Error loading %s\n", filename
);
966 for (wrk
= target_environ
; *wrk
; wrk
++) {
970 g_free(target_environ
);
972 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
973 qemu_log("guest_base %p\n", (void *)guest_base
);
974 log_page_dump("binary load");
976 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
977 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
978 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
980 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
982 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
983 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
985 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
986 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
989 target_set_brk(info
->brk
);
993 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
994 generating the prologue until now so that the prologue can take
995 the real value of GUEST_BASE into account. */
996 tcg_prologue_init(tcg_ctx
);
999 /* build Task State */
1000 memset(ts
, 0, sizeof(TaskState
));
1001 init_task_state(ts
);
1005 #if defined(TARGET_I386)
1006 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
1007 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
1008 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
1009 env
->cr
[4] |= CR4_OSFXSR_MASK
;
1010 env
->hflags
|= HF_OSFXSR_MASK
;
1012 #ifndef TARGET_ABI32
1013 /* enable 64 bit mode if possible */
1014 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
1015 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1018 env
->cr
[4] |= CR4_PAE_MASK
;
1019 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1020 env
->hflags
|= HF_LMA_MASK
;
1023 /* flags setup : we activate the IRQs by default as in user mode */
1024 env
->eflags
|= IF_MASK
;
1026 /* linux register setup */
1027 #ifndef TARGET_ABI32
1028 env
->regs
[R_EAX
] = regs
->rax
;
1029 env
->regs
[R_EBX
] = regs
->rbx
;
1030 env
->regs
[R_ECX
] = regs
->rcx
;
1031 env
->regs
[R_EDX
] = regs
->rdx
;
1032 env
->regs
[R_ESI
] = regs
->rsi
;
1033 env
->regs
[R_EDI
] = regs
->rdi
;
1034 env
->regs
[R_EBP
] = regs
->rbp
;
1035 env
->regs
[R_ESP
] = regs
->rsp
;
1036 env
->eip
= regs
->rip
;
1038 env
->regs
[R_EAX
] = regs
->eax
;
1039 env
->regs
[R_EBX
] = regs
->ebx
;
1040 env
->regs
[R_ECX
] = regs
->ecx
;
1041 env
->regs
[R_EDX
] = regs
->edx
;
1042 env
->regs
[R_ESI
] = regs
->esi
;
1043 env
->regs
[R_EDI
] = regs
->edi
;
1044 env
->regs
[R_EBP
] = regs
->ebp
;
1045 env
->regs
[R_ESP
] = regs
->esp
;
1046 env
->eip
= regs
->eip
;
1049 /* linux interrupt setup */
1050 #ifndef TARGET_ABI32
1051 env
->idt
.limit
= 511;
1053 env
->idt
.limit
= 255;
1055 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1056 PROT_READ
|PROT_WRITE
,
1057 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1058 idt_table
= g2h_untagged(env
->idt
.base
);
1081 /* linux segment setup */
1083 uint64_t *gdt_table
;
1084 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1085 PROT_READ
|PROT_WRITE
,
1086 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1087 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1088 gdt_table
= g2h_untagged(env
->gdt
.base
);
1090 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1091 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1092 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1094 /* 64 bit code segment */
1095 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1096 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1098 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1100 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1101 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1102 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1105 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1106 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1108 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1109 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1110 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1111 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1112 /* This hack makes Wine work... */
1113 env
->segs
[R_FS
].selector
= 0;
1115 cpu_x86_load_seg(env
, R_DS
, 0);
1116 cpu_x86_load_seg(env
, R_ES
, 0);
1117 cpu_x86_load_seg(env
, R_FS
, 0);
1118 cpu_x86_load_seg(env
, R_GS
, 0);
1120 #elif defined(TARGET_SPARC)
1124 env
->npc
= regs
->npc
;
1126 for(i
= 0; i
< 8; i
++)
1127 env
->gregs
[i
] = regs
->u_regs
[i
];
1128 for(i
= 0; i
< 8; i
++)
1129 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1132 #error unsupported target CPU
1136 gdbserver_start(gdbstub
);
1137 gdb_handlesig(cpu
, 0);