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_nvme_rw_test.sh

254 lines · 11954 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# P3 native NVMe WRITE + FLUSH durability proof in QEMU (disposable media).
18#
19# Two boots over ONE raw image file:
20# boot 1 writes a deterministic pattern to a read-write LBA and issues a
21# real NVMe Flush, then the host reads the image file back and checks
22# the exact bytes and SHA-256 (power-off persistence).
23# boot 2 boots again on the same image and reads the target natively; the
24# kernel reports durability across the restart.
25#
26# Read side (sentinel LBA 2048) is unchanged from scripts/qemu_nvme_test.sh.
27# Write side targets LBA 4096 with the 16-byte magic "AIENOS-NVME-RW01" x32.
28# The image starts with "AIENOS-NVME-OLD0" x32 there.
29#
30# DMA rule (M3): no SMMU confinement means no DMA. Mirrors the read harness:
31# default QEMU SMMUv3; NVMe DMA only via a translated stream.
32# AIENOS_QEMU_SMMU=0 no SMMU, normal build: NVMe stays unavailable,
33# no write, bus mastering off (fail-closed).
34#
35# Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64).
36#
37# scripts/verify_all.sh step 6e runs this harness in both DMA modes.
38set -euo pipefail
39
40repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
41cd "${repo_root}"
42
43code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
44vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
45command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
46[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; }
47
48attempts="${AIENOS_NVME_ATTEMPTS:-3}"
49boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
50if [[ "${AIENOS_QEMU_SMMU:-1}" == "1" ]]; then
51 mode="smmu"
52 machine="virt,virtualization=on,gic-version=3,iommu=smmuv3"
53else
54 mode="fail-closed"
55 machine="virt,virtualization=on,gic-version=3"
56fi
57target_dir="target/qemu-nvme-rw"
58
59commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
60AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
61 -p aienos-boot --target aarch64-unknown-uefi --features nvme-write --bin aienos-handoff \
62 --target-dir "${target_dir}"
63
64work="$(mktemp -d)"
65qemu_pid=""
66cleanup() {
67 [[ -n "${qemu_pid}" ]] && kill "${qemu_pid}" 2>/dev/null || true
68 exec 3>&- 2>/dev/null || true
69 rm -rf "${work}"
70}
71trap cleanup EXIT
72mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
73touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
74cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
75
76# ---------------------------------------------------------------------------
77# Disposable media. 64 MiB raw, 131072 LBAs of 512 bytes.
78# sentinel (read) LBA 2048 : "AIENOS-NVME-SENT" x32
79# rw target LBA 4096 : "AIENOS-NVME-OLD0" x32 (host seed)
80# written pattern LBA 4096 : "AIENOS-NVME-RW01" x32 (guest write)
81# bounds write LBA 131072 (== block_count), must be rejected OutOfRange
82# write error NSID 0xffffffff, must return a nonzero completion status
83# ---------------------------------------------------------------------------
84img_bytes=67108864
85lba_bytes=512
86sentinel_lba=2048
87rw_lba=4096
88bounds_lba=$(( img_bytes / lba_bytes ))
89
90pattern() { # magic -> 512-byte file
91 local out="$1" magic="$2"
92 : > "${out}"
93 for _ in {1..32}; do printf '%s' "${magic}" >> "${out}"; done
94 [[ "$(wc -c <"${out}")" == "512" ]] || { echo "STOP: payload must be 512 bytes"; exit 2; }
95}
96
97image="${work}/nvme.img"
98pattern "${work}/sentinel.bin" 'AIENOS-NVME-SENT'
99pattern "${work}/old.bin" 'AIENOS-NVME-OLD0'
100pattern "${work}/new.bin" 'AIENOS-NVME-RW01'
101sentinel_sha="$(sha256sum "${work}/sentinel.bin" | cut -d' ' -f1)"
102old_sha="$(sha256sum "${work}/old.bin" | cut -d' ' -f1)"
103new_sha="$(sha256sum "${work}/new.bin" | cut -d' ' -f1)"
104
105truncate -s "${img_bytes}" "${image}"
106dd if="${work}/sentinel.bin" of="${image}" bs="${lba_bytes}" seek="${sentinel_lba}" conv=notrunc status=none
107dd if="${work}/old.bin" of="${image}" bs="${lba_bytes}" seek="${rw_lba}" conv=notrunc status=none
108[[ "$(dd if="${image}" bs="${lba_bytes}" skip="${rw_lba}" count=1 status=none | sha256sum | cut -d' ' -f1)" == "${old_sha}" ]] \
109 || { echo "STOP: OLD seed not at LBA ${rw_lba}"; exit 2; }
110
111echo "nvme image: ${img_bytes} bytes, ${bounds_lba} x ${lba_bytes} B LBAs"
112echo "sentinel LBA ${sentinel_lba} sha256 ${sentinel_sha}"
113echo "rw old LBA ${rw_lba} sha256 ${old_sha}"
114echo "rw new LBA ${rw_lba} sha256 ${new_sha}"
115
116serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -q -- "$1"; }
117qemu_running() { kill -0 "${qemu_pid}" 2>/dev/null; }
118wait_for() { # seconds, pattern...
119 local deadline=$(( $(date +%s) + $1 )); shift
120 while (( $(date +%s) < deadline )) && qemu_running; do
121 for p in "$@"; do serial_has "${p}" && return 0; done
122 sleep 0.2
123 done
124 for p in "$@"; do serial_has "${p}" && return 0; done
125 return 1
126}
127stop_qemu() {
128 if [[ -n "${qemu_pid}" ]]; then
129 kill "${qemu_pid}" 2>/dev/null || true
130 wait "${qemu_pid}" 2>/dev/null || true
131 fi
132 qemu_pid=""
133 exec 3>&- 2>/dev/null || true
134}
135
136# One boot against the shared image. $1 is the terminal marker to wait for.
137boot_once() {
138 cp "${vars_fd}" "${work}/vars.fd"
139 rm -f "${work}/serial.log"
140 qemu-system-aarch64 \
141 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
142 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
143 -drive if=pflash,format=raw,file="${work}/vars.fd" \
144 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
145 -device virtio-blk-pci,drive=esp \
146 -drive if=none,id=nvme0,format=raw,file="${image}" \
147 -device nvme,drive=nvme0,serial=aienos-nvme-rw-test \
148 -device ramfb -display none -nic none \
149 -serial file:"${work}/serial.log" -no-reboot &
150 qemu_pid=$!
151
152 if ! wait_for "${boot_timeout}" "report_kind: pre_exit"; then
153 echo "no AIENOS output within ${boot_timeout} s (firmware hang, issue #61)"
154 stop_qemu
155 return 2
156 fi
157 wait_for "${boot_timeout}" "$1" "report_kind: panic" "report_kind: fault" || true
158 local deadline=$(( $(date +%s) + 20 ))
159 while qemu_running && (( $(date +%s) < deadline )); do sleep 0.5; done
160 stop_qemu
161 return 0
162}
163
164started=$(date +%s)
165if [[ "${mode}" == "smmu" ]]; then
166 terminal="NVME_WRITE_ERROR_QEMU"
167else
168 terminal="nvme: unavailable (SMMU DMA isolation not active)"
169fi
170attempt=1
171while :; do
172 status=0
173 boot_once "${terminal}" || status=$?
174 [[ "${status}" != 2 || "${attempt}" -ge "${attempts}" ]] && break
175 attempt=$(( attempt + 1 ))
176done
177elapsed=$(( $(date +%s) - started ))
178tr -d '\r' <"${work}/serial.log" >"${work}/serial_boot1.txt" 2>/dev/null || true
179
180# boot 2: same image, expect persistence across the restart.
181if [[ "${mode}" == "smmu" ]]; then
182 boot_once "NVME_DURABILITY_QEMU" || true
183 tr -d '\r' <"${work}/serial.log" >"${work}/serial_boot2.txt" 2>/dev/null || true
184fi
185
186[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial_boot1.txt" "${AIENOS_LOG_DIR}/qemu_nvme_rw_serial.log" 2>/dev/null || true
187boot1="${work}/serial_boot1.txt"
188failed=0
189check() { if grep -q -- "$2" "${boot1}"; then echo "PASS $1"; else echo "FAIL $1"; failed=1; fi; }
190check_absent() { if grep -q -- "$2" "${boot1}"; then echo "FAIL $1"; failed=1; else echo "PASS $1"; fi; }
191
192if [[ "${mode}" == "smmu" ]]; then
193 check "ECAM discovery found the NVMe class device" "NVME_DISCOVERY_QEMU: PASS"
194 check "controller and namespace identify completed" "NVME_IDENTIFY_QEMU: PASS"
195 check "namespace geometry matches the image" "NVME_GEOMETRY_QEMU: PASS (nsid=1 block_count=${bounds_lba} block_size=${lba_bytes})"
196 check "NVMe power-fail atomicity fields observed" "NVME_ATOMICITY_IDENTIFY_QEMU:"
197 if grep -q -- "NVME_STORE_ROOT_ATOMICITY_QEMU: PASS" "${boot1}"; then
198 echo "PASS Store root-write power-fail atomicity satisfied"
199 else
200 echo "INFO Store root-write power-fail atomicity NOT proven on this namespace"
201 grep -o -- "NVME_ATOMICITY_IDENTIFY_QEMU:.*" "${boot1}" | head -1 | sed 's/^/INFO /' || true
202 grep -o -- "NVME_STORE_ROOT_ATOMICITY_QEMU:.*" "${boot1}" | head -1 | sed 's/^/INFO /' || true
203 fi
204 check "read sentinel LBA returned exact bytes and SHA-256" "NVME_READ_QEMU: PASS (lba=${sentinel_lba} blocks=1 bytes=512 sha256=${sentinel_sha})"
205 check "read past the namespace end is rejected" "NVME_BOUNDS_QEMU: PASS"
206 check "device-reported read error is surfaced" "NVME_ERROR_QEMU: PASS"
207 check "write past the namespace end is rejected" "NVME_WRITE_BOUNDS_QEMU: PASS"
208 check "native write returned exact bytes and SHA-256" "NVME_WRITE_QEMU: PASS (lba=${rw_lba} blocks=1 bytes=512 sha256=${new_sha})"
209 check "native flush completed" "NVME_FLUSH_QEMU: PASS"
210 check "write then flush then read-back matched" "NVME_DURABILITY_QEMU: PASS (lba=${rw_lba} blocks=1 bytes=512 sha256=${new_sha})"
211 check "invalid-namespace write surfaced a device error" "NVME_WRITE_ERROR_QEMU: PASS"
212 check "IORT stream configured for NVMe DMA" "smmu: enabled"
213 check "NVMe DMA granted only as confined" "dma_gate: nvme granted (Confined), bus master on"
214 check "NVMe bus master revoked after the phase" "dma_gate: nvme bus master revoked"
215
216 # Host persistence check: the image file must now hold the written pattern.
217 host_sha="$(dd if="${image}" bs="${lba_bytes}" skip="${rw_lba}" count=1 status=none | sha256sum | cut -d' ' -f1)"
218 if [[ "${host_sha}" == "${new_sha}" ]]; then
219 echo "PASS host read-back after power off equals the written bytes"
220 else
221 echo "FAIL host read-back ${host_sha} != written ${new_sha}"
222 failed=1
223 fi
224
225 # Boot 2: the kernel must observe the persisted pattern natively.
226 if grep -q -- "NVME_DURABILITY_QEMU: PASS (persisted lba=${rw_lba}" "${work}/serial_boot2.txt" 2>/dev/null; then
227 echo "PASS restart read-back observed the persisted pattern natively"
228 else
229 echo "FAIL restart read-back did not observe the persisted pattern"
230 failed=1
231 fi
232else
233 check "discovery found the device before the DMA gate" "NVME_DISCOVERY_QEMU: PASS"
234 check "NVMe DMA denied without an SMMU" "dma_gate: nvme denied (NoSmmu), bus master stays off"
235 check "NVMe controller fail-closed without an SMMU" "nvme: unavailable (SMMU DMA isolation not active)"
236 check_absent "no write marker without DMA" "NVME_WRITE_QEMU: PASS"
237 check_absent "no flush marker without DMA" "NVME_FLUSH_QEMU: PASS"
238 check_absent "no durability marker without DMA" "NVME_DURABILITY_QEMU: PASS"
239fi
240if grep -qE "report_kind: (panic|fault)" "${boot1}"; then
241 echo "FAIL panic or fault reported (boot 1)"
242 failed=1
243fi
244if [[ -f "${work}/serial_boot2.txt" ]] && grep -qE "report_kind: (panic|fault)" "${work}/serial_boot2.txt"; then
245 echo "FAIL panic or fault reported (boot 2)"
246 failed=1
247fi
248
249if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
250 echo "---- serial console (boot 1) ----"
251 cat "${boot1}"
252 [[ -f "${work}/serial_boot2.txt" ]] && { echo "---- serial console (boot 2) ----"; cat "${work}/serial_boot2.txt"; }
253fi
254[[ "${failed}" == 0 ]] && echo "QEMU_NVME_RW: PASS (${mode})" || { echo "QEMU_NVME_RW: FAIL (${mode})"; exit 1; }