target-arm: A64: Create Instruction Syndromes for Data Aborts

Add support for generating the ISS (Instruction Specific Syndrome) for
Data Abort exceptions taken from AArch64.
These syndromes are used by hypervisors for example to trap and emulate
memory accesses.

We save the decoded data out-of-band with the TBs at translation time.
When exceptions hit, the extra data attached to the TB is used to
recreate the state needed to encode instruction syndromes.
This avoids the need to emit moves with every load/store.

Based on a suggestion from Peter Maydell.

Backports commit aaa1f954d4cab243e3d5337a72bc6d104e1c4808 from qemu
This commit is contained in:
Edgar E. Iglesias 2018-02-24 16:38:49 -05:00 committed by Lioncash
parent 25daa5363e
commit 8aee797956
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
5 changed files with 201 additions and 52 deletions

View file

@ -95,7 +95,20 @@
#define ARM_CPU_VFIQ 3 #define ARM_CPU_VFIQ 3
#define NB_MMU_MODES 7 #define NB_MMU_MODES 7
#define TARGET_INSN_START_EXTRA_WORDS 1
/* ARM-specific extra insn start words:
* 1: Conditional execution bits
* 2: Partial exception syndrome for data aborts
*/
#define TARGET_INSN_START_EXTRA_WORDS 2
/* The 2nd extra word holding syndrome info for data aborts does not use
* the upper 6 bits nor the lower 14 bits. We mask and shift it down to
* help the sleb128 encoder do a better job.
* When restoring the CPU state, we shift it back up.
*/
#define ARM_INSN_START_WORD2_MASK ((1 << 26) - 1)
#define ARM_INSN_START_WORD2_SHIFT 14
/* We currently assume float and double are IEEE single and double /* We currently assume float and double are IEEE single and double
precision respectively. precision respectively.

View file

@ -76,6 +76,43 @@ uint32_t HELPER(neon_tbl)(CPUARMState *env, uint32_t ireg, uint32_t def,
#if !defined(CONFIG_USER_ONLY) #if !defined(CONFIG_USER_ONLY)
static inline uint32_t merge_syn_data_abort(uint32_t template_syn,
unsigned int target_el,
bool same_el,
bool s1ptw, int is_write,
int fsc)
{
uint32_t syn;
/* ISV is only set for data aborts routed to EL2 and
* never for stage-1 page table walks faulting on stage 2.
*
* Furthermore, ISV is only set for certain kinds of load/stores.
* If the template syndrome does not have ISV set, we should leave
* it cleared.
*
* See ARMv8 specs, D7-1974:
* ISS encoding for an exception from a Data Abort, the
* ISV field.
*/
if (!(template_syn & ARM_EL_ISV) || target_el != 2 || s1ptw) {
syn = syn_data_abort_no_iss(same_el,
0, 0, s1ptw, is_write == 1, fsc);
} else {
/* Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
* syndrome created at translation time.
* Now we create the runtime syndrome with the remaining fields.
*/
syn = syn_data_abort_with_iss(same_el,
0, 0, 0, 0, 0,
0, 0, s1ptw, is_write == 1, fsc,
false);
/* Merge the runtime syndrome with the template syndrome. */
syn |= template_syn;
}
return syn;
}
/* try to fill the TLB and return an exception if error. If retaddr is /* try to fill the TLB and return an exception if error. If retaddr is
* NULL, it means that the function was called in C code (i.e. not * NULL, it means that the function was called in C code (i.e. not
* from generated code or from helper.c) * from generated code or from helper.c)
@ -116,8 +153,8 @@ void tlb_fill(CPUState *cs, target_ulong addr, int is_write, int mmu_idx,
syn = syn_insn_abort(same_el, 0, fi.s1ptw, syn); syn = syn_insn_abort(same_el, 0, fi.s1ptw, syn);
exc = EXCP_PREFETCH_ABORT; exc = EXCP_PREFETCH_ABORT;
} else { } else {
syn = syn_data_abort_no_iss(same_el, syn = merge_syn_data_abort(env->exception.syndrome, target_el,
0, 0, fi.s1ptw, is_write == 1, syn); same_el, fi.s1ptw, is_write, syn);
if (is_write == 1 && arm_feature(env, ARM_FEATURE_V6)) { if (is_write == 1 && arm_feature(env, ARM_FEATURE_V6)) {
fsr |= (1 << 11); fsr |= (1 << 11);
} }
@ -138,6 +175,7 @@ void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, int is_write,
CPUARMState *env = &cpu->env; CPUARMState *env = &cpu->env;
int target_el; int target_el;
bool same_el; bool same_el;
uint32_t syn;
if (retaddr) { if (retaddr) {
/* now we have a real cpu fault */ /* now we have a real cpu fault */
@ -162,10 +200,9 @@ void arm_cpu_do_unaligned_access(CPUState *cs, vaddr vaddr, int is_write,
env->exception.fsr |= (1 << 11); env->exception.fsr |= (1 << 11);
} }
raise_exception(env, EXCP_DATA_ABORT, syn = merge_syn_data_abort(env->exception.syndrome, target_el,
syn_data_abort_no_iss(same_el, same_el, 0, is_write, 0x21);
0, 0, 0, is_write == 1, 0x21), raise_exception(env, EXCP_DATA_ABORT, syn, target_el);
target_el);
} }
#endif /* !defined(CONFIG_USER_ONLY) */ #endif /* !defined(CONFIG_USER_ONLY) */

View file

@ -324,6 +324,22 @@ static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
} }
} }
static void disas_set_insn_syndrome(DisasContext *s, uint32_t syn)
{
TCGContext *tcg_ctx = s->uc->tcg_ctx;
/* We don't need to save all of the syndrome so we mask and shift
* out uneeded bits to help the sleb128 encoder do a better job.
*/
syn &= ARM_INSN_START_WORD2_MASK;
syn >>= ARM_INSN_START_WORD2_SHIFT;
/* We check and clear insn_start_idx to catch multiple updates. */
assert(s->insn_start_idx != 0);
tcg_set_insn_param(tcg_ctx, s->insn_start_idx, 2, syn);
s->insn_start_idx = 0;
}
static void unallocated_encoding(DisasContext *s) static void unallocated_encoding(DisasContext *s)
{ {
/* Unallocated and reserved encodings are uncategorized */ /* Unallocated and reserved encodings are uncategorized */
@ -748,23 +764,47 @@ static void gen_adc_CC(DisasContext *s, int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv
* Store from GPR register to memory. * Store from GPR register to memory.
*/ */
static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source, static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
TCGv_i64 tcg_addr, int size, int memidx) TCGv_i64 tcg_addr, int size, int memidx,
bool iss_valid,
unsigned int iss_srt,
bool iss_sf, bool iss_ar)
{ {
g_assert(size <= 3); g_assert(size <= 3);
tcg_gen_qemu_st_i64(s->uc, source, tcg_addr, memidx, s->be_data + size); tcg_gen_qemu_st_i64(s->uc, source, tcg_addr, memidx, s->be_data + size);
if (iss_valid) {
uint32_t syn;
syn = syn_data_abort_with_iss(0,
size,
false,
iss_srt,
iss_sf,
iss_ar,
0, 0, 0, 0, 0, false);
disas_set_insn_syndrome(s, syn);
}
} }
static void do_gpr_st(DisasContext *s, TCGv_i64 source, static void do_gpr_st(DisasContext *s, TCGv_i64 source,
TCGv_i64 tcg_addr, int size) TCGv_i64 tcg_addr, int size,
bool iss_valid,
unsigned int iss_srt,
bool iss_sf, bool iss_ar)
{ {
do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s)); do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
iss_valid, iss_srt, iss_sf, iss_ar);
} }
/* /*
* Load from memory to GPR register * Load from memory to GPR register
*/ */
static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr, static void do_gpr_ld_memidx(DisasContext *s,
int size, bool is_signed, bool extend, int memidx) TCGv_i64 dest, TCGv_i64 tcg_addr,
int size, bool is_signed,
bool extend, int memidx,
bool iss_valid, unsigned int iss_srt,
bool iss_sf, bool iss_ar)
{ {
TCGContext *tcg_ctx = s->uc->tcg_ctx; TCGContext *tcg_ctx = s->uc->tcg_ctx;
TCGMemOp memop = s->be_data + size; TCGMemOp memop = s->be_data + size;
@ -781,13 +821,30 @@ static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
g_assert(size < 3); g_assert(size < 3);
tcg_gen_ext32u_i64(tcg_ctx, dest, dest); tcg_gen_ext32u_i64(tcg_ctx, dest, dest);
} }
if (iss_valid) {
uint32_t syn;
syn = syn_data_abort_with_iss(0,
size,
is_signed,
iss_srt,
iss_sf,
iss_ar,
0, 0, 0, 0, 0, false);
disas_set_insn_syndrome(s, syn);
}
} }
static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr, static void do_gpr_ld(DisasContext *s,
int size, bool is_signed, bool extend) TCGv_i64 dest, TCGv_i64 tcg_addr,
int size, bool is_signed, bool extend,
bool iss_valid, unsigned int iss_srt,
bool iss_sf, bool iss_ar)
{ {
do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend, do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
get_mem_index(s)); get_mem_index(s),
iss_valid, iss_srt, iss_sf, iss_ar);
} }
/* /*
@ -1854,6 +1911,22 @@ static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
} }
#endif #endif
/* Update the Sixty-Four bit (SF) registersize. This logic is derived
* from the ARMv8 specs for LDR (Shared decode for all encodings).
*/
static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
{
int opc0 = extract32(opc, 0, 1);
int regsize;
if (is_signed) {
regsize = opc0 ? 32 : 64;
} else {
regsize = size == 3 ? 64 : 32;
}
return regsize == 64;
}
/* C3.3.6 Load/store exclusive /* C3.3.6 Load/store exclusive
* *
* 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
@ -1905,10 +1978,15 @@ static void disas_ldst_excl(DisasContext *s, uint32_t insn)
} }
} else { } else {
TCGv_i64 tcg_rt = cpu_reg(s, rt); TCGv_i64 tcg_rt = cpu_reg(s, rt);
bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
/* Generate ISS for non-exclusive accesses including LASR. */
if (is_store) { if (is_store) {
do_gpr_st(s, tcg_rt, tcg_addr, size); do_gpr_st(s, tcg_rt, tcg_addr, size,
true, rt, iss_sf, is_lasr);
} else { } else {
do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false); do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
true, rt, iss_sf, is_lasr);
} }
} }
} }
@ -1961,7 +2039,11 @@ static void disas_ld_lit(DisasContext *s, uint32_t insn)
if (is_vector) { if (is_vector) {
do_fp_ld(s, rt, tcg_addr, size); do_fp_ld(s, rt, tcg_addr, size);
} else { } else {
do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false); /* Only unsigned 32bit loads target 32bit registers. */
bool iss_sf = opc == 0 ? 32 : 64;
do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
true, rt, iss_sf, false);
} }
tcg_temp_free_i64(tcg_ctx, tcg_addr); tcg_temp_free_i64(tcg_ctx, tcg_addr);
} }
@ -2078,40 +2160,39 @@ static void disas_ldst_pair(DisasContext *s, uint32_t insn)
} else { } else {
do_fp_st(s, rt, tcg_addr, size); do_fp_st(s, rt, tcg_addr, size);
} }
tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, 1 << size); } else {
if (is_load) {
do_fp_ld(s, rt2, tcg_addr, size);
} else {
do_fp_st(s, rt2, tcg_addr, size);
}
} else {
TCGv_i64 tcg_rt = cpu_reg(s, rt); TCGv_i64 tcg_rt = cpu_reg(s, rt);
TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
if (is_load) { if (is_load) {
TCGv_i64 tmp = tcg_temp_new_i64(tcg_ctx); do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
false, 0, false, false);
/* Do not modify tcg_rt before recognizing any exception
* from the second load.
*/
do_gpr_ld(s, tmp, tcg_addr, size, is_signed, false);
tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, 1 << size);
do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
tcg_gen_mov_i64(tcg_ctx, tcg_rt, tmp);
tcg_temp_free_i64(tcg_ctx, tmp);
} else { } else {
do_gpr_st(s, tcg_rt, tcg_addr, size); do_gpr_st(s, tcg_rt, tcg_addr, size,
tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, 1 << size); false, 0, false, false);
do_gpr_st(s, tcg_rt2, tcg_addr, size);
} }
} }
tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, 1 << size);
if (is_vector) {
if (is_load) {
do_fp_ld(s, rt2, tcg_addr, size);
} else {
do_fp_st(s, rt2, tcg_addr, size);
}
} else {
TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
if (is_load) {
do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
false, 0, false, false);
} else {
do_gpr_st(s, tcg_rt2, tcg_addr, size,
false, 0, false, false);
}
}
if (wback) { if (wback) {
if (postindex) { if (postindex) {
tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, offset - (1ULL << size)); tcg_gen_addi_i64(tcg_ctx, tcg_addr, tcg_addr, offset - (1 << size));
} else { } else {
tcg_gen_subi_i64(tcg_ctx, tcg_addr, tcg_addr, 1ULL << size); tcg_gen_subi_i64(tcg_ctx, tcg_addr, tcg_addr, 1 << size);
} }
tcg_gen_mov_i64(tcg_ctx, cpu_reg_sp(s, rn), tcg_addr); tcg_gen_mov_i64(tcg_ctx, cpu_reg_sp(s, rn), tcg_addr);
} }
@ -2147,6 +2228,7 @@ static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
bool is_store = false; bool is_store = false;
bool is_extended = false; bool is_extended = false;
bool is_unpriv = (idx == 2); bool is_unpriv = (idx == 2);
bool iss_valid = !is_vector;
bool post_index; bool post_index;
bool writeback; bool writeback;
@ -2214,12 +2296,15 @@ static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
} else { } else {
TCGv_i64 tcg_rt = cpu_reg(s, rt); TCGv_i64 tcg_rt = cpu_reg(s, rt);
int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s); int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
if (is_store) { if (is_store) {
do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx); do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
iss_valid, rt, iss_sf, false);
} else { } else {
do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size, do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
is_signed, is_extended, memidx); is_signed, is_extended, memidx,
iss_valid, rt, iss_sf, false);
} }
} }
@ -2318,10 +2403,14 @@ static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
} }
} else { } else {
TCGv_i64 tcg_rt = cpu_reg(s, rt); TCGv_i64 tcg_rt = cpu_reg(s, rt);
bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
if (is_store) { if (is_store) {
do_gpr_st(s, tcg_rt, tcg_addr, size); do_gpr_st(s, tcg_rt, tcg_addr, size,
true, rt, iss_sf, false);
} else { } else {
do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended); do_gpr_ld(s, tcg_rt, tcg_addr, size,
is_signed, is_extended,
true, rt, iss_sf, false);
} }
} }
} }
@ -2399,10 +2488,14 @@ static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
} }
} else { } else {
TCGv_i64 tcg_rt = cpu_reg(s, rt); TCGv_i64 tcg_rt = cpu_reg(s, rt);
bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
if (is_store) { if (is_store) {
do_gpr_st(s, tcg_rt, tcg_addr, size); do_gpr_st(s, tcg_rt, tcg_addr, size,
true, rt, iss_sf, false);
} else { } else {
do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended); do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
true, rt, iss_sf, false);
} }
} }
} }
@ -11340,7 +11433,8 @@ void gen_intermediate_code_a64(ARMCPU *cpu, TranslationBlock *tb)
gen_tb_start(tcg_ctx); gen_tb_start(tcg_ctx);
do { do {
tcg_gen_insn_start(tcg_ctx, dc->pc, 0); dc->insn_start_idx = tcg_op_buf_count(tcg_ctx);
tcg_gen_insn_start(tcg_ctx, dc->pc, 0, 0);
num_insns++; num_insns++;
if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) { if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {

View file

@ -11937,7 +11937,8 @@ void gen_intermediate_code(CPUARMState *env, TranslationBlock *tb)
} }
do { do {
tcg_gen_insn_start(tcg_ctx, dc->pc, tcg_gen_insn_start(tcg_ctx, dc->pc,
(dc->condexec_cond << 4) | (dc->condexec_mask >> 1)); (dc->condexec_cond << 4) | (dc->condexec_mask >> 1),
0);
num_insns++; num_insns++;
#ifdef CONFIG_USER_ONLY #ifdef CONFIG_USER_ONLY
/* Intercept jump to the magic kernel page. */ /* Intercept jump to the magic kernel page. */
@ -12296,8 +12297,10 @@ void restore_state_to_opc(CPUARMState *env, TranslationBlock *tb,
if (is_a64(env)) { if (is_a64(env)) {
env->pc = data[0]; env->pc = data[0];
env->condexec_bits = 0; env->condexec_bits = 0;
env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
} else { } else {
env->regs[15] = data[0]; env->regs[15] = data[0];
env->condexec_bits = data[1]; env->condexec_bits = data[1];
env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
} }
} }

View file

@ -59,6 +59,8 @@ typedef struct DisasContext {
bool ss_same_el; bool ss_same_el;
/* Bottom two bits of XScale c15_cpar coprocessor access control reg */ /* Bottom two bits of XScale c15_cpar coprocessor access control reg */
int c15_cpar; int c15_cpar;
/* TCG op index of the current insn_start. */
int insn_start_idx;
#define TMP_A64_MAX 16 #define TMP_A64_MAX 16
int tmp_a64_count; int tmp_a64_count;
TCGv_i64 tmp_a64[TMP_A64_MAX]; TCGv_i64 tmp_a64[TMP_A64_MAX];