Skip to content

Commit ebbe2cf

Browse files
authored
Ensure interrupts are enabled at first task start (#214)
Critical sections in FreeRTOS are implemented using the following two functions: void vPortEnterCritical( void ) { portDISABLE_INTERRUPTS(); uxCriticalNesting++; } void vPortExitCritical( void ) { uxCriticalNesting--; if( uxCriticalNesting == 0 ) { portENABLE_INTERRUPTS(); } } uxCriticalNesting is initialized to a large value at the start and set to zero when the scheduler is started (xPortStartScheduler). As a result, before the scheduler is started, a pair of enter/exit critical section will leave the interrupts disabled because uxCriticalNesting will not reach zero in the vPortExitCritical function. This is done to ensure that the interrupts remain disabled from the time first FreeRTOS API is called to the time when the scheduler is started. The scheduler starting code is expected to enure that interrupts are enabled before the first task starts executing. Cortex-M33 ports were not enabling interrupts before starting the first task and as a result, the first task was started with interrupts disabled. This PR fixes the issue by ensuring that interrupts are enabled before the first task is started. Signed-off-by: Gaurav Aggarwal <[email protected]>
1 parent 1431b65 commit ebbe2cf

File tree

16 files changed

+59
-33
lines changed

16 files changed

+59
-33
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
8484
" adds r0, #32 \n"/* Discard everything up to r0. */
8585
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
8686
" isb \n"
87+
" mov r0, #0 \n"
88+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
8789
" bx r4 \n"/* Finally, branch to EXC_RETURN. */
8890
#else /* configENABLE_MPU */
8991
" ldm r0!, {r1-r3} \n"/* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */
@@ -95,6 +97,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
9597
" adds r0, #32 \n"/* Discard everything up to r0. */
9698
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
9799
" isb \n"
100+
" mov r0, #0 \n"
101+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
98102
" bx r3 \n"/* Finally, branch to EXC_RETURN. */
99103
#endif /* configENABLE_MPU */
100104
" \n"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
8282
" adds r0, #32 \n"/* Discard everything up to r0. */
8383
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
8484
" isb \n"
85+
" mov r0, #0 \n"
86+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
8587
" bx r3 \n"/* Finally, branch to EXC_RETURN. */
8688
#else /* configENABLE_MPU */
8789
" ldm r0!, {r1-r2} \n"/* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
@@ -91,6 +93,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
9193
" adds r0, #32 \n"/* Discard everything up to r0. */
9294
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
9395
" isb \n"
96+
" mov r0, #0 \n"
97+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
9498
" bx r2 \n"/* Finally, branch to EXC_RETURN. */
9599
#endif /* configENABLE_MPU */
96100
" \n"

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ vRestoreContextOfFirstTask:
128128
adds r0, #32 /* Discard everything up to r0. */
129129
msr psp, r0 /* This is now the new top of stack to use in the task. */
130130
isb
131+
mov r0, #0
132+
msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
131133
bx r4 /* Finally, branch to EXC_RETURN. */
132134
#else /* configENABLE_MPU */
133135
ldm r0!, {r1-r3} /* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */
@@ -139,6 +141,8 @@ vRestoreContextOfFirstTask:
139141
adds r0, #32 /* Discard everything up to r0. */
140142
msr psp, r0 /* This is now the new top of stack to use in the task. */
141143
isb
144+
mov r0, #0
145+
msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
142146
bx r3 /* Finally, branch to EXC_RETURN. */
143147
#endif /* configENABLE_MPU */
144148
/*-----------------------------------------------------------*/

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ vRestoreContextOfFirstTask:
116116
adds r0, #32 /* Discard everything up to r0. */
117117
msr psp, r0 /* This is now the new top of stack to use in the task. */
118118
isb
119+
mov r0, #0
120+
msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
119121
bx r3 /* Finally, branch to EXC_RETURN. */
120122
#else /* configENABLE_MPU */
121123
ldm r0!, {r1-r2} /* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
@@ -125,6 +127,8 @@ vRestoreContextOfFirstTask:
125127
adds r0, #32 /* Discard everything up to r0. */
126128
msr psp, r0 /* This is now the new top of stack to use in the task. */
127129
isb
130+
mov r0, #0
131+
msr basepri, r0 /* Ensure that interrupts are enabled when the first task starts. */
128132
bx r2 /* Finally, branch to EXC_RETURN. */
129133
#endif /* configENABLE_MPU */
130134
/*-----------------------------------------------------------*/

portable/GCC/ARM_CM23/non_secure/port.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
* on the secure side. The following are the valid configuration seetings:
5555
*
5656
* 1. Run FreeRTOS on the Secure Side:
57-
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
57+
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
5858
*
5959
* 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:
60-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
60+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
6161
*
6262
* 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:
63-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
63+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
6464
*/
6565
#if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )
6666
#error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.
@@ -875,7 +875,7 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
875875
}
876876
}
877877
/*-----------------------------------------------------------*/
878-
878+
/* *INDENT-OFF* */
879879
#if ( configENABLE_MPU == 1 )
880880
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
881881
StackType_t * pxEndOfStack,
@@ -888,6 +888,7 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
888888
TaskFunction_t pxCode,
889889
void * pvParameters ) /* PRIVILEGED_FUNCTION */
890890
#endif /* configENABLE_MPU */
891+
/* *INDENT-ON* */
891892
{
892893
/* Simulate the stack frame as it would be created by a context switch
893894
* interrupt. */

portable/GCC/ARM_CM23_NTZ/non_secure/port.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
* on the secure side. The following are the valid configuration seetings:
5555
*
5656
* 1. Run FreeRTOS on the Secure Side:
57-
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
57+
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
5858
*
5959
* 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:
60-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
60+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
6161
*
6262
* 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:
63-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
63+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
6464
*/
6565
#if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )
6666
#error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.
@@ -875,7 +875,6 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
875875
}
876876
}
877877
/*-----------------------------------------------------------*/
878-
879878
/* *INDENT-OFF* */
880879
#if ( configENABLE_MPU == 1 )
881880
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,

portable/GCC/ARM_CM33/non_secure/port.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
* on the secure side. The following are the valid configuration seetings:
5555
*
5656
* 1. Run FreeRTOS on the Secure Side:
57-
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
57+
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
5858
*
5959
* 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:
60-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
60+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
6161
*
6262
* 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:
63-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
63+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
6464
*/
6565
#if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )
6666
#error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.
@@ -875,7 +875,6 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
875875
}
876876
}
877877
/*-----------------------------------------------------------*/
878-
879878
/* *INDENT-OFF* */
880879
#if ( configENABLE_MPU == 1 )
881880
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,

portable/GCC/ARM_CM33/non_secure/portasm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
8484
" adds r0, #32 \n"/* Discard everything up to r0. */
8585
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
8686
" isb \n"
87+
" mov r0, #0 \n"
88+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
8789
" bx r4 \n"/* Finally, branch to EXC_RETURN. */
8890
#else /* configENABLE_MPU */
8991
" ldm r0!, {r1-r3} \n"/* Read from stack - r1 = xSecureContext, r2 = PSPLIM and r3 = EXC_RETURN. */
@@ -95,6 +97,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
9597
" adds r0, #32 \n"/* Discard everything up to r0. */
9698
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
9799
" isb \n"
100+
" mov r0, #0 \n"
101+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
98102
" bx r3 \n"/* Finally, branch to EXC_RETURN. */
99103
#endif /* configENABLE_MPU */
100104
" \n"

portable/GCC/ARM_CM33_NTZ/non_secure/port.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
* on the secure side. The following are the valid configuration seetings:
5555
*
5656
* 1. Run FreeRTOS on the Secure Side:
57-
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
57+
* configRUN_FREERTOS_SECURE_ONLY = 1 and configENABLE_TRUSTZONE = 0
5858
*
5959
* 2. Run FreeRTOS on the Non-Secure Side with Secure Side function call support:
60-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
60+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 1
6161
*
6262
* 3. Run FreeRTOS on the Non-Secure Side only i.e. no Secure Side function call support:
63-
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
63+
* configRUN_FREERTOS_SECURE_ONLY = 0 and configENABLE_TRUSTZONE = 0
6464
*/
6565
#if ( ( configRUN_FREERTOS_SECURE_ONLY == 1 ) && ( configENABLE_TRUSTZONE == 1 ) )
6666
#error TrustZone needs to be disabled in order to run FreeRTOS on the Secure Side.
@@ -875,7 +875,6 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
875875
}
876876
}
877877
/*-----------------------------------------------------------*/
878-
879878
/* *INDENT-OFF* */
880879
#if ( configENABLE_MPU == 1 )
881880
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
@@ -887,7 +886,7 @@ void vPortSVCHandler_C( uint32_t * pulCallerStackAddress ) /* PRIVILEGED_FUNCTIO
887886
StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
888887
StackType_t * pxEndOfStack,
889888
TaskFunction_t pxCode,
890-
void * pvParameters ) /* PRIVILEGED_FUNCTION */
889+
void * pvParameters ) /* PRIVILEGED_FUNCTION */
891890
#endif /* configENABLE_MPU */
892891
/* *INDENT-ON* */
893892
{

portable/GCC/ARM_CM33_NTZ/non_secure/portasm.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
8282
" adds r0, #32 \n"/* Discard everything up to r0. */
8383
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
8484
" isb \n"
85+
" mov r0, #0 \n"
86+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
8587
" bx r3 \n"/* Finally, branch to EXC_RETURN. */
8688
#else /* configENABLE_MPU */
8789
" ldm r0!, {r1-r2} \n"/* Read from stack - r1 = PSPLIM and r2 = EXC_RETURN. */
@@ -91,6 +93,8 @@ void vRestoreContextOfFirstTask( void ) /* __attribute__ (( naked )) PRIVILEGED_
9193
" adds r0, #32 \n"/* Discard everything up to r0. */
9294
" msr psp, r0 \n"/* This is now the new top of stack to use in the task. */
9395
" isb \n"
96+
" mov r0, #0 \n"
97+
" msr basepri, r0 \n"/* Ensure that interrupts are enabled when the first task starts. */
9498
" bx r2 \n"/* Finally, branch to EXC_RETURN. */
9599
#endif /* configENABLE_MPU */
96100
" \n"

0 commit comments

Comments
 (0)