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_ck_keyboard_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_ck_keyboard_test.sh` 529 lines ยท 29666 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 # KEYBOARD gate for the AIENOS C kernel: rows 25-32 (C-only 30a-30c, 32a, 32b) of native/kernel/GATES.md 18 # "SEED-0A keyboard" (Rust oracle: scripts/qemu_keyboard_test.sh with SMMU=1 19 # and SMMU=0) plus the C-only rows 30a-30c (recovery access, line overflow). 20 # QEMU is not hardware: a PASS here qualifies nothing physical. 21 # 22 # Boots the full C image (make full) in QEMU AArch64 with UEFI (AAVMF), an 23 # NVMe disk (normal full boot), a qemu-xhci controller with a usb-kbd on it 24 # (the Rust gate's devices) and no NIC. The devices stage 25 # (native/kernel/dev/devices.c) runs the post-exit bus-master sweep first and 26 # the xHCI DMA fence (native/kernel/dev/xhci_fence.c) last; inside the 27 # confined grant the operator phase (native/kernel/dev/usb_kbd.c) drives the 28 # keyboard. Keys are typed from OUTSIDE the guest through the QEMU monitor 29 # ("sendkey", as the Rust gate does); the kernel never fakes the device. 30 # 31 # boot kbd-smmu iommu=smmuv3 (Rust SMMU=1). No key in the recovery 32 # window (normal boot by timeout), then the lines "abc", 33 # "help", "el", "mem", 65 x "x" (overflow) and "exit": 34 # row 25 "keyboard: xhci SSSS:BB:DD.F mmio 0x.." (found on the ECAM walk) 35 # row 26 "dma_sweep: seg 0000 bus .." before the first device DMA grant, 36 # no "dma_sweep: bme STUCK" 37 # row 27 "xhci_pci: command=0x.... bus_master=off" before the xHCI DMA gate 38 # row 29 "keyboard: ready", "keyboard_echo: abc", "keyboard_line: abc", 39 # "keyboard: done (enter)", all between the confined grant and the 40 # revoke 41 # row 30 "commands: help mem el report uptime exit", "EL1", 42 # "conventional_memory_kb: N" (N > 0), "keyboard: done (exit)" 43 # row 30a (C only) recovery access offered after "keyboard: ready" and 44 # decided "choice=normal reason=timeout" before the shell starts 45 # row 30c (C only) the 65-key line is refused ("keyboard_line: overflow 46 # (line refused: 65 keys typed, limit 64)"), never echoed back as a 47 # line and never run as a command 48 # row 31 grant confined ("smmu_dma_window: xhci only, translation active", 49 # "dma_gate: xhci granted (Confined), bus master on"), controller 50 # halted, "dma_gate: xhci bus master revoked", COMMAND read back 51 # "xhci_pci: after phase command=0x.... bus_master=off", "smmu: xhci 52 # stream 0x.. returned to abort (rc=0)", in that order, all before 53 # the "devices: xhci=fenced (rc=0)" line 54 # row 32 no "report_kind: (panic|fault)", "report_kind: final", QEMU exit 0 55 # row 32a (C only, every boot; NEXT-PHASE-3 cut 2) the ACPI platform walker runs on 56 # QEMU's own tables: "acpi_scan: tables=N refused=0 57 # first_refusal=0 devices=M" with N, M > 0, the positive control 58 # "acpi_scan: control ARMH0011 mmio=0x9000000+0x1000" (the 59 # QEMU virt PL011 UART), "xhci_acpi: 0 platform controller(s)" (QEMU 60 # virt describes no platform xHCI), and the PCI path is kept: no 61 # "xhci_plat:" line and no fallback to platform controllers 62 # row 32b (C only) hardware expectation, always NOT_RUN here: the exact lines the 63 # DGX Spark should print, predicted by tools/ck_acpi_scan.c from 64 # its firmware tables (native/kernel/tests/fixtures/ 65 # spark_xhci_acpi_expected.txt); never counted toward the verdict 66 # boot kbd-nosmmu no SMMU (Rust SMMU=0), same image, no keys: 67 # rows 25, 26, 27, 32, 32a as above 68 # row 28 "dma_gate: xhci denied (NoSmmu), bus master stays off", "keyboard: 69 # unavailable (SMMU DMA isolation not active)", COMMAND read back 70 # after the deny with bus_master=off, no "dma_gate: xhci granted", 71 # no "keyboard: ready" 72 # row 30a recovery access reported unavailable, normal boot 73 # ("choice=normal reason=no-keyboard") 74 # boot kbd-recovery iommu=smmuv3, same image; "r" typed in the recovery window: 75 # row 30b (C only) "recovery_access: choice=recovery reason=key-r"; then, in 76 # order: xHCI bus master revoked, xHCI stream back to abort, 77 # "devices: recovery halt nvme=released virtio_net=released 78 # xhci=released", the stub's identity line (the SHA-256 this script 79 # recomputes from the commit), "recovery_console: halted"; no shell, 80 # no Store stage, no final report, no panic or fault, and QEMU still 81 # running 3 s after the halt line (halted, not reset; the script 82 # then stops QEMU) 83 # kbd-smmu and kbd-nosmmu must also pass every M1 check 84 # (scripts/lib_ck_m1_checks.sh); all boots carry no TEST-ONLY mutation 85 # banner, no "UNSAFE DMA BYPASS" and no "keyboard: hid NOT_IMPLEMENTED". 86 # 87 # --mutation (self-test of the DMA checks; TEST-ONLY images): builds 88 # "make full CK_TEST_XHCI_MUTATION=" for each mutation and boots it once 89 # without keys; the named row must FAIL (and the boot must still reach 90 # "report_kind: final" with row 25 PASS, so the failure is the mutation's, 91 # not a dead boot): 92 # bm-left-on xHCI bus mastering switched back on after the sweep (smmu) -> row 27 93 # no-revoke release skips the bus-master clear (smmu) -> row 31 94 # grant-no-smmu DMA granted without an SMMU (nosmmu) -> row 28 95 # no-sweep post-exit bus-master sweep skipped (nosmmu) -> row 26 96 # It also checks the build refuses a mutation together with 97 # CK_HARDWARE_STAGING=1 (and with CK_QEMU_UNSAFE_DMA=1), and that the default 98 # image carries no mutation banner. 99 # 100 # Env: AIENOS_QEMU_SMMU=1 runs only kbd-smmu and kbd-recovery, =0 only 101 # kbd-nosmmu (a partial run: the gate verdict is then NOT_RUN). 102 # Like the FPU/INFER/SCREEN children: NOT_RUN (exit 3) while the machine quiet 103 # flag (AIENOS_QUIET_FLAG, default ~/workspace/.spark-quiet) exists, which this 104 # script only reads and never writes, or while the QEMU gate lock 105 # (AIENOS_GATE_LOCK, default ~/workspace/.qemu-gate-lock, exclusive create) is 106 # held by another gate run. 107 # Final lines (normal run): one "KEYBOARD_ROW : PASS|FAIL|NOT_RUN (...)" 108 # per row, "AIENOS_CK_KEYBOARD_DMA: PASS|FAIL (rows 25-28,31,32)" and 109 # "AIENOS_CK_KEYBOARD: PASS|FAIL|NOT_RUN (...)" (read by scripts/ck_gates.sh). 110 # Exit 0 when the gate PASSes, 1 FAIL, 2 missing tools, 3 NOT_RUN. 111 # --mutation: "AIENOS_CK_KEYBOARD_MUTATION: PASS|FAIL"; exit 0 / 1. 112 # Needs qemu-system-aarch64, AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64) 113 # and a C compiler. 114 set -euo pipefail 115 116 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 117 cd "${repo_root}" 118 119 mutation_run=0 120 case "${1:-}" in 121 "") ;; 122 --mutation) mutation_run=1 ;; 123 *) echo "usage: $0 [--mutation]"; exit 2 ;; 124 esac 125 final_tag="AIENOS_CK_KEYBOARD" 126 [[ "${mutation_run}" == 0 ]] || final_tag="AIENOS_CK_KEYBOARD_MUTATION" 127 128 code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" 129 vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" 130 command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${final_tag}: NOT_RUN (qemu-system-aarch64 missing)"; exit 2; } 131 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${final_tag}: NOT_RUN (AAVMF missing)"; exit 2; } 132 133 cross="" 134 if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi 135 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 136 out="${repo_root}/target/native-kernel" 137 mk() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@"; } 138 mk full >/dev/null 139 default_efi="${out}/full/BOOTAA64.EFI" 140 mutations=(bm-left-on no-revoke grant-no-smmu no-sweep) 141 if [[ "${mutation_run}" == 1 ]]; then 142 for m in "${mutations[@]}"; do mk full CK_TEST_XHCI_MUTATION="${m}" >/dev/null; done 143 fi 144 145 # Machine-wide quiet flag: read only, never written here (Drake's rule: no 146 # agent raises it without approval). If someone holds it: NOT_RUN. 147 # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): 148 # scripts/lib_gate_hold.sh (aienos#278). 149 . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" 150 if ! gh_quiet_check; then 151 echo "NOT_RUN ${gh_why}" 152 echo "${final_tag}: NOT_RUN (quiet flag held)" 153 exit 3 154 fi 155 if ! gh_lock_take qemu_ck_keyboard_test "${AIENOS_GATE_MINUTES:-60}"; then 156 echo "NOT_RUN ${gh_why}" 157 echo "${final_tag}: NOT_RUN (QEMU gate lock held)" 158 exit 3 159 fi 160 # Release only this run's own gate lock record, at most once. 161 release_lock() { gh_lock_release; } 162 top="$(mktemp -d)" 163 qemu_pid="" 164 cleanup() { 165 [[ -z "${qemu_pid}" ]] || kill "${qemu_pid}" 2>/dev/null || true 166 exec 3>&- 2>/dev/null || true 167 rm -rf "${top}" 168 release_lock 169 } 170 trap cleanup EXIT 171 172 # shellcheck source=scripts/lib_ck_m1_checks.sh 173 source "${repo_root}/scripts/lib_ck_m1_checks.sh" 174 img_bytes=`67108864` 175 image="${top}/nvme.img" 176 truncate -s "${img_bytes}" "${image}" 177 m1_fail=0 178 boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}" 179 overflow_keys=65 180 181 serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -qE -- "$1"; } 182 qemu_running() { [[ -n "${qemu_pid}" ]] && kill -0 "${qemu_pid}" 2>/dev/null; } 183 # wait_for seconds ERE...: 0 once the serial log matches one, 1 on timeout or QEMU exit. 184 wait_for() { 185 local deadline=$(( $(date +%s) + $1 )) p; shift 186 while (( $(date +%s) < deadline )) && qemu_running; do 187 for p in "$@"; do serial_has "${p}" && return 0; done 188 sleep 0.2 189 done 190 for p in "$@"; do serial_has "${p}" && return 0; done 191 return 1 192 } 193 # Keys go in through the QEMU monitor (outside the guest), 0.12 s apart. 194 send_keys() { local k; for k in "$@"; do echo "sendkey ${k}" >&3; sleep 0.12; done; } 195 send_line() { send_keys "$@" ret; sleep 0.5; } 196 197 # boot smmu|nosmmu : one QEMU boot; serial text in 198 # ${work}/serial.txt, QEMU status in qemu_status, for recovery boots 199 # halted_running=yes|no. action: none | shell | recovery. 200 boot() { 201 work="${top}/$1" 202 local action="$4" 203 mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" 204 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" 205 cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" 206 cp "${vars_fd}" "${work}/vars.fd" 207 rm -f "${work}/mon.in" "${work}/mon.out" 208 mkfifo "${work}/mon.in" "${work}/mon.out" 209 : >"${work}/serial.log" 210 local machine="virt,virtualization=on,gic-version=3" 211 [[ "$3" != smmu ]] || machine+=",iommu=smmuv3" 212 # Issue #61: single-threaded TCG (see qemu_boot_test.sh). 213 timeout "${boot_timeout}" qemu-system-aarch64 \ 214 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ 215 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ 216 -drive if=pflash,format=raw,file="${work}/vars.fd" \ 217 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ 218 -device virtio-blk-pci,drive=esp \ 219 -drive if=none,id=nvme0,format=raw,file="${image}" \ 220 -device nvme,drive=nvme0,serial=aienos-kbd-test \ 221 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0 \ 222 -device ramfb -display none -nic none \ 223 -chardev pipe,id=mon,path="${work}/mon" -mon chardev=mon,mode=readline \ 224 -serial file:"${work}/serial.log" -no-reboot 2>"${work}/qemu.err" & 225 qemu_pid=$! 226 cat "${work}/mon.out" >/dev/null & # drain monitor output so QEMU never blocks 227 exec 3>"${work}/mon.in" 228 halted_running=no 229 case "${action}" in 230 shell) 231 # No key in the recovery window: the boot must continue by timeout. 232 if wait_for "${boot_timeout}" '^keyboard: shell ready' '^keyboard: unavailable' '^report_kind: (panic|fault|final)'; then 233 if serial_has '^keyboard: shell ready'; then 234 send_line a b c 235 send_line h e l p 236 send_line e l 237 send_line m e m 238 local -a xs=() 239 for ((i = 0; i < overflow_keys; i++)); do xs+=(x); done 240 send_line "${xs[@]}" 241 send_line e x i t 242 fi 243 fi ;; 244 recovery) 245 if wait_for "${boot_timeout}" '^recovery_access: waiting' '^recovery_access: unavailable' '^report_kind: (panic|fault|final)'; then 246 if serial_has '^recovery_access: waiting'; then 247 send_keys r 248 if wait_for 60 '^recovery_console: halted' '^report_kind: (panic|fault|final)' && serial_has '^recovery_console: halted'; then 249 sleep 3 250 qemu_running && halted_running=yes 251 fi 252 fi 253 fi 254 kill "${qemu_pid}" 2>/dev/null || true ;; 255 esac 256 set +e 257 wait "${qemu_pid}" 258 qemu_status=$? 259 set -e 260 qemu_pid="" 261 exec 3>&- 2>/dev/null || true 262 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 263 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_keyboard_$1.log" 264 echo "== boot $1 ($3, keys: ${action}): qemu exit ${qemu_status}$([[ "${action}" != recovery ]] || echo ", halted_running=${halted_running}")" 265 } 266 267 # Row bookkeeping: rowfail[n]=1 once any check of row n failed in this 268 # evaluation; rowmodes[n] lists the boots that checked it. 269 all_rows=(25 26 27 28 29 30 30a 30b 30c 31 32 32a) 270 declare -A rowfail rowmodes 271 reset_rows() { local r; for r in "${all_rows[@]}"; do rowfail[$r]=0; rowmodes[$r]=""; done; } 272 reset_rows 273 mode="" 274 note_mode() { [[ " ${rowmodes[$1]} " == *" ${mode} "* ]] || rowmodes[$1]="${rowmodes[$1]:+${rowmodes[$1]} }${mode}"; } 275 rpass() { note_mode "$1"; echo "ROW $1 PASS ${mode}: $2"; } 276 rfail() { note_mode "$1"; rowfail[$1]=1; echo "ROW $1 FAIL ${mode}: $2"; } 277 rcheck() { # row, description, ERE pattern 278 if grep -qE -- "$3" "${work}/serial.txt"; then rpass "$1" "$2"; else rfail "$1" "$2"; fi 279 } 280 rabsent() { # row, description, ERE pattern 281 if grep -qE -- "$3" "${work}/serial.txt"; then rfail "$1" "$2"; else rpass "$1" "$2"; fi 282 } 283 line_of() { { grep -nE -m1 -- "$1" "${work}/serial.txt" || true; } | cut -d: -f1; } 284 before() { # row, description, earlier ERE, later ERE: both present, earlier first 285 local a b 286 a="$(line_of "$3")"; b="$(line_of "$4")" 287 if [[ -n "${a}" && -n "${b}" && "${a}" -lt "${b}" ]]; then rpass "$1" "$2 (lines ${a} < ${b})" 288 else rfail "$1" "$2 (lines ${a:-none} ${b:-none})"; fi 289 } 290 291 # evaluate_dma smmu|nosmmu|recovery: rows 25-28 and 31 on ${work}/serial.txt. 292 evaluate_dma() { 293 mode="$1" 294 rcheck 25 "xHCI found on the ECAM walk (Rust: keyboard: xhci )" \ 295 '^keyboard: xhci 0000:[0-9a-f]{2}:[0-9a-f]{2}\.[0-7] mmio 0x[0-9a-f]+ ' 296 evaluate_acpi 297 rcheck 26 "post-exit bus-master sweep of the segment" \ 298 '^dma_sweep: seg 0000 bus [0-9a-f]{2}-[0-9a-f]{2} functions=[1-9][0-9]* bridges=[0-9]+ bridges_bme=[0-9]+ endpoints_bme_found=[0-9]+ still_enabled=0$' 299 rabsent 26 "no endpoint left with bus master stuck on" '^dma_sweep: bme STUCK' 300 before 26 "sweep before the first device DMA gate" '^dma_sweep: seg ' '^dma_gate: ' 301 before 26 "sweep before the xHCI is looked at" '^dma_sweep: seg ' '^keyboard: xhci ' 302 rcheck 27 "xHCI bus master off before the DMA gate (Rust: xhci_pci: command=0x.. bus_master=off)" \ 303 '^xhci_pci: command=0x[0-9a-f]{4} bus_master=off$' 304 before 27 "COMMAND read back before the xHCI DMA gate" '^xhci_pci: command=' '^dma_gate: xhci ' 305 if [[ "${mode}" == nosmmu ]]; then 306 rcheck 28 "xHCI DMA denied without an SMMU" '^dma_gate: xhci denied \(NoSmmu\), bus master stays off$' 307 rcheck 28 "keyboard fail-closed without an SMMU" '^keyboard: unavailable \(SMMU DMA isolation not active\)$' 308 rcheck 28 "COMMAND read back after the deny: bus master off" '^xhci_pci: after deny command=0x[0-9a-f]{4} bus_master=off$' 309 rabsent 28 "xHCI never granted DMA" 'dma_gate: xhci granted' 310 rabsent 28 "keyboard never attached" 'keyboard: ready' 311 rabsent 28 "no xHCI SMMU window claimed without an SMMU" '^smmu_dma_window: xhci' 312 rcheck 28 "devices stage reports the deny" '^devices: xhci=denied \(rc=-603\)$' 313 else 314 rcheck 31 "xHCI SMMU window (Rust: smmu_dma_window: xhci only, translation active)" \ 315 '^smmu_dma_window: xhci only, translation active iova=0x[0-9a-f]+ len=0x[0-9a-f]+ rid=0x[0-9a-f]+$' 316 rcheck 31 "xHCI DMA granted only as confined" '^dma_gate: xhci granted \(Confined\), bus master on$' 317 rabsent 31 "no unconfined xHCI grant" 'dma_gate: xhci granted \((UnsafeBypass|TestMutation)' 318 rcheck 31 "controller halted before the revoke" '^xhci: halt before revoke usbcmd=0x[0-9a-f]{8} usbsts=0x[0-9a-f]{8} halted=yes$' 319 rcheck 31 "xHCI bus master revoked after the phase" '^dma_gate: xhci bus master revoked$' 320 rcheck 31 "COMMAND read back after the phase: bus master off" '^xhci_pci: after phase command=0x[0-9a-f]{4} bus_master=off$' 321 rcheck 31 "xHCI SMMU stream returned to abort" '^smmu: xhci stream 0x[0-9a-f]+ returned to abort \(rc=0\)$' 322 rabsent 31 "no revoke failure" '^dma_gate: xhci bus master revoke FAILED' 323 before 31 "order: grant < revoke" '^dma_gate: xhci granted \(Confined\)' '^dma_gate: xhci bus master revoked$' 324 before 31 "order: revoke < COMMAND read back" '^dma_gate: xhci bus master revoked$' '^xhci_pci: after phase command=' 325 before 31 "order: read back < stream abort" '^xhci_pci: after phase command=' '^smmu: xhci stream .* returned to abort' 326 before 31 "order: stream abort < end of the xHCI phase (not a reset-time revoke)" \ 327 '^smmu: xhci stream .* returned to abort' '^devices: xhci=fenced \(rc=0\)$' 328 fi 329 } 330 331 # evaluate_acpi: row 32a (every boot). QEMU virt has no platform xHCI, so the 332 # walker must find its tables, the UART control and zero controllers, and the 333 # PCI xHCI path must not fall back to the platform list. 334 evaluate_acpi() { 335 rcheck 32a "ACPI walker read QEMU's DSDT/SSDTs with no refusal" \ 336 '^acpi_scan: tables=[1-9][0-9]* refused=0 first_refusal=0 devices=[1-9][0-9]* \(static scan; _STA not evaluated\)$' 337 rcheck 32a "positive control: QEMU virt UART ARMH0011 at 0x9000000" \ 338 '^acpi_scan: control ARMH0011 [A-Z0-9_]{1,4} mmio=0x9000000\+0x1000$' 339 rcheck 32a "no ACPI platform xHCI on QEMU virt" '^xhci_acpi: 0 platform controller\(s\)$' 340 rabsent 32a "PCI path kept: no platform controller tried" '^(xhci_plat: |keyboard: (no PCI xHCI controller|none on the PCI xHCI))' 341 } 342 343 # evaluate_final smmu|nosmmu: row 32 for a boot that must end with the final report. 344 evaluate_final() { 345 mode="$1" 346 rabsent 32 "no panic or fault" '^report_kind: (panic|fault)' 347 rcheck 32 "final report reached" 'report_kind: final' 348 if [[ "${qemu_status}" == 0 ]]; then rpass 32 "QEMU exit 0 after PSCI reset"; else rfail 32 "QEMU exit ${qemu_status} (expected 0)"; fi 349 } 350 351 # evaluate_input smmu|nosmmu: rows 29, 30, 30a, 30c. 352 evaluate_input() { 353 mode="$1" 354 if [[ "${mode}" == nosmmu ]]; then 355 rcheck 30a "recovery access reported unavailable, normal boot" \ 356 '^recovery_access: unavailable \(no operator keyboard: xhci=denied\); choice=normal reason=no-keyboard$' 357 rabsent 30a "no recovery wait without a keyboard" '^recovery_access: waiting' 358 return 359 fi 360 rcheck 29 "keyboard attached by the C driver (Rust: keyboard: ready)" '^keyboard: ready \(port [0-9]+, slot [0-9]+, endpoint 0x8[0-9a-f]\)$' 361 before 29 "keyboard attached inside the confined grant" '^dma_gate: xhci granted \(Confined\)' '^keyboard: ready' 362 rcheck 29 "typed text echoed (Rust: keyboard_echo: abc)" '^keyboard_echo: abc$' 363 rcheck 29 "line ended by Enter and reported (Rust: keyboard_line: abc)" '^keyboard_line: abc$' 364 rcheck 29 "keyboard phase line finished on Enter (Rust: keyboard: done (enter))" '^keyboard: done \(enter\)$' 365 before 29 "typed line before the revoke" '^keyboard_line: abc$' '^dma_gate: xhci bus master revoked$' 366 rcheck 30 "help command output" '^commands: help mem el report uptime exit$' 367 rcheck 30 "EL command output" '^EL1$' 368 rcheck 30 "memory command output" '^conventional_memory_kb: [1-9][0-9]*$' 369 rcheck 30 "keyboard phase finished on exit" '^keyboard: done \(exit\)$' 370 before 30 "exit before the revoke" '^keyboard: done \(exit\)$' '^dma_gate: xhci bus master revoked$' 371 rcheck 30a "recovery window offered" '^recovery_access: waiting [0-9]+ ms for an operator key' 372 rcheck 30a "no key: normal boot by timeout" '^recovery_access: choice=normal reason=timeout$' 373 before 30a "keyboard ready < recovery window" '^keyboard: ready' '^recovery_access: waiting' 374 before 30a "recovery decision < shell" '^recovery_access: choice=' '^keyboard: shell ready' 375 rabsent 30a "no recovery console on a normal boot" '^recovery_console:' 376 rcheck 30c "overlong line refused (fail closed)" \ 377 "^keyboard_line: overflow \\(line refused: ${overflow_keys} keys typed, limit 64\\)\$" 378 rabsent 30c "overlong line never reported as a line" '^keyboard_line: x' 379 rabsent 30c "overlong line never run as a command" '^unknown command: x' 380 } 381 382 # evaluate_recovery: rows 30b, 31 and the recovery boot's no-panic check. 383 evaluate_recovery() { 384 mode=recovery 385 local want 386 want="$(printf 'AIENOS-CK-RECOVERY-IDENTITY-V1\0%s' "${commit}" | sha256sum | cut -d' ' -f1)" 387 rcheck 29 "keyboard attached by the C driver" '^keyboard: ready \(port [0-9]+, slot [0-9]+, endpoint 0x8[0-9a-f]\)$' 388 rcheck 30b "operator key r selects the recovery console" '^recovery_access: choice=recovery reason=key-r$' 389 before 30b "order: choice < xHCI revoke" '^recovery_access: choice=recovery' '^dma_gate: xhci bus master revoked$' 390 before 30b "order: xHCI stream abort < recovery halt line" '^smmu: xhci stream .* returned to abort \(rc=0\)$' '^devices: recovery halt ' 391 rcheck 30b "every device DMA released before the halt" '^devices: recovery halt nvme=released virtio_net=released xhci=released$' 392 rcheck 30b "stub prints the identity digest (recomputed here from the commit)" \ 393 "^recovery_console: identity build_sha256=${want} commit=${commit} " 394 before 30b "order: recovery halt line < identity" '^devices: recovery halt ' '^recovery_console: identity ' 395 before 30b "order: identity < halted" '^recovery_console: identity ' '^recovery_console: halted' 396 rabsent 30b "no shell on the recovery path" '^keyboard: shell ready' 397 rabsent 30b "no Store stage on the recovery path" '^stage store' 398 rabsent 30b "no final report on the recovery path" 'report_kind: final' 399 rabsent 30b "no panic or fault" '^report_kind: (panic|fault)' 400 if [[ "${halted_running}" == yes ]]; then rpass 30b "QEMU still running 3 s after the halt line (halted, not reset)" 401 else rfail 30b "QEMU was not running after the halt line (or the halt line never came)"; fi 402 } 403 404 image_checks() { 405 check_absent "no TEST-ONLY mutation banner in the default image" "TEST-ONLY xHCI MUTATION" 406 check_absent "no unsafe DMA bypass in this image" "UNSAFE DMA BYPASS" 407 check_absent "no leftover NOT_IMPLEMENTED HID marker" "^keyboard: hid NOT_IMPLEMENTED" 408 check "image is this commit" "aienos_commit: ${commit}" 409 } 410 411 if [[ "${mutation_run}" == 0 ]]; then 412 modes=(smmu nosmmu recovery) 413 case "${AIENOS_QEMU_SMMU:-}" in 414 1) modes=(smmu recovery) ;; 415 0) modes=(nosmmu) ;; 416 "") ;; 417 *) echo "AIENOS_QEMU_SMMU must be 1, 0 or unset"; release_lock; echo "${final_tag}: NOT_RUN (bad AIENOS_QEMU_SMMU)"; exit 2 ;; 418 esac 419 other_fail=0 420 for md in "${modes[@]}"; do 421 case "${md}" in 422 smmu) boot kbd-smmu "${default_efi}" smmu shell ;; 423 nosmmu) boot kbd-nosmmu "${default_efi}" nosmmu none ;; 424 recovery) boot kbd-recovery "${default_efi}" smmu recovery ;; 425 esac 426 failed=0 427 if [[ "${md}" != recovery ]]; then 428 ck_m1_checks 429 [[ "${failed}" == 0 ]] || m1_fail=1 430 fi 431 failed=0 432 image_checks 433 [[ "${failed}" == 0 ]] || other_fail=1 434 failed=0 435 evaluate_dma "${md}" 436 if [[ "${md}" == recovery ]]; then 437 evaluate_recovery 438 else 439 evaluate_input "${md}" 440 evaluate_final "${md}" 441 fi 442 echo "RECOVERY_CHOICE kbd-${md}: $(grep -m1 -E '^recovery_access: (choice=|unavailable)' "${work}/serial.txt" || echo 'none recorded')" 443 done 444 release_lock 445 any_fail=0 446 for r in "${all_rows[@]}"; do any_fail=$(( any_fail | rowfail[$r] )); done 447 if [[ "${m1_fail}${other_fail}" != 00 || -n "${AIENOS_QEMU_VERBOSE:-}" || "${any_fail}" != 0 ]]; then 448 for s in "${top}"/*/serial.txt; do 449 echo "---- serial console $(basename "$(dirname "${s}")") (device lines) ----" 450 grep -E '^(keyboard|xhci|xhci_pci|dma_sweep|dma_gate|smmu|devices:|stage |report_kind:|recovery_|commands|EL[0-9]|conventional)' "${s}" | head -80 || true 451 done 452 fi 453 dma_fail=$(( m1_fail | other_fail )) 454 gate_fail=$(( m1_fail | other_fail )) 455 partial=0 456 [[ -z "${AIENOS_QEMU_SMMU:-}" ]] || partial=1 457 echo "== per-row results (QEMU only; hardware NOT_RUN)" 458 for r in "${all_rows[@]}"; do 459 if [[ -z "${rowmodes[$r]}" ]]; then 460 echo "KEYBOARD_ROW ${r}: NOT_RUN (not checked in this run's modes)" 461 partial=1 462 elif [[ "${rowfail[$r]}" == 0 && "${m1_fail}" == 0 ]]; then 463 echo "KEYBOARD_ROW ${r}: PASS (${rowmodes[$r]})" 464 else 465 echo "KEYBOARD_ROW ${r}: FAIL (${rowmodes[$r]}$([[ "${m1_fail}" == 0 ]] || echo '; an M1 check failed'))" 466 gate_fail=1 467 case "${r}" in 25|26|27|28|31|32) dma_fail=1 ;; esac 468 fi 469 done 470 echo "KEYBOARD_ROW 32b: NOT_RUN (hardware only; expected DGX Spark lines: native/kernel/tests/fixtures/spark_xhci_acpi_expected.txt)" 471 [[ "${m1_fail}" == 0 ]] || echo "M1 checks failed on at least one boot: no row can pass" 472 [[ "${other_fail}" == 0 ]] || echo "image checks failed (mutation banner, unsafe bypass, HID marker or commit)" 473 if [[ "${dma_fail}" == 0 ]]; then echo "AIENOS_CK_KEYBOARD_DMA: PASS (rows 25-28,31,32; modes ${modes[*]})"; else echo "AIENOS_CK_KEYBOARD_DMA: FAIL (rows 25-28,31,32; modes ${modes[*]})"; fi 474 if [[ "${gate_fail}" != 0 ]]; then 475 echo "${final_tag}: FAIL (rows 25-32,30a-30c,32a; modes ${modes[*]}; QEMU only)" 476 exit 1 477 elif [[ "${partial}" != 0 ]]; then 478 echo "${final_tag}: NOT_RUN (partial run, modes ${modes[*]}; every boot is needed for the gate)" 479 exit 3 480 fi 481 echo "${final_tag}: PASS (rows 25-32,30a-30c,32a; boots kbd-smmu kbd-nosmmu kbd-recovery; QEMU only, hardware NOT_RUN)" 482 exit 0 483 fi 484 485 # ---- --mutation: every mutation must make its row FAIL ---- 486 mut_fail=0 487 mut_ok() { echo "MUTATION PASS $1"; } 488 mut_bad() { echo "MUTATION FAIL $1"; mut_fail=1; } 489 if grep -aqF "TEST-ONLY xHCI MUTATION" "${default_efi}"; then mut_bad "default image carries the mutation banner"; else mut_ok "default image carries no mutation banner"; fi 490 for combo in "CK_HARDWARE_STAGING=1" "CK_QEMU_UNSAFE_DMA=1"; do 491 set +e 492 msg="$(make -s -n -C native/kernel OUT="${top}/refuse" full CK_TEST_XHCI_MUTATION=no-revoke "${combo}" 2>&1)" 493 st=$? 494 set -e 495 if [[ "${st}" != 0 ]] && grep -qE "CK_TEST_XHCI_MUTATION.*cannot be combined with ${combo%%=*}" <<<"${msg}"; then 496 mut_ok "build refuses CK_TEST_XHCI_MUTATION with ${combo}" 497 else 498 mut_bad "build did not refuse CK_TEST_XHCI_MUTATION with ${combo} (status ${st}): $(head -c 200 <<<"${msg}")" 499 fi 500 done 501 declare -A mut_mode=([bm-left-on]=smmu [no-revoke]=smmu [grant-no-smmu]=nosmmu [no-sweep]=nosmmu) 502 declare -A mut_row=([bm-left-on]=27 [no-revoke]=31 [grant-no-smmu]=28 [no-sweep]=26) 503 for m in "${mutations[@]}"; do 504 efi="${out}/full-test-xhci-${m}/BOOTAA64.EFI" 505 boot "mut-${m}" "${efi}" "${mut_mode[$m]}" none 506 reset_rows 507 evaluate_dma "${mut_mode[$m]}" 508 evaluate_final "${mut_mode[$m]}" 509 r="${mut_row[$m]}" 510 if ! grep -qF "WARNING: TEST-ONLY xHCI MUTATION BUILD (CK_TEST_XHCI_MUTATION=${m}," "${work}/serial.txt"; then 511 mut_bad "${m}: image did not announce the mutation" 512 elif ! grep -q "report_kind: final" "${work}/serial.txt" || [[ "${rowfail[25]}" != 0 ]]; then 513 mut_bad "${m}: boot did not reach the final report with the xHCI found (a dead boot proves nothing)" 514 elif [[ "${rowfail[$r]}" == 1 ]]; then 515 mut_ok "${m} (${mut_mode[$m]}): row ${r} FAILs as it must" 516 else 517 mut_bad "${m} (${mut_mode[$m]}): row ${r} still PASSES: the check does not catch this mutation" 518 fi 519 done 520 release_lock 521 if [[ "${mut_fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then 522 for s in "${top}"/*/serial.txt; do 523 echo "---- serial console $(basename "$(dirname "${s}")") (device lines) ----" 524 grep -E '^(WARNING|keyboard|xhci|xhci_pci|dma_sweep|dma_gate|smmu|devices:|report_kind:)' "${s}" | head -60 || true 525 done 526 fi 527 if [[ "${mut_fail}" == 0 ]]; then echo "${final_tag}: PASS (bm-left-on->27 no-revoke->31 grant-no-smmu->28 no-sweep->26)"; exit 0; fi 528 echo "${final_tag}: FAIL" 529 exit 1