Skip to contentSkip to conversation

lib: sbi: add some macros to detect BUG at runtime

PATCH Superseded Xiang W 2 participants 3 messages Sep 15, 2021 RISC-V

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH 1/2 lib: sbi: add some macros to detect BUG at runtime

Two macros are mainly added. One is called BUG(), which is used to put in unreachable branches. One named BUG_ON, used for assertion.

---
 include/sbi/sbi_console.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/sbi/sbi_console.h +14 -0
index e24ba5f..ce08541 100644
@@ -11,6 +11,7 @@
 #define __SBI_CONSOLE_H__
 
 #include <sbi/sbi_types.h>
+#include <sbi/riscv_asm.h>
 
 struct sbi_console_device {
 	/** Name of the console device */
@@ -51,4 +52,17 @@ struct sbi_scratch;
 
 int sbi_console_init(struct sbi_scratch *scratch);
 
+#define BUG() do { \
+	sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \
+	while (1)	\
+		wfi();	\
+	__builtin_unreachable(); \
+} while (0)
+
+#define BUG_ON(cond) do { \
+	if (cond)	\
+		break;	\
+	BUG(); \
+} while (1)
+
 #endif
Signature · 1 line
2.30.2
author
#1 lore raw

PATCH 2/2 lib: sbi: Add runtime bug detection for csr_read_num/csr_write_num

---
 lib/sbi/riscv_asm.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/riscv_asm.c +2 -0
index 4c24a51..ed9cee7 100644
@@ -134,6 +134,7 @@ unsigned long csr_read_num(int csr_num)
 #endif
 
 	default:
+		BUG();
 		break;
 	};
 
@@ -197,6 +198,7 @@ void csr_write_num(int csr_num, unsigned long val)
 	switchcase_csr_write_16(CSR_MHPMEVENT16, val)
 
 	default:
+		BUG();
 		break;
 	};
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH 1/2 lib: sbi: add some macros to detect BUG at runtime

In reply to Xiang W
Quoted 27 lines · Xiang W

Two macros are mainly added. One is called BUG(), which is used to put in unreachable branches. One named BUG_ON, used for assertion.

---
 include/sbi/sbi_console.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/sbi/sbi_console.h +3 -0
index e24ba5f..ce08541 100644
@@ -11,6 +11,7 @@
 #define __SBI_CONSOLE_H__
 
 #include <sbi/sbi_types.h>
+#include <sbi/riscv_asm.h>
 
 struct sbi_console_device {
        /** Name of the console device */
@@ -51,4 +52,17 @@ struct sbi_scratch;
 
 int sbi_console_init(struct sbi_scratch *scratch);
 
+#define BUG() do { \
+       sbi_printf("BUG: failure at %s:%d/%s()!\n", __FILE__, __LINE__, __func__); \

include/sbi/sbi_console.h struct sbi_scratch; · line 66

Quoted 10 lines
include/sbi/sbi_console.h +10 -0
+       while (1)       \
+               wfi();  \
+       __builtin_unreachable(); \
+} while (0)
+
+#define BUG_ON(cond) do { \
+       if (cond)       \
+               break;  \
+       BUG(); \
+} while (1)

Shouldn't this be:

if (cond)

    BUG()

?

Regards,
Bin