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

319 lines · 17932 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# Boot the AIENOS C kernel core image in QEMU AArch64 (UEFI/AAVMF, EL2 like
18# Machine 1, -smp 4) and check the secondary-core bring-up of core/smp.c:
19# PSCI CPU_ON for every MADT core except the boot core, each core on its own
20# stack with the MMU on the boot core's tables, checked in (its own MPIDR
21# reported) and parked in WFE; the boot core waits with a bounded timeout.
22# Last line: AIENOS_CK_SMP: PASS|FAIL|NOT_RUN (read by scripts/ck_gates.sh).
23# QEMU is not hardware: a PASS here qualifies nothing physical.
24#
25# FAIL when (the script re-checks the lines itself, it does not trust the
26# kernel's result=ok): a core never checks in; PSCI CPU_ON returns any error
27# code; an MPIDR is duplicated or missing (boot + secondaries must be exactly
28# the -smp N distinct values, each secondary must report the MPIDR it was
29# started for); a secondary reports EL != 1, MMU off, other tables or a stack
30# outside its own; the MADT core count differs from -smp N; the summary line
31# is missing or not ok; the TEST-ONLY mutation image was booted; the boot
32# panics or faults; QEMU exits non-zero.
33#
34# Usage: bash scripts/qemu_ck_smp_test.sh the gate (default image)
35# bash scripts/qemu_ck_smp_test.sh --mutation TEST-ONLY image built with
36# CK_TEST_SMP_SKIP_CPU=1 (CPU_ON skipped for one core): the gate
37# checks must FAIL on it; ends AIENOS_CK_SMP_MUTATION: PASS when
38# they do (the mutation is killed), FAIL when they pass
39# bash scripts/qemu_ck_smp_test.sh --self-test canned serial logs for
40# every failure mode above, plus the Makefile refusals of the
41# mutation flag (with CK_HARDWARE_STAGING, bad value); no QEMU,
42# no compile (make -n stops at parse time). Ends
43# AIENOS_CK_SMP_SELF_TEST: PASS|FAIL
44# Environment: AIENOS_CK_SMP_CPUS (default 4), AIENOS_QEMU_TIMEOUT (180 s),
45# AIENOS_QUIET_FLAG (read only), AIENOS_GATE_LOCK / AIENOS_GATE_MINUTES,
46# AIENOS_LOG_DIR (copy of the serial log), AIENOS_QEMU_VERBOSE.
47# Prints NOT_RUN (exit 3) while the quiet flag or the QEMU gate lock is held
48# (scripts/lib_gate_hold.sh). Needs qemu-system-aarch64 and AAVMF.
49set -euo pipefail
50
51repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
52cd "${repo_root}"
53
54mode=gate
55case "${1:-}" in
56 "") ;;
57 --mutation) mode=mutation ;;
58 --self-test) mode=self ;;
59 *) echo "usage: $0 [--mutation|--self-test]" >&2; exit 2 ;;
60esac
61ncpus="${AIENOS_CK_SMP_CPUS:-4}"
62[[ "${ncpus}" =~ ^[0-9]+$ && "${ncpus}" -ge 2 && "${ncpus}" -le 16 ]] || { echo "AIENOS_CK_SMP_CPUS must be 2..16" >&2; exit 2; }
63mut_marker="TEST-ONLY SMP mutation"
64
65# smp_check_log FILE N: one PASS/FAIL line per check; sets smp_failed=1 on
66# any failure (never resets it). Pure text checks, used by the gate, the
67# mutation run and the self-test alike.
68smp_check_log() {
69 local f="$1" n="$2" line boot_re cpu_re sum_re tgt seen
70 ok() { echo "PASS $*"; }
71 bad() { echo "FAIL $*"; smp_failed=1; }
72 boot_re='^smp: boot_mpidr=0x([0-9a-f]+) madt_cpus=([0-9]+) conduit=(smc|hvc) psci_version=([0-9]+)\.([0-9]+)$'
73 if [[ "$(grep -cE "${boot_re}" "${f}" || true)" != 1 ]]; then
74 bad "exactly one smp: boot_mpidr line"
75 return
76 fi
77 line="$(grep -E "${boot_re}" "${f}")"
78 local boot_mpidr madt
79 boot_mpidr="$(sed -E "s/${boot_re}/\\1/" <<<"${line}")"
80 madt="$(sed -E "s/${boot_re}/\\2/" <<<"${line}")"
81 [[ "${madt}" == "${n}" ]] && ok "MADT lists ${n} cores (-smp ${n})" || bad "MADT lists ${madt} cores, QEMU has ${n}"
82 if grep -qE '^smp: FAIL' "${f}"; then bad "kernel reported $(grep -E '^smp: FAIL' "${f}" | head -1)"; else ok "no smp: FAIL line"; fi
83
84 # PSCI CPU_ON: one call per secondary, every one SUCCESS.
85 local on_n on_ok
86 on_n="$(grep -cE '^smp_cpu_on: target=0x[0-9a-f]+ ' "${f}" || true)"
87 on_ok="$(grep -cE '^smp_cpu_on: target=0x[0-9a-f]+ psci_rc=0 \(SUCCESS\)$' "${f}" || true)"
88 [[ "${on_n}" == $((n - 1)) ]] && ok "CPU_ON issued for ${on_n} secondary cores" || bad "CPU_ON lines ${on_n}, expected $((n - 1))"
89 if [[ "${on_ok}" == "${on_n}" && "${on_n}" -gt 0 ]]; then
90 ok "PSCI CPU_ON returned SUCCESS for every core"
91 else
92 bad "PSCI CPU_ON error or skip: $(grep -E '^smp_cpu_on: ' "${f}" | grep -vE 'psci_rc=0 \(SUCCESS\)$' | head -3 | paste -sd';' -)"
93 fi
94
95 # Check-in: every secondary on EL1, MMU on our tables, its own stack,
96 # reporting the MPIDR it was started for.
97 cpu_re='^smp_cpu: target=0x([0-9a-f]+) checked_in=yes seen_mpidr=0x([0-9a-f]+) el=1 mmu=on ttbr0_match=yes stack_ok=yes parked=wfe$'
98 local in_n mism=0 mpidrs="${boot_mpidr}"
99 in_n="$(grep -cE "${cpu_re}" "${f}" || true)"
100 if grep -qE '^smp_cpu: target=0x[0-9a-f]+ checked_in=no' "${f}"; then
101 bad "core never checked in: $(grep -E '^smp_cpu: .*checked_in=no' "${f}" | head -3 | paste -sd';' -)"
102 fi
103 if grep -E '^smp_cpu: ' "${f}" | grep -qvE "${cpu_re}|checked_in=no"; then
104 bad "secondary state wrong: $(grep -E '^smp_cpu: ' "${f}" | grep -vE "${cpu_re}|checked_in=no" | head -2 | paste -sd';' -)"
105 fi
106 [[ "${in_n}" == $((n - 1)) ]] && ok "${in_n} secondary cores checked in on EL1, MMU on our tables, own stack, parked in WFE" \
107 || bad "checked-in secondaries ${in_n}, expected $((n - 1))"
108 while IFS= read -r line; do
109 [[ -n "${line}" ]] || continue
110 tgt="$(sed -E "s/${cpu_re}/\\1/" <<<"${line}")"
111 seen="$(sed -E "s/${cpu_re}/\\2/" <<<"${line}")"
112 [[ "${tgt}" == "${seen}" ]] || mism=1
113 grep -qE "^smp_cpu_on: target=0x${tgt} psci_rc=0 \\(SUCCESS\\)$" "${f}" || mism=1
114 mpidrs+=$'\n'"${seen}"
115 done < <(grep -E "${cpu_re}" "${f}" || true)
116 [[ "${mism}" == 0 ]] && ok "each secondary reports the MPIDR it was started for" || bad "a secondary reported another MPIDR or was not started by CPU_ON"
117 local total distinct
118 total="$(grep -c . <<<"${mpidrs}")"
119 distinct="$(sort -u <<<"${mpidrs}" | grep -c .)"
120 if [[ "${total}" == "${n}" && "${distinct}" == "${n}" ]]; then
121 ok "per-core MPIDRs distinct and complete: $(paste -sd, - <<<"${mpidrs}" | sed 's/[0-9a-f]\+/0x&/g')"
122 else
123 bad "MPIDRs duplicated or missing: ${total} reported, ${distinct} distinct, ${n} expected"
124 fi
125
126 sum_re="^smp: cpus=${n} started=${n} checked_in=${n} psci_errors=0 distinct_mpidrs=${n} wait_us=[0-9]+ result=ok$"
127 [[ "$(grep -cE "${sum_re}" "${f}" || true)" == 1 ]] && ok "summary: all ${n} cores, no PSCI error, bounded wait, result=ok" \
128 || bad "summary line missing or not ok: $(grep -E '^smp: cpus=' "${f}" | head -1)"
129
130 grep -q "kernel: alive" "${f}" && ok "kernel alive" || bad "kernel never reached kernel: alive"
131 grep -q "report_kind: final" "${f}" && ok "final report reached" || bad "no final report"
132 if grep -qE 'report_kind: (panic|fault)' "${f}"; then bad "panic or fault report"; else ok "no panic or fault"; fi
133}
134
135# ----------------------------------------------------------------- self-test
136self_test() {
137 local tmp st_fail=0 f
138 tmp="$(mktemp -d "${TMPDIR:-/tmp}/ck-smp-selftest.XXXXXX")"
139 trap 'rm -rf --one-file-system "${tmp}"' EXIT
140 # good_log N: what a correct boot prints (MPIDRs 0..N-1 as QEMU virt gives).
141 good_log() {
142 local n="$1" i
143 echo "kernel: alive"
144 echo "smp: boot_mpidr=0x0 madt_cpus=${n} conduit=smc psci_version=1.1"
145 for ((i = 1; i < n; i++)); do echo "smp_cpu_on: target=0x${i} psci_rc=0 (SUCCESS)"; done
146 for ((i = 1; i < n; i++)); do
147 echo "smp_cpu: target=0x${i} checked_in=yes seen_mpidr=0x${i} el=1 mmu=on ttbr0_match=yes stack_ok=yes parked=wfe"
148 done
149 echo "smp: cpus=${n} started=${n} checked_in=${n} psci_errors=0 distinct_mpidrs=${n} wait_us=1234 result=ok"
150 echo "report_kind: final"
151 }
152 expect() { # NAME pass|fail FILE
153 local out
154 smp_failed=0
155 out="$(smp_check_log "$3" 4)"
156 smp_failed=0
157 grep -q '^FAIL' <<<"${out}" && smp_failed=1
158 if [[ "$2" == pass && "${smp_failed}" == 0 ]] || [[ "$2" == fail && "${smp_failed}" == 1 ]]; then
159 echo "PASS self-test $1 -> $2"
160 else
161 echo "FAIL self-test $1: expected $2"; printf '%s\n' "${out}" | sed 's/^/ /'; st_fail=1
162 fi
163 }
164 good_log 4 >"${tmp}/good"; expect good pass "${tmp}/good"
165 # A core that never checks in (kernel honest).
166 sed -e 's/^smp_cpu: target=0x3 .*/smp_cpu: target=0x3 checked_in=no/' \
167 -e 's/checked_in=4 psci_errors=0 distinct_mpidrs=4 wait_us=1234 result=ok/checked_in=3 psci_errors=0 distinct_mpidrs=4 wait_us=5000000 result=FAIL/' \
168 "${tmp}/good" >"${tmp}/nocheckin"; expect never-checks-in fail "${tmp}/nocheckin"
169 # A core line missing while the kernel summary still claims ok.
170 grep -v '^smp_cpu: target=0x2 ' "${tmp}/good" >"${tmp}/missingline"; expect missing-core-line fail "${tmp}/missingline"
171 # Duplicate MPIDR: two cores report 0x1 (summary forged ok).
172 sed 's/^smp_cpu: target=0x2 checked_in=yes seen_mpidr=0x2/smp_cpu: target=0x2 checked_in=yes seen_mpidr=0x1/' \
173 "${tmp}/good" >"${tmp}/dup"; expect duplicate-mpidr fail "${tmp}/dup"
174 # Secondary reports the boot core's MPIDR (duplicate with boot).
175 sed 's/^smp_cpu: target=0x3 checked_in=yes seen_mpidr=0x3/smp_cpu: target=0x3 checked_in=yes seen_mpidr=0x0/' \
176 "${tmp}/good" >"${tmp}/dupboot"; expect duplicate-boot-mpidr fail "${tmp}/dupboot"
177 # MADT lists fewer cores than QEMU has (a missing MPIDR), lines consistent with 3.
178 good_log 3 >"${tmp}/three"; expect madt-missing-core fail "${tmp}/three"
179 # PSCI error code (ALREADY_ON), check-in line forged yes.
180 sed 's/^smp_cpu_on: target=0x2 psci_rc=0 (SUCCESS)$/smp_cpu_on: target=0x2 psci_rc=-4 (ALREADY_ON)/' \
181 "${tmp}/good" >"${tmp}/psci"; expect psci-error fail "${tmp}/psci"
182 # The mutation image's skip line.
183 sed 's/^smp_cpu_on: target=0x3 psci_rc=0 (SUCCESS)$/smp_cpu_on: target=0x3 psci_rc=SKIPPED (TEST-ONLY SMP mutation)/' \
184 "${tmp}/good" >"${tmp}/skip"; expect cpu-on-skipped fail "${tmp}/skip"
185 # Secondary on the wrong tables / MMU off / EL2.
186 sed 's/^\(smp_cpu: target=0x1 .*\) ttbr0_match=yes/\1 ttbr0_match=no/' "${tmp}/good" >"${tmp}/ttbr"; expect wrong-tables fail "${tmp}/ttbr"
187 sed 's/^\(smp_cpu: target=0x1 .*\) mmu=on/\1 mmu=off/' "${tmp}/good" >"${tmp}/mmu"; expect mmu-off fail "${tmp}/mmu"
188 sed 's/^\(smp_cpu: target=0x1 .*\) el=1/\1 el=2/' "${tmp}/good" >"${tmp}/el"; expect wrong-el fail "${tmp}/el"
189 # Kernel says FAIL somewhere, or no summary, or no smp lines at all.
190 { cat "${tmp}/good"; echo "smp: FAIL duplicate mpidr=0x1 in the MADT"; } >"${tmp}/kfail"; expect kernel-fail-line fail "${tmp}/kfail"
191 grep -v '^smp: cpus=' "${tmp}/good" >"${tmp}/nosum"; expect no-summary fail "${tmp}/nosum"
192 printf 'kernel: alive\nreport_kind: final\n' >"${tmp}/none"; expect no-smp-lines fail "${tmp}/none"
193 { cat "${tmp}/good"; echo "report_kind: panic"; } >"${tmp}/panic"; expect panic fail "${tmp}/panic"
194
195 # Makefile refusals: parse-time $(error), nothing is compiled (make -n).
196 local out rc
197 out="$(make -n -s -C native/kernel CK_TEST_SMP_SKIP_CPU=1 CK_HARDWARE_STAGING=1 OUT="${tmp}/o1" 2>&1)"; rc=$?
198 if [[ ${rc} != 0 ]] && grep -qF "CK_TEST_SMP_SKIP_CPU (TEST-only SMP mutation) cannot be combined with CK_HARDWARE_STAGING" <<<"${out}"; then
199 echo "PASS self-test mutation flag refused with CK_HARDWARE_STAGING"
200 else
201 echo "FAIL self-test mutation flag not refused with CK_HARDWARE_STAGING (rc=${rc}): ${out:0:200}"; st_fail=1
202 fi
203 out="$(make -n -s -C native/kernel CK_TEST_SMP_SKIP_CPU=2 OUT="${tmp}/o2" 2>&1)"; rc=$?
204 if [[ ${rc} != 0 ]] && grep -qF "CK_TEST_SMP_SKIP_CPU must be empty or 1" <<<"${out}"; then
205 echo "PASS self-test mutation flag value other than 1 refused"
206 else
207 echo "FAIL self-test bad mutation flag value accepted (rc=${rc})"; st_fail=1
208 fi
209 [[ ! -e "${tmp}/o1" && ! -e "${tmp}/o2" ]] && echo "PASS self-test refusals built nothing" || { echo "FAIL self-test a refused build wrote output"; st_fail=1; }
210 if grep -qE '^#if defined\(CK_TEST_SMP_SKIP_CPU\) && defined\(CK_HARDWARE_STAGING\)$' native/kernel/core/smp.c; then
211 echo "PASS self-test core/smp.c also refuses the mutation in a hardware staging compile"
212 else
213 echo "FAIL self-test core/smp.c lacks the #error guard"; st_fail=1
214 fi
215
216 [[ ${st_fail} == 0 ]] && { echo "AIENOS_CK_SMP_SELF_TEST: PASS"; exit 0; }
217 echo "AIENOS_CK_SMP_SELF_TEST: FAIL"; exit 1
218}
219if [[ "${mode}" == self ]]; then
220 set +e
221 self_test
222fi
223
224# --------------------------------------------------------------- QEMU boot
225verdict_name=AIENOS_CK_SMP
226[[ "${mode}" == mutation ]] && verdict_name=AIENOS_CK_SMP_MUTATION
227
228code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
229vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
230command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict_name}: NOT_RUN"; exit 2; }
231[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict_name}: NOT_RUN"; exit 2; }
232
233cross=""
234if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
235commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
236if [[ "${mode}" == mutation ]]; then
237 out="${repo_root}/target/native-kernel-test-smp-skip"
238 make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" CK_TEST_SMP_SKIP_CPU=1 >/dev/null
239else
240 out="${repo_root}/target/native-kernel"
241 make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" >/dev/null
242fi
243
244# Machine quiet flag: one heavy run at a time on the Spark.
245# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
246# scripts/lib_gate_hold.sh (aienos#278).
247. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
248if ! gh_quiet_check || ! gh_lock_take "qemu_ck_smp_test" "${AIENOS_GATE_MINUTES:-60}"; then
249 echo "NOT_RUN ${gh_why}"
250 echo "${verdict_name}: NOT_RUN"
251 exit 3
252fi
253# Release only this run's own gate lock record, at most once.
254release_flag() { gh_lock_release; }
255work="$(mktemp -d)"
256cleanup() {
257 rm -rf "${work}"
258 release_flag
259}
260trap cleanup EXIT
261mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
262touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
263cp "${out}/BOOTAA64.EFI" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
264cp "${vars_fd}" "${work}/vars.fd"
265log="${work}/serial.log"
266
267# Same machine as scripts/qemu_ck_boot_test.sh (issue #61: single-threaded TCG).
268started=$(date +%s)
269set +e
270timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \
271 -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp "${ncpus}" -m 2048 \
272 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
273 -drive if=pflash,format=raw,file="${work}/vars.fd" \
274 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
275 -device virtio-blk-pci,drive=esp \
276 -device ramfb -display none -nic none \
277 -serial file:"${log}" -no-reboot
278qemu_status=$?
279set -e
280elapsed=$(( $(date +%s) - started ))
281release_flag
282
283tr -d '\r' <"${log}" >"${work}/serial.txt"
284[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_smp_${mode}_serial.log"
285echo "qemu exit ${qemu_status} after ${elapsed} s (commit ${commit:0:12}, -smp ${ncpus}, ${mode})"
286
287smp_failed=0
288results="$(smp_check_log "${work}/serial.txt" "${ncpus}")" || true
289grep -q '^FAIL' <<<"${results}" && smp_failed=1
290printf '%s\n' "${results}"
291if [[ "${qemu_status}" == 0 ]]; then echo "PASS QEMU exit 0 (PSCI reset)"; else echo "FAIL QEMU exit ${qemu_status}"; smp_failed=1; fi
292
293if [[ "${mode}" == mutation ]]; then
294 # The mutation must be the reason: the TEST-ONLY image announced itself,
295 # skipped one CPU_ON, and that core never checked in.
296 killed=1
297 grep -qF "${mut_marker} build" "${work}/serial.txt" && echo "PASS mutation image announced itself" || { echo "FAIL mutation image did not announce itself"; killed=0; }
298 grep -qE '^smp_cpu_on: target=0x[0-9a-f]+ psci_rc=SKIPPED' "${work}/serial.txt" && echo "PASS one CPU_ON skipped" || { echo "FAIL no CPU_ON was skipped"; killed=0; }
299 grep -qE '^smp_cpu: target=0x[0-9a-f]+ checked_in=no$' "${work}/serial.txt" && echo "PASS the skipped core never checked in" || { echo "FAIL no core reported missing"; killed=0; }
300 if [[ -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then echo "---- serial console ----"; cat "${work}/serial.txt"; fi
301 if [[ "${killed}" == 1 && "${smp_failed}" == 1 ]]; then
302 echo "AIENOS_CK_SMP_MUTATION: PASS (gate checks FAIL on the CPU_ON-skip image; mutation killed)"
303 exit 0
304 fi
305 echo "AIENOS_CK_SMP_MUTATION: FAIL (mutation survived or did not run as built)"
306 exit 1
307fi
308
309if grep -qF "${mut_marker}" "${work}/serial.txt"; then echo "FAIL gate image is the TEST-ONLY mutation build"; smp_failed=1; else echo "PASS default image (no TEST-ONLY mutation)"; fi
310if [[ "${smp_failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
311 echo "---- serial console ----"
312 cat "${work}/serial.txt"
313fi
314if [[ "${smp_failed}" == 0 ]]; then
315 echo "AIENOS_CK_SMP: PASS"
316 exit 0
317fi
318echo "AIENOS_CK_SMP: FAIL"
319exit 1