@@ -54,18 +54,33 @@ void FUN(void) __attribute__ ((weak, alias(#FUN_ALIAS)));
54
54
55
55
#endif
56
56
57
-
58
57
/* Initialize segments */
59
58
#if defined(__ARMCC_VERSION )
60
59
#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U ) && (TFM_LVL > 0 )
61
60
extern uint32_t Image$$ARM_LIB_STACK_MSP$$ZI$$Limit ;
61
+ extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
62
62
#else
63
63
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit ;
64
64
#endif
65
65
extern void __main (void );
66
66
#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
67
76
void __iar_program_start (void );
68
77
#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
69
84
extern uint32_t __StackTop ;
70
85
extern uint32_t __copy_table_start__ ;
71
86
extern uint32_t __copy_table_end__ ;
@@ -195,12 +210,6 @@ WEAK_ALIAS_FUNC(TRNG_IRQHandler, Default_Handler) // 101:
195
210
__attribute__ ((section ("RESET" )))
196
211
const uint32_t __vector_handlers [] = {
197
212
#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
204
213
const uint32_t __vector_table [] @ ".intvec" = {
205
214
#elif defined(__GNUC__ )
206
215
__attribute__ ((section (".vector_table" )))
@@ -347,65 +356,91 @@ const uint32_t __vector_handlers[] = {
347
356
(uint32_t ) TRNG_IRQHandler , // 101:
348
357
};
349
358
350
- #if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
351
-
352
359
/* Some reset handler code cannot implement in pure C. Implement it in inline/embedded assembly.
353
360
*
354
361
* 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
357
364
*
358
- * Reset_Handler_1
365
+ * Reset_Handler_1:
366
+ * Platform initialization
359
367
* C/C++ runtime initialization
360
368
*/
361
369
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
362
377
void Reset_Handler_1 (void );
363
378
364
379
/* Add '__attribute__((naked))' here to make sure compiler does not generate prologue and
365
380
* 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
+ */
367
388
__attribute__((naked )) void Reset_Handler (void )
368
389
{
369
- #if ! defined(__ICCARM__ )
390
+ #if defined(__GNUC__ )
370
391
__asm(".syntax unified \n" );
371
- __asm(".globl Reset_Handler_1 \n" );
372
392
#endif
373
393
374
394
/* 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 */
376
397
#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" );
380
400
#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" );
383
402
#endif
403
+ __asm("blx r0 \n" );
404
+
405
+ /* Switch from MSP to PSP */
384
406
__asm("msr psp, r0 \n" );
385
- __asm("mrs r0, control \n" ); // Switch SP to PSP
407
+ __asm("mrs r0, control \n" );
386
408
__asm("movs r1, #2 \n" );
387
409
__asm("orrs r0, r1 \n" );
388
410
__asm("msr control, r0 \n" );
389
411
#endif
390
412
413
+ /* Jump to Reset_Handler_1 */
391
414
#if !defined(__ICCARM__ )
392
415
__asm("movw r0, #:lower16:Reset_Handler_1 \n" );
393
416
__asm("movt r0, #:upper16:Reset_Handler_1 \n" );
394
417
#else
395
- __asm("mov32 r0, Reset_Handler_1 \n" );
418
+ __asm("mov32 r0, Reset_Handler_1 \n" );
396
419
#endif
397
420
__asm("bx r0 \n" );
398
421
}
399
422
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 ;
405
428
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
+ );
407
438
439
+ return stacklimit_psp ;
440
+ }
441
+ #endif
408
442
443
+ void Reset_Handler_1 (void )
409
444
{
410
445
#if defined(__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
411
446
/* Disable register write-protection function */
0 commit comments