How RTOS does task switching from interrupt
Asked Answered
L

2

8

Suppose there is two task running TASK_A and TASK_B. While TASK_A is running an interrupt occurred and a context switch to TASK_B is needed.

While inside ISR, TASK_B should not be directly jumped since it is still in ISR and have'nt returned yet (for eg: RETI is not executed). If normally returned from ISR it will as usual go back to the TASK_A's last program counter. So the RTOS has to get out of the ISR and as soon as it is out of the ISR it has to do a context switch to TASK_B. Instead of going back to TASK_A how does a RTOS get back control as soon as it exit ISR.

In case of switching from a RTOS function such as while unlocking a mutex a direct switch can be made by backing up the registers.

How does an RTOS handle this situation

Pardon me if this is a generalized question...

Lagan answered 12/2, 2015 at 19:34 Comment(0)
O
14

The ISR has to be implemented in a way that allows for a context switch. Typically, at the end of the ISR there will be a call to an RTOS function that checks for and performs the context switch.

When the interrupt occurs, the CPU saves its context and jumps to the ISR. The way the context is saved varies among CPU families. When the ISR is complete, it should call a special RTOS routine that allows for a context switch after an ISR. If there is a higher priority task ready to run then this routine will perform a context switch. It will take the pre-interrupt context saved by the CPU and save it with TASK_A. Then it will get the saved context of TASK_B and restore it into the CPU such that when the end-of-interrupt instruction is called, execution returns to the context of TASK_B.

The details of all of this is very CPU and RTOS dependent.

Ochs answered 12/2, 2015 at 22:3 Comment(2)
Thanks kkrambo... So if we take an example of the arm series, the link register which stores the address to where it should be returned after ISR will be replaced with the TASK_B 's last PC along with other registers also. Is my understanding correct?Lagan
Yes, that sounds correct. The FreeRTOS Putting It All Together page gives a detailed example of how FreeRTOS performs a context switch on an AVR microcontroller. You could download the ARM port of FreeRTOS to see the details of the ARM implementation.Ochs
D
5

An RTOS will require specific ISR entry/exit code to be included in each ISR that may cause a context switch (typically any that call the RTOS API). These functions maintain a counter that allows nested interrupts; when the counter is decremented to zero, the outermost ISR is about to return, and the exit code invokes the kernel scheduler.

The scheduler will restore the context to the highest priority ready task; this includes modifying the return address so that the RETI instruction causes the program counter to be set to TASK_B's restart point rather the TASK_A. The scheduler will store the TASK_A restart point in the task control block(TCB) so that its context can be similarly restored.

Deportment answered 15/2, 2015 at 20:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.