target/arm: Fix insn exception priorities
cover target/arm: Fix insn exception priorities
As discussed earlier today at
https://lists.gnu.org/archive/html/qemu-devel/2021-08/msg02686.html
Raise pc alignment faults.
Fix single-step and pc-align priority over breakpoints. Not yet fixing insn abort priority over breakpoints.
r~
Peter Maydell (1):
target/arm: Take an exception if PSTATE.IL is set
Richard Henderson (3):
target/arm: Merge disas_a64_insn into aarch64_tr_translate_insn target/arm: Take an exception if PC is misaligned target/arm: Suppress bp for exceptions with more priority
target/arm/cpu.h | 1 + target/arm/syndrome.h | 10 ++ target/arm/translate.h | 2 + target/arm/debug_helper.c | 23 ++++ target/arm/helper-a64.c | 1 + target/arm/helper.c | 8 ++ target/arm/translate-a64.c | 267 ++++++++++++++++++++----------------- target/arm/translate.c | 71 ++++++++-- 8 files changed, 244 insertions(+), 139 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/4 target/arm: Take an exception if PSTATE.IL is set
From: Peter Maydell <[email protected]>
In v8A, the PSTATE.IL bit is set for various kinds of illegal exception return or mode-change attempts. We already set PSTATE.IL (or its AArch32 equivalent CPSR.IL) in all those cases, but we weren't implementing the part of the behaviour where attempting to execute an instruction with PSTATE.IL takes an immediate exception with an appropriate syndrome value.
Add a new TB flags bit tracking PSTATE.IL/CPSR.IL, and generate code to take an exception instead of whatever the instruction would have been.
PSTATE.IL and CPSR.IL change only on exception entry, attempted exception exit, and various AArch32 mode changes via cpsr_write(). These places generally already rebuild the hflags, so the only place we need an extra rebuild_hflags call is in the illegal-return codepath of the AArch64 exception_return helper.
Message-Id: <[email protected]>
--- target/arm/cpu.h | 1 + target/arm/syndrome.h | 5 +++++ target/arm/translate.h | 2 ++ target/arm/helper-a64.c | 1 + target/arm/helper.c | 8 ++++++++ target/arm/translate-a64.c | 11 +++++++++++ target/arm/translate.c | 21 +++++++++++++++++++++ 7 files changed, 49 insertions(+)
target/arm/cpu.h +1 -0
@@ -3441,6 +3441,7 @@ FIELD(TBFLAG_ANY, FPEXC_EL, 8, 2)FIELD (TBFLAG_ANY ,DEBUG_TARGET_EL ,10 ,2 ) /* Memory operations require alignment: SCTLR_ELx.A or CCR.UNALIGN_TRP */ FIELD(TBFLAG_ANY, ALIGN_MEM, 12, 1) +FIELD (TBFLAG_ANY ,PSTATE__IL ,13 ,1 )/* * Bit usage when in AArch32 state, both A- and M-profile.
target/arm/syndrome.h +5 -0
@@ -270,4 +270,9 @@ static inline uint32_t syn_wfx(int cv, int cond, int ti, bool is_16bit) (cv << 24) | (cond << 20) | ti; } +static inline uint32_t syn_illegalstate (void ) +{ +return EC_ILLEGALSTATE <<ARM_EL_EC_SHIFT ; +} +#endif /* TARGET_ARM_SYNDROME_H */
target/arm/translate.h +2 -0
@@ -98,6 +98,8 @@ typedef struct DisasContext {bool hstr_active;/* True if memory operations require alignment */ bool align_mem; +/* True if PSTATE.IL is set */ +bool pstate_il;/* * >= 0, a copy of PSTATE.BTYPE, which will be 0 without v8.5-BTI. * < 0, set by the current instruction.
target/arm/helper-a64.c +1 -0
@@ -1071,6 +1071,7 @@ illegal_return:if (!arm_singlestep_active (env)) { env->pstate &= ~PSTATE_SS ; } +helper_rebuild_hflags_a64 (env, cur_el);qemu_log_mask (LOG_GUEST_ERROR ,"Illegal exception return at EL%d: " "resuming execution at 0x%" PRIx64"\n" , cur_el, env->pc); }
target/arm/helper.c +8 -0
@@ -13408,6 +13408,10 @@ static CPUARMTBFlags rebuild_hflags_a32(CPUARMState *env, int fp_el,DP_TBFLAG_A32 (flags,HSTR_ACTIVE ,1 ); } +if (env->uncached_cpsr &CPSR_IL ) { +DP_TBFLAG_ANY (flags,PSTATE__IL ,1 ); + } +return rebuild_hflags_common_32 (env, fp_el, mmu_idx, flags); }
@@ -13502,6 +13506,10 @@ static CPUARMTBFlags rebuild_hflags_a64(CPUARMState *env, int el, int fp_el, } } +if (env->pstate &PSTATE_IL ) { +DP_TBFLAG_ANY (flags,PSTATE__IL ,1 ); + } +if (cpu_isar_feature (aa64_mte,env_archcpu (env))) {/* * Set MTE_ACTIVE if any access may be Checked, and leave clear
target/arm/translate-a64.c +11 -0
@@ -14662,6 +14662,16 @@ static void disas_a64_insn(CPUARMState *env, DisasContext *s) s->fp_access_checked = false; s->sve_access_checked = false; +if (s->pstate_il) { +/* +* Illegal execution state. This has priority over BTI +* exceptions, but comes after instruction abort exceptions. +*/ +gen_exception_insn (s, s->pc_curr,EXCP_UDEF , +syn_illegalstate (),default_exception_el (s)); +return ; + } +if (dc_isar_feature (aa64_bti, s)) {if (s->base.num_insns ==1 ) {/*
@@ -14780,6 +14790,7 @@ static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,#endif dc->fp_excp_el =EX_TBFLAG_ANY (tb_flags,FPEXC_EL ); dc->align_mem =EX_TBFLAG_ANY (tb_flags,ALIGN_MEM ); + dc->pstate_il =EX_TBFLAG_ANY (tb_flags,PSTATE__IL ); dc->sve_excp_el =EX_TBFLAG_A64 (tb_flags,SVEEXC_EL ); dc->sve_len = (EX_TBFLAG_A64 (tb_flags,ZCR_LEN ) +1 ) *16 ; dc->pauth_active =EX_TBFLAG_A64 (tb_flags,PAUTH_ACTIVE );
target/arm/translate.c +21 -0
@@ -9045,6 +9045,16 @@ static void disas_arm_insn(DisasContext *s, unsigned int insn)return ; } +if (s->pstate_il) { +/* +* Illegal execution state. This has priority over BTI +* exceptions, but comes after instruction abort exceptions. +*/ +gen_exception_insn (s, s->pc_curr,EXCP_UDEF , +syn_illegalstate (),default_exception_el (s)); +return ; + } +if (cond ==0xf ) {/* In ARMv3 and v4 the NV condition is UNPREDICTABLE; we * choose to UNDEF. In ARMv5 and above the space is used
@@ -9313,6 +9323,7 @@ static void arm_tr_init_disas_context(DisasContextBase *dcbase, CPUState *cs)#endif dc->fp_excp_el =EX_TBFLAG_ANY (tb_flags,FPEXC_EL ); dc->align_mem =EX_TBFLAG_ANY (tb_flags,ALIGN_MEM ); + dc->pstate_il =EX_TBFLAG_ANY (tb_flags,PSTATE__IL );if (arm_feature (env,ARM_FEATURE_M )) { dc->vfp_enabled =1 ;
@@ -9576,6 +9587,16 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) } dc->insn = insn; +if (dc->pstate_il) { +/* +* Illegal execution state. This has priority over BTI +* exceptions, but comes after instruction abort exceptions. +*/ +gen_exception_insn (dc, dc->pc_curr,EXCP_UDEF , +syn_illegalstate (),default_exception_el (dc)); +return ; + } +if (dc->eci) {/* * For M-profile continuable instructions, ECI/ICI handling
Signature · 1 line
2.25.1
PATCH 2/4 target/arm: Merge disas_a64_insn into aarch64_tr_translate_insn
In reply to Richard HendersonIt is confusing to have different exits from translation for various conditions in separate functions.
Merge disas_a64_insn into its only caller. Standardize on the "s" name for the DisasContext, as the code from disas_a64_insn had more instances.
--- target/arm/translate-a64.c | 224 ++++++++++++++++++------------------- 1 file changed, 109 insertions(+), 115 deletions(-)
target/arm/translate-a64.c +109 -115
@@ -14649,113 +14649,6 @@ static bool btype_destination_ok(uint32_t insn, bool bt, int btype)return false; } -/* C3.1 A64 instruction index by encoding */ -static void disas_a64_insn (CPUARMState *env,DisasContext *s) -{ -uint32_t insn; - - s->pc_curr = s->base.pc_next; - insn =arm_ldl_code (env, s->base.pc_next, s->sctlr_b); - s->insn = insn; - s->base.pc_next +=4 ; - - s->fp_access_checked = false; - s->sve_access_checked = false; - -if (s->pstate_il) { -/* -* Illegal execution state. This has priority over BTI -* exceptions, but comes after instruction abort exceptions. -*/ -gen_exception_insn (s, s->pc_curr,EXCP_UDEF , -syn_illegalstate (),default_exception_el (s)); -return ; - } - -if (dc_isar_feature (aa64_bti, s)) { -if (s->base.num_insns ==1 ) { -/* -* At the first insn of the TB, compute s->guarded_page. -* We delayed computing this until successfully reading -* the first insn of the TB, above. This (mostly) ensures -* that the softmmu tlb entry has been populated, and the -* page table GP bit is available. -* -* Note that we need to compute this even if btype == 0, -* because this value is used for BR instructions later -* where ENV is not available. -*/ - s->guarded_page =is_guarded_page (env, s); - -/* First insn can have btype set to non-zero. */ -tcg_debug_assert (s->btype >=0 ); - -/* -* Note that the Branch Target Exception has fairly high -* priority -- below debugging exceptions but above most -* everything else. This allows us to handle this now -* instead of waiting until the insn is otherwise decoded. -*/ -if (s->btype !=0 - && s->guarded_page - && !btype_destination_ok (insn, s->bt, s->btype)) { -gen_exception_insn (s, s->pc_curr,EXCP_UDEF , -syn_btitrap (s->btype), -default_exception_el (s)); -return ; - } - }else { -/* Not the first insn: btype must be 0. */ -tcg_debug_assert (s->btype ==0 ); - } - } - -switch (extract32 (insn,25 ,4 )) { -case 0x0 :case 0x1 :case 0x3 :/* UNALLOCATED */ -unallocated_encoding (s); -break ; -case 0x2 : -if (!dc_isar_feature (aa64_sve, s) || !disas_sve (s, insn)) { -unallocated_encoding (s); - } -break ; -case 0x8 :case 0x9 :/* Data processing - immediate */ -disas_data_proc_imm (s, insn); -break ; -case 0xa :case 0xb :/* Branch, exception generation and system insns */ -disas_b_exc_sys (s, insn); -break ; -case 0x4 : -case 0x6 : -case 0xc : -case 0xe :/* Loads and stores */ -disas_ldst (s, insn); -break ; -case 0x5 : -case 0xd :/* Data processing - register */ -disas_data_proc_reg (s, insn); -break ; -case 0x7 : -case 0xf :/* Data processing - SIMD and floating point */ -disas_data_proc_simd_fp (s, insn); -break ; -default : -assert (FALSE);/* all 15 cases should be handled above */ -break ; - } - -/* if we allocated any temporaries, free them here */ -free_tmp_a64 (s); - -/* -* After execution of most insns, btype is reset to 0. -* Note that we set btype == -1 when the insn sets btype. -*/ -if (s->btype >0 && s->base.is_jmp !=DISAS_NORETURN ) { -reset_btype (s); - } -} -static void aarch64_tr_init_disas_context (DisasContextBase *dcbase,CPUState *cpu) {
@@ -14857,10 +14750,11 @@ static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)static void aarch64_tr_translate_insn (DisasContextBase *dcbase,CPUState *cpu) { -DisasContext *dc =container_of (dcbase, DisasContext, base); +DisasContext *s =container_of (dcbase, DisasContext, base);CPUARMState *env = cpu->env_ptr; +uint32_t insn; -if (dc->ss_active && !dc->pstate_ss) { +if (s->ss_active && !s->pstate_ss) {/* Singlestep state is Active-pending. * If we're in this state at the start of a TB then either * a) we just took an exception to an EL which is being debugged
@@ -14871,14 +14765,114 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)* "did not step an insn" case, and so the syndrome ISV and EX * bits should be zero. */ -assert (dc->base.num_insns ==1 ); -gen_swstep_exception (dc,0 ,0 ); - dc->base.is_jmp =DISAS_NORETURN ; - }else { -disas_a64_insn (env, dc); +assert (s->base.num_insns ==1 ); +gen_swstep_exception (s,0 ,0 ); + s->base.is_jmp =DISAS_NORETURN ; +return ; } -translator_loop_temp_check (&dc->base); + s->pc_curr = s->base.pc_next; + insn =arm_ldl_code (env, s->base.pc_next, s->sctlr_b); + s->insn = insn; + s->base.pc_next +=4 ; + + s->fp_access_checked = false; + s->sve_access_checked = false; + +if (s->pstate_il) { +/* +* Illegal execution state. This has priority over BTI +* exceptions, but comes after instruction abort exceptions. +*/ +gen_exception_insn (s, s->pc_curr,EXCP_UDEF , +syn_illegalstate (),default_exception_el (s)); +return ; + } + +if (dc_isar_feature (aa64_bti, s)) { +if (s->base.num_insns ==1 ) { +/* +* At the first insn of the TB, compute s->guarded_page. +* We delayed computing this until successfully reading +* the first insn of the TB, above. This (mostly) ensures +* that the softmmu tlb entry has been populated, and the +* page table GP bit is available. +* +* Note that we need to compute this even if btype == 0, +* because this value is used for BR instructions later +* where ENV is not available. +*/ + s->guarded_page =is_guarded_page (env, s); + +/* First insn can have btype set to non-zero. */ +tcg_debug_assert (s->btype >=0 ); + +/* +* Note that the Branch Target Exception has fairly high +* priority -- below debugging exceptions but above most +* everything else. This allows us to handle this now +* instead of waiting until the insn is otherwise decoded. +*/ +if (s->btype !=0 + && s->guarded_page + && !btype_destination_ok (insn, s->bt, s->btype)) { +gen_exception_insn (s, s->pc_curr,EXCP_UDEF , +syn_btitrap (s->btype), +default_exception_el (s)); +return ; + } + }else { +/* Not the first insn: btype must be 0. */ +tcg_debug_assert (s->btype ==0 ); + } + } + +switch (extract32 (insn,25 ,4 )) { +case 0x0 :case 0x1 :case 0x3 :/* UNALLOCATED */ +unallocated_encoding (s); +break ; +case 0x2 : +if (!dc_isar_feature (aa64_sve, s) || !disas_sve (s, insn)) { +unallocated_encoding (s); + } +break ; +case 0x8 :case 0x9 :/* Data processing - immediate */ +disas_data_proc_imm (s, insn); +break ; +case 0xa :case 0xb :/* Branch, exception generation and system insns */ +disas_b_exc_sys (s, insn); +break ; +case 0x4 : +case 0x6 : +case 0xc : +case 0xe :/* Loads and stores */ +disas_ldst (s, insn); +break ; +case 0x5 : +case 0xd :/* Data processing - register */ +disas_data_proc_reg (s, insn); +break ; +case 0x7 : +case 0xf :/* Data processing - SIMD and floating point */ +disas_data_proc_simd_fp (s, insn); +break ; +default : +assert (FALSE);/* all 15 cases should be handled above */ +break ; + } + +/* if we allocated any temporaries, free them here */ +free_tmp_a64 (s); + +/* +* After execution of most insns, btype is reset to 0. +* Note that we set btype == -1 when the insn sets btype. +*/ +if (s->btype >0 && s->base.is_jmp !=DISAS_NORETURN ) { +reset_btype (s); + } + +translator_loop_temp_check (&s->base); }static void aarch64_tr_tb_stop (DisasContextBase *dcbase,CPUState *cpu)
Signature · 1 line
2.25.1
Re: [PATCH 2/4] target/arm: Merge disas_a64_insn into aarch64_tr_translate_insn
Quoted 9 lines · Richard Henderson
It is confusing to have different exits from translation for various conditions in separate functions.
Merge
disas_a64_insninto its only caller. Standardize on the "s" name for the DisasContext, as the code fromdisas_a64_insnhad more instances.Signed-off-by Richard Henderson <[email protected]>
thanks
-- PMM
PATCH 3/4 target/arm: Take an exception if PC is misaligned
In reply to Richard HendersonFor A64, any input to an indirect branch can cause this.
For A32, many indirect branch paths force the branch to be aligned, but BXWritePC does not. This includes the BX instruction but also other interworking changes to PC. Prior to v8, this case is UNDEFINED. With v8, this is CONSTRAINED UNDEFINED and may either raise an exception or force align the PC.
We choose to raise an exception because we have the infrastructure, it makes the generated code for gen_bx simpler, and it has the possibility of catching more guest bugs.
--- target/arm/syndrome.h | 5 ++++ target/arm/translate-a64.c | 12 +++++++++ target/arm/translate.c | 50 +++++++++++++++++++++++++++----------- 3 files changed, 53 insertions(+), 14 deletions(-)
target/arm/syndrome.h +5 -0
@@ -275,4 +275,9 @@ static inline uint32_t syn_illegalstate(void) return EC_ILLEGALSTATE << ARM_EL_EC_SHIFT; } +static inline uint32_t syn_pcalignment (void ) +{ +return EC_PCALIGNMENT <<ARM_EL_EC_SHIFT ; +} +#endif /* TARGET_ARM_SYNDROME_H */
target/arm/translate-a64.c +12 -0
@@ -14754,6 +14754,7 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPUARMState *env = cpu->env_ptr;uint32_t insn; +/* Singlestep exceptions have the highest priority. */ if (s->ss_active && !s->pstate_ss) {/* Singlestep state is Active-pending. * If we're in this state at the start of a TB then either
@@ -14771,6 +14772,17 @@ static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)return ; } +if (s->base.pc_next &3 ) { +/* +* PC alignment fault. This has priority over the instruction abort +* that we would receive from a translation fault via arm_ldl_code. +*/ +gen_exception_insn (s, s->base.pc_next,EXCP_UDEF , +syn_pcalignment (),default_exception_el (s)); + s->base.pc_next =QEMU_ALIGN_UP (s->base.pc_next,4 ); +return ; + } + s->pc_curr = s->base.pc_next; insn =arm_ldl_code (env, s->base.pc_next, s->sctlr_b); s->insn = insn;
target/arm/translate.c +36 -14
75 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 1 line · Richard Henderson
With v8, this is CONSTRAINED UNDEFINED and may either raise an
Bah, UNPREDICTABLE, of course, not UNDEFINED.
r~
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
In reply to Richard HendersonQuoted 45 lines · Richard Henderson
For A64, any input to an indirect branch can cause this.
For A32, many indirect branch paths force the branch to be aligned, but BXWritePC does not. This includes the BX instruction but also other interworking changes to PC. Prior to v8, this case is UNDEFINED. With v8, this is CONSTRAINED UNDEFINED and may either raise an exception or force align the PC.
We choose to raise an exception because we have the infrastructure, it makes the generated code for
gen_bxsimpler, and it has the possibility of catching more guest bugs.Signed-off-by Richard Henderson <[email protected]>static void
arm_post_translate_insn(DisasContext *dc){ if (dc->condjmp && !dc->base.is_jmp) {target/arm/translate.c +19
-1@@ -9500,7 +9504,25 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPUARMState *env = cpu->env_ptr;unsigned int insn; -if (arm_pre_translate_insn (dc)) { +/* Singlestep exceptions have the highest priority. */ +if (arm_check_ss_active (dc)) { + dc->base.pc_next +=4 ; +return ; + } + +if (dc->base.pc_next &3 ) { +/* +* PC alignment fault. This has priority over the instruction abort +* that we would receive from a translation fault via arm_ldl_code +* (or the execution of the kernelpage entrypoint). +*/ +gen_exception_insn (dc, dc->base.pc_next,EXCP_UDEF , +syn_pcalignment (),default_exception_el (dc)); + dc->base.pc_next =QEMU_ALIGN_UP (dc->base.pc_next,4 ); +return ; + } + +if (arm_check_kernelpage (dc)) { dc->base.pc_next +=4 ;return ; }
target/arm/translate.c static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 9595
Quoted 5 lines
target/arm/translate.c +1
-1@@ -9570,7 +9592,7 @@ static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)uint32_t insn;bool is_16bit; -if (arm_pre_translate_insn (dc)) { +if (arm_check_ss_active (dc) ||arm_check_kernelpage (dc)) {
Is it not possible to get a misaligned PC in the Thumb case ?
target/arm/translate.c static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 9598
Quoted 3 lines
target/arm/translate.c +0
-0dc->base.pc_next +=2 ;return ; }
-- PMM
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 8 lines · Peter Maydell
target/arm/translate.c static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 9595
Quoted 5 lines
uint32_t insn; bool is_16bit;target/arm/translate.c +1
-1-if (arm_pre_translate_insn (dc)) { +if (arm_check_ss_active (dc) ||arm_check_kernelpage (dc)) {Is it not possible to get a misaligned PC in the Thumb case ?
No. The thumb bit is always removed, leaving all pc aligned mod 2. Both BXWritePC and BranchWritePC do this, as do we in gen_bx and store_reg.
r~
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Peter Maydell
Quoted 3 lines
uint32_tinsn;
boolis_16bit;
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
- if (
arm_pre_translate_insn(dc)) {
+ if (arm_check_ss_active(dc)||arm_check_kernelpage(dc)) {Is it not possible to get a misaligned PC in the Thumb case ?
No. The thumb bit is always removed, leaving all pc aligned mod 2. Both BXWritePC and BranchWritePC do this, as do we in
gen_bxandstore_reg.
Do you think it's worth an assert here to make sure we never miss a case? I did an audit of the exception code and it looks like we mask everything correctly there, but...
r~
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 10 lines · Richard Henderson
Quoted 8 lines · Richard Henderson
Quoted 7 lines · Peter Maydell
Quoted 5 lines
uint32_t insn; bool is_16bit;target/arm/translate.c +1
-1-if (arm_pre_translate_insn (dc)) { +if (arm_check_ss_active (dc) ||arm_check_kernelpage (dc)) {
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Is it not possible to get a misaligned PC in the Thumb case ?
No. The thumb bit is always removed, leaving all pc aligned mod 2. Both BXWritePC and BranchWritePC do this, as do we in
gen_bxandstore_reg.Do you think it's worth an assert here to make sure we never miss a case? I did an audit of the exception code and it looks like we mask everything correctly there, but...
(Did you check the M-profile code too? That also architecturally I think should never let PC have the low bit set; hopefully the code I wrote actually ensures that...)
I guess an assert() is more helpful than ploughing ahead with a misaligned PC value. If we don't assert we should at least have a comment saying misaligned Thumb PCs are architecturally impossible.
If we do go for the assert, then the comment in arm_cpu_gdb_write_register() about why we don't let GDB set bit 0 in the PC would need updating (there would now be two reasons). We should probably also fail the migration if we get an unaligned Thumb PC in the inbound data.
-- PMM
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 3 lines · Peter Maydell
(Did you check the M-profile code too? That also architecturally I think should never let PC have the low bit set; hopefully the code I wrote actually ensures that...)
Exception handling in m-profile is much harder to follow, but certainly normal updates to _NextInstrAddr are all force aligned.
r~
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
In reply to Richard HendersonQuoted 23 lines · Richard Henderson
For A64, any input to an indirect branch can cause this.
For A32, many indirect branch paths force the branch to be aligned, but BXWritePC does not. This includes the BX instruction but also other interworking changes to PC. Prior to v8, this case is UNDEFINED. With v8, this is CONSTRAINED UNDEFINED and may either raise an exception or force align the PC.
We choose to raise an exception because we have the infrastructure, it makes the generated code for
gen_bxsimpler, and it has the possibility of catching more guest bugs.target/arm/translate.c +5
-1@@ -9500,7 +9504,25 @@ static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)CPUARMState *env = cpu->env_ptr;unsigned int insn; -if (arm_pre_translate_insn (dc)) { +/* Singlestep exceptions have the highest priority. */ +if (arm_check_ss_active (dc)) { + dc->base.pc_next +=4 ; +return ; + }
target/arm/translate.c static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu) · line 9521
Quoted 10 lines
target/arm/translate.c +10
-0+ +if (dc->base.pc_next &3 ) { +/* +* PC alignment fault. This has priority over the instruction abort +* that we would receive from a translation fault via arm_ldl_code +* (or the execution of the kernelpage entrypoint). +*/ +gen_exception_insn (dc, dc->base.pc_next,EXCP_UDEF , +syn_pcalignment (),default_exception_el (dc)); + dc->base.pc_next =QEMU_ALIGN_UP (dc->base.pc_next,4 );
Just noticed that section G1.16.7 says that when we report PC alignment faults to AArch32 they should be prefetch aborts, not UDEF. The fault address and fault status registers also need to be set (with slightly varying behaviour for when the fault is taken to Hyp mode).
For AArch64 we should also be setting the FAR, which means that for consistency it's better to use EXCP_PREFETCH_ABORT and set exception.vaddress in the translate-a64.c code (you get better logging in the exception-entry code) even though these different EXCP_* all boil down to the same synchronous-exception vector.
-- PMM
Re: [PATCH 3/4] target/arm: Take an exception if PC is misaligned
Quoted 6 lines · Peter Maydell
Just noticed that section G1.16.7 says that when we report PC alignment faults to AArch32 they should be prefetch aborts, not UDEF. The fault address and fault status registers also need to be set (with slightly varying behaviour for when the fault is taken to Hyp mode).
Quoted 6 lines
For AArch64 we should also be setting the FAR, which means that for consistency it's better to use
EXCP_PREFETCH_ABORTand set exception.vaddress in thetranslate-a64.ccode (you get better logging in the exception-entry code) even though these different EXCP_* all boil down to the same synchronous-exception vector.
Also, looking at kernel code while reviewing your alignment-checking patchset, I realized that we should also catch this case of a prefetch abort in linux-user/ and turn it into a SIGBUS/BUS_ADRALN with the address being whatever the value in the FAR is (for both arm and aarch64).
-- PMM
PATCH 4/4 target/arm: Suppress bp for exceptions with more priority
In reply to Richard HendersonBoth single-step and pc alignment faults have priority over breakpoint exceptions.
--- target/arm/debug_helper.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
target/arm/debug_helper.c +23 -0
@@ -220,6 +220,7 @@ bool arm_debug_check_breakpoint(CPUState *cs) {ARMCPU *cpu =ARM_CPU (cs);CPUARMState *env = &cpu->env; +target_ulong pc;int n;/*
@@ -231,6 +232,28 @@ bool arm_debug_check_breakpoint(CPUState *cs)return false; } +/* +* Single-step exceptions have priority over breakpoint exceptions. +* If single-step state is active-pending, suppress the bp. +*/ +if (arm_singlestep_active (env) && !(env->pstate &PSTATE_SS )) { +return false; + } + +/* +* PC alignment faults have priority over breakpoint exceptions. +*/ + pc =is_a64 (env) ? env->pc : env->regs[15 ]; +if ((is_a64 (env) || !env->thumb) && (pc &3 ) !=0 ) { +return false; + } + +/* +* Instruction aborts have priority over breakpoint exceptions. +* TODO: We would need to look up the page for PC and verify that +* it is present and executable. +*/ +for (n =0 ; n <ARRAY_SIZE (env->cpu_breakpoint); n++) {if (bp_wp_matches (cpu, n, false)) {return true;
Signature · 1 line
2.25.1
Re: [PATCH 4/4] target/arm: Suppress bp for exceptions with more priority
Quoted 8 lines · Richard Henderson
Both single-step and pc alignment faults have priority over breakpoint exceptions.
Signed-off-by Richard Henderson <[email protected]>--- target/arm/debug_helper.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
target/arm/debug_helper.c bool arm_debug_check_breakpoint(CPUState *cs) · line 249
Quoted 8 lines
target/arm/debug_helper.c +8
-0+ +/* +* PC alignment faults have priority over breakpoint exceptions. +*/ + pc =is_a64 (env) ? env->pc : env->regs[15 ]; +if ((is_a64 (env) || !env->thumb) && (pc &3 ) !=0 ) { +return false; + }
Other than the obvious adjustment if we need to handle env->thumb && (pc & 1)
thanks
-- PMM