Skip to content

soc: apple: sep: implement xars shutdown unmount and flush barrier - #3296

Open
RyanTheTide wants to merge 5 commits into
mainfrom
feat/sep-xars-barrier
Open

RyanTheTide wants to merge 5 commits into
mainfrom
feat/sep-xars-barrier

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What

Implement a typed Linux kernel driver client for the Apple Secure Enclave xART control endpoint (xars, endpoint 0x10) to run the orderly shutdown durability barrier before power-off or reboot:

  • Typed endpoint client (drivers/soc/apple/xars.rs): Discovers xars by endpoint name ("xars") and advertised buffer layout (0x02020202, two 16 KiB pages each way) with no SoC, board, or firmware version gates. Registers out-of-line buffers lazily on first use and sends zero xars messages during boot, idle, display sleep, or ordinary system sleep (s2idle/suspend).
  • Orderly shutdown barrier (drivers/soc/apple/pm_shim.c, pm.rs, sep.rs): Registers a blocking reboot notifier (SYS_RESTART, SYS_HALT, SYS_POWER_OFF) that runs before device_shutdown() while the mailbox, DART, and NVMe gigalocker handle are still live. While xarm continues servicing and fsyncing enclave-initiated writes, the barrier transmits:
    1. notify_storage_unmount (op 0x0d, 0 OOL bytes), then
    2. non-forced flush_cached_state (op 0x0c, 1-byte 0x00 payload),
      and closes the host xART store only after both operations succeed.
  • Closed command set & fail-closed policy: The sendable command set is closed to ShutdownOp::{NotifyStorageUnmount, FlushCachedState} — no raw opcode selector, no forced flush, no session-seed erasure, and no userspace ABI. Any timeout, refusal, or malformed reply after submission is treated as indeterminate (no blind retry, no flush after an indeterminate unmount, xarm stays open until normal teardown, bounded by 5 s per operation / 10 s total budget so shutdown never hangs).
  • Diagnostics & tooling: Exposes passive read-only status at diag/xars (Documentation/ABI/testing/sysfs-driver-apple-sep, diag.rs, diag_shim.c) and updates tools/aurora-sep/aurora-sep (status) and its unit tests.

Tested

Tested:

  • Unit / KUnit (arm64 QEMU, exact commit 74651281458b6e): 328/328 apple_sep* tests PASS, including 11/11 apple_sep_xars subtests (discovery/layout gating, request encoding, reply validation, monotonic tag tracking, clean unmount+flush barrier, indeterminate unmount/flush handling, read-only store gating, budget exhaustion). Zero new compiler warnings.
  • Host Python tests (sep-source.yml suite): 131/131 tests PASS (plus 13/13 -O passes), including test_aurora_sep_status.py, test_diag_policy.py, and test_log_ratelimit.py.
  • Hardware row 1 — Mac mini M2 (J473, T8112, warm SEP boot, variant5, xart_guard=1, default cmdline, exact commit 74651281458b6e): 5 consecutive reboots + 12 s s2idle (rtc1) PASS. Live netconsole over wired Ethernet captured all 5 shutdown barriers completing cleanly in 1-5 ms; diag/xars=ready, diag/keystore=open, diag/xarm_failures=0, diag/unhandled_messages=0, hwrng 32 B, and xART gigalocker healthy (170 slots, 12 live records, max revision 566, 0 malformed, 0 duplicate, 0 repaired) on every cycle.
  • Hardware row 2 — MacBook Pro M1 Pro (J316s, T6000, cold SEP boot, sepos13, xart_guard=0, Touch ID active, default cmdline, exact commit 74651281458b6e): 5 consecutive reboots + 12 s s2idle (rtc1) PASS. aurora-sep status PASS (pass 27 warn 1 fail 0) on all 5 cycles; 2 enrolled Touch ID identities survived all 5 reboots (diag/touchid=ready, diag/restore=restored, diag/matching=usable), diag/xars=ready, diag/xarm_failures=0, diag/unhandled_messages=0, and xART gigalocker healthy (170 slots, 16 live records, max revision 6430, 0 malformed, 0 duplicate, 0 repaired) on every cycle.
Detailed test logs and netconsole shutdown barrier captures

J473 (T8112, warm SEP boot, xart_guard=1) — 5 reboots + s2idle

Live netconsole shutdown captures across all 5 consecutive reboots (7.3.0-aurora-sepxars1+, commit 74651281458b6e):

[   35.844550] apple_sep 25e400000.sep: xars: shutdown barrier complete (unmount + flush, 0 xarm writes in 5 ms)
[   32.781490] apple_sep 25e400000.sep: xars: shutdown barrier complete (unmount + flush, 0 xarm writes in 5 ms)
[   39.690341] apple_sep 25e400000.sep: xars: shutdown barrier complete (unmount + flush, 0 xarm writes in 1 ms)
[   35.950345] apple_sep 25e400000.sep: xars: shutdown barrier complete (unmount + flush, 0 xarm writes in 5 ms)
[   25.973720] apple_sep 25e400000.sep: xars: shutdown barrier complete (unmount + flush, 0 xarm writes in 1 ms)

Cycle 1 s2idle (rtcwake -d rtc1 -m freeze -s 12):

before: sleeps=0 xars=ready unhandled=0
after:  sleeps=1 xars=ready unhandled=0 success=0->1
hwrng_bytes=32

Every cycle (1-5):

xART: APFS-resolved .gl extent (base 0x4d4000); 170 slots, 12 live records, max revision 566, 0 malformed, 0 duplicate, 0 repaired; writes ENABLED
diag.xars=ready diag.keystore=open diag.xarm_failures=0 diag.unhandled_messages=0 diag.xart_guard_refusals=0 hwrng_bytes=32

J316s (T6000, cold SEP boot, xart_guard=0, Touch ID active) — 5 reboots + s2idle

Cycle 1-5 aurora-sep status:

PASS xars                 ready  (xART shutdown unmount and flush barrier is armed)
PASS touchid              ready  (Touch ID activated and the sensor online)
PASS restore              restored  (enrolled fingerprints restored)
PASS matching             usable  (a match can save its state)
PASS xarm-failures        0
PASS unhandled-messages   0
aurora-sep: PASS  pass 27 warn 1 fail 0

Cycle 1 s2idle (rtcwake -d rtc1 -m freeze -s 12):

before: sleeps=0 xars=ready unhandled=0
after:  sleeps=1 xars=ready unhandled=0 success=0->1
hwrng_bytes=32

Every cycle (1-5):

xART: APFS-resolved .gl extent (base 0x4d4000); 170 slots, 16 live records, max revision 6430, 0 malformed, 0 duplicate, 0 repaired; writes ENABLED
Touch ID: restored 2 enrolled identity/identities; enrolment survived reboot

Arm64 QEMU KUnit (apple_sep*)

Testing complete. Ran 328 tests: passed: 328

Risk

Risk: two-way · all Macs with Apple SEP (apple_sep) that advertise xars. Low-to-moderate (touches SEP shutdown ordering); reverting restores the previous shutdown path.

  • Mitigation: Runs only from the blocking reboot notifier (SYS_RESTART, SYS_HALT, SYS_POWER_OFF), never on panic, emergency restart, or suspend. Strictly gated on endpoint discovery ("xars" with layout 0x02020202) and writable xART store. Bounded by 5 s per operation / 10 s total budget; any error or timeout fails closed without retrying or hanging shutdown, and keeps xarm alive until teardown.
  • No destructive operations: Session-seed erase and forced flush are not implemented or exposed; existing macOS records remain protected by xart_guard.

Not yet

  • Session-seed erasure (op 0x02, used by macOS only when deleting an APFS volume group) is intentionally omitted until a dedicated, explicitly gated OS-uninstall flow requires it.
  • Testing on an active Touch ID match/enrolment write immediately before shutdown (to observe non-zero xarm write counts drained inside the barrier) is not yet exercised in this automated reboot loop; the interleaved xarm-during-barrier path is covered by KUnit and verified against the live single-threaded rx_work FIFO design.

Add a typed client for the Secure Enclave xART control endpoint (xars,
endpoint 0x10) that runs the orderly shutdown durability barrier from a
blocking reboot notifier (SYS_RESTART, SYS_HALT, SYS_POWER_OFF) before
device_shutdown().

The client registers its two-page out-of-line buffers lazily on first
use and sends nothing at boot, idle, display sleep or ordinary system
sleep. During an orderly shutdown, while xarm remains live to service
and fsync enclave-initiated writes, the barrier sends
notify_storage_unmount (op 0x0d, zero OOL bytes) followed by non-forced
flush_cached_state (op 0x0c, 1-byte false payload), and closes the host
xART store only after both operations succeed.

The sendable command set is closed to those two lifecycle operations
(no raw selector, no forced flush, no seed erasure, no userspace ABI).
Any timeout, refusal or malformed reply is treated as indeterminate:
the barrier does not retry blindly, does not send flush after an
indeterminate unmount, keeps xarm open until teardown and allows
shutdown to proceed within a 10-second total budget (5 seconds per op).

Expose passive status in diag/xars and report it in aurora-sep status,
with KUnit coverage for discovery, wire encoding, reply validation, tag
monotonicity and all shutdown-barrier state transitions.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide
RyanTheTide requested review from a team as code owners November 26, 2026 18:36
@github-actions github-actions Bot added area:platform Area from CODEOWNERS (bot) ci-failing A check failed at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Merge readiness at 461da6d9: not ready yet

Check Detail
CI at head no failing: host-tests, KUnit ane-boot, rust-build, KUnit dcp-topology, KUnit asahi, KUnit spi-hid-apple
Review no needs one approving review from Core or Omarchy Mac (not the author)
Requested changes ok none
Hardware row at head no no line with the head SHA 461da6d9, a Mac and a result in the PR body or a comment
Conflicts ok none
PR text ok Risk: two-way · all Macs with Apple SEP (applesep) that advertise xars. Low-to-moderate (touches SEP shutdown ordering); reverting restores the previous shutdown path. - Mitigation: Runs only from the blocking reboot notifier (SYS_RESTART, SYS_HALT, SYS_POWER_OFF), never on panic, emergency restart, or suspend. Strictly gated on endpoint discovery ("xars" with layout 0x02020202) and writable xART store. Bounded by 5 s per operation / 10 s total budget; any error or timeout fails closed without retrying or hanging shutdown, and keeps xarm alive until teardown. - No destructive operations: Session-seed erase and forced flush are not implemented or exposed; existing macOS records remain protected by xart_guard.

Areas: area:platform

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 27, 2026
Resolve the remove() conflict with the HDCP client (#3256): wake
both the xars and hdcp waiters. The auto-merged parts keep the
hdcp and xars OOL buffers and discovery hooks side by side.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Forward-merged main (fcb12427) into this branch: new head 87974c3a (merge commit, no rebase, no force push).

Review items: there are no review comments or requested changes on this PR yet. It still needs one approving review from Core or Omarchy Mac.

pr-text: the Risk line now says two-way, as the readiness check asked. I edited the PR body; the code is unchanged.

The red CI at 74651281: every listed job (apple-build, host-tests, source, pr-text, KUnit, rust-build) ended cancelled, with no failed steps, during the overnight CI capacity hold. None of them was a real failure of this PR or of main. The push starts them again.

Conflict: one conflict, in drivers/soc/apple/sep.rs remove(), against the HDCP client (#3256). The fix wakes both xars_wq and hdcp_wq. Git merged the rest on its own: both OOL allocations, both discovery hooks, and both OOL buffers in the teardown list. I checked each of those by hand.

Overlap with #3275 (attach-only endpoints): the two PRs conflict with each other in sep.rs and tools/aurora-sep/aurora-sep. Neither PR needs the other. Whichever lands second needs one more forward merge: move self.xars_attach() into #3275's attach_idle_endpoints(), which runs before finish_bringup() and on the early-failure paths, and keep both status blocks.

Side note, not blocking: on this branch alone, xars_attach() runs after finish_bringup(). A reader can therefore briefly see diag/xars=pending after PHASE_READY. On the bring-up early-exit paths (no endpoints advertised, or xarm OOL registration fails) it stays pending. #3275 fixes the same pattern for sse/hibe/skdl, and the resolution above covers xars as well. I didn't change it here so the hardware rows stay valid.

Tested at 87974c3a (buildbox, LLVM=1, clang 22.1.6, rustc 1.97.1):

  • arm64 QEMU KUnit sep.kunitconfig apple_sep*: 347/347 PASS (xars 11/11, hdcp 13/13)
  • sep-source.yml Python steps: 22/22 commands rc=0
  • test_aurora_sep_status.py: 16/16 PASS, and 16/16 with -O
  • run-host-tests.py --only sep: 23/23 PASS
  • checkpatch --strict on 74651281: 0 errors and 3 warnings. Two are the void (*fn)(void) function-pointer parse that the existing sep_pm_register() prototype also triggers. The third is "MAINTAINERS for a new file"; there is no SEP MAINTAINERS entry.

Hardware row: the PR's code is byte-identical to 74651281, the head of the J473 and m1pro 5x-reboot rows in the body. The merge only adds main's code. No new row is owed for this PR's changes. The merged head itself has not been booted on hardware.

@github-actions github-actions Bot added blast:all Macs affected, from Merge risk (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) and removed merge-conflict Conflicts with the base branch (bot) needs-template PR body lacks Tested or Risk (bot) labels Nov 27, 2026
Picks up the revert of #3302 (fca33ed), whose host-test breakage came in with the previous main merge.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Merged main again at 715556a (main fca33ed). The previous main merge brought #3302, whose host-test breakage (apple-dcp, dcpext26-startup, hardware-defaults, backlight-coldboot: undeclared dcp_hdcp_match_service in the harnesses) main has since reverted in #3374. This PR's code is unchanged.

Tested: run-host-tests.py --only dcp 65/65, --only sep 23/23 at 715556a.
Risk: none beyond the previous head; the KUnit jobs failing with apt 403 from packages.microsoft.com are runner infra.

@github-actions github-actions Bot removed the ci-failing A check failed at the PR head (bot) label Nov 27, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: Changes requested at 715556a143e2.

The claimed ten-second shutdown bound starts after begin_closing() and register_ool(), and the final close_store() takes a mutex that can wait behind a callback doing filesystem sync without consulting the deadline. The RPC wait budget therefore does not bound the blocking notifier. Please account for registration and the final drain in the shutdown contract and test a delayed trailing write. Keep the write path available until it is safe to detach the store, including when the result is indeterminate.

@github-actions github-actions Bot added the merge-conflict Conflicts with the base branch (bot) label Nov 28, 2026
The ten-second barrier budget started only after begin_closing() and OOL
registration, and the final store close took the store mutex with no
deadline, so it could wait behind an xarm callback that is still syncing
a write. The blocking reboot notifier was therefore not bounded.

Take the deadline when the notifier enters the barrier. The precondition
check and the final close now take the store lock with a deadline-bounded
try_lock loop; OOL registration sends a control request only while its
whole reply timeout still ends by the deadline; both replies keep using
the remaining budget. If the store is still held at the deadline, leave
it open (xarm keeps servicing) and report the barrier as not completed.

Add KUnit cases for a slow closing/registration and for a trailing xarm
write that outlives the budget.

Signed-off-by: Ryan Murray <[email protected]>
Conflict in drivers/soc/apple/shim.h: keep both the xars and the trusted
diag state definitions.

Signed-off-by: Ryan Murray <[email protected]>
@RyanTheTide

Copy link
Copy Markdown
Contributor Author

Pushed a fix for the review finding and a forward merge. New head is 461da6d.

  • 51a6146: the 10 s budget now starts when the reboot notifier enters the barrier, and every later wait is bounded by it:
    • The precondition check and close_store() take the store mutex through a deadline-bounded try_lock loop.
    • OOL registration sends each control request only if its full reply timeout still ends before the deadline.
    • Both RPCs use the remaining budget.
    • If an xarm callback still holds the store at the deadline, the store stays open (xarm keeps servicing) and the result is CloseTimedOut, with the barrier state set to Indeterminate. The store is never detached after an indeterminate step.
    • begin_closing() stays bounded by its own drain limits, which are below the budget.
  • New KUnit cases: budget_starts_at_notifier_entry (slow closing or registration uses up the budget; registration that cannot finish in time is not started) and trailing_xarm_write_past_budget_leaves_store_open (returns by the deadline with the store still open).
  • 461da6d merges main. The shim.h conflict is resolved by keeping both the xars and the trusted diag definitions.
  • Tests: sep.kunitconfig apple_sep* on arm64 QEMU passes 351/351, including the 13 apple_sep_xars cases. test_aurora_sep_status.py passes 16/16.
  • Still needed: the shutdown path changed, so the rows must be redone: J473 5x reboot with netconsole and J316s 5x reboot with Touch ID surviving. Then App re-review. This should land after soc: apple: sep: harden attach-only endpoints (sse, hibe, skdl) and update specs #3275, and Ryan needs to OK the protocol provenance first.

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH at 461da6d

Checked: The deadline now starts at notifier entry and the final store drain has a bounded path. register_ool_by still takes the slot mutex with an unconditional lock both before the request loop and after its last reply. The timeout check around the requests cannot bound either wait. Make both acquisitions deadline-aware, or prove the shutdown path excludes competing holders, and test a delayed registration lock. The existing provenance disposition is still open.

@github-actions github-actions Bot added ci-failing A check failed at the PR head (bot) and removed merge-conflict Conflicts with the base branch (bot) ci-failing A check failed at the PR head (bot) labels Nov 29, 2026

@aurora-silicon-review aurora-silicon-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 461da6d9b6be

Converts this App's earlier review at the same head to an approval under the 12-01 merge bar (better than before and doesn't break; block only on a shown failure). No code blocker was found in it. The merge gate (a row on one Mac at this head + fast CI) is tracked by pr-readiness, not held here.

Checked: Code reviewed in the earlier App review at this head (6101680648).

Follow-ups (not blocking): deadline-aware slot mutex in register_ool_by (or prove no competing holder), delayed-lock test, provenance disposition

This branch has not been deployed

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

Labels

area:platform Area from CODEOWNERS (bot) blast:all Macs affected, from Merge risk (bot) ci-failing A check failed at the PR head (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant