lib: sbi: add some macros to detect BUG at runtime
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.
Signed-off-by Xiang W <[email protected]>
--- include/sbi/sbi_console.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/sbi/sbi_console.h +14 -0
@@ -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
PATCH 2/2 lib: sbi: Add runtime bug detection for csr_read_num/csr_write_num
Signed-off-by Xiang W <[email protected]>
--- lib/sbi/riscv_asm.c | 2 ++ 1 file changed, 2 insertions(+)
lib/sbi/riscv_asm.c +2 -0
@@ -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
PATCH 1/2 lib: sbi: add some macros to detect BUG at runtime
In reply to Xiang WQuoted 27 lines · Xiang W
Two macros are mainly added. One is called
BUG(), which is used to put in unreachable branches. One namedBUG_ON, used for assertion.Signed-off-by Xiang W <[email protected]>--- include/sbi/sbi_console.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/sbi/sbi_console.h +3
-0@@ -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