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

508 lines · 28589 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# CONSOLE gate for the AIENOS C kernel (Campaign 3 cut C3-1a): rows 146-155 of
18# native/kernel/GATES.md "C3-1a console session". C-only (no Rust oracle).
19# QEMU is not hardware: a PASS here qualifies nothing physical.
20#
21# Boots the TEST-ONLY console session image (make full CK_CONSOLE_SESSION=1)
22# in QEMU AArch64 with UEFI (AAVMF), an NVMe disk, a qemu-xhci controller with
23# a usb-kbd and iommu=smmuv3. The image has a final boot stage "console"
24# (native/kernel/dev/devices.c) that runs after every other boot step: serial
25# (PL011, polled) and the USB keyboard (inside the xHCI DMA fence) feed one
26# bounded line editor and the existing shell until "exit". Serial input comes
27# from OUTSIDE the guest through a QEMU pipe chardev, USB keys through the QEMU
28# monitor ("sendkey"); the kernel never fakes either device.
29#
30# Timeline after "console_session: ready" (T0, measured here on the host):
31# T0+70 s serial "uptime", serial "el", usb "uptime", usb "help"
32# T0+130 s the same again (sustained past the old 60 s shell limit)
33# then interleave: serial "ab" + usb "xy" (dropped) + serial "c" Enter;
34# usb "el" prefix + serial "zz" (dropped) + usb Enter
35# then overflow: 65 x from serial, 65 x from usb (both refused)
36# then "exit" from usb: the session revokes
37# (AIENOS_CONSOLE_T1 and AIENOS_CONSOLE_T2 override 70 and 130 for debugging.)
38#
39# row 146 the session starts after every other boot step (after mm_usage,
40# before the final report), PL011 receive ready on the console UART
41# base the kernel already uses (no second address in the session code)
42# row 147 serial input echoed and run at ~70 s and ~130 s (uptime_ms past
43# the old 60 s limit each time)
44# row 148 USB keyboard input echoed and run at ~70 s and ~130 s
45# row 149 an overlong line is refused whole, from each source, never run
46# row 150 a line is owned by the first source; the other source's keys are
47# dropped and counted in a printed line; the line holds only the
48# owner's keys (both directions)
49# row 151 "exit" revokes inside the session: controller halted, bus master
50# revoked, COMMAND read back off, stream back to abort, in order,
51# before "console_session: end (exit) xhci=released"
52# row 152 the existing DMA rows hold for the session image: sweep before any
53# DMA grant, xHCI bus master off before the gate, both fences
54# (devices stage and session) confined and revoked, no bypass
55# row 153 no panic or fault, final report, QEMU exit 0, M1 checks
56# row 154 the USB keyboard unplugged mid-session (QEMU device_del): the session
57# notices, revokes the fence (halt, bus master off, stream abort) and
58# serial goes on and still runs commands, then exit ends it (own boot)
59# row 155 a boot with no USB keyboard runs serial-only after the failed attach was
60# revoked; serial input works and exit ends it (own boot)
61#
62# --mutation (red before green): "make full CK_CONSOLE_SESSION=1
63# CK_TEST_CONSOLE_MUTATION=bm-left-on-after-exit" leaves the xHCI bus master on
64# after the session exit; with the SMMU on, row 151 must FAIL (and the boot
65# must still reach the final report with row 146 passing, so the failure is
66# the mutation's, not a dead boot). It also checks the build refuses the
67# mutation with CK_HARDWARE_STAGING=1, CK_QEMU_UNSAFE_DMA=1 and without
68# CK_CONSOLE_SESSION=1, and that the default image carries no session code.
69#
70# Like the other QEMU children: NOT_RUN (exit 3) while the machine quiet flag
71# (AIENOS_QUIET_FLAG, default ~/workspace/.spark-quiet) exists, which this
72# script only reads and never writes, or while the QEMU gate lock
73# (AIENOS_GATE_LOCK, default ~/workspace/.qemu-gate-lock, exclusive create) is
74# held by another gate run.
75# Final lines: one "CONSOLE_ROW <n>: PASS|FAIL|NOT_RUN (...)" per row and
76# "AIENOS_CK_CONSOLE: PASS|FAIL|NOT_RUN (...)" (read by scripts/ck_gates.sh).
77# Exit 0 PASS, 1 FAIL, 2 missing tools, 3 NOT_RUN.
78# --mutation: "AIENOS_CK_CONSOLE_MUTATION: PASS|FAIL"; exit 0 / 1.
79set -euo pipefail
80
81repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
82cd "${repo_root}"
83
84mutation_run=0
85case "${1:-}" in
86 "") ;;
87 --mutation) mutation_run=1 ;;
88 *) echo "usage: $0 [--mutation]"; exit 2 ;;
89esac
90final_tag="AIENOS_CK_CONSOLE"
91[[ "${mutation_run}" == 0 ]] || final_tag="AIENOS_CK_CONSOLE_MUTATION"
92
93code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
94vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
95command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${final_tag}: NOT_RUN (qemu-system-aarch64 missing)"; exit 2; }
96[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${final_tag}: NOT_RUN (AAVMF missing)"; exit 2; }
97
98cross=""
99if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
100commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
101out="${repo_root}/target/native-kernel"
102mk() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" "$@"; }
103mk full >/dev/null
104default_efi="${out}/full/BOOTAA64.EFI"
105if [[ "${mutation_run}" == 1 ]]; then
106 mk full CK_CONSOLE_SESSION=1 CK_TEST_CONSOLE_MUTATION=bm-left-on-after-exit >/dev/null
107 session_efi="${out}/full-test-console-mutation/BOOTAA64.EFI"
108else
109 mk full CK_CONSOLE_SESSION=1 >/dev/null
110 session_efi="${out}/full-console-session/BOOTAA64.EFI"
111fi
112
113# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
114# scripts/lib_gate_hold.sh (aienos#278).
115. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
116if ! gh_quiet_check; then
117 echo "NOT_RUN ${gh_why}"
118 echo "${final_tag}: NOT_RUN (quiet flag held)"
119 exit 3
120fi
121if ! gh_lock_take qemu_ck_console_test "${AIENOS_GATE_MINUTES:-60}"; then
122 echo "NOT_RUN ${gh_why}"
123 echo "${final_tag}: NOT_RUN (QEMU gate lock held)"
124 exit 3
125fi
126# Release only this run's own gate lock record, at most once.
127release_lock() { gh_lock_release; }
128top="$(mktemp -d)"
129qemu_pid=""
130drain_pid=""
131cleanup() {
132 [[ -z "${qemu_pid}" ]] || kill "${qemu_pid}" 2>/dev/null || true
133 [[ -z "${drain_pid}" ]] || kill "${drain_pid}" 2>/dev/null || true
134 exec 3>&- 4>&- 2>/dev/null || true
135 rm -rf "${top}"
136 release_lock
137}
138trap cleanup EXIT
139
140# shellcheck source=scripts/lib_ck_m1_checks.sh
141source "${repo_root}/scripts/lib_ck_m1_checks.sh"
142img_bytes=67108864
143image="${top}/nvme.img"
144truncate -s "${img_bytes}" "${image}"
145m1_fail=0
146boot_timeout="${AIENOS_QEMU_TIMEOUT:-700}"
147t1="${AIENOS_CONSOLE_T1:-70}"
148t2="${AIENOS_CONSOLE_T2:-130}"
149overflow_keys=65
150
151serial_has() { tr -d '\r' 2>/dev/null <"${work}/serial.log" | grep -qE -- "$1"; }
152qemu_running() { [[ -n "${qemu_pid}" ]] && kill -0 "${qemu_pid}" 2>/dev/null; }
153wait_for() { # seconds ERE...: 0 once the serial log matches one, 1 on timeout or QEMU exit
154 local deadline=$(( $(date +%s) + $1 )) p; shift
155 while (( $(date +%s) < deadline )) && qemu_running; do
156 for p in "$@"; do serial_has "${p}" && return 0; done
157 sleep 0.3
158 done
159 for p in "$@"; do serial_has "${p}" && return 0; done
160 return 1
161}
162sleep_until() { local now; now=$(date +%s); (( $1 > now )) && sleep $(( $1 - now )) || true; }
163# USB keys go in through the QEMU monitor (fd 3), serial bytes through the pipe chardev (fd 4).
164usb_keys() { local k; for k in "$@"; do echo "sendkey ${k}" >&3; sleep 0.12; done; }
165usb_line() { usb_keys "$@" ret; sleep 0.6; }
166ser_text() { local c; for c in "$@"; do printf '%s' "${c}" >&4; sleep 0.05; done; }
167ser_line() { ser_text "$@"; printf '\r' >&4; sleep 0.6; }
168letters() { local s="$1" i; for ((i = 0; i < ${#s}; i++)); do printf '%s\n' "${s:i:1}"; done; }
169ser_word() { mapfile -t _l < <(letters "$1"); ser_line "${_l[@]}"; }
170usb_word() { mapfile -t _l < <(letters "$1"); usb_line "${_l[@]}"; }
171xs() { local i; for ((i = 0; i < overflow_keys; i++)); do printf 'x\n'; done; }
172
173# boot <name> <efi> <full|mutation|unplug|nokbd>: one QEMU boot driven as described above.
174boot() {
175 work="${top}/$1"
176 mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
177 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
178 cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
179 kbd_dev="-device usb-kbd,bus=xhci.0,id=kbd0"
180 [[ "$3" != nokbd ]] || kbd_dev=""
181 cp "${vars_fd}" "${work}/vars.fd"
182 rm -f "${work}/mon.in" "${work}/mon.out" "${work}/ser.in" "${work}/ser.out"
183 mkfifo "${work}/mon.in" "${work}/mon.out" "${work}/ser.in" "${work}/ser.out"
184 : >"${work}/serial.log"
185 timeout "${boot_timeout}" qemu-system-aarch64 \
186 -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
187 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
188 -drive if=pflash,format=raw,file="${work}/vars.fd" \
189 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
190 -device virtio-blk-pci,drive=esp \
191 -drive if=none,id=nvme0,format=raw,file="${image}" \
192 -device nvme,drive=nvme0,serial=aienos-console-test \
193 -device qemu-xhci,id=xhci $kbd_dev \
194 -device ramfb -display none -nic none \
195 -chardev pipe,id=mon,path="${work}/mon" -mon chardev=mon,mode=readline \
196 -chardev pipe,id=ser,path="${work}/ser" -serial chardev:ser -no-reboot 2>"${work}/qemu.err" &
197 qemu_pid=$!
198 cat "${work}/mon.out" >/dev/null &
199 cat "${work}/ser.out" >>"${work}/serial.log" &
200 drain_pid=$!
201 exec 3>"${work}/mon.in" 4>"${work}/ser.in"
202 t0=0
203 if wait_for "${boot_timeout}" '^console_session: ready' '^report_kind: (panic|fault|final)' && serial_has '^console_session: ready'; then
204 t0=$(date +%s)
205 if [[ "$3" == full ]]; then
206 sleep_until $(( t0 + t1 ))
207 ser_word uptime; ser_word el; usb_word uptime; usb_word help
208 sleep_until $(( t0 + t2 ))
209 ser_word uptime; ser_word el; usb_word uptime; usb_word help
210 # interleave A: serial owns the line, usb keys dropped
211 ser_text a b; usb_keys x y; ser_text c; printf '\r' >&4; sleep 0.8
212 # interleave B: usb owns the line, serial bytes dropped
213 usb_keys e l; ser_text z z; usb_keys ret; sleep 0.8
214 # overflow from each source
215 mapfile -t _x < <(xs); ser_line "${_x[@]}"; usb_line "${_x[@]}"
216 usb_word exit
217 elif [[ "$3" == unplug ]]; then
218 sleep 3
219 echo "device_del kbd0" >&3; sleep 8
220 ser_word el; ser_word exit
221 elif [[ "$3" == nokbd ]]; then
222 sleep 3
223 ser_word el; ser_word exit
224 else
225 sleep 3
226 usb_word exit
227 fi
228 fi
229 wait_for 120 '^report_kind: (panic|fault|final)' || true
230 set +e
231 wait "${qemu_pid}"
232 qemu_status=$?
233 set -e
234 qemu_pid=""
235 exec 3>&- 4>&- 2>/dev/null || true
236 wait "${drain_pid}" 2>/dev/null || true
237 drain_pid=""
238 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt"
239 # The session window: from its begin line to the stage's own result line.
240 awk '/^console_session: begin/{on=1} on{print} /^stage console:/{if(on) exit}' "${work}/serial.txt" >"${work}/sess.txt"
241 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_console_$1.log"
242 echo "== boot $1 (smmu, $3): qemu exit ${qemu_status}, session ready at host second ${t0}"
243}
244
245all_rows=(146 147 148 149 150 151 152 153 154 155)
246declare -A rowfail rowchk
247reset_rows() { local r; for r in "${all_rows[@]}"; do rowfail[$r]=0; rowchk[$r]=0; done; }
248reset_rows
249mode="smmu"
250# f: the file the next checks read (serial.txt or the session window sess.txt).
251f=serial.txt
252rpass() { rowchk[$1]=$(( ${rowchk[$1]} + 1 )); echo "ROW $1 PASS ${mode}: $2"; }
253rfail() { rowchk[$1]=$(( ${rowchk[$1]} + 1 )); rowfail[$1]=1; echo "ROW $1 FAIL ${mode}: $2"; }
254rcheck() { if grep -qE -- "$3" "${work}/${f}"; then rpass "$1" "$2"; else rfail "$1" "$2"; fi; }
255rabsent() { if grep -qE -- "$3" "${work}/${f}"; then rfail "$1" "$2"; else rpass "$1" "$2"; fi; }
256line_of() { { grep -nE -m1 -- "$1" "${work}/${f}" || true; } | cut -d: -f1; }
257before() {
258 local a b
259 a="$(line_of "$3")"; b="$(line_of "$4")"
260 if [[ -n "${a}" && -n "${b}" && "${a}" -lt "${b}" ]]; then rpass "$1" "$2 (lines ${a} < ${b})"
261 else rfail "$1" "$2 (lines ${a:-none} ${b:-none})"; fi
262}
263# uptimes <source>: the uptime_ms value printed after each "console_line: uptime source=<src>".
264uptimes() {
265 awk -v want="source=$1" '/^console_line: uptime source=/{ s=$NF; w=(s==want) } /^uptime_ms: /{ if (w) { print $2; w=0 } }' "${work}/${f}"
266}
267
268# row 146: placement and serial receive.
269eval_start() {
270 f=serial.txt
271 rcheck 146 "console session begins" '^console_session: begin \(TEST-ONLY QEMU image, CK_CONSOLE_SESSION=1; QEMU only, hardware NOT_RUN\)$'
272 before 146 "after the Store stage and the artifact loader's mm_usage" '^mm_usage: ' '^console_session: begin'
273 before 146 "begin < ready" '^console_session: begin' '^console_session: ready '
274 before 146 "ready < the stage result" '^console_session: ready ' '^stage console: ok rc=0$'
275 before 146 "stage result < final report" '^stage console: ok rc=0$' '^report_kind: final'
276 before 146 "after the devices stage fence" '^devices: xhci=' '^console_session: begin'
277 rcheck 146 "PL011 receive ready on the console UART (QEMU virt PL011 at 0x9000000, UARTEN+RXE set)" \
278 '^console_session: serial rx pl011 base=0x9000000 uartcr=0x[0-9a-f]{4} ready$'
279 rcheck 146 "ready line says no time limit, no idle exit, ends on exit" \
280 '^console_session: ready \(usb keyboard ready; no time limit, no idle exit; ends on exit\)$'
281 rcheck 146 "USB keyboard attached inside the xHCI fence" '^console_session: usb keyboard attached \(port [0-9]+, slot [0-9]+, endpoint 0x8[0-9a-f]\), inside the xHCI fence$'
282 rabsent 146 "no session time limit or idle exit fired" '^console_session: end \((timeout|idle)'
283 if grep -nE '0x9000000|9000000' native/kernel/dev/usb_kbd.c native/kernel/dev/devices.c >/dev/null 2>&1; then
284 rfail 146 "session code hard-codes a UART address"
285 else rpass 146 "session code reads the console's UART base only (no second address)"; fi
286}
287
288# rows 147, 148: sustained input, per source.
289eval_sustained() { # source row
290 local s="$1" r="$2" ups
291 f=serial.txt
292 rcheck "${r}" "${s}: 'uptime' echoed and run (round 1 and 2)" "^console_line: uptime source=${s}$"
293 [[ "$(grep -cE -- "^console_line: uptime source=${s}$" "${work}/${f}")" -ge 2 ]] && rpass "${r}" "${s}: two uptime lines" || rfail "${r}" "${s}: fewer than two uptime lines"
294 rcheck "${r}" "${s}: echo line" '^console_echo: uptime$'
295 ups="$(uptimes "${s}" | tr '\n' ' ')"
296 local u1 u2
297 u1="$(echo "${ups}" | awk '{print $1+0}')"; u2="$(echo "${ups}" | awk '{print $2+0}')"
298 if [[ "${u1}" -ge $(( (t1 - 5) * 1000 )) ]]; then rpass "${r}" "${s}: first input ran ${u1} ms into the session (>= $(( (t1 - 5) * 1000 )))"
299 else rfail "${r}" "${s}: first input uptime_ms=${u1:-none}"; fi
300 if [[ "${u2}" -ge $(( (t2 - 5) * 1000 )) && "${u2}" -gt 60000 ]]; then rpass "${r}" "${s}: second input ran ${u2} ms into the session (past the old 60000 ms limit)"
301 else rfail "${r}" "${s}: second input uptime_ms=${u2:-none}"; fi
302 if [[ "${s}" == serial ]]; then
303 rcheck "${r}" "serial: 'el' ran" '^console_line: el source=serial$'
304 [[ "$(grep -cE -- '^EL1$' "${work}/${f}")" -ge 2 ]] && rpass "${r}" "serial: EL1 printed after both rounds" || rfail "${r}" "serial: EL1 missing"
305 else
306 rcheck "${r}" "usb: 'help' ran" '^console_line: help source=usb$'
307 [[ "$(grep -cE -- '^commands: help mem el report uptime exit$' "${work}/${f}")" -ge 2 ]] && rpass "${r}" "usb: help output after both rounds" || rfail "${r}" "usb: help output missing"
308 fi
309}
310
311eval_overflow() {
312 f=serial.txt
313 local s
314 for s in serial usb; do
315 rcheck 149 "${s}: overlong line refused whole" "^console_line: overflow \\(line refused: ${overflow_keys} keys typed, limit 64\\) source=${s}\$"
316 done
317 rabsent 149 "overlong line never reported as a line" '^console_line: x'
318 rabsent 149 "overlong line never run as a command" '^unknown command: x'
319 rabsent 149 "overlong line never echoed" '^console_echo: x'
320}
321
322eval_interleave() {
323 f=serial.txt
324 rcheck 150 "serial owned the line: only its keys ran" '^console_line: abc source=serial$'
325 rcheck 150 "serial line's usb keys dropped and counted" '^console_input: dropped 2 key\(s\) from usb while serial owned the line$'
326 rcheck 150 "the dropped keys ran as nothing" '^unknown command: abc$'
327 rcheck 150 "usb owned the line: only its keys ran" '^console_line: el source=usb$'
328 rcheck 150 "usb line's serial bytes dropped and counted" '^console_input: dropped 2 key\(s\) from serial while usb owned the line$'
329 rabsent 150 "no mixed line (abcxy, elzz, xy, zz)" '^console_line: (abcxy|abxyc|elzz|xy|zz|el[a-z]+) source='
330 before 150 "serial line before the usb line" '^console_line: abc source=serial$' '^console_line: el source=usb$'
331}
332
333eval_revoke() { # row 151 on the session window
334 f=sess.txt
335 rcheck 151 "session fence granted only as confined" '^dma_gate: xhci granted \(Confined\), bus master on$'
336 rcheck 151 "session window: xHCI SMMU window" '^smmu_dma_window: xhci only, translation active iova=0x[0-9a-f]+ len=0x[0-9a-f]+ rid=0x[0-9a-f]+$'
337 rcheck 151 "session exit seen" '^console_session: exit \(source=usb\)$'
338 rcheck 151 "controller halted before the revoke" '^xhci: halt before revoke usbcmd=0x[0-9a-f]{8} usbsts=0x[0-9a-f]{8} halted=yes$'
339 rcheck 151 "xHCI bus master revoked after the session" '^dma_gate: xhci bus master revoked$'
340 rabsent 151 "no revoke failure" '^dma_gate: xhci bus master revoke FAILED'
341 rcheck 151 "COMMAND read back after the session: bus master off" '^xhci_pci: after phase command=0x[0-9a-f]{4} bus_master=off$'
342 rcheck 151 "xHCI SMMU stream returned to abort" '^smmu: xhci stream 0x[0-9a-f]+ returned to abort \(rc=0\)$'
343 rcheck 151 "session ends with the controller released" '^console_session: end \(exit\) xhci=released$'
344 before 151 "order: exit < halt" '^console_session: exit ' '^xhci: halt before revoke'
345 before 151 "order: halt < revoke" '^xhci: halt before revoke' '^dma_gate: xhci bus master revoked$'
346 before 151 "order: revoke < COMMAND read back" '^dma_gate: xhci bus master revoked$' '^xhci_pci: after phase command='
347 before 151 "order: read back < stream abort" '^xhci_pci: after phase command=' '^smmu: xhci stream .* returned to abort'
348 before 151 "order: stream abort < session end" '^smmu: xhci stream .* returned to abort' '^console_session: end '
349}
350
351eval_dma() { # row 152 on the whole log
352 f=serial.txt
353 rcheck 152 "post-exit bus-master sweep of the segment, nothing stuck" \
354 '^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$'
355 rabsent 152 "no endpoint left with bus master stuck on" '^dma_sweep: bme STUCK'
356 before 152 "sweep before the first device DMA gate" '^dma_sweep: seg ' '^dma_gate: '
357 rcheck 152 "xHCI bus master off before the DMA gate" '^xhci_pci: command=0x[0-9a-f]{4} bus_master=off$'
358 before 152 "COMMAND read back before the first xHCI DMA gate" '^xhci_pci: command=' '^dma_gate: xhci '
359 local g; g="$(grep -cE -- '^dma_gate: xhci granted \(Confined\), bus master on$' "${work}/${f}")"
360 [[ "${g}" == 2 ]] && rpass 152 "two confined grants (devices-stage shell and session)" || rfail 152 "confined grants: ${g} (expected 2)"
361 g="$(grep -cE -- '^dma_gate: xhci bus master revoked$' "${work}/${f}")"
362 [[ "${g}" -ge 2 ]] && rpass 152 "bus master revoked after each grant" || rfail 152 "revokes: ${g} (expected 2)"
363 g="$(grep -cE -- '^smmu: xhci stream 0x[0-9a-f]+ returned to abort \(rc=0\)$' "${work}/${f}")"
364 [[ "${g}" -ge 2 ]] && rpass 152 "stream back to abort after each grant" || rfail 152 "stream aborts: ${g} (expected 2)"
365 rabsent 152 "no unconfined xHCI grant" 'dma_gate: xhci granted \((UnsafeBypass|TestMutation)'
366 rabsent 152 "no unsafe DMA bypass in this image" 'UNSAFE DMA BYPASS'
367 rabsent 152 "no xHCI fence mutation banner" 'TEST-ONLY xHCI MUTATION'
368 rcheck 152 "devices stage shell still ran and revoked before the session" '^devices: xhci=fenced \(rc=0\)$'
369 before 152 "devices-stage fence < session" '^devices: xhci=fenced \(rc=0\)$' '^console_session: begin'
370 rcheck 152 "existing boot shell unchanged: it still reports its bounded limits" '^keyboard: shell ready \(60 s, idle 15 s\)$'
371}
372
373eval_final() { # row 153
374 f=serial.txt
375 rabsent 153 "no panic or fault" '^report_kind: (panic|fault)'
376 rcheck 153 "final report reached" 'report_kind: final'
377 if [[ "${qemu_status}" == 0 ]]; then rpass 153 "QEMU exit 0 after PSCI reset"; else rfail 153 "QEMU exit ${qemu_status} (expected 0)"; fi
378}
379
380# rows 154, 155: the keyboard going away. 154: the USB keyboard is unplugged
381# mid-session (QEMU monitor device_del): the session revokes the fence and
382# serial goes on and still works. 155: a boot with no USB keyboard at all runs
383# serial-only, after the failed attach was revoked.
384eval_serial_only() { # row, boot kind (unplug|nokbd)
385 local r="$1" k="$2"
386 f=sess.txt
387 if [[ "${k}" == unplug ]]; then
388 rcheck "${r}" "keyboard attached first" '^console_session: usb keyboard attached '
389 rcheck "${r}" "disconnect noticed, keyboard revoked, serial goes on" '^console_session: usb keyboard disconnected \(port [0-9]+\); the keyboard is revoked, serial goes on$'
390 before "${r}" "ready < disconnect" '^console_session: ready ' '^console_session: usb keyboard disconnected'
391 before "${r}" "disconnect < halt" '^console_session: usb keyboard disconnected' '^xhci: halt before revoke'
392 else
393 rcheck "${r}" "attach refused for lack of a keyboard" '^console_session: usb keyboard unavailable \('
394 rcheck "${r}" "ready line says the keyboard is unavailable" '^console_session: ready \(usb keyboard unavailable; no time limit, no idle exit; ends on exit\)$'
395 before "${r}" "failed attach revoked before the session is ready" '^smmu: xhci stream .* returned to abort' '^console_session: ready '
396 fi
397 rcheck "${r}" "controller halted before the revoke" '^xhci: halt before revoke usbcmd=0x[0-9a-f]{8} usbsts=0x[0-9a-f]{8} halted=yes$'
398 rcheck "${r}" "xHCI bus master revoked" '^dma_gate: xhci bus master revoked$'
399 rcheck "${r}" "COMMAND read back: bus master off" '^xhci_pci: after phase command=0x[0-9a-f]{4} bus_master=off$'
400 rcheck "${r}" "stream returned to abort" '^smmu: xhci stream 0x[0-9a-f]+ returned to abort \(rc=0\)$'
401 before "${r}" "revoke before the serial line runs" '^dma_gate: xhci bus master revoked$' '^console_line: el source=serial$'
402 rcheck "${r}" "serial input works after the keyboard is gone: 'el' echoed and run" '^console_echo: el$'
403 rcheck "${r}" "EL1 printed" '^EL1$'
404 rcheck "${r}" "serial exit ends the session with the controller released" '^console_session: exit \(source=serial\)$'
405 rcheck "${r}" "session end" '^console_session: end \(exit\) xhci=released$'
406 f=serial.txt
407 rabsent "${r}" "no panic or fault" '^report_kind: (panic|fault)'
408 rcheck "${r}" "final report reached" 'report_kind: final'
409 if [[ "${qemu_status}" == 0 ]]; then rpass "${r}" "QEMU exit 0"; else rfail "${r}" "QEMU exit ${qemu_status}"; fi
410}
411
412image_checks() {
413 check "image is this commit" "aienos_commit: ${commit}"
414 check_absent "no xHCI fence mutation banner" "TEST-ONLY xHCI MUTATION"
415}
416
417if [[ "${mutation_run}" == 0 ]]; then
418 boot console-smmu "${session_efi}" full
419 failed=0
420 ck_m1_checks
421 [[ "${failed}" == 0 ]] || m1_fail=1
422 failed=0
423 image_checks
424 [[ "${failed}" == 0 ]] || m1_fail=1
425 eval_start
426 eval_sustained serial 147
427 eval_sustained usb 148
428 eval_overflow
429 eval_interleave
430 eval_revoke
431 eval_dma
432 eval_final
433 mode=unplug; boot console-unplug "${session_efi}" unplug; eval_serial_only 154 unplug
434 mode=nokbd; boot console-nokbd "${session_efi}" nokbd; eval_serial_only 155 nokbd
435 mode=smmu
436 release_lock
437 any_fail=0
438 for r in "${all_rows[@]}"; do any_fail=$(( any_fail | rowfail[$r] )); done
439 if [[ "${m1_fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" || "${any_fail}" != 0 ]]; then
440 echo "---- serial console (session lines) ----"
441 grep -E '^(console|uptime_ms|commands|EL[0-9]|unknown|dma_gate|xhci|smmu:|stage |report_kind:|devices:)' "${work}/serial.txt" | head -150 || true
442 fi
443 echo "== per-row results (QEMU only; hardware NOT_RUN)"
444 gate_fail="${m1_fail}"
445 for r in "${all_rows[@]}"; do
446 if [[ "${rowchk[$r]}" == 0 ]]; then
447 echo "CONSOLE_ROW ${r}: NOT_RUN (not checked)"; gate_fail=1
448 elif [[ "${rowfail[$r]}" == 0 && "${m1_fail}" == 0 ]]; then
449 echo "CONSOLE_ROW ${r}: PASS (smmu; ${rowchk[$r]} checks)"
450 else
451 echo "CONSOLE_ROW ${r}: FAIL (smmu$([[ "${m1_fail}" == 0 ]] || echo '; an M1 or image check failed'))"; gate_fail=1
452 fi
453 done
454 if [[ "${gate_fail}" != 0 ]]; then
455 echo "${final_tag}: FAIL (rows 146-155; QEMU only)"
456 exit 1
457 fi
458 echo "${final_tag}: PASS (rows 146-155; serial + usb input at ~${t1} s and ~${t2} s, overflow, interleave, exit revoke; boot console-smmu; QEMU only, hardware NOT_RUN)"
459 exit 0
460fi
461
462# ---- --mutation: bus master left on after the session exit must make row 151 FAIL ----
463mut_fail=0
464mut_ok() { echo "MUTATION PASS $1"; }
465mut_bad() { echo "MUTATION FAIL $1"; mut_fail=1; }
466if grep -aqF "console_session:" "${default_efi}"; then mut_bad "default image carries console session code"; else mut_ok "default image carries no console session code"; fi
467for combo in "CK_HARDWARE_STAGING=1" "CK_QEMU_UNSAFE_DMA=1"; do
468 set +e
469 msg="$(make -s -n -C native/kernel OUT="${top}/refuse" full CK_CONSOLE_SESSION=1 "${combo}" 2>&1)"
470 st=$?
471 set -e
472 if [[ "${st}" != 0 ]] && grep -qE "CK_CONSOLE_SESSION.*cannot be combined with ${combo%%=*}" <<<"${msg}"; then
473 mut_ok "build refuses CK_CONSOLE_SESSION with ${combo}"
474 else
475 mut_bad "build did not refuse CK_CONSOLE_SESSION with ${combo} (status ${st}): $(head -c 200 <<<"${msg}")"
476 fi
477done
478set +e
479msg="$(make -s -n -C native/kernel OUT="${top}/refuse" full CK_TEST_CONSOLE_MUTATION=bm-left-on-after-exit 2>&1)"
480st=$?
481set -e
482if [[ "${st}" != 0 ]] && grep -qF "CK_TEST_CONSOLE_MUTATION needs CK_CONSOLE_SESSION=1" <<<"${msg}"; then
483 mut_ok "build refuses CK_TEST_CONSOLE_MUTATION without CK_CONSOLE_SESSION=1"
484else
485 mut_bad "build did not refuse the mutation without the session (status ${st})"
486fi
487boot console-mut "${session_efi}" mutation
488reset_rows
489eval_start
490eval_revoke
491eval_final
492release_lock
493if ! grep -qF "WARNING: TEST-ONLY console session MUTATION BUILD (CK_TEST_CONSOLE_MUTATION=bm-left-on-after-exit," "${work}/serial.txt"; then
494 mut_bad "image did not announce the mutation"
495elif ! grep -q "report_kind: final" "${work}/serial.txt" || [[ "${rowfail[146]}" != 0 ]]; then
496 mut_bad "boot did not reach the final report with the session running (a dead boot proves nothing)"
497elif [[ "${rowfail[151]}" == 1 ]]; then
498 mut_ok "bus master left on after the session exit (smmu): row 151 FAILs as it must"
499else
500 mut_bad "bus master left on after the session exit: row 151 still PASSES: the check does not catch it"
501fi
502if [[ "${mut_fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
503 echo "---- serial console (session lines) ----"
504 grep -E '^(WARNING|console|dma_gate|xhci|smmu:|report_kind:)' "${work}/serial.txt" | head -60 || true
505fi
506if [[ "${mut_fail}" == 0 ]]; then echo "${final_tag}: PASS (bm-left-on-after-exit->151)"; exit 0; fi
507echo "${final_tag}: FAIL"
508exit 1