2.6 How an RTOS uses SVC and PendSV
The FreeRTOS Cortex-M ports, including the v8-M CM33/CM23 ports you may have brought up, use SVC and PendSV as follows.
Starting the first task: SVC. vStartFirstTask resets MSP from word 0 of the vector table at VTOR, enables interrupts, and executes SVC. The SVCall handler loads the first task’s saved context and points PSP (and PSPLIM on v8-M) at its frame. It then returns with an EXC_RETURN such as 0xFFFFFFBC (Non-secure Thread, PSP). That single exception return switches to Thread mode, selects PSP, pops the task’s crafted frame and jumps to its entry point. The crafted frame has RETPSR.T = 1, PC = task function and LR = the task-exit trap. Using an exception return does all of this in one architected step.
Context switching: PendSV at the lowest priority. SysTick and other ISRs request a switch by writing ICSR.PENDSVSET. When a task yields, portYIELD follows that write with DSB; ISB: the DSB makes the write complete, and the ISB makes sure the now-pending PendSV is taken before the next instruction when it is not masked. PendSV runs at the lowest priority, so it is taken only once no other handler is active, typically by tail-chaining out of the last ISR. At that point the frame on PSP is the task’s frame, and no ISR is delayed by the switch.
The handler does the following:
- saves R4-R11, EXC_RETURN and (v8-M) PSPLIM below the hardware frame;
- if
EXC_RETURN.FType == 0, also saves S16-S31; that VSTM is an FP instruction, so it triggers any pending lazy save of S0-S15; - stores PSP in the TCB and calls
vTaskSwitchContext; - restores the next task’s registers and returns through its EXC_RETURN.
Critical sections raise BASEPRI to configMAX_SYSCALL_INTERRUPT_PRIORITY (Mainline). ISRs with a higher priority (a numerically lower value) are never masked, so they must not call FreeRTOS APIs. With configASSERT defined, vPortValidateInterruptPriority asserts on this. Baseline has no BASEPRI, so the CM23 port uses PRIMASK.
Coming from A-profile: There is no EL1 kernel entry vector and no TPIDR/SP_EL0 swap. The OS uses the M-profile exception return itself as its context-switch primitive: PendSV plays the role of a deferred-reschedule IRQ, and SVC the role of the first ERET to EL0.
Checkpoint
Q2.6.1 Why does FreeRTOS run PendSV at the lowest configurable priority?
- A. Because PendSV is the only exception allowed to write PSP
- B. Because the architecture fixes PendSV at the lowest priority
- C. So that the switch is deferred until no other handler is active, typically tail-chained from the last ISR. The frame on PSP is then the interrupted task’s, and no ISR is delayed by the context switch
- D. Because a lower priority makes PendSV non-preemptible
Answer
C. At the lowest priority, PendSV can only run when it would return to Thread mode, so it swaps the task context cleanly and never nests inside an ISR. PendSV’s priority is configurable (SHPR3), not fixed, and any privileged code can write PSP. A low priority makes a handler more preemptible, not less.
Read more: Armv8-M ARM (DDI0553), PendSV, SHPR3, ICSR.PENDSVSET; FreeRTOS ARM_CM33 port.c
Q2.6.2 What is the essential reason the port starts the first task with SVC rather than simply jumping to the task function from main?
- A. Privileged Thread mode cannot write CONTROL.SPSEL, so only an exception can select PSP
- B. SVC is the only way to enable interrupts on Armv8-M
- C. Without SVC, the MPU would block execution of task code
- D. An exception return loads the task’s crafted frame (PC, RETPSR, R0, LR, …) and selects Thread mode, PSP and Security state from EXC_RETURN in one step, exactly as every later context switch does
Answer
D. Using an exception return makes the first task start the same way as every later switch, from a frame the PendSV/SVC path understands, and it loads xPSR from the frame’s RETPSR in the same step. Privileged Thread code can write CONTROL.SPSEL with MSR, so that tempting option is false. Interrupts are enabled with CPSIE or MSR, not SVC.
Read more: Armv8-M ARM (DDI0553), EXC_RETURN; CONTROL register; FreeRTOS ARM_CM33 port.c
Q2.6.3 In a FreeRTOS PendSV handler on Cortex-M33, why does the code test bit[4] of the EXC_RETURN value in LR before saving S16-S31?
- A. FType = 0 means the hardware built an extended frame because the task had live FP context (CONTROL.FPCA = 1), so the callee-saved S16-S31 must also be saved
- B. Bit[4] is SPSEL; it tells the handler which stack the task used
- C. Bit[4] indicates whether the task was Secure
- D. S16-S31 must always be saved, and the test only selects the instruction encoding
Answer
A. EXC_RETURN.FType = 0 indicates an extended frame. The hardware (lazily) handles S0-S15 and FPSCR, but S16-S31 are callee-saved and belong to the task, so the switch code must save them. Tasks that never used FP skip that cost. SPSEL is bit[2] and the S bit is bit[6].
Read more: Armv8-M ARM (DDI0553), EXC_RETURN.FType; FreeRTOS ARM_CM33 portasm.c