Skip to contentSkip to conversation

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

PATCH Superseded Jonas Karlman 2 participants 8 messages Aug 7 – Aug 9

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data
      [0xff0e08c0]                            stack pointer
    [0xff0e1000, 0xff0e4000)        12 KiB   TPL area
      [0xff0e1000, 0xff0e3800)      10 KiB    code

Update TPL_STACK to use the top of SRAM address and reduce MALLOC_F_LEN to 0x0 to avoid overflowing global data and stack into BootROM reserved region.

After those changes the memory layout looks something like:

  [0xff0e0000, 0xff0e4000)          16 KiB   SRAM
    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
    [0xff0e1000, 0xff0e4000)        12 KiB   TPL area
      [0xff0e1000, 0xff0e3800)      10 KiB    code
      [0xff0e3ed0, 0xff0e4000)     304 bytes  global data
      [0xff0e3ed0]                            stack pointer
        [0xff0e3800, 0xff0e3ed0)  1744 bytes  stack area

With these changes the TPL_MAX_SIZE can safely be extended to 11 KiB, leaving enough for the stack to grow down around 720 bytes before it starts to overlap into TPL.

And with all changes the memory layout looks something like:

  [0xff0e0000, 0xff0e4000)          16 KiB   SRAM
    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
    [0xff0e1000, 0xff0e4000)        12 KiB   TPL area
      [0xff0e1000, 0xff0e3c00)      11 KiB    code
      [0xff0e3ed0, 0xff0e4000)     304 bytes  global data
      [0xff0e3ed0]                            stack pointer
        [0xff0e3c00, 0xff0e3ed0)   720 bytes  stack area

---
The overflow/wrapping was runtime validated on a RK3326 ODROID-GO Super by printing out content of [0xff0e0000, 0xff0e5000] and comparing the initial 4 KiB with the 16-20 KiB and the data matched 1:1.

---
 arch/arm/mach-rockchip/px30/Kconfig | 4 ++--
 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/arm/mach-rockchip/px30/Kconfig +2 -2
index adba1b49a52b..cc0632cf7b2d 100644
@@ -81,10 +81,10 @@ config TPL_TEXT_BASE
 	default 0xff0e1000
 
 config TPL_STACK
-	default 0xff0e4fff
+	default 0xff0e4000
 
 config TPL_SYS_MALLOC_F_LEN
-	default 0x600
+	default 0x0
 
 config DEBUG_UART_CHANNEL
 	int "Mux channel to use for debug UART2/UART3"
common/spl/Kconfig.tpl +1 -1
index a535b61ecd35..cdb9cb188c7c 100644
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
 	default 0x2e000 if ROCKCHIP_RK3399
 	default 0x8000 if ROCKCHIP_RK3288 || ROCKCHIP_RV1126
 	default 0x7000 if ROCKCHIP_RK322X || ROCKCHIP_RK3328 || ROCKCHIP_RK3368
-	default 0x2800 if ROCKCHIP_PX30
+	default 0x2c00 if ROCKCHIP_PX30
 	default 0x0
 	help
 	  The maximum size (in bytes) of the TPL stage.
tools/rkcommon.c +1 -1
index b39777fc0607..872fc21c039d 100644
@@ -137,7 +137,7 @@ struct spl_info {
 };
 
 static struct spl_info spl_infos[] = {
-	{ "px30", "RK33", 0x2800, false, RK_HEADER_V1 },
+	{ "px30", "RK33", 0x2c00, false, RK_HEADER_V1 },
 	{ "rk3036", "RK30", 0x1000, false, RK_HEADER_V1 },
 	{ "rk3066", "RK30", 0x8000 - 0x800, true, RK_HEADER_V1 },
 	{ "rk3128", "RK31", 0x1800, false, RK_HEADER_V1 },
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Jonas,

Quoted 7 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Quoted 22 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 2 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

The rest checks out: crt0_64.S does bic sp, x0, #0xf then board_init_f_alloc_reserve(), so with 0xff0e4000 the GD ends up at rounddown(0xff0e4000 - sizeof(gd), 16) and nothing writes at or above 0xff0e4000, and px30-board-tpl.c uses no malloc, so a zero TPL_SYS_MALLOC_F_LEN is safe.

Regards,
Simon

#2 lore raw

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Regards,
Jonas

Quoted 11 lines

The rest checks out: crt0_64.S does bic sp, x0, #0xf then board_init_f_alloc_reserve(), so with 0xff0e4000 the GD ends up at rounddown(0xff0e4000 - sizeof(gd), 16) and nothing writes at or above 0xff0e4000, and px30-board-tpl.c uses no malloc, so a zero TPL_SYS_MALLOC_F_LEN is safe.

Regards,
Simon

#3 lore raw

Hi Jonas,

Quoted 52 lines · Jonas Karlman

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

Quoted 7 lines

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Well we have DRAM in there now. Actually now that Ilas' series is in, we could take a look at trimming and aligning things - some things are long which could be int, which matters on arm64.

I also did some work on struct board_f which is another way to reduce space, if we can find more things which are only needed in early board start-up. I suppose we could even expand that to include the board_init_r() calls.

Regards,
Simon

#4 lore raw

Hi Simon,

Quoted 58 lines · Simon Glass

Hi Jonas,

Quoted 55 lines · Jonas Karlman

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

Quoted 8 lines
Quoted 4 lines

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Well we have DRAM in there now. Actually now that Ilas' series is in, we could take a look at trimming and aligning things - some things are long which could be int, which matters on arm64.

The px30/rk3326 boards do use CONFIG_NR_DRAM_BANKS=1 or 2, so dram should at least be limited somewhat.

I think there are some DM related fields that could be excluded to save little bit more space. With below diff the GD_SIZE is down to 240 bytes, 16 for TPL_SYS_MALLOC_F and remaining 48 due to the DM related fields. Not sure this is safe, but the odroid-go2_defconfig could be built.

I do not think the global data size is really a big issue for this board, but rk3036 only have 4 KiB that need to fit SPL (2716b), BSS (0b), global data (160b) and stack. SPL image is down to 2716 bytes after upcoming TPL refactoring at [1] (tpl branch).

configs/odroid-go2_defconfig +1 -0
index dfdea66cd70c..c5fd6514010c 100644
@@ -118,3 +118,4 @@ CONFIG_USB_FUNCTION_ROCKUSB=y
 CONFIG_TPL_TINY_MEMSET=y
 CONFIG_LZO=y
 CONFIG_ERRNO_STR=y
+# CONFIG_TPL_SYS_MALLOC_F is not set
include/asm-generic/global_data.h +3 -1
index fd116b0cebf2..886c12900ddb 100644
@@ -174,10 +174,12 @@ struct global_data {
 	 * @arch: architecture-specific data
 	 */
 	struct arch_global_data arch;
+#if CONFIG_IS_ENABLED(DM)
 	/**
 	 * @dmtag_list: List of DM tags
 	 */
 	struct list_head dmtag_list;
+#endif
 	/**
 	 * @timebase_h: high 32 bits of timer
 	 */
@@ -228,7 +230,7 @@ struct global_data {
 	 */
 	long precon_buf_idx;
 #endif
-#ifdef CONFIG_DM
+#if CONFIG_IS_ENABLED(DM)
 	/**
 	 * @dm_root: root instance for Driver Model
 	 */

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/commit/8c8b98a29aa35880b93bbacce8daf68edb537eca

Regards,
Jonas

Quoted 8 lines

I also did some work on struct board_f which is another way to reduce space, if we can find more things which are only needed in early board start-up. I suppose we could even expand that to include the board_init_r() calls.

Regards,
Simon

#5 lore raw

Hi Jonas,

Quoted 111 lines · Jonas Karlman

Hi Simon,

Quoted 58 lines · Simon Glass

Hi Jonas,

Quoted 55 lines · Jonas Karlman

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

Quoted 8 lines
Quoted 4 lines

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Well we have DRAM in there now. Actually now that Ilas' series is in, we could take a look at trimming and aligning things - some things are long which could be int, which matters on arm64.

The px30/rk3326 boards do use CONFIG_NR_DRAM_BANKS=1 or 2, so dram should at least be limited somewhat.

I think there are some DM related fields that could be excluded to save little bit more space. With below diff the GD_SIZE is down to 240 bytes, 16 for TPL_SYS_MALLOC_F and remaining 48 due to the DM related fields. Not sure this is safe, but the odroid-go2_defconfig could be built.

I do not think the global data size is really a big issue for this board, but rk3036 only have 4 KiB that need to fit SPL (2716b), BSS (0b), global data (160b) and stack. SPL image is down to 2716 bytes after upcoming TPL refactoring at [1] (tpl branch).

configs/odroid-go2_defconfig +1 -0
index dfdea66cd70c..c5fd6514010c 100644
@@ -118,3 +118,4 @@ CONFIG_USB_FUNCTION_ROCKUSB=y
 CONFIG_TPL_TINY_MEMSET=y
 CONFIG_LZO=y
 CONFIG_ERRNO_STR=y
+# CONFIG_TPL_SYS_MALLOC_F is not set
include/asm-generic/global_data.h +2 -0
index fd116b0cebf2..886c12900ddb 100644
@@ -174,10 +174,12 @@ struct global_data {
         * @arch: architecture-specific data
         */
        struct arch_global_data arch;
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dmtag_list: List of DM tags
         */
        struct list_head dmtag_list;
+#endif
        /**
         * @timebase_h: high 32 bits of timer
         */

include/asm-generic/global_data.h struct global_data { · line 236

Quoted 8 lines
include/asm-generic/global_data.h +1 -1
@@ -228,7 +230,7 @@ struct global_data {
         */
        long precon_buf_idx;
 #endif
-#ifdef CONFIG_DM
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dm_root: root instance for Driver Model
         */

Yes please, that's a huge improvement!

Quoted 15 lines

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/commit/8c8b98a29aa35880b93bbacce8daf68edb537eca

Regards,
Jonas

Quoted 8 lines

I also did some work on struct board_f which is another way to reduce space, if we can find more things which are only needed in early board start-up. I suppose we could even expand that to include the board_init_r() calls.

Regards,
Simon

Regards,
Simon

#6 lore raw

Hi Simon,

Quoted 116 lines · Simon Glass

Hi Jonas,

Quoted 113 lines · Jonas Karlman

Hi Simon,

Quoted 58 lines · Simon Glass

Hi Jonas,

Quoted 55 lines · Jonas Karlman

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

Quoted 8 lines
Quoted 4 lines

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Well we have DRAM in there now. Actually now that Ilas' series is in, we could take a look at trimming and aligning things - some things are long which could be int, which matters on arm64.

The px30/rk3326 boards do use CONFIG_NR_DRAM_BANKS=1 or 2, so dram should at least be limited somewhat.

I think there are some DM related fields that could be excluded to save little bit more space. With below diff the GD_SIZE is down to 240 bytes, 16 for TPL_SYS_MALLOC_F and remaining 48 due to the DM related fields. Not sure this is safe, but the odroid-go2_defconfig could be built.

I do not think the global data size is really a big issue for this board, but rk3036 only have 4 KiB that need to fit SPL (2716b), BSS (0b), global data (160b) and stack. SPL image is down to 2716 bytes after upcoming TPL refactoring at [1] (tpl branch).

configs/odroid-go2_defconfig +1 -0
index dfdea66cd70c..c5fd6514010c 100644
@@ -118,3 +118,4 @@ CONFIG_USB_FUNCTION_ROCKUSB=y
 CONFIG_TPL_TINY_MEMSET=y
 CONFIG_LZO=y
 CONFIG_ERRNO_STR=y
+# CONFIG_TPL_SYS_MALLOC_F is not set
include/asm-generic/global_data.h +2 -0
index fd116b0cebf2..886c12900ddb 100644
@@ -174,10 +174,12 @@ struct global_data {
         * @arch: architecture-specific data
         */
        struct arch_global_data arch;
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dmtag_list: List of DM tags
         */
        struct list_head dmtag_list;
+#endif
        /**
         * @timebase_h: high 32 bits of timer
         */
@@ -228,7 +230,7 @@ struct global_data {
         */
        long precon_buf_idx;
Quoted 8 lines

include/asm-generic/global_data.h struct global_data { · line 236

Quoted 6 lines
include/asm-generic/global_data.h +1 -1
 #endif
-#ifdef CONFIG_DM
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dm_root: root instance for Driver Model
         */

Yes please, that's a huge improvement!

Will send out above struct global_data in a separate patch, CI seem to pass with above changes applied, see [2].

[2] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/pipelines/924

Regards,
Jonas

Quoted 19 lines
Quoted 16 lines

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/commit/8c8b98a29aa35880b93bbacce8daf68edb537eca

Regards,
Jonas

Quoted 8 lines

I also did some work on struct board_f which is another way to reduce space, if we can find more things which are only needed in early board start-up. I suppose we could even expand that to include the board_init_r() calls.

Regards,
Simon

Regards,
Simon

#7 lore raw

Hi Jonas,

Quoted 129 lines · Jonas Karlman

Hi Simon,

Quoted 116 lines · Simon Glass

Hi Jonas,

Quoted 113 lines · Jonas Karlman

Hi Simon,

Quoted 58 lines · Simon Glass

Hi Jonas,

Quoted 55 lines · Jonas Karlman

Hi Simon,

Quoted 5 lines · Simon Glass

Hi Jonas,

Quoted 2 lines · Jonas Karlman

rockchip: px30: Fix TPL_STACK overflow into BootROM reserved region

Quoted 8 lines
Quoted 5 lines

The PX30/RK3326 has 16 KiB SRAM at [0xff0e0000, 0xff0e4000), with the initial 4 KiB at [0xff0e0000, 0xff0e1000) reserved for BootROM at boot.

SRAM addressing only seem to use 14 bits, meaning that reading from 0xff0e4000+ wraps around and instad reads back data at 0xff0e0000+.

Typos: 'seem' -> 'seems', 'instad' -> 'instead' and 'possible looks' below -> 'possibly looks'.

Thanks, will update in a v2.

Quoted 21 lines
Quoted 20 lines

Using a TPL_STACK at 0xff0e4ff0 (16-bytes aligned) means TPL use BootROM reserved region for its global data, malloc area and runtime stack.

TPL on PX30/RK3326 does not have any use for the malloc area and the generated GD_SIZE is typically around 304 bytes.

Current memory layout possible looks something like:

[0xff0e0000, 0xff0e4000) 16 KiB SRAM

    [0xff0e0000, 0xff0e1000)         4 KiB   BROM area
      [0xff0e09f0, 0xff0e0ff0)    1536 bytes  malloc area (unused)
      [0xff0e08c0, 0xff0e09f0)     304 bytes  global data

[...]

arch/arm/mach-rockchip/px30/Kconfig | 4 ++--

 common/spl/Kconfig.tpl              | 2 +-
 tools/rkcommon.c                    | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
Quoted 10 lines

common/spl/Kconfig.tpl config TPL_MAX_SIZE · line 129

Quoted 4 lines
common/spl/Kconfig.tpl +1 -1
@@ -129,7 +129,7 @@ config TPL_MAX_SIZE
-     default 0x2800 if ROCKCHIP_PX30
+     default 0x2c00 if ROCKCHIP_PX30

Just to check, the layout shows [0xff0e3c00, 0xff0e3ed0) as pure stack, but BSS (e.g. dram_info in sdram_px30.c) sits after __image_copy_end and is not counted by the TPL_MAX_SIZE assert in u-boot-tpl-v8.lds, so with an image close to 11 KiB the real stack headroom is a bit less than 720 bytes.

You are correct, current BSS is 72 bytes, will update the commit message to mention that the 720 bytes must fit both BSS and the stack in a v2.

Quoted 8 lines
Quoted 4 lines

What is interesting is that global data is so large, especially when in this case nothing here is really using global data. We could save 16 bytes by fully disable CONFIG_TPL_SYS_MALLOC_F, but that was too intrusive to be part of this fix.

Well we have DRAM in there now. Actually now that Ilas' series is in, we could take a look at trimming and aligning things - some things are long which could be int, which matters on arm64.

The px30/rk3326 boards do use CONFIG_NR_DRAM_BANKS=1 or 2, so dram should at least be limited somewhat.

I think there are some DM related fields that could be excluded to save little bit more space. With below diff the GD_SIZE is down to 240 bytes, 16 for TPL_SYS_MALLOC_F and remaining 48 due to the DM related fields. Not sure this is safe, but the odroid-go2_defconfig could be built.

I do not think the global data size is really a big issue for this board, but rk3036 only have 4 KiB that need to fit SPL (2716b), BSS (0b), global data (160b) and stack. SPL image is down to 2716 bytes after upcoming TPL refactoring at [1] (tpl branch).

configs/odroid-go2_defconfig +1 -0
index dfdea66cd70c..c5fd6514010c 100644
@@ -118,3 +118,4 @@ CONFIG_USB_FUNCTION_ROCKUSB=y
 CONFIG_TPL_TINY_MEMSET=y
 CONFIG_LZO=y
 CONFIG_ERRNO_STR=y
+# CONFIG_TPL_SYS_MALLOC_F is not set
include/asm-generic/global_data.h +2 -0
index fd116b0cebf2..886c12900ddb 100644
@@ -174,10 +174,12 @@ struct global_data {
         * @arch: architecture-specific data
         */
        struct arch_global_data arch;
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dmtag_list: List of DM tags
         */
        struct list_head dmtag_list;
+#endif
        /**
         * @timebase_h: high 32 bits of timer
         */
@@ -228,7 +230,7 @@ struct global_data {
         */
        long precon_buf_idx;
Quoted 8 lines

include/asm-generic/global_data.h struct global_data { · line 236

Quoted 6 lines
include/asm-generic/global_data.h +1 -1
 #endif
-#ifdef CONFIG_DM
+#if CONFIG_IS_ENABLED(DM)
        /**
         * @dm_root: root instance for Driver Model
         */

Yes please, that's a huge improvement!

Quoted 2 lines

Will send out above struct global_data in a separate patch, CI seem to pass with above changes applied, see [2].

OK good. Ilias may have some comment too.

Regards,
Simon

Quoted 26 lines

[2] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/pipelines/924

Regards,
Jonas

Quoted 19 lines
Quoted 16 lines

[1] https://git.u-boot-project.org/u-boot/contributors/kwiboo/u-boot/-/commit/8c8b98a29aa35880b93bbacce8daf68edb537eca

Regards,
Jonas

Quoted 8 lines

I also did some work on struct board_f which is another way to reduce space, if we can find more things which are only needed in early board start-up. I suppose we could even expand that to include the board_init_r() calls.

Regards,
Simon

Regards,
Simon