@@ -54,18 +54,33 @@ void FUN(void) __attribute__ ((weak, alias(#FUN_ALIAS)));
5454
5555#endif
5656
57-
5857/* Initialize segments */
5958#if defined(__ARMCC_VERSION )
6059#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
6160extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit ;
61+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
6262#else
6363extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
6464#endif
6565extern void __main (void );
6666#elif defined(__ICCARM__ )
67+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
68+ extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit ;
69+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
70+ extern uint32_t CSTACK_MSP$$Limit ;
71+ extern uint32_t CSTACK$$Limit ;
72+ #else
73+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
74+ extern uint32_t CSTACK$$Limit ;
75+ #endif
6776void __iar_program_start (void );
6877#elif defined(__GNUC__ )
78+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
79+ extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit ;
80+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
81+ #else
82+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
83+ #endif
6984extern uint32_t __StackTop ;
7085extern uint32_t __copy_table_start__ ;
7186extern uint32_t __copy_table_end__ ;
@@ -195,12 +210,6 @@ WEAK_ALIAS_FUNC(TRNG_IRQHandler, Default_Handler) // 101:
195210__attribute__ ((section ("RESET" )))
196211const uint32_t __vector_handlers [] = {
197212#elif defined(__ICCARM__ )
198- #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
199- extern uint32_t CSTACK_MSP$$Limit ;
200- extern uint32_t CSTACK$$Limit ;
201- #else
202- extern uint32_t CSTACK$$Limit ;
203- #endif
204213const uint32_t __vector_table [] @ ".intvec" = {
205214#elif defined(__GNUC__ )
206215__attribute__ ((section (".vector_table" )))
@@ -347,65 +356,91 @@ const uint32_t __vector_handlers[] = {
347356 (uint32_t ) TRNG_IRQHandler , // 101:
348357};
349358
350- #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
351-
352359/* Some reset handler code cannot implement in pure C. Implement it in inline/embedded assembly.
353360 *
354361 * Reset_Handler:
355- * For non-secure PSA/non-secure non-PSA/secure non-PSA, do as usual
356- * For secure PSA, switch from MSP to PSP, then jump to Reset_Handler_1 for usual work
362+ * For non-secure PSA/non-secure non-PSA/secure non-PSA, jump directly to Reset_Handler_1
363+ * For secure PSA, switch from MSP to PSP, then jump to Reset_Handler_1
357364 *
358- * Reset_Handler_1
365+ * Reset_Handler_1:
366+ * Platform initialization
359367 * C/C++ runtime initialization
360368 */
361369
370+ /* Forward declaration */
371+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
372+ /* Limited by inline assembly syntax, esp. on IAR, we cannot get stack limit
373+ * for PSP just from external symbol. To avoid re-write in assembly, We make up
374+ * a function here to get this value indirectly. */
375+ uint32_t StackLimit_PSP (void );
376+ #endif
362377void Reset_Handler_1 (void );
363378
364379/* Add '__attribute__((naked))' here to make sure compiler does not generate prologue and
365380 * epilogue sequences for Reset_Handler. We don't want MSP is updated by compiler-generated
366- * code during stack switch. */
381+ * code during stack switch.
382+ *
383+ * Don't allow extended assembly in naked functions:
384+ * The compiler only supports basic __asm statements in __attribute__((naked))
385+ * functions. Using extended assembly, parameter references or mixing C code with
386+ * __asm statements might not work reliably.
387+ */
367388__attribute__((naked )) void Reset_Handler (void )
368389{
369- #if ! defined(__ICCARM__ )
390+ #if defined(__GNUC__ )
370391 __asm(".syntax unified \n" );
371- __asm(".globl Reset_Handler_1 \n" );
372392#endif
373393
374394 /* Secure TFM requires PSP as boot stack */
375- #if TFM_LVL != 0
395+ #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
396+ /* Get stack limit for PSP */
376397#if !defined(__ICCARM__ )
377- __asm(".globl Image$$ARM_LIB_STACK$$ZI$$Limit \n" );
378- __asm("movw r0, #:lower16:Image$$ARM_LIB_STACK$$ZI$$Limit \n" ); // Initialize PSP
379- __asm("movt r0, #:upper16:Image$$ARM_LIB_STACK$$ZI$$Limit \n" );
398+ __asm("movw r0, #:lower16:StackLimit_PSP \n" );
399+ __asm("movt r0, #:upper16:StackLimit_PSP \n" );
380400#else
381- __asm(".globl Image$$ARM_LIB_STACK$$ZI$$Limit \n" );
382- __asm("mov32 r0, Image$$ARM_LIB_STACK$$ZI$$Limit \n" );
401+ __asm("mov32 r0, StackLimit_PSP \n" );
383402#endif
403+ __asm("blx r0 \n" );
404+
405+ /* Switch from MSP to PSP */
384406 __asm("msr psp, r0 \n" );
385- __asm("mrs r0, control \n" ); // Switch SP to PSP
407+ __asm("mrs r0, control \n" );
386408 __asm("movs r1, #2 \n" );
387409 __asm("orrs r0, r1 \n" );
388410 __asm("msr control, r0 \n" );
389411#endif
390412
413+ /* Jump to Reset_Handler_1 */
391414#if !defined(__ICCARM__ )
392415 __asm("movw r0, #:lower16:Reset_Handler_1 \n" );
393416 __asm("movt r0, #:upper16:Reset_Handler_1 \n" );
394417#else
395- __asm("mov32 r0, Reset_Handler_1 \n" );
418+ __asm("mov32 r0, Reset_Handler_1 \n" );
396419#endif
397420 __asm("bx r0 \n" );
398421}
399422
400- void Reset_Handler_1 ( void )
401-
402- #else
403-
404- void Reset_Handler ( void )
423+ #if defined ( __ARM_FEATURE_CMSE ) && ( __ARM_FEATURE_CMSE == 3U ) && ( TFM_LVL > 0 )
424+ /* Return stack limit for PSP */
425+ uint32_t StackLimit_PSP ( void )
426+ {
427+ uint32_t stacklimit_psp ;
405428
406- #endif /* defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) */
429+ __asm volatile (
430+ #if defined(__GNUC__ )
431+ ".syntax unified \n"
432+ #endif
433+ "mov %[Rd], %[Rn] \n"
434+ : [Rd ] "= r " (stacklimit_psp) /* comma-separated list of output operands */
435+ : [Rn ] " r " (&Image$$ARM_LIB_STACK$$ZI$$Limit) /* comma-separated list of input operands */
436+ : " cc " /* comma-separated list of clobbered resources */
437+ );
407438
439+ return stacklimit_psp ;
440+ }
441+ #endif
408442
443+ void Reset_Handler_1 (void )
409444{
410445#if defined(__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
411446 /* Disable register write-protection function */
0 commit comments