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

247 lines · 10821 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# 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).
28set -euo pipefail
29
30repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
31cd "${repo_root}"
32
33code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
34vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
35command -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
38keys=(a b c)
39expected="abc"
40attempts="${AIENOS_KEYBOARD_ATTEMPTS:-3}"
41boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
42machine="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.
52bypass_feature="unsafe-debug-dma-without-smmu"
53if [[ "${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 "################################################################"
62elif [[ "${AIENOS_QEMU_SMMU:-1}" == "1" ]]; then
63 mode="smmu"
64 machine+=",iommu=smmuv3"
65 default_features="usb-keyboard"
66 target_dir="target/qemu-keyboard"
67else
68 mode="fail-closed"
69 default_features="usb-keyboard"
70 target_dir="target/qemu-keyboard"
71fi
72features="${AIENOS_BUILD_FEATURES:-$default_features}"
73if [[ "${mode}" != "unsafe-bypass" && ",${features}," == *",${bypass_feature},"* ]]; then
74 echo "STOP: ${bypass_feature} requested without AIENOS_UNSAFE_DMA_BYPASS=1"
75 exit 2
76fi
77
78# Own target directory: a keyboard-enabled image never lands where hardware
79# staging or the other tests pick up the handoff image.
80commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
81AIENOS_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
85work="$(mktemp -d)"
86qemu_pid=""
87cleanup() {
88 [[ -n "${qemu_pid}" ]] && kill "${qemu_pid}" 2>/dev/null || true
89 exec 3>&- 2>/dev/null || true
90 rm -rf "${work}"
91}
92trap cleanup EXIT
93mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
94touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
95cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
96
97serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -q -- "$1"; }
98qemu_running() { kill -0 "${qemu_pid}" 2>/dev/null; }
99
100# Wait until the serial log contains one of the patterns or QEMU exits.
101wait_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
111stop_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
120boot_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
172started=$(date +%s)
173attempt=1
174while :; do
175 status=0
176 boot_once || status=$?
177 [[ "${status}" != 2 || "${attempt}" -ge "${attempts}" ]] && break
178 attempt=$(( attempt + 1 ))
179done
180elapsed=$(( $(date +%s) - started ))
181
182tr -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
184failed=0
185check() { # 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}
193check_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}
201echo "attempts ${attempt}, ${elapsed} s (commit ${commit:0:12}), dma mode ${mode}, sent lines: abc, help, el, mem, exit"
202check "left firmware and entered the kernel" "kernel: alive"
203check "xHCI controller found before exit" "keyboard: xhci "
204check "PCI segment swept for bus masters after exit" "dma_sweep: seg "
205check_absent "no endpoint left with bus master stuck on" "dma_sweep: bme STUCK"
206check "xHCI bus master off before the DMA gate" "xhci_pci: command=0x[0-9a-f]* bus_master=off"
207if [[ "${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"
212else
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"
231fi
232if [[ "${mode}" != "unsafe-bypass" ]]; then
233 check_absent "no unsafe DMA bypass in this image" "UNSAFE DMA BYPASS"
234fi
235if grep -qE "report_kind: (panic|fault)" "${work}/serial.txt"; then
236 echo "FAIL panic or fault reported"
237 failed=1
238fi
239
240if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
241 echo "---- serial console ----"
242 cat "${work}/serial.txt"
243fi
244if [[ "${mode}" == "unsafe-bypass" ]]; then
245 echo "WARNING: this was an UNSAFE DMA BYPASS run (no SMMU confinement)."
246fi
247[[ "${failed}" == 0 ]] && echo "QEMU_KEYBOARD: PASS (${mode})" || { echo "QEMU_KEYBOARD: FAIL (${mode})"; exit 1; }