Skip to contentSkip to conversation

riscv: sbi: Provide sbiret_report/check

PATCH Superseded Andrew Jones 2 participants 9 messages Jan 29, 2025 – Feb 3, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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.

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

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


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

PATCH 1/3 riscv: sbi: Improve gen_report

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
#4 lore raw

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

In reply to Andrew Jones

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
#6 lore raw

PATCH 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.

---
 riscv/sbi-tests.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
riscv/sbi-tests.h +12 -3
index 2bdc9440d82d..f6e646a79dbe 100644
@@ -36,20 +36,29 @@
 #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, 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__);						\
+	bool pass = report(ch_err && (!(has_value) || ch_val), fmt, ##__VA_ARGS__);				\
 														\
-	if (!pass)												\
+	if (!pass && (has_value))										\
 		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 ": expected (error: %ld), received: (error: %ld)",				\
+			    ##__VA_ARGS__, ex_err, (ret)->error);						\
 														\
 	pass;													\
 })
 
+#define sbiret_report(ret, expected_error, expected_value, ...) \
+	__sbiret_report(ret, expected_error, expected_value, true, __VA_ARGS__)
+
+#define sbiret_report_error(ret, expected_error, ...) \
+	__sbiret_report(ret, expected_error, 0, false, __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
Tested-by Reviewed-by on v1 1/3
#2 lore raw

Re: [kvm-unit-tests PATCH 1/3] riscv: sbi: Improve gen_report

In reply to Andrew Jones

Hey Andrew,

While using it, I wondered if it would be helpful to display the expected return value even in the case it was successful and only display the return value if it was different. I feel like it avoid the need to specify it in the reported string, for instance,

I have a few "test foo no error" or "test invalid event id 0x0". So that displaying them like:

"test foo: SBI_SUCCESS"
"test event id 0x0: SBI_INVALID_PARAM"

could be better. But that's only a personal taste. Anyway, I used these macros and it works has expected:

Thanks,

Clément

Quoted 121 lines · 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 · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
Reviewed-by on v1 2/3
#5 lore raw

Re: [kvm-unit-tests PATCH 2/3] riscv: sbi: Export sbiret_report/check

In reply to Andrew Jones
Quoted 57 lines · Andrew Jones

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 -10
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);				\

riscv/sbi.c static bool get_invalid_addr(phys_addr_t *paddr, bool allow_default) · line 161

Quoted 10 lines
riscv/sbi.c +0 -7
-														\
-	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;

Thanks,

Clément

Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
Tested-by Reviewed-by on v1 3/3
#7 lore raw

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

In reply to Andrew Jones
Quoted 15 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.

---
 riscv/sbi-tests.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
riscv/sbi-tests.h +0 -0
index 2bdc9440d82d..f6e646a79dbe 100644
@@ -36,20 +36,29 @@
 #ifndef __ASSEMBLY__

riscv/sbi-tests.h line 44

Quoted 10 lines
riscv/sbi-tests.h +2 -2
 #include <asm/sbi.h>
 
-#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+#define __sbiret_report(ret, expected_error, expected_value, has_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__);						\
+	bool pass = report(ch_err && (!(has_value) || ch_val), fmt, ##__VA_ARGS__);				\

Nit: I would have moved the has_value check in the ch_val bool but I'm good with both way.

Quoted 11 lines
riscv/sbi-tests.h +4 -1
 														\
-	if (!pass)												\
+	if (!pass && (has_value))										\
 		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 ": expected (error: %ld), received: (error: %ld)",				\
+			    ##__VA_ARGS__, ex_err, (ret)->error);						\
 														\
 	pass;													\
 })

riscv/sbi-tests.h line 64

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, __VA_ARGS__)
+
+#define sbiret_report_error(ret, expected_error, ...) \
+	__sbiret_report(ret, expected_error, 0, false, __VA_ARGS__)
+
 #define sbiret_check(ret, expected_error, expected_value) \
 	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
 

Thanks,

Clément

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

Re: [kvm-unit-tests PATCH 1/3] riscv: sbi: Improve gen_report

In reply to Clément Léger
Quoted 7 lines · Clément Léger

Hey Andrew,

While using it, I wondered if it would be helpful to display the expected return value even in the case it was successful and only display the return value if it was different. I feel like it avoid the need to specify it in the reported string, for instance,

Quoted 5 lines

I have a few "test foo no error" or "test invalid event id 0x0". So that displaying them like:

"test foo: SBI_SUCCESS"
"test event id 0x0: SBI_INVALID_PARAM"

This looks nice and is easy to do now that we use a macro. Of course, in the end, a PASSing line is almost never descriptive enough for people to truly know what was tested anyway. If somebody wants to know what was actually passing then they'll need to read the code. So as long as the strings are unique, even if only by prefix, then we're pretty much good to go.

Thanks,
drew

Quoted 140 lines

could be better. But that's only a personal taste. Anyway, I used these macros and it works has expected:

Thanks,

Clément

Quoted 121 lines · 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 · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
Signature · 3 lines
kvm-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kvm-riscv
#8 lore raw

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

In reply to Clément Léger
Quoted 20 lines · Clément Léger
Quoted 17 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.

---
 riscv/sbi-tests.h | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
riscv/sbi-tests.h +0 -0
index 2bdc9440d82d..f6e646a79dbe 100644
@@ -36,20 +36,29 @@
 #ifndef __ASSEMBLY__
 #include <asm/sbi.h>
 
Quoted 11 lines

riscv/sbi-tests.h line 44

Quoted 8 lines
riscv/sbi-tests.h +2 -2
-#define sbiret_report(ret, expected_error, expected_value, fmt, ...) ({						\
+#define __sbiret_report(ret, expected_error, expected_value, has_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__);						\
+	bool pass = report(ch_err && (!(has_value) || ch_val), fmt, ##__VA_ARGS__);				\

Nit: I would have moved the has_value check in the ch_val bool but I'm good with both way.

Thanks for the review. I'll spin a v2 with this change and also look into outputting the expected error code.

drew

Quoted 34 lines
Quoted 12 lines
riscv/sbi-tests.h +4 -1
 														\
-	if (!pass)												\
+	if (!pass && (has_value))										\
 		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 ": expected (error: %ld), received: (error: %ld)",				\
+			    ##__VA_ARGS__, ex_err, (ret)->error);						\
 														\
 	pass;													\
 })

riscv/sbi-tests.h line 64

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, __VA_ARGS__)
+
+#define sbiret_report_error(ret, expected_error, ...) \
+	__sbiret_report(ret, expected_error, 0, false, __VA_ARGS__)
+
 #define sbiret_check(ret, expected_error, expected_value) \
 	sbiret_report(ret, expected_error, expected_value, "check sbi.error and sbi.value")
 

Thanks,

Clément

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