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_kernel_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_kernel_test.sh` 244 lines ยท 17605 bytes Raw Blame History 1 #!/usr/bin/env bash 2 # SPDX-License-Identifier: Apache-2.0 3 # Copyright 2026 Drake Stapleton (AIEN / aien-dev) 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 # qemu_ck_imac_kernel_test.sh -- QEMU test for IMACKERN.EFI (AIENOS-KERNEL-0, 18 # native/kernel/x86). QEMU is an emulator: a PASS here qualifies nothing about a 19 # 2017 iMac or any other real x86_64 machine. 20 # 21 # Builds IMACKERN.EFI, puts it on a FAT image as EFI/BOOT/BOOTX64.EFI attached as a 22 # SATA disk, and boots it twice in QEMU q35 with OVMF: "-cpu max" and "-cpu qemu64" 23 # (two CPU models; the kernel drives the local APIC in xAPIC mode on both). Each run 24 # must leave boot services, switch to the kernel's own page tables, prove the guard 25 # page and an unmapped page 0 (a NULL read and write fault, #487), measure the TSC 26 # against the HPET, take 20 local APIC timer interrupts, print 27 # start the three other cores (-smp 4) drawing the night sky of the startup, find both SATA disks behind 28 # the q35 AHCI controller and an NVMe disk with its own GPT and read them without 29 # writing (the ESP, and a data disk with a GPT whose AIENOS partition the kernel 30 # must locate where ck_gpt_image wrote it; the data disk must be byte-identical 31 # afterwards), print "kernel: alive", hold the screen and power QEMU off through 32 # ACPI S5 by itself. W^X (#409): each run must show writes to code and read-only 33 # data and execution from heap and stack faulting with the right #PF error code, 34 # and an audit of every page table leaf (only the code executable, nothing 35 # writable and executable, NXE and WP on every core). Three TEST-ONLY mutant builds 36 # (IMACKERN_WX_MUTANT=no_nx, =text_writable at -smp 1; =ap_no_wp, secondary cores 37 # without CR0.WP, at -smp 4) must make those checks panic. 38 # TLB shootdown (#485): each run must show every secondary core's stale writable 39 # and stale present translation of a page gone after the boot core made it 40 # read-only and then unmapped it; the TEST-ONLY mutant no_shootdown (invlpg on the 41 # boot core only, -smp 4) must make that probe panic. 42 # The kernel's report reaches the serial log through COM1, which QEMU has and the 43 # iMac does not. Needs qemu-system-x86_64, OVMF, mtools and an x86_64 gcc. Final line: 44 # AIENOS_IMAC_KERNEL: PASS|FAIL|NOT_RUN [(...)] exit 0 / 1 / 2 (tools) / 3 (NOT_RUN) 45 set -euo pipefail 46 repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 47 cd "${repo_root}" 48 tag="AIENOS_IMAC_KERNEL" 49 # Opt-in firmware-inventory coverage. This device advertises a DMAR table; 50 # the kernel only reads it and never enables or configures DMA remapping. 51 iommu=() 52 case "${AIENOS_IMAC_DMAR:-0}" in 53 0) ;; 54 1) iommu=(-device intel-iommu,intremap=on,caching-mode=on) ;; 55 *) echo "${tag}: FAIL (AIENOS_IMAC_DMAR must be 0 or 1)"; exit 1 ;; 56 esac 57 code_fd="${OVMF_CODE:-/usr/share/OVMF/OVMF_CODE_4M.fd}" 58 vars_fd="${OVMF_VARS:-/usr/share/OVMF/OVMF_VARS_4M.fd}" 59 cross="" 60 if [ "$(uname -m)" != "x86_64" ]; then cross="x86_64-linux-gnu-"; fi 61 for t in qemu-system-x86_64 mformat mmd mcopy "${cross}gcc" "${cross}ld"; do 62 command -v "${t}" >/dev/null || { echo "${tag}: NOT_RUN (${t} missing)"; exit 2; } 63 done 64 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "${tag}: NOT_RUN (no OVMF)"; exit 2; } 65 out="${IMACKERN_OUT:-${repo_root}/target/native-kernel-imac-kernel}" 66 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 67 make -s -C native/kernel X86_CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" imac-kernel >/dev/null 68 make -s -C native/kernel OUT="${out}" gpt-image >/dev/null 69 70 . "${repo_root}/scripts/lib_gate_hold.sh" 71 if ! gh_quiet_check || ! gh_lock_take qemu_ck_imac_kernel_test "${AIENOS_GATE_MINUTES:-60}"; then 72 echo "${tag}: NOT_RUN (${gh_why})" 73 exit 3 74 fi 75 work="$(mktemp -d)" 76 qpid="" 77 cleanup() { 78 [[ -z "${qpid}" ]] || kill "${qpid}" 2>/dev/null || true 79 rm -rf "${work}" 80 gh_lock_release 81 } 82 trap cleanup EXIT 83 84 img="${work}/esp.img" 85 truncate -s 64M "${img}" 86 mformat -i "${img}" -F :: 87 mmd -i "${img}" ::/EFI ::/EFI/BOOT 88 mcopy -i "${img}" "${out}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI 89 data="${work}/data.img" 90 gpt_line="$("${out}/host/ck_gpt_image" create "${data}" 512 64 aienos-middle)" 91 read -r a_first a_last < <(sed -E "s/.*aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+).*/\1 \2/" <<<"${gpt_line}") 92 data_sum="$(sha256sum "${data}" | cut -d" " -f1)" 93 nvm="${work}/nvme.img" 94 nvm_line="$("${out}/host/ck_gpt_image" create "${nvm}" 512 32 aienos-middle)" 95 read -r n_first n_last < <(sed -E "s/.*aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+).*/\1 \2/" <<<"${nvm_line}") 96 nvm_sum="$(sha256sum "${nvm}" | cut -d" " -f1)" 97 98 fails=0 99 chk() { # description, file, pattern 100 if grep -aqE -- "$3" "$2"; then echo "PASS $1"; else echo "FAIL $1 (no match for: $3)"; fails=$((fails + 1)); fi 101 } 102 run() { # name, -cpu model, expected APIC mode 103 local name="$1" cpu="$2" mode="$3" log="${work}/$1.log" txt="${work}/$1.txt" 104 cp "${vars_fd}" "${work}/vars.fd" 105 # AIENOS_IMAC_KERNEL_KEEP_DIR: keep each run's serial log (.txt) and a recording of its sound (.wav) there 106 local audio="none,id=snd0" keep="${AIENOS_IMAC_KERNEL_KEEP_DIR:-}" 107 [[ -z "${keep}" ]] || audio="wav,id=snd0,path=${keep}/${name}.wav,out.frequency=48000,out.channels=2,out.format=s16" 108 qemu-system-x86_64 -M q35 -accel tcg -cpu "${cpu}" -smp 4 -m 1024 \ 109 "${iommu[@]}" \ 110 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ 111 -drive if=pflash,format=raw,file="${work}/vars.fd" \ 112 -drive if=none,id=esp,format=raw,file="${img}" -device ide-hd,drive=esp,bus=ide.0 \ 113 -drive if=none,id=data,format=raw,file="${data}" -device ide-hd,drive=data,bus=ide.1 \ 114 -drive if=none,id=nvm,format=raw,file="${nvm}" -device nvme,drive=nvm,serial=AIENOSQEMU \ 115 -audiodev "${audio}" -device ich9-intel-hda -device hda-duplex,audiodev=snd0 \ 116 -display none -nic none -serial file:"${log}" -no-reboot & 117 qpid=$! 118 local self_exit=0 119 for _ in $(seq 1 "${AIENOS_IMAC_KERNEL_WAIT:-300}"); do 120 if ! kill -0 "${qpid}" 2>/dev/null; then self_exit=1; break; fi 121 sleep 1 122 done 123 kill "${qpid}" 2>/dev/null || true 124 wait "${qpid}" 2>/dev/null || true 125 qpid="" 126 tr -d '\r' <"${log}" >"${txt}" 2>/dev/null || : >"${txt}" 127 [[ -z "${keep}" ]] || cp "${txt}" "${keep}/${name}.txt" 128 echo "== run ${name} (-cpu ${cpu}) ==" 129 chk "${name}: title" "${txt}" '^AIENOS-KERNEL-0$' 130 chk "${name}: commit" "${txt}" "^aienos_commit: ${commit}\$" 131 chk "${name}: GOP found" "${txt}" '^gop: ok ' 132 chk "${name}: RSDP" "${txt}" '^acpi_rsdp: 0x[0-9a-f]*[1-9a-f][0-9a-f]*$' 133 if [[ "${AIENOS_IMAC_DMAR:-0}" == 1 ]]; then 134 chk "${name}: DMAR firmware inventory" "${txt}" '^dmar: inventory bytes=[0-9]+ address_bits=[0-9]+ flags=[0-9]+ units=[1-9][0-9]* regions=[0-9]+ scopes=[1-9][0-9]* other=[0-9]+$' 135 chk "${name}: DMAR remapping unit" "${txt}" '^dmar: unit=0 segment=0 base=0x[0-9a-f]+ register_bytes=[0-9]+ include_all=[01]$' 136 chk "${name}: DMAR IOAPIC path" "${txt}" '^dmar: scope=[0-9]+ owner=unit:0 segment=0 type=3 enum=[0-9]+ start_bus=[0-9]+ path=[0-9a-f.]+$' 137 chk "${name}: inventory grants no protection" "${txt}" '^dmar: inventory-only register_access=none; DMA confinement NOT QUALIFIED$' 138 else 139 chk "${name}: absent DMAR grants no protection" "${txt}" '^dmar: absent-or-invalid; DMA confinement NOT QUALIFIED$' 140 fi 141 chk "${name}: PCI segment and requester reported" "${txt}" '^pci: fn .* segment=0 requester=[0-9a-f]{4}$' 142 chk "${name}: left boot services" "${txt}" '^exit_boot_services: ok attempts=[1-8]$' 143 chk "${name}: descriptor tables" "${txt}" '^descriptors: gdt=7 tss=yes ist1=df,nmi,mc idt=256$' 144 chk "${name}: handoff checked" "${txt}" '^handoff: CHANDOF3 ok$' 145 chk "${name}: screen on the framebuffer" "${txt}" '^screen: gop [0-9]+x[0-9]+ .* map=wc scale=[1-4] grid=[0-9]+x[0-9]+$' 146 chk "${name}: own page tables" "${txt}" '^paging: root=0x[0-9a-f]+ firmware_cr3=0x[0-9a-f]+ switched=yes top_gib=[0-9]+ pages_2m_wb=[1-9][0-9]* pages_2m_wc=[1-9][0-9]* pages_2m_uc=[1-9][0-9]* pt_frames=[0-9]+ fb=wc pat1=wc$' 147 chk "${name}: heap" "${txt}" '^memory: frame_ranges=[1-9][0-9]* free_bytes=[1-9][0-9]* .* heap_test=ok$' 148 chk "${name}: guard page fault contained" "${txt}" '^guard_page: ok fault=contained \(#PF cr2=0x[0-9a-f]+ error=0x[0-9a-f]+, not present\)$' 149 chk "${name}: NULL read and write fault contained" "${txt}" '^null_page: ok read=fault write=fault \(#PF cr2=0x0 error=0x[0-9a-f]+,0x[0-9a-f]+, not present\)$' 150 chk "${name}: TSC timed by the HPET" "${txt}" '^timebase: hpet period_fs=[1-9][0-9]* tsc_hz=[1-9][0-9]*$' 151 chk "${name}: APIC mode ${mode}" "${txt}" "^apic: mode=${mode} id=0 " 152 chk "${name}: MADT four CPUs" "${txt}" '^madt: cpus=4 ioapics=1 ioapic_lines_masked=[1-9][0-9]*$' 153 chk "${name}: 20 timer interrupts" "${txt}" '^timer_irq: 20 ticks in [0-9]+ ms$' 154 chk "${name}: AHCI controller found" "${txt}" "^pci: ahci [0-9a-f:.]+ id=8086:2922 " 155 chk "${name}: AHCI polled" "${txt}" "^ahci: ctrl=0 abar=0x[0-9a-f]+ version=1\\.[0-9]+ ports=[0-9]+ implemented=0x[0-9a-f]+ s64a=[01] polled=yes\$" 156 chk "${name}: ESP disk identified" "${txt}" "^ahci_disk: ctrl=0 disk=0 port=0 link=gen[0-9] model=\"QEMU HARDDISK\" blocks=131072 block=512 mib=64 access=read-only\$" 157 chk "${name}: ESP sector 0 read" "${txt}" "^ahci_read: ctrl=0 disk=0 lba=0 bytes=512 boot_sig=55aa " 158 chk "${name}: ESP has no GPT" "${txt}" "^ahci_gpt: ctrl=0 disk=0 refused: no protective MBR\$" 159 chk "${name}: data disk identified" "${txt}" "^ahci_disk: ctrl=0 disk=1 port=1 link=gen[0-9] model=\"QEMU HARDDISK\" blocks=131072 block=512 mib=64 access=read-only\$" 160 chk "${name}: data disk AIENOS partition where written" "${txt}" "^ahci_gpt: ctrl=0 disk=1 gpt=valid used=3 aienos first_lba=${a_first} last_lba=${a_last}\$" 161 chk "${name}: three secondary cores started, drawing the night" "${txt}" "^smp: cpus=4 started=3 failed=0 bsp_id=0 ap_ids=[0-9]+,[0-9]+,[0-9]+ trampoline=0x[0-9a-f]+ state=drawing helpers=[0-2]\$" 162 chk "${name}: TLB shootdown reaches every secondary core" "${txt}" '^tlb_shootdown: ok cores=3 stale_write_allowed=0 stale_read_allowed=0 shootdowns=14 acks=36$' 163 chk "${name}: NVMe controller found" "${txt}" "^pci: nvme [0-9a-f:.]+ id=1b36:0010 " 164 chk "${name}: NVMe disk identified" "${txt}" "^nvme_disk: ctrl=0 disk=0 version=1\\.[0-9]+ nsid=1 blocks=65536 block=512 mib=32 access=read-only\$" 165 chk "${name}: NVMe AIENOS partition where written" "${txt}" "^nvme_gpt: ctrl=0 disk=0 gpt=valid used=3 aienos first_lba=${n_first} last_lba=${n_last}\$" 166 chk "${name}: the night began at boot" "${txt}" "^screen: night: the sky made in [0-9]+ ms \([0-9]+ MiB drawing memory\); stars, black holes, then Allen\$" 167 chk "${name}: sound controller found" "${txt}" "^pci: sound [0-9a-f:.]+ id=8086:293e class=0403[0-9a-f]{2} " 168 chk "${name}: sound controller looked at" "${txt}" "^sound: ctrl=0 version=1\\.0 streams out=[1-9][0-9]* " 169 chk "${name}: codec mapped through the command rings" "${txt}" "^sound: codec=0 id=1af4:[0-9a-f]{4} sub=[0-9a-f]{4}:[0-9a-f]{4} rev=0x[0-9a-f]{8} afg=1 power=D0 widgets=0x02\\.\\.0x05 " 170 chk "${name}: output path opened" "${txt}" "^sound: ctrl=0 opened 1 output pin\\(s\\): codec 0 pins 0x03\\(line\\)<-0x02\$" 171 chk "${name}: chirp stream running" "${txt}" "^sound: ctrl=0 stream 4 playing the chirp from night [0-9]+ ms .* position after 2 ms [1-9][0-9]* bytes, status 0x[0-9a-f]{2}\$" 172 chk "${name}: chirp stream stopped after the chirp" "${txt}" "^sound: chirp over; stream stopped at night [0-9]+ ms " 173 chk "${name}: microphone path opened" "${txt}" "^sound: codec=0 microphone: pin 0x05 \\(line in\\) -> converter 0x04\$" 174 chk "${name}: microphone stream recording" "${txt}" "^sound: ctrl=0 stream 0 recording 1000 ms from the microphone \\(pin 0x05 -> converter 0x04, format 0x0011, 192000 bytes\\); position after 2 ms [0-9]+ bytes, status 0x[0-9a-f]{2}\$" 175 chk "${name}: microphone recording summed up" "${txt}" "^sound: microphone recorded 1000 ms: 96000 samples, [0-9]+% nonzero \\(first at [0-9]+\\), peak [0-9]+, rms [0-9]+, dc -?[0-9]+ \\(16-bit scale\\), position [0-9]+ bytes, status 0x[0-9a-f]{2}\$" 176 chk "${name}: no Ethernet in this VM" "${txt}" "^net: no Ethernet controller\$" 177 chk "${name}: storage summary" "${txt}" "^storage: pci_functions=[1-9][0-9]* ahci_controllers=1 disks=2 nvme_controllers=1 nvme_disks=1 gpt_valid=2 writes=none\$" 178 chk "${name}: one-line summary" "${txt}" "^summary: cores=4/4 timer=ok sata_disks=2 nvme_disks=1 gpt_valid=2 writes=none net=none\$" 179 [[ "$(sha256sum "${nvm}" | cut -d" " -f1)" == "${nvm_sum}" ]] && echo "PASS ${name}: NVMe disk unchanged" || { echo "FAIL ${name}: NVMe disk changed"; fails=$((fails + 1)); } 180 [[ "$(sha256sum "${data}" | cut -d" " -f1)" == "${data_sum}" ]] && echo "PASS ${name}: data disk unchanged" || { echo "FAIL ${name}: data disk changed"; fails=$((fails + 1)); } 181 chk "${name}: W^X probes fault" "${txt}" '^wx: ok nx=on wp=on text_write=fault ro_write=fault heap_exec=fault stack_exec=fault$' 182 chk "${name}: W^X audit, only code executable, every core" "${txt}" '^wx_audit: ok wx_pages=0 exec_pages=([1-9][0-9]*) text_pages=\1 exec_outside_text=0 ro_writable=0 cores_nx_wp=4/4 trampoline=retired$' 183 if grep -aq '^wx: TEST-ONLY' "${txt}"; then echo "FAIL ${name}: default image is a W^X mutant"; fails=$((fails + 1)); fi 184 chk "${name}: kernel alive" "${txt}" '^kernel: alive$' 185 chk "${name}: DONE" "${txt}" '^AIENOS-KERNEL-0 DONE$' 186 chk "${name}: ACPI S5 power off" "${txt}" '^power: off via ACPI S5 pm1a=0x[0-9a-f]+ ' 187 if grep -aqE '^(panic|power: still running|power: halted)' "${txt}"; then 188 echo "FAIL ${name}: panic or failed power off"; fails=$((fails + 1)) 189 else 190 echo "PASS ${name}: no panic" 191 fi 192 # The default image carries no network report (lab-only, CK_X86_REPORT=1). 193 if grep -aqE '^report: ' "${txt}"; then 194 echo "FAIL ${name}: default image ran the lab-only network report"; fails=$((fails + 1)) 195 else 196 echo "PASS ${name}: no network report in the default image" 197 fi 198 [[ ${self_exit} == 1 ]] && echo "PASS ${name}: QEMU powered off by itself" || { echo "FAIL ${name}: QEMU still running"; fails=$((fails + 1)); } 199 echo "---- ${name} serial ----"; cat "${txt}"; echo "----" 200 } 201 run max max xapic 202 run qemu64 qemu64 xapic 203 # W^X and TLB negative controls: the W^X probes run before the clock is measured, so 204 # a mutant's panic may halt instead of powering off; wait for the panic line, then stop QEMU. 205 mutant() { # IMACKERN_WX_MUTANT value, expected panic pattern, QEMU core count (default 1) 206 local m="$1" mo="${out}-wx-$1" mi="${work}/wx-$1.img" log="${work}/wx-$1.log" hit=0 smp="${3:-1}" 207 make -s -C native/kernel X86_CROSS="${cross}" OUT="${mo}" AIENOS_COMMIT="${commit}" IMACKERN_WX_MUTANT="${m}" imac-kernel >/dev/null 208 truncate -s 64M "${mi}" 209 mformat -i "${mi}" -F :: 210 mmd -i "${mi}" ::/EFI ::/EFI/BOOT 211 mcopy -i "${mi}" "${mo}/IMACKERN.EFI" ::/EFI/BOOT/BOOTX64.EFI 212 cp "${vars_fd}" "${work}/vars.fd" 213 qemu-system-x86_64 -M q35 -accel tcg -cpu max -smp "${smp}" -m 1024 \ 214 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ 215 -drive if=pflash,format=raw,file="${work}/vars.fd" \ 216 -drive if=none,id=esp,format=raw,file="${mi}" -device ide-hd,drive=esp,bus=ide.0 \ 217 -display none -nic none -serial file:"${log}" -no-reboot & 218 qpid=$! 219 for _ in $(seq 1 "${AIENOS_IMAC_KERNEL_WAIT:-300}"); do 220 if tr -d '\r' <"${log}" 2>/dev/null | grep -aqE -- "$2"; then hit=1; break; fi 221 kill -0 "${qpid}" 2>/dev/null || break 222 sleep 1 223 done 224 kill "${qpid}" 2>/dev/null || true 225 wait "${qpid}" 2>/dev/null || true 226 qpid="" 227 if [[ ${hit} == 1 ]] && tr -d '\r' <"${log}" | grep -aq '^wx: TEST-ONLY W^X mutant build'; then 228 echo "PASS mutant ${m}: W^X probes catch it" 229 tr -d '\r' <"${log}" | grep -aE -- "$2" 230 else 231 echo "FAIL mutant ${m}: no match for: $2"; fails=$((fails + 1)) 232 tr -d '\r' <"${log}" | tail -20 233 fi 234 } 235 mutant no_nx '^panic: wx: FAIL nx=on wp=on text_write=fault ro_write=fault heap_exec=ALLOWED stack_exec=ALLOWED$' 236 mutant text_writable '^panic: wx: FAIL nx=on wp=on text_write=ALLOWED ro_write=fault heap_exec=fault stack_exec=fault$' 237 mutant no_shootdown '^panic: tlb_shootdown: FAIL cores=3 stale_write_allowed=3 stale_read_allowed=3$' 4 238 # The secondary cores start without CR0.WP (#484): the boot core's probes pass. The TLB shootdown probe (#485) 239 # runs on the secondary cores before wx_audit, and without WP their writes to the page made read-only succeed 240 # while the unmapped page still faults: write side fails on all three cores, read side holds. (Without the 241 # shootdown probe, the wx_audit core count, 1 of 4 cores with NXE and WP, catches the same mutant.) 242 mutant ap_no_wp '^panic: tlb_shootdown: FAIL cores=3 stale_write_allowed=3 stale_read_allowed=0$' 4 243 if [[ ${fails} == 0 ]]; then echo "${tag}: PASS"; exit 0; fi 244 echo "${tag}: FAIL (${fails} checks)"; exit 1