target/arm: Enforce implemented granule size limits
cover target/arm: Enforce implemented granule size limits
Arm CPUs support some subset of the granule (page) sizes 4K, 16K and 64K. The guest selects the one it wants using bits in the TCR_ELx registers. If it tries to program these registers with a value that is either reserved or which requests a size that the CPU does not implement, the architecture requires that the CPU behaves as if the field was programmed to some size that has been implemented. Currently we don't implement this, and instead let the guest use any granule size, even if the CPU ID register fields say it isn't present.
Patch 1 in this series makes us enforce this architectural requirement (the main effect will be that we stop incorrectly implementing 16K granules on most of the non-cpu-max CPUs).
Patch 2 adds FEAT_GTG to the list of supported features, because all this feature really is is the definition of the separate fields for stage1 and stage2 granule support in ID_AA64MMFR0_EL1, and we already updated -cpu max to report its granule support that way when we were adding the LPA2 support.
thanks
-- PMM
Peter Maydell (2):
target/arm: Don't allow guest to use unimplemented granule sizes docs/system/arm/emulation.rst: Report FEAT_GTG support
docs/system/arm/emulation.rst | 1 + target/arm/cpu.h | 33 ++++++++++ target/arm/helper.c | 110 +++++++++++++++++++++++++++++++--- 3 files changed, 136 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
PATCH 1/2 target/arm: Don't allow guest to use unimplemented granule sizes
Arm CPUs support some subset of the granule (page) sizes 4K, 16K and 64K. The guest selects the one it wants using bits in the TCR_ELx registers. If it tries to program these registers with a value that is either reserved or which requests a size that the CPU does not implement, the architecture requires that the CPU behaves as if the field was programmed to some size that has been implemented. Currently we don't implement this, and instead let the guest use any granule size, even if the CPU ID register fields say it isn't present.
Make aa64_va_parameters() check against the supported granule size and force use of a different one if it is not implemented.
---
Unusually, the architecture would allow us to do this sanitizing when the TCR_ELx register is written, because it permits that the value of the register read back can be one corresponding to the IMPDEF chosen size rather than having to be the value written. But I opted to do the handling in aa64_va_parameters() anyway, on the assumption that this isn't critically in the fast path.
--- target/arm/cpu.h | 33 +++++++++++++ target/arm/helper.c | 110 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 135 insertions(+), 8 deletions(-)
target/arm/cpu.h +33 -0
@@ -4103,6 +4103,39 @@ static inline bool isar_feature_aa64_tgran16_2_lpa2(const ARMISARegisters *id)return t >=3 || (t ==0 &&isar_feature_aa64_tgran16_lpa2 (id)); } +static inline bool isar_feature_aa64_tgran4 (const ARMISARegisters *id) +{ +return FIELD_SEX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN4 ) >=0 ; +} + +static inline bool isar_feature_aa64_tgran16 (const ARMISARegisters *id) +{ +return FIELD_EX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN16 ) >=1 ; +} + +static inline bool isar_feature_aa64_tgran64 (const ARMISARegisters *id) +{ +return FIELD_SEX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN64 ) >=0 ; +} + +static inline bool isar_feature_aa64_tgran4_2 (const ARMISARegisters *id) +{ +unsigned t =FIELD_EX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN4_2 ); +return t >=2 || (t ==0 &&isar_feature_aa64_tgran4 (id)); +} + +static inline bool isar_feature_aa64_tgran16_2 (const ARMISARegisters *id) +{ +unsigned t =FIELD_EX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN16_2 ); +return t >=2 || (t ==0 &&isar_feature_aa64_tgran16 (id)); +} + +static inline bool isar_feature_aa64_tgran64_2 (const ARMISARegisters *id) +{ +unsigned t =FIELD_EX64 (id->id_aa64mmfr0,ID_AA64MMFR0 ,TGRAN64_2 ); +return t >=2 || (t ==0 &&isar_feature_aa64_tgran64 (id)); +} +static inline bool isar_feature_aa64_ccidx (const ARMISARegisters *id) {return FIELD_EX64 (id->id_aa64mmfr2,ID_AA64MMFR2 ,CCIDX ) !=0 ;
target/arm/helper.c +102 -8
144 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 1/2] target/arm: Don't allow guest to use unimplemented granule sizes
target/arm/helper.c static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) · line 10298
Quoted 11 lines · Peter Maydell
target/arm/helper.c +7
-0@@ -10289,20 +10289,113 @@ static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) } } +typedef enum GranuleSize { +/* Same order as TG0 encoding */ + Gran4K, + Gran64K, + Gran16K, + GranInvalid, +}GranuleSize ;
It might be worth using this in ARMVAParameters. Even if you don't do that now, it would be worth putting this typedef in internals.h.
Otherwise,
r~
Re: [PATCH 1/2] target/arm: Don't allow guest to use unimplemented granule sizes
Quoted 6 lines · Richard Henderson
Quoted 4 lines · Peter Maydell
(quoted diff) +0
-0@@ -10289,20 +10289,113 @@ static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) } }
Quoted 10 lines
target/arm/helper.c static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) · line 10298
Quoted 7 lines
target/arm/helper.c +7
-0+typedef enum GranuleSize { +/* Same order as TG0 encoding */ + Gran4K, + Gran64K, + Gran16K, + GranInvalid, +}GranuleSize ;It might be worth using this in ARMVAParameters. Even if you don't do that now, it would be worth putting this typedef in
internals.h.
I'd assumed you had a reason for using two bools rather than an enum in ARMVAParameters, so I left that alone :-) But it would make more sense to consistently use GranuleSize. (I guess for putting it in internals.h it ought to be named ARMGranSize.)
Putting GranuleSize into ARMVAParameters brings us into the area of enums in bitfields, where I'm not sure exactly what the standard says or our supported compilers allow as an impdef extension.
GranuleSize gsize : 2;
seems to work, but is it portable? Or should I just put
GranuleSize gsize;
and accept that that means the ARMVAParameters goes from 4 bytes to 8 bytes in size?
thanks
-- PMM
Re: [PATCH 1/2] target/arm: Don't allow guest to use unimplemented granule sizes
Quoted 2 lines · Peter Maydell
I'd assumed you had a reason for using two bools rather than an enum in ARMVAParameters, so I left that alone :-)
I think it was because the existing code had variables of those names, so the patch changed "foo" to "param.foo". :-)
Quoted 6 lines
Putting GranuleSize into ARMVAParameters brings us into the area of enums in bitfields, where I'm not sure exactly what the standard says or our supported compilers allow as an impdef extension.
GranuleSize gsize : 2;
seems to work, but is it portable?
I think so. Leastwise, we use those elsewhere. I would certainly expect a warning from any of our supported compilers if we were to use too few bits for the enum type.
r~
PATCH 2/2 docs/system/arm/emulation.rst: Report FEAT_GTG support
In reply to Peter MaydellFEAT_GTG is a change tho the ID register ID_AA64MMFR0_EL1 so that it can report a different set of supported granule (page) sizes for stage 1 and stage 2 translation tables. As of commit c20281b2a5048 we already report the granule sizes that way for '-cpu max', and now we also correctly make attempts to use unimplemented granule sizes fail, so we can report the support of the feature in the documentation.
--- docs/system/arm/emulation.rst | 1 + 1 file changed, 1 insertion(+)
docs/system/arm/emulation.rst +1 -0
@@ -31,6 +31,7 @@ the following architecture extensions: - FEAT_FRINTTS (Floating-point to integer instructions) - FEAT_FlagM (Flag manipulation instructions v2) - FEAT_FlagM2 (Enhancements to flag manipulation instructions) +- FEAT_GTG (Guest translation granule size) - FEAT_HCX (Support for the HCRX_EL2 register) - FEAT_HPDS (Hierarchical permission disables) - FEAT_I8MM (AArch64 Int8 matrix multiplication instructions)
Signature · 1 line
2.25.1
Re: [PATCH 2/2] docs/system/arm/emulation.rst: Report FEAT_GTG support
Quoted 8 lines · Peter Maydell
FEAT_GTGis a change tho the ID registerID_AA64MMFR0_EL1so that it can report a different set of supported granule (page) sizes for stage 1 and stage 2 translation tables. As of commitc20281b2a5048we already report the granule sizes that way for '-cpu max', and now we also correctly make attempts to use unimplemented granule sizes fail, so we can report the support of the feature in the documentation.
Quoted 4 lines
Signed-off-by Peter Maydell<[email protected]>--- docs/system/arm/emulation.rst | 1 + 1 file changed, 1 insertion(+)
r~