interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / spark / run_spark_qemu_e2e.sh

76 lines · 4626 bytes Raw Blame History
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0
3# aienux/spark/run_spark_qemu_e2e.sh [EFI_OMC]: build the REAL spark_main.om with the QEMU platform profile
4# (PLAT=qemu -> SPARK-QEMU.EFI, gate-only) and run it under QEMU virt,iommu=smmuv3 with a read-only NVMe
5# disk (GPT + ext4 + synthetic OMFWBND1 bundle) and an edu device standing in for the GPU endpoint.
6# Real: Stage 1, Stage 2 (MCFG/ECAM probe, BAR map, quiesce, IORT, mode-1 SMMUv3 bring-up, per-device
7# streams, BME enable), Stage 3 disk + bundle (NVMe RO, GPT, ext4, TOC + SHA-256). Stubbed: GB20B
8# register access, RTL8127/TCP, Stages 4..6. QEMU-only; touches no hardware.
9set -eu
10ROOT=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd)
11OMC="${1:-${OMC:-$ROOT/seed/out/omc}}"
12TMP=$(mktemp -d "${TMPDIR:-/tmp}/spark-qemu-e2e.XXXXXX")
13trap 'rm -rf "$TMP"' EXIT
14PLAT=qemu OMC="$OMC" sh "$ROOT/aienux/spark/build_spark_efi.sh" "$TMP/SPARK-QEMU.EFI"
15python3 "$ROOT/aienux/spark/mk_qemu_disk.py" "$TMP/disk.img" "$TMP"
16code=${A64EFI_CODE:-}
17for f in /opt/homebrew/share/qemu/edk2-aarch64-code.fd \
18 /usr/share/qemu-efi-aarch64/QEMU_EFI.fd \
19 /usr/share/edk2/aarch64/QEMU_EFI.fd \
20 /usr/share/AAVMF/QEMU_EFI.fd; do
21 [ -z "$code" ] && [ -f "$f" ] && code=$f
22done
23[ -n "$code" ] && [ -f "$code" ] || { echo "FAIL: EDK2 firmware not found" >&2; exit 1; }
24mkdir -p "$TMP/esp/EFI/BOOT"
25cp "$TMP/SPARK-QEMU.EFI" "$TMP/esp/EFI/BOOT/BOOTAA64.EFI"
26run_qemu() { # $1 = disk image, $2 = serial log, $3 = ESP dir (default $TMP/esp)
27 timeout 120 qemu-system-aarch64 \
28 -M virt,gic-version=3,its=on,virtualization=on,iommu=smmuv3 -cpu max -smp 20 -m 4096 \
29 -display none -bios "$code" \
30 -drive if=none,id=d0,format=raw,file=fat:rw:"${3:-$TMP/esp}" \
31 -device virtio-blk-device,drive=d0 \
32 -drive if=none,id=nv0,format=raw,readonly=on,file="$1" \
33 -device nvme,serial=spk0,drive=nv0 \
34 -device edu,dma_mask=0xffffffffff \
35 -net none -serial file:"$2" -monitor none -no-reboot </dev/null >/dev/null 2>&1 || true
36}
37run_qemu "$TMP/disk.img" "$TMP/qemu.log"
38tr -d '\r' < "$TMP/qemu.log" | grep -E '\[SPARK|SMP_ONLINE|SPARK_QEMU|PANIC' || true
39if grep -q 'PANIC' "$TMP/qemu.log"; then echo "FAIL: PANIC" >&2; exit 1; fi
40if grep -q '\[SPARK FAIL' "$TMP/qemu.log"; then echo "FAIL: spark_main stage failure under QEMU" >&2; exit 1; fi
41grep -q 'SMP_ONLINE online=20 expected=20 failed=0' "$TMP/qemu.log"
42grep -q '\[SPARK 2/6\]' "$TMP/qemu.log"
43grep -q '\[SPARK QEMU\] stage 2 SMMUv3 streams live' "$TMP/qemu.log"
44grep -q '\[SPARK 3/6\]' "$TMP/qemu.log"
45grep -q '\[SPARK QEMU\] stage 3 bundle verified' "$TMP/qemu.log"
46grep -q 'SPARK_QEMU_E2E_PASS real=stage1,stage2' "$TMP/qemu.log"
47# Negative: flip one payload byte of bundle entry 0 -> SHA-256 mismatch must fail closed at stage 3 (304).
48python3 - "$TMP/disk.img" "$TMP/disk-bad.img" <<'PY'
49import sys
50d = bytearray(open(sys.argv[1], "rb").read())
51i = d.find(b"OMFWBND1")
52assert i > 0
53d[i + 4096 + 100] ^= 1
54open(sys.argv[2], "wb").write(d)
55PY
56run_qemu "$TMP/disk-bad.img" "$TMP/qemu-bad.log"
57tr -d '\r' < "$TMP/qemu-bad.log" | grep -E '\[SPARK FAIL|SPARK_QEMU_E2E_PASS' || true
58grep -q '\[SPARK FAIL stage=3 code=304\]' "$TMP/qemu-bad.log"
59if grep -q 'SPARK_QEMU_E2E_PASS' "$TMP/qemu-bad.log"; then echo "FAIL: tampered bundle accepted" >&2; exit 1; fi
60# Negative: a platform profile whose SMMU base disagrees with the firmware IORT must fail closed at
61# stage 2 with SPK_E_SMMU_BASE (208) before any SMMU is programmed.
62sed 's/ret 0x09050000/ret 0x09060000/' "$ROOT/aienux/spark/plat_qemu.om" > "$TMP/plat_qemu_badbase.om"
63if cmp -s "$ROOT/aienux/spark/plat_qemu.om" "$TMP/plat_qemu_badbase.om"; then
64 echo "FAIL: 208 negative control could not alter the QEMU SMMU base" >&2; exit 1
65fi
66PLAT=qemu PLAT_OM_OVERRIDE="$TMP/plat_qemu_badbase.om" OMC="$OMC" \
67 sh "$ROOT/aienux/spark/build_spark_efi.sh" "$TMP/SPARK-QEMU-BADBASE.EFI" >/dev/null
68mkdir -p "$TMP/esp-badbase/EFI/BOOT"
69cp "$TMP/SPARK-QEMU-BADBASE.EFI" "$TMP/esp-badbase/EFI/BOOT/BOOTAA64.EFI"
70run_qemu "$TMP/disk.img" "$TMP/qemu-badbase.log" "$TMP/esp-badbase"
71tr -d '\r' < "$TMP/qemu-badbase.log" | grep -E '\[SPARK FAIL|SPARK_QEMU_E2E_PASS|streams live' || true
72grep -q '\[SPARK FAIL stage=2 code=208\]' "$TMP/qemu-badbase.log"
73if grep -qE 'SPARK_QEMU_E2E_PASS|stage 2 SMMUv3 streams live' "$TMP/qemu-badbase.log"; then
74 echo "FAIL: mismatched SMMU base accepted" >&2; exit 1
75fi
76echo "PASS spark_main_qemu_e2e (real spark_main Stage 1 + Stage 2 incl. SMMUv3 mode-1 + Stage 3 NVMe/GPT/ext4/bundle; GB20B regs, net, Stages 4-6 stubbed; tampered bundle -> stage 3 code 304; IORT/platform SMMU base mismatch -> stage 2 code 208)"