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_allen_memory_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_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 35 set -euo pipefail 36 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 37 cd "${repo_root}" 38 tag="AIENOS_ALLEN_MEMORY_QEMU" 39 code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}" 40 vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}" 41 cross="" 42 if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi 43 for 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; } 45 done 46 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; } 47 out="${IMACKERN_OUT:-${repo_root}/target/native-kernel-allen-memory}" 48 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 49 make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null 50 make -s -C native/kernel OUT="${out}" gpt-image >/dev/null 51 gpt="${out}/host/ck_gpt_image" 52 53 . "${repo_root}/scripts/lib_gate_hold.sh" 54 if ! 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 57 fi 58 work="$(mktemp -d)" 59 pids=() 60 cleanup() { 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 } 66 trap cleanup EXIT 67 68 esp="${work}/esp.img" 69 truncate -s 64M "${esp}" 70 mformat -i "${esp}" -F :: 71 mmd -i "${esp}" ::/EFI ::/EFI/BOOT 72 mcopy -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 76 cp --sparse=always "${work}/hdd.img" "${work}/hdd.before" 77 cp --sparse=always "${work}/whole.img" "${work}/whole.before" 78 cp --sparse=always "${work}/fresh.img" "${work}/fresh.before" 79 approved_model="APPLE HDD ST1000DM003" 80 other_model="WDC WD10EZEX" 81 82 fails=0 83 pass() { echo "PASS $1"; } 84 fail() { echo "FAIL $1"; fails=$((fails + 1)); } 85 chk() { # description, file, pattern 86 if grep -aqE -- "$3" "$2"; then pass "$1"; else fail "$1 (no match for: $3)"; fi 87 } 88 nochk() { 89 if grep -aqE -- "$3" "$2"; then fail "$1 (unexpected: $3)"; else pass "$1"; fi 90 } 91 txt() { tr -d '\r' <"${work}/$1.log" 2>/dev/null || true; } 92 has() { txt "$1" | grep -aqE -- "$2"; } 93 wait_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 102 start() { # 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 } 122 running() { local v="pid_$1"; kill -0 "${!v}" 2>/dev/null; } 123 stop() { 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. 125 say() { 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 } 141 off() { # 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 } 147 ready='^keyboard: type a line and press Enter' 148 149 # ---- boot 1 (and the whole-disk machine alongside) ---- 150 start one "${work}/hdd.img" 151 start whole "${work}/whole.img" 152 b1_off=0 153 b1_setup=0 154 if 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 176 fi 177 stop one 178 wait_for whole 300 "${ready}" || true 179 stop whole 180 txt one >"${work}/one.txt" 181 txt whole >"${work}/whole.txt" 182 183 echo "== boot 1: first start on a disk with one old partition and unused space ==" 184 chk "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\)$' 185 chk "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="' 186 chk "1: one partition counted" "${work}/one.txt" '^allen_memory: the disk has 1 partition$' 187 chk "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$' 188 chk "1: write ranges closed after the add" "${work}/one.txt" 'write ranges closed$' 189 chk "1: memory empty on a first start" "${work}/one.txt" '^allen_memory: empty \(a first start\); Allen starts fresh$' 190 chk "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" 192 chk "1: no notes yet" "${work}/one.txt" '^allen_memory: 0 notes loaded \(0 damaged\)$' 193 chk "1: start 1 saved" "${work}/one.txt" '^allen_memory: saved generation 1 to copy [AB] \(0 facts, 0 notes\) in [0-9]+ ms$' 194 chk "1: Allen learns the name" "${work}/one.txt" "^allen: Nice to meet you, Drake\. I'll remember that\.\$" 195 chk "1: the name saved" "${work}/one.txt" '^allen_memory: saved generation 2 to copy [AB] \(0 facts' 196 chk "1: Allen is taught a fact" "${work}/one.txt" "^allen: Got it: your favorite color is blue\. I'll remember that\.\$" 197 chk "1: the fact saved" "${work}/one.txt" '^allen_memory: saved generation 3 to copy [AB] \(1 fact, 0 notes\)' 198 chk "1: Allen saves a note" "${work}/one.txt" "^allen: Saved\. It's your first note\.\$" 199 chk "1: the note written" "${work}/one.txt" '^allen_memory: note 0 written in [0-9]+ ms$' 200 chk "1: then the copy that counts it" "${work}/one.txt" '^allen_memory: saved generation 4 to copy [AB] \(1 fact, 1 note\)' 201 if 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" 203 else 204 fail "1: the note was not written first" 205 fi 206 chk "1: goodbye saves the line count" "${work}/one.txt" '^allen_memory: saved generation 5 to copy [AB] \(1 fact, 1 note\)' 207 chk "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" 209 nochk "1: no write refused by the fence" "${work}/one.txt" 'ahci_write: .*refused' 210 nochk "1: no failed save" "${work}/one.txt" 'save FAILED' 211 212 echo "== the disk after boot 1, read on the host ==" 213 found="$("${gpt}" find "${work}/hdd.img" 512 || true)" 214 echo "${found}" 215 pf="$(sed -nE 's/^aienos first_lba=([0-9]+) last_lba=([0-9]+) blocks=[0-9]+$/\1/p' <<<"${found}")" 216 pl="$(sed -nE 's/^aienos first_lba=([0-9]+) last_lba=([0-9]+) blocks=[0-9]+$/\2/p' <<<"${found}")" 217 if [[ -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 218 if [[ -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 219 blocks=$(( $(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). 224 bad="$(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}" 234 s0="$(dd if="${work}/hdd.before" bs=512 skip=2048 count=16384 status=none | sha256sum)" 235 s1="$(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" 237 cmp -s "${work}/whole.before" "${work}/whole.img" && pass "whole: disk byte-identical" || fail "whole: disk was written" 238 239 echo "== whole: one partition fills the disk ==" 240 chk "whole: the approved disk found" "${work}/whole.txt" '^allen_memory: no section yet; the approved disk is disk ' 241 chk "whole: AIENOS partition not added, nothing written" "${work}/whole.txt" '^allen_memory: AIENOS partition not added: .*; nothing was written$' 242 chk "whole: Allen remembers only until power off" "${work}/whole.txt" '^allen_memory: no AIENOS partition; Allen remembers only until this computer turns off$' 243 nochk "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 ---- 246 cp --sparse=always "${work}/hdd.img" "${work}/other.img" 247 start two "${work}/hdd.img" 248 start other "${work}/other.img" "${other_model}" 249 start fresh "${work}/fresh.img" "${other_model}" 250 b2_off=0 251 if 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 265 fi 266 stop two 267 wait_for other 300 '^allen: Welcome back' || true 268 stop other 269 wait_for fresh 300 "${ready}" || true 270 stop fresh 271 txt two >"${work}/two.txt" 272 txt other >"${work}/other.txt" 273 txt fresh >"${work}/fresh.txt" 274 275 echo "== boot 2: the same disk after a stop ==" 276 chk "2: Allen's section found" "${work}/two.txt" '^allen_memory: Allen'"'"'s section is on disk [0-9]+ \([0-9]+ blocks of 512 bytes\)$' 277 chk "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}"'$' 278 nochk "2: no second partition" "${work}/two.txt" 'AIENOS partition added' 279 chk "2: completed setup stays closed" "${work}/two.txt" '^wizard: setup finished on an earlier start' 280 chk "2: memory loaded" "${work}/two.txt" '^allen_memory: loaded generation 5 \(2 of 2 copies valid\): 1 fact, name known, 1 earlier start$' 281 chk "2: the note loaded" "${work}/two.txt" '^allen_memory: 1 note loaded \(0 damaged\)$' 282 chk "2: start 2 saved" "${work}/two.txt" '^allen_memory: saved generation 6 ' 283 chk "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\.$' 284 chk "2: the fact remembered across the power off" "${work}/two.txt" '^allen: Your favorite color is blue\.$' 285 chk "2: the note handed back across the power off" "${work}/two.txt" '^allen: From your notes: the gate code is 4471\.$' 286 chk "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\.\$" 287 chk "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" 289 nochk "2: no write refused by the fence" "${work}/two.txt" 'ahci_write: .*refused' 290 291 echo "== other: the same section on another machine, under a model on no list ==" 292 chk "other: the section alone makes the disk Allen's" "${work}/other.txt" '^allen_memory: Allen'"'"'s section is on disk ' 293 chk "other: memory loaded" "${work}/other.txt" '^allen_memory: loaded generation 5 \(2 of 2 copies valid\): 1 fact, name known' 294 chk "other: the note loaded" "${work}/other.txt" '^allen_memory: 1 note loaded \(0 damaged\)$' 295 chk "other: Allen welcomes Drake back" "${work}/other.txt" '^allen: Welcome back, Drake\. I still remember 1 thing you taught me, and your 1 note\.$' 296 nochk "other: no partition added" "${work}/other.txt" 'AIENOS partition added' 297 298 echo "== fresh: an unlisted disk with no section ==" 299 chk "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$' 300 nochk "fresh: no save" "${work}/fresh.txt" '^allen_memory: saved' 301 cmp -s "${work}/fresh.before" "${work}/fresh.img" && pass "fresh: disk byte-identical" || fail "fresh: disk was written" 302 303 for 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)' 306 done 307 for r in one two whole other fresh; do echo "---- ${r} (memory lines) ----"; grep -aE '^(allen_memory|ahci_part)' "${work}/${r}.txt" || true; done 308 if [[ ${fails} == 0 ]]; then 309 echo "${tag}: PASS" 310 exit 0 311 fi 312 echo "${tag}: FAIL (${fails})" 313 exit 1