|
| 1 | + /* |
| 2 | + * Copyright (c) 2019-2019 ARM Limited. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Setup a fixed single stack/heap memory model, |
| 17 | + * between the top of the RW/ZI region and the stackpointer |
| 18 | + */ |
| 19 | + |
| 20 | +#ifdef __cplusplus |
| 21 | +extern "C" { |
| 22 | +#endif |
| 23 | + |
| 24 | +#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 25 | +#include <arm_compat.h> |
| 26 | +#endif |
| 27 | + |
| 28 | +#include <rt_misc.h> |
| 29 | +#include <stdint.h> |
| 30 | + |
| 31 | +extern char Image$$ARM_LIB_STACK$$ZI$$Limit[]; |
| 32 | +extern char Image$$ARM_LIB_HEAP$$Base[]; |
| 33 | +extern char Image$$ARM_LIB_HEAP$$ZI$$Limit[]; |
| 34 | +extern __value_in_regs struct __initial_stackheap _mbed_user_setup_stackheap(uint32_t R0, uint32_t R1, uint32_t R2, uint32_t R3) { |
| 35 | + |
| 36 | + struct __initial_stackheap r; |
| 37 | + r.heap_base = (uint32_t)Image$$ARM_LIB_HEAP$$Base; |
| 38 | + r.heap_limit = (uint32_t)Image$$ARM_LIB_HEAP$$ZI$$Limit; |
| 39 | + return r; |
| 40 | +} |
| 41 | + |
| 42 | +#if !defined(MBED_CONF_RTOS_PRESENT) || !MBED_CONF_RTOS_PRESENT |
| 43 | + |
| 44 | +/* The single region memory model would check stack collision at run time, verifying that |
| 45 | + * the heap pointer is underneath the stack pointer. With two-region memory model/RTOS-less or |
| 46 | + * multiple threads(stacks)/RTOS, the check gets meaningless and we must disable it. */ |
| 47 | +#if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 48 | +__asm(".global __use_two_region_memory\n\t"); |
| 49 | +__asm(".global __use_no_semihosting\n\t"); |
| 50 | +#else |
| 51 | +#pragma import(__use_two_region_memory) |
| 52 | +#endif |
| 53 | + |
| 54 | +/* Fix __user_setup_stackheap and ARM_LIB_STACK/ARM_LIB_HEAP cannot co-exist in RTOS-less build |
| 55 | + * |
| 56 | + * According AN241 (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0241b/index.html), |
| 57 | + * __rt_entry has the following call sequence: |
| 58 | + * 1. _platform_pre_stackheap_init |
| 59 | + * 2. __user_setup_stackheap or setup the Stack Pointer (SP) by another method |
| 60 | + * 3. _platform_post_stackheap_init |
| 61 | + * 4. __rt_lib_init |
| 62 | + * 5. _platform_post_lib_init |
| 63 | + * 6. main() |
| 64 | + * 7. exit() |
| 65 | + * |
| 66 | + * Per our check, when __user_setup_stackheap and ARM_LIB_STACK/ARM_LIB_HEAP co-exist, neither |
| 67 | + * does __user_setup_stackheap get called and nor is ARM_LIB_HEAP used to get heap base/limit, |
| 68 | + * which are required to pass to __rt_lib_init later. To fix the issue, by subclass'ing |
| 69 | + * __rt_lib_init, heap base/limit are replaced with Image$$ARM_LIB_HEAP$$ZI$$Base/Limit if |
| 70 | + * ARM_LIB_HEAP region is defined in scatter file. |
| 71 | + * |
| 72 | + * The overriding __rt_lib_init is needed only for rtos-less code. For rtos code, __rt_entry is |
| 73 | + * overridden and the overriding __rt_lib_init here gets meaningless. |
| 74 | + */ |
| 75 | +extern __value_in_regs struct __argc_argv $Super$$__rt_lib_init(unsigned heapbase, unsigned heaptop); |
| 76 | + |
| 77 | +__value_in_regs struct __argc_argv $Sub$$__rt_lib_init (unsigned heapbase, unsigned heaptop) |
| 78 | +{ |
| 79 | + return $Super$$__rt_lib_init((unsigned) Image$$ARM_LIB_HEAP$$Base, (unsigned) Image$$ARM_LIB_HEAP$$ZI$$Limit); |
| 80 | +} |
| 81 | + |
| 82 | +#endif |
| 83 | + |
| 84 | +#ifdef __cplusplus |
| 85 | +} |
| 86 | +#endif |
0 commit comments