Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/patches/e06518848daa31df # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/aienux#308 (code only) closed derived `e06518848daa31df` #308 Imported from GitHub aien-dev/aienux#308 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `51c05daff49b0836692eb1c777ec2758cf0746ae` · Head `6c513ff8819e6be7c8fe109f614c953d8aa73b27` ### Apply it ``` git checkout -b patch-e06518848daa31df 51c05daff49b0836692eb1c777ec2758cf0746ae curl -fsSL https://interplane.io/drakestapleton/aienux/patches/e06518848daa31df.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 308 6c513ff8819e6be7c8fe109f614c953d8aa73b27 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":308,"url":"https://github.com/aien-dev/aienux/pull/308","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:11Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index eeea1794..b3bb6039 100644 --- a/.crumb +++ b/.crumb @@ -22,38 +22,38 @@ "children": [ { "digest": "e4f7c9f4b54d792a3d410c4143742cd0f95b64858494d6bfa9ffc6cc9ff7687a", - "last_commit": "362cba9", + "last_commit": "51c05da", "name": "crates", "purpose": "Directory crates. Purpose not yet described by a human." }, { "digest": "4a006f504e49f1586a9591657f799ab634dd4b8256fbbed233d2bfbf541a7907", - "last_commit": "362cba9", + "last_commit": "51c05da", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "6e42346a1b795fb11cb1fe616b7e144e92002110f9de17fbfe18efab8843fde0", - "last_commit": "34d6878", + "digest": "0ab785d09f7eab5872d69b17ee8ffe01f169d64fc0dd477f0719e29d6faee7d7", + "last_commit": "8daa91d", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { - "digest": "9cb7696ff81e7d3e7cc312f7249aba2f0a7e7345b9ba7b80a3ab4515bbad10d9", - "last_commit": "219a061", + "digest": "9fac5e2c6ace7bf64f1773b6da11b091ec82d394f285baad457758435df36f98", + "last_commit": "8daa91d", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:1540413c18548a793b8a0ccd8641b1b4ac2dc9c0b1054dba818f7206d801e992", - "compiled_at": "2026-10-11T21:20:24Z", + "children_root": "sha256:5e87521eb4857240f8ddf1b69307ff45a8a912351e609b143352fa4b70fb3cfe", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:50df40dfa027ff79cf79a85ae9ffb9f83164f8de53001a363a35869f86f8c609", + "digest": "sha256:3f9f11932753ab5f47d4768bbdbc99cc4b3497386fa188754c3aa18182b29e79", "evidence_files": 155, "evidence_root": "sha256:d59c0cef4e7dd5172923754a95b411ca54b01f35baca4587f792f1b259f1eaf7", - "files": 3067, - "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", - "source_tree": "sha256:8e0cdae2c426f575f843943b3a3d88eb7afe8a8692c43a9668218f99cd01a8b4" + "files": 3072, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:a56b9be1d834f2d8ff5040e496999910fb2bd99d179546628aaae9945defac06" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index f9359f72..2884e5b8 100644 --- a/native/.crumb +++ b/native/.crumb @@ -62,7 +62,7 @@ }, { "digest": "aaee52694510c1aea1e16ffd54201ce8e900742974d538bbf4640ab01d239872", - "last_commit": "34d6878", + "last_commit": "51c05da", "name": "capability", "purpose": "Directory native/capability. Purpose not yet described by a human." }, @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "fdb1af443afa270d2ce93cdec1e65f2d6e3ae9e69295a919e89b4de015c95267", - "last_commit": "863bef8", + "digest": "fd5099789ea8d7044949cbad944c1a3a2d8fa4ebd820ab1d6f1c5b05007810f1", + "last_commit": "8daa91d", "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:cb6dc92fb6138b21f29fd64099df425f2ffb2a81072b694fb8cb75e96ba2fdfc", - "compiled_at": "2026-10-11T21:20:24Z", + "children_root": "sha256:4761990bc08d0c36329945919f884b543bbf945b65fa6b5a85c9e9b4f2b6cd8f", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:6e42346a1b795fb11cb1fe616b7e144e92002110f9de17fbfe18efab8843fde0", + "digest": "sha256:0ab785d09f7eab5872d69b17ee8ffe01f169d64fc0dd477f0719e29d6faee7d7", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 456, - "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", - "source_tree": "sha256:45c0f7ec63b02b683ac09a9ff1556a2e9f45e84ffbd11ac0cb9603a0e1ada20e" + "files": 460, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:08b3892231de7346823a8554cdf4c4056a54a0a87cb555a28bca19ba2a1087e5" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 22d39278..f92d63f6 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -57,8 +57,8 @@ "purpose": "Directory native/kernel/artifact. Purpose not yet described by a human." }, { - "digest": "85b29782329a9f640ff976034d8bda060b51ff8288e2547151aa6f30639f2bb1", - "last_commit": "3f79153", + "digest": "6c183da30a2362e1e5c86b485cc6f6be46cfe9a9e30960f8f395e47659b18d42", + "last_commit": "8daa91d", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, @@ -69,8 +69,8 @@ "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, { - "digest": "2e52b5bd3addb2fff0ab5f761e1daf67befcc72291117a03534857fff229ae45", - "last_commit": "e90226b", + "digest": "bad996a1149ddaa6a4404ac97b147414f8df3b376d9f403d9bca7b0ea72a4e9c", + "last_commit": "8daa91d", "name": "include", "purpose": "Directory native/kernel/include. Purpose not yet described by a human." }, @@ -81,14 +81,14 @@ "purpose": "Directory native/kernel/mm. Purpose not yet described by a human." }, { - "digest": "2712bd906a89792008981a3dc88d1f28f5c4ebcf278751ab852bfd0e76a62e1c", - "last_commit": "98fbc3c", + "digest": "97067439083188400b87693964d28f9a06fb8b6cf3ff1271c4016ecbe80e5133", + "last_commit": "8daa91d", "name": "svc", "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "7d63e0731c50350099774d048adcb20faa53e61648fb164ead40e7f7b3f3cd92", - "last_commit": "e90226b", + "digest": "c1ffd88afd48fbef6e14bcec2a9c1318e372ab31c01481f3fb60a8ed0b04a6e1", + "last_commit": "8daa91d", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Directory native/kernel/tools. Purpose not yet described by a human." } ], - "children_root": "sha256:597549bbdb05f151819fc87743590e13b43e0a9bd054b578367e68db6bba6050", - "compiled_at": "2026-10-11T01:27:19Z", + "children_root": "sha256:5cc12ebd8eba25b26b0dc719d258803dd234004d199c22427d41e9c53fa63725", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:fdb1af443afa270d2ce93cdec1e65f2d6e3ae9e69295a919e89b4de015c95267", + "digest": "sha256:fd5099789ea8d7044949cbad944c1a3a2d8fa4ebd820ab1d6f1c5b05007810f1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 292, - "generated_at_commit": "3f791539d41d852b2b6fd36fdebaebb9f76dc2a0", - "source_tree": "sha256:4311aa32fe1225a33b88780b6c2a248d051ecdb10a079c8df0bad4870df9c2bb" + "files": 296, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:a5866f1308a2b86c92c89d410e332e90e781a7b188265dd566f7f4d34359bb65" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 8fde5c3c..3a7e1982 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -805,3 +805,20 @@ real asynchronous events. (4) Numeric resource-class and operation ids, the REVO are this adapter's choices (UNVERIFIED against a frozen contract). (5) The request digest covers the request cells and the OUT bytes; the contract's exact digest input is not defined (UNVERIFIED). (6) The "task" is the adapter's session; the EL0 shell units hold no capability (caps=0) and the check is made by the kernel-side adapter. + +## LIFECYCLE: resident production lifecycle (native v1 plan P03, C-only) -> CK `LIFECYCLE` (scripts/qemu_ck_lifecycle_test.sh) + +The product boundary. A `CK_SERVICE=1` image (`full-service/`, allowed with `CK_HARDWARE_STAGING`, refused with every TEST-only flag) links `core/lifecycle.c` + `core/lifecycle_fsm.c`: after every boot stage the kernel serves bounded operator requests on the serial console (states init, locked, ready, serve, quiesce, stop; verbs status, unlock, task , approve, deny, cancel, evidence, help, shutdown; one 64-byte line, one 48-byte task, one pending task at a time) until `shutdown`, then runs the stop order (Store closed, NVMe normal shutdown + bus master off + SMMU stream to abort, final report, reset) instead of resetting at once. The diagnostic images are unchanged (the weak `ck_lifecycle_run` is absent). The production key source still refuses (no in-kernel K_vol unwrap, P04), so the production service image comes up LOCKED. The TEST-ONLY `CK_SERVICE_TEST_KEYS=1` image (`full-service-testkeys/`, refused with `CK_HARDWARE_STAGING`) opens the Store with the TEST keys so the ready/serve/commit paths run in QEMU. Host test: `tests/test_lifecycle.c` (`make test`, 41 checks on the state machine and decoder). QEMU qualifies nothing physical. + +| # | Check | CK gate | Status | +|---|---|---|---| +| 160 | production service image: the lifecycle starts after `mm_usage`, `state=locked store=locked`; the Store stage refused with the production key source (`rc=-2002` BlockedOperator, disk not read) and released the NVMe before the lifecycle; no TEST key banner | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 161 | locked: `task` and `unlock` REFUSED (state stays locked; `unlock` asks the production key source again and it refuses); `status`, `help`, `evidence` answered | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 162 | locked shutdown: `stop order store=was-locked nvme=released`, then `stage lifecycle: ok`, then the final report; no panic or fault; QEMU exit 0; M1 checks | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 163 | test-keys image: TEST-ONLY banner; Store open and NVMe kept bound for the service lifetime (no bus master revoke before `lifecycle: ready state=ready store=open`) | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 164 | serve: `task ` -> `task pending ... proposed="note: "`; `approve` -> `approve committed gen=N id=...` (one sealed Store object, kind 0x4e31 'N1'), state back to ready | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 165 | one task at a time: a second `task` while one is pending is refused; `approve` with nothing pending is refused; `deny` and `cancel` drop the pending task | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 166 | bounds: a 65-byte line is refused whole and never run; `evidence` counts agree (`tasks=3 approved=1 denied=1 cancelled=1 overlong=1`) with the last commit generation and object id | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 167 | stop order: `lifecycle: ready` < `dma_gate: nvme bus master revoked` < `stop order store=closed nvme=released` < `stage lifecycle: ok` < final report; no panic or fault; QEMU exit 0; M1 checks | LIFECYCLE | QEMU PASS (hardware NOT_RUN) | +| 167a | build shape: the default image carries no lifecycle; `CK_SERVICE` refused with `CK_CONSOLE_SESSION`, `CK_QEMU_UNSAFE_DMA` and any `CK_TEST_*`; `CK_SERVICE_TEST_KEYS` refused with `CK_HARDWARE_STAGING` | LIFECYCLE (build) | PASS | +| 167b | limits: operator input is serial only (USB keyboard binding stays with the xHCI session code, later cut); a committed note is not yet read back after a cold boot (P05/P06); the production image cannot leave LOCKED until P04 lands the real K_vol path; the lifecycle loop polls with interrupts as the boot stages left them (no scheduler integration yet) | LIFECYCLE | LIMIT (declared) | diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 0145b394..db638178 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -326,6 +326,43 @@ $(error CK_TEST_CONSOLE_MUTATION needs CK_CONSOLE_SESSION=1) endif endif +# CK_SERVICE=1 builds the resident production lifecycle image (core/lifecycle.c, +# native v1 plan P03): after every boot stage the kernel serves bounded operator +# requests on the serial console until "shutdown" (Store closed, NVMe released, +# then reset), instead of resetting at once. Own OUT subdirectory full-service/. +# Allowed with CK_HARDWARE_STAGING (it is the production entry); refused with +# every TEST-only flag. The production key source refuses today, so this image +# stays LOCKED. CK_SERVICE_TEST_KEYS=1 (TEST-ONLY, full-service-testkeys/, only +# scripts/qemu_ck_lifecycle_test.sh) opens the Store with the TEST keys so the +# ready/serve/commit paths run in QEMU; refused with CK_HARDWARE_STAGING. +CK_SERVICE ?= +CK_SERVICE_TEST_KEYS ?= +ifneq ($(strip $(CK_SERVICE)),) +ifneq ($(strip $(CK_SERVICE)),1) +$(error CK_SERVICE must be empty or 1) +endif +ifneq ($(strip $(CK_QEMU_UNSAFE_DMA)),) +$(error CK_SERVICE cannot be combined with CK_QEMU_UNSAFE_DMA) +endif +ifneq ($(strip $(CK_CONSOLE_SESSION)),) +$(error CK_SERVICE cannot be combined with CK_CONSOLE_SESSION) +endif +ifneq ($(strip $(CK_TEST_XHCI_MUTATION)$(CK_TEST_STORE_CRASH)$(CK_TEST_CONTINUITY)$(CK_TEST_FWCFG_ARTIFACTS)$(CK_TEST_SMP_SKIP_CPU)$(CK_TEST_ROLLBACK)$(CK_TEST_STALE_HANDOFF)$(CK_TEST_DISK_XLATE_BYPASS)),) +$(error CK_SERVICE cannot be combined with a CK_TEST_* flag) +endif +endif +ifneq ($(strip $(CK_SERVICE_TEST_KEYS)),) +ifneq ($(strip $(CK_SERVICE_TEST_KEYS)),1) +$(error CK_SERVICE_TEST_KEYS must be empty or 1) +endif +ifneq ($(strip $(CK_SERVICE)),1) +$(error CK_SERVICE_TEST_KEYS needs CK_SERVICE=1) +endif +ifneq ($(strip $(CK_HARDWARE_STAGING)),) +$(error CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING) +endif +endif + # TEST-ONLY Store crash hook (svc/store_crash.h; only # scripts/qemu_ck_store_crash_test.sh sets it): the Store stage reads a crash # plan from the scratch unit, runs on a volatile write cache and halts at the @@ -606,6 +643,17 @@ STAGE_SRCS += $(KDIR)/svc/continuity_codec.c $(KDIR)/svc/continuity_resolve.c \ $(KDIR)/svc/continuity_resolve_sealed.c $(KDIR)/svc/continuity_commit.c $(KDIR)/svc/continuity_recovery.c \ $(KDIR)/svc/continuity_boot.c $(KDIR)/svc/continuity_subject.c $(KDIR)/svc/continuity_subject_provision.c endif +ifeq ($(strip $(CK_SERVICE)),1) +# Own OUT subdirectory: service objects never mix with a diagnostic image. +ifeq ($(strip $(CK_SERVICE_TEST_KEYS)),1) +FULL_NAME := full-service-testkeys +STAGE_DMA_FLAGS += -DCK_SERVICE=1 -DCK_SERVICE_TEST_KEYS=1 +else +FULL_NAME := full-service$(if $(strip $(CK_HARDWARE_STAGING)),-hardware-staging,) +STAGE_DMA_FLAGS += -DCK_SERVICE=1 +endif +STAGE_SRCS += $(KDIR)/core/lifecycle.c $(KDIR)/core/lifecycle_fsm.c +endif ifeq ($(strip $(CK_TEST_DISK_XLATE_BYPASS)),1) FULL_NAME := $(FULL_NAME)-test-xlate-bypass STAGE_DMA_FLAGS += -DCK_TEST_DISK_XLATE_BYPASS=1 @@ -718,6 +766,22 @@ define console_check fi endef +# service_check(elf): only a CK_SERVICE image may carry the resident lifecycle; +# only a CK_SERVICE_TEST_KEYS image its TEST-ONLY banner. +define service_check + @if [ "$(strip $(CK_SERVICE))" = 1 ]; then \ + grep -aqF "lifecycle: ready" $(1) || { echo "service image lacks the resident lifecycle"; rm -f $(1); exit 1; }; \ + else \ + if grep -aqF "lifecycle: ready" $(1); then echo "diagnostic image carries the resident lifecycle"; rm -f $(1); exit 1; fi; \ + echo "no resident lifecycle in $(1)"; \ + fi + @if [ "$(strip $(CK_SERVICE_TEST_KEYS))" = 1 ]; then \ + grep -aqF "TEST-ONLY service image" $(1) || { echo "service test-keys image lacks its TEST-ONLY banner"; rm -f $(1); exit 1; }; \ + else \ + if grep -aqF "TEST-ONLY service image" $(1); then echo "image carries TEST Store keys without asking"; rm -f $(1); exit 1; fi; \ + fi +endef + # xhci_mut_check(elf): only a CK_TEST_XHCI_MUTATION image may carry the # TEST-ONLY xHCI mutation banner (and it must carry it). define xhci_mut_check @@ -850,6 +914,7 @@ $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld $(call owner_check,$@) $(call xhci_mut_check,$@) $(call console_check,$@) + $(call service_check,$@) $(call smp_check,$@) $(call crash_check,$@) $(call b7b_check,$@) @@ -864,10 +929,10 @@ $(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 $(KDIR)/core/acpi_dev.c + $(KDIR)/core/handoff_check.c $(KDIR)/core/fbcon.c $(KDIR)/core/acpi_dev.c $(KDIR)/core/lifecycle_fsm.c TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_osc_unit test_osc_launch test_osc_admit test_entropy test_handoff test_fbcon test_osh_op \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ - test_continuity_subject test_continuity_subject_provision + test_continuity_subject test_continuity_subject_provision test_lifecycle HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer diff --git a/native/kernel/core/.crumb b/native/kernel/core/.crumb index 30980479..73c4476c 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-11T01:27:19Z", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:85b29782329a9f640ff976034d8bda060b51ff8288e2547151aa6f30639f2bb1", + "digest": "sha256:6c183da30a2362e1e5c86b485cc6f6be46cfe9a9e30960f8f395e47659b18d42", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 39, - "generated_at_commit": "3f791539d41d852b2b6fd36fdebaebb9f76dc2a0", - "source_tree": "sha256:411de5db646b8379ee626b5b41660ceb1688f7c2262d33f3fa6568cb5aad4d9b" + "files": 42, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:ba44efeb611bb205e9e1954fecd0ee7c58cbe4cbc568b80337883289d3f8ca1a" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index d00a23e0..079f87a2 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -259,6 +259,17 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) ck_printf("stage console: %s rc=%d\n", crc == 0 ? "ok" : "FAIL", crc); } + /* Resident production lifecycle (core/lifecycle.c; only a CK_SERVICE=1 + * image links it, weak and absent otherwise). The product boundary: the + * service serves bounded operator requests here until its stop order, + * then falls through to the final report and the reset below. */ + if (ck_lifecycle_run) { + ck_set_stage("lifecycle"); + int lrc = ck_lifecycle_run(); + ck_irq_cpu_enable(0); + ck_printf("stage lifecycle: %s rc=%d\n", lrc == 0 ? "ok" : "FAIL", lrc); + } + ck_set_stage("final"); ck_puts("\n"); ck_report_header("final"); diff --git a/native/kernel/core/lifecycle.c b/native/kernel/core/lifecycle.c new file mode 100644 index 00000000..ec1b466b --- /dev/null +++ b/native/kernel/core/lifecycle.c @@ -0,0 +1,221 @@ +/* lifecycle.c -- resident production lifecycle (native v1 plan P03). + * + * Linked only into a CK_SERVICE=1 image (Makefile: full-service / + * full-service-testkeys). kmain calls ck_lifecycle_run after every boot + * stage instead of resetting: the product boundary where the diagnostic + * image ends in ck_reset(). The diagnostic images are untouched (they do not + * link this file; the weak ck_lifecycle_run is absent there). + * + * Operator input: the polled PL011 serial receive the console already owns + * (core/console.c), one bounded line editor (CK_LC_LINE_MAX bytes, overlong + * lines refused whole). The USB keyboard binding stays with the xHCI session + * code and is a later cut. Verbs and transitions: core/lifecycle_fsm.c. + * + * Store: the sealed Store opened by the store stage stays open and the NVMe + * stays bound for the service lifetime (svc/store_boot.c, CK_SERVICE). An + * approved task is committed as one Store object (kind CK_LC_OBJ_KIND). + * Stop order on "shutdown": (1) close the Store (keys erased; every commit + * is already durable: ss_transact returns after its final flush), (2) NVMe + * normal shutdown, bus master off, SMMU stream to abort (ck_dev_nvme_release), + * (3) return to kmain: final report, ck_reset (quiesce hook, PSCI). Never + * the other way round. + * + * LOCKED is the production truth today: ck_store_production_keys refuses + * (no in-kernel K_vol unwrap until P04). A full-service image therefore + * stays locked; only the TEST-ONLY full-service-testkeys image (refused + * with CK_HARDWARE_STAGING) opens the Store with TEST keys so the READY and + * SERVE paths can run in QEMU. */ +#include +#include +#include "ck.h" +#include "lifecycle_fsm.h" +#include "store_boot.h" +#include "store_sealed.h" + +#if !defined(CK_SERVICE) +#error "lifecycle.c is only part of a CK_SERVICE=1 image" +#endif +#if defined(CK_CONSOLE_SESSION) || defined(CK_TEST_STORE_CRASH) || defined(CK_TEST_CONTINUITY) || \ + defined(CK_QEMU_UNSAFE_DMA) || defined(CK_TEST_FWCFG_ARTIFACTS) +#error "CK_SERVICE cannot be combined with a TEST-only image flag" +#endif +#if defined(CK_SERVICE_TEST_KEYS) && defined(CK_HARDWARE_STAGING) +#error "CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING" +#endif + +#define CK_LC_OBJ_KIND 0x4e31u /* 'N1': v1 approved note, version 1 */ + +void ck_dev_nvme_release(void); +int ck_store_service_ready(void); +int ck_store_service_close(void); +int ck_store_service_unlock(void); + +static ck_lc g_lc; +static uint8_t g_last_id[32]; +static uint64_t g_last_gen; +static unsigned g_overlong, g_dropped; + +static void help(void) +{ + ck_puts("lifecycle: verbs: status unlock task approve deny cancel evidence help shutdown\n"); +} + +static void status(void) +{ + ck_printf("lifecycle: state=%s store=%s pending=%s requests=%u\n", ck_lc_state_name(g_lc.state), + ck_store_service_ready() ? "open" : "locked", g_lc.task_len ? g_lc.task : "-", g_lc.requests); +} + +static void evidence(void) +{ + ck_printf("lifecycle: evidence requests=%u refused=%u invalid=%u tasks=%u approved=%u denied=%u cancelled=%u " + "overlong=%u last_commit_gen=%llu last_id=%02x%02x%02x%02x\n", + g_lc.requests, g_lc.refused, g_lc.invalid, g_lc.tasks, g_lc.approved, g_lc.denied, g_lc.cancelled, + g_overlong, (unsigned long long)g_last_gen, g_last_id[0], g_last_id[1], g_last_id[2], g_last_id[3]); +} + +/* Commit the pending task as one sealed Store object. Bytes: a fixed header + * then the operator's text, bounded by CK_LC_TASK_MAX. */ +static int commit_task(void) +{ + static const char hdr[] = "aienos.note.v1\n"; + uint8_t buf[sizeof hdr - 1 + CK_LC_TASK_MAX]; + size_t n = 0, i; + ss_store *s = store_boot_store(); + if (!s || !ck_store_service_ready()) return -1; + for (i = 0; i < sizeof hdr - 1; i++) buf[n++] = (uint8_t)hdr[i]; + for (i = 0; i < g_lc.task_len; i++) buf[n++] = (uint8_t)g_lc.task[i]; + ss_object o = { CK_LC_OBJ_KIND, 1, buf, n }; + uint8_t ids[1][32]; + int rc = ss_transact(s, &o, 1, 0, 0, ids); + if (rc == 0) { + for (i = 0; i < 32; i++) g_last_id[i] = ids[0][i]; + g_last_gen = ss_generation(s); + } + return rc; +} + +static void handle_line(const char *line, size_t len) +{ + const char *arg; + size_t arg_len; + enum ck_lc_verb v = ck_lc_decode(line, len, &arg, &arg_len); + enum ck_lc_result r = ck_lc_apply(&g_lc, v, arg, arg_len); + switch (r) { + case CK_LC_R_INVALID: + ck_printf("lifecycle: REFUSED verb=%s reason=\"%s\"\n", ck_lc_verb_name(v), g_lc.reason); + return; + case CK_LC_R_REFUSED: + ck_printf("lifecycle: REFUSED verb=%s state=%s reason=\"%s\"\n", ck_lc_verb_name(v), + ck_lc_state_name(g_lc.state), g_lc.reason); + return; + case CK_LC_R_UNLOCK: { + int opened = ck_store_service_unlock(); + ck_lc_unlock_done(&g_lc, opened); + if (opened) ck_printf("lifecycle: unlock ok state=%s\n", ck_lc_state_name(g_lc.state)); + else ck_printf("lifecycle: REFUSED verb=unlock state=%s reason=\"%s\"\n", ck_lc_state_name(g_lc.state), g_lc.reason); + return; + } + case CK_LC_R_COMMIT: { + int rc = commit_task(); + ck_lc_commit_done(&g_lc, rc); + if (rc == 0) + ck_printf("lifecycle: approve committed gen=%llu id=%02x%02x%02x%02x state=%s\n", + (unsigned long long)g_last_gen, g_last_id[0], g_last_id[1], g_last_id[2], g_last_id[3], + ck_lc_state_name(g_lc.state)); + else + ck_printf("lifecycle: REFUSED verb=approve rc=%d state=%s reason=\"%s\"\n", rc, + ck_lc_state_name(g_lc.state), g_lc.reason); + return; + } + case CK_LC_R_STOP: + ck_printf("lifecycle: shutdown accepted state=%s\n", ck_lc_state_name(g_lc.state)); + return; + case CK_LC_R_OK: break; + } + switch (v) { + case CK_LC_V_HELP: help(); break; + case CK_LC_V_STATUS: status(); break; + case CK_LC_V_EVIDENCE: evidence(); break; + case CK_LC_V_TASK: + ck_printf("lifecycle: task pending state=%s proposed=\"note: %s\" (approve | deny | cancel)\n", + ck_lc_state_name(g_lc.state), g_lc.task); + break; + case CK_LC_V_DENY: ck_printf("lifecycle: denied state=%s\n", ck_lc_state_name(g_lc.state)); break; + case CK_LC_V_CANCEL: ck_printf("lifecycle: cancelled state=%s\n", ck_lc_state_name(g_lc.state)); break; + default: break; + } +} + +/* Bounded serial line editor: printable bytes collected, Backspace edits, + * CR or LF ends the line; a line past CK_LC_LINE_MAX is refused whole (the + * rest of it is dropped until the next Enter). Returns 1 when the line is complete. */ +static int rx_line(char *buf, size_t *len, int *overflow) +{ + for (;;) { + int b = ck_console_rx_poll(); + if (b == -1) return 0; + if (b == -2) continue; /* error byte: counted by the console, dropped here */ + char c = (char)b; + if (c == '\r' || c == '\n') { + if (*overflow) { + g_overlong++; + ck_puts("\n"); + ck_printf("lifecycle: REFUSED verb=invalid reason=\"line longer than %u bytes\"\n", (unsigned)CK_LC_LINE_MAX); + *overflow = 0; + *len = 0; + return 0; + } + ck_puts("\n"); + return 1; + } + if (c == 0x7f || c == 0x08) { + if (*len && !*overflow) { (*len)--; ck_puts("\b \b"); } + continue; + } + if (c < 0x20 || c > 0x7e) { g_dropped++; continue; } + if (*overflow) continue; + if (*len >= CK_LC_LINE_MAX) { *overflow = 1; continue; } + buf[(*len)++] = c; + { char e[2] = { c, 0 }; ck_puts(e); } + } +} + +/* Returns 0 after an orderly stop (kmain then prints the final report and + * resets). Never returns while serving. */ +int ck_lifecycle_run(void) +{ + char line[CK_LC_LINE_MAX + 1]; + size_t len = 0; + int overflow = 0; + uint32_t cr; + ck_lc_init(&g_lc); +#ifdef CK_SERVICE_TEST_KEYS + ck_puts("lifecycle: TEST-ONLY service image (CK_SERVICE_TEST_KEYS=1: TEST Store keys; QEMU only, never a production boot)\n"); +#endif + ck_lc_start(&g_lc, ck_store_service_ready()); + if (!ck_console_rx_ready(&cr)) { + ck_puts("lifecycle: REFUSED no operator input (console UART has no PL011 receiver): stop order\n"); + ck_lc_apply(&g_lc, CK_LC_V_SHUTDOWN, "", 0); + } else { + ck_printf("lifecycle: ready state=%s store=%s (operator input: serial)\n", ck_lc_state_name(g_lc.state), + ck_store_service_ready() ? "open" : "locked"); + ck_puts("> "); + while (g_lc.state != CK_LC_QUIESCE) { + if (rx_line(line, &len, &overflow)) { + handle_line(line, len); + len = 0; + if (g_lc.state != CK_LC_QUIESCE) ck_puts("> "); + } else { + __asm__ volatile("yield"); + } + } + } + /* Stop order: Store closed, then NVMe released, then the caller resets. */ + int closed = ck_store_service_close(); + ck_dev_nvme_release(); + ck_lc_stopped(&g_lc); + ck_printf("lifecycle: stop order store=%s nvme=released state=%s requests=%u\n", closed ? "closed" : "was-locked", + ck_lc_state_name(g_lc.state), g_lc.requests); + return 0; +} diff --git a/native/kernel/core/lifecycle_fsm.c b/native/kernel/core/lifecycle_fsm.c new file mode 100644 index 00000000..9d0a9394 --- /dev/null +++ b/native/kernel/core/lifecycle_fsm.c @@ -0,0 +1,195 @@ +/* lifecycle_fsm.c -- see lifecycle_fsm.h. No kernel calls, no libc. */ +#include "lifecycle_fsm.h" + +static int printable(char c) { return c >= 0x20 && c <= 0x7e; } + +static int word_is(const char *w, size_t n, const char *lit) +{ + size_t i; + for (i = 0; lit[i]; i++) + if (i >= n || w[i] != lit[i]) return 0; + return i == n; +} + +enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, size_t *arg_len) +{ + size_t i = 0, ws, we; + *arg = line; + *arg_len = 0; + if (len > CK_LC_LINE_MAX) return CK_LC_V_INVALID; + for (i = 0; i < len; i++) + if (!printable(line[i])) return CK_LC_V_INVALID; + i = 0; + while (i < len && line[i] == ' ') i++; + if (i == len) return CK_LC_V_EMPTY; + ws = i; + while (i < len && line[i] != ' ') i++; + we = i; + while (i < len && line[i] == ' ') i++; + /* trailing blanks are not part of the argument */ + { + size_t end = len; + while (end > i && line[end - 1] == ' ') end--; + *arg = line + i; + *arg_len = end - i; + } + const char *w = line + ws; + size_t n = we - ws; + if (word_is(w, n, "task")) { + if (*arg_len == 0 || *arg_len > CK_LC_TASK_MAX) return CK_LC_V_INVALID; + return CK_LC_V_TASK; + } + if (*arg_len != 0) return CK_LC_V_INVALID; /* every other verb takes no argument */ + if (word_is(w, n, "help")) return CK_LC_V_HELP; + if (word_is(w, n, "status")) return CK_LC_V_STATUS; + if (word_is(w, n, "unlock")) return CK_LC_V_UNLOCK; + if (word_is(w, n, "approve")) return CK_LC_V_APPROVE; + if (word_is(w, n, "deny")) return CK_LC_V_DENY; + if (word_is(w, n, "cancel")) return CK_LC_V_CANCEL; + if (word_is(w, n, "evidence")) return CK_LC_V_EVIDENCE; + if (word_is(w, n, "shutdown")) return CK_LC_V_SHUTDOWN; + return CK_LC_V_UNKNOWN; +} + +const char *ck_lc_verb_name(enum ck_lc_verb v) +{ + switch (v) { + case CK_LC_V_INVALID: return "invalid"; + case CK_LC_V_UNKNOWN: return "unknown"; + case CK_LC_V_EMPTY: return "empty"; + case CK_LC_V_HELP: return "help"; + case CK_LC_V_STATUS: return "status"; + case CK_LC_V_UNLOCK: return "unlock"; + case CK_LC_V_TASK: return "task"; + case CK_LC_V_APPROVE: return "approve"; + case CK_LC_V_DENY: return "deny"; + case CK_LC_V_CANCEL: return "cancel"; + case CK_LC_V_EVIDENCE: return "evidence"; + case CK_LC_V_SHUTDOWN: return "shutdown"; + } + return "?"; +} + +const char *ck_lc_state_name(enum ck_lc_state s) +{ + switch (s) { + case CK_LC_INIT: return "init"; + case CK_LC_LOCKED: return "locked"; + case CK_LC_READY: return "ready"; + case CK_LC_SERVE: return "serve"; + case CK_LC_QUIESCE: return "quiesce"; + case CK_LC_STOP: return "stop"; + } + return "?"; +} + +void ck_lc_init(ck_lc *lc) +{ + size_t i; + unsigned char *p = (unsigned char *)lc; + for (i = 0; i < sizeof *lc; i++) p[i] = 0; + lc->state = CK_LC_INIT; + lc->reason = ""; +} + +void ck_lc_start(ck_lc *lc, int store_open) +{ + lc->state = store_open ? CK_LC_READY : CK_LC_LOCKED; +} + +static enum ck_lc_result refuse(ck_lc *lc, const char *why) +{ + lc->refused++; + lc->reason = why; + return CK_LC_R_REFUSED; +} + +static void drop_task(ck_lc *lc) +{ + size_t i; + for (i = 0; i < sizeof lc->task; i++) lc->task[i] = 0; + lc->task_len = 0; +} + +enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, size_t arg_len) +{ + lc->requests++; + lc->reason = ""; + if (lc->state == CK_LC_QUIESCE || lc->state == CK_LC_STOP) return refuse(lc, "stopping"); + if (lc->state == CK_LC_INIT) return refuse(lc, "not started"); + switch (v) { + case CK_LC_V_INVALID: + lc->invalid++; + lc->reason = "line refused (bounds or syntax)"; + return CK_LC_R_INVALID; + case CK_LC_V_UNKNOWN: return refuse(lc, "unknown verb"); + case CK_LC_V_EMPTY: + case CK_LC_V_HELP: + case CK_LC_V_STATUS: + case CK_LC_V_EVIDENCE: return CK_LC_R_OK; + case CK_LC_V_UNLOCK: + if (lc->state != CK_LC_LOCKED) return refuse(lc, "already unlocked"); + return CK_LC_R_UNLOCK; + case CK_LC_V_TASK: + if (lc->state == CK_LC_LOCKED) return refuse(lc, "locked: no production Store"); + if (lc->state == CK_LC_SERVE) return refuse(lc, "a task is pending: approve, deny or cancel it first"); + if (arg_len == 0 || arg_len > CK_LC_TASK_MAX) return refuse(lc, "task text out of bounds"); + { + size_t i; + for (i = 0; i < arg_len; i++) lc->task[i] = arg[i]; + lc->task[arg_len] = 0; + lc->task_len = arg_len; + } + lc->tasks++; + lc->state = CK_LC_SERVE; + return CK_LC_R_OK; + case CK_LC_V_APPROVE: + if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing pending"); + return CK_LC_R_COMMIT; + case CK_LC_V_DENY: + if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing pending"); + lc->denied++; + drop_task(lc); + lc->state = CK_LC_READY; + return CK_LC_R_OK; + case CK_LC_V_CANCEL: + if (lc->state != CK_LC_SERVE) return refuse(lc, "nothing to cancel"); + lc->cancelled++; + drop_task(lc); + lc->state = CK_LC_READY; + return CK_LC_R_OK; + case CK_LC_V_SHUTDOWN: + if (lc->state == CK_LC_SERVE) drop_task(lc); /* a pending, unapproved task is dropped, never committed */ + lc->state = CK_LC_QUIESCE; + return CK_LC_R_STOP; + } + return refuse(lc, "unknown verb"); +} + +void ck_lc_commit_done(ck_lc *lc, int rc) +{ + if (lc->state != CK_LC_SERVE) return; + if (rc == 0) { + lc->approved++; + drop_task(lc); + lc->state = CK_LC_READY; + } else { + lc->refused++; + lc->reason = "commit failed: task still pending (approve again or cancel)"; + } +} + +void ck_lc_unlock_done(ck_lc *lc, int opened) +{ + if (lc->state != CK_LC_LOCKED) return; + if (opened) lc->state = CK_LC_READY; + else { + lc->refused++; + lc->reason = "production key source refused: still locked"; + } +} + +void ck_lc_stopped(ck_lc *lc) +{ + lc->state = CK_LC_STOP; +} diff --git a/native/kernel/core/lifecycle_fsm.h b/native/kernel/core/lifecycle_fsm.h new file mode 100644 index 00000000..eb090fc2 --- /dev/null +++ b/native/kernel/core/lifecycle_fsm.h @@ -0,0 +1,81 @@ +/* lifecycle_fsm.h -- resident production lifecycle: states, verbs, transitions. + * + * Pure logic (no kernel calls, no libc): the host test tests/test_lifecycle.c + * compiles this file as is. core/lifecycle.c binds it to the serial console, + * the sealed Store and the device teardown order (CK_SERVICE=1 image only). + * + * States (native v1 plan P03: initialize, locked/ready, serve, quiesce, stop): + * INIT before the lifecycle took over from the boot stages + * LOCKED production Store not open (no production key source yet, or it + * refused): status/unlock/help/shutdown only; every task refused + * READY Store open: a task may be submitted + * SERVE one bounded task pending: approve commits it, deny or cancel drops it + * QUIESCE stop order running: flush, revoke, halt + * STOP nothing more is accepted + * Verbs are the v1 operator surface and nothing else: status, unlock, task + * , approve, deny, cancel, evidence, help, shutdown. */ +#ifndef AIENOS_CK_LIFECYCLE_FSM_H +#define AIENOS_CK_LIFECYCLE_FSM_H + +#include +#include + +enum ck_lc_state { CK_LC_INIT = 0, CK_LC_LOCKED, CK_LC_READY, CK_LC_SERVE, CK_LC_QUIESCE, CK_LC_STOP }; + +enum ck_lc_verb { + CK_LC_V_INVALID = -1, /* overlong, unprintable or malformed: refused whole */ + CK_LC_V_UNKNOWN = 0, + CK_LC_V_EMPTY, + CK_LC_V_HELP, + CK_LC_V_STATUS, + CK_LC_V_UNLOCK, + CK_LC_V_TASK, + CK_LC_V_APPROVE, + CK_LC_V_DENY, + CK_LC_V_CANCEL, + CK_LC_V_EVIDENCE, + CK_LC_V_SHUTDOWN, +}; + +/* Bounds: one request line, and the task text that a line may carry. */ +#define CK_LC_LINE_MAX 64 +#define CK_LC_TASK_MAX 48 + +/* Outcome of one request against the state machine. */ +enum ck_lc_result { + CK_LC_R_OK = 0, /* accepted, state may have changed */ + CK_LC_R_REFUSED, /* legal verb, refused in this state (reason set) */ + CK_LC_R_INVALID, /* the line itself was refused (bounds / syntax) */ + CK_LC_R_COMMIT, /* approve accepted: the binding must commit the task */ + CK_LC_R_UNLOCK, /* unlock accepted: the binding must try the key source */ + CK_LC_R_STOP, /* shutdown accepted: the binding must run the stop order */ +}; + +typedef struct { + enum ck_lc_state state; + char task[CK_LC_TASK_MAX + 1]; /* pending task text in SERVE, else "" */ + size_t task_len; + /* counters for the evidence verb (bounded, wrap is harmless) */ + uint32_t requests, refused, invalid, tasks, approved, denied, cancelled; + const char *reason; /* last refusal reason, static string */ +} ck_lc; + +/* Decode one request line (len bytes, no terminator required). arg and arg_len + * receive the text after "task" (trimmed), else an empty span. */ +enum ck_lc_verb ck_lc_decode(const char *line, size_t len, const char **arg, size_t *arg_len); +const char *ck_lc_verb_name(enum ck_lc_verb v); +const char *ck_lc_state_name(enum ck_lc_state s); + +void ck_lc_init(ck_lc *lc); +/* Boot stages are over: Store open -> READY, else LOCKED. */ +void ck_lc_start(ck_lc *lc, int store_open); +/* Apply one decoded request. Pure transition; the binding performs the side + * effect that the result names and then calls the *_done function. */ +enum ck_lc_result ck_lc_apply(ck_lc *lc, enum ck_lc_verb v, const char *arg, size_t arg_len); +/* Binding results: commit outcome (0 ok: SERVE -> READY; else stays SERVE + * with reason), unlock outcome (1 opened: LOCKED -> READY). */ +void ck_lc_commit_done(ck_lc *lc, int rc); +void ck_lc_unlock_done(ck_lc *lc, int opened); +void ck_lc_stopped(ck_lc *lc); + +#endif diff --git a/native/kernel/include/.crumb b/native/kernel/include/.crumb index 483da3df..054b5c88 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-10T04:29:33Z", + "compiled_at": "2026-10-12T03:57:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:2e52b5bd3addb2fff0ab5f761e1daf67befcc72291117a03534857fff229ae45", + "digest": "sha256:bad996a1149ddaa6a4404ac97b147414f8df3b376d9f403d9bca7b0ea72a4e9c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 1, - "generated_at_commit": "ed308de52ede1d4c8a211d88e0e4a3ac5c92ec23", - "source_tree": "sha256:2128bf32de038e79e9edcc00174b47170014c8ad879f910f70c5ba3e19a0ed48" + "generated_at_commit": "51c05daff49b0836692eb1c777ec2758cf0746ae", + "source_tree": "sha256:b3b1ead81ab787c8cbc4f733ff7cba42097995619da3380a717038889e1bfaec" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/include/ck.h b/native/kernel/include/ck.h index d86e60d1..610f9248 100644 --- a/native/kernel/include/ck.h +++ b/native/kernel/include/ck.h @@ -201,6 +201,11 @@ __attribute__((weak)) void ck_stage_quiesce(void); * CK_CONSOLE_SESSION=1): the operator console session. kmain calls it after every * other boot step and before the final report; absent in every default image. */ __attribute__((weak)) int ck_stage_console_session(void); +/* Resident production lifecycle (core/lifecycle.c, only in a CK_SERVICE=1 image): + * serves bounded operator requests after every boot stage until its stop order, + * then returns 0 so kmain prints the final report and resets. Absent (weak) in + * every diagnostic image. */ +__attribute__((weak)) int ck_lifecycle_run(void); /* PL011 serial receive (core/console.c), polled. ck_console_rx_ready: 1 when the * console UART is a PL011 whose UARTCR has UARTEN and RXE set (*cr gets UARTCR); diff --git a/native/kernel/svc/.crumb b/native/kernel/svc/.crumb index e94d8ca3..99637e42 100644 --- a/native/kernel/svc/.crumb +++ b/native/kernel/svc/.crumb @@ -24,20 +24,20 @@ }, { "digest": "b44c6286f48a561179cc97442ad6fa4c368b53f802bad1b35a493b6a7ed037cf", - "last_commit": "ee2ff16", + "last_commit": "98fbc3c", "name": "tests", "purpose": "Directory native/kernel/svc/tests. Purpose not yet described by a human." } ], "children_root": "sha256:663de76750aa7914e1994534b4284995ddc98a54574ed3d29e0368506e9a0041", - "compiled_at": "2026-10-09T02:37:36Z", + "compiled_at": "2026-10-12T03:57:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:2712bd906a89792008981a3dc88d1f28f5c4ebcf278751ab852bfd0e76a62e1c", + "digest": "sha256:97067439083188400b87693964d28f9a06fb8b6cf3ff1271c4016ecbe80e5133", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 35, - "generated_at_commit": "23b2249dd1bbac3314e17b5d70357e71e226cf03", - "source_tree": "sha256:495f8cee8153c2abee8e361c7739463c4e5e5df5a428df16884cd80f56c319c3" + "generated_at_commit": "51c05daff49b0836692eb1c777ec2758cf0746ae", + "source_tree": "sha256:018b5ce1814892e07f893a5834f48f5967b793f81619f536cfbcad95c11cc074" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index fac490ae..08e151ec 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -151,6 +151,10 @@ static const char *classify(int rc) /* Workspace lives across boots of the host test; the kernel uses it once. */ static ss_workspace *g_ws; static ss_store *g_ss; +#ifdef CK_SERVICE +static int g_service_open; /* Store open for the service lifetime (core/lifecycle.c) */ +static int g_service_locked_rc; /* last production key source refusal */ +#endif int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, ck_store_report *r) @@ -360,18 +364,76 @@ int ck_stage_store(void) } } #endif +#if defined(CK_SERVICE) && !defined(CK_SERVICE_TEST_KEYS) + /* Production service image without owner provisioning: the production + * key source only (it refuses today: no in-kernel K_vol unwrap, P04). No + * TEST key is ever used here. */ + rc = ck_store_production_keys(&keys); + bz(&keys, sizeof keys); + if (rc == 0) rc = CK_SB_E_BLOCKED_OPERATOR; /* no Store UUID without CK_HARDWARE_STAGING provisioning */ + ck_printf("store: REFUSED proof=keyed step=\"production Store key source\" rc=%d (service image stays LOCKED: " + "K_vol needs the TRUST-1 key ceremony, Gate 6 TPM keyslot or the operator recovery keyslot); " + "disk not read, not formatted\n", rc); + ck_art_stage_load(0, "production Store key source BLOCKED_OPERATOR"); + ck_dev_nvme_release(); + g_service_locked_rc = rc; + (void)commit; + return rc; +#else ck_store_test_keys(&keys); rc = store_boot_run(d, &keys, ck_store_test_uuid, commit ? commit : "unknown", &r); +#endif #endif bz(&keys, sizeof keys); store_boot_print(&r); /* Signed artifact candidates live in this Store: read them while the disk * is still bound (the loader verifies them later, as untrusted bytes). */ ck_art_stage_load(r.verdict == CK_SB_COMMITTED ? g_ss : 0, r.step); +#ifdef CK_SERVICE + /* Service lifetime (core/lifecycle.c): an open Store keeps the NVMe bound + * until the lifecycle stop order releases it, in that order. A refused + * Store releases the disk now, exactly as the diagnostic image does. */ + if (r.verdict == CK_SB_COMMITTED) { + g_service_open = 1; + ck_puts("store: service lifetime: Store open, NVMe stays bound until the lifecycle stop order\n"); + return rc; + } +#endif ck_dev_nvme_release(); /* last disk user: revoke NVMe bus mastering */ return rc; } +#ifdef CK_SERVICE +int ck_store_service_ready(void) +{ + return g_service_open && g_ss; +} + +/* Stop order step 1: erase the Store keys. Every commit is already durable + * (ss_transact returns after its final flush), so nothing is pending. */ +int ck_store_service_close(void) +{ + if (!g_service_open) return 0; + g_service_open = 0; + if (g_ss) bz(&g_ss->keys, sizeof g_ss->keys); + return 1; +} + +/* "unlock" while LOCKED: ask the production key source again. Today it + * refuses (CK_SB_E_BLOCKED_OPERATOR); the Store is never opened with any + * other key here. Returns 1 only when the Store is open. */ +int ck_store_service_unlock(void) +{ + if (g_service_open) return 1; + ss_keys keys; + int rc = ck_store_production_keys(&keys); + bz(&keys, sizeof keys); + ck_printf("store: unlock REFUSED proof=keyed step=\"production Store key source\" rc=%d (%s)\n", rc ? rc : g_service_locked_rc, + "BlockedOperator: no in-kernel K_vol unwrap yet"); + return 0; +} +#endif + ss_store *store_boot_store(void) { return g_ss; diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index 78c97e86..a54efeda 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-10T04:29:33Z", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:7d63e0731c50350099774d048adcb20faa53e61648fb164ead40e7f7b3f3cd92", + "digest": "sha256:c1ffd88afd48fbef6e14bcec2a9c1318e372ab31c01481f3fb60a8ed0b04a6e1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 149, - "generated_at_commit": "ed308de52ede1d4c8a211d88e0e4a3ac5c92ec23", - "source_tree": "sha256:013cd0c0c95a0421fa4c01157ab0901ff0b64450aa40d22b5ae8a11b16d0a5c0" + "files": 150, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:f6cbc8239856f043ddb0f3e67cc681c10c453efe9ebf9df3f4ddc7f8ee9703e7" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/test_lifecycle.c b/native/kernel/tests/test_lifecycle.c new file mode 100644 index 00000000..710c761d --- /dev/null +++ b/native/kernel/tests/test_lifecycle.c @@ -0,0 +1,85 @@ +/* test_lifecycle.c -- the resident lifecycle state machine and request + * decoder (core/lifecycle_fsm.c) on the host: bounds, verbs, every state + * transition, refusals in the wrong state, the stop order flag. */ +#include +#include "ck_test.h" +#include "lifecycle_fsm.h" + +static enum ck_lc_verb dec(const char *s, const char **a, size_t *al) { return ck_lc_decode(s, strlen(s), a, al); } +static enum ck_lc_result req(ck_lc *lc, const char *s) +{ + const char *a; size_t al; + enum ck_lc_verb v = dec(s, &a, &al); + return ck_lc_apply(lc, v, a, al); +} + +int main(void) +{ + const char *a; size_t al; + /* decoder */ + CHECK(dec("status", &a, &al) == CK_LC_V_STATUS && al == 0); + CHECK(dec(" help ", &a, &al) == CK_LC_V_HELP); + CHECK(dec("", &a, &al) == CK_LC_V_EMPTY && dec(" ", &a, &al) == CK_LC_V_EMPTY); + CHECK(dec("task write the note", &a, &al) == CK_LC_V_TASK && al == 14 && memcmp(a, "write the note", 14) == 0); + CHECK(dec("task padded ", &a, &al) == CK_LC_V_TASK && al == 6 && memcmp(a, "padded", 6) == 0); + CHECK(dec("task", &a, &al) == CK_LC_V_INVALID); /* task needs text */ + CHECK(dec("status now", &a, &al) == CK_LC_V_INVALID); /* no argument allowed */ + CHECK(dec("frobnicate", &a, &al) == CK_LC_V_UNKNOWN); + CHECK(dec("sta\x01tus", &a, &al) == CK_LC_V_INVALID); /* unprintable */ + { + char big[CK_LC_LINE_MAX + 2]; + memset(big, 'x', sizeof big - 1); big[sizeof big - 1] = 0; + CHECK(dec(big, &a, &al) == CK_LC_V_INVALID); /* 65 bytes refused whole */ + char t[CK_LC_LINE_MAX + 1]; + memset(t, 'y', CK_LC_LINE_MAX); t[CK_LC_LINE_MAX] = 0; + memcpy(t, "task ", 5); + CHECK(dec(t, &a, &al) == CK_LC_V_INVALID && al == CK_LC_LINE_MAX - 5); /* text 59 > 48 */ + } + CHECK(strcmp(ck_lc_verb_name(CK_LC_V_SHUTDOWN), "shutdown") == 0 && strcmp(ck_lc_state_name(CK_LC_SERVE), "serve") == 0); + + /* locked: nothing but status/help/evidence/unlock/shutdown */ + ck_lc lc; + ck_lc_init(&lc); + CHECK(lc.state == CK_LC_INIT && req(&lc, "status") == CK_LC_R_REFUSED); + ck_lc_start(&lc, 0); + CHECK(lc.state == CK_LC_LOCKED); + CHECK(req(&lc, "status") == CK_LC_R_OK && req(&lc, "help") == CK_LC_R_OK && req(&lc, "evidence") == CK_LC_R_OK); + CHECK(req(&lc, "task hello") == CK_LC_R_REFUSED && lc.state == CK_LC_LOCKED && lc.tasks == 0); + CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "deny") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); + CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); + ck_lc_unlock_done(&lc, 0); + CHECK(lc.state == CK_LC_LOCKED && strstr(lc.reason, "still locked")); + CHECK(req(&lc, "unlock") == CK_LC_R_UNLOCK); + ck_lc_unlock_done(&lc, 1); + CHECK(lc.state == CK_LC_READY); + CHECK(req(&lc, "unlock") == CK_LC_R_REFUSED); /* already unlocked */ + CHECK(req(&lc, "shutdown") == CK_LC_R_STOP && lc.state == CK_LC_QUIESCE); + CHECK(req(&lc, "status") == CK_LC_R_REFUSED && strcmp(lc.reason, "stopping") == 0); + ck_lc_stopped(&lc); + CHECK(lc.state == CK_LC_STOP && req(&lc, "task x") == CK_LC_R_REFUSED); + + /* ready / serve: one pending task, approve commits, deny and cancel drop */ + ck_lc_init(&lc); + ck_lc_start(&lc, 1); + CHECK(lc.state == CK_LC_READY); + CHECK(req(&lc, "approve") == CK_LC_R_REFUSED && req(&lc, "cancel") == CK_LC_R_REFUSED); + CHECK(req(&lc, "task draft the plan") == CK_LC_R_OK && lc.state == CK_LC_SERVE && lc.tasks == 1); + CHECK(strcmp(lc.task, "draft the plan") == 0 && lc.task_len == 14); + CHECK(req(&lc, "task another") == CK_LC_R_REFUSED && strcmp(lc.task, "draft the plan") == 0); /* one at a time */ + CHECK(req(&lc, "approve") == CK_LC_R_COMMIT && lc.state == CK_LC_SERVE); + ck_lc_commit_done(&lc, -5); /* commit failed: still pending */ + CHECK(lc.state == CK_LC_SERVE && lc.approved == 0 && strcmp(lc.task, "draft the plan") == 0); + CHECK(req(&lc, "approve") == CK_LC_R_COMMIT); + ck_lc_commit_done(&lc, 0); + CHECK(lc.state == CK_LC_READY && lc.approved == 1 && lc.task_len == 0 && lc.task[0] == 0); + CHECK(req(&lc, "task b") == CK_LC_R_OK && req(&lc, "deny") == CK_LC_R_OK && lc.state == CK_LC_READY && lc.denied == 1); + CHECK(req(&lc, "task c") == CK_LC_R_OK && req(&lc, "cancel") == CK_LC_R_OK && lc.state == CK_LC_READY && lc.cancelled == 1); + CHECK(req(&lc, "nonsense") == CK_LC_R_REFUSED && req(&lc, "task") == CK_LC_R_INVALID && lc.invalid == 1); + /* a pending unapproved task is dropped by shutdown, never committed */ + CHECK(req(&lc, "task d") == CK_LC_R_OK && req(&lc, "shutdown") == CK_LC_R_STOP); + CHECK(lc.state == CK_LC_QUIESCE && lc.task_len == 0 && lc.approved == 1); + ck_lc_commit_done(&lc, 0); /* ignored outside SERVE */ + CHECK(lc.approved == 1); + CHECK(lc.requests == 14); + return ck_t_verdict("CK_LIFECYCLE"); +} diff --git a/scripts/.crumb b/scripts/.crumb index cc1fa7c4..d711f3a3 100644 --- a/scripts/.crumb +++ b/scripts/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-11T21:20:24Z", + "compiled_at": "2026-10-12T04:00:08Z", "compiler": "crumb-compile/1", - "digest": "sha256:9cb7696ff81e7d3e7cc312f7249aba2f0a7e7345b9ba7b80a3ab4515bbad10d9", + "digest": "sha256:9fac5e2c6ace7bf64f1773b6da11b091ec82d394f285baad457758435df36f98", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 80, - "generated_at_commit": "362cba9653c8021996a21b17ead00d5d1140b371", - "source_tree": "sha256:65b8e19c864e095499af61a79ad42f28fe71ef034c647bfb1fee138785c1e1f6" + "files": 81, + "generated_at_commit": "8daa91dcd1e546cae2e8d39fada6d59b759804c3", + "source_tree": "sha256:0736d952a514f1142055a7057ead3db0bc952bdcc1ba333bb7935a7c2008be34" }, "owner_lane": "", "provenance": { diff --git a/scripts/qemu_ck_lifecycle_test.sh b/scripts/qemu_ck_lifecycle_test.sh new file mode 100755 index 00000000..259d5fef --- /dev/null +++ b/scripts/qemu_ck_lifecycle_test.sh @@ -0,0 +1,216 @@ +#!/usr/bin/env bash +# LIFECYCLE gate for the AIENOS C kernel (native v1 plan P03): rows 160-167 of +# native/kernel/GATES.md "LIFECYCLE resident production lifecycle". C-only. +# QEMU is not hardware: a PASS here qualifies nothing physical. +# +# Boots two CK_SERVICE images in QEMU AArch64 (AAVMF, NVMe disk, iommu=smmuv3), +# operator input over the PL011 serial from OUTSIDE the guest (pipe chardev): +# service make full CK_SERVICE=1: the production entry. The production key +# source refuses (no in-kernel K_vol unwrap, P04), so the lifecycle +# must come up LOCKED, refuse every task and unlock, still answer +# status/help/evidence, and shut down in order. +# testkeys make full CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 (TEST-ONLY): the +# Store opens with TEST keys so READY/SERVE run: a task is proposed, +# approve commits it to the Store, deny and cancel drop tasks, a +# second task while one is pending is refused, an overlong line is +# refused whole, evidence counts agree, shutdown closes the Store +# before the NVMe release before the final report. +# +# row 160 service image: lifecycle starts after mm_usage, state=locked, +# store=locked; the Store stage refused with BlockedOperator and +# released the NVMe; no TEST key banner +# row 161 locked: task and unlock REFUSED (state stays locked), status/help/ +# evidence answered +# row 162 locked shutdown: stop order store=was-locked, then stage result, +# then final report; no panic/fault; QEMU exit 0; M1 checks +# row 163 testkeys image: announces TEST-ONLY banner, Store open, NVMe kept +# bound for the service lifetime (no dma_gate revoke before ready) +# row 164 serve: task -> pending with the proposed note; approve -> committed +# with a Store generation and object id; state back to ready +# row 165 deny and cancel drop pending tasks; a second task while pending is +# refused; approve with nothing pending is refused +# row 166 bounds: a 65-byte line is refused whole and never run; evidence +# shows tasks=3 approved=1 denied=1 cancelled=1 overlong=1 +# row 167 stop order: store=closed, then dma_gate nvme bus master revoked, +# then smmu stream back to abort, then stage lifecycle ok, then the +# final report; QEMU exit 0; M1 checks +# build the default (diagnostic) image carries no lifecycle; CK_SERVICE is +# refused with CK_CONSOLE_SESSION, CK_QEMU_UNSAFE_DMA and a CK_TEST_* +# flag; CK_SERVICE_TEST_KEYS is refused with CK_HARDWARE_STAGING +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" +final_tag="AIENOS_CK_LIFECYCLE" + +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" +command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${final_tag}: NOT_RUN (qemu-system-aarch64 missing)"; exit 2; } +[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${final_tag}: NOT_RUN (AAVMF missing)"; exit 2; } + +cross="" +if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" +out="${repo_root}/target/native-kernel" +mk() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@"; } +mk full >/dev/null +mk full CK_SERVICE=1 >/dev/null +mk full CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 >/dev/null +mk gpt-image >/dev/null +gpt_tool="${out}/host/ck_gpt_image" +default_efi="${out}/full/BOOTAA64.EFI" +service_efi="${out}/full-service/BOOTAA64.EFI" +testkeys_efi="${out}/full-service-testkeys/BOOTAA64.EFI" + +. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" +if ! gh_quiet_check; then echo "NOT_RUN ${gh_why}"; echo "${final_tag}: NOT_RUN (quiet flag held)"; exit 3; fi +if ! gh_lock_take qemu_ck_lifecycle_test "${AIENOS_GATE_MINUTES:-40}"; then echo "NOT_RUN ${gh_why}"; echo "${final_tag}: NOT_RUN (QEMU gate lock held)"; exit 3; fi +top="$(mktemp -d)" +qemu_pid="" +drain_pid="" +cleanup() { + [[ -z "${qemu_pid}" ]] || kill "${qemu_pid}" 2>/dev/null || true + [[ -z "${drain_pid}" ]] || kill "${drain_pid}" 2>/dev/null || true + exec 4>&- 2>/dev/null || true + rm -rf "${top}" + gh_lock_release +} +trap cleanup EXIT + +source "${repo_root}/scripts/lib_ck_m1_checks.sh" +boot_timeout="${AIENOS_QEMU_TIMEOUT:-600}" +fail=0 +ok() { echo "ROW $1 PASS $2"; } +bad() { echo "ROW $1 FAIL $2"; fail=1; } +has() { grep -qE -- "$1" "${work}/serial.txt"; } +rcheck() { if has "$3"; then ok "$1" "$2"; else bad "$1" "$2"; fi; } +rabsent() { if has "$3"; then bad "$1" "$2 (present)"; else ok "$1" "$2"; fi; } +line_of() { { grep -nE -m1 -- "$1" "${work}/serial.txt" || true; } | cut -d: -f1; } +before() { local a b; a="$(line_of "$3")"; b="$(line_of "$4")" + if [[ -n "${a}" && -n "${b}" && "${a}" -lt "${b}" ]]; then ok "$1" "$2 (lines ${a} < ${b})"; else bad "$1" "$2 (lines ${a:-none} ${b:-none})"; fi; } +serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -qE -- "$1"; } +wait_for() { local deadline=$(( $(date +%s) + $1 )); shift + while (( $(date +%s) < deadline )); do for p in "$@"; do serial_has "${p}" && return 0; done + kill -0 "${qemu_pid}" 2>/dev/null || break; sleep 1; done + for p in "$@"; do serial_has "${p}" && return 0; done; return 1; } +ser_line() { printf '%s\r' "$1" >&4; sleep 1.2; } + +boot() { # name efi + work="${top}/$1" + mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" + touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" + cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" + cp "${vars_fd}" "${work}/vars.fd" + # GPT boot disk (tools/ck_gpt_image.c, as the Store gate): protective MBR, + # primary + backup GPT, sentinels, AIENOS partition all zero (Store at genesis). + "${gpt_tool}" create "${work}/nvme.img" 512 64 aienos-middle >/dev/null + mkfifo "${work}/ser.in" "${work}/ser.out" + : >"${work}/serial.log" + timeout "${boot_timeout}" qemu-system-aarch64 \ + -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ + -drive if=pflash,format=raw,file="${work}/vars.fd" \ + -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ + -device virtio-blk-pci,drive=esp \ + -drive if=none,id=nvme0,format=raw,file="${work}/nvme.img" \ + -device nvme,drive=nvme0,serial=aienos-lifecycle-test \ + -device ramfb -display none -nic none -monitor none \ + -chardev pipe,id=ser,path="${work}/ser" -serial chardev:ser -no-reboot 2>"${work}/qemu.err" & + qemu_pid=$! + cat "${work}/ser.out" >>"${work}/serial.log" & + drain_pid=$! + exec 4>"${work}/ser.in" + if wait_for "${boot_timeout}" '^lifecycle: ready state=' '^report_kind: (panic|fault|final)' && serial_has '^lifecycle: ready state='; then + sleep 1 + "$3" + fi + wait_for 120 '^report_kind: (panic|fault|final)' || true + set +e; wait "${qemu_pid}"; qemu_status=$?; set -e + qemu_pid="" + exec 4>&- 2>/dev/null || true + wait "${drain_pid}" 2>/dev/null || true + drain_pid="" + tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" + [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_lifecycle_$1.log" + echo "== boot $1: qemu exit ${qemu_status}" +} + +drive_locked() { + ser_line status; ser_line "task write a note"; ser_line unlock; ser_line help; ser_line evidence; ser_line shutdown +} +drive_ready() { + ser_line status + ser_line "task draft the plan" + ser_line "task second while pending" + ser_line approve + ser_line approve + ser_line "task to be denied"; ser_line deny + ser_line "task to be cancelled"; ser_line cancel + ser_line "$(printf 'x%.0s' $(seq 65))" + ser_line evidence + ser_line shutdown +} + +m1() { # row: the shared M1 checks read ${work}/serial.txt and set failed + failed=0 + ck_m1_checks >"${work}/m1.txt" 2>&1 || true + if [[ "${failed}" == 0 ]]; then ok "$1" "M1 checks"; else bad "$1" "M1 checks: $(grep -E "^FAIL" "${work}/m1.txt" | head -3 | tr "\n" " ")"; fi +} + +# --- build-shape checks --- +if grep -aqF "lifecycle: ready" "${default_efi}"; then bad build "default image carries the resident lifecycle"; else ok build "default image carries no resident lifecycle"; fi +if grep -aqF "TEST-ONLY service image" "${service_efi}"; then bad build "production service image carries the TEST key banner"; else ok build "production service image carries no TEST key banner"; fi +for combo in "CK_CONSOLE_SESSION=1" "CK_QEMU_UNSAFE_DMA=1" "CK_TEST_STORE_CRASH=1"; do + set +e; msg="$(make -s -n -C native/kernel OUT="${top}/refuse" full CK_SERVICE=1 "${combo}" 2>&1)"; st=$?; set -e + if [[ "${st}" != 0 ]] && grep -qE "CK_SERVICE cannot be combined with" <<<"${msg}"; then ok build "refuses CK_SERVICE with ${combo}"; else bad build "did not refuse CK_SERVICE with ${combo} (status ${st})"; fi +done +set +e; msg="$(make -s -n -C native/kernel OUT="${top}/refuse" full CK_SERVICE=1 CK_SERVICE_TEST_KEYS=1 CK_HARDWARE_STAGING=1 2>&1)"; st=$?; set -e +if [[ "${st}" != 0 ]] && grep -qF "CK_SERVICE_TEST_KEYS (TEST keys) cannot be combined with CK_HARDWARE_STAGING" <<<"${msg}"; then ok build "refuses TEST keys with CK_HARDWARE_STAGING"; else bad build "did not refuse TEST keys with CK_HARDWARE_STAGING (status ${st})"; fi + +# --- boot 1: production service image, LOCKED --- +boot service "${service_efi}" drive_locked +before 160 "lifecycle after mm_usage" '^mm_usage: ' '^lifecycle: ready state=locked store=locked' +rcheck 160 "Store stage refused with the production key source (BlockedOperator), disk not read" '^store: REFUSED proof=keyed step="production Store key source" rc=-2002 \(service image stays LOCKED' +before 160 "NVMe released by the refused Store stage before the lifecycle" '^dma_gate: nvme bus master revoked' '^lifecycle: ready state=locked' +rabsent 160 "no TEST key banner" 'TEST-ONLY service image' +rcheck 161 "task refused while locked" '^lifecycle: REFUSED verb=task state=locked reason="locked: no production Store"' +rcheck 161 "unlock refused, still locked" '^lifecycle: REFUSED verb=unlock state=locked reason="production key source refused: still locked"' +rcheck 161 "unlock asked the production key source again" '^store: unlock REFUSED proof=keyed step="production Store key source" rc=-2002' +rcheck 161 "status answered" '^lifecycle: state=locked store=locked pending=- requests=' +rcheck 161 "help answered" '^lifecycle: verbs: status unlock task approve deny cancel evidence help shutdown$' +rcheck 161 "evidence answered" '^lifecycle: evidence requests=5 refused=2 invalid=0 tasks=0 approved=0' +rcheck 162 "stop order with no Store" '^lifecycle: stop order store=was-locked nvme=released state=stop requests=6$' +before 162 "stop order < stage result" '^lifecycle: stop order' '^stage lifecycle: ok rc=0$' +before 162 "stage result < final report" '^stage lifecycle: ok rc=0$' '^report_kind: final' +rabsent 162 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 162 "QEMU exit 0"; else bad 162 "QEMU exit ${qemu_status}"; fi +m1 162 + +# --- boot 2: TEST-ONLY test-keys image, READY/SERVE --- +boot testkeys "${testkeys_efi}" drive_ready +rcheck 163 "TEST-ONLY banner" '^lifecycle: TEST-ONLY service image \(CK_SERVICE_TEST_KEYS=1: TEST Store keys; QEMU only, never a production boot\)$' +rcheck 163 "Store open, NVMe kept bound for the service lifetime" '^store: service lifetime: Store open, NVMe stays bound until the lifecycle stop order$' +rcheck 163 "lifecycle ready" '^lifecycle: ready state=ready store=open \(operator input: serial\)$' +if [[ -n "$(line_of '^dma_gate: nvme bus master revoked')" && "$(line_of '^dma_gate: nvme bus master revoked')" -lt "$(line_of '^lifecycle: ready state=ready')" ]]; then bad 163 "NVMe revoked before the lifecycle (service lifetime broken)"; else ok 163 "no NVMe revoke before the lifecycle"; fi +rcheck 164 "task proposed" '^lifecycle: task pending state=serve proposed="note: draft the plan" \(approve \| deny \| cancel\)$' +rcheck 164 "approve committed to the Store" '^lifecycle: approve committed gen=[0-9]+ id=[0-9a-f]{8} state=ready$' +rcheck 165 "second task while pending refused" '^lifecycle: REFUSED verb=task state=serve reason="a task is pending: approve, deny or cancel it first"' +rcheck 165 "approve with nothing pending refused" '^lifecycle: REFUSED verb=approve state=ready reason="nothing pending"' +rcheck 165 "deny drops" '^lifecycle: denied state=ready$' +rcheck 165 "cancel drops" '^lifecycle: cancelled state=ready$' +rcheck 166 "overlong line refused whole" '^lifecycle: REFUSED verb=invalid reason="line longer than 64 bytes"$' +rcheck 166 "evidence counts" '^lifecycle: evidence requests=10 refused=2 invalid=0 tasks=3 approved=1 denied=1 cancelled=1 overlong=1 last_commit_gen=[0-9]+ last_id=[0-9a-f]{8}$' +rcheck 167 "stop order closed the Store" '^lifecycle: stop order store=closed nvme=released state=stop requests=11$' +before 167 "lifecycle ready < NVMe revoke (revoke only at the stop order)" '^lifecycle: ready state=ready' '^dma_gate: nvme bus master revoked' +before 167 "NVMe revoke < stop order line" '^dma_gate: nvme bus master revoked' '^lifecycle: stop order store=closed' +before 167 "stop order < stage result" '^lifecycle: stop order store=closed' '^stage lifecycle: ok rc=0$' +before 167 "stage result < final report" '^stage lifecycle: ok rc=0$' '^report_kind: final' +rabsent 167 "no panic or fault" '^report_kind: (panic|fault)' +if [[ "${qemu_status}" == 0 ]]; then ok 167 "QEMU exit 0"; else bad 167 "QEMU exit ${qemu_status}"; fi +m1 167 + +if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then + for b in service testkeys; do echo "---- ${b} serial (lifecycle lines) ----"; grep -E '^(lifecycle|store|stage lifecycle|dma_gate|smmu: nvme|report_kind|mm_usage)' "${top}/${b}/serial.txt" | head -60 || true; done +fi +if [[ "${fail}" == 0 ]]; then echo "${final_tag}: PASS (QEMU only; hardware NOT_RUN)"; exit 0; fi +echo "${final_tag}: FAIL"; exit 1 ```