Skip to contentSkip to conversation

s390x: Test effect of storage keys on some instructions

PATCH In review Janis Schoetterl-Glausch 3 participants 4 messages Apr 25, 2022 – Apr 26, 2022

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

PATCH v5 1/1 s390x: Test effect of storage keys on some instructions

Some instructions are emulated by KVM. Test that KVM correctly emulates storage key checking for two of those instructions (STORE CPU ADDRESS, SET PREFIX).
Test success and error conditions, including coverage of storage and fetch protection override.
Also add test for TEST PROTECTION, even if that instruction will not be emulated by KVM under normal conditions.

---
Range-diff against v4:
1: a9af3e08 ! 1: 89e59626 s390x: Test effect of storage keys on some instructions

    @@ s390x/skey.c: static void test_invalid_address(void)
     +	load_psw_mask(psw_mask);
     +}
     +
    ++#define PREFIX_AREA_SIZE (PAGE_SIZE * 2)
    ++static char lowcore_tmp[PREFIX_AREA_SIZE] __attribute__((aligned(PREFIX_AREA_SIZE)));
    ++
     +static void test_set_prefix(void)
     +{
    -+	char lowcore_tmp[PAGE_SIZE * 2] __attribute__((aligned(PAGE_SIZE * 2)));
     +	uint32_t *prefix_ptr = (uint32_t *)pagebuf;
    ++	uint32_t *no_override_prefix_ptr;
     +	uint32_t old_prefix;
     +	pgd_t *root;
     +
     +	report_prefix_push("SET PREFIX");
     +	root = (pgd_t *)(stctg(1) & PAGE_MASK);
     +	old_prefix = get_prefix();
    -+	memcpy(lowcore_tmp, 0, PAGE_SIZE * 2);
    ++	memcpy(lowcore_tmp, 0, sizeof(lowcore_tmp));
     +	assert(((uint64_t)&lowcore_tmp >> 31) == 0);
     +	*prefix_ptr = (uint32_t)(uint64_t)&lowcore_tmp;
     +
    @@ s390x/skey.c: static void test_invalid_address(void)
     +	expect_pgm_int();
     +	set_prefix_key_1(prefix_ptr);
     +	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
    -+	report(get_prefix() != *prefix_ptr, "did not set prefix");
    ++	report(get_prefix() == old_prefix, "did not set prefix");
     +	report_prefix_pop();
     +
     +	register_pgm_cleanup_func(dat_fixup_pgm_int);
    @@ s390x/skey.c: static void test_invalid_address(void)
     +	set_prefix_key_1((uint32_t *)0);
     +	install_page(root, 0, 0);
     +	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
    -+	report(get_prefix() != *prefix_ptr, "did not set prefix");
    ++	report(get_prefix() == old_prefix, "did not set prefix");
     +	report_prefix_pop();
     +
     +	ctl_set_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
    @@ s390x/skey.c: static void test_invalid_address(void)
     +	report(get_prefix() == *prefix_ptr, "set prefix");
     +	report_prefix_pop();
     +
    -+	{
    -+		uint32_t *prefix_ptr = (uint32_t *)(pagebuf + 2048);
    -+
    -+		WRITE_ONCE(*prefix_ptr, (uint32_t)(uint64_t)&lowcore_tmp);
    -+		report_prefix_push("fetch protection override does not apply");
    -+		set_prefix(old_prefix);
    -+		set_storage_key(pagebuf, 0x28, 0);
    -+		expect_pgm_int();
    -+		install_page(root, virt_to_pte_phys(root, pagebuf), 0);
    -+		set_prefix_key_1((uint32_t *)2048);
    -+		install_page(root, 0, 0);
    -+		check_pgm_int_code(PGM_INT_CODE_PROTECTION);
    -+		report(get_prefix() != *prefix_ptr, "did not set prefix");
    -+		report_prefix_pop();
    -+	}
    ++	no_override_prefix_ptr = (uint32_t *)(pagebuf + 2048);
    ++	WRITE_ONCE(*no_override_prefix_ptr, (uint32_t)(uint64_t)&lowcore_tmp);
    ++	report_prefix_push("fetch protection override does not apply");
    ++	set_prefix(old_prefix);
    ++	set_storage_key(pagebuf, 0x28, 0);
    ++	expect_pgm_int();
    ++	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
    ++	set_prefix_key_1((uint32_t *)2048);
    ++	install_page(root, 0, 0);
    ++	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
    ++	report(get_prefix() == old_prefix, "did not set prefix");
    ++	report_prefix_pop();
     +
     +	ctl_clear_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
     +	register_pgm_cleanup_func(NULL);
 lib/s390x/asm/arch_def.h |  20 ++--
 s390x/skey.c             | 227 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 238 insertions(+), 9 deletions(-)
lib/s390x/asm/arch_def.h +11 -9
index bab3c374..676a2753 100644
@@ -55,15 +55,17 @@ struct psw {
 #define PSW_MASK_BA			0x0000000080000000UL
 #define PSW_MASK_64			(PSW_MASK_BA | PSW_MASK_EA)
 
-#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 CTL0_LOW_ADDR_PROT			(63 - 35)
+#define CTL0_EDAT				(63 - 40)
+#define CTL0_FETCH_PROTECTION_OVERRIDE		(63 - 38)
+#define CTL0_STORAGE_PROTECTION_OVERRIDE	(63 - 39)
+#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 CR0_EXTM_MASK			0x0000000000006200UL /* Combined external masks */
 
 #define CTL2_GUARDED_STORAGE		(63 - 59)
s390x/skey.c +227 -0

245 lines not on this page · raw message

base-commit: 6a7a83ed106211fc0ee530a3a05f171f6a4c4e66

Signature · 1 line
2.33.1
#1 lore raw
Quoted 10 lines · Janis Schoetterl-Glausch

Some instructions are emulated by KVM. Test that KVM correctly emulates storage key checking for two of those instructions (STORE CPU ADDRESS, SET PREFIX).
Test success and error conditions, including coverage of storage and fetch protection override.
Also add test for TEST PROTECTION, even if that instruction will not be emulated by KVM under normal conditions.

---

[...]

Quoted 51 lines
  lib/s390x/asm/arch_def.h |  20 ++--
  s390x/skey.c             | 227 +++++++++++++++++++++++++++++++++++++++
  2 files changed, 238 insertions(+), 9 deletions(-)
lib/s390x/asm/arch_def.h +11 -9
index bab3c374..676a2753 100644
@@ -55,15 +55,17 @@ struct psw {
  #define PSW_MASK_BA			0x0000000080000000UL
  #define PSW_MASK_64			(PSW_MASK_BA | PSW_MASK_EA)
 
-#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 CTL0_LOW_ADDR_PROT			(63 - 35)
+#define CTL0_EDAT				(63 - 40)
+#define CTL0_FETCH_PROTECTION_OVERRIDE		(63 - 38)
+#define CTL0_STORAGE_PROTECTION_OVERRIDE	(63 - 39)
+#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 CR0_EXTM_MASK			0x0000000000006200UL /* Combined external masks */
 
  #define CTL2_GUARDED_STORAGE		(63 - 59)
s390x/skey.c +1 -0
index edad53e9..39429960 100644
@@ -10,6 +10,7 @@
  #include <libcflat.h>
  #include <asm/asm-offsets.h>
  #include <asm/interrupt.h>
+#include <vmalloc.h>
  #include <asm/page.h>
  #include <asm/facility.h>
  #include <asm/mem.h>
@@ -118,6 +119,227 @@ static void test_invalid_address(void)
  	report_prefix_pop();
  }
 

s390x/skey.c static void test_invalid_address(void) · line 131

Quoted 10 lines
s390x/skey.c +10 -0
+static void test_test_protection(void)
+{
+	unsigned long addr = (unsigned long)pagebuf;
+
+	report_prefix_push("TPROT");
+
+	set_storage_key(pagebuf, 0x10, 0);
+	report(tprot(addr, 0) == 0, "access key 0 -> no protection");
+	report(tprot(addr, 1) == 0, "access key matches -> no protection");
+	report(tprot(addr, 2) == 1, "access key mismatches, no fetch protection -> store protection");

I'd love to see these numerical constants replaced with named constants in the future so I don't have to look up the tprot CCs every time :)

Quoted 12 lines
s390x/skey.c +12 -0
+
+	set_storage_key(pagebuf, 0x18, 0);
+	report(tprot(addr, 2) == 2, "access key mismatches, fetch protection -> fetch & store protection");
+
+	ctl_set_bit(0, CTL0_STORAGE_PROTECTION_OVERRIDE);
+	set_storage_key(pagebuf, 0x90, 0);
+	report(tprot(addr, 2) == 0, "access key mismatches, storage protection override -> no protection");
+	ctl_clear_bit(0, CTL0_STORAGE_PROTECTION_OVERRIDE);
+
+	set_storage_key(pagebuf, 0x00, 0);
+	report_prefix_pop();
+}

[...]

Quoted 20 lines
s390x/skey.c +20 -0
+/*
+ * Perform SET PREFIX (SPX) instruction while temporarily executing
+ * with access key 1.
+ */
+static void set_prefix_key_1(uint32_t *prefix_ptr)
+{
+	asm volatile (
+		"spka	0x10\n\t"
+		"spx	%0\n\t"
+		"spka	0\n"
+	     :: "Q" (*prefix_ptr)
+	);
+}
+
+/*
+ * We remapped page 0, making the lowcore inaccessible, which breaks the normal
+ * handler and breaks skipping the faulting instruction.
+ * Just disable dynamic address translation to make things work.
+ */
+static void dat_fixup_pgm_int(void)

s390x/skey.c static void test_invalid_address(void) · line 252

Quoted 10 lines
s390x/skey.c +10 -0
+{
+	uint64_t psw_mask = extract_psw_mask();
+
+	psw_mask &= ~PSW_MASK_DAT;
+	load_psw_mask(psw_mask);
+}
+
+#define PREFIX_AREA_SIZE (PAGE_SIZE * 2)
+static char lowcore_tmp[PREFIX_AREA_SIZE] __attribute__((aligned(PREFIX_AREA_SIZE)));
+

Please add a comment that we're testing the fetch access of the operand since the prefix is only checked for addressing.

Quoted 107 lines
s390x/skey.c +95 -0
+static void test_set_prefix(void)
+{
+	uint32_t *prefix_ptr = (uint32_t *)pagebuf;
+	uint32_t *no_override_prefix_ptr;
+	uint32_t old_prefix;
+	pgd_t *root;
+
+	report_prefix_push("SET PREFIX");
+	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	old_prefix = get_prefix();
+	memcpy(lowcore_tmp, 0, sizeof(lowcore_tmp));
+	assert(((uint64_t)&lowcore_tmp >> 31) == 0);
+	*prefix_ptr = (uint32_t)(uint64_t)&lowcore_tmp;
+
+	report_prefix_push("zero key");
+	set_prefix(old_prefix);
+	set_storage_key(prefix_ptr, 0x20, 0);
+	set_prefix(*prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("matching key");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x10, 0);
+	set_prefix_key_1(prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("mismatching key");
+
+	report_prefix_push("no fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x20, 0);
+	set_prefix_key_1(prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	set_prefix_key_1(prefix_ptr);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	register_pgm_cleanup_func(dat_fixup_pgm_int);
+
+	report_prefix_push("remapped page, fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)0);
+	install_page(root, 0, 0);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	ctl_set_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
+
+	report_prefix_push("fetch protection override applies");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)0);
+	install_page(root, 0, 0);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	no_override_prefix_ptr = (uint32_t *)(pagebuf + 2048);
+	WRITE_ONCE(*no_override_prefix_ptr, (uint32_t)(uint64_t)&lowcore_tmp);
+	report_prefix_push("fetch protection override does not apply");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)2048);
+	install_page(root, 0, 0);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	ctl_clear_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
+	register_pgm_cleanup_func(NULL);
+	report_prefix_pop();
+	set_storage_key(pagebuf, 0x00, 0);
+	report_prefix_pop();
+}
+
  int main(void)
  {
  	report_prefix_push("skey");
@@ -130,6 +352,11 @@ int main(void)
  	test_set();
  	test_set_mb();
  	test_chg();
+	test_test_protection();
+	test_store_cpu_address();
+
+	setup_vm();
+	test_set_prefix();
  done:
  	report_prefix_pop();
  	return report_summary();

base-commit: 6a7a83ed106211fc0ee530a3a05f171f6a4c4e66

Reviewed-by
#2 lore raw
Quoted 10 lines · Janosch Frank
Quoted 9 lines · Janis Schoetterl-Glausch

Some instructions are emulated by KVM. Test that KVM correctly emulates storage key checking for two of those instructions (STORE CPU ADDRESS, SET PREFIX).
Test success and error conditions, including coverage of storage and fetch protection override.
Also add test for TEST PROTECTION, even if that instruction will not be emulated by KVM under normal conditions.

[...]

We need to:
a) Fence this in our CI environments
b) Fence this for the gitlab ci s390x kvm entry

Could you please add a patch that removes the skey test from .gitlab-ci.yml? It's at the very end of that file.

If the nit below is fixed:

Quoted 111 lines

Please add a comment that we're testing the fetch access of the operand since the prefix is only checked for addressing.

Quoted 107 lines
s390x/skey.c +95 -0
+static void test_set_prefix(void)
+{
+	uint32_t *prefix_ptr = (uint32_t *)pagebuf;
+	uint32_t *no_override_prefix_ptr;
+	uint32_t old_prefix;
+	pgd_t *root;
+
+	report_prefix_push("SET PREFIX");
+	root = (pgd_t *)(stctg(1) & PAGE_MASK);
+	old_prefix = get_prefix();
+	memcpy(lowcore_tmp, 0, sizeof(lowcore_tmp));
+	assert(((uint64_t)&lowcore_tmp >> 31) == 0);
+	*prefix_ptr = (uint32_t)(uint64_t)&lowcore_tmp;
+
+	report_prefix_push("zero key");
+	set_prefix(old_prefix);
+	set_storage_key(prefix_ptr, 0x20, 0);
+	set_prefix(*prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("matching key");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x10, 0);
+	set_prefix_key_1(prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("mismatching key");
+
+	report_prefix_push("no fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x20, 0);
+	set_prefix_key_1(prefix_ptr);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	report_prefix_push("fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	set_prefix_key_1(prefix_ptr);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	register_pgm_cleanup_func(dat_fixup_pgm_int);
+
+	report_prefix_push("remapped page, fetch protection");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)0);
+	install_page(root, 0, 0);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	ctl_set_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
+
+	report_prefix_push("fetch protection override applies");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)0);
+	install_page(root, 0, 0);
+	report(get_prefix() == *prefix_ptr, "set prefix");
+	report_prefix_pop();
+
+	no_override_prefix_ptr = (uint32_t *)(pagebuf + 2048);
+	WRITE_ONCE(*no_override_prefix_ptr, (uint32_t)(uint64_t)&lowcore_tmp);
+	report_prefix_push("fetch protection override does not apply");
+	set_prefix(old_prefix);
+	set_storage_key(pagebuf, 0x28, 0);
+	expect_pgm_int();
+	install_page(root, virt_to_pte_phys(root, pagebuf), 0);
+	set_prefix_key_1((uint32_t *)2048);
+	install_page(root, 0, 0);
+	check_pgm_int_code(PGM_INT_CODE_PROTECTION);
+	report(get_prefix() == old_prefix, "did not set prefix");
+	report_prefix_pop();
+
+	ctl_clear_bit(0, CTL0_FETCH_PROTECTION_OVERRIDE);
+	register_pgm_cleanup_func(NULL);
+	report_prefix_pop();
+	set_storage_key(pagebuf, 0x00, 0);
+	report_prefix_pop();
+}
+
   int main(void)
   {
   	report_prefix_push("skey");
@@ -130,6 +352,11 @@ int main(void)
   	test_set();
   	test_set_mb();
   	test_chg();
+	test_test_protection();
+	test_store_cpu_address();
+
+	setup_vm();
+	test_set_prefix();
   done:
   	report_prefix_pop();
   	return report_summary();

base-commit: 6a7a83ed106211fc0ee530a3a05f171f6a4c4e66

Reviewed-by on v5 1/1
#3 lore raw
In reply to Janis Schoetterl-Glausch
Quoted 10 lines · Janis Schoetterl-Glausch

Some instructions are emulated by KVM. Test that KVM correctly emulates storage key checking for two of those instructions (STORE CPU ADDRESS, SET PREFIX).
Test success and error conditions, including coverage of storage and fetch protection override.
Also add test for TEST PROTECTION, even if that instruction will not be emulated by KVM under normal conditions.