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

288 lines · 15221 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_imac_swap_test.sh -- QEMU test for the AIENOS live kernel swap (disk-staged path).
3# QEMU is an emulator: a PASS here qualifies nothing about a real iMac. The UDP receiver is
4# not built (swap_x86.c: the UDP path is a stub); this test covers the verify, quiesce and
5# jump path with the slot object staged on a disk.
6#
7# Builds lab kernel A (commit=<sha>-A, with the disk-staged swap receiver) and kernel B
8# (commit=<sha>-B), both with the RFC 8032 TEST owner key (CK_TEST_SLOT_KEY=1: QEMU only,
9# never for hardware). B is wrapped as a signed slot object by ck_slot_tool and written raw at
10# LBA 2048 of a third disk. Boot A: A teaches Allen one fact (saves Allen's memory), verifies B
11# with ck_slot_verify (the NETLDR verifier), quiesces and jumps into B in the same QEMU process.
12# A second run stages a bad-signature object and must refuse it. A third run boots a kernel whose
13# trampoline is padded past its page (CK_X86_SWAP_TRAMP_PAD=1) and must refuse before it stops anything.
14#
15# Checks:
16# 1 kernel A boots (commit -A) and saves Allen's memory
17# 1b A saves Allen's memory before the swap
18# 2 A reads the staged slot object
19# 3 the swap verifies with ck_slot_verify ("swap: verified ok") and jumps
20# 4 B boots (commit -B) and reaches DONE, after the jump
21# 4b B starts every core again (the cores A stopped with INIT)
22# 4c one swap only: B, entered by the swap, leaves the staged object on the test disk alone
23# 5 no reset: the QEMU process that started A is still running with B up (-no-reboot
24# would have ended it on a guest reset)
25# 6 Allen's memory is intact: B loads the generation A saved, with the name A taught
26# 7 a bad signature is refused: "REJECTED (signature does not verify)", no jump, A keeps running
27# 7d a trampoline too large for its page is refused once, before the quiesce: no core, xHCI,
28# tg3 or HDA stop line, no jump, A keeps running
29# 8 the verify path is the netboot one: swap_x86.c calls ck_slot_verify, net_loader.c calls
30# it too, and swap_x86.c has no direct aienos_ed25519_verify (no copy of the check)
31# 9 the default image (CK_X86_REPORT=0) links no swap receiver
32# Final line: AIENOS_IMAC_SWAP: PASS|FAIL|NOT_RUN exit 0/1/2/3
33set -euo pipefail
34repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
35cd "${repo_root}"
36tag="AIENOS_IMAC_SWAP"
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 truncate od dd "${cross}gcc" "${cross}ld" "${cross}nm"; 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; }
45
46. "${repo_root}/scripts/lib_gate_hold.sh"
47if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_swap_test "${AIENOS_GATE_MINUTES:-60}"; then
48 echo "${tag}: NOT_RUN (${gh_why})"
49 exit 3
50fi
51work="$(mktemp -d)"
52qpid=""
53cleanup() {
54 [[ -z "${qpid}" ]] || kill "${qpid}" 2>/dev/null || true
55 # a red run keeps its serial logs for diagnosis
56 if [[ "${fails:-0}" -gt 0 ]]; then
57 keepdir="${HOME}/handoffs/gpu-bench-logs/${tag}-$(date -u +%Y%m%dT%H%M%SZ)"
58 mkdir -p "${keepdir}" && cp "${work}"/*.log "${keepdir}/" 2>/dev/null && echo "logs kept: ${keepdir}"
59 fi
60 rm -rf "${work}"
61 gh_lock_release
62}
63trap cleanup EXIT
64
65out="${work}/out"
66outdef="${work}/outdef"
67commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
68# mkimg OUTDIR COMMIT-SUFFIX [make vars...]: builds imac-kernel into OUTDIR
69mkimg() {
70 local od="$1" suffix="$2"; shift 2
71 make -s -C native/kernel X86_CROSS="${cross}" OUT="${od}" AIENOS_COMMIT="${commit}${suffix}" "$@" imac-kernel >/dev/null
72}
73A_WAIT="${AIENOS_SWAP_A_WAIT:-240}"
74B_WAIT="${AIENOS_SWAP_B_WAIT:-300}"
75fails=0
76pass() { echo "PASS $1"; }
77fail() { echo "FAIL $1"; fails=$((fails + 1)); }
78
79# ---- builds ----
80mkimg "${out}" -A CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 CK_X86_SWAP_TEST_DISK=1
81cp "${out}/IMACKERN-LAB.EFI" "${work}/KERNA.EFI"
82mkimg "${out}" -B CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1
83cp "${out}/IMACKERN-LAB.EFI" "${work}/KERNB.EFI"
84mkimg "${outdef}" -D
85outpad="${work}/outpad"
86mkimg "${outpad}" -P CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 CK_X86_SWAP_TEST_DISK=1 CK_X86_SWAP_TRAMP_PAD=1
87cp "${outpad}/IMACKERN-LAB.EFI" "${work}/KERNP.EFI"
88make -s -C native/kernel OUT="${out}" slot-tool >/dev/null
89make -s -C native/kernel OUT="${out}" gpt-image >/dev/null
90gpt="${out}/host/ck_gpt_image"
91SLOT="${out}/host/ck_slot_tool"
92
93# ---- the slot object for B: manifest unit signed with the TEST key, then the kernel ----
94"${SLOT}" make-manifest --image "${work}/KERNB.EFI" --generation 2 --policy-generation 2 \
95 --out "${work}/M.bin" >/dev/null
96"${SLOT}" sign-test "${work}/M.bin" --out "${work}/MS.bin" >/dev/null
97"${SLOT}" verify "${work}/MS.bin" --image "${work}/KERNB.EFI" --test-key >/dev/null
98cat "${work}/MS.bin" "${work}/KERNB.EFI" > "${work}/SLOT_B.bin"
99# a copy with one signature byte flipped (the signature is manifest bytes 448..511; byte 458 is inside)
100cp "${work}/SLOT_B.bin" "${work}/SLOT_BAD.bin"
101b=$(od -An -tu1 -j458 -N1 "${work}/SLOT_BAD.bin" | tr -d ' ')
102printf "\\$(printf '%03o' $((b ^ 1)))" | dd of="${work}/SLOT_BAD.bin" bs=1 seek=458 conv=notrunc 2>/dev/null
103
104# disks: ESP (kernel A as BOOTX64.EFI), data (Allen's memory), swap (slot object raw at LBA 2048)
105mkesp() { # image [kernel]
106 local img="$1"
107 truncate -s 128M "${img}"
108 mformat -i "${img}" -F ::
109 mmd -i "${img}" ::/EFI ::/EFI/BOOT
110 mcopy -i "${img}" "${2:-${work}/KERNA.EFI}" ::/EFI/BOOT/BOOTX64.EFI
111}
112mkswap() { # image slot-object
113 truncate -s 16M "$1"
114 dd if="$2" of="$1" bs=512 seek=2048 conv=notrunc 2>/dev/null
115}
116esp="${work}/esp.img"; mkesp "${esp}"
117data="${work}/data.img"; truncate -s 1280M "${data}"; "${gpt}" create "${data}" 512 1280 one-old >/dev/null
118swp="${work}/swap.img"; mkswap "${swp}" "${work}/SLOT_B.bin"
119
120# boot ESP DATA SWAP SERIAL-LOG: starts QEMU in the background, sets qpid. A USB keyboard is
121# attached because Allen's window (and with it Allen's memory) starts only when a keyboard is ready;
122# -monitor none keeps QEMU from reading this job's stdin (EOF makes QEMU quit).
123boot() {
124 local v="${work}/vars-$RANDOM$RANDOM.fd"
125 # AIENOS_SWAP_QEMU_LOG=FILE (diagnosis only): QEMU logs exceptions and CPU resets there
126 local dbg=()
127 [[ -z "${AIENOS_SWAP_QEMU_LOG:-}" ]] || dbg=(-d int,cpu_reset -D "${AIENOS_SWAP_QEMU_LOG}.$RANDOM")
128 cp "${vars_fd}" "${v}"
129 qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 4 -m 1024 \
130 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
131 -drive if=pflash,format=raw,file="${v}" \
132 -drive if=none,id=esp,format=raw,file="$1" -device ide-hd,drive=esp,bus=ide.0 \
133 -drive if=none,id=data,format=raw,file="$2" -device "ide-hd,drive=data,bus=ide.1,model=APPLE HDD ST1000DM003" \
134 -drive if=none,id=swap,format=raw,file="$3" -device ide-hd,drive=swap,bus=ide.2 \
135 -device qemu-xhci -device usb-kbd \
136 "${dbg[@]}" \
137 -nographic -monitor none -serial file:"$4" -no-reboot 2>/dev/null &
138 qpid=$!
139}
140# wait_for REGEX FILE SECONDS: 0 when REGEX shows up in FILE; stops early if QEMU has exited
141wait_for() {
142 local i
143 for ((i = 0; i < $3; i++)); do
144 grep -aqE -- "$1" "$2" 2>/dev/null && return 0
145 kill -0 "${qpid}" 2>/dev/null || { grep -aqE -- "$1" "$2" 2>/dev/null && return 0; return 1; }
146 sleep 1
147 done
148 return 1
149}
150stop_qemu() { kill "${qpid}" 2>/dev/null || true; wait "${qpid}" 2>/dev/null || true; qpid=""; }
151
152# ---- run 1: the good swap ----
153logA="${work}/serialA.log"
154boot "${esp}" "${data}" "${swp}" "${logA}"
155wait_for "AIENOS-KERNEL-0 DONE" "${logA}" "${A_WAIT}" || true
156if grep -aq -- "${commit}-A" "${logA}" && grep -aq "AIENOS-KERNEL-0 DONE" "${logA}"; then
157 pass "1 kernel A boots (commit -A)"
158else
159 fail "1 kernel A boots (commit -A): DONE not seen in ${A_WAIT} s"
160fi
161# Allen's memory is saved after DONE (Allen's window opens, the start count and the taught name go to disk)
162wait_for "allen: Nice to meet you, Ada" "${logA}" "${A_WAIT}" || true
163if grep -aq "allen_memory: saved generation" "${logA}"; then
164 pass "1b A saves Allen's memory before the swap"
165else
166 fail "1b A saves Allen's memory before the swap"
167fi
168wait_for "swap: inbox at LBA 2048" "${logA}" "${A_WAIT}" || true
169if grep -aq "swap: inbox at LBA 2048" "${logA}"; then pass "2 A reads the staged slot object"; else fail "2 A reads the staged slot object"; fi
170wait_for "swap: jumping to new kernel|swap: REJECTED" "${logA}" "${B_WAIT}" || true
171if grep -aq "swap: verified ok" "${logA}"; then pass "3 swap verifies with ck_slot_verify"; else fail "3 swap verifies"; fi
172if grep -aq "swap: jumping to new kernel" "${logA}"; then pass "3b swap jumps to B"; else fail "3b swap jumps to B"; fi
173
174jump_line="$(grep -an "swap: jumping to new kernel" "${logA}" | head -1 | cut -d: -f1 || true)"
175GA="$(head -n "${jump_line:-0}" "${logA}" | grep -a "allen_memory: saved generation" | tail -1 | sed -n 's/.*saved generation \([0-9][0-9]*\).*/\1/p' || true)"
176after="${work}/after_jump.log"
177# wait_after REGEX SECONDS: 0 when REGEX shows up in B's output; the serial log keeps growing,
178# so the copy of what follows the jump line is taken again on every try
179wait_after() {
180 local i
181 for ((i = 0; i < $2; i++)); do
182 if [[ -n "${jump_line}" ]]; then tail -n +"$((jump_line + 1))" "${logA}" > "${after}"; else : > "${after}"; fi
183 grep -aqE -- "$1" "${after}" && return 0
184 kill -0 "${qpid}" 2>/dev/null || return 1
185 sleep 1
186 done
187 return 1
188}
189wait_after "AIENOS-KERNEL-0 DONE" "${B_WAIT}" || true
190# B's greeting comes after DONE, once it has loaded Allen's memory
191wait_after "^allen: " "${B_WAIT}" || true
192if grep -aq -- "${commit}-B" "${after}" && grep -aq "AIENOS-KERNEL-0 DONE" "${after}"; then
193 pass "4 kernel B boots (commit -B) and reaches DONE after the jump"
194else
195 fail "4 kernel B boots (commit -B) and reaches DONE after the jump"
196fi
197b_cores="$(grep -a "^summary: cores=" "${after}" | head -1 | sed -n 's/^summary: cores=\([0-9]*\/[0-9]*\).*/\1/p' || true)"
198if [[ -n "${b_cores}" && "${b_cores%/*}" == "${b_cores#*/}" && "${b_cores%/*}" -gt 1 ]]; then
199 pass "4b B starts every core again after the swap (cores=${b_cores})"
200else
201 fail "4b B starts every core again after the swap: cores=${b_cores:-none}, $(grep -a '^smp:' "${after}" | head -1 || echo 'no smp line')"
202fi
203n_jumps="$(grep -ac "swap: jumping to new kernel" "${logA}" || true)"
204if [[ "${n_jumps}" -eq 1 ]] && grep -aq "swap: entered by a swap, the test disk inbox is left alone" "${after}"; then
205 pass "4c one swap: B leaves the staged object alone"
206else
207 fail "4c one swap: ${n_jumps} jumps, B's inbox line: $(grep -a '^swap: ' "${after}" | head -1 || echo none)"
208fi
209if kill -0 "${qpid}" 2>/dev/null; then
210 pass "5 no reset: the QEMU process that booted A is still running with B up"
211else
212 fail "5 no reset: QEMU ended (guest reset or crash)"
213fi
214stop_qemu
215
216if [[ -n "${GA}" ]] && grep -aq "allen_memory: loaded generation ${GA} " "${after}" && grep -aq "allen: Welcome back, Ada." "${after}"; then
217 pass "6 Allen's memory intact: B loads generation ${GA} with the name A taught"
218else
219 fail "6 Allen's memory intact: A saved generation '${GA:-none}', B's load line: $(grep -a 'loaded generation' "${after}" | head -1 || echo none)"
220fi
221
222# ---- run 2: the bad signature must be refused ----
223badesp="${work}/esp_bad.img"; mkesp "${badesp}"
224badswp="${work}/swap_bad.img"; mkswap "${badswp}" "${work}/SLOT_BAD.bin"
225badata="${work}/data_bad.img"; truncate -s 1280M "${badata}"; "${gpt}" create "${badata}" 512 1280 one-old >/dev/null
226logR="${work}/serialR.log"
227boot "${badesp}" "${badata}" "${badswp}" "${logR}"
228wait_for "swap: REJECTED" "${logR}" "${B_WAIT}" || true
229sleep 5
230if grep -aq "swap: REJECTED (signature does not verify)" "${logR}"; then
231 pass "7a bad signature refused: REJECTED (signature does not verify)"
232else
233 fail "7a bad signature refused: $(grep -a 'swap:' "${logR}" | tail -2 | tr '\n' ' ' || echo 'no swap line')"
234fi
235if grep -aq "swap: jumping to new kernel" "${logR}"; then
236 fail "7b bad signature: the swap jumped anyway"
237else
238 pass "7b bad signature: no jump"
239fi
240if grep -aq "AIENOS-KERNEL-0 DONE" "${logR}" && kill -0 "${qpid}" 2>/dev/null; then
241 pass "7c A keeps running after the refusal"
242else
243 fail "7c A keeps running after the refusal"
244fi
245stop_qemu
246
247# ---- run 3: a trampoline larger than its page must be refused before the quiesce ----
248padesp="${work}/esp_pad.img"; mkesp "${padesp}" "${work}/KERNP.EFI"
249padswp="${work}/swap_pad.img"; mkswap "${padswp}" "${work}/SLOT_B.bin"
250padata="${work}/data_pad.img"; truncate -s 1280M "${padata}"; "${gpt}" create "${padata}" 512 1280 one-old >/dev/null
251logP="${work}/serialP.log"
252boot "${padesp}" "${padata}" "${padswp}" "${logP}"
253wait_for "swap: refused \(the trampoline|swap: trampoline too large|swap: jumping to new kernel" "${logP}" "${B_WAIT}" || true
254sleep 10
255n_ref="$(grep -acE "swap: refused \(the trampoline does not fit its page\)|swap: trampoline too large" "${logP}" || true)"
256stops="$(grep -aE "other cores stopped|xHCI quiesced for swap|net_tg3: stopped|HDA stopped|screen: panel off \(register" "${logP}" | tr '\n' ' ' || true)"
257if grep -aq "swap: verified ok" "${logP}" && [[ "${n_ref}" -eq 1 && -z "${stops}" ]] \
258 && grep -aq "swap: refused (the trampoline does not fit its page)" "${logP}" \
259 && ! grep -aq "swap: jumping to new kernel" "${logP}" \
260 && grep -aq "AIENOS-KERNEL-0 DONE" "${logP}" && kill -0 "${qpid}" 2>/dev/null; then
261 pass "7d oversized trampoline refused once before the quiesce; nothing stopped, A keeps running"
262else
263 fail "7d oversized trampoline: refusals=${n_ref}, stop lines: ${stops:-none}, last swap lines: $(grep -a 'swap:' "${logP}" | tail -2 | tr '\n' ' ' || echo none)"
264fi
265stop_qemu
266
267# ---- source and image checks ----
268if grep -q "ck_slot_verify(" native/kernel/x86/swap_x86.c \
269 && ! grep -q "aienos_ed25519_verify" native/kernel/x86/swap_x86.c \
270 && grep -q "ck_slot_verify(m, obj" native/boot/net_loader.c; then
271 pass "8 swap verifies through ck_slot_verify, the netboot function (swap_x86.c; net_loader.c:578)"
272else
273 fail "8 swap verify path is not the netboot function"
274fi
275# ck_slot_verify is not on this list: since 0a80baf4 the default image verifies the owner signature
276# with it before boot_disk installs the loader's kernel (memory_x86.c), so only the swap's own
277# trampoline and inbox reader mark a swap receiver.
278syms_def="$("${cross}nm" "${outdef}/imackern/imackern.elf" 2>/dev/null || true)"
279if grep -q ' T ck_x86_kernel_entry$' <<<"${syms_def}" \
280 && ! grep -qE ' [TtDdBbRr] (ck_x86_swap_tramp|ck_x86_swap_read_inbox)$' <<<"${syms_def}"; then
281 pass "9 the default image (CK_X86_REPORT=0) links no swap receiver"
282else
283 fail "9 the default image links a swap receiver symbol (or has no symbols to check)"
284fi
285
286[[ ${fails} -eq 0 ]] && echo "${tag}: PASS" && exit 0
287echo "${tag}: FAIL (${fails} failures)"
288exit 1