interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_ck_store_test.sh

475 lines · 28710 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Store gate for the AIENOS C kernel (Lane 18): boots the full C image (core +
18# devices, security and Store stages) in QEMU AArch64 with UEFI (AAVMF) and an
19# NVMe disk, using the base QEMU invocation of scripts/qemu_boot_test.sh plus
20# the NVMe device flags of the Rust NVMe/Store QEMU tests. QEMU is not
21# hardware: a PASS here qualifies nothing physical.
22#
23# Images (native/kernel/Makefile):
24# - make full: the default build. With an SMMUv3 (QEMU iommu=smmuv3, found
25# through the ACPI IORT) the NVMe stream is confined to its DMA window
26# (boots 1-4); without one NVMe DMA must be refused (boot 5).
27# - make full CK_QEMU_UNSAFE_DMA=1: TEST-ONLY build with the unconfined NVMe
28# DMA bypass, kept so the bypass path stays honest (boot 6 only).
29#
30# Per NVMe geometry (512 B blocks like qemu_nvme_test.sh, 4 KiB blocks like
31# qemu_store_crash_test.sh / qemu_continuity_test.sh), on one fresh 64 MiB GPT
32# disk image from native/kernel/tools/ck_gpt_image.c (protective MBR, primary
33# and backup GPT, sentinel-a [1,9) MiB filled, AIENOS partition [9,57) MiB all
34# zero, sentinel-b [57,63) MiB filled). The kernel works only inside the AIENOS
35# partition (dev/disk_part.h); boots 1-3 also check the GPT line, the
36# translation-layer lines and that every byte outside the partition is
37# unchanged (sha256 of the image with the partition cut out):
38# boots 1-3 same image each time. Every boot must pass all M1 checks
39# (scripts/lib_ck_m1_checks.sh, the same checks as
40# qemu_ck_boot_test.sh), stage devices/security/store ok, the NVMe
41# observables, the caps and argus narrow-revoke lines, and the
42# Store line: boot_count 1,2,3; committed generation strictly
43# increasing (and each boot opens the previous boot's generation);
44# prev_commit=none on boot 1 and this commit on boots 2 and 3.
45# boot 4 after XOR 0xa5 over the first 2 x 4096 bytes of the Store region
46# (the Store superblock; same corruption as svc/tests/stage_test.c):
47# must be refused as proof=structural with nothing committed, and
48# the image sha256 must be the same before and after the boot.
49# boot 5 default image, no SMMU, same disk: NVMe DMA denied, no
50# unsafe-bypass text, Store refused (no disk), image unchanged.
51# boot 6 TEST-ONLY bypass image, no SMMU, same (corrupt) disk: the bypass
52# warnings and grant are printed, the Store is still refused as
53# structural and the image is unchanged.
54# Once, after both geometries:
55# boot 7 default image, SMMU, fresh 512 B disk, on QEMU -cpu neoverse-n1
56# (no FEAT_RNG, so no RNDR). The kernel has no fallback entropy:
57# M1 must show "entropy: unavailable reason=absent" instead of
58# "entropy: rndr", the capability office token read must be
59# refused, caps/ARGUS must fail closed and "stage security: FAIL"
60# must print (into ARGUS1_REVOKE: that gate cannot PASS unless the
61# no-RNDR boot refuses); the Store still opens (its nonces are
62# keyed, not random). AIENOS_QEMU_CPU overrides "-cpu max" on boots
63# 1-6 only for manual negative runs; scripts/ck_gates.sh unsets it.
64# SMMU observables (boots 1-4; patterns of qemu_nvme_test.sh where the Rust
65# kernel has them): "smmu: enabled", "smmu_dma_window: nvme only, translation
66# active", "dma_gate: nvme granted (Confined), bus master on", absent
67# "UNSAFE NVME DMA BYPASS"; C only (differs): "smmu_negative: refused dma
68# outside window ... page=intact ... recovery_read=ok" (the controller is
69# pointed at a page outside the window, the SMMU must report a translation
70# fault for exactly that page and the page must be unchanged) and "smmu: nvme
71# stream 0x.. returned to abort" after the bus master is revoked.
72#
73# Patterns copied verbatim from the Rust scripts (qemu_nvme_test.sh):
74# "WARNING: UNSAFE NVME DMA BYPASS BUILD", "WARNING: UNSAFE NVME DMA BYPASS
75# ACTIVE", "dma_gate: nvme granted (UnsafeBypass)", "dma_gate: nvme bus
76# master revoked", "dma_gate: nvme denied (NoSmmu), bus master stays off",
77# "nvme: unavailable (SMMU DMA isolation not active)", absent "dma_gate: nvme
78# granted" and absent "UNSAFE NVME DMA BYPASS" in the safe image, and
79# "report_kind: (panic|fault)" (via the M1 checks).
80# Patterns that differ (the C kernel prints its own line format; the Rust
81# NVME_*_QEMU and STORE_* marker lines have no C equivalent):
82# "nvme: discovery " (Rust NVME_DISCOVERY_QEMU), "nvme: identify ok "
83# (NVME_IDENTIFY_QEMU), "nvme: geometry nsid=1 block_count=N block_size=B"
84# (NVME_GEOMETRY_QEMU, same values), "nvme: bounds read lba=N -> refused"
85# (NVME_BOUNDS_QEMU), "nvme: read lba=0 blocks=1 ok" and "nvme: rw probe ...
86# match" (NVME_READ_QEMU reads a host sentinel; the C kernel reads LBA 0 and
87# write/flush/reads back then restores its scratch unit), "devices: pci=ok
88# nvme=bound", "stage <name>: ok|FAIL", "store: ..." lines, "caps: ok ..."
89# and "argus: ok narrow_revoke=1 ...". The Rust NVME_ERROR_QEMU check (a
90# device-reported command error) has no C counterpart and is not run.
91# Boots 1-4 add iommu=smmuv3 to the machine like the Rust Store scripts.
92# NVMe shutdown (C-only gate, no Rust counterpart): on every boot where the
93# NVMe bus master was on (boots 1-4 and 6) the kernel must print exactly one
94# "nvme: shutdown normal cc=..->.. csts=.. shst=complete" line with CC.SHN read
95# back as 01b and CSTS.SHST as 10b, before "dma_gate: nvme bus master
96# revoked"; QEMU's own trace (pci_nvme_mmio_shutdown_set, pci_cfg_write) must
97# show the shutdown landing between the BME set and BME clear writes. Boot 5
98# must show no shutdown. Every boot must show the ck_reset quiesce hook.
99#
100# Reads the machine quiet flag and takes the QEMU gate lock like
101# qemu_ck_boot_test.sh (scripts/lib_gate_hold.sh; the lock is released at most
102# once, only if it still holds exactly this run's record). Any QEMU exit status other than 0 fails.
103# Final lines: AIENOS_CK_M4_NVME, AIENOS_CK_M4_STORE, AIENOS_CK_ARGUS1_REVOKE,
104# AIENOS_CK_SMMU, AIENOS_CK_NVME_SHUTDOWN, each PASS|FAIL|NOT_RUN. A boot that
105# fails an M1 check fails all five.
106# Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64).
107set -euo pipefail
108
109repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
110cd "${repo_root}"
111
112verdicts() { # one value for all three
113 echo "AIENOS_CK_M4_NVME: $1"
114 echo "AIENOS_CK_M4_STORE: $1"
115 echo "AIENOS_CK_ARGUS1_REVOKE: $1"
116 echo "AIENOS_CK_SMMU: $1"
117 echo "AIENOS_CK_NVME_SHUTDOWN: $1"
118}
119code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
120vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
121command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdicts NOT_RUN; exit 2; }
122[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdicts NOT_RUN; exit 2; }
123
124cross=""
125if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
126commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
127out="${repo_root}/target/native-kernel"
128make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full CK_QEMU_UNSAFE_DMA=1 >/dev/null
129make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full >/dev/null
130make -s -C native/kernel OUT="${out}" gpt-image >/dev/null
131gpt_tool="${out}/host/ck_gpt_image"
132unsafe_efi="${out}/full-qemu-unsafe-dma/BOOTAA64.EFI"
133safe_efi="${out}/full/BOOTAA64.EFI"
134
135# Machine quiet flag: one heavy run at a time on the Spark.
136# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
137# scripts/lib_gate_hold.sh (aienos#278).
138. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
139if ! gh_quiet_check || ! gh_lock_take "qemu_ck_store_test" "${AIENOS_GATE_MINUTES:-60}"; then
140 echo "NOT_RUN ${gh_why}"
141 verdicts NOT_RUN
142 exit 3
143fi
144# Release only this run's own gate lock record, at most once.
145release_flag() { gh_lock_release; }
146top="$(mktemp -d)"
147printf '%s\n' pci_nvme_mmio_shutdown_set pci_nvme_mmio_shutdown_cleared pci_cfg_write >"${top}/trace.events"
148cleanup() {
149 rm -rf "${top}"
150 release_flag
151}
152trap cleanup EXIT
153
154# shellcheck source=scripts/lib_ck_m1_checks.sh
155source "${repo_root}/scripts/lib_ck_m1_checks.sh"
156img_bytes=67108864
157m1_fail=0; nvme_fail=0; store_fail=0; argus_fail=0; smmu_fail=0; shut_fail=0
158fail_into() { # bucket variable name: move the current failed flag into it
159 if [[ "${failed}" != 0 ]]; then printf -v "$1" 1; fi
160 failed=0
161}
162sha() { sha256sum "$1" | cut -d' ' -f1; }
163# GPT boot disk (tools/ck_gpt_image.c): sentinel-a, AIENOS (all zero), sentinel-b.
164# new_disk IMG BS: create it, set part_first/part_last (as WRITTEN by the tool)
165# and outside_ref (sha256 of every byte outside the AIENOS partition: MBR,
166# primary + backup GPT and both sentinels).
167new_disk() {
168 local l re='^gpt_image bs=[0-9]+ blocks=[0-9]+ aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+)$'
169 l=$("${gpt_tool}" create "$1" "$2" $(( img_bytes / 1048576 )) aienos-middle) || l=""
170 if [[ "${l}" =~ ${re} ]]; then part_first="${BASH_REMATCH[1]}"; part_last="${BASH_REMATCH[2]}"
171 else echo "FAIL GPT boot disk image not created (${l})"; part_first=0; part_last=0; m1_fail=1; fi
172 outside_ref=$(outside_sha "$1" "$2")
173}
174# outside_sha IMG BS: sha256 of the image with the AIENOS partition cut out.
175outside_sha() {
176 { head -c $(( part_first * $2 )) "$1"; tail -c +$(( (part_last + 1) * $2 + 1 )) "$1"; } | sha256sum | cut -d' ' -f1
177}
178# layout_checks BS: the kernel found the AIENOS partition where the tool wrote
179# it, its translation refuses past the end, and nothing outside it changed.
180layout_checks() {
181 check "GPT parsed, AIENOS partition found where the image tool wrote it (C only)" \
182 "^disk: gpt ok primary+backup crc32 entries=128 used=3 aienos_index=1 first_lba=${part_first} last_lba=${part_last} "
183 check "partition LBA 0 translates to the partition start (C only)" "^disk: xlate part_lba=0 -> disk_lba=${part_first} (rc=0)$"
184 check "write past the partition end refused by the translation layer (C only)" \
185 "^disk: write past partition end part_lba=[0-9]* -> refused "
186 check_absent "no AIENOS-partition refusal on the good disk (C only)" "disk: no AIENOS partition"
187 local now; now=$(outside_sha "$2" "$1")
188 [[ "${now}" == "${outside_ref}" ]] && echo "PASS MBR, primary+backup GPT and both sentinel partitions byte-identical (C only)" \
189 || { echo "FAIL bytes outside the AIENOS partition changed (${outside_ref:0:16} -> ${now:0:16})"; failed=1; }
190}
191
192qemu_cpu="${AIENOS_QEMU_CPU:-max}"
193boot_cpu="${qemu_cpu}"
194# boot <name> <efi> <disk image> <block bytes> <serial> [smmu]: one QEMU boot
195# (with "smmu" the machine gets an SMMUv3); leaves
196# the serial text in ${work}/serial.txt and the exit status in qemu_status.
197boot() {
198 work="${top}/$1"
199 mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
200 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
201 cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
202 cp "${vars_fd}" "${work}/vars.fd"
203 local nvme_dev="nvme,drive=nvme0,serial=$5"
204 [[ "$4" == 512 ]] || nvme_dev="${nvme_dev},logical_block_size=$4,physical_block_size=$4"
205 local machine="virt,virtualization=on,gic-version=3"
206 [[ "${6:-}" != smmu ]] || machine+=",iommu=smmuv3"
207 set +e
208 # Issue #61: single-threaded TCG (see qemu_boot_test.sh).
209 timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \
210 -M "${machine}" -accel tcg,thread=single -cpu "${boot_cpu}" -smp 4 -m 2048 \
211 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
212 -drive if=pflash,format=raw,file="${work}/vars.fd" \
213 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
214 -device virtio-blk-pci,drive=esp \
215 -drive if=none,id=nvme0,format=raw,file="$3" \
216 -device "${nvme_dev}" \
217 -device ramfb -display none -nic none \
218 -serial file:"${work}/serial.log" -no-reboot \
219 -D "${work}/trace.log" -trace events="${top}/trace.events"
220 qemu_status=$?
221 set -e
222 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt"
223 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_store_$1.log"
224 echo "== boot $1: qemu exit ${qemu_status} (-cpu ${boot_cpu})"
225 failed=0
226 ck_m1_checks
227 fail_into m1_fail
228}
229# field <regex with one group>: first group of the last matching line.
230field() {
231 local line
232 line=$(grep -E "$1" "${work}/serial.txt" | tail -1 || true)
233 [[ "${line}" =~ $1 ]] && echo "${BASH_REMATCH[1]}" || echo ""
234}
235nvme_checks() { # block bytes, confined|bypass: the bound NVMe path
236 local bs="$1" count=$(( img_bytes / $1 ))
237 if [[ "$2" == confined ]]; then
238 check "SMMUv3 enabled from the IORT" "smmu: enabled"
239 check "NVMe DMA window mapped through the SMMU" "smmu_dma_window: nvme only, translation active"
240 check "NVMe DMA granted confined" "dma_gate: nvme granted (Confined), bus master on"
241 check_absent "no unsafe DMA bypass in this image" "UNSAFE NVME DMA BYPASS"
242 check_absent "no unconfined grant" "dma_gate: nvme granted (UnsafeBypass)"
243 check "DMA outside the window faulted, page intact, controller still usable (differs)" \
244 "smmu_negative: refused dma outside window iova=0x[0-9a-f]* faults=[1-9][0-9]* type=0x10 sid=0x[0-9a-f]* addr=0x[0-9a-f]* page=intact cmd_rc=-\?[0-9]* recovery_read=ok$"
245 check "NVMe stream returned to abort after the revoke (differs)" "smmu: nvme stream 0x[0-9a-f]* returned to abort (rc=0)"
246 fail_into smmu_fail
247 else
248 check "TEST-ONLY bypass build announced on serial" "WARNING: UNSAFE NVME DMA BYPASS BUILD"
249 check "TEST-ONLY bypass grant announced on serial" "WARNING: UNSAFE NVME DMA BYPASS ACTIVE"
250 check "NVMe DMA granted through the TEST-ONLY bypass" "dma_gate: nvme granted (UnsafeBypass)"
251 check_absent "no SMMU window claimed by the bypass" "smmu_dma_window:"
252 fi
253 check "ECAM discovery found the NVMe class device (differs)" "nvme: discovery "
254 check "controller and namespace identify completed (differs)" "nvme: identify ok "
255 check "namespace geometry matches the image (differs)" \
256 "nvme: geometry nsid=1 block_count=${count} block_size=${bs}$"
257 check "read past the namespace end is rejected before any command (differs)" \
258 "nvme: bounds read lba=${count} -> refused"
259 check "LBA 0 read (differs)" "nvme: read lba=0 blocks=1 ok"
260 check "scratch unit (last unit of the AIENOS partition) write+flush+readback matched (differs)" \
261 "nvme: rw probe part_lba=[0-9]* disk_lba=[0-9]* bytes=4096 write+flush+readback match"
262 check "NVMe bound by the devices stage (differs)" "devices: pci=ok nvme=bound"
263 check "stage devices ok (differs)" "stage devices: ok"
264 check "NVMe bus master revoked after the read phase" "dma_gate: nvme bus master revoked"
265 check_absent "no bus master revoke failure (differs)" "bus master revoke FAILED"
266 fail_into nvme_fail
267}
268# shutdown_checks: the bound NVMe path must do an NVMe normal shutdown
269# (CC.SHN = 01b, then CSTS.SHST = 10b) before the bus-master revoke. Three
270# independent views: the kernel's register readback on serial, the serial
271# order, and QEMU's own device trace (pci_nvme_mmio_shutdown_set must fall
272# between the guest setting BME in the NVMe command register and clearing it).
273shutdown_checks() {
274 local sre='^nvme: shutdown normal cc=0x([0-9a-f]{8})->0x([0-9a-f]{8}) csts=0x([0-9a-f]{8}) shst=([A-Za-z-]+) waited_us=([0-9]+)$'
275 local n line ls lr order
276 n=$(grep -cE "${sre}" "${work}/serial.txt" || true)
277 line=$(grep -E "${sre}" "${work}/serial.txt" | tail -1 || true)
278 if [[ "${n}" == 1 && "${line}" =~ ${sre} ]]; then
279 local cc_a=$(( 16#${BASH_REMATCH[2]} )) csts=$(( 16#${BASH_REMATCH[3]} )) res="${BASH_REMATCH[4]}"
280 if (( ((cc_a >> 14) & 3) == 1 )); then echo "PASS CC.SHN read back as 01b (normal shutdown requested)"
281 else echo "FAIL CC.SHN read back as $(( (cc_a >> 14) & 3 )), expected 1"; failed=1; fi
282 if (( ((csts >> 2) & 3) == 2 )) && [[ "${res}" == complete ]]; then
283 echo "PASS CSTS.SHST read back as 10b (shutdown complete, waited ${BASH_REMATCH[5]} us)"
284 else echo "FAIL shutdown not complete: ${line}"; failed=1; fi
285 else
286 echo "FAIL exactly one NVMe shutdown line reported (found ${n})"; failed=1
287 fi
288 ls=$(grep -nE '^nvme: shutdown normal ' "${work}/serial.txt" | head -1 | cut -d: -f1)
289 lr=$(grep -n 'dma_gate: nvme bus master revoked' "${work}/serial.txt" | head -1 | cut -d: -f1)
290 if [[ -n "${ls}" && -n "${lr}" && "${ls}" -lt "${lr}" ]]; then echo "PASS shutdown reported before the bus-master revoke (serial order)"
291 else echo "FAIL shutdown not before the bus-master revoke (serial lines ${ls:-none} / ${lr:-none})"; failed=1; fi
292 order=$(grep -E '^pci_nvme_mmio_shutdown_set|^pci_cfg_write nvme [0-9a-f:.]+ @0x4 <- 0x[0-9a-f]+$' "${work}/trace.log" 2>/dev/null | {
293 on=0; s=0; out=none
294 while read -r a _ _ _ _ v; do
295 if [[ "${a}" == pci_nvme_mmio_shutdown_set ]]; then (( on )) && s=1
296 elif (( v & 4 )); then on=1; s=0
297 elif (( on )); then if (( s )); then out=ok; else out=bad; fi; on=0
298 fi
299 done
300 echo "${out}"; })
301 [[ "${order}" == ok ]] && echo "PASS QEMU device trace: shutdown set while BME on, then BME cleared" \
302 || { echo "FAIL QEMU device trace order (${order})"; failed=1; }
303 check_absent "no fallback disable needed (normal shutdown completed)" "nvme: shutdown fallback disable"
304 check "reset quiesce hook ran after the release (differs)" "devices: quiesce before reset nvme=already-released"
305 fail_into shut_fail
306}
307argus_checks() {
308 check "capabilities: grant, attenuate, deny amplify/forged/revoked, RNDR token (differs)" \
309 "caps: ok granted=yes attenuated=yes amplify=denied forged=denied revoked=denied office_token=rndr$"
310 check "ARGUS narrow revoke: revoked denied, unrelated granted, authority unchanged (differs)" \
311 "argus: ok narrow_revoke=1 revoked=denied unrelated=granted authority=unchanged$"
312 check "stage security ok (differs)" "stage security: ok"
313 fail_into argus_fail
314}
315store_fail_msg() { echo "FAIL $1"; store_fail=1; }
316
317for bs in 512 4096; do
318 if [[ "${bs}" == 512 ]]; then serial=aienos-nvme-test; else serial=aienos-continuity; fi
319 image="${top}/nvme-${bs}.img"
320 new_disk "${image}" "${bs}"
321 prev_gen=""
322 for k in 1 2 3; do
323 boot "${bs}-boot${k}" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu
324 nvme_checks "${bs}" confined
325 layout_checks "${bs}" "${image}"
326 fail_into nvme_fail
327 shutdown_checks
328 argus_checks
329 check "stage store ok (differs)" "stage store: ok"
330 check_absent "Store not refused (differs)" "store: REFUSED"
331 if [[ "${k}" == 1 ]]; then
332 check "blank disk formatted as a TEST store (differs)" \
333 "store: blank disk (all-zero anchor + Store head): formatted TEST store"
334 else
335 check_absent "existing Store not reformatted (differs)" "formatted TEST store"
336 fi
337 fail_into store_fail
338 open_re='^store: opened generation=([0-9]+) boot_count=([0-9]+) prev_commit=([^ ]+) anchor=([a-z?-]+)$'
339 commit_re='^store: committed generation=([0-9]+) boot_count=([0-9]+)$'
340 oline=$(grep -E "${open_re}" "${work}/serial.txt" | tail -1 || true)
341 cline=$(grep -E "${commit_re}" "${work}/serial.txt" | tail -1 || true)
342 if [[ "${oline}" =~ ${open_re} ]]; then
343 o_gen="${BASH_REMATCH[1]}"; o_bc="${BASH_REMATCH[2]}"; o_prev="${BASH_REMATCH[3]}"; o_anchor="${BASH_REMATCH[4]}"
344 else
345 o_gen=""; o_bc=""; o_prev=""; o_anchor=""
346 fi
347 if [[ "${cline}" =~ ${commit_re} ]]; then c_gen="${BASH_REMATCH[1]}"; c_bc="${BASH_REMATCH[2]}"; else c_gen=""; c_bc=""; fi
348 if [[ -z "${o_gen}" || -z "${c_gen}" ]]; then
349 store_fail_msg "Store opened and committed lines reported (boot ${k})"
350 else
351 echo " store boot ${k}: opened gen=${o_gen} boot_count=${o_bc} prev_commit=${o_prev:0:12} anchor=${o_anchor}; committed gen=${c_gen} boot_count=${c_bc}"
352 [[ "${c_bc}" == "${k}" && "${o_bc}" == $(( k - 1 )) ]] && echo "PASS boot_count ${k}" \
353 || store_fail_msg "boot_count ${c_bc} (opened ${o_bc}), expected ${k}"
354 [[ "${c_gen}" == $(( o_gen + 1 )) ]] && echo "PASS commit advanced the generation by one" \
355 || store_fail_msg "committed generation ${c_gen} after opening ${o_gen}"
356 if [[ -n "${prev_gen}" ]]; then
357 [[ "${c_gen}" -gt "${prev_gen}" && "${o_gen}" == "${prev_gen}" ]] \
358 && echo "PASS generation strictly increasing and opened at the previous boot's commit (${prev_gen} -> ${c_gen})" \
359 || store_fail_msg "generation ${o_gen}->${c_gen} does not follow previous commit ${prev_gen}"
360 fi
361 if [[ "${k}" == 1 ]]; then
362 [[ "${o_prev}" == none ]] && echo "PASS prev_commit none on the first boot" \
363 || store_fail_msg "prev_commit ${o_prev} on the first boot, expected none"
364 else
365 [[ "${o_prev}" == "${commit}" ]] && echo "PASS prev_commit is this commit" \
366 || store_fail_msg "prev_commit ${o_prev}, expected ${commit}"
367 fi
368 prev_gen="${c_gen}"
369 fi
370 done
371
372 # Boot 4: corrupt the Store superblock on the host image.
373 store_lba=$(field '^store: TEST identity.* store lba=([0-9]+) ')
374 # store lba is relative to the AIENOS partition (dev/disk_layout.h).
375 soff=$(( ${store_lba:-0} * bs ))
376 if [[ -z "${store_lba}" || "${soff}" != 16384 ]]; then
377 store_fail_msg "Store region offset ${soff} in the partition (lba ${store_lba:-?}), expected byte 16384 (4 anchor units)"
378 soff=16384
379 fi
380 soff=$(( part_first * bs + soff ))
381 sha_clean=$(sha "${image}")
382 # XOR 0xa5 over 2 x 4096 bytes, as svc/tests/stage_test.c does (shell only).
383 fmt=""
384 for b in $(od -An -v -tu1 -j "${soff}" -N 8192 "${image}"); do fmt+=$(printf '\\%03o' $(( b ^ 0xa5 ))); done
385 printf "${fmt}" | dd of="${image}" bs=4096 seek=$(( soff / 4096 )) conv=notrunc status=none
386 sha_corrupt=$(sha "${image}")
387 [[ "${sha_corrupt}" != "${sha_clean}" && $(stat -c %s "${image}") == "${img_bytes}" ]] \
388 && echo "PASS host corrupted the Store superblock (8192 bytes at ${soff})" \
389 || store_fail_msg "host corruption did not change the image"
390 boot "${bs}-boot4-corrupt" "${safe_efi}" "${image}" "${bs}" "${serial}" smmu
391 nvme_checks "${bs}" confined
392 shutdown_checks
393 argus_checks
394 check "corrupt Store refused as structural (differs)" "store: REFUSED proof=structural "
395 check "corrupt Store left as found, not reformatted (differs)" "disk left as found, not reformatted"
396 check "stage store failed (differs)" "stage store: FAIL"
397 check_absent "nothing committed on the corrupt Store (differs)" "store: committed"
398 check_absent "corrupt Store not reformatted (differs)" "formatted TEST store"
399 fail_into store_fail
400 sha_after=$(sha "${image}")
401 [[ "${sha_after}" == "${sha_corrupt}" ]] && echo "PASS image sha256 unchanged by the refused boot (${sha_after:0:16})" \
402 || store_fail_msg "image changed by the refused boot (${sha_corrupt:0:16} -> ${sha_after:0:16})"
403
404 # Boot 5: default image, NVMe DMA must stay refused.
405 boot "${bs}-boot5-safe" "${safe_efi}" "${image}" "${bs}" "${serial}"
406 check "discovery found the device before the DMA gate (differs)" "nvme: discovery "
407 check "NVMe DMA denied without an SMMU" "dma_gate: nvme denied (NoSmmu), bus master stays off"
408 check "NVMe controller fail-closed without an SMMU" "nvme: unavailable (SMMU DMA isolation not active)"
409 check_absent "NVMe controller never granted DMA" "dma_gate: nvme granted"
410 check_absent "no unsafe DMA bypass in this image" "UNSAFE NVME DMA BYPASS"
411 check_absent "NVMe controller never identified (differs)" "nvme: identify"
412 check_absent "no NVMe shutdown without a bound controller (differs)" "nvme: shutdown normal"
413 check "reset quiesce hook found nothing live (differs)" "devices: quiesce before reset nvme=already-released"
414 check "NVMe left unbound (differs)" "devices: pci=ok nvme=unbound"
415 fail_into nvme_fail
416 check "Store refused without a disk (differs)" 'store: REFUSED proof=io step="no boot disk"'
417 fail_into store_fail
418 argus_checks
419 sha_safe=$(sha "${image}")
420 [[ "${sha_safe}" == "${sha_corrupt}" ]] && echo "PASS image sha256 unchanged by the fail-closed boot" \
421 || store_fail_msg "image changed by the fail-closed boot"
422
423 # Boot 6: TEST-ONLY bypass image, no SMMU. Kept so the bypass path stays
424 # honest: it must announce itself, and the corrupt Store is still refused.
425 boot "${bs}-boot6-bypass" "${unsafe_efi}" "${image}" "${bs}" "${serial}"
426 nvme_checks "${bs}" bypass
427 shutdown_checks
428 argus_checks
429 check "corrupt Store refused under the bypass image too (differs)" "store: REFUSED proof=structural "
430 check_absent "nothing committed under the bypass image (differs)" "store: committed"
431 fail_into store_fail
432 sha_bypass=$(sha "${image}")
433 [[ "${sha_bypass}" == "${sha_corrupt}" ]] && echo "PASS image sha256 unchanged by the bypass boot" \
434 || store_fail_msg "image changed by the bypass boot"
435done
436
437# Boot 7: no FEAT_RNG (QEMU -cpu neoverse-n1 has no RNDR). The kernel has no
438# fallback entropy source, so the office token must be refused and the
439# security stage must fail closed; the Store still opens (keyed nonces).
440image="${top}/nvme-nornd.img"
441new_disk "${image}" 512
442boot_cpu=neoverse-n1
443ck_m1_entropy=absent
444boot "boot7-no-rndr" "${safe_efi}" "${image}" 512 aienos-nvme-test smmu
445boot_cpu="${qemu_cpu}"
446ck_m1_entropy=rndr
447check "office token read refused without RNDR, service fails closed (C only)" \
448 "^entropy: refused consumer=/dev/urandom (capability office token) reason=absent; service fails closed$"
449check "security stage refuses without kernel entropy (C only)" \
450 "^security: REFUSED kernel entropy absent (office token needs RNDR); capability office and ARGUS fail closed$"
451check "capabilities not started, no office token (C only)" "^caps: failed .* office_token=none$"
452check "ARGUS not started: capability office refused (C only)" "^argus: FAIL step=cap_start "
453check "stage security failed (C only)" "^stage security: FAIL"
454check_absent "no capability grant without entropy (C only)" "^caps: ok"
455check_absent "no ARGUS narrow revoke without entropy (C only)" "^argus: ok"
456fail_into argus_fail
457check "Store still opens without RNDR: its nonces are keyed, not random (C only)" "^stage store: ok"
458fail_into store_fail
459release_flag
460
461if [[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}${shut_fail}" != 000000 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
462 for s in "${top}"/*/serial.txt; do
463 echo "---- serial console $(basename "$(dirname "${s}")") (stage lines) ----"
464 grep -E '^(stage |smmu|nvme:|dma_gate:|WARNING|devices:|store:|caps:|argus:|entropy:|security:|report_kind:|pci_nvme_mmio_shutdown)' "${s}" | head -60 || true
465 done
466fi
467[[ "${m1_fail}" != 0 ]] && echo "M1 checks failed on at least one boot: no verdict can pass"
468v() { [[ "${m1_fail}" == 0 && "$1" == 0 ]] && echo PASS || echo FAIL; }
469# Confined mode is part of the NVMe gate: an SMMU failure fails it too.
470echo "AIENOS_CK_M4_NVME: $(v "$(( nvme_fail | smmu_fail ))")"
471echo "AIENOS_CK_M4_STORE: $(v "${store_fail}")"
472echo "AIENOS_CK_ARGUS1_REVOKE: $(v "${argus_fail}")"
473echo "AIENOS_CK_SMMU: $(v "${smmu_fail}")"
474echo "AIENOS_CK_NVME_SHUTDOWN: $(v "${shut_fail}")"
475[[ "${m1_fail}${nvme_fail}${store_fail}${argus_fail}${smmu_fail}${shut_fail}" == 000000 ]] || exit 1