Deep dive

m33mu

m33mu is an open-source Cortex-M33 emulator with TrustZone support. wolfTrust and wolfBoot boot their whole secure chain on it in CI. When firmware failed under emulation but not on hardware, I found the bugs in the emulator's CPU model and fixed them upstream.

Role: contributor C, Armv8-M architecture m33mu/m33mu
01 / Problem and boundary

An emulator bug looks exactly like a firmware bug

Emulated CI lets every pull request boot the full secure chain without a board, but only if the emulator behaves like the silicon. A TrustZone runtime depends on exact architectural rules: which stack an exception returns to, which security state a branch lands in, which memory is Secure. If the emulator gets one wrong, the firmware crashes and you go hunting for a bug in your own code that is not there. I did not write m33mu. My work was proving where its model disagreed with the Arm Architecture Reference Manual or the chip, then fixing it with a test that fails on master.

02 / Architecture and tradeoffs

Follow the architecture, not the shortcut

Each fix chose the rule the hardware follows over the one that was easier to model. For the SAU fix, a Secure MPCBB block now upgrades only a Non-secure attribution and leaves Secure and Non-secure-callable regions alone, because the SG instruction fetch is itself a Secure transaction #18. That matched the i.MX RT700 board, where secure veneer calls worked but the emulator faulted them.

For the UART, the interrupt is now raised at the register write instead of up to 64 cycles later at the next host poll, and console output is never dropped or reordered #21. That costs a little emulator speed and buys runs that behave the same every time, which is what a CI gate needs. It fixed flaky wolfTrust emulator runs.

03 / What I built

Main changes

04 / Debugging

The ELSE arm that never ran

While running the Arm PSA Architecture Test Suite (FF-M IPC v1.8) against wolfTrust under the emulator, a Zephyr guest crashed on a branch through a data pointer. The same image ran fine on hardware, and the 89-test suite wedged partway through. I traced it with m33mu's stack and undefined-instruction traces (M33MU_STACK_TRACE=1, M33MU_UNDEF_TRACE=1) down to a single stacked xPSR value.

A Secure SysTick had preempted the guest between the two arms of an if-then-else (ITE) block in Zephyr's printk path. On exception entry the CPU saves the IT-block state (ITSTATE) so it can resume the block correctly. The emulator's itstate_advance() shifted only the 4-bit mask and left bits 7:4 alone, so the current-condition bit, IT[4], never moved as the block advanced. The Arm ARM's ITAdvance() shifts ITSTATE[4:0] as one 5-bit field. The stacked state therefore still carried the THEN condition, and after the return the ELSE arm ran under the wrong condition, skipped a conditional load that real hardware executes, and left a stale register that crashed later.

The fix advances ITSTATE[4:0] as one field and preserves bits 7:5. I added a test that walks ITE, ITT and ITETE blocks step by step, and a second one that enters and returns from an exception mid-ITE block to prove the restored state decodes back to the ELSE condition. The exception-entry bug in #16 came out of the same test suite: a Secure SysTick landing inside the Non-secure SysTick handler cleared the stack-select bit of a Secure thread parked mid-SVC, so the next return used the wrong stack.

m33mu before the fixArchitecture (and after the fix)
Stacked ITSTATE, preempted before the ELSE arm0x080x18
Condition used for the ELSE arm on returnTHEN conditionELSE condition
Conditional load in Zephyr printkskippedexecuted
89-test PSA suitewedgesruns to completion

Fixed in #17.

05 / Verification

How it was checked

PREvidence
#17itstate_advance_test.c fails on master (0x08 for 0x18); ctest 71/71 with the fix
#16exception_spsel_parked_thread_test.c fails on master; ctest 69/69, full firmware suite clean
#18New NSC-in-MPCBB test; ctest 76/76; firmware on STM32H563, H5F4, U585 and L552
#21Two UART tests that fail on master; ctest 77/77
06 / Limits and next steps

What is not done yet

m33mu still does not fault a Non-secure entry into a Non-secure-callable region at an instruction other than SG, which real hardware does. The tail-chaining paths are compiled out; I corrected them so re-enabling them will not bring the SPSEL bug back, but CI does not exercise them yet.

All my merged pull requests 10 in m33mu/m33mu
  • #23Stop checking MCXN947 RAM against the flash block checker
  • #21Add deterministic interrupt timing and a lossless --uart-stdout console
  • #20Allow overriding DBGMCU_IDCODE with M33MU_STM32H5_IDCODE
  • #19model the DBGMCU_IDCODE as a read-only per-part value for H5
  • #18keep SAU NSC callable inside a Secure MPCBB block
  • #17Fix ITSTATE advance losing the current condition bit
  • #16Fix Armv8-M cross-domain EXC_RETURN and CONTROL.SPSEL exception bookkeeping
  • #13Model STM32H5 FLASH ECCDETR clear behavior
  • #10Add STM32H5 96-bit unique device ID region at 0x08FFF800
  • #11Fix the USART RCC gate test to match alias-shared clock state

all work view the repository