Skip to contentSkip to conversation

riscv: sbi: Provide sbiret_report/check

PATCH In review Andrew Jones 2 participants 6 messages Feb 18, 2025 – Mar 4, 2025

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

v2 cover riscv: sbi: Provide sbiret_report/check

It's commom for SBI tests to expect a particular error return (or no error, which case it expects the returned error to be SBI_SUCCESS). When we don't get the expected error it'd be nice to output what we did get. gen_report() in the SBI tests were doing that for the BASE tests, improve it and export it to be used by all SBI tests.

v2:
- Output expected error string with sbiret_report_error() [Clément]
- Picked up Clément's tags except for patch3 since it changed a decent amount.

Andrew Jones (3):
riscv: sbi: Improve gen_report
riscv: sbi: Export sbiret_report/check
riscv: sbi: Add sbiret_report_error

 riscv/sbi-tests.h | 36 ++++++++++++++++++++++++++++++++++++
 riscv/sbi.c       | 30 ++++++++----------------------
 2 files changed, 44 insertions(+), 22 deletions(-)
Signature · 7 lines
2.48.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#2 lore raw

PATCH v2 2/3 riscv: sbi: Export sbiret_report/check

As more SBI tests are written not all will be in riscv/sbi.c. Export sbiret reporting functions to be used in other SBI test files.

---
 riscv/sbi-tests.h | 21 +++++++++++++++++++++
 riscv/sbi.c       | 17 -----------------
 2 files changed, 21 insertions(+), 17 deletions(-)
riscv/sbi-tests.h +21 -0
index ce129968fe99..2bdc9440d82d 100644
@@ -33,4 +33,25 @@
 #define SBI_SUSP_TEST_HARTID	(1 << 2)
 #define SBI_SUSP_TEST_MASK	7
 
+#ifndef __ASSEMBLY__
+#include <asm/sbi.h>
+
+#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+	long ex_err = expected_error;										\
+	long ex_val = expected_value;										\
+	bool ch_err = (ret)->error == ex_err;									\
+	bool ch_val = (ret)->value == ex_val;									\
+	bool pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+														\
+	if (!pass)												\
+		report_info(fmt ": expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",	\
+			    ##__VA_ARGS__, ex_err, ex_val, (ret)->error, (ret)->value);				\
+														\
+	pass;													\
+})
+
+#define sbiret_check(ret, expected_error, expected_value) \
+	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
+
+#endif /* __ASSEMBLY__ */
 #endif /* _RISCV_SBI_TESTS_H_ */
riscv/sbi.c +0 -17
index 9f591f8ff76a..3eca8c7e35ed 100644
@@ -156,23 +156,6 @@ static bool get_invalid_addr(phys_addr_t *paddr, bool allow_default)
 	return false;
 }
 
-#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
-	long ex_err = expected_error;										\
-	long ex_val = expected_value;										\
-	bool ch_err = (ret)->error == ex_err;									\
-	bool ch_val = (ret)->value == ex_val;									\
-	bool pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
-														\
-	if (!pass)												\
-		report_info(fmt ": expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",	\
-			    ##__VA_ARGS__, ex_err, ex_val, (ret)->error, (ret)->value);				\
-														\
-	pass;													\
-})
-
-#define sbiret_check(ret, expected_error, expected_value) \
-	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
-
 static void check_base(void)
 {
 	struct sbiret ret;
Signature · 7 lines
2.48.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#3 lore raw

PATCH v2 3/3 riscv: sbi: Add sbiret_report_error

In reply to Andrew Jones

An sbiret value may be unspecified in the case of errors, and even in the case of success when the function doesn't return anything. Provide an sbiret report function that only checks sbiret.error.

sbiret_report_error() has a different PASS format than sbiret_report(). The former will always output the stringized expected error so the format string doesn't need to contain it, e.g.

PASS: sbi: test foo: SBI_SUCCESS
PASS: sbi: test event id 0x0: SBI_INVALID_PARAM

---
 riscv/sbi-tests.h | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
riscv/sbi-tests.h +18 -3
index 2bdc9440d82d..7a24e083a7b8 100644
@@ -36,20 +36,35 @@
 #ifndef __ASSEMBLY__
 #include <asm/sbi.h>
 
-#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+#define __sbiret_report(ret, expected_error, expected_value, has_value, expected_error_name, fmt, ...) ({	\
 	long ex_err = expected_error;										\
 	long ex_val = expected_value;										\
+	bool has_val = !!(has_value);										\
 	bool ch_err = (ret)->error == ex_err;									\
 	bool ch_val = (ret)->value == ex_val;									\
-	bool pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+	bool pass;												\
 														\
-	if (!pass)												\
+	if (has_val)												\
+		pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+	else													\
+		pass = report(ch_err, fmt ": %s", ##__VA_ARGS__, expected_error_name);				\
+														\
+	if (!pass && has_val)											\
 		report_info(fmt ": expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",	\
 			    ##__VA_ARGS__, ex_err, ex_val, (ret)->error, (ret)->value);				\
+	else if (!pass)												\
+		report_info(fmt ": %s (%ld): received error %ld",						\
+			    ##__VA_ARGS__, expected_error_name, ex_err, (ret)->error);				\
 														\
 	pass;													\
 })
 
+#define sbiret_report(ret, expected_error, expected_value, ...) \
+	__sbiret_report(ret, expected_error, expected_value, true, #expected_error, __VA_ARGS__)
+
+#define sbiret_report_error(ret, expected_error, ...) \
+	__sbiret_report(ret, expected_error, 0, false, #expected_error, __VA_ARGS__)
+
 #define sbiret_check(ret, expected_error, expected_value) \
 	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
 
Signature · 7 lines
2.48.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#1 lore raw

PATCH v2 1/3 riscv: sbi: Improve gen_report

In reply to Andrew Jones

Make several improvements to gen_report(), starting by renaming it to something less generic (sbiret_report) and then, instead of relying on report prefix to link the report_info with the unexpected return values to the failed test, use the test string itself, by taking it as a parameter. And, reporting sbiret.error and sbiret.value separately was more verbose than necessary since the report_info can be used to see which one failed, so combine them. Finally, return the pass/fail result of the test in case a caller wants to use it.

---
 riscv/sbi.c | 45 ++++++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 21 deletions(-)
riscv/sbi.c +24 -21
index 6f4ddaf13df3..9f591f8ff76a 100644
@@ -156,19 +156,22 @@ static bool get_invalid_addr(phys_addr_t *paddr, bool allow_default)
 	return false;
 }
 
-static void gen_report(struct sbiret *ret,
-		       long expected_error, long expected_value)
-{
-	bool check_error = ret->error == expected_error;
-	bool check_value = ret->value == expected_value;
-
-	if (!check_error || !check_value)
-		report_info("expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",
-			    expected_error, expected_value, ret->error, ret->value);
-
-	report(check_error, "expected sbi.error");
-	report(check_value, "expected sbi.value");
-}
+#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+	long ex_err = expected_error;										\
+	long ex_val = expected_value;										\
+	bool ch_err = (ret)->error == ex_err;									\
+	bool ch_val = (ret)->value == ex_val;									\
+	bool pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+														\
+	if (!pass)												\
+		report_info(fmt ": expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",	\
+			    ##__VA_ARGS__, ex_err, ex_val, (ret)->error, (ret)->value);				\
+														\
+	pass;													\
+})
+
+#define sbiret_check(ret, expected_error, expected_value) \
+	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
 
 static void check_base(void)
 {
@@ -184,7 +187,7 @@ static void check_base(void)
 		expected = (long)strtoul(getenv("SBI_SPEC_VERSION"), NULL, 0);
 		assert_msg(!(expected & BIT(31)), "SBI spec version bit 31 must be zero");
 		assert_msg(__riscv_xlen == 32 || !(expected >> 32), "SBI spec version bits greater than 31 must be zero");
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_pop();
 
@@ -199,7 +202,7 @@ static void check_base(void)
 	if (env_or_skip("SBI_IMPL_ID")) {
 		expected = (long)strtoul(getenv("SBI_IMPL_ID"), NULL, 0);
 		ret = sbi_base(SBI_EXT_BASE_GET_IMP_ID, 0);
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_pop();
 
@@ -207,17 +210,17 @@ static void check_base(void)
 	if (env_or_skip("SBI_IMPL_VERSION")) {
 		expected = (long)strtoul(getenv("SBI_IMPL_VERSION"), NULL, 0);
 		ret = sbi_base(SBI_EXT_BASE_GET_IMP_VERSION, 0);
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_pop();
 
 	report_prefix_push("probe_ext");
 	expected = getenv("SBI_PROBE_EXT") ? (long)strtoul(getenv("SBI_PROBE_EXT"), NULL, 0) : 1;
 	ret = sbi_base(SBI_EXT_BASE_PROBE_EXT, SBI_EXT_BASE);
-	gen_report(&ret, 0, expected);
+	sbiret_check(&ret, 0, expected);
 	report_prefix_push("unavailable");
 	ret = sbi_base(SBI_EXT_BASE_PROBE_EXT, 0xb000000);
-	gen_report(&ret, 0, 0);
+	sbiret_check(&ret, 0, 0);
 	report_prefix_popn(2);
 
 	report_prefix_push("mvendorid");
@@ -225,7 +228,7 @@ static void check_base(void)
 		expected = (long)strtoul(getenv("MVENDORID"), NULL, 0);
 		assert(__riscv_xlen == 32 || !(expected >> 32));
 		ret = sbi_base(SBI_EXT_BASE_GET_MVENDORID, 0);
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_pop();
 
@@ -233,7 +236,7 @@ static void check_base(void)
 	if (env_or_skip("MARCHID")) {
 		expected = (long)strtoul(getenv("MARCHID"), NULL, 0);
 		ret = sbi_base(SBI_EXT_BASE_GET_MARCHID, 0);
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_pop();
 
@@ -241,7 +244,7 @@ static void check_base(void)
 	if (env_or_skip("MIMPID")) {
 		expected = (long)strtoul(getenv("MIMPID"), NULL, 0);
 		ret = sbi_base(SBI_EXT_BASE_GET_MIMPID, 0);
-		gen_report(&ret, 0, expected);
+		sbiret_check(&ret, 0, expected);
 	}
 	report_prefix_popn(2);
 }
Signature · 7 lines
2.48.1


-- 
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
Tested-by Reviewed-by on v2 3/3
#4 lore raw

Re: [kvm-unit-tests PATCH v2 3/3] riscv: sbi: Add sbiret_report_error

In reply to Andrew Jones
Quoted 49 lines · Andrew Jones

An sbiret value may be unspecified in the case of errors, and even in the case of success when the function doesn't return anything. Provide an sbiret report function that only checks sbiret.error.

sbiret_report_error() has a different PASS format than sbiret_report(). The former will always output the stringized expected error so the format string doesn't need to contain it, e.g.

PASS: sbi: test foo: SBI_SUCCESS
PASS: sbi: test event id 0x0: SBI_INVALID_PARAM

---
 riscv/sbi-tests.h | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
riscv/sbi-tests.h +12 -3
index 2bdc9440d82d..7a24e083a7b8 100644
@@ -36,20 +36,35 @@
 #ifndef __ASSEMBLY__
 #include <asm/sbi.h>
 
-#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+#define __sbiret_report(ret, expected_error, expected_value, has_value, expected_error_name, fmt, ...) ({	\
 	long ex_err = expected_error;										\
 	long ex_val = expected_value;										\
+	bool has_val = !!(has_value);										\
 	bool ch_err = (ret)->error == ex_err;									\
 	bool ch_val = (ret)->value == ex_val;									\
-	bool pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+	bool pass;												\
 														\
-	if (!pass)												\
+	if (has_val)												\
+		pass = report(ch_err && ch_val, fmt, ##__VA_ARGS__);						\
+	else													\
+		pass = report(ch_err, fmt ": %s", ##__VA_ARGS__, expected_error_name);				\
+														\
+	if (!pass && has_val)											\
 		report_info(fmt ": expected (error: %ld, value: %ld), received: (error: %ld, value %ld)",	\
 			    ##__VA_ARGS__, ex_err, ex_val, (ret)->error, (ret)->value);				\
+	else if (!pass)												\
+		report_info(fmt ": %s (%ld): received error %ld",						\
+			    ##__VA_ARGS__, expected_error_name, ex_err, (ret)->error);				\
 														\
 	pass;													\
 })

riscv/sbi-tests.h line 70

Quoted 10 lines
riscv/sbi-tests.h +6 -0
 
+#define sbiret_report(ret, expected_error, expected_value, ...) \
+	__sbiret_report(ret, expected_error, expected_value, true, #expected_error, __VA_ARGS__)
+
+#define sbiret_report_error(ret, expected_error, ...) \
+	__sbiret_report(ret, expected_error, 0, false, #expected_error, __VA_ARGS__)
+
 #define sbiret_check(ret, expected_error, expected_value) \
 	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
 

Hey Andrew,

Thanks,

Clément

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#5 lore raw
In reply to Andrew Jones
Quoted 11 lines · Andrew Jones

It's commom for SBI tests to expect a particular error return (or no error, which case it expects the returned error to be SBI_SUCCESS). When we don't get the expected error it'd be nice to output what we did get. gen_report() in the SBI tests were doing that for the BASE tests, improve it and export it to be used by all SBI tests.

v2:
- Output expected error string with sbiret_report_error() [Clément]
- Picked up Clément's tags except for patch3 since it changed a decent amount.

Quoted 12 lines

Andrew Jones (3):
riscv: sbi: Improve gen_report
riscv: sbi: Export sbiret_report/check
riscv: sbi: Add sbiret_report_error

 riscv/sbi-tests.h | 36 ++++++++++++++++++++++++++++++++++++
 riscv/sbi.c       | 30 ++++++++----------------------
 2 files changed, 44 insertions(+), 22 deletions(-)
Signature · 1 line
2.48.1

Merged.

Thanks,
drew

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv