Skip to content

Commit ae46383

Browse files
authored
Fix vPortFreeSecureContext to read xSecureContext at correct offset (#1441)
When MPU is enabled, the first item in the TCB is not the top of the stack but the stored context location. As a result, xSecureContext is located at a negative offset from that position rather than at offset 0. The current implementation unconditionally reads xSecureContext at offset 0, which returns an incorrect value when MPU is enabled. This commit updates vPortFreeSecureContext to read xSecureContext at the correcct offset based on the port configuration: - CM33/CM35P/CM52/CM55/CM85/STAR_MC3: -20 (or -36 with PAC enabled) - CM23: -20 (no PAC support) - Without MPU: 0 (xSecureContext remains at the top of stack) Signed-off-by: Gaurav Aggarwal <aggarg@amazon.com>
1 parent 49cec3e commit ae46383

18 files changed

Lines changed: 222 additions & 54 deletions

File tree

portable/ARMv8M/non_secure/portable/GCC/ARM_CM23/portasm.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#error Cortex-M23 does not have a Floating Point Unit (FPU) and therefore configENABLE_FPU must be set to 0.
4848
#endif
4949

50+
#if ( configENABLE_MPU == 1 )
51+
#define SECURE_CONTEXT_OFFSET -20
52+
#else
53+
#define SECURE_CONTEXT_OFFSET 0
54+
#endif
55+
5056
#if ( configENABLE_MPU == 1 )
5157

5258
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -590,15 +596,16 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
590596
(
591597
" .syntax unified \n"
592598
" \n"
593-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
594-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
599+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
600+
" adds r2, r2, %0 \n" /* r2 = r2 + SECURE_CONTEXT_OFFSET. */
601+
" ldr r1, [r2] \n" /* Read xSecureContext from the task's context. */
595602
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
596603
" bne free_secure_context \n" /* Branch if r1 != 0. */
597604
" bx lr \n" /* There is no secure context (xSecureContext is NULL). */
598605
" free_secure_context: \n"
599-
" svc %0 \n" /* Secure context is freed in the supervisor call. */
606+
" svc %1 \n" /* Secure context is freed in the supervisor call. */
600607
" bx lr \n" /* Return. */
601-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
608+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
602609
);
603610
}
604611
/*-----------------------------------------------------------*/

portable/ARMv8M/non_secure/portable/GCC/ARM_CM33/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

portable/ARMv8M/non_secure/portable/IAR/ARM_CM23/portasm.s

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler.
4040
#define configUSE_MPU_WRAPPERS_V1 0
4141
#endif
4242

43+
#if ( configENABLE_MPU == 1 )
44+
#define SECURE_CONTEXT_OFFSET -20
45+
#else
46+
#define SECURE_CONTEXT_OFFSET 0
47+
#endif
48+
4349

4450
EXTERN pxCurrentTCB
4551
EXTERN xSecureContext
@@ -512,8 +518,9 @@ SVC_Handler:
512518
/*-----------------------------------------------------------*/
513519

514520
vPortFreeSecureContext:
515-
ldr r2, [r0] /* The first item in the TCB is the top of the stack. */
516-
ldr r1, [r2] /* The first item on the stack is the task's xSecureContext. */
521+
ldr r2, [r0] /* The first item in the TCB is the stored context location. */
522+
adds r2, r2, #SECURE_CONTEXT_OFFSET /* r2 = r2 + SECURE_CONTEXT_OFFSET. */
523+
ldr r1, [r2] /* Read xSecureContext from the task's context. */
517524
cmp r1, #0 /* Raise svc if task's xSecureContext is not NULL. */
518525
bne free_secure_context /* Branch if r1 != 0. */
519526
bx lr /* There is no secure context (xSecureContext is NULL). */

portable/ARMv8M/non_secure/portable/IAR/ARM_CM33/portasm.s

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ files (__ICCARM__ is defined by the IAR C compiler but not by the IAR assembler.
4141
#define configUSE_MPU_WRAPPERS_V1 0
4242
#endif
4343

44+
#if ( configENABLE_MPU == 1 )
45+
#if ( configENABLE_PAC == 1 )
46+
#define SECURE_CONTEXT_OFFSET -36
47+
#else
48+
#define SECURE_CONTEXT_OFFSET -20
49+
#endif
50+
#else
51+
#define SECURE_CONTEXT_OFFSET 0
52+
#endif
53+
4454
EXTERN pxCurrentTCB
4555
EXTERN xSecureContext
4656
EXTERN vTaskSwitchContext
@@ -532,8 +542,8 @@ SVC_Handler:
532542

533543
vPortFreeSecureContext:
534544
/* r0 = uint32_t *pulTCB. */
535-
ldr r2, [r0] /* The first item in the TCB is the top of the stack. */
536-
ldr r1, [r2] /* The first item on the stack is the task's xSecureContext. */
545+
ldr r2, [r0] /* The first item in the TCB is the stored context location. */
546+
ldr r1, [r2, #SECURE_CONTEXT_OFFSET] /* Read xSecureContext from the task's context. */
537547
cmp r1, #0 /* Raise svc if task's xSecureContext is not NULL. */
538548
it ne
539549
svcne 101 /* Secure context is freed in the supervisor call. portSVC_FREE_SECURE_CONTEXT = 101. */

portable/GCC/ARM_CM23/non_secure/portasm.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#error Cortex-M23 does not have a Floating Point Unit (FPU) and therefore configENABLE_FPU must be set to 0.
4848
#endif
4949

50+
#if ( configENABLE_MPU == 1 )
51+
#define SECURE_CONTEXT_OFFSET -20
52+
#else
53+
#define SECURE_CONTEXT_OFFSET 0
54+
#endif
55+
5056
#if ( configENABLE_MPU == 1 )
5157

5258
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -590,15 +596,16 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
590596
(
591597
" .syntax unified \n"
592598
" \n"
593-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
594-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
599+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
600+
" adds r2, r2, %0 \n" /* r2 = r2 + SECURE_CONTEXT_OFFSET. */
601+
" ldr r1, [r2] \n" /* Read xSecureContext from the task's context. */
595602
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
596603
" bne free_secure_context \n" /* Branch if r1 != 0. */
597604
" bx lr \n" /* There is no secure context (xSecureContext is NULL). */
598605
" free_secure_context: \n"
599-
" svc %0 \n" /* Secure context is freed in the supervisor call. */
606+
" svc %1 \n" /* Secure context is freed in the supervisor call. */
600607
" bx lr \n" /* Return. */
601-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
608+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
602609
);
603610
}
604611
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM33/non_secure/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM35P/non_secure/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM52/non_secure/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM55/non_secure/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM85/non_secure/portasm.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
* header files. */
4646
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
4747

48+
#if ( configENABLE_MPU == 1 )
49+
#if ( configENABLE_PAC == 1 )
50+
#define SECURE_CONTEXT_OFFSET -36
51+
#else
52+
#define SECURE_CONTEXT_OFFSET -20
53+
#endif
54+
#else
55+
#define SECURE_CONTEXT_OFFSET 0
56+
#endif
57+
4858
#if ( configENABLE_MPU == 1 )
4959

5060
void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_FUNCTION */
@@ -609,13 +619,13 @@ void vPortFreeSecureContext( uint32_t * pulTCB ) /* __attribute__ (( naked )) PR
609619
(
610620
" .syntax unified \n"
611621
" \n"
612-
" ldr r2, [r0] \n" /* The first item in the TCB is the top of the stack. */
613-
" ldr r1, [r2] \n" /* The first item on the stack is the task's xSecureContext. */
622+
" ldr r2, [r0] \n" /* The first item in the TCB is the stored context location. */
623+
" ldr r1, [r2, %0] \n" /* Read xSecureContext from the task's context. */
614624
" cmp r1, #0 \n" /* Raise svc if task's xSecureContext is not NULL. */
615625
" it ne \n"
616-
" svcne %0 \n" /* Secure context is freed in the supervisor call. */
626+
" svcne %1 \n" /* Secure context is freed in the supervisor call. */
617627
" bx lr \n" /* Return. */
618-
::"i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
628+
::"i" ( SECURE_CONTEXT_OFFSET ), "i" ( portSVC_FREE_SECURE_CONTEXT ) : "memory"
619629
);
620630
}
621631
/*-----------------------------------------------------------*/

0 commit comments

Comments
 (0)