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

166 lines · 9065 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_gpu_bench_test.sh -- QEMU test for the GPU bench console, driven by the real Spark tool
3# (tools/ck_gpu_bench.c) over a COM1 socket. QEMU is an emulator: the BARs are RAM stand-ins
4# (CK_X86_BENCH_TEST=1, see native/kernel/x86/bench_x86.c), not PCI hardware. A PASS qualifies
5# nothing about the AMD GPU, and the UDP/tg3 transport is not exercised here.
6#
7# The kernel is built as the lab image with the test transport and the test key (CK_TEST_SLOT_KEY=1
8# is the swap owner key; the bench key is the test key in bench_proto.h). The tool connects, waits
9# for "bench: GPU bench console ready", then sends the script below. Every reply is HMAC-checked by
10# the tool with its own SHA-256 (a kernel/tool HMAC mismatch fails the run).
11#
12# Checks:
13# ping round trip; dword config read; BAR read and write (write logged before/after in the
14# kernel); allowlist refusal (RANGE); TSC poll (OK and TIMEOUT); read-modify-write;
15# replay refused (same counter); bad HMAC refused (AUTH); stale counter refused (AUTH);
16# all-ones reply stops the tool (device gone): the command after it is never sent.
17# Final line: AIENOS_GPU_BENCH: PASS|FAIL|NOT_RUN exit 0/1/2/3
18set -euo pipefail
19repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
20cd "${repo_root}"
21tag="AIENOS_GPU_BENCH"
22code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
23vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
24cross=""
25if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
26for t in qemu-system-x86_64 mformat mmd mcopy timeout "${cross}gcc" "${cross}ld"; do
27 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
28done
29[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
30
31. "${repo_root}/scripts/lib_gate_hold.sh"
32if ! gh_quiet_check || ! gh_lock_take qemu_ck_gpu_bench_test "${AIENOS_GATE_MINUTES:-60}"; then
33 echo "${tag}: NOT_RUN (${gh_why})"
34 exit 3
35fi
36work="$(mktemp -d)"
37qpid=""
38cleanup() {
39 [[ -z "${qpid}" ]] || kill "${qpid}" 2>/dev/null || true
40 # a red run keeps its logs for diagnosis
41 if [[ "${fails:-0}" -gt 0 ]]; then
42 keepdir="${HOME}/handoffs/gpu-bench-logs/${tag}-$(date -u +%Y%m%dT%H%M%SZ)"
43 mkdir -p "${keepdir}" && cp "${work}"/*.log "${work}"/*.txt "${keepdir}/" 2>/dev/null && echo "logs kept: ${keepdir}"
44 fi
45 rm -rf "${work}"
46 gh_lock_release
47}
48trap cleanup EXIT
49
50out="${work}/out"
51commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
52make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" \
53 CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 CK_X86_BENCH=1 CK_X86_BENCH_TEST=1 imac-kernel >/dev/null
54make -s -C native/kernel OUT="${out}" ck-gpu-bench >/dev/null
55TOOL="${out}/host/ck_gpu_bench"
56
57img="${work}/esp.img"
58truncate -s 64M "${img}"
59mformat -i "${img}" -F ::
60mmd -i "${img}" ::/EFI ::/EFI/BOOT
61mcopy -i "${img}" "${out}/IMACKERN-LAB.EFI" ::/EFI/BOOT/BOOTX64.EFI
62
63# The script. Register numbers are DWORD indexes: 18432 = 0x4800 (panel allowlist),
64# 256 = 0x100 (not allowlisted), 4095 = 0x0FFF (the emulated all-ones register).
65# Allowlist edges (dce_11_2_d.h, smu_7_1_3_d.h): 6660 = 0x1A04 GRPH_PRIMARY_SURFACE_ADDRESS and
66# 6674 = 0x1A12 GRPH_FLIP_CONTROL (both ends of the DCP0 surface range), 148 = 0x94 SMC_MESSAGE_0,
67# 149 = 0x95 SMC_RESP_0, 164 = 0xA4 SMC_MSG_ARG_0; refused neighbours 6659 = 0x1A03 GRPH_SWAP_CNTL,
68# 6675 = 0x1A13, 150 = 0x96 SMC_MESSAGE_1, 165 = 0xA5 SMC_MSG_ARG_1.
69# The two polls on 0x4800 (holding 0xABCD0001): the first matches the high half; the second waits
70# for a low half of 0x0002 that never comes, so it must end in TIMEOUT.
71cat > "${work}/test.bench" <<'BENCH'
72ping
73pci_cfg 0
74bar5_read 16
75bar5_write 18432 0xABCD0001
76bar5_read 18432
77bar5_write 256 1
78bar0_write 5 0x42
79bar0_read 5
80bar5_poll 18432 0xFFFF0000 0xABCD0000 100
81bar5_poll 18432 0x0000FFFF 0x00000002 50
82bar5_rmw 18432 0x0000FF00 0x00005500
83bar5_write 6660 0x11110001
84bar5_write 6674 0x11110002
85bar5_write 148 0x11110003
86bar5_write 149 0x11110004
87bar5_write 164 0x11110005
88bar5_write 6659 1
89bar5_write 6675 1
90bar5_write 150 1
91bar5_write 165 1
92pci_cfg 256
93replay
94badauth ping
95stale ping
96ping
97bar5_read 4095
98ping
99BENCH
100
101sock="${work}/com1.sock"
102serial_log="${work}/serial.log"
103cp "${vars_fd}" "${work}/vars.fd"
104qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 4 -m 1024 \
105 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
106 -drive if=pflash,format=raw,file="${work}/vars.fd" \
107 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0 \
108 -device qemu-xhci -device usb-kbd \
109 -nographic -monitor none -serial unix:"${sock}",server=on,wait=on -no-reboot >"${serial_log}" 2>&1 &
110qpid=$!
111for _ in $(seq 1 100); do [[ -S "${sock}" ]] && break; sleep 0.2; done
112
113fails=0
114pass() { echo "PASS $1"; }
115fail() { echo "FAIL $1"; fails=$((fails + 1)); }
116chk() { # name regex file
117 if grep -aqE -- "$2" "$3"; then pass "$1"; else fail "$1 (no line matching: $2)"; fi
118}
119
120tool_rc=0
121timeout "${AIENOS_BENCH_TOOL_TIMEOUT:-1500}" "${TOOL}" --serial "${sock}" \
122 --transcript "${work}/transcript.txt" "${work}/test.bench" \
123 >"${work}/tool.out" 2>"${work}/tool.err" || tool_rc=$?
124kill "${qpid}" 2>/dev/null || true; wait "${qpid}" 2>/dev/null || true; qpid=""
125
126tr -d '\r' < "${work}/tool.out" > "${work}/tool.txt"
127echo "--- tool output ---"; cat "${work}/tool.txt"
128echo "--- tool stderr ---"; cat "${work}/tool.err"
129
130chk "kernel: bench console up (test build, emulated BARs)" "bench: GPU bench console ready" "${work}/transcript.txt"
131chk "ping round trip" "^ping OK: result=0x00000000" "${work}/tool.txt"
132chk "config read (dword 0 = 1002:67ef)" "^pci_cfg OK: result=0x67ef1002" "${work}/tool.txt"
133chk "BAR5 read of a known word" "^bar5_read OK: result=0xc0ffee00" "${work}/tool.txt"
134chk "BAR5 write on the allowlist (before 0, after ABCD0001)" "^bar5_write OK: result=0xabcd0001 before=0x00000000" "${work}/tool.txt"
135chk "kernel logged the write before and after" "bench: bar5_write reg=0x4800 0x00000000 -> 0xabcd0001" "${work}/transcript.txt"
136chk "BAR5 read back the written value" "^bar5_read OK: result=0xabcd0001" "${work}/tool.txt"
137chk "BAR5 write off the allowlist refused (RANGE)" "^bar5_write ERR_RANGE: result=0x00000000" "${work}/tool.txt"
138chk "GRPH primary surface address 0x1A04 writable" "^bar5_write OK: result=0x11110001 before=0x00000000" "${work}/tool.txt"
139chk "GRPH flip control 0x1A12 writable (top of the range)" "^bar5_write OK: result=0x11110002 before=0x00000000" "${work}/tool.txt"
140chk "SMC_MESSAGE_0 0x94 writable" "^bar5_write OK: result=0x11110003 before=0x00000000" "${work}/tool.txt"
141chk "SMC_RESP_0 0x95 writable" "^bar5_write OK: result=0x11110004 before=0x00000000" "${work}/tool.txt"
142chk "SMC_MSG_ARG_0 0xA4 writable" "^bar5_write OK: result=0x11110005 before=0x00000000" "${work}/tool.txt"
143chk "kernel logged the GRPH write" "bench: bar5_write reg=0x1a04 0x00000000 -> 0x11110001" "${work}/transcript.txt"
144ranges=$(grep -ac "^bar5_write ERR_RANGE" "${work}/tool.txt" || true)
145if [[ "${ranges}" == "5" ]]; then pass "writes just outside each new range refused (0x1A03, 0x1A13, 0x96, 0xA5, plus 0x100): 5 RANGE"; else fail "bar5_write RANGE refusals: ${ranges} (expected 5)"; fi
146chk "BAR0 write and read back" "^bar0_read OK: result=0x00000042" "${work}/tool.txt"
147chk "TSC poll satisfied" "^bar5_poll OK: result=0xabcd0001" "${work}/tool.txt"
148chk "TSC poll timed out" "^bar5_poll ERR_TIMEOUT" "${work}/tool.txt"
149chk "read-modify-write" "^bar5_rmw OK: result=0xabcd5501 before=0xabcd0001" "${work}/tool.txt"
150chk "config offset out of range refused" "^pci_cfg ERR_RANGE" "${work}/tool.txt"
151chk "replay refused (same counter)" "^replay ERR_AUTH" "${work}/tool.txt"
152chk "bad HMAC refused" "^badauth ping ERR_AUTH" "${work}/tool.txt"
153chk "stale counter refused" "^stale ping ERR_AUTH" "${work}/tool.txt"
154chk "all-ones read reported (device gone)" "^bar5_read ERR_ALLONES" "${work}/tool.txt"
155if grep -aq "device gone (all-ones); stopping" "${work}/tool.err"; then pass "tool stopped at the all-ones reply"; else fail "tool stopped at the all-ones reply"; fi
156pings=$(grep -ac "^ping " "${work}/tool.txt" || true)
157if [[ "${pings}" == "2" ]]; then pass "the ping after the all-ones reply was never sent (2 ping replies, not 3)"; else fail "ping replies: ${pings} (expected 2)"; fi
158reqs=$(grep -ac "^ABCHREQ " "${work}/transcript.txt" || true)
159results=$(grep -acE " (OK|ERR_[A-Z]+): result=" "${work}/tool.txt" || true)
160if [[ "${reqs}" == "${results}" && "${results}" -gt 0 ]]; then pass "one reply per request on the link (${results})"; else fail "requests ${reqs} vs results ${results}"; fi
161if grep -aq "HMAC mismatch\|no response" "${work}/tool.err"; then fail "kernel and tool disagree on an HMAC or a reply"; else pass "every kernel reply carries the HMAC the tool expects"; fi
162if [[ "${tool_rc}" == "1" ]]; then pass "tool exit 1 (the ALLONES stop)"; else fail "tool exit ${tool_rc} (expected 1)"; fi
163
164[[ ${fails} -eq 0 ]] && echo "${tag}: PASS" && exit 0
165echo "${tag}: FAIL (${fails} failures)"
166exit 1