Skip to contentSkip to conversation

riscv: Kconfig: Add SPL_ZERO_MEM_BEFORE_USE

PATCH Superseded Shengyu Qu 2 participants 7 messages Aug 8, 2023 – Aug 9, 2023 1 message not in the archive RISC-V

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Shengyu Qu

author
#1 lore raw

PATCH v3 1/3 riscv: Kconfig: Add SPL_ZERO_MEM_BEFORE_USE

Add a Kconfig item to allow SPL to clear stack/GD/malloc area before using them.

---
 arch/riscv/Kconfig | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/Kconfig +8 -0
index 867cbcbe74..6771d8d919 100644
@@ -64,6 +64,14 @@ config SPL_SYS_DCACHE_OFF
 	help
 	  Do not enable data cache in SPL.
 
+config SPL_ZERO_MEM_BEFORE_USE
+	bool "Zero memory before use"
+	depends on SPL
+	default n
+	help
+	  Zero stack/GD/malloc area in SPL before using them, this is needed for
+	  Sifive core devices that uses L2 cache to store SPL.
+
 # board-specific options below
 source "board/AndesTech/ae350/Kconfig"
 source "board/emulation/qemu-riscv/Kconfig"
Signature · 1 line
2.41.0
author
#3 lore raw

PATCH v3 2/3 riscv: Add SPL_ZERO_MEM_BEFORE_USE implementation

In reply to a message not in the archive

Add the actual support code for SPL_ZERO_MEM_BEFORE_USE and remove existing Starfive JH7110's L2 LIM clean code, since existing code has following issues:
1. Each hart (in the middle of a function call) overwriting its own stack and other harts' stacks.
(data-race and data-corruption)
2. Lottery winner hart can be doing "board_init_f_init_reserve", while other harts are in the middle of zeroing L2 LIM. (data-race)

---
Changes since v2:
- Fix typo (ZERO_MEM_BEFORE_USE to SPL_ZERO_MEM_BEFORE_USE)

---
 arch/riscv/cpu/jh7110/spl.c | 25 -------------------------
 arch/riscv/cpu/start.S      | 12 ++++++++++++
 common/init/board_init.c    |  3 +++
 3 files changed, 15 insertions(+), 25 deletions(-)
arch/riscv/cpu/jh7110/spl.c +0 -25
index 72adcefa0e..4047b10efe 100644
@@ -13,7 +13,6 @@
 #include <init.h>
 
 #define CSR_U74_FEATURE_DISABLE	0x7c1
-#define L2_LIM_MEM_END	0x81FFFFFUL
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -59,9 +58,6 @@ int spl_soc_init(void)
 
 void harts_early_init(void)
 {
-	ulong *ptr;
-	u8 *tmp;
-	ulong len, remain;
 	/*
 	 * Feature Disable CSR
 	 *
@@ -70,25 +66,4 @@ void harts_early_init(void)
 	 */
 	if (CONFIG_IS_ENABLED(RISCV_MMODE))
 		csr_write(CSR_U74_FEATURE_DISABLE, 0);
-
-	/* clear L2 LIM  memory
-	 * set __bss_end to 0x81FFFFF region to zero
-	 * The L2 Cache Controller supports ECC. ECC is applied to SRAM.
-	 * If it is not cleared, the ECC part is invalid, and an ECC error
-	 * will be reported when reading data.
-	 */
-	ptr = (ulong *)&__bss_end;
-	len = L2_LIM_MEM_END - (ulong)&__bss_end;
-	remain = len % sizeof(ulong);
-	len /= sizeof(ulong);
-
-	while (len--)
-		*ptr++ = 0;
-
-	/* clear the remain bytes */
-	if (remain) {
-		tmp = (u8 *)ptr;
-		while (remain--)
-			*tmp++ = 0;
-	}
 }
arch/riscv/cpu/start.S +12 -0
index 59d58a5a57..930309d8d2 100644
@@ -111,6 +111,18 @@ call_board_init_f:
  * It's essential before any function call, otherwise, we get data-race.
  */
 
+/* clear stack if necessary */
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)
+clear_stack:
+	li	t1, 1
+	slli	t1, t1, CONFIG_STACK_SIZE_SHIFT
+	sub	t1, sp, t1
+clear_stack_loop:
+	SREG	zero, 0(t1)		/* t1 is always 16 byte aligned */
+	addi	t1, t1, REGBYTES
+	blt	t1, sp, clear_stack_loop
+#endif
+
 call_board_init_f_0:
 	/* find top of reserve space */
 #if CONFIG_IS_ENABLED(SMP)
common/init/board_init.c +3 -0
index 96ffb79a98..51d9ec9a13 100644
@@ -162,6 +162,9 @@ void board_init_f_init_reserve(ulong base)
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* go down one 'early malloc arena' */
 	gd->malloc_base = base;
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)
+	memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
+#endif
 #endif
 
 	if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH v3 2/3] riscv: Add SPL_ZERO_MEM_BEFORE_USE implementation

Hi Shengyu,

Quoted 74 lines · Shengyu Qu

Add the actual support code for SPL_ZERO_MEM_BEFORE_USE and remove existing Starfive JH7110's L2 LIM clean code, since existing code has following issues:
1. Each hart (in the middle of a function call) overwriting its own stack and other harts' stacks.
(data-race and data-corruption)
2. Lottery winner hart can be doing "board_init_f_init_reserve", while other harts are in the middle of zeroing L2 LIM. (data-race)

---
Changes since v2:
- Fix typo (ZERO_MEM_BEFORE_USE to SPL_ZERO_MEM_BEFORE_USE)

---
 arch/riscv/cpu/jh7110/spl.c | 25 -------------------------
 arch/riscv/cpu/start.S      | 12 ++++++++++++
 common/init/board_init.c    |  3 +++
 3 files changed, 15 insertions(+), 25 deletions(-)
arch/riscv/cpu/jh7110/spl.c +0 -25
index 72adcefa0e..4047b10efe 100644
@@ -13,7 +13,6 @@
 #include <init.h>
 
 #define CSR_U74_FEATURE_DISABLE	0x7c1
-#define L2_LIM_MEM_END	0x81FFFFFUL
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -59,9 +58,6 @@ int spl_soc_init(void)
 
 void harts_early_init(void)
 {
-	ulong *ptr;
-	u8 *tmp;
-	ulong len, remain;
 	/*
 	 * Feature Disable CSR
 	 *
@@ -70,25 +66,4 @@ void harts_early_init(void)
 	 */
 	if (CONFIG_IS_ENABLED(RISCV_MMODE))
 		csr_write(CSR_U74_FEATURE_DISABLE, 0);
-
-	/* clear L2 LIM  memory
-	 * set __bss_end to 0x81FFFFF region to zero
-	 * The L2 Cache Controller supports ECC. ECC is applied to SRAM.
-	 * If it is not cleared, the ECC part is invalid, and an ECC error
-	 * will be reported when reading data.
-	 */
-	ptr = (ulong *)&__bss_end;
-	len = L2_LIM_MEM_END - (ulong)&__bss_end;
-	remain = len % sizeof(ulong);
-	len /= sizeof(ulong);
-
-	while (len--)
-		*ptr++ = 0;
-
-	/* clear the remain bytes */
-	if (remain) {
-		tmp = (u8 *)ptr;
-		while (remain--)
-			*tmp++ = 0;
-	}
 }
arch/riscv/cpu/start.S +0 -0
index 59d58a5a57..930309d8d2 100644

arch/riscv/cpu/start.S call_board_init_f: · line 115

Quoted 5 lines
arch/riscv/cpu/start.S +2 -0
@@ -111,6 +111,18 @@ call_board_init_f:
  * It's essential before any function call, otherwise, we get data-race.
  */
 
+/* clear stack if necessary */
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)

I think what you have in v2 patch was the correct one.

Could you refer to include/linux/kconfig.h to check if this usage fits your expectation ?

Quoted 18 lines
arch/riscv/cpu/start.S +10 -0
+clear_stack:
+	li	t1, 1
+	slli	t1, t1, CONFIG_STACK_SIZE_SHIFT
+	sub	t1, sp, t1
+clear_stack_loop:
+	SREG	zero, 0(t1)		/* t1 is always 16 byte aligned */
+	addi	t1, t1, REGBYTES
+	blt	t1, sp, clear_stack_loop
+#endif
+
 call_board_init_f_0:
 	/* find top of reserve space */
 #if CONFIG_IS_ENABLED(SMP)
common/init/board_init.c +0 -0
index 96ffb79a98..51d9ec9a13 100644

common/init/board_init.c void board_init_f_init_reserve(ulong base) · line 165

Quoted 4 lines
common/init/board_init.c +1 -0
@@ -162,6 +162,9 @@ void board_init_f_init_reserve(ulong base)
 #if CONFIG_VAL(SYS_MALLOC_F_LEN)
 	/* go down one 'early malloc arena' */
 	gd->malloc_base = base;
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)

Ditto.

Best regards,
Leo

Quoted 8 lines
common/init/board_init.c +2 -0
+	memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
+#endif
 #endif
 
 	if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
Signature · 1 line
2.41.0
author
#5 lore raw

Re: [PATCH v3 2/3] riscv: Add SPL_ZERO_MEM_BEFORE_USE implementation

Hi Leo,

Seems you are right. I'll send v4 to fix this. Thank you.

Best regards,

Shengyu

Quoted 121 lines

Hi Shengyu,

Quoted 74 lines · Shengyu Qu

Add the actual support code for SPL_ZERO_MEM_BEFORE_USE and remove existing Starfive JH7110's L2 LIM clean code, since existing code has following issues:
1. Each hart (in the middle of a function call) overwriting its own stack and other harts' stacks.
(data-race and data-corruption)
2. Lottery winner hart can be doing "board_init_f_init_reserve", while other harts are in the middle of zeroing L2 LIM. (data-race)

---
Changes since v2:
- Fix typo (ZERO_MEM_BEFORE_USE to SPL_ZERO_MEM_BEFORE_USE)

---
  arch/riscv/cpu/jh7110/spl.c | 25 -------------------------
  arch/riscv/cpu/start.S      | 12 ++++++++++++
  common/init/board_init.c    |  3 +++
  3 files changed, 15 insertions(+), 25 deletions(-)
arch/riscv/cpu/jh7110/spl.c +0 -25
index 72adcefa0e..4047b10efe 100644
@@ -13,7 +13,6 @@
  #include <init.h>
 
  #define CSR_U74_FEATURE_DISABLE	0x7c1
-#define L2_LIM_MEM_END	0x81FFFFFUL
 
  DECLARE_GLOBAL_DATA_PTR;
 
@@ -59,9 +58,6 @@ int spl_soc_init(void)
 
  void harts_early_init(void)
  {
-	ulong *ptr;
-	u8 *tmp;
-	ulong len, remain;
  	/*
  	 * Feature Disable CSR
  	 *
@@ -70,25 +66,4 @@ void harts_early_init(void)
  	 */
  	if (CONFIG_IS_ENABLED(RISCV_MMODE))
  		csr_write(CSR_U74_FEATURE_DISABLE, 0);
-
-	/* clear L2 LIM  memory
-	 * set __bss_end to 0x81FFFFF region to zero
-	 * The L2 Cache Controller supports ECC. ECC is applied to SRAM.
-	 * If it is not cleared, the ECC part is invalid, and an ECC error
-	 * will be reported when reading data.
-	 */
-	ptr = (ulong *)&__bss_end;
-	len = L2_LIM_MEM_END - (ulong)&__bss_end;
-	remain = len % sizeof(ulong);
-	len /= sizeof(ulong);
-
-	while (len--)
-		*ptr++ = 0;
-
-	/* clear the remain bytes */
-	if (remain) {
-		tmp = (u8 *)ptr;
-		while (remain--)
-			*tmp++ = 0;
-	}
  }
arch/riscv/cpu/start.S +0 -0
index 59d58a5a57..930309d8d2 100644

arch/riscv/cpu/start.S call_board_init_f: · line 115

Quoted 5 lines
arch/riscv/cpu/start.S +2 -0
@@ -111,6 +111,18 @@ call_board_init_f:
   * It's essential before any function call, otherwise, we get data-race.
   */
 
+/* clear stack if necessary */
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)

I think what you have in v2 patch was the correct one.

Could you refer to include/linux/kconfig.h to check if this usage fits your expectation ?

Quoted 18 lines
arch/riscv/cpu/start.S +10 -0
+clear_stack:
+	li	t1, 1
+	slli	t1, t1, CONFIG_STACK_SIZE_SHIFT
+	sub	t1, sp, t1
+clear_stack_loop:
+	SREG	zero, 0(t1)		/* t1 is always 16 byte aligned */
+	addi	t1, t1, REGBYTES
+	blt	t1, sp, clear_stack_loop
+#endif
+
  call_board_init_f_0:
  	/* find top of reserve space */
  #if CONFIG_IS_ENABLED(SMP)
common/init/board_init.c +0 -0
index 96ffb79a98..51d9ec9a13 100644

common/init/board_init.c void board_init_f_init_reserve(ulong base) · line 165

Quoted 4 lines
common/init/board_init.c +1 -0
@@ -162,6 +162,9 @@ void board_init_f_init_reserve(ulong base)
  #if CONFIG_VAL(SYS_MALLOC_F_LEN)
  	/* go down one 'early malloc arena' */
  	gd->malloc_base = base;
+#if CONFIG_IS_ENABLED(SPL_ZERO_MEM_BEFORE_USE)

Ditto.

Best regards,
Leo

Quoted 8 lines
common/init/board_init.c +2 -0
+	memset((void *)base, '\0', CONFIG_VAL(SYS_MALLOC_F_LEN));
+#endif
  #endif
 
  	if (CONFIG_IS_ENABLED(SYS_REPORT_STACK_F_USAGE))
Signature · 1 line
2.41.0
author
#6 lore raw

PATCH v3 3/3 riscv: cpu: jh7110: Select SPL_ZERO_MEM_BEFORE_USE

In reply to a message not in the archive

Add Kconfig item for Starfive JH7110 to select SPL_ZERO_MEM_BEFORE_USE.

---
 arch/riscv/cpu/jh7110/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/riscv/cpu/jh7110/Kconfig +1 -0
index 4d9581165b..2e26d0731f 100644
@@ -13,6 +13,7 @@ config STARFIVE_JH7110
 	select SUPPORT_SPL
 	select SPL_RAM if SPL
 	select SPL_STARFIVE_DDR
+	select SPL_ZERO_MEM_BEFORE_USE
 	select PINCTRL_STARFIVE_JH7110
 	imply MMC
 	imply MMC_BROKEN_CD
Signature · 1 line
2.41.0