Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/qemu_ck_imac_swap_test.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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=-A, with the disk-staged swap receiver) and kernel B 8 # (commit=-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 33 set -euo pipefail 34 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 35 cd "${repo_root}" 36 tag="AIENOS_IMAC_SWAP" 37 code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}" 38 vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}" 39 cross="" 40 if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi 41 for 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; } 43 done 44 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; } 45 46 . "${repo_root}/scripts/lib_gate_hold.sh" 47 if ! 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 50 fi 51 work="$(mktemp -d)" 52 qpid="" 53 cleanup() { 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 } 63 trap cleanup EXIT 64 65 out="${work}/out" 66 outdef="${work}/outdef" 67 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 68 # mkimg OUTDIR COMMIT-SUFFIX [make vars...]: builds imac-kernel into OUTDIR 69 mkimg() { 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 } 73 A_WAIT="${AIENOS_SWAP_A_WAIT:-240}" 74 B_WAIT="${AIENOS_SWAP_B_WAIT:-300}" 75 fails=0 76 pass() { echo "PASS $1"; } 77 fail() { echo "FAIL $1"; fails=$((fails + 1)); } 78 79 # ---- builds ---- 80 mkimg "${out}" -A CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 CK_X86_SWAP_TEST_DISK=1 81 cp "${out}/IMACKERN-LAB.EFI" "${work}/KERNA.EFI" 82 mkimg "${out}" -B CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 83 cp "${out}/IMACKERN-LAB.EFI" "${work}/KERNB.EFI" 84 mkimg "${outdef}" -D 85 outpad="${work}/outpad" 86 mkimg "${outpad}" -P CK_X86_REPORT=1 CK_TEST_SLOT_KEY=1 CK_X86_SWAP_TEST_DISK=1 CK_X86_SWAP_TRAMP_PAD=1 87 cp "${outpad}/IMACKERN-LAB.EFI" "${work}/KERNP.EFI" 88 make -s -C native/kernel OUT="${out}" slot-tool >/dev/null 89 make -s -C native/kernel OUT="${out}" gpt-image >/dev/null 90 gpt="${out}/host/ck_gpt_image" 91 SLOT="${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 98 cat "${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) 100 cp "${work}/SLOT_B.bin" "${work}/SLOT_BAD.bin" 101 b=$(od -An -tu1 -j458 -N1 "${work}/SLOT_BAD.bin" | tr -d ' ') 102 printf "\\$(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) 105 mkesp() { # 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 } 112 mkswap() { # image slot-object 113 truncate -s 16M "$1" 114 dd if="$2" of="$1" bs=512 seek=2048 conv=notrunc 2>/dev/null 115 } 116 esp="${work}/esp.img"; mkesp "${esp}" 117 data="${work}/data.img"; truncate -s 1280M "${data}"; "${gpt}" create "${data}" 512 1280 one-old >/dev/null 118 swp="${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). 123 boot() { 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 141 wait_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 } 150 stop_qemu() { kill "${qpid}" 2>/dev/null || true; wait "${qpid}" 2>/dev/null || true; qpid=""; } 151 152 # ---- run 1: the good swap ---- 153 logA="${work}/serialA.log" 154 boot "${esp}" "${data}" "${swp}" "${logA}" 155 wait_for "AIENOS-KERNEL-0 DONE" "${logA}" "${A_WAIT}" || true 156 if grep -aq -- "${commit}-A" "${logA}" && grep -aq "AIENOS-KERNEL-0 DONE" "${logA}"; then 157 pass "1 kernel A boots (commit -A)" 158 else 159 fail "1 kernel A boots (commit -A): DONE not seen in ${A_WAIT} s" 160 fi 161 # Allen's memory is saved after DONE (Allen's window opens, the start count and the taught name go to disk) 162 wait_for "allen: Nice to meet you, Ada" "${logA}" "${A_WAIT}" || true 163 if grep -aq "allen_memory: saved generation" "${logA}"; then 164 pass "1b A saves Allen's memory before the swap" 165 else 166 fail "1b A saves Allen's memory before the swap" 167 fi 168 wait_for "swap: inbox at LBA 2048" "${logA}" "${A_WAIT}" || true 169 if 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 170 wait_for "swap: jumping to new kernel|swap: REJECTED" "${logA}" "${B_WAIT}" || true 171 if grep -aq "swap: verified ok" "${logA}"; then pass "3 swap verifies with ck_slot_verify"; else fail "3 swap verifies"; fi 172 if grep -aq "swap: jumping to new kernel" "${logA}"; then pass "3b swap jumps to B"; else fail "3b swap jumps to B"; fi 173 174 jump_line="$(grep -an "swap: jumping to new kernel" "${logA}" | head -1 | cut -d: -f1 || true)" 175 GA="$(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)" 176 after="${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 179 wait_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 } 189 wait_after "AIENOS-KERNEL-0 DONE" "${B_WAIT}" || true 190 # B's greeting comes after DONE, once it has loaded Allen's memory 191 wait_after "^allen: " "${B_WAIT}" || true 192 if 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" 194 else 195 fail "4 kernel B boots (commit -B) and reaches DONE after the jump" 196 fi 197 b_cores="$(grep -a "^summary: cores=" "${after}" | head -1 | sed -n 's/^summary: cores=\([0-9]*\/[0-9]*\).*/\1/p' || true)" 198 if [[ -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})" 200 else 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')" 202 fi 203 n_jumps="$(grep -ac "swap: jumping to new kernel" "${logA}" || true)" 204 if [[ "${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" 206 else 207 fail "4c one swap: ${n_jumps} jumps, B's inbox line: $(grep -a '^swap: ' "${after}" | head -1 || echo none)" 208 fi 209 if kill -0 "${qpid}" 2>/dev/null; then 210 pass "5 no reset: the QEMU process that booted A is still running with B up" 211 else 212 fail "5 no reset: QEMU ended (guest reset or crash)" 213 fi 214 stop_qemu 215 216 if [[ -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" 218 else 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)" 220 fi 221 222 # ---- run 2: the bad signature must be refused ---- 223 badesp="${work}/esp_bad.img"; mkesp "${badesp}" 224 badswp="${work}/swap_bad.img"; mkswap "${badswp}" "${work}/SLOT_BAD.bin" 225 badata="${work}/data_bad.img"; truncate -s 1280M "${badata}"; "${gpt}" create "${badata}" 512 1280 one-old >/dev/null 226 logR="${work}/serialR.log" 227 boot "${badesp}" "${badata}" "${badswp}" "${logR}" 228 wait_for "swap: REJECTED" "${logR}" "${B_WAIT}" || true 229 sleep 5 230 if grep -aq "swap: REJECTED (signature does not verify)" "${logR}"; then 231 pass "7a bad signature refused: REJECTED (signature does not verify)" 232 else 233 fail "7a bad signature refused: $(grep -a 'swap:' "${logR}" | tail -2 | tr '\n' ' ' || echo 'no swap line')" 234 fi 235 if grep -aq "swap: jumping to new kernel" "${logR}"; then 236 fail "7b bad signature: the swap jumped anyway" 237 else 238 pass "7b bad signature: no jump" 239 fi 240 if grep -aq "AIENOS-KERNEL-0 DONE" "${logR}" && kill -0 "${qpid}" 2>/dev/null; then 241 pass "7c A keeps running after the refusal" 242 else 243 fail "7c A keeps running after the refusal" 244 fi 245 stop_qemu 246 247 # ---- run 3: a trampoline larger than its page must be refused before the quiesce ---- 248 padesp="${work}/esp_pad.img"; mkesp "${padesp}" "${work}/KERNP.EFI" 249 padswp="${work}/swap_pad.img"; mkswap "${padswp}" "${work}/SLOT_B.bin" 250 padata="${work}/data_pad.img"; truncate -s 1280M "${padata}"; "${gpt}" create "${padata}" 512 1280 one-old >/dev/null 251 logP="${work}/serialP.log" 252 boot "${padesp}" "${padata}" "${padswp}" "${logP}" 253 wait_for "swap: refused \(the trampoline|swap: trampoline too large|swap: jumping to new kernel" "${logP}" "${B_WAIT}" || true 254 sleep 10 255 n_ref="$(grep -acE "swap: refused \(the trampoline does not fit its page\)|swap: trampoline too large" "${logP}" || true)" 256 stops="$(grep -aE "other cores stopped|xHCI quiesced for swap|net_tg3: stopped|HDA stopped|screen: panel off \(register" "${logP}" | tr '\n' ' ' || true)" 257 if 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" 262 else 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)" 264 fi 265 stop_qemu 266 267 # ---- source and image checks ---- 268 if 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)" 272 else 273 fail "8 swap verify path is not the netboot function" 274 fi 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. 278 syms_def="$("${cross}nm" "${outdef}/imackern/imackern.elf" 2>/dev/null || true)" 279 if 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" 282 else 283 fail "9 the default image links a swap receiver symbol (or has no symbols to check)" 284 fi 285 286 [[ ${fails} -eq 0 ]] && echo "${tag}: PASS" && exit 0 287 echo "${tag}: FAIL (${fails} failures)" 288 exit 1