arm/ptw: fix missing sctlr.{u}wxn
v2 cover arm/ptw: fix missing sctlr.{u}wxn
We observed failing WXN tests in our OS which we tracked down to missing WXN handling in qemu.
The problem was in that short descriptor format walker did not respect WXN bit. To fix it, make it possible to call get_S1prot() from get_phys_addr_v6().
Tested localy that all permission-related tests in our OS passed on following platforms:
- arm64-virt
- arm-vexpress (v7)
- arm-virt (v7)
Changes from v1:
- Fix style errors
- Add cover letter
- Use get_S1prot() instead of open-coded checks
v1: https://lore.kernel.org/qemu-devel/[email protected]/
Pavel Skripkin (2):
arm/ptw: make get_S1prot accept decoded AP
arm/ptw: use get_S1prot in get_phys_addr_v6
target/arm/ptw.c | 54 +++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 30 deletions(-)
Signature · 1 line
2.46.0
PATCH v2 1/2 arm/ptw: make get_S1prot accept decoded AP
AP in armv7 short descriptor mode has 3 bits and also domain, which makes it incompatible with other arm schemas.
To make it possible to share get_S1prot between armv8, armv7 long format, armv7 short format and armv6 it's easier to make caller decode AP.
--- target/arm/ptw.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
target/arm/ptw.c +6 -7
@@ -1357,25 +1357,24 @@ static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)* @env: CPUARMState * @mmu_idx: MMU index indicating required translation regime * @is_aa64: TRUE if AArch64 -* @ap: The 2-bit simple AP (AP[2:1]) +* @user_rw: Translated AP for user access +* @prot_rw: Translated AP for privileged access * @xn: XN (execute-never) bit * @pxn: PXN (privileged execute-never) bit * @in_pa: The original input pa space * @out_pa: The output pa space, modified by NSTable, NS, and NSE */ static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64, -int ap,int xn,int pxn, +int user_rw,int prot_rw,int xn,int pxn,ARMSecuritySpace in_pa,ARMSecuritySpace out_pa) {ARMCPU *cpu =env_archcpu (env);bool is_user =regime_is_user (env, mmu_idx); -int prot_rw, user_rw;bool have_wxn;int wxn =0 ;assert (!regime_is_stage2 (mmu_idx)); - user_rw =simple_ap_to_rw_prot_is_user (ap, true);if (is_user) { prot_rw = user_rw; }else {
@@ -1393,8 +1392,6 @@ static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool 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); } }
@@ -2044,6 +2041,8 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,int nse, ns =extract32 (attrs,5 ,1 );uint8_t attrindx;uint64_t mair; +int ap_usr =simple_ap_to_rw_prot_is_user (ap, true); +int ap_priv =simple_ap_to_rw_prot_is_user (ap, false);switch (out_space) {case ARMSS_Root:
@@ -2114,7 +2113,7 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,* Note that we modified ptw->in_space earlier for NSTable, but * result->f.attrs retains a copy of the original security space. */ - result->f.prot =get_S1prot (env, mmu_idx, aarch64, ap, xn, pxn, + result->f.prot =get_S1prot (env, mmu_idx, aarch64, ap_usr, ap_priv, xn, pxn, result->f.attrs.space, out_space);/* Index into MAIR registers for cache attributes */
Signature · 1 line
2.46.0
PATCH v2 2/2 arm/ptw: use get_S1prot in get_phys_addr_v6
In reply to Pavel SkripkinTo share missing SCTRL.{U}WXN and SCR.SIF in short format walker, use get_S1prot instead of open-coded checks.
--- target/arm/ptw.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-)
target/arm/ptw.c +18 -23
@@ -85,6 +85,10 @@ static bool get_phys_addr_gpc(CPUARMState *env, S1Translate *ptw, GetPhysAddrResult *result, ARMMMUFaultInfo *fi); +static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64, +int user_rw,int prot_rw,int xn,int pxn, +ARMSecuritySpace in_pa,ARMSecuritySpace out_pa); +/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */ static const uint8_t pamax_map[] = { [0 ] =32 ,
@@ -1148,7 +1152,6 @@ static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw,hwaddr phys_addr;uint32_t dacr;bool ns; -int user_prot;/* Pagetable walk. */ /* Lookup l1 descriptor. */
@@ -1243,13 +1246,13 @@ static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw,if (domain_prot ==3 ) { result->f.prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; }else { -if (pxn && !regime_is_user (env, mmu_idx)) { - xn =1 ; - } -if (xn && access_type ==MMU_INST_FETCH ) { - fi->type = ARMFault_Permission; - goto do_fault; - } +int ap_usr; +int ap_priv; +ARMSecuritySpace out_space = ARMSS_NonSecure; + +/* NS bit is ignored in NWd. */ +if (result->f.attrs.space == ARMSS_Secure && !ns) + out_space = ARMSS_Secure;if (arm_feature (env,ARM_FEATURE_V6K ) && (regime_sctlr (env, mmu_idx) &SCTLR_AFE )) {
@@ -1259,28 +1262,20 @@ static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw, fi->type = ARMFault_AccessFlag; goto do_fault; } - result->f.prot =simple_ap_to_rw_prot (env, mmu_idx, ap >>1 ); - user_prot =simple_ap_to_rw_prot_is_user (ap >>1 ,1 ); + ap_priv =simple_ap_to_rw_prot (env, mmu_idx, ap >>1 ); + ap_usr =simple_ap_to_rw_prot_is_user (ap >>1 ,1 ); }else { - result->f.prot =ap_to_rw_prot (env, mmu_idx, ap, domain_prot); - user_prot =ap_to_rw_prot_is_user (env, mmu_idx, ap, domain_prot,1 ); - } -if (result->f.prot && !xn) { - result->f.prot |=PAGE_EXEC ; + ap_priv =ap_to_rw_prot (env, mmu_idx, ap, domain_prot); + ap_usr =ap_to_rw_prot_is_user (env, mmu_idx, ap, domain_prot,1 ); } + + result->f.prot =get_S1prot (env, mmu_idx, false, ap_usr, ap_priv, xn, pxn, + result->f.attrs.space, out_space);if (!(result->f.prot & (1 << access_type))) {/* Access permission fault. */ fi->type = ARMFault_Permission; goto do_fault; } -if (regime_is_pan (env, mmu_idx) && - !regime_is_user (env, mmu_idx) && - user_prot && - access_type !=MMU_INST_FETCH ) { -/* Privileged Access Never fault */ - fi->type = ARMFault_Permission; - goto do_fault; - } }if (ns) {/* The NS bit will (as required by the architecture) have no effect if
Signature · 1 line
2.46.0
Re: [PATCH v2 1/2] arm/ptw: make get_S1prot accept decoded AP
In reply to Pavel SkripkinQuoted 7 lines · Pavel Skripkin
AP in armv7 short descriptor mode has 3 bits and also domain, which makes it incompatible with other arm schemas.
To make it possible to share
get_S1protbetween armv8, armv7 long format, armv7 short format and armv6 it's easier to make caller decode AP.
Yep, I agree that this is the best approach to sharing the function between short and long desc.
Quoted 48 lines
Signed-off-by Pavel Skripkin <[email protected]>--- target/arm/ptw.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)target/arm/ptw.c +3
-6@@ -1357,25 +1357,24 @@ static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0)* @env: CPUARMState * @mmu_idx: MMU index indicating required translation regime * @is_aa64: TRUE if AArch64 -* @ap: The 2-bit simple AP (AP[2:1]) +* @user_rw: Translated AP for user access +* @prot_rw: Translated AP for privileged access * @xn: XN (execute-never) bit * @pxn: PXN (privileged execute-never) bit * @in_pa: The original input pa space * @out_pa: The output pa space, modified by NSTable, NS, and NSE */ static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64, -int ap,int xn,int pxn, +int user_rw,int prot_rw,int xn,int pxn,ARMSecuritySpace in_pa,ARMSecuritySpace out_pa) {ARMCPU *cpu =env_archcpu (env);bool is_user =regime_is_user (env, mmu_idx); -int prot_rw, user_rw;bool have_wxn;int wxn =0 ;assert (!regime_is_stage2 (mmu_idx)); - user_rw =simple_ap_to_rw_prot_is_user (ap, true);if (is_user) { prot_rw = user_rw; }else {@@ -1393,8 +1392,6 @@ static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool 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); } }
target/arm/ptw.c static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, · line 2045
Quoted 5 lines
target/arm/ptw.c +2
-0@@ -2044,6 +2041,8 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,int nse, ns =extract32 (attrs,5 ,1 );uint8_t attrindx;uint64_t mair; +int ap_usr =simple_ap_to_rw_prot_is_user (ap, true); +int ap_priv =simple_ap_to_rw_prot_is_user (ap, false);
Doing the calls here is too early, because after this point there is a FEAT_NV case which may change the value of ap.
Also, could we give the variables the same name as the get_S1prot() argument names, for consistency? (The return values from simple_ap_to_rw_prot_is_user() are not ap bit fields, they're rw indications, so the S1prot argument names seem more appropriate here anyway.)
target/arm/ptw.c static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw, · line 2119
Quoted 11 lines
target/arm/ptw.c +1
-1switch (out_space) {case ARMSS_Root:@@ -2114,7 +2113,7 @@ static bool get_phys_addr_lpae(CPUARMState *env, S1Translate *ptw,* Note that we modified ptw->in_space earlier for NSTable, but * result->f.attrs retains a copy of the original security space. */ - result->f.prot =get_S1prot (env, mmu_idx, aarch64, ap, xn, pxn, + result->f.prot =get_S1prot (env, mmu_idx, aarch64, ap_usr, ap_priv, xn, pxn, result->f.attrs.space, out_space);/* Index into MAIR registers for cache attributes */
thanks
-- PMM
Re: [PATCH v2 2/2] arm/ptw: use get_S1prot in get_phys_addr_v6
In reply to Pavel SkripkinQuoted 40 lines · Pavel Skripkin
To share missing SCTRL.{U}WXN and SCR.SIF in short format walker, use
get_S1protinstead of open-coded checks.Signed-off-by Pavel Skripkin <[email protected]>--- target/arm/ptw.c | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-)target/arm/ptw.c +4
-5@@ -85,6 +85,10 @@ static bool get_phys_addr_gpc(CPUARMState *env, S1Translate *ptw, GetPhysAddrResult *result, ARMMMUFaultInfo *fi); +static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64, +int user_rw,int prot_rw,int xn,int pxn, +ARMSecuritySpace in_pa,ARMSecuritySpace out_pa); +/* This mapping is common between ID_AA64MMFR0.PARANGE and TCR_ELx.{I}PS. */ static const uint8_t pamax_map[] = { [0 ] =32 ,@@ -1148,7 +1152,6 @@ static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw,hwaddr phys_addr;uint32_t dacr;bool ns; -int user_prot;/* Pagetable walk. */ /* Lookup l1 descriptor. */ @@ -1243,13 +1246,13 @@ static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw,if (domain_prot ==3 ) { result->f.prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; }else { -if (pxn && !regime_is_user (env, mmu_idx)) { - xn =1 ; - } -if (xn && access_type ==MMU_INST_FETCH ) {
target/arm/ptw.c static bool get_phys_addr_v6(CPUARMState *env, S1Translate *ptw, · line 1255
Quoted 10 lines
target/arm/ptw.c +7
-3- fi->type = ARMFault_Permission; - goto do_fault; - } +int ap_usr; +int ap_priv; +ARMSecuritySpace out_space = ARMSS_NonSecure; + +/* NS bit is ignored in NWd. */ +if (result->f.attrs.space == ARMSS_Secure && !ns) + out_space = ARMSS_Secure;
Instead of doing this, I think we should align the code a bit more closely to how we do it in the lpae codepath. So before "if (domain_prot == 3)" put
out_space = ptw->in_space;
if (ns) {
/*
* The NS bit will (as required by the architecture) have no effect if
* the CPU doesn't support TZ or this is a non-secure translation
* regime, because the output space will already be non-secure.
*/
out_space = ARMSS_NonSecure;
}and then delete the current "if (ns)" code block at the end of get_phys_addr_v6() and replace it with an unconditional
result->f.attrs.space = out_space;
result->f.attrs.secure = arm_space_is_secure(out_space);That way we're determining the output space in exactly one place, and the code at the end that updates result->f.attrs is the same as the way we do it in the lpae function.
(PS: note that QEMU coding style mandates braces on if() statements even if there is just one statement in the body.)
Otherwise I think this looks good -- thanks for doing the refactoring.
-- PMM