Skip to contentSkip to conversation

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

PATCH Superseded Xiang W 5 participants 9 messages Sep 15, 2021 – Sep 16, 2021 RISC-V

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

PATCH v2 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..e75a279 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 v2 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
#2 lore raw

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

In reply to Xiang W

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

Quoted 3 lines · Xiang W
include/sbi/sbi_console.h +3 -0
+#define BUG_ON(cond) do { \
+	if (!(cond))	\
+		BUG(); \

Isn't that backwards?

Andreas.

Signature · 3 lines
Andreas Schwab, schwab at linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
author
#3 lore raw

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

? 2021-09-15???? 11:27 +0200?Andreas Schwab???

Quoted 7 lines · Xiang W
Quoted 4 lines

+#define BUG_ON(cond) do { \
+???????if (!(cond))????\
+???????????????BUG(); \

Isn't that backwards?

I can't understand what you mean.

Regards,
Xiang W

Quoted 3 lines

Andreas.

#4 lore raw

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

In reply to Xiang W

----- Original Message -----

Quoted 32 lines

From: "Xiang W" <[email protected]>
To: "opensbi" <[email protected]>
Cc: "atish patra" <[email protected]>, "anup patel" <[email protected]>, "Xiang W" <[email protected]>
Sent: Wednesday, September 15, 2021 5:03:29 PM
Subject: [PATCH V2 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 +3 -0
index e24ba5f..e75a279 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__); \

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))	\
+		BUG(); \
+} while (0)
+

If the BUG_ON has a similar semantics as BUG_ON in Linux, it should be:
+ if (cond) \
+ BUG(); \

Quoted 9 lines
include/sbi/sbi_console.h +0 -1
 #endif
--

2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Dong

author
#5 lore raw

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

? 2021-09-15???? 20:39 +0800??????

Quoted 46 lines

----- Original Message -----

Quoted 43 lines

From: "Xiang W" <[email protected]>
To: "opensbi" <[email protected]>
Cc: "atish patra" <[email protected]>, "anup patel" < anup.patel at wdc.com>, "Xiang W" <[email protected]>
Sent: Wednesday, September 15, 2021 5:03:29 PM
Subject: [PATCH V2 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 +1 -0
index e24ba5f..e75a279 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 */

include/sbi/sbi_console.h +9 -0
@@ -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))????\
Quoted 9 lines

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

Quoted 3 lines
include/sbi/sbi_console.h +3 -0
+???????????????BUG(); \
+} while (0)
+

If the BUG_ON has a similar semantics as BUG_ON in Linux, it should be:
? + if (cond)???\
? +?????????????BUG(); \

I want to implement BUG_ON like assert. If the meaning of linux is like this, I think it can be used as a reference

Regards?
Xiang W

Quoted 13 lines
Quoted 9 lines

#endif
--
2.30.2

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Dong

#6 lore raw

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

Quoted 57 lines · Xiang W

? 2021-09-15???? 20:39 +0800??????

Quoted 46 lines

----- Original Message -----

Quoted 43 lines

From: "Xiang W" <[email protected]>
To: "opensbi" <[email protected]>
Cc: "atish patra" <[email protected]>, "anup patel" < anup.patel at wdc.com>, "Xiang W" <[email protected]>
Sent: Wednesday, September 15, 2021 5:03:29 PM
Subject: [PATCH V2 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 +10 -0
index e24ba5f..e75a279 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))    \
Quoted 9 lines

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

Quoted 3 lines
include/sbi/sbi_console.h +3 -0
+               BUG(); \
+} while (0)
+

If the BUG_ON has a similar semantics as BUG_ON in Linux, it should be:
+ if (cond) \
+ BUG(); \

Quoted 3 lines

I want to implement BUG_ON like assert. If the meaning of linux is like this, I think it can be used as a reference

Maybe it should be named ASSERT or SBI_ASSERT then? It does not seem like your other patch even uses this however.

In my opinion, BUG() and BUG_ON() are confusing names to begin with; they do not obviously describe their semantics. If you insist on using these names, their behaviour should match Linux.

Cheers,
Mitchell

Quoted 22 lines

Regards?
Xiang W

Quoted 5 lines
Quoted 5 lines

#endif
--
2.30.2

Quoted 8 lines
Quoted 4 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Dong

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#7 lore raw

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

Quoted 63 lines · Mitchell Horne
Quoted 57 lines · Xiang W

? 2021-09-15???? 20:39 +0800??????

Quoted 46 lines

----- Original Message -----

Quoted 43 lines

From: "Xiang W" <[email protected]>
To: "opensbi" <[email protected]>
Cc: "atish patra" <[email protected]>, "anup patel" < anup.patel at wdc.com>, "Xiang W" <[email protected]>
Sent: Wednesday, September 15, 2021 5:03:29 PM
Subject: [PATCH V2 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 +10 -0
index e24ba5f..e75a279 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))    \
Quoted 9 lines

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

Quoted 3 lines
include/sbi/sbi_console.h +3 -0
+               BUG(); \
+} while (0)
+

If the BUG_ON has a similar semantics as BUG_ON in Linux, it should be:
+ if (cond) \
+ BUG(); \

Quoted 3 lines

I want to implement BUG_ON like assert. If the meaning of linux is like this, I think it can be used as a reference

Quoted 6 lines

Maybe it should be named ASSERT or SBI_ASSERT then? It does not seem like your other patch even uses this however.

In my opinion, BUG() and BUG_ON() are confusing names to begin with; they do not obviously describe their semantics. If you insist on using these names, their behaviour should match Linux.

Also BUG is a bit of a lazy interface; line numbers change over time, really you should be using some kind of panic function that takes a format string with a meaningful message. That also gives the user some hope of knowing what went wrong and maybe working around or fixing it. And why not reuse sbi_hart_hang rather than inlining another copy of it?

Jess

author
#8 lore raw

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

? 2021-09-15???? 15:49 +0100?Jessica Clarke???

Quoted 80 lines · Mitchell Horne
Quoted 73 lines · Xiang W
Quoted 62 lines

? 2021-09-15???? 20:39 +0800??????

Quoted 51 lines

----- Original Message -----

Quoted 48 lines

From: "Xiang W" <[email protected]>
To: "opensbi" <[email protected]>
Cc: "atish patra" <[email protected]>, "anup patel" < anup.patel at wdc.com>, "Xiang W" <[email protected]>
Sent: Wednesday, September 15, 2021 5:03:29 PM
Subject: [PATCH V2 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 +1 -0
index e24ba5f..e75a279 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 */

include/sbi/sbi_console.h +10 -0
@@ -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(); \
Quoted 9 lines

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

Quoted 2 lines
include/sbi/sbi_console.h +2 -0
+} while (0)
+

If the BUG_ON has a similar semantics as BUG_ON in Linux, it should
be:
?+ if (cond)?? \
?+???????????? BUG(); \

Quoted 4 lines

I want to implement BUG_ON like assert. If the meaning of linux is like
this, I think it can be used as a reference

Maybe it should be named ASSERT or SBI_ASSERT then? It does not seem
like your other patch even uses this however.

Quoted 5 lines

In my opinion, BUG() and BUG_ON() are confusing names to begin with;
they do not obviously describe their semantics. If you insist on using
these names, their behaviour should match Linux.

Quoted 8 lines

Also BUG is a bit of a lazy interface; line numbers change over time, really you should be using some kind of panic function that takes a format string with a meaningful message. That also gives the user some
hope of knowing what went wrong and maybe working around or fixing it.
And why not reuse sbi_hart_hang rather than inlining another copy of it?

Thanks for the suggestion

Regards?
Xiang W

Quoted 2 lines

Jess