4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
23 #include "cpu_loop-common.h"
26 #include "fpu_helper.h"
28 # ifdef TARGET_ABI_MIPSO32
29 # define MIPS_SYSCALL_NUMBER_UNUSED -1
30 static const int8_t mips_syscall_args
[] = {
31 #include "syscall-args-o32.c.inc"
41 static int do_break(CPUMIPSState
*env
, target_siginfo_t
*info
,
49 info
->si_signo
= TARGET_SIGFPE
;
51 info
->si_code
= (code
== BRK_OVERFLOW
) ? FPE_INTOVF
: FPE_INTDIV
;
52 queue_signal(env
, info
->si_signo
, QEMU_SI_FAULT
, &*info
);
56 info
->si_signo
= TARGET_SIGTRAP
;
58 queue_signal(env
, info
->si_signo
, QEMU_SI_FAULT
, &*info
);
66 void cpu_loop(CPUMIPSState
*env
)
68 CPUState
*cs
= env_cpu(env
);
69 target_siginfo_t info
;
72 # ifdef TARGET_ABI_MIPSO32
73 unsigned int syscall_num
;
78 trapnr
= cpu_exec(cs
);
80 process_queued_cpu_work(cs
);
84 env
->active_tc
.PC
+= 4;
85 # ifdef TARGET_ABI_MIPSO32
86 syscall_num
= env
->active_tc
.gpr
[2] - 4000;
87 if (syscall_num
>= sizeof(mips_syscall_args
)) {
88 /* syscall_num is larger that any defined for MIPS O32 */
90 } else if (mips_syscall_args
[syscall_num
] ==
91 MIPS_SYSCALL_NUMBER_UNUSED
) {
92 /* syscall_num belongs to the range not defined for MIPS O32 */
95 /* syscall_num is valid */
98 abi_ulong arg5
= 0, arg6
= 0, arg7
= 0, arg8
= 0;
100 nb_args
= mips_syscall_args
[syscall_num
];
101 sp_reg
= env
->active_tc
.gpr
[29];
103 /* these arguments are taken from the stack */
105 if ((ret
= get_user_ual(arg8
, sp_reg
+ 28)) != 0) {
110 if ((ret
= get_user_ual(arg7
, sp_reg
+ 24)) != 0) {
115 if ((ret
= get_user_ual(arg6
, sp_reg
+ 20)) != 0) {
120 if ((ret
= get_user_ual(arg5
, sp_reg
+ 16)) != 0) {
127 ret
= do_syscall(env
, env
->active_tc
.gpr
[2],
128 env
->active_tc
.gpr
[4],
129 env
->active_tc
.gpr
[5],
130 env
->active_tc
.gpr
[6],
131 env
->active_tc
.gpr
[7],
132 arg5
, arg6
, arg7
, arg8
);
136 ret
= do_syscall(env
, env
->active_tc
.gpr
[2],
137 env
->active_tc
.gpr
[4], env
->active_tc
.gpr
[5],
138 env
->active_tc
.gpr
[6], env
->active_tc
.gpr
[7],
139 env
->active_tc
.gpr
[8], env
->active_tc
.gpr
[9],
140 env
->active_tc
.gpr
[10], env
->active_tc
.gpr
[11]);
142 if (ret
== -TARGET_ERESTARTSYS
) {
143 env
->active_tc
.PC
-= 4;
146 if (ret
== -TARGET_QEMU_ESIGRETURN
) {
147 /* Returning from a successful sigreturn syscall.
148 Avoid clobbering register state. */
151 if ((abi_ulong
)ret
>= (abi_ulong
)-1133) {
152 env
->active_tc
.gpr
[7] = 1; /* error flag */
155 env
->active_tc
.gpr
[7] = 0; /* error flag */
157 env
->active_tc
.gpr
[2] = ret
;
163 info
.si_signo
= TARGET_SIGSEGV
;
165 /* XXX: check env->error_code */
166 info
.si_code
= TARGET_SEGV_MAPERR
;
167 info
._sifields
._sigfault
._addr
= env
->CP0_BadVAddr
;
168 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
172 info
.si_signo
= TARGET_SIGILL
;
175 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
178 /* just indicate that signals should be handled asap */
181 info
.si_signo
= TARGET_SIGTRAP
;
183 info
.si_code
= TARGET_TRAP_BRKPT
;
184 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
187 info
.si_signo
= TARGET_SIGILL
;
189 info
.si_code
= TARGET_ILL_ILLOPC
;
190 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
193 info
.si_signo
= TARGET_SIGFPE
;
195 info
.si_code
= TARGET_FPE_FLTUNK
;
196 if (GET_FP_CAUSE(env
->active_fpu
.fcr31
) & FP_INVALID
) {
197 info
.si_code
= TARGET_FPE_FLTINV
;
198 } else if (GET_FP_CAUSE(env
->active_fpu
.fcr31
) & FP_DIV0
) {
199 info
.si_code
= TARGET_FPE_FLTDIV
;
200 } else if (GET_FP_CAUSE(env
->active_fpu
.fcr31
) & FP_OVERFLOW
) {
201 info
.si_code
= TARGET_FPE_FLTOVF
;
202 } else if (GET_FP_CAUSE(env
->active_fpu
.fcr31
) & FP_UNDERFLOW
) {
203 info
.si_code
= TARGET_FPE_FLTUND
;
204 } else if (GET_FP_CAUSE(env
->active_fpu
.fcr31
) & FP_INEXACT
) {
205 info
.si_code
= TARGET_FPE_FLTRES
;
207 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
209 /* The code below was inspired by the MIPS Linux kernel trap
210 * handling code in arch/mips/kernel/traps.c.
214 abi_ulong trap_instr
;
217 if (env
->hflags
& MIPS_HFLAG_M16
) {
218 if (env
->insn_flags
& ASE_MICROMIPS
) {
220 ret
= get_user_u16(trap_instr
, env
->active_tc
.PC
);
225 if ((trap_instr
>> 10) == 0x11) {
226 /* 16-bit instruction */
227 code
= trap_instr
& 0xf;
229 /* 32-bit instruction */
232 ret
= get_user_u16(instr_lo
,
233 env
->active_tc
.PC
+ 2);
237 trap_instr
= (trap_instr
<< 16) | instr_lo
;
238 code
= ((trap_instr
>> 6) & ((1 << 20) - 1));
239 /* Unfortunately, microMIPS also suffers from
240 the old assembler bug... */
241 if (code
>= (1 << 10)) {
247 ret
= get_user_u16(trap_instr
, env
->active_tc
.PC
);
251 code
= (trap_instr
>> 6) & 0x3f;
254 ret
= get_user_u32(trap_instr
, env
->active_tc
.PC
);
259 /* As described in the original Linux kernel code, the
260 * below checks on 'code' are to work around an old
263 code
= ((trap_instr
>> 6) & ((1 << 20) - 1));
264 if (code
>= (1 << 10)) {
269 if (do_break(env
, &info
, code
) != 0) {
276 abi_ulong trap_instr
;
277 unsigned int code
= 0;
279 if (env
->hflags
& MIPS_HFLAG_M16
) {
283 ret
= get_user_u16(instr
[0], env
->active_tc
.PC
) ||
284 get_user_u16(instr
[1], env
->active_tc
.PC
+ 2);
286 trap_instr
= (instr
[0] << 16) | instr
[1];
288 ret
= get_user_u32(trap_instr
, env
->active_tc
.PC
);
295 /* The immediate versions don't provide a code. */
296 if (!(trap_instr
& 0xFC000000)) {
297 if (env
->hflags
& MIPS_HFLAG_M16
) {
299 code
= ((trap_instr
>> 12) & ((1 << 4) - 1));
301 code
= ((trap_instr
>> 6) & ((1 << 10) - 1));
305 if (do_break(env
, &info
, code
) != 0) {
311 cpu_exec_step_atomic(cs
);
315 EXCP_DUMP(env
, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr
);
318 process_pending_signals(env
);
322 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
324 CPUState
*cpu
= env_cpu(env
);
325 TaskState
*ts
= cpu
->opaque
;
326 struct image_info
*info
= ts
->info
;
337 static const struct mode_req fpu_reqs
[] = {
338 [MIPS_ABI_FP_ANY
] = { true, true, true, true, true },
339 [MIPS_ABI_FP_DOUBLE
] = { false, false, false, true, true },
340 [MIPS_ABI_FP_SINGLE
] = { true, false, false, false, false },
341 [MIPS_ABI_FP_SOFT
] = { false, true, false, false, false },
342 [MIPS_ABI_FP_OLD_64
] = { false, false, false, false, false },
343 [MIPS_ABI_FP_XX
] = { false, false, true, true, true },
344 [MIPS_ABI_FP_64
] = { false, false, true, false, false },
345 [MIPS_ABI_FP_64A
] = { false, false, true, false, true }
349 * Mode requirements when .MIPS.abiflags is not present in the ELF.
350 * Not present means that everything is acceptable except FR1.
352 static struct mode_req none_req
= { true, true, false, true, true };
354 struct mode_req prog_req
;
355 struct mode_req interp_req
;
357 for(i
= 0; i
< 32; i
++) {
358 env
->active_tc
.gpr
[i
] = regs
->regs
[i
];
360 env
->active_tc
.PC
= regs
->cp0_epc
& ~(target_ulong
)1;
361 if (regs
->cp0_epc
& 1) {
362 env
->hflags
|= MIPS_HFLAG_M16
;
365 #ifdef TARGET_ABI_MIPSO32
366 # define MAX_FP_ABI MIPS_ABI_FP_64A
368 # define MAX_FP_ABI MIPS_ABI_FP_SOFT
370 if ((info
->fp_abi
> MAX_FP_ABI
&& info
->fp_abi
!= MIPS_ABI_FP_UNKNOWN
)
371 || (info
->interp_fp_abi
> MAX_FP_ABI
&&
372 info
->interp_fp_abi
!= MIPS_ABI_FP_UNKNOWN
)) {
373 fprintf(stderr
, "qemu: Unexpected FPU mode\n");
377 prog_req
= (info
->fp_abi
== MIPS_ABI_FP_UNKNOWN
) ? none_req
378 : fpu_reqs
[info
->fp_abi
];
379 interp_req
= (info
->interp_fp_abi
== MIPS_ABI_FP_UNKNOWN
) ? none_req
380 : fpu_reqs
[info
->interp_fp_abi
];
382 prog_req
.single
&= interp_req
.single
;
383 prog_req
.soft
&= interp_req
.soft
;
384 prog_req
.fr1
&= interp_req
.fr1
;
385 prog_req
.frdefault
&= interp_req
.frdefault
;
386 prog_req
.fre
&= interp_req
.fre
;
388 bool cpu_has_mips_r2_r6
= env
->insn_flags
& ISA_MIPS_R2
||
389 env
->insn_flags
& ISA_MIPS_R6
;
391 if (prog_req
.fre
&& !prog_req
.frdefault
&& !prog_req
.fr1
) {
392 env
->CP0_Config5
|= (1 << CP0C5_FRE
);
393 if (env
->active_fpu
.fcr0
& (1 << FCR0_FREP
)) {
394 env
->hflags
|= MIPS_HFLAG_FRE
;
396 } else if ((prog_req
.fr1
&& prog_req
.frdefault
) ||
397 (prog_req
.single
&& !prog_req
.frdefault
)) {
398 if ((env
->active_fpu
.fcr0
& (1 << FCR0_F64
)
399 && cpu_has_mips_r2_r6
) || prog_req
.fr1
) {
400 env
->CP0_Status
|= (1 << CP0St_FR
);
401 env
->hflags
|= MIPS_HFLAG_F64
;
403 } else if (!prog_req
.fre
&& !prog_req
.frdefault
&&
404 !prog_req
.fr1
&& !prog_req
.single
&& !prog_req
.soft
) {
405 fprintf(stderr
, "qemu: Can't find a matching FPU mode\n");
409 if (env
->insn_flags
& ISA_NANOMIPS32
) {
412 if (((info
->elf_flags
& EF_MIPS_NAN2008
) != 0) !=
413 ((env
->active_fpu
.fcr31
& (1 << FCR31_NAN2008
)) != 0)) {
414 if ((env
->active_fpu
.fcr31_rw_bitmask
&
415 (1 << FCR31_NAN2008
)) == 0) {
416 fprintf(stderr
, "ELF binary's NaN mode not supported by CPU\n");
419 if ((info
->elf_flags
& EF_MIPS_NAN2008
) != 0) {
420 env
->active_fpu
.fcr31
|= (1 << FCR31_NAN2008
);
422 env
->active_fpu
.fcr31
&= ~(1 << FCR31_NAN2008
);
424 restore_snan_bit_mode(env
);