[PATCH] riscv: kgdb: Ensure that BUFMAX > NUMREGBYTES

Miquel Sabaté Solà posted 1 patch 2 weeks, 2 days ago
arch/riscv/include/asm/kgdb.h | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] riscv: kgdb: Ensure that BUFMAX > NUMREGBYTES
Posted by Miquel Sabaté Solà 2 weeks, 2 days ago
The current value of BUFMAX is similar as in other architectures, but as
per documentation on KGDB (see
'Documentation/process/debugging/kgdb.rst'), BUFMAX has to be larger
than NUMREGBYTES.

Some NUMREGBYTES architectures (e.g. powerpc or hexagon) actually define
BUFMAX in relation to NUMREGBYTES, and thus this condition is always
guaranteed. Since 2048 is a value that is generally accepted on all
architectures, and that is larger than the current value of NUMREGBYTES,
we can keep this value in arch/riscv, but we can at least add an
'static_assert' as an extra measure just in case NUMREGBYTES changes in
the future for some unforseen reason.

Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
---
 arch/riscv/include/asm/kgdb.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h
index cc11c4544cff..66ff46e434fa 100644
--- a/arch/riscv/include/asm/kgdb.h
+++ b/arch/riscv/include/asm/kgdb.h
@@ -3,14 +3,18 @@
 #ifndef __ASM_KGDB_H_
 #define __ASM_KGDB_H_
 
+#include <linux/build_bug.h>
+
 #ifdef __KERNEL__
 
 #define GDB_SIZEOF_REG sizeof(unsigned long)
 
-#define DBG_MAX_REG_NUM (36)
-#define NUMREGBYTES ((DBG_MAX_REG_NUM) * GDB_SIZEOF_REG)
+#define DBG_MAX_REG_NUM 36
+#define NUMREGBYTES (DBG_MAX_REG_NUM * GDB_SIZEOF_REG)
 #define CACHE_FLUSH_IS_SAFE     1
 #define BUFMAX                  2048
+static_assert(BUFMAX > NUMREGBYTES,
+	      "As per KGDB documentation, BUFMAX must be larger than NUMREGBYTES");
 #ifdef CONFIG_RISCV_ISA_C
 #define BREAK_INSTR_SIZE	2
 #else
@@ -97,6 +101,7 @@ extern unsigned long kgdb_compiled_break;
 #define DBG_REG_STATUS_OFF 33
 #define DBG_REG_BADADDR_OFF 34
 #define DBG_REG_CAUSE_OFF 35
+/* NOTE: increase DBG_MAX_REG_NUM if you add more values here. */
 
 extern const char riscv_gdb_stub_feature[64];
 
-- 
2.51.0

Re: [PATCH] riscv: kgdb: Ensure that BUFMAX > NUMREGBYTES
Posted by Miquel Sabaté Solà 3 days, 4 hours ago
Miquel Sabaté Solà @ 2025-09-15 16:32 +02:

> The current value of BUFMAX is similar as in other architectures, but as
> per documentation on KGDB (see
> 'Documentation/process/debugging/kgdb.rst'), BUFMAX has to be larger
> than NUMREGBYTES.
>
> Some NUMREGBYTES architectures (e.g. powerpc or hexagon) actually define
> BUFMAX in relation to NUMREGBYTES, and thus this condition is always
> guaranteed. Since 2048 is a value that is generally accepted on all
> architectures, and that is larger than the current value of NUMREGBYTES,
> we can keep this value in arch/riscv, but we can at least add an
> 'static_assert' as an extra measure just in case NUMREGBYTES changes in
> the future for some unforseen reason.
>
> Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
> ---
>  arch/riscv/include/asm/kgdb.h | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/arch/riscv/include/asm/kgdb.h b/arch/riscv/include/asm/kgdb.h
> index cc11c4544cff..66ff46e434fa 100644
> --- a/arch/riscv/include/asm/kgdb.h
> +++ b/arch/riscv/include/asm/kgdb.h
> @@ -3,14 +3,18 @@
>  #ifndef __ASM_KGDB_H_
>  #define __ASM_KGDB_H_
>
> +#include <linux/build_bug.h>
> +
>  #ifdef __KERNEL__
>
>  #define GDB_SIZEOF_REG sizeof(unsigned long)
>
> -#define DBG_MAX_REG_NUM (36)
> -#define NUMREGBYTES ((DBG_MAX_REG_NUM) * GDB_SIZEOF_REG)
> +#define DBG_MAX_REG_NUM 36
> +#define NUMREGBYTES (DBG_MAX_REG_NUM * GDB_SIZEOF_REG)
>  #define CACHE_FLUSH_IS_SAFE     1
>  #define BUFMAX                  2048
> +static_assert(BUFMAX > NUMREGBYTES,
> +	      "As per KGDB documentation, BUFMAX must be larger than NUMREGBYTES");
>  #ifdef CONFIG_RISCV_ISA_C
>  #define BREAK_INSTR_SIZE	2
>  #else
> @@ -97,6 +101,7 @@ extern unsigned long kgdb_compiled_break;
>  #define DBG_REG_STATUS_OFF 33
>  #define DBG_REG_BADADDR_OFF 34
>  #define DBG_REG_CAUSE_OFF 35
> +/* NOTE: increase DBG_MAX_REG_NUM if you add more values here. */
>
>  extern const char riscv_gdb_stub_feature[64];

Gentle ping :)