MiRTOS switch bench

NUCLEO-G474RE · Cortex-M4 · ARMv7E-M · FPU off · EXC_RETURN 0xFFFFFFFD · SysTick 1 kHz

One PendSV context switch, stepped word by word — what the hardware stacks, what switch.S saves, and where all thirty-two of those bits land in SRAM1.

handler modethread mode
g_current → tcb_ag_current → tcb_b
00/12thread A running

THREAD MODE — blink, prio 3blink is on the CPU. CONTROL.SPSEL = 1, so SP means PSP — the thread runs on its own 1 K stack in SRAM1, while the kernel's MSP sits untouched at the top of SRAM1. tcb_a.sp is stale right now: the live pointer is in the PSP register, and the TCB copy only becomes valid again once PendSV saves it.

modethread
SP isPSP
g_currenttcb_a
LR holds0x080002C9
this step touchesFLASH→SRAM1→

Registers

thread mode
caller-saved · stacked by hardware
r00x40021000periph
r10x000000011
r20x20000C00SRAM
r30x0000000A10
r120x00000000—
callee-saved · stacked by switch.S
r40x00000064100
r50x080004A0flash
r60x000000033
r70x20000048SRAM
r80x0000002A42
r90x00000000—
r100xE000E010core
r110x00000000—
special
MSP0x20014000SRAM
SPPSP0x200007A0SRAM
LR0x080002C9flash
PC0x080002F0flash
xPSR0x21000000
TNZCVthread
system
BASEPRI0x00
nothing masked
CONTROL0x02
SPSEL 1 · PSPprivileged
ICSR0x00000000
PENDSVSETnone active

stack_a · blink

MIRTOS_T_RUNNING
24 of 256 words usedgrows ↓
0x20000800top
0x0000000B11
0x20000A14SRAM
hardware · 8 words
xPSR0x21000000
PC0x080002F4flash
LR0x080002C9flash
r120x00000000—
r30x0000000A10
r20x20000C00SRAM
r10x000000011
r00x40021000periph
switch.S · 8 words
r110x00000000—
r100xE000E010core
r90x00000000—
r80x0000002A42
r70x20000048SRAM
r60x000000033
r50x080004A0flash
r40x00000064100
0xA5A5A5A5
0xA5A5A5A5
0x20000400base · fill below
PSP0x200007A0

stack_b · uart

MIRTOS_T_SLEEPING
40 of 256 words usedgrows ↓
0x20000C00top
0x0000004872
0x2000006CSRAM
hardware · 8 words
xPSR0x01000000
PC0x08000348flash
LR0x0800031Dflash
r120x00000000—
r30x000000C0192
r20x00000000—
r10x0000004872
r00x40008000periph
switch.S · 8 words
r110x000000011
r100x00000000—
r90x40008000periph
r80x00000000—
r70x20000064SRAM
r60x0000001016
r50x08000510flash
r40x0000000D13
0xA5A5A5A5
0xA5A5A5A5
0x20000800base · fill below

MSP 0x20014000 — the kernel stack at the top of SRAM1, where every handler runs. It is never one of these two, and the switch never writes to it.

PendSV_Handler

port/armv7em/switch.S
PendSV_Handler:
mrs r0, psp
isb
stmdb r0!, {r4-r11}
ldr r3, =g_current
ldr r2, [r3]
str r0, [r2] @ sp is field 0
push {r3, lr} @ lr = EXC_RETURN
bl sched_pick
pop {r3, lr}
ldr r2, [r3]
ldr r0, [r2]
ldmia r0!, {r4-r11}
msr psp, r0
bx lr @ 0xFFFFFFFD

Thread control blocks

28 B · align 4
tcb_a 0x20000048← g_current
0
sp0x20000738 stale
4
stateMIRTOS_T_RUNNING
prio3
base_prio3
padfree
8
wake_tick0x00000000
12
next0x00000000
16
stack_base0x20000400
20
stack_words256
24
name"blink"
offset 0 is the saved PSP — the one word switch.S writes
tcb_b 0x20000064← g_current
0
sp0x20000B60
4
stateMIRTOS_T_SLEEPING
prio2
base_prio2
padfree
8
wake_tick0x000004D2
12
next0x00000000
16
stack_base0x20000800
20
stack_words256
24
name"uart"
offset 0 is the saved PSP — the one word switch.S writes
28
24
20
16
12
8
4
0
bit 31 · priority 0 · highestready_bitmap = 0x11000000bit 0 · priority 31

next = 31 − __CLZ(ready_bitmap)blink runs at priority 3 (bit 28); idle is always ready at 7 (bit 24). uart is asleep, so bit 29 is clear.

A thread that has never run

port_stack_init()

The switch has no special case for a brand-new thread. mirtos_thread_create() fabricates the same sixteen words on its stack that a real switch would have left there, so the first entry into a thread is an ordinary exception return — the hardware unstacks a frame that no exception ever pushed.

xPSR carries only the Thumb bit. PC is the thread function. LR is mirtos_thread_exit, so a thread that returns lands somewhere deliberate instead of off the end of the world. Saved sp points at the r4 slot, 64 bytes below an 8-byte-aligned top — so it is 8-byte aligned too, and every switch after it keeps that for free.

unstacked by hardware
xPSR0x01000000
PCentry
LRmirtos_thread_exit
r120x00000000
r30x00000000
r20x00000000
r10x00000000
r0arg
popped by switch.S
r110xA5A5A5A5
r100xA5A5A5A5
r90xA5A5A5A5
r80xA5A5A5A5
r70xA5A5A5A5
r60xA5A5A5A5
r50xA5A5A5A5
r40xA5A5A5A5

Where it all lives

Four address spaces, high address at the top. Blocks are drawn on a compressed scale so every object is readable — the breaks between them carry the real size of the empty space. Whatever the current step reaches into lifts off the column.

flashkernel objectsthread Athread B and the MSP stackcrash recordtouched this step
FLASHrx512 K · 1.2 K used
0x08080000
510.8 K free
0x080004E0
_sidata (.data LMA)4 B
0x080004E0 – 0x080004E31 word
0x080004DC
.init_array4 B
0x080004DC – 0x080004DF1 word
0x08000480
.rodata92 B
0x08000480 – 0x080004DB23 words
0x080001D8
.text680 B
0x080001D8 – 0x0800047F170 words
0x08000000
.isr_vector472 B
0x08000000 – 0x080001D7118 words
0x08000000
SRAM1xrw80 K · 6.6 K used
0x20014000
0x20013000
.stack (MSP)4 K
0x20013000 – 0x20013FFF1024 words
72.5 K free
0x20000C00
idle_stackplan512 B
0x20000C00 – 0x20000DFF128 words
0x20000800
stack_b (uart)plan1 K
0x20000800 – 0x20000BFF256 words
0x20000400
stack_a (blink)plan1 K
0x20000400 – 0x200007FF256 words
896 B free
0x20000064
tcb_b (uart)plan28 B
0x20000064 – 0x2000007F7 words
0x20000048
tcb_a (blink)plan28 B
0x20000048 – 0x200000637 words
0x2000002C
tcb_idleplan28 B
0x2000002C – 0x200000477 words
0x20000028
g_tickplan4 B
0x20000028 – 0x2000002B1 word
0x20000008
g_ready[8]plan32 B
0x20000008 – 0x200000278 words
0x20000004
g_ready_bitmapplan4 B
0x20000004 – 0x200000071 word
0x20000000
g_currentplan4 B
0x20000000 – 0x200000031 word
0x20000000
SRAM2xrw16 K · 256 B used
0x20018000
0x20017F00
.noinit (crash record)256 B
0x20017F00 – 0x20017FFF64 words
15.8 K free
0x20014000
CCMRAMxrw32 K · unused in v1
0x10008000
32 K free
0x10000000

Not to scale between blocks — a proportional drawing of 512 K of flash would render .rodata a tenth of a pixel tall. Sizes and addresses are exact; only the empty runs are compressed.