Skip to contentSkip to conversation

arm: MMU extentions to enable litmus7

PATCH Superseded Nikos Nikoleris 2 participants 5 messages Nov 10, 2020 KVM

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover arm: MMU extentions to enable litmus7

Hi all,

litmus7 [1][2], a tool that we develop and use to test the memory model on hardware, is building on kvm-unit-tests to encapsulate full system tests and control address translation. This series extends the kvm-unit-tests arm MMU API and adds two memory attributes to MAIR_EL1 to make them available to the litmus tests.

[1]: http://diy.inria.fr/doc/litmus.html
[2]: https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/expanding-memory-model-tools-system-level-architecture

v1: https://lore.kernel.org/kvm/[email protected]/T/

Changes in v2:
- Add comment on break-before-make in get_mmu_pte()
- Signed-off-by tag from all co-authors
- Minor formatting changes

Thanks,

Nikos

Luc Maranget (1):
arm: Add mmu_get_pte() to the MMU API

Nikos Nikoleris (1):
arm: Add support for the DEVICE_nGRE and NORMAL_WT memory types

 lib/arm/asm/mmu-api.h         |  1 +
 lib/arm64/asm/pgtable-hwdef.h |  2 ++
 lib/arm/mmu.c                 | 32 +++++++++++++++++++++-----------
 arm/cstart64.S                |  6 +++++-
 4 files changed, 29 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/2 arm: Add mmu_get_pte() to the MMU API

From: Luc Maranget <[email protected]>

Add the mmu_get_pte() function that allows a test to get a pointer to the PTE for a valid virtual address. Return NULL if the MMU is off.

---
 lib/arm/asm/mmu-api.h |  1 +
 lib/arm/mmu.c         | 32 +++++++++++++++++++++-----------
 2 files changed, 22 insertions(+), 11 deletions(-)
lib/arm/asm/mmu-api.h +1 -0
index 2bbe1fa..3d04d03 100644
@@ -22,5 +22,6 @@ extern void mmu_set_range_sect(pgd_t *pgtable, uintptr_t virt_offset,
 extern void mmu_set_range_ptes(pgd_t *pgtable, uintptr_t virt_offset,
 			       phys_addr_t phys_start, phys_addr_t phys_end,
 			       pgprot_t prot);
+extern pteval_t *mmu_get_pte(pgd_t *pgtable, uintptr_t vaddr);
 extern void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr);
 #endif
lib/arm/mmu.c +21 -11
index d937f20..e58da10 100644
@@ -212,15 +212,21 @@ unsigned long __phys_to_virt(phys_addr_t addr)
 	return addr;
 }
 
-void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr)
+pteval_t *mmu_get_pte(pgd_t *pgtable, uintptr_t vaddr)
 {
+	/*
+	 * NOTE: The Arm architecture requires the use of a
+	 * break-before-make sequence before making any changes to
+	 * PTEs (Arm ARM D5-2669 for AArch64, B3-1378 for AArch32).
+	 */
+
 	pgd_t *pgd;
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
 
 	if (!mmu_enabled())
-		return;
+		return NULL;
 
 	pgd = pgd_offset(pgtable, vaddr);
 	assert(pgd_valid(*pgd));
@@ -229,17 +235,21 @@ void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr)
 	pmd = pmd_offset(pud, vaddr);
 	assert(pmd_valid(*pmd));
 
-	if (pmd_huge(*pmd)) {
-		pmd_t entry = __pmd(pmd_val(*pmd) & ~PMD_SECT_USER);
-		WRITE_ONCE(*pmd, entry);
-		goto out_flush_tlb;
-	}
+	if (pmd_huge(*pmd))
+		return &pmd_val(*pmd);
 
 	pte = pte_offset(pmd, vaddr);
 	assert(pte_valid(*pte));
-	pte_t entry = __pte(pte_val(*pte) & ~PTE_USER);
-	WRITE_ONCE(*pte, entry);
 
-out_flush_tlb:
-	flush_tlb_page(vaddr);
+        return &pte_val(*pte);
+}
+
+void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr)
+{
+	pteval_t *p_pte = mmu_get_pte(pgtable, vaddr);
+	if (p_pte) {
+		pteval_t entry = *p_pte & ~PTE_USER;
+		WRITE_ONCE(*p_pte, entry);
+		flush_tlb_page(vaddr);
+	}
 }
Signature · 1 line
2.17.1
#2 lore raw

Re: [kvm-unit-tests PATCH v2 1/2] arm: Add mmu_get_pte() to the MMU API

Hi Nikos,

Quoted 32 lines · Nikos Nikoleris

From: Luc Maranget <[email protected]>

Add the mmu_get_pte() function that allows a test to get a pointer to the PTE for a valid virtual address. Return NULL if the MMU is off.

---
 lib/arm/asm/mmu-api.h |  1 +
 lib/arm/mmu.c         | 32 +++++++++++++++++++++-----------
 2 files changed, 22 insertions(+), 11 deletions(-)
lib/arm/asm/mmu-api.h +1 -0
index 2bbe1fa..3d04d03 100644
@@ -22,5 +22,6 @@ extern void mmu_set_range_sect(pgd_t *pgtable, uintptr_t virt_offset,
 extern void mmu_set_range_ptes(pgd_t *pgtable, uintptr_t virt_offset,
 			       phys_addr_t phys_start, phys_addr_t phys_end,
 			       pgprot_t prot);
+extern pteval_t *mmu_get_pte(pgd_t *pgtable, uintptr_t vaddr);
 extern void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr);
 #endif
lib/arm/mmu.c +0 -0
index d937f20..e58da10 100644
@@ -212,15 +212,21 @@ unsigned long __phys_to_virt(phys_addr_t addr)
 	return addr;

lib/arm/mmu.c unsigned long __phys_to_virt(phys_addr_t addr) · line 221

Quoted 10 lines
lib/arm/mmu.c +6 -1
 }
 
-void mmu_clear_user(pgd_t *pgtable, unsigned long vaddr)
+pteval_t *mmu_get_pte(pgd_t *pgtable, uintptr_t vaddr)
 {
+	/*
+	 * NOTE: The Arm architecture requires the use of a
+	 * break-before-make sequence before making any changes to
+	 * PTEs (Arm ARM D5-2669 for AArch64, B3-1378 for AArch32).
+	 */

That should go above the function definition. Also, break-before-make is not required for every PTE change, only when certain conditions are met.

Thanks,
Alex

#3 lore raw

PATCH v2 2/2 arm: Add support for the DEVICE_nGRE and NORMAL_WT memory types

In reply to Nikos Nikoleris
---
 lib/arm64/asm/pgtable-hwdef.h | 2 ++
 arm/cstart64.S                | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
lib/arm64/asm/pgtable-hwdef.h +2 -0
index c31bc11..48a1d1a 100644
@@ -153,5 +153,7 @@
 #define MT_DEVICE_GRE		2
 #define MT_NORMAL_NC		3	/* writecombine */
 #define MT_NORMAL		4
+#define MT_NORMAL_WT		5
+#define MT_DEVICE_nGRE		6
 
 #endif /* _ASMARM64_PGTABLE_HWDEF_H_ */
arm/cstart64.S +5 -1
index 6610779..0428014 100644
@@ -154,6 +154,8 @@ halt:
  *   DEVICE_GRE         010     00001100
  *   NORMAL_NC          011     01000100
  *   NORMAL             100     11111111
+ *   NORMAL_WT          101     10111011
+ *   DEVICE_nGRE        110     00001000
  */
 #define MAIR(attr, mt) ((attr) << ((mt) * 8))
 
@@ -184,7 +186,9 @@ asm_mmu_enable:
 		     MAIR(0x04, MT_DEVICE_nGnRE) |	\
 		     MAIR(0x0c, MT_DEVICE_GRE) |	\
 		     MAIR(0x44, MT_NORMAL_NC) |		\
-		     MAIR(0xff, MT_NORMAL)
+		     MAIR(0xff, MT_NORMAL) |	        \
+		     MAIR(0xbb, MT_NORMAL_WT) |         \
+		     MAIR(0x08, MT_DEVICE_nGRE)
 	msr	mair_el1, x1
 
 	/* TTBR0 */
Signature · 1 line
2.17.1
Reviewed-by on v2 2/2
#4 lore raw

Re: [kvm-unit-tests PATCH v2 2/2] arm: Add support for the DEVICE_nGRE and NORMAL_WT memory types

Hi Nikos,

Quoted 33 lines · Nikos Nikoleris
---
 lib/arm64/asm/pgtable-hwdef.h | 2 ++
 arm/cstart64.S                | 6 +++++-
 2 files changed, 7 insertions(+), 1 deletion(-)
lib/arm64/asm/pgtable-hwdef.h +2 -0
index c31bc11..48a1d1a 100644
@@ -153,5 +153,7 @@
 #define MT_DEVICE_GRE		2
 #define MT_NORMAL_NC		3	/* writecombine */
 #define MT_NORMAL		4
+#define MT_NORMAL_WT		5
+#define MT_DEVICE_nGRE		6
 
 #endif /* _ASMARM64_PGTABLE_HWDEF_H_ */
arm/cstart64.S +2 -0
index 6610779..0428014 100644
@@ -154,6 +154,8 @@ halt:
  *   DEVICE_GRE         010     00001100
  *   NORMAL_NC          011     01000100
  *   NORMAL             100     11111111
+ *   NORMAL_WT          101     10111011
+ *   DEVICE_nGRE        110     00001000
  */
 #define MAIR(attr, mt) ((attr) << ((mt) * 8))
 

arm/cstart64.S asm_mmu_enable: · line 191

Quoted 7 lines
arm/cstart64.S +3 -1
@@ -184,7 +186,9 @@ asm_mmu_enable:
 		     MAIR(0x04, MT_DEVICE_nGnRE) |	\
 		     MAIR(0x0c, MT_DEVICE_GRE) |	\
 		     MAIR(0x44, MT_NORMAL_NC) |		\
-		     MAIR(0xff, MT_NORMAL)
+		     MAIR(0xff, MT_NORMAL) |	        \
+		     MAIR(0xbb, MT_NORMAL_WT) |         \
+		     MAIR(0x08, MT_DEVICE_nGRE)

Compared the values with ARM DDI 0487F.b, pages D13-333{5,6}. 0xbb matches Normal memory, Inner and Outer Write-Through Non-transient (where RW=0b11). 0x08 matches with Device nGRE memory:

Thanks,
Alex