target/arm: Fix ESR_EL2 buglet, implement FEAT_PAN3
cover target/arm: Fix ESR_EL2 buglet, implement FEAT_PAN3
The main purpose of this patchset is to implement FEAT_PAN3, which allows the guest to force privileged code to not be able to access memory that can be executed by user code. (This is an extension of the existing FEAT_PAN which denies access if user code could read or write the memory.) That is all done in patch 3.
Patches 1 and 2 fix a buglet in our ESR_EL2 syndrome reporting that I happened to notice while testing the FEAT_PAN3 code:
we were reporting the detailed instruction syndrome information for all data aborts reported to EL2, whereas the architecture requires this to happen only for stage-2 aborts, not stage-1 aborts.
This is all for-8.1 material -- the syndrome bug is minor and has been around forever so isn't worth trying to fix for 8.0 at this point in the release cycle.
thanks
-- PMM
Peter Maydell (3):
target/arm: Pass ARMMMUFaultInfo to merge_syn_data_abort() target/arm: Don't set ISV when reporting stage 1 faults in ESR_EL2 target/arm: Implement FEAT_PAN3
docs/system/arm/emulation.rst | 1 + target/arm/cpu.h | 5 +++++ target/arm/cpu64.c | 2 +- target/arm/ptw.c | 14 +++++++++++++- target/arm/tcg/tlb_helper.c | 26 ++++++++++++++++---------- 5 files changed, 36 insertions(+), 12 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 target/arm: Pass ARMMMUFaultInfo to merge_syn_data_abort()
We already pass merge_syn_data_abort() two fields from the ARMMMUFaultInfo struct, and we're about to want to use a third field. Refactor to just pass a pointer to the fault info.
--- target/arm/tcg/tlb_helper.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
target/arm/tcg/tlb_helper.c +7 -8
@@ -24,9 +24,9 @@ bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) }static inline uint32_t merge_syn_data_abort (uint32_t template_syn, +ARMMMUFaultInfo *fi,unsigned int target_el, -bool same_el,bool ea, -bool s1ptw,bool is_write, +bool same_el,bool is_write,int fsc) {uint32_t syn;
@@ -43,9 +43,9 @@ static inline uint32_t merge_syn_data_abort(uint32_t template_syn,* ISS encoding for an exception from a Data Abort, the * ISV field. */ -if (!(template_syn &ARM_EL_ISV ) || target_el !=2 || s1ptw) { +if (!(template_syn &ARM_EL_ISV ) || target_el !=2 || fi->s1ptw) { syn =syn_data_abort_no_iss (same_el,0 , - ea,0 , s1ptw, is_write, fsc); + fi->ea,0 , fi->s1ptw, is_write, fsc); }else {/* * Fields: IL, ISV, SAS, SSE, SRT, SF and AR come from the template
@@ -54,7 +54,7 @@ static inline uint32_t merge_syn_data_abort(uint32_t template_syn,*/ syn =syn_data_abort_with_iss (same_el,0 ,0 ,0 ,0 ,0 , - ea,0 , s1ptw, is_write, fsc, + fi->ea,0 , fi->s1ptw, is_write, fsc, true);/* Merge the runtime syndrome with the template syndrome. */ syn |= template_syn;
@@ -117,9 +117,8 @@ void arm_deliver_fault(ARMCPU *cpu, vaddr addr, syn =syn_insn_abort (same_el, fi->ea, fi->s1ptw, fsc); exc =EXCP_PREFETCH_ABORT ; }else { - syn =merge_syn_data_abort (env->exception.syndrome, target_el, - same_el, fi->ea, fi->s1ptw, - access_type ==MMU_DATA_STORE , + syn =merge_syn_data_abort (env->exception.syndrome, fi, target_el, + same_el, access_type ==MMU_DATA_STORE , fsc);if (access_type ==MMU_DATA_STORE &&arm_feature (env,ARM_FEATURE_V6 )) {
Signature · 1 line
2.34.1
Re: [PATCH 1/3] target/arm: Pass ARMMMUFaultInfo to merge_syn_data_abort()
Quoted 8 lines · Peter Maydell
We already pass
merge_syn_data_abort()two fields from the ARMMMUFaultInfo struct, and we're about to want to use a third field. Refactor to just pass a pointer to the fault info.Signed-off-by Peter Maydell<[email protected]>--- target/arm/tcg/tlb_helper.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
r~
Re: [PATCH 1/3] target/arm: Pass ARMMMUFaultInfo to merge_syn_data_abort()
In reply to Peter MaydellQuoted 8 lines · Peter Maydell
We already pass
merge_syn_data_abort()two fields from the ARMMMUFaultInfo struct, and we're about to want to use a third field. Refactor to just pass a pointer to the fault info.Signed-off-by Peter Maydell <[email protected]>--- target/arm/tcg/tlb_helper.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
PATCH 2/3 target/arm: Don't set ISV when reporting stage 1 faults in ESR_EL2
In reply to Peter MaydellThe syndrome value reported to ESR_EL2 should only contain the detailed instruction syndrome information when the fault has been caused by a stage 2 abort, not when the fault was a stage 1 abort (i.e. caused by execution at EL2). We were getting this wrong and reporting the detailed ISV information all the time.
Fix the bug by checking fi->stage2. Add a TODO comment noting the cases where we'll have to come back and revisit this when we implement FEAT_LS64 and friends.
--- target/arm/tcg/tlb_helper.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
target/arm/tcg/tlb_helper.c +10 -3
@@ -32,8 +32,9 @@ static inline uint32_t merge_syn_data_abort(uint32_t template_syn,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. +* ISV is only set for stage-2 data aborts routed to EL2 and +* never for stage-1 page table walks faulting on stage 2 +* or for stage-1 faults. * * Furthermore, ISV is only set for certain kinds of load/stores. * If the template syndrome does not have ISV set, we should leave
@@ -42,8 +43,14 @@ static inline uint32_t merge_syn_data_abort(uint32_t template_syn,* See ARMv8 specs, D7-1974: * ISS encoding for an exception from a Data Abort, the * ISV field. +* +* TODO: FEAT_LS64/FEAT_LS64_V/FEAT_SL64_ACCDATA: Translation, +* Access Flag, and Permission faults caused by LD64B, ST64B, +* ST64BV, or ST64BV0 insns report syndrome info even for stage-1 +* faults and regardless of the target EL. */ -if (!(template_syn &ARM_EL_ISV ) || target_el !=2 || fi->s1ptw) { +if (!(template_syn &ARM_EL_ISV ) || target_el !=2 + || fi->s1ptw || !fi->stage2) { syn =syn_data_abort_no_iss (same_el,0 , fi->ea,0 , fi->s1ptw, is_write, fsc); }else {
Signature · 1 line
2.34.1
Re: [PATCH 2/3] target/arm: Don't set ISV when reporting stage 1 faults in ESR_EL2
Quoted 6 lines · Peter Maydell
The syndrome value reported to
ESR_EL2should only contain the detailed instruction syndrome information when the fault has been caused by a stage 2 abort, not when the fault was a stage 1 abort (i.e. caused by execution at EL2). We were getting this wrong and reporting the detailed ISV information all the time.
Quoted 8 lines
Fix the bug by checking
fi->stage2. Add a TODO comment noting the cases where we'll have to come back and revisit this when we implementFEAT_LS64and friends.Signed-off-by Peter Maydell<[email protected]>--- target/arm/tcg/tlb_helper.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
r~
PATCH 3/3 target/arm: Implement FEAT_PAN3
In reply to Peter MaydellFEAT_PAN3 adds an EPAN bit to SCTLR_EL1 and SCTLR_EL2, which allows the PAN bit to make memory non-privileged-read/write if it is user-executable as well as if it is user-read/write.
Implement this feature and enable it in the AArch64 'max' CPU.
--- docs/system/arm/emulation.rst | 1 + target/arm/cpu.h | 5 +++++ target/arm/cpu64.c | 2 +- target/arm/ptw.c | 14 +++++++++++++- 4 files changed, 20 insertions(+), 2 deletions(-)
docs/system/arm/emulation.rst +1 -0
@@ -56,6 +56,7 @@ the following architecture extensions: - FEAT_MTE3 (MTE Asymmetric Fault Handling) - FEAT_PAN (Privileged access never) - FEAT_PAN2 (AT S1E1R and AT S1E1W instruction variants affected by PSTATE.PAN) +- FEAT_PAN3 (Support for SCTLR_ELx.EPAN) - FEAT_PAuth (Pointer authentication) - FEAT_PMULL (PMULL, PMULL2 instructions) - FEAT_PMUv3p1 (PMU Extensions v3.1)
target/arm/cpu.h +5 -0
@@ -3823,6 +3823,11 @@ static inline bool isar_feature_aa64_ats1e1(const ARMISARegisters *id)return FIELD_EX64 (id->id_aa64mmfr1,ID_AA64MMFR1 ,PAN ) >=2 ; } +static inline bool isar_feature_aa64_pan3 (const ARMISARegisters *id) +{ +return FIELD_EX64 (id->id_aa64mmfr1,ID_AA64MMFR1 ,PAN ) >=3 ; +} +static inline bool isar_feature_aa64_hcx (const ARMISARegisters *id) {return FIELD_EX64 (id->id_aa64mmfr1,ID_AA64MMFR1 ,HCX ) !=0 ;
target/arm/cpu64.c +1 -1
@@ -1302,7 +1302,7 @@ static void aarch64_max_initfn(Object *obj) t =FIELD_DP64 (t,ID_AA64MMFR1 ,VH ,1 );/* FEAT_VHE */ t =FIELD_DP64 (t,ID_AA64MMFR1 ,HPDS ,1 );/* FEAT_HPDS */ t =FIELD_DP64 (t,ID_AA64MMFR1 ,LO ,1 );/* FEAT_LOR */ - t =FIELD_DP64 (t,ID_AA64MMFR1 ,PAN ,2 );/* FEAT_PAN2 */ + t =FIELD_DP64 (t,ID_AA64MMFR1 ,PAN ,3 );/* FEAT_PAN3 */ t =FIELD_DP64 (t,ID_AA64MMFR1 ,XNX ,1 );/* FEAT_XNX */ t =FIELD_DP64 (t,ID_AA64MMFR1 ,ETS ,1 );/* FEAT_ETS */ t =FIELD_DP64 (t,ID_AA64MMFR1 ,HCX ,1 );/* FEAT_HCX */
target/arm/ptw.c +13 -1
@@ -947,6 +947,7 @@ static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64,int ap,int ns,int xn,int pxn) { +ARMCPU *cpu =env_archcpu (env);bool is_user =regime_is_user (env, mmu_idx);int prot_rw, user_rw;bool have_wxn;
@@ -958,8 +959,19 @@ static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,if (is_user) { prot_rw = user_rw; }else { +/* +* PAN controls can forbid data accesses but don't affect insn fetch. +* Plain PAN forbids data accesses if EL0 has data permissions; +* PAN3 forbids data accesses if EL0 has either data or exec perms. +* Note that for AArch64 the 'user can exec' case is exactly !xn. +* We make the IMPDEF choices that SCR_EL3.SIF and Realm EL2&0 +* do not affect EPAN. +*/ if (user_rw &®ime_is_pan (env, mmu_idx)) { -/* PAN forbids data accesses but doesn't affect insn fetch */ + prot_rw =0 ; + }else if (cpu_isar_feature (aa64_pan3, cpu) && is_aa64 && +regime_is_pan (env, mmu_idx) && + (regime_sctlr (env, mmu_idx) &SCTLR_EPAN ) && !xn) { prot_rw =0 ; }else { prot_rw =simple_ap_to_rw_prot_is_user (ap, false);
Signature · 1 line
2.34.1
Re: [PATCH 3/3] target/arm: Implement FEAT_PAN3
Quoted 6 lines · Peter Maydell
FEAT_PAN3adds an EPAN bit toSCTLR_EL1andSCTLR_EL2, which allows the PAN bit to make memory non-privileged-read/write if it is user-executable as well as if it is user-read/write.Implement this feature and enable it in the AArch64 'max' CPU.
Quoted 7 lines
Signed-off-by Peter Maydell<[email protected]>--- docs/system/arm/emulation.rst | 1 + target/arm/cpu.h | 5 +++++ target/arm/cpu64.c | 2 +- target/arm/ptw.c | 14 +++++++++++++- 4 files changed, 20 insertions(+), 2 deletions(-)
r~