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/lib_ck_cont_qemu.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 / lib_ck_cont_qemu.sh` 240 lines ยท 11800 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 2 # Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 # Shared machinery of the C kernel continuity and Recovery Core QEMU gates 17 # (scripts/qemu_ck_continuity_test.sh = CK gate M4_CONTINUITY, 18 # scripts/qemu_ck_recovery_test.sh = CK gate M4_RECOVERY). Sourced, not run. 19 # 20 # Mode selection is contract K-7 (native/kernel/CONTINUITY_RECOVERY_CONTRACT.md, 21 # native/kernel/svc/continuity_boot.h): the host writes one ASCII plan line 22 # AIENCONT v1 mode=[ cp=][ response=<64 hex>] 23 # into the last 4096-byte unit of the AIENOS partition of a 4096-byte-LBA GPT 24 # image; the TEST image "make full CK_TEST_CONTINUITY=1" (refused with 25 # CK_HARDWARE_STAGING / CK_QEMU_UNSAFE_DMA / CK_TEST_STORE_CRASH) runs the 26 # continuity or Recovery Core wiring instead of the boot-record commit. Every 27 # boot is a fresh QEMU process (cold restart); only the NVMe image persists. 28 # 29 # The caller defines before sourcing: gate_name (e.g. M4_CONTINUITY), script_tag, 30 # and calls cq_init "$@" (parses --mutant NAME), then uses the helpers below. 31 # QEMU is not hardware: a PASS qualifies nothing physical. 32 33 cq_mutant="" 34 cq_mode=gate 35 cq_init() { 36 case "${1:-}" in 37 "") ;; 38 --mutant) 39 cq_mode=mutant; cq_mutant="${2:-}" 40 case "${cq_mutant}" in 41 resume_no_commit|resume_provisions|degraded_is_unprovisioned|compare_16|bare_sha256|subject_restore_mints|subject_accept_foreign) ;; 42 *) echo "usage: $0 [--mutant resume_no_commit|resume_provisions|degraded_is_unprovisioned|compare_16|bare_sha256|subject_restore_mints|subject_accept_foreign]" >&2; exit 2 ;; 43 esac ;; 44 *) echo "usage: $0 [--mutant NAME]" >&2; exit 2 ;; 45 esac 46 } 47 verdict() { 48 if [[ "${cq_mode}" == mutant ]]; then 49 echo "AIENOS_CK_${gate_name}_MUTANT_${cq_mutant^^}: $1" 50 else 51 echo "AIENOS_CK_${gate_name}: $1" 52 fi 53 } 54 55 img_bytes=`67108864` 56 unit=4096 57 plan_unit=0 58 set_part() { plan_unit=$(( ($2 + 1) * $1 / unit - 1 )); } 59 60 cq_build() { 61 code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" 62 vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" 63 command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdict NOT_RUN; exit 2; } 64 [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdict NOT_RUN; exit 2; } 65 commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" 66 cross="" 67 if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi 68 out="${repo_root}/target/native-kernel" 69 mk_full() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full "$@" >/dev/null; } 70 if [[ "${cq_mode}" == mutant ]]; then 71 mk_full CK_TEST_CONTINUITY=1 CK_TEST_CONTINUITY_MUTANT="${cq_mutant}" || { echo "mutant build failed"; verdict NOT_RUN; exit 2; } 72 test_efi="${out}/full-test-continuity-mutant-${cq_mutant}/BOOTAA64.EFI" 73 plain_efi="${test_efi}" 74 else 75 mk_full || { echo "build failed"; verdict NOT_RUN; exit 2; } 76 mk_full CK_TEST_CONTINUITY=1 || { echo "TEST continuity build failed"; verdict NOT_RUN; exit 2; } 77 test_efi="${out}/full-test-continuity/BOOTAA64.EFI" 78 plain_efi="${out}/full/BOOTAA64.EFI" 79 fi 80 make -s -C native/kernel CROSS="${cross}" OUT="${out}" gpt-image cont-tool >/dev/null || { echo "host tools build failed"; verdict NOT_RUN; exit 2; } 81 gpt_tool="${out}/host/ck_gpt_image" 82 cont_tool="${out}/host/ck_cont_tool" 83 } 84 85 # make_refusals: parse only (make -n); nothing is built. 86 make_refusals() { 87 local out f=0 tmpo 88 mk() { make -n -s -C native/kernel OUT="${1}" full "${@:2}" 2>&1; } 89 tmpo="$(mktemp -d)" 90 out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=/dev/null CK_MACHINE_ID=/dev/null)" 91 grep -q "CK_TEST_CONTINUITY (TEST-ONLY continuity mode selection) cannot be combined with CK_HARDWARE_STAGING" <<<"${out}" \ 92 && echo "PASS make refuses the continuity test mode with CK_HARDWARE_STAGING" || { echo "FAIL hardware staging accepted the continuity test mode"; f=1; } 93 out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_QEMU_UNSAFE_DMA=1)" 94 grep -q "cannot be combined with CK_QEMU_UNSAFE_DMA" <<<"${out}" \ 95 && echo "PASS make refuses the continuity test mode with CK_QEMU_UNSAFE_DMA" || { echo "FAIL unsafe DMA build accepted the continuity test mode"; f=1; } 96 out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_TEST_STORE_CRASH=1)" 97 grep -q "cannot be combined with CK_TEST_STORE_CRASH" <<<"${out}" \ 98 && echo "PASS make refuses the continuity test mode with the Store crash hook" || { echo "FAIL crash hook build accepted the continuity test mode"; f=1; } 99 out="$(mk "${tmpo}" CK_TEST_CONTINUITY_MUTANT=resume_no_commit)" 100 grep -q "CK_TEST_CONTINUITY_MUTANT needs CK_TEST_CONTINUITY=1" <<<"${out}" \ 101 && echo "PASS make refuses a continuity mutant without the TEST mode" || { echo "FAIL mutant built without the TEST mode"; f=1; } 102 out="$(mk "${tmpo}" CK_TEST_CONTINUITY=1 CK_TEST_CONTINUITY_MUTANT=bogus)" 103 grep -q "CK_TEST_CONTINUITY_MUTANT must be" <<<"${out}" \ 104 && echo "PASS make refuses an unknown continuity mutant" || { echo "FAIL unknown continuity mutant accepted"; f=1; } 105 rm -rf "${tmpo}" 106 return "${f}" 107 } 108 109 # cq_default_image_checks: the default image carries no TEST wiring; the TEST 110 # image announces itself. Sets hw_ok. 111 cq_default_image_checks() { 112 hw_ok=1 113 make_refusals || hw_ok=0 114 if grep -aqE "AIENCONT|RECOVERY_OPERATOR_KEY|TEST-ONLY continuity" "${plain_efi}"; then 115 echo "FAIL default image carries continuity test strings"; hw_ok=0 116 else 117 echo "PASS default image carries no continuity test strings" 118 fi 119 if grep -aqF "TEST-ONLY continuity mode selection image" "${test_efi}"; then 120 echo "PASS TEST continuity image announces itself" 121 else 122 echo "FAIL TEST continuity image lacks its announcement"; hw_ok=0 123 fi 124 } 125 126 cq_quiet_and_trap() { 127 # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): 128 # scripts/lib_gate_hold.sh (aienos#278). 129 . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" 130 if ! gh_quiet_check || ! gh_lock_take "${script_tag}" "${AIENOS_GATE_MINUTES:-60}"; then 131 echo "NOT_RUN ${gh_why}" 132 verdict NOT_RUN 133 exit 3 134 fi 135 # Release only this run's own gate lock record, at most once. 136 release_flag() { gh_lock_release; } 137 top="$(mktemp -d)" 138 qemu_pid="" 139 cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; } 140 trap cleanup EXIT 141 # shellcheck source=scripts/lib_ck_m1_checks.sh 142 source "${repo_root}/scripts/lib_ck_m1_checks.sh" 143 fail=0; m1_fail=0; rows=0; rows_ok=0 144 work="${top}/boot" 145 } 146 147 pass() { echo "PASS $1"; rows=$(( rows + 1 )); rows_ok=$(( rows_ok + 1 )); } 148 bad() { 149 echo "FAIL $1"; fail=1; rows=$(( rows + 1 )) 150 if [[ "${cq_mode}" == mutant ]]; then 151 release_flag 152 verdict "KILLED (the gate's check failed on the mutant: $1)" 153 exit 0 154 fi 155 } 156 sha() { sha256sum "$1" | cut -d' ' -f1; } 157 digest() { sha "${image}"; } 158 159 new_disk() { # -> ${gpt_tmpl}; sets plan_unit from the range the tool WROTE 160 local l re='^gpt_image bs=[0-9]+ blocks=[0-9]+ aienos_first_lba=([0-9]+) aienos_last_lba=([0-9]+)$' 161 gpt_tmpl="${top}/gpt-4096.img" 162 l=$("${gpt_tool}" create "${gpt_tmpl}" 4096 $(( img_bytes / `1048576` )) aienos-middle) || l="" 163 if [[ "${l}" =~ ${re} ]]; then set_part 4096 "${BASH_REMATCH[2]}"; return 0; fi 164 echo "FAIL GPT boot disk image not created (${l})"; verdict NOT_RUN; exit 2 165 } 166 fresh_image() { image="${top}/disk.img"; cp "${gpt_tmpl}" "${image}"; } 167 168 # plan MODE [cp=NAME] [response=HEX]: write the K-7 plan line into the scratch unit. 169 plan() { 170 local line="AIENCONT v1 mode=$1"; shift 171 local w; for w in "$@"; do line+=" ${w}"; done 172 { printf '%s\n' "${line}"; head -c "${unit}" /dev/zero; } | head -c "${unit}" \ 173 | dd of="${image}" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none 174 } 175 clear_plan() { head -c "${unit}" /dev/zero | dd of="${image}" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; } 176 177 qemu_args() { 178 qargs=(-M "virt,virtualization=on,gic-version=3,iommu=smmuv3" -accel tcg,thread=single -cpu max -smp 4 -m 2048 179 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" 180 -drive if=pflash,format=raw,file="${work}/vars.fd" 181 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" 182 -device virtio-blk-pci,drive=esp 183 -drive if=none,id=nvme0,format=raw,file="${image}" 184 -device "nvme,drive=nvme0,serial=aienos-continuity,logical_block_size=4096,physical_block_size=4096" 185 -device ramfb -display none -nic none 186 -serial file:"${work}/serial.log" -no-reboot) 187 } 188 prep() { # efi 189 rm -rf "${work}"; mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" 190 touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" 191 cp "$1" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" 192 cp "${vars_fd}" "${work}/vars.fd" 193 } 194 # boot: one cold boot that runs to its end (the guest resets); M1 checks. 195 # Sets before/after (image digests bracketing only the guest's writes). 196 boot() { 197 prep "${test_efi}"; qemu_args 198 before=$(digest) 199 timeout "${AIENOS_QEMU_TIMEOUT:-240}" qemu-system-aarch64 "${qargs[@]}" 200 qemu_status=$? 201 after=$(digest) 202 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 203 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/${script_tag}_${boot_n:-0}.log" 204 boot_n=$(( ${boot_n:-0} + 1 )) 205 failed=0 206 ck_m1_checks >"${work}/m1.txt" 207 if [[ "${failed}" != 0 || "${qemu_status}" != 0 ]]; then 208 echo "FAIL boot ${boot_n}: M1 checks or QEMU exit ${qemu_status}"; grep '^FAIL' "${work}/m1.txt" | sed 's/^/ /' 209 m1_fail=1 210 if [[ "${cq_mode}" == mutant ]]; then :; else fail=1; fi 211 return 1 212 fi 213 return 0 214 } 215 # boot_kill CP: run until "continuity: HALT at CP", then SIGKILL. 216 boot_kill() { 217 prep "${test_efi}"; qemu_args 218 : >"${work}/serial.log" 219 qemu-system-aarch64 "${qargs[@]}" & 220 qemu_pid=$! 221 local deadline=$(( $(date +%s) + ${AIENOS_QEMU_TIMEOUT:-240} )) 222 while (( $(date +%s) < deadline )); do 223 grep -qE '^(continuity: HALT at |CONTINUITY: )' "${work}/serial.log" 2>/dev/null && break 224 kill -0 "${qemu_pid}" 2>/dev/null || break 225 sleep 0.1 226 done 227 sleep 0.2 228 kill -9 "${qemu_pid}" 2>/dev/null 229 wait "${qemu_pid}" 2>/dev/null 230 qemu_pid="" 231 tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" 232 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/${script_tag}_kill_${1}.log" 233 } 234 # line: the last serial line of this boot that starts with the pattern (ERE). 235 out() { grep -E "$1" "${work}/serial.txt" | tail -1; } 236 cont() { grep -oE "^CONTINUITY: $1( .*)?$" "${work}/serial.txt" | tail -1; } 237 field() { sed -nE "s/.* $1=([0-9a-f]+).*/\1/p" <<<"$2"; } 238 agent_of() { sed -nE 's/.*agent=([0-9a-f]{64}).*/\1/p' <<<"$1"; } 239 unchanged() { [[ "${before}" == "${after}" ]]; } 240 serial_cont() { grep -E '^(CONTINUITY|RECOVERY)' "${work}/serial.txt" | head -4 | tr '\n' '|'; }