Skip to contentSkip to conversation

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

PATCH Superseded Xiang W 1 participant 2 messages Sep 15, 2021 RISC-V

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH v3 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 | 13 +++++++++++++
 1 file changed, 13 insertions(+)
include/sbi/sbi_console.h +13 -0
index e24ba5f..a6c4f9c 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,16 @@ 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)	\
+		BUG();	\
+} while (0)
+
 #endif
Signature · 1 line
2.30.2
author
#1 lore raw

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

---
 lib/sbi/riscv_asm.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/riscv_asm.c +4 -0
index 4c24a51..281e50a 100644
@@ -11,6 +11,7 @@
 #include <sbi/riscv_encoding.h>
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_platform.h>
+#include <sbi/sbi_console.h>
 
 /* determine CPU extension, return non-zero support */
 int misa_extension_imp(char ext)
@@ -75,6 +76,7 @@ void misa_string(int xlen, char *out, unsigned int out_sz)
 			out[pos++] = '8';
 			break;
 		default:
+			BUG();
 			return;
 		}
 	}
@@ -134,6 +136,7 @@ unsigned long csr_read_num(int csr_num)
 #endif
 
 	default:
+		BUG();
 		break;
 	};
 
@@ -197,6 +200,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