KVM : selftests: arm64: Explicitly set the page attrs to Inner-Shareable
cover KVM : selftests: arm64: Explicitly set the page attrs to Inner-Shareable
The series fixes a conflict in memory attributes in some implementations, such as Neoverse-N3, that causes a data abort in guest EL1 with FSC 0x35 (IMPLEMENTATION DEFINED fault (Unsupported Exclusive or Atomic access)).
Patch-1 is a cleanup patch that replaces numbers (and comments) to using proper macros for hardware configuration, such as registers and page-table entries.
Patch-2 fixes the actual bug and sets the page attrs to Inner-Shareable by default for the VMs created in the selftests. More details are presented in the commit text.
Raghavendra Rao Ananta (2):
KVM: selftests: arm64: Introduce and use hardware-definition macros
KVM: selftests: arm64: Explicitly set the page attrs to
Inner-Shareable
tools/arch/arm64/include/asm/sysreg.h | 38 ++++++++++++ .../selftests/kvm/arm64/page_fault_test.c | 2 +- .../selftests/kvm/include/arm64/processor.h | 29 +++++++-- .../selftests/kvm/lib/arm64/processor.c | 60 +++++++++++-------- 4 files changed, 96 insertions(+), 33 deletions(-)
base-commit: 38fec10eb60d687e30c8c6b5420d86e8149f7557
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
PATCH 1/2 KVM: selftests: arm64: Introduce and use hardware-definition macros
The kvm selftest library for arm64 currently configures the hardware fields, such as shift and mask in the page-table entries and registers, directly with numbers. While it add comments at places, it's better to rewrite them with appropriate macros to improve the readability and reduce the risk of errors. Hence, introduce macros to define the hardware fields and use them in the arm64 processor library.
Most of the definitions are primary copied from the Linux's header, arch/arm64/include/asm/pgtable-hwdef.h.
No functional change intended.
--- tools/arch/arm64/include/asm/sysreg.h | 38 +++++++++++++ .../selftests/kvm/arm64/page_fault_test.c | 2 +- .../selftests/kvm/include/arm64/processor.h | 28 +++++++-- .../selftests/kvm/lib/arm64/processor.c | 57 ++++++++++--------- 4 files changed, 92 insertions(+), 33 deletions(-)
tools/arch/arm64/include/asm/sysreg.h +38 -0
@@ -884,6 +884,44 @@ SCTLR_EL1_LSMAOE | SCTLR_EL1_nTLSMD | SCTLR_EL1_EIS | \ SCTLR_EL1_TSCXT | SCTLR_EL1_EOS) +/* TCR_EL1 specific flags */ +#define TCR_T0SZ_OFFSET 0 +#define TCR_T0SZ (x) ((UL(64) - (x)) << TCR_T0SZ_OFFSET) + +#define TCR_IRGN0_SHIFT 8 +#define TCR_IRGN0_MASK (UL(3) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_NC (UL(0) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WBWA (UL(1) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WT (UL(2) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WBnWA (UL(3) << TCR_IRGN0_SHIFT) + +#define TCR_ORGN0_SHIFT 10 +#define TCR_ORGN0_MASK (UL(3) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_NC (UL(0) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WBWA (UL(1) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WT (UL(2) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WBnWA (UL(3) << TCR_ORGN0_SHIFT) + +#define TCR_SH0_SHIFT 12 +#define TCR_SH0_MASK (UL(3) << TCR_SH0_SHIFT) +#define TCR_SH0_INNER (UL(3) << TCR_SH0_SHIFT) + +#define TCR_TG0_SHIFT 14 +#define TCR_TG0_MASK (UL(3) << TCR_TG0_SHIFT) +#define TCR_TG0_4K (UL(0) << TCR_TG0_SHIFT) +#define TCR_TG0_64K (UL(1) << TCR_TG0_SHIFT) +#define TCR_TG0_16K (UL(2) << TCR_TG0_SHIFT) + +#define TCR_IPS_SHIFT 32 +#define TCR_IPS_MASK (UL(7) << TCR_IPS_SHIFT) +#define TCR_IPS_52_BITS (UL(6) << TCR_IPS_SHIFT) +#define TCR_IPS_48_BITS (UL(5) << TCR_IPS_SHIFT) +#define TCR_IPS_40_BITS (UL(2) << TCR_IPS_SHIFT) +#define TCR_IPS_36_BITS (UL(1) << TCR_IPS_SHIFT) + +#define TCR_HA (UL(1) << 39) +#define TCR_DS (UL(1) << 59) +/* MAIR_ELx memory attributes (used by Linux) */ #define MAIR_ATTR_DEVICE_nGnRnE UL(0x00)#define MAIR_ATTR_DEVICE_nGnRE UL(0x04)
tools/testing/selftests/kvm/arm64/page_fault_test.c +1 -1
@@ -199,7 +199,7 @@ static bool guest_set_ha(void)if (hadbs ==0 )return false; - tcr =read_sysreg (tcr_el1) |TCR_EL1_HA ; + tcr =read_sysreg (tcr_el1) |TCR_HA ;write_sysreg (tcr, tcr_el1);isb ();
tools/testing/selftests/kvm/include/arm64/processor.h +22 -6
@@ -62,6 +62,28 @@ MAIR_ATTRIDX(MAIR_ATTR_NORMAL, MT_NORMAL) | \ MAIR_ATTRIDX(MAIR_ATTR_NORMAL_WT, MT_NORMAL_WT)) +/* +* AttrIndx[2:0] encoding (mapping attributes defined in the MAIR* registers). +*/ +#define PTE_ATTRINDX (t) ((t) << 2) +#define PTE_ATTRINDX_MASK GENMASK(4, 2) +#define PTE_ATTRINDX_SHIFT 2 + +#define PTE_VALID BIT(0) +#define PGD_TYPE_TABLE BIT(1) +#define PUD_TYPE_TABLE BIT(1) +#define PMD_TYPE_TABLE BIT(1) +#define PTE_TYPE_PAGE BIT(1) + +#define PTE_AF BIT(10) + +#define PTE_ADDR_MASK (page_shift) GENMASK(47, (page_shift)) +#define PTE_ADDR_51_48 GENMASK(15, 12) +#define PTE_ADDR_51_48_SHIFT 12 +#define PTE_ADDR_MASK_LPA2 (page_shift) GENMASK(49, (page_shift)) +#define PTE_ADDR_51_50_LPA2 GENMASK(9, 8) +#define PTE_ADDR_51_50_LPA2_SHIFT 8 +void aarch64_vcpu_setup (struct kvm_vcpu *vcpu,struct kvm_vcpu_init *init);struct kvm_vcpu *aarch64_vcpu_add (struct kvm_vm *vm,uint32_t vcpu_id,struct kvm_vcpu_init *init,void *guest_code);
@@ -102,12 +124,6 @@ enum { (v) == VECTOR_SYNC_LOWER_64 || \ (v) == VECTOR_SYNC_LOWER_32) -/* Access flag */ -#define PTE_AF (1ULL << 10) - -/* Access flag update enable/disable */ -#define TCR_EL1_HA (1ULL << 39) -void aarch64_get_supported_page_sizes (uint32_t ipa,uint32_t *ipa4k,uint32_t *ipa16k,uint32_t *ipa64k);
tools/testing/selftests/kvm/lib/arm64/processor.c +31 -26
143 lines not on this page · raw message
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
PATCH 2/2 KVM: selftests: arm64: Explicitly set the page attrs to Inner-Shareable
In reply to Raghavendra Rao AnantaAtomic instructions such as 'ldset' over (global) variables in the guest is observed to cause an EL1 data abort with FSC 0x35 (IMPLEMENTATION DEFINED fault (Unsupported Exclusive or Atomic access)). The observation was particularly apparent on Neoverse-N3.
According to DDI0487L.a C3.2.6 (Single-copy atomic 64-byte load/store), it is implementation defined that a data abort with the mentioned FSC is reported for the first stage of translation that provides an inappropriate memory type. It's likely that the same rule also applies to memory attribute mismatch. When the guest loads the memory location of the variable that was already cached during the host userspace's copying of the ELF into the memory, the core is likely running into a mismatch of memory attrs that's checked in stage-1 itself, and thus causing the abort in EL1.
Fix this by explicitly setting the memory attribute to Inner-Shareable to avoid the mismatch, and by extension, the data abort.
--- tools/testing/selftests/kvm/include/arm64/processor.h | 1 + tools/testing/selftests/kvm/lib/arm64/processor.c | 3 +++ 2 files changed, 4 insertions(+)
tools/testing/selftests/kvm/include/arm64/processor.h +1 -0
@@ -75,6 +75,7 @@#define PMD_TYPE_TABLE BIT(1)#define PTE_TYPE_PAGE BIT(1) +#define PTE_SHARED (UL(3) << 8)/* SH[1:0], inner shareable */ #define PTE_AF BIT(10)#define PTE_ADDR_MASK (page_shift) GENMASK(47, (page_shift))
tools/testing/selftests/kvm/lib/arm64/processor.c +3 -0
@@ -172,6 +172,9 @@ static void _virt_pg_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, } pg_attr =PTE_AF |PTE_ATTRINDX (attr_idx) |PTE_TYPE_PAGE |PTE_VALID ; +if (!use_lpa2_pte_format (vm)) + pg_attr |=PTE_SHARED ; + *ptep =addr_pte (vm, paddr, pg_attr); }
Signature · 1 line
2.49.0.504.g3bcea36a83-goog
Re: [PATCH 1/2] KVM: selftests: arm64: Introduce and use hardware-definition macros
In reply to Raghavendra Rao AnantaHi Raghu,
Quoted 9 lines · Raghavendra Rao Ananta
The kvm selftest library for arm64 currently configures the hardware fields, such as shift and mask in the page-table entries and registers, directly with numbers. While it add comments at places, it's better to rewrite them with appropriate macros to improve the readability and reduce the risk of errors. Hence, introduce macros to define the hardware fields and use them in the arm64 processor library.
Most of the definitions are primary copied from the Linux's header,
arch/arm64/include/asm/pgtable-hwdef.h.
Thank you for doing this. Having magic numbers all around the shop was a complete mess. Just a single comment:
Quoted 47 lines
No functional change intended.
Suggested-by Oliver Upton <[email protected]>Signed-off-by Raghavendra Rao Ananta <[email protected]>--- tools/arch/arm64/include/asm/sysreg.h | 38 +++++++++++++ .../selftests/kvm/arm64/page_fault_test.c | 2 +- .../selftests/kvm/include/arm64/processor.h | 28 +++++++-- .../selftests/kvm/lib/arm64/processor.c | 57 ++++++++++--------- 4 files changed, 92 insertions(+), 33 deletions(-)tools/arch/arm64/include/asm/sysreg.h +28
-0@@ -884,6 +884,44 @@ SCTLR_EL1_LSMAOE | SCTLR_EL1_nTLSMD | SCTLR_EL1_EIS | \ SCTLR_EL1_TSCXT | SCTLR_EL1_EOS) +/* TCR_EL1 specific flags */ +#define TCR_T0SZ_OFFSET 0 +#define TCR_T0SZ (x) ((UL(64) - (x)) << TCR_T0SZ_OFFSET) + +#define TCR_IRGN0_SHIFT 8 +#define TCR_IRGN0_MASK (UL(3) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_NC (UL(0) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WBWA (UL(1) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WT (UL(2) << TCR_IRGN0_SHIFT) +#define TCR_IRGN0_WBnWA (UL(3) << TCR_IRGN0_SHIFT) + +#define TCR_ORGN0_SHIFT 10 +#define TCR_ORGN0_MASK (UL(3) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_NC (UL(0) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WBWA (UL(1) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WT (UL(2) << TCR_ORGN0_SHIFT) +#define TCR_ORGN0_WBnWA (UL(3) << TCR_ORGN0_SHIFT) + +#define TCR_SH0_SHIFT 12 +#define TCR_SH0_MASK (UL(3) << TCR_SH0_SHIFT) +#define TCR_SH0_INNER (UL(3) << TCR_SH0_SHIFT) + +#define TCR_TG0_SHIFT 14 +#define TCR_TG0_MASK (UL(3) << TCR_TG0_SHIFT) +#define TCR_TG0_4K (UL(0) << TCR_TG0_SHIFT) +#define TCR_TG0_64K (UL(1) << TCR_TG0_SHIFT) +#define TCR_TG0_16K (UL(2) << TCR_TG0_SHIFT) +
tools/arch/arm64/include/asm/sysreg.h line 924
Quoted 10 lines
tools/arch/arm64/include/asm/sysreg.h +10
-0+#define TCR_IPS_SHIFT 32 +#define TCR_IPS_MASK (UL(7) << TCR_IPS_SHIFT) +#define TCR_IPS_52_BITS (UL(6) << TCR_IPS_SHIFT) +#define TCR_IPS_48_BITS (UL(5) << TCR_IPS_SHIFT) +#define TCR_IPS_40_BITS (UL(2) << TCR_IPS_SHIFT) +#define TCR_IPS_36_BITS (UL(1) << TCR_IPS_SHIFT) + +#define TCR_HA (UL(1) << 39) +#define TCR_DS (UL(1) << 59) +
sysreg.h isn't the right home for these definitions since it is meant to be a copy of the corresponding kernel header.
Since KVM selftests are likely the only thing in tools to care about setting up page tables, adding this to processor.h seems like a better place.
Thanks,
Oliver
Re: [PATCH 2/2] KVM: selftests: arm64: Explicitly set the page attrs to Inner-Shareable
In reply to Raghavendra Rao AnantaQuoted 5 lines · Raghavendra Rao Ananta
Atomic instructions such as 'ldset' over (global) variables in the guest is observed to cause an EL1 data abort with FSC
0x35(IMPLEMENTATION DEFINED fault (Unsupported Exclusive or Atomic access)). The observation was particularly apparent on Neoverse-N3.
Quoted 9 lines
According to DDI0487L.a C3.2.6 (Single-copy atomic 64-byte load/store), it is implementation defined that a data abort with the mentioned FSC is reported for the first stage of translation that provides an inappropriate memory type. It's likely that the same rule also applies to memory attribute mismatch. When the guest loads the memory location of the variable that was already cached during the host userspace's copying of the ELF into the memory, the core is likely running into a mismatch of memory attrs that's checked in stage-1 itself, and thus causing the abort in EL1.
Sorry, my index of the ARM ARM was trashed when we were discussing this before.
DDI0487L.a B2.2.6 describes the exact situation you encountered, where atomics are only guaranteed to work on Inner/Outer Shareable MT_NORMAL memory.
What's a bit more explicit for other memory attribute aborts (like the one you've cited) is whether or not the implementation can generate the abort solely on the stage-1 attributes vs. the combined stage-1/stage-2 attributes at the end of translation.
Either way, let's correct the citation to point at the right section.
Thanks,
Oliver
Re: [PATCH 2/2] KVM: selftests: arm64: Explicitly set the page attrs to Inner-Shareable
Hi Oliver
Quoted 24 lines · Oliver Upton
Quoted 5 lines · Raghavendra Rao Ananta
Atomic instructions such as 'ldset' over (global) variables in the guest is observed to cause an EL1 data abort with FSC
0x35(IMPLEMENTATION DEFINED fault (Unsupported Exclusive or Atomic access)). The observation was particularly apparent on Neoverse-N3.Quoted 9 lines
According to DDI0487L.a C3.2.6 (Single-copy atomic 64-byte load/store), it is implementation defined that a data abort with the mentioned FSC is reported for the first stage of translation that provides an inappropriate memory type. It's likely that the same rule also applies to memory attribute mismatch. When the guest loads the memory location of the variable that was already cached during the host userspace's copying of the ELF into the memory, the core is likely running into a mismatch of memory attrs that's checked in stage-1 itself, and thus causing the abort in EL1.
Sorry, my index of the ARM ARM was trashed when we were discussing this before.
DDI0487L.a B2.2.6 describes the exact situation you encountered, where atomics are only guaranteed to work on Inner/Outer Shareable
MT_NORMALmemory.
Quoted 7 lines
What's a bit more explicit for other memory attribute aborts (like the one you've cited) is whether or not the implementation can generate the abort solely on the stage-1 attributes vs. the combined stage-1/stage-2 attributes at the end of translation.
Either way, let's correct the citation to point at the right section.
Ah yes, DDI0487L.a B2.2.6 seems to be very close. OTOH DDI0487L.a C3.2.6 explains why we see an abort in EL1. I can cite both to get a full picture.
Thank you.
Raghavendra
Quoted 2 lines
Thanks,
Oliver