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
0x200007A40x0000000B11
0x200007A00x20000A14SRAM
hardware · 8 words
0x2000079CxPSR0x21000000
0x20000798PC0x080002F4flash
0x20000794LR0x080002C9flash
0x20000790r120x00000000—
0x2000078Cr30x0000000A10
0x20000788r20x20000C00SRAM
0x20000784r10x000000011
0x20000780r00x40021000periph
switch.S · 8 words
0x2000077Cr110x00000000—
0x20000778r100xE000E010core
0x20000774r90x00000000—
0x20000770r80x0000002A42
0x2000076Cr70x20000048SRAM
0x20000768r60x000000033
0x20000764r50x080004A0flash
0x20000760r40x00000064100
0x2000075C0xA5A5A5A5
0x200007580xA5A5A5A5
0x20000400base · fill below
PSP0x200007A0
stack_b · uart
MIRTOS_T_SLEEPING
40 of 256 words usedgrows ↓
0x20000C00top
0x20000BA40x0000004872
0x20000BA00x2000006CSRAM
hardware · 8 words
0x20000B9CxPSR0x01000000
0x20000B98PC0x08000348flash
0x20000B94LR0x0800031Dflash
0x20000B90r120x00000000—
0x20000B8Cr30x000000C0192
0x20000B88r20x00000000—
0x20000B84r10x0000004872
0x20000B80r00x40008000periph
switch.S · 8 words
0x20000B7Cr110x000000011
0x20000B78r100x00000000—
0x20000B74r90x40008000periph
0x20000B70r80x00000000—
0x20000B6Cr70x20000064SRAM
0x20000B68r60x0000001016
0x20000B64r50x08000510flash
0x20000B60r40x0000000D13
0x20000B5C0xA5A5A5A5
0x20000B580xA5A5A5A5
0x20000800base · fill below
MSP0x20014000 — 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
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
top-4xPSR0x01000000
top-8PCentry
top-12LRmirtos_thread_exit
top-16r120x00000000
top-20r30x00000000
top-24r20x00000000
top-28r10x00000000
top-32r0arg
popped by switch.S
top-36r110xA5A5A5A5
top-40r100xA5A5A5A5
top-44r90xA5A5A5A5
top-48r80xA5A5A5A5
top-52r70xA5A5A5A5
top-56r60xA5A5A5A5
top-60r50xA5A5A5A5
top-64r40xA5A5A5A5
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.