Skip to contentSkip to conversation

riscv: sbi: Add SUSP tests

PATCH Superseded Andrew Jones 1 participant 6 messages Oct 24, 2024 – Nov 11, 2024

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover riscv: sbi: Add SUSP tests

Add tests for the SBI SUSP extension.

Based-on: https://gitlab.com/jones-drew/kvm-unit-tests/-/commits/riscv/sbi

Andrew Jones (3):
riscv: Implement setjmp/longjmp
riscv: sbi: Clean up env checking
riscv: sbi: Add SUSP tests

 lib/riscv/asm/asm.h |   3 +
 lib/riscv/asm/sbi.h |   1 +
 lib/riscv/setjmp.S  |  50 +++++++
 lib/setjmp.h        |   4 +
 riscv/Makefile      |   1 +
 riscv/sbi-asm.S     |  55 ++++++++
 riscv/sbi-tests.h   |  19 +++
 riscv/sbi.c         | 308 ++++++++++++++++++++++++++++++++++++++++----
 8 files changed, 417 insertions(+), 24 deletions(-)
 create mode 100644 lib/riscv/setjmp.S
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/3 riscv: Implement setjmp/longjmp

Being able to do setjmp and longjmp can be quite useful for tests. Implement the functions for riscv.

---
 lib/riscv/setjmp.S | 50 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/setjmp.h       |  4 ++++
 riscv/Makefile     |  1 +
 3 files changed, 55 insertions(+)
 create mode 100644 lib/riscv/setjmp.S
lib/riscv/setjmp.S new file +50 -0
new file mode 100644
index 000000000000..38b0f1cab576
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#define __ASSEMBLY__
+#include <asm/asm.h>
+
+.section .text
+
+.balign 4
+.global setjmp
+setjmp:
+	REG_S	ra, (0 * SZREG)(a0)
+	REG_S	s0, (1 * SZREG)(a0)
+	REG_S	s1, (2 * SZREG)(a0)
+	REG_S	s2, (3 * SZREG)(a0)
+	REG_S	s3, (4 * SZREG)(a0)
+	REG_S	s4, (5 * SZREG)(a0)
+	REG_S	s5, (6 * SZREG)(a0)
+	REG_S	s6, (7 * SZREG)(a0)
+	REG_S	s7, (8 * SZREG)(a0)
+	REG_S	s8, (9 * SZREG)(a0)
+	REG_S	s9, (10 * SZREG)(a0)
+	REG_S	s10, (11 * SZREG)(a0)
+	REG_S	s11, (12 * SZREG)(a0)
+	REG_S	sp, (13 * SZREG)(a0)
+	REG_S	gp, (14 * SZREG)(a0)
+	REG_S	tp, (15 * SZREG)(a0)
+	li	a0, 0
+	ret
+
+.balign 4
+.global longjmp
+longjmp:
+	REG_L	ra, (0 * SZREG)(a0)
+	REG_L	s0, (1 * SZREG)(a0)
+	REG_L	s1, (2 * SZREG)(a0)
+	REG_L	s2, (3 * SZREG)(a0)
+	REG_L	s3, (4 * SZREG)(a0)
+	REG_L	s4, (5 * SZREG)(a0)
+	REG_L	s5, (6 * SZREG)(a0)
+	REG_L	s6, (7 * SZREG)(a0)
+	REG_L	s7, (8 * SZREG)(a0)
+	REG_L	s8, (9 * SZREG)(a0)
+	REG_L	s9, (10 * SZREG)(a0)
+	REG_L	s10, (11 * SZREG)(a0)
+	REG_L	s11, (12 * SZREG)(a0)
+	REG_L	sp, (13 * SZREG)(a0)
+	REG_L	gp, (14 * SZREG)(a0)
+	REG_L	tp, (15 * SZREG)(a0)
+	seqz	a0, a1
+	add	a0, a0, a1
+	ret
lib/setjmp.h +4 -0
index 6afdf665681a..f878ad81c645 100644
@@ -8,7 +8,11 @@
 #define _LIBCFLAT_SETJMP_H_
 
 typedef struct jmp_buf_tag {
+#if defined(__i386__) || defined(__x86_64__)
 	long int regs[8];
+#elif defined(__riscv)
+	long int regs[16];
+#endif
 } jmp_buf[1];
 
 extern int setjmp (struct jmp_buf_tag env[1]);
riscv/Makefile +1 -0
index 734441f94dad..28b04156bfd5 100644
@@ -36,6 +36,7 @@ cflatobjs += lib/riscv/isa.o
 cflatobjs += lib/riscv/mmu.o
 cflatobjs += lib/riscv/processor.o
 cflatobjs += lib/riscv/sbi.o
+cflatobjs += lib/riscv/setjmp.o
 cflatobjs += lib/riscv/setup.o
 cflatobjs += lib/riscv/smp.o
 cflatobjs += lib/riscv/stack.o
Signature · 1 line
2.47.0
#2 lore raw

PATCH 2/3 riscv: sbi: Clean up env checking

In reply to Andrew Jones

Add a couple helpers to cleanup checking of test configuration environment variables.

---
 riscv/sbi.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
riscv/sbi.c +19 -13
index d46befa1c6c1..1e7314ec8d98 100644
@@ -74,6 +74,20 @@ static phys_addr_t get_highest_addr(void)
 	return highest_end - 1;
 }
 
+static bool env_enabled(const char *env)
+{
+	char *s = getenv(env);
+
+	return s && (*s == '1' || *s == 'y' || *s == 'Y');
+}
+
+static bool env_disabled(const char *env)
+{
+	char *s = getenv(env);
+
+	return !(s && (*s == '0' || *s == 'n' || *s == 'N'));
+}
+
 static bool env_or_skip(const char *env)
 {
 	if (!getenv(env)) {
@@ -348,7 +362,6 @@ static void check_dbcn(void)
 	bool highmem_supported = true;
 	phys_addr_t paddr;
 	struct sbiret ret;
-	const char *tmp;
 	char *buf;
 
 	report_prefix_push("dbcn");
@@ -371,13 +384,11 @@ static void check_dbcn(void)
 	dbcn_write_test(&buf[PAGE_SIZE - num_bytes / 2], num_bytes, false);
 	report_prefix_pop();
 
-	tmp = getenv("SBI_HIGHMEM_NOT_SUPPORTED");
-	if (tmp && atol(tmp) != 0)
+	if (env_enabled("SBI_HIGHMEM_NOT_SUPPORTED"))
 		highmem_supported = false;
 
 	report_prefix_push("high boundary");
-	tmp = getenv("SBI_DBCN_SKIP_HIGH_BOUNDARY");
-	if (!tmp || atol(tmp) == 0)
+	if (env_disabled("SBI_DBCN_SKIP_HIGH_BOUNDARY"))
 		dbcn_high_write_test(DBCN_WRITE_TEST_STRING, num_bytes,
 				     HIGH_ADDR_BOUNDARY - PAGE_SIZE, PAGE_SIZE - num_bytes / 2,
 				     highmem_supported);
@@ -386,12 +397,8 @@ static void check_dbcn(void)
 	report_prefix_pop();
 
 	report_prefix_push("high page");
-	tmp = getenv("SBI_DBCN_SKIP_HIGH_PAGE");
-	if (!tmp || atol(tmp) == 0) {
-		paddr = HIGH_ADDR_BOUNDARY;
-		tmp = getenv("HIGH_PAGE");
-		if (tmp)
-			paddr = strtoull(tmp, NULL, 0);
+	if (env_disabled("SBI_DBCN_SKIP_HIGH_PAGE")) {
+		paddr = getenv("HIGH_PAGE") ? strtoull(getenv("HIGH_PAGE"), NULL, 0) : HIGH_ADDR_BOUNDARY;
 		dbcn_high_write_test(DBCN_WRITE_TEST_STRING, num_bytes, paddr, 0, highmem_supported);
 	} else {
 		report_skip("user disabled");
@@ -400,8 +407,7 @@ static void check_dbcn(void)
 
 	/* Bytes are read from memory and written to the console */
 	report_prefix_push("invalid parameter");
-	tmp = getenv("INVALID_ADDR_AUTO");
-	if (tmp && atol(tmp) == 1) {
+	if (env_enabled("INVALID_ADDR_AUTO")) {
 		paddr = get_highest_addr() + 1;
 		do_invalid_addr = true;
 	} else if (env_or_skip("INVALID_ADDR")) {
Signature · 1 line
2.47.0
#4 lore raw

PATCH 3/3 riscv: sbi: Add SUSP tests

In reply to Andrew Jones

Introduce tests for SBI system suspend. The basic test makes sure it works and other tests make sure it fails as expected with invalid entry criteria.

To test on QEMU or hardware the firmware needs to support system suspend. For QEMU, OpenSBI can be told to enable its system suspend test mode by creating a new DTB which has

    opensbi-config {
        compatible = "opensbi,config";
        system-suspend-test;
    };

added to the 'chosen' node. Then, run with '-dtb susp.dtb'.

---
 lib/riscv/asm/asm.h |   3 +
 lib/riscv/asm/sbi.h |   1 +
 riscv/sbi-asm.S     |  55 +++++++++
 riscv/sbi-tests.h   |  19 +++
 riscv/sbi.c         | 278 ++++++++++++++++++++++++++++++++++++++++++--
 5 files changed, 344 insertions(+), 12 deletions(-)
lib/riscv/asm/asm.h +3 -0
index 763b28e6ad3c..107b5bb7e981 100644
@@ -14,6 +14,9 @@
 #define REG_S	__REG_SEL(sd, sw)
 #define SZREG	__REG_SEL(8, 4)
 
+/* ASMARR() may be used with arrays of longs */
+#define ASMARR(reg, idx)	((idx) * SZREG)(reg)
+
 #define FP_SIZE 16
 
 #endif /* _ASMRISCV_ASM_H_ */
lib/riscv/asm/sbi.h +1 -0
index 1319439b7118..4e72f125fb43 100644
@@ -22,6 +22,7 @@ enum sbi_ext_id {
 	SBI_EXT_HSM = 0x48534d,
 	SBI_EXT_SRST = 0x53525354,
 	SBI_EXT_DBCN = 0x4442434E,
+	SBI_EXT_SUSP = 0x53555350,
 };
 
 enum sbi_ext_base_fid {
riscv/sbi-asm.S +55 -0

64 lines not on this page · raw message

riscv/sbi-tests.h +19 -0
index f5cc8635d2aa..d0a7561a47b3 100644
@@ -2,9 +2,28 @@
 #ifndef _RISCV_SBI_TESTS_H_
 #define _RISCV_SBI_TESTS_H_
 
+#define SBI_CSR_SSTATUS_IDX	0
+#define SBI_CSR_SIE_IDX		1
+#define SBI_CSR_STVEC_IDX	2
+#define SBI_CSR_SSCRATCH_IDX	3
+#define SBI_CSR_SATP_IDX	4
+
 #define SBI_HSM_TEST_DONE	(1 << 0)
 #define SBI_HSM_TEST_HARTID_A1	(1 << 1)
 #define SBI_HSM_TEST_SATP	(1 << 2)
 #define SBI_HSM_TEST_SIE	(1 << 3)
 
+#define SBI_SUSP_TEST_SATP	(1 << 0)
+#define SBI_SUSP_TEST_SIE	(1 << 1)
+#define SBI_SUSP_TEST_HARTID	(1 << 2)
+#define SBI_SUSP_TEST_MASK	7
+
+#define SBI_SUSP_MAGIC		0x505b
+
+#define SBI_SUSP_MAGIC_IDX	0
+#define SBI_SUSP_CSRS_IDX	1
+#define SBI_SUSP_HARTID_IDX	2
+#define SBI_SUSP_TESTNUM_IDX	3
+#define SBI_SUSP_RESULTS_IDX	4
+
 #endif /* _RISCV_SBI_TESTS_H_ */
riscv/sbi.c +266 -12

329 lines not on this page · raw message

Signature · 1 line
2.47.0
#3 lore raw

Re: [kvm-unit-tests PATCH 2/3] riscv: sbi: Clean up env checking

In reply to Andrew Jones
Quoted 19 lines · Andrew Jones

Add a couple helpers to cleanup checking of test configuration environment variables.

---
 riscv/sbi.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
riscv/sbi.c +3 -0
index d46befa1c6c1..1e7314ec8d98 100644
@@ -74,6 +74,20 @@ static phys_addr_t get_highest_addr(void)
 	return highest_end - 1;
 }
 
+static bool env_enabled(const char *env)
+{
+	char *s = getenv(env);

riscv/sbi.c static phys_addr_t get_highest_addr(void) · line 89

Quoted 10 lines
riscv/sbi.c +10 -0
+
+	return s && (*s == '1' || *s == 'y' || *s == 'Y');
+}
+
+static bool env_disabled(const char *env)
+{
+	char *s = getenv(env);
+
+	return !(s && (*s == '0' || *s == 'n' || *s == 'N'));
+}

I've dropped env_disabled() since what I really want is !env_enabled()

Quoted 61 lines
riscv/sbi.c +6 -13
+
 static bool env_or_skip(const char *env)
 {
 	if (!getenv(env)) {
@@ -348,7 +362,6 @@ static void check_dbcn(void)
 	bool highmem_supported = true;
 	phys_addr_t paddr;
 	struct sbiret ret;
-	const char *tmp;
 	char *buf;
 
 	report_prefix_push("dbcn");
@@ -371,13 +384,11 @@ static void check_dbcn(void)
 	dbcn_write_test(&buf[PAGE_SIZE - num_bytes / 2], num_bytes, false);
 	report_prefix_pop();
 
-	tmp = getenv("SBI_HIGHMEM_NOT_SUPPORTED");
-	if (tmp && atol(tmp) != 0)
+	if (env_enabled("SBI_HIGHMEM_NOT_SUPPORTED"))
 		highmem_supported = false;
 
 	report_prefix_push("high boundary");
-	tmp = getenv("SBI_DBCN_SKIP_HIGH_BOUNDARY");
-	if (!tmp || atol(tmp) == 0)
+	if (env_disabled("SBI_DBCN_SKIP_HIGH_BOUNDARY"))
 		dbcn_high_write_test(DBCN_WRITE_TEST_STRING, num_bytes,
 				     HIGH_ADDR_BOUNDARY - PAGE_SIZE, PAGE_SIZE - num_bytes / 2,
 				     highmem_supported);
@@ -386,12 +397,8 @@ static void check_dbcn(void)
 	report_prefix_pop();
 
 	report_prefix_push("high page");
-	tmp = getenv("SBI_DBCN_SKIP_HIGH_PAGE");
-	if (!tmp || atol(tmp) == 0) {
-		paddr = HIGH_ADDR_BOUNDARY;
-		tmp = getenv("HIGH_PAGE");
-		if (tmp)
-			paddr = strtoull(tmp, NULL, 0);
+	if (env_disabled("SBI_DBCN_SKIP_HIGH_PAGE")) {
+		paddr = getenv("HIGH_PAGE") ? strtoull(getenv("HIGH_PAGE"), NULL, 0) : HIGH_ADDR_BOUNDARY;
 		dbcn_high_write_test(DBCN_WRITE_TEST_STRING, num_bytes, paddr, 0, highmem_supported);
 	} else {
 		report_skip("user disabled");
@@ -400,8 +407,7 @@ static void check_dbcn(void)
 
 	/* Bytes are read from memory and written to the console */
 	report_prefix_push("invalid parameter");
-	tmp = getenv("INVALID_ADDR_AUTO");
-	if (tmp && atol(tmp) == 1) {
+	if (env_enabled("INVALID_ADDR_AUTO")) {
 		paddr = get_highest_addr() + 1;
 		do_invalid_addr = true;
 	} else if (env_or_skip("INVALID_ADDR")) {
Signature · 7 lines
2.47.0


-- 
kvm-riscv mailing list
kvm-riscv at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#5 lore raw
In reply to Andrew Jones
Quoted 9 lines · Andrew Jones

Add tests for the SBI SUSP extension.

Based-on: https://gitlab.com/jones-drew/kvm-unit-tests/-/commits/riscv/sbi

Andrew Jones (3):
riscv: Implement setjmp/longjmp
riscv: sbi: Clean up env checking
riscv: sbi: Add SUSP tests

Quoted 14 lines
 lib/riscv/asm/asm.h |   3 +
 lib/riscv/asm/sbi.h |   1 +
 lib/riscv/setjmp.S  |  50 +++++++
 lib/setjmp.h        |   4 +
 riscv/Makefile      |   1 +
 riscv/sbi-asm.S     |  55 ++++++++
 riscv/sbi-tests.h   |  19 +++
 riscv/sbi.c         | 308 ++++++++++++++++++++++++++++++++++++++++----
 8 files changed, 417 insertions(+), 24 deletions(-)
 create mode 100644 lib/riscv/setjmp.S
Signature · 1 line
2.47.0

Merged through riscv/sbi.

Thanks,
drew