Skip to contentSkip to conversation

s390: Add support for large pages

PATCH Superseded Claudio Imbrenda 2 participants 14 messages Feb 23, 2021

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover s390: Add support for large pages

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

Add a simple testcase for EDAT1 and EDAT2.

v1->v2

* split patch 2 -> new patch 2 and new patch 3
* patch 2: fixes pgtable.h, also fixes wrong usage of REGION_TABLE_LENGTH instead of SEGMENT_TABLE_LENGTH
* patch 3: introduces new macros and functions for large pages
* patch 4: remove erroneous double call to pte_alloc in get_pte
* patch 4: added comment in mmu.c to bridge the s390x architecural names with the Linux ones used in the kvm-unit-tests
* patch 5: added and fixed lots of comments to explain what's going on
* patch 5: set FC for region 3 after writing the canary, like for segments
* patch 5: use uintptr_t instead of intptr_t for set_prefix
* patch 5: introduce new macro PGD_PAGE_SHIFT instead of using magic value 41
* patch 5: use VIRT(0) instead of mem to make it more clear what we are doing, even though VIRT(0) expands to mem

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

 s390x/Makefile          |   1 +
 lib/s390x/asm/pgtable.h |  40 +++++-
 lib/libcflat.h          |   2 +
 lib/s390x/mmu.h         |  73 +++++++++-
 lib/s390x/mmu.c         | 260 ++++++++++++++++++++++++++++++++----
 s390x/edat.c            | 285 ++++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg     |   3 +
 7 files changed, 633 insertions(+), 31 deletions(-)
 create mode 100644 s390x/edat.c
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/5 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
#2 lore raw

PATCH v2 2/5 s390x: lib: fix pgtable.h

In reply to Claudio Imbrenda

Fix 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
* pud entries should use SEGMENT_TABLE_LENGTH, as they point to segment tables

---
 lib/s390x/asm/pgtable.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/s390x/asm/pgtable.h +4 -5
index 277f3480..a2ff2d4e 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
@@ -183,7 +183,7 @@ static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr)
 	if (pud_none(*pud)) {
 		pmd_t *pmd = pmd_alloc_one();
 		pud_val(*pud) = __pa(pmd) | REGION_ENTRY_TT_REGION3 |
-				REGION_TABLE_LENGTH;
+				SEGMENT_TABLE_LENGTH;
 	}
 	return pmd_offset(pud, addr);
 }
@@ -202,15 +202,14 @@ 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"
Signature · 1 line
2.26.2
#3 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] s390x: lib: fix pgtable.h

Quoted 26 lines · Claudio Imbrenda

Fix 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
* pud entries should use SEGMENT_TABLE_LENGTH, as they point to segment tables

---
 lib/s390x/asm/pgtable.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/s390x/asm/pgtable.h +1 -1
index 277f3480..a2ff2d4e 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

lib/s390x/asm/pgtable.h static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr) · line 186

Quoted 5 lines
lib/s390x/asm/pgtable.h +1 -1
@@ -183,7 +183,7 @@ static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr)
 	if (pud_none(*pud)) {
 		pmd_t *pmd = pmd_alloc_one();
 		pud_val(*pud) = __pa(pmd) | REGION_ENTRY_TT_REGION3 |
-				REGION_TABLE_LENGTH;
+				SEGMENT_TABLE_LENGTH;

@David: I'd much rather have REGION_ENTRY_LENGTH instead of REGION_TABLE_LENGTH and SEGMENT_TABLE_LENGTH.

My argument is that this is not really an attribute of the table and very much specific to the format of the (region table) entry. We already have the table order as a length anyway...

Could you tell me what you had in mind when splitting this?

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

Quoted 10 lines
lib/s390x/asm/pgtable.h +1 -2
 	}
 	return pmd_offset(pud, addr);
 }
@@ -202,15 +202,14 @@ 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;

Uhhhh good catch!

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

Quoted 12 lines
lib/s390x/asm/pgtable.h +1 -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"

IPTE ignores that data but having the page mask also doesn't hurt so generally this is not a fix, right?

#4 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] s390x: lib: fix pgtable.h

Quoted 21 lines · On Tue, 23 Feb 2021 15:31:06 +0100 Janosch Frank <[email protected]> wrote:
Quoted 20 lines · Claudio Imbrenda

Fix 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
* pud entries should use SEGMENT_TABLE_LENGTH, as they point to segment tables

---
 lib/s390x/asm/pgtable.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/s390x/asm/pgtable.h +0 -0
index 277f3480..a2ff2d4e 100644
@@ -60,7 +60,7 @@
 #define SEGMENT_SHIFT			20
 
Quoted 16 lines

lib/s390x/asm/pgtable.h static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr) · line 186

Quoted 13 lines
lib/s390x/asm/pgtable.h +1 -1
 #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
@@ -183,7 +183,7 @@ static inline pmd_t *pmd_alloc(pud_t *pud,
 unsigned long addr) if (pud_none(*pud)) {
 		pmd_t *pmd = pmd_alloc_one();
 		pud_val(*pud) = __pa(pmd) |

REGION_ENTRY_TT_REGION3 |

lib/s390x/asm/pgtable.h +1 -1
-				REGION_TABLE_LENGTH;
+				SEGMENT_TABLE_LENGTH;  

@David: I'd much rather have REGION_ENTRY_LENGTH instead of REGION_TABLE_LENGTH and SEGMENT_TABLE_LENGTH.

I'm weakly against it

Quoted 1 line

My argument is that this is not really an attribute of the table and

it actually is an attribute of the table. the *_TABLE_LENGTH fields tell how long the _table pointed to_ is. we always allocate the full 4 pages, so in our case it will always be 0x3.

segment table entries don't have a length because they point to page tables. region3 table entries point to segment tables, so they have SEGMENT_TABLE_LENGTH in their length field.

Quoted 30 lines

very much specific to the format of the (region table) entry. We already have the table order as a length anyway...

Could you tell me what you had in mind when splitting this?

Quoted 11 lines
lib/s390x/asm/pgtable.h +0 -1
 	}
 	return pmd_offset(pud, addr);
 }
@@ -202,15 +202,14 @@ 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 |

lib/s390x/asm/pgtable.h +1 -1
-				SEGMENT_TABLE_LENGTH;
+		pmd_val(*pmd) = __pa(pte) |
Quoted 1 line

SEGMENT_ENTRY_TT_SEGMENT;

Uhhhh good catch!

Quoted 10 lines
lib/s390x/asm/pgtable.h +0 -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;

lib/s390x/asm/pgtable.h +1 -0
+	unsigned long table_origin = (unsigned long)p_pte;
Quoted 6 lines

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

Quoted 3 lines
lib/s390x/asm/pgtable.h +0 -0
 	asm volatile(
 		"	ipte %0,%1\n"

IPTE ignores that data but having the page mask also doesn't hurt so generally this is not a fix, right?

apart from avoiding an unnecessary operation, there is a small nit:
IPTE wants the address of the page table, ignoring the rightmost _11_ bits. with PAGE_MASK we ignore the rightmost _12_ bits instead. it's not an issue in practice, because we allocate one page table per page anyway, wasting the second half of the page, so in our case that stray bit will always be 0. but in case we decide to allocate the page tables more tightly, or in case some testcase wants to manually play tricks with the page tables, there might be the risk that IPTE would target the wrong page table.

by not clearing the lower 12 bits we not only save an unnecessary operation, but we are actually more architecturally correct.

#5 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] s390x: lib: fix pgtable.h

Quoted 41 lines · Claudio Imbrenda
Quoted 21 lines · On Tue, 23 Feb 2021 15:31:06 +0100 Janosch Frank <[email protected]> wrote:
Quoted 20 lines · Claudio Imbrenda

Fix 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
* pud entries should use SEGMENT_TABLE_LENGTH, as they point to segment tables

---
 lib/s390x/asm/pgtable.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/s390x/asm/pgtable.h +0 -0
index 277f3480..a2ff2d4e 100644
@@ -60,7 +60,7 @@
 #define SEGMENT_SHIFT			20
 
Quoted 16 lines

lib/s390x/asm/pgtable.h static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr) · line 186

Quoted 13 lines
lib/s390x/asm/pgtable.h +1 -1
 #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
@@ -183,7 +183,7 @@ static inline pmd_t *pmd_alloc(pud_t *pud,
 unsigned long addr) if (pud_none(*pud)) {
 		pmd_t *pmd = pmd_alloc_one();
 		pud_val(*pud) = __pa(pmd) |

REGION_ENTRY_TT_REGION3 |

lib/s390x/asm/pgtable.h +1 -1
-				REGION_TABLE_LENGTH;
+				SEGMENT_TABLE_LENGTH;  

@David: I'd much rather have REGION_ENTRY_LENGTH instead of REGION_TABLE_LENGTH and SEGMENT_TABLE_LENGTH.

Quoted 8 lines

I'm weakly against it

Quoted 1 line

My argument is that this is not really an attribute of the table and

it actually is an attribute of the table. the *_TABLE_LENGTH fields tell how long the _table pointed to_ is. we always allocate the full 4 pages, so in our case it will always be 0x3.

It's part of the entry nevertheless and should not be a *_TABLE_* constant. It's used in entries and has a very specific format that only makes sense if it's being used in a region entry.

Every other thing that you or into the entry apart from the address is named *_ENTRY_* so this should be too.

Quoted 3 lines

segment table entries don't have a length because they point to page tables. region3 table entries point to segment tables, so they have SEGMENT_TABLE_LENGTH in their length field.

Btw. I'd guess the SEGMENT_TABLE_LENGTH name is the reason that you had to fix the problem in the next hunk...

Quoted 47 lines
Quoted 30 lines

very much specific to the format of the (region table) entry. We already have the table order as a length anyway...

Could you tell me what you had in mind when splitting this?

Quoted 11 lines
lib/s390x/asm/pgtable.h +0 -1
 	}
 	return pmd_offset(pud, addr);
 }
@@ -202,15 +202,14 @@ 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 |

lib/s390x/asm/pgtable.h +1 -1
-				SEGMENT_TABLE_LENGTH;
+		pmd_val(*pmd) = __pa(pte) |
Quoted 1 line

SEGMENT_ENTRY_TT_SEGMENT;

Uhhhh good catch!

Quoted 10 lines
lib/s390x/asm/pgtable.h +0 -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;

lib/s390x/asm/pgtable.h +1 -0
+	unsigned long table_origin = (unsigned long)p_pte;
Quoted 6 lines

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

Quoted 3 lines
lib/s390x/asm/pgtable.h +0 -0
 	asm volatile(
 		"	ipte %0,%1\n"

IPTE ignores that data but having the page mask also doesn't hurt so generally this is not a fix, right?

apart from avoiding an unnecessary operation, there is a small nit:
IPTE wants the address of the page table, ignoring the rightmost _11_ bits. with PAGE_MASK we ignore the rightmost _12_ bits instead. it's not an issue in practice, because we allocate one page table per page anyway, wasting the second half of the page, so in our case that stray bit will always be 0. but in case we decide to allocate the page tables more tightly, or in case some testcase wants to manually play tricks with the page tables, there might be the risk that IPTE would target the wrong page table.

Quoted 3 lines

by not clearing the lower 12 bits we not only save an unnecessary operation, but we are actually more architecturally correct.

Right, the old 2k pgtable thing...
Would you mind putting that into the patch description?

#6 lore raw

Re: [kvm-unit-tests PATCH v2 2/5] s390x: lib: fix pgtable.h

Quoted 47 lines · On Tue, 23 Feb 2021 16:44:43 +0100 Janosch Frank <[email protected]> wrote:
Quoted 46 lines · Claudio Imbrenda
Quoted 21 lines · On Tue, 23 Feb 2021 15:31:06 +0100 Janosch Frank <[email protected]> wrote:
Quoted 20 lines · Claudio Imbrenda

Fix 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
* pud entries should use SEGMENT_TABLE_LENGTH, as they point to segment tables

---
 lib/s390x/asm/pgtable.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/s390x/asm/pgtable.h +0 -0
index 277f3480..a2ff2d4e 100644
@@ -60,7 +60,7 @@
 #define SEGMENT_SHIFT			20
 
Quoted 16 lines

lib/s390x/asm/pgtable.h static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr) · line 186

Quoted 13 lines
lib/s390x/asm/pgtable.h +1 -1
 #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 @@ -183,7 +183,7 @@ static inline pmd_t *pmd_alloc(pud_t *pud, unsigned long addr) if (pud_none(*pud)) {

lib/s390x/asm/pgtable.h +0 -0
 		pmd_t *pmd = pmd_alloc_one();
 		pud_val(*pud) = __pa(pmd) |

REGION_ENTRY_TT_REGION3 |

lib/s390x/asm/pgtable.h +1 -1
-				REGION_TABLE_LENGTH;
+				SEGMENT_TABLE_LENGTH;    

@David: I'd much rather have REGION_ENTRY_LENGTH instead of REGION_TABLE_LENGTH and SEGMENT_TABLE_LENGTH.

I'm weakly against it

Quoted 2 lines

My argument is that this is not really an attribute of the table and

Quoted 8 lines
Quoted 4 lines

it actually is an attribute of the table. the *_TABLE_LENGTH fields tell how long the _table pointed to_ is. we always allocate the full 4 pages, so in our case it will always be 0x3.

It's part of the entry nevertheless and should not be a *_TABLE_* constant. It's used in entries and has a very specific format that only makes sense if it's being used in a region entry.

or in the ASCE, yes

Quoted 2 lines

Every other thing that you or into the entry apart from the address is named *_ENTRY_* so this should be too.

fair enough

Quoted 6 lines
Quoted 3 lines

segment table entries don't have a length because they point to page tables. region3 table entries point to segment tables, so they have SEGMENT_TABLE_LENGTH in their length field.

Btw. I'd guess the SEGMENT_TABLE_LENGTH name is the reason that you had to fix the problem in the next hunk...

yes, I am quite sure of this too :)

I'll figure out a better name and respin

Quoted 47 lines
Quoted 47 lines
Quoted 30 lines

very much specific to the format of the (region table) entry. We already have the table order as a length anyway...

Could you tell me what you had in mind when splitting this?

Quoted 11 lines
lib/s390x/asm/pgtable.h +0 -1
 	}
 	return pmd_offset(pud, addr);
 }
@@ -202,15 +202,14 @@ 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 |

lib/s390x/asm/pgtable.h +1 -1
-				SEGMENT_TABLE_LENGTH;
+		pmd_val(*pmd) = __pa(pte) |
Quoted 1 line

SEGMENT_ENTRY_TT_SEGMENT;

Uhhhh good catch!

Quoted 10 lines
lib/s390x/asm/pgtable.h +0 -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;

lib/s390x/asm/pgtable.h +1 -0
+	unsigned long table_origin = (unsigned long)p_pte;
Quoted 6 lines

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

Quoted 3 lines
lib/s390x/asm/pgtable.h +0 -0
 	asm volatile(
 		"	ipte %0,%1\n"

IPTE ignores that data but having the page mask also doesn't hurt so generally this is not a fix, right?

apart from avoiding an unnecessary operation, there is a small nit:
IPTE wants the address of the page table, ignoring the rightmost _11_ bits. with PAGE_MASK we ignore the rightmost _12_ bits instead. it's not an issue in practice, because we allocate one page table per page anyway, wasting the second half of the page, so in our case that stray bit will always be 0. but in case we decide to allocate the page tables more tightly, or in case some testcase wants to manually play tricks with the page tables, there might be the risk that IPTE would target the wrong page table.

Quoted 6 lines
Quoted 3 lines

by not clearing the lower 12 bits we not only save an unnecessary operation, but we are actually more architecturally correct.

Right, the old 2k pgtable thing...
Would you mind putting that into the patch description?

will do

#7 lore raw

PATCH v2 3/5 s390x: lib: improve pgtable.h

In reply to Claudio Imbrenda

Improve pgtable.h:

* 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 | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/s390x/asm/pgtable.h +31 -0
index a2ff2d4e..70d4afde 100644
@@ -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)
@@ -216,6 +219,34 @@ static inline void ipte(unsigned long vaddr, pteval_t *p_pte)
 		: : "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
Acked-by on v2 3/5
#8 lore raw

Re: [kvm-unit-tests PATCH v2 3/5] s390x: lib: improve pgtable.h

Quoted 6 lines · Claudio Imbrenda

Improve pgtable.h:

* add macros to check whether a pmd or a pud are large / huge
* add idte functions for pmd, pud, p4d and pgd

Could you please make the subject more specific? "s390x: lib: Add idte and huge entry check functions"

Quoted 54 lines
---
 lib/s390x/asm/pgtable.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/s390x/asm/pgtable.h +31 -0
index a2ff2d4e..70d4afde 100644
@@ -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)
@@ -216,6 +219,34 @@ static inline void ipte(unsigned long vaddr, pteval_t *p_pte)
 		: : "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_ */
#9 lore raw

Re: [kvm-unit-tests PATCH v2 3/5] s390x: lib: improve pgtable.h

Quoted 10 lines · On Tue, 23 Feb 2021 15:35:28 +0100 Janosch Frank <[email protected]> wrote:
Quoted 6 lines · Claudio Imbrenda

Improve pgtable.h:

* add macros to check whether a pmd or a pud are large / huge
* add idte functions for pmd, pud, p4d and pgd

Could you please make the subject more specific? "s390x: lib: Add idte and huge entry check functions"

will do

Quoted 57 lines
Quoted 54 lines
---
 lib/s390x/asm/pgtable.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/s390x/asm/pgtable.h +21 -0
index a2ff2d4e..70d4afde 100644
@@ -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)
@@ -216,6 +219,34 @@ static inline void ipte(unsigned long vaddr,
 pteval_t *p_pte) : : "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); +}

lib/s390x/asm/pgtable.h +4 -0
+
+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_ */

#10 lore raw

PATCH v2 4/5 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 | 260 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 307 insertions(+), 26 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 +236 -24

310 lines not on this page · raw message

Signature · 1 line
2.26.2
#11 lore raw

PATCH v2 5/5 s390x: edat test

In reply to Claudio Imbrenda

Simple EDAT test.

---
 s390x/Makefile      |   1 +
 s390x/edat.c        | 285 ++++++++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg |   3 +
 3 files changed, 289 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 +285 -0

285 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
#12 lore raw

Re: [kvm-unit-tests PATCH v2 5/5] s390x: edat test

Quoted 42 lines · Claudio Imbrenda

Simple EDAT test.

---
 s390x/Makefile      |   1 +
 s390x/edat.c        | 285 ++++++++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg |   3 +
 3 files changed, 289 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 +14 -0
new file mode 100644
index 00000000..0bd16efc
@@ -0,0 +1,285 @@
+/* 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>

s390x/edat.c line 24

Quoted 10 lines
s390x/edat.c new file +10 -0
+#include <asm/pgtable.h>
+#include <asm-generic/barrier.h>
+
+#define PGD_PAGE_SHIFT (REGION1_SHIFT - PAGE_SHIFT)
+
+#define TEID_ADDR	PAGE_MASK
+#define TEID_AI		0x003
+#define TEID_M		0x004
+#define TEID_A		0x008
+#define TEID_FS		0xc00

I'm thinking about presenting faults in more detail in the future i.e. printing the TEID or at least the important parts of it. I'd guess this should go into the library.

Quoted 33 lines
s390x/edat.c new file +33 -0
+
+#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();
+	lc->trans_exc_id = 0;
+}
+
+/*
+ * Check if a non-access-list protection exception happened for the given
+ * address, in the primary address space.
+ */
+static bool check_pgm_prot(void *ptr)
+{
+	unsigned long teid = lc->trans_exc_id;
+
+	if (lc->pgm_int_code != PGM_INT_CODE_PROTECTION)
+		return false;
+	/*
+	 * if for any reason TEID_M is not present, the rest of the field is
+	 * not meaningful, so no point in checking it
+	 */
+	if (~teid & TEID_M)
+		return true;

s390x/edat.c line 67

Quoted 10 lines
s390x/edat.c new file +10 -0
+	return (~teid & TEID_A) &&
+		((teid & TEID_ADDR) == ((uint64_t)ptr & PAGE_MASK)) &&
+		!(teid & TEID_AI);
+}
+
+static void test_dat(void)
+{
+	report_prefix_push("edat off");
+	/* disable EDAT */
+	ctl_clear_bit(0, 23);

It's about time we properly define CTL0 bits.

Quoted 230 lines
s390x/edat.c new file +218 -0
+
+	/* Check some basics */
+	p[0] = 42;
+	report(p[0] == 42, "pte, r/w");
+	p[0] = 0;
+
+	/* Write protect the page and try to write, expect a fault */
+	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 (for large pages) should be ignored because EDAT is
+	 * off. We set a value and then we try to read it back again after
+	 * setting the FC bit. This way we can check if large pages were
+	 * erroneously enabled despite EDAT being off.
+	 */
+	p[0] = 42;
+	protect_dat_entry(m, SEGMENT_ENTRY_FC, 4);
+	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, try to write
+	 * anyway and expect a fault
+	 */
+	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, like above */
+	p[0] = 42;
+	protect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	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 the various region1/2/3 entries and write, expect the
+	 * write to be successful.
+	 */
+	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);
+	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();
+}
+
+static void test_edat1(void)
+{
+	report_prefix_push("edat1");
+	/* Enable EDAT */
+	ctl_set_bit(0, 23);
+	p[0] = 0;
+
+	/*
+	 * Segment protection should work normally, try to write and expect
+	 * a fault.
+	 */
+	expect_dat_fault();
+	protect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pmd, ro");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+
+	/*
+	 * Region1/2/3 protection should work now, because EDAT is on. Try
+	 * to write anyway and expect a fault.
+	 */
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pud, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 3);
+
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 2);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "p4d, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 2);
+
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pgd, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 1);
+
+	/* Large pages should work */
+	p[0] = 42;
+	install_large_page(root, 0, mem);
+	report(p[0] == 42, "pmd, large");
+
+	/*
+	 * Prefixing should not work with large pages. Since the lower
+	 * addresses are mapped with small pages, which are subject to
+	 * prefixing, and the pages mapped with large pages are not subject
+	 * to prefixing, this is the resulting scenario:
+	 *
+	 * virtual 0 = real 0 -> absolute prefix_buf
+	 * virtual prefix_buf = real prefix_buf -> absolute 0
+	 * VIRT(0) -> absolute 0
+	 * VIRT(prefix_buf) -> absolute prefix_buf
+	 *
+	 * The testcase checks if the memory at virtual 0 has the same
+	 * content as the memory at VIRT(prefix_buf) and the memory at
+	 * VIRT(0) has the same content as the memory at virtual prefix_buf.
+	 * If prefixing is erroneously applied for large pages, the testcase
+	 * will therefore fail.
+	 */
+	report(!memcmp(0, VIRT(prefix_buf), LC_SIZE) &&
+		!memcmp(prefix_buf, VIRT(0), LC_SIZE),
+		"pmd, large, prefixing");
+
+	report_prefix_pop();
+}
+
+static void test_edat2(void)
+{
+	report_prefix_push("edat2");
+	p[0] = 42;
+
+	/* Huge pages should work */
+	install_huge_page(root, 0, mem);
+	report(p[0] == 42, "pud, huge");
+
+	/* Prefixing should not work with huge pages, just like large pages */
+	report(!memcmp(0, VIRT(prefix_buf), LC_SIZE) &&
+		!memcmp(prefix_buf, VIRT(0), LC_SIZE),
+		"pmd, large, prefixing");
+
+	report_prefix_pop();
+}
+
+static unsigned int setup(void)
+{
+	bool has_edat1 = test_facility(8);
+	bool has_edat2 = test_facility(78);
+	unsigned long pa, va;
+
+	if (has_edat2 && !has_edat1)
+		report_abort("EDAT2 available, but EDAT1 not available");
+
+	/* Setup DAT 1:1 mapping and memory management */
+	setup_vm();
+	root = (void *)(stctg(1) & PAGE_MASK);
+
+	/*
+	 * Get a pgd worth of virtual memory, so we can test things later
+	 * without interfering with the test code or the interrupt handler
+	 */
+	mem = alloc_vpages_aligned(BIT_ULL(PGD_PAGE_SHIFT), PGD_PAGE_SHIFT);
+	assert(mem);
+	va = (unsigned long)mem;
+
+	/* Map the first 1GB of real memory */
+	for (pa = 0; pa < SZ_1G; pa += PAGE_SIZE, va += PAGE_SIZE)
+		install_page(root, pa, (void *)va);
+
+	/*
+	 * Move the lowcore to a known non-zero location. This is needed
+	 * later to check whether prefixing is working with large pages.
+	 */
+	assert((unsigned long)&prefix_buf < SZ_2G);
+	memcpy(prefix_buf, 0, LC_SIZE);
+	set_prefix((uint32_t)(uintptr_t)prefix_buf);
+	/* Clear the old copy */
+	memset(prefix_buf, 0, LC_SIZE);
+
+	/* m will point to tmp through the new virtual mapping */
+	m = VIRT(&tmp);
+	/* p is the same as m but volatile */
+	p = (volatile unsigned int *)m;
+
+	return has_edat1 + has_edat2;
+}
+
+int main(void)
+{
+	unsigned int edat;
+
+	report_prefix_push("edat");
+	edat = setup();
+
+	test_dat();
+
+	if (edat)
+		test_edat1();
+	else
+		report_skip("EDAT not available");
+
+	if (edat >= 2)
+		test_edat2();
+	else
+		report_skip("EDAT2 not available");
+
+	report_prefix_pop();
+	return report_summary();
+}
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
#13 lore raw

Re: [kvm-unit-tests PATCH v2 5/5] s390x: edat test

Quoted 50 lines · On Tue, 23 Feb 2021 15:57:30 +0100 Janosch Frank <[email protected]> wrote:
Quoted 49 lines · Claudio Imbrenda

Simple EDAT test.

---
 s390x/Makefile      |   1 +
 s390x/edat.c        | 285
++++++++++++++++++++++++++++++++++++++++++++ s390x/unittests.cfg |
 3 + 3 files changed, 289 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 +21 -0
new file mode 100644
index 00000000..0bd16efc
@@ -0,0 +1,285 @@
+/* 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 PGD_PAGE_SHIFT (REGION1_SHIFT - PAGE_SHIFT)
+
+#define TEID_ADDR	PAGE_MASK
+#define TEID_AI		0x003
Quoted 8 lines

s390x/edat.c line 24

Quoted 3 lines
s390x/edat.c new file +3 -0
+#define TEID_M		0x004
+#define TEID_A		0x008
+#define TEID_FS		0xc00  

I'm thinking about presenting faults in more detail in the future i.e. printing the TEID or at least the important parts of it. I'd guess this should go into the library.

will do

Quoted 41 lines
Quoted 41 lines
s390x/edat.c new file +3 -0
+
+#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 +12 -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 non-access-list protection exception happened for

the given
+ * address, in the primary address space.
+ */

s390x/edat.c new file +8 -0
+static bool check_pgm_prot(void *ptr)
+{
+	unsigned long teid = lc->trans_exc_id;
+
+	if (lc->pgm_int_code != PGM_INT_CODE_PROTECTION)
+		return false;
+	/*
+	 * if for any reason TEID_M is not present, the rest of

the field is

s390x/edat.c new file +6 -0
+	 * not meaningful, so no point in checking it
+	 */
+	if (~teid & TEID_M)
+		return true;
+	return (~teid & TEID_A) &&
+		((teid & TEID_ADDR) == ((uint64_t)ptr &

PAGE_MASK)) &&

s390x/edat.c new file +2 -0
+		!(teid & TEID_AI);
+}
Quoted 8 lines

s390x/edat.c line 67

Quoted 6 lines
s390x/edat.c new file +6 -0
+
+static void test_dat(void)
+{
+	report_prefix_push("edat off");
+	/* disable EDAT */
+	ctl_clear_bit(0, 23);  

It's about time we properly define CTL0 bits.

will do

Quoted 254 lines
Quoted 253 lines
s390x/edat.c new file +7 -0
+
+	/* Check some basics */
+	p[0] = 42;
+	report(p[0] == 42, "pte, r/w");
+	p[0] = 0;
+
+	/* Write protect the page and try to write, expect a fault

*/

s390x/edat.c new file +8 -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 (for large pages) should be ignored because

EDAT is

s390x/edat.c new file +1 -0
+	 * off. We set a value and then we try to read it back

again after

s390x/edat.c new file +1 -0
+	 * setting the FC bit. This way we can check if large

pages were

s390x/edat.c new file +10 -0
+	 * erroneously enabled despite EDAT being off.
+	 */
+	p[0] = 42;
+	protect_dat_entry(m, SEGMENT_ENTRY_FC, 4);
+	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, try

to write

s390x/edat.c new file +9 -0
+	 * anyway and expect a fault
+	 */
+	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, like

above */

s390x/edat.c new file +8 -0
+	p[0] = 42;
+	protect_dat_entry(m, REGION3_ENTRY_FC, 3);
+	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 +1 -0
+	 * Protect the various region1/2/3 entries and write,

expect the

s390x/edat.c new file +32 -0
+	 * write to be successful.
+	 */
+	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);
+	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();
+}
+
+static void test_edat1(void)
+{
+	report_prefix_push("edat1");
+	/* Enable EDAT */
+	ctl_set_bit(0, 23);
+	p[0] = 0;
+
+	/*
+	 * Segment protection should work normally, try to write

and expect

s390x/edat.c new file +10 -0
+	 * a fault.
+	 */
+	expect_dat_fault();
+	protect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pmd, ro");
+	unprotect_dat_entry(m, SEGMENT_ENTRY_P, 4);
+
+	/*
+	 * Region1/2/3 protection should work now, because EDAT is

on. Try

s390x/edat.c new file +27 -0
+	 * to write anyway and expect a fault.
+	 */
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 3);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pud, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 3);
+
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 2);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "p4d, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 2);
+
+	expect_dat_fault();
+	protect_dat_entry(m, REGION_ENTRY_P, 1);
+	p[0] = 42;
+	report(!p[0] && check_pgm_prot(m), "pgd, ro");
+	unprotect_dat_entry(m, REGION_ENTRY_P, 1);
+
+	/* Large pages should work */
+	p[0] = 42;
+	install_large_page(root, 0, mem);
+	report(p[0] == 42, "pmd, large");
+
+	/*
+	 * Prefixing should not work with large pages. Since the

lower

s390x/edat.c new file +1 -0
+	 * addresses are mapped with small pages, which are

subject to

s390x/edat.c new file +1 -0
+	 * prefixing, and the pages mapped with large pages are

not subject

s390x/edat.c new file +8 -0
+	 * to prefixing, this is the resulting scenario:
+	 *
+	 * virtual 0 = real 0 -> absolute prefix_buf
+	 * virtual prefix_buf = real prefix_buf -> absolute 0
+	 * VIRT(0) -> absolute 0
+	 * VIRT(prefix_buf) -> absolute prefix_buf
+	 *
+	 * The testcase checks if the memory at virtual 0 has the

same

s390x/edat.c new file +1 -0
+	 * content as the memory at VIRT(prefix_buf) and the

memory at

s390x/edat.c new file +1 -0
+	 * VIRT(0) has the same content as the memory at virtual

prefix_buf.

s390x/edat.c new file +1 -0
+	 * If prefixing is erroneously applied for large pages,

the testcase

s390x/edat.c new file +19 -0
+	 * will therefore fail.
+	 */
+	report(!memcmp(0, VIRT(prefix_buf), LC_SIZE) &&
+		!memcmp(prefix_buf, VIRT(0), LC_SIZE),
+		"pmd, large, prefixing");
+
+	report_prefix_pop();
+}
+
+static void test_edat2(void)
+{
+	report_prefix_push("edat2");
+	p[0] = 42;
+
+	/* Huge pages should work */
+	install_huge_page(root, 0, mem);
+	report(p[0] == 42, "pud, huge");
+
+	/* Prefixing should not work with huge pages, just like

large pages */

s390x/edat.c new file +15 -0
+	report(!memcmp(0, VIRT(prefix_buf), LC_SIZE) &&
+		!memcmp(prefix_buf, VIRT(0), LC_SIZE),
+		"pmd, large, prefixing");
+
+	report_prefix_pop();
+}
+
+static unsigned int setup(void)
+{
+	bool has_edat1 = test_facility(8);
+	bool has_edat2 = test_facility(78);
+	unsigned long pa, va;
+
+	if (has_edat2 && !has_edat1)
+		report_abort("EDAT2 available, but EDAT1 not

available"); +

s390x/edat.c new file +6 -0
+	/* Setup DAT 1:1 mapping and memory management */
+	setup_vm();
+	root = (void *)(stctg(1) & PAGE_MASK);
+
+	/*
+	 * Get a pgd worth of virtual memory, so we can test

things later

s390x/edat.c new file +1 -0
+	 * without interfering with the test code or the interrupt

handler

s390x/edat.c new file +2 -0
+	 */
+	mem = alloc_vpages_aligned(BIT_ULL(PGD_PAGE_SHIFT),

PGD_PAGE_SHIFT);

s390x/edat.c new file +9 -0
+	assert(mem);
+	va = (unsigned long)mem;
+
+	/* Map the first 1GB of real memory */
+	for (pa = 0; pa < SZ_1G; pa += PAGE_SIZE, va += PAGE_SIZE)
+		install_page(root, pa, (void *)va);
+
+	/*
+	 * Move the lowcore to a known non-zero location. This is

needed

s390x/edat.c new file +1 -0
+	 * later to check whether prefixing is working with large

pages.

s390x/edat.c new file +37 -0
+	 */
+	assert((unsigned long)&prefix_buf < SZ_2G);
+	memcpy(prefix_buf, 0, LC_SIZE);
+	set_prefix((uint32_t)(uintptr_t)prefix_buf);
+	/* Clear the old copy */
+	memset(prefix_buf, 0, LC_SIZE);
+
+	/* m will point to tmp through the new virtual mapping */
+	m = VIRT(&tmp);
+	/* p is the same as m but volatile */
+	p = (volatile unsigned int *)m;
+
+	return has_edat1 + has_edat2;
+}
+
+int main(void)
+{
+	unsigned int edat;
+
+	report_prefix_push("edat");
+	edat = setup();
+
+	test_dat();
+
+	if (edat)
+		test_edat1();
+	else
+		report_skip("EDAT not available");
+
+	if (edat >= 2)
+		test_edat2();
+	else
+		report_skip("EDAT2 not available");
+
+	report_prefix_pop();
+	return report_summary();
+}
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