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/blob/main/scripts/qemu_nvme_test.sh # 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 branch `main` Branches Tags History `aienux / scripts / qemu_nvme_test.sh` 291 lines ยท 13164 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 read substrate proof in QEMU (read-only qualification). 18 # 19 # Boot the handoff image built with the `nvme-read` feature with a raw, 20 # disposable NVMe namespace attached as 21 # -drive if=none,id=nvme0,... -device nvme,drive=nvme0,serial=aienos-nvme-test 22 # and let the native AIENOS driver drive the controller directly after 23 # ExitBootServices: ECAM discovery, BAR0 MMIO, polled admin/I/O queues, and a 24 # read of a deterministic sentinel LBA. The host builds the same sentinel and 25 # checks the exact bytes and SHA-256 the kernel reports. 26 # 27 # Read-only: the driver only issues Identify and Read commands. No media write, 28 # flush, format or namespace-management command is claimed or exercised. 29 # 30 # DMA rule (M3): no SMMU confinement means no DMA. Mirrors 31 # scripts/qemu_keyboard_test.sh: 32 # default QEMU SMMUv3 (iommu=smmuv3); the NVMe function 33 # gets DMA only through a translated stream. 34 # AIENOS_QEMU_SMMU=0 no SMMU, normal build: the controller must stay 35 # unavailable with bus mastering off (fail-closed). 36 # AIENOS_UNSAFE_DMA_BYPASS=1 no SMMU, UNSAFE debug build that lets the NVMe 37 # function DMA to physical memory unconfined. 38 # QEMU debugging only; never part of verify_all.sh. 39 # 40 # A boot that shows no AIENOS output at all is retried (firmware hang before 41 # AIENOS runs, issue #61); any failure after AIENOS output fails at once. 42 # 43 # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). 44 # 45 # scripts/verify_all.sh step 6e runs this harness in both DMA modes, and 46 # step 3b lints the nvme-read feature set. 47 set -euo pipefail 48 49 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 50 cd "${repo_root}" 51 52 code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" 53 vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" 54 command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; } 55 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; } 56 57 attempts="${AIENOS_NVME_ATTEMPTS:-3}" 58 boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}" 59 machine="virt,virtualization=on,gic-version=3" 60 61 # DMA mode (M3 rule: no SMMU confinement means no DMA), as in qemu_keyboard_test.sh. 62 bypass_feature="unsafe-debug-nvme-dma-without-smmu" 63 if [[ "${AIENOS_UNSAFE_DMA_BYPASS:-0}" == "1" ]]; then 64 mode="unsafe-bypass" 65 default_features="nvme-read,${bypass_feature}" 66 target_dir="target/qemu-nvme-unsafe-dma-bypass" 67 echo "################################################################" 68 echo "WARNING: AIENOS_UNSAFE_DMA_BYPASS=1: building ${bypass_feature}." 69 echo "WARNING: NVMe DMA runs WITHOUT SMMU confinement. QEMU debug only." 70 echo "WARNING: this run proves nothing about DMA isolation." 71 echo "################################################################" 72 elif [[ "${AIENOS_QEMU_SMMU:-1}" == "1" ]]; then 73 mode="smmu" 74 machine+=",iommu=smmuv3" 75 default_features="nvme-read" 76 target_dir="target/qemu-nvme" 77 else 78 mode="fail-closed" 79 default_features="nvme-read" 80 target_dir="target/qemu-nvme" 81 fi 82 features="${AIENOS_BUILD_FEATURES:-$default_features}" 83 if [[ "${mode}" != "unsafe-bypass" && ",${features}," == *",${bypass_feature},"* ]]; then 84 echo "STOP: ${bypass_feature} requested without AIENOS_UNSAFE_DMA_BYPASS=1" 85 exit 2 86 fi 87 88 # Own target directory: an NVMe-enabled image never lands where hardware 89 # staging or the other tests pick up the handoff image. 90 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 91 AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \ 92 -p aienos-boot --target aarch64-unknown-uefi --features "${features}" --bin aienos-handoff \ 93 --target-dir "${target_dir}" 94 95 work="$(mktemp -d)" 96 qemu_pid="" 97 cleanup() { 98 [[ -n "${qemu_pid}" ]] && kill "${qemu_pid}" 2>/dev/null || true 99 exec 3>&- 2>/dev/null || true 100 rm -rf "${work}" 101 } 102 trap cleanup EXIT 103 mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" 104 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" 105 cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" 106 107 # --------------------------------------------------------------------------- 108 # Deterministic, disposable raw NVMe image (64 MiB), built with the same 109 # tools the repo already uses (dd / od / printf / truncate / sha256sum). 110 # 111 # Geometry: 112 # image : 64 MiB = `67108864` bytes = 131072 LBAs of 512 bytes 113 # sentinel : LBA 2048 (offset `1048576`), one 512-byte block 114 # payload : the 16-byte magic "AIENOS-NVME-SENT" repeated 32 times 115 # bounds LBA : 131072 (== block_count), must be rejected OutOfRange 116 # --------------------------------------------------------------------------- 117 magic='AIENOS-NVME-SENT' 118 img_bytes=`67108864` 119 lba_bytes=512 120 sent_lba=2048 121 sent_offset=$(( sent_lba * lba_bytes )) 122 bounds_lba=$(( img_bytes / lba_bytes )) 123 124 image="${work}/nvme.img" 125 sentinel="${work}/sentinel.bin" 126 # 16 bytes * 32 = 512 bytes, exactly one LBA, no newline. 127 : > "${sentinel}" 128 for _ in {1..32}; do printf '%s' "${magic}" >> "${sentinel}"; done 129 [[ "$(wc -c <"${sentinel}")" == "512" ]] || { echo "STOP: sentinel must be exactly 512 bytes"; exit 2; } 130 # Sparse zero image of the exact namespace size, then drop the sentinel block in. 131 truncate -s "${img_bytes}" "${image}" 132 dd if="${sentinel}" of="${image}" bs="${lba_bytes}" seek="${sent_lba}" conv=notrunc status=none 133 [[ "$(wc -c <"${image}")" == "${img_bytes}" ]] || { echo "STOP: NVMe image is not ${img_bytes} bytes"; exit 2; } 134 135 sentinel_sha="$(sha256sum "${sentinel}")" 136 sentinel_sha="${sentinel_sha%% *}" 137 readback_sha="$(dd if="${image}" bs="${lba_bytes}" skip="${sent_lba}" count=1 status=none | sha256sum)" 138 readback_sha="${readback_sha%% *}" 139 if [[ "${readback_sha}" != "${sentinel_sha}" ]]; then 140 echo "STOP: sentinel block was not written at LBA ${sent_lba} (offset ${sent_offset})" 141 exit 2 142 fi 143 echo "nvme image: ${img_bytes} bytes, ${bounds_lba} x ${lba_bytes} B LBAs" 144 echo "sentinel: LBA ${sent_lba} offset ${sent_offset}, magic '${magic}' x32, sha256 ${sentinel_sha}" 145 146 serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -q -- "$1"; } 147 qemu_running() { kill -0 "${qemu_pid}" 2>/dev/null; } 148 149 # Wait until the serial log contains one of the patterns or QEMU exits. 150 wait_for() { # seconds, pattern... 151 local deadline=$(( $(date +%s) + $1 )); shift 152 while (( $(date +%s) < deadline )) && qemu_running; do 153 for p in "$@"; do serial_has "${p}" && return 0; done 154 sleep 0.2 155 done 156 for p in "$@"; do serial_has "${p}" && return 0; done 157 return 1 158 } 159 160 stop_qemu() { 161 if [[ -n "${qemu_pid}" ]]; then 162 kill "${qemu_pid}" 2>/dev/null || true 163 wait "${qemu_pid}" 2>/dev/null || true 164 fi 165 qemu_pid="" 166 exec 3>&- 2>/dev/null || true 167 } 168 169 boot_once() { 170 cp "${vars_fd}" "${work}/vars.fd" 171 rm -f "${work}/serial.log" "${work}/mon.in" "${work}/mon.out" 172 mkfifo "${work}/mon.in" "${work}/mon.out" 173 # Single-threaded TCG: multi-threaded TCG intermittently loses the 174 # firmware's timer wake-up and hangs before AIENOS output (#61). 175 local -a smmu_trace=() 176 if [[ "${AIENOS_QEMU_SMMU_TRACE:-0}" == "1" ]]; then 177 smmu_trace=(-d unimp,guest_errors -trace "smmuv3_*") 178 fi 179 # The NVMe namespace is a fresh image rebuilt above for every run; QEMU 180 # only ever sees reads from the guest driver. 181 qemu-system-aarch64 \ 182 "${smmu_trace[@]}" \ 183 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ 184 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ 185 -drive if=pflash,format=raw,file="${work}/vars.fd" \ 186 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ 187 -device virtio-blk-pci,drive=esp \ 188 -drive if=none,id=nvme0,format=raw,file="${image}" \ 189 -device nvme,drive=nvme0,serial=aienos-nvme-test \ 190 -device ramfb -display none -nic none \ 191 -chardev pipe,id=mon,path="${work}/mon" -mon chardev=mon,mode=readline \ 192 -serial file:"${work}/serial.log" -no-reboot & 193 qemu_pid=$! 194 # Drain monitor output so QEMU never blocks writing it. 195 cat "${work}/mon.out" >/dev/null & 196 exec 3>"${work}/mon.in" 197 198 if ! wait_for "${boot_timeout}" "report_kind: pre_exit"; then 199 echo "attempt ${attempt}: no AIENOS output within ${boot_timeout} s (firmware hang, issue #61)" 200 stop_qemu 201 return 2 202 fi 203 # The native NVMe phase runs after the final report, so the last marker is 204 # the terminal signal; a panic or fault also ends the phase. 205 wait_for "${boot_timeout}" "NVME_ERROR_QEMU: PASS" "report_kind: panic" "report_kind: fault" || true 206 local deadline=$(( $(date +%s) + 30 )) 207 while qemu_running && (( $(date +%s) < deadline )); do sleep 0.5; done 208 qemu_running && echo "QEMU still running after the NVMe phase" 209 stop_qemu 210 return 0 211 } 212 213 started=$(date +%s) 214 attempt=1 215 while :; do 216 status=0 217 boot_once || status=$? 218 [[ "${status}" != 2 || "${attempt}" -ge "${attempts}" ]] && break 219 attempt=$(( attempt + 1 )) 220 done 221 elapsed=$(( $(date +%s) - started )) 222 223 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 2>/dev/null || true 224 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_nvme_serial.log" 2>/dev/null || true 225 failed=0 226 check() { # description, pattern 227 if grep -q -- "$2" "${work}/serial.txt"; then 228 echo "PASS $1" 229 else 230 echo "FAIL $1" 231 failed=1 232 fi 233 } 234 check_absent() { # description, pattern 235 if grep -q -- "$2" "${work}/serial.txt"; then 236 echo "FAIL $1" 237 failed=1 238 else 239 echo "PASS $1" 240 fi 241 } 242 echo "attempts ${attempt}, ${elapsed} s (commit ${commit:0:12}), dma mode ${mode}" 243 244 # The six required native read markers. They must only appear once the 245 # NVMe function has been granted DMA; in fail-closed mode they must be absent. 246 if [[ "${mode}" != "fail-closed" ]]; then 247 check "ECAM discovery found the NVMe class device" "NVME_DISCOVERY_QEMU: PASS" 248 check "controller and namespace identify completed" "NVME_IDENTIFY_QEMU: PASS" 249 check "namespace geometry matches the image" \ 250 "NVME_GEOMETRY_QEMU: PASS (nsid=1 block_count=${bounds_lba} block_size=${lba_bytes})" 251 check "sentinel LBA read back exact bytes and SHA-256" \ 252 "NVME_READ_QEMU: PASS (lba=${sent_lba} blocks=1 bytes=512 sha256=${sentinel_sha})" 253 check "read past the namespace end is rejected before any command" "NVME_BOUNDS_QEMU: PASS" 254 check "device-reported command error is surfaced" "NVME_ERROR_QEMU: PASS" 255 fi 256 257 if [[ "${mode}" == "fail-closed" ]]; then 258 check "discovery found the device before the DMA gate" "NVME_DISCOVERY_QEMU: PASS" 259 check "NVMe DMA denied without an SMMU" "dma_gate: nvme denied (NoSmmu), bus master stays off" 260 check "NVMe controller fail-closed without an SMMU" "nvme: unavailable (SMMU DMA isolation not active)" 261 check_absent "NVMe controller never granted DMA" "dma_gate: nvme granted" 262 check_absent "NVMe controller never identified" "NVME_IDENTIFY_QEMU" 263 check_absent "NVMe read never ran" "NVME_READ_QEMU" 264 else 265 if [[ "${mode}" == "smmu" ]]; then 266 check "IORT stream configured for NVMe DMA" "smmu: enabled" 267 check "NVMe DMA window translated" "smmu_dma_window: nvme only, translation active" 268 check "NVMe DMA granted only as confined" "dma_gate: nvme granted (Confined), bus master on" 269 else 270 check "unsafe bypass build announced on serial" "WARNING: UNSAFE NVME DMA BYPASS BUILD" 271 check "unsafe bypass grant announced on serial" "WARNING: UNSAFE NVME DMA BYPASS ACTIVE" 272 check "NVMe DMA granted through the unsafe bypass" "dma_gate: nvme granted (UnsafeBypass)" 273 fi 274 check "NVMe bus master revoked after the read phase" "dma_gate: nvme bus master revoked" 275 fi 276 if [[ "${mode}" != "unsafe-bypass" ]]; then 277 check_absent "no unsafe DMA bypass in this image" "UNSAFE NVME DMA BYPASS" 278 fi 279 if grep -qE "report_kind: (panic|fault)" "${work}/serial.txt"; then 280 echo "FAIL panic or fault reported" 281 failed=1 282 fi 283 284 if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then 285 echo "---- serial console ----" 286 cat "${work}/serial.txt" 287 fi 288 if [[ "${mode}" == "unsafe-bypass" ]]; then 289 echo "WARNING: this was an UNSAFE NVME DMA BYPASS run (no SMMU confinement)." 290 fi 291 [[ "${failed}" == 0 ]] && echo "QEMU_NVME: PASS (${mode})" || { echo "QEMU_NVME: FAIL (${mode})"; exit 1; }