2018-04-11 21:56:33 +03: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 18:44:03 +03:00
|
|
|
#include "user-internals.h"
|
2018-04-11 21:56:33 +03:00
|
|
|
#include "cpu_loop-common.h"
|
2021-09-08 18:43:59 +03:00
|
|
|
#include "signal-common.h"
|
2018-04-11 21:56:33 +03:00
|
|
|
|
2018-04-11 21:56:40 +03:00
|
|
|
void cpu_loop(CPUNios2State *env)
|
|
|
|
{
|
2019-03-23 02:07:18 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2018-04-11 21:56:40 +03:00
|
|
|
target_siginfo_t info;
|
2018-10-19 20:49:58 +03:00
|
|
|
int trapnr, ret;
|
2018-04-11 21:56:40 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cpu_exec_start(cs);
|
|
|
|
trapnr = cpu_exec(cs);
|
|
|
|
cpu_exec_end(cs);
|
|
|
|
|
|
|
|
switch (trapnr) {
|
|
|
|
case EXCP_INTERRUPT:
|
|
|
|
/* just indicate that signals should be handled asap */
|
|
|
|
break;
|
2021-12-21 05:50:06 +03:00
|
|
|
|
2018-04-11 21:56:40 +03:00
|
|
|
case EXCP_TRAP:
|
2021-12-21 05:50:06 +03:00
|
|
|
switch (env->error_code) {
|
|
|
|
case 0:
|
2018-04-11 21:56:40 +03:00
|
|
|
qemu_log_mask(CPU_LOG_INT, "\nSyscall\n");
|
|
|
|
|
|
|
|
ret = do_syscall(env, env->regs[2],
|
|
|
|
env->regs[4], env->regs[5], env->regs[6],
|
|
|
|
env->regs[7], env->regs[8], env->regs[9],
|
|
|
|
0, 0);
|
|
|
|
|
|
|
|
if (env->regs[2] == 0) { /* FIXME: syscall 0 workaround */
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
env->regs[2] = abs(ret);
|
|
|
|
/* Return value is 0..4096 */
|
2021-12-21 05:50:06 +03:00
|
|
|
env->regs[7] = ret > 0xfffff000u;
|
2018-04-11 21:56:40 +03:00
|
|
|
env->regs[R_PC] += 4;
|
|
|
|
break;
|
|
|
|
|
2021-12-21 05:50:06 +03:00
|
|
|
case 1:
|
|
|
|
qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n");
|
|
|
|
force_sig_fault(TARGET_SIGUSR1, 0, env->regs[R_PC]);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n");
|
|
|
|
force_sig_fault(TARGET_SIGUSR2, 0, env->regs[R_PC]);
|
|
|
|
break;
|
|
|
|
case 31:
|
|
|
|
qemu_log_mask(CPU_LOG_INT, "\nTrap 31\n");
|
|
|
|
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->regs[R_PC]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->error_code);
|
|
|
|
force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP,
|
|
|
|
env->regs[R_PC]);
|
2018-04-11 21:56:40 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-12-21 05:50:06 +03:00
|
|
|
break;
|
|
|
|
|
2019-02-13 19:50:26 +03:00
|
|
|
case EXCP_DEBUG:
|
|
|
|
info.si_signo = TARGET_SIGTRAP;
|
|
|
|
info.si_errno = 0;
|
|
|
|
info.si_code = TARGET_TRAP_BRKPT;
|
|
|
|
queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
|
|
|
|
break;
|
2018-04-11 21:56:40 +03:00
|
|
|
case 0xaa:
|
|
|
|
switch (env->regs[R_PC]) {
|
|
|
|
/*case 0x1000:*/ /* TODO:__kuser_helper_version */
|
|
|
|
case 0x1004: /* __kuser_cmpxchg */
|
|
|
|
start_exclusive();
|
|
|
|
if (env->regs[4] & 0x3) {
|
|
|
|
goto kuser_fail;
|
|
|
|
}
|
|
|
|
ret = get_user_u32(env->regs[2], env->regs[4]);
|
|
|
|
if (ret) {
|
|
|
|
end_exclusive();
|
|
|
|
goto kuser_fail;
|
|
|
|
}
|
|
|
|
env->regs[2] -= env->regs[5];
|
|
|
|
if (env->regs[2] == 0) {
|
|
|
|
put_user_u32(env->regs[6], env->regs[4]);
|
|
|
|
}
|
|
|
|
end_exclusive();
|
|
|
|
env->regs[R_PC] = env->regs[R_RA];
|
|
|
|
break;
|
|
|
|
/*case 0x1040:*/ /* TODO:__kuser_sigtramp */
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
kuser_fail:
|
|
|
|
info.si_signo = TARGET_SIGSEGV;
|
|
|
|
info.si_errno = 0;
|
|
|
|
/* TODO: check env->error_code */
|
|
|
|
info.si_code = TARGET_SEGV_MAPERR;
|
|
|
|
info._sifields._sigfault._addr = env->regs[R_PC];
|
|
|
|
queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
|
|
|
|
trapnr);
|
2018-10-19 20:49:58 +03:00
|
|
|
abort();
|
2018-04-11 21:56:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
process_pending_signals(env);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:56:33 +03:00
|
|
|
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
|
|
|
|
{
|
2018-04-11 21:56:40 +03:00
|
|
|
env->regs[0] = 0;
|
|
|
|
env->regs[1] = regs->r1;
|
|
|
|
env->regs[2] = regs->r2;
|
|
|
|
env->regs[3] = regs->r3;
|
|
|
|
env->regs[4] = regs->r4;
|
|
|
|
env->regs[5] = regs->r5;
|
|
|
|
env->regs[6] = regs->r6;
|
|
|
|
env->regs[7] = regs->r7;
|
|
|
|
env->regs[8] = regs->r8;
|
|
|
|
env->regs[9] = regs->r9;
|
|
|
|
env->regs[10] = regs->r10;
|
|
|
|
env->regs[11] = regs->r11;
|
|
|
|
env->regs[12] = regs->r12;
|
|
|
|
env->regs[13] = regs->r13;
|
|
|
|
env->regs[14] = regs->r14;
|
|
|
|
env->regs[15] = regs->r15;
|
|
|
|
/* TODO: unsigned long orig_r2; */
|
|
|
|
env->regs[R_RA] = regs->ra;
|
|
|
|
env->regs[R_FP] = regs->fp;
|
|
|
|
env->regs[R_SP] = regs->sp;
|
|
|
|
env->regs[R_GP] = regs->gp;
|
|
|
|
env->regs[CR_ESTATUS] = regs->estatus;
|
|
|
|
env->regs[R_EA] = regs->ea;
|
|
|
|
/* TODO: unsigned long orig_r7; */
|
|
|
|
|
|
|
|
/* Emulate eret when starting thread. */
|
|
|
|
env->regs[R_PC] = regs->ea;
|
2018-04-11 21:56:33 +03:00
|
|
|
}
|