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

261 lines · 16621 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# Boot the AIENOS C kernel core in QEMU AArch64 (UEFI/AAVMF, EL2 like
18# Machine 1) with a ramfb display and check that the kernel drew its console
19# on the UEFI Graphics Output Protocol framebuffer the boot stub handed over
20# in the CHANDOF3 record (native/boot/efi_gop.c, native/kernel/core/fbcon.c).
21# Last line: AIENOS_CK_SCREEN: PASS|FAIL|NOT_RUN (read by scripts/ck_gates.sh).
22#
23# Two boots, both with "-device ramfb" and a QMP socket; QEMU runs with
24# "-action reboot=shutdown,shutdown=pause", so the kernel's final PSCI reset
25# pauses the guest with the last screen still in place, and the script takes a
26# QMP "screendump" (binary PPM) before quitting QEMU.
27# 1. default image (core only, as built by make): the serial log must show
28# the stub's "gop: ok" line, exactly one "screen: gop" line, "kernel:
29# alive", no panic/fault, a reset (QEMU status "shutdown"), and the host
30# checker (native/kernel/tools/ck_fb_check.c) must find the screendump
31# pixel-identical to the screen the kernel's own console code renders
32# from the serial bytes, decode every cell against the kernel's font,
33# and find the rows "report_kind: final" and "note: QEMU qualifies
34# nothing physical" on screen.
35# 2. negative control, TEST-only image (make CK_TEST_STALE_HANDOFF=2): the
36# stub hands over a CHANDOF2 record; the kernel must refuse it with
37# "panic: handoff: unsupported version 2 (kernel reads CHANDOF3 only)",
38# never draw ("screen: gop" absent, "kernel: alive" absent), and the
39# host checker must FAIL on that boot's serial log and screendump.
40# QEMU is not hardware: a PASS here qualifies nothing physical. It says
41# nothing about the Spark's own GOP (UNVERIFIED until an attended boot).
42#
43# NOT_RUN (never a silent pass) when qemu, AAVMF, socat or the host compiler
44# is missing, the QEMU gate lock (~/workspace/.qemu-gate-lock, exclusive
45# create, same rule as the FPU and INFER gates) is held, or the machine quiet
46# flag (~/workspace/.spark-quiet) exists; this script never writes the quiet
47# flag.
48#
49# Usage: bash scripts/qemu_ck_screen_test.sh the gate
50# bash scripts/qemu_ck_screen_test.sh --self-test canned logs and
51# rendered screens, no QEMU
52# Environment: AIENOS_QEMU_TIMEOUT (300 s per boot), AIENOS_GATE_LOCK,
53# AIENOS_GATE_MINUTES, AIENOS_QUIET_FLAG (read only), AIENOS_LOG_DIR (serial logs,
54# screendumps, decoded rows and PNGs when ffmpeg exists), AIENOS_QEMU_VERBOSE,
55# AIENOS_FB_CHECK_TOOL (prebuilt checker, for a red run on an older commit).
56set -euo pipefail
57
58repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
59cd "${repo_root}"
60verdict_name=AIENOS_CK_SCREEN
61stale_line='panic: handoff: unsupported version 2 (kernel reads CHANDOF3 only)'
62expect_rows=("report_kind: final" "note: QEMU qualifies nothing physical")
63
64# positive_checks SERIAL PPM TOOL STATUS OUTPREFIX: PASS/FAIL line per check.
65positive_checks() {
66 local serial="$1" ppm="$2" tool="$3" status="$4" pre="$5" n args=() e
67 ok() { echo "PASS $*"; }
68 bad() { echo "FAIL $*"; }
69 grep -qE '^gop: ok ' "${serial}" && ok "boot stub found a GOP linear framebuffer" || bad "no 'gop: ok' line from the boot stub"
70 n="$(grep -cE '^screen: gop ' "${serial}" || true)"
71 [[ "${n}" == 1 ]] && ok "kernel put its console on the framebuffer (one 'screen: gop' line)" || bad "${n} 'screen: gop' lines (want 1)"
72 grep -qx 'kernel: alive' "${serial}" && ok "kernel alive" || bad "kernel never reached kernel: alive"
73 if grep -qE 'report_kind: (panic|fault)' "${serial}"; then bad "panic or fault report"; else ok "no panic or fault"; fi
74 [[ "${status}" == shutdown ]] && ok "guest reset reached (QEMU paused at shutdown)" || bad "QEMU status '${status}' (want shutdown)"
75 for e in "${expect_rows[@]}"; do args+=(--expect "${e}"); done
76 if "${tool}" --serial "${serial}" --ppm "${ppm}" --render "${pre}.expected.ppm" "${args[@]}" >"${pre}.fbcheck.txt" 2>&1; then
77 ok "screendump matches the rendered console: $(tail -1 "${pre}.fbcheck.txt")"
78 else
79 bad "screen check: $(grep -E '^fb_check: (FAIL|expect .*MISSING|pixel)' "${pre}.fbcheck.txt" | head -3 | paste -sd';' -)"
80 fi
81}
82
83# negative_checks SERIAL PPM TOOL STATUS OUTPREFIX
84negative_checks() {
85 local serial="$1" ppm="$2" tool="$3" status="$4" pre="$5"
86 ok() { echo "PASS $*"; }
87 bad() { echo "FAIL $*"; }
88 grep -qF 'TEST-ONLY stale handoff image' "${serial}" && ok "negative image announced itself (TEST-ONLY)" || bad "negative image did not announce itself"
89 grep -qxF "${stale_line}" "${serial}" && ok "kernel refused the CHANDOF2 record: ${stale_line}" || bad "no '${stale_line}' line"
90 if grep -qE '^screen: gop ' "${serial}"; then bad "kernel drew on the screen after a stale record"; else ok "no screen console after the refusal"; fi
91 if grep -qx 'kernel: alive' "${serial}"; then bad "kernel went on after a stale record"; else ok "kernel stopped before kernel: alive"; fi
92 [[ "${status}" == shutdown ]] && ok "refusal ended in a reset (QEMU paused at shutdown)" || bad "QEMU status '${status}' (want shutdown)"
93 if "${tool}" --serial "${serial}" --ppm "${ppm}" --expect "${stale_line}" >"${pre}.fbcheck.txt" 2>&1; then
94 bad "screen check PASSED on the refused boot (checker does not discriminate)"
95 else
96 ok "screen check FAILS on the refused boot as required ($(tail -1 "${pre}.fbcheck.txt"))"
97 fi
98}
99
100build_tool() { # OUT -> path of ck_fb_check (AIENOS_FB_CHECK_TOOL: a prebuilt
101 # checker, only for the red run on a commit that predates the tool)
102 if [[ -n "${AIENOS_FB_CHECK_TOOL:-}" ]]; then echo "${AIENOS_FB_CHECK_TOOL}"; return; fi
103 make -s -C native/kernel OUT="$1" fb-check >/dev/null
104 echo "$1/host/ck_fb_check"
105}
106
107if [[ "${1:-}" == --self-test ]]; then
108 command -v "${HOSTCC:-cc}" >/dev/null || command -v gcc >/dev/null || { echo "${verdict_name}_SELF_TEST: NOT_RUN (no host compiler)"; exit 2; }
109 tmp="$(mktemp -d)"; trap 'rm -rf "${tmp}"' EXIT
110 tool="$(build_tool "${tmp}/out")"
111 st=0
112 expect_result() { # NAME pass|fail OUTPUT-FILE
113 local f=0; grep -q '^FAIL' "$3" && f=1
114 if [[ "$2" == pass && ${f} == 0 ]] || [[ "$2" == fail && ${f} == 1 ]]; then echo "PASS self-test $1"; else echo "FAIL self-test $1"; sed 's/^/ /' "$3"; st=1; fi
115 }
116 # A canned positive boot: firmware noise, the marker, 80 numbered lines
117 # (forces one half-scroll on the 60-row 800x600 grid), the final report.
118 {
119 printf 'BdsDxe: loading Boot0001\r\ngop: ok from=conout handles=2 mode=1/3 800x600 pitch=800 format=1 base=0xbc7a0000 size=0x300000\r\n'
120 printf 'screen: gop 800x600 pitch=800 format=bgrx base=0xbc7a0000 bytes=0x1d4c00 scale=1 grid=100x60 font=8x8 mode=half-scroll\r\n'
121 printf '\r\nkernel: alive\r\n'
122 for i in $(seq 1 80); do printf 'line %02d: the quick brown fox jumps over the lazy dog ~!@#$%%^&*()_+{}|:"<>?\r\n' "${i}"; done
123 printf 'report_kind: final\r\nnote: QEMU qualifies nothing physical\r\n'
124 } >"${tmp}/good.raw"
125 tr -d '\r' <"${tmp}/good.raw" >"${tmp}/good.log"
126 "${tool}" --serial "${tmp}/good.raw" --render "${tmp}/good.ppm" >/dev/null
127 positive_checks "${tmp}/good.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p1" >"${tmp}/r1"; expect_result positive-good pass "${tmp}/r1"
128 # one flipped pixel byte in the last row of the image
129 cp "${tmp}/good.ppm" "${tmp}/flip.ppm"; sz=$(stat -c %s "${tmp}/flip.ppm")
130 printf '\x80' | dd of="${tmp}/flip.ppm" bs=1 seek=$((sz - 7)) conv=notrunc status=none
131 positive_checks "${tmp}/good.log" "${tmp}/flip.ppm" "${tool}" shutdown "${tmp}/p2" >"${tmp}/r2"; expect_result one-pixel-off fail "${tmp}/r2"
132 # the screen of a different text (one character changed in the log)
133 sed 's/line 79: the quick/line 79: the quack/' "${tmp}/good.log" >"${tmp}/other.log"
134 positive_checks "${tmp}/other.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p3" >"${tmp}/r3"; expect_result one-char-off fail "${tmp}/r3"
135 # no marker line; marker twice; expected row missing; not reset; panic
136 grep -v '^screen: gop' "${tmp}/good.log" >"${tmp}/nomark.log"
137 positive_checks "${tmp}/nomark.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p4" >"${tmp}/r4"; expect_result no-marker fail "${tmp}/r4"
138 { cat "${tmp}/good.log"; grep '^screen: gop' "${tmp}/good.log"; } >"${tmp}/twomark.log"
139 positive_checks "${tmp}/twomark.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p5" >"${tmp}/r5"; expect_result two-markers fail "${tmp}/r5"
140 grep -v '^note: QEMU' "${tmp}/good.raw" >"${tmp}/nonote.raw"; tr -d '\r' <"${tmp}/nonote.raw" >"${tmp}/nonote.log"
141 "${tool}" --serial "${tmp}/nonote.raw" --render "${tmp}/nonote.ppm" >/dev/null
142 positive_checks "${tmp}/nonote.log" "${tmp}/nonote.ppm" "${tool}" shutdown "${tmp}/p6" >"${tmp}/r6"; expect_result expected-row-missing fail "${tmp}/r6"
143 positive_checks "${tmp}/good.log" "${tmp}/good.ppm" "${tool}" running "${tmp}/p7" >"${tmp}/r7"; expect_result no-reset fail "${tmp}/r7"
144 { cat "${tmp}/good.log"; echo "report_kind: panic"; } >"${tmp}/panic.log"
145 positive_checks "${tmp}/panic.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p8" >"${tmp}/r8"; expect_result panic fail "${tmp}/r8"
146 # negative control: a refused boot, and a "refused" boot that drew anyway
147 printf 'handoff: TEST-ONLY stale handoff image: the kernel gets a CHANDOF2 record; never counts toward a PASS\nreport_kind: panic\n%s\n' "${stale_line}" >"${tmp}/neg.log"
148 positive_checks "${tmp}/neg.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/p9" >"${tmp}/r9"; expect_result stale-boot-is-not-a-positive fail "${tmp}/r9"
149 negative_checks "${tmp}/neg.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/n1" >"${tmp}/r10"; expect_result negative-good pass "${tmp}/r10"
150 { cat "${tmp}/neg.log"; cat "${tmp}/good.log"; } >"${tmp}/negdrew.log"
151 negative_checks "${tmp}/negdrew.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/n2" >"${tmp}/r11"; expect_result negative-that-drew fail "${tmp}/r11"
152 grep -v "^panic: handoff" "${tmp}/neg.log" >"${tmp}/negnoline.log"
153 negative_checks "${tmp}/negnoline.log" "${tmp}/good.ppm" "${tool}" shutdown "${tmp}/n3" >"${tmp}/r12"; expect_result negative-without-refusal-line fail "${tmp}/r12"
154 [[ ${st} == 0 ]] && { echo "${verdict_name}_SELF_TEST: PASS"; exit 0; }
155 echo "${verdict_name}_SELF_TEST: FAIL"; exit 1
156fi
157
158notrun() { echo "NOT_RUN $*"; echo "${verdict_name}: NOT_RUN ($*)"; exit 2; }
159command -v qemu-system-aarch64 >/dev/null || notrun "qemu-system-aarch64 not installed"
160command -v socat >/dev/null || notrun "socat not installed (QMP screendump)"
161code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
162vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
163[[ -r "${code_fd}" && -r "${vars_fd}" ]] || notrun "AAVMF firmware not found"
164
165cross=""
166if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
167commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
168out="${repo_root}/target/native-kernel-screen"
169out_neg="${repo_root}/target/native-kernel-test-stale-handoff"
170make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" >/dev/null
171make -s -C native/kernel CROSS="${cross}" OUT="${out_neg}" AIENOS_COMMIT="${commit}" CK_TEST_STALE_HANDOFF=2 >/dev/null
172tool="$(build_tool "${out}")"
173
174# Machine-wide quiet flag: read only, never written here.
175# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
176# scripts/lib_gate_hold.sh (aienos#278).
177. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
178if ! gh_quiet_check; then
179 echo "NOT_RUN ${gh_why}"
180 echo "${verdict_name}: NOT_RUN"
181 exit 3
182fi
183if ! gh_lock_take qemu_ck_screen_test "${AIENOS_GATE_MINUTES:-60}"; then
184 echo "NOT_RUN ${gh_why}"
185 echo "${verdict_name}: NOT_RUN"
186 exit 3
187fi
188# Release only this run's own gate lock record, at most once.
189release_lock() { gh_lock_release; }
190work="$(mktemp -d)"
191cleanup() { rm -rf "${work}"; release_lock; }
192trap cleanup EXIT
193
194qmp() { # SOCKET JSON-COMMAND -> QEMU's replies
195 printf '{"execute":"qmp_capabilities"}\n%s\n' "$2" | socat -t3 - "UNIX-CONNECT:$1" 2>/dev/null || true
196}
197
198# boot NAME EFI: one QEMU run; sets boot_status, boot_rc, boot_secs.
199boot() {
200 local name="$1" efi="$2" d="${work}/$1" t0 deadline st qpid
201 mkdir -p "${d}/esp/EFI/BOOT" "${d}/esp/EFI/AIENOS"
202 touch "${d}/esp/EFI/AIENOS/BOOTREPORT.TXT"
203 cp "${efi}" "${d}/esp/EFI/BOOT/BOOTAA64.EFI"
204 cp "${vars_fd}" "${d}/vars.fd"
205 t0=$(date +%s); deadline=$(( t0 + ${AIENOS_QEMU_TIMEOUT:-300} ))
206 qemu-system-aarch64 \
207 -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
208 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
209 -drive if=pflash,format=raw,file="${d}/vars.fd" \
210 -drive if=none,id=esp,format=raw,file=fat:rw:"${d}/esp" \
211 -device virtio-blk-pci,drive=esp \
212 -device ramfb -display none -nic none \
213 -serial file:"${d}/serial.raw" \
214 -qmp unix:"${d}/qmp.sock",server=on,wait=off \
215 -action reboot=shutdown,shutdown=pause &
216 qpid=$!
217 boot_status=timeout
218 while (( $(date +%s) < deadline )) && kill -0 "${qpid}" 2>/dev/null; do
219 sleep 2
220 st="$(qmp "${d}/qmp.sock" '{"execute":"query-status"}' | sed -n 's/.*"status": *"\([a-z-]*\)".*/\1/p' | tail -1)"
221 if [[ -n "${st}" && "${st}" != running && "${st}" != prelaunch ]]; then boot_status="${st}"; break; fi
222 done
223 qmp "${d}/qmp.sock" "{\"execute\":\"screendump\",\"arguments\":{\"filename\":\"${d}/screen.ppm\"}}" >"${d}/qmp_screendump.txt"
224 qmp "${d}/qmp.sock" '{"execute":"quit"}' >/dev/null
225 set +e; wait "${qpid}"; boot_rc=$?; set -e
226 boot_secs=$(( $(date +%s) - t0 ))
227 tr -d '\r' <"${d}/serial.raw" >"${d}/serial.txt" 2>/dev/null || : >"${d}/serial.txt"
228 echo "qemu ${name}: status ${boot_status}, exit ${boot_rc} after ${boot_secs} s"
229}
230
231started=$(date +%s)
232boot positive "${out}/BOOTAA64.EFI"; pos_status="${boot_status}"
233boot negative "${out_neg}/BOOTAA64.EFI"; neg_status="${boot_status}"
234release_lock
235echo "commit ${commit:0:12}, both boots in $(( $(date +%s) - started )) s"
236
237failed=0
238pos="$(positive_checks "${work}/positive/serial.txt" "${work}/positive/screen.ppm" "${tool}" "${pos_status}" "${work}/positive/out")" || true
239neg="$(negative_checks "${work}/negative/serial.txt" "${work}/negative/screen.ppm" "${tool}" "${neg_status}" "${work}/negative/out")" || true
240echo "-- default image (GOP console)"; printf '%s\n' "${pos}"
241echo "-- negative control (TEST-only CHANDOF2 record)"; printf '%s\n' "${neg}"
242grep -q '^FAIL' <<<"${pos}${neg}" && failed=1
243grep -E '^(gop|screen): ' "${work}/positive/serial.txt" || true
244grep -E '^screen_row ' "${work}/positive/out.fbcheck.txt" 2>/dev/null | grep -v '||$' | tail -8 || true
245
246if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then
247 for b in positive negative; do
248 cp "${work}/${b}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_screen_${b}_serial.log" 2>/dev/null || true
249 cp "${work}/${b}/screen.ppm" "${AIENOS_LOG_DIR}/qemu_ck_screen_${b}_screendump.ppm" 2>/dev/null || true
250 cp "${work}/${b}/out.fbcheck.txt" "${AIENOS_LOG_DIR}/qemu_ck_screen_${b}_fbcheck.txt" 2>/dev/null || true
251 if command -v ffmpeg >/dev/null && [[ -f "${work}/${b}/screen.ppm" ]]; then
252 ffmpeg -loglevel error -y -i "${work}/${b}/screen.ppm" "${AIENOS_LOG_DIR}/qemu_ck_screen_${b}_screendump.png" || true
253 fi
254 done
255 cp "${work}/positive/out.expected.ppm" "${AIENOS_LOG_DIR}/qemu_ck_screen_positive_expected.ppm" 2>/dev/null || true
256fi
257if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
258 for b in positive negative; do echo "---- ${b} serial console (last 40 lines) ----"; tail -40 "${work}/${b}/serial.txt"; done
259fi
260if [[ "${failed}" == 0 ]]; then echo "${verdict_name}: PASS"; exit 0; fi
261echo "${verdict_name}: FAIL"; exit 1