Skip to contentSkip to conversation

s390: Add support for large pages

PATCH Superseded Claudio Imbrenda 2 participants 11 messages Feb 9, 2021 – Feb 11, 2021

Subsystems (3)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover s390: Add support for large pages

Introduce support for large (1M) and huge (2G) pages.

Add a simple testcase for EDAT1 and EDAT2.

Claudio Imbrenda (4):
libcflat: add SZ_1M and SZ_2G
s390x: lib: fix and improve pgtable.h
s390x: mmu: add support for large pages
s390x: edat test

 s390x/Makefile          |   1 +
 lib/s390x/asm/pgtable.h |  38 ++++++-
 lib/libcflat.h          |   2 +
 lib/s390x/mmu.h         |  73 +++++++++++-
 lib/s390x/mmu.c         | 246 ++++++++++++++++++++++++++++++++++++----
 s390x/edat.c            | 238 ++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg     |   3 +
 7 files changed, 572 insertions(+), 29 deletions(-)
 create mode 100644 s390x/edat.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/4 libcflat: add SZ_1M and SZ_2G

Add SZ_1M and SZ_2G to libcflat.h

s390x needs those for large/huge pages

---
 lib/libcflat.h | 2 ++
 1 file changed, 2 insertions(+)
lib/libcflat.h +2 -0
index 460a1234..8dac0621 100644
@@ -157,7 +157,9 @@ extern void setup_vm(void);
 #define SZ_8K			(1 << 13)
 #define SZ_16K			(1 << 14)
 #define SZ_64K			(1 << 16)
+#define SZ_1M			(1 << 20)
 #define SZ_2M			(1 << 21)
 #define SZ_1G			(1 << 30)
+#define SZ_2G			(1ul << 31)
 
 #endif
Signature · 1 line
2.26.2
Reviewed-by on v1 1/4
#2 lore raw

Re: [kvm-unit-tests PATCH v1 1/4] libcflat: add SZ_1M and SZ_2G

Quoted 15 lines · Claudio Imbrenda

Add SZ_1M and SZ_2G to libcflat.h

s390x needs those for large/huge pages

---
  lib/libcflat.h | 2 ++
  1 file changed, 2 insertions(+)
lib/libcflat.h +0 -0
index 460a1234..8dac0621 100644

lib/libcflat.h extern void setup_vm(void); · line 165

Quoted 9 lines
lib/libcflat.h +2 -0
@@ -157,7 +157,9 @@ extern void setup_vm(void);
  #define SZ_8K			(1 << 13)
  #define SZ_16K			(1 << 14)
  #define SZ_64K			(1 << 16)
+#define SZ_1M			(1 << 20)
  #define SZ_2M			(1 << 21)
  #define SZ_1G			(1 << 30)
+#define SZ_2G			(1ul << 31)
 
  #endif
#3 lore raw

PATCH 2/4 s390x: lib: fix and improve pgtable.h

In reply to Claudio Imbrenda

Fix and improve pgtable.h:

* SEGMENT_ENTRY_SFAA had one extra bit set
* pmd entries don't have a length
* ipte does not need to clear the lower bits
* add macros to check whether a pmd or a pud are large / huge
* add idte functions for pmd, pud, p4d and pgd

---
 lib/s390x/asm/pgtable.h | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)
lib/s390x/asm/pgtable.h +34 -4
index 277f3480..4269ab62 100644
@@ -60,7 +60,7 @@
 #define SEGMENT_SHIFT			20
 
 #define SEGMENT_ENTRY_ORIGIN		0xfffffffffffff800UL
-#define SEGMENT_ENTRY_SFAA		0xfffffffffff80000UL
+#define SEGMENT_ENTRY_SFAA		0xfffffffffff00000UL
 #define SEGMENT_ENTRY_AV		0x0000000000010000UL
 #define SEGMENT_ENTRY_ACC		0x000000000000f000UL
 #define SEGMENT_ENTRY_F			0x0000000000000800UL
@@ -100,6 +100,9 @@
 #define pmd_none(entry) (pmd_val(entry) & SEGMENT_ENTRY_I)
 #define pte_none(entry) (pte_val(entry) & PAGE_ENTRY_I)
 
+#define pud_huge(entry)  (pud_val(entry) & REGION3_ENTRY_FC)
+#define pmd_large(entry) (pmd_val(entry) & SEGMENT_ENTRY_FC)
+
 #define pgd_addr(entry) __va(pgd_val(entry) & REGION_ENTRY_ORIGIN)
 #define p4d_addr(entry) __va(p4d_val(entry) & REGION_ENTRY_ORIGIN)
 #define pud_addr(entry) __va(pud_val(entry) & REGION_ENTRY_ORIGIN)
@@ -202,21 +205,48 @@ static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr)
 {
 	if (pmd_none(*pmd)) {
 		pte_t *pte = pte_alloc_one();
-		pmd_val(*pmd) = __pa(pte) | SEGMENT_ENTRY_TT_SEGMENT |
-				SEGMENT_TABLE_LENGTH;
+		pmd_val(*pmd) = __pa(pte) | SEGMENT_ENTRY_TT_SEGMENT;
 	}
 	return pte_offset(pmd, addr);
 }
 
 static inline void ipte(unsigned long vaddr, pteval_t *p_pte)
 {
-	unsigned long table_origin = (unsigned long)p_pte & PAGE_MASK;
+	unsigned long table_origin = (unsigned long)p_pte;
 
 	asm volatile(
 		"	ipte %0,%1\n"
 		: : "a" (table_origin), "a" (vaddr) : "memory");
 }
 
+static inline void idte(unsigned long table_origin, unsigned long vaddr)
+{
+	vaddr &= SEGMENT_ENTRY_SFAA;
+	asm volatile(
+		"	idte %0,0,%1\n"
+		: : "a" (table_origin), "a" (vaddr) : "memory");
+}
+
+static inline void idte_pmdp(unsigned long vaddr, pmdval_t *pmdp)
+{
+	idte((unsigned long)(pmdp - pmd_index(vaddr)) | ASCE_DT_SEGMENT, vaddr);
+}
+
+static inline void idte_pudp(unsigned long vaddr, pudval_t *pudp)
+{
+	idte((unsigned long)(pudp - pud_index(vaddr)) | ASCE_DT_REGION3, vaddr);
+}
+
+static inline void idte_p4dp(unsigned long vaddr, p4dval_t *p4dp)
+{
+	idte((unsigned long)(p4dp - p4d_index(vaddr)) | ASCE_DT_REGION2, vaddr);
+}
+
+static inline void idte_pgdp(unsigned long vaddr, pgdval_t *pgdp)
+{
+	idte((unsigned long)(pgdp - pgd_index(vaddr)) | ASCE_DT_REGION1, vaddr);
+}
+
 void configure_dat(int enable);
 
 #endif /* _ASMS390X_PGTABLE_H_ */
Signature · 1 line
2.26.2
#4 lore raw

Re: [kvm-unit-tests PATCH v1 2/4] s390x: lib: fix and improve pgtable.h

Quoted 37 lines · Claudio Imbrenda

Fix and improve pgtable.h:

* SEGMENT_ENTRY_SFAA had one extra bit set
* pmd entries don't have a length
* ipte does not need to clear the lower bits
* add macros to check whether a pmd or a pud are large / huge
* add idte functions for pmd, pud, p4d and pgd

---
  lib/s390x/asm/pgtable.h | 38 ++++++++++++++++++++++++++++++++++----
  1 file changed, 34 insertions(+), 4 deletions(-)
lib/s390x/asm/pgtable.h +4 -1
index 277f3480..4269ab62 100644
@@ -60,7 +60,7 @@
  #define SEGMENT_SHIFT			20
 
  #define SEGMENT_ENTRY_ORIGIN		0xfffffffffffff800UL
-#define SEGMENT_ENTRY_SFAA		0xfffffffffff80000UL
+#define SEGMENT_ENTRY_SFAA		0xfffffffffff00000UL
  #define SEGMENT_ENTRY_AV		0x0000000000010000UL
  #define SEGMENT_ENTRY_ACC		0x000000000000f000UL
  #define SEGMENT_ENTRY_F			0x0000000000000800UL
@@ -100,6 +100,9 @@
  #define pmd_none(entry) (pmd_val(entry) & SEGMENT_ENTRY_I)
  #define pte_none(entry) (pte_val(entry) & PAGE_ENTRY_I)
 
+#define pud_huge(entry)  (pud_val(entry) & REGION3_ENTRY_FC)
+#define pmd_large(entry) (pmd_val(entry) & SEGMENT_ENTRY_FC)
+
  #define pgd_addr(entry) __va(pgd_val(entry) & REGION_ENTRY_ORIGIN)
  #define p4d_addr(entry) __va(p4d_val(entry) & REGION_ENTRY_ORIGIN)
  #define pud_addr(entry) __va(pud_val(entry) & REGION_ENTRY_ORIGIN)

lib/s390x/asm/pgtable.h static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) · line 208

Quoted 6 lines
lib/s390x/asm/pgtable.h +1 -2
@@ -202,21 +205,48 @@ static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr)
  {
  	if (pmd_none(*pmd)) {
  		pte_t *pte = pte_alloc_one();
-		pmd_val(*pmd) = __pa(pte) | SEGMENT_ENTRY_TT_SEGMENT |
-				SEGMENT_TABLE_LENGTH;
+		pmd_val(*pmd) = __pa(pte) | SEGMENT_ENTRY_TT_SEGMENT;

I think you could even remove the #define SEGMENT_TABLE_LENGTH now, since this define does not make much sense, does it?

Quoted 31 lines
lib/s390x/asm/pgtable.h +18 -1
  	}
  	return pte_offset(pmd, addr);
  }
 
  static inline void ipte(unsigned long vaddr, pteval_t *p_pte)
  {
-	unsigned long table_origin = (unsigned long)p_pte & PAGE_MASK;
+	unsigned long table_origin = (unsigned long)p_pte;
 
  	asm volatile(
  		"	ipte %0,%1\n"
  		: : "a" (table_origin), "a" (vaddr) : "memory");
  }
 
+static inline void idte(unsigned long table_origin, unsigned long vaddr)
+{
+	vaddr &= SEGMENT_ENTRY_SFAA;
+	asm volatile(
+		"	idte %0,0,%1\n"
+		: : "a" (table_origin), "a" (vaddr) : "memory");
+}
+
+static inline void idte_pmdp(unsigned long vaddr, pmdval_t *pmdp)
+{
+	idte((unsigned long)(pmdp - pmd_index(vaddr)) | ASCE_DT_SEGMENT, vaddr);
+}
+
+static inline void idte_pudp(unsigned long vaddr, pudval_t *pudp)
+{
+	idte((unsigned long)(pudp - pud_index(vaddr)) | ASCE_DT_REGION3, vaddr);
+}

lib/s390x/asm/pgtable.h static inline pte_t *pte_alloc(pmd_t *pmd, unsigned long addr) · line 248

Quoted 10 lines
lib/s390x/asm/pgtable.h +10 -0
+
+static inline void idte_p4dp(unsigned long vaddr, p4dval_t *p4dp)
+{
+	idte((unsigned long)(p4dp - p4d_index(vaddr)) | ASCE_DT_REGION2, vaddr);
+}
+
+static inline void idte_pgdp(unsigned long vaddr, pgdval_t *pgdp)
+{
+	idte((unsigned long)(pgdp - pgd_index(vaddr)) | ASCE_DT_REGION1, vaddr);
+}

I think it would be cleaner to separate the fixes from the new functions, i.e. put the new functions into a separate patch.

Thomas

#5 lore raw

PATCH 3/4 s390x: mmu: add support for large pages

In reply to Claudio Imbrenda

Add support for 1M and 2G pages.

---
 lib/s390x/mmu.h |  73 +++++++++++++-
 lib/s390x/mmu.c | 246 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 294 insertions(+), 25 deletions(-)
lib/s390x/mmu.h +71 -2
index 603f289e..93208467 100644
@@ -10,9 +10,78 @@
 #ifndef _ASMS390X_MMU_H_
 #define _ASMS390X_MMU_H_
 
-void protect_page(void *vaddr, unsigned long prot);
+/*
+ * Splits the pagetables down to the given DAT tables level.
+ * Returns a pointer to the DAT table entry of the given level.
+ * @pgtable root of the page table tree
+ * @vaddr address whose page tables are to split
+ * @level 3 (for 2GB pud), 4 (for 1 MB pmd) or 5 (for 4KB pages)
+ */
+void *split_page(pgd_t *pgtable, void *vaddr, unsigned int level);
+
+/*
+ * Applies the given protection bits to the given DAT tables level,
+ * splitting if necessary.
+ * @pgtable root of the page table tree
+ * @vaddr address whose protection bits are to be changed
+ * @prot the protection bits to set
+ * @level 3 (for 2GB pud), 4 (for 1MB pmd) or 5 (for 4KB pages)
+ */
+void protect_dat_entry(void *vaddr, unsigned long prot, unsigned int level);
+/*
+ * Clears the given protection bits from the given DAT tables level,
+ * splitting if necessary.
+ * @pgtable root of the page table tree
+ * @vaddr address whose protection bits are to be changed
+ * @prot the protection bits to clear
+ * @level 3 (for 2GB pud), 4 (for 1MB pmd) or 5 (for 4kB pages)
+ */
+void unprotect_dat_entry(void *vaddr, unsigned long prot, unsigned int level);
+
+/*
+ * Applies the given protection bits to the given 4kB pages range,
+ * splitting if necessary.
+ * @start starting address whose protection bits are to be changed
+ * @len size in bytes
+ * @prot the protection bits to set
+ */
 void protect_range(void *start, unsigned long len, unsigned long prot);
-void unprotect_page(void *vaddr, unsigned long prot);
+/*
+ * Clears the given protection bits from the given 4kB pages range,
+ * splitting if necessary.
+ * @start starting address whose protection bits are to be changed
+ * @len size in bytes
+ * @prot the protection bits to set
+ */
 void unprotect_range(void *start, unsigned long len, unsigned long prot);
 
+/* Similar to install_page, maps the virtual address to the physical address
+ * for the given page tables, using 1MB large pages.
+ * Returns a pointer to the DAT table entry.
+ * @pgtable root of the page table tree
+ * @phys physical address to map, must be 1MB aligned!
+ * @vaddr virtual address to map, must be 1MB aligned!
+ */
+pmdval_t *install_large_page(pgd_t *pgtable, phys_addr_t phys, void *vaddr);
+
+/* Similar to install_page, maps the virtual address to the physical address
+ * for the given page tables, using 2GB huge pages.
+ * Returns a pointer to the DAT table entry.
+ * @pgtable root of the page table tree
+ * @phys physical address to map, must be 2GB aligned!
+ * @vaddr virtual address to map, must be 2GB aligned!
+ */
+pudval_t *install_huge_page(pgd_t *pgtable, phys_addr_t phys, void *vaddr);
+
+static inline void protect_page(void *vaddr, unsigned long prot)
+{
+	protect_dat_entry(vaddr, prot, 5);
+}
+
+static inline void unprotect_page(void *vaddr, unsigned long prot)
+{
+	unprotect_dat_entry(vaddr, prot, 5);
+}
+void *get_dat_entry(pgd_t *pgtable, void *vaddr, unsigned int level);
+
 #endif /* _ASMS390X_MMU_H_ */
lib/s390x/mmu.c +223 -23

291 lines not on this page · raw message

Signature · 1 line
2.26.2
#6 lore raw

Re: [kvm-unit-tests PATCH v1 3/4] s390x: mmu: add support for large pages

Quoted 7 lines · Claudio Imbrenda

Add support for 1M and 2G pages.

---
  lib/s390x/mmu.h |  73 +++++++++++++-
  lib/s390x/mmu.c | 246 +++++++++++++++++++++++++++++++++++++++++++-----
  2 files changed, 294 insertions(+), 25 deletions(-)

[...]

lib/s390x/mmu.c static void mmu_enable(pgd_t *pgtable) · line 86

Quoted 12 lines
lib/s390x/mmu.c +9 -1
+/*
+ * Get the pte (page) DAT table entry for the given address and pmd,
+ * allocating it if necessary.
+ * The pmd must not be large.
+ */
+static inline pte_t *get_pte(pmd_t *pmd, uintptr_t vaddr)
+{
  	pte_t *pte = pte_alloc(pmd, vaddr);
 
-	return &pte_val(*pte);
+	assert(!pmd_large(*pmd));
+	pte = pte_alloc(pmd, vaddr);

Why is this function doing "pte = pte_alloc(pmd, vaddr)" twice now?

Quoted 2 lines
(quoted diff) +2 -0
+	return pte;
+}

[...]

lib/s390x/mmu.c static void mmu_enable(pgd_t *pgtable) · line 210

Quoted 10 lines
lib/s390x/mmu.c +10 -0
+	if ((level == 1) && !pgd_none(*(pgd_t *)ptr))
+		idte_pgdp(va, ptr);
+	else if ((level == 2) && !p4d_none(*(p4d_t *)ptr))
+		idte_p4dp(va, ptr);
+	else if ((level == 3) && !pud_none(*(pud_t *)ptr))
+		idte_pudp(va, ptr);
+	else if ((level == 4) && !pmd_none(*(pmd_t *)ptr))
+		idte_pmdp(va, ptr);
+	else if (!pte_none(*(pte_t *)ptr))
+		ipte(va, ptr);

Meta-comment: Being someone who worked quite a bit with the page tables on s390x, but never really got in touch with the way it is handled in the Linux kernel, I'm always having a hard time to match all these TLAs to the PoP: pmd, pud, p4d ... Can we please have a proper place in the kvm-unit-tests sources somewhere (maybe at the beginning of mmu.c), where the TLAs are explained and how they map to the region and segment tables of the Z architecture? (I personally would prefer to completely switch to the Z arch naming instead, but I guess that's too much of a change right now)

Thomas

#7 lore raw

Re: [kvm-unit-tests PATCH v1 3/4] s390x: mmu: add support for large pages

Quoted 20 lines · On Thu, 11 Feb 2021 11:06:06 +0100 Thomas Huth <[email protected]> wrote:
Quoted 8 lines · Claudio Imbrenda

Add support for 1M and 2G pages.

---
  lib/s390x/mmu.h |  73 +++++++++++++-
  lib/s390x/mmu.c | 246

+++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 294 insertions(+), 25 deletions(-)

[...]

Quoted 10 lines

+/*
+ * Get the pte (page) DAT table entry for the given address and pmd,
+ * allocating it if necessary.
+ * The pmd must not be large.
+ */

lib/s390x/mmu.c +2 -0
+static inline pte_t *get_pte(pmd_t *pmd, uintptr_t vaddr)
+{
  	pte_t *pte = pte_alloc(pmd, vaddr);
Quoted 5 lines

lib/s390x/mmu.c static void mmu_enable(pgd_t *pgtable) · line 86

Quoted 3 lines
lib/s390x/mmu.c +2 -1
-	return &pte_val(*pte);
+	assert(!pmd_large(*pmd));
+	pte = pte_alloc(pmd, vaddr);  

Why is this function doing "pte = pte_alloc(pmd, vaddr)" twice now?

ooops! the first pte_alloc is not supposed to be there! good catch - will fix

Quoted 16 lines
Quoted 2 lines
(quoted diff) +2 -0
+	return pte;
+}  

[...]

lib/s390x/mmu.c static void mmu_enable(pgd_t *pgtable) · line 210

Quoted 10 lines
lib/s390x/mmu.c +10 -0
+	if ((level == 1) && !pgd_none(*(pgd_t *)ptr))
+		idte_pgdp(va, ptr);
+	else if ((level == 2) && !p4d_none(*(p4d_t *)ptr))
+		idte_p4dp(va, ptr);
+	else if ((level == 3) && !pud_none(*(pud_t *)ptr))
+		idte_pudp(va, ptr);
+	else if ((level == 4) && !pmd_none(*(pmd_t *)ptr))
+		idte_pmdp(va, ptr);
+	else if (!pte_none(*(pte_t *)ptr))
+		ipte(va, ptr);  

Meta-comment: Being someone who worked quite a bit with the page tables on s390x, but never really got in touch with the way it is

Quoted 8 lines

handled in the Linux kernel, I'm always having a hard time to match all these TLAs to the PoP: pmd, pud, p4d ...
Can we please have a proper place in the kvm-unit-tests sources somewhere (maybe at the beginning of mmu.c), where the TLAs are explained and how they map to the region and segment tables of the Z architecture? (I personally would prefer to completely switch to the Z arch naming instead, but I guess that's too much of a change right now)

makes sense, I can add that

Quoted 2 lines

Thomas

#8 lore raw

PATCH 4/4 s390x: edat test

In reply to Claudio Imbrenda

Simple EDAT test.

---
 s390x/Makefile      |   1 +
 s390x/edat.c        | 238 ++++++++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg |   3 +
 3 files changed, 242 insertions(+)
 create mode 100644 s390x/edat.c
s390x/Makefile +1 -0
index 08d85c9f..fc885150 100644
@@ -20,6 +20,7 @@ tests += $(TEST_DIR)/sclp.elf
 tests += $(TEST_DIR)/css.elf
 tests += $(TEST_DIR)/uv-guest.elf
 tests += $(TEST_DIR)/sie.elf
+tests += $(TEST_DIR)/edat.elf
 
 tests_binary = $(patsubst %.elf,%.bin,$(tests))
 ifneq ($(HOST_KEY_DOCUMENT),)
s390x/edat.c new file +238 -0

238 lines not on this page · raw message

s390x/unittests.cfg +3 -0
index 2298be6c..8da4a0a3 100644
@@ -99,3 +99,6 @@ file = uv-guest.elf
 
 [sie]
 file = sie.elf
+
+[edat]
+file = edat.elf
Signature · 1 line
2.26.2
#9 lore raw

Re: [kvm-unit-tests PATCH v1 4/4] s390x: edat test

Quoted 64 lines · Claudio Imbrenda

Simple EDAT test.

---
  s390x/Makefile      |   1 +
  s390x/edat.c        | 238 ++++++++++++++++++++++++++++++++++++++++++++
  s390x/unittests.cfg |   3 +
  3 files changed, 242 insertions(+)

create mode 100644 s390x/edat.c

s390x/Makefile +1 -0
index 08d85c9f..fc885150 100644
@@ -20,6 +20,7 @@ tests += $(TEST_DIR)/sclp.elf
  tests += $(TEST_DIR)/css.elf
  tests += $(TEST_DIR)/uv-guest.elf
  tests += $(TEST_DIR)/sie.elf
+tests += $(TEST_DIR)/edat.elf
 
  tests_binary = $(patsubst %.elf,%.bin,$(tests))
  ifneq ($(HOST_KEY_DOCUMENT),)
s390x/edat.c new file +36 -0
new file mode 100644
index 00000000..504a1501
@@ -0,0 +1,238 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * EDAT test.
+ *
+ * Copyright (c) 2021 IBM Corp
+ *
+ * Authors:
+ *	Claudio Imbrenda <[email protected]>
+ */
+#include <libcflat.h>
+#include <vmalloc.h>
+#include <asm/facility.h>
+#include <asm/interrupt.h>
+#include <mmu.h>
+#include <asm/pgtable.h>
+#include <asm-generic/barrier.h>
+
+#define TEID_ADDR	PAGE_MASK
+#define TEID_AI		0x003
+#define TEID_M		0x004
+#define TEID_A		0x008
+#define TEID_FS		0xc00
+
+#define LC_SIZE	(2 * PAGE_SIZE)
+#define VIRT(x)	((void *)((unsigned long)(x) + (unsigned long)mem))
+
+static uint8_t prefix_buf[LC_SIZE] __attribute__((aligned(LC_SIZE)));
+static unsigned int tmp[1024] __attribute__((aligned(PAGE_SIZE)));
+static void *root, *mem, *m;
+static struct lowcore *lc;
+volatile unsigned int *p;
+
+/* Expect a program interrupt, and clear the TEID */
+static void expect_dat_fault(void)
+{
+	expect_pgm_int();

s390x/edat.c line 46

Quoted 10 lines
s390x/edat.c new file +10 -0
+	lc->trans_exc_id = 0;
+}
+
+/* Check if a protection exception happened for the given address */
+static bool check_pgm_prot(void *ptr)
+{
+	unsigned long teid = lc->trans_exc_id;
+
+	if (lc->pgm_int_code != PGM_INT_CODE_PROTECTION)
+		return 0;

return false.
It's a bool return type.

s390x/edat.c line 47

Quoted 1 line
s390x/edat.c new file +1 -0
+	if (~teid & TEID_M)

I'd maybe rather write this as:

        if (!(teid & TEID_M))

... but it's just a matter of taste.

s390x/edat.c line 48

Quoted 1 line
s390x/edat.c new file +1 -0
+		return 1;
                return true;

So this is for backward compatiblity with older Z systems that do not have the corresponding facility? Should there be a corresponding facility check somewhere? Or maybe add at least a comment?

s390x/edat.c line 51

Quoted 3 lines
s390x/edat.c new file +3 -0
+	return (~teid & TEID_A) &&
+		((teid & TEID_ADDR) == ((uint64_t)ptr & PAGE_MASK)) &&
+		!(teid & TEID_AI);

So you're checking for one specific type of protection exception here only ... please add an appropriate comment.

Quoted 11 lines
s390x/edat.c new file +11 -0
+}
+
+static void test_dat(void)
+{
+	report_prefix_push("edat off");
+	/* disable EDAT */
+	ctl_clear_bit(0, 23);
+
+	/* Check some basics */
+	p[0] = 42;
+	report(p[0] == 42, "pte, r/w");

s390x/edat.c line 72

Quoted 10 lines
s390x/edat.c new file +10 -0
+	p[0] = 0;
+
+	protect_page(m, PAGE_ENTRY_P);
+	expect_dat_fault();
+	p[0] = 42;
+	unprotect_page(m, PAGE_ENTRY_P);
+	report(!p[0] && check_pgm_prot(m), "pte, ro");
+
+	/* The FC bit should be ignored because EDAT is off */
+	p[0] = 42;

I'd suggest to set p[0] = 0 here...

s390x/edat.c line 73

Quoted 1 line
s390x/edat.c new file +1 -0
+	protect_dat_entry(m, SEGMENT_ENTRY_FC, 4);

... and change the value to 42 after enabling the protection ... otherwise you don't really test the non-working write protection here, do you?

s390x/edat.c line 85

Quoted 12 lines
s390x/edat.c new file +12 -0
+	report(p[0] == 42, "pmd, fc=1, r/w");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_FC, 4);
+	p[0] = 0;
+
+	/* Segment protection should work even with EDAT off */
+	protect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+	expect_dat_fault();
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pmd, ro");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+
+	/* The FC bit should be ignored because EDAT is off*/

Set p[0] to 0 again before enabling the protection? Or maybe use a different value than 42 below...?

Quoted 17 lines
s390x/edat.c new file +17 -0
+	protect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	p[0] = 42;
+	report(p[0] == 42, "pud, fc=1, r/w");
+	unprotect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	p[0] = 0;
+
+	/* Region1/2/3 protection should not work, because EDAT is off */
+	protect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 42;
+	report(p[0] == 42, "pud, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 0;
+
+	protect_dat_entry(m, REGION_ENTRY_P, 2);
+	p[0] = 42;
+	report(p[0] == 42, "p4d, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 2);

s390x/edat.c line 112

Quoted 10 lines
s390x/edat.c new file +10 -0
+	p[0] = 0;
+
+	protect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 42;
+	report(p[0] == 42, "pgd, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 0;
+
+	report_prefix_pop();
+}

Thomas

#10 lore raw

Re: [kvm-unit-tests PATCH v1 4/4] s390x: edat test

Quoted 70 lines · On Thu, 11 Feb 2021 12:35:49 +0100 Thomas Huth <[email protected]> wrote:
Quoted 69 lines · Claudio Imbrenda

Simple EDAT test.

---
  s390x/Makefile      |   1 +
  s390x/edat.c        | 238

++++++++++++++++++++++++++++++++++++++++++++ s390x/unittests.cfg |
3 + 3 files changed, 242 insertions(+)
create mode 100644 s390x/edat.c

s390x/Makefile +1 -0
index 08d85c9f..fc885150 100644
@@ -20,6 +20,7 @@ tests += $(TEST_DIR)/sclp.elf
  tests += $(TEST_DIR)/css.elf
  tests += $(TEST_DIR)/uv-guest.elf
  tests += $(TEST_DIR)/sie.elf
+tests += $(TEST_DIR)/edat.elf
 
  tests_binary = $(patsubst %.elf,%.bin,$(tests))
  ifneq ($(HOST_KEY_DOCUMENT),)
s390x/edat.c new file +25 -0
new file mode 100644
index 00000000..504a1501
@@ -0,0 +1,238 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * EDAT test.
+ *
+ * Copyright (c) 2021 IBM Corp
+ *
+ * Authors:
+ *	Claudio Imbrenda <[email protected]>
+ */
+#include <libcflat.h>
+#include <vmalloc.h>
+#include <asm/facility.h>
+#include <asm/interrupt.h>
+#include <mmu.h>
+#include <asm/pgtable.h>
+#include <asm-generic/barrier.h>
+
+#define TEID_ADDR	PAGE_MASK
+#define TEID_AI		0x003
+#define TEID_M		0x004
+#define TEID_A		0x008
+#define TEID_FS		0xc00
+
+#define LC_SIZE	(2 * PAGE_SIZE)
+#define VIRT(x)	((void *)((unsigned long)(x) + (unsigned

long)mem)) +
+static uint8_t prefix_buf[LC_SIZE] __attribute__((aligned(LC_SIZE))); +static unsigned int tmp[1024] __attribute__((aligned(PAGE_SIZE))); +static void *root, *mem, *m;

s390x/edat.c new file +11 -0
+static struct lowcore *lc;
+volatile unsigned int *p;
+
+/* Expect a program interrupt, and clear the TEID */
+static void expect_dat_fault(void)
+{
+	expect_pgm_int();
+	lc->trans_exc_id = 0;
+}
+
+/* Check if a protection exception happened for the given address

*/ +static bool check_pgm_prot(void *ptr)

Quoted 8 lines

s390x/edat.c line 46

Quoted 5 lines
s390x/edat.c new file +5 -0
+{
+	unsigned long teid = lc->trans_exc_id;
+
+	if (lc->pgm_int_code != PGM_INT_CODE_PROTECTION)
+		return 0;  

return false.
It's a bool return type.

yeah, that looks cleaner, I'll fix it

Quoted 7 lines

s390x/edat.c line 47

Quoted 1 line
s390x/edat.c new file +1 -0
+	if (~teid & TEID_M)  

I'd maybe rather write this as:

         if (!(teid & TEID_M))

... but it's just a matter of taste.

yes, I actually had it that way in the beginning, but using ~ is shorter and does not need parentheses

Quoted 7 lines

s390x/edat.c line 48

Quoted 1 line
s390x/edat.c new file +1 -0
+		return 1;  
                 return true;

So this is for backward compatiblity with older Z systems that do not have the corresponding facility? Should there be a corresponding facility check somewhere? Or maybe add at least a comment?

no, it's not for backwards compatibility as far as I know. If I read the documentation correctly, that bit might be zero under some circumstances, and here I will just give up instead of checking if the circumstances were actually correct.

Quoted 7 lines

s390x/edat.c line 51

Quoted 4 lines
s390x/edat.c new file +2 -0
+	return (~teid & TEID_A) &&
+		((teid & TEID_ADDR) == ((uint64_t)ptr &

PAGE_MASK)) &&

s390x/edat.c new file +1 -0
+		!(teid & TEID_AI);  

So you're checking for one specific type of protection exception here only ... please add an appropriate comment.

more or less, but I'll add a comment to explain what's going on

Quoted 24 lines
Quoted 11 lines
s390x/edat.c new file +11 -0
+}
+
+static void test_dat(void)
+{
+	report_prefix_push("edat off");
+	/* disable EDAT */
+	ctl_clear_bit(0, 23);
+
+	/* Check some basics */
+	p[0] = 42;
+	report(p[0] == 42, "pte, r/w");

s390x/edat.c line 72

Quoted 10 lines
s390x/edat.c new file +10 -0
+	p[0] = 0;
+
+	protect_page(m, PAGE_ENTRY_P);
+	expect_dat_fault();
+	p[0] = 42;
+	unprotect_page(m, PAGE_ENTRY_P);
+	report(!p[0] && check_pgm_prot(m), "pte, ro");
+
+	/* The FC bit should be ignored because EDAT is off */
+	p[0] = 42;  

I'd suggest to set p[0] = 0 here...

Quoted 5 lines

s390x/edat.c line 73

Quoted 1 line
s390x/edat.c new file +1 -0
+	protect_dat_entry(m, SEGMENT_ENTRY_FC, 4);  

... and change the value to 42 after enabling the protection ... otherwise you don't really test the non-working write protection here, do you?

but this is not the write protection. here I'm setting the bit for large pages. so first I write something, then I set the bit, then I check if I can still read it. if not, it means that the FC bit was not ignored (i.e. the entry was considered as a large page instead of a normal segment table entry pointing to a page table)

Write protection for segment entries _should_ work even with EDAT off, and that is in fact what the next test checks...

Quoted 4 lines

s390x/edat.c line 77

Quoted 4 lines
s390x/edat.c new file +4 -0
+	report(p[0] == 42, "pmd, fc=1, r/w");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_FC, 4);
+	p[0] = 0;
+

... this one here:

Quoted 11 lines

s390x/edat.c line 85

Quoted 8 lines
s390x/edat.c new file +8 -0
+	/* Segment protection should work even with EDAT off */
+	protect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+	expect_dat_fault();
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pmd, ro");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+
+	/* The FC bit should be ignored because EDAT is off*/  

Set p[0] to 0 again before enabling the protection? Or maybe use a different value than 42 below...?

why? we already checked that p[0] == 0, and if p[0] somehow still is 42, we are going to set it to 42 again

Quoted 2 lines

s390x/edat.c

Quoted 2 lines
s390x/edat.c new file +2 -0
+	protect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	p[0] = 42;

but! we should set it to 42 BEFORE setting the FC bit! I will fix this

and maybe add a few more comments to explain what's going on

Quoted 29 lines
Quoted 16 lines
s390x/edat.c new file +5 -0
+	report(p[0] == 42, "pud, fc=1, r/w");
+	unprotect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	p[0] = 0;
+
+	/* Region1/2/3 protection should not work, because EDAT is

off */

s390x/edat.c new file +10 -0
+	protect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 42;
+	report(p[0] == 42, "pud, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 0;
+
+	protect_dat_entry(m, REGION_ENTRY_P, 2);
+	p[0] = 42;
+	report(p[0] == 42, "p4d, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 2);

s390x/edat.c line 112

Quoted 10 lines
s390x/edat.c new file +10 -0
+	p[0] = 0;
+
+	protect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 42;
+	report(p[0] == 42, "pgd, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 0;
+
+	report_prefix_pop();
+}  

Thomas