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

335 lines · 19242 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_imac_keyboard_test.sh -- QEMU test for the iMac kernel's USB keyboard
3# (native/kernel/x86/xhci_x86.c). QEMU is an emulator: a PASS here qualifies nothing
4# about a 2017 iMac, its USB controller or any real keyboard.
5#
6# Builds IMACKERN.EFI (the default image) and boots it three times at once in QEMU q35
7# with OVMF and a qemu-xhci controller, plus a fourth machine on the same kernel built
8# with a 25 s idle time (CK_X86_IDLE_S=25, own object directory):
9# hub a usb-kbd behind a usb-hub (the shape of Apple's wired keyboards, which
10# carry their own hub); keys are typed from OUTSIDE the guest through the
11# QEMU monitor ("sendkey"): "Hello Allen" Enter, "xy" Backspace "z?!," Enter, then
12# after 70 s "goodbye" Enter. QEMU's unsupported display selects ACPI S5,
13# confirmed by a guest-shutdown QMP event and clean process exit, not a reset.
14# The kernel must find the keyboard behind the hub (QEMU numbers its USB 2
15# ports after the USB 3 ones, so "port=1.1" shows as e.g. 5.1), echo both lines as
16# "keyboard_line: Hello Allen" and "keyboard_line: xz?!,", have Allen answer them
17# ("allen: Hello Humanity.", "allen: I don't know that yet.", plus the
18# content-free "screen: Allen answered (11 characters in, 15 out)"), switch the
19# screen to Allen's window (a QEMU screendump of it is kept as
20# $out/allen-qemu.png), keep it up with no countdown to power-off (still on and
21# awake 70 s after the last key, the default idle time being 600 s), then
22# power off on "goodbye" rather than falsely claim any-key panel wake;
23# direct a usb-kbd on a root port: found on that port;
24# none the controller with nothing on it: "keyboard: none (no USB keyboard
25# found)" and the normal boot otherwise;
26# idle a usb-kbd on a root port and no key typed: 25 s after Allen's window
27# opens the computer requests rest, then powers off through ACPI S5.
28# The same QMP and process checks distinguish shutdown from reset/halt.
29# Reversible panel-off/any-key wake needs the real supported AMD path: NOT_RUN here.
30# The kernel's lines reach the serial log through COM1, which QEMU has and the iMac
31# does not. Needs qemu-system-x86_64, OVMF, mtools and an x86_64 gcc. Final line:
32# AIENOS_IMAC_KEYBOARD: PASS|FAIL|NOT_RUN [(...)] exit 0 / 1 / 2 (tools) / 3 (NOT_RUN)
33set -euo pipefail
34repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
35cd "${repo_root}"
36tag="AIENOS_IMAC_KEYBOARD"
37code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
38vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
39cross=""
40if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
41for t in qemu-system-x86_64 mformat mmd mcopy "${cross}gcc" "${cross}ld"; do
42 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
43done
44[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
45out="${IMACKERN_OUT:-${repo_root}/target/native-kernel-imac-keyboard}"
46commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
47make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null
48make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}/idle" AIENOS_COMMIT="${commit}" CK_X86_IDLE_S=25 imac-kernel >/dev/null
49
50. "${repo_root}/scripts/lib_gate_hold.sh"
51if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_keyboard_test "${AIENOS_GATE_MINUTES:-30}"; then
52 echo "${tag}: NOT_RUN (${gh_why})"
53 exit 3
54fi
55rm -f "${out}/allen-qemu.png"
56work="$(mktemp -d)"
57pids=()
58qmp_fds=()
59cleanup() {
60 local p fd
61 for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done
62 for fd in "${qmp_fds[@]}"; do exec {fd}>&-; done
63 if [[ -n "${AIENOS_KEEP_WORK:-}" ]]; then echo "work kept: ${work}"; else rm -rf "${work}"; fi
64 gh_lock_release
65}
66trap cleanup EXIT
67
68img="${work}/esp.img"
69truncate -s 64M "${img}"
70mformat -i "${img}" -F ::
71mmd -i "${img}" ::/EFI ::/EFI/BOOT
72mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI
73idle_img="${work}/idle-esp.img"
74truncate -s 64M "${idle_img}"
75mformat -i "${idle_img}" -F ::
76mmd -i "${idle_img}" ::/EFI ::/EFI/BOOT
77mcopy -i "${idle_img}" "${out}/idle/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI
78
79fails=0
80chk() { # description, file, pattern
81 if grep -aqE -- "$3" "$2"; then echo "PASS $1"; else echo "FAIL $1 (no match for: $3)"; fails=$((fails + 1)); fi
82}
83txt() { tr -d '\r' <"${work}/$1.log" 2>/dev/null || true; }
84nohas() {
85 if has "$1" "$2"; then echo "FAIL $3"; fails=$((fails + 1)); else echo "PASS $3"; fi
86}
87has() { txt "$1" | grep -aqE -- "$2"; }
88# wait_for name seconds pattern: 0 once the serial log of run `name` matches.
89wait_for() {
90 local i
91 for ((i = 0; i < $2 * 5; i++)); do
92 has "$1" "$3" && return 0
93 sleep 0.2
94 done
95 return 1
96}
97# QMP's greeting establishes that QEMU has opened the FIFO. Sending before
98# that point and closing the sole O_RDWR descriptor can discard the command.
99# The writer must also stay open after negotiation so events remain enabled.
100qmp_wait() {
101 local name="$1" pattern="$2" i
102 for ((i = 0; i < 150; i++)); do
103 if grep -aqE -- "${pattern}" "${work}/${name}.qmp.log"; then return 0; fi
104 running "${name}" || return 1
105 sleep 0.2
106 done
107 return 1
108}
109
110
111# A serial S5 write is only an attempt. Require QEMU's guest shutdown event
112# and a clean process exit before cleanup can send any signal. -no-reboot
113# alone cannot distinguish successful S5 from a reset/triple-fault exit.
114wait_off() {
115 local name="$1" v="pid_$1" i rc
116 wait_for "${name}" 120 '^power: off via ACPI S5 ' || return 1
117 for ((i = 0; i < 300; i++)); do
118 # Wait for both independently: the QMP drain can run after the
119 # QEMU child exits. Match fields on one event without key-order assumptions.
120 if ! running "${name}" &&
121 grep -aE '"event":[[:space:]]*"SHUTDOWN"' "${work}/${name}.qmp.log" |
122 grep -aqE '"reason":[[:space:]]*"guest-shutdown"'; then
123 if wait "${!v}"; then rc=0; else rc=$?; fi
124 printf '%s\n' "${rc}" >"${work}/${name}.exit"
125 [[ ${rc} == 0 ]]
126 return
127 fi
128 sleep 0.2
129 done
130 return 1
131}
132
133# start name usb-devices...: one QEMU in the background, monitor on a pipe
134# (START_IMG: another ESP image than the default kernel's).
135start() {
136 local name="$1" qmp_fd; shift
137 cp "${vars_fd}" "${work}/${name}.vars"
138 cp "${START_IMG:-${img}}" "${work}/${name}.esp"
139 mkfifo "${work}/${name}.mon.in" "${work}/${name}.mon.out"
140 mkfifo "${work}/${name}.qmp.in" "${work}/${name}.qmp.out"
141 : >"${work}/${name}.qmp.log"
142 cat "${work}/${name}.qmp.out" >"${work}/${name}.qmp.log" &
143 pids+=($!)
144 : >"${work}/${name}.log"
145 qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
146 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
147 -drive if=pflash,format=raw,file="${work}/${name}.vars" \
148 -drive if=none,id=esp,format=raw,file="${work}/${name}.esp" -device ide-hd,drive=esp,bus=ide.0 \
149 -device qemu-xhci,id=xhci "$@" \
150 -chardev pipe,id=mon,path="${work}/${name}.mon" -mon chardev=mon,mode=readline \
151 -qmp pipe:"${work}/${name}.qmp" \
152 -display none -nic none -serial file:"${work}/${name}.log" -no-reboot 2>"${work}/${name}.err" &
153 pids+=($!)
154 eval "pid_${name}=$!"
155 cat "${work}/${name}.mon.out" >/dev/null & # drain monitor output so QEMU never blocks
156 pids+=($!)
157 # Hold a separate writer for every machine until cleanup, even after
158 # capabilities succeeds. A one-command FD can close before QEMU opens
159 # the FIFO, losing the command and therefore every asynchronous event.
160 exec {qmp_fd}<>"${work}/${name}.qmp.in"
161 qmp_fds+=("${qmp_fd}")
162 if ! qmp_wait "${name}" '"QMP":[[:space:]]*\{'; then
163 echo "FAIL ${name}: QMP greeting missing"
164 cat "${work}/${name}.qmp.log"
165 fails=$((fails + 1))
166 return
167 fi
168 printf '{"execute":"qmp_capabilities","id":"caps"}\n' >&"${qmp_fd}"
169 if qmp_wait "${name}" '"id":[[:space:]]*"caps"' &&
170 grep -aE '"id":[[:space:]]*"caps"' "${work}/${name}.qmp.log" |
171 grep -aqE '"return":[[:space:]]*\{[[:space:]]*\}'; then
172 echo "PASS ${name}: QMP capabilities acknowledged before guest shutdown"
173 else
174 echo "FAIL ${name}: QMP capabilities not acknowledged"
175 cat "${work}/${name}.qmp.log"
176 fails=$((fails + 1))
177 fi
178}
179running() { local v="pid_$1"; kill -0 "${!v}" 2>/dev/null; }
180stop() { local v="pid_$1"; kill "${!v}" 2>/dev/null || true; }
181
182start hub -device usb-hub,bus=xhci.0,port=1 -device usb-kbd,bus=xhci.0,port=1.1
183start direct -device usb-kbd,bus=xhci.0,port=1,id=kbd0
184start none
185START_IMG="${idle_img}" start idle -device usb-kbd,bus=xhci.0,port=1
186
187# Keys go in through the QEMU monitor (outside the guest), 0.15 s apart.
188typed_ok=0
189if wait_for hub 300 '^keyboard: type a line and press Enter'; then
190 exec 3>"${work}/hub.mon.in"
191 for k in shift-h e l l o spc shift-a l l e n ret; do echo "sendkey ${k}" >&3; sleep 0.15; done
192 typed_ok=1
193 # Allen's answer appears letter by letter; give it time, then photograph the screen.
194 if wait_for hub 120 "^allen: Hello Humanity\.\$"; then
195 sleep 4
196 echo "screendump ${work}/hub.png -f png" >&3
197 for _ in $(seq 1 50); do [[ -s "${work}/hub.png" ]] && break; sleep 0.2; done
198 sleep 1
199 [[ -s "${work}/hub.png" ]] && cp "${work}/hub.png" "${out}/allen-qemu.png"
200 fi
201 for k in x y backspace z shift-slash shift-1 comma ret; do echo "sendkey ${k}" >&3; sleep 0.15; done
202fi
203# direct: unplug the keyboard from outside the guest, plug one back in, type on it.
204replug_ok=0
205if wait_for direct 300 '^keyboard: type a line and press Enter'; then
206 exec 4>"${work}/direct.mon.in"
207 echo "device_del kbd0" >&4
208 if wait_for direct 30 '^keyboard: unplugged'; then
209 sleep 1
210 echo "device_add usb-kbd,bus=xhci.0,port=1,id=kbd1" >&4
211 for _ in $(seq 1 150); do
212 [[ $(txt direct | grep -acE '^keyboard: ready ') -ge 2 ]] && { replug_ok=1; break; }
213 sleep 0.2
214 done
215 if [[ ${replug_ok} == 1 ]]; then
216 sleep 1
217 for k in o k ret; do echo "sendkey ${k}" >&4; sleep 0.15; done
218 wait_for direct 60 '^keyboard_line: ok$' || true
219 fi
220 fi
221fi
222wait_for direct 300 '^power: Allen.s window stays on' || true
223wait_for none 300 '^power: holding the screen' || true
224stop direct
225stop none
226# idle: no key is typed. The unsupported display must select S5 on its own.
227idle_off=0
228if wait_off idle; then idle_off=1; fi
229stop idle
230# Allen's window has no countdown to power-off, and the default idle time is
231# 600 s: 70 s after the last key (the text screen's hold is 60 s) the hub run
232# must still be up and awake. Then "goodbye" must power off through S5:
233# no panel-off/any-key-wake claim and no automatic reboot.
234stayed=0
235hub_off=0
236if [[ ${typed_ok} == 1 ]] && wait_for hub 60 "^allen: I don't know that yet\."; then
237 sleep 70
238 running hub && ! has hub '^power: off' && ! has hub '^power: (no key|Allen asleep)' && stayed=1
239 for k in g o o d b y e ret; do echo "sendkey ${k}" >&3; sleep 0.15; done
240 if wait_off hub; then hub_off=1; fi
241fi
242stop hub
243
244for r in hub direct none idle; do txt "${r}" >"${work}/${r}.txt"; done
245
246echo "== hub: usb-kbd behind usb-hub =="
247chk "hub: controller running" "${work}/hub.txt" '^xhci: ctrl=0 running version=[0-9a-f]+ ports=[1-9][0-9]* slots=[1-8] context_bytes=(32|64) scratchpads=[0-9]+$'
248chk "hub: hub walked" "${work}/hub.txt" '^usb: port [0-9]+: hub, [1-9][0-9]* ports, (full|high) speed$'
249chk "hub: keyboard found behind the hub" "${work}/hub.txt" '^keyboard: ready \(controller 0, port [0-9]+\.1, full speed, slot [1-8], endpoint 0x8[1-9a-f]\)$'
250chk "hub: honest typing prompt" "${work}/hub.txt" '^keyboard: type a line and press Enter; goodbye switches the display off; any-key wake only after confirmed panel off, otherwise ACPI S5 \(power button to start\)$'
251chk "hub: no countdown, with explicit S5 fallback" "${work}/hub.txt" '^power: Allen.s window stays on; goodbye, idle or a power-button tap switches the display off; confirmed panel off permits any-key wake, otherwise ACPI S5$'
252chk "hub: power button watched" "${work}/hub.txt" '^power: button watched at PM1 status port 0x[0-9a-f]+ '
253chk "hub: default idle time 600 s" "${work}/hub.txt" '^power: this computer rests after 600 s without a key$'
254# QEMU is not the supported AMD display; no MMIO panel writes are permitted.
255chk "hub: display chip listed" "${work}/hub.txt" '^pci: display [0-9a-f:.]+ id=1234:1111 class=030000 bar0=0x[0-9a-f]+ bar5=0x[0-9a-f]+ mem_decode=[01]$'
256chk "hub: unsupported panel reports S5 policy" "${work}/hub.txt" '^screen: panel: the display chip is not an AMD VGA controller; reversible panel off unavailable \(rest uses ACPI S5\)$'
257
258[[ ${typed_ok} == 1 ]] && echo "PASS hub: keys sent from outside the guest" || { echo "FAIL hub: keys not sent"; fails=$((fails + 1)); }
259chk "hub: Allen's window took the screen" "${work}/hub.txt" '^screen: Allen.s window \([0-9]+x[0-9]+, [0-9]+ MiB drawing memory\)$'
260chk "hub: the night drawn by the secondary core, frames timed" "${work}/hub.txt" '^screen: night: [1-9][0-9]* frames before Allen woke, average [0-9]+\.[0-9] ms, slowest [0-9]+\.[0-9] ms \(drawn by a secondary core\)$'
261chk "hub: first line typed" "${work}/hub.txt" '^keyboard_line: Hello Allen$'
262chk "hub: Allen answers Hello Humanity." "${work}/hub.txt" '^allen: Hello Humanity\.$'
263chk "hub: the answer reported by lengths only (11 characters in, 15 out)" "${work}/hub.txt" '^screen: Allen answered \(11 characters in, 15 out\)$'
264[[ -s "${out}/allen-qemu.png" ]] && echo "PASS hub: screenshot of Allen's window kept (${out}/allen-qemu.png)" || { echo "FAIL hub: no screenshot"; fails=$((fails + 1)); }
265chk "hub: backspace edits the line; shifted and plain symbols typed" "${work}/hub.txt" '^keyboard_line: xz\?!,$'
266chk "hub: Allen says what it does not know" "${work}/hub.txt" "^allen: I don't know that yet\."
267[[ ${stayed} == 1 ]] && echo "PASS hub: still on and awake 70 s after the last key (idle time 600 s)" || { echo "FAIL hub: Allen's window did not stay up and awake"; fails=$((fails + 1)); }
268chk "hub: Allen says goodbye" "${work}/hub.txt" '^allen: Goodbye\. Until next time\.$'
269chk "hub: kernel alive" "${work}/hub.txt" '^kernel: alive$'
270[[ ${hub_off} == 1 ]] && echo "PASS hub: guest S5 shutdown and clean QEMU exit after goodbye" || { echo "FAIL hub: no confirmed S5 shutdown after goodbye"; fails=$((fails + 1)); }
271chk "hub: S5 selected instead of black-only rest" "${work}/hub.txt" '^power: panel off unsupported; ACPI S5 fallback \(power button to start; no any-key wake\)$'
272chk "hub: actual ACPI S5 write attempted" "${work}/hub.txt" '^power: off via ACPI S5 '
273nohas hub '^power: (Allen asleep|Allen awake|restart|ACPI S5 failed)' "hub: no false sleep/wake, restart or failed S5"
274nohas hub '^screen: panel (off|on)' "hub: no unsupported panel writes"
275
276
277echo "== direct: usb-kbd on a root port =="
278chk "direct: keyboard found on a root port" "${work}/direct.txt" '^keyboard: ready \(controller 0, port [0-9]+, (full|high) speed, slot 1, endpoint 0x8[1-9a-f]\)$'
279chk "direct: Allen's window stays on" "${work}/direct.txt" '^power: Allen.s window stays on; goodbye, idle or a power-button tap switches the display off; confirmed panel off permits any-key wake, otherwise ACPI S5$'
280
281chk "direct: unplug noticed" "${work}/direct.txt" '^keyboard: unplugged'
282[[ ${replug_ok} == 1 ]] && echo "PASS direct: keyboard found again after replug" || { echo "FAIL direct: no second keyboard: ready after replug"; fails=$((fails + 1)); }
283chk "direct: line typed on the replugged keyboard" "${work}/direct.txt" '^keyboard_line: ok$'
284
285echo "== none: controller, no keyboard =="
286chk "none: controller found" "${work}/none.txt" '^pci: xhci [0-9a-f:.]+ id=1b36:000d '
287chk "none: says there is no keyboard" "${work}/none.txt" '^keyboard: none \(no USB keyboard found\)$'
288chk "none: kernel alive" "${work}/none.txt" '^kernel: alive$'
289if grep -aqE '^keyboard: ready' "${work}/none.txt"; then
290 echo "FAIL none: claimed a keyboard"; fails=$((fails + 1))
291else
292 echo "PASS none: no keyboard claimed"
293fi
294if grep -aqE '^screen: Allen' "${work}/none.txt"; then
295 echo "FAIL none: Allen's window without a keyboard"; fails=$((fails + 1))
296else
297 echo "PASS none: text screen kept without a keyboard"
298fi
299
300echo "== idle: nobody types, idle time 25 s =="
301chk "idle: keyboard found" "${work}/idle.txt" '^keyboard: ready \(controller 0, port [0-9]+, (full|high) speed, slot 1, endpoint 0x8[1-9a-f]\)$'
302chk "idle: idle time 25 s" "${work}/idle.txt" '^power: this computer rests after 25 s without a key$'
303# Either way of resting is accepted: whether QEMU's -cpu max lists MONITOR/MWAIT is QEMU's choice.
304chk "idle: how the cores rest, meter off under a hypervisor" "${work}/idle.txt" '^power: asleep, the drawing cores (rest in mwait \(hint 0x[0-9a-f]+\) and the boot core in mwait|spin \(no MONITOR/MWAIT\) and the boot core halts) between keyboard polls every 20 ms; meter: none \(under a hypervisor\)$'
305chk "idle: the idle timer requested rest" "${work}/idle.txt" '^power: no key for 25 s; the computer rests$'
306chk "idle: unsupported panel selected S5" "${work}/idle.txt" '^power: panel off unsupported; ACPI S5 fallback \(power button to start; no any-key wake\)$'
307[[ ${idle_off} == 1 ]] && echo "PASS idle: guest S5 shutdown and clean QEMU exit without keys" || { echo "FAIL idle: no confirmed S5 shutdown without keys"; fails=$((fails + 1)); }
308nohas idle '^power: (Allen asleep|Allen awake|restart|ACPI S5 failed)' "idle: no false sleep/wake, restart or failed S5"
309nohas idle '^keyboard_line:' "idle: no input needed for S5"
310nohas idle '^screen: panel (off|on)' "idle: no unsupported panel writes"
311if grep -aqE '^power: processor ' "${work}/idle.txt"; then echo "FAIL idle: processor meter read under a hypervisor"; fails=$((fails + 1)); else echo "PASS idle: no processor meter under a hypervisor"; fi
312chk "idle: kernel alive" "${work}/idle.txt" '^kernel: alive$'
313
314for r in hub direct none idle; do
315 if grep -aqE '^(panic|power: still running|power: halted|power: ACPI S5 failed)' "${work}/${r}.txt"; then
316 echo "FAIL ${r}: panic or failed power off"; fails=$((fails + 1))
317 else
318 echo "PASS ${r}: no panic"
319 fi
320done
321for r in hub idle; do
322 boots=$(grep -acE '^report_kind: pre_exit$' "${work}/${r}.txt" || true)
323 if [[ ${boots} == 1 ]]; then echo "PASS ${r}: one boot, no reboot"; else
324 echo "FAIL ${r}: expected one boot, saw ${boots}"; fails=$((fails + 1))
325 fi
326 echo "---- ${r} QMP power events ----"
327 grep -aE '"event".*"(SHUTDOWN|RESET)"' "${work}/${r}.qmp.log" || true
328done
329for r in hub direct none idle; do echo "---- ${r} serial (keyboard, power and panel lines) ----"; grep -aE '^(pci: xhci|pci: display|xhci|usb|keyboard|power|screen: panel)' "${work}/${r}.txt" || true; done
330if [[ ${fails} == 0 ]]; then
331 echo "${tag}: PASS"
332 exit 0
333fi
334echo "${tag}: FAIL (${fails})"
335exit 1