Skip to content

Commit 307e2a8

Browse files
committed
ARMCC - Initialize RTOS before standard library
Initialize the RTOS before initializing the standard library. This allows C++ constructors to be called in a well defined thread context.
1 parent fcea510 commit 307e2a8

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

core/mbed-rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) {
257257
*---------------------------------------------------------------------------*/
258258

259259
/* Main Thread definition */
260-
extern int main (void);
261-
osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1U, 0U, NULL};
260+
extern void pre_main (void);
261+
osThreadDef_t os_thread_def_main = {(os_pthread)pre_main, osPriorityNormal, 1U, 0U, NULL};
262262

263263
// This define should be probably moved to the CMSIS layer
264264
#if defined(TARGET_LPC1768)
@@ -445,6 +445,32 @@ void _main_init (void) {
445445
}
446446
#else
447447

448+
void * armcc_heap_base;
449+
void * armcc_heap_top;
450+
451+
__asm void pre_main (void)
452+
{
453+
IMPORT __rt_lib_init
454+
IMPORT main
455+
IMPORT armcc_heap_base
456+
IMPORT armcc_heap_top
457+
458+
LDR R0,=armcc_heap_base
459+
LDR R1,=armcc_heap_top
460+
LDR R0,[R0]
461+
LDR R1,[R1]
462+
/* Save link register (keep 8 byte alignment with dummy r4) */
463+
push {r4, lr}
464+
BL __rt_lib_init
465+
/* Restore link register and branch so when main returns it
466+
* goes to the thread destroy function.
467+
*/
468+
pop {r4, lr}
469+
B main
470+
471+
ALIGN
472+
}
473+
448474
/* The single memory model is checking for stack collision at run time, verifing
449475
that the heap pointer is underneath the stack pointer.
450476
@@ -456,19 +482,29 @@ void _main_init (void) {
456482
__asm void __rt_entry (void) {
457483

458484
IMPORT __user_setup_stackheap
459-
IMPORT __rt_lib_init
485+
IMPORT armcc_heap_base
486+
IMPORT armcc_heap_top
460487
IMPORT os_thread_def_main
461488
IMPORT osKernelInitialize
462489
#ifdef __MBED_CMSIS_RTOS_CM
463490
IMPORT set_main_stack
464491
#endif
465492
IMPORT osKernelStart
466493
IMPORT osThreadCreate
467-
IMPORT exit
468494

495+
/* __user_setup_stackheap returns:
496+
* - Heap base in r0 (if the program uses the heap).
497+
* - Stack base in sp.
498+
* - Heap limit in r2 (if the program uses the heap and uses two-region memory).
499+
*
500+
* More info can be found in:
501+
* ARM Compiler ARM C and C++ Libraries and Floating-Point Support User Guide
502+
*/
469503
BL __user_setup_stackheap
470-
MOV R1,R2
471-
BL __rt_lib_init
504+
LDR R3,=armcc_heap_base
505+
LDR R4,=armcc_heap_top
506+
STR R0,[R3]
507+
STR R2,[R4]
472508
BL osKernelInitialize
473509
#ifdef __MBED_CMSIS_RTOS_CM
474510
BL set_main_stack
@@ -477,7 +513,8 @@ __asm void __rt_entry (void) {
477513
MOVS R1,#0
478514
BL osThreadCreate
479515
BL osKernelStart
480-
BL exit
516+
/* osKernelStart should not return */
517+
B .
481518

482519
ALIGN
483520
}

0 commit comments

Comments
 (0)