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"
29 #include "exec/exec-all.h"
31 #include "qemu/timer.h"
32 #include "qemu/envlist.h"
34 #include "trace/control.h"
35 #include "glib-compat.h"
38 unsigned long mmap_min_addr
;
39 unsigned long guest_base
;
41 unsigned long reserved_va
;
43 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
44 const char *qemu_uname_release
;
45 extern char **environ
;
46 enum BSDType bsd_type
;
48 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
49 we allocate a bigger stack. Need a better solution, for example
50 by remapping the process stack directly at the right place */
51 unsigned long x86_stack_size
= 512 * 1024;
53 void gemu_log(const char *fmt
, ...)
58 vfprintf(stderr
, fmt
, ap
);
62 #if defined(TARGET_I386)
63 int cpu_get_pic_interrupt(CPUX86State
*env
)
73 void fork_end(int child
)
76 gdbserver_fork(thread_cpu
);
81 /***********************************************************/
82 /* CPUX86 core interface */
84 uint64_t cpu_get_tsc(CPUX86State
*env
)
86 return cpu_get_host_ticks();
89 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
94 e1
= (addr
<< 16) | (limit
& 0xffff);
95 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
102 static uint64_t *idt_table
;
104 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
105 uint64_t addr
, unsigned int sel
)
108 e1
= (addr
& 0xffff) | (sel
<< 16);
109 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
113 p
[2] = tswap32(addr
>> 32);
116 /* only dpl matters as we do only user space emulation */
117 static void set_idt(int n
, unsigned int dpl
)
119 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
122 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
123 uint32_t addr
, unsigned int sel
)
126 e1
= (addr
& 0xffff) | (sel
<< 16);
127 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
133 /* only dpl matters as we do only user space emulation */
134 static void set_idt(int n
, unsigned int dpl
)
136 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
140 void cpu_loop(CPUX86State
*env
)
142 X86CPU
*cpu
= x86_env_get_cpu(env
);
143 CPUState
*cs
= CPU(cpu
);
146 //target_siginfo_t info;
150 trapnr
= cpu_exec(cs
);
152 process_queued_cpu_work(cs
);
156 /* syscall from int $0x80 */
157 if (bsd_type
== target_freebsd
) {
158 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
160 int32_t syscall_nr
= env
->regs
[R_EAX
];
161 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
163 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
164 get_user_s32(syscall_nr
, params
);
165 params
+= sizeof(int32_t);
166 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
167 get_user_s32(syscall_nr
, params
);
168 params
+= sizeof(int64_t);
170 get_user_s32(arg1
, params
);
171 params
+= sizeof(int32_t);
172 get_user_s32(arg2
, params
);
173 params
+= sizeof(int32_t);
174 get_user_s32(arg3
, params
);
175 params
+= sizeof(int32_t);
176 get_user_s32(arg4
, params
);
177 params
+= sizeof(int32_t);
178 get_user_s32(arg5
, params
);
179 params
+= sizeof(int32_t);
180 get_user_s32(arg6
, params
);
181 params
+= sizeof(int32_t);
182 get_user_s32(arg7
, params
);
183 params
+= sizeof(int32_t);
184 get_user_s32(arg8
, params
);
185 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
195 } else { //if (bsd_type == target_openbsd)
196 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
205 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
206 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
209 env
->eflags
&= ~CC_C
;
214 /* syscall from syscall instruction */
215 if (bsd_type
== target_freebsd
)
216 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
224 else { //if (bsd_type == target_openbsd)
225 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
234 env
->eip
= env
->exception_next_eip
;
235 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
236 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
239 env
->eflags
&= ~CC_C
;
246 info
.si_signo
= SIGBUS
;
248 info
.si_code
= TARGET_SI_KERNEL
;
249 info
._sifields
._sigfault
._addr
= 0;
250 queue_signal(env
, info
.si_signo
, &info
);
253 /* XXX: potential problem if ABI32 */
254 #ifndef TARGET_X86_64
255 if (env
->eflags
& VM_MASK
) {
256 handle_vm86_fault(env
);
260 info
.si_signo
= SIGSEGV
;
262 info
.si_code
= TARGET_SI_KERNEL
;
263 info
._sifields
._sigfault
._addr
= 0;
264 queue_signal(env
, info
.si_signo
, &info
);
268 info
.si_signo
= SIGSEGV
;
270 if (!(env
->error_code
& 1))
271 info
.si_code
= TARGET_SEGV_MAPERR
;
273 info
.si_code
= TARGET_SEGV_ACCERR
;
274 info
._sifields
._sigfault
._addr
= env
->cr
[2];
275 queue_signal(env
, info
.si_signo
, &info
);
278 #ifndef TARGET_X86_64
279 if (env
->eflags
& VM_MASK
) {
280 handle_vm86_trap(env
, trapnr
);
284 /* division by zero */
285 info
.si_signo
= SIGFPE
;
287 info
.si_code
= TARGET_FPE_INTDIV
;
288 info
._sifields
._sigfault
._addr
= env
->eip
;
289 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 info
.si_signo
= SIGTRAP
;
302 if (trapnr
== EXCP01_DB
) {
303 info
.si_code
= TARGET_TRAP_BRKPT
;
304 info
._sifields
._sigfault
._addr
= env
->eip
;
306 info
.si_code
= TARGET_SI_KERNEL
;
307 info
._sifields
._sigfault
._addr
= 0;
309 queue_signal(env
, info
.si_signo
, &info
);
314 #ifndef TARGET_X86_64
315 if (env
->eflags
& VM_MASK
) {
316 handle_vm86_trap(env
, trapnr
);
320 info
.si_signo
= SIGSEGV
;
322 info
.si_code
= TARGET_SI_KERNEL
;
323 info
._sifields
._sigfault
._addr
= 0;
324 queue_signal(env
, info
.si_signo
, &info
);
328 info
.si_signo
= SIGILL
;
330 info
.si_code
= TARGET_ILL_ILLOPN
;
331 info
._sifields
._sigfault
._addr
= env
->eip
;
332 queue_signal(env
, info
.si_signo
, &info
);
336 /* just indicate that signals should be handled asap */
343 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
348 info
.si_code
= TARGET_TRAP_BRKPT
;
349 queue_signal(env
, info
.si_signo
, &info
);
355 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
356 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
360 process_pending_signals(env
);
366 #define SPARC64_STACK_BIAS 2047
369 /* WARNING: dealing with register windows _is_ complicated. More info
370 can be found at http://www.sics.se/~psm/sparcstack.html */
371 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
373 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
374 /* wrap handling : if cwp is on the last window, then we use the
375 registers 'after' the end */
376 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
377 index
+= 16 * env
->nwindows
;
381 /* save the register window 'cwp1' */
382 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
387 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
388 #ifdef TARGET_SPARC64
390 sp_ptr
+= SPARC64_STACK_BIAS
;
392 #if defined(DEBUG_WIN)
393 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
396 for(i
= 0; i
< 16; i
++) {
397 /* FIXME - what to do if put_user() fails? */
398 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
399 sp_ptr
+= sizeof(abi_ulong
);
403 static void save_window(CPUSPARCState
*env
)
405 #ifndef TARGET_SPARC64
406 unsigned int new_wim
;
407 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
408 ((1LL << env
->nwindows
) - 1);
409 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
412 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
418 static void restore_window(CPUSPARCState
*env
)
420 #ifndef TARGET_SPARC64
421 unsigned int new_wim
;
423 unsigned int i
, cwp1
;
426 #ifndef TARGET_SPARC64
427 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
428 ((1LL << env
->nwindows
) - 1);
431 /* restore the invalid window */
432 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
433 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
434 #ifdef TARGET_SPARC64
436 sp_ptr
+= SPARC64_STACK_BIAS
;
438 #if defined(DEBUG_WIN)
439 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
442 for(i
= 0; i
< 16; i
++) {
443 /* FIXME - what to do if get_user() fails? */
444 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
445 sp_ptr
+= sizeof(abi_ulong
);
447 #ifdef TARGET_SPARC64
449 if (env
->cleanwin
< env
->nwindows
- 1)
457 static void flush_windows(CPUSPARCState
*env
)
463 /* if restore would invoke restore_window(), then we can stop */
464 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
465 #ifndef TARGET_SPARC64
466 if (env
->wim
& (1 << cwp1
))
469 if (env
->canrestore
== 0)
474 save_window_offset(env
, cwp1
);
477 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
478 #ifndef TARGET_SPARC64
479 /* set wim so that restore will reload the registers */
480 env
->wim
= 1 << cwp1
;
482 #if defined(DEBUG_WIN)
483 printf("flush_windows: nb=%d\n", offset
- 1);
487 void cpu_loop(CPUSPARCState
*env
)
489 CPUState
*cs
= CPU(sparc_env_get_cpu(env
));
490 int trapnr
, ret
, syscall_nr
;
491 //target_siginfo_t info;
495 trapnr
= cpu_exec(cs
);
497 process_queued_cpu_work(cs
);
500 #ifndef TARGET_SPARC64
503 /* FreeBSD uses 0x141 for syscalls too */
505 if (bsd_type
!= target_freebsd
)
509 syscall_nr
= env
->gregs
[1];
510 if (bsd_type
== target_freebsd
)
511 ret
= do_freebsd_syscall(env
, syscall_nr
,
512 env
->regwptr
[0], env
->regwptr
[1],
513 env
->regwptr
[2], env
->regwptr
[3],
514 env
->regwptr
[4], env
->regwptr
[5], 0, 0);
515 else if (bsd_type
== target_netbsd
)
516 ret
= do_netbsd_syscall(env
, syscall_nr
,
517 env
->regwptr
[0], env
->regwptr
[1],
518 env
->regwptr
[2], env
->regwptr
[3],
519 env
->regwptr
[4], env
->regwptr
[5]);
520 else { //if (bsd_type == target_openbsd)
521 #if defined(TARGET_SPARC64)
522 syscall_nr
&= ~(TARGET_OPENBSD_SYSCALL_G7RFLAG
|
523 TARGET_OPENBSD_SYSCALL_G2RFLAG
);
525 ret
= do_openbsd_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]);
530 if ((unsigned int)ret
>= (unsigned int)(-515)) {
532 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
533 env
->xcc
|= PSR_CARRY
;
535 env
->psr
|= PSR_CARRY
;
538 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
539 env
->xcc
&= ~PSR_CARRY
;
541 env
->psr
&= ~PSR_CARRY
;
544 env
->regwptr
[0] = ret
;
545 /* next instruction */
546 #if defined(TARGET_SPARC64)
547 if (bsd_type
== target_openbsd
&&
548 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G2RFLAG
) {
549 env
->pc
= env
->gregs
[2];
550 env
->npc
= env
->pc
+ 4;
551 } else if (bsd_type
== target_openbsd
&&
552 env
->gregs
[1] & TARGET_OPENBSD_SYSCALL_G7RFLAG
) {
553 env
->pc
= env
->gregs
[7];
554 env
->npc
= env
->pc
+ 4;
557 env
->npc
= env
->npc
+ 4;
561 env
->npc
= env
->npc
+ 4;
564 case 0x83: /* flush windows */
569 /* next instruction */
571 env
->npc
= env
->npc
+ 4;
573 #ifndef TARGET_SPARC64
574 case TT_WIN_OVF
: /* window overflow */
577 case TT_WIN_UNF
: /* window underflow */
584 info
.si_signo
= SIGSEGV
;
586 /* XXX: check env->error_code */
587 info
.si_code
= TARGET_SEGV_MAPERR
;
588 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
589 queue_signal(env
, info
.si_signo
, &info
);
594 case TT_SPILL
: /* window overflow */
597 case TT_FILL
: /* window underflow */
604 info
.si_signo
= SIGSEGV
;
606 /* XXX: check env->error_code */
607 info
.si_code
= TARGET_SEGV_MAPERR
;
608 if (trapnr
== TT_DFAULT
)
609 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
611 info
._sifields
._sigfault
._addr
= env
->tsptr
->tpc
;
612 //queue_signal(env, info.si_signo, &info);
618 /* just indicate that signals should be handled asap */
625 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"
690 QEMU_HELP_BOTTOM
"\n"
698 THREAD CPUState
*thread_cpu
;
700 bool qemu_cpu_is_self(CPUState
*cpu
)
702 return thread_cpu
== cpu
;
705 void qemu_cpu_kick(CPUState
*cpu
)
710 /* Assumes contents are already zeroed. */
711 void init_task_state(TaskState
*ts
)
716 ts
->first_free
= ts
->sigqueue_table
;
717 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
718 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
720 ts
->sigqueue_table
[i
].next
= NULL
;
723 int main(int argc
, char **argv
)
725 const char *filename
;
726 const char *cpu_model
;
727 const char *log_file
= NULL
;
728 const char *log_mask
= NULL
;
729 struct target_pt_regs regs1
, *regs
= ®s1
;
730 struct image_info info1
, *info
= &info1
;
731 TaskState ts1
, *ts
= &ts1
;
736 int gdbstub_port
= 0;
737 char **target_environ
, **wrk
;
738 envlist_t
*envlist
= NULL
;
739 char *trace_file
= NULL
;
740 bsd_type
= target_openbsd
;
745 module_call_init(MODULE_INIT_TRACE
);
746 qemu_init_cpu_list();
747 module_call_init(MODULE_INIT_QOM
);
749 envlist
= envlist_create();
751 /* add current environment into the list */
752 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
753 (void) envlist_setenv(envlist
, *wrk
);
758 qemu_add_opts(&qemu_trace_opts
);
769 if (!strcmp(r
, "-")) {
771 } else if (!strcmp(r
, "d")) {
772 if (optind
>= argc
) {
775 log_mask
= argv
[optind
++];
776 } else if (!strcmp(r
, "D")) {
777 if (optind
>= argc
) {
780 log_file
= argv
[optind
++];
781 } else if (!strcmp(r
, "E")) {
783 if (envlist_setenv(envlist
, r
) != 0)
785 } else if (!strcmp(r
, "ignore-environment")) {
786 envlist_free(envlist
);
787 envlist
= envlist_create();
788 } else if (!strcmp(r
, "U")) {
790 if (envlist_unsetenv(envlist
, r
) != 0)
792 } else if (!strcmp(r
, "s")) {
794 x86_stack_size
= strtol(r
, (char **)&r
, 0);
795 if (x86_stack_size
<= 0)
798 x86_stack_size
*= 1024 * 1024;
799 else if (*r
== 'k' || *r
== 'K')
800 x86_stack_size
*= 1024;
801 } else if (!strcmp(r
, "L")) {
802 interp_prefix
= argv
[optind
++];
803 } else if (!strcmp(r
, "p")) {
804 qemu_host_page_size
= atoi(argv
[optind
++]);
805 if (qemu_host_page_size
== 0 ||
806 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
807 fprintf(stderr
, "page size must be a power of two\n");
810 } else if (!strcmp(r
, "g")) {
811 gdbstub_port
= atoi(argv
[optind
++]);
812 } else if (!strcmp(r
, "r")) {
813 qemu_uname_release
= argv
[optind
++];
814 } else if (!strcmp(r
, "cpu")) {
815 cpu_model
= argv
[optind
++];
816 if (is_help_option(cpu_model
)) {
817 /* XXX: implement xxx_cpu_list for targets that still miss it */
818 #if defined(cpu_list)
819 cpu_list(stdout
, &fprintf
);
823 } else if (!strcmp(r
, "B")) {
824 guest_base
= strtol(argv
[optind
++], NULL
, 0);
826 } else if (!strcmp(r
, "drop-ld-preload")) {
827 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
828 } else if (!strcmp(r
, "bsd")) {
829 if (!strcasecmp(argv
[optind
], "freebsd")) {
830 bsd_type
= target_freebsd
;
831 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
832 bsd_type
= target_netbsd
;
833 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
834 bsd_type
= target_openbsd
;
839 } else if (!strcmp(r
, "singlestep")) {
841 } else if (!strcmp(r
, "strace")) {
843 } else if (!strcmp(r
, "trace")) {
845 trace_file
= trace_opt_parse(optarg
);
852 qemu_log_needs_buffers();
853 qemu_set_log_filename(log_file
, &error_fatal
);
857 mask
= qemu_str_to_log_mask(log_mask
);
859 qemu_print_log_usage(stdout
);
865 if (optind
>= argc
) {
868 filename
= argv
[optind
];
870 if (!trace_init_backends()) {
873 trace_init_file(trace_file
);
876 memset(regs
, 0, sizeof(struct target_pt_regs
));
878 /* Zero out image_info */
879 memset(info
, 0, sizeof(struct image_info
));
881 /* Scan interp_prefix dir for replacement files. */
882 init_paths(interp_prefix
);
884 if (cpu_model
== NULL
) {
885 #if defined(TARGET_I386)
887 cpu_model
= "qemu64";
889 cpu_model
= "qemu32";
891 #elif defined(TARGET_SPARC)
892 #ifdef TARGET_SPARC64
893 cpu_model
= "TI UltraSparc II";
895 cpu_model
= "Fujitsu MB86904";
902 /* NOTE: we need to init the CPU at this stage to get
903 qemu_host_page_size */
904 cpu
= cpu_init(cpu_model
);
906 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
911 if (getenv("QEMU_STRACE")) {
915 target_environ
= envlist_to_environ(envlist
, NULL
);
916 envlist_free(envlist
);
919 * Now that page sizes are configured in cpu_init() we can do
920 * proper page alignment for guest_base.
922 guest_base
= HOST_PAGE_ALIGN(guest_base
);
925 * Read in mmap_min_addr kernel parameter. This value is used
926 * When loading the ELF image to determine whether guest_base
929 * When user has explicitly set the quest base, we skip this
932 if (!have_guest_base
) {
935 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
937 if (fscanf(fp
, "%lu", &tmp
) == 1) {
939 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
945 if (loader_exec(filename
, argv
+optind
, target_environ
, regs
, info
) != 0) {
946 printf("Error loading %s\n", filename
);
950 for (wrk
= target_environ
; *wrk
; wrk
++) {
954 g_free(target_environ
);
956 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
957 qemu_log("guest_base 0x%lx\n", guest_base
);
960 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
961 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
962 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
964 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
966 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
967 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
969 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
970 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
973 target_set_brk(info
->brk
);
977 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
978 generating the prologue until now so that the prologue can take
979 the real value of GUEST_BASE into account. */
980 tcg_prologue_init(tcg_ctx
);
983 /* build Task State */
984 memset(ts
, 0, sizeof(TaskState
));
989 #if defined(TARGET_I386)
990 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
991 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
992 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
993 env
->cr
[4] |= CR4_OSFXSR_MASK
;
994 env
->hflags
|= HF_OSFXSR_MASK
;
997 /* enable 64 bit mode if possible */
998 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
999 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
1002 env
->cr
[4] |= CR4_PAE_MASK
;
1003 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
1004 env
->hflags
|= HF_LMA_MASK
;
1007 /* flags setup : we activate the IRQs by default as in user mode */
1008 env
->eflags
|= IF_MASK
;
1010 /* linux register setup */
1011 #ifndef TARGET_ABI32
1012 env
->regs
[R_EAX
] = regs
->rax
;
1013 env
->regs
[R_EBX
] = regs
->rbx
;
1014 env
->regs
[R_ECX
] = regs
->rcx
;
1015 env
->regs
[R_EDX
] = regs
->rdx
;
1016 env
->regs
[R_ESI
] = regs
->rsi
;
1017 env
->regs
[R_EDI
] = regs
->rdi
;
1018 env
->regs
[R_EBP
] = regs
->rbp
;
1019 env
->regs
[R_ESP
] = regs
->rsp
;
1020 env
->eip
= regs
->rip
;
1022 env
->regs
[R_EAX
] = regs
->eax
;
1023 env
->regs
[R_EBX
] = regs
->ebx
;
1024 env
->regs
[R_ECX
] = regs
->ecx
;
1025 env
->regs
[R_EDX
] = regs
->edx
;
1026 env
->regs
[R_ESI
] = regs
->esi
;
1027 env
->regs
[R_EDI
] = regs
->edi
;
1028 env
->regs
[R_EBP
] = regs
->ebp
;
1029 env
->regs
[R_ESP
] = regs
->esp
;
1030 env
->eip
= regs
->eip
;
1033 /* linux interrupt setup */
1034 #ifndef TARGET_ABI32
1035 env
->idt
.limit
= 511;
1037 env
->idt
.limit
= 255;
1039 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
1040 PROT_READ
|PROT_WRITE
,
1041 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1042 idt_table
= g2h(env
->idt
.base
);
1065 /* linux segment setup */
1067 uint64_t *gdt_table
;
1068 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
1069 PROT_READ
|PROT_WRITE
,
1070 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
1071 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
1072 gdt_table
= g2h(env
->gdt
.base
);
1074 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1075 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1076 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1078 /* 64 bit code segment */
1079 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
1080 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1082 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
1084 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
1085 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
1086 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
1089 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
1090 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
1092 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
1093 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
1094 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
1095 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
1096 /* This hack makes Wine work... */
1097 env
->segs
[R_FS
].selector
= 0;
1099 cpu_x86_load_seg(env
, R_DS
, 0);
1100 cpu_x86_load_seg(env
, R_ES
, 0);
1101 cpu_x86_load_seg(env
, R_FS
, 0);
1102 cpu_x86_load_seg(env
, R_GS
, 0);
1104 #elif defined(TARGET_SPARC)
1108 env
->npc
= regs
->npc
;
1110 for(i
= 0; i
< 8; i
++)
1111 env
->gregs
[i
] = regs
->u_regs
[i
];
1112 for(i
= 0; i
< 8; i
++)
1113 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
1116 #error unsupported target CPU
1120 gdbserver_start (gdbstub_port
);
1121 gdb_handlesig(cpu
, 0);