Skip to contentSkip to conversation

s390x update 2021-22-06

GIT PULL Applied Janosch Frank 2 participants 14 messages Jun 22, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Dear Paolo,

please merge or pull the following changes:

Merge:
https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/merge_requests/11

Pipeline:
https://gitlab.com/frankja/kvm-unit-tests/-/pipelines/324608397

Pull:
The following changes since commit f09465ac9044145f20435344d41566aede62fc08:

x86: Flush the TLB after setting user-bit (2021-06-17 14:36:25 -0400)

are available in the Git repository at:

https://gitlab.com/frankja/kvm-unit-tests.git s390x-pull-2021-22-06

for you to fetch changes up to d58ec2ec341cbea746944a8ae92c737041c35172:

s390x: edat test (2021-06-21 14:55:12 +0000)

Claudio Imbrenda (7):
s390x: lib: add and use macros for control register bits
libcflat: add SZ_1M and SZ_2G
s390x: lib: fix pgtable.h
s390x: lib: Add idte and other huge pages functions/macros
s390x: lib: add teid union and clear teid from lowcore
s390x: mmu: add support for large pages
s390x: edat test

Janosch Frank (5):
s390x: sie: Only overwrite r3 if it isn't needed anymore
s390x: selftest: Add prefixes to fix report output
s390x: Don't run PV testcases under tcg
configure: s390x: Check if the host key document exists
s390x: run: Skip PV tests when tcg is the accelerator

 configure                 |   5 +
 lib/libcflat.h            |   2 +
 lib/s390x/asm/arch_def.h  |  12 ++
 lib/s390x/asm/float.h     |   4 +-
 lib/s390x/asm/interrupt.h |  28 +++-
 lib/s390x/asm/pgtable.h   |  44 +++++-
 lib/s390x/interrupt.c     |   2 +
 lib/s390x/mmu.c           | 264 ++++++++++++++++++++++++++++++++----
 lib/s390x/mmu.h           |  84 +++++++++++-
 lib/s390x/sclp.c          |   4 +-
 s390x/Makefile            |   1 +
 s390x/cpu.S               |   2 +-
 s390x/diag288.c           |   2 +-
 s390x/edat.c              | 274 ++++++++++++++++++++++++++++++++++++++
 s390x/gs.c                |   2 +-
 s390x/iep.c               |   4 +-
 s390x/run                 |   5 +
 s390x/selftest.c          |  26 ++--
 s390x/skrf.c              |   2 +-
 s390x/smp.c               |   8 +-
 s390x/unittests.cfg       |   3 +
 s390x/vector.c            |   2 +-
 scripts/s390x/func.bash   |   3 +
 23 files changed, 724 insertions(+), 59 deletions(-)
 create mode 100644 s390x/edat.c
Signature · 1 line
2.31.1
#1 lore raw

[kvm-unit-tests GIT PULL 01/12] s390x: sie: Only overwrite r3 if it isn't needed anymore

The lmg overwrites r3 which we later use to reference the fprs and fpc. Let's do the lmg at the end where overwriting is fine.

---
 s390x/cpu.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
s390x/cpu.S +1 -1
index e2ad56c8..82b5e25d 100644
@@ -81,11 +81,11 @@ sie64a:
 	stg	%r3,__SF_SIE_SAVEAREA(%r15)	# save guest register save area
 
 	# Load guest's gprs, fprs and fpc
-	lmg	%r0,%r13,SIE_SAVEAREA_GUEST_GRS(%r3)
 	.irp i, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
 	ld	\i, \i * 8 + SIE_SAVEAREA_GUEST_FPRS(%r3)
 	.endr
 	lfpc	SIE_SAVEAREA_GUEST_FPC(%r3)
+	lmg	%r0,%r13,SIE_SAVEAREA_GUEST_GRS(%r3)
 
 	# Move scb ptr into r14 for the sie instruction
 	lg	%r14,__SF_SIE_CONTROL(%r15)
Signature · 1 line
2.31.1
#2 lore raw

[kvm-unit-tests GIT PULL 02/12] s390x: selftest: Add prefixes to fix report output

In reply to Janosch Frank

To make our TAP parser (and me) happy we don't want to have two reports with exactly the same wording so I added in two new prefix pushes.

Also moving the code inside of the region of a prefix will give us more data when a problem arises.

---
 s390x/selftest.c | 26 ++++++++++++++++++--------
 1 file changed, 18 insertions(+), 8 deletions(-)
s390x/selftest.c +18 -8
index b2fe2e7b..0f099ca0 100644
@@ -40,19 +40,28 @@ static void test_pgm_int(void)
 
 static void test_malloc(void)
 {
-	int *tmp = malloc(sizeof(int));
-	int *tmp2 = malloc(sizeof(int));
+	int *tmp, *tmp2;
 
+	report_prefix_push("malloc");
+
+	report_prefix_push("ptr_0");
+	tmp = malloc(sizeof(int));
+	report((uintptr_t)tmp & 0xf000000000000000ul, "allocated memory");
 	*tmp = 123456789;
+	mb();
+	report(*tmp == 123456789, "wrote allocated memory");
+	report_prefix_pop();
+
+	report_prefix_push("ptr_1");
+	tmp2 = malloc(sizeof(int));
+	report((uintptr_t)tmp2 & 0xf000000000000000ul,
+	       "allocated memory");
 	*tmp2 = 123456789;
 	mb();
+	report((*tmp2 == 123456789), "wrote allocated memory");
+	report_prefix_pop();
 
-	report((uintptr_t)tmp & 0xf000000000000000ul, "malloc: got vaddr");
-	report(*tmp == 123456789, "malloc: access works");
-	report((uintptr_t)tmp2 & 0xf000000000000000ul,
-	       "malloc: got 2nd vaddr");
-	report((*tmp2 == 123456789), "malloc: access works");
-	report(tmp != tmp2, "malloc: addresses differ");
+	report(tmp != tmp2, "allocated memory addresses differ");
 
 	expect_pgm_int();
 	configure_dat(0);
@@ -62,6 +71,7 @@ static void test_malloc(void)
 
 	free(tmp);
 	free(tmp2);
+	report_prefix_pop();
 }
 
 int main(int argc, char**argv)
Signature · 1 line
2.31.1
#3 lore raw

[kvm-unit-tests GIT PULL 03/12] s390x: Don't run PV testcases under tcg

In reply to Janosch Frank

The UV call facility is only available on hardware.

---
 scripts/s390x/func.bash | 3 +++
 1 file changed, 3 insertions(+)
scripts/s390x/func.bash +3 -0
index b3912081..bf799a56 100644
@@ -21,6 +21,9 @@ function arch_cmd_s390x()
 	"$cmd" "$testname" "$groups" "$smp" "$kernel" "$opts" "$arch" "$check" "$accel" "$timeout"
 
 	# run PV test case
+	if [ "$ACCEL" = 'tcg' ]; then
+		return
+	fi
 	kernel=${kernel%.elf}.pv.bin
 	testname=${testname}_PV
 	if [ ! -f "${kernel}" ]; then
Signature · 1 line
2.31.1
#4 lore raw

[kvm-unit-tests GIT PULL 04/12] configure: s390x: Check if the host key document exists

In reply to Janosch Frank

I'd rather have a readable error message than make failing the build with cryptic errors.

---
 configure | 5 +++++
 1 file changed, 5 insertions(+)
configure +5 -0
index b8442d61..71aaad43 100755
@@ -137,6 +137,11 @@ while [[ "$1" = -* ]]; do
     esac
 done
 
+if [ -n "$host_key_document" ] && [ ! -f "$host_key_document" ]; then
+    echo "Host key document doesn't exist at the specified location."
+    exit 1
+fi
+
 if [ "$erratatxt" ] && [ ! -f "$erratatxt" ]; then
     echo "erratatxt: $erratatxt does not exist or is not a regular file"
     exit 1
Signature · 1 line
2.31.1
#5 lore raw

[kvm-unit-tests GIT PULL 06/12] s390x: lib: add and use macros for control register bits

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Add CTL0_* and CTL2_* macros for specific control register bits.

Replace all hardcoded values in the library and in the existing testcases so that they use the new macros.

Message-Id: <[email protected]>

---
 lib/s390x/asm/arch_def.h  | 12 ++++++++++++
 lib/s390x/asm/float.h     |  4 ++--
 lib/s390x/asm/interrupt.h |  4 ++--
 lib/s390x/sclp.c          |  4 ++--
 s390x/diag288.c           |  2 +-
 s390x/gs.c                |  2 +-
 s390x/iep.c               |  4 ++--
 s390x/skrf.c              |  2 +-
 s390x/smp.c               |  8 ++++----
 s390x/vector.c            |  2 +-
 10 files changed, 28 insertions(+), 16 deletions(-)
lib/s390x/asm/arch_def.h +12 -0
index 76f9e386..3aa5da9c 100644
@@ -229,6 +229,18 @@ static inline uint64_t stctg(int cr)
 	return value;
 }
 
+#define CTL0_LOW_ADDR_PROT	(63 - 35)
+#define CTL0_EDAT		(63 - 40)
+#define CTL0_IEP		(63 - 43)
+#define CTL0_AFP		(63 - 45)
+#define CTL0_VECTOR		(63 - 46)
+#define CTL0_EMERGENCY_SIGNAL	(63 - 49)
+#define CTL0_EXTERNAL_CALL	(63 - 50)
+#define CTL0_CLOCK_COMPARATOR	(63 - 52)
+#define CTL0_SERVICE_SIGNAL	(63 - 54)
+
+#define CTL2_GUARDED_STORAGE	(63 - 59)
+
 static inline void ctl_set_bit(int cr, unsigned int bit)
 {
         uint64_t reg;
lib/s390x/asm/float.h +2 -2
index eb752050..73c642d4 100644
@@ -38,12 +38,12 @@ static inline void set_fpc_dxc(uint8_t dxc)
 
 static inline void afp_enable(void)
 {
-	ctl_set_bit(0, 63 - 45);
+	ctl_set_bit(0, CTL0_AFP);
 }
 
 static inline void afp_disable(void)
 {
-	ctl_clear_bit(0, 63 - 45);
+	ctl_clear_bit(0, CTL0_AFP);
 }
 
 #endif
lib/s390x/asm/interrupt.h +2 -2
index 31e4766d..bf0eb40d 100644
@@ -27,13 +27,13 @@ void check_pgm_int_code(uint16_t code);
 /* Activate low-address protection */
 static inline void low_prot_enable(void)
 {
-	ctl_set_bit(0, 63 - 35);
+	ctl_set_bit(0, CTL0_LOW_ADDR_PROT);
 }
 
 /* Disable low-address protection */
 static inline void low_prot_disable(void)
 {
-	ctl_clear_bit(0, 63 - 35);
+	ctl_clear_bit(0, CTL0_LOW_ADDR_PROT);
 }
 
 #endif
lib/s390x/sclp.c +2 -2
index 291924b0..9502d161 100644
@@ -50,7 +50,7 @@ void sclp_setup_int(void)
 {
 	uint64_t mask;
 
-	ctl_set_bit(0, 9);
+	ctl_set_bit(0, CTL0_SERVICE_SIGNAL);
 
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
@@ -59,7 +59,7 @@ void sclp_setup_int(void)
 
 void sclp_handle_ext(void)
 {
-	ctl_clear_bit(0, 9);
+	ctl_clear_bit(0, CTL0_SERVICE_SIGNAL);
 	spin_lock(&sclp_lock);
 	sclp_busy = false;
 	spin_unlock(&sclp_lock);
s390x/diag288.c +1 -1
index e132ff04..82b6ec17 100644
@@ -86,7 +86,7 @@ static void test_bite(void)
 	asm volatile("stck %0" : "=Q" (time) : : "cc");
 	time += (uint64_t)(16000 * 1000) << 12;
 	asm volatile("sckc %0" : : "Q" (time));
-	ctl_set_bit(0, 11);
+	ctl_set_bit(0, CTL0_CLOCK_COMPARATOR);
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
 	load_psw_mask(mask);
s390x/gs.c +1 -1
index 1376d0e6..a017a97d 100644
@@ -145,7 +145,7 @@ static void test_special(void)
 static void init(void)
 {
 	/* Enable control bit for gs */
-	ctl_set_bit(2, 4);
+	ctl_set_bit(2, CTL2_GUARDED_STORAGE);
 
 	/* Setup gs registers to guard the gs_area */
 	gs_cb.gsd = gs_area | 25;
s390x/iep.c +2 -2
index fe167ef0..906c77b3 100644
@@ -22,7 +22,7 @@ static void test_iep(void)
 	void (*fn)(void);
 
 	/* Enable IEP */
-	ctl_set_bit(0, 20);
+	ctl_set_bit(0, CTL0_IEP);
 
 	/* Get and protect a page with the IEP bit */
 	iepbuf = alloc_page();
@@ -40,7 +40,7 @@ static void test_iep(void)
 	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
 	report_prefix_pop();
 	unprotect_page(iepbuf, PAGE_ENTRY_IEP);
-	ctl_clear_bit(0, 20);
+	ctl_clear_bit(0, CTL0_IEP);
 	free_page(iepbuf);
 }
 
s390x/skrf.c +1 -1
index 57524ba8..94e906a6 100644
@@ -150,7 +150,7 @@ static void ecall_setup(void)
 	/* Put a skey into the ext new psw */
 	lc->ext_new_psw.mask = 0x00F0000180000000UL;
 	/* Open up ext masks */
-	ctl_set_bit(0, 13);
+	ctl_set_bit(0, CTL0_EXTERNAL_CALL);
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
 	load_psw_mask(mask);
s390x/smp.c +4 -4
index b0ece491..f25ec769 100644
@@ -154,7 +154,7 @@ static void ecall(void)
 	struct lowcore *lc = (void *)0x0;
 
 	expect_ext_int();
-	ctl_set_bit(0, 13);
+	ctl_set_bit(0, CTL0_EXTERNAL_CALL);
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
 	load_psw_mask(mask);
@@ -188,7 +188,7 @@ static void emcall(void)
 	struct lowcore *lc = (void *)0x0;
 
 	expect_ext_int();
-	ctl_set_bit(0, 14);
+	ctl_set_bit(0, CTL0_EMERGENCY_SIGNAL);
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
 	load_psw_mask(mask);
@@ -283,8 +283,8 @@ static void test_local_ints(void)
 	unsigned long mask;
 
 	/* Open masks for ecall and emcall */
-	ctl_set_bit(0, 13);
-	ctl_set_bit(0, 14);
+	ctl_set_bit(0, CTL0_EXTERNAL_CALL);
+	ctl_set_bit(0, CTL0_EMERGENCY_SIGNAL);
 	mask = extract_psw_mask();
 	mask |= PSW_MASK_EXT;
 	load_psw_mask(mask);
s390x/vector.c +1 -1
index b052de55..fdb0eee2 100644
@@ -106,7 +106,7 @@ static void test_bcd_add(void)
 static void init(void)
 {
 	/* Enable vector instructions */
-	ctl_set_bit(0, 17);
+	ctl_set_bit(0, CTL0_VECTOR);
 
 	/* Preset vector registers to 0xff */
 	memset(pagebuf, 0xff, PAGE_SIZE);
Signature · 1 line
2.31.1
#6 lore raw

[kvm-unit-tests GIT PULL 05/12] s390x: run: Skip PV tests when tcg is the accelerator

In reply to Janosch Frank

TCG doesn't support PV.

---
 s390x/run | 5 +++++
 1 file changed, 5 insertions(+)
s390x/run +5 -0
index 09805044..c615caa1 100755
@@ -15,6 +15,11 @@ ACCEL=$(get_qemu_accelerator) ||
 qemu=$(search_qemu_binary) ||
 	exit $?
 
+if [ "${1: -7}" = ".pv.bin" ] || [ "${TESTNAME: -3}" = "_PV" ] && [ "$ACCEL" = "tcg" ]; then
+	echo "Protected Virtualization isn't supported under TCG"
+	exit 2
+fi
+
 M='-machine s390-ccw-virtio'
 M+=",accel=$ACCEL"
 command="$qemu -nodefaults -nographic $M"
Signature · 1 line
2.31.1
#7 lore raw

[kvm-unit-tests GIT PULL 09/12] s390x: lib: Add idte and other huge pages functions/macros

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

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

Message-Id: <[email protected]>

---
 lib/s390x/asm/pgtable.h | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
lib/s390x/asm/pgtable.h +31 -0
index 1a21f175..f166dcc6 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.31.1
#8 lore raw

[kvm-unit-tests GIT PULL 11/12] s390x: mmu: add support for large pages

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Add support for 1M and 2G pages.

Message-Id: <[email protected]>

---
 lib/s390x/mmu.c | 264 +++++++++++++++++++++++++++++++++++++++++++-----
 lib/s390x/mmu.h |  84 ++++++++++++++-
 2 files changed, 321 insertions(+), 27 deletions(-)
lib/s390x/mmu.c +239 -25
index 5c517366..c973443b 100644
@@ -15,6 +15,18 @@
 #include <vmalloc.h>
 #include "mmu.h"
 
+/*
+ * The naming convention used here is the same as used in the Linux kernel;
+ * this is the correspondence between the s390x architectural names and the
+ * Linux ones:
+ *
+ * pgd - region 1 table entry
+ * p4d - region 2 table entry
+ * pud - region 3 table entry
+ * pmd - segment table entry
+ * pte - page table entry
+ */
+
 static pgd_t *table_root;
 
 void configure_dat(int enable)
@@ -46,54 +58,256 @@ static void mmu_enable(pgd_t *pgtable)
 	lc->pgm_new_psw.mask |= PSW_MASK_DAT;
 }
 
-static pteval_t *get_pte(pgd_t *pgtable, uintptr_t vaddr)
+/*
+ * Get the pud (region 3) DAT table entry for the given address and root,
+ * allocating it if necessary
+ */
+static inline pud_t *get_pud(pgd_t *pgtable, uintptr_t vaddr)
 {
 	pgd_t *pgd = pgd_offset(pgtable, vaddr);
 	p4d_t *p4d = p4d_alloc(pgd, vaddr);
 	pud_t *pud = pud_alloc(p4d, vaddr);
-	pmd_t *pmd = pmd_alloc(pud, vaddr);
-	pte_t *pte = pte_alloc(pmd, vaddr);
 
-	return &pte_val(*pte);
+	return pud;
+}
+
+/*
+ * Get the pmd (segment) DAT table entry for the given address and pud,
+ * allocating it if necessary.
+ * The pud must not be huge.
+ */
+static inline pmd_t *get_pmd(pud_t *pud, uintptr_t vaddr)
+{
+	pmd_t *pmd;
+
+	assert(!pud_huge(*pud));
+	pmd = pmd_alloc(pud, vaddr);
+	return pmd;
+}
+
+/*
+ * 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;
+
+	assert(!pmd_large(*pmd));
+	pte = pte_alloc(pmd, vaddr);
+	return pte;
+}
+
+/*
+ * Splits a large pmd (segment) DAT table entry into equivalent 4kB small
+ * pages.
+ * @pmd The pmd to split, it must be large.
+ * @va the virtual address corresponding to this pmd.
+ */
+static void split_pmd(pmd_t *pmd, uintptr_t va)
+{
+	phys_addr_t pa = pmd_val(*pmd) & SEGMENT_ENTRY_SFAA;
+	unsigned long i, prot;
+	pte_t *pte;
+
+	assert(pmd_large(*pmd));
+	pte = alloc_pages(PAGE_TABLE_ORDER);
+	prot = pmd_val(*pmd) & (SEGMENT_ENTRY_IEP | SEGMENT_ENTRY_P);
+	for (i = 0; i < PAGE_TABLE_ENTRIES; i++)
+		pte_val(pte[i]) =  pa | PAGE_SIZE * i | prot;
+	idte_pmdp(va, &pmd_val(*pmd));
+	pmd_val(*pmd) = __pa(pte) | SEGMENT_ENTRY_TT_SEGMENT;
+
+}
+
+/*
+ * Splits a huge pud (region 3) DAT table entry into equivalent 1MB large
+ * pages.
+ * @pud The pud to split, it must be huge.
+ * @va the virtual address corresponding to this pud.
+ */
+static void split_pud(pud_t *pud, uintptr_t va)
+{
+	phys_addr_t pa = pud_val(*pud) & REGION3_ENTRY_RFAA;
+	unsigned long i, prot;
+	pmd_t *pmd;
+
+	assert(pud_huge(*pud));
+	pmd = alloc_pages(SEGMENT_TABLE_ORDER);
+	prot = pud_val(*pud) & (REGION3_ENTRY_IEP | REGION_ENTRY_P);
+	for (i = 0; i < SEGMENT_TABLE_ENTRIES; i++)
+		pmd_val(pmd[i]) =  pa | SZ_1M * i | prot | SEGMENT_ENTRY_FC | SEGMENT_ENTRY_TT_SEGMENT;
+	idte_pudp(va, &pud_val(*pud));
+	pud_val(*pud) = __pa(pmd) | REGION_ENTRY_TT_REGION3 | REGION_TABLE_LENGTH;
+}
+
+void *get_dat_entry(pgd_t *pgtable, void *vaddr, enum pgt_level level)
+{
+	uintptr_t va = (uintptr_t)vaddr;
+	pgd_t *pgd;
+	p4d_t *p4d;
+	pud_t *pud;
+	pmd_t *pmd;
+
+	assert(level && (level <= 5));
+	pgd = pgd_offset(pgtable, va);
+	if (level == pgtable_level_pgd)
+		return pgd;
+	p4d = p4d_alloc(pgd, va);
+	if (level == pgtable_level_p4d)
+		return p4d;
+	pud = pud_alloc(p4d, va);
+
+	if (level == pgtable_level_pud)
+		return pud;
+	if (!pud_none(*pud) && pud_huge(*pud))
+		split_pud(pud, va);
+	pmd = get_pmd(pud, va);
+	if (level == pgtable_level_pmd)
+		return pmd;
+	if (!pmd_none(*pmd) && pmd_large(*pmd))
+		split_pmd(pmd, va);
+	return get_pte(pmd, va);
+}
+
+void *split_page(pgd_t *pgtable, void *vaddr, enum pgt_level level)
+{
+	assert((level >= 3) && (level <= 5));
+	return get_dat_entry(pgtable ? pgtable : table_root, vaddr, level);
 }
 
 phys_addr_t virt_to_pte_phys(pgd_t *pgtable, void *vaddr)
 {
-	return (*get_pte(pgtable, (uintptr_t)vaddr) & PAGE_MASK) +
-	       ((unsigned long)vaddr & ~PAGE_MASK);
+	uintptr_t va = (uintptr_t)vaddr;
+	pud_t *pud;
+	pmd_t *pmd;
+	pte_t *pte;
+
+	pud = get_pud(pgtable, va);
+	if (pud_huge(*pud))
+		return (pud_val(*pud) & REGION3_ENTRY_RFAA) | (va & ~REGION3_ENTRY_RFAA);
+	pmd = get_pmd(pud, va);
+	if (pmd_large(*pmd))
+		return (pmd_val(*pmd) & SEGMENT_ENTRY_SFAA) | (va & ~SEGMENT_ENTRY_SFAA);
+	pte = get_pte(pmd, va);
+	return (pte_val(*pte) & PAGE_MASK) | (va & ~PAGE_MASK);
 }
 
-static pteval_t *set_pte(pgd_t *pgtable, pteval_t val, void *vaddr)
+/*
+ * Get the DAT table entry of the given level for the given address,
+ * splitting if necessary. If the entry was not invalid, invalidate it, and
+ * return the pointer to the entry and, if requested, its old value.
+ * @pgtable root of the page tables
+ * @vaddr virtual address
+ * @level 3 (for 2GB pud), 4 (for 1MB pmd) or 5 (for 4kB pages)
+ * @old if not NULL, will be written with the old value of the DAT table
+ * entry before invalidation
+ */
+static void *dat_get_and_invalidate(pgd_t *pgtable, void *vaddr, enum pgt_level level, unsigned long *old)
 {
-	pteval_t *p_pte = get_pte(pgtable, (uintptr_t)vaddr);
+	unsigned long va = (unsigned long)vaddr;
+	void *ptr;
 
-	/* first flush the old entry (if we're replacing anything) */
-	if (!(*p_pte & PAGE_ENTRY_I))
-		ipte((uintptr_t)vaddr, p_pte);
+	ptr = get_dat_entry(pgtable, vaddr, level);
+	if (old)
+		*old = *(unsigned long *)ptr;
+	if ((level == pgtable_level_pgd) && !pgd_none(*(pgd_t *)ptr))
+		idte_pgdp(va, ptr);
+	else if ((level == pgtable_level_p4d) && !p4d_none(*(p4d_t *)ptr))
+		idte_p4dp(va, ptr);
+	else if ((level == pgtable_level_pud) && !pud_none(*(pud_t *)ptr))
+		idte_pudp(va, ptr);
+	else if ((level == pgtable_level_pmd) && !pmd_none(*(pmd_t *)ptr))
+		idte_pmdp(va, ptr);
+	else if (!pte_none(*(pte_t *)ptr))
+		ipte(va, ptr);
+	return ptr;
+}
 
-	*p_pte = val;
-	return p_pte;
+static void cleanup_pmd(pmd_t *pmd)
+{
+	/* was invalid or large, nothing to do */
+	if (pmd_none(*pmd) || pmd_large(*pmd))
+		return;
+	/* was not large, free the corresponding page table */
+	free_pages((void *)(pmd_val(*pmd) & PAGE_MASK));
+}
+
+static void cleanup_pud(pud_t *pud)
+{
+	unsigned long i;
+	pmd_t *pmd;
+
+	/* was invalid or large, nothing to do */
+	if (pud_none(*pud) || pud_huge(*pud))
+		return;
+	/* recursively clean up all pmds if needed */
+	pmd = (pmd_t *)(pud_val(*pud) & PAGE_MASK);
+	for (i = 0; i < SEGMENT_TABLE_ENTRIES; i++)
+		cleanup_pmd(pmd + i);
+	/* free the corresponding segment table */
+	free_pages(pmd);
+}
+
+/*
+ * Set the DAT entry for the given level of the given virtual address. If a
+ * mapping already existed, it is overwritten. If an existing mapping with
+ * smaller pages existed, all the lower tables are freed.
+ * Returns the pointer to the DAT table entry.
+ * @pgtable root of the page tables
+ * @val the new value for the DAT table entry
+ * @vaddr the virtual address
+ * @level 3 for pud (region 3), 4 for pmd (segment) and 5 for pte (pages)
+ */
+static void *set_dat_entry(pgd_t *pgtable, unsigned long val, void *vaddr, enum pgt_level level)
+{
+	unsigned long old, *res;
+
+	res = dat_get_and_invalidate(pgtable, vaddr, level, &old);
+	if (level == pgtable_level_pmd)
+		cleanup_pmd((pmd_t *)&old);
+	if (level == pgtable_level_pud)
+		cleanup_pud((pud_t *)&old);
+	*res = val;
+	return res;
 }
 
 pteval_t *install_page(pgd_t *pgtable, phys_addr_t phys, void *vaddr)
 {
-	return set_pte(pgtable, __pa(phys), vaddr);
+	assert(IS_ALIGNED(phys, PAGE_SIZE));
+	assert(IS_ALIGNED((uintptr_t)vaddr, PAGE_SIZE));
+	return set_dat_entry(pgtable, phys, vaddr, pgtable_level_pte);
 }
 
-void protect_page(void *vaddr, unsigned long prot)
+pmdval_t *install_large_page(pgd_t *pgtable, phys_addr_t phys, void *vaddr)
 {
-	pteval_t *p_pte = get_pte(table_root, (uintptr_t)vaddr);
-	pteval_t n_pte = *p_pte | prot;
-
-	set_pte(table_root, n_pte, vaddr);
+	assert(IS_ALIGNED(phys, SZ_1M));
+	assert(IS_ALIGNED((uintptr_t)vaddr, SZ_1M));
+	return set_dat_entry(pgtable, phys | SEGMENT_ENTRY_FC, vaddr, pgtable_level_pmd);
 }
 
-void unprotect_page(void *vaddr, unsigned long prot)
+pudval_t *install_huge_page(pgd_t *pgtable, phys_addr_t phys, void *vaddr)
 {
-	pteval_t *p_pte = get_pte(table_root, (uintptr_t)vaddr);
-	pteval_t n_pte = *p_pte & ~prot;
+	assert(IS_ALIGNED(phys, SZ_2G));
+	assert(IS_ALIGNED((uintptr_t)vaddr, SZ_2G));
+	return set_dat_entry(pgtable, phys | REGION3_ENTRY_FC | REGION_ENTRY_TT_REGION3, vaddr, pgtable_level_pud);
+}
 
-	set_pte(table_root, n_pte, vaddr);
+void protect_dat_entry(void *vaddr, unsigned long prot, enum pgt_level level)
+{
+	unsigned long old, *ptr;
+
+	ptr = dat_get_and_invalidate(table_root, vaddr, level, &old);
+	*ptr = old | prot;
+}
+
+void unprotect_dat_entry(void *vaddr, unsigned long prot, enum pgt_level level)
+{
+	unsigned long old, *ptr;
+
+	ptr = dat_get_and_invalidate(table_root, vaddr, level, &old);
+	*ptr = old & ~prot;
 }
 
 void protect_range(void *start, unsigned long len, unsigned long prot)
@@ -102,7 +316,7 @@ void protect_range(void *start, unsigned long len, unsigned long prot)
 
 	len &= PAGE_MASK;
 	for (; len; len -= PAGE_SIZE, curr += PAGE_SIZE)
-		protect_page((void *)curr, prot);
+		protect_dat_entry((void *)curr, prot, 5);
 }
 
 void unprotect_range(void *start, unsigned long len, unsigned long prot)
@@ -111,7 +325,7 @@ void unprotect_range(void *start, unsigned long len, unsigned long prot)
 
 	len &= PAGE_MASK;
 	for (; len; len -= PAGE_SIZE, curr += PAGE_SIZE)
-		unprotect_page((void *)curr, prot);
+		unprotect_dat_entry((void *)curr, prot, 5);
 }
 
 static void setup_identity(pgd_t *pgtable, phys_addr_t start_addr,
lib/s390x/mmu.h +82 -2

91 lines not on this page · raw message

Signature · 1 line
2.31.1
#9 lore raw

[kvm-unit-tests GIT PULL 07/12] libcflat: add SZ_1M and SZ_2G

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Add SZ_1M and SZ_2G to libcflat.h

s390x needs those for large/huge pages

Message-Id: <[email protected]>

---
 lib/libcflat.h | 2 ++
 1 file changed, 2 insertions(+)
lib/libcflat.h +2 -0
index f40b431d..97db9e38 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.31.1
#10 lore raw

[kvm-unit-tests GIT PULL 08/12] s390x: lib: fix pgtable.h

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Fix pgtable.h:

* SEGMENT_ENTRY_SFAA had one extra bit set
* pmd entries don't have a length field
* ipte does not need to clear the lower bits
- clearing the 12 lower bits is technically incorrect, as page tables are architecturally aligned to 11 bit addresses (even though the unit tests allocate always one full page)
* region table entries should use REGION_ENTRY_TL instead of *_TABLE_LENGTH
- *_TABLE_LENGTH need to stay, because they should be used for ASCEs

Message-Id: <[email protected]>

---
 lib/s390x/asm/pgtable.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
lib/s390x/asm/pgtable.h +6 -7
index 277f3480..1a21f175 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
@@ -143,7 +143,7 @@ static inline p4d_t *p4d_alloc(pgd_t *pgd, unsigned long addr)
 	if (pgd_none(*pgd)) {
 		p4d_t *p4d = p4d_alloc_one();
 		pgd_val(*pgd) = __pa(p4d) | REGION_ENTRY_TT_REGION1 |
-				REGION_TABLE_LENGTH;
+				REGION_ENTRY_TL;
 	}
 	return p4d_offset(pgd, addr);
 }
@@ -163,7 +163,7 @@ static inline pud_t *pud_alloc(p4d_t *p4d, unsigned long addr)
 	if (p4d_none(*p4d)) {
 		pud_t *pud = pud_alloc_one();
 		p4d_val(*p4d) = __pa(pud) | REGION_ENTRY_TT_REGION2 |
-				REGION_TABLE_LENGTH;
+				REGION_ENTRY_TL;
 	}
 	return pud_offset(p4d, addr);
 }
@@ -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;
+				REGION_ENTRY_TL;
 	}
 	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.31.1
#11 lore raw

[kvm-unit-tests GIT PULL 10/12] s390x: lib: add teid union and clear teid from lowcore

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Add a union to represent Translation-Exception Identification (TEID).

Clear the TEID in expect_pgm_int clear_pgm_int.

Message-Id: <[email protected]>

---
 lib/s390x/asm/interrupt.h | 24 ++++++++++++++++++++++++
 lib/s390x/interrupt.c     |  2 ++
 2 files changed, 26 insertions(+)
lib/s390x/asm/interrupt.h +24 -0
index bf0eb40d..d9ab0bd7 100644
@@ -13,6 +13,30 @@
 #define EXT_IRQ_EXTERNAL_CALL	0x1202
 #define EXT_IRQ_SERVICE_SIG	0x2401
 
+#define TEID_ASCE_PRIMARY	0
+#define TEID_ASCE_AR		1
+#define TEID_ASCE_SECONDARY	2
+#define TEID_ASCE_HOME		3
+
+union teid {
+	unsigned long val;
+	struct {
+		unsigned long addr:52;
+		unsigned long fetch:1;
+		unsigned long store:1;
+		unsigned long reserved:6;
+		unsigned long acc_list_prot:1;
+		/*
+		 * depending on the exception and the installed facilities,
+		 * the m field can indicate several different things,
+		 * including whether the exception was triggered by a MVPG
+		 * instruction, or whether the addr field is meaningful
+		 */
+		unsigned long m:1;
+		unsigned long asce_id:2;
+	};
+};
+
 void register_pgm_cleanup_func(void (*f)(void));
 void handle_pgm_int(struct stack_frame_int *stack);
 void handle_ext_int(struct stack_frame_int *stack);
lib/s390x/interrupt.c +2 -0
index ce0003de..b627942f 100644
@@ -22,6 +22,7 @@ void expect_pgm_int(void)
 {
 	pgm_int_expected = true;
 	lc->pgm_int_code = 0;
+	lc->trans_exc_id = 0;
 	mb();
 }
 
@@ -39,6 +40,7 @@ uint16_t clear_pgm_int(void)
 	mb();
 	code = lc->pgm_int_code;
 	lc->pgm_int_code = 0;
+	lc->trans_exc_id = 0;
 	pgm_int_expected = false;
 	return code;
 }
Signature · 1 line
2.31.1
#12 lore raw

[kvm-unit-tests GIT PULL 12/12] s390x: edat test

In reply to Janosch Frank

From: Claudio Imbrenda <[email protected]>

Simple EDAT test.

Message-Id: <[email protected]>

---
 s390x/Makefile      |   1 +
 s390x/edat.c        | 274 ++++++++++++++++++++++++++++++++++++++++++++
 s390x/unittests.cfg |   3 +
 3 files changed, 278 insertions(+)
 create mode 100644 s390x/edat.c
s390x/Makefile +1 -0
index 8de926ab..8820e998 100644
@@ -22,6 +22,7 @@ tests += $(TEST_DIR)/uv-guest.elf
 tests += $(TEST_DIR)/sie.elf
 tests += $(TEST_DIR)/mvpg.elf
 tests += $(TEST_DIR)/uv-host.elf
+tests += $(TEST_DIR)/edat.elf
 
 tests_binary = $(patsubst %.elf,%.bin,$(tests))
 ifneq ($(HOST_KEY_DOCUMENT),)
s390x/edat.c new file +274 -0

274 lines not on this page · raw message

s390x/unittests.cfg +3 -0
index 9f81a608..a0ec8864 100644
@@ -103,3 +103,6 @@ file = sie.elf
 [mvpg]
 file = mvpg.elf
 timeout = 10
+
+[edat]
+file = edat.elf
Signature · 1 line
2.31.1
maintainer Applied
#13 lore raw
In reply to Janosch Frank
Quoted 64 lines · Janosch Frank

Dear Paolo,

please merge or pull the following changes:

Merge:
https://gitlab.com/kvm-unit-tests/kvm-unit-tests/-/merge_requests/11

Pipeline:
https://gitlab.com/frankja/kvm-unit-tests/-/pipelines/324608397

Pull:
The following changes since commit f09465ac9044145f20435344d41566aede62fc08:

   x86: Flush the TLB after setting user-bit (2021-06-17 14:36:25 -0400)

are available in the Git repository at:

   https://gitlab.com/frankja/kvm-unit-tests.git s390x-pull-2021-22-06

for you to fetch changes up to d58ec2ec341cbea746944a8ae92c737041c35172:

   s390x: edat test (2021-06-21 14:55:12 +0000)

Claudio Imbrenda (7):
s390x: lib: add and use macros for control register bits
libcflat: add SZ_1M and SZ_2G
s390x: lib: fix pgtable.h
s390x: lib: Add idte and other huge pages functions/macros
s390x: lib: add teid union and clear teid from lowcore
s390x: mmu: add support for large pages
s390x: edat test

Janosch Frank (5):
s390x: sie: Only overwrite r3 if it isn't needed anymore
s390x: selftest: Add prefixes to fix report output
s390x: Don't run PV testcases under tcg
configure: s390x: Check if the host key document exists
s390x: run: Skip PV tests when tcg is the accelerator

  configure                 |   5 +
  lib/libcflat.h            |   2 +
  lib/s390x/asm/arch_def.h  |  12 ++
  lib/s390x/asm/float.h     |   4 +-
  lib/s390x/asm/interrupt.h |  28 +++-
  lib/s390x/asm/pgtable.h   |  44 +++++-
  lib/s390x/interrupt.c     |   2 +
  lib/s390x/mmu.c           | 264 ++++++++++++++++++++++++++++++++----
  lib/s390x/mmu.h           |  84 +++++++++++-
  lib/s390x/sclp.c          |   4 +-
  s390x/Makefile            |   1 +
  s390x/cpu.S               |   2 +-
  s390x/diag288.c           |   2 +-
  s390x/edat.c              | 274 ++++++++++++++++++++++++++++++++++++++
  s390x/gs.c                |   2 +-
  s390x/iep.c               |   4 +-
  s390x/run                 |   5 +
  s390x/selftest.c          |  26 ++--
  s390x/skrf.c              |   2 +-
  s390x/smp.c               |   8 +-
  s390x/unittests.cfg       |   3 +
  s390x/vector.c            |   2 +-
  scripts/s390x/func.bash   |   3 +
  23 files changed, 724 insertions(+), 59 deletions(-)
Quoted 2 lines

create mode 100644 s390x/edat.c

Merged, thanks!

Paolo