Skip to contentSkip to conversation

s390x: pv: Add test for large host pages backing

PATCH In review Claudio Imbrenda 3 participants 8 messages Dec 18, 2024 – Jan 9, 2025

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

v3 cover s390x: pv: Add test for large host pages backing

Add a new test to check that the host can use 1M large pages to back protected guests when the corresponding feature is present.

v2->v3
* remove pointless timing measurements
* add two missing patches (oops!)
* minor fixes (thanks Christoph)

Claudio Imbrenda (3):
lib: s390: add ptlb wrapper
lib: s390x: add function to test available UV features
s390x: pv: Add test for large host pages backing

 s390x/Makefile               |   2 +
 lib/s390x/asm/arch_def.h     |   1 +
 lib/s390x/asm/pgtable.h      |   5 +
 lib/s390x/asm/uv.h           |  18 ++
 lib/s390x/uv.h               |   1 +
 lib/s390x/uv.c               |   9 +
 s390x/pv-edat1.c             | 387 +++++++++++++++++++++++++++++++++++
 s390x/snippets/c/pv-memhog.c |  59 ++++++
 8 files changed, 482 insertions(+)
 create mode 100644 s390x/pv-edat1.c
 create mode 100644 s390x/snippets/c/pv-memhog.c
Signature · 1 line
2.47.1
#1 lore raw

PATCH v3 1/3 lib: s390: add ptlb wrapper

Add wrapper function for the Purge TLB instruction.

---
 lib/s390x/asm/pgtable.h | 5 +++++
 1 file changed, 5 insertions(+)
lib/s390x/asm/pgtable.h +5 -0
index 7b556ad9..fcb30a1e 100644
@@ -247,4 +247,9 @@ static inline void idte_pgdp(unsigned long vaddr, pgdval_t *pgdp)
 	idte((unsigned long)(pgdp - pgd_index(vaddr)) | ASCE_DT_REGION1, vaddr);
 }
 
+static inline void ptlb(void)
+{
+	asm volatile("ptlb" : : : "memory");
+}
+
 #endif /* _ASMS390X_PGTABLE_H_ */
Signature · 1 line
2.47.1
#2 lore raw

PATCH v3 2/3 lib: s390x: add function to test available UV features

In reply to Claudio Imbrenda

Add uv_query_test_feature(), to test specific bits in the uv_feature_indications bitmask, similar to uv_query_test_call().

---
 lib/s390x/uv.h | 1 +
 lib/s390x/uv.c | 9 +++++++++
 2 files changed, 10 insertions(+)
lib/s390x/uv.h +1 -0
index 00a37041..b0fbc478 100644
@@ -10,6 +10,7 @@ bool uv_os_is_guest(void);
 bool uv_os_is_host(void);
 bool uv_host_requirement_checks(void);
 bool uv_query_test_call(unsigned int nr);
+bool uv_query_test_feature(unsigned int nr);
 const struct uv_cb_qui *uv_get_query_data(void);
 void uv_init(void);
 int uv_setup(void);
lib/s390x/uv.c +9 -0
index 723bb4f2..03499272 100644
@@ -67,6 +67,15 @@ bool uv_query_test_call(unsigned int nr)
 	return test_bit_inv(nr, uvcb_qui.inst_calls_list);
 }
 
+bool uv_query_test_feature(unsigned int nr)
+{
+	/* Query needs to be called first */
+	assert(uvcb_qui.header.rc);
+	assert(nr < BITS_PER_LONG);
+
+	return test_bit_inv(nr, &uvcb_qui.uv_feature_indications);
+}
+
 const struct uv_cb_qui *uv_get_query_data(void)
 {
 	/* Query needs to be called first */
Signature · 1 line
2.47.1
#3 lore raw

PATCH v3 3/3 s390x: pv: Add test for large host pages backing

In reply to Claudio Imbrenda

Add a new test to check that the host can use 1M large pages to back protected guests when the corresponding feature is present.

---
 s390x/Makefile               |   2 +
 lib/s390x/asm/arch_def.h     |   1 +
 lib/s390x/asm/uv.h           |  18 ++
 s390x/pv-edat1.c             | 387 +++++++++++++++++++++++++++++++++++
 s390x/snippets/c/pv-memhog.c |  59 ++++++
 5 files changed, 467 insertions(+)
 create mode 100644 s390x/pv-edat1.c
 create mode 100644 s390x/snippets/c/pv-memhog.c
s390x/Makefile +2 -0
index 23342bd6..c5c6f92c 100644
@@ -48,6 +48,7 @@ tests += $(TEST_DIR)/sie-dat.elf
 pv-tests += $(TEST_DIR)/pv-diags.elf
 pv-tests += $(TEST_DIR)/pv-icptcode.elf
 pv-tests += $(TEST_DIR)/pv-ipl.elf
+pv-tests += $(TEST_DIR)/pv-edat1.elf
 
 ifneq ($(HOST_KEY_DOCUMENT),)
 ifneq ($(GEN_SE_HEADER),)
@@ -137,6 +138,7 @@ $(TEST_DIR)/pv-icptcode.elf: pv-snippets += $(SNIPPET_DIR)/asm/icpt-loop.gbin
 $(TEST_DIR)/pv-icptcode.elf: pv-snippets += $(SNIPPET_DIR)/asm/loop.gbin
 $(TEST_DIR)/pv-icptcode.elf: pv-snippets += $(SNIPPET_DIR)/asm/pv-icpt-vir-timing.gbin
 $(TEST_DIR)/pv-ipl.elf: pv-snippets += $(SNIPPET_DIR)/asm/pv-diag-308.gbin
+$(TEST_DIR)/pv-edat1.elf: pv-snippets += $(SNIPPET_DIR)/c/pv-memhog.gbin
 
 ifneq ($(GEN_SE_HEADER),)
 snippets += $(pv-snippets)
lib/s390x/asm/arch_def.h +1 -0
index 745a3387..481ede8f 100644
@@ -249,6 +249,7 @@ extern struct lowcore lowcore;
 #define PGM_INT_CODE_REGION_FIRST_TRANS		0x39
 #define PGM_INT_CODE_REGION_SECOND_TRANS	0x3a
 #define PGM_INT_CODE_REGION_THIRD_TRANS		0x3b
+#define PGM_INT_CODE_SECURE_PAGE_SIZE		0x3c
 #define PGM_INT_CODE_SECURE_STOR_ACCESS		0x3d
 #define PGM_INT_CODE_NON_SECURE_STOR_ACCESS	0x3e
 #define PGM_INT_CODE_SECURE_STOR_VIOLATION	0x3f
lib/s390x/asm/uv.h +18 -0
index 611dcd3f..7527be48 100644
@@ -35,6 +35,7 @@
 #define UVC_CMD_CONV_TO_SEC_STOR	0x0200
 #define UVC_CMD_CONV_FROM_SEC_STOR	0x0201
 #define UVC_CMD_DESTR_SEC_STOR		0x0202
+#define UVC_CMD_VERIFY_LARGE_FRAME	0x0203
 #define UVC_CMD_SET_SEC_CONF_PARAMS	0x0300
 #define UVC_CMD_UNPACK_IMG		0x0301
 #define UVC_CMD_VERIFY_IMG		0x0302
@@ -74,6 +75,11 @@ enum uv_cmds_inst {
 	BIT_UVC_CMD_PIN_PAGE_SHARED = 21,
 	BIT_UVC_CMD_UNPIN_PAGE_SHARED = 22,
 	BIT_UVC_CMD_ATTESTATION = 28,
+	BIT_UVC_CMD_VERIFY_LARGE_FRAME = 32,
+};
+
+enum uv_features {
+	BIT_UV_1M_BACKING = 6,
 };
 
 struct uv_cb_header {
@@ -312,6 +318,18 @@ static inline int uv_import(uint64_t handle, unsigned long gaddr)
 	return uv_call(0, (uint64_t)&uvcb);
 }
 
+static inline int uv_merge(uint64_t handle, unsigned long gaddr)
+{
+	struct uv_cb_cts uvcb = {
+		.header.cmd = UVC_CMD_VERIFY_LARGE_FRAME,
+		.header.len = sizeof(uvcb),
+		.guest_handle = handle,
+		.gaddr = gaddr,
+	};
+
+	return uv_call(0, (uint64_t)&uvcb);
+}
+
 static inline int uv_export(unsigned long paddr)
 {
 	struct uv_cb_cfs uvcb = {
s390x/pv-edat1.c new file +387 -0

387 lines not on this page · raw message

s390x/snippets/c/pv-memhog.c new file +59 -0

59 lines not on this page · raw message

Signature · 1 line
2.47.1
#4 lore raw

On Wed Dec 18, 2024 at 2:51 PM CET, Claudio Imbrenda wrote:
[...]

Quoted 4 lines
lib/s390x/asm/uv.h +0 -0
index 611dcd3f..7527be48 100644

[...]

lib/s390x/asm/uv.h static inline int uv_import(uint64_t handle, unsigned long gaddr) · line 331

Quoted 11 lines
lib/s390x/asm/uv.h +11 -0
+static inline int uv_merge(uint64_t handle, unsigned long gaddr)
+{
+	struct uv_cb_cts uvcb = {
+		.header.cmd = UVC_CMD_VERIFY_LARGE_FRAME,
+		.header.len = sizeof(uvcb),
+		.guest_handle = handle,
+		.gaddr = gaddr,
+	};
+
+	return uv_call(0, (uint64_t)&uvcb);
+}

This function seems unused and uvc_merge() below looks very similar.

[...]

Quoted 5 lines
s390x/pv-edat1.c new file +0 -0
new file mode 100644
index 00000000..3f96c716

[...]

s390x/pv-edat1.c line 27

Quoted 2 lines
s390x/pv-edat1.c new file +2 -0
+#define FIRST		42
+#define SECOND		23

It was not obvious to me what these mean. It would be easier for me to understand if they had some name like GUEST_READ_DONE_GET_PARAM and SHOULD_EXIT_LOOP (or so) and share the define with the guest or at least have defines with the same name in the guest (see also below).

[...]

s390x/pv-edat1.c line 141

Quoted 5 lines
s390x/pv-edat1.c new file +5 -0
+static inline void assert_diag500_val(struct vm *vm, uint64_t val)
+{
+	assert(pv_icptdata_check_diag(vm, 0x500));
+	assert(vm->save_area.guest.grs[2] == val);
+}

I would appreciate it if you could base on Ninas STFLE series and use snippet_check_force_exit_value() here. See
https://lore.kernel.org/kvm/[email protected]/
See also below.

[...]

s390x/pv-edat1.c line 184

Quoted 7 lines
s390x/pv-edat1.c new file +7 -0
+static void test_run(void)
+{
+	int init1m, import1m, merge, run1m;
+
+	report_prefix_push("test run");
+
+	for (init1m = 0; init1m < 1; init1m++) {

Are you sure this does what you want it to do?

[...]

Quoted 4 lines
s390x/pv-edat1.c new file +4 -0
+static void test_merge(void)
+{
+	uint64_t tmp, mem;
+	int cc;

s390x/pv-edat1.c line 216

Quoted 10 lines
s390x/pv-edat1.c new file +10 -0
+
+	report_prefix_push("merge");
+	init_snippet(&vm);
+
+	mem = guest_start(&vm);
+
+	map_identity_all(&vm, false);
+	install_page(root, mem + 0x101000, (void *)(mem + 0x102000));
+	install_page(root, mem + 0x102000, (void *)(mem + 0x101000));
+	install_page(root, mem + 0x205000, (void *)(mem + 0x305000));

(see below)

[...]

s390x/pv-edat1.c line 242

Quoted 3 lines
s390x/pv-edat1.c new file +3 -0
+	/* Not all pages are aligned correctly */
+	report(uvc_merge(&vm, mem + 0x100000) == 0x104, "Pages not consecutive");
+	report(uvc_merge(&vm, mem + 0x200000) == 0x104, "Pages not in the same 1M frame");

It would be easier for me to understand if the regions were named, e.g. with a variable for each region, for example:

uint64_t non_consecutive = mem + 0x100000

and then above

install_page(root, mem + 0x101000, (void *)(non_consecutive + 0x2000)); install_page(root, mem + 0x102000, (void *)(non_consecutive + 0x1000));

[...]

Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +0 -0
new file mode 100644
index 00000000..43f0c2b1
@@ -0,0 +1,59 @@

[...]

s390x/snippets/c/pv-memhog.c line 42

Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +6 -0
+int main(void)
+{
+	uint64_t param, addr, i, n;
+
+	READ_ONCE(*MIDPAGE_PTR(SZ_1M + 42 * PAGE_SIZE));
+	param = get_value(42);

(see below)

s390x/snippets/c/pv-memhog.c line 55

Quoted 12 lines
s390x/snippets/c/pv-memhog.c new file +12 -0
+	n = (param >> 32) & 0x1fffffff;
+	n = n ? n : N_PAGES;
+	param &= 0x7fffffff;
+
+	while (true) {
+		for (i = 0; i < n; i++) {
+			addr = ((param ? i * param : i * i * i) * PAGE_SIZE) & MASK_2G;
+			WRITE_ONCE(*MIDPAGE_PTR(addr), addr);
+		}
+
+		i = get_value(23);
+		if (i != 42)

I would like some defines for 23 and 42 and possibly share them with the host.

#5 lore raw
Quoted 7 lines · On Fri, 20 Dec 2024 16:22:31 +0100 "Nico Boehr" <[email protected]> wrote:

On Wed Dec 18, 2024 at 2:51 PM CET, Claudio Imbrenda wrote:
[...]

Quoted 4 lines
lib/s390x/asm/uv.h +0 -0
index 611dcd3f..7527be48 100644

[...]

Quoted 13 lines

lib/s390x/asm/uv.h static inline int uv_import(uint64_t handle, unsigned long gaddr) · line 331

Quoted 11 lines
lib/s390x/asm/uv.h +11 -0
+static inline int uv_merge(uint64_t handle, unsigned long gaddr)
+{
+	struct uv_cb_cts uvcb = {
+		.header.cmd = UVC_CMD_VERIFY_LARGE_FRAME,
+		.header.len = sizeof(uvcb),
+		.guest_handle = handle,
+		.gaddr = gaddr,
+	};
+
+	return uv_call(0, (uint64_t)&uvcb);
+}  

This function seems unused and uvc_merge() below looks very similar.

it's a leftover, will remove

Quoted 9 lines

[...]

Quoted 5 lines
s390x/pv-edat1.c new file +0 -0
new file mode 100644
index 00000000..3f96c716

[...]

Quoted 7 lines

s390x/pv-edat1.c line 27

Quoted 2 lines
s390x/pv-edat1.c new file +2 -0
+#define FIRST		42
+#define SECOND		23  

It was not obvious to me what these mean. It would be easier for me to understand if they had some name like GUEST_READ_DONE_GET_PARAM and SHOULD_EXIT_LOOP (or so) and share the define with the guest or at least have defines with the same name in the guest (see also below).

will do

Quoted 14 lines

[...]

s390x/pv-edat1.c line 141

Quoted 5 lines
s390x/pv-edat1.c new file +5 -0
+static inline void assert_diag500_val(struct vm *vm, uint64_t val)
+{
+	assert(pv_icptdata_check_diag(vm, 0x500));
+	assert(vm->save_area.guest.grs[2] == val);
+}  

I would appreciate it if you could base on Ninas STFLE series and use snippet_check_force_exit_value() here. See
https://lore.kernel.org/kvm/[email protected]/
See also below.

[...]

Quoted 9 lines

s390x/pv-edat1.c line 184

Quoted 7 lines
s390x/pv-edat1.c new file +7 -0
+static void test_run(void)
+{
+	int init1m, import1m, merge, run1m;
+
+	report_prefix_push("test run");
+
+	for (init1m = 0; init1m < 1; init1m++) {  

Are you sure this does what you want it to do?

I'm quite sure it does __not__ do what I want it to :D I'll fix it

Quoted 27 lines

[...]

Quoted 4 lines
s390x/pv-edat1.c new file +4 -0
+static void test_merge(void)
+{
+	uint64_t tmp, mem;
+	int cc;

s390x/pv-edat1.c line 216

Quoted 10 lines
s390x/pv-edat1.c new file +10 -0
+
+	report_prefix_push("merge");
+	init_snippet(&vm);
+
+	mem = guest_start(&vm);
+
+	map_identity_all(&vm, false);
+	install_page(root, mem + 0x101000, (void *)(mem + 0x102000));
+	install_page(root, mem + 0x102000, (void *)(mem + 0x101000));
+	install_page(root, mem + 0x205000, (void *)(mem + 0x305000));  

(see below)

[...]

s390x/pv-edat1.c line 242

Quoted 3 lines
s390x/pv-edat1.c new file +3 -0
+	/* Not all pages are aligned correctly */
+	report(uvc_merge(&vm, mem + 0x100000) == 0x104, "Pages not consecutive");
+	report(uvc_merge(&vm, mem + 0x200000) == 0x104, "Pages not in the same 1M frame");  

It would be easier for me to understand if the regions were named, e.g. with a variable for each region, for example:

Quoted 6 lines

uint64_t non_consecutive = mem + 0x100000

and then above

install_page(root, mem + 0x101000, (void *)(non_consecutive + 0x2000)); install_page(root, mem + 0x102000, (void *)(non_consecutive + 0x1000));

hmmm ok I'll do that

Quoted 18 lines

[...]

Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +0 -0
new file mode 100644
index 00000000..43f0c2b1
@@ -0,0 +1,59 @@  

[...]

s390x/snippets/c/pv-memhog.c line 42

Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +6 -0
+int main(void)
+{
+	uint64_t param, addr, i, n;
+
+	READ_ONCE(*MIDPAGE_PTR(SZ_1M + 42 * PAGE_SIZE));
+	param = get_value(42);  

(see below)

Quoted 14 lines

s390x/snippets/c/pv-memhog.c line 55

Quoted 12 lines
s390x/snippets/c/pv-memhog.c new file +12 -0
+	n = (param >> 32) & 0x1fffffff;
+	n = n ? n : N_PAGES;
+	param &= 0x7fffffff;
+
+	while (true) {
+		for (i = 0; i < n; i++) {
+			addr = ((param ? i * param : i * i * i) * PAGE_SIZE) & MASK_2G;
+			WRITE_ONCE(*MIDPAGE_PTR(addr), addr);
+		}
+
+		i = get_value(23);
+		if (i != 42)  

I would like some defines for 23 and 42 and possibly share them with the host.

not sure what's the easiest way to share with the host, but I can just copy the defines

#6 lore raw

On Fri Dec 20, 2024 at 4:52 PM CET, Claudio Imbrenda wrote:
[...]

Quoted 31 lines
Quoted 16 lines
Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +0 -0
new file mode 100644
index 00000000..43f0c2b1
@@ -0,0 +1,59 @@  

[...]

s390x/snippets/c/pv-memhog.c line 42

Quoted 6 lines
s390x/snippets/c/pv-memhog.c new file +6 -0
+int main(void)
+{
+	uint64_t param, addr, i, n;
+
+	READ_ONCE(*MIDPAGE_PTR(SZ_1M + 42 * PAGE_SIZE));
+	param = get_value(42);  

(see below)

Quoted 14 lines

s390x/snippets/c/pv-memhog.c line 55

Quoted 12 lines
s390x/snippets/c/pv-memhog.c new file +12 -0
+	n = (param >> 32) & 0x1fffffff;
+	n = n ? n : N_PAGES;
+	param &= 0x7fffffff;
+
+	while (true) {
+		for (i = 0; i < n; i++) {
+			addr = ((param ? i * param : i * i * i) * PAGE_SIZE) & MASK_2G;
+			WRITE_ONCE(*MIDPAGE_PTR(addr), addr);
+		}
+
+		i = get_value(23);
+		if (i != 42)  

I would like some defines for 23 and 42 and possibly share them with the host.

Quoted 2 lines

not sure what's the easiest way to share with the host, but I can just copy the defines

The sie-dat.c test does this, there's a header file

s390x/snippets/c/sie-dat.h

which is included in the host:

#include "snippets/c/sie-dat.h"

Not the most beautiful way, but works.