public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Nico Boehr <nrb@linux.ibm.com>
To: kvm@vger.kernel.org
Cc: frankja@linux.ibm.com, imbrenda@linux.ibm.com, thuth@redhat.com,
	pbonzini@redhat.com
Subject: [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions
Date: Thu,  1 Dec 2022 09:46:40 +0100	[thread overview]
Message-ID: <20221201084642.3747014-2-nrb@linux.ibm.com> (raw)
In-Reply-To: <20221201084642.3747014-1-nrb@linux.ibm.com>

Upcoming changes will add a test which is very similar to the existing
skey migration test. To reduce code duplication, move the common
functions to a library which can be re-used by both tests.

Signed-off-by: Nico Boehr <nrb@linux.ibm.com>
---
 lib/s390x/skey.c       | 92 ++++++++++++++++++++++++++++++++++++++++++
 lib/s390x/skey.h       | 32 +++++++++++++++
 s390x/Makefile         |  1 +
 s390x/migration-skey.c | 44 +++-----------------
 4 files changed, 131 insertions(+), 38 deletions(-)
 create mode 100644 lib/s390x/skey.c
 create mode 100644 lib/s390x/skey.h

diff --git a/lib/s390x/skey.c b/lib/s390x/skey.c
new file mode 100644
index 000000000000..100f0949a244
--- /dev/null
+++ b/lib/s390x/skey.c
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <nrb@linux.ibm.com>
+ */
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/mem.h>
+#include <skey.h>
+
+/*
+ * Set storage keys on pagebuf.
+ * pagebuf must point to page_count consecutive pages.
+ */
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	unsigned char key_to_set;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		/*
+		 * Storage keys are 7 bit, lowest bit is always returned as zero
+		 * by iske.
+		 * This loop will set all 7 bits which means we set fetch
+		 * protection as well as reference and change indication for
+		 * some keys.
+		 */
+		key_to_set = i * 2;
+		set_storage_key(pagebuf + i * PAGE_SIZE, key_to_set, 1);
+	}
+}
+
+/*
+ * Verify storage keys on pagebuf.
+ * Storage keys must have been set by skey_set_keys on pagebuf before.
+ *
+ * If storage keys match the expected result, will return a skey_verify_result
+ * with verify_failed false. All other fields are then invalid.
+ * If there is a mismatch, returned struct will have verify_failed true and will
+ * be filled with the details on the first mismatch encountered.
+ */
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count)
+{
+	union skey expected_key, actual_key;
+	struct skey_verify_result result = {
+		.verify_failed = true
+	};
+	uint8_t *cur_page;
+	unsigned long i;
+
+	for (i = 0; i < page_count; i++) {
+		cur_page = pagebuf + i * PAGE_SIZE;
+		actual_key.val = get_storage_key(cur_page);
+		expected_key.val = i * 2;
+
+		/*
+		 * The PoP neither gives a guarantee that the reference bit is
+		 * accurate nor that it won't be cleared by hardware. Hence we
+		 * don't rely on it and just clear the bits to avoid compare
+		 * errors.
+		 */
+		actual_key.str.rf = 0;
+		expected_key.str.rf = 0;
+
+		if (actual_key.val != expected_key.val) {
+			result.expected_key.val = expected_key.val;
+			result.actual_key.val = actual_key.val;
+			result.page_mismatch_idx = i;
+			result.page_mismatch_addr = (unsigned long)cur_page;
+			return result;
+		}
+	}
+
+	result.verify_failed = false;
+	return result;
+}
+
+void skey_report_verify(struct skey_verify_result * const result)
+{
+	if (result->verify_failed)
+		report_fail("page skey mismatch: first page idx = %lu, addr = 0x%lx, "
+			"expected_key = 0x%x, actual_key = 0x%x",
+			result->page_mismatch_idx, result->page_mismatch_addr,
+			result->expected_key.val, result->actual_key.val);
+	else
+		report_pass("skeys match");
+}
diff --git a/lib/s390x/skey.h b/lib/s390x/skey.h
new file mode 100644
index 000000000000..a0f8caa1270b
--- /dev/null
+++ b/lib/s390x/skey.h
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Storage key migration test library
+ *
+ * Copyright IBM Corp. 2022
+ *
+ * Authors:
+ *  Nico Boehr <nrb@linux.ibm.com>
+ */
+#ifndef S390X_SKEY_H
+#define S390X_SKEY_H
+
+#include <libcflat.h>
+#include <asm/facility.h>
+#include <asm/page.h>
+#include <asm/mem.h>
+
+struct skey_verify_result {
+	bool verify_failed;
+	union skey expected_key;
+	union skey actual_key;
+	unsigned long page_mismatch_idx;
+	unsigned long page_mismatch_addr;
+};
+
+void skey_set_keys(uint8_t *pagebuf, unsigned long page_count);
+
+struct skey_verify_result skey_verify_keys(uint8_t *pagebuf, unsigned long page_count);
+
+void skey_report_verify(struct skey_verify_result * const result);
+
+#endif /* S390X_SKEY_H */
diff --git a/s390x/Makefile b/s390x/Makefile
index bf1504f9d58c..d097b7071dfb 100644
--- a/s390x/Makefile
+++ b/s390x/Makefile
@@ -99,6 +99,7 @@ cflatobjs += lib/s390x/malloc_io.o
 cflatobjs += lib/s390x/uv.o
 cflatobjs += lib/s390x/sie.o
 cflatobjs += lib/s390x/fault.o
+cflatobjs += lib/s390x/skey.o
 
 OBJDIRS += lib/s390x
 
diff --git a/s390x/migration-skey.c b/s390x/migration-skey.c
index b7bd82581abe..fed6fc1ed0f8 100644
--- a/s390x/migration-skey.c
+++ b/s390x/migration-skey.c
@@ -10,55 +10,23 @@
 
 #include <libcflat.h>
 #include <asm/facility.h>
-#include <asm/page.h>
-#include <asm/mem.h>
-#include <asm/interrupt.h>
 #include <hardware.h>
+#include <skey.h>
 
 #define NUM_PAGES 128
-static uint8_t pagebuf[NUM_PAGES][PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
+static uint8_t pagebuf[NUM_PAGES * PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
 
 static void test_migration(void)
 {
-	union skey expected_key, actual_key;
-	int i, key_to_set, key_mismatches = 0;
+	struct skey_verify_result result;
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		/*
-		 * Storage keys are 7 bit, lowest bit is always returned as zero
-		 * by iske.
-		 * This loop will set all 7 bits which means we set fetch
-		 * protection as well as reference and change indication for
-		 * some keys.
-		 */
-		key_to_set = i * 2;
-		set_storage_key(pagebuf[i], key_to_set, 1);
-	}
+	skey_set_keys(pagebuf, NUM_PAGES);
 
 	puts("Please migrate me, then press return\n");
 	(void)getchar();
 
-	for (i = 0; i < NUM_PAGES; i++) {
-		actual_key.val = get_storage_key(pagebuf[i]);
-		expected_key.val = i * 2;
-
-		/*
-		 * The PoP neither gives a guarantee that the reference bit is
-		 * accurate nor that it won't be cleared by hardware. Hence we
-		 * don't rely on it and just clear the bits to avoid compare
-		 * errors.
-		 */
-		actual_key.str.rf = 0;
-		expected_key.str.rf = 0;
-
-		/* don't log anything when key matches to avoid spamming the log */
-		if (actual_key.val != expected_key.val) {
-			key_mismatches++;
-			report_fail("page %d expected_key=0x%x actual_key=0x%x", i, expected_key.val, actual_key.val);
-		}
-	}
-
-	report(!key_mismatches, "skeys after migration match");
+	result = skey_verify_keys(pagebuf, NUM_PAGES);
+	skey_report_verify(&result);
 }
 
 int main(void)
-- 
2.36.1


  reply	other threads:[~2022-12-01  8:48 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-01  8:46 [kvm-unit-tests PATCH v1 0/3] s390x: test storage keys during migration Nico Boehr
2022-12-01  8:46 ` Nico Boehr [this message]
2022-12-01 13:16   ` [kvm-unit-tests PATCH v1 1/3] s390x: add library for skey-related functions Claudio Imbrenda
2022-12-01 15:55     ` Nico Boehr
2022-12-01 16:46       ` Claudio Imbrenda
2022-12-02  9:03   ` Janosch Frank
2022-12-02  9:09     ` Thomas Huth
2022-12-02 10:44       ` Nico Boehr
2022-12-02 11:32         ` Thomas Huth
2022-12-02 11:56           ` Janosch Frank
2022-12-02 12:48             ` Thomas Huth
2022-12-02 12:56               ` Claudio Imbrenda
2022-12-09  9:02                 ` Nico Boehr
2022-12-02 10:39     ` Nico Boehr
2022-12-02 11:20       ` Janosch Frank
2022-12-02 11:29         ` Claudio Imbrenda
2022-12-01  8:46 ` [kvm-unit-tests PATCH v1 2/3] lib: s390x: skey: add seed value for storage keys Nico Boehr
2022-12-01 13:27   ` Claudio Imbrenda
2022-12-01 15:16     ` Nico Boehr
2022-12-01  8:46 ` [kvm-unit-tests PATCH v1 3/3] s390x: add storage key test during migration Nico Boehr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221201084642.3747014-2-nrb@linux.ibm.com \
    --to=nrb@linux.ibm.com \
    --cc=frankja@linux.ibm.com \
    --cc=imbrenda@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=thuth@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox