2018-04-11 20:56:33 +02:00
|
|
|
/*
|
|
|
|
* qemu user cpu loop
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu.h"
|
2021-09-08 17:44:03 +02:00
|
|
|
#include "user-internals.h"
|
2018-04-11 20:56:33 +02:00
|
|
|
#include "cpu_loop-common.h"
|
2021-09-08 17:43:59 +02:00
|
|
|
#include "signal-common.h"
|
2018-04-11 20:56:39 +02:00
|
|
|
#include "elf.h"
|
2019-08-08 18:27:31 +02:00
|
|
|
#include "internal.h"
|
2020-11-14 19:03:11 +01:00
|
|
|
#include "fpu_helper.h"
|
2018-04-11 20:56:39 +02:00
|
|
|
|
|
|
|
# ifdef TARGET_ABI_MIPSO32
|
2020-02-13 13:29:10 +01:00
|
|
|
# define MIPS_SYSCALL_NUMBER_UNUSED -1
|
|
|
|
static const int8_t mips_syscall_args[] = {
|
2020-03-10 11:34:02 +01:00
|
|
|
#include "syscall-args-o32.c.inc"
|
2018-04-11 20:56:39 +02:00
|
|
|
};
|
|
|
|
# endif /* O32 */
|
|
|
|
|
|
|
|
/* Break codes */
|
|
|
|
enum {
|
|
|
|
BRK_OVERFLOW = 6,
|
|
|
|
BRK_DIVZERO = 7
|
|
|
|
};
|
|
|
|
|
2022-01-07 22:32:33 +01:00
|
|
|
static void do_tr_or_bp(CPUMIPSState *env, unsigned int code, bool trap)
|
2018-04-11 20:56:39 +02:00
|
|
|
{
|
2022-01-07 22:32:33 +01:00
|
|
|
target_ulong pc = env->active_tc.PC;
|
2018-04-11 20:56:39 +02:00
|
|
|
|
|
|
|
switch (code) {
|
|
|
|
case BRK_OVERFLOW:
|
2022-01-07 22:32:33 +01:00
|
|
|
force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, pc);
|
|
|
|
break;
|
2018-04-11 20:56:39 +02:00
|
|
|
case BRK_DIVZERO:
|
2022-01-07 22:32:33 +01:00
|
|
|
force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTDIV, pc);
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-01-07 22:32:33 +01:00
|
|
|
if (trap) {
|
|
|
|
force_sig(TARGET_SIGTRAP);
|
|
|
|
} else {
|
|
|
|
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, pc);
|
|
|
|
}
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cpu_loop(CPUMIPSState *env)
|
|
|
|
{
|
2019-03-23 02:38:42 +01:00
|
|
|
CPUState *cs = env_cpu(env);
|
2022-01-07 22:32:34 +01:00
|
|
|
int trapnr, si_code;
|
2022-01-07 22:32:35 +01:00
|
|
|
unsigned int code;
|
2018-04-11 20:56:39 +02:00
|
|
|
abi_long ret;
|
|
|
|
# ifdef TARGET_ABI_MIPSO32
|
|
|
|
unsigned int syscall_num;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
cpu_exec_start(cs);
|
|
|
|
trapnr = cpu_exec(cs);
|
|
|
|
cpu_exec_end(cs);
|
|
|
|
process_queued_cpu_work(cs);
|
|
|
|
|
|
|
|
switch(trapnr) {
|
|
|
|
case EXCP_SYSCALL:
|
|
|
|
env->active_tc.PC += 4;
|
|
|
|
# ifdef TARGET_ABI_MIPSO32
|
|
|
|
syscall_num = env->active_tc.gpr[2] - 4000;
|
|
|
|
if (syscall_num >= sizeof(mips_syscall_args)) {
|
2020-02-13 13:29:10 +01:00
|
|
|
/* syscall_num is larger that any defined for MIPS O32 */
|
|
|
|
ret = -TARGET_ENOSYS;
|
|
|
|
} else if (mips_syscall_args[syscall_num] ==
|
|
|
|
MIPS_SYSCALL_NUMBER_UNUSED) {
|
|
|
|
/* syscall_num belongs to the range not defined for MIPS O32 */
|
2018-04-11 20:56:39 +02:00
|
|
|
ret = -TARGET_ENOSYS;
|
|
|
|
} else {
|
2020-02-13 13:29:10 +01:00
|
|
|
/* syscall_num is valid */
|
2018-04-11 20:56:39 +02:00
|
|
|
int nb_args;
|
|
|
|
abi_ulong sp_reg;
|
|
|
|
abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
|
|
|
|
|
|
|
|
nb_args = mips_syscall_args[syscall_num];
|
|
|
|
sp_reg = env->active_tc.gpr[29];
|
|
|
|
switch (nb_args) {
|
|
|
|
/* these arguments are taken from the stack */
|
|
|
|
case 8:
|
|
|
|
if ((ret = get_user_ual(arg8, sp_reg + 28)) != 0) {
|
|
|
|
goto done_syscall;
|
|
|
|
}
|
2020-10-30 01:40:42 +01:00
|
|
|
/* fall through */
|
2018-04-11 20:56:39 +02:00
|
|
|
case 7:
|
|
|
|
if ((ret = get_user_ual(arg7, sp_reg + 24)) != 0) {
|
|
|
|
goto done_syscall;
|
|
|
|
}
|
2020-10-30 01:40:42 +01:00
|
|
|
/* fall through */
|
2018-04-11 20:56:39 +02:00
|
|
|
case 6:
|
|
|
|
if ((ret = get_user_ual(arg6, sp_reg + 20)) != 0) {
|
|
|
|
goto done_syscall;
|
|
|
|
}
|
2020-10-30 01:40:42 +01:00
|
|
|
/* fall through */
|
2018-04-11 20:56:39 +02:00
|
|
|
case 5:
|
|
|
|
if ((ret = get_user_ual(arg5, sp_reg + 16)) != 0) {
|
|
|
|
goto done_syscall;
|
|
|
|
}
|
2020-10-30 01:40:42 +01:00
|
|
|
/* fall through */
|
2018-04-11 20:56:39 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = do_syscall(env, env->active_tc.gpr[2],
|
|
|
|
env->active_tc.gpr[4],
|
|
|
|
env->active_tc.gpr[5],
|
|
|
|
env->active_tc.gpr[6],
|
|
|
|
env->active_tc.gpr[7],
|
|
|
|
arg5, arg6, arg7, arg8);
|
|
|
|
}
|
|
|
|
done_syscall:
|
|
|
|
# else
|
|
|
|
ret = do_syscall(env, env->active_tc.gpr[2],
|
|
|
|
env->active_tc.gpr[4], env->active_tc.gpr[5],
|
|
|
|
env->active_tc.gpr[6], env->active_tc.gpr[7],
|
|
|
|
env->active_tc.gpr[8], env->active_tc.gpr[9],
|
|
|
|
env->active_tc.gpr[10], env->active_tc.gpr[11]);
|
|
|
|
# endif /* O32 */
|
2021-11-22 19:47:33 +01:00
|
|
|
if (ret == -QEMU_ERESTARTSYS) {
|
2018-04-11 20:56:39 +02:00
|
|
|
env->active_tc.PC -= 4;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-17 14:14:52 +01:00
|
|
|
if (ret == -QEMU_ESIGRETURN) {
|
2018-04-11 20:56:39 +02:00
|
|
|
/* Returning from a successful sigreturn syscall.
|
|
|
|
Avoid clobbering register state. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ((abi_ulong)ret >= (abi_ulong)-1133) {
|
|
|
|
env->active_tc.gpr[7] = 1; /* error flag */
|
|
|
|
ret = -ret;
|
|
|
|
} else {
|
|
|
|
env->active_tc.gpr[7] = 0; /* error flag */
|
|
|
|
}
|
|
|
|
env->active_tc.gpr[2] = ret;
|
|
|
|
break;
|
|
|
|
case EXCP_CpU:
|
|
|
|
case EXCP_RI:
|
2022-01-07 22:32:34 +01:00
|
|
|
case EXCP_DSPDIS:
|
|
|
|
force_sig(TARGET_SIGILL);
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
|
|
|
case EXCP_INTERRUPT:
|
|
|
|
/* just indicate that signals should be handled asap */
|
|
|
|
break;
|
|
|
|
case EXCP_DEBUG:
|
2022-01-07 22:32:34 +01:00
|
|
|
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT,
|
|
|
|
env->active_tc.PC);
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
2019-06-28 12:43:38 +02:00
|
|
|
case EXCP_FPE:
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTUNK;
|
2019-06-28 12:43:38 +02:00
|
|
|
if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INVALID) {
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTINV;
|
2019-06-28 12:43:38 +02:00
|
|
|
} else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_DIV0) {
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTDIV;
|
2019-06-28 12:43:38 +02:00
|
|
|
} else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_OVERFLOW) {
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTOVF;
|
2019-06-28 12:43:38 +02:00
|
|
|
} else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_UNDERFLOW) {
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTUND;
|
2019-06-28 12:43:38 +02:00
|
|
|
} else if (GET_FP_CAUSE(env->active_fpu.fcr31) & FP_INEXACT) {
|
2022-01-07 22:32:34 +01:00
|
|
|
si_code = TARGET_FPE_FLTRES;
|
2019-06-28 12:43:38 +02:00
|
|
|
}
|
2022-01-07 22:32:34 +01:00
|
|
|
force_sig_fault(TARGET_SIGFPE, si_code, env->active_tc.PC);
|
2019-06-28 12:43:38 +02:00
|
|
|
break;
|
2023-09-28 21:55:20 +02:00
|
|
|
case EXCP_OVERFLOW:
|
|
|
|
force_sig_fault(TARGET_SIGFPE, TARGET_FPE_INTOVF, env->active_tc.PC);
|
|
|
|
break;
|
2018-04-11 20:56:39 +02:00
|
|
|
/* The code below was inspired by the MIPS Linux kernel trap
|
|
|
|
* handling code in arch/mips/kernel/traps.c.
|
|
|
|
*/
|
|
|
|
case EXCP_BREAK:
|
2022-01-07 22:32:35 +01:00
|
|
|
/*
|
|
|
|
* As described in the original Linux kernel code, the below
|
|
|
|
* checks on 'code' are to work around an old assembly bug.
|
|
|
|
*/
|
|
|
|
code = env->error_code;
|
|
|
|
if (code >= (1 << 10)) {
|
|
|
|
code >>= 10;
|
2018-04-11 20:56:39 +02:00
|
|
|
}
|
2022-01-07 22:32:35 +01:00
|
|
|
do_tr_or_bp(env, code, false);
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
|
|
|
case EXCP_TRAP:
|
2022-01-07 22:32:36 +01:00
|
|
|
do_tr_or_bp(env, env->error_code, true);
|
2018-04-11 20:56:39 +02:00
|
|
|
break;
|
|
|
|
case EXCP_ATOMIC:
|
|
|
|
cpu_exec_step_atomic(cs);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
process_pending_signals(env);
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 20:56:33 +02:00
|
|
|
|
|
|
|
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
|
|
|
|
{
|
2019-03-23 00:07:18 +01:00
|
|
|
CPUState *cpu = env_cpu(env);
|
2018-04-11 20:56:39 +02:00
|
|
|
TaskState *ts = cpu->opaque;
|
|
|
|
struct image_info *info = ts->info;
|
|
|
|
int i;
|
|
|
|
|
2018-10-23 11:44:36 +02:00
|
|
|
struct mode_req {
|
|
|
|
bool single;
|
|
|
|
bool soft;
|
|
|
|
bool fr1;
|
|
|
|
bool frdefault;
|
|
|
|
bool fre;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct mode_req fpu_reqs[] = {
|
|
|
|
[MIPS_ABI_FP_ANY] = { true, true, true, true, true },
|
|
|
|
[MIPS_ABI_FP_DOUBLE] = { false, false, false, true, true },
|
|
|
|
[MIPS_ABI_FP_SINGLE] = { true, false, false, false, false },
|
|
|
|
[MIPS_ABI_FP_SOFT] = { false, true, false, false, false },
|
|
|
|
[MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
|
|
|
|
[MIPS_ABI_FP_XX] = { false, false, true, true, true },
|
|
|
|
[MIPS_ABI_FP_64] = { false, false, true, false, false },
|
|
|
|
[MIPS_ABI_FP_64A] = { false, false, true, false, true }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mode requirements when .MIPS.abiflags is not present in the ELF.
|
|
|
|
* Not present means that everything is acceptable except FR1.
|
|
|
|
*/
|
|
|
|
static struct mode_req none_req = { true, true, false, true, true };
|
|
|
|
|
|
|
|
struct mode_req prog_req;
|
|
|
|
struct mode_req interp_req;
|
|
|
|
|
2018-04-11 20:56:39 +02:00
|
|
|
for(i = 0; i < 32; i++) {
|
|
|
|
env->active_tc.gpr[i] = regs->regs[i];
|
|
|
|
}
|
|
|
|
env->active_tc.PC = regs->cp0_epc & ~(target_ulong)1;
|
|
|
|
if (regs->cp0_epc & 1) {
|
|
|
|
env->hflags |= MIPS_HFLAG_M16;
|
|
|
|
}
|
2018-10-23 11:44:36 +02:00
|
|
|
|
|
|
|
#ifdef TARGET_ABI_MIPSO32
|
|
|
|
# define MAX_FP_ABI MIPS_ABI_FP_64A
|
|
|
|
#else
|
|
|
|
# define MAX_FP_ABI MIPS_ABI_FP_SOFT
|
|
|
|
#endif
|
|
|
|
if ((info->fp_abi > MAX_FP_ABI && info->fp_abi != MIPS_ABI_FP_UNKNOWN)
|
|
|
|
|| (info->interp_fp_abi > MAX_FP_ABI &&
|
|
|
|
info->interp_fp_abi != MIPS_ABI_FP_UNKNOWN)) {
|
|
|
|
fprintf(stderr, "qemu: Unexpected FPU mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
prog_req = (info->fp_abi == MIPS_ABI_FP_UNKNOWN) ? none_req
|
|
|
|
: fpu_reqs[info->fp_abi];
|
|
|
|
interp_req = (info->interp_fp_abi == MIPS_ABI_FP_UNKNOWN) ? none_req
|
|
|
|
: fpu_reqs[info->interp_fp_abi];
|
|
|
|
|
|
|
|
prog_req.single &= interp_req.single;
|
|
|
|
prog_req.soft &= interp_req.soft;
|
|
|
|
prog_req.fr1 &= interp_req.fr1;
|
|
|
|
prog_req.frdefault &= interp_req.frdefault;
|
|
|
|
prog_req.fre &= interp_req.fre;
|
|
|
|
|
2020-12-16 12:29:00 +01:00
|
|
|
bool cpu_has_mips_r2_r6 = env->insn_flags & ISA_MIPS_R2 ||
|
2020-12-16 12:34:42 +01:00
|
|
|
env->insn_flags & ISA_MIPS_R6;
|
2018-10-23 11:44:36 +02:00
|
|
|
|
|
|
|
if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1) {
|
|
|
|
env->CP0_Config5 |= (1 << CP0C5_FRE);
|
|
|
|
if (env->active_fpu.fcr0 & (1 << FCR0_FREP)) {
|
|
|
|
env->hflags |= MIPS_HFLAG_FRE;
|
|
|
|
}
|
|
|
|
} else if ((prog_req.fr1 && prog_req.frdefault) ||
|
|
|
|
(prog_req.single && !prog_req.frdefault)) {
|
|
|
|
if ((env->active_fpu.fcr0 & (1 << FCR0_F64)
|
|
|
|
&& cpu_has_mips_r2_r6) || prog_req.fr1) {
|
|
|
|
env->CP0_Status |= (1 << CP0St_FR);
|
|
|
|
env->hflags |= MIPS_HFLAG_F64;
|
|
|
|
}
|
2023-04-04 07:21:54 +02:00
|
|
|
} else if (prog_req.fr1) {
|
|
|
|
env->CP0_Status |= (1 << CP0St_FR);
|
|
|
|
env->hflags |= MIPS_HFLAG_F64;
|
|
|
|
} else if (!prog_req.fre && !prog_req.frdefault &&
|
2018-10-23 11:44:36 +02:00
|
|
|
!prog_req.fr1 && !prog_req.single && !prog_req.soft) {
|
|
|
|
fprintf(stderr, "qemu: Can't find a matching FPU mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-08-02 16:16:41 +02:00
|
|
|
if (env->insn_flags & ISA_NANOMIPS32) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-11 20:56:39 +02:00
|
|
|
if (((info->elf_flags & EF_MIPS_NAN2008) != 0) !=
|
|
|
|
((env->active_fpu.fcr31 & (1 << FCR31_NAN2008)) != 0)) {
|
|
|
|
if ((env->active_fpu.fcr31_rw_bitmask &
|
|
|
|
(1 << FCR31_NAN2008)) == 0) {
|
|
|
|
fprintf(stderr, "ELF binary's NaN mode not supported by CPU\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((info->elf_flags & EF_MIPS_NAN2008) != 0) {
|
|
|
|
env->active_fpu.fcr31 |= (1 << FCR31_NAN2008);
|
|
|
|
} else {
|
|
|
|
env->active_fpu.fcr31 &= ~(1 << FCR31_NAN2008);
|
|
|
|
}
|
|
|
|
restore_snan_bit_mode(env);
|
|
|
|
}
|
2018-04-11 20:56:33 +02:00
|
|
|
}
|