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/>.
26 #include <sys/syscall.h>
27 #include <sys/resource.h>
30 #include "qemu-common.h"
31 #include "cache-utils.h"
34 #include "qemu-timer.h"
37 #define DEBUG_LOGFILE "/tmp/qemu.log"
42 unsigned long mmap_min_addr
;
43 #if defined(CONFIG_USE_GUEST_BASE)
44 unsigned long guest_base
;
46 unsigned long reserved_va
;
49 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
50 const char *qemu_uname_release
= CONFIG_UNAME_RELEASE
;
52 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
53 we allocate a bigger stack. Need a better solution, for example
54 by remapping the process stack directly at the right place */
55 unsigned long guest_stack_size
= 8 * 1024 * 1024UL;
57 void gemu_log(const char *fmt
, ...)
62 vfprintf(stderr
, fmt
, ap
);
66 #if defined(TARGET_I386)
67 int cpu_get_pic_interrupt(CPUState
*env
)
73 /* timers for rdtsc */
77 static uint64_t emu_time
;
79 int64_t cpu_get_real_ticks(void)
86 #if defined(CONFIG_USE_NPTL)
87 /***********************************************************/
88 /* Helper routines for implementing atomic operations. */
90 /* To implement exclusive operations we force all cpus to syncronise.
91 We don't require a full sync, only that no cpus are executing guest code.
92 The alternative is to map target atomic ops onto host equivalents,
93 which requires quite a lot of per host/target work. */
94 static pthread_mutex_t cpu_list_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 static pthread_mutex_t exclusive_lock
= PTHREAD_MUTEX_INITIALIZER
;
96 static pthread_cond_t exclusive_cond
= PTHREAD_COND_INITIALIZER
;
97 static pthread_cond_t exclusive_resume
= PTHREAD_COND_INITIALIZER
;
98 static int pending_cpus
;
100 /* Make sure everything is in a consistent state for calling fork(). */
101 void fork_start(void)
103 pthread_mutex_lock(&tb_lock
);
104 pthread_mutex_lock(&exclusive_lock
);
108 void fork_end(int child
)
110 mmap_fork_end(child
);
112 /* Child processes created by fork() only have a single thread.
113 Discard information about the parent threads. */
114 first_cpu
= thread_env
;
115 thread_env
->next_cpu
= NULL
;
117 pthread_mutex_init(&exclusive_lock
, NULL
);
118 pthread_mutex_init(&cpu_list_mutex
, NULL
);
119 pthread_cond_init(&exclusive_cond
, NULL
);
120 pthread_cond_init(&exclusive_resume
, NULL
);
121 pthread_mutex_init(&tb_lock
, NULL
);
122 gdbserver_fork(thread_env
);
124 pthread_mutex_unlock(&exclusive_lock
);
125 pthread_mutex_unlock(&tb_lock
);
129 /* Wait for pending exclusive operations to complete. The exclusive lock
131 static inline void exclusive_idle(void)
133 while (pending_cpus
) {
134 pthread_cond_wait(&exclusive_resume
, &exclusive_lock
);
138 /* Start an exclusive operation.
139 Must only be called from outside cpu_arm_exec. */
140 static inline void start_exclusive(void)
143 pthread_mutex_lock(&exclusive_lock
);
147 /* Make all other cpus stop executing. */
148 for (other
= first_cpu
; other
; other
= other
->next_cpu
) {
149 if (other
->running
) {
154 if (pending_cpus
> 1) {
155 pthread_cond_wait(&exclusive_cond
, &exclusive_lock
);
159 /* Finish an exclusive operation. */
160 static inline void end_exclusive(void)
163 pthread_cond_broadcast(&exclusive_resume
);
164 pthread_mutex_unlock(&exclusive_lock
);
167 /* Wait for exclusive ops to finish, and begin cpu execution. */
168 static inline void cpu_exec_start(CPUState
*env
)
170 pthread_mutex_lock(&exclusive_lock
);
173 pthread_mutex_unlock(&exclusive_lock
);
176 /* Mark cpu as not executing, and release pending exclusive ops. */
177 static inline void cpu_exec_end(CPUState
*env
)
179 pthread_mutex_lock(&exclusive_lock
);
181 if (pending_cpus
> 1) {
183 if (pending_cpus
== 1) {
184 pthread_cond_signal(&exclusive_cond
);
188 pthread_mutex_unlock(&exclusive_lock
);
191 void cpu_list_lock(void)
193 pthread_mutex_lock(&cpu_list_mutex
);
196 void cpu_list_unlock(void)
198 pthread_mutex_unlock(&cpu_list_mutex
);
200 #else /* if !CONFIG_USE_NPTL */
201 /* These are no-ops because we are not threadsafe. */
202 static inline void cpu_exec_start(CPUState
*env
)
206 static inline void cpu_exec_end(CPUState
*env
)
210 static inline void start_exclusive(void)
214 static inline void end_exclusive(void)
218 void fork_start(void)
222 void fork_end(int child
)
225 gdbserver_fork(thread_env
);
229 void cpu_list_lock(void)
233 void cpu_list_unlock(void)
240 /***********************************************************/
241 /* CPUX86 core interface */
243 void cpu_smm_update(CPUState
*env
)
247 uint64_t cpu_get_tsc(CPUX86State
*env
)
249 return cpu_get_real_ticks();
252 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
257 e1
= (addr
<< 16) | (limit
& 0xffff);
258 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
265 static uint64_t *idt_table
;
267 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
268 uint64_t addr
, unsigned int sel
)
271 e1
= (addr
& 0xffff) | (sel
<< 16);
272 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
276 p
[2] = tswap32(addr
>> 32);
279 /* only dpl matters as we do only user space emulation */
280 static void set_idt(int n
, unsigned int dpl
)
282 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
285 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
286 uint32_t addr
, unsigned int sel
)
289 e1
= (addr
& 0xffff) | (sel
<< 16);
290 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
296 /* only dpl matters as we do only user space emulation */
297 static void set_idt(int n
, unsigned int dpl
)
299 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
303 void cpu_loop(CPUX86State
*env
)
307 target_siginfo_t info
;
310 trapnr
= cpu_x86_exec(env
);
313 /* linux syscall from int $0x80 */
314 env
->regs
[R_EAX
] = do_syscall(env
,
326 /* linux syscall from syscall instruction */
327 env
->regs
[R_EAX
] = do_syscall(env
,
336 env
->eip
= env
->exception_next_eip
;
341 info
.si_signo
= SIGBUS
;
343 info
.si_code
= TARGET_SI_KERNEL
;
344 info
._sifields
._sigfault
._addr
= 0;
345 queue_signal(env
, info
.si_signo
, &info
);
348 /* XXX: potential problem if ABI32 */
349 #ifndef TARGET_X86_64
350 if (env
->eflags
& VM_MASK
) {
351 handle_vm86_fault(env
);
355 info
.si_signo
= SIGSEGV
;
357 info
.si_code
= TARGET_SI_KERNEL
;
358 info
._sifields
._sigfault
._addr
= 0;
359 queue_signal(env
, info
.si_signo
, &info
);
363 info
.si_signo
= SIGSEGV
;
365 if (!(env
->error_code
& 1))
366 info
.si_code
= TARGET_SEGV_MAPERR
;
368 info
.si_code
= TARGET_SEGV_ACCERR
;
369 info
._sifields
._sigfault
._addr
= env
->cr
[2];
370 queue_signal(env
, info
.si_signo
, &info
);
373 #ifndef TARGET_X86_64
374 if (env
->eflags
& VM_MASK
) {
375 handle_vm86_trap(env
, trapnr
);
379 /* division by zero */
380 info
.si_signo
= SIGFPE
;
382 info
.si_code
= TARGET_FPE_INTDIV
;
383 info
._sifields
._sigfault
._addr
= env
->eip
;
384 queue_signal(env
, info
.si_signo
, &info
);
389 #ifndef TARGET_X86_64
390 if (env
->eflags
& VM_MASK
) {
391 handle_vm86_trap(env
, trapnr
);
395 info
.si_signo
= SIGTRAP
;
397 if (trapnr
== EXCP01_DB
) {
398 info
.si_code
= TARGET_TRAP_BRKPT
;
399 info
._sifields
._sigfault
._addr
= env
->eip
;
401 info
.si_code
= TARGET_SI_KERNEL
;
402 info
._sifields
._sigfault
._addr
= 0;
404 queue_signal(env
, info
.si_signo
, &info
);
409 #ifndef TARGET_X86_64
410 if (env
->eflags
& VM_MASK
) {
411 handle_vm86_trap(env
, trapnr
);
415 info
.si_signo
= SIGSEGV
;
417 info
.si_code
= TARGET_SI_KERNEL
;
418 info
._sifields
._sigfault
._addr
= 0;
419 queue_signal(env
, info
.si_signo
, &info
);
423 info
.si_signo
= SIGILL
;
425 info
.si_code
= TARGET_ILL_ILLOPN
;
426 info
._sifields
._sigfault
._addr
= env
->eip
;
427 queue_signal(env
, info
.si_signo
, &info
);
430 /* just indicate that signals should be handled asap */
436 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
441 info
.si_code
= TARGET_TRAP_BRKPT
;
442 queue_signal(env
, info
.si_signo
, &info
);
447 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
448 fprintf(stderr
, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
452 process_pending_signals(env
);
459 /* Handle a jump to the kernel code page. */
461 do_kernel_trap(CPUARMState
*env
)
467 switch (env
->regs
[15]) {
468 case 0xffff0fa0: /* __kernel_memory_barrier */
469 /* ??? No-op. Will need to do better for SMP. */
471 case 0xffff0fc0: /* __kernel_cmpxchg */
472 /* XXX: This only works between threads, not between processes.
473 It's probably possible to implement this with native host
474 operations. However things like ldrex/strex are much harder so
475 there's not much point trying. */
477 cpsr
= cpsr_read(env
);
479 /* FIXME: This should SEGV if the access fails. */
480 if (get_user_u32(val
, addr
))
482 if (val
== env
->regs
[0]) {
484 /* FIXME: Check for segfaults. */
485 put_user_u32(val
, addr
);
492 cpsr_write(env
, cpsr
, CPSR_C
);
495 case 0xffff0fe0: /* __kernel_get_tls */
496 env
->regs
[0] = env
->cp15
.c13_tls2
;
501 /* Jump back to the caller. */
502 addr
= env
->regs
[14];
507 env
->regs
[15] = addr
;
512 static int do_strex(CPUARMState
*env
)
520 addr
= env
->exclusive_addr
;
521 if (addr
!= env
->exclusive_test
) {
524 size
= env
->exclusive_info
& 0xf;
527 segv
= get_user_u8(val
, addr
);
530 segv
= get_user_u16(val
, addr
);
534 segv
= get_user_u32(val
, addr
);
540 env
->cp15
.c6_data
= addr
;
543 if (val
!= env
->exclusive_val
) {
547 segv
= get_user_u32(val
, addr
+ 4);
549 env
->cp15
.c6_data
= addr
+ 4;
552 if (val
!= env
->exclusive_high
) {
556 val
= env
->regs
[(env
->exclusive_info
>> 8) & 0xf];
559 segv
= put_user_u8(val
, addr
);
562 segv
= put_user_u16(val
, addr
);
566 segv
= put_user_u32(val
, addr
);
570 env
->cp15
.c6_data
= addr
;
574 val
= env
->regs
[(env
->exclusive_info
>> 12) & 0xf];
575 segv
= put_user_u32(val
, addr
+ 4);
577 env
->cp15
.c6_data
= addr
+ 4;
584 env
->regs
[(env
->exclusive_info
>> 4) & 0xf] = rc
;
590 void cpu_loop(CPUARMState
*env
)
593 unsigned int n
, insn
;
594 target_siginfo_t info
;
599 trapnr
= cpu_arm_exec(env
);
604 TaskState
*ts
= env
->opaque
;
608 /* we handle the FPU emulation here, as Linux */
609 /* we get the opcode */
610 /* FIXME - what to do if get_user() fails? */
611 get_user_u32(opcode
, env
->regs
[15]);
613 rc
= EmulateAll(opcode
, &ts
->fpa
, env
);
614 if (rc
== 0) { /* illegal instruction */
615 info
.si_signo
= SIGILL
;
617 info
.si_code
= TARGET_ILL_ILLOPN
;
618 info
._sifields
._sigfault
._addr
= env
->regs
[15];
619 queue_signal(env
, info
.si_signo
, &info
);
620 } else if (rc
< 0) { /* FP exception */
623 /* translate softfloat flags to FPSR flags */
624 if (-rc
& float_flag_invalid
)
626 if (-rc
& float_flag_divbyzero
)
628 if (-rc
& float_flag_overflow
)
630 if (-rc
& float_flag_underflow
)
632 if (-rc
& float_flag_inexact
)
635 FPSR fpsr
= ts
->fpa
.fpsr
;
636 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
638 if (fpsr
& (arm_fpe
<< 16)) { /* exception enabled? */
639 info
.si_signo
= SIGFPE
;
642 /* ordered by priority, least first */
643 if (arm_fpe
& BIT_IXC
) info
.si_code
= TARGET_FPE_FLTRES
;
644 if (arm_fpe
& BIT_UFC
) info
.si_code
= TARGET_FPE_FLTUND
;
645 if (arm_fpe
& BIT_OFC
) info
.si_code
= TARGET_FPE_FLTOVF
;
646 if (arm_fpe
& BIT_DZC
) info
.si_code
= TARGET_FPE_FLTDIV
;
647 if (arm_fpe
& BIT_IOC
) info
.si_code
= TARGET_FPE_FLTINV
;
649 info
._sifields
._sigfault
._addr
= env
->regs
[15];
650 queue_signal(env
, info
.si_signo
, &info
);
655 /* accumulate unenabled exceptions */
656 if ((!(fpsr
& BIT_IXE
)) && (arm_fpe
& BIT_IXC
))
658 if ((!(fpsr
& BIT_UFE
)) && (arm_fpe
& BIT_UFC
))
660 if ((!(fpsr
& BIT_OFE
)) && (arm_fpe
& BIT_OFC
))
662 if ((!(fpsr
& BIT_DZE
)) && (arm_fpe
& BIT_DZC
))
664 if ((!(fpsr
& BIT_IOE
)) && (arm_fpe
& BIT_IOC
))
667 } else { /* everything OK */
678 if (trapnr
== EXCP_BKPT
) {
680 /* FIXME - what to do if get_user() fails? */
681 get_user_u16(insn
, env
->regs
[15]);
685 /* FIXME - what to do if get_user() fails? */
686 get_user_u32(insn
, env
->regs
[15]);
687 n
= (insn
& 0xf) | ((insn
>> 4) & 0xff0);
692 /* FIXME - what to do if get_user() fails? */
693 get_user_u16(insn
, env
->regs
[15] - 2);
696 /* FIXME - what to do if get_user() fails? */
697 get_user_u32(insn
, env
->regs
[15] - 4);
702 if (n
== ARM_NR_cacheflush
) {
704 } else if (n
== ARM_NR_semihosting
705 || n
== ARM_NR_thumb_semihosting
) {
706 env
->regs
[0] = do_arm_semihosting (env
);
707 } else if (n
== 0 || n
>= ARM_SYSCALL_BASE
708 || (env
->thumb
&& n
== ARM_THUMB_SYSCALL
)) {
710 if (env
->thumb
|| n
== 0) {
713 n
-= ARM_SYSCALL_BASE
;
716 if ( n
> ARM_NR_BASE
) {
718 case ARM_NR_cacheflush
:
722 cpu_set_tls(env
, env
->regs
[0]);
726 gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
728 env
->regs
[0] = -TARGET_ENOSYS
;
732 env
->regs
[0] = do_syscall(env
,
748 /* just indicate that signals should be handled asap */
750 case EXCP_PREFETCH_ABORT
:
751 addr
= env
->cp15
.c6_insn
;
753 case EXCP_DATA_ABORT
:
754 addr
= env
->cp15
.c6_data
;
758 info
.si_signo
= SIGSEGV
;
760 /* XXX: check env->error_code */
761 info
.si_code
= TARGET_SEGV_MAPERR
;
762 info
._sifields
._sigfault
._addr
= addr
;
763 queue_signal(env
, info
.si_signo
, &info
);
770 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
775 info
.si_code
= TARGET_TRAP_BRKPT
;
776 queue_signal(env
, info
.si_signo
, &info
);
780 case EXCP_KERNEL_TRAP
:
781 if (do_kernel_trap(env
))
786 addr
= env
->cp15
.c6_data
;
792 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
794 cpu_dump_state(env
, stderr
, fprintf
, 0);
797 process_pending_signals(env
);
803 #ifdef TARGET_UNICORE32
805 void cpu_loop(CPUState
*env
)
808 unsigned int n
, insn
;
809 target_siginfo_t info
;
813 trapnr
= uc32_cpu_exec(env
);
819 get_user_u32(insn
, env
->regs
[31] - 4);
822 if (n
>= UC32_SYSCALL_BASE
) {
824 n
-= UC32_SYSCALL_BASE
;
825 if (n
== UC32_SYSCALL_NR_set_tls
) {
826 cpu_set_tls(env
, env
->regs
[0]);
829 env
->regs
[0] = do_syscall(env
,
845 info
.si_signo
= SIGSEGV
;
847 /* XXX: check env->error_code */
848 info
.si_code
= TARGET_SEGV_MAPERR
;
849 info
._sifields
._sigfault
._addr
= env
->cp0
.c4_faultaddr
;
850 queue_signal(env
, info
.si_signo
, &info
);
853 /* just indicate that signals should be handled asap */
859 sig
= gdb_handlesig(env
, TARGET_SIGTRAP
);
863 info
.si_code
= TARGET_TRAP_BRKPT
;
864 queue_signal(env
, info
.si_signo
, &info
);
871 process_pending_signals(env
);
875 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr
);
876 cpu_dump_state(env
, stderr
, fprintf
, 0);
882 #define SPARC64_STACK_BIAS 2047
886 /* WARNING: dealing with register windows _is_ complicated. More info
887 can be found at http://www.sics.se/~psm/sparcstack.html */
888 static inline int get_reg_index(CPUSPARCState
*env
, int cwp
, int index
)
890 index
= (index
+ cwp
* 16) % (16 * env
->nwindows
);
891 /* wrap handling : if cwp is on the last window, then we use the
892 registers 'after' the end */
893 if (index
< 8 && env
->cwp
== env
->nwindows
- 1)
894 index
+= 16 * env
->nwindows
;
898 /* save the register window 'cwp1' */
899 static inline void save_window_offset(CPUSPARCState
*env
, int cwp1
)
904 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
905 #ifdef TARGET_SPARC64
907 sp_ptr
+= SPARC64_STACK_BIAS
;
909 #if defined(DEBUG_WIN)
910 printf("win_overflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" save_cwp=%d\n",
913 for(i
= 0; i
< 16; i
++) {
914 /* FIXME - what to do if put_user() fails? */
915 put_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
916 sp_ptr
+= sizeof(abi_ulong
);
920 static void save_window(CPUSPARCState
*env
)
922 #ifndef TARGET_SPARC64
923 unsigned int new_wim
;
924 new_wim
= ((env
->wim
>> 1) | (env
->wim
<< (env
->nwindows
- 1))) &
925 ((1LL << env
->nwindows
) - 1);
926 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
929 save_window_offset(env
, cpu_cwp_dec(env
, env
->cwp
- 2));
935 static void restore_window(CPUSPARCState
*env
)
937 #ifndef TARGET_SPARC64
938 unsigned int new_wim
;
940 unsigned int i
, cwp1
;
943 #ifndef TARGET_SPARC64
944 new_wim
= ((env
->wim
<< 1) | (env
->wim
>> (env
->nwindows
- 1))) &
945 ((1LL << env
->nwindows
) - 1);
948 /* restore the invalid window */
949 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
950 sp_ptr
= env
->regbase
[get_reg_index(env
, cwp1
, 6)];
951 #ifdef TARGET_SPARC64
953 sp_ptr
+= SPARC64_STACK_BIAS
;
955 #if defined(DEBUG_WIN)
956 printf("win_underflow: sp_ptr=0x" TARGET_ABI_FMT_lx
" load_cwp=%d\n",
959 for(i
= 0; i
< 16; i
++) {
960 /* FIXME - what to do if get_user() fails? */
961 get_user_ual(env
->regbase
[get_reg_index(env
, cwp1
, 8 + i
)], sp_ptr
);
962 sp_ptr
+= sizeof(abi_ulong
);
964 #ifdef TARGET_SPARC64
966 if (env
->cleanwin
< env
->nwindows
- 1)
974 static void flush_windows(CPUSPARCState
*env
)
980 /* if restore would invoke restore_window(), then we can stop */
981 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ offset
);
982 #ifndef TARGET_SPARC64
983 if (env
->wim
& (1 << cwp1
))
986 if (env
->canrestore
== 0)
991 save_window_offset(env
, cwp1
);
994 cwp1
= cpu_cwp_inc(env
, env
->cwp
+ 1);
995 #ifndef TARGET_SPARC64
996 /* set wim so that restore will reload the registers */
997 env
->wim
= 1 << cwp1
;
999 #if defined(DEBUG_WIN)
1000 printf("flush_windows: nb=%d\n", offset
- 1);
1004 void cpu_loop (CPUSPARCState
*env
)
1008 target_siginfo_t info
;
1011 trapnr
= cpu_sparc_exec (env
);
1014 #ifndef TARGET_SPARC64
1021 ret
= do_syscall (env
, env
->gregs
[1],
1022 env
->regwptr
[0], env
->regwptr
[1],
1023 env
->regwptr
[2], env
->regwptr
[3],
1024 env
->regwptr
[4], env
->regwptr
[5],
1026 if ((abi_ulong
)ret
>= (abi_ulong
)(-515)) {
1027 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1028 env
->xcc
|= PSR_CARRY
;
1030 env
->psr
|= PSR_CARRY
;
1034 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
1035 env
->xcc
&= ~PSR_CARRY
;
1037 env
->psr
&= ~PSR_CARRY
;
1040 env
->regwptr
[0] = ret
;
1041 /* next instruction */
1043 env
->npc
= env
->npc
+ 4;
1045 case 0x83: /* flush windows */
1050 /* next instruction */
1052 env
->npc
= env
->npc
+ 4;
1054 #ifndef TARGET_SPARC64
1055 case TT_WIN_OVF
: /* window overflow */
1058 case TT_WIN_UNF
: /* window underflow */
1059 restore_window(env
);
1064 info
.si_signo
= SIGSEGV
;
1066 /* XXX: check env->error_code */
1067 info
.si_code
= TARGET_SEGV_MAPERR
;
1068 info
._sifields
._sigfault
._addr
= env
->mmuregs
[4];
1069 queue_signal(env
, info
.si_signo
, &info
);
1073 case TT_SPILL
: /* window overflow */
1076 case TT_FILL
: /* window underflow */
1077 restore_window(env
);
1082 info
.si_signo
= SIGSEGV
;
1084 /* XXX: check env->error_code */
1085 info
.si_code
= TARGET_SEGV_MAPERR
;
1086 if (trapnr
== TT_DFAULT
)
1087 info
._sifields
._sigfault
._addr
= env
->dmmuregs
[4];
1089 info
._sifields
._sigfault
._addr
= cpu_tsptr(env
)->tpc
;
1090 queue_signal(env
, info
.si_signo
, &info
);
1093 #ifndef TARGET_ABI32
1096 sparc64_get_context(env
);
1100 sparc64_set_context(env
);
1104 case EXCP_INTERRUPT
:
1105 /* just indicate that signals should be handled asap */
1111 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
1114 info
.si_signo
= sig
;
1116 info
.si_code
= TARGET_TRAP_BRKPT
;
1117 queue_signal(env
, info
.si_signo
, &info
);
1122 printf ("Unhandled trap: 0x%x\n", trapnr
);
1123 cpu_dump_state(env
, stderr
, fprintf
, 0);
1126 process_pending_signals (env
);
1133 static inline uint64_t cpu_ppc_get_tb (CPUState
*env
)
1139 uint64_t cpu_ppc_load_tbl (CPUState
*env
)
1141 return cpu_ppc_get_tb(env
);
1144 uint32_t cpu_ppc_load_tbu (CPUState
*env
)
1146 return cpu_ppc_get_tb(env
) >> 32;
1149 uint64_t cpu_ppc_load_atbl (CPUState
*env
)
1151 return cpu_ppc_get_tb(env
);
1154 uint32_t cpu_ppc_load_atbu (CPUState
*env
)
1156 return cpu_ppc_get_tb(env
) >> 32;
1159 uint32_t cpu_ppc601_load_rtcu (CPUState
*env
)
1160 __attribute__ (( alias ("cpu_ppc_load_tbu") ));
1162 uint32_t cpu_ppc601_load_rtcl (CPUState
*env
)
1164 return cpu_ppc_load_tbl(env
) & 0x3FFFFF80;
1167 /* XXX: to be fixed */
1168 int ppc_dcr_read (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t *valp
)
1173 int ppc_dcr_write (ppc_dcr_t
*dcr_env
, int dcrn
, uint32_t val
)
1178 #define EXCP_DUMP(env, fmt, ...) \
1180 fprintf(stderr, fmt , ## __VA_ARGS__); \
1181 cpu_dump_state(env, stderr, fprintf, 0); \
1182 qemu_log(fmt, ## __VA_ARGS__); \
1184 log_cpu_state(env, 0); \
1187 static int do_store_exclusive(CPUPPCState
*env
)
1190 target_ulong page_addr
;
1195 addr
= env
->reserve_ea
;
1196 page_addr
= addr
& TARGET_PAGE_MASK
;
1199 flags
= page_get_flags(page_addr
);
1200 if ((flags
& PAGE_READ
) == 0) {
1203 int reg
= env
->reserve_info
& 0x1f;
1204 int size
= (env
->reserve_info
>> 5) & 0xf;
1207 if (addr
== env
->reserve_addr
) {
1209 case 1: segv
= get_user_u8(val
, addr
); break;
1210 case 2: segv
= get_user_u16(val
, addr
); break;
1211 case 4: segv
= get_user_u32(val
, addr
); break;
1212 #if defined(TARGET_PPC64)
1213 case 8: segv
= get_user_u64(val
, addr
); break;
1217 if (!segv
&& val
== env
->reserve_val
) {
1218 val
= env
->gpr
[reg
];
1220 case 1: segv
= put_user_u8(val
, addr
); break;
1221 case 2: segv
= put_user_u16(val
, addr
); break;
1222 case 4: segv
= put_user_u32(val
, addr
); break;
1223 #if defined(TARGET_PPC64)
1224 case 8: segv
= put_user_u64(val
, addr
); break;
1233 env
->crf
[0] = (stored
<< 1) | xer_so
;
1234 env
->reserve_addr
= (target_ulong
)-1;
1244 void cpu_loop(CPUPPCState
*env
)
1246 target_siginfo_t info
;
1251 cpu_exec_start(env
);
1252 trapnr
= cpu_ppc_exec(env
);
1255 case POWERPC_EXCP_NONE
:
1258 case POWERPC_EXCP_CRITICAL
: /* Critical input */
1259 cpu_abort(env
, "Critical interrupt while in user mode. "
1262 case POWERPC_EXCP_MCHECK
: /* Machine check exception */
1263 cpu_abort(env
, "Machine check exception while in user mode. "
1266 case POWERPC_EXCP_DSI
: /* Data storage exception */
1267 EXCP_DUMP(env
, "Invalid data memory access: 0x" TARGET_FMT_lx
"\n",
1269 /* XXX: check this. Seems bugged */
1270 switch (env
->error_code
& 0xFF000000) {
1272 info
.si_signo
= TARGET_SIGSEGV
;
1274 info
.si_code
= TARGET_SEGV_MAPERR
;
1277 info
.si_signo
= TARGET_SIGILL
;
1279 info
.si_code
= TARGET_ILL_ILLADR
;
1282 info
.si_signo
= TARGET_SIGSEGV
;
1284 info
.si_code
= TARGET_SEGV_ACCERR
;
1287 /* Let's send a regular segfault... */
1288 EXCP_DUMP(env
, "Invalid segfault errno (%02x)\n",
1290 info
.si_signo
= TARGET_SIGSEGV
;
1292 info
.si_code
= TARGET_SEGV_MAPERR
;
1295 info
._sifields
._sigfault
._addr
= env
->nip
;
1296 queue_signal(env
, info
.si_signo
, &info
);
1298 case POWERPC_EXCP_ISI
: /* Instruction storage exception */
1299 EXCP_DUMP(env
, "Invalid instruction fetch: 0x\n" TARGET_FMT_lx
1300 "\n", env
->spr
[SPR_SRR0
]);
1301 /* XXX: check this */
1302 switch (env
->error_code
& 0xFF000000) {
1304 info
.si_signo
= TARGET_SIGSEGV
;
1306 info
.si_code
= TARGET_SEGV_MAPERR
;
1310 info
.si_signo
= TARGET_SIGSEGV
;
1312 info
.si_code
= TARGET_SEGV_ACCERR
;
1315 /* Let's send a regular segfault... */
1316 EXCP_DUMP(env
, "Invalid segfault errno (%02x)\n",
1318 info
.si_signo
= TARGET_SIGSEGV
;
1320 info
.si_code
= TARGET_SEGV_MAPERR
;
1323 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1324 queue_signal(env
, info
.si_signo
, &info
);
1326 case POWERPC_EXCP_EXTERNAL
: /* External input */
1327 cpu_abort(env
, "External interrupt while in user mode. "
1330 case POWERPC_EXCP_ALIGN
: /* Alignment exception */
1331 EXCP_DUMP(env
, "Unaligned memory access\n");
1332 /* XXX: check this */
1333 info
.si_signo
= TARGET_SIGBUS
;
1335 info
.si_code
= TARGET_BUS_ADRALN
;
1336 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1337 queue_signal(env
, info
.si_signo
, &info
);
1339 case POWERPC_EXCP_PROGRAM
: /* Program exception */
1340 /* XXX: check this */
1341 switch (env
->error_code
& ~0xF) {
1342 case POWERPC_EXCP_FP
:
1343 EXCP_DUMP(env
, "Floating point program exception\n");
1344 info
.si_signo
= TARGET_SIGFPE
;
1346 switch (env
->error_code
& 0xF) {
1347 case POWERPC_EXCP_FP_OX
:
1348 info
.si_code
= TARGET_FPE_FLTOVF
;
1350 case POWERPC_EXCP_FP_UX
:
1351 info
.si_code
= TARGET_FPE_FLTUND
;
1353 case POWERPC_EXCP_FP_ZX
:
1354 case POWERPC_EXCP_FP_VXZDZ
:
1355 info
.si_code
= TARGET_FPE_FLTDIV
;
1357 case POWERPC_EXCP_FP_XX
:
1358 info
.si_code
= TARGET_FPE_FLTRES
;
1360 case POWERPC_EXCP_FP_VXSOFT
:
1361 info
.si_code
= TARGET_FPE_FLTINV
;
1363 case POWERPC_EXCP_FP_VXSNAN
:
1364 case POWERPC_EXCP_FP_VXISI
:
1365 case POWERPC_EXCP_FP_VXIDI
:
1366 case POWERPC_EXCP_FP_VXIMZ
:
1367 case POWERPC_EXCP_FP_VXVC
:
1368 case POWERPC_EXCP_FP_VXSQRT
:
1369 case POWERPC_EXCP_FP_VXCVI
:
1370 info
.si_code
= TARGET_FPE_FLTSUB
;
1373 EXCP_DUMP(env
, "Unknown floating point exception (%02x)\n",
1378 case POWERPC_EXCP_INVAL
:
1379 EXCP_DUMP(env
, "Invalid instruction\n");
1380 info
.si_signo
= TARGET_SIGILL
;
1382 switch (env
->error_code
& 0xF) {
1383 case POWERPC_EXCP_INVAL_INVAL
:
1384 info
.si_code
= TARGET_ILL_ILLOPC
;
1386 case POWERPC_EXCP_INVAL_LSWX
:
1387 info
.si_code
= TARGET_ILL_ILLOPN
;
1389 case POWERPC_EXCP_INVAL_SPR
:
1390 info
.si_code
= TARGET_ILL_PRVREG
;
1392 case POWERPC_EXCP_INVAL_FP
:
1393 info
.si_code
= TARGET_ILL_COPROC
;
1396 EXCP_DUMP(env
, "Unknown invalid operation (%02x)\n",
1397 env
->error_code
& 0xF);
1398 info
.si_code
= TARGET_ILL_ILLADR
;
1402 case POWERPC_EXCP_PRIV
:
1403 EXCP_DUMP(env
, "Privilege violation\n");
1404 info
.si_signo
= TARGET_SIGILL
;
1406 switch (env
->error_code
& 0xF) {
1407 case POWERPC_EXCP_PRIV_OPC
:
1408 info
.si_code
= TARGET_ILL_PRVOPC
;
1410 case POWERPC_EXCP_PRIV_REG
:
1411 info
.si_code
= TARGET_ILL_PRVREG
;
1414 EXCP_DUMP(env
, "Unknown privilege violation (%02x)\n",
1415 env
->error_code
& 0xF);
1416 info
.si_code
= TARGET_ILL_PRVOPC
;
1420 case POWERPC_EXCP_TRAP
:
1421 cpu_abort(env
, "Tried to call a TRAP\n");
1424 /* Should not happen ! */
1425 cpu_abort(env
, "Unknown program exception (%02x)\n",
1429 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1430 queue_signal(env
, info
.si_signo
, &info
);
1432 case POWERPC_EXCP_FPU
: /* Floating-point unavailable exception */
1433 EXCP_DUMP(env
, "No floating point allowed\n");
1434 info
.si_signo
= TARGET_SIGILL
;
1436 info
.si_code
= TARGET_ILL_COPROC
;
1437 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1438 queue_signal(env
, info
.si_signo
, &info
);
1440 case POWERPC_EXCP_SYSCALL
: /* System call exception */
1441 cpu_abort(env
, "Syscall exception while in user mode. "
1444 case POWERPC_EXCP_APU
: /* Auxiliary processor unavailable */
1445 EXCP_DUMP(env
, "No APU instruction allowed\n");
1446 info
.si_signo
= TARGET_SIGILL
;
1448 info
.si_code
= TARGET_ILL_COPROC
;
1449 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1450 queue_signal(env
, info
.si_signo
, &info
);
1452 case POWERPC_EXCP_DECR
: /* Decrementer exception */
1453 cpu_abort(env
, "Decrementer interrupt while in user mode. "
1456 case POWERPC_EXCP_FIT
: /* Fixed-interval timer interrupt */
1457 cpu_abort(env
, "Fix interval timer interrupt while in user mode. "
1460 case POWERPC_EXCP_WDT
: /* Watchdog timer interrupt */
1461 cpu_abort(env
, "Watchdog timer interrupt while in user mode. "
1464 case POWERPC_EXCP_DTLB
: /* Data TLB error */
1465 cpu_abort(env
, "Data TLB exception while in user mode. "
1468 case POWERPC_EXCP_ITLB
: /* Instruction TLB error */
1469 cpu_abort(env
, "Instruction TLB exception while in user mode. "
1472 case POWERPC_EXCP_SPEU
: /* SPE/embedded floating-point unavail. */
1473 EXCP_DUMP(env
, "No SPE/floating-point instruction allowed\n");
1474 info
.si_signo
= TARGET_SIGILL
;
1476 info
.si_code
= TARGET_ILL_COPROC
;
1477 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1478 queue_signal(env
, info
.si_signo
, &info
);
1480 case POWERPC_EXCP_EFPDI
: /* Embedded floating-point data IRQ */
1481 cpu_abort(env
, "Embedded floating-point data IRQ not handled\n");
1483 case POWERPC_EXCP_EFPRI
: /* Embedded floating-point round IRQ */
1484 cpu_abort(env
, "Embedded floating-point round IRQ not handled\n");
1486 case POWERPC_EXCP_EPERFM
: /* Embedded performance monitor IRQ */
1487 cpu_abort(env
, "Performance monitor exception not handled\n");
1489 case POWERPC_EXCP_DOORI
: /* Embedded doorbell interrupt */
1490 cpu_abort(env
, "Doorbell interrupt while in user mode. "
1493 case POWERPC_EXCP_DOORCI
: /* Embedded doorbell critical interrupt */
1494 cpu_abort(env
, "Doorbell critical interrupt while in user mode. "
1497 case POWERPC_EXCP_RESET
: /* System reset exception */
1498 cpu_abort(env
, "Reset interrupt while in user mode. "
1501 case POWERPC_EXCP_DSEG
: /* Data segment exception */
1502 cpu_abort(env
, "Data segment exception while in user mode. "
1505 case POWERPC_EXCP_ISEG
: /* Instruction segment exception */
1506 cpu_abort(env
, "Instruction segment exception "
1507 "while in user mode. Aborting\n");
1509 /* PowerPC 64 with hypervisor mode support */
1510 case POWERPC_EXCP_HDECR
: /* Hypervisor decrementer exception */
1511 cpu_abort(env
, "Hypervisor decrementer interrupt "
1512 "while in user mode. Aborting\n");
1514 case POWERPC_EXCP_TRACE
: /* Trace exception */
1516 * we use this exception to emulate step-by-step execution mode.
1519 /* PowerPC 64 with hypervisor mode support */
1520 case POWERPC_EXCP_HDSI
: /* Hypervisor data storage exception */
1521 cpu_abort(env
, "Hypervisor data storage exception "
1522 "while in user mode. Aborting\n");
1524 case POWERPC_EXCP_HISI
: /* Hypervisor instruction storage excp */
1525 cpu_abort(env
, "Hypervisor instruction storage exception "
1526 "while in user mode. Aborting\n");
1528 case POWERPC_EXCP_HDSEG
: /* Hypervisor data segment exception */
1529 cpu_abort(env
, "Hypervisor data segment exception "
1530 "while in user mode. Aborting\n");
1532 case POWERPC_EXCP_HISEG
: /* Hypervisor instruction segment excp */
1533 cpu_abort(env
, "Hypervisor instruction segment exception "
1534 "while in user mode. Aborting\n");
1536 case POWERPC_EXCP_VPU
: /* Vector unavailable exception */
1537 EXCP_DUMP(env
, "No Altivec instructions allowed\n");
1538 info
.si_signo
= TARGET_SIGILL
;
1540 info
.si_code
= TARGET_ILL_COPROC
;
1541 info
._sifields
._sigfault
._addr
= env
->nip
- 4;
1542 queue_signal(env
, info
.si_signo
, &info
);
1544 case POWERPC_EXCP_PIT
: /* Programmable interval timer IRQ */
1545 cpu_abort(env
, "Programable interval timer interrupt "
1546 "while in user mode. Aborting\n");
1548 case POWERPC_EXCP_IO
: /* IO error exception */
1549 cpu_abort(env
, "IO error exception while in user mode. "
1552 case POWERPC_EXCP_RUNM
: /* Run mode exception */
1553 cpu_abort(env
, "Run mode exception while in user mode. "
1556 case POWERPC_EXCP_EMUL
: /* Emulation trap exception */
1557 cpu_abort(env
, "Emulation trap exception not handled\n");
1559 case POWERPC_EXCP_IFTLB
: /* Instruction fetch TLB error */
1560 cpu_abort(env
, "Instruction fetch TLB exception "
1561 "while in user-mode. Aborting");
1563 case POWERPC_EXCP_DLTLB
: /* Data load TLB miss */
1564 cpu_abort(env
, "Data load TLB exception while in user-mode. "
1567 case POWERPC_EXCP_DSTLB
: /* Data store TLB miss */
1568 cpu_abort(env
, "Data store TLB exception while in user-mode. "
1571 case POWERPC_EXCP_FPA
: /* Floating-point assist exception */
1572 cpu_abort(env
, "Floating-point assist exception not handled\n");
1574 case POWERPC_EXCP_IABR
: /* Instruction address breakpoint */
1575 cpu_abort(env
, "Instruction address breakpoint exception "
1578 case POWERPC_EXCP_SMI
: /* System management interrupt */
1579 cpu_abort(env
, "System management interrupt while in user mode. "
1582 case POWERPC_EXCP_THERM
: /* Thermal interrupt */
1583 cpu_abort(env
, "Thermal interrupt interrupt while in user mode. "
1586 case POWERPC_EXCP_PERFM
: /* Embedded performance monitor IRQ */
1587 cpu_abort(env
, "Performance monitor exception not handled\n");
1589 case POWERPC_EXCP_VPUA
: /* Vector assist exception */
1590 cpu_abort(env
, "Vector assist exception not handled\n");
1592 case POWERPC_EXCP_SOFTP
: /* Soft patch exception */
1593 cpu_abort(env
, "Soft patch exception not handled\n");
1595 case POWERPC_EXCP_MAINT
: /* Maintenance exception */
1596 cpu_abort(env
, "Maintenance exception while in user mode. "
1599 case POWERPC_EXCP_STOP
: /* stop translation */
1600 /* We did invalidate the instruction cache. Go on */
1602 case POWERPC_EXCP_BRANCH
: /* branch instruction: */
1603 /* We just stopped because of a branch. Go on */
1605 case POWERPC_EXCP_SYSCALL_USER
:
1606 /* system call in user-mode emulation */
1608 * PPC ABI uses overflow flag in cr0 to signal an error
1612 printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env
->gpr
[0],
1613 env
->gpr
[3], env
->gpr
[4], env
->gpr
[5], env
->gpr
[6]);
1615 env
->crf
[0] &= ~0x1;
1616 ret
= do_syscall(env
, env
->gpr
[0], env
->gpr
[3], env
->gpr
[4],
1617 env
->gpr
[5], env
->gpr
[6], env
->gpr
[7],
1619 if (ret
== (uint32_t)(-TARGET_QEMU_ESIGRETURN
)) {
1620 /* Returning from a successful sigreturn syscall.
1621 Avoid corrupting register state. */
1624 if (ret
> (uint32_t)(-515)) {
1630 printf("syscall returned 0x%08x (%d)\n", ret
, ret
);
1633 case POWERPC_EXCP_STCX
:
1634 if (do_store_exclusive(env
)) {
1635 info
.si_signo
= TARGET_SIGSEGV
;
1637 info
.si_code
= TARGET_SEGV_MAPERR
;
1638 info
._sifields
._sigfault
._addr
= env
->nip
;
1639 queue_signal(env
, info
.si_signo
, &info
);
1646 sig
= gdb_handlesig(env
, TARGET_SIGTRAP
);
1648 info
.si_signo
= sig
;
1650 info
.si_code
= TARGET_TRAP_BRKPT
;
1651 queue_signal(env
, info
.si_signo
, &info
);
1655 case EXCP_INTERRUPT
:
1656 /* just indicate that signals should be handled asap */
1659 cpu_abort(env
, "Unknown exception 0x%d. Aborting\n", trapnr
);
1662 process_pending_signals(env
);
1669 #define MIPS_SYS(name, args) args,
1671 static const uint8_t mips_syscall_args
[] = {
1672 MIPS_SYS(sys_syscall
, 0) /* 4000 */
1673 MIPS_SYS(sys_exit
, 1)
1674 MIPS_SYS(sys_fork
, 0)
1675 MIPS_SYS(sys_read
, 3)
1676 MIPS_SYS(sys_write
, 3)
1677 MIPS_SYS(sys_open
, 3) /* 4005 */
1678 MIPS_SYS(sys_close
, 1)
1679 MIPS_SYS(sys_waitpid
, 3)
1680 MIPS_SYS(sys_creat
, 2)
1681 MIPS_SYS(sys_link
, 2)
1682 MIPS_SYS(sys_unlink
, 1) /* 4010 */
1683 MIPS_SYS(sys_execve
, 0)
1684 MIPS_SYS(sys_chdir
, 1)
1685 MIPS_SYS(sys_time
, 1)
1686 MIPS_SYS(sys_mknod
, 3)
1687 MIPS_SYS(sys_chmod
, 2) /* 4015 */
1688 MIPS_SYS(sys_lchown
, 3)
1689 MIPS_SYS(sys_ni_syscall
, 0)
1690 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_stat */
1691 MIPS_SYS(sys_lseek
, 3)
1692 MIPS_SYS(sys_getpid
, 0) /* 4020 */
1693 MIPS_SYS(sys_mount
, 5)
1694 MIPS_SYS(sys_oldumount
, 1)
1695 MIPS_SYS(sys_setuid
, 1)
1696 MIPS_SYS(sys_getuid
, 0)
1697 MIPS_SYS(sys_stime
, 1) /* 4025 */
1698 MIPS_SYS(sys_ptrace
, 4)
1699 MIPS_SYS(sys_alarm
, 1)
1700 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_fstat */
1701 MIPS_SYS(sys_pause
, 0)
1702 MIPS_SYS(sys_utime
, 2) /* 4030 */
1703 MIPS_SYS(sys_ni_syscall
, 0)
1704 MIPS_SYS(sys_ni_syscall
, 0)
1705 MIPS_SYS(sys_access
, 2)
1706 MIPS_SYS(sys_nice
, 1)
1707 MIPS_SYS(sys_ni_syscall
, 0) /* 4035 */
1708 MIPS_SYS(sys_sync
, 0)
1709 MIPS_SYS(sys_kill
, 2)
1710 MIPS_SYS(sys_rename
, 2)
1711 MIPS_SYS(sys_mkdir
, 2)
1712 MIPS_SYS(sys_rmdir
, 1) /* 4040 */
1713 MIPS_SYS(sys_dup
, 1)
1714 MIPS_SYS(sys_pipe
, 0)
1715 MIPS_SYS(sys_times
, 1)
1716 MIPS_SYS(sys_ni_syscall
, 0)
1717 MIPS_SYS(sys_brk
, 1) /* 4045 */
1718 MIPS_SYS(sys_setgid
, 1)
1719 MIPS_SYS(sys_getgid
, 0)
1720 MIPS_SYS(sys_ni_syscall
, 0) /* was signal(2) */
1721 MIPS_SYS(sys_geteuid
, 0)
1722 MIPS_SYS(sys_getegid
, 0) /* 4050 */
1723 MIPS_SYS(sys_acct
, 0)
1724 MIPS_SYS(sys_umount
, 2)
1725 MIPS_SYS(sys_ni_syscall
, 0)
1726 MIPS_SYS(sys_ioctl
, 3)
1727 MIPS_SYS(sys_fcntl
, 3) /* 4055 */
1728 MIPS_SYS(sys_ni_syscall
, 2)
1729 MIPS_SYS(sys_setpgid
, 2)
1730 MIPS_SYS(sys_ni_syscall
, 0)
1731 MIPS_SYS(sys_olduname
, 1)
1732 MIPS_SYS(sys_umask
, 1) /* 4060 */
1733 MIPS_SYS(sys_chroot
, 1)
1734 MIPS_SYS(sys_ustat
, 2)
1735 MIPS_SYS(sys_dup2
, 2)
1736 MIPS_SYS(sys_getppid
, 0)
1737 MIPS_SYS(sys_getpgrp
, 0) /* 4065 */
1738 MIPS_SYS(sys_setsid
, 0)
1739 MIPS_SYS(sys_sigaction
, 3)
1740 MIPS_SYS(sys_sgetmask
, 0)
1741 MIPS_SYS(sys_ssetmask
, 1)
1742 MIPS_SYS(sys_setreuid
, 2) /* 4070 */
1743 MIPS_SYS(sys_setregid
, 2)
1744 MIPS_SYS(sys_sigsuspend
, 0)
1745 MIPS_SYS(sys_sigpending
, 1)
1746 MIPS_SYS(sys_sethostname
, 2)
1747 MIPS_SYS(sys_setrlimit
, 2) /* 4075 */
1748 MIPS_SYS(sys_getrlimit
, 2)
1749 MIPS_SYS(sys_getrusage
, 2)
1750 MIPS_SYS(sys_gettimeofday
, 2)
1751 MIPS_SYS(sys_settimeofday
, 2)
1752 MIPS_SYS(sys_getgroups
, 2) /* 4080 */
1753 MIPS_SYS(sys_setgroups
, 2)
1754 MIPS_SYS(sys_ni_syscall
, 0) /* old_select */
1755 MIPS_SYS(sys_symlink
, 2)
1756 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_lstat */
1757 MIPS_SYS(sys_readlink
, 3) /* 4085 */
1758 MIPS_SYS(sys_uselib
, 1)
1759 MIPS_SYS(sys_swapon
, 2)
1760 MIPS_SYS(sys_reboot
, 3)
1761 MIPS_SYS(old_readdir
, 3)
1762 MIPS_SYS(old_mmap
, 6) /* 4090 */
1763 MIPS_SYS(sys_munmap
, 2)
1764 MIPS_SYS(sys_truncate
, 2)
1765 MIPS_SYS(sys_ftruncate
, 2)
1766 MIPS_SYS(sys_fchmod
, 2)
1767 MIPS_SYS(sys_fchown
, 3) /* 4095 */
1768 MIPS_SYS(sys_getpriority
, 2)
1769 MIPS_SYS(sys_setpriority
, 3)
1770 MIPS_SYS(sys_ni_syscall
, 0)
1771 MIPS_SYS(sys_statfs
, 2)
1772 MIPS_SYS(sys_fstatfs
, 2) /* 4100 */
1773 MIPS_SYS(sys_ni_syscall
, 0) /* was ioperm(2) */
1774 MIPS_SYS(sys_socketcall
, 2)
1775 MIPS_SYS(sys_syslog
, 3)
1776 MIPS_SYS(sys_setitimer
, 3)
1777 MIPS_SYS(sys_getitimer
, 2) /* 4105 */
1778 MIPS_SYS(sys_newstat
, 2)
1779 MIPS_SYS(sys_newlstat
, 2)
1780 MIPS_SYS(sys_newfstat
, 2)
1781 MIPS_SYS(sys_uname
, 1)
1782 MIPS_SYS(sys_ni_syscall
, 0) /* 4110 was iopl(2) */
1783 MIPS_SYS(sys_vhangup
, 0)
1784 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_idle() */
1785 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_vm86 */
1786 MIPS_SYS(sys_wait4
, 4)
1787 MIPS_SYS(sys_swapoff
, 1) /* 4115 */
1788 MIPS_SYS(sys_sysinfo
, 1)
1789 MIPS_SYS(sys_ipc
, 6)
1790 MIPS_SYS(sys_fsync
, 1)
1791 MIPS_SYS(sys_sigreturn
, 0)
1792 MIPS_SYS(sys_clone
, 6) /* 4120 */
1793 MIPS_SYS(sys_setdomainname
, 2)
1794 MIPS_SYS(sys_newuname
, 1)
1795 MIPS_SYS(sys_ni_syscall
, 0) /* sys_modify_ldt */
1796 MIPS_SYS(sys_adjtimex
, 1)
1797 MIPS_SYS(sys_mprotect
, 3) /* 4125 */
1798 MIPS_SYS(sys_sigprocmask
, 3)
1799 MIPS_SYS(sys_ni_syscall
, 0) /* was create_module */
1800 MIPS_SYS(sys_init_module
, 5)
1801 MIPS_SYS(sys_delete_module
, 1)
1802 MIPS_SYS(sys_ni_syscall
, 0) /* 4130 was get_kernel_syms */
1803 MIPS_SYS(sys_quotactl
, 0)
1804 MIPS_SYS(sys_getpgid
, 1)
1805 MIPS_SYS(sys_fchdir
, 1)
1806 MIPS_SYS(sys_bdflush
, 2)
1807 MIPS_SYS(sys_sysfs
, 3) /* 4135 */
1808 MIPS_SYS(sys_personality
, 1)
1809 MIPS_SYS(sys_ni_syscall
, 0) /* for afs_syscall */
1810 MIPS_SYS(sys_setfsuid
, 1)
1811 MIPS_SYS(sys_setfsgid
, 1)
1812 MIPS_SYS(sys_llseek
, 5) /* 4140 */
1813 MIPS_SYS(sys_getdents
, 3)
1814 MIPS_SYS(sys_select
, 5)
1815 MIPS_SYS(sys_flock
, 2)
1816 MIPS_SYS(sys_msync
, 3)
1817 MIPS_SYS(sys_readv
, 3) /* 4145 */
1818 MIPS_SYS(sys_writev
, 3)
1819 MIPS_SYS(sys_cacheflush
, 3)
1820 MIPS_SYS(sys_cachectl
, 3)
1821 MIPS_SYS(sys_sysmips
, 4)
1822 MIPS_SYS(sys_ni_syscall
, 0) /* 4150 */
1823 MIPS_SYS(sys_getsid
, 1)
1824 MIPS_SYS(sys_fdatasync
, 0)
1825 MIPS_SYS(sys_sysctl
, 1)
1826 MIPS_SYS(sys_mlock
, 2)
1827 MIPS_SYS(sys_munlock
, 2) /* 4155 */
1828 MIPS_SYS(sys_mlockall
, 1)
1829 MIPS_SYS(sys_munlockall
, 0)
1830 MIPS_SYS(sys_sched_setparam
, 2)
1831 MIPS_SYS(sys_sched_getparam
, 2)
1832 MIPS_SYS(sys_sched_setscheduler
, 3) /* 4160 */
1833 MIPS_SYS(sys_sched_getscheduler
, 1)
1834 MIPS_SYS(sys_sched_yield
, 0)
1835 MIPS_SYS(sys_sched_get_priority_max
, 1)
1836 MIPS_SYS(sys_sched_get_priority_min
, 1)
1837 MIPS_SYS(sys_sched_rr_get_interval
, 2) /* 4165 */
1838 MIPS_SYS(sys_nanosleep
, 2)
1839 MIPS_SYS(sys_mremap
, 4)
1840 MIPS_SYS(sys_accept
, 3)
1841 MIPS_SYS(sys_bind
, 3)
1842 MIPS_SYS(sys_connect
, 3) /* 4170 */
1843 MIPS_SYS(sys_getpeername
, 3)
1844 MIPS_SYS(sys_getsockname
, 3)
1845 MIPS_SYS(sys_getsockopt
, 5)
1846 MIPS_SYS(sys_listen
, 2)
1847 MIPS_SYS(sys_recv
, 4) /* 4175 */
1848 MIPS_SYS(sys_recvfrom
, 6)
1849 MIPS_SYS(sys_recvmsg
, 3)
1850 MIPS_SYS(sys_send
, 4)
1851 MIPS_SYS(sys_sendmsg
, 3)
1852 MIPS_SYS(sys_sendto
, 6) /* 4180 */
1853 MIPS_SYS(sys_setsockopt
, 5)
1854 MIPS_SYS(sys_shutdown
, 2)
1855 MIPS_SYS(sys_socket
, 3)
1856 MIPS_SYS(sys_socketpair
, 4)
1857 MIPS_SYS(sys_setresuid
, 3) /* 4185 */
1858 MIPS_SYS(sys_getresuid
, 3)
1859 MIPS_SYS(sys_ni_syscall
, 0) /* was sys_query_module */
1860 MIPS_SYS(sys_poll
, 3)
1861 MIPS_SYS(sys_nfsservctl
, 3)
1862 MIPS_SYS(sys_setresgid
, 3) /* 4190 */
1863 MIPS_SYS(sys_getresgid
, 3)
1864 MIPS_SYS(sys_prctl
, 5)
1865 MIPS_SYS(sys_rt_sigreturn
, 0)
1866 MIPS_SYS(sys_rt_sigaction
, 4)
1867 MIPS_SYS(sys_rt_sigprocmask
, 4) /* 4195 */
1868 MIPS_SYS(sys_rt_sigpending
, 2)
1869 MIPS_SYS(sys_rt_sigtimedwait
, 4)
1870 MIPS_SYS(sys_rt_sigqueueinfo
, 3)
1871 MIPS_SYS(sys_rt_sigsuspend
, 0)
1872 MIPS_SYS(sys_pread64
, 6) /* 4200 */
1873 MIPS_SYS(sys_pwrite64
, 6)
1874 MIPS_SYS(sys_chown
, 3)
1875 MIPS_SYS(sys_getcwd
, 2)
1876 MIPS_SYS(sys_capget
, 2)
1877 MIPS_SYS(sys_capset
, 2) /* 4205 */
1878 MIPS_SYS(sys_sigaltstack
, 2)
1879 MIPS_SYS(sys_sendfile
, 4)
1880 MIPS_SYS(sys_ni_syscall
, 0)
1881 MIPS_SYS(sys_ni_syscall
, 0)
1882 MIPS_SYS(sys_mmap2
, 6) /* 4210 */
1883 MIPS_SYS(sys_truncate64
, 4)
1884 MIPS_SYS(sys_ftruncate64
, 4)
1885 MIPS_SYS(sys_stat64
, 2)
1886 MIPS_SYS(sys_lstat64
, 2)
1887 MIPS_SYS(sys_fstat64
, 2) /* 4215 */
1888 MIPS_SYS(sys_pivot_root
, 2)
1889 MIPS_SYS(sys_mincore
, 3)
1890 MIPS_SYS(sys_madvise
, 3)
1891 MIPS_SYS(sys_getdents64
, 3)
1892 MIPS_SYS(sys_fcntl64
, 3) /* 4220 */
1893 MIPS_SYS(sys_ni_syscall
, 0)
1894 MIPS_SYS(sys_gettid
, 0)
1895 MIPS_SYS(sys_readahead
, 5)
1896 MIPS_SYS(sys_setxattr
, 5)
1897 MIPS_SYS(sys_lsetxattr
, 5) /* 4225 */
1898 MIPS_SYS(sys_fsetxattr
, 5)
1899 MIPS_SYS(sys_getxattr
, 4)
1900 MIPS_SYS(sys_lgetxattr
, 4)
1901 MIPS_SYS(sys_fgetxattr
, 4)
1902 MIPS_SYS(sys_listxattr
, 3) /* 4230 */
1903 MIPS_SYS(sys_llistxattr
, 3)
1904 MIPS_SYS(sys_flistxattr
, 3)
1905 MIPS_SYS(sys_removexattr
, 2)
1906 MIPS_SYS(sys_lremovexattr
, 2)
1907 MIPS_SYS(sys_fremovexattr
, 2) /* 4235 */
1908 MIPS_SYS(sys_tkill
, 2)
1909 MIPS_SYS(sys_sendfile64
, 5)
1910 MIPS_SYS(sys_futex
, 2)
1911 MIPS_SYS(sys_sched_setaffinity
, 3)
1912 MIPS_SYS(sys_sched_getaffinity
, 3) /* 4240 */
1913 MIPS_SYS(sys_io_setup
, 2)
1914 MIPS_SYS(sys_io_destroy
, 1)
1915 MIPS_SYS(sys_io_getevents
, 5)
1916 MIPS_SYS(sys_io_submit
, 3)
1917 MIPS_SYS(sys_io_cancel
, 3) /* 4245 */
1918 MIPS_SYS(sys_exit_group
, 1)
1919 MIPS_SYS(sys_lookup_dcookie
, 3)
1920 MIPS_SYS(sys_epoll_create
, 1)
1921 MIPS_SYS(sys_epoll_ctl
, 4)
1922 MIPS_SYS(sys_epoll_wait
, 3) /* 4250 */
1923 MIPS_SYS(sys_remap_file_pages
, 5)
1924 MIPS_SYS(sys_set_tid_address
, 1)
1925 MIPS_SYS(sys_restart_syscall
, 0)
1926 MIPS_SYS(sys_fadvise64_64
, 7)
1927 MIPS_SYS(sys_statfs64
, 3) /* 4255 */
1928 MIPS_SYS(sys_fstatfs64
, 2)
1929 MIPS_SYS(sys_timer_create
, 3)
1930 MIPS_SYS(sys_timer_settime
, 4)
1931 MIPS_SYS(sys_timer_gettime
, 2)
1932 MIPS_SYS(sys_timer_getoverrun
, 1) /* 4260 */
1933 MIPS_SYS(sys_timer_delete
, 1)
1934 MIPS_SYS(sys_clock_settime
, 2)
1935 MIPS_SYS(sys_clock_gettime
, 2)
1936 MIPS_SYS(sys_clock_getres
, 2)
1937 MIPS_SYS(sys_clock_nanosleep
, 4) /* 4265 */
1938 MIPS_SYS(sys_tgkill
, 3)
1939 MIPS_SYS(sys_utimes
, 2)
1940 MIPS_SYS(sys_mbind
, 4)
1941 MIPS_SYS(sys_ni_syscall
, 0) /* sys_get_mempolicy */
1942 MIPS_SYS(sys_ni_syscall
, 0) /* 4270 sys_set_mempolicy */
1943 MIPS_SYS(sys_mq_open
, 4)
1944 MIPS_SYS(sys_mq_unlink
, 1)
1945 MIPS_SYS(sys_mq_timedsend
, 5)
1946 MIPS_SYS(sys_mq_timedreceive
, 5)
1947 MIPS_SYS(sys_mq_notify
, 2) /* 4275 */
1948 MIPS_SYS(sys_mq_getsetattr
, 3)
1949 MIPS_SYS(sys_ni_syscall
, 0) /* sys_vserver */
1950 MIPS_SYS(sys_waitid
, 4)
1951 MIPS_SYS(sys_ni_syscall
, 0) /* available, was setaltroot */
1952 MIPS_SYS(sys_add_key
, 5)
1953 MIPS_SYS(sys_request_key
, 4)
1954 MIPS_SYS(sys_keyctl
, 5)
1955 MIPS_SYS(sys_set_thread_area
, 1)
1956 MIPS_SYS(sys_inotify_init
, 0)
1957 MIPS_SYS(sys_inotify_add_watch
, 3) /* 4285 */
1958 MIPS_SYS(sys_inotify_rm_watch
, 2)
1959 MIPS_SYS(sys_migrate_pages
, 4)
1960 MIPS_SYS(sys_openat
, 4)
1961 MIPS_SYS(sys_mkdirat
, 3)
1962 MIPS_SYS(sys_mknodat
, 4) /* 4290 */
1963 MIPS_SYS(sys_fchownat
, 5)
1964 MIPS_SYS(sys_futimesat
, 3)
1965 MIPS_SYS(sys_fstatat64
, 4)
1966 MIPS_SYS(sys_unlinkat
, 3)
1967 MIPS_SYS(sys_renameat
, 4) /* 4295 */
1968 MIPS_SYS(sys_linkat
, 5)
1969 MIPS_SYS(sys_symlinkat
, 3)
1970 MIPS_SYS(sys_readlinkat
, 4)
1971 MIPS_SYS(sys_fchmodat
, 3)
1972 MIPS_SYS(sys_faccessat
, 3) /* 4300 */
1973 MIPS_SYS(sys_pselect6
, 6)
1974 MIPS_SYS(sys_ppoll
, 5)
1975 MIPS_SYS(sys_unshare
, 1)
1976 MIPS_SYS(sys_splice
, 4)
1977 MIPS_SYS(sys_sync_file_range
, 7) /* 4305 */
1978 MIPS_SYS(sys_tee
, 4)
1979 MIPS_SYS(sys_vmsplice
, 4)
1980 MIPS_SYS(sys_move_pages
, 6)
1981 MIPS_SYS(sys_set_robust_list
, 2)
1982 MIPS_SYS(sys_get_robust_list
, 3) /* 4310 */
1983 MIPS_SYS(sys_kexec_load
, 4)
1984 MIPS_SYS(sys_getcpu
, 3)
1985 MIPS_SYS(sys_epoll_pwait
, 6)
1986 MIPS_SYS(sys_ioprio_set
, 3)
1987 MIPS_SYS(sys_ioprio_get
, 2)
1988 MIPS_SYS(sys_utimensat
, 4)
1989 MIPS_SYS(sys_signalfd
, 3)
1990 MIPS_SYS(sys_ni_syscall
, 0) /* was timerfd */
1991 MIPS_SYS(sys_eventfd
, 1)
1992 MIPS_SYS(sys_fallocate
, 6) /* 4320 */
1993 MIPS_SYS(sys_timerfd_create
, 2)
1994 MIPS_SYS(sys_timerfd_gettime
, 2)
1995 MIPS_SYS(sys_timerfd_settime
, 4)
1996 MIPS_SYS(sys_signalfd4
, 4)
1997 MIPS_SYS(sys_eventfd2
, 2) /* 4325 */
1998 MIPS_SYS(sys_epoll_create1
, 1)
1999 MIPS_SYS(sys_dup3
, 3)
2000 MIPS_SYS(sys_pipe2
, 2)
2001 MIPS_SYS(sys_inotify_init1
, 1)
2002 MIPS_SYS(sys_preadv
, 6) /* 4330 */
2003 MIPS_SYS(sys_pwritev
, 6)
2004 MIPS_SYS(sys_rt_tgsigqueueinfo
, 4)
2005 MIPS_SYS(sys_perf_event_open
, 5)
2006 MIPS_SYS(sys_accept4
, 4)
2007 MIPS_SYS(sys_recvmmsg
, 5) /* 4335 */
2008 MIPS_SYS(sys_fanotify_init
, 2)
2009 MIPS_SYS(sys_fanotify_mark
, 6)
2010 MIPS_SYS(sys_prlimit64
, 4)
2011 MIPS_SYS(sys_name_to_handle_at
, 5)
2012 MIPS_SYS(sys_open_by_handle_at
, 3) /* 4340 */
2013 MIPS_SYS(sys_clock_adjtime
, 2)
2014 MIPS_SYS(sys_syncfs
, 1)
2019 static int do_store_exclusive(CPUMIPSState
*env
)
2022 target_ulong page_addr
;
2030 page_addr
= addr
& TARGET_PAGE_MASK
;
2033 flags
= page_get_flags(page_addr
);
2034 if ((flags
& PAGE_READ
) == 0) {
2037 reg
= env
->llreg
& 0x1f;
2038 d
= (env
->llreg
& 0x20) != 0;
2040 segv
= get_user_s64(val
, addr
);
2042 segv
= get_user_s32(val
, addr
);
2045 if (val
!= env
->llval
) {
2046 env
->active_tc
.gpr
[reg
] = 0;
2049 segv
= put_user_u64(env
->llnewval
, addr
);
2051 segv
= put_user_u32(env
->llnewval
, addr
);
2054 env
->active_tc
.gpr
[reg
] = 1;
2061 env
->active_tc
.PC
+= 4;
2068 void cpu_loop(CPUMIPSState
*env
)
2070 target_siginfo_t info
;
2072 unsigned int syscall_num
;
2075 cpu_exec_start(env
);
2076 trapnr
= cpu_mips_exec(env
);
2080 syscall_num
= env
->active_tc
.gpr
[2] - 4000;
2081 env
->active_tc
.PC
+= 4;
2082 if (syscall_num
>= sizeof(mips_syscall_args
)) {
2083 ret
= -TARGET_ENOSYS
;
2087 abi_ulong arg5
= 0, arg6
= 0, arg7
= 0, arg8
= 0;
2089 nb_args
= mips_syscall_args
[syscall_num
];
2090 sp_reg
= env
->active_tc
.gpr
[29];
2092 /* these arguments are taken from the stack */
2093 /* FIXME - what to do if get_user() fails? */
2094 case 8: get_user_ual(arg8
, sp_reg
+ 28);
2095 case 7: get_user_ual(arg7
, sp_reg
+ 24);
2096 case 6: get_user_ual(arg6
, sp_reg
+ 20);
2097 case 5: get_user_ual(arg5
, sp_reg
+ 16);
2101 ret
= do_syscall(env
, env
->active_tc
.gpr
[2],
2102 env
->active_tc
.gpr
[4],
2103 env
->active_tc
.gpr
[5],
2104 env
->active_tc
.gpr
[6],
2105 env
->active_tc
.gpr
[7],
2106 arg5
, arg6
, arg7
, arg8
);
2108 if (ret
== -TARGET_QEMU_ESIGRETURN
) {
2109 /* Returning from a successful sigreturn syscall.
2110 Avoid clobbering register state. */
2113 if ((unsigned int)ret
>= (unsigned int)(-1133)) {
2114 env
->active_tc
.gpr
[7] = 1; /* error flag */
2117 env
->active_tc
.gpr
[7] = 0; /* error flag */
2119 env
->active_tc
.gpr
[2] = ret
;
2125 info
.si_signo
= TARGET_SIGSEGV
;
2127 /* XXX: check env->error_code */
2128 info
.si_code
= TARGET_SEGV_MAPERR
;
2129 info
._sifields
._sigfault
._addr
= env
->CP0_BadVAddr
;
2130 queue_signal(env
, info
.si_signo
, &info
);
2134 info
.si_signo
= TARGET_SIGILL
;
2137 queue_signal(env
, info
.si_signo
, &info
);
2139 case EXCP_INTERRUPT
:
2140 /* just indicate that signals should be handled asap */
2146 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2149 info
.si_signo
= sig
;
2151 info
.si_code
= TARGET_TRAP_BRKPT
;
2152 queue_signal(env
, info
.si_signo
, &info
);
2157 if (do_store_exclusive(env
)) {
2158 info
.si_signo
= TARGET_SIGSEGV
;
2160 info
.si_code
= TARGET_SEGV_MAPERR
;
2161 info
._sifields
._sigfault
._addr
= env
->active_tc
.PC
;
2162 queue_signal(env
, info
.si_signo
, &info
);
2167 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
2169 cpu_dump_state(env
, stderr
, fprintf
, 0);
2172 process_pending_signals(env
);
2178 void cpu_loop (CPUState
*env
)
2181 target_siginfo_t info
;
2184 trapnr
= cpu_sh4_exec (env
);
2189 ret
= do_syscall(env
,
2198 env
->gregs
[0] = ret
;
2200 case EXCP_INTERRUPT
:
2201 /* just indicate that signals should be handled asap */
2207 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2210 info
.si_signo
= sig
;
2212 info
.si_code
= TARGET_TRAP_BRKPT
;
2213 queue_signal(env
, info
.si_signo
, &info
);
2219 info
.si_signo
= SIGSEGV
;
2221 info
.si_code
= TARGET_SEGV_MAPERR
;
2222 info
._sifields
._sigfault
._addr
= env
->tea
;
2223 queue_signal(env
, info
.si_signo
, &info
);
2227 printf ("Unhandled trap: 0x%x\n", trapnr
);
2228 cpu_dump_state(env
, stderr
, fprintf
, 0);
2231 process_pending_signals (env
);
2237 void cpu_loop (CPUState
*env
)
2240 target_siginfo_t info
;
2243 trapnr
= cpu_cris_exec (env
);
2247 info
.si_signo
= SIGSEGV
;
2249 /* XXX: check env->error_code */
2250 info
.si_code
= TARGET_SEGV_MAPERR
;
2251 info
._sifields
._sigfault
._addr
= env
->pregs
[PR_EDA
];
2252 queue_signal(env
, info
.si_signo
, &info
);
2255 case EXCP_INTERRUPT
:
2256 /* just indicate that signals should be handled asap */
2259 ret
= do_syscall(env
,
2268 env
->regs
[10] = ret
;
2274 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2277 info
.si_signo
= sig
;
2279 info
.si_code
= TARGET_TRAP_BRKPT
;
2280 queue_signal(env
, info
.si_signo
, &info
);
2285 printf ("Unhandled trap: 0x%x\n", trapnr
);
2286 cpu_dump_state(env
, stderr
, fprintf
, 0);
2289 process_pending_signals (env
);
2294 #ifdef TARGET_MICROBLAZE
2295 void cpu_loop (CPUState
*env
)
2298 target_siginfo_t info
;
2301 trapnr
= cpu_mb_exec (env
);
2305 info
.si_signo
= SIGSEGV
;
2307 /* XXX: check env->error_code */
2308 info
.si_code
= TARGET_SEGV_MAPERR
;
2309 info
._sifields
._sigfault
._addr
= 0;
2310 queue_signal(env
, info
.si_signo
, &info
);
2313 case EXCP_INTERRUPT
:
2314 /* just indicate that signals should be handled asap */
2317 /* Return address is 4 bytes after the call. */
2319 ret
= do_syscall(env
,
2329 env
->sregs
[SR_PC
] = env
->regs
[14];
2332 env
->regs
[17] = env
->sregs
[SR_PC
] + 4;
2333 if (env
->iflags
& D_FLAG
) {
2334 env
->sregs
[SR_ESR
] |= 1 << 12;
2335 env
->sregs
[SR_PC
] -= 4;
2336 /* FIXME: if branch was immed, replay the imm aswell. */
2339 env
->iflags
&= ~(IMM_FLAG
| D_FLAG
);
2341 switch (env
->sregs
[SR_ESR
] & 31) {
2342 case ESR_EC_DIVZERO
:
2343 info
.si_signo
= SIGFPE
;
2345 info
.si_code
= TARGET_FPE_FLTDIV
;
2346 info
._sifields
._sigfault
._addr
= 0;
2347 queue_signal(env
, info
.si_signo
, &info
);
2350 info
.si_signo
= SIGFPE
;
2352 if (env
->sregs
[SR_FSR
] & FSR_IO
) {
2353 info
.si_code
= TARGET_FPE_FLTINV
;
2355 if (env
->sregs
[SR_FSR
] & FSR_DZ
) {
2356 info
.si_code
= TARGET_FPE_FLTDIV
;
2358 info
._sifields
._sigfault
._addr
= 0;
2359 queue_signal(env
, info
.si_signo
, &info
);
2362 printf ("Unhandled hw-exception: 0x%x\n",
2363 env
->sregs
[SR_ESR
] & ESR_EC_MASK
);
2364 cpu_dump_state(env
, stderr
, fprintf
, 0);
2373 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2376 info
.si_signo
= sig
;
2378 info
.si_code
= TARGET_TRAP_BRKPT
;
2379 queue_signal(env
, info
.si_signo
, &info
);
2384 printf ("Unhandled trap: 0x%x\n", trapnr
);
2385 cpu_dump_state(env
, stderr
, fprintf
, 0);
2388 process_pending_signals (env
);
2395 void cpu_loop(CPUM68KState
*env
)
2399 target_siginfo_t info
;
2400 TaskState
*ts
= env
->opaque
;
2403 trapnr
= cpu_m68k_exec(env
);
2407 if (ts
->sim_syscalls
) {
2409 nr
= lduw(env
->pc
+ 2);
2411 do_m68k_simcall(env
, nr
);
2417 case EXCP_HALT_INSN
:
2418 /* Semihosing syscall. */
2420 do_m68k_semihosting(env
, env
->dregs
[0]);
2424 case EXCP_UNSUPPORTED
:
2426 info
.si_signo
= SIGILL
;
2428 info
.si_code
= TARGET_ILL_ILLOPN
;
2429 info
._sifields
._sigfault
._addr
= env
->pc
;
2430 queue_signal(env
, info
.si_signo
, &info
);
2434 ts
->sim_syscalls
= 0;
2437 env
->dregs
[0] = do_syscall(env
,
2448 case EXCP_INTERRUPT
:
2449 /* just indicate that signals should be handled asap */
2453 info
.si_signo
= SIGSEGV
;
2455 /* XXX: check env->error_code */
2456 info
.si_code
= TARGET_SEGV_MAPERR
;
2457 info
._sifields
._sigfault
._addr
= env
->mmu
.ar
;
2458 queue_signal(env
, info
.si_signo
, &info
);
2465 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2468 info
.si_signo
= sig
;
2470 info
.si_code
= TARGET_TRAP_BRKPT
;
2471 queue_signal(env
, info
.si_signo
, &info
);
2476 fprintf(stderr
, "qemu: unhandled CPU exception 0x%x - aborting\n",
2478 cpu_dump_state(env
, stderr
, fprintf
, 0);
2481 process_pending_signals(env
);
2484 #endif /* TARGET_M68K */
2487 static void do_store_exclusive(CPUAlphaState
*env
, int reg
, int quad
)
2489 target_ulong addr
, val
, tmp
;
2490 target_siginfo_t info
;
2493 addr
= env
->lock_addr
;
2494 tmp
= env
->lock_st_addr
;
2495 env
->lock_addr
= -1;
2496 env
->lock_st_addr
= 0;
2502 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
2506 if (val
== env
->lock_value
) {
2508 if (quad
? put_user_u64(tmp
, addr
) : put_user_u32(tmp
, addr
)) {
2525 info
.si_signo
= TARGET_SIGSEGV
;
2527 info
.si_code
= TARGET_SEGV_MAPERR
;
2528 info
._sifields
._sigfault
._addr
= addr
;
2529 queue_signal(env
, TARGET_SIGSEGV
, &info
);
2532 void cpu_loop (CPUState
*env
)
2535 target_siginfo_t info
;
2539 trapnr
= cpu_alpha_exec (env
);
2541 /* All of the traps imply a transition through PALcode, which
2542 implies an REI instruction has been executed. Which means
2543 that the intr_flag should be cleared. */
2548 fprintf(stderr
, "Reset requested. Exit\n");
2552 fprintf(stderr
, "Machine check exception. Exit\n");
2555 case EXCP_SMP_INTERRUPT
:
2556 case EXCP_CLK_INTERRUPT
:
2557 case EXCP_DEV_INTERRUPT
:
2558 fprintf(stderr
, "External interrupt. Exit\n");
2562 env
->lock_addr
= -1;
2563 info
.si_signo
= TARGET_SIGSEGV
;
2565 info
.si_code
= (page_get_flags(env
->trap_arg0
) & PAGE_VALID
2566 ? TARGET_SEGV_ACCERR
: TARGET_SEGV_MAPERR
);
2567 info
._sifields
._sigfault
._addr
= env
->trap_arg0
;
2568 queue_signal(env
, info
.si_signo
, &info
);
2571 env
->lock_addr
= -1;
2572 info
.si_signo
= TARGET_SIGBUS
;
2574 info
.si_code
= TARGET_BUS_ADRALN
;
2575 info
._sifields
._sigfault
._addr
= env
->trap_arg0
;
2576 queue_signal(env
, info
.si_signo
, &info
);
2580 env
->lock_addr
= -1;
2581 info
.si_signo
= TARGET_SIGILL
;
2583 info
.si_code
= TARGET_ILL_ILLOPC
;
2584 info
._sifields
._sigfault
._addr
= env
->pc
;
2585 queue_signal(env
, info
.si_signo
, &info
);
2588 env
->lock_addr
= -1;
2589 info
.si_signo
= TARGET_SIGFPE
;
2591 info
.si_code
= TARGET_FPE_FLTINV
;
2592 info
._sifields
._sigfault
._addr
= env
->pc
;
2593 queue_signal(env
, info
.si_signo
, &info
);
2596 /* No-op. Linux simply re-enables the FPU. */
2599 env
->lock_addr
= -1;
2600 switch (env
->error_code
) {
2603 info
.si_signo
= TARGET_SIGTRAP
;
2605 info
.si_code
= TARGET_TRAP_BRKPT
;
2606 info
._sifields
._sigfault
._addr
= env
->pc
;
2607 queue_signal(env
, info
.si_signo
, &info
);
2611 info
.si_signo
= TARGET_SIGTRAP
;
2614 info
._sifields
._sigfault
._addr
= env
->pc
;
2615 queue_signal(env
, info
.si_signo
, &info
);
2619 trapnr
= env
->ir
[IR_V0
];
2620 sysret
= do_syscall(env
, trapnr
,
2621 env
->ir
[IR_A0
], env
->ir
[IR_A1
],
2622 env
->ir
[IR_A2
], env
->ir
[IR_A3
],
2623 env
->ir
[IR_A4
], env
->ir
[IR_A5
],
2625 if (trapnr
== TARGET_NR_sigreturn
2626 || trapnr
== TARGET_NR_rt_sigreturn
) {
2629 /* Syscall writes 0 to V0 to bypass error check, similar
2630 to how this is handled internal to Linux kernel. */
2631 if (env
->ir
[IR_V0
] == 0) {
2632 env
->ir
[IR_V0
] = sysret
;
2634 env
->ir
[IR_V0
] = (sysret
< 0 ? -sysret
: sysret
);
2635 env
->ir
[IR_A3
] = (sysret
< 0);
2640 /* ??? We can probably elide the code using page_unprotect
2641 that is checking for self-modifying code. Instead we
2642 could simply call tb_flush here. Until we work out the
2643 changes required to turn off the extra write protection,
2644 this can be a no-op. */
2648 /* Handled in the translator for usermode. */
2652 /* Handled in the translator for usermode. */
2656 info
.si_signo
= TARGET_SIGFPE
;
2657 switch (env
->ir
[IR_A0
]) {
2658 case TARGET_GEN_INTOVF
:
2659 info
.si_code
= TARGET_FPE_INTOVF
;
2661 case TARGET_GEN_INTDIV
:
2662 info
.si_code
= TARGET_FPE_INTDIV
;
2664 case TARGET_GEN_FLTOVF
:
2665 info
.si_code
= TARGET_FPE_FLTOVF
;
2667 case TARGET_GEN_FLTUND
:
2668 info
.si_code
= TARGET_FPE_FLTUND
;
2670 case TARGET_GEN_FLTINV
:
2671 info
.si_code
= TARGET_FPE_FLTINV
;
2673 case TARGET_GEN_FLTINE
:
2674 info
.si_code
= TARGET_FPE_FLTRES
;
2676 case TARGET_GEN_ROPRAND
:
2680 info
.si_signo
= TARGET_SIGTRAP
;
2685 info
._sifields
._sigfault
._addr
= env
->pc
;
2686 queue_signal(env
, info
.si_signo
, &info
);
2693 info
.si_signo
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2694 if (info
.si_signo
) {
2695 env
->lock_addr
= -1;
2697 info
.si_code
= TARGET_TRAP_BRKPT
;
2698 queue_signal(env
, info
.si_signo
, &info
);
2703 do_store_exclusive(env
, env
->error_code
, trapnr
- EXCP_STL_C
);
2706 printf ("Unhandled trap: 0x%x\n", trapnr
);
2707 cpu_dump_state(env
, stderr
, fprintf
, 0);
2710 process_pending_signals (env
);
2713 #endif /* TARGET_ALPHA */
2716 void cpu_loop(CPUS390XState
*env
)
2719 target_siginfo_t info
;
2722 trapnr
= cpu_s390x_exec (env
);
2725 case EXCP_INTERRUPT
:
2726 /* just indicate that signals should be handled asap */
2732 sig
= gdb_handlesig (env
, TARGET_SIGTRAP
);
2734 info
.si_signo
= sig
;
2736 info
.si_code
= TARGET_TRAP_BRKPT
;
2737 queue_signal(env
, info
.si_signo
, &info
);
2743 int n
= env
->int_svc_code
;
2745 /* syscalls > 255 */
2748 env
->psw
.addr
+= env
->int_svc_ilc
;
2749 env
->regs
[2] = do_syscall(env
, n
,
2761 info
.si_signo
= SIGSEGV
;
2763 /* XXX: check env->error_code */
2764 info
.si_code
= TARGET_SEGV_MAPERR
;
2765 info
._sifields
._sigfault
._addr
= env
->__excp_addr
;
2766 queue_signal(env
, info
.si_signo
, &info
);
2771 fprintf(stderr
,"specification exception insn 0x%08x%04x\n", ldl(env
->psw
.addr
), lduw(env
->psw
.addr
+ 4));
2772 info
.si_signo
= SIGILL
;
2774 info
.si_code
= TARGET_ILL_ILLOPC
;
2775 info
._sifields
._sigfault
._addr
= env
->__excp_addr
;
2776 queue_signal(env
, info
.si_signo
, &info
);
2780 printf ("Unhandled trap: 0x%x\n", trapnr
);
2781 cpu_dump_state(env
, stderr
, fprintf
, 0);
2784 process_pending_signals (env
);
2788 #endif /* TARGET_S390X */
2790 static void version(void)
2792 printf("qemu-" TARGET_ARCH
" version " QEMU_VERSION QEMU_PKGVERSION
2793 ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2796 static void usage(void)
2799 printf("usage: qemu-" TARGET_ARCH
" [options] program [arguments...]\n"
2800 "Linux CPU emulator (compiled for %s emulation)\n"
2802 "Standard options:\n"
2803 "-h print this help\n"
2804 "-version display version information and exit\n"
2805 "-g port wait gdb connection to port\n"
2806 "-L path set the elf interpreter prefix (default=%s)\n"
2807 "-s size set the stack size in bytes (default=%ld)\n"
2808 "-cpu model select CPU (-cpu ? for list)\n"
2809 "-drop-ld-preload drop LD_PRELOAD for target process\n"
2810 "-E var=value sets/modifies targets environment variable(s)\n"
2811 "-U var unsets targets environment variable(s)\n"
2812 "-0 argv0 forces target process argv[0] to be argv0\n"
2813 #if defined(CONFIG_USE_GUEST_BASE)
2814 "-B address set guest_base address to address\n"
2815 "-R size reserve size bytes for guest virtual address space\n"
2819 "-d options activate log (logfile=%s)\n"
2820 "-p pagesize set the host page size to 'pagesize'\n"
2821 "-singlestep always run in singlestep mode\n"
2822 "-strace log system calls\n"
2824 "Environment variables:\n"
2825 "QEMU_STRACE Print system calls and arguments similar to the\n"
2826 " 'strace' program. Enable by setting to any value.\n"
2827 "You can use -E and -U options to set/unset environment variables\n"
2828 "for target process. It is possible to provide several variables\n"
2829 "by repeating the option. For example:\n"
2830 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
2831 "Note that if you provide several changes to single variable\n"
2832 "last change will stay in effect.\n"
2841 THREAD CPUState
*thread_env
;
2843 void task_settid(TaskState
*ts
)
2845 if (ts
->ts_tid
== 0) {
2846 #ifdef CONFIG_USE_NPTL
2847 ts
->ts_tid
= (pid_t
)syscall(SYS_gettid
);
2849 /* when no threads are used, tid becomes pid */
2850 ts
->ts_tid
= getpid();
2855 void stop_all_tasks(void)
2858 * We trust that when using NPTL, start_exclusive()
2859 * handles thread stopping correctly.
2864 /* Assumes contents are already zeroed. */
2865 void init_task_state(TaskState
*ts
)
2870 ts
->first_free
= ts
->sigqueue_table
;
2871 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
2872 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
2874 ts
->sigqueue_table
[i
].next
= NULL
;
2877 int main(int argc
, char **argv
, char **envp
)
2879 const char *filename
;
2880 const char *cpu_model
;
2881 const char *log_file
= DEBUG_LOGFILE
;
2882 const char *log_mask
= NULL
;
2883 struct target_pt_regs regs1
, *regs
= ®s1
;
2884 struct image_info info1
, *info
= &info1
;
2885 struct linux_binprm bprm
;
2890 int gdbstub_port
= 0;
2891 char **target_environ
, **wrk
;
2894 envlist_t
*envlist
= NULL
;
2895 const char *argv0
= NULL
;
2902 qemu_cache_utils_init(envp
);
2904 if ((envlist
= envlist_create()) == NULL
) {
2905 (void) fprintf(stderr
, "Unable to allocate envlist\n");
2909 /* add current environment into the list */
2910 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
2911 (void) envlist_setenv(envlist
, *wrk
);
2914 /* Read the stack limit from the kernel. If it's "unlimited",
2915 then we can do little else besides use the default. */
2918 if (getrlimit(RLIMIT_STACK
, &lim
) == 0
2919 && lim
.rlim_cur
!= RLIM_INFINITY
2920 && lim
.rlim_cur
== (target_long
)lim
.rlim_cur
) {
2921 guest_stack_size
= lim
.rlim_cur
;
2926 #if defined(cpudef_setup)
2927 cpudef_setup(); /* parse cpu definitions in target config file (TBD) */
2939 if (!strcmp(r
, "-")) {
2941 } else if (!strcmp(r
, "d")) {
2942 if (optind
>= argc
) {
2945 log_mask
= argv
[optind
++];
2946 } else if (!strcmp(r
, "D")) {
2947 if (optind
>= argc
) {
2950 log_file
= argv
[optind
++];
2951 } else if (!strcmp(r
, "E")) {
2953 if (envlist_setenv(envlist
, r
) != 0)
2955 } else if (!strcmp(r
, "ignore-environment")) {
2956 envlist_free(envlist
);
2957 if ((envlist
= envlist_create()) == NULL
) {
2958 (void) fprintf(stderr
, "Unable to allocate envlist\n");
2961 } else if (!strcmp(r
, "U")) {
2963 if (envlist_unsetenv(envlist
, r
) != 0)
2965 } else if (!strcmp(r
, "0")) {
2968 } else if (!strcmp(r
, "s")) {
2972 guest_stack_size
= strtoul(r
, (char **)&r
, 0);
2973 if (guest_stack_size
== 0)
2976 guest_stack_size
*= 1024 * 1024;
2977 else if (*r
== 'k' || *r
== 'K')
2978 guest_stack_size
*= 1024;
2979 } else if (!strcmp(r
, "L")) {
2980 interp_prefix
= argv
[optind
++];
2981 } else if (!strcmp(r
, "p")) {
2984 qemu_host_page_size
= atoi(argv
[optind
++]);
2985 if (qemu_host_page_size
== 0 ||
2986 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
2987 fprintf(stderr
, "page size must be a power of two\n");
2990 } else if (!strcmp(r
, "g")) {
2993 gdbstub_port
= atoi(argv
[optind
++]);
2994 } else if (!strcmp(r
, "r")) {
2995 qemu_uname_release
= argv
[optind
++];
2996 } else if (!strcmp(r
, "cpu")) {
2997 cpu_model
= argv
[optind
++];
2998 if (cpu_model
== NULL
|| strcmp(cpu_model
, "?") == 0) {
2999 /* XXX: implement xxx_cpu_list for targets that still miss it */
3000 #if defined(cpu_list_id)
3001 cpu_list_id(stdout
, &fprintf
, "");
3002 #elif defined(cpu_list)
3003 cpu_list(stdout
, &fprintf
); /* deprecated */
3007 #if defined(CONFIG_USE_GUEST_BASE)
3008 } else if (!strcmp(r
, "B")) {
3009 guest_base
= strtol(argv
[optind
++], NULL
, 0);
3010 have_guest_base
= 1;
3011 } else if (!strcmp(r
, "R")) {
3014 reserved_va
= strtoul(argv
[optind
++], &p
, 0);
3028 unsigned long unshifted
= reserved_va
;
3030 reserved_va
<<= shift
;
3031 if (((reserved_va
>> shift
) != unshifted
)
3032 #if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
3033 || (reserved_va
> (1ul << TARGET_VIRT_ADDR_SPACE_BITS
))
3036 fprintf(stderr
, "Reserved virtual address too big\n");
3041 fprintf(stderr
, "Unrecognised -R size suffix '%s'\n", p
);
3045 } else if (!strcmp(r
, "drop-ld-preload")) {
3046 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
3047 } else if (!strcmp(r
, "singlestep")) {
3049 } else if (!strcmp(r
, "strace")) {
3051 } else if (!strcmp(r
, "version")) {
3059 cpu_set_log_filename(log_file
);
3062 const CPULogItem
*item
;
3064 mask
= cpu_str_to_log_mask(log_mask
);
3066 printf("Log items (comma separated):\n");
3067 for (item
= cpu_log_items
; item
->mask
!= 0; item
++) {
3068 printf("%-10s %s\n", item
->name
, item
->help
);
3075 if (optind
>= argc
) {
3078 filename
= argv
[optind
];
3079 exec_path
= argv
[optind
];
3082 memset(regs
, 0, sizeof(struct target_pt_regs
));
3084 /* Zero out image_info */
3085 memset(info
, 0, sizeof(struct image_info
));
3087 memset(&bprm
, 0, sizeof (bprm
));
3089 /* Scan interp_prefix dir for replacement files. */
3090 init_paths(interp_prefix
);
3092 if (cpu_model
== NULL
) {
3093 #if defined(TARGET_I386)
3094 #ifdef TARGET_X86_64
3095 cpu_model
= "qemu64";
3097 cpu_model
= "qemu32";
3099 #elif defined(TARGET_ARM)
3101 #elif defined(TARGET_UNICORE32)
3103 #elif defined(TARGET_M68K)
3105 #elif defined(TARGET_SPARC)
3106 #ifdef TARGET_SPARC64
3107 cpu_model
= "TI UltraSparc II";
3109 cpu_model
= "Fujitsu MB86904";
3111 #elif defined(TARGET_MIPS)
3112 #if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64)
3117 #elif defined(TARGET_PPC)
3119 cpu_model
= "970fx";
3128 cpu_exec_init_all();
3129 /* NOTE: we need to init the CPU at this stage to get
3130 qemu_host_page_size */
3131 env
= cpu_init(cpu_model
);
3133 fprintf(stderr
, "Unable to find CPU definition\n");
3136 #if defined(TARGET_I386) || defined(TARGET_SPARC) || defined(TARGET_PPC)
3142 if (getenv("QEMU_STRACE")) {
3146 target_environ
= envlist_to_environ(envlist
, NULL
);
3147 envlist_free(envlist
);
3149 #if defined(CONFIG_USE_GUEST_BASE)
3151 * Now that page sizes are configured in cpu_init() we can do
3152 * proper page alignment for guest_base.
3154 guest_base
= HOST_PAGE_ALIGN(guest_base
);
3160 flags
= MAP_ANONYMOUS
| MAP_PRIVATE
| MAP_NORESERVE
;
3161 if (have_guest_base
) {
3164 p
= mmap((void *)guest_base
, reserved_va
, PROT_NONE
, flags
, -1, 0);
3165 if (p
== MAP_FAILED
) {
3166 fprintf(stderr
, "Unable to reserve guest address space\n");
3169 guest_base
= (unsigned long)p
;
3170 /* Make sure the address is properly aligned. */
3171 if (guest_base
& ~qemu_host_page_mask
) {
3172 munmap(p
, reserved_va
);
3173 p
= mmap((void *)guest_base
, reserved_va
+ qemu_host_page_size
,
3174 PROT_NONE
, flags
, -1, 0);
3175 if (p
== MAP_FAILED
) {
3176 fprintf(stderr
, "Unable to reserve guest address space\n");
3179 guest_base
= HOST_PAGE_ALIGN((unsigned long)p
);
3181 qemu_log("Reserved 0x%lx bytes of guest address space\n", reserved_va
);
3183 #endif /* CONFIG_USE_GUEST_BASE */
3186 * Read in mmap_min_addr kernel parameter. This value is used
3187 * When loading the ELF image to determine whether guest_base
3188 * is needed. It is also used in mmap_find_vma.
3193 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
3195 if (fscanf(fp
, "%lu", &tmp
) == 1) {
3196 mmap_min_addr
= tmp
;
3197 qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr
);
3204 * Prepare copy of argv vector for target.
3206 target_argc
= argc
- optind
;
3207 target_argv
= calloc(target_argc
+ 1, sizeof (char *));
3208 if (target_argv
== NULL
) {
3209 (void) fprintf(stderr
, "Unable to allocate memory for target_argv\n");
3214 * If argv0 is specified (using '-0' switch) we replace
3215 * argv[0] pointer with the given one.
3218 if (argv0
!= NULL
) {
3219 target_argv
[i
++] = strdup(argv0
);
3221 for (; i
< target_argc
; i
++) {
3222 target_argv
[i
] = strdup(argv
[optind
+ i
]);
3224 target_argv
[target_argc
] = NULL
;
3226 ts
= g_malloc0 (sizeof(TaskState
));
3227 init_task_state(ts
);
3228 /* build Task State */
3234 ret
= loader_exec(filename
, target_argv
, target_environ
, regs
,
3237 printf("Error %d while loading %s\n", ret
, filename
);
3241 for (i
= 0; i
< target_argc
; i
++) {
3242 free(target_argv
[i
]);
3246 for (wrk
= target_environ
; *wrk
; wrk
++) {
3250 free(target_environ
);
3252 if (qemu_log_enabled()) {
3253 #if defined(CONFIG_USE_GUEST_BASE)
3254 qemu_log("guest_base 0x%lx\n", guest_base
);
3258 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
3259 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
3260 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
3262 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
3264 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
3265 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
3267 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
3268 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
3271 target_set_brk(info
->brk
);
3275 #if defined(CONFIG_USE_GUEST_BASE)
3276 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
3277 generating the prologue until now so that the prologue can take
3278 the real value of GUEST_BASE into account. */
3279 tcg_prologue_init(&tcg_ctx
);
3282 #if defined(TARGET_I386)
3283 cpu_x86_set_cpl(env
, 3);
3285 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
3286 env
->hflags
|= HF_PE_MASK
;
3287 if (env
->cpuid_features
& CPUID_SSE
) {
3288 env
->cr
[4] |= CR4_OSFXSR_MASK
;
3289 env
->hflags
|= HF_OSFXSR_MASK
;
3291 #ifndef TARGET_ABI32
3292 /* enable 64 bit mode if possible */
3293 if (!(env
->cpuid_ext2_features
& CPUID_EXT2_LM
)) {
3294 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
3297 env
->cr
[4] |= CR4_PAE_MASK
;
3298 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
3299 env
->hflags
|= HF_LMA_MASK
;
3302 /* flags setup : we activate the IRQs by default as in user mode */
3303 env
->eflags
|= IF_MASK
;
3305 /* linux register setup */
3306 #ifndef TARGET_ABI32
3307 env
->regs
[R_EAX
] = regs
->rax
;
3308 env
->regs
[R_EBX
] = regs
->rbx
;
3309 env
->regs
[R_ECX
] = regs
->rcx
;
3310 env
->regs
[R_EDX
] = regs
->rdx
;
3311 env
->regs
[R_ESI
] = regs
->rsi
;
3312 env
->regs
[R_EDI
] = regs
->rdi
;
3313 env
->regs
[R_EBP
] = regs
->rbp
;
3314 env
->regs
[R_ESP
] = regs
->rsp
;
3315 env
->eip
= regs
->rip
;
3317 env
->regs
[R_EAX
] = regs
->eax
;
3318 env
->regs
[R_EBX
] = regs
->ebx
;
3319 env
->regs
[R_ECX
] = regs
->ecx
;
3320 env
->regs
[R_EDX
] = regs
->edx
;
3321 env
->regs
[R_ESI
] = regs
->esi
;
3322 env
->regs
[R_EDI
] = regs
->edi
;
3323 env
->regs
[R_EBP
] = regs
->ebp
;
3324 env
->regs
[R_ESP
] = regs
->esp
;
3325 env
->eip
= regs
->eip
;
3328 /* linux interrupt setup */
3329 #ifndef TARGET_ABI32
3330 env
->idt
.limit
= 511;
3332 env
->idt
.limit
= 255;
3334 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
3335 PROT_READ
|PROT_WRITE
,
3336 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
3337 idt_table
= g2h(env
->idt
.base
);
3360 /* linux segment setup */
3362 uint64_t *gdt_table
;
3363 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
3364 PROT_READ
|PROT_WRITE
,
3365 MAP_ANONYMOUS
|MAP_PRIVATE
, -1, 0);
3366 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
3367 gdt_table
= g2h(env
->gdt
.base
);
3369 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
3370 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
3371 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
3373 /* 64 bit code segment */
3374 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
3375 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
3377 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
3379 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
3380 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
3381 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
3383 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
3384 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
3386 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
3387 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
3388 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
3389 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
3390 /* This hack makes Wine work... */
3391 env
->segs
[R_FS
].selector
= 0;
3393 cpu_x86_load_seg(env
, R_DS
, 0);
3394 cpu_x86_load_seg(env
, R_ES
, 0);
3395 cpu_x86_load_seg(env
, R_FS
, 0);
3396 cpu_x86_load_seg(env
, R_GS
, 0);
3398 #elif defined(TARGET_ARM)
3401 cpsr_write(env
, regs
->uregs
[16], 0xffffffff);
3402 for(i
= 0; i
< 16; i
++) {
3403 env
->regs
[i
] = regs
->uregs
[i
];
3406 #elif defined(TARGET_UNICORE32)
3409 cpu_asr_write(env
, regs
->uregs
[32], 0xffffffff);
3410 for (i
= 0; i
< 32; i
++) {
3411 env
->regs
[i
] = regs
->uregs
[i
];
3414 #elif defined(TARGET_SPARC)
3418 env
->npc
= regs
->npc
;
3420 for(i
= 0; i
< 8; i
++)
3421 env
->gregs
[i
] = regs
->u_regs
[i
];
3422 for(i
= 0; i
< 8; i
++)
3423 env
->regwptr
[i
] = regs
->u_regs
[i
+ 8];
3425 #elif defined(TARGET_PPC)
3429 #if defined(TARGET_PPC64)
3430 #if defined(TARGET_ABI32)
3431 env
->msr
&= ~((target_ulong
)1 << MSR_SF
);
3433 env
->msr
|= (target_ulong
)1 << MSR_SF
;
3436 env
->nip
= regs
->nip
;
3437 for(i
= 0; i
< 32; i
++) {
3438 env
->gpr
[i
] = regs
->gpr
[i
];
3441 #elif defined(TARGET_M68K)
3444 env
->dregs
[0] = regs
->d0
;
3445 env
->dregs
[1] = regs
->d1
;
3446 env
->dregs
[2] = regs
->d2
;
3447 env
->dregs
[3] = regs
->d3
;
3448 env
->dregs
[4] = regs
->d4
;
3449 env
->dregs
[5] = regs
->d5
;
3450 env
->dregs
[6] = regs
->d6
;
3451 env
->dregs
[7] = regs
->d7
;
3452 env
->aregs
[0] = regs
->a0
;
3453 env
->aregs
[1] = regs
->a1
;
3454 env
->aregs
[2] = regs
->a2
;
3455 env
->aregs
[3] = regs
->a3
;
3456 env
->aregs
[4] = regs
->a4
;
3457 env
->aregs
[5] = regs
->a5
;
3458 env
->aregs
[6] = regs
->a6
;
3459 env
->aregs
[7] = regs
->usp
;
3461 ts
->sim_syscalls
= 1;
3463 #elif defined(TARGET_MICROBLAZE)
3465 env
->regs
[0] = regs
->r0
;
3466 env
->regs
[1] = regs
->r1
;
3467 env
->regs
[2] = regs
->r2
;
3468 env
->regs
[3] = regs
->r3
;
3469 env
->regs
[4] = regs
->r4
;
3470 env
->regs
[5] = regs
->r5
;
3471 env
->regs
[6] = regs
->r6
;
3472 env
->regs
[7] = regs
->r7
;
3473 env
->regs
[8] = regs
->r8
;
3474 env
->regs
[9] = regs
->r9
;
3475 env
->regs
[10] = regs
->r10
;
3476 env
->regs
[11] = regs
->r11
;
3477 env
->regs
[12] = regs
->r12
;
3478 env
->regs
[13] = regs
->r13
;
3479 env
->regs
[14] = regs
->r14
;
3480 env
->regs
[15] = regs
->r15
;
3481 env
->regs
[16] = regs
->r16
;
3482 env
->regs
[17] = regs
->r17
;
3483 env
->regs
[18] = regs
->r18
;
3484 env
->regs
[19] = regs
->r19
;
3485 env
->regs
[20] = regs
->r20
;
3486 env
->regs
[21] = regs
->r21
;
3487 env
->regs
[22] = regs
->r22
;
3488 env
->regs
[23] = regs
->r23
;
3489 env
->regs
[24] = regs
->r24
;
3490 env
->regs
[25] = regs
->r25
;
3491 env
->regs
[26] = regs
->r26
;
3492 env
->regs
[27] = regs
->r27
;
3493 env
->regs
[28] = regs
->r28
;
3494 env
->regs
[29] = regs
->r29
;
3495 env
->regs
[30] = regs
->r30
;
3496 env
->regs
[31] = regs
->r31
;
3497 env
->sregs
[SR_PC
] = regs
->pc
;
3499 #elif defined(TARGET_MIPS)
3503 for(i
= 0; i
< 32; i
++) {
3504 env
->active_tc
.gpr
[i
] = regs
->regs
[i
];
3506 env
->active_tc
.PC
= regs
->cp0_epc
& ~(target_ulong
)1;
3507 if (regs
->cp0_epc
& 1) {
3508 env
->hflags
|= MIPS_HFLAG_M16
;
3511 #elif defined(TARGET_SH4)
3515 for(i
= 0; i
< 16; i
++) {
3516 env
->gregs
[i
] = regs
->regs
[i
];
3520 #elif defined(TARGET_ALPHA)
3524 for(i
= 0; i
< 28; i
++) {
3525 env
->ir
[i
] = ((abi_ulong
*)regs
)[i
];
3527 env
->ir
[IR_SP
] = regs
->usp
;
3530 #elif defined(TARGET_CRIS)
3532 env
->regs
[0] = regs
->r0
;
3533 env
->regs
[1] = regs
->r1
;
3534 env
->regs
[2] = regs
->r2
;
3535 env
->regs
[3] = regs
->r3
;
3536 env
->regs
[4] = regs
->r4
;
3537 env
->regs
[5] = regs
->r5
;
3538 env
->regs
[6] = regs
->r6
;
3539 env
->regs
[7] = regs
->r7
;
3540 env
->regs
[8] = regs
->r8
;
3541 env
->regs
[9] = regs
->r9
;
3542 env
->regs
[10] = regs
->r10
;
3543 env
->regs
[11] = regs
->r11
;
3544 env
->regs
[12] = regs
->r12
;
3545 env
->regs
[13] = regs
->r13
;
3546 env
->regs
[14] = info
->start_stack
;
3547 env
->regs
[15] = regs
->acr
;
3548 env
->pc
= regs
->erp
;
3550 #elif defined(TARGET_S390X)
3553 for (i
= 0; i
< 16; i
++) {
3554 env
->regs
[i
] = regs
->gprs
[i
];
3556 env
->psw
.mask
= regs
->psw
.mask
;
3557 env
->psw
.addr
= regs
->psw
.addr
;
3560 #error unsupported target CPU
3563 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
3564 ts
->stack_base
= info
->start_stack
;
3565 ts
->heap_base
= info
->brk
;
3566 /* This will be filled in on the first SYS_HEAPINFO call. */
3571 gdbserver_start (gdbstub_port
);
3572 gdb_handlesig(env
, 0);