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"
|
2019-03-13 05:22:20 +03:00
|
|
|
#include "qemu/guest-random.h"
|
2021-03-05 16:54:49 +03:00
|
|
|
#include "semihosting/common-semi.h"
|
2021-02-12 21:48:57 +03:00
|
|
|
#include "target/arm/syndrome.h"
|
2023-10-24 19:35:05 +03:00
|
|
|
#include "target/arm/cpu-features.h"
|
2018-04-11 21:56:33 +03:00
|
|
|
|
2018-04-11 21:56:35 +03:00
|
|
|
#define get_user_code_u32(x, gaddr, env) \
|
|
|
|
({ abi_long __r = get_user_u32((x), (gaddr)); \
|
|
|
|
if (!__r && bswap_code(arm_sctlr_b(env))) { \
|
|
|
|
(x) = bswap32(x); \
|
|
|
|
} \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define get_user_code_u16(x, gaddr, env) \
|
|
|
|
({ abi_long __r = get_user_u16((x), (gaddr)); \
|
|
|
|
if (!__r && bswap_code(arm_sctlr_b(env))) { \
|
|
|
|
(x) = bswap16(x); \
|
|
|
|
} \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define get_user_data_u32(x, gaddr, env) \
|
|
|
|
({ abi_long __r = get_user_u32((x), (gaddr)); \
|
|
|
|
if (!__r && arm_cpu_bswap_data(env)) { \
|
|
|
|
(x) = bswap32(x); \
|
|
|
|
} \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define get_user_data_u16(x, gaddr, env) \
|
|
|
|
({ abi_long __r = get_user_u16((x), (gaddr)); \
|
|
|
|
if (!__r && arm_cpu_bswap_data(env)) { \
|
|
|
|
(x) = bswap16(x); \
|
|
|
|
} \
|
|
|
|
__r; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define put_user_data_u32(x, gaddr, env) \
|
|
|
|
({ typeof(x) __x = (x); \
|
|
|
|
if (arm_cpu_bswap_data(env)) { \
|
|
|
|
__x = bswap32(__x); \
|
|
|
|
} \
|
|
|
|
put_user_u32(__x, (gaddr)); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define put_user_data_u16(x, gaddr, env) \
|
|
|
|
({ typeof(x) __x = (x); \
|
|
|
|
if (arm_cpu_bswap_data(env)) { \
|
|
|
|
__x = bswap16(__x); \
|
|
|
|
} \
|
|
|
|
put_user_u16(__x, (gaddr)); \
|
|
|
|
})
|
|
|
|
|
|
|
|
/* AArch64 main loop */
|
|
|
|
void cpu_loop(CPUARMState *env)
|
|
|
|
{
|
2019-03-23 03:41:14 +03:00
|
|
|
CPUState *cs = env_cpu(env);
|
2021-07-24 01:22:54 +03:00
|
|
|
int trapnr, ec, fsc, si_code, si_signo;
|
2018-04-11 21:56:35 +03:00
|
|
|
abi_long ret;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cpu_exec_start(cs);
|
|
|
|
trapnr = cpu_exec(cs);
|
|
|
|
cpu_exec_end(cs);
|
|
|
|
process_queued_cpu_work(cs);
|
|
|
|
|
|
|
|
switch (trapnr) {
|
|
|
|
case EXCP_SWI:
|
2023-01-12 13:24:35 +03:00
|
|
|
/* On syscall, PSTATE.ZA is preserved, PSTATE.SM is cleared. */
|
2023-01-12 13:24:32 +03:00
|
|
|
aarch64_set_svcr(env, 0, R_SVCR_SM_MASK);
|
2018-04-11 21:56:35 +03:00
|
|
|
ret = do_syscall(env,
|
|
|
|
env->xregs[8],
|
|
|
|
env->xregs[0],
|
|
|
|
env->xregs[1],
|
|
|
|
env->xregs[2],
|
|
|
|
env->xregs[3],
|
|
|
|
env->xregs[4],
|
|
|
|
env->xregs[5],
|
|
|
|
0, 0);
|
2021-11-22 21:47:33 +03:00
|
|
|
if (ret == -QEMU_ERESTARTSYS) {
|
2018-04-11 21:56:35 +03:00
|
|
|
env->pc -= 4;
|
2021-11-17 16:14:52 +03:00
|
|
|
} else if (ret != -QEMU_ESIGRETURN) {
|
2018-04-11 21:56:35 +03:00
|
|
|
env->xregs[0] = ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EXCP_INTERRUPT:
|
|
|
|
/* just indicate that signals should be handled asap */
|
|
|
|
break;
|
|
|
|
case EXCP_UDEF:
|
2021-08-13 16:18:09 +03:00
|
|
|
force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLOPN, env->pc);
|
2018-04-11 21:56:35 +03:00
|
|
|
break;
|
|
|
|
case EXCP_PREFETCH_ABORT:
|
|
|
|
case EXCP_DATA_ABORT:
|
2021-02-12 21:48:57 +03:00
|
|
|
ec = syn_get_ec(env->exception.syndrome);
|
2021-11-03 07:03:49 +03:00
|
|
|
switch (ec) {
|
|
|
|
case EC_DATAABORT:
|
|
|
|
case EC_INSNABORT:
|
|
|
|
/* Both EC have the same format for FSC, or close enough. */
|
|
|
|
fsc = extract32(env->exception.syndrome, 0, 6);
|
|
|
|
switch (fsc) {
|
|
|
|
case 0x04 ... 0x07: /* Translation fault, level {0-3} */
|
|
|
|
si_signo = TARGET_SIGSEGV;
|
|
|
|
si_code = TARGET_SEGV_MAPERR;
|
|
|
|
break;
|
|
|
|
case 0x09 ... 0x0b: /* Access flag fault, level {1-3} */
|
|
|
|
case 0x0d ... 0x0f: /* Permission fault, level {1-3} */
|
|
|
|
si_signo = TARGET_SIGSEGV;
|
|
|
|
si_code = TARGET_SEGV_ACCERR;
|
|
|
|
break;
|
|
|
|
case 0x11: /* Synchronous Tag Check Fault */
|
|
|
|
si_signo = TARGET_SIGSEGV;
|
|
|
|
si_code = TARGET_SEGV_MTESERR;
|
|
|
|
break;
|
|
|
|
case 0x21: /* Alignment fault */
|
|
|
|
si_signo = TARGET_SIGBUS;
|
|
|
|
si_code = TARGET_BUS_ADRALN;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
2021-02-12 21:48:58 +03:00
|
|
|
break;
|
2021-11-03 07:03:49 +03:00
|
|
|
case EC_PCALIGNMENT:
|
2021-07-24 01:22:54 +03:00
|
|
|
si_signo = TARGET_SIGBUS;
|
|
|
|
si_code = TARGET_BUS_ADRALN;
|
|
|
|
break;
|
2021-02-12 21:48:57 +03:00
|
|
|
default:
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
2021-07-24 01:22:54 +03:00
|
|
|
force_sig_fault(si_signo, si_code, env->exception.vaddress);
|
2018-04-11 21:56:35 +03:00
|
|
|
break;
|
|
|
|
case EXCP_DEBUG:
|
|
|
|
case EXCP_BKPT:
|
2021-08-13 16:18:09 +03:00
|
|
|
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
|
2018-04-11 21:56:35 +03:00
|
|
|
break;
|
|
|
|
case EXCP_SEMIHOST:
|
2022-04-28 11:10:55 +03:00
|
|
|
do_common_semihosting(cs);
|
2019-12-17 18:08:57 +03:00
|
|
|
env->pc += 4;
|
2018-04-11 21:56:35 +03:00
|
|
|
break;
|
|
|
|
case EXCP_YIELD:
|
|
|
|
/* nothing to do here for user-mode, just resume guest code */
|
|
|
|
break;
|
|
|
|
case EXCP_ATOMIC:
|
|
|
|
cpu_exec_step_atomic(cs);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
EXCP_DUMP(env, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr);
|
|
|
|
abort();
|
|
|
|
}
|
2021-02-12 21:48:59 +03:00
|
|
|
|
|
|
|
/* Check for MTE asynchronous faults */
|
|
|
|
if (unlikely(env->cp15.tfsr_el[0])) {
|
|
|
|
env->cp15.tfsr_el[0] = 0;
|
2021-08-13 16:18:09 +03:00
|
|
|
force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MTEAERR, 0);
|
2021-02-12 21:48:59 +03:00
|
|
|
}
|
|
|
|
|
2018-04-11 21:56:35 +03:00
|
|
|
process_pending_signals(env);
|
|
|
|
/* Exception return on AArch64 always clears the exclusive monitor,
|
|
|
|
* so any return to running guest code implies this.
|
|
|
|
*/
|
|
|
|
env->exclusive_addr = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 21:56:33 +03:00
|
|
|
void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
|
|
|
|
{
|
2019-03-23 03:41:14 +03:00
|
|
|
ARMCPU *cpu = env_archcpu(env);
|
|
|
|
CPUState *cs = env_cpu(env);
|
2019-01-26 01:57:12 +03:00
|
|
|
TaskState *ts = cs->opaque;
|
2018-04-11 21:56:35 +03:00
|
|
|
struct image_info *info = ts->info;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(arm_feature(env, ARM_FEATURE_AARCH64))) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"The selected ARM CPU does not support 64 bit mode\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 31; i++) {
|
|
|
|
env->xregs[i] = regs->regs[i];
|
|
|
|
}
|
|
|
|
env->pc = regs->pc;
|
|
|
|
env->xregs[31] = regs->sp;
|
2022-03-23 18:57:18 +03:00
|
|
|
#if TARGET_BIG_ENDIAN
|
2018-04-11 21:56:35 +03:00
|
|
|
env->cp15.sctlr_el[1] |= SCTLR_E0E;
|
|
|
|
for (i = 1; i < 4; ++i) {
|
|
|
|
env->cp15.sctlr_el[i] |= SCTLR_EE;
|
|
|
|
}
|
2019-10-23 18:00:55 +03:00
|
|
|
arm_rebuild_hflags(env);
|
2018-04-11 21:56:35 +03:00
|
|
|
#endif
|
|
|
|
|
2019-01-26 01:57:12 +03:00
|
|
|
if (cpu_isar_feature(aa64_pauth, cpu)) {
|
2019-03-15 03:28:32 +03:00
|
|
|
qemu_guest_getrandom_nofail(&env->keys, sizeof(env->keys));
|
2019-01-26 01:57:12 +03:00
|
|
|
}
|
|
|
|
|
2018-04-11 21:56:35 +03:00
|
|
|
ts->stack_base = info->start_stack;
|
|
|
|
ts->heap_base = info->brk;
|
|
|
|
/* This will be filled in on the first SYS_HEAPINFO call. */
|
|
|
|
ts->heap_limit = 0;
|
2018-04-11 21:56:33 +03:00
|
|
|
}
|