Skip to contentSkip to conversation

U-Boot-V2:ARM: Introduce capability to have different stack/malloc area

PATCH New Menon 2 participants 6 messages May 21, 2008 – Jun 4, 2008 Arm (32-bit)

lore
Share & tools
lore

Menon

#0 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability to have different stack/malloc area

The default setup of ARM memory allocation is as follows:
----
Stack Area
----
Heap Area
---
_u_boot_start
Rest of U-Boot..

This is an excellent strategy to catch stack overflow and memory buffer overflow issues. However, in conditions where:
a) U-Boot is automatically loaded by h/w in the default location without a writable memory area above it, this will crash,
b) Multiple non-contiguous memory areas available in system (e.g. SRAM and SDRAM), but the area where U-Boot is loaded is restricted in size and cannot fit the required heap and stack areas. In these cases, we need to have ability to move the Heap and stack to the area of our choice. This patch introduces CONFIG_MALLOC_START for machine configurations which require this. This would need to be enabled only if MACH_CAN_MODIFY_MALLOC_START is defined by the machine configurations that desire it. I have clubbed both heap and stack area to still catch some of those overflow issues.

---
 arch/arm/Kconfig         |   22 ++++++++++++++++++++++
 arch/arm/cpu/start-arm.S |    9 +++++++++
 arch/arm/lib/arm.c       |    5 +++++
 3 files changed, 36 insertions(+)
arch/arm/lib/arm.c +5 -0
Index: u-boot-v2.git/arch/arm/lib/arm.c
===================================================================
@@ -6,8 +6,13 @@
 
 int arm_mem_malloc_init(void)
 {
+#ifndef CONFIG_MALLOC_START
 	mem_malloc_init((void *)(_u_boot_start - CFG_MALLOC_LEN),
 			(void *)_u_boot_start);
+#else
+	mem_malloc_init((void *)(CONFIG_MALLOC_START - CFG_MALLOC_LEN),
+			(void *)CONFIG_MALLOC_START);
+#endif
 	return 0;
 }
 
arch/arm/Kconfig +22 -0
Index: u-boot-v2.git/arch/arm/Kconfig
===================================================================
@@ -168,6 +168,28 @@
 	  If you want to start a 2.6 kernel and use an
 	  initrd image say y here.
 
+config MACH_CAN_MODIFY_MALLOC_START
+	bool
+
+config MALLOC_START_MODIFY
+	bool "Change Malloc Address location from default"
+	default n
+	depends on MACH_CAN_MODIFY_MALLOC_START
+	help
+	  Say Y here if you meanto put malloc and stack elsewhere.
+	  The default is to put Malloc and stack just above the
+	  interrupt vectors(_start). It is usually desired to keep it here
+	  as we can catch stack overflow and corruption issues easily.
+	  USE THIS OPTION WITH CAUTION
+
+config MALLOC_START
+	hex
+	prompt "Provide Alternate Malloc Start address"
+	depends on MALLOC_START_MODIFY
+	help
+	  Provide the alternate malloc start address. Remember that the area
+	  that will be used will be (this address) to (this address - CFG_MALLOC_LEN - CONFIG_STACKSIZE)
+
 endmenu
 
 source common/Kconfig
arch/arm/cpu/start-arm.S +9 -0
Index: u-boot-v2.git/arch/arm/cpu/start-arm.S
===================================================================
@@ -90,11 +90,20 @@
  * FIXME
  *************************************************************************/
 
+#ifndef CONFIG_MALLOC_START
 _MALLOC_START:
 	.word _start - CFG_MALLOC_LEN
 
 _STACK_START:
 	.word _start - CFG_MALLOC_LEN - CONFIG_STACKSIZE
+#else
+_MALLOC_START:
+	.word CONFIG_MALLOC_START - CFG_MALLOC_LEN
+
+_STACK_START:
+	.word CONFIG_MALLOC_START - CFG_MALLOC_LEN - CONFIG_STACKSIZE
+#endif
+
 
 /*
  * These are defined in the board-specific linker script.
#1 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability to have different stack/malloc area

Quoted 9 lines · Nishanth

The default setup of ARM memory allocation is as follows:
----
Stack Area
----
Heap Area
---
_u_boot_start
Rest of U-Boot..

Quoted 5 lines

This is an excellent strategy to catch stack overflow and memory buffer overflow issues. However, in conditions where:
a) U-Boot is automatically loaded by h/w in the default location without a writable memory area above it, this will crash,
b) Multiple non-contiguous memory areas available in system (e.g. SRAM and SDRAM), but the area where U-Boot is loaded is restricted in size and cannot fit the required heap and stack areas. In these cases, we need to have ability to move the Heap and stack to the area of our choice. This patch introduces CONFIG_MALLOC_START for machine configurations which require this. This would need to be enabled only if MACH_CAN_MODIFY_MALLOC_START is defined by the machine configurations that desire it. I have clubbed both heap and stack area to still catch some of those overflow issues.

This patch rises an issue we currently have in U-Boot-v2: We have as many stack/memory strategies as we have architectures :(

Instead of modifying this for one architecture I would prefer something like this (currently no implementation, only kconfig).
Would this solve your issues?

regards,
Sascha

common/Kconfig +53 -0
index 79a3684..d764482 100644
@@ -19,13 +19,66 @@ menu "General Settings              "
 config BOARDINFO
 	string
 
+menu "memory layout                 "
+
 config TEXT_BASE
+	depends on HAVE_CONFIGURABLE_TEXT_BASE
 	prompt "TEXT_BASE"
 	hex
 	default ARCH_TEXT_BASE
 	help
 	  The Address U-Boot gets linked at.
 
+choice
+	prompt "select stack base policy"
+	depends on HAVE_CONFIGURABLE_STACK
+	default STACK_BASE_FIXED
+
+config STACK_BASE_DEFAULT
+	bool "place stack before malloc space"
+
+config STACK_BASE_FIXED
+	bool "manually assign a stack base"
+
+endchoice
+
+config STACK_BASE
+	depends on STACK_BASE_FIXED
+	hex
+	prompt "STACK_BASE"
+
+config STACK_SIZE
+	hex
+	depends on HAVE_CONFIGURABLE_STACK
+	default 0x8000
+	prompt "Stack size"
+
+choice
+	prompt "select stack base policy"
+	depends on HAVE_CONFIGURABLE_MALLOC
+
+config MALLOC_BASE_DEFAULT
+	bool
+	prompt "place stack before U-Boot"
+
+config MALLOC_BASE_FIXED
+	bool
+	prompt "manually assign a malloc base"
+
+endchoice
+
+config MALLOC_BASE
+	depends on MALLOC_BASE_FIXED
+	hex
+	prompt "MALLOC_BASE"
+
+config MALLOC_SIZE
+	hex
+	depends on HAVE_CONFIGURABLE_MALLOC
+	default 0x400000
+	prompt "malloc area size"
+endmenu
+
 config BROKEN
 	bool
 	prompt "Prompt for broken or incomplete code"
Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry
-----------------------------------------------------------
Kontakt-Informationen finden Sie im Header dieser Mail oder
auf der Webseite -> http://www.pengutronix.de/impressum/ <-
#2 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability to havedifferent stack/malloc area

Sascha,

Quoted 8 lines

-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 3:08 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 05/17] U-Boot-V2:ARM: Introduce capability to havedifferent stack/malloc area

Quoted 6 lines

This patch rises an issue we currently have in U-Boot-v2: We have as many stack/memory strategies as we have architectures :(

Instead of modifying this for one architecture I would prefer something like this (currently no implementation, only kconfig).
Would this solve your issues?

Yes, :). Couple of minor comments though..

common/Kconfig menu "General Settings " · line 22

Quoted 9 lines
common/Kconfig +1 -0
index 79a3684..d764482 100644
@@ -19,13 +19,66 @@ menu "General Settings              "
 config BOARDINFO
 	string
 
+menu "memory layout                 "

Curious: Why have the extra space? I see this in other places too.. it kind of messes up the defconfig with trailing spaces :(.

common/Kconfig menu "General Settings " · line 24

Quoted 2 lines
common/Kconfig +2 -0
+choice
+	prompt "select stack base policy"

s/stack/heap?

common/Kconfig menu "General Settings " · line 62

Quoted 5 lines
common/Kconfig +5 -0
+	depends on HAVE_CONFIGURABLE_MALLOC
+
+config MALLOC_BASE_DEFAULT
+	bool
+	prompt "place stack before U-Boot"

s/stack/heap?

common/Kconfig menu "General Settings " · line 81

Quoted 9 lines
common/Kconfig +9 -0
+
+config MALLOC_BASE_FIXED
+	bool
+	prompt "manually assign a malloc base"
+
+endchoice
+
+endmenu
+

Ack to the changes. This should be good for me.

Regards,
Nishanth Menon

#3 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability to havedifferent stack/malloc area

Quoted 26 lines · Nishanth

Sascha,

Quoted 8 lines

-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 3:08 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 05/17] U-Boot-V2:ARM: Introduce capability to havedifferent stack/malloc area

Quoted 6 lines

This patch rises an issue we currently have in U-Boot-v2: We have as many stack/memory strategies as we have architectures :(

Instead of modifying this for one architecture I would prefer something like this (currently no implementation, only kconfig).
Would this solve your issues?

Yes, :). Couple of minor comments though..

Quoted 8 lines
common/Kconfig +0 -0
index 79a3684..d764482 100644
@@ -19,13 +19,66 @@ menu "General Settings              "
 config BOARDINFO
 	string
 
Quoted 2 lines

common/Kconfig menu "General Settings " · line 22

Quoted 1 line
common/Kconfig +1 -0
+menu "memory layout                 "

Curious: Why have the extra space? I see this in other places too.. it kind of messes up the defconfig with trailing spaces :(.

It's just to align the menu arrows nicely under each other in make menuconfig.

Quoted 10 lines

common/Kconfig menu "General Settings " · line 24

Quoted 3 lines
common/Kconfig +2 -0
+choice
+	prompt "select stack base policy"

s/stack/heap?

common/Kconfig menu "General Settings " · line 62

Quoted 5 lines
common/Kconfig +5 -0
+	depends on HAVE_CONFIGURABLE_MALLOC
+
+config MALLOC_BASE_DEFAULT
+	bool
+	prompt "place stack before U-Boot"

s/stack/heap?

No, this is the processor stack...

Quoted 5 lines

common/Kconfig menu "General Settings " · line 67

Quoted 5 lines
common/Kconfig +5 -0
+
+config MALLOC_BASE_FIXED
+	bool
+	prompt "manually assign a malloc base"
+

...while this is the malloc heap.

Quoted 9 lines

common/Kconfig menu "General Settings " · line 81

Quoted 4 lines
common/Kconfig +4 -0
+endchoice
+
+endmenu
+

Ack to the changes. This should be good for me.

Regards,
Nishanth Menon

Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry
-----------------------------------------------------------
Kontakt-Informationen finden Sie im Header dieser Mail oder
auf der Webseite -> http://www.pengutronix.de/impressum/ <-
#4 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability tohavedifferent stack/malloc area

Sascha,

Quoted 11 lines

-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 10:24 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 05/17] U-Boot-V2:ARM: Introduce capability tohavedifferent stack/malloc area

Quoted 3 lines

Instead of modifying this for one architecture I would prefer something like this (currently no implementation, only kconfig).
Would this solve your issues?

Modification only done for ARM.

---
 arch/arm/cpu/start-arm.S |   27 +++++++++++++++++++-
 arch/arm/lib/arm.c       |   16 ++++++++++--
 common/Kconfig           |   61 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 4 deletions(-)
arch/arm/lib/arm.c 2008-06-03 20:56:46.000000000 -0500 renamed from arch/arm/lib/arm.c 2008-06-03 20:56:27.000000000 -0500 +14 -2
Index: u-boot.v2/arch/arm/lib/arm.c
===================================================================
@@ -4,10 +4,22 @@
 #include <asm/u-boot-arm.h>
 #include <reloc.h>
 
+#ifdef CONFIG_MALLOC_BASE
+#define ARM_MALLOC_START CONFIG_MALLOC_BASE
+#else
+#define ARM_MALLOC_START _u_boot_start
+#endif
+
+#ifdef CONFIG_MALLOC_SIZE
+#define ARM_MALLOC_SIZE CONFIG_MALLOC_SIZE
+#else
+#define ARM_MALLOC_SIZE CFG_MALLOC_LEN
+#endif
+
 int arm_mem_malloc_init(void)
 {
-       mem_malloc_init((void *)(_u_boot_start - CFG_MALLOC_LEN),
-                       (void *)_u_boot_start);
+       mem_malloc_init((void *)(ARM_MALLOC_START - ARM_MALLOC_SIZE),
+                       (void *)ARM_MALLOC_START);
        return 0;
 }
 
arch/arm/cpu/start-arm.S 2008-06-03 21:08:04.000000000 -0500 renamed from arch/arm/cpu/start-arm.S 2008-06-03 20:56:40.000000000 -0500 +25 -2
Index: u-boot.v2/arch/arm/cpu/start-arm.S
===================================================================
@@ -90,11 +90,34 @@
  * FIXME
  *************************************************************************/
 
+#ifdef CONFIG_MALLOC_BASE
+#define ARM_MALLOC_START CONFIG_MALLOC_BASE
+#else
+#define ARM_MALLOC_START _start
+#endif
+
+#ifdef CONFIG_MALLOC_SIZE
+#define ARM_MALLOC_SIZE CONFIG_MALLOC_SIZE
+#else
+#define ARM_MALLOC_SIZE CFG_MALLOC_LEN
+#endif
 _MALLOC_START:
-       .word _start - CFG_MALLOC_LEN
+       .word ARM_MALLOC_START - ARM_MALLOC_SIZE
 
+#ifdef CONFIG_STACK_BASE
+#define ARM_STACK_START CONFIG_STACK_BASE
+#else
+#define ARM_STACK_START (ARM_MALLOC_START - ARM_MALLOC_SIZE)
+#endif
+
+#ifdef CONFIG_STACK_SIZE
+#define ARM_STACK_SIZE CONFIG_STACK_SIZE
+#else
+#define ARM_STACK_SIZE CFG_STACK_LEN
+#endif
 _STACK_START:
-       .word _start - CFG_MALLOC_LEN - CONFIG_STACKSIZE
+       .word ARM_STACK_START - CONFIG_STACKSIZE
+
 
 /*
  * These are defined in the board-specific linker script.
common/Kconfig 2008-06-03 21:08:37.000000000 -0500 renamed from common/Kconfig 2008-06-03 20:56:40.000000000 -0500 +61 -0

74 lines not on this page · raw message

#5 lore raw

PATCH 5/17 U-Boot-V2:ARM: Introduce capability tohavedifferent stack/malloc area

Quoted 9 lines · Nishanth

Sascha,

Quoted 8 lines

-----Original Message-----
From: Sascha Hauer [mailto:s.hauer at pengutronix.de]
Sent: Tuesday, June 03, 2008 10:24 AM
To: Menon, Nishanth
Cc: u-boot-users at lists.sourceforge.net; Laurent Desnogues; dirk.behme at googlemail.com; philip.balister at gmail.com; Gopinath, Thara; Kamat, Nishant; Syed Mohammed, Khasim
Subject: Re: [Patch 05/17] U-Boot-V2:ARM: Introduce capability tohavedifferent stack/malloc area

Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Instead of modifying this for one architecture I would prefer something like this (currently no implementation, only kconfig).
Would this solve your issues?

Modification only done for ARM.

I checked in a similar version of the patch, should work for you.

Sascha

Signature · 4 lines
Pengutronix e.K. - Linux Solutions for Science and Industry
-----------------------------------------------------------
Kontakt-Informationen finden Sie im Header dieser Mail oder
auf der Webseite -> http://www.pengutronix.de/impressum/ <-