This repository has been archived on 2023-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
FreeRTOS-Kernel/portable/GCC
Simon Beaudoin 61635d5b8b
Fix race condition when tracing is enabled (#95)
* Update port.c

I discovered a very snicky and tricky race condition scenario when integrating tracealyzer code into our project.

A little background  on CortexR5 : When the IRQ line (comming from the interrupt controller, to which every peripheral IRQ lines connect) of the processor rises and the IRQ Enable bit in the status register of the CPU permits it, the CPU traps into interrupt mode. Several things happen. First, the CPU finishes the instruction it was performing. Second, it places the content of the CPSR register into the SPSR_irq register. And third, the mode of the CPU is changed to IRQ_Mode and /!\ THE IRQ ENABLE BIT IN CPSR_irq IS AUTOMATICALLY CLEARED /!\. The reason is to ensure that upon landing into IRQ code, we find ourselves automatically in a critical section because we cannot be interrupted again because the bit is cleared. The programmer can, if he wants, re-enable IRQs inside IRQ code itself to allow interrupt nesting. But it has to be wanted and meditated. 


Now, inside portASM.S, at the end of 'FreeRTOS_IRQ_Handler' assembly function, a call to 'vTaskSwitchContextConst' is made if the variable 'ulPortYieldRequired' was set by someone while executing the interrupt. Before branching to that function, a 'CPSID	i' instruction was placed to ensure that interrupts are disabled in case someone re-enabled it. Inside 'vTaskSwitchContext', there is the macro 'traceTASK_SWITCHED_OUT' that gets populated when tracing is enabled. 

The bug is right there.. If the macro is populated and inside that macro there is a matching call to 'ulPortSetInterruptMask' and 'vPortClearInterruptMask', a race condition can occure is there is a OS Tick timer interrupt waiting at the interrupt controller's door. Upon calling 'vTaskSwitchContext', the interrupts are not masked in the interrupt controller, the only barrier against the CPU servicing that tick interrupt while already performing the function is that the IRQ Enable bit cleared. 'ulPortSetInterruptMask' 
does what's its supposed to do, but doesn't take into account the IRQ Enable bit in CPSR. Wheter or not the bit was cleared, the function sets it at the end. When calling the matching 'vPortClearInterruptMask', the function clears the interrupt mask in the interrupt controller. Because the IRQ Enable bit (that was cleared) has been set no matter what in 'ulPortSetInterruptMask', the CPU services the OS Tick Interrupt right away. 

The bug is there : instead of completing the 'vTaskSwitchContext' function, the CPU re-enters the switch context path right after 'traceTASK_SWITCHED_OUT' thus corrupting the CPU state and eventually triggering either an undefined instruction, data or instruction abort.

* Update port.c

Error on my part, this is the right inline asm code to retreive CPSR register

* Update port.c

Forgot an * while writing comment..
2020-08-10 09:46:39 -07:00
..
ARM7_AT91FR40008 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM7_AT91SAM7S Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM7_LPC23xx Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM7_LPC2000 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM_CA9 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM_CA53_64_BIT Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ARM_CM0 Update portNVIC_SYSPRI2_REG to portNVIC_SHPR3_REG (#86) 2020-07-15 19:44:45 -07:00
ARM_CM3 Update portNVIC_SYSPRI2_REG to portNVIC_SHPR3_REG (#86) 2020-07-15 19:44:45 -07:00
ARM_CM3_MPU Update incorrect port in comments (#87) 2020-07-15 19:44:57 -07:00
ARM_CM4_MPU Allow application to override TEX,S,C and B bits for Flash and RAM (#113) 2020-08-08 18:37:14 -07:00
ARM_CM4F Update portNVIC_SYSPRI2_REG to portNVIC_SHPR3_REG (#86) 2020-07-15 19:44:45 -07:00
ARM_CM7 Update incorrect port in comments (#87) 2020-07-15 19:44:57 -07:00
ARM_CM23 Use configSYSTICK_CLOCK_HZ to configure SysTick (#103) 2020-07-24 09:45:42 -07:00
ARM_CM23_NTZ/non_secure Use configSYSTICK_CLOCK_HZ to configure SysTick (#103) 2020-07-24 09:45:42 -07:00
ARM_CM33 Use configSYSTICK_CLOCK_HZ to configure SysTick (#103) 2020-07-24 09:45:42 -07:00
ARM_CM33_NTZ/non_secure Use configSYSTICK_CLOCK_HZ to configure SysTick (#103) 2020-07-24 09:45:42 -07:00
ARM_CR5 Fix race condition when tracing is enabled (#95) 2020-08-10 09:46:39 -07:00
ARM_CRx_No_GIC Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
ATMega323 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
AVR32_UC3 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
AVR_AVRDx Update to AVR_Mega0 and AVR_Dx GCC ports + addition of their IAR equivalents (#106) 2020-08-06 16:24:05 -07:00
AVR_Mega0 Update to AVR_Mega0 and AVR_Dx GCC ports + addition of their IAR equivalents (#106) 2020-08-06 16:24:05 -07:00
ColdFire_V2 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
CORTUS_APS3 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
H8S2329 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
HCS12 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
IA32_flat Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
MCF5235 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
MicroBlaze Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
MicroBlazeV8 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
MicroBlazeV9 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
MSP430F449 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
NiosII Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
PPC405_Xilinx Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
PPC440_Xilinx Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
RISC-V Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
RL78 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
RX100 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
RX600 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
RX600v2 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
STR75x Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00
TriCore_1782 Style: uncrustify kernel files 2020-07-08 10:24:06 -07:00