diff --git a/TESTS/mbedmicro-rtos-mbed/heap_and_stack/main.cpp b/TESTS/mbedmicro-rtos-mbed/heap_and_stack/main.cpp index 4d6c8f86749..c0f6f4cf351 100644 --- a/TESTS/mbedmicro-rtos-mbed/heap_and_stack/main.cpp +++ b/TESTS/mbedmicro-rtos-mbed/heap_and_stack/main.cpp @@ -48,6 +48,12 @@ extern uint32_t mbed_heap_size; extern uint32_t mbed_stack_isr_start; extern uint32_t mbed_stack_isr_size; +#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP) +extern uint32_t __mbed_sbrk_start_0; +extern uint32_t __mbed_krbs_start_0; +unsigned char *mbed_heap_start_0 = (unsigned char *) &__mbed_sbrk_start_0;; +uint32_t mbed_heap_size_0 = (uint32_t) &__mbed_krbs_start_0 - (uint32_t) &__mbed_sbrk_start_0; +#endif struct linked_list { linked_list *next; @@ -121,7 +127,11 @@ static void allocate_and_fill_heap(linked_list *&head) break; } bool result = rangeinrange((uint32_t) temp, sizeof(linked_list), mbed_heap_start, mbed_heap_size); - +#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP) + if (false == result) { + result = rangeinrange((uint32_t) temp, sizeof(linked_list), (uint32_t)mbed_heap_start_0, mbed_heap_size_0); + } +#endif TEST_ASSERT_TRUE_MESSAGE(result, "Memory allocation out of range"); // Init @@ -169,7 +179,11 @@ void test_heap_in_range(void) TEST_ASSERT_NOT_NULL(initial_heap); bool result = inrange((uint32_t) initial_heap, mbed_heap_start, mbed_heap_size); - +#if defined(TOOLCHAIN_GCC_ARM) && defined(MBED_SPLIT_HEAP) + if (false == result) { + result = inrange((uint32_t) initial_heap, (uint32_t)mbed_heap_start_0, mbed_heap_size_0); + } +#endif TEST_ASSERT_TRUE_MESSAGE(result, "Heap in wrong location"); free(initial_heap); } diff --git a/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp b/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp index 004aa4a9223..ef126bda8a3 100644 --- a/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp +++ b/TESTS/mbedmicro-rtos-mbed/malloc/main.cpp @@ -107,6 +107,44 @@ void test_multithread_allocation(void) TEST_ASSERT_FALSE(thread_alloc_failure); } +/** Test for multiple heap alloc and free calls */ +#define ALLOC_ARRAY_SIZE 100 +#define ALLOC_LOOP 20 +#define SIZE_INCREMENTS 1023 +#define SIZE_MODULO 31 + +void test_alloc_and_free(void) +{ + void *array[ALLOC_ARRAY_SIZE]; + void *data = NULL; + long total_allocated = 0; + int count = 0; + int size = SIZE_INCREMENTS; + int loop = ALLOC_LOOP; + while (loop) { + data = malloc(size); + if (NULL != data) { + array[count++] = data; + memset((void *)data, 0xdeadbeef, size); + total_allocated += size; + size += SIZE_INCREMENTS; + if (size > 10000) { + size %= SIZE_MODULO; + } + } else { + for (int i = 0; i < count; i++) { + free(array[i]); + array[i] = NULL; + } + loop--; + printf("Total size dynamically allocated: %luB\n", total_allocated); + total_allocated = 0; + count = 0; + continue; + } + } +} + /** Test for large heap allocation Given a heap of size mbed_heap_size @@ -167,7 +205,8 @@ Case cases[] = { Case("Test 0 size allocation", test_zero_allocation), Case("Test NULL pointer free", test_null_free), Case("Test multithreaded allocations", test_multithread_allocation), - Case("Test large allocation", test_big_allocation) + Case("Test large allocation", test_big_allocation), + Case("Test multiple alloc and free calls", test_alloc_and_free) }; utest::v1::status_t greentea_test_setup(const size_t number_of_cases) diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index 36836469940..ad235cfeb60 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -1240,6 +1240,46 @@ extern "C" WEAK void __cxa_pure_virtual(void) // SP. This make it compatible with RTX RTOS thread stacks. #if defined(TOOLCHAIN_GCC_ARM) +#if defined(MBED_SPLIT_HEAP) + +// Default RAM memory used for heap +extern uint32_t __mbed_sbrk_start; +extern uint32_t __mbed_krbs_start; +/* Additional RAM memory used for heap - please note this + * address should be lower address then the previous default address + */ +extern uint32_t __mbed_sbrk_start_0; +extern uint32_t __mbed_krbs_start_0; + +extern "C" WEAK caddr_t _sbrk(int incr) +{ + static uint32_t heap = (uint32_t) &__mbed_sbrk_start_0; + static bool once = true; + uint32_t prev_heap = heap; + uint32_t new_heap = heap + incr; + + /** + * If the new address is outside the first region, start allocating from the second region. + * Jump to second region is done just once, and `static bool once` is used to keep track of that. + */ + if (once && (new_heap > (uint32_t) &__mbed_krbs_start_0)) { + once = false; + prev_heap = (uint32_t) &__mbed_sbrk_start; + new_heap = prev_heap + incr; + } else if (new_heap > (uint32_t) &__mbed_krbs_start) { + /** + * If the new address is outside the second region, return out-of-memory. + */ + errno = ENOMEM; + return (caddr_t) - 1; + } + + heap = new_heap; + return (caddr_t) prev_heap; +} + +#else + extern "C" uint32_t __end__; extern "C" uint32_t __HeapLimit; @@ -1264,6 +1304,7 @@ extern "C" WEAK caddr_t _sbrk(int incr) return (caddr_t) prev_heap; } #endif +#endif #if defined(TOOLCHAIN_GCC_ARM) extern "C" void _exit(int return_code) diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/device/TOOLCHAIN_GCC_ARM/STM32L471XX.ld b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/device/TOOLCHAIN_GCC_ARM/STM32L471XX.ld index b98d61b39c1..bf4e69ae82f 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/device/TOOLCHAIN_GCC_ARM/STM32L471XX.ld +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_MTS_DRAGONFLY_L471QG/device/TOOLCHAIN_GCC_ARM/STM32L471XX.ld @@ -14,7 +14,7 @@ STACK_SIZE = MBED_BOOT_STACK_SIZE; /* Linker script to configure memory regions. */ MEMORY -{ +{ FLASH (rx) : ORIGIN = MBED_APP_START, LENGTH = MBED_APP_SIZE SRAM2 (rwx) : ORIGIN = 0x10000188, LENGTH = 32k - 0x188 SRAM1 (rwx) : ORIGIN = 0x20000000, LENGTH = 96k @@ -24,7 +24,7 @@ MEMORY * with other linker script that defines memory regions FLASH and RAM. * It references following symbols, which must be defined in code: * Reset_Handler : Entry of reset handler - * + * * It defines following symbols, which code can use without definition: * __exidx_start * __exidx_end @@ -92,6 +92,35 @@ SECTIONS __etext = .; _sidata = .; + /* .stack section doesn't contains any symbols. It is only + * used for linker to reserve space for the isr stack section + * WARNING: .stack should come immediately after the last secure memory + * section. This provides stack overflow detection. */ + .stack (NOLOAD): + { + __StackLimit = .; + *(.stack*); + . += STACK_SIZE - (. - __StackLimit); + } > SRAM2 + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ADDR(.stack) + SIZEOF(.stack); + _estack = __StackTop; + __StackLimit = ADDR(.stack); + PROVIDE(__stack = __StackTop); + + /* Place holder for additional heap */ + .heap_0 (COPY): + { + __mbed_sbrk_start_0 = .; + . += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .); + __mbed_krbs_start_0 = .; + } > SRAM2 + + /* Check if heap exceeds SRAM2 */ + ASSERT(__mbed_krbs_start_0 <= (ORIGIN(SRAM2)+LENGTH(SRAM2)), "Heap is too big for SRAM2") + .data : AT (__etext) { __data_start__ = .; @@ -99,21 +128,20 @@ SECTIONS *(vtable) *(.data*) - . = ALIGN(4); + . = ALIGN(8); /* preinit data */ PROVIDE_HIDDEN (__preinit_array_start = .); KEEP(*(.preinit_array)) PROVIDE_HIDDEN (__preinit_array_end = .); - . = ALIGN(4); + . = ALIGN(8); /* init data */ PROVIDE_HIDDEN (__init_array_start = .); KEEP(*(SORT(.init_array.*))) KEEP(*(.init_array)) PROVIDE_HIDDEN (__init_array_end = .); - - . = ALIGN(4); + . = ALIGN(8); /* finit data */ PROVIDE_HIDDEN (__fini_array_start = .); KEEP(*(SORT(.fini_array.*))) @@ -121,52 +149,43 @@ SECTIONS PROVIDE_HIDDEN (__fini_array_end = .); KEEP(*(.jcr*)) - . = ALIGN(4); + . = ALIGN(8); /* All data end */ __data_end__ = .; _edata = .; } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__data_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), ".data is too big for SRAM1") + .bss : { - . = ALIGN(4); + . = ALIGN(8); __bss_start__ = .; _sbss = .; *(.bss*) *(COMMON) - . = ALIGN(4); + . = ALIGN(8); __bss_end__ = .; _ebss = .; } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__bss_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "BSS is too big for SRAM1") + + /* Placeholder for default single heap */ .heap (COPY): { __end__ = .; end = __end__; + __mbed_sbrk_start = .; *(.heap*) . += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .); + __mbed_krbs_start = .; __HeapLimit = .; } > SRAM1 - PROVIDE(__heap_size = SIZEOF(.heap)); - PROVIDE(__mbed_sbrk_start = ADDR(.heap)); - PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap)); - /* Check if data + heap exceeds RAM1 limit */ - ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow") - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > SRAM2 - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2); - _estack = __StackTop; - __StackLimit = __StackTop - STACK_SIZE; - PROVIDE(__stack = __StackTop); - /* Check if stack exceeds RAM2 limit */ - ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow") -} + /* Check if heap exceeds SRAM1 */ + ASSERT(__HeapLimit <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "Heap is too big for SRAM1") +} \ No newline at end of file diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L443xC/device/TOOLCHAIN_GCC_ARM/STM32L443XX.ld b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L443xC/device/TOOLCHAIN_GCC_ARM/STM32L443XX.ld index c57cb94fdda..c4899c932af 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L443xC/device/TOOLCHAIN_GCC_ARM/STM32L443XX.ld +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L443xC/device/TOOLCHAIN_GCC_ARM/STM32L443XX.ld @@ -93,6 +93,35 @@ SECTIONS __etext = .; _sidata = .; + /* .stack section doesn't contains any symbols. It is only + * used for linker to reserve space for the isr stack section + * WARNING: .stack should come immediately after the last secure memory + * section. This provides stack overflow detection. */ + .stack (NOLOAD): + { + __StackLimit = .; + *(.stack*); + . += STACK_SIZE - (. - __StackLimit); + } > SRAM2 + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ADDR(.stack) + SIZEOF(.stack); + _estack = __StackTop; + __StackLimit = ADDR(.stack); + PROVIDE(__stack = __StackTop); + + /* Place holder for additional heap */ + .heap_0 (COPY): + { + __mbed_sbrk_start_0 = .; + . += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .); + __mbed_krbs_start_0 = .; + } > SRAM2 + + /* Check if heap exceeds SRAM2 */ + ASSERT(__mbed_krbs_start_0 <= (ORIGIN(SRAM2)+LENGTH(SRAM2)), "Heap is too big for SRAM2") + .data : AT (__etext) { __data_start__ = .; @@ -129,6 +158,9 @@ SECTIONS } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__data_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), ".data is too big for SRAM1") + .bss : { . = ALIGN(8); @@ -141,30 +173,21 @@ SECTIONS _ebss = .; } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__bss_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "BSS is too big for SRAM1") + + /* Placeholder for default single heap */ .heap (COPY): { __end__ = .; end = __end__; + __mbed_sbrk_start = .; *(.heap*) - . = ORIGIN(SRAM1) + LENGTH(SRAM1) - STACK_SIZE; + . += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .); + __mbed_krbs_start = .; __HeapLimit = .; } > SRAM1 - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > SRAM1 - - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(SRAM1) + LENGTH(SRAM1); - _estack = __StackTop; - __StackLimit = __StackTop - STACK_SIZE; - PROVIDE(__stack = __StackTop); - - /* Check if data + heap + stack exceeds RAM limit */ - ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") + /* Check if heap exceeds SRAM1 */ + ASSERT(__HeapLimit <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "Heap is too big for SRAM1") } diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/device/TOOLCHAIN_GCC_ARM/STM32L475XX.ld b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/device/TOOLCHAIN_GCC_ARM/STM32L475XX.ld index 2ffa1c3a085..e7bccd34acb 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/device/TOOLCHAIN_GCC_ARM/STM32L475XX.ld +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/device/TOOLCHAIN_GCC_ARM/STM32L475XX.ld @@ -6,17 +6,17 @@ #define MBED_APP_SIZE 1024k #endif +M_CRASH_DATA_RAM_SIZE = 0x100; + #if !defined(MBED_BOOT_STACK_SIZE) #define MBED_BOOT_STACK_SIZE 0x400 #endif STACK_SIZE = MBED_BOOT_STACK_SIZE; -M_CRASH_DATA_RAM_SIZE = 0x100; - /* Linker script to configure memory regions. */ MEMORY -{ +{ FLASH (rx) : ORIGIN = MBED_APP_START, LENGTH = MBED_APP_SIZE SRAM2 (rwx) : ORIGIN = 0x10000188, LENGTH = 32k - 0x188 SRAM1 (rwx) : ORIGIN = 0x20000000, LENGTH = 96k @@ -26,7 +26,7 @@ MEMORY * with other linker script that defines memory regions FLASH and RAM. * It references following symbols, which must be defined in code: * Reset_Handler : Entry of reset handler - * + * * It defines following symbols, which code can use without definition: * __exidx_start * __exidx_end @@ -93,7 +93,7 @@ SECTIONS __etext = .; _sidata = .; - + .crash_data_ram : { . = ALIGN(8); @@ -104,7 +104,36 @@ SECTIONS . += M_CRASH_DATA_RAM_SIZE; . = ALIGN(8); __CRASH_DATA_RAM_END__ = .; /* Define a global symbol at data end */ - } > SRAM1 + } > SRAM2 + + /* .stack section doesn't contains any symbols. It is only + * used for linker to reserve space for the isr stack section + * WARNING: .stack should come immediately after the last secure memory + * section. This provides stack overflow detection. */ + .stack (NOLOAD): + { + __StackLimit = .; + *(.stack*); + . += STACK_SIZE - (. - __StackLimit); + } > SRAM2 + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ADDR(.stack) + SIZEOF(.stack); + _estack = __StackTop; + __StackLimit = ADDR(.stack); + PROVIDE(__stack = __StackTop); + + /* Place holder for additional heap */ + .heap_0 (COPY): + { + __mbed_sbrk_start_0 = .; + . += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .); + __mbed_krbs_start_0 = .; + } > SRAM2 + + /* Check if heap exceeds SRAM2 */ + ASSERT(__mbed_krbs_start_0 <= (ORIGIN(SRAM2)+LENGTH(SRAM2)), "Heap is too big for SRAM2") .data : AT (__etext) { @@ -142,6 +171,9 @@ SECTIONS } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__data_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), ".data is too big for SRAM1") + .bss : { . = ALIGN(8); @@ -152,37 +184,23 @@ SECTIONS . = ALIGN(8); __bss_end__ = .; _ebss = .; - } > SRAM2 + } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__bss_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "BSS is too big for SRAM1") + + /* Placeholder for default single heap */ .heap (COPY): { __end__ = .; end = __end__; + __mbed_sbrk_start = .; *(.heap*) . += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .); + __mbed_krbs_start = .; __HeapLimit = .; } > SRAM1 - PROVIDE(__heap_size = SIZEOF(.heap)); - PROVIDE(__mbed_sbrk_start = ADDR(.heap)); - PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap)); - /* Check if data + heap exceeds RAM1 limit */ - ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow") - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > SRAM2 - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2); - _estack = __StackTop; - __StackLimit = __StackTop - STACK_SIZE; - PROVIDE(__stack = __StackTop); - /* Check if stack exceeds RAM2 limit */ - ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow") - /* Check if bss exceeds __StackLimit */ - ASSERT(__bss_end__ <= __StackLimit, "BSS is too big for RAM2") + /* Check if heap exceeds SRAM1 */ + ASSERT(__HeapLimit <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "Heap is too big for SRAM1") } diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/device/TOOLCHAIN_GCC_ARM/STM32L476XX.ld b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/device/TOOLCHAIN_GCC_ARM/STM32L476XX.ld index 71f28a5c25a..00ace2cd76b 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/device/TOOLCHAIN_GCC_ARM/STM32L476XX.ld +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/device/TOOLCHAIN_GCC_ARM/STM32L476XX.ld @@ -104,7 +104,36 @@ SECTIONS . += M_CRASH_DATA_RAM_SIZE; . = ALIGN(8); __CRASH_DATA_RAM_END__ = .; /* Define a global symbol at data end */ - } > SRAM1 + } > SRAM2 + + /* .stack section doesn't contains any symbols. It is only + * used for linker to reserve space for the isr stack section + * WARNING: .stack should come immediately after the last secure memory + * section. This provides stack overflow detection. */ + .stack (NOLOAD): + { + __StackLimit = .; + *(.stack*); + . += STACK_SIZE - (. - __StackLimit); + } > SRAM2 + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ADDR(.stack) + SIZEOF(.stack); + _estack = __StackTop; + __StackLimit = ADDR(.stack); + PROVIDE(__stack = __StackTop); + + /* Place holder for additional heap */ + .heap_0 (COPY): + { + __mbed_sbrk_start_0 = .; + . += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .); + __mbed_krbs_start_0 = .; + } > SRAM2 + + /* Check if heap exceeds SRAM2 */ + ASSERT(__mbed_krbs_start_0 <= (ORIGIN(SRAM2)+LENGTH(SRAM2)), "Heap is too big for SRAM2") .data : AT (__etext) { @@ -126,7 +155,6 @@ SECTIONS KEEP(*(.init_array)) PROVIDE_HIDDEN (__init_array_end = .); - . = ALIGN(8); /* finit data */ PROVIDE_HIDDEN (__fini_array_start = .); @@ -142,6 +170,9 @@ SECTIONS } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__data_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), ".data is too big for SRAM1") + .bss : { . = ALIGN(8); @@ -154,34 +185,21 @@ SECTIONS _ebss = .; } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__bss_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "BSS is too big for SRAM1") + + /* Placeholder for default single heap */ .heap (COPY): { __end__ = .; end = __end__; + __mbed_sbrk_start = .; *(.heap*) . += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .); + __mbed_krbs_start = .; __HeapLimit = .; } > SRAM1 - PROVIDE(__heap_size = SIZEOF(.heap)); - PROVIDE(__mbed_sbrk_start = ADDR(.heap)); - PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap)); - /* Check if data + heap exceeds RAM1 limit */ - ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow") - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > SRAM2 - - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2); - _estack = __StackTop; - __StackLimit = __StackTop - STACK_SIZE; - PROVIDE(__stack = __StackTop); - /* Check if stack exceeds RAM2 limit */ - ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow") + /* Check if heap exceeds SRAM1 */ + ASSERT(__HeapLimit <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "Heap is too big for SRAM1") } diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/device/TOOLCHAIN_GCC_ARM/STM32L486XX.ld b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/device/TOOLCHAIN_GCC_ARM/STM32L486XX.ld index 71f28a5c25a..00ace2cd76b 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/device/TOOLCHAIN_GCC_ARM/STM32L486XX.ld +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/device/TOOLCHAIN_GCC_ARM/STM32L486XX.ld @@ -104,7 +104,36 @@ SECTIONS . += M_CRASH_DATA_RAM_SIZE; . = ALIGN(8); __CRASH_DATA_RAM_END__ = .; /* Define a global symbol at data end */ - } > SRAM1 + } > SRAM2 + + /* .stack section doesn't contains any symbols. It is only + * used for linker to reserve space for the isr stack section + * WARNING: .stack should come immediately after the last secure memory + * section. This provides stack overflow detection. */ + .stack (NOLOAD): + { + __StackLimit = .; + *(.stack*); + . += STACK_SIZE - (. - __StackLimit); + } > SRAM2 + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ADDR(.stack) + SIZEOF(.stack); + _estack = __StackTop; + __StackLimit = ADDR(.stack); + PROVIDE(__stack = __StackTop); + + /* Place holder for additional heap */ + .heap_0 (COPY): + { + __mbed_sbrk_start_0 = .; + . += (ORIGIN(SRAM2) + LENGTH(SRAM2) - .); + __mbed_krbs_start_0 = .; + } > SRAM2 + + /* Check if heap exceeds SRAM2 */ + ASSERT(__mbed_krbs_start_0 <= (ORIGIN(SRAM2)+LENGTH(SRAM2)), "Heap is too big for SRAM2") .data : AT (__etext) { @@ -126,7 +155,6 @@ SECTIONS KEEP(*(.init_array)) PROVIDE_HIDDEN (__init_array_end = .); - . = ALIGN(8); /* finit data */ PROVIDE_HIDDEN (__fini_array_start = .); @@ -142,6 +170,9 @@ SECTIONS } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__data_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), ".data is too big for SRAM1") + .bss : { . = ALIGN(8); @@ -154,34 +185,21 @@ SECTIONS _ebss = .; } > SRAM1 + /* Check if bss exceeds SRAM1 */ + ASSERT(__bss_end__ <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "BSS is too big for SRAM1") + + /* Placeholder for default single heap */ .heap (COPY): { __end__ = .; end = __end__; + __mbed_sbrk_start = .; *(.heap*) . += (ORIGIN(SRAM1) + LENGTH(SRAM1) - .); + __mbed_krbs_start = .; __HeapLimit = .; } > SRAM1 - PROVIDE(__heap_size = SIZEOF(.heap)); - PROVIDE(__mbed_sbrk_start = ADDR(.heap)); - PROVIDE(__mbed_krbs_start = ADDR(.heap) + SIZEOF(.heap)); - /* Check if data + heap exceeds RAM1 limit */ - ASSERT((ORIGIN(SRAM1)+LENGTH(SRAM1)) >= __HeapLimit, "SRAM1 overflow") - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > SRAM2 - - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(SRAM2) + LENGTH(SRAM2); - _estack = __StackTop; - __StackLimit = __StackTop - STACK_SIZE; - PROVIDE(__stack = __StackTop); - /* Check if stack exceeds RAM2 limit */ - ASSERT((ORIGIN(SRAM2)+LENGTH(SRAM2)) >= __StackLimit, "SRAM2 overflow") + /* Check if heap exceeds SRAM1 */ + ASSERT(__HeapLimit <= (ORIGIN(SRAM1)+LENGTH(SRAM1)), "Heap is too big for SRAM1") } diff --git a/targets/targets.json b/targets/targets.json index bb9ba496184..9f86a470345 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -3465,7 +3465,7 @@ "MPU" ], "device_has_remove": ["LPTICKER"], - "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT"], + "macros_add": ["MBEDTLS_CONFIG_HW_SUPPORT", "MBED_SPLIT_HEAP"], "device_name": "STM32L443RC", "detect_code": ["0458"], "bootloader_supported": true @@ -3490,7 +3490,8 @@ "detect_code": ["0765"], "macros_add": [ "MBED_TICKLESS", - "USBHOST_OTHER" + "USBHOST_OTHER", + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -3552,7 +3553,8 @@ "macros_add": [ "MBED_TICKLESS", "USBHOST_OTHER", - "MBEDTLS_CONFIG_HW_SUPPORT" + "MBEDTLS_CONFIG_HW_SUPPORT", + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -3587,7 +3589,8 @@ "detect_code": ["0460"], "macros_add": [ "MBEDTLS_CONFIG_HW_SUPPORT", - "WISE_1570" + "WISE_1570", + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -4147,7 +4150,8 @@ "detect_code": ["0764"], "macros_add": [ "MBED_TICKLESS", - "USBHOST_OTHER" + "USBHOST_OTHER", + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -4178,7 +4182,7 @@ } }, "detect_code": ["0468"], - "macros_add": ["USBHOST_OTHER", "TWO_RAM_REGIONS"], + "macros_add": ["USBHOST_OTHER", "MBED_SPLIT_HEAP"], "device_has_add": [ "ANALOGOUT", "CAN", @@ -4211,7 +4215,8 @@ "detect_code": ["0820"], "macros_add": [ "MBED_TICKLESS", - "USBHOST_OTHER" + "USBHOST_OTHER", + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -4226,7 +4231,7 @@ "device_name": "STM32L476VG", "bootloader_supported": true }, - "RHOMBIO_L476DMW1K": { + "RHOMBIO_L476DMW1K": { "components_add": ["FLASHIAP"], "inherits": ["FAMILY_STM32"], "core": "Cortex-M4F", @@ -4247,7 +4252,7 @@ "macros_add": [ "MBED_TICKLESS", "USBHOST_OTHER", - "TWO_RAM_REGIONS" + "MBED_SPLIT_HEAP" ], "device_has_add": [ "ANALOGOUT", @@ -4356,6 +4361,9 @@ "FLASH", "MPU" ], + "macros_add": [ + "MBED_SPLIT_HEAP" + ], "release_versions": ["2", "5"], "device_name": "STM32L471QG", "bootloader_supported": true