Skip to content

Commit a883150

Browse files
author
Deepika
committed
Update memory model for stack and heap memory
Memory model for RTOS and No RTOS was initially single stack and heap, only few targets implemented 2-region RAM model. 2-region RAM model is applied for all toolchains and targets, more information can be found in docs\design-documents\platform\memory-model\ram_memory_model.md
1 parent 8ba28c0 commit a883150

File tree

5 files changed

+55
-80
lines changed

5 files changed

+55
-80
lines changed

platform/mbed_retarget.cpp

Lines changed: 33 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -904,76 +904,59 @@ extern "C" long PREFIX(_flen)(FILEHANDLE fh)
904904
return size;
905905
}
906906

907-
#if defined(TARGET_NUVOTON) || defined(TARGET_RZ_A1XX)
908-
extern char Image$$ARM_LIB_HEAP$$ZI$$Base[];
909-
extern char Image$$ARM_LIB_STACK$$ZI$$Base[];
910-
extern char Image$$ARM_LIB_HEAP$$ZI$$Limit[];
911-
#define HEAP_BASE (Image$$ARM_LIB_HEAP$$ZI$$Base)
912-
#define HEAP_LIMIT (Image$$ARM_LIB_HEAP$$ZI$$Limit)
913-
#define STACK_BASE (Image$$ARM_LIB_STACK$$ZI$$Base)
907+
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
908+
__asm(".global __use_two_region_memory\n\t");
909+
__asm(".global __use_no_semihosting\n\t");
914910
#else
915-
extern char Image$$RW_IRAM1$$ZI$$Limit[];
916-
extern char Image$$ARM_LIB_STACK$$ZI$$Base[];
917-
#define HEAP_BASE (Image$$RW_IRAM1$$ZI$$Limit)
918-
#define HEAP_LIMIT (Image$$ARM_LIB_STACK$$ZI$$Base)
919-
#define STACK_BASE (Image$$ARM_LIB_STACK$$ZI$$Base)
911+
#pragma import(__use_two_region_memory)
920912
#endif
921913

914+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
915+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
916+
917+
extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
918+
919+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
920+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
921+
922+
#if !defined(ISR_STACK_SIZE)
923+
#define ISR_STACK_START Image$$ARM_LIB_STACK$$ZI$$Base
924+
#define ISR_STACK_SIZE Image$$ARM_LIB_STACK$$ZI$$Length
925+
#endif
926+
927+
#if !defined(HEAP_START)
928+
#if defined(Image$$ARM_LIB_HEAP$$ZI$$Base) && defined(Image$$ARM_LIB_HEAP$$ZI$$Length)
929+
#define HEAP_START Image$$ARM_LIB_HEAP$$ZI$$Base
930+
#define HEAP_SIZE Image$$ARM_LIB_HEAP$$ZI$$Length
931+
#else
932+
// Heap here is considered starting after ZI ends to Stack start
933+
#define HEAP_START Image$$RW_IRAM1$$ZI$$Limit
934+
#define HEAP_SIZE ((uint32_t)(ISR_STACK_START - HEAP_START))
935+
#endif
936+
#endif
937+
938+
#define HEAP_LIMIT (HEAP_START + HEAP_SIZE)
939+
922940
extern __value_in_regs struct __initial_stackheap _mbed_user_setup_stackheap(uint32_t R0, uint32_t R1, uint32_t R2, uint32_t R3)
923941
{
924942
struct __initial_stackheap r;
925-
r.heap_base = (uint32_t)HEAP_BASE;
943+
r.heap_base = (uint32_t)HEAP_START;
926944
r.heap_limit = (uint32_t)HEAP_LIMIT;
945+
927946
return r;
928947
}
929948

930-
#ifndef MBED_CONF_RTOS_PRESENT
931-
932-
/* The single region memory model would check stack collision at run time, verifying that
933-
* the heap pointer is underneath the stack pointer. With two-region memory model/RTOS-less or
934-
* multiple threads(stacks)/RTOS, the check gets meaningless and we must disable it. */
935-
#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
936-
__asm(".global __use_two_region_memory\n\t");
937-
__asm(".global __use_no_semihosting\n\t");
938-
#else
939-
#pragma import(__use_two_region_memory)
940-
#endif
941-
942-
/* Fix __user_setup_stackheap and ARM_LIB_STACK/ARM_LIB_HEAP cannot co-exist in RTOS-less build
943-
*
944-
* According AN241 (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0241b/index.html),
945-
* __rt_entry has the following call sequence:
946-
* 1. _platform_pre_stackheap_init
947-
* 2. __user_setup_stackheap or setup the Stack Pointer (SP) by another method
948-
* 3. _platform_post_stackheap_init
949-
* 4. __rt_lib_init
950-
* 5. _platform_post_lib_init
951-
* 6. main()
952-
* 7. exit()
953-
*
954-
* Per our check, when __user_setup_stackheap and ARM_LIB_STACK/ARM_LIB_HEAP co-exist, neither
955-
* does __user_setup_stackheap get called and nor is ARM_LIB_HEAP used to get heap base/limit,
956-
* which are required to pass to __rt_lib_init later. To fix the issue, by subclass'ing
957-
* __rt_lib_init, heap base/limit are replaced with Image$$ARM_LIB_HEAP$$ZI$$Base/Limit if
958-
* ARM_LIB_HEAP region is defined in scatter file.
959-
*
960-
* The overriding __rt_lib_init is needed only for rtos-less code. For rtos code, __rt_entry is
961-
* overridden and the overriding __rt_lib_init here gets meaningless.
962-
*/
963949
extern "C" extern __value_in_regs struct __argc_argv $Super$$__rt_lib_init(unsigned heapbase, unsigned heaptop);
964950

965951
extern "C" __value_in_regs struct __argc_argv $Sub$$__rt_lib_init(unsigned heapbase, unsigned heaptop)
966952
{
967-
return $Super$$__rt_lib_init((unsigned)HEAP_BASE, (unsigned)HEAP_LIMIT);
953+
return $Super$$__rt_lib_init((unsigned)HEAP_START, (unsigned)HEAP_LIMIT);
968954
}
969955

970-
#endif
971-
972956
extern "C" __value_in_regs struct __initial_stackheap __user_setup_stackheap(uint32_t R0, uint32_t R1, uint32_t R2, uint32_t R3)
973957
{
974958
return _mbed_user_setup_stackheap(R0, R1, R2, R3);
975959
}
976-
977960
#endif
978961

979962

rtos/TARGET_CORTEX/TOOLCHAIN_ARM_STD/mbed_boot_arm_std.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,28 @@
2727
__value_in_regs struct __argc_argv __rt_lib_init(unsigned heapbase, unsigned heaptop);
2828
void _platform_post_stackheap_init(void);
2929

30+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
31+
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
32+
33+
extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
34+
35+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
36+
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
37+
3038
#if !defined(ISR_STACK_SIZE)
31-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
32-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
33-
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
34-
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
39+
#define ISR_STACK_START Image$$ARM_LIB_STACK$$ZI$$Base
40+
#define ISR_STACK_SIZE Image$$ARM_LIB_STACK$$ZI$$Length
3541
#endif
3642

3743
#if !defined(HEAP_START)
38-
/* Defined by linker script */
39-
extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
40-
#define HEAP_START ((unsigned char*)Image$$RW_IRAM1$$ZI$$Limit)
41-
#define HEAP_SIZE ((uint32_t)((uint32_t)ISR_STACK_START - (uint32_t)HEAP_START))
44+
#if defined(Image$$ARM_LIB_HEAP$$ZI$$Base) && defined(Image$$ARM_LIB_HEAP$$ZI$$Length)
45+
#define HEAP_START IImage$$ARM_LIB_HEAP$$ZI$$Base
46+
#define HEAP_SIZE Image$$ARM_LIB_HEAP$$ZI$$Length
47+
#else
48+
// Heap here is considered starting after ZI ends to Stack start
49+
#define HEAP_START Image$$RW_IRAM1$$ZI$$Limit
50+
#define HEAP_SIZE ((uint32_t)(ISR_STACK_START - HEAP_START))
51+
#endif
4252
#endif
4353

4454
/*
@@ -58,7 +68,7 @@ extern uint32_t Image$$RW_IRAM1$$ZI$$Limit[];
5868
*/
5969
void __rt_entry(void)
6070
{
61-
unsigned char *free_start = HEAP_START;
71+
unsigned char *free_start = (unsigned char *)HEAP_START;
6272
uint32_t free_size = HEAP_SIZE;
6373

6474
#ifdef ISR_STACK_START

targets/TARGET_NUVOTON/mbed_rtx.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,8 @@
2121

2222
#if defined(TARGET_NUVOTON)
2323

24-
#if defined(__CC_ARM) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
25-
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
26-
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
27-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
28-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
29-
#define HEAP_START ((unsigned char*) Image$$ARM_LIB_HEAP$$ZI$$Base)
30-
#define HEAP_SIZE ((uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Length)
31-
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
32-
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
24+
#if defined(__CC_ARM) || defined(__ARMCC_VERSION)
25+
/* No region declarations needed */
3326
#elif defined(__GNUC__)
3427
extern uint32_t __StackTop;
3528
extern uint32_t __StackLimit;

targets/TARGET_NXP/mbed_rtx.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,8 @@
100100
#if defined(__CC_ARM) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))
101101
extern uint32_t Image$$RW_IRAM1$$ZI$$Base[];
102102
extern uint32_t Image$$RW_IRAM1$$ZI$$Length[];
103-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
104-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
105103
#define HEAP_START ((unsigned char*) Image$$RW_IRAM1$$ZI$$Base)
106104
#define HEAP_SIZE ((uint32_t) Image$$RW_IRAM1$$ZI$$Length)
107-
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
108-
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
109105
#elif defined(__GNUC__)
110106
extern uint32_t __StackTop[];
111107
extern uint32_t __StackLimit[];

targets/TARGET_RDA/mbed_rtx.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@
3030
#endif
3131

3232
#if defined(__CC_ARM)
33-
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Base[];
34-
extern uint32_t Image$$ARM_LIB_HEAP$$ZI$$Length[];
35-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
36-
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Length[];
37-
#define HEAP_START ((unsigned char*) Image$$ARM_LIB_HEAP$$ZI$$Base)
38-
#define HEAP_SIZE ((uint32_t) Image$$ARM_LIB_HEAP$$ZI$$Length)
39-
#define ISR_STACK_START ((unsigned char*)Image$$ARM_LIB_STACK$$ZI$$Base)
40-
#define ISR_STACK_SIZE ((uint32_t)Image$$ARM_LIB_STACK$$ZI$$Length)
33+
/* No region declarations needed */
4134
#elif defined(__GNUC__)
4235
extern uint32_t __StackTop[];
4336
extern uint32_t __StackLimit[];

0 commit comments

Comments
 (0)