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

313 lines · 17777 bytes Raw Blame History
1#!/usr/bin/env bash
2# qemu_ck_allen_memory_test.sh -- QEMU test for Allen's memory and notes on a hard
3# disk (native/kernel/x86/memory_x86.c, dev/gpt_add.c, svc/allen_memory.c). QEMU is
4# an emulator: a PASS here qualifies nothing about any real machine or disk.
5#
6# Builds IMACKERN.EFI (the default image) and boots it in QEMU q35 with OVMF, a
7# usb-kbd and a second AHCI disk whose IDENTIFY model is "APPLE HDD ST1000DM003"
8# (on the approved list). That disk starts as ck_gpt_image "one-old": one existing
9# partition (sentinel-a, filled with a pattern) and the rest unused.
10# boot 1 the kernel lists the partition, adds a 1 GiB AIENOS partition in the
11# unused space, finds the memory empty and saves start 1; the setup
12# wizard opens (a first start) and is left with Escape at each step;
13# then keys typed from outside the guest: "my name is drake", "remember
14# my favorite color is blue", "save the gate code is 4471", "goodbye"
15# (each change saved, the note before the copy that counts it; QEMU
16# powers off). Then, on the host: the kernel's own parser finds the
17# AIENOS partition, and every block that changed is a GPT block, one of
18# the two memory copies, note 0 or one of the two setup copies; the
19# sentinel partition is byte-identical.
20# boot 2 the same disk: the partition is found (not added again), memory
21# loads (1 fact, name known, 1 earlier start) and 1 note, Allen speaks
22# first ("Welcome back, Drake. I still remember 1 thing you taught me,
23# and your 1 note."), answers "what is my favorite color", "what is
24# the gate code" and "how many times have you restarted" from the
25# disk, then goodbye (the unsupported QEMU display selects ACPI S5).
26# other a copy of the disk after boot 1 on another machine, under a model
27# on no list ("WDC WD10EZEX"): the section alone makes it Allen's,
28# memory and the note load (booting anywhere).
29# fresh a fresh "one-old" disk under that unlisted model: no section, not
30# approved, nothing written.
31# whole an approved disk whose one partition fills it ("one-whole"): nothing
32# is written, Allen says it remembers only until the computer turns off.
33# The disk holding the kernel (model "QEMU HARDDISK") must never be written.
34# Final line: AIENOS_ALLEN_MEMORY_QEMU: PASS|FAIL|NOT_RUN exit 0 / 1 / 2 (tools) / 3
35set -euo pipefail
36repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
37cd "${repo_root}"
38tag="AIENOS_ALLEN_MEMORY_QEMU"
39code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}"
40vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}"
41cross=""
42if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi
43for t in qemu-system-x86_64 mformat mmd mcopy cmp sha256sum "${cross}gcc" "${cross}ld"; do
44 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; }
45done
46[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; }
47out="${IMACKERN_OUT:-${repo_root}/target/native-kernel-allen-memory}"
48commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
49make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null
50make -s -C native/kernel OUT="${out}" gpt-image >/dev/null
51gpt="${out}/host/ck_gpt_image"
52
53. "${repo_root}/scripts/lib_gate_hold.sh"
54if ! gh_quiet_check || ! gh_lock_take qemu_ck_allen_memory_test "${AIENOS_GATE_MINUTES:-30}"; then
55 echo "${tag}: NOT_RUN (${gh_why})"
56 exit 3
57fi
58work="$(mktemp -d)"
59pids=()
60cleanup() {
61 local p
62 for p in "${pids[@]}"; do kill "${p}" 2>/dev/null || true; done
63 rm -rf "${work}"
64 gh_lock_release
65}
66trap cleanup EXIT
67
68esp="${work}/esp.img"
69truncate -s 64M "${esp}"
70mformat -i "${esp}" -F ::
71mmd -i "${esp}" ::/EFI ::/EFI/BOOT
72mcopy -i "${esp}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI
73"${gpt}" create "${work}/hdd.img" 512 2048 one-old >"${work}/hdd.create"
74"${gpt}" create "${work}/whole.img" 512 2048 one-whole >/dev/null
75"${gpt}" create "${work}/fresh.img" 512 2048 one-old >/dev/null
76cp --sparse=always "${work}/hdd.img" "${work}/hdd.before"
77cp --sparse=always "${work}/whole.img" "${work}/whole.before"
78cp --sparse=always "${work}/fresh.img" "${work}/fresh.before"
79approved_model="APPLE HDD ST1000DM003"
80other_model="WDC WD10EZEX"
81
82fails=0
83pass() { echo "PASS $1"; }
84fail() { echo "FAIL $1"; fails=$((fails + 1)); }
85chk() { # description, file, pattern
86 if grep -aqE -- "$3" "$2"; then pass "$1"; else fail "$1 (no match for: $3)"; fi
87}
88nochk() {
89 if grep -aqE -- "$3" "$2"; then fail "$1 (unexpected: $3)"; else pass "$1"; fi
90}
91txt() { tr -d '\r' <"${work}/$1.log" 2>/dev/null || true; }
92has() { txt "$1" | grep -aqE -- "$2"; }
93wait_for() { # name seconds pattern
94 local i
95 for ((i = 0; i < $2 * 5; i++)); do
96 has "$1" "$3" && return 0
97 sleep 0.2
98 done
99 return 1
100}
101
102start() { # name disk-image [disk-model]
103 local name="$1" disk="$2" model="${3:-${approved_model}}"
104 cp "${vars_fd}" "${work}/${name}.vars"
105 cp "${esp}" "${work}/${name}.esp"
106 mkfifo "${work}/${name}.mon.in" "${work}/${name}.mon.out"
107 : >"${work}/${name}.log"
108 qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp 2 -m 1024 \
109 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
110 -drive if=pflash,format=raw,file="${work}/${name}.vars" \
111 -drive if=none,id=esp,format=raw,file="${work}/${name}.esp" -device ide-hd,drive=esp,bus=ide.0 \
112 -drive if=none,id=hdd,format=raw,file="${disk}" \
113 -device ide-hd,drive=hdd,bus=ide.1,model="${model}" \
114 -device qemu-xhci,id=xhci -device usb-kbd,bus=xhci.0,port=1 \
115 -chardev pipe,id=mon,path="${work}/${name}.mon" -mon chardev=mon,mode=readline \
116 -display none -nic none -serial file:"${work}/${name}.log" -no-reboot 2>"${work}/${name}.err" &
117 pids+=($!)
118 eval "pid_${name}=$!"
119 cat "${work}/${name}.mon.out" >/dev/null &
120 pids+=($!)
121}
122running() { local v="pid_$1"; kill -0 "${!v}" 2>/dev/null; }
123stop() { local v="pid_$1"; kill "${!v}" 2>/dev/null || true; }
124# say name "text": type a line through the QEMU monitor (outside the guest), then Enter.
125say() {
126 local name="$1" text="$2" c k i
127 exec 3>"${work}/${name}.mon.in"
128 for ((i = 0; i < ${#text}; i++)); do
129 c="${text:i:1}"
130 case "${c}" in
131 " ") k=spc ;;
132 [a-z0-9]) k="${c}" ;;
133 *) echo "say: no key for '${c}'" >&2; exit 2 ;;
134 esac
135 echo "sendkey ${k}" >&3
136 sleep 0.15
137 done
138 echo "sendkey ret" >&3
139 exec 3>&-
140}
141off() { # name: QEMU has no supported panel, so goodbye selects ACPI S5 after the black frame
142 local r=1
143 wait_for "$1" 60 '^power: off via ACPI S5 ' && r=0
144 stop "$1"
145 return $r
146}
147ready='^keyboard: type a line and press Enter'
148
149# ---- boot 1 (and the whole-disk machine alongside) ----
150start one "${work}/hdd.img"
151start whole "${work}/whole.img"
152b1_off=0
153b1_setup=0
154if wait_for one 300 "${ready}" && wait_for one 60 '^allen_memory: saved generation 1 '; then
155 # A first start on a disk with a section opens the setup wizard
156 # (x86/setup_x86.c), which takes every key whole until it finishes; lines
157 # typed before that never reach Allen. Leave each step with Escape, as a
158 # person declining the optional consents would, then talk to him.
159 for step in intent scan consent llm ready; do
160 if wait_for one 40 "^wizard: step ${step}$"; then
161 printf 'sendkey esc\n' >"${work}/one.mon.in"
162 sleep 0.2
163 else
164 break
165 fi
166 done
167 wait_for one 40 '^wizard: finished' && b1_setup=1
168 say one "my name is drake"
169 wait_for one 120 '^allen_memory: saved generation 2 ' || true
170 say one "remember my favorite color is blue"
171 wait_for one 120 '^allen_memory: saved generation 3 ' || true
172 say one "save the gate code is 4471"
173 wait_for one 120 '^allen_memory: saved generation 4 ' || true
174 say one "goodbye"
175 off one && b1_off=1
176fi
177stop one
178wait_for whole 300 "${ready}" || true
179stop whole
180txt one >"${work}/one.txt"
181txt whole >"${work}/whole.txt"
182
183echo "== boot 1: first start on a disk with one old partition and unused space =="
184chk "1: the approved disk found" "${work}/one.txt" '^allen_memory: no section yet; the approved disk is disk [0-9]+ \([0-9]+ blocks of 512 bytes\)$'
185chk "1: the old partition listed" "${work}/one.txt" '^ahci_part: disk=[0-9]+ entry=[0-9]+ type=[0-9a-f-]+ first_lba=2048 last_lba=18431 mib=8 name="'
186chk "1: one partition counted" "${work}/one.txt" '^allen_memory: the disk has 1 partition$'
187chk "1: AIENOS partition added in unused space" "${work}/one.txt" '^allen_memory: AIENOS partition added, entry [0-9]+, first_lba=[0-9]+ last_lba=[0-9]+ \(1024 MiB\); the existing partitions were not touched$'
188chk "1: write ranges closed after the add" "${work}/one.txt" 'write ranges closed$'
189chk "1: memory empty on a first start" "${work}/one.txt" '^allen_memory: empty \(a first start\); Allen starts fresh$'
190chk "1: setup opens on the first start" "${work}/one.txt" '^wizard: step hello$'
191[[ ${b1_setup} == 1 ]] && pass "1: setup finished before the typed lines" || fail "1: setup did not finish; the typed lines went into it"
192chk "1: no notes yet" "${work}/one.txt" '^allen_memory: 0 notes loaded \(0 damaged\)$'
193chk "1: start 1 saved" "${work}/one.txt" '^allen_memory: saved generation 1 to copy [AB] \(0 facts, 0 notes\) in [0-9]+ ms$'
194chk "1: Allen learns the name" "${work}/one.txt" "^allen: Nice to meet you, Drake\. I'll remember that\.\$"
195chk "1: the name saved" "${work}/one.txt" '^allen_memory: saved generation 2 to copy [AB] \(0 facts'
196chk "1: Allen is taught a fact" "${work}/one.txt" "^allen: Got it: your favorite color is blue\. I'll remember that\.\$"
197chk "1: the fact saved" "${work}/one.txt" '^allen_memory: saved generation 3 to copy [AB] \(1 fact, 0 notes\)'
198chk "1: Allen saves a note" "${work}/one.txt" "^allen: Saved\. It's your first note\.\$"
199chk "1: the note written" "${work}/one.txt" '^allen_memory: note 0 written in [0-9]+ ms$'
200chk "1: then the copy that counts it" "${work}/one.txt" '^allen_memory: saved generation 4 to copy [AB] \(1 fact, 1 note\)'
201if grep -anE '^allen_memory: (note 0 written|saved generation 4 )' "${work}/one.txt" | head -1 | grep -q 'note 0 written'; then
202 pass "1: the note is on disk before the copy that counts it"
203else
204 fail "1: the note was not written first"
205fi
206chk "1: goodbye saves the line count" "${work}/one.txt" '^allen_memory: saved generation 5 to copy [AB] \(1 fact, 1 note\)'
207chk "1: ACPI S5 after goodbye" "${work}/one.txt" '^power: off via ACPI S5 '
208[[ ${b1_off} == 1 ]] && pass "1: S5 requested before QEMU cleanup" || fail "1: no S5 after goodbye"
209nochk "1: no write refused by the fence" "${work}/one.txt" 'ahci_write: .*refused'
210nochk "1: no failed save" "${work}/one.txt" 'save FAILED'
211
212echo "== the disk after boot 1, read on the host =="
213found="$("${gpt}" find "${work}/hdd.img" 512 || true)"
214echo "${found}"
215pf="$(sed -nE 's/^aienos first_lba=([0-9]+) last_lba=([0-9]+) blocks=[0-9]+$/\1/p' <<<"${found}")"
216pl="$(sed -nE 's/^aienos first_lba=([0-9]+) last_lba=([0-9]+) blocks=[0-9]+$/\2/p' <<<"${found}")"
217if [[ -n "${pf}" && $((pl - pf + 1)) == 2097152 ]]; then pass "host: the kernel's parser finds one 1 GiB AIENOS partition"; else fail "host: no 1 GiB AIENOS partition"; fi
218if [[ -n "${pf}" ]] && grep -aqE "first_lba=${pf} last_lba=${pl} " "${work}/one.txt"; then pass "host: same range the kernel reported"; else fail "host: range differs from the kernel's"; fi
219blocks=$(( $(stat -c %s "${work}/hdd.img") / 512 ))
220# Every changed byte must be in LBA 1..33, the last 33 LBAs, the two 4096-byte copies
221# or note 0 (the third 4096-byte unit): the first 24 blocks of the partition; or the
222# setup wizard's two copies (svc/setup_store.h: units CK_SETUP_FIRST =
223# CK_ALLEN_DISK_UNITS = 2 + 512 notes and the next, so blocks 4112..4127).
224bad="$(cmp -l "${work}/hdd.before" "${work}/hdd.img" 2>/dev/null | awk -v pf="${pf:-0}" -v last=$((blocks - 1)) '
225 { lba = int(($1 - 1) / 512); n++
226 if (lba == 0) { print "lba 0 changed"; exit }
227 if (lba >= 1 && lba <= 33) next
228 if (lba >= last - 32 && lba <= last) next
229 if (pf > 0 && lba >= pf && lba < pf + 24) next
230 if (pf > 0 && lba >= pf + 4112 && lba < pf + 4128) next
231 print "lba " lba " changed"; exit }
232 END { if (n == 0) print "nothing changed" }' || true)"
233[[ -z "${bad}" ]] && pass "host: only the GPT, the two memory copies, note 0 and the setup copies changed" || fail "host: ${bad}"
234s0="$(dd if="${work}/hdd.before" bs=512 skip=2048 count=16384 status=none | sha256sum)"
235s1="$(dd if="${work}/hdd.img" bs=512 skip=2048 count=16384 status=none | sha256sum)"
236[[ "${s0}" == "${s1}" ]] && pass "host: the old partition is byte-identical" || fail "host: the old partition changed"
237cmp -s "${work}/whole.before" "${work}/whole.img" && pass "whole: disk byte-identical" || fail "whole: disk was written"
238
239echo "== whole: one partition fills the disk =="
240chk "whole: the approved disk found" "${work}/whole.txt" '^allen_memory: no section yet; the approved disk is disk '
241chk "whole: AIENOS partition not added, nothing written" "${work}/whole.txt" '^allen_memory: AIENOS partition not added: .*; nothing was written$'
242chk "whole: Allen remembers only until power off" "${work}/whole.txt" '^allen_memory: no AIENOS partition; Allen remembers only until this computer turns off$'
243nochk "whole: no save" "${work}/whole.txt" '^allen_memory: saved'
244
245# ---- boot 2: the same disk; alongside, a copy of it on another machine, and a fresh unlisted disk ----
246cp --sparse=always "${work}/hdd.img" "${work}/other.img"
247start two "${work}/hdd.img"
248start other "${work}/other.img" "${other_model}"
249start fresh "${work}/fresh.img" "${other_model}"
250b2_off=0
251if wait_for two 300 "${ready}"; then
252 wait_for two 60 '^allen: Welcome back' || true
253 sleep 3
254 say two "what is my favorite color"
255 wait_for two 120 '^allen: Your favorite color is blue\.$' || true
256 sleep 3
257 say two "what is the gate code"
258 wait_for two 120 '^allen: From your notes' || true
259 sleep 3
260 say two "how many times have you restarted"
261 wait_for two 120 '^allen: This is start number' || true
262 sleep 3
263 say two "goodbye"
264 off two && b2_off=1
265fi
266stop two
267wait_for other 300 '^allen: Welcome back' || true
268stop other
269wait_for fresh 300 "${ready}" || true
270stop fresh
271txt two >"${work}/two.txt"
272txt other >"${work}/other.txt"
273txt fresh >"${work}/fresh.txt"
274
275echo "== boot 2: the same disk after a stop =="
276chk "2: Allen's section found" "${work}/two.txt" '^allen_memory: Allen'"'"'s section is on disk [0-9]+ \([0-9]+ blocks of 512 bytes\)$'
277chk "2: AIENOS partition found, not added again" "${work}/two.txt" '^allen_memory: AIENOS partition found, entry [0-9]+, first_lba='"${pf:-x}"' last_lba='"${pl:-x}"'$'
278nochk "2: no second partition" "${work}/two.txt" 'AIENOS partition added'
279chk "2: completed setup stays closed" "${work}/two.txt" '^wizard: setup finished on an earlier start'
280chk "2: memory loaded" "${work}/two.txt" '^allen_memory: loaded generation 5 \(2 of 2 copies valid\): 1 fact, name known, 1 earlier start$'
281chk "2: the note loaded" "${work}/two.txt" '^allen_memory: 1 note loaded \(0 damaged\)$'
282chk "2: start 2 saved" "${work}/two.txt" '^allen_memory: saved generation 6 '
283chk "2: Allen welcomes Drake back first" "${work}/two.txt" '^allen: Welcome back, Drake\. I still remember 1 thing you taught me, and your 1 note\.$'
284chk "2: the fact remembered across the power off" "${work}/two.txt" '^allen: Your favorite color is blue\.$'
285chk "2: the note handed back across the power off" "${work}/two.txt" '^allen: From your notes: the gate code is 4471\.$'
286chk "2: the start count remembered" "${work}/two.txt" "^allen: This is start number 2 for me, and I've answered [0-9]+ lines in all\.\$"
287chk "2: ACPI S5 after goodbye" "${work}/two.txt" '^power: off via ACPI S5 '
288[[ ${b2_off} == 1 ]] && pass "2: S5 requested before QEMU cleanup" || fail "2: no S5 after goodbye"
289nochk "2: no write refused by the fence" "${work}/two.txt" 'ahci_write: .*refused'
290
291echo "== other: the same section on another machine, under a model on no list =="
292chk "other: the section alone makes the disk Allen's" "${work}/other.txt" '^allen_memory: Allen'"'"'s section is on disk '
293chk "other: memory loaded" "${work}/other.txt" '^allen_memory: loaded generation 5 \(2 of 2 copies valid\): 1 fact, name known'
294chk "other: the note loaded" "${work}/other.txt" '^allen_memory: 1 note loaded \(0 damaged\)$'
295chk "other: Allen welcomes Drake back" "${work}/other.txt" '^allen: Welcome back, Drake\. I still remember 1 thing you taught me, and your 1 note\.$'
296nochk "other: no partition added" "${work}/other.txt" 'AIENOS partition added'
297
298echo "== fresh: an unlisted disk with no section =="
299chk "fresh: nothing chosen" "${work}/fresh.txt" '^allen_memory: no disk holds Allen'"'"'s section and none is approved for one; Allen remembers only until this computer turns off$'
300nochk "fresh: no save" "${work}/fresh.txt" '^allen_memory: saved'
301cmp -s "${work}/fresh.before" "${work}/fresh.img" && pass "fresh: disk byte-identical" || fail "fresh: disk was written"
302
303for r in one two whole other fresh; do
304 nochk "${r}: no panic" "${work}/${r}.txt" '^(panic|power: still running|power: halted)'
305 nochk "${r}: no iMac or Spark named on screen" "${work}/${r}.txt" '^(allen|allen_memory|screen): .*(iMac|Spark)'
306done
307for r in one two whole other fresh; do echo "---- ${r} (memory lines) ----"; grep -aE '^(allen_memory|ahci_part)' "${work}/${r}.txt" || true; done
308if [[ ${fails} == 0 ]]; then
309 echo "${tag}: PASS"
310 exit 0
311fi
312echo "${tag}: FAIL (${fails})"
313exit 1