diff --git a/.crumb b/.crumb index 3da141cc..0858dce2 100644 --- a/.crumb +++ b/.crumb @@ -33,27 +33,27 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "5c12ec8f30db11dfd3faa66837610a3639cb256608864623ee71c297ed176124", - "last_commit": "2b4fd9f", + "digest": "d6f18b3969b054e8692396c36ab3d897e039f15516d089c3776ad3de47354ac1", + "last_commit": "b79954c", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "e8e8c21a3f1b0e2a2df96918fd438edf57c0f38095d04c9abc529225c4da9363", - "last_commit": "f921666", + "digest": "e05c9be44a099db64118bb542ef8f97041efe9642107c7007291c44b7aac1ff4", + "last_commit": "b79954c", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:0398c76942ffe114dad9c4b3aec2fe5ce98e9b1f592411512bb8cd048ae6c7b3", - "compiled_at": "2026-10-06T01:42:05Z", + "children_root": "sha256:30b1bd25ba5cf8166dbb3a6bd1f9bdfa4d9cdbfdbbfb4f90dd672129addfdbb9", + "compiled_at": "2026-10-06T03:00:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:e3c013fa59d843e3834df9f3748d07229862e72ad5ff9be2352dc3d0666f5cc0", - "evidence_files": 149, - "evidence_root": "sha256:e682703b498863c134532be1f72e10bebee3a00764307206e1f410fb2bf0390a", - "files": 2863, - "generated_at_commit": "2b4fd9f620e23e1be64f34e531ceac198e51321c", - "source_tree": "sha256:f6408e48d33dc70c59e7e4225cae9379ed91599d3f6d0423002e80254650785f" + "digest": "sha256:77853dbc20a35f043e835cc53c32958914eac171e1c5cfa652728d8275b10195", + "evidence_files": 150, + "evidence_root": "sha256:47a75cce7791857aaf61e70f3e80b9d5d42c0a814a27e02fe91226c4e8804933", + "files": 2872, + "generated_at_commit": "b79954c7278df871a8226d51c6a594918676aba3", + "source_tree": "sha256:a1780e94e691b57f203cc211bcbf82c6000f3570ea77a41680560374ec1f1e89" }, "owner_lane": "", "provenance": { diff --git a/ROADMAP.md b/ROADMAP.md index 2a7370db..4dbb9e67 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -17,13 +17,16 @@ NOT_RUN, BLOCKED_HARDWARE, BLOCKED_OPERATOR, MISSING_IMPLEMENTATION. C kernel series #194 34c6053 .. #204 c3878bb IMPLEMENTED / NOT QUALIFIED evidence class: QEMU, plus GitHub ARM64 CI (which is still QEMU). No physical boot of the C kernel has happened ("physical": NOT_RUN). - newest ck_gates receipt: evidence/ck_gates_bc306a04d8ba513b2364fbee644f37375dc686ccead97c8759191b76ebec94eb.json - run at f921666 (NEXT-PHASE-3 cut 1, C keyboard + recovery access); verdict NOT_ALL_GATES_PASS + newest ck_gates receipt: evidence/ck_gates_05762a0f09ee9ca1453ebc80b1ee904a92de8cd6a73e2012e1bed7eeda0f8089.json + run at b79954c (NEXT-PHASE-3 cut 2, platform xHCI discovery from ACPI); verdict NOT_ALL_GATES_PASS 19 PASS / 0 FAIL / 1 NOT_RUN of 20 gates NOT_RUN: M0_ROLLBACK - KEYBOARD PASS in QEMU only; the Spark's USB controllers are ACPI platform - xHCI devices, not PCI, so the C driver does not reach them yet - (native/kernel/README.md, "Operator input and recovery access"). + KEYBOARD PASS in QEMU only. The C driver now finds the Spark's six ACPI + platform xHCI controllers (from its firmware tables, hardware NOT_RUN), + but the SMMU service refuses the Spark IORT (15 root-complex mappings, + limit 8; stream ids past its linear table), so every one is denied + and fails closed (native/kernel/README.md, "Operator input and + recovery access"). M0_ROLLBACK is still MISSING_IMPLEMENTATION in the C kernel (the C loader BootNext / A-B / rollback is parked, native/boot/README.md), despite the title of #204 ("P2_ARTIFACT loader and M0_ROLLBACK gates"). #204 diff --git a/evidence/ck_gates_05762a0f09ee9ca1453ebc80b1ee904a92de8cd6a73e2012e1bed7eeda0f8089.json b/evidence/ck_gates_05762a0f09ee9ca1453ebc80b1ee904a92de8cd6a73e2012e1bed7eeda0f8089.json new file mode 100644 index 00000000..6f8597f9 --- /dev/null +++ b/evidence/ck_gates_05762a0f09ee9ca1453ebc80b1ee904a92de8cd6a73e2012e1bed7eeda0f8089.json @@ -0,0 +1,58 @@ +{ + "schema": "aienos.ck_gates.v1", + "started_utc": "2026-10-06T02:21:29Z", + "finished_utc": "2026-10-06T03:00:07Z", + "commit": "b79954c7278df871a8226d51c6a594918676aba3", + "commit_subject": "ck kernel: platform xHCI discovery from ACPI (NEXT-PHASE-3 cut 2)", + "tree_clean_before": true, + "tree_clean_after": true, + "quiet_flag": "/home/drakestapleton/.claude/jobs/9bfe8553/tmp/locks/quiet-c2after", + "quiet_flag_present_at_start": false, + "host": {"hostname": "spark-b87b", "arch": "aarch64", "kernel": "7.0.0-1019-nvidia", "cpu_model": "Cortex-A725;Cortex-X925"}, + "qemu": {"binary": "/usr/bin/qemu-system-aarch64", "version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)"}, + "firmware": {"aavmf_code": "/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd", "aavmf_code_sha256": "4a4cb7f6d8106bb2a7dd8c763fab14b1810152136fc4304e5b728f0043e84f12", "aavmf_vars": "/usr/share/AAVMF/AAVMF_VARS.fd", "aavmf_vars_sha256": "b3b855c5a80310168051164986855692d1bdb06e67619856177965cd87c6774f"}, + "nvme_dma_mode": "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately", + "children": [ + {"name": "boot", "script": "scripts/qemu_ck_boot_test.sh", "present": true, "script_sha256": "d17a7e0f00081e935a804c092c9b05c0bd1f319f5dd5c8bd90f3982e8939c3c8", "exit": "0", "seconds": 6, "log_sha256": "923d975fc8452544683a84e1f57d09f25c12d2a568b1feefa9f97a97a469e589", "log_lines": 24}, + {"name": "store", "script": "scripts/qemu_ck_store_test.sh", "present": true, "script_sha256": "0b31a44af9388302d5488fdef318a509c4f26c2420f82ceb2fa7f7b698647748", "exit": "0", "seconds": 91, "log_sha256": "c48fbb001bb5ee4c24b012c35396d9dc851ea94537c2715a9f0e0273ae5b010f", "log_lines": 612}, + {"name": "net", "script": "scripts/qemu_ck_net_test.sh", "present": true, "script_sha256": "ea051cc946c206f92c0c1563b3463b6bebffbce04e361e76d51dd49c1be4a6cf", "exit": "0", "seconds": 18, "log_sha256": "69fb63d5c246aea247166aba079bba7c04357ee99244ff7af25de9baf4ad2cd4", "log_lines": 95}, + {"name": "artifact", "script": "scripts/qemu_ck_artifact_test.sh", "present": true, "script_sha256": "e60f6c4aed822f693a530cb98b2fa4689b7b56e7c0efed5f9bf9ff4e9f75fc67", "exit": "0", "seconds": 32, "log_sha256": "36cc57728441e2e27ad56a76819297b5681143c9638871c233671d429e006599", "log_lines": 215}, + {"name": "smp", "script": "scripts/qemu_ck_smp_test.sh", "present": true, "script_sha256": "b5665568394f391c17aaa9148bce2358f1145240cc38422c2927664b0ffd41d6", "exit": "0", "seconds": 6, "log_sha256": "9ceecfc0b22b9c5ba5bc37f8b5b117f1285cfa61f3c10b6419d0ff288d2cbf83", "log_lines": 16}, + {"name": "crash", "script": "scripts/qemu_ck_store_crash_test.sh", "present": true, "script_sha256": "3d04ca0775db7e70a244b6a09b19c5adf987f777b6ff630874c42e339add7765", "exit": "0", "seconds": 676, "log_sha256": "4059e2ca3fc2df078693ca717505424da031e32d9b8da1a159ced4e993dc12c6", "log_lines": 239}, + {"name": "disk", "script": "scripts/qemu_ck_disk_layout_test.sh", "present": true, "script_sha256": "f490548245252b97518e9049ce8e7078f5297c81584b60f08f802cfea6bb3110", "exit": "0", "seconds": 57, "log_sha256": "af29849e3455e615af2a221fa0bf69519ccc8e46d6bad15e6c504dbb33d76904", "log_lines": 264}, + {"name": "cont", "script": "scripts/qemu_ck_continuity_test.sh", "present": true, "script_sha256": "30a8eb2dbb949eab29ad046231869a306199908022cb2345151988ea4957cf2e", "exit": "0", "seconds": 185, "log_sha256": "dfa0c6f26e202c797239b07187fe7a09e661733133ebc1a6f8278f144cc89462", "log_lines": 57}, + {"name": "recov", "script": "scripts/qemu_ck_recovery_test.sh", "present": true, "script_sha256": "89c461e211532f6577d2c88668ae8ca0d32d91f3b29ce6e39f7082a03a45b8cc", "exit": "0", "seconds": 115, "log_sha256": "500cd02050eae8f2f4aa34fdb004ff4d5399751de25de9dfe554ed5cdca32e30", "log_lines": 46}, + {"name": "allen", "script": "scripts/qemu_ck_allen_test.sh", "present": true, "script_sha256": "0fe58465d5183781b8339f50861fd76f7773051bee939b73fdaede4b7a752355", "exit": "0", "seconds": 202, "log_sha256": "ef32d7f9d44ea520f258c84b3ac89a4537068a6d9f98c018a75ef49504318ef2", "log_lines": 73}, + {"name": "kbd", "script": "scripts/qemu_ck_keyboard_test.sh", "present": true, "script_sha256": "c51c365ceb10f33e15c54e23d11267b7233ffd2f388958f2f305e79c6b4fee5c", "exit": "0", "seconds": 41, "log_sha256": "eff7c7fa15162712a491eae47b334b597f1069230eb3c1d4d65d72e619796fb5", "log_lines": 168}, + {"name": "fpu", "script": "scripts/qemu_ck_fpu_test.sh", "present": true, "script_sha256": "f04fa538752f6864bc2d5f9ff5af6eac71a5e3a005fe9c76b77477012325cc14", "exit": "0", "seconds": 8, "log_sha256": "0c2d7c21c8f3fd972339f8717f031b32dbea57720d3b0c31b6da6f544a2898a5", "log_lines": 13}, + {"name": "infer", "script": "scripts/qemu_ck_infer_test.sh", "present": true, "script_sha256": "185c78b8688004dc6a807182c81e319b1598414935934b6c881cfafae5ce31fa", "exit": "0", "seconds": 863, "log_sha256": "e238e4d671160a2cbdcf056e1622105facad1e219f4b6cb103a61ecb30dc020e", "log_lines": 35}, + {"name": "screen", "script": "scripts/qemu_ck_screen_test.sh", "present": true, "script_sha256": "b2b71aff7fb9274eb15d8381e44dda9d7b700821fd45a23cde1c9a1132dc6b3f", "exit": "0", "seconds": 16, "log_sha256": "00a8729965d1e2b8fcde7f08ebeb378e584f9653c3fd1db13b75f585c59dfcc9", "log_lines": 30} + ], + "gates": [ + {"id": "M1", "marker": "AIENOS_CK_M1", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M3", "marker": "AIENOS_CK_M3", "source": "boot", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "SMMU", "marker": "AIENOS_CK_SMMU", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "NVME_SHUTDOWN", "marker": "AIENOS_CK_NVME_SHUTDOWN", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "P2_ARTIFACT", "marker": "AIENOS_CK_P2_ARTIFACT", "source": "artifact", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M0_ROLLBACK", "marker": "AIENOS_CK_M0_ROLLBACK", "source": "missing", "verdict": "NOT_RUN", "reason": "MISSING_IMPLEMENTATION: C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md)"}, + {"id": "M4_NVME", "marker": "AIENOS_CK_M4_NVME", "source": "store", "verdict": "PASS", "reason": "SMMU-confined NVMe DMA in QEMU (iommu=smmuv3, default build); TEST-ONLY bypass build boot-checked separately"}, + {"id": "M4_STORE", "marker": "AIENOS_CK_M4_STORE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_STORE_CRASH", "marker": "AIENOS_CK_M4_STORE_CRASH", "source": "crash", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_CONTINUITY", "marker": "AIENOS_CK_M4_CONTINUITY", "source": "cont", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_RECOVERY", "marker": "AIENOS_CK_M4_RECOVERY", "source": "recov", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "M4_ALLEN", "marker": "AIENOS_CK_M4_ALLEN", "source": "allen", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "ARGUS1_REVOKE", "marker": "AIENOS_CK_ARGUS1_REVOKE", "source": "store", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "KEYBOARD", "marker": "AIENOS_CK_KEYBOARD", "source": "kbd", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "NET", "marker": "AIENOS_CK_NET", "source": "net", "verdict": "PASS", "reason": "QEMU user networking (slirp), virtio-net with ACCESS_PLATFORM required behind the smmuv3 vIOMMU; this PASS covers: attach, UDP TX seen by the host helper, UDP reply RX parsed by M6-A, an out-of-window device DMA refused (F_TRANSLATION, page intact) and recovery by reset, as checked by this run only; QEMU, not hardware"}, + {"id": "SMP", "marker": "AIENOS_CK_SMP", "source": "smp", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "DISK_LAYOUT", "marker": "AIENOS_CK_DISK_LAYOUT", "source": "disk", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "FPU", "marker": "AIENOS_CK_FPU", "source": "fpu", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "INFER", "marker": "AIENOS_CK_INFER", "source": "infer", "verdict": "PASS", "reason": "child reported PASS, exit 0"}, + {"id": "SCREEN", "marker": "AIENOS_CK_SCREEN", "source": "screen", "verdict": "PASS", "reason": "child reported PASS, exit 0"} + ], + "counts": {"total": 20, "pass": 19, "fail": 0, "not_run": 1}, + "verdict": "NOT_ALL_GATES_PASS", + "parity": "native/kernel/GATES.md", + "physical": "NOT_RUN", + "physical_statement": "QEMU emulator runs only. Nothing here was run on Machine 1 or any GB10; no physical qualification is claimed." +} diff --git a/native/.crumb b/native/.crumb index 5d7ec0e8..875370c1 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "e2bbca7edcc6a7f38cdd2287cd8ed3dfc10e33b674f771a689f0c6d7d26fb682", - "last_commit": "f921666", + "digest": "88f28f8c28bdd707b7b26e829417a785970606218731c306522932b3edb171a9", + "last_commit": "b79954c", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:dc7d8a5fe8b7cec6d8c4d5b039db6f337c2969022aeb261b5dacaa3d07e16a5d", - "compiled_at": "2026-10-06T01:42:01Z", + "children_root": "sha256:ab2490af7c9ae0e68d90d6feeb0698f5e839cfd545cc95dfc732913f27a1c574", + "compiled_at": "2026-10-06T03:00:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:5c12ec8f30db11dfd3faa66837610a3639cb256608864623ee71c297ed176124", + "digest": "sha256:d6f18b3969b054e8692396c36ab3d897e039f15516d089c3776ad3de47354ac1", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 310, - "generated_at_commit": "f921666189e67fbc80cc511d84d9991ec76c26b4", - "source_tree": "sha256:c32c59720817576798a7212f48e9f37ff32c9bc9bedb01745939da1250532a7c" + "files": 317, + "generated_at_commit": "b79954c7278df871a8226d51c6a594918676aba3", + "source_tree": "sha256:9909eb6f7717e72c6948417f6b6ca232b686f5a8f9332f005ed69164830c1947" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 01d70120..c5cac71d 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,57 +57,57 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "e5ebaf34e03225eabc74a1b335f9aea4635942690cd0d8aeb22cfa6addd4e833", - "last_commit": "f921666", + "digest": "af2d1e1f63f7489840073bbe10705d8cae0c3886fb0768c829940a33e71490d4", + "last_commit": "b79954c", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "914c089b5c7d4cf9f5ffd86440ae5a8250bb7e518c497aa65e9829797b1f1e57", - "last_commit": "f921666", + "digest": "6f1c98b95ea2d94ef3d0230a38fd121e9244c8f3f8dc8d65c3cf2957a8bccff1", + "last_commit": "b79954c", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { - "digest": "67502074fde0b499b68d22d8498c2e410e365f1d9f2dc69b890fac52a56a2d93", - "last_commit": "f921666", + "digest": "9b69f8190384ecb91f1d09f7109e0d08a84c601bc566e480f5098e6106ec6530", + "last_commit": "b79954c", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, { - "digest": "272bc1dd7957e2024660c4f2850462671069bec77f4eee8d2c71b5b8788ab97e", - "last_commit": "46f22fe", + "digest": "05fee5afbbfcbca9eab294bfc3778bc380b2eaa1c95a595b6946971a180f0665", + "last_commit": "b79954c", "name": "mm", "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "2c634b363be2e02a04c0fdb3cf56c88bebf0a86ee172f3416989df3764ea7a0d", - "last_commit": "f921666", + "digest": "ffa91a4e0808986fd633979835ad7cb183995cf87939f16f55da4e58f9e24254", + "last_commit": "b79954c", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "20157ea957a698743f9078faf6c765b507733f9a9e67877ea5602c7f461b7a7b", - "last_commit": "9d41efc", + "digest": "182f6664539cfc8d8731eb824c5df6ce700059e666dac474500929fce0b99c0d", + "last_commit": "b79954c", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, { - "digest": "5e6a44b4b936178bd58d2840921cc5d478ed9126ffea1aa3c36d628a6598549f", - "last_commit": "9d41efc", + "digest": "342735b38f7498b022914ea412e4b8e798a916786b31ad02516ef7d9e518acba", + "last_commit": "b79954c", "name": "tools", "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:0b4520412eaa0fbd41c3c2686b6a590d39a836ca4875d3e317730a40e384989c", - "compiled_at": "2026-10-06T01:42:01Z", + "children_root": "sha256:6937dde85c1b4a70880d1fa5f93cbd306121c699b075b508adccd7e1f357f9d7", + "compiled_at": "2026-10-06T03:00:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:e2bbca7edcc6a7f38cdd2287cd8ed3dfc10e33b674f771a689f0c6d7d26fb682", + "digest": "sha256:88f28f8c28bdd707b7b26e829417a785970606218731c306522932b3edb171a9", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 146, - "generated_at_commit": "f921666189e67fbc80cc511d84d9991ec76c26b4", - "source_tree": "sha256:ee6bfdcb90b79757e6dd8bf893def42c954f46042882cca2216467c2ddaf5bcf" + "files": 153, + "generated_at_commit": "b79954c7278df871a8226d51c6a594918676aba3", + "source_tree": "sha256:876668dde168f0f1ec103a72031f777343a294516ec3899b5faebe81cedb2d53" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 17824546..67e1ffe6 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -25,7 +25,7 @@ C gates (one line each from `ck_gates.sh`): `M1` and `M3` (from `scripts/qemu_ck_net_test.sh`; rows 102-106 and 110-112; the script now also checks the virtio-net SMMU fence (ACCESS_PLATFORM required, out-of-window device DMA refused), rows 102 and 110-112 are QEMU PASS on this branch from the committed forge log `evidence/ck_net_qemu_8bdbc2e46b85d04c422f9d2830c1c4b263e254b3cfecfe21ff0063c9e33c86e8.log`, hardware NOT_RUN), the kernel entropy rows 107-109 (M1 via `scripts/lib_ck_m1_checks.sh`, ARGUS1_REVOKE and M4_STORE via the Store gate boot 7), `DISK_LAYOUT` (C-only, from `scripts/qemu_ck_disk_layout_test.sh`, rows 118-123: QEMU PASS at 741b2b8 before the merge with main 8555049, rerun pending at the merged head; row 123 host test), and the NOT_RUN gates `SMP` (C-only, from `scripts/qemu_ck_smp_test.sh`, rows 113-117, NOT_RUN pending forge receipt), `M0_ROLLBACK`, `M4_STORE_CRASH` (now read from `scripts/qemu_ck_store_crash_test.sh`, rows 73-80; NOT_RUN until a forge receipt exists), -`M4_CONTINUITY`, `M4_RECOVERY`, `M4_ALLEN` (C-only, from `scripts/qemu_ck_allen_test.sh`, rows A1-A30; QEMU PASS, hardware NOT_RUN), `KEYBOARD` (from `scripts/qemu_ck_keyboard_test.sh`, rows 25-32 and C-only 30a-30c; QEMU PASS, hardware NOT_RUN), `FPU` (C-only, from `scripts/qemu_ck_fpu_test.sh`, rows 124-128; QEMU PASS, hardware NOT_RUN), `INFER` (C-only, from `scripts/qemu_ck_infer_test.sh`, rows 129-139; QEMU PASS, hardware NOT_RUN), and `SCREEN` (C-only, from `scripts/qemu_ck_screen_test.sh`, rows 140-145; QEMU PASS, hardware NOT_RUN). +`M4_CONTINUITY`, `M4_RECOVERY`, `M4_ALLEN` (C-only, from `scripts/qemu_ck_allen_test.sh`, rows A1-A30; QEMU PASS, hardware NOT_RUN), `KEYBOARD` (from `scripts/qemu_ck_keyboard_test.sh`, rows 25-32 and C-only 30a-30c and 32a; QEMU PASS; C-only row 32b and all hardware NOT_RUN), `FPU` (C-only, from `scripts/qemu_ck_fpu_test.sh`, rows 124-128; QEMU PASS, hardware NOT_RUN), `INFER` (C-only, from `scripts/qemu_ck_infer_test.sh`, rows 129-139; QEMU PASS, hardware NOT_RUN), and `SCREEN` (C-only, from `scripts/qemu_ck_screen_test.sh`, rows 140-145; QEMU PASS, hardware NOT_RUN). `M4_NVME` and `SMMU` PASS in the SMMU-confined mode (default `make full` image, QEMU `iommu=smmuv3`). The unconfined bypass build @@ -169,6 +169,23 @@ digest). One deliberate difference from Rust: the Rust shell drops keys past its 64-byte line; the C editor refuses the whole line (row 30c). Hardware NOT_RUN: no physical xHCI, keyboard or SMMU run. +Platform xHCI (NEXT-PHASE-3 cut 2, C only): when no PCI xHCI exists, or the +PCI one has no keyboard, the fence tries the ACPI platform controllers that +`native/kernel/core/acpi_dev.c` finds in the DSDT/SSDTs (`_HID`/`_CID` +NVDA8000, NVDA8001, PNP0D10, PNP0D15), each confined by its IORT named +component stream (`ck_dma_confine_named`), fail closed otherwise. QEMU virt +has no such controller, so QEMU proves the walker on QEMU's own tables (row +32a) and that the PCI path is unchanged (rows 25-32); the platform grant path +itself has no QEMU run. Host tests: `tests/test_acpi_dev.c` (string and +EisaId ids, Name and Method `_CRS`, Memory32Fixed and QWord ranges, nested +devices, malformed packages and descriptors, IORT named components found, +absent, ambiguous, malformed, behind another SMMU), and `make acpi-dev-mutant` +requires it to FAIL against three walker mutants (table signature check, +checksum check, length-versus-buffer check removed). `make acpi-scan` builds +`tools/ck_acpi_scan.c`, which runs the same scan over firmware tables dumped +under Linux and prints the kernel's lines; its output for this Spark is +`tests/fixtures/spark_xhci_acpi_expected.txt` (row 32b). + | # | Rust check (pattern) | CK gate | Status | | --- | --- | --- | --- | | 25 | xHCI found before exit (`keyboard: xhci `) | KEYBOARD | QEMU PASS, DIFFERS: found after exit on the ECAM walk, class 0x0c0330, `keyboard: xhci 0000:BB:DD.F mmio 0x.. (found post-exit on the ECAM walk)`, all three boots | @@ -182,6 +199,8 @@ NOT_RUN: no physical xHCI, keyboard or SMMU run. | 30c | (C only) overlong line fails closed | KEYBOARD | C only, QEMU PASS: 65 keys then Enter gives `keyboard_line: overflow (line refused: 65 keys typed, limit 64)` and `keyboard: done (overflow)`; no `keyboard_line:` with that text and no command run from it; boot kbd-smmu (Rust silently truncates instead) | | 31 | xHCI bus master revoked after the phase (`dma_gate: xhci bus master revoked`) | KEYBOARD | QEMU PASS, DIFFERS (stronger): the same line after `smmu_dma_window: xhci only, translation active ...` and `dma_gate: xhci granted (Confined), bus master on`, controller halted first (`xhci: halt before revoke ... halted=yes`), COMMAND read back `xhci_pci: after phase command=0x.... bus_master=off`, `smmu: xhci stream 0x.. returned to abort (rc=0)`, in that order and before the end of the xHCI phase; boots kbd-smmu and kbd-recovery; mutation no-revoke | | 32 | no panic or fault | KEYBOARD | QEMU PASS, IDENTICAL: `report_kind: (panic\|fault)` absent, plus `report_kind: final`, QEMU exit 0 and every M1 check of scripts/lib_ck_m1_checks.sh; boots kbd-smmu and kbd-nosmmu | +| 32a | (C only) ACPI platform walker on QEMU's own tables, PCI path kept | KEYBOARD | C only, QEMU PASS: `acpi_scan: tables=N refused=0 first_refusal=0 devices=M (static scan; _STA not evaluated)` with N, M > 0 (QEMU: tables=1 devices=46), positive control `acpi_scan: control ARMH0011 COM0 mmio=0x9000000+0x1000` (the QEMU virt UART), `xhci_acpi: 0 platform controller(s)`, and no `xhci_plat:` line or platform fallback; all three boots | +| 32b | (C only) DGX Spark platform xHCI expectation | KEYBOARD | C only, hardware NOT_RUN (never counted): the lines in `native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt`, predicted by `tools/ck_acpi_scan.c` from this Spark's DSDT/SSDT/IORT: six controllers `xhci_acpi: USB0..USB5`, IORT streams 0x0-0x5 on the first SMMUv3 (0x13800000), each then denied `dma_gate: xhci USBn denied (SmmuNotReady), controller left halted` because the SMMU service refuses the Spark IORT (15 root-complex mappings to that SMMU, limit 8; stream ids up to 0xfffff, linear table 0x0-0xfff) | ## SEED-0B / P2-5 artifact loader: scripts/qemu_artifact_test.sh -> CK `P2_ARTIFACT` (scripts/qemu_ck_artifact_test.sh) @@ -545,7 +564,7 @@ corruption is covered only by the host test `dev/tests/disk_part_test.c` ## Summary counts -**C kernel receipt tally (QEMU only, no physical run):** Newest: `evidence/ck_gates_bc306a04d8ba513b2364fbee644f37375dc686ccead97c8759191b76ebec94eb.json` (full `ck_gates.sh` at f921666, NEXT-PHASE-3 cut 1 branch, KEYBOARD from its own child): 19 PASS, 0 FAIL, 1 NOT_RUN (M0_ROLLBACK: MISSING_IMPLEMENTATION); KEYBOARD PASS (rows 25-32 and C-only 30a-30c, QEMU only, hardware NOT_RUN). Baseline for that change, same runner on main 2058747: `evidence/ck_gates_79a843817161b68d71889201bd4407a3b4e432ac808503b017836f71abc3f2db.json`: 18 PASS, 0 FAIL, 2 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION). Rows 25-32 move from NOT_RUN to QEMU PASS (8 rows: IDENTICAL 4, DIFFERS 4 by their parity labels) and C-only rows 30a-30c are added as QEMU PASS; the older row recounts below predate this change. Before it: `evidence/ck_gates_1ecf5bcd303bf88422dd0542f14851c931f5d52276838fa182c812f3c73f3c1b.json` (full `ck_gates.sh` at 640522a, ALLEN native genesis branch, 20 CK gates with `M4_ALLEN`): 15 PASS, 0 FAIL, 5 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION; FPU, INFER, SCREEN: the shared QEMU gate lock was held by another run). Before it: `evidence/ck_gates_ad05e004e30237e57e72fc1d3362cfe2b31af9b99451d9d2bb2414238e4d6590.json` (full `ck_gates.sh` at 2e44c0b, L6-C branch, 19 CK gates with `SCREEN`): 17 PASS, 0 FAIL, 2 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION). Earlier: 16 CK gates since the merge of main 8555049 (#224 adds `SMP`, #226 moves `M4_STORE_CRASH` to its own child script) into the CK-4 branch. The newest full receipt, `evidence/ck_gates_d80363cc871372963743ae85fc08c6fa4608f9870c4e06e8988f21950c73ace6.json` (full `ck_gates.sh` run at 741b2b8, the CK-4 branch after merging main #221 and #225; log CK-4-184524.log, `AIENOS_CK_GATES: NOT_ALL_GATES_PASS (pass=10 fail=0 not_run=5 of 15; physical NOT_RUN)`), predates that merge: by it, 10 of 16 CK gates PASS (the 9 of the earlier receipt plus `DISK_LAYOUT`), 6 NOT_RUN (M0_ROLLBACK, M4_STORE_CRASH, M4_CONTINUITY, M4_RECOVERY, KEYBOARD; and SMP, not in that receipt, NOT_RUN pending forge receipt), 0 FAIL. No `ck_gates.sh` run exists at the merged head yet; `M4_STORE_CRASH` stays NOT_RUN until a forge receipt. `DISK_LAYOUT` also PASSed alone (log CK-4-184423.log, translation-bypass mutant killed); the Store, NET and artifact gates boot GPT images now and PASS in the same receipt. The receipt before it was 9 of 14 CK gates PASS, 5 NOT_RUN, 0 FAIL (`evidence/ck_gates_929f287e9c950c45c7dcd569c7caa03709ea62ecfe393635b435058afa969c54.json`, run at 41aa0e6, which adds the kernel entropy rows 107-109; that commit was then rebased onto #210, which changed only README.md and CONTRIBUTING.md). The row counts below are per Rust-parity row, not per gate. +**C kernel receipt tally (QEMU only, no physical run):** Newest: `evidence/ck_gates_05762a0f09ee9ca1453ebc80b1ee904a92de8cd6a73e2012e1bed7eeda0f8089.json` (full `ck_gates.sh` at b79954c, NEXT-PHASE-3 cut 2 branch, platform xHCI discovery): 19 PASS, 0 FAIL, 1 NOT_RUN (M0_ROLLBACK: MISSING_IMPLEMENTATION); KEYBOARD PASS (rows 25-32, C-only 30a-30c and 32a; row 32b hardware NOT_RUN). Baseline for that change is the receipt below (same 19/0/1; its tree equals the cut 2 base). Before it: `evidence/ck_gates_bc306a04d8ba513b2364fbee644f37375dc686ccead97c8759191b76ebec94eb.json` (full `ck_gates.sh` at f921666, NEXT-PHASE-3 cut 1 branch, KEYBOARD from its own child): 19 PASS, 0 FAIL, 1 NOT_RUN (M0_ROLLBACK: MISSING_IMPLEMENTATION); KEYBOARD PASS (rows 25-32 and C-only 30a-30c, QEMU only, hardware NOT_RUN). Baseline for that change, same runner on main 2058747: `evidence/ck_gates_79a843817161b68d71889201bd4407a3b4e432ac808503b017836f71abc3f2db.json`: 18 PASS, 0 FAIL, 2 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION). Rows 25-32 move from NOT_RUN to QEMU PASS (8 rows: IDENTICAL 4, DIFFERS 4 by their parity labels) and C-only rows 30a-30c are added as QEMU PASS; the older row recounts below predate this change. Before it: `evidence/ck_gates_1ecf5bcd303bf88422dd0542f14851c931f5d52276838fa182c812f3c73f3c1b.json` (full `ck_gates.sh` at 640522a, ALLEN native genesis branch, 20 CK gates with `M4_ALLEN`): 15 PASS, 0 FAIL, 5 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION; FPU, INFER, SCREEN: the shared QEMU gate lock was held by another run). Before it: `evidence/ck_gates_ad05e004e30237e57e72fc1d3362cfe2b31af9b99451d9d2bb2414238e4d6590.json` (full `ck_gates.sh` at 2e44c0b, L6-C branch, 19 CK gates with `SCREEN`): 17 PASS, 0 FAIL, 2 NOT_RUN (M0_ROLLBACK, KEYBOARD: MISSING_IMPLEMENTATION). Earlier: 16 CK gates since the merge of main 8555049 (#224 adds `SMP`, #226 moves `M4_STORE_CRASH` to its own child script) into the CK-4 branch. The newest full receipt, `evidence/ck_gates_d80363cc871372963743ae85fc08c6fa4608f9870c4e06e8988f21950c73ace6.json` (full `ck_gates.sh` run at 741b2b8, the CK-4 branch after merging main #221 and #225; log CK-4-184524.log, `AIENOS_CK_GATES: NOT_ALL_GATES_PASS (pass=10 fail=0 not_run=5 of 15; physical NOT_RUN)`), predates that merge: by it, 10 of 16 CK gates PASS (the 9 of the earlier receipt plus `DISK_LAYOUT`), 6 NOT_RUN (M0_ROLLBACK, M4_STORE_CRASH, M4_CONTINUITY, M4_RECOVERY, KEYBOARD; and SMP, not in that receipt, NOT_RUN pending forge receipt), 0 FAIL. No `ck_gates.sh` run exists at the merged head yet; `M4_STORE_CRASH` stays NOT_RUN until a forge receipt. `DISK_LAYOUT` also PASSed alone (log CK-4-184423.log, translation-bypass mutant killed); the Store, NET and artifact gates boot GPT images now and PASS in the same receipt. The receipt before it was 9 of 14 CK gates PASS, 5 NOT_RUN, 0 FAIL (`evidence/ck_gates_929f287e9c950c45c7dcd569c7caa03709ea62ecfe393635b435058afa969c54.json`, run at 41aa0e6, which adds the kernel entropy rows 107-109; that commit was then rebased onto #210, which changed only README.md and CONTRIBUTING.md). The row counts below are per Rust-parity row, not per gate. Rows 118-123 (DISK_LAYOUT, C-only, CK-4; numbered 113-118 on the CK-4 branch before the merge with main, which gave 113-117 to SMP) are 5 QEMU PASS (118-122, receipt `ck_gates_d80363...` at 741b2b8, before the merge with main 8555049) and 1 host-test PASS (123), none NOT_RUN. With them, rows 1-123 plus the M4 continuity/recovery sub-rows: IDENTICAL 28, DIFFERS 39, NOT_RUN 91, PASS 6 (C-only DISK_LAYOUT, hardware NOT_RUN) (91 from the SMP line next; recounted from the table at the merge commit, status = each row's first status cell, escaped pipes read as text: 28 IDENTICAL, 39 DIFFERS, 87 NOT_RUN (82 + SMP 113-117), 9 QEMU PASS (NET 102, 110-112, counted as NOT_RUN as below, and DISK_LAYOUT 118-122), 1 PASS (123); sub-rows 24a and 24b not counted). diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 4475bc42..58de2b3c 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -391,7 +391,7 @@ KLDFLAGS := -nostdlib -pie --no-dynamic-linker -z norelro -z noexecstack -z max- CORE_C := $(BOOT)/efi_main.c $(BOOT)/efi_model.c $(BOOT)/efi_gop.c \ $(KDIR)/arch/exception.c $(KDIR)/arch/gic.c $(KDIR)/arch/timer.c \ - $(KDIR)/core/acpi.c $(KDIR)/core/console.c $(KDIR)/core/fbcon.c $(KDIR)/core/fmt.c \ + $(KDIR)/core/acpi.c $(KDIR)/core/acpi_dev.c $(KDIR)/core/acpi_platform.c $(KDIR)/core/console.c $(KDIR)/core/fbcon.c $(KDIR)/core/fmt.c \ $(KDIR)/core/kmain.c $(KDIR)/core/handoff_check.c $(KDIR)/core/libc_min.c $(KDIR)/core/report.c \ $(KDIR)/core/smmu.c $(KDIR)/core/smmu_svc.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c \ $(KDIR)/core/fpu.c $(KDIR)/core/infer.c $(KDIR)/core/m3.c $(KDIR)/core/entropy.c $(KDIR)/arch/rndr.c $(KDIR)/core/smp.c \ @@ -654,8 +654,8 @@ $(FULL_EFI): $(FULL_ELF) # ---- host unit tests ---- HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/mm/frames.c \ $(KDIR)/mm/pt.c $(KDIR)/mm/heap.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c $(KDIR)/core/entropy.c \ - $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c -TESTS := test_fmt test_frames test_pt test_acpi test_heap test_smmu test_sched test_ipc test_artifact test_entropy test_handoff test_fbcon \ + $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/acpi_dev.c +TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_entropy test_handoff test_fbcon \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ test_continuity_subject test_continuity_subject_provision HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) @@ -681,8 +681,19 @@ handoff-mutant: $(OUT)/host/test_handoff_mutant @if $< > $<.log 2>&1; then cat $<.log; echo "test_handoff mutant (reserved0 check removed): PASSED, test does not catch it"; exit 1; \ else grep -F "test_handoff: FAIL" $<.log && echo "test_handoff mutant (reserved0 check removed): FAILS as required"; fi -test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant test-continuity-subject-restart - @fail=0; for t in $(filter-out handoff-mutant test-continuity-subject-restart,$^); do $$t || fail=1; done; \ +# TEST-ONLY mutants of core/acpi_dev.c (NEXT-PHASE-3 cut 2): 1 = table +# signature check removed, 2 = checksum check removed, 3 = length-versus- +# buffer check removed (truncated table trusted). test_acpi_dev must FAIL +# against each. Host only (acpi_dev.c refuses CK_AML_MUTANT freestanding). +acpi-dev-mutant: $(KDIR)/tests/test_acpi_dev.c $(HOST_SRCS) $(KDIR)/tests/ck_test.h $(HDRS) + @mkdir -p $(OUT)/host + @for m in 1 2 3; do b=$(OUT)/host/test_acpi_dev_mutant$$m; \ + $(HOSTCC) $(HCFLAGS) -DCK_AML_MUTANT=$$m -o $$b $(KDIR)/tests/test_acpi_dev.c $(HOST_SRCS) || exit 1; \ + if $$b > $$b.log 2>&1; then cat $$b.log; echo "test_acpi_dev mutant $$m: PASSED, test does not catch it"; exit 1; \ + else grep -F "test_acpi_dev: FAIL" $$b.log >/dev/null && echo "test_acpi_dev mutant $$m: FAILS as required" || { cat $$b.log; exit 1; }; fi; done + +test: $(addprefix $(OUT)/host/,$(TESTS)) handoff-mutant acpi-dev-mutant test-continuity-subject-restart + @fail=0; for t in $(filter-out handoff-mutant acpi-dev-mutant test-continuity-subject-restart,$^); do $$t || fail=1; done; \ if [ $$fail = 0 ]; then echo "CK_CORE_HOST: PASS"; else echo "CK_CORE_HOST: FAIL"; exit 1; fi sanitize: $(addprefix $(OUT)/host-san/,$(TESTS)) @@ -1034,6 +1045,18 @@ $(FB_CHECK_TOOL): $(KDIR)/tools/ck_fb_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/ $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -o $@ $(KDIR)/tools/ck_fb_check.c $(KDIR)/core/fbcon.c fb-check: $(FB_CHECK_TOOL) +# ---- platform xHCI discovery over dumped firmware tables (NEXT-PHASE-3 cut 2) ---- +# Runs the kernel scan (core/acpi_dev.c) and IORT lookup (core/acpi.c) over +# DSDT/SSDT/IORT files read under Linux and prints the lines the kernel +# fence prints (dev/xhci_acpi_fmt.h). Hosted; never part of an image. +ACPI_SCAN_TOOL := $(OUT)/host/ck_acpi_scan +$(ACPI_SCAN_TOOL): $(KDIR)/tools/ck_acpi_scan.c $(KDIR)/core/acpi_dev.c $(KDIR)/core/acpi.c \ + $(KDIR)/core/acpi_dev.h $(KDIR)/core/acpi.h $(KDIR)/dev/xhci_acpi_fmt.h + @mkdir -p $(dir $@) + $(HOSTCC) -std=gnu11 -O2 -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/dev -I$(BOOT) -o $@ \ + $(KDIR)/tools/ck_acpi_scan.c $(KDIR)/core/acpi_dev.c $(KDIR)/core/acpi.c +acpi-scan: $(ACPI_SCAN_TOOL) + # Owner provisioning generator (hosted; never part of an image) and the # generated header for the hardware staging image. OWNER_GEN := $(OUT)/host/ck_owner_gen @@ -1051,4 +1074,4 @@ clean: rm -rf $(OUT) FORCE: -.PHONY: all full test sanitize handoff-mutant clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE +.PHONY: all full test sanitize handoff-mutant acpi-dev-mutant acpi-scan clean print-kcflags udp-echo tool store-image gpt-image cont-tool owner-gen fb-check continuity-mutants continuity-resolve-mutants FORCE diff --git a/native/kernel/README.md b/native/kernel/README.md index 0e0893d4..3f2e2718 100644 --- a/native/kernel/README.md +++ b/native/kernel/README.md @@ -97,7 +97,7 @@ the operator-facing warning: `docs/TRUST-1-OPERATOR-STEPS.md`. ## Operator input and recovery access (C kernel) -Status: **QEMU only.** CK gate `KEYBOARD` (rows 25-32 and 30a-30c in +Status: **QEMU only.** CK gate `KEYBOARD` (rows 25-32, 30a-30c, 32a and 32b in `GATES.md`) runs in QEMU; no physical keyboard, xHCI controller or SMMU has been exercised by this code. Hardware NOT_RUN. @@ -139,33 +139,95 @@ What is implemented (NEXT-PHASE-3 cut 1): Bus. The Rust kernel and both QEMU gates use a PCI xHCI controller (`qemu-xhci`, class 0x0c0330) with a `usb-kbd` attached. The C driver looks -only there. - -**The Spark's USB controllers are not on PCI.** Read on this Spark under -Ubuntu on 2026-10-05 (sysfs, read-only): no PCI function has class 0x0c03. -The USB buses hang off six ACPI platform devices, `NVDA8000:00` to -`NVDA8000:04` and `NVDA8001:00`, bound by Linux `xhci_plat_hcd` through the -ACPI id `PNP0D15` (an xHCI controller), each behind the SMMUv3 at -0x13800000. The DGX Spark hardware guide lists four USB Type-C ports on the -rear panel and is silent on the controller behind them. Consequence -(INFERRED, high confidence): on the Spark this cut finds no controller and -prints `keyboard: unavailable (no xHCI controller)` and `recovery_access: -unavailable (no operator keyboard: xhci=absent); choice=normal -reason=no-keyboard`. The xHCI register work in `dev/usb_kbd.c` follows the -xHCI specification and should carry over (UNVERIFIED, medium confidence), -but the Spark needs discovery from the ACPI tables (DSDT `PNP0D15` devices -and their IORT stream ids) instead of the PCI walk. That is the next cut, -not this one. Which of the six controllers serves which rear port is +there first, then at the ACPI platform controllers (below). + +**The Spark's USB controllers are not on PCI** (MEASURED under Ubuntu on +2026-10-05, read only: no PCI function has class 0x0c03). They are six ACPI +platform devices, bound by Linux `xhci_plat_hcd` through the ACPI id +`PNP0D15` (an xHCI controller). The DSDT describes each one as +`Device (USBn) { Name (_HID, "NVDA8000") Name (_CID, "PNP0D15") ... +Method (_CRS) { Name (RBUF, Buffer () { Memory32Fixed ... }) Return (RBUF) } }` +(`_DSD` names a MediaTek xHCI, `xhci-nvidia-mediatek-host`). The DGX Spark +hardware guide lists four USB Type-C ports on the rear panel and is silent on +the controllers behind them. Which controller serves which rear port is UNKNOWN. +Platform xHCI discovery (NEXT-PHASE-3 cut 2). The fence now tries the PCI +xHCI first (QEMU, and any machine with one) and, when there is none or it +has no keyboard, the ACPI platform controllers: + +- **Walker** (`core/acpi_dev.c`, `core/acpi_platform.c`): a bounded static + scan of the DSDT (from the FADT) and every SSDT in the XSDT for `Device` + objects with `_HID` or `_CID` in `NVDA8000`, `NVDA8001`, `PNP0D10`, + `PNP0D15`, reading the first memory range of a constant `_CRS` template. + Nothing is executed: `_STA` is not evaluated. A table with a bad + signature, a bad checksum or a length past what is mapped is refused and + counted (`acpi_scan: tables=N refused=R first_refusal=E devices=M`). + Positive control on every boot: the Arm PL011 UART id `ARMH0011`. +- **SMMU stream by ACPI name** (`ck_dma_confine_named`, `core/smmu_svc.c`): + the IORT named component whose name ends in the device's name gives the + stream id, as the IORT root-complex mapping does for the NVMe and the PCI + xHCI. Two components with that name, no single-id mapping, or a stream + behind an SMMU other than the one this kernel drives (`OtherSmmu`) are + refused: the controller is left halted and gets no DMA. +- **Per controller** (`dev/xhci_fence.c`): map the range, halt whatever the + firmware left running, ask for the confined window, run the same keyboard + phase, then halt, reset (HCRST) and return the stream to abort. A platform + controller has no bus-master bit; the SMMU stream is the gate. + +Spark firmware tables (MEASURED, read only with `sudo cat +/sys/firmware/acpi/tables/`; acpidump and iasl are not installed; +decoded by `tools/ck_acpi_scan.c`, which runs the kernel's own scan; Linux +names and SMMU placement from `/sys`): + +| Linux device | ACPI name | `_HID` / `_CID` | main MMIO (`_CRS` first range) | IORT stream | SMMUv3 | +| --- | --- | --- | --- | --- | --- | +| NVDA8000:00 | `\_SB_.USB0` | NVDA8000 / PNP0D15 | 0x1db60000 + 0x7800 | 0x0 | 0x13800000 (first in IORT) | +| NVDA8000:01 | `\_SB_.USB1` | NVDA8000 / PNP0D15 | 0x1db90000 + 0x7800 | 0x1 | 0x13800000 | +| NVDA8000:02 | `\_SB_.USB2` | NVDA8000 / PNP0D15 | 0x1dde0000 + 0x7800 | 0x2 | 0x13800000 | +| NVDA8000:03 | `\_SB_.USB3` | NVDA8000 / PNP0D15 | 0x1de10000 + 0x7800 | 0x3 | 0x13800000 | +| NVDA8001:00 | `\_SB_.USB4` | NVDA8001 / PNP0D15 | 0x1d860000 + 0x7800 | 0x4 | 0x13800000 | +| NVDA8000:04 | `\_SB_.USB5` | NVDA8000 / PNP0D15 | 0x1d870000 + 0x7800 | 0x5 | 0x13800000 | + +Each `_CRS` has three more memory ranges (+0x8000 size 0x100, a 0x4000 or +0x5000 block, and a 0x18-byte register window), an interrupt and a GPIO +interrupt; the walker takes only the first, the xHCI register block that +Linux `xhci_plat_hcd` maps. Table digests are in +`tests/fixtures/spark_xhci_acpi_expected.txt`. + +**What the Spark will print** (INFERRED from those tables, high confidence; +hardware NOT_RUN): the walker finds all six controllers, and every one is +then **denied** with `dma_gate: xhci USBn denied (SmmuNotReady), controller +left halted`. The reason is not USB: the kernel's SMMU service refuses the +Spark's IORT as a whole (`smmu: IORT malformed, SMMU unusable (fail +closed)`), because the first SMMUv3 (0x13800000, the one behind the USB +controllers) has 15 PCI root-complex mappings (the service keeps at most 8) +with stream ids up to 0xfffff (its linear stream table holds 0x0-0xfff). +The same refusal already applies to the NVMe on the Spark. The USB streams +themselves (0x0-0x5) would fit. So on the Spark this cut still gives +`keyboard: unavailable (no keyboard on any platform xHCI)` and normal boot, +now for a stated reason, and fails closed. Making the SMMU service accept +the Spark IORT (a two-level stream table, more root-complex mappings) is +the next cut. Also UNKNOWN until a hardware run: whether the MediaTek glue +around each controller is powered and clocked when the kernel reads its +registers (Linux handles that in firmware `_STA`/`_PS0` and its own glue +driver; reading a powered-down block could fault), and whether +`ck_mm_mmio_try_map` accepts these ranges. The exact expected lines are in +`tests/fixtures/spark_xhci_acpi_expected.txt` (KEYBOARD row 32b, NOT_RUN). + + ### Physical attended boot: what it will look like -Do not run this until two things exist, because without them the run proves -only the "no keyboard" line above: (1) ACPI discovery of the Spark's -platform xHCI controllers, and (2) a staging command for the full hardware -image (today `scripts/stage_one_time_boot_ck.sh` stages only the inference -image, which has no devices stage and so no keyboard). The steps below are -the procedure that run will follow, so it can be reviewed now. +Both prerequisites named in cut 1 now exist: ACPI discovery of the Spark's +platform xHCI controllers (above) and a staging command for the full +hardware image, `scripts/stage_one_time_boot_ck_full.sh` (`--dry-run` prints +every command and touches nothing; `--apply` builds `make full +CK_HARDWARE_STAGING=1` with the operator's own owner files, copies it to +`\EFI\AIENOS\aienos-ck-full.efi`, and sets BootNext once; it never +reboots). Do not run it yet: until the SMMU service accepts the Spark IORT, +the run can only show the fail-closed deny lines above, not a keyboard. +The steps below are the procedure that run will follow, so it can be +reviewed now. Ground rules: nothing here changes firmware settings, Secure Boot keys or the TPM. The boot is one-time (BootNext): the next restart returns to Ubuntu on @@ -184,9 +246,10 @@ improvise. *You should see:* nothing changes on screen yet. -2. **Stage the one-time boot** with the staging command the orchestrator - names for that run (it will be a `--dry-run` first, then `--apply`, in the - style of `scripts/stage_one_time_boot_ck.sh`). *Changes:* it copies the +2. **Stage the one-time boot** with `scripts/stage_one_time_boot_ck_full.sh`, + `--dry-run` first, then `--apply`, with `AIENOS_OWNER_PUBKEYS` and + `AIENOS_MACHINE_ID` set to the operator's own files (the orchestrator + names them for that run; the TEST-FIXTURE files are refused). *Changes:* it copies the image to the EFI system partition and sets BootNext once; it never reboots. *You should see:* the dry run's list of commands, then the apply ending without `FAIL`. @@ -195,7 +258,8 @@ improvise. shows AIENOS lines (white text on black, starting with the boot report). 4. **Normal boot (do nothing).** Watch for, in order: - `keyboard: xhci ...`, `dma_gate: xhci granted (Confined), bus master on`, + `xhci_acpi: 6 platform controller(s)`, `xhci_plat: USBn try ...`, + `dma_gate: xhci USBn granted (Confined) smmu=0x13800000 stream=0xn`, `keyboard: ready (port P, slot S, endpoint 0x81)`, then `recovery_access: waiting 5000 ms ...`. Keep your hands off for those 5 seconds. *You should see:* `recovery_access: choice=normal reason=timeout`, diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index ff5d2c4c..79031c73 100644 --- a/native/kernel/core/.crumb +++ b/native/kernel/core/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T01:02:05Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:e5ebaf34e03225eabc74a1b335f9aea4635942690cd0d8aeb22cfa6addd4e833", + "digest": "sha256:af2d1e1f63f7489840073bbe10705d8cae0c3886fb0768c829940a33e71490d4", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 28, - "generated_at_commit": "20587476bf0d4b7577cc3b62857a9166300681bf", - "source_tree": "sha256:d856e6b4eb46f3348ada53d964701e8a69c5f58a832db6c0265c9e49e3e43d2f" + "files": 31, + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:3b03b6caeaf2211d6f3e0e4cb6eafffbdc576c39d99fff6a7de1de5f05a325f9" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/acpi.c b/native/kernel/core/acpi.c index f214d38d..2296aeca 100644 --- a/native/kernel/core/acpi.c +++ b/native/kernel/core/acpi.c @@ -398,3 +398,92 @@ int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t rid, uint32_t *sid) } return -1; } + +/* ---- IORT named components (platform devices such as the DGX Spark USB + * controllers, ACPI ids NVDA8000/NVDA8001) ---- */ +#define IORT_NODE_NAMED 1u +#define IORT_NAMED_NAME_OFF 29u /* flags u32 @16, memory properties u64 @20, address size u8 @28, name @29 */ +#define IORT_MAP_SINGLE 0x1u + +/* Final segment of an ASCII object path, padded to 4 with '_'; -1 if a + * segment is longer than 4 or the path is empty. */ +static int last_seg4(const char *p, const char *end, char seg[4]) +{ + const char *s = p; + for (const char *q = p; q < end && *q; q++) + if (*q == '.' || *q == '\\' || *q == '^') s = q + 1; + int i = 0; + for (; s < end && *s && i < 4; i++, s++) seg[i] = *s; + if (i == 0 || (s < end && *s)) return -1; + for (; i < 4; i++) seg[i] = '_'; + return 0; +} + +int ck_iort_named(const void *iort, const char *name, struct ck_iort_named *out) +{ + const uint8_t *t = iort; + uint32_t len = rd32(t + 4), count, at; + if (!name || iort_nodes(t, len, &count, &at)) + return -1; + char want[4]; + if (last_seg4(name, name + 64, want)) + return -1; + int found = 0; + struct ck_iort_named r = { 0 }; + uint32_t off = at; + for (uint32_t i = 0; i < count; i++) { + if (off < IORT_TABLE_HDR || off > len || len - off < IORT_NODE_HDR) + return -1; + const uint8_t *n = t + off; + uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; + if (nlen < IORT_NODE_HDR || nlen > len - off) + return -1; + uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); + if (mcount && (mcount > nlen / IORT_MAP_BYTES || moff < IORT_NODE_HDR || moff > nlen || + (uint64_t)mcount * IORT_MAP_BYTES > (uint64_t)(nlen - moff))) + return -1; + if (n[0] == IORT_NODE_NAMED) { + if (nlen <= IORT_NAMED_NAME_OFF) + return -1; + const char *nm = (const char *)(n + IORT_NAMED_NAME_OFF); + const char *nend = (const char *)(n + (mcount ? moff : nlen)); + if (nend <= nm) + return -1; + char seg[4]; + if (last_seg4(nm, nend, seg) == 0 && seg[0] == want[0] && seg[1] == want[1] && seg[2] == want[2] && + seg[3] == want[3]) { + if (found) + return -2; /* two named components carry this name: refuse */ + found = 1; + r.node_off = off; + uint32_t k = 0; + for (; k < sizeof r.name - 1 && nm + k < nend && nm[k]; k++) + r.name[k] = nm[k]; + r.name[k] = 0; + r.nmaps = mcount; + /* The stream: a single mapping, else a mapping of exactly one id. */ + r.target_off = 0; + for (uint32_t m = 0; m < mcount; m++) { + const uint8_t *e = n + moff + m * IORT_MAP_BYTES; + uint32_t ref = rd32(e + 12); + if (ref < IORT_TABLE_HDR || ref > len || len - ref < IORT_NODE_HDR) + return -1; + if (t[ref] != IORT_NODE_SMMUV3) + continue; + if ((rd32(e + 16) & IORT_MAP_SINGLE) || rd32(e + 4) == 0) { + r.stream_id = rd32(e + 8); + r.target_off = ref; + uint32_t tlen = (uint32_t)t[ref + 1] | (uint32_t)t[ref + 2] << 8; + r.target_base = tlen >= 24 && tlen <= len - ref ? rd64(t + ref + 16) : 0; + break; + } + } + } + } + off += nlen; + } + if (!found) + return 0; + *out = r; + return 1; +} diff --git a/native/kernel/core/acpi.h b/native/kernel/core/acpi.h index 137d33f8..feba46be 100644 --- a/native/kernel/core/acpi.h +++ b/native/kernel/core/acpi.h @@ -106,4 +106,20 @@ int ck_iort_parse(const void *iort, struct ck_iort_smmu *out); * no mapping covers it. */ int ck_iort_stream_id(const struct ck_iort_smmu *s, uint32_t rid, uint32_t *sid); +/* IORT named component (node type 1) whose device object name has the same + * final segment as `name` ("USB0" matches "\\_SB_.USB0"). out gets the node + * name, the stream id of its single (or one-id) mapping to an SMMUv3 node, + * and that SMMUv3 node (offset, register base). target_off == 0: no such + * mapping (no stream for this device). Returns 1 found, 0 no such node, -1 + * malformed (signature, truncation, node, name or mapping outside the + * table), -2 ambiguous (two named components with that final segment). */ +struct ck_iort_named { + char name[48]; + uint32_t node_off, nmaps; + uint32_t stream_id; + uint32_t target_off; /* SMMUv3 node the stream belongs to, 0 if none */ + uint64_t target_base; +}; +int ck_iort_named(const void *iort, const char *name, struct ck_iort_named *out); + #endif diff --git a/native/kernel/core/acpi_dev.c b/native/kernel/core/acpi_dev.c new file mode 100644 index 00000000..c06e1aa7 --- /dev/null +++ b/native/kernel/core/acpi_dev.c @@ -0,0 +1,376 @@ +/* acpi_dev.c -- bounded static scan of DSDT/SSDT Device objects (see + * acpi_dev.h). Freestanding; host tested in tests/test_acpi_dev.c. */ +#include "acpi_dev.h" + +/* TEST-ONLY code mutants (host test tests/test_acpi_dev.c must FAIL against + * each; Makefile target acpi-dev-mutant): 1 = signature check removed, + * 2 = checksum check removed, 3 = length-versus-buffer check removed. Never + * in a kernel image or a hardware staging build. */ +#ifdef CK_AML_MUTANT +#if !__STDC_HOSTED__ +#error "CK_AML_MUTANT is a host test mutant only" +#endif +#ifdef CK_HARDWARE_STAGING +#error "CK_AML_MUTANT cannot be combined with CK_HARDWARE_STAGING" +#endif +#else +#define CK_AML_MUTANT 0 +#endif + +#define AML_HDR 36u +#define OP_NAME 0x08u +#define OP_ZERO 0x00u +#define OP_ONE 0x01u +#define OP_BYTE 0x0au +#define OP_WORD 0x0bu +#define OP_DWORD 0x0cu +#define OP_STRING 0x0du +#define OP_QWORD 0x0eu +#define OP_BUFFER 0x11u +#define OP_METHOD 0x14u +#define OP_EXT 0x5bu +#define OP_EXT_DEVICE 0x82u +#define PFX_ROOT 0x5cu /* '\\' */ +#define PFX_PARENT 0x5eu /* '^' */ +#define PFX_DUAL 0x2eu +#define PFX_MULTI 0x2fu + +static uint32_t rd16(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8; } +static uint32_t rd32(const uint8_t *p) { return rd16(p) | rd16(p + 2) << 16; } +static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; } + +static int str_eq(const char *a, const char *b) +{ + while (*a && *a == *b) a++, b++; + return *a == *b; +} + +int ck_aml_table_ok(const void *table, size_t avail) +{ + const uint8_t *t = table; + if (!t || avail < AML_HDR) return CK_AML_E_LEN; + int dsdt = t[0] == 'D' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T'; + int ssdt = t[0] == 'S' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T'; + if (!dsdt && !ssdt && CK_AML_MUTANT != 1) return CK_AML_E_SIG; + uint32_t len = rd32(t + 4); + if (len < AML_HDR) return CK_AML_E_LEN; + if (len > avail && CK_AML_MUTANT != 3) return CK_AML_E_LEN; + if (CK_AML_MUTANT == 3 && len > avail) len = (uint32_t)avail; /* mutant: trusts a truncated table */ + uint8_t sum = 0; + for (uint32_t i = 0; i < len; i++) sum = (uint8_t)(sum + t[i]); + return sum && CK_AML_MUTANT != 2 ? CK_AML_E_SUM : CK_AML_OK; +} + +/* PkgLength at p (ACPI 6.5 20.2.4): *value is the package length (it counts + * its own encoding bytes), *nbytes the encoding size. -1 if past end. */ +static int pkg_length(const uint8_t *p, const uint8_t *end, uint32_t *value, uint32_t *nbytes) +{ + if (p >= end) return -1; + uint32_t follow = p[0] >> 6; + if ((uint32_t)(end - p) < follow + 1u) return -1; + if (follow == 0) { + *value = p[0] & 0x3fu; + } else { + if (p[0] & 0x30u) return -1; /* bits 5:4 must be zero in the multi-byte form */ + uint32_t v = p[0] & 0x0fu; + for (uint32_t i = 0; i < follow; i++) v |= (uint32_t)p[1 + i] << (4u + 8u * i); + *value = v; + } + *nbytes = follow + 1u; + return 0; +} + +static int seg_char(uint8_t c, int first) +{ + if (c == '_' || (c >= 'A' && c <= 'Z')) return 1; + return !first && c >= '0' && c <= '9'; +} +static int name_seg_ok(const uint8_t *s) +{ + return seg_char(s[0], 1) && seg_char(s[1], 0) && seg_char(s[2], 0) && seg_char(s[3], 0); +} + +/* NameString at p into out ("\\", "^" prefixes kept, segments joined by + * '.'). Returns the encoded size, or 0 if malformed / past end / too long. */ +static uint32_t name_string(const uint8_t *p, const uint8_t *end, char *out, uint32_t cap) +{ + const uint8_t *q = p; + uint32_t o = 0; + if (q < end && *q == PFX_ROOT) { + if (o + 1 >= cap) return 0; + out[o++] = '\\'; + q++; + } else { + while (q < end && *q == PFX_PARENT) { + if (o + 1 >= cap) return 0; + out[o++] = '^'; + q++; + } + } + if (q >= end) return 0; + uint32_t segs; + if (*q == PFX_DUAL) { + segs = 2; + q++; + } else if (*q == PFX_MULTI) { + if (end - q < 2) return 0; + segs = q[1]; + q += 2; + if (segs == 0) return 0; + } else if (*q == 0x00) { + segs = 0; /* NullName */ + q++; + } else { + segs = 1; + } + for (uint32_t s = 0; s < segs; s++) { + if (end - q < 4 || !name_seg_ok(q)) return 0; + if (o + (s ? 5u : 4u) >= cap) return 0; + if (s) out[o++] = '.'; + for (int i = 0; i < 4; i++) out[o++] = (char)q[i]; + q += 4; + } + out[o] = 0; + return (uint32_t)(q - p); +} + +void ck_aml_eisaid(uint32_t v, char out[8]) +{ + /* Stored little-endian; the id is the big-endian reading of the DWORD. */ + uint32_t b = ((v & 0xffu) << 24) | ((v & 0xff00u) << 8) | ((v >> 8) & 0xff00u) | (v >> 24); + static const char hex[] = "0123456789ABCDEF"; + out[0] = (char)('@' + ((b >> 26) & 0x1fu)); + out[1] = (char)('@' + ((b >> 21) & 0x1fu)); + out[2] = (char)('@' + ((b >> 16) & 0x1fu)); + out[3] = hex[(b >> 12) & 0xfu]; + out[4] = hex[(b >> 8) & 0xfu]; + out[5] = hex[(b >> 4) & 0xfu]; + out[6] = hex[b & 0xfu]; + out[7] = 0; +} + +/* The value of Name(XXXX, ...) for _HID / _CID: a String or an EisaId + * DWORD. 0 ok, -1 other forms (packages, integers other than DWORD). */ +static int id_value(const uint8_t *p, const uint8_t *end, char out[CK_AML_ID_MAX]) +{ + if (p >= end) return -1; + if (p[0] == OP_STRING) { + uint32_t i = 0; + for (const uint8_t *s = p + 1; s < end; s++, i++) { + if (*s == 0) { + out[i] = 0; + return i ? 0 : -1; + } + if (i + 1 >= CK_AML_ID_MAX || *s < 0x20 || *s > 0x7e) return -1; + out[i] = (char)*s; + } + return -1; + } + if (p[0] == OP_DWORD && end - p >= 5) { + ck_aml_eisaid(rd32(p + 1), out); + return 0; + } + return -1; +} + +/* First memory range of a resource template [p, end) (ACPI 6.5 6.4): + * Memory32Fixed, Memory32, DWord / QWord address space of type memory. + * 0 found, -1 none or malformed before one was found. */ +static int first_memory(const uint8_t *p, const uint8_t *end, uint64_t *base, uint64_t *len) +{ + while (p < end) { + uint8_t tag = p[0]; + if (!(tag & 0x80u)) { /* small item */ + uint32_t n = tag & 0x7u; + if ((tag >> 3) == 0x0fu) return -1; /* End Tag: no memory range */ + if ((uint32_t)(end - p) < 1u + n) return -1; + p += 1u + n; + continue; + } + if (end - p < 3) return -1; + uint32_t n = rd16(p + 1); + if ((uint32_t)(end - p) < 3u + n) return -1; + const uint8_t *d = p + 3; + switch (tag) { + case 0x86: /* Memory32Fixed: info, base, length */ + if (n >= 9) { + *base = rd32(d + 1); + *len = rd32(d + 5); + if (*len) return 0; + } + break; + case 0x85: /* Memory32: info, min, max, align, length */ + if (n >= 17 && rd32(d + 1) == rd32(d + 5)) { + *base = rd32(d + 1); + *len = rd32(d + 13); + if (*len) return 0; + } + break; + case 0x87: /* DWord address space: type, gflags, tflags, gran, min, max, tra, len */ + if (n >= 23 && d[0] == 0) { + *base = rd32(d + 7); + *len = rd32(d + 19); + if (*len) return 0; + } + break; + case 0x8a: /* QWord address space */ + if (n >= 43 && d[0] == 0) { + *base = rd64(d + 11); + *len = rd64(d + 35); + if (*len) return 0; + } + break; + default: + break; + } + p += 3u + n; + } + return -1; +} + +/* Buffer object at p: [data, data_end). 0 ok, -1 malformed. */ +static int buffer_at(const uint8_t *p, const uint8_t *end, const uint8_t **data, const uint8_t **data_end) +{ + if (p >= end || p[0] != OP_BUFFER) return -1; + uint32_t plen, pn; + if (pkg_length(p + 1, end, &plen, &pn)) return -1; + const uint8_t *pend = p + 1 + plen; + if (plen < pn || pend > end) return -1; + const uint8_t *q = p + 1 + pn; + uint64_t size; + if (q >= pend) return -1; + switch (q[0]) { + case OP_ZERO: size = 0; q += 1; break; + case OP_ONE: size = 1; q += 1; break; + case OP_BYTE: if (pend - q < 2) return -1; size = q[1]; q += 2; break; + case OP_WORD: if (pend - q < 3) return -1; size = rd16(q + 1); q += 3; break; + case OP_DWORD: if (pend - q < 5) return -1; size = rd32(q + 1); q += 5; break; + default: return -1; + } + if (size > (uint64_t)(pend - q)) size = (uint64_t)(pend - q); /* initializer shorter than the size: rest is zero */ + *data = q; + *data_end = q + size; + return 0; +} + +/* Valid DeviceOp at p: *body / *dend span the device's term list, name in + * path. 0 ok, -1 not a device. */ +static int device_at(const uint8_t *p, const uint8_t *end, const uint8_t **body, const uint8_t **dend, char *path) +{ + if (end - p < 3 || p[0] != OP_EXT || p[1] != OP_EXT_DEVICE) return -1; + uint32_t plen, pn; + if (pkg_length(p + 2, end, &plen, &pn)) return -1; + const uint8_t *pend = p + 2 + plen; + if (plen < pn || pend > end) return -1; + uint32_t nn = name_string(p + 2 + pn, pend, path, CK_AML_PATH_MAX); + if (!nn) return -1; + *body = p + 2 + pn + nn; + *dend = pend; + return 0; +} + +static int seg_is(const uint8_t *p, const uint8_t *end, const char *s) +{ + return end - p >= 4 && p[0] == (uint8_t)s[0] && p[1] == (uint8_t)s[1] && p[2] == (uint8_t)s[2] && p[3] == (uint8_t)s[3]; +} + +/* Reads _HID, _CID and _CRS of the device body [b, e), stopping at the first + * nested device (its objects belong to that device). */ +static void device_fields(const uint8_t *b, const uint8_t *e, struct ck_acpi_device *d) +{ + for (const uint8_t *p = b; p < e; p++) { + const uint8_t *nb, *ne; + char tmp[CK_AML_PATH_MAX]; + if (p[0] == OP_EXT && device_at(p, e, &nb, &ne, tmp) == 0) break; + if (p[0] == OP_NAME && seg_is(p + 1, e, "_HID") && !d->hid[0]) { + if (id_value(p + 5, e, d->hid)) d->hid[0] = 0; + } else if (p[0] == OP_NAME && seg_is(p + 1, e, "_CID") && !d->cid[0]) { + if (id_value(p + 5, e, d->cid)) d->cid[0] = 0; + } else if (p[0] == OP_NAME && seg_is(p + 1, e, "_CRS") && !d->mmio_len) { + const uint8_t *db, *de; + if (buffer_at(p + 5, e, &db, &de) == 0 && first_memory(db, de, &d->mmio_base, &d->mmio_len)) + d->mmio_base = d->mmio_len = 0; + } else if (p[0] == OP_METHOD && !d->mmio_len) { + uint32_t plen, pn; + if (pkg_length(p + 1, e, &plen, &pn) || plen < pn) continue; + const uint8_t *mend = p + 1 + plen; + const uint8_t *mname = p + 1 + pn; + if (mend > e || !seg_is(mname, mend, "_CRS")) continue; + /* first Buffer inside the method body (after name and flags) */ + for (const uint8_t *q = mname + 5; q < mend; q++) { + const uint8_t *db, *de; + if (q[0] != OP_BUFFER || buffer_at(q, mend, &db, &de)) continue; + if (first_memory(db, de, &d->mmio_base, &d->mmio_len)) d->mmio_base = d->mmio_len = 0; + break; + } + } + } +} + +static int id_match(const struct ck_acpi_device *d, const char *const *ids, unsigned nids) +{ + if (!nids) return 1; + for (unsigned i = 0; i < nids; i++) + if ((d->hid[0] && str_eq(d->hid, ids[i])) || (d->cid[0] && str_eq(d->cid, ids[i]))) return 1; + return 0; +} + +int ck_aml_scan(const void *table, size_t avail, const char *const *ids, unsigned nids, + struct ck_acpi_device *out, unsigned max, unsigned *seen) +{ + int rc = ck_aml_table_ok(table, avail); + if (seen) *seen = 0; + if (rc) return rc; + const uint8_t *t = table; + const uint8_t *end = t + rd32(t + 4); + unsigned n = 0, parsed = 0; + for (const uint8_t *p = t + AML_HDR; p + 3 <= end; p++) { + const uint8_t *b, *e; + struct ck_acpi_device d; + for (unsigned i = 0; i < sizeof d; i++) ((uint8_t *)&d)[i] = 0; + if (p[0] != OP_EXT || device_at(p, end, &b, &e, d.path)) continue; + parsed++; + d.offset = (uint32_t)(p - t); + device_fields(b, e, &d); + if (!id_match(&d, ids, nids)) continue; + if (n < max && out) out[n] = d; + n++; + } + if (seen) *seen = parsed; + return (int)n; +} + +/* Copies one segment (up to 4 chars, padded with '_'); NULL if longer. */ +static const char *seg4(const char *p, char seg[4]) +{ + int i = 0; + for (; *p && *p != '.' && i < 4; i++, p++) seg[i] = *p; + if (*p && *p != '.') return 0; + for (; i < 4; i++) seg[i] = '_'; + return p; +} + +int ck_aml_path_eq(const char *a, const char *b) +{ + for (;;) { + while (*a == '\\' || *a == '.') a++; + while (*b == '\\' || *b == '.') b++; + if (!*a || !*b) return !*a && !*b; + char sa[4], sb[4]; + a = seg4(a, sa); + b = seg4(b, sb); + if (!a || !b) return 0; + for (int i = 0; i < 4; i++) + if (sa[i] != sb[i]) return 0; + } +} + +int ck_aml_last_seg_eq(const char *a, const char *b) +{ + const char *la = a, *lb = b; + for (const char *p = a; *p; p++) + if (*p == '.' || *p == '\\' || *p == '^') la = p + 1; + for (const char *p = b; *p; p++) + if (*p == '.' || *p == '\\' || *p == '^') lb = p + 1; + return *la && *lb && ck_aml_path_eq(la, lb); +} diff --git a/native/kernel/core/acpi_dev.h b/native/kernel/core/acpi_dev.h new file mode 100644 index 00000000..82b6f03e --- /dev/null +++ b/native/kernel/core/acpi_dev.h @@ -0,0 +1,69 @@ +/* acpi_dev.h -- bounded static scan of ACPI definition blocks (DSDT, SSDT) + * for Device objects: name path, _HID / _CID and the first memory range of + * a constant _CRS resource template. Pure functions over memory (identity + * mapped in the kernel, host buffers in the tests and the host tool). + * + * This is a scanner, not an AML interpreter: nothing is executed. It reads + * Device (NAME) { Name (_HID, "STRING" | EisaId) Name (_CID, ...) + * Name (_CRS, Buffer) | Method (_CRS) { ... Buffer ... } } + * where the _CRS buffer is a constant resource template (the first buffer + * inside a _CRS method is taken; the DGX Spark USB controllers and the QEMU + * virt UART use exactly these forms, see native/kernel/README.md). _STA is + * never evaluated: a device the firmware would report absent is still + * listed, and the caller must cope with a controller that does not answer. + * Every read is bounds checked against the table length; the table itself + * must pass signature, length and checksum checks first. + * Spec: ACPI 6.5 sections 5.2.11 (DSDT/SSDT), 6.1.5 (_HID), 6.4 (resource + * descriptors), 20.2 (AML grammar: DefDevice, PkgLength, NameString). */ +#ifndef AIENOS_CK_ACPI_DEV_H +#define AIENOS_CK_ACPI_DEV_H +#include +#include + +#define CK_AML_PATH_MAX 48u /* "\\" + up to 9 segments "XXXX." + NUL */ +#define CK_AML_ID_MAX 16u /* _HID / _CID string, NUL terminated */ + +struct ck_acpi_device { + char path[CK_AML_PATH_MAX]; /* name as written at the DeviceOp, usually relative ("USB0", "COM0") */ + char hid[CK_AML_ID_MAX]; /* "" when the device has no _HID */ + char cid[CK_AML_ID_MAX]; /* "" when no _CID */ + uint64_t mmio_base; /* first memory range of _CRS, 0 when none */ + uint64_t mmio_len; + uint32_t offset; /* DeviceOp offset in the table */ +}; + +enum { + CK_AML_OK = 0, + CK_AML_E_SIG = -1, /* signature is not DSDT or SSDT */ + CK_AML_E_LEN = -2, /* header length shorter than 36 or longer than the buffer */ + CK_AML_E_SUM = -3, /* checksum does not sum to zero */ +}; + +/* Checks a definition block: signature DSDT or SSDT, 36 <= length <= avail, + * byte sum zero. CK_AML_OK or a CK_AML_E_*. */ +int ck_aml_table_ok(const void *table, size_t avail); + +/* Scans one checked definition block. For every Device object whose _HID or + * _CID equals one of ids[0..nids) (nids == 0: every device), fills out[n] + * while n < max. Returns the number of matching devices (it may exceed max; + * only max are stored), or a CK_AML_E_* for a table that fails + * ck_aml_table_ok. *seen (may be NULL) gets the number of Device objects + * parsed, matching or not. A malformed device (package past the end, bad + * name) is skipped, never followed past the table. */ +int ck_aml_scan(const void *table, size_t avail, const char *const *ids, unsigned nids, + struct ck_acpi_device *out, unsigned max, unsigned *seen); + +/* Decodes a compressed EISA id (the DWORD form of EisaId("PNP0D15")) into 7 + * characters + NUL. */ +void ck_aml_eisaid(uint32_t v, char out[8]); + +/* Final name segments equal (each padded to 4 with '_'): "USB0" matches "\\_SB_.USB0". + * The scan reports each device by the name written at its DeviceOp, which is + * usually relative to an enclosing Scope; callers match IORT names this way + * and must refuse a name that more than one device carries. */ +int ck_aml_last_seg_eq(const char *a, const char *b); + +/* Same name, ignoring a leading "\\" and padding each dot-separated segment + * to 4 characters with '_' ("\\_SB.USB0" equals "_SB_.USB0"). */ +int ck_aml_path_eq(const char *a, const char *b); +#endif diff --git a/native/kernel/core/acpi_platform.c b/native/kernel/core/acpi_platform.c new file mode 100644 index 00000000..fa6eac13 --- /dev/null +++ b/native/kernel/core/acpi_platform.c @@ -0,0 +1,93 @@ +/* acpi_platform.c -- ck_acpi_platform_devices (ck.h): the DSDT named by the + * FADT plus every SSDT the XSDT lists, each scanned by core/acpi_dev.c. + * Kernel only; the scan itself is host tested (tests/test_acpi_dev.c) and + * run over real firmware tables by tools/ck_acpi_scan.c. */ +#include "ck_internal.h" +#include "acpi.h" +#include "acpi_dev.h" + +#define SCAN_MAX 8u + +struct scan_ctx { + const char *const *ids; + unsigned nids, max; + struct ck_platform_dev *out; + struct ck_acpi_scan_info info; + int n; +}; + +static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } + +static void copy(char *dst, size_t cap, const char *src) +{ + size_t i = 0; + for (; i + 1 < cap && src[i]; i++) dst[i] = src[i]; + dst[i] = 0; +} + +static void scan_block(struct scan_ctx *c, uint64_t pa) +{ + if (!pa || !ck_mm_mapped(pa, 36)) { + c->info.refused++; + if (!c->info.first_refusal) c->info.first_refusal = CK_AML_E_LEN; + return; + } + const uint8_t *t = (const uint8_t *)(uintptr_t)pa; + uint32_t len = rd32(t + 4); + c->info.tables++; + /* avail is what is mapped: a length that runs past the mapping is a + * truncated table and is refused by the length check. */ + size_t avail = len >= 36 && ck_mm_mapped(pa, len) ? len : 36; + struct ck_acpi_device d[SCAN_MAX]; + unsigned seen = 0; + int rc = ck_aml_scan(t, avail, c->ids, c->nids, d, SCAN_MAX, &seen); + if (rc < 0) { + c->info.refused++; + if (!c->info.first_refusal) c->info.first_refusal = rc; + return; + } + c->info.devices += seen; + for (int i = 0; i < rc; i++) { + if (i < (int)SCAN_MAX && c->n < (int)c->max && c->out) { + struct ck_platform_dev *o = &c->out[c->n]; + copy(o->name, sizeof o->name, d[i].path); + copy(o->hid, sizeof o->hid, d[i].hid); + copy(o->cid, sizeof o->cid, d[i].cid); + o->mmio_base = d[i].mmio_base; + o->mmio_len = d[i].mmio_len; + o->table[0] = (char)t[0], o->table[1] = (char)t[1], o->table[2] = (char)t[2], o->table[3] = (char)t[3]; + o->table[4] = 0; + } + c->n++; + } +} + +static void each_table(uint64_t table, uint32_t len, void *ctx) +{ + (void)len; + if (!table || !ck_mm_mapped(table, 4)) return; + const uint8_t *t = (const uint8_t *)(uintptr_t)table; + if (t[0] == 'S' && t[1] == 'S' && t[2] == 'D' && t[3] == 'T') scan_block(ctx, table); +} + +int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max, + struct ck_acpi_scan_info *info) +{ + struct scan_ctx c = { ids, nids, max, out, { 0, 0, 0, 0 }, 0 }; + const struct ck_handoff *h = ck_handoff_get(); + const uint8_t *fadt = ck_acpi_find("FACP"); + uint64_t dsdt = 0; + if (fadt) { + uint32_t flen = rd32(fadt + 4); + if (flen >= 148) dsdt = (uint64_t)rd32(fadt + 140) | (uint64_t)rd32(fadt + 144) << 32; /* X_DSDT */ + if (!dsdt && flen >= 44) dsdt = rd32(fadt + 40); /* DSDT */ + } + if (!dsdt || !h || !h->rsdp) { + if (info) *info = c.info; + return -1; + } + scan_block(&c, dsdt); + (void)ck_acpi_each(h->rsdp, each_table, &c); + if (info) *info = c.info; + return c.n; +} diff --git a/native/kernel/core/smmu_svc.c b/native/kernel/core/smmu_svc.c index 275e8071..0c50fca1 100644 --- a/native/kernel/core/smmu_svc.c +++ b/native/kernel/core/smmu_svc.c @@ -93,6 +93,8 @@ static int bring_up(void) return 0; } +static int confine_sid(uint32_t sid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out); + int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) { if (!len || (phys & 0xfffu) || (len & 0xfffu) || phys + len < phys) @@ -103,6 +105,37 @@ int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_conf uint32_t sid; if (ck_iort_stream_id(&g.iort, rid, &sid)) return CK_SMMU_NOSTREAM; + return confine_sid(sid, phys, len, out); +} + +int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) +{ + if (!acpi_name || !len || (phys & 0xfffu) || (len & 0xfffu) || phys + len < phys) + return CK_SMMU_EARG; + int rc = bring_up(); + if (rc) + return rc; + const void *iort = ck_acpi_find("IORT"); + struct ck_iort_named nc; + int nrc = iort ? ck_iort_named(iort, acpi_name, &nc) : 0; + if (nrc != 1 || !nc.target_off) + return nrc == -2 ? CK_SMMU_EARG : CK_SMMU_NOSTREAM; + /* Only the SMMU this kernel drives (the IORT's first SMMUv3 node) can + * confine a stream; a stream behind another SMMU is refused, never + * granted unconfined. */ + if (nc.target_off != g.iort.node_off) { + out->smmu_base = nc.target_base; /* reported, never used */ + out->stream_id = nc.stream_id; + out->iova = out->len = 0; + return CK_SMMU_OTHER; + } + return confine_sid(nc.stream_id, phys, len, out); +} + +/* Stage-1 window [phys, phys+len) for stream sid on the SMMU brought up. */ +static int confine_sid(uint32_t sid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) +{ + int rc; if (sid >= STE_N) return CK_SMMU_NOSTREAM; int slot = -1; diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 8546283f..72ac6c9c 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T01:02:24Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:914c089b5c7d4cf9f5ffd86440ae5a8250bb7e518c497aa65e9829797b1f1e57", + "digest": "sha256:6f1c98b95ea2d94ef3d0230a38fd121e9244c8f3f8dc8d65c3cf2957a8bccff1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 24, - "generated_at_commit": "0a3c4a4561b78f46b992596dbbf4cee446c8c9a1", - "source_tree": "sha256:811db9300c61e8a70dce6db0476745c17c1d860495c8921f4dd96470ebffd6cd" + "files": 25, + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:1833f2852366a6eeeb3fab36e98f699f7efd21117b35ca6e3d7cc2bb4345f675" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/usb_kbd.c b/native/kernel/dev/usb_kbd.c index a0a0345c..ae5cbe3a 100644 --- a/native/kernel/dev/usb_kbd.c +++ b/native/kernel/dev/usb_kbd.c @@ -373,6 +373,8 @@ static int poll_report(uint8_t rep[CK_HID_REPORT_LEN]) static int start(volatile uint8_t *bar0, uint8_t *mem, uint64_t phys, size_t bytes) { + /* Fresh state per controller (the fence may try several, cut 2). */ + for (size_t i = 0; i < sizeof g_k; i++) ((volatile uint8_t *)&g_k)[i] = 0; g_k.base = bar0; g_k.mem = mem; g_k.phys = phys; diff --git a/native/kernel/dev/xhci_acpi_fmt.h b/native/kernel/dev/xhci_acpi_fmt.h new file mode 100644 index 00000000..72c1c3e2 --- /dev/null +++ b/native/kernel/dev/xhci_acpi_fmt.h @@ -0,0 +1,32 @@ +/* xhci_acpi_fmt.h -- console lines of the platform xHCI discovery (ACPI + * DSDT/SSDT + IORT), shared by dev/xhci_fence.c (the kernel) and + * tools/ck_acpi_scan.c (the host tool that runs the same scan over firmware + * tables dumped under Linux and prints the lines the kernel will print). + * NEXT-PHASE-3 cut 2. */ +#ifndef AIENOS_CK_XHCI_ACPI_FMT_H +#define AIENOS_CK_XHCI_ACPI_FMT_H + +/* ACPI ids of a platform xHCI: the DGX Spark controllers (NVDA8000, + * NVDA8001, measured under Ubuntu) and the generic xHCI ids Linux + * xhci_plat_hcd binds (PNP0D10, PNP0D15). */ +#define CK_XHCI_ACPI_IDS { "NVDA8000", "NVDA8001", "PNP0D10", "PNP0D15" } +#define CK_XHCI_ACPI_NIDS 4u +#define CK_XHCI_PLAT_MAX 8u +/* Positive control for the walker: the Arm PL011 UART id. QEMU virt + * describes its UART as ARMH0011 at 0x09000000 (QEMU hw/arm/virt.c memory + * map); the kernel prints the first one found. */ +#define CK_ACPI_CONTROL_ID "ARMH0011" + +#define CK_ACPI_SCAN_FMT "acpi_scan: tables=%u refused=%u first_refusal=%d devices=%u (static scan; _STA not evaluated)\n" +#define CK_ACPI_SCAN_NONE "acpi_scan: no DSDT reachable (FADT missing or table not mapped)\n" +#define CK_ACPI_CONTROL_FMT "acpi_scan: control %s %s mmio=0x%llx+0x%llx\n" +#define CK_ACPI_CONTROL_NONE "acpi_scan: control " CK_ACPI_CONTROL_ID " none\n" +#define CK_XHCI_ACPI_FMT "xhci_acpi: %s hid=%s cid=%s table=%s mmio=0x%llx+0x%llx\n" +#define CK_XHCI_ACPI_COUNT_FMT "xhci_acpi: %d platform controller(s)\n" +/* Platform fence (one line per controller tried, in table order). */ +#define CK_XHCI_PLAT_TRY_FMT "xhci_plat: %s try mmio=0x%llx+0x%llx\n" +#define CK_XHCI_PLAT_SKIP_FMT "xhci_plat: %s skipped (%s)\n" +#define CK_XHCI_PLAT_DENY_FMT "dma_gate: xhci %s denied (%s), controller left halted\n" +#define CK_XHCI_PLAT_OTHER_FMT "dma_gate: xhci %s denied (OtherSmmu smmu=0x%llx stream=0x%x), controller left halted\n" +#define CK_XHCI_PLAT_GRANT_FMT "dma_gate: xhci %s granted (Confined) smmu=0x%llx stream=0x%x\n" +#endif diff --git a/native/kernel/dev/xhci_fence.c b/native/kernel/dev/xhci_fence.c index 9c30e22b..5e4f04c5 100644 --- a/native/kernel/dev/xhci_fence.c +++ b/native/kernel/dev/xhci_fence.c @@ -2,6 +2,7 @@ #include "xhci_fence.h" #include "ck.h" #include "usb_kbd.h" +#include "xhci_acpi_fmt.h" #if defined(CK_TEST_XHCI_MUTATION) && defined(CK_HARDWARE_STAGING) #error "CK_TEST_XHCI_MUTATION (TEST-ONLY xHCI DMA fence mutation) cannot be combined with CK_HARDWARE_STAGING" @@ -25,10 +26,14 @@ #define XHCI_USBCMD 0x00u /* operational */ #define XHCI_USBSTS 0x04u #define USBCMD_RS 0x1u +#define USBCMD_HCRST 0x2u #define USBSTS_HCH 0x1u +#define USBSTS_CNR 0x800u typedef struct { const pci_func *pf; + int plat; /* ACPI platform controller (no PCI function, no bus-master bit) */ + char name[48]; /* its ACPI name (platform only) */ volatile uint8_t *bar0; uint32_t op; /* operational register offset (CAPLENGTH) */ int bm_on, confined, mapped; @@ -43,7 +48,7 @@ static uint64_t g_dma_phys; static uint32_t x_r32(uint32_t off) { return *(volatile uint32_t *)(g_x.bar0 + off); } static void x_w32(uint32_t off, uint32_t v) { *(volatile uint32_t *)(g_x.bar0 + off) = v; } -int ck_xhci_live(void) { return g_x.pf && (g_x.bm_on || g_x.confined); } +int ck_xhci_live(void) { return (g_x.pf || g_x.plat) && (g_x.bm_on || g_x.confined); } const char *ck_xhci_state(void) { return g_x.state ? g_x.state : "absent"; } #if CK_XHCI_MUT @@ -96,8 +101,26 @@ static int halt(const char *when) void ck_xhci_release(void) { ck_xhci *x = &g_x; - if (!x->pf) return; - if (x->bm_on) { + if (!x->pf && !x->plat) return; + if (x->plat && x->bm_on) { + /* No bus-master bit on a platform controller: halt it, reset it (drops + * every ring pointer it holds), then the stream goes back to abort + * below, which is the gate that actually stops its DMA. */ + int h = halt("halt before revoke"); + int reset_ok = 0; + if (h == 0) { + x_w32(x->op + XHCI_USBCMD, USBCMD_HCRST); + uint64_t t0 = ck_time_us(); + while (((x_r32(x->op + XHCI_USBCMD) & USBCMD_HCRST) || (x_r32(x->op + XHCI_USBSTS) & USBSTS_CNR)) && + ck_time_us() - t0 < 100000u) + ck_udelay(20); + reset_ok = !(x_r32(x->op + XHCI_USBCMD) & USBCMD_HCRST); + } + x->bm_on = 0; + ck_printf("dma_gate: xhci %s quiesced (halted=%s reset=%s)\n", x->name, h == 0 ? "yes" : "NO", + reset_ok ? "yes" : "NO"); + } + if (x->pf && x->bm_on) { (void)halt("halt before revoke"); #if CK_XHCI_MUT == CK_XHCI_MUT_NO_REVOKE /* TEST-ONLY mutation: bus mastering is left on. */ @@ -121,6 +144,155 @@ void ck_xhci_release(void) } } + +/* ---- ACPI platform controllers (NEXT-PHASE-3 cut 2) ---- + * Discovery runs once per boot, read-only, before any controller is + * touched; the lines it prints are the ones tools/ck_acpi_scan.c predicts + * from firmware tables. */ +static struct ck_platform_dev g_plat[CK_XHCI_PLAT_MAX]; +static int g_nplat = -2; /* -2 not scanned yet, -1 no DSDT */ + +static void acpi_discover(void) +{ + if (g_nplat != -2) return; + static const char *const ids[] = CK_XHCI_ACPI_IDS; + static const char *const ctl[] = { CK_ACPI_CONTROL_ID }; + struct ck_acpi_scan_info info; + struct ck_platform_dev c; + int nc = ck_acpi_platform_devices(ctl, 1, &c, 1, &info); + if (nc < 0) { + ck_printf(CK_ACPI_SCAN_NONE); + g_nplat = -1; + return; + } + ck_printf(CK_ACPI_SCAN_FMT, info.tables, info.refused, info.first_refusal, info.devices); + if (nc > 0) + ck_printf(CK_ACPI_CONTROL_FMT, c.hid, c.name, (unsigned long long)c.mmio_base, (unsigned long long)c.mmio_len); + else + ck_printf(CK_ACPI_CONTROL_NONE); + int n = ck_acpi_platform_devices(ids, CK_XHCI_ACPI_NIDS, g_plat, CK_XHCI_PLAT_MAX, &info); + if (n > (int)CK_XHCI_PLAT_MAX) n = (int)CK_XHCI_PLAT_MAX; + g_nplat = n < 0 ? -1 : n; + for (int i = 0; i < g_nplat; i++) + ck_printf(CK_XHCI_ACPI_FMT, g_plat[i].name, g_plat[i].hid[0] ? g_plat[i].hid : "-", + g_plat[i].cid[0] ? g_plat[i].cid : "-", g_plat[i].table, (unsigned long long)g_plat[i].mmio_base, + (unsigned long long)g_plat[i].mmio_len); + ck_printf(CK_XHCI_ACPI_COUNT_FMT, g_nplat < 0 ? 0 : g_nplat); +} + +static int same_last_seg(const char *a, const char *b) +{ + const char *la = a, *lb = b; + for (const char *p = a; *p; p++) + if (*p == '.' || *p == '\\' || *p == '^') la = p + 1; + for (const char *p = b; *p; p++) + if (*p == '.' || *p == '\\' || *p == '^') lb = p + 1; + while (*la && *la == *lb) la++, lb++; + return *la == *lb; +} + +/* One platform controller: map, halt whatever the firmware left running, + * SMMU-confined grant by its ACPI name or fail-closed deny, keyboard phase, + * then release (halt, reset, stream back to abort). *kbd_rc gets the phase + * result, or 1 when the phase did not run. */ +static int plat_fence_one(int idx, int *kbd_rc) +{ + const struct ck_platform_dev *p = &g_plat[idx]; + ck_xhci *x = &g_x; + *kbd_rc = 1; + x->pf = 0; + x->plat = 0; + x->bar0 = 0; + x->op = 0; + x->bm_on = x->confined = x->mapped = 0; + x->stream_id = 0; + unsigned k = 0; + for (; k + 1 < sizeof x->name && p->name[k]; k++) x->name[k] = p->name[k]; + x->name[k] = 0; + ck_printf(CK_XHCI_PLAT_TRY_FMT, x->name, (unsigned long long)p->mmio_base, (unsigned long long)p->mmio_len); + for (int j = 0; j < g_nplat; j++) + if (j != idx && same_last_seg(g_plat[j].name, p->name)) { + ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "name not unique in DSDT/SSDT"); + x->state = "ambiguous"; + return CK_XHCI_E_ARG; + } + if (!p->mmio_base || p->mmio_len < 0x1000u || (p->mmio_base & 0xfffu)) { + ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "no usable _CRS memory range"); + x->state = "bar-unusable"; + return CK_XHCI_E_ARG; + } + volatile uint8_t *bar = (volatile uint8_t *)ck_mmio_try_map(p->mmio_base, (size_t)p->mmio_len); + if (!bar) { + ck_printf(CK_XHCI_PLAT_SKIP_FMT, x->name, "memory range not mappable as MMIO"); + x->state = "bar-unusable"; + return CK_XHCI_E_ARG; + } + x->bar0 = bar; + x->mapped = 1; + uint32_t cap = x_r32(XHCI_CAPLENGTH); + x->op = cap & 0xffu; + ck_printf("xhci: %s caplength=0x%02x hciversion=0x%04x (platform)\n", x->name, cap & 0xffu, cap >> 16); + /* Stop any DMA the firmware left running before the SMMU decision; a + * denied controller stays halted. */ + if (halt("halted before gate")) { + ck_printf("keyboard: unavailable (platform xHCI %s would not halt)\n", x->name); + x->state = "halt-failed"; + return CK_XHCI_E_HALT; + } + if (!g_dma_mem) g_dma_mem = ck_dma_alloc(CK_XHCI_DMA_BYTES, 4096, &g_dma_phys); + if (!g_dma_mem) { + ck_printf("keyboard: unavailable (DMA allocation of %u bytes failed)\n", (unsigned)CK_XHCI_DMA_BYTES); + x->state = "dma-alloc-failed"; + return CK_XHCI_E_DMA; + } + struct ck_dma_confinement cf = { 0, 0, 0, 0 }; + int src = ck_dma_confine_named(x->name, g_dma_phys, CK_XHCI_DMA_BYTES, &cf); + if (src) { + if (src == CK_SMMU_OTHER) + ck_printf(CK_XHCI_PLAT_OTHER_FMT, x->name, (unsigned long long)cf.smmu_base, cf.stream_id); + else + ck_printf(CK_XHCI_PLAT_DENY_FMT, x->name, + src == CK_SMMU_ABSENT ? "NoSmmu" + : src == CK_SMMU_NOSTREAM ? "NoStream" + : src == CK_SMMU_EARG ? "Ambiguous" + : "SmmuNotReady"); + x->state = "denied"; + return CK_XHCI_E_DENIED; + } + x->plat = 1; + x->confined = 1; + x->bm_on = 1; /* may DMA from here until the release */ + x->stream_id = cf.stream_id; + ck_printf(CK_XHCI_PLAT_GRANT_FMT, x->name, (unsigned long long)cf.smmu_base, cf.stream_id); + ck_printf("smmu_dma_window: xhci %s only, translation active iova=0x%llx len=0x%llx\n", x->name, + (unsigned long long)cf.iova, (unsigned long long)cf.len); + *kbd_rc = ck_kbd_phase(x->bar0, g_dma_mem, g_dma_phys, CK_XHCI_DMA_BYTES); + x->state = "fenced"; + ck_xhci_release(); + return CK_XHCI_OK; +} + +/* Every platform controller in table order until one carries a keyboard. */ +static int plat_fence_all(void) +{ + if (g_nplat <= 0) { + ck_printf("keyboard: unavailable (no xHCI controller)\n"); + g_x.state = "absent"; + return CK_XHCI_ABSENT; + } + int rc = CK_XHCI_ABSENT, any_fenced = 0; + for (int i = 0; i < g_nplat; i++) { + int krc; + rc = plat_fence_one(i, &krc); + if (rc == CK_XHCI_OK) any_fenced = 1; + if (rc == CK_XHCI_OK && krc == 0) return CK_XHCI_OK; /* keyboard found and its phase ran */ + if (ck_kbd_recovery_requested()) return rc; + } + if (any_fenced) g_x.state = "fenced"; + ck_printf("keyboard: unavailable (no keyboard on any platform xHCI)\n"); + return any_fenced ? CK_XHCI_OK : rc; +} + int ck_xhci_fence(const pci_system *pci) { ck_xhci *x = &g_x; @@ -128,7 +300,10 @@ int ck_xhci_fence(const pci_system *pci) ck_printf("keyboard: unavailable (previous xHCI fence still live; release it first)\n"); return CK_XHCI_E_ARG; } + acpi_discover(); x->pf = 0; + x->plat = 0; + x->name[0] = 0; x->bar0 = 0; x->op = 0; x->bm_on = x->confined = x->mapped = 0; @@ -136,8 +311,10 @@ int ck_xhci_fence(const pci_system *pci) x->state = "absent"; const pci_func *f = pci ? pci_find_class(pci, CK_XHCI_CLASS, 0xffffffu) : 0; if (!f) { - ck_printf("keyboard: unavailable (no xHCI controller)\n"); - return CK_XHCI_ABSENT; + /* No PCI xHCI (the DGX Spark has none): the ACPI platform + * controllers found by acpi_discover, each behind its own fence. */ + ck_printf("keyboard: no PCI xHCI controller; trying %d ACPI platform controller(s)\n", g_nplat < 0 ? 0 : g_nplat); + return plat_fence_all(); } x->pf = f; const pci_bar *b = &f->bar[0]; @@ -205,8 +382,16 @@ int ck_xhci_fence(const pci_system *pci) * boot-keyboard driver runs only here, inside the confined grant, on the * fence's DMA region; the release below halts and revokes whatever it * returns. A controller that would not halt is never driven. */ - if (hrc == 0) (void)ck_kbd_phase(x->bar0, g_dma_mem, g_dma_phys, CK_XHCI_DMA_BYTES); + int krc = hrc == 0 ? ck_kbd_phase(x->bar0, g_dma_mem, g_dma_phys, CK_XHCI_DMA_BYTES) : 1; x->state = hrc ? "halt-failed" : "fenced"; ck_xhci_release(); + /* A PCI controller without a keyboard: try the platform ones next. */ + if (krc == CK_KBD_E_NOKBD && g_nplat > 0 && !ck_kbd_recovery_requested()) { + ck_printf("keyboard: none on the PCI xHCI; trying %d ACPI platform controller(s)\n", g_nplat); + x->pf = 0; + int prc = plat_fence_all(); + if (prc == CK_XHCI_OK) return CK_XHCI_OK; + x->state = "fenced"; + } return hrc ? CK_XHCI_E_HALT : CK_XHCI_OK; } diff --git a/native/kernel/include/.crumb b/native/kernel/include/.crumb index 13692160..0a1bbc6c 100644 --- a/native/kernel/include/.crumb +++ b/native/kernel/include/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T01:02:05Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:67502074fde0b499b68d22d8498c2e410e365f1d9f2dc69b890fac52a56a2d93", + "digest": "sha256:9b69f8190384ecb91f1d09f7109e0d08a84c601bc566e480f5098e6106ec6530", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "20587476bf0d4b7577cc3b62857a9166300681bf", - "source_tree": "sha256:5a733890470cd8be98d366d5ead40c39c9de64daa8a0d0ea3e02f2acda692e84" + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:90d1e9fd53b300b5e3cadd120244acd4b0beedafde669b17cf46e702f8ac1e89" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index 91462a18..3055aa51 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -44,6 +44,10 @@ void *ck_dma_alloc(size_t bytes, size_t align, uint64_t *phys); /* Map [phys, phys+len) as Device-nGnRE (identity) and return the pointer. * Panics if the range overlaps RAM. */ volatile void *ck_mmio_map(uint64_t phys, size_t len); +/* Same mapping, but NULL instead of a panic when the range overlaps RAM or + * cannot be mapped (for addresses read from firmware tables; added for the + * platform xHCI path, NEXT-PHASE-3 cut 2). */ +volatile void *ck_mmio_try_map(uint64_t phys, size_t len); /* Full system barrier (dsb sy). */ void ck_mb(void); @@ -94,6 +98,7 @@ void ck_irq_cpu_enable(int on); #define CK_SMMU_FAILED (-2) #define CK_SMMU_NOSTREAM (-3) #define CK_SMMU_EARG (-4) +#define CK_SMMU_OTHER (-5) /* the device's stream belongs to an SMMU this kernel does not drive */ #define CK_DMA_MAX_STREAMS 8 struct ck_dma_confinement { uint64_t smmu_base; @@ -101,6 +106,39 @@ struct ck_dma_confinement { uint64_t iova, len; }; int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_confinement *out); +/* Same window for an ACPI platform device (no PCI requester id), added for + * the platform xHCI path (NEXT-PHASE-3 cut 2): the stream comes from the + * IORT named component whose object name has the same final segment as + * acpi_name ("USB0" for "\\_SB_.USB0"), through its single mapping. Returns + * as ck_dma_confine, plus CK_SMMU_NOSTREAM when no named component or no + * stream mapping exists, CK_SMMU_EARG when two named components carry that + * name, and CK_SMMU_OTHER when the stream belongs to an SMMUv3 other than + * the one this kernel brings up (the IORT's first; *out then names that + * SMMU and the stream, with len 0, for the report). Never grants unconfined. */ +int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out); + +/* ACPI platform devices (core/acpi_platform.c, NEXT-PHASE-3 cut 2): a + * static scan of the DSDT (from the FADT) and every SSDT the XSDT lists for + * Device objects whose _HID or _CID is one of ids[0..nids). Nothing is + * executed: _STA is not evaluated, _CRS is read only when it is a constant + * resource template (see core/acpi_dev.h). Each table must pass signature, + * length and checksum checks; a refused table is counted and skipped. + * Returns the number of matches (only max are stored), or -1 when no DSDT is + * reachable. info (may be NULL) gets the scan counters. */ +struct ck_platform_dev { + char name[48]; /* name at the DeviceOp, usually relative ("USB0") */ + char hid[16], cid[16]; + uint64_t mmio_base, mmio_len; /* first memory range of _CRS; 0 if none */ + char table[5]; /* "DSDT" or "SSDT" */ +}; +struct ck_acpi_scan_info { + unsigned tables; /* definition blocks scanned (DSDT + SSDTs) */ + unsigned refused; /* blocks refused: signature, length or checksum */ + unsigned devices; /* Device objects parsed in the accepted blocks */ + int first_refusal; /* CK_AML_E_* of the first refused block, 0 if none */ +}; +int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max, + struct ck_acpi_scan_info *info); /* Return a confined stream to abort (after the device's bus mastering is * off). 0 ok, CK_SMMU_EARG if not confined, CK_SMMU_FAILED on a command * timeout. */ diff --git a/native/kernel/mm/.crumb b/native/kernel/mm/.crumb index 52d19725..a52f4bcf 100644 --- a/native/kernel/mm/.crumb +++ b/native/kernel/mm/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-04T16:43:31Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:272bc1dd7957e2024660c4f2850462671069bec77f4eee8d2c71b5b8788ab97e", + "digest": "sha256:05fee5afbbfcbca9eab294bfc3778bc380b2eaa1c95a595b6946971a180f0665", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 7, - "generated_at_commit": "a93e27b8a26d45330e58e1b8df1b35faf7146886", - "source_tree": "sha256:5b0b5bcd5a7997bd091f9c7e9683022723ea3fd5113d77ec265b36405612f3c3" + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:e02f28ddffec29c55a3c740c286b81751c77d73398db03c14077aaecd4c276d5" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/mm/mmu.c b/native/kernel/mm/mmu.c index 9026c817..2388c6d4 100644 --- a/native/kernel/mm/mmu.c +++ b/native/kernel/mm/mmu.c @@ -415,3 +415,8 @@ volatile void *ck_mm_mmio_try_map(uint64_t phys, size_t len) tlb_sync(); return (volatile void *)(uintptr_t)phys; } + +volatile void *ck_mmio_try_map(uint64_t phys, size_t len) +{ + return len ? ck_mm_mmio_try_map(phys, len) : 0; +} diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index 7d6c7460..b18fd7c2 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -62,6 +62,7 @@ STAGE_FREE_FLAGS := -std=gnu11 -O2 -Wall -Wextra -Werror -ffreestanding -nostdli STAGE_ALLOWED_U := ck_puts ck_printf ck_vprintf ck_panic ck_alloc ck_free ck_dma_alloc ck_mmio_map \ ck_dma_confine ck_dma_unconfine ck_dma_faults \ ck_mb ck_acpi_find ck_time_us ck_udelay ck_commit ck_exception_level ck_conventional_memory_kb \ + ck_acpi_platform_devices ck_dma_confine_named ck_mmio_try_map \ ck_entropy_fill ck_entropy_status ck_entropy_reason STAGE_FREE_OBJS := $(foreach s,$(STAGE_SRCS),$(STAGE_OUT)/free/$(subst /,_,$(subst $(STAGE_NATIVE)/,,$(s:.c=.o)))) diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index a81ece95..6b9925fd 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -23,21 +23,21 @@ "purpose": "Directory native/kernel/svc/compat. Purpose not yet described by a human." }, { - "digest": "c64e9742262522ca9b9a0b76ec28beb02a1ab7a3b9284bd85f5710391d143dcf", - "last_commit": "2f393a4", + "digest": "c0bfdf17e8c27bac0c6f65d02e19498fef4a1140fbdd70546d08e691eed56424", + "last_commit": "f921666", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], - "children_root": "sha256:556579a3c48b50e544dc026c1a4300a68b8fc92f4b7b4ebad24a74341178f62f", - "compiled_at": "2026-10-06T01:02:05Z", + "children_root": "sha256:fa0e2a0cc0c70b3e86f73dfb158e4531edcf82370ba5e1c582c36bdf8536f3d6", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:2c634b363be2e02a04c0fdb3cf56c88bebf0a86ee172f3416989df3764ea7a0d", + "digest": "sha256:ffa91a4e0808986fd633979835ad7cb183995cf87939f16f55da4e58f9e24254", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 35, - "generated_at_commit": "20587476bf0d4b7577cc3b62857a9166300681bf", - "source_tree": "sha256:a49f575fb80438c30583914ccbe3149a5fb9c12e625392ffcf76fe42844723fb" + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:6b336f28ca71ecd41b770e83dc71bd88c86c87d03b3d9ee1d7c8743e48de0baf" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/.crumb b/native/kernel/svc/tests/.crumb index 7d50576c..c99db880 100644 --- a/native/kernel/svc/tests/.crumb +++ b/native/kernel/svc/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T01:02:05Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:c64e9742262522ca9b9a0b76ec28beb02a1ab7a3b9284bd85f5710391d143dcf", + "digest": "sha256:c0bfdf17e8c27bac0c6f65d02e19498fef4a1140fbdd70546d08e691eed56424", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 3, - "generated_at_commit": "20587476bf0d4b7577cc3b62857a9166300681bf", - "source_tree": "sha256:e0eb26d5ea897406da2b1fd37c676ec2b2a206468d14c70317073c00e1cbd716" + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:694ade7ff54895da71314a11d5452d7f74a3820d769842de91a6abcca21ee4ac" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/tests/ck_host.c b/native/kernel/svc/tests/ck_host.c index d31aea9d..6b707182 100644 --- a/native/kernel/svc/tests/ck_host.c +++ b/native/kernel/svc/tests/ck_host.c @@ -57,6 +57,20 @@ volatile void *ck_mmio_map(uint64_t phys, size_t len) (void)len; return (volatile void *)(uintptr_t)phys; } +/* Host: no platform MMIO (the ACPI platform xHCI list is always empty). */ +volatile void *ck_mmio_try_map(uint64_t phys, size_t len) +{ + (void)phys; (void)len; + return NULL; +} +/* Host: no DSDT reachable (no FADT in ck_acpi_find). */ +int ck_acpi_platform_devices(const char *const *ids, unsigned nids, struct ck_platform_dev *out, unsigned max, + struct ck_acpi_scan_info *info) +{ + (void)ids; (void)nids; (void)out; (void)max; + if (info) info->tables = info->refused = info->devices = 0, info->first_refusal = 0; + return -1; +} void ck_mb(void) { __sync_synchronize(); } const void *ck_acpi_find(const char sig[4]) { @@ -87,6 +101,11 @@ int ck_dma_confine(uint32_t rid, uint64_t phys, uint64_t len, struct ck_dma_conf (void)rid; (void)phys; (void)len; (void)out; return CK_SMMU_ABSENT; } +int ck_dma_confine_named(const char *acpi_name, uint64_t phys, uint64_t len, struct ck_dma_confinement *out) +{ + (void)acpi_name; (void)phys; (void)len; (void)out; + return CK_SMMU_ABSENT; +} int ck_dma_unconfine(uint32_t stream_id) { (void)stream_id; return CK_SMMU_EARG; } int ck_dma_faults(uint32_t stream_id, struct ck_dma_fault *first) { diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 3ea618c5..6723e5f2 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T02:01:59Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:20157ea957a698743f9078faf6c765b507733f9a9e67877ea5602c7f461b7a7b", + "digest": "sha256:182f6664539cfc8d8731eb824c5df6ce700059e666dac474500929fce0b99c0d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 24, - "generated_at_commit": "71d5b326eea6a330f3cc1c4377fc4bc2d134d5aa", - "source_tree": "sha256:9f9b47091ea12cf2b6e0e95fe6e0e9ea655a0203c432d1a5a720c07179035fb0" + "files": 26, + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:c4027e326b679843fda1701bd290f41b39156d49db542133a65e4971ce027a58" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt b/native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt new file mode 100644 index 00000000..4b4465d3 --- /dev/null +++ b/native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt @@ -0,0 +1,64 @@ +# DGX Spark (Machine 1): console lines the C kernel should print for the +# platform xHCI discovery and fence (NEXT-PHASE-3 cut 2). Hardware NOT_RUN. +# Produced by: make -C native/kernel acpi-scan, then +# ck_acpi_scan --iort IORT DSDT SSDT1 SSDT2 SSDT3 SSDT4 SSDT5 +# over the firmware tables read on this Spark under Ubuntu on 2026-10-05 +# (sudo cat /sys/firmware/acpi/tables/, read only). Table sha256: +# DSDT 2871cbcb8992f7bd3915d6f19e7cfab577e068dcad58b6f00317a9f2d4e1b28e (70143 bytes) +# SSDT1 85f6ba0352ed5b63ae8a17239462c032717a2ffe53488df38c6618fab3b95ffe (6664 bytes) +# SSDT2 4de6724fcfb6e1ca9c9aeae4102a9ac10794c9dc6f1a3637ccf04aea9aa36a15 (773 bytes) +# SSDT3 50016e1cff2b1ca6fc107c42ad3c30afcdd6ecacf6b73f738ede186e38e2a4bd (1939 bytes) +# SSDT4 3f5cab8ad107d011648e60708d93dde0655556fd5bb300599c365704d01f869d (369 bytes) +# SSDT5 ed01843deef37e26beff21f7c149c317d3cf891028cadd779875d313b04d8b57 (901 bytes) +# IORT 5e0096a2be83ce2025c5c6395455c6814c16e01ff433791e8057c245f4939cb1 (3904 bytes) +# Plain lines: printed byte for byte. "? " lines: the kernel prints a line +# there whose values come from the hardware. "# " lines: tool notes. +# Assumes no PCI xHCI (measured) and no keyboard found on an earlier +# controller. SSDT order = sysfs numbering (XSDT order INFERRED). +# +acpi_scan: tables=6 refused=0 first_refusal=0 devices=119 (static scan; _STA not evaluated) +acpi_scan: control ARMH0011 none +xhci_acpi: USB0 hid=NVDA8000 cid=PNP0D15 table=DSDT mmio=0x1db60000+0x7800 +xhci_acpi: USB1 hid=NVDA8000 cid=PNP0D15 table=DSDT mmio=0x1db90000+0x7800 +xhci_acpi: USB2 hid=NVDA8000 cid=PNP0D15 table=DSDT mmio=0x1dde0000+0x7800 +xhci_acpi: USB3 hid=NVDA8000 cid=PNP0D15 table=DSDT mmio=0x1de10000+0x7800 +xhci_acpi: USB4 hid=NVDA8001 cid=PNP0D15 table=DSDT mmio=0x1d860000+0x7800 +xhci_acpi: USB5 hid=NVDA8000 cid=PNP0D15 table=DSDT mmio=0x1d870000+0x7800 +xhci_acpi: 6 platform controller(s) +# iort: length=3904 nodes=68 +# smmu: ck_iort_parse=-1 (malformed) +# smmu: 15 PCI root-complex mapping(s) to the first SMMUv3 (limit CK_IORT_MAX_MAPS=8), highest stream id 0xfffff (linear table holds 0x0-0xfff) +keyboard: no PCI xHCI controller; trying 6 ACPI platform controller(s) +xhci_plat: USB0 try mmio=0x1db60000+0x7800 +? xhci: USB0 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +? smmu: IORT malformed, SMMU unusable (fail closed) [once per boot, at the first DMA grant request] +dma_gate: xhci USB0 denied (SmmuNotReady), controller left halted +# USB0: IORT named component \_SB_.USB0 stream 0x0 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +xhci_plat: USB1 try mmio=0x1db90000+0x7800 +? xhci: USB1 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +dma_gate: xhci USB1 denied (SmmuNotReady), controller left halted +# USB1: IORT named component \_SB_.USB1 stream 0x1 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +xhci_plat: USB2 try mmio=0x1dde0000+0x7800 +? xhci: USB2 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +dma_gate: xhci USB2 denied (SmmuNotReady), controller left halted +# USB2: IORT named component \_SB_.USB2 stream 0x2 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +xhci_plat: USB3 try mmio=0x1de10000+0x7800 +? xhci: USB3 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +dma_gate: xhci USB3 denied (SmmuNotReady), controller left halted +# USB3: IORT named component \_SB_.USB3 stream 0x3 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +xhci_plat: USB4 try mmio=0x1d860000+0x7800 +? xhci: USB4 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +dma_gate: xhci USB4 denied (SmmuNotReady), controller left halted +# USB4: IORT named component \_SB_.USB4 stream 0x4 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +xhci_plat: USB5 try mmio=0x1d870000+0x7800 +? xhci: USB5 caplength= hciversion= (platform) +? xhci: halted before gate usbcmd= usbsts= halted= +dma_gate: xhci USB5 denied (SmmuNotReady), controller left halted +# USB5: IORT named component \_SB_.USB5 stream 0x5 on the SMMUv3 at 0x13800000 (the first SMMUv3 node; granted only once the kernel can bring it up) +keyboard: unavailable (no keyboard on any platform xHCI) +# 0 controller(s) would reach the keyboard phase diff --git a/native/kernel/tests/test_acpi_dev.c b/native/kernel/tests/test_acpi_dev.c new file mode 100644 index 00000000..eea0f5fb --- /dev/null +++ b/native/kernel/tests/test_acpi_dev.c @@ -0,0 +1,399 @@ +/* test_acpi_dev.c -- core/acpi_dev.c (static DSDT/SSDT device scan) and the + * IORT named-component lookup in core/acpi.c, on synthetic tables built + * here in the encodings the real firmware uses: + * - DGX Spark form (measured from the Spark DSDT under Ubuntu, 2026-10-05): + * Scope(\_SB) { Device(USB0) { Name(_HID, "NVDA8000") Name(_CID, + * "PNP0D15") ... Method(_CRS) { Name(RBUF, Buffer() {Memory32Fixed ...}) + * Return(RBUF) } } } + * - QEMU virt form: Device(COM0) { Name(_HID, "ARMH0011") Name(_CRS, + * ResourceTemplate() { Memory32Fixed(..., 0x09000000, 0x1000) ... }) } + * - EisaId _HID/_CID, QWord memory, nested devices. + * Negative: bad signature, bad checksum, truncated table, short header, + * device package past the end, descriptor past the buffer, bad names. The + * Makefile target acpi-dev-mutant builds this test against code mutants + * (signature / checksum / length check removed) and requires it to FAIL. */ +#include +#include + +#include "acpi.h" +#include "acpi_dev.h" +#include "ck_test.h" + +static uint8_t buf[8192]; +static uint32_t at; + +static void b1(uint8_t v) { buf[at++] = v; } +static void b4(uint32_t v) { for (int i = 0; i < 4; i++) b1((uint8_t)(v >> (8 * i))); } +static void b8(uint64_t v) { for (int i = 0; i < 8; i++) b1((uint8_t)(v >> (8 * i))); } +static void bs(const char *s) { while (*s) b1((uint8_t)*s++); } + +/* Package length covering `content` bytes after the encoding. Writes the + * encoding at buf[pos] (space reserved by pk_open). */ +static uint32_t pk_open(void) { uint32_t p = at; at += 2; return p; } /* always 2-byte form */ +static void pk_close(uint32_t p) +{ + uint32_t total = at - p; /* includes the 2 encoding bytes */ + buf[p] = (uint8_t)(0x40u | (total & 0x0fu)); + buf[p + 1] = (uint8_t)(total >> 4); +} + +static uint32_t hdr(const char *sig) +{ + uint32_t start = at; + bs(sig); + b4(0); /* length, fixed by finish */ + b1(2); b1(0); bs("AIENOS"); bs("CKTEST01"); b4(1); bs("CKT "); b4(1); + return start; +} +static uint32_t finish(uint32_t start) +{ + uint32_t len = at - start; + buf[start + 4] = (uint8_t)len; + buf[start + 5] = (uint8_t)(len >> 8); + buf[start + 6] = (uint8_t)(len >> 16); + buf[start + 7] = (uint8_t)(len >> 24); + buf[start + 9] = 0; + uint8_t s = 0; + for (uint32_t i = 0; i < len; i++) s = (uint8_t)(s + buf[start + i]); + buf[start + 9] = (uint8_t)(0u - s); + return len; +} + +static void name_str(const char *seg, const char *val) { b1(0x08); bs(seg); b1(0x0d); bs(val); b1(0); } +static void name_dword(const char *seg, uint32_t v) { b1(0x08); bs(seg); b1(0x0c); b4(v); } +static void mem32fixed(uint32_t base, uint32_t len) { b1(0x86); b1(9); b1(0); b1(1); b4(base); b4(len); } +static void irq(uint32_t intid) { b1(0x89); b1(6); b1(0); b1(0x01); b1(1); b4(intid); } +static void end_tag(void) { b1(0x79); b1(0); } + +/* Buffer(size) { bytes } where the bytes are emitted by fn. */ +static void buffer_of(void (*fn)(void)) +{ + b1(0x11); + uint32_t p = pk_open(); + b1(0x0b); /* WordPrefix size, fixed below */ + uint32_t szpos = at; + at += 2; + uint32_t d0 = at; + fn(); + uint32_t n = at - d0; + buf[szpos] = (uint8_t)n; + buf[szpos + 1] = (uint8_t)(n >> 8); + pk_close(p); +} + +static void spark_rbuf(void) +{ + mem32fixed(0x1db60000u, 0x7800u); + mem32fixed(0x1db68000u, 0x100u); + mem32fixed(0x1db70000u, 0x4000u); + irq(0xaf); + end_tag(); +} +static void qemu_uart(void) +{ + mem32fixed(0x09000000u, 0x1000u); + irq(0x21); + end_tag(); +} +static void qword_mem(void) +{ + b1(0x8a); b1(43); b1(0); + b1(0); b1(0x0c); b1(0x01); /* memory, flags */ + b8(0); b8(0x4000000000ull); b8(0x400000ffffull); b8(0); b8(0x10000); + end_tag(); +} +static void desc_past_end(void) +{ + b1(0x86); b1(40); b1(0); b1(1); b4(0x12340000u); /* claims 40 bytes, has 5 */ +} + +/* Device(NAME) { fn() } */ +static void device(const char *seg, void (*fn)(void)) +{ + b1(0x5b); b1(0x82); + uint32_t p = pk_open(); + bs(seg); + fn(); + pk_close(p); +} + +static void usb0_body(void) +{ + name_str("_HID", "NVDA8000"); + name_str("_CID", "PNP0D15"); + b1(0x08); bs("_UID"); b1(0x00); + /* Method(_STA) { If (UP0E == 0) { Return (0) } Return (0x0F) } (shape only) */ + b1(0x14); { uint32_t p = pk_open(); bs("_STA"); b1(0x00); b1(0xa4); b1(0x0a); b1(0x0f); pk_close(p); } + /* Method(_CRS, 0, Serialized) { Name(RBUF, Buffer(){...}) Return(RBUF) } */ + b1(0x14); + uint32_t p = pk_open(); + bs("_CRS"); + b1(0x08); + b1(0x08); bs("RBUF"); + buffer_of(spark_rbuf); + b1(0xa4); bs("RBUF"); + pk_close(p); +} +static void usb4_body(void) +{ + name_str("_HID", "NVDA8001"); + name_str("_CID", "PNP0D15"); + b1(0x14); + uint32_t p = pk_open(); + bs("_CRS"); + b1(0x08); + b1(0x08); bs("RBUF"); + buffer_of(spark_rbuf); + b1(0xa4); bs("RBUF"); + pk_close(p); +} +static void com0_body(void) +{ + name_str("_HID", "ARMH0011"); + b1(0x08); bs("_UID"); b1(0x00); + b1(0x08); bs("_CRS"); + buffer_of(qemu_uart); +} +static void xhc_eisa_body(void) +{ + name_dword("_HID", 0x100dd041u); /* EisaId("PNP0D10") */ + b1(0x08); bs("_CRS"); + buffer_of(qword_mem); +} +static void pci0_body(void) +{ + name_dword("_HID", 0x080ad041u); /* EisaId("PNP0A08") */ + name_dword("_CID", 0x030ad041u); /* EisaId("PNP0A03") */ +} +static void child_body(void) { name_str("_HID", "NVDA8001"); } +static void parent_body(void) +{ + device("CHL0", child_body); + name_str("_HID", "PNP0C02"); /* after the nested device: not read for PAR0 */ +} +static void bad_crs_body(void) +{ + name_str("_HID", "PNP0D15"); + b1(0x08); bs("_CRS"); + buffer_of(desc_past_end); +} +static void sb_scope(void) +{ + device("USB0", usb0_body); + device("USB4", usb4_body); + device("COM0", com0_body); + device("XHC1", xhc_eisa_body); + device("PCI0", pci0_body); + device("PAR0", parent_body); + device("BADC", bad_crs_body); +} + +static uint32_t build_dsdt(const char *sig) +{ + at = 0; + uint32_t s = hdr(sig); + b1(0x10); /* Scope(\_SB_) */ + uint32_t p = pk_open(); + b1(0x5c); bs("_SB_"); + sb_scope(); + pk_close(p); + return finish(s); +} + +static const struct ck_acpi_device *by_name(const struct ck_acpi_device *d, int n, const char *name) +{ + for (int i = 0; i < n; i++) + if (!strcmp(d[i].path, name)) return &d[i]; + return 0; +} + +static void test_scan(void) +{ + uint32_t len = build_dsdt("DSDT"); + CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK); + struct ck_acpi_device d[16]; + unsigned seen = 0; + int n = ck_aml_scan(buf, len, 0, 0, d, 16, &seen); + CHECK(n == 8 && seen == 8); /* USB0 USB4 COM0 XHC1 PCI0 PAR0 CHL0 BADC */ + + const struct ck_acpi_device *u = by_name(d, n, "USB0"); + CHECK(u && !strcmp(u->hid, "NVDA8000") && !strcmp(u->cid, "PNP0D15")); + CHECK(u && u->mmio_base == 0x1db60000u && u->mmio_len == 0x7800u); /* first range only */ + const struct ck_acpi_device *c = by_name(d, n, "COM0"); + CHECK(c && !strcmp(c->hid, "ARMH0011") && c->cid[0] == 0 && c->mmio_base == 0x09000000u && c->mmio_len == 0x1000u); + const struct ck_acpi_device *x = by_name(d, n, "XHC1"); + CHECK(x && !strcmp(x->hid, "PNP0D10") && x->mmio_base == 0x4000000000ull && x->mmio_len == 0x10000u); + const struct ck_acpi_device *p = by_name(d, n, "PCI0"); + CHECK(p && !strcmp(p->hid, "PNP0A08") && !strcmp(p->cid, "PNP0A03") && p->mmio_len == 0); + const struct ck_acpi_device *par = by_name(d, n, "PAR0"); + const struct ck_acpi_device *chl = by_name(d, n, "CHL0"); + CHECK(par && par->hid[0] == 0); /* the child's _HID is not the parent's */ + CHECK(chl && !strcmp(chl->hid, "NVDA8001")); + const struct ck_acpi_device *bc = by_name(d, n, "BADC"); + CHECK(bc && bc->mmio_base == 0 && bc->mmio_len == 0); /* descriptor past its buffer: no range */ + + /* The xHCI id filter (the fence's list). */ + static const char *const ids[] = { "NVDA8000", "NVDA8001", "PNP0D10", "PNP0D15" }; + n = ck_aml_scan(buf, len, ids, 4, d, 16, &seen); + CHECK(n == 5); /* USB0 USB4 XHC1 CHL0 BADC */ + CHECK(by_name(d, n, "USB0") && by_name(d, n, "USB4") && by_name(d, n, "XHC1") && by_name(d, n, "CHL0") && + by_name(d, n, "BADC") && !by_name(d, n, "COM0")); + /* max smaller than the matches: count returned, only max stored. */ + memset(d, 0x5a, sizeof d); + CHECK(ck_aml_scan(buf, len, ids, 4, d, 2, 0) == 5); + CHECK(d[2].path[0] == 0x5a); + static const char *const ctl[] = { "ARMH0011" }; + n = ck_aml_scan(buf, len, ctl, 1, d, 16, 0); + CHECK(n == 1 && !strcmp(d[0].path, "COM0")); + static const char *const none[] = { "QEMU0002" }; + CHECK(ck_aml_scan(buf, len, none, 1, d, 16, 0) == 0); + + /* SSDT is accepted too. */ + len = build_dsdt("SSDT"); + CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK && ck_aml_scan(buf, len, ids, 4, d, 16, 0) == 5); +} + +static void test_refusals(void) +{ + struct ck_acpi_device d[16]; + unsigned seen = 7; + /* bad signature */ + uint32_t len = build_dsdt("XSDT"); + CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_SIG); + CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, &seen) == CK_AML_E_SIG && seen == 0); + /* bad checksum (one byte flipped after the checksum was fixed) */ + len = build_dsdt("DSDT"); + buf[len - 1] ^= 0x01; + CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_SUM); + CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, 0) == CK_AML_E_SUM); + /* truncated: the header claims more bytes than are available */ + len = build_dsdt("DSDT"); + CHECK(ck_aml_table_ok(buf, len - 1) == CK_AML_E_LEN); + CHECK(ck_aml_scan(buf, len - 40, 0, 0, d, 16, 0) == CK_AML_E_LEN); + CHECK(ck_aml_table_ok(buf, 35) == CK_AML_E_LEN); + /* header length below 36 */ + len = build_dsdt("DSDT"); + buf[4] = 20, buf[5] = buf[6] = buf[7] = 0; + CHECK(ck_aml_table_ok(buf, len) == CK_AML_E_LEN); + CHECK(ck_aml_table_ok(0, 100) == CK_AML_E_LEN); + + /* A device whose package runs past the table end is skipped, not followed. */ + at = 0; + uint32_t s = hdr("SSDT"); + device("GOOD", child_body); + b1(0x5b); b1(0x82); b1(0x4f); b1(0x40); bs("LONG"); /* 2-byte PkgLength 0x40f, far past the end */ + name_str("_HID", "NVDA8000"); + len = finish(s); + CHECK(ck_aml_table_ok(buf, len) == CK_AML_OK); + int n = ck_aml_scan(buf, len, 0, 0, d, 16, &seen); + CHECK(n == 1 && seen == 1 && !strcmp(d[0].path, "GOOD")); + /* A bad name segment (lower case) is not a device. */ + at = 0; + s = hdr("SSDT"); + b1(0x5b); b1(0x82); b1(0x0d); bs("usb0"); name_str("_HID", "NVDA"); + len = finish(s); + CHECK(ck_aml_scan(buf, len, 0, 0, d, 16, &seen) == 0 && seen == 0); +} + +static void test_helpers(void) +{ + char id[8]; + ck_aml_eisaid(0x150dd041u, id); + CHECK(!strcmp(id, "PNP0D15")); + ck_aml_eisaid(0x080ad041u, id); + CHECK(!strcmp(id, "PNP0A08")); + CHECK(ck_aml_path_eq("\\_SB.USB0", "_SB_.USB0")); + CHECK(ck_aml_path_eq("\\_SB_.USB0", "\\_SB_.USB0")); + CHECK(!ck_aml_path_eq("\\_SB_.USB0", "\\_SB_.USB1")); + CHECK(!ck_aml_path_eq("\\_SB_.USB0", "\\_SB_")); + CHECK(!ck_aml_path_eq("\\_SB_.USB00", "\\_SB_.USB0")); + CHECK(ck_aml_last_seg_eq("USB0", "\\_SB_.USB0")); + CHECK(ck_aml_last_seg_eq("\\_SB_.PCI0.XHC", "XHC_")); + CHECK(!ck_aml_last_seg_eq("USB0", "\\_SB_.USB1")); + CHECK(!ck_aml_last_seg_eq("", "USB0")); +} + +/* ---- IORT named components ---- */ +static uint32_t iort_smmu(uint64_t base) +{ + uint32_t off = at; + b1(4); b1(68); b1(0); b1(4); b4(0); b4(0); b4(0); /* type, len, rev, id, no mappings */ + b8(base); + while (at < off + 68) b1(0); + return off; +} +static void iort_named(const char *name, uint32_t sid, uint32_t target, int single, int with_map) +{ + uint32_t off = at; + uint32_t nlen = 29 + 32; /* fixed fields + name area */ + uint32_t total = nlen + (with_map ? 20u : 0u); + b1(1); b1((uint8_t)total); b1((uint8_t)(total >> 8)); b1(4); b4(0); + b4(with_map ? 1u : 0u); b4(with_map ? nlen : 0u); + b4(0); b8(0x0000000000000001ull); b1(48); + uint32_t nm = at; + bs(name); + while (at < nm + 32) b1(0); + if (with_map) { + b4(0); b4(0); b4(sid); b4(target); b4(single ? 1u : 0u); + } + (void)off; +} +static uint32_t build_iort(int dup) +{ + at = 0; + uint32_t s = at; + bs("IORT"); b4(0); b1(3); b1(0); bs("AIENOS"); bs("CKTEST01"); b4(1); bs("CKT "); b4(1); + uint32_t count_pos = at; + b4(0); b4(48); b4(0); + uint32_t a = iort_smmu(0x13800000ull); /* the first SMMUv3: the one the kernel drives */ + uint32_t b = iort_smmu(0x14900000ull); + iort_named("\\_SB_.USB0", 0x30, a, 1, 1); + iort_named("\\_SB_.USB1", 0x31, b, 1, 1); + iort_named("\\_SB_.USB2", 0, 0, 0, 0); + iort_named("\\_SB_.USB3", 0x33, a, 0, 1); /* not flagged single, but one id (count 0) */ + uint32_t nodes = 6; + if (dup) { + iort_named("\\_SB_.PCI1.USB0", 0x40, a, 1, 1); + nodes++; + } + buf[count_pos] = (uint8_t)nodes; + return finish(s); +} + +static void test_iort_named(void) +{ + build_iort(0); + struct ck_iort_named r; + struct ck_iort_smmu first; + CHECK(ck_iort_parse(buf, &first) == 1 && first.base == 0x13800000ull); + CHECK(ck_iort_named(buf, "USB0", &r) == 1); + CHECK(r.stream_id == 0x30 && r.target_off == first.node_off && r.target_base == 0x13800000ull); + CHECK(!strcmp(r.name, "\\_SB_.USB0")); + CHECK(ck_iort_named(buf, "\\_SB.USB1", &r) == 1); + CHECK(r.stream_id == 0x31 && r.target_off != first.node_off && r.target_base == 0x14900000ull); + CHECK(ck_iort_named(buf, "USB2", &r) == 1 && r.target_off == 0); /* no mapping: no stream */ + CHECK(ck_iort_named(buf, "USB3", &r) == 1 && r.stream_id == 0x33 && r.target_off == first.node_off); + CHECK(ck_iort_named(buf, "USB9", &r) == 0); + CHECK(ck_iort_named(buf, "TOOLONG5", &r) == -1); + build_iort(1); + CHECK(ck_iort_named(buf, "USB0", &r) == -2); /* two named components end in USB0 */ + /* malformed: a mapping that references outside the table */ + uint32_t len = build_iort(0); + for (uint32_t i = 48; i + 20 <= len; i++) /* find USB0's mapping (sid 0x30) */ + if (buf[i] == 0x30 && buf[i + 1] == 0 && buf[i + 4] != 0 && buf[i - 4] == 0) { + buf[i + 4] = 0xff, buf[i + 5] = 0xff, buf[i + 6] = 0, buf[i + 7] = 0; + break; + } + CHECK(ck_iort_named(buf, "USB0", &r) == -1); + memcpy(buf, "XXXX", 4); + CHECK(ck_iort_named(buf, "USB0", &r) == -1); +} + +int main(void) +{ + test_scan(); + test_refusals(); + test_helpers(); + test_iort_named(); + return ck_t_verdict("test_acpi_dev"); +} diff --git a/native/kernel/tools/.crumb b/native/kernel/tools/.crumb index 19b356f2..24515961 100644 --- a/native/kernel/tools/.crumb +++ b/native/kernel/tools/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-05T02:01:59Z", + "compiled_at": "2026-10-06T02:21:10Z", "compiler": "crumb-compile/1", - "digest": "sha256:5e6a44b4b936178bd58d2840921cc5d478ed9126ffea1aa3c36d628a6598549f", + "digest": "sha256:342735b38f7498b022914ea412e4b8e798a916786b31ad02516ef7d9e518acba", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 10, - "generated_at_commit": "71d5b326eea6a330f3cc1c4377fc4bc2d134d5aa", - "source_tree": "sha256:c41d2afc039bf7d3064effb8d26770dad71d9432e6a06b3e8cb28c7eaea5f334" + "files": 11, + "generated_at_commit": "4bbc7e66f4e28b91c523acf8f4b296dfe2141e71", + "source_tree": "sha256:aa1ee438169ab780d8defb5862895c5229695217cd120db604c96aca2a5ecb04" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tools/ck_acpi_scan.c b/native/kernel/tools/ck_acpi_scan.c new file mode 100644 index 00000000..f7c0efe2 --- /dev/null +++ b/native/kernel/tools/ck_acpi_scan.c @@ -0,0 +1,303 @@ +/* ck_acpi_scan.c -- host tool (never part of an image): runs the kernel's + * platform xHCI discovery over firmware tables dumped under Linux and + * prints the console lines the kernel will print on that machine. + * + * ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...] + * + * Tables are given in the order the kernel visits them: the DSDT named by + * the FADT first, then every SSDT in XSDT order. The scan is the kernel's + * own code (core/acpi_dev.c, core/acpi.c) and the lines come from the same + * format strings (dev/xhci_acpi_fmt.h). + * + * Output, one line each: + * plain lines the kernel prints them byte for byte (they depend only on + * the tables); + * "? " lines the kernel prints a line here whose values come from the + * hardware (controller registers, the SMMU, DMA addresses); + * shown with the part that is not known from tables; + * "# " lines tool notes: IORT nodes and stream mappings, assumptions. + * The per-controller lines assume no PCI xHCI (measured on the DGX Spark: + * no PCI class 0x0c03 device) and that every controller is tried, i.e. no + * keyboard is found on an earlier one (the kernel stops at the first + * controller whose keyboard phase succeeds). Exit 0, or 2 on a usage or + * read error. */ +#include +#include +#include +#include + +#include "acpi.h" +#include "acpi_dev.h" +#include "xhci_acpi_fmt.h" + +#define FILE_MAX (4u << 20) +#define TABLES_MAX 32 +#define STE_N 4096u /* core/smmu_svc.c linear stream table */ + +struct table { + const char *path; + uint8_t *data; + size_t len; +}; + +/* Same fields as struct ck_acpi_scan_info in include/ck.h (kernel only). */ +struct ck_acpi_scan_info { + unsigned tables, refused, devices; + int first_refusal; +}; + +struct plat { + struct ck_acpi_device d; + char table[5]; +}; + +static uint8_t *read_file(const char *path, size_t *len) +{ + FILE *f = fopen(path, "rb"); + if (!f) { + fprintf(stderr, "ck_acpi_scan: cannot open %s\n", path); + return 0; + } + uint8_t *b = malloc(FILE_MAX); + size_t n = b ? fread(b, 1, FILE_MAX, f) : 0; + int more = b && fgetc(f) != EOF; + fclose(f); + if (!b || more) { + fprintf(stderr, "ck_acpi_scan: %s missing or larger than %u bytes\n", path, FILE_MAX); + free(b); + return 0; + } + *len = n; + return b; +} + +static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; } +static uint64_t rd64(const uint8_t *p) { return (uint64_t)rd32(p) | (uint64_t)rd32(p + 4) << 32; } + +/* Mirrors core/acpi_platform.c scan_block over every table. */ +static int scan_all(struct table *t, int nt, const char *const *ids, unsigned nids, struct plat *out, unsigned max, + struct ck_acpi_scan_info *info) +{ + int n = 0; + memset(info, 0, sizeof *info); + for (int i = 0; i < nt; i++) { + if (t[i].len < 36) { + info->refused++; + if (!info->first_refusal) info->first_refusal = CK_AML_E_LEN; + continue; + } + info->tables++; + uint32_t len = rd32(t[i].data + 4); + size_t avail = len >= 36 && len <= t[i].len ? len : 36; + struct ck_acpi_device d[CK_XHCI_PLAT_MAX]; + unsigned seen = 0; + int rc = ck_aml_scan(t[i].data, avail, ids, nids, d, CK_XHCI_PLAT_MAX, &seen); + if (rc < 0) { + info->refused++; + if (!info->first_refusal) info->first_refusal = rc; + continue; + } + info->devices += seen; + for (int k = 0; k < rc; k++) { + if (k < (int)CK_XHCI_PLAT_MAX && n < (int)max && out) { + out[n].d = d[k]; + memcpy(out[n].table, t[i].data, 4); + out[n].table[4] = 0; + } + n++; + } + } + return n; +} + +static void iort_notes(const uint8_t *t) +{ + uint32_t len = rd32(t + 4), count = rd32(t + 36), off = rd32(t + 40); + printf("# iort: length=%u nodes=%u\n", len, count); + for (uint32_t i = 0; i < count && off >= 48 && off < len && len - off >= 16; i++) { + const uint8_t *n = t + off; + uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; + if (nlen < 16 || nlen > len - off) break; + uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); + if (n[0] == 4 && nlen >= 24) + printf("# iort: node@0x%x smmuv3 base=0x%llx\n", off, (unsigned long long)rd64(n + 16)); + else if (n[0] == 1 && nlen > 29) { + char name[64]; + uint32_t end = mcount ? moff : nlen, k = 0; + for (; k + 1 < sizeof name && 29 + k < end && n[29 + k]; k++) name[k] = (char)n[29 + k]; + name[k] = 0; + printf("# iort: node@0x%x named %s maps=%u\n", off, name, mcount); + } else + printf("# iort: node@0x%x type=%u maps=%u\n", off, n[0], mcount); + for (uint32_t m = 0; m < mcount && moff + (m + 1) * 20 <= nlen; m++) { + const uint8_t *e = n + moff + m * 20; + uint32_t ref = rd32(e + 12); + unsigned long long tb = ref + 24 <= len && t[ref] == 4 ? (unsigned long long)rd64(t + ref + 16) : 0; + printf("# map in=0x%x count=%u out=0x%x ref=0x%x flags=0x%x smmu=0x%llx\n", rd32(e), rd32(e + 4), + rd32(e + 8), ref, rd32(e + 16), tb); + } + off += nlen; + } +} + +/* Why ck_iort_parse refuses: root-complex (type 2) mappings that target the + * first SMMUv3 node, against CK_IORT_MAX_MAPS, and the largest output id + * against the linear stream table (STE_N). */ +static void parse_notes(const uint8_t *t) +{ + uint32_t len = rd32(t + 4), count = rd32(t + 36), off = rd32(t + 40), first = 0, rc_maps = 0, max_out = 0; + for (int pass = 0; pass < 2; pass++) { + uint32_t o = off; + for (uint32_t i = 0; i < count && o >= 48 && o < len && len - o >= 16; i++) { + const uint8_t *n = t + o; + uint32_t nlen = (uint32_t)n[1] | (uint32_t)n[2] << 8; + if (nlen < 16 || nlen > len - o) break; + if (pass == 0 && n[0] == 4 && !first) first = o; + uint32_t mcount = rd32(n + 8), moff = rd32(n + 12); + if (pass == 1 && n[0] == 2) + for (uint32_t m = 0; m < mcount && moff + (m + 1) * 20 <= nlen; m++) { + const uint8_t *e = n + moff + m * 20; + if (rd32(e + 12) != first) continue; + rc_maps++; + uint32_t top = rd32(e + 8) + rd32(e + 4); + if (top > max_out) max_out = top; + } + o += nlen; + } + } + printf("# smmu: %u PCI root-complex mapping(s) to the first SMMUv3 (limit CK_IORT_MAX_MAPS=%u), highest stream id 0x%x (linear table holds 0x0-0x%x)\n", + rc_maps, (unsigned)CK_IORT_MAX_MAPS, max_out, STE_N - 1u); +} + +static int last_seg_same(const char *a, const char *b) { return ck_aml_last_seg_eq(a, b); } + +int main(int argc, char **argv) +{ + const char *iort_path = 0; + struct table t[TABLES_MAX]; + int nt = 0; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--iort") && i + 1 < argc) { + iort_path = argv[++i]; + continue; + } + if (argv[i][0] == '-' || nt == TABLES_MAX) { + fprintf(stderr, "usage: ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...]\n"); + return 2; + } + t[nt].path = argv[i]; + if (!(t[nt].data = read_file(argv[i], &t[nt].len))) return 2; + nt++; + } + if (!nt) { + fprintf(stderr, "usage: ck_acpi_scan [--iort IORT-FILE] DSDT-FILE [SSDT-FILE ...]\n"); + return 2; + } + uint8_t *iort = 0; + size_t iort_len = 0; + if (iort_path && !(iort = read_file(iort_path, &iort_len))) return 2; + if (iort && (iort_len < 48 || rd32(iort + 4) > iort_len)) { + fprintf(stderr, "ck_acpi_scan: %s is shorter than its header length\n", iort_path); + return 2; + } + + /* ---- discovery (dev/xhci_fence.c acpi_discover) ---- */ + static const char *const ids[] = CK_XHCI_ACPI_IDS; + static const char *const ctl[] = { CK_ACPI_CONTROL_ID }; + struct ck_acpi_scan_info info; + struct plat c, p[CK_XHCI_PLAT_MAX]; + int nc = scan_all(t, nt, ctl, 1, &c, 1, &info); + printf(CK_ACPI_SCAN_FMT, info.tables, info.refused, info.first_refusal, info.devices); + if (nc > 0) + printf(CK_ACPI_CONTROL_FMT, c.d.hid, c.d.path, (unsigned long long)c.d.mmio_base, (unsigned long long)c.d.mmio_len); + else + printf(CK_ACPI_CONTROL_NONE); + int n = scan_all(t, nt, ids, CK_XHCI_ACPI_NIDS, p, CK_XHCI_PLAT_MAX, &info); + if (n > (int)CK_XHCI_PLAT_MAX) n = (int)CK_XHCI_PLAT_MAX; + for (int i = 0; i < n; i++) + printf(CK_XHCI_ACPI_FMT, p[i].d.path, p[i].d.hid[0] ? p[i].d.hid : "-", p[i].d.cid[0] ? p[i].d.cid : "-", + p[i].table, (unsigned long long)p[i].d.mmio_base, (unsigned long long)p[i].d.mmio_len); + printf(CK_XHCI_ACPI_COUNT_FMT, n); + + /* ---- IORT (core/acpi.c, core/smmu_svc.c) ---- */ + struct ck_iort_smmu first; + int prc = 0; + if (iort) { + iort_notes(iort); + prc = ck_iort_parse(iort, &first); + if (prc == 1) + printf("# smmu: the kernel drives the first SMMUv3 node: node@0x%x base=0x%llx (pci maps=%u)\n", + first.node_off, (unsigned long long)first.base, first.nmaps); + else + printf("# smmu: ck_iort_parse=%d (%s)\n", prc, prc == 0 ? "no SMMUv3 node" : "malformed"); + parse_notes(iort); + } else + printf("# smmu: no IORT given; dma_gate lines below assume none (NoSmmu)\n"); + + /* ---- fence (dev/xhci_fence.c ck_xhci_fence, plat_fence_one) ---- */ + printf("keyboard: no PCI xHCI controller; trying %d ACPI platform controller(s)\n", n); + if (n == 0) printf("keyboard: unavailable (no xHCI controller)\n"); + int fenced = 0, smmu_said = 0; + for (int i = 0; i < n; i++) { + const char *nm = p[i].d.path; + printf(CK_XHCI_PLAT_TRY_FMT, nm, (unsigned long long)p[i].d.mmio_base, (unsigned long long)p[i].d.mmio_len); + int amb = 0; + for (int j = 0; j < n; j++) + if (j != i && last_seg_same(p[j].d.path, nm)) amb = 1; + if (amb) { + printf(CK_XHCI_PLAT_SKIP_FMT, nm, "name not unique in DSDT/SSDT"); + continue; + } + if (!p[i].d.mmio_base || p[i].d.mmio_len < 0x1000u || (p[i].d.mmio_base & 0xfffu)) { + printf(CK_XHCI_PLAT_SKIP_FMT, nm, "no usable _CRS memory range"); + continue; + } + printf("? xhci: %s caplength= hciversion= (platform)\n", nm); + printf("? xhci: halted before gate usbcmd= usbsts= halted=\n"); + const char *deny = 0; + if (!iort || prc == 0) + deny = "NoSmmu"; + else if (prc < 0) { + if (!smmu_said++) /* bring_up caches its state: once per boot, by the first DMA grant request (may be an earlier device) */ + printf("? smmu: IORT malformed, SMMU unusable (fail closed) [once per boot, at the first DMA grant request]\n"); + deny = "SmmuNotReady"; + } + if (deny) { + printf(CK_XHCI_PLAT_DENY_FMT, nm, deny); + if (iort) { + struct ck_iort_named w; + int wrc = ck_iort_named(iort, nm, &w); + if (wrc == 1 && w.target_off) + printf("# %s: IORT named component %s stream 0x%x on the SMMUv3 at 0x%llx%s\n", nm, w.name, + w.stream_id, (unsigned long long)w.target_base, + prc != 1 ? " (the first SMMUv3 node; granted only once the kernel can bring it up)" : ""); + else + printf("# %s: no IORT stream (ck_iort_named=%d)\n", nm, wrc); + } + continue; + } + printf("# assumes the SMMUv3 at 0x%llx comes up (else a smmu: bring-up line and SmmuNotReady)\n", + (unsigned long long)first.base); + struct ck_iort_named nc2; + int nrc = ck_iort_named(iort, nm, &nc2); + if (nrc != 1 || !nc2.target_off) { + printf(CK_XHCI_PLAT_DENY_FMT, nm, nrc == -2 ? "Ambiguous" : "NoStream"); + continue; + } + if (nc2.target_off != first.node_off) { + printf(CK_XHCI_PLAT_OTHER_FMT, nm, (unsigned long long)nc2.target_base, nc2.stream_id); + continue; + } + if (nc2.stream_id >= STE_N) { + printf(CK_XHCI_PLAT_DENY_FMT, nm, "NoStream"); + continue; + } + printf(CK_XHCI_PLAT_GRANT_FMT, nm, (unsigned long long)first.base, nc2.stream_id); + printf("? smmu_dma_window: xhci %s only, translation active iova= len=\n", nm); + printf("? keyboard phase on %s, then release: halt, reset, stream 0x%x back to abort\n", nm, nc2.stream_id); + fenced++; + } + if (n > 0) printf("keyboard: unavailable (no keyboard on any platform xHCI)\n"); + printf("# %d controller(s) would reach the keyboard phase\n", fenced); + return 0; +} diff --git a/scripts/.crumb b/scripts/.crumb index c423ddb6..f8c6817f 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-06T01:02:05Z", + "compiled_at": "2026-10-06T03:00:56Z", "compiler": "crumb-compile/1", - "digest": "sha256:e8e8c21a3f1b0e2a2df96918fd438edf57c0f38095d04c9abc529225c4da9363", + "digest": "sha256:e05c9be44a099db64118bb542ef8f97041efe9642107c7007291c44b7aac1ff4", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 65, - "generated_at_commit": "20587476bf0d4b7577cc3b62857a9166300681bf", - "source_tree": "sha256:66055c3a599af41cb73b5dd2d177ea04241662ce3e3ceaad59e3012cc218faea" + "files": 66, + "generated_at_commit": "b79954c7278df871a8226d51c6a594918676aba3", + "source_tree": "sha256:99777fce9b13056d307ffa853656a83a7cd72d5ec5e2e6da1ba6fe5ac8925e53" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_keyboard_test.sh b/scripts/qemu_ck_keyboard_test.sh index b61edb15..b1e91634 100755 --- a/scripts/qemu_ck_keyboard_test.sh +++ b/scripts/qemu_ck_keyboard_test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# KEYBOARD gate for the AIENOS C kernel: rows 25-32 of native/kernel/GATES.md +# KEYBOARD gate for the AIENOS C kernel: rows 25-32 (C-only 30a-30c, 32a, 32b) of native/kernel/GATES.md # "SEED-0A keyboard" (Rust oracle: scripts/qemu_keyboard_test.sh with SMMU=1 # and SMMU=0) plus the C-only rows 30a-30c (recovery access, line overflow). # QEMU is not hardware: a PASS here qualifies nothing physical. @@ -37,8 +37,19 @@ # stream 0x.. returned to abort (rc=0)", in that order, all before # the "devices: xhci=fenced (rc=0)" line # row 32 no "report_kind: (panic|fault)", "report_kind: final", QEMU exit 0 +# row 32a (C only, every boot; NEXT-PHASE-3 cut 2) the ACPI platform walker runs on +# QEMU's own tables: "acpi_scan: tables=N refused=0 +# first_refusal=0 devices=M" with N, M > 0, the positive control +# "acpi_scan: control ARMH0011 mmio=0x9000000+0x1000" (the +# QEMU virt PL011 UART), "xhci_acpi: 0 platform controller(s)" (QEMU +# virt describes no platform xHCI), and the PCI path is kept: no +# "xhci_plat:" line and no fallback to platform controllers +# row 32b (C only) hardware expectation, always NOT_RUN here: the exact lines the +# DGX Spark should print, predicted by tools/ck_acpi_scan.c from +# its firmware tables (native/kernel/tests/fixtures/ +# spark_xhci_acpi_expected.txt); never counted toward the verdict # boot kbd-nosmmu no SMMU (Rust SMMU=0), same image, no keys: -# rows 25, 26, 27, 32 as above +# rows 25, 26, 27, 32, 32a as above # row 28 "dma_gate: xhci denied (NoSmmu), bus master stays off", "keyboard: # unavailable (SMMU DMA isolation not active)", COMMAND read back # after the deny with bus_master=off, no "dma_gate: xhci granted", @@ -246,7 +257,7 @@ boot() { # Row bookkeeping: rowfail[n]=1 once any check of row n failed in this # evaluation; rowmodes[n] lists the boots that checked it. -all_rows=(25 26 27 28 29 30 30a 30b 30c 31 32) +all_rows=(25 26 27 28 29 30 30a 30b 30c 31 32 32a) declare -A rowfail rowmodes reset_rows() { local r; for r in "${all_rows[@]}"; do rowfail[$r]=0; rowmodes[$r]=""; done; } reset_rows @@ -273,6 +284,7 @@ evaluate_dma() { mode="$1" rcheck 25 "xHCI found on the ECAM walk (Rust: keyboard: xhci )" \ '^keyboard: xhci 0000:[0-9a-f]{2}:[0-9a-f]{2}\.[0-7] mmio 0x[0-9a-f]+ ' + evaluate_acpi rcheck 26 "post-exit bus-master sweep of the segment" \ '^dma_sweep: seg 0000 bus [0-9a-f]{2}-[0-9a-f]{2} functions=[1-9][0-9]* bridges=[0-9]+ bridges_bme=[0-9]+ endpoints_bme_found=[0-9]+ still_enabled=0$' rabsent 26 "no endpoint left with bus master stuck on" '^dma_sweep: bme STUCK' @@ -307,6 +319,18 @@ evaluate_dma() { fi } +# evaluate_acpi: row 32a (every boot). QEMU virt has no platform xHCI, so the +# walker must find its tables, the UART control and zero controllers, and the +# PCI xHCI path must not fall back to the platform list. +evaluate_acpi() { + rcheck 32a "ACPI walker read QEMU's DSDT/SSDTs with no refusal" \ + '^acpi_scan: tables=[1-9][0-9]* refused=0 first_refusal=0 devices=[1-9][0-9]* \(static scan; _STA not evaluated\)$' + rcheck 32a "positive control: QEMU virt UART ARMH0011 at 0x9000000" \ + '^acpi_scan: control ARMH0011 [A-Z0-9_]{1,4} mmio=0x9000000\+0x1000$' + rcheck 32a "no ACPI platform xHCI on QEMU virt" '^xhci_acpi: 0 platform controller\(s\)$' + rabsent 32a "PCI path kept: no platform controller tried" '^(xhci_plat: |keyboard: (no PCI xHCI controller|none on the PCI xHCI))' +} + # evaluate_final smmu|nosmmu: row 32 for a boot that must end with the final report. evaluate_final() { mode="$1" @@ -434,17 +458,18 @@ if [[ "${mutation_run}" == 0 ]]; then case "${r}" in 25|26|27|28|31|32) dma_fail=1 ;; esac fi done + echo "KEYBOARD_ROW 32b: NOT_RUN (hardware only; expected DGX Spark lines: native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt)" [[ "${m1_fail}" == 0 ]] || echo "M1 checks failed on at least one boot: no row can pass" [[ "${other_fail}" == 0 ]] || echo "image checks failed (mutation banner, unsafe bypass, HID marker or commit)" if [[ "${dma_fail}" == 0 ]]; then echo "AIENOS_CK_KEYBOARD_DMA: PASS (rows 25-28,31,32; modes ${modes[*]})"; else echo "AIENOS_CK_KEYBOARD_DMA: FAIL (rows 25-28,31,32; modes ${modes[*]})"; fi if [[ "${gate_fail}" != 0 ]]; then - echo "${final_tag}: FAIL (rows 25-32,30a-30c; modes ${modes[*]}; QEMU only)" + echo "${final_tag}: FAIL (rows 25-32,30a-30c,32a; modes ${modes[*]}; QEMU only)" exit 1 elif [[ "${partial}" != 0 ]]; then echo "${final_tag}: NOT_RUN (partial run, modes ${modes[*]}; every boot is needed for the gate)" exit 3 fi - echo "${final_tag}: PASS (rows 25-32,30a-30c; boots kbd-smmu kbd-nosmmu kbd-recovery; QEMU only, hardware NOT_RUN)" + echo "${final_tag}: PASS (rows 25-32,30a-30c,32a; boots kbd-smmu kbd-nosmmu kbd-recovery; QEMU only, hardware NOT_RUN)" exit 0 fi diff --git a/scripts/stage_one_time_boot_ck_full.sh b/scripts/stage_one_time_boot_ck_full.sh new file mode 100755 index 00000000..91880756 --- /dev/null +++ b/scripts/stage_one_time_boot_ck_full.sh @@ -0,0 +1,241 @@ +#!/usr/bin/env bash +# Stage the FULL C kernel hardware image (native/kernel `make full +# CK_HARDWARE_STAGING=1`: devices stage with the xHCI keyboard and recovery +# access hook, NVMe and Store stages) on Machine 1 for exactly one attended +# boot. NEXT-PHASE-3 cut 2. Variant of scripts/stage_one_time_boot_ck.sh, +# which stages the inference image (no devices stage, so no keyboard). +# +# scripts/stage_one_time_boot_ck_full.sh --dry-run print every command, touch nothing +# scripts/stage_one_time_boot_ck_full.sh --apply do it (needs sudo; operator only) +# +# Exactly one of the two is required; there is no default mode. +# +# Owner files (the hardware staging image refuses the TEST Store keys and +# TEST machine id; native/kernel/README.md, "Owner provisioning"): +# AIENOS_OWNER_PUBKEYS= +# AIENOS_MACHINE_ID= +# The labelled TEST-FIXTURE files under native/kernel/tests/fixtures/owner/ +# are refused here: they are for build checks, not for a boot. +# +# --dry-run runs only read-only probes (git, findmnt, lsblk, efibootmgr +# without arguments, the Secure Boot variable) and prints every command +# --apply would run, in order. It builds nothing, writes nothing, creates no +# file or directory, and calls no sudo. +# +# --apply, in order: +# 1. builds the hardware staging image with CK_FB_HOLD_S=120 (the last +# report stays on screen for two minutes before the reset) and checks +# it carries the platform xHCI discovery and no TEST-only marker; +# 2. sudo scripts/verify_native_rollback.sh --capture-pre (before BootNext, +# which the capture requires absent); +# 3. copies the image to \EFI\AIENOS on the ESP (the only scratch +# directory, docs/NATIVE_BOOT_ONE_TIME.md invariant 4) with a +# STAGED-CK-FULL.TXT record; +# 4. creates a boot entry WITHOUT adding it to BootOrder (BootOrder is +# restored if anything changed it), checks that Boot0004 AIENOSRECOV is +# still there, and sets BootNext once. +# +# It never reboots. The firmware consumes BootNext on that boot, so the next +# boot after it returns to the normal BootOrder (Linux). The boot itself is +# attended: Drake at the machine, AIENOSRECOV stick inserted (Boot0004). +# What the screen should show is in native/kernel/README.md ("Physical +# attended boot") and is predicted from this machine's firmware tables by +# `make -C native/kernel acpi-scan` (tools/ck_acpi_scan.c). +# +# UNVERIFIED (no hardware run yet): that the full image leaves the internal +# disk unwritten on the Spark. By construction its Store stage refuses +# (production key source BLOCKED_OPERATOR) and NVMe DMA is granted only +# through the SMMU; no hardware run has confirmed it. +# No Python. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" + +mode="" +case "${1:-}" in + --dry-run) mode=dry ;; + --apply) mode=apply ;; + *) echo "usage: $0 --dry-run | --apply" >&2; exit 2 ;; +esac +[[ $# -eq 1 ]] || { echo "usage: $0 --dry-run | --apply" >&2; exit 2; } + +disk="/dev/nvme0n1" +esp="/boot/efi" +owner_pubkeys="${AIENOS_OWNER_PUBKEYS:-}" +machine_id="${AIENOS_MACHINE_ID:-}" +out="${repo_root}/target/native-kernel-full-staging" +image="${out}/full-hardware-staging/BOOTAA64.EFI" +loader='\EFI\AIENOS\aienos-ck-full.efi' +label="AIENOS C kernel full (one-time)" +recov_entry="0004" +recov_label="AIENOSRECOV" +hold_s=120 +secure_boot_var="/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c" +stamp="$(date -u +%Y%m%dT%H%M%SZ)" +evidence_dir="${AIENOS_STAGE_OUT:-${HOME}/workspace/evidence-out/NP3/staging}/${stamp}" + +stops=() +stop() { stops+=("$*"); echo "STOP: $*"; } + +# Print a command; run it only with --apply. +run() { + printf '+' + printf ' %q' "$@" + printf '\n' + [[ "${mode}" == apply ]] || return 0 + "$@" +} + +echo "mode: ${mode}" + +# ---- read-only pre-flight ---- +if [[ -n "$(git status --porcelain)" ]]; then + stop "the checkout has uncommitted changes; the boot must map to one exact commit" +fi +commit="$(git rev-parse HEAD)" +echo "commit: ${commit}" + +for f in owner_pubkeys machine_id; do + v="${!f}" + if [[ -z "${v}" ]]; then + stop "${f} file not given (set AIENOS_OWNER_PUBKEYS and AIENOS_MACHINE_ID)" + elif [[ ! -f "${v}" ]]; then + stop "${f} file not found: ${v}" + elif [[ "$(basename "${v}")" == TEST-FIXTURE* ]]; then + stop "${f} is a labelled TEST-FIXTURE file (${v}); a boot needs the operator's own file" + else + v="$(realpath -- "${v}")" # make -C native/kernel resolves relative paths from there + printf -v "${f}" %s "${v}" + echo "${f}: ${v}" + fi +done + +esp_dev="$(findmnt -no SOURCE "${esp}" 2>/dev/null || true)" +esp_opts="$(findmnt -no OPTIONS "${esp}" 2>/dev/null || true)" +[[ -n "${esp_dev}" ]] || stop "the ESP is not mounted at ${esp}" +esp_disk="/dev/$(lsblk -no PKNAME "${esp_dev}" 2>/dev/null | head -1)" +esp_part="$(cat "/sys/class/block/$(basename "${esp_dev:-none}")/partition" 2>/dev/null || true)" +echo "esp: ${esp_dev:-?} partition ${esp_part:-?} of ${esp_disk} (${esp_opts%%,*})" +[[ "${esp_disk}" == "${disk}" ]] || stop "the ESP is not on ${disk}" +[[ ",${esp_opts}," == *",rw,"* ]] || stop "the ESP is not mounted read-write" + +if [[ -r "${secure_boot_var}" ]] && [[ "$(od -An -t u1 -j 4 -N 1 "${secure_boot_var}" | tr -d ' ')" == "1" ]]; then + stop "Secure Boot is enabled; the firmware will refuse the unsigned C image" +else + echo "secure boot: off or unreadable (recorded only; development decision 2026-10-01)" +fi + +boot_state="$(efibootmgr 2>/dev/null || true)" +[[ -n "${boot_state}" ]] || stop "efibootmgr gave no output" +order_before="$(sed -n 's/^BootOrder: //p' <<<"${boot_state}")" +current_before="$(sed -n 's/^BootCurrent: //p' <<<"${boot_state}")" +next_before="$(sed -n 's/^BootNext: //p' <<<"${boot_state}")" +recov_line="$(grep -E "^Boot${recov_entry}\*? " <<<"${boot_state}" || true)" +echo "BootCurrent: ${current_before}" +echo "BootOrder: ${order_before} (never changed)" +[[ -z "${next_before}" ]] || stop "BootNext is already set (${next_before}); another staging is pending" +if [[ "${recov_line}" == *"${recov_label}"* ]]; then + echo "recovery: Boot${recov_entry} ${recov_label} present (kept untouched)" +else + stop "Boot${recov_entry} ${recov_label} not present: insert the recovery stick and check docs/RECOVERY_MEDIA_MACHINE1.md (got: '${recov_line:-none}')" +fi +existing="$(sed -n "s/^Boot\([0-9A-F]\{4\}\)\*\{0,1\} ${label}.*/\1/p" <<<"${boot_state}" | head -1)" +echo "boot entry: '${label}' ${existing:+exists as Boot${existing}}${existing:-will be created (create-only, not in BootOrder)}" +echo "evidence: ${evidence_dir}" +echo "screen hold: ${hold_s} s before the reset (CK_FB_HOLD_S)" + +if [[ "${mode}" == apply && ${#stops[@]} -gt 0 ]]; then + echo "STOPPED before touching anything: ${#stops[@]} pre-flight problem(s) above." + exit 1 +fi + +echo +echo "commands (--apply runs them in this order; --dry-run only prints them):" + +# ---- 1. build ---- +cross="" +[[ "$(uname -m)" == aarch64 ]] || cross="aarch64-linux-gnu-" +run make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full \ + CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${owner_pubkeys:-}" \ + CK_MACHINE_ID="${machine_id:-}" CK_FB_HOLD_S="${hold_s}" +if [[ "${mode}" == apply ]]; then + grep -aqF "screen: gop " "${image}" || { echo "STOP: the image has no screen console"; exit 1; } + grep -aqF "xhci_acpi: " "${image}" || { echo "STOP: the image has no platform xHCI discovery"; exit 1; } + grep -aqF "recovery_access: " "${image}" || { echo "STOP: the image has no recovery access hook"; exit 1; } + for m in "TEST-ONLY" "TEST Store" "TEST machine id"; do + if grep -aqF "${m}" "${image}"; then echo "STOP: the image carries a TEST marker (${m})"; exit 1; fi + done + digest="$(sha256sum "${image}" | cut -d' ' -f1)" + echo "image sha256: ${digest}" +else + echo "+ (check the image: screen console, xhci_acpi discovery and recovery_access present; no TEST-ONLY, TEST Store or TEST machine id marker)" + digest="(computed after the build)" +fi + +# ---- 2. pre-boot capture (BootNext still absent) ---- +run mkdir -p "${evidence_dir}" +run sudo scripts/verify_native_rollback.sh --capture-pre "${evidence_dir}/pre_boot_capture.json" + +# ---- 3. ESP scratch directory \EFI\AIENOS ---- +record="${evidence_dir}/STAGED-CK-FULL.TXT" +if [[ "${mode}" == apply ]]; then + cat >"${record}" <}" \ + --label "${label}" --loader "${loader}" + if [[ "${mode}" == apply ]]; then + entry="$(efibootmgr | sed -n "s/^Boot\([0-9A-F]\{4\}\)\*\{0,1\} ${label}.*/\1/p" | head -1)" + [[ -n "${entry}" ]] || { echo "STOP: the boot entry was not created"; exit 1; } + fi +fi +entry="${entry:-NNNN}" +[[ "${entry}" != "${recov_entry}" ]] || { echo "STOP: the new entry took the recovery entry's number"; exit 1; } +if [[ "${mode}" == apply ]]; then + order_after="$(efibootmgr | sed -n 's/^BootOrder: //p')" + if [[ "${order_after}" != "${order_before}" ]]; then + run sudo efibootmgr --quiet --bootorder "${order_before}" + fi + efibootmgr | grep -qE "^Boot${recov_entry}\*? .*${recov_label}" \ + || { echo "STOP: Boot${recov_entry} ${recov_label} is gone; BootNext NOT set"; exit 1; } + echo "aienos_boot_entry: ${entry}" >>"${record}" +else + echo "+ (only if BootOrder changed: sudo efibootmgr --quiet --bootorder ${order_before})" + echo "+ (check Boot${recov_entry} ${recov_label} still present, else stop before BootNext)" +fi +run sudo install -m 0644 "${record}" "${esp}/EFI/AIENOS/STAGED-CK-FULL.TXT" +run sudo efibootmgr --quiet --bootnext "${entry}" + +echo +if [[ "${mode}" == dry ]]; then + if [[ ${#stops[@]} -gt 0 ]]; then + echo "Dry run: nothing touched. --apply would STOP now on ${#stops[@]} pre-flight problem(s) above." + else + echo "Dry run: nothing touched. Pre-flight clean; --apply would stage the boot above." + fi + exit 0 +fi +cat "${record}" +efibootmgr | sed -n '1,4p' +echo "Staged: the next boot runs Boot${entry} once. Nothing was rebooted." +echo "Reboot only with the operator at the machine and the ${recov_label} stick inserted." +echo "After the return to Linux: sudo scripts/verify_native_rollback.sh --capture-post."