Skip to content

nvme: one STOPPED ANS handoff between boot stages on post-M4 chips - #380

Closed
DjDeveloperr wants to merge 9 commits into
mainfrom
fix/post-m4-ans-stopped-handoff
Closed

DjDeveloperr wants to merge 9 commits into
mainfrom
fix/post-m4-ans-stopped-handoff

Conversation

@DjDeveloperr

@DjDeveloperr DjDeveloperr commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Draft: nothing here has run on hardware yet. Owner direction: a minimal, generic post-M4 ANS reset, so that a stage that touches ANS leaves it in a clean state the next stage (or XNU) can start from, instead of today's per-chip power cycles.

What broke (measured on an M4 Mac mini, T8132, 2026-11-19)

  • On every post-M4 chip except T6050, Stage 1 1.0.0b13 ends nvme_shutdown() with pcie_ans_power_off(). That takes the shared APCIe PHY down and gates everything under ANS: APCIE_PHY_SW, APCIE_ST, APCIE_SYS_ST and ANS were all off afterwards.
  • Restarting from that state is flaky, and XNU then takes LLC Bus error (SLVERR) on the empty APCIe port 1.
  • When no stage has touched ANS, macOS boots fine under the hypervisor.
  • XNU cold-starts ANS from "SLEEP, CPU stop, ANS-only reset, iBoot's SART entries kept" (T8132, T8140).

The contract

docs/post-m4-ans-handoff.md defines one state between stages, STOPPED:

  • ANS stays powered;
  • the controller is shut down and disabled;
  • RTKit is put to SLEEP (IDLE as a fallback);
  • ANS alone is reset with its gate held on;
  • iBoot's SART entries are kept, the stage's own cleared;
  • the shared PHY and GP root ports are never touched.

The doc also lists the failure modes, written before the code.

nvme_init() starts from any of:

  • iBoot's session;
  • an older stage's running session;
  • STOPPED;
  • legacy OFF (nvme.sart= from b13/b8): only the ANS gates powered back on, the shared PHY brought up with no root port, iBoot's SART entries restored.

The next stage gets nvme.ans=stopped.

chips behaviour
T8132, T8140, T6040, T6041, T8142 the contract (T8142 moves off its storage-gate power cycle)
T6050 unchanged
T8152, M1–M3 unchanged

Also included: aura stages clear iBoot's failed-boot count on T8132 (the same commit as #378).

U-Boot and Linux (from reading the code)

  • U-Boot adopts only a running ANS, so Stage 2 starts ANS and hands it over running for every payload, as boot= already did.
  • Linux apple-nvme has a run-bit-clear path, so the proxy kboot route now hands it a STOPPED ANS. Before, that route passed apple,sart-entries, which Linux never reads.

Host checks

  • Stage 1, ESP Stage 2 and SPTM_VIRT build. tools/stage1_size.sh passes; Stage 1 headroom 17392 B (16624 on main).
  • clang-format 20.1.8 is clean.
  • pytest: no new failures against main's baseline.
  • test_ans_gates_adt against real J773g and J700 ADTs: the legacy repair powers on exactly the four gates measured off.

Not yet tested on hardware; open risks, most serious first

  1. Legacy OFF plus XNU on T8132 (the mini's current b13 Stage 1) may still hit the port-1 SLVERR. A storage-only power-on (hv9) did. The real fix for that machine is a Stage 1 built from this branch.
  2. Every first stage now cold-starts ANS. That adds a failure point before the first disk read.
  3. A new Stage 1 with an old Stage 2 doesn't boot. Ship Stage 2 first, or both together.
  4. The direct Linux kboot route is unexercised.
  5. T8142, T6040 and T6041 STOPPED cycles are unmeasured.

E2E plan (3 boots per row, logs kept)

  • M4 mini:
    • b13 Stage 1 → new Stage 2 → Linux;
    • b13 → new HV → macOS;
    • new Stage 1 → Stage 2 → Linux.
  • Neo: Stage 2 → Linux; HV → macOS.
  • M5 Pro: regression only.

Log lines to look for are in the doc.

Update 11-20: Ryan Murray's start order (d5eb5f6)

  • Every start from STOPPED now follows the order from Ryan's post-M4 start-from-off fix (c0a50c0, lab/j713-ans-reset-main, the J713's working 27.2 chain):
    1. read the ASC CPU status;
    2. without the reset-state bit (bit 3), stop the CPU and reset ANS, its SART entries put back;
    3. then the receive side on and the warm start (rtkit_boot_warm()), instead of the cold boot.
  • The rebase is onto main 4b96d54. The T8132 counter clear this branch carried is dropped: main's d1f6740 covers it.
  • Builds: Stage 1, ESP Stage 2 and SPTM-virt (clang, -Oz) all build. The host model now counts warm starts. pytest tests/python: 795 passed. The 9 failures are test_m3_defaults_optout updater cases and test_loadobjs, which fail on main too.
  • What it doesn't cover: on the J773g mini (11-20), Stage 1 1.0.0b13's idle before its chainload timed out (aura#305's timeout variant). After that, neither a warm start, a cold start after an ANS-only reset, nor this order revived the firmware in the same boot: CPU status 0x62 after the reset, without bit 3. Only a SoC reset helps there, so avoiding that idle (aura#305) remains its own fix.
  • Still untested on hardware; the rows above are unchanged.

🤖 Generated with Claude Code

@DjDeveloperr
DjDeveloperr force-pushed the fix/post-m4-ans-stopped-handoff branch from bd63b83 to d51aa57 Compare November 20, 2026 06:46
DjDeveloperr and others added 7 commits November 20, 2026 06:14
Post-M4 ANS reaches each boot stage in a different state today: iBoot's
live session, a session an older stage handed on running, or off with
the SART entries passed on (nvme.sart=). Off is the one that breaks.
A whole-APCIe-block power-off takes the shared PHY down with ANS. On
T8132 the restart from off is flaky, and after a full re-init XNU
panics on the empty port 1's window. T6050 is fine only because it
cycles its storage gates alone.

Write down the one state every stage leaves ANS in and starts it from:
STOPPED. In it ANS stays powered, the controller is off, the firmware
has acknowledged SLEEP, its CPU is stopped and ANS alone is reset, and
the SART holds iBoot's entries. XNU starts ANS cold from STOPPED
(the hypervisor's cold handoff, measured on T8140 and T8132).

The note also covers how nvme_init() gets to STOPPED from each state
it meets, what each payload gets, the failure modes listed before
the code is written, and the end-to-end rows.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es under one

Two pieces the post-M4 ANS handoff (docs/post-m4-ans-handoff.md) needs
outside the hypervisor build:

- pmgr_reset_held() leaves #ifdef SPTM_VIRT. It forces the device on
  for the reset, then puts its target and auto-enable back. A plain
  pmgr_reset() refuses an ANS that sits idle under auto-enable. The
  hypervisor's cold handoff reset ANS this way on T8140 and T8132,
  and XNU then started it cold.
- pmgr_adt_power_enable_under() is the inverse of
  pmgr_adt_power_disable_under(). It powers on, parents first, the
  gates of an ADT node that are the named device or depend on it, and
  leaves a gate that is on already as it is. For ANS under
  /arm-io/apcie it picks exactly the gates that 1.0.0b13's whole-block
  power-off took down: on T8132 APCIE_PHY_SW, APCIE_SYS_ST, APCIE_ST
  and ANS. APCIE_GP and APCIE_SYS_GP stay as iBoot left them.

Nothing calls the new function yet.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…here

From 1.0.0b13 a post-M4 stage done with ANS powered it off with the
whole APCIe block (pcie_ans_power_off()) and passed its SART entries
on as nvme.sart=. The next stage powered the block back with a full
pcie_init() and warm-started the firmware. On T8132 that restart is
flaky ("ANS stays off until a reboot", hv7). When it works, XNU then
reads the empty port 1's window and panics with an LLC SLVERR (hv3,
hv5). Every stage also met ANS in a different state.

Every SoC with a split-BAR ANS but T6050 now follows one contract
(docs/post-m4-ans-handoff.md). That is T8132, T8140, T6040, T6041 and
T8142.

nvme_shutdown() leaves ANS STOPPED, and never powers anything off:
- the controller gets a normal shutdown if this stage wrote, then
  CC.EN=0;
- RTKit is put to SLEEP (IDLE if it won't acknowledge, a plain stop
  if neither) and the CPU is stopped;
- the stage's buffers and their SART entries go;
- ANS alone is reset with its gate held on, and any SART entry the
  reset takes is written back.
iBoot's SART entries stay in hardware.

nvme_init() reads PMGR's ANS power state before any ANS register, then
takes ANS to STOPPED from whatever it finds:
- A running session gets its controller turned off and a SLEEP, and
  is reset. For iBoot's session (no aura queue setup) the one-time
  RTKit handshake comes first, since it answers nothing before it. An
  older stage's session handed on running is asked directly, woken
  if it doesn't answer, and its buffers' SART entries are cleared.
- STOPPED is reset again.
- Off, from an older stage that passed nvme.sart=: ANS's side is
  powered back (pcie_ans_power_on()), and iBoot's entries are put
  back without that stage's buffers.
Then every stage starts ANS the same way: the mailbox receive side
on, the run bit, the RTKit handshake. This is the start the
hypervisor's cold handoff probed on T8132 and T8140 (HELLO
unprompted).

The rest of the handoff:
- The stage tells the next one with nvme.ans=stopped (payload.c,
  chainload.c; secboot passes the line, as it names no address).
  nvme.sart= is still produced for T6050 and still accepted from
  older stages.
- U-Boot adopts only a running session. Its probe needs RUN and
  BOOT_STATUS OK. So payload_run() starts a STOPPED ANS for the
  payload, as it did for one passed off
  (nvme_post_m4_needs_start()).
- kboot passes apple,sart-entries only for an ANS that will be off,
  which on these chips is now only one an older stage powered off.
- nvme_cold_handoff() reuses the same pieces (nvme_session_stop(),
  nvme_ans_reset(), the SART classification) and powers back an ANS
  that an older stage powered off, instead of failing on it.

T6050 keeps its measured path unchanged: adopt, suspend, idle, storage
gates off, warm start. T8152 and M1-M3 don't change.
nvme_ans_power_off() keeps only T6050's branch. The T8142 clause and
the T8142 adoption went; T8142 is now STOPPED like the others.

test_nvme_adoption models ANS's power and run bit. Its T8142 cases
check the STOPPED cycle: SLEEP then the held reset, no power-off, and
the next start from STOPPED. The other harnesses follow the renames.

Not run on hardware yet. Builds and the host tests pass. The next
commit brings ANS back without touching any root port.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… port

1.0.0b13's whole-block power-off (pcie_init_controller(..., ans_off))
puts the shared APCIe PHY into reset, drops its clock requests and
powers off every gate that depends on ANS. On T8132 that leaves
APCIE_PHY_SW, APCIE_ST, APCIE_SYS_ST and ANS off, with APCIE_GP and
APCIE_SYS_GP on. Its counterpart, pcie_ans_power_on(), was a full
pcie_init(), so it also brought up the GP root ports. They then
differed from what iBoot left. XNU read the empty port 1's window and
panicked with an LLC SLVERR. Port 1 never reaches RUN under aura.

Since the previous commit no stage takes that path. What remains is
bringing back an ANS that an older stage left off:

- pcie_ans_power_on() without a storage gate table now runs
  pcie_init_controller() with storage_only:
  - only the gates that depend on ANS go back on
    (pmgr_adt_power_enable_under());
  - the shared PHY comes up as for a full init;
  - no root port is touched;
  - the controller still reads as not initialized, so kboot's
    pcie_init() brings the root ports up for Linux as before.
  On real ADTs the gates are ANS, APCIE_ST, APCIE_SYS_ST and
  APCIE_PHY_SW on both J773g (T8132) and J700 (T8140); see
  test_ans_gates_adt.
- pcie_ans_power_off() refuses on a chip without a storage gate table,
  so no caller can take that PHY down again. The ans_off branch and
  pmgr_adt_power_disable_under() go.
- T6050's and T8142's storage gate tables are unchanged.

Not run on hardware: on T8132 this is the WIP pcie_ans_power_on_storage()
of the hvtrace branch, with the GP gates left alone too.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… STOPPED

Only T8140 started XNU with the cold handoff by default. Elsewhere the
runner kept "quiesced" in the guest's iop-ans-nub, so XNU tried to wake
iBoot's quiesced session with one INIT. On T8132, T6040, T6041 and
T8142 an aura stage that used ANS now leaves it STOPPED: the CPU is
stopped and ANS reset. There is no session left to wake there.

The cold handoff (sleep) is now the default on those SoCs too. On a
STOPPED ANS it only checks: nothing runs, no aura SART entry is left,
and ANS is reset and probed. AURA_ANS_COLD=off still gives XNU an
untouched iBoot session to wake. T6050 keeps "off" by default.

docs/sptm-virt-ans-cold-handoff.md gets the new defaults and a section
on an ANS that an older stage powered off.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Read from the U-Boot and Linux trees in use:

- U-Boot's nvme_apple adopts only a running session (RUN and BOOT_STATUS
  OK). It doesn't bind the T8142 or T6050 compatibles. It can't start
  from STOPPED, which is why a stage starts ANS for every payload.
- Linux's apple-nvme starts an ANS whose run bit is clear on every
  post-M4 compatible. On all but T8132 it relies on the bootloader's
  reset. It turns the mailbox receive side on only for T8140, and it
  must not get apple,rtkit-handoff for a stopped ANS.

None of this is tested on hardware.

Also list the functions that implement each part.

Signed-off-by: DjDeveloperr <hey@djdev.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A post-M4 ANS CPU that was stopped without a reset since it last ran
doesn't start again from the run bit: its firmware sends no HELLO, then
or later (aura c0a50c0, Ryan Murray, from the J713's 27.2 chain, where
U-Boot then gets ANS running). Every start from STOPPED now reads the
ASC CPU status first; without the reset state (bit 3) it stops the CPU
and resets ANS, its SART entries put back, before the run bit. Then the
mailbox's receive side goes on and the start is the warm one,
rtkit_boot_warm(), as after a power loss, instead of the cold boot with
its wake message: the order that works on the J713.

On a J773g mini (27.2, 11-20) Stage 1 1.0.0b13's idle before its
chainload timed out ("IOP power transition timed out", aura#305), and
neither a warm start, a cold start after an ANS-only reset, nor this
order brought that firmware back in the same boot: CPU status 0x62
after the reset, no reset-state bit. Only a SoC reset recovers that
case. This change is for the clean STOPPED handoff, not for it.

The host model counts the warm starts: iBoot's session gets one cold
boot for its handshake, every start from STOPPED one warm start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr
DjDeveloperr force-pushed the fix/post-m4-ans-stopped-handoff branch from d51aa57 to d5eb5f6 Compare November 20, 2026 11:20
@RyanTheTide

RyanTheTide commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Data point from a 13" M4 MacBook Air (J713, T8132) on the 27.2 stub, for the legacy-OFF path. It doesn't test this branch. (Edited 2026-11-21: replaced the first version, which relied on a photo of the panel, with the console log read from the proxy over USB.)

Stage 1 is aura main 3d517e6 (dev build), which still hands ANS on powered off with nvme.sart=. Our first Stage-2-only attempt (lab/j713-ans-reset-main c0a50c0) reads the ASC CPU status before the run bit. If the reset-state bit is clear, it stops the CPU, runs pmgr_reset(die, "ANS"), rewrites the SART entries and starts it. If there's still no answer, it resets once more and retries.

Where it failed. It was the boot after a failed aura-try trial, on the configured c0a50c0 (the trial itself most likely died earlier, in aura#320's MCC init; see below). Console buffer:

nvme: entry RUN=0 BOOT_STATUS=0x0 saved SART entries=9
nvme: entry CC=0x470000 CSTS=0x0 AQA=0x3f003f ...
nvme: ANS CPU status 0x6a before the run bit
rtkit(nvme): booting with version 12
rtkit(nvme): unknown system endpoint 0x0a
rtkit(nvme): timed out waiting for IOP power state 0x20        (+2.000 s)
nvme: ANS didn't answer the run bit (status 0x6e): resetting it, one more try
pmgr: resetting device 0.ANS
nvme: ANS reset: status 0x6a
rtkit(nvme): expected HELLO but got message with type 0x07
nvme: ANS didn't start from off; it stays down until a reboot

So the firmware did start, but its IOP power report came later than RTKit's 2 s wait. After the ANS-only reset, the first message read was a stale one (type 0x07), not HELLO. An ESP Stage 2 has no watchdog, so the Mac stayed in the proxy until a host rebooted it over the USB tether.

What fixed it on this machine: #394 (main-based, 2 commits). It drains stale ANS messages before the warm start and gives the IOP power report 6 s instead of 2. Its lab form is now the J713's configured Stage 2. On normal boots the IOP power state arrives about 68 ms after HELLO. The slow case looks specific to the state after a watchdog reset. Warm reboots since then pass.

Separate T8132 issue we hit on the same night: with aura#320 in the tree, mcc_init() is fatal on the J713. amcc_aperture_count is 4 bytes (value 2), as on the J773g. c0a50c0, which predates it, logs MCC: Failed to get mcc count! and carries on.

For this PR: the legacy-OFF start on T8132 worked once stale messages were drained and the IOP power wait was long enough. We aren't proposing c0a50c0 itself (it has no PR). The J713 can run this branch as an ESP Stage 2 one-shot once it's ready for hardware: say so here and we'll queue it.

Assisted-by: LLM (Claude:Opus 5.5)

@RyanTheTide

Copy link
Copy Markdown
Contributor

Blockers: 4.

Main has since taken a different handoff model for these chips, so this branch needs to be re-done against main or closed.

  1. Superseded design. Since this was opened, main merged nvme: T8132: keep NVMe across warm reboots on the M4 MacBook Air #305 and nvme: T8132 hands ANS on powered off; live handoff only for a session that came running #410 (3b835c2): on T6040/T6041/T8132 a session that came running goes on live, and one started from off goes on powered off with nvme.sart= (nvme_post_m4_keeps_live(), main src/nvme.c:484). It also merged nvme, rtkit: post-M4 ANS warm start: drop stale messages, wait 6 s for the IOP power report #394 (stale-message drain plus a 6 s IOP power wait, main src/nvme.c:547). The STOPPED contract here contradicts the first, and the head carries neither the drain nor the longer wait. The J713 log in the earlier comment shows that a warm start without the drain fails. The branch conflicts and has not run on any Mac. If STOPPED is still wanted, please open a fresh PR on main that says why it should replace nvme: T8132 hands ANS on powered off; live handoff only for a session that came running #410's model, with rows.

  2. Chip gates where the ADT already carries the answer. src/nvme.c:518 and :524 pick the contract with chip_id != T6050, although nvme-secure-bar is already read on the same line. proxyclient/tools/run_guest_sptm.py:205 keeps its own ANS_STOPPED_CHIPS list of chip ids, which can drift from the C side. Key both on a property of the ANS node, and make the Python tool read that property from the ADT.

  3. Stage ABI break with no fallback. A new Stage 1 passes nvme.ans=stopped (src/nvme.c:1497). An older Stage 2 doesn't parse it, finds ANS powered but not running, and can't start it. The PR body confirms that combination doesn't boot. A Stage 1 must keep emitting something every shipped Stage 2 can start from (today's nvme.sart=) until a release has carried the consumer.

  4. Attribution. All seven commits carry Co-Authored-By: Claude .... Commits should have the human Signed-off-by only. The subject of d5eb5f6 names a person rather than the change. The PR body ends in a "Generated with Claude Code" line instead of an Assisted-by: line.

HW row: on current head, rebased: J773g and J713 (T8132), Neo (T8140): b13 Stage 1 -> new Stage 2 -> Linux, new Stage 1 -> old Stage 2, and HV -> macOS, 3 boots each, NVMe up every time with no SLVERR. Plus a T6050 and a T6041 regression boot. All with 0 failed units and 0 new kernel errors.

Assisted-by: LLM (Claude:Opus 5.5)

github-actions Bot and others added 2 commits November 22, 2026 11:37
Aurora agent policy: remove assistant instruction files that are
not copies of .github/aurora/AGENTS.md on main, and keep the copies on policy
branches identical to it.
Revert the automated agent-policy bot commit a90f0c2 that deleted assistant instruction files on this PR branch. The policy workflow has since been updated (aura#475) to stop pushing to PR branches.

Signed-off-by: Dj <43033058+DjDeveloperr@users.noreply.github.com>
@RyanTheTide

Copy link
Copy Markdown
Contributor

@DjDeveloperr since this was opened, main took a different handoff model for these chips (#305 and #410, 3b835c2), so this branch would need to be redone against main. The review with details is #380 (comment).

If nothing here still needs to land, I'll close it after 2026-11-28 03:00 AEDT (24 hours from now). The branch stays, so it can be reopened. If something does still need to land, say so here and I'll leave it open.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Closing as superseded, per #380 (comment) (no objection in 24 hours). The branch stays, so this can be reopened if something here still needs to land.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants