5.16 Context switch

Shows how to perform a context switch on the User mode process.

The code is based around a list of pointers to Process Control Blocks (PCBs) of processes that are ready to run.

This figure shows the layout of the PCBs that the example expects.

Figure 5-2 PCB layout
To view this graphic, your browser must support the SVG format. Either install a browser with native support, or install an appropriate plugin such as Adobe SVG Viewer.


The pointer to the PCB of the next process to run is pointed to by R12, and the end of the list has a zero pointer. Register R13 is a pointer to the PCB, and is preserved between time slices, so that on entry it points to the PCB of the currently running process.

Context switch on the User mode process

    STM     sp,{R0-lr}^       ; Dump user registers above R13.
    MRS     R0, SPSR          ; Pick up the user status
    STMDB   sp, {R0, lr}      ; and dump with return address below.
    LDR     sp, [R12], #4     ; Load next process info pointer.
    CMP     sp, #0            ; If it is zero, it is invalid
    LDMDBNE sp, {R0, lr}      ; Pick up status and return address.
    MSRNE   SPSR_cxsf, R0     ; Restore the status.
    LDMNE   sp, {R0 - lr}^    ; Get the rest of the registers
    NOP
    SUBSNE pc, lr, #4         ; and return and restore CPSR.
                              ; Insert "no next process code" here.
Non-ConfidentialPDF file icon PDF versionARM DUI0471M
Copyright © 2010-2016 ARM Limited or its affiliates. All rights reserved.