tcg-arm: LPAE: fix and extend xn control
v4 cover tcg-arm: LPAE: fix and extend xn control
This series fixes and extends the determination of whether or not an address is executable for LPAE translations. The main patch is 3/3, and describes the details in its commit message. Patch 1/3 prepares for patch 2/3, which is prep for 3/3, and also fixes a potential problem with checking access permissions on v6 processors when they use the simple AP format.
Changes since v3:
* Remove a useless is_aa64/case1/is_user code setting wxn=0 [Peter]
Changes since v2:
* Collect NSTable bits into ns
* Explicitly check {prot,user}_rw against PAGE_READ. !{prot,user}_rw means no read or write, but, since we can't have write without read, it also means !({prot,user}_rw & PAGE_READ). Making this explicit can reduce confusion when trying to determine with prot flag we actually care about. [Peter Maydell]
Changes since v1:
* There are now 3 patches instead of 5. This is due to approaching the prep patches in a different way. Most notably, simple AP format is now handled with its own function. [Peter Maydell]
* A check of SCTLR_AFE is guarded with ARM_FEATURE_V6K [Peter Maydell]
* All other cleanups suggested by Peter Maydell.
Tested by booting Linux on mach-virt with cortex-a15 and cortex-a57 (just up to 'Unable to mount root fs'), and also with a kvm-unit-tests test. The curious can check out the unit test here [1].
Thanks in advance for reviews!
drew
[1] https://github.com/rhdrjones/kvm-unit-tests/commit/ee553e4bb795b0150e31c794bf8953dfb08d619a
Andrew Jones (3):
target-arm: convert check_ap to ap_to_rw_prot
target-arm: fix get_phys_addr_v6/SCTLR_AFE access check
target-arm: get_phys_addr_lpae: more xn control
target-arm/helper.c | 214 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 154 insertions(+), 60 deletions(-)
Signature · 1 line
1.9.3
PATCH v4 1/3 target-arm: convert check_ap to ap_to_rw_prot
Instead of mixing access permission checking with access permissions to page protection flags translation, just do the translation, and leave it to the caller to check the protection flags against the access type. Also rename to ap_to_rw_prot to better describe the new behavior.
--- target-arm/helper.c | 49 +++++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 30 deletions(-)
target-arm/helper.c +19 -30
@@ -4903,34 +4903,23 @@ static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) } } -/* Check section/page access permissions. -Returns the page protection flags, or zero if the access is not -permitted. */ -static inline int check_ap (CPUARMState *env,ARMMMUIdx mmu_idx, -int ap,int domain_prot, -int access_type) -{ -int prot_ro; +/* Translate section/page access permissions to page +* R/W protection flags +*/ +static inline int ap_to_rw_prot (CPUARMState *env,ARMMMUIdx mmu_idx, +int ap,int domain_prot) +{bool is_user =regime_is_user (env, mmu_idx);if (domain_prot ==3 ) {return PAGE_READ |PAGE_WRITE ; } -if (access_type ==1 ) { - prot_ro =0 ; - }else { - prot_ro =PAGE_READ ; - } -switch (ap) {case 0 :if (arm_feature (env,ARM_FEATURE_V7 )) {return 0 ; } -if (access_type ==1 ) { -return 0 ; - }switch (regime_sctlr (env, mmu_idx) & (SCTLR_S |SCTLR_R )) {case SCTLR_S :return is_user ?0 :PAGE_READ ;
@@ -4943,7 +4932,7 @@ static inline int check_ap(CPUARMState *env, ARMMMUIdx mmu_idx,return is_user ?0 :PAGE_READ |PAGE_WRITE ;case 2 :if (is_user) { -return prot_ro; +return PAGE_READ ; }else {return PAGE_READ |PAGE_WRITE ; }
@@ -4952,16 +4941,16 @@ static inline int check_ap(CPUARMState *env, ARMMMUIdx mmu_idx,case 4 :/* Reserved. */ return 0 ;case 5 : -return is_user ?0 : prot_ro; +return is_user ?0 :PAGE_READ ;case 6 : -return prot_ro; +return PAGE_READ ;case 7 :if (!arm_feature (env,ARM_FEATURE_V6K )) {return 0 ; } -return prot_ro; +return PAGE_READ ;default : -abort (); +g_assert_not_reached (); } }
@@ -5083,12 +5072,12 @@ static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type, } code =15 ; } - *prot =check_ap (env, mmu_idx, ap, domain_prot, access_type); -if (!*prot) { + *prot =ap_to_rw_prot (env, mmu_idx, ap, domain_prot); + *prot |= *prot ?PAGE_EXEC :0 ; +if (!(*prot & (1 << access_type))) {/* Access permission fault. */ goto do_fault; } - *prot |=PAGE_EXEC ; *phys_ptr = phys_addr;return 0 ; do_fault:
@@ -5204,14 +5193,14 @@ static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type, code = (code ==15 ) ?6 :3 ; goto do_fault; } - *prot =check_ap (env, mmu_idx, ap, domain_prot, access_type); -if (!*prot) { + *prot =ap_to_rw_prot (env, mmu_idx, ap, domain_prot); +if (*prot && !xn) { + *prot |=PAGE_EXEC ; + } +if (!(*prot & (1 << access_type))) {/* Access permission fault. */ goto do_fault; } -if (!xn) { - *prot |=PAGE_EXEC ; - } } *phys_ptr = phys_addr;return 0 ;
Signature · 1 line
1.9.3
PATCH v4 2/3 target-arm: fix get_phys_addr_v6/SCTLR_AFE access check
In reply to Andrew JonesIntroduce simple_ap_to_rw_prot(), which has the same behavior as ap_to_rw_prot(), but takes the 2-bit simple AP[2:1] instead of the 3-bit AP[2:0]. Use this in get_phys_addr_v6 when SCTLR_AFE is set, as that bit indicates we should be using the simple AP format.
It's unlikely this path is getting used. I don't see CR_AFE getting used by Linux, so possibly not. If it had been, then the check would have been wrong for all but AP[2:1] = 0b11. Anyway, this should fix it up, in case it ever does get used.
--- target-arm/helper.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 7 deletions(-)
target-arm/helper.c +42 -7
@@ -4905,6 +4905,11 @@ static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)/* Translate section/page access permissions to page * R/W protection flags +* +* @env: CPUARMState +* @mmu_idx: MMU index indicating required translation regime +* @ap: The 3-bit access permissions (AP[2:0]) +* @domain_prot: The 2-bit domain access permissions */ static inline int ap_to_rw_prot (CPUARMState *env,ARMMMUIdx mmu_idx,int ap,int domain_prot)
@@ -4954,6 +4959,32 @@ static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, } } +/* Translate section/page access permissions to page +* R/W protection flags. +* +* @env: CPUARMState +* @mmu_idx: MMU index indicating required translation regime +* @ap: The 2-bit simple AP (AP[2:1]) +*/ +static inline int +simple_ap_to_rw_prot (CPUARMState *env,ARMMMUIdx mmu_idx,int ap) +{ +bool is_user =regime_is_user (env, mmu_idx); + +switch (ap) { +case 0 : +return is_user ?0 :PAGE_READ |PAGE_WRITE ; +case 1 : +return PAGE_READ |PAGE_WRITE ; +case 2 : +return is_user ?0 :PAGE_READ ; +case 3 : +return PAGE_READ ; +default : +g_assert_not_reached (); + } +} +static bool get_level1_table_address (CPUARMState *env,ARMMMUIdx mmu_idx,uint32_t *table,uint32_t address) {
@@ -5186,14 +5217,18 @@ static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,if (xn && access_type ==2 ) goto do_fault; -/* The simplified model uses AP[0] as an access control bit. */ -if ((regime_sctlr (env, mmu_idx) &SCTLR_AFE ) - && (ap &1 ) ==0 ) { -/* Access flag fault. */ - code = (code ==15 ) ?6 :3 ; - goto do_fault; +if (arm_feature (env,ARM_FEATURE_V6K ) && + (regime_sctlr (env, mmu_idx) &SCTLR_AFE )) { +/* The simplified model uses AP[0] as an access control bit. */ +if ((ap &1 ) ==0 ) { +/* Access flag fault. */ + code = (code ==15 ) ?6 :3 ; + goto do_fault; + } + *prot =simple_ap_to_rw_prot (env, mmu_idx, ap >>1 ); + }else { + *prot =ap_to_rw_prot (env, mmu_idx, ap, domain_prot); } - *prot =ap_to_rw_prot (env, mmu_idx, ap, domain_prot);if (*prot && !xn) { *prot |=PAGE_EXEC ; }
Signature · 1 line
1.9.3
PATCH v4 3/3 target-arm: get_phys_addr_lpae: more xn control
In reply to Andrew JonesThis patch makes the following changes to the determination of whether an address is executable, when translating addresses using LPAE.
1. No longer assumes that PL0 can't execute when it can't read. It can in AArch64, a difference from AArch32.
2. Use va_size == 64 to determine we're in AArch64, rather than arm_feature(env, ARM_FEATURE_V8), which is insufficient.
3. Add additional XN determinants
- NS && is_secure && (SCR & SCR_SIF)
- WXN && (prot & PAGE_WRITE)
- AArch64: (prot_PL0 & PAGE_WRITE)
- AArch32: UWXN && (prot_PL0 & PAGE_WRITE)
- XN determination should also work in secure mode (untested)
- XN may even work in EL2 (currently impossible to test)
4. Cleans up the bloated PAGE_EXEC condition - by removing it.
The helper get_S1prot is introduced. It may even work in EL2, when support for that comes, but, as the function name implies, it only works for stage 1 translations.
--- target-arm/helper.c | 130 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 100 insertions(+), 30 deletions(-)
target-arm/helper.c +100 -30
@@ -4962,15 +4962,11 @@ static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,/* Translate section/page access permissions to page * R/W protection flags. * -* @env: CPUARMState -* @mmu_idx: MMU index indicating required translation regime * @ap: The 2-bit simple AP (AP[2:1]) +* @is_user: TRUE if accessing from PL0 */ -static inline int -simple_ap_to_rw_prot (CPUARMState *env,ARMMMUIdx mmu_idx,int ap) +static inline int simple_ap_to_rw_prot_is_user (int ap,bool is_user) { -bool is_user =regime_is_user (env, mmu_idx); -switch (ap) {case 0 :return is_user ?0 :PAGE_READ |PAGE_WRITE ;
@@ -4985,6 +4981,93 @@ simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) } } +static inline int +simple_ap_to_rw_prot (CPUARMState *env,ARMMMUIdx mmu_idx,int ap) +{ +return simple_ap_to_rw_prot_is_user (ap,regime_is_user (env, mmu_idx)); +} + +/* Translate section/page access permissions to protection flags +* +* @env: CPUARMState +* @mmu_idx: MMU index indicating required translation regime +* @is_aa64: TRUE if AArch64 +* @ap: The 2-bit simple AP (AP[2:1]) +* @ns: NS (non-secure) bit +* @xn: XN (execute-never) bit +* @pxn: PXN (privileged execute-never) bit +*/ +static int get_S1prot (CPUARMState *env,ARMMMUIdx mmu_idx,bool is_aa64, +int ap,int ns,int xn,int pxn) +{ +bool is_user =regime_is_user (env, mmu_idx); +int prot_rw, user_rw; +bool have_wxn; +int wxn =0 ; + +assert (mmu_idx != ARMMMUIdx_S2NS); + + user_rw =simple_ap_to_rw_prot_is_user (ap, true); +if (is_user) { + prot_rw = user_rw; + }else { + prot_rw =simple_ap_to_rw_prot_is_user (ap, false); + } + +if (ns &&arm_is_secure (env) && (env->cp15.scr_el3 &SCR_SIF )) { +return prot_rw; + } + +/* TODO have_wxn should be replaced with +* ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) +* when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE +* compatible processors have EL2, which is required for [U]WXN. +*/ + have_wxn =arm_feature (env,ARM_FEATURE_LPAE ); + +if (have_wxn) { + wxn =regime_sctlr (env, mmu_idx) &SCTLR_WXN ; + } + +if (is_aa64) { +switch (regime_el (env, mmu_idx)) { +case 1 : +if (!is_user) { + xn = pxn || (user_rw &PAGE_WRITE ); + } +break ; +case 2 : +case 3 : +break ; + } + }else if (arm_feature (env,ARM_FEATURE_V7 )) { +switch (regime_el (env, mmu_idx)) { +case 1 : +case 3 : +if (is_user) { + xn = xn || !(user_rw &PAGE_READ ); + }else { +int uwxn =0 ; +if (have_wxn) { + uwxn =regime_sctlr (env, mmu_idx) &SCTLR_UWXN ; + } + xn = xn || !(prot_rw &PAGE_READ ) || pxn || + (uwxn && (user_rw &PAGE_WRITE )); + } +break ; +case 2 : +break ; + } + }else { + xn = wxn =0 ; + } + +if (xn || (wxn && (prot_rw &PAGE_WRITE ))) { +return prot_rw; + } +return prot_rw |PAGE_EXEC ; +} +static bool get_level1_table_address (CPUARMState *env,ARMMMUIdx mmu_idx,uint32_t *table,uint32_t address) {
@@ -5273,8 +5356,8 @@ static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,int32_t granule_sz =9 ;int32_t va_size =32 ;int32_t tbi =0 ; -bool is_user;TCR *tcr =regime_tcr (env, mmu_idx); +int ap, ns, xn, pxn;/* TODO: * This code assumes we're either a 64-bit EL1 or a 32-bit PL1;
@@ -5435,7 +5518,7 @@ static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,if (extract32 (tableattrs,2 ,1 )) { attrs &= ~(1 <<4 ); } -/* Since we're always in the Non-secure state, NSTable is ignored. */ + attrs |=extract32 (tableattrs,4 ,1 ) <<3 ;/* NS */ break ; }/* Here descaddr is the final physical address, and attributes
@@ -5446,31 +5529,18 @@ static int get_phys_addr_lpae(CPUARMState *env, target_ulong address,/* Access flag */ goto do_fault; } + + ap =extract32 (attrs,4 ,2 ); + ns =extract32 (attrs,3 ,1 ); + xn =extract32 (attrs,12 ,1 ); + pxn =extract32 (attrs,11 ,1 ); + + *prot =get_S1prot (env, mmu_idx, va_size ==64 , ap, ns, xn, pxn); + fault_type = permission_fault; - is_user =regime_is_user (env, mmu_idx); -if (is_user && !(attrs & (1 <<4 ))) { -/* Unprivileged access not enabled */ +if (!(*prot & (1 << access_type))) { goto do_fault; } - *prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; -if ((arm_feature (env,ARM_FEATURE_V8 ) && is_user && (attrs & (1 <<12 ))) || - (!arm_feature (env,ARM_FEATURE_V8 ) && (attrs & (1 <<12 ))) || - (!is_user && (attrs & (1 <<11 )))) { -/* XN/UXN or PXN. Since we only implement EL0/EL1 we unconditionally -* treat XN/UXN as UXN for v8. -*/ -if (access_type ==2 ) { - goto do_fault; - } - *prot &= ~PAGE_EXEC ; - } -if (attrs & (1 <<5 )) { -/* Write access forbidden */ -if (access_type ==1 ) { - goto do_fault; - } - *prot &= ~PAGE_WRITE ; - } *phys_ptr = descaddr; *page_size_ptr = page_size;
Signature · 1 line
1.9.3
Quoted 31 lines · Andrew Jones
This series fixes and extends the determination of whether or not an address is executable for LPAE translations. The main patch is 3/3, and describes the details in its commit message. Patch 1/3 prepares for patch 2/3, which is prep for 3/3, and also fixes a potential problem with checking access permissions on v6 processors when they use the simple AP format.
Changes since v3:
* Remove a useless is_aa64/case1/is_user code setting wxn=0 [Peter]Changes since v2:
* Collect NSTable bits into ns
* Explicitly check {prot,user}_rw againstPAGE_READ. !{prot,user}_rw means no read or write, but, since we can't have write without read, it also means !({prot,user}_rw &PAGE_READ). Making this explicit can reduce confusion when trying to determine with prot flag we actually care about. [Peter Maydell]Changes since v1:
* There are now 3 patches instead of 5. This is due to approaching the prep patches in a different way. Most notably, simple AP format is now handled with its own function. [Peter Maydell]
* A check ofSCTLR_AFEis guarded withARM_FEATURE_V6K[Peter Maydell]
* All other cleanups suggested by Peter Maydell.Tested by booting Linux on mach-virt with cortex-a15 and cortex-a57 (just up to 'Unable to mount root fs'), and also with a kvm-unit-tests test. The curious can check out the unit test here [1].
Thanks in advance for reviews!
Quoted 8 lines
drew
[1] https://github.com/rhdrjones/kvm-unit-tests/commit/ee553e4bb795b0150e31c794bf8953dfb08d619a
Andrew Jones (3):
target-arm: convertcheck_aptoap_to_rw_prot
target-arm: fix get_phys_addr_v6/SCTLR_AFE access check
target-arm:get_phys_addr_lpae: more xn control
Thanks; applied all to target-arm.next. Since this is a bugfix I'm aiming it to go into 2.3.
-- PMM