aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / qemu_keyboard_test.sh
| 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 | # SEED-0A `input.keyboard.usb` proof in QEMU (ADR 0012): boot the handoff |
| 18 | # image built with the `usb-keyboard` feature on a qemu-xhci controller with |
| 19 | # a usb-kbd, wait until the AIENOS driver reports the keyboard ready after |
| 20 | # ExitBootServices, type keys through the QEMU monitor (`sendkey`), and check |
| 21 | # that the same text comes back on the serial console. Emulator only: it says |
| 22 | # nothing about Machine 1 hardware. |
| 23 | # |
| 24 | # A boot that shows no AIENOS output at all is retried (firmware hang before |
| 25 | # AIENOS runs, issue #61); any failure after AIENOS output fails at once. |
| 26 | # |
| 27 | # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). |
| 28 | set -euo pipefail |
| 29 | |
| 30 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 31 | cd "${repo_root}" |
| 32 | |
| 33 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 34 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 35 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; } |
| 36 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; } |
| 37 | |
| 38 | keys=(a b c) |
| 39 | expected="abc" |
| 40 | attempts="${AIENOS_KEYBOARD_ATTEMPTS:-3}" |
| 41 | boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}" |
| 42 | machine="virt,virtualization=on,gic-version=3" |
| 43 | |
| 44 | # DMA mode (M3 rule: no SMMU confinement means no DMA). |
| 45 | # default QEMU SMMUv3 (iommu=smmuv3); the xHCI gets DMA |
| 46 | # only through a translated SMMU stream. |
| 47 | # AIENOS_QEMU_SMMU=0 no SMMU, normal build: the keyboard must stay |
| 48 | # unavailable with bus mastering off (fail-closed). |
| 49 | # AIENOS_UNSAFE_DMA_BYPASS=1 no SMMU, UNSAFE debug build that lets the xHCI |
| 50 | # DMA to physical memory unconfined. QEMU |
| 51 | # debugging only; never part of verify_all.sh. |
| 52 | bypass_feature="unsafe-debug-dma-without-smmu" |
| 53 | if [[ "${AIENOS_UNSAFE_DMA_BYPASS:-0}" == "1" ]]; then |
| 54 | mode="unsafe-bypass" |
| 55 | default_features="usb-keyboard,${bypass_feature}" |
| 56 | target_dir="target/qemu-keyboard-unsafe-dma-bypass" |
| 57 | echo "################################################################" |
| 58 | echo "WARNING: AIENOS_UNSAFE_DMA_BYPASS=1: building ${bypass_feature}." |
| 59 | echo "WARNING: xHCI DMA runs WITHOUT SMMU confinement. QEMU debug only." |
| 60 | echo "WARNING: this run proves nothing about DMA isolation." |
| 61 | echo "################################################################" |
| 62 | elif [[ "${AIENOS_QEMU_SMMU:-1}" == "1" ]]; then |
| 63 | mode="smmu" |
| 64 | machine+=",iommu=smmuv3" |
| 65 | default_features="usb-keyboard" |
| 66 | target_dir="target/qemu-keyboard" |
| 67 | else |
| 68 | mode="fail-closed" |
| 69 | default_features="usb-keyboard" |
| 70 | target_dir="target/qemu-keyboard" |
| 71 | fi |
| 72 | features="${AIENOS_BUILD_FEATURES:-$default_features}" |
| 73 | if [[ "${mode}" != "unsafe-bypass" && ",${features}," == *",${bypass_feature},"* ]]; then |
| 74 | echo "STOP: ${bypass_feature} requested without AIENOS_UNSAFE_DMA_BYPASS=1" |
| 75 | exit 2 |
| 76 | fi |
| 77 | |
| 78 | # Own target directory: a keyboard-enabled image never lands where hardware |
| 79 | # staging or the other tests pick up the handoff image. |
| 80 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 81 | AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 AIENOS_KEYBOARD_SECS=60 cargo build --quiet --release \ |
| 82 | -p aienos-boot --target aarch64-unknown-uefi --features "${features}" --bin aienos-handoff \ |
| 83 | --target-dir "${target_dir}" |
| 84 | |
| 85 | work="$(mktemp -d)" |
| 86 | qemu_pid="" |
| 87 | cleanup() { |
| 88 | [[ -n "${qemu_pid}" ]] && kill "${qemu_pid}" 2>/dev/null || true |
| 89 | exec 3>&- 2>/dev/null || true |
| 90 | rm -rf "${work}" |
| 91 | } |
| 92 | trap cleanup EXIT |
| 93 | mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" |
| 94 | touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" |
| 95 | cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" |
| 96 | |
| 97 | serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -q -- "$1"; } |
| 98 | qemu_running() { kill -0 "${qemu_pid}" 2>/dev/null; } |
| 99 | |
| 100 | # Wait until the serial log contains one of the patterns or QEMU exits. |
| 101 | wait_for() { # seconds, pattern... |
| 102 | local deadline=$(( $(date +%s) + $1 )); shift |
| 103 | while (( $(date +%s) < deadline )) && qemu_running; do |
| 104 | for p in "$@"; do serial_has "${p}" && return 0; done |
| 105 | sleep 0.2 |
| 106 | done |
| 107 | for p in "$@"; do serial_has "${p}" && return 0; done |
| 108 | return 1 |
| 109 | } |
| 110 | |
| 111 | stop_qemu() { |
| 112 | if [[ -n "${qemu_pid}" ]]; then |
| 113 | kill "${qemu_pid}" 2>/dev/null || true |
| 114 | wait "${qemu_pid}" 2>/dev/null || true |
| 115 | fi |
| 116 | qemu_pid="" |
| 117 | exec 3>&- 2>/dev/null || true |
| 118 | } |
| 119 | |
| 120 | boot_once() { |
| 121 | cp "${vars_fd}" "${work}/vars.fd" |
| 122 | rm -f "${work}/serial.log" "${work}/mon.in" "${work}/mon.out" |
| 123 | mkfifo "${work}/mon.in" "${work}/mon.out" |
| 124 | # Single-threaded TCG: multi-threaded TCG intermittently loses the |
| 125 | # firmware's timer wake-up and hangs before AIENOS output (#61). |
| 126 | local -a smmu_trace=() |
| 127 | if [[ "${AIENOS_QEMU_SMMU_TRACE:-0}" == "1" ]]; then |
| 128 | smmu_trace=(-d unimp,guest_errors -trace "smmuv3_*") |
| 129 | fi |
| 130 | qemu-system-aarch64 \ |
| 131 | "${smmu_trace[@]}" \ |
| 132 | -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ |
| 133 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 134 | -drive if=pflash,format=raw,file="${work}/vars.fd" \ |
| 135 | -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ |
| 136 | -device virtio-blk-pci,drive=esp \ |
| 137 | -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0 \ |
| 138 | -device ramfb -display none -nic none \ |
| 139 | -chardev pipe,id=mon,path="${work}/mon" -mon chardev=mon,mode=readline \ |
| 140 | -serial file:"${work}/serial.log" -no-reboot & |
| 141 | qemu_pid=$! |
| 142 | # Drain monitor output so QEMU never blocks writing it. |
| 143 | cat "${work}/mon.out" >/dev/null & |
| 144 | exec 3>"${work}/mon.in" |
| 145 | |
| 146 | if ! wait_for "${boot_timeout}" "report_kind: pre_exit"; then |
| 147 | echo "attempt ${attempt}: no AIENOS output within ${boot_timeout} s (firmware hang, issue #61)" |
| 148 | stop_qemu |
| 149 | return 2 |
| 150 | fi |
| 151 | if wait_for "${boot_timeout}" "keyboard: ready" "keyboard: unavailable" "report_kind: panic" "report_kind: fault" \ |
| 152 | && serial_has "keyboard: ready"; then |
| 153 | send_line() { |
| 154 | local key |
| 155 | for key in "$@"; do echo "sendkey ${key}" >&3; sleep 0.12; done |
| 156 | echo "sendkey ret" >&3 |
| 157 | sleep 0.5 |
| 158 | } |
| 159 | send_line "${keys[@]}" |
| 160 | send_line h e l p |
| 161 | send_line e l |
| 162 | send_line m e m |
| 163 | send_line e x i t |
| 164 | fi |
| 165 | local deadline=$(( $(date +%s) + 90 )) |
| 166 | while qemu_running && (( $(date +%s) < deadline )); do sleep 0.5; done |
| 167 | qemu_running && echo "QEMU still running after the keyboard phase" |
| 168 | stop_qemu |
| 169 | return 0 |
| 170 | } |
| 171 | |
| 172 | started=$(date +%s) |
| 173 | attempt=1 |
| 174 | while :; do |
| 175 | status=0 |
| 176 | boot_once || status=$? |
| 177 | [[ "${status}" != 2 || "${attempt}" -ge "${attempts}" ]] && break |
| 178 | attempt=$(( attempt + 1 )) |
| 179 | done |
| 180 | elapsed=$(( $(date +%s) - started )) |
| 181 | |
| 182 | tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 2>/dev/null || true |
| 183 | [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_$([[ "${AIENOS_QEMU_SMMU:-0}" == "1" ]] && echo smmu || echo keyboard)_serial.log" 2>/dev/null || true |
| 184 | failed=0 |
| 185 | check() { # description, pattern |
| 186 | if grep -q -- "$2" "${work}/serial.txt"; then |
| 187 | echo "PASS $1" |
| 188 | else |
| 189 | echo "FAIL $1" |
| 190 | failed=1 |
| 191 | fi |
| 192 | } |
| 193 | check_absent() { # description, pattern |
| 194 | if grep -q -- "$2" "${work}/serial.txt"; then |
| 195 | echo "FAIL $1" |
| 196 | failed=1 |
| 197 | else |
| 198 | echo "PASS $1" |
| 199 | fi |
| 200 | } |
| 201 | echo "attempts ${attempt}, ${elapsed} s (commit ${commit:0:12}), dma mode ${mode}, sent lines: abc, help, el, mem, exit" |
| 202 | check "left firmware and entered the kernel" "kernel: alive" |
| 203 | check "xHCI controller found before exit" "keyboard: xhci " |
| 204 | check "PCI segment swept for bus masters after exit" "dma_sweep: seg " |
| 205 | check_absent "no endpoint left with bus master stuck on" "dma_sweep: bme STUCK" |
| 206 | check "xHCI bus master off before the DMA gate" "xhci_pci: command=0x[0-9a-f]* bus_master=off" |
| 207 | if [[ "${mode}" == "fail-closed" ]]; then |
| 208 | check "xHCI DMA denied without an SMMU" "dma_gate: xhci denied (NoSmmu), bus master stays off" |
| 209 | check "keyboard fail-closed without an SMMU" "keyboard: unavailable (SMMU DMA isolation not active)" |
| 210 | check_absent "xHCI never granted DMA" "dma_gate: xhci granted" |
| 211 | check_absent "keyboard never attached" "keyboard: ready" |
| 212 | else |
| 213 | check "keyboard attached by the AIENOS driver" "keyboard: ready" |
| 214 | if [[ "${mode}" == "smmu" ]]; then |
| 215 | check "IORT stream configured for xHCI DMA" "smmu: enabled" |
| 216 | check "xHCI DMA window translated" "smmu_dma_window: xhci only, translation active" |
| 217 | check "xHCI DMA granted only as confined" "dma_gate: xhci granted (Confined), bus master on" |
| 218 | else |
| 219 | check "unsafe bypass build announced on serial" "WARNING: UNSAFE DMA BYPASS BUILD" |
| 220 | check "unsafe bypass grant announced on serial" "WARNING: UNSAFE DMA BYPASS ACTIVE" |
| 221 | check "xHCI DMA granted through the unsafe bypass" "dma_gate: xhci granted (UnsafeBypass)" |
| 222 | fi |
| 223 | check "typed text echoed on the serial console" "keyboard_echo: ${expected}" |
| 224 | check "line ended by Enter and reported" "keyboard_line: ${expected}\$" |
| 225 | check "keyboard phase finished on Enter" "keyboard: done (enter)" |
| 226 | check "help command output" "commands: help mem el report uptime exit" |
| 227 | check "EL command output" "EL1" |
| 228 | check "memory command output" "conventional_memory_kb:" |
| 229 | check "keyboard phase finished on exit" "keyboard: done (exit)" |
| 230 | check "xHCI bus master revoked after the keyboard phase" "dma_gate: xhci bus master revoked" |
| 231 | fi |
| 232 | if [[ "${mode}" != "unsafe-bypass" ]]; then |
| 233 | check_absent "no unsafe DMA bypass in this image" "UNSAFE DMA BYPASS" |
| 234 | fi |
| 235 | if grep -qE "report_kind: (panic|fault)" "${work}/serial.txt"; then |
| 236 | echo "FAIL panic or fault reported" |
| 237 | failed=1 |
| 238 | fi |
| 239 | |
| 240 | if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 241 | echo "---- serial console ----" |
| 242 | cat "${work}/serial.txt" |
| 243 | fi |
| 244 | if [[ "${mode}" == "unsafe-bypass" ]]; then |
| 245 | echo "WARNING: this was an UNSAFE DMA BYPASS run (no SMMU confinement)." |
| 246 | fi |
| 247 | [[ "${failed}" == 0 ]] && echo "QEMU_KEYBOARD: PASS (${mode})" || { echo "QEMU_KEYBOARD: FAIL (${mode})"; exit 1; } |