diff --git a/.github/workflows/ck-kernel.yml b/.github/workflows/ck-kernel.yml index 3276e7ca..750531c1 100644 --- a/.github/workflows/ck-kernel.yml +++ b/.github/workflows/ck-kernel.yml @@ -32,6 +32,7 @@ on: - 'native/**' - 'scripts/ck_gates.sh' - 'scripts/qemu_ck_*.sh' + - 'crates/aienos-fpu-probe/**' - 'scripts/lib_ck_m1_checks.sh' - '.github/workflows/ck-kernel.yml' pull_request: @@ -40,6 +41,7 @@ on: - 'native/**' - 'scripts/ck_gates.sh' - 'scripts/qemu_ck_*.sh' + - 'crates/aienos-fpu-probe/**' - 'scripts/lib_ck_m1_checks.sh' - '.github/workflows/ck-kernel.yml' @@ -96,9 +98,11 @@ jobs: mkdir -p "${RUNNER_TEMP}/ck-receipt/logs" cp -r /tmp/ck-gates-work.*/logs/* "${RUNNER_TEMP}/ck-receipt/logs/" 2>/dev/null || true [ "${rc}" = 0 ] || exit 1 + # FPU is not required: it needs the Rust target aarch64-unknown-none, which this + # runner image is not known to have, so it prints NOT_RUN there (aienos#34 lane 0). # Every gate with a C implementation (source != missing in the # ck_gates.sh table) must be PASS. - required=$(sed -n "/^CK_GATE_TABLE='/,/^'/p" scripts/ck_gates.sh | awk -F'|' 'NF==3 && $2!="missing" {print $1}') + required=$(sed -n "/^CK_GATE_TABLE='/,/^'/p" scripts/ck_gates.sh | awk -F'|' 'NF==3 && $2!="missing" && $1!="FPU" {print $1}') [ -n "${required}" ] || { echo "could not read the gate table"; exit 1; } fail=0 for g in ${required}; do diff --git a/crates/aienos-fpu-probe/Cargo.lock b/crates/aienos-fpu-probe/Cargo.lock new file mode 100644 index 00000000..db84a858 --- /dev/null +++ b/crates/aienos-fpu-probe/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aienos-fpu-probe" +version = "0.1.0" diff --git a/crates/aienos-fpu-probe/Cargo.toml b/crates/aienos-fpu-probe/Cargo.toml new file mode 100644 index 00000000..67060340 --- /dev/null +++ b/crates/aienos-fpu-probe/Cargo.toml @@ -0,0 +1,22 @@ +# Standalone (not a member of the host workspace): built only for +# aarch64-unknown-none and linked into the C kernel image (CK_RUST_LIBS). +[package] +name = "aienos-fpu-probe" +version = "0.1.0" +edition = "2021" +license = "Apache-2.0 WITH LLVM-exception" +description = "One FP/SIMD-enabled Rust unit called from the general-regs-only C kernel (aienos#34 lane 0)." + +[lib] +crate-type = ["staticlib"] + +[workspace] + +[profile.release] +panic = "abort" +opt-level = 2 +lto = false +codegen-units = 1 + +[profile.dev] +panic = "abort" diff --git a/crates/aienos-fpu-probe/src/lib.rs b/crates/aienos-fpu-probe/src/lib.rs new file mode 100644 index 00000000..86e099a6 --- /dev/null +++ b/crates/aienos-fpu-probe/src/lib.rs @@ -0,0 +1,97 @@ +//! FP/SIMD probe: one unit that uses f32 math and NEON, called from the C +//! kernel (which is built -mgeneral-regs-only and never touches FP itself). +//! Inputs go through black_box so LLVM cannot constant-fold the FP work away +//! (the gate must execute real FP/SIMD instructions). No alloc, no std, no panics reachable (panic=abort, handler spins). +#![no_std] + +#[cfg(target_arch = "aarch64")] +use core::arch::aarch64::*; + +#[panic_handler] +fn panic(_: &core::panic::PanicInfo) -> ! { + loop {} +} + +/// Words written to `out` by `aienos_fpu_probe`. +pub const OUT_WORDS: usize = 6; + +/// exp(x) for |x| < 80: x*log2(e) = n + f, 2^f by a degree-6 polynomial +/// (Taylor of e^(f*ln2), f in [0,1)), 2^n by building the exponent bits. +fn exp_approx(x: f32) -> f32 { + let t = x * core::f32::consts::LOG2_E; + let mut n = t as i32; // truncates toward zero + if (n as f32) > t { + n -= 1; // floor + } + let f = (t - n as f32) * core::f32::consts::LN_2; // in [0, ln2) + let p = 1.0 + + f * (1.0 + + f * (0.5 + + f * (1.0 / 6.0 + f * (1.0 / 24.0 + f * (1.0 / 120.0 + f * (1.0 / 720.0)))))); + let scale = f32::from_bits(((n + 127) as u32) << 23); + p * scale +} + +/// Writes six u32 words to `out`: +/// [0] bits of dot(a, b) with a[i]=i+1, b[i]=0.5 (i in 0..16): exactly 68.0 +/// [1] bits of exp_approx(1.0) +/// [2..6] NEON vaddq_f32([1,2,3,4], [10,20,30,40]) lanes as bits: 11,22,33,44 +/// Returns 0 when all three agree with their known answers (dot exact, +/// exp(1) within 1e-4, NEON exact), else a bit mask of the failing parts +/// (1 dot, 2 exp, 4 neon), or -1 for a null `out`. +/// +/// # Safety +/// `out` must point to at least 6 writable u32. FP/SIMD must be enabled for +/// the calling exception level (CPACR_EL1.FPEN) and interrupts masked: the +/// kernel does not save FP state across exceptions. +#[no_mangle] +pub unsafe extern "C" fn aienos_fpu_probe(out: *mut u32) -> i32 { + if out.is_null() { + return -1; + } + let mut a = [0.0f32; 16]; + let mut b = [0.0f32; 16]; + let mut i = 0; + while i < 16 { + a[i] = core::hint::black_box((i + 1) as f32); + b[i] = core::hint::black_box(0.5f32); + i += 1; + } + let mut dot = 0.0f32; + i = 0; + while i < 16 { + dot += a[i] * b[i]; + i += 1; + } + let e = exp_approx(core::hint::black_box(1.0f32)); + + let mut lanes = [0.0f32; 4]; + #[cfg(target_arch = "aarch64")] + { + let x = core::hint::black_box([1.0f32, 2.0, 3.0, 4.0]); + let y = core::hint::black_box([10.0f32, 20.0, 30.0, 40.0]); + let s = vaddq_f32(vld1q_f32(x.as_ptr()), vld1q_f32(y.as_ptr())); + vst1q_f32(lanes.as_mut_ptr(), s); + } + + out.add(0).write_volatile(dot.to_bits()); + out.add(1).write_volatile(e.to_bits()); + i = 0; + while i < 4 { + out.add(2 + i).write_volatile(lanes[i].to_bits()); + i += 1; + } + + let mut rc = 0; + if dot != 68.0 { + rc |= 1; + } + let d = e - core::f32::consts::E; + if !(d < 1e-4 && d > -1e-4) { + rc |= 2; + } + if lanes != [11.0, 22.0, 33.0, 44.0] { + rc |= 4; + } + rc +} diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 6e76b9ed..5694ae6e 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -25,7 +25,7 @@ C gates (one line each from `ck_gates.sh`): `M1` and `M3` (from `scripts/qemu_ck_net_test.sh`; rows 102-106 and 110-112; the script now also checks the virtio-net SMMU fence (ACCESS_PLATFORM required, out-of-window device DMA refused), rows 102 and 110-112 are QEMU PASS on this branch from the committed forge log `evidence/ck_net_qemu_8bdbc2e46b85d04c422f9d2830c1c4b263e254b3cfecfe21ff0063c9e33c86e8.log`, hardware NOT_RUN), the kernel entropy rows 107-109 (M1 via `scripts/lib_ck_m1_checks.sh`, ARGUS1_REVOKE and M4_STORE via the Store gate boot 7), `DISK_LAYOUT` (C-only, from `scripts/qemu_ck_disk_layout_test.sh`, rows 118-123: QEMU PASS at 741b2b8 before the merge with main 8555049, rerun pending at the merged head; row 123 host test), and the NOT_RUN gates `SMP` (C-only, from `scripts/qemu_ck_smp_test.sh`, rows 113-117, NOT_RUN pending forge receipt), `M0_ROLLBACK`, `M4_STORE_CRASH` (now read from `scripts/qemu_ck_store_crash_test.sh`, rows 73-80; NOT_RUN until a forge receipt exists), -`M4_CONTINUITY`, `M4_RECOVERY`, `KEYBOARD`. +`M4_CONTINUITY`, `M4_RECOVERY`, `KEYBOARD`, and `FPU` (C-only, from `scripts/qemu_ck_fpu_test.sh`, rows 124-128; QEMU PASS, hardware NOT_RUN). `M4_NVME` and `SMMU` PASS in the SMMU-confined mode (default `make full` image, QEMU `iommu=smmuv3`). The unconfined bypass build @@ -512,3 +512,22 @@ qemu rows: `ck_m1_boot_qemu` (M1), `ck_store_kernel_qemu` (M4_STORE) and `ck_argus1_revoke_qemu` (ARGUS1_REVOKE). Its `m5_store_kernel_binding` row stays MISSING_IMPLEMENTATION: it asks for the binding on a real device, which QEMU cannot show. + +## FPU: scripts/qemu_ck_fpu_test.sh -> CK `FPU` (aienos#34 lane 0, C-only, no Rust-kernel counterpart) + +One floating-point/SIMD-enabled Rust unit (`crates/aienos-fpu-probe`, a no_std staticlib) is linked into a +QEMU-only probe image (`make -C native/kernel CK_RUST_LIBS=`, which adds +`-DCK_FPU_PROBE=1` for `core/fpu.c`) and called from the C kernel, which stays `-mgeneral-regs-only`. The +default image is unchanged (no Rust, no FP code). The kernel runs at EL1h; `ck_fpu_enable()` sets +`CPACR_EL1.FPEN = 0b11` + `isb` (vectors.S already did at the EL2 -> EL1 drop; CPTR_EL2 is set to no-trap +there). The unit runs with DAIF masked: the kernel saves no FP state. The gate is NOT_RUN (never a silent +pass) when cargo, the `aarch64-unknown-none` target, QEMU or AAVMF is missing. Not required in CI (the +runner is not known to have the Rust target); required list in `ck-kernel.yml` skips it. + +| # | check | gate | status | +|---|---|---|---| +| 124 | probe returns 0; f32 dot product of two 16-element arrays = 68.0 exactly (`dot_bits=0x42880000`) | FPU | QEMU PASS (aienos-ni-fp branch; hardware NOT_RUN) | +| 125 | f32 exp approximation: exp(1) within 1e-4 of e (bits checked by the script, not the kernel) | FPU | QEMU PASS (hardware NOT_RUN) | +| 126 | NEON `vaddq_f32` lanes = 11,22,33,44 via `core::arch::aarch64` | FPU | QEMU PASS (hardware NOT_RUN) | +| 127 | `CPACR_EL1.FPEN = 0b11` read back at EL1 after `ck_fpu_enable()` | FPU | QEMU PASS (hardware NOT_RUN) | +| 128 | negative control: with FPEN cleared in `ck_fpu_enable()` the first FP instruction traps and the gate FAILs (kernel fault report, no result line); canned-log self-test (`--self-test`) rejects wrong dot/exp/NEON/rc/FPEN/missing lines | FPU | QEMU PASS (control killed; hardware NOT_RUN) | diff --git a/native/kernel/Makefile b/native/kernel/Makefile index ca4d400a..4ec3d82b 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -145,6 +145,19 @@ CK_EXTRA_CFLAGS ?= # no SMMU exists. Never with hardware staging. CK_QEMU_UNSAFE_DMA ?= CK_HARDWARE_STAGING ?= + +# FP/SIMD probe (aienos#34 lane 0): CK_RUST_LIBS= +# links that Rust no_std staticlib into the image and compiles core/fpu.c's +# probe (-DCK_FPU_PROBE=1). The C compile flags stay -mgeneral-regs-only; only +# the Rust unit uses FP/SIMD. Empty (default) = no Rust, no FP code. QEMU +# gate only: never combined with hardware staging. +CK_RUST_LIBS ?= +ifneq ($(strip $(CK_RUST_LIBS)),) +ifneq ($(strip $(CK_HARDWARE_STAGING)),) +$(error CK_RUST_LIBS (FP probe, QEMU gate only) cannot be combined with CK_HARDWARE_STAGING) +endif +CK_EXTRA_CFLAGS += -DCK_FPU_PROBE=1 +endif ifneq ($(strip $(CK_QEMU_UNSAFE_DMA)),) ifneq ($(strip $(CK_HARDWARE_STAGING)),) $(error CK_QEMU_UNSAFE_DMA (unconfined DMA, QEMU debug only) cannot be combined with CK_HARDWARE_STAGING: no SMMU confinement means no DMA) @@ -333,7 +346,7 @@ CORE_C := $(BOOT)/efi_main.c \ $(KDIR)/core/acpi.c $(KDIR)/core/console.c $(KDIR)/core/fmt.c \ $(KDIR)/core/kmain.c $(KDIR)/core/handoff_check.c $(KDIR)/core/libc_min.c $(KDIR)/core/report.c \ $(KDIR)/core/smmu.c $(KDIR)/core/smmu_svc.c $(KDIR)/core/sched.c $(KDIR)/core/ipc.c \ - $(KDIR)/core/m3.c $(KDIR)/core/entropy.c $(KDIR)/arch/rndr.c $(KDIR)/core/smp.c \ + $(KDIR)/core/fpu.c $(KDIR)/core/m3.c $(KDIR)/core/entropy.c $(KDIR)/arch/rndr.c $(KDIR)/core/smp.c \ $(KDIR)/mm/frames.c $(KDIR)/mm/heap.c $(KDIR)/mm/mmu.c $(KDIR)/mm/pt.c # P2 sealed-artifact loader: format/admission/receipt (artifact/), SHA-256 # (argus), Ed25519/SHA-512 (sig). Built into the core so every image carries @@ -548,14 +561,14 @@ define pe_image endef $(ELF): $(CORE_OBJS) $(BOOT)/image.ld - $(call link_image,$@,$(CORE_OBJS)) + $(call link_image,$@,$(CORE_OBJS) $(CK_RUST_LIBS)) @s=$$($(KNM) $@ | grep -E ' [TtDdBbWwVv] ck_stage_(devices|security|store|quiesce)$$' || true); \ if [ -n "$$s" ]; then echo "core image defines stage symbols: $$s"; rm -f $@; exit 1; fi $(call fwcfg_check,$@) $(call smp_check,$@) $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld - $(call link_image,$@,$(CORE_OBJS) $(STAGE_LINK)) + $(call link_image,$@,$(CORE_OBJS) $(STAGE_LINK) $(CK_RUST_LIBS)) @for s in ck_stage_devices ck_stage_security ck_stage_store ck_stage_quiesce ck_stage_disk_artifacts; do \ $(KNM) $@ | grep -qE " [TW] $$s$$" || { echo "full image lacks $$s"; rm -f $@; exit 1; }; done $(call fwcfg_check,$@) diff --git a/native/kernel/core/ck_internal.h b/native/kernel/core/ck_internal.h index ef5b6441..b8175e14 100644 --- a/native/kernel/core/ck_internal.h +++ b/native/kernel/core/ck_internal.h @@ -103,5 +103,6 @@ uint64_t ck_entropy_retries(void); * the MMU on the boot core's tables, checked in and parked in WFE. Prints * the smp: lines scripts/qemu_ck_smp_test.sh judges; never panics. */ void ck_smp_run(void); +void ck_fpu_run(void); /* core/fpu.c: probe build only (CK_RUST_LIBS), else a no-op */ #endif diff --git a/native/kernel/core/fpu.c b/native/kernel/core/fpu.c new file mode 100644 index 00000000..9c1ead6c --- /dev/null +++ b/native/kernel/core/fpu.c @@ -0,0 +1,57 @@ +/* fpu.c -- run ONE floating-point/SIMD-enabled unit (a Rust no_std staticlib, + * crates/aienos-fpu-probe) from the general-regs-only C kernel (aienos#34 + * lane 0). Only compiled into the probe build: Makefile CK_RUST_LIBS=<.a> + * adds -DCK_FPU_PROBE=1; the default image carries none of this. + * + * The kernel runs at EL1h (EL2 -> EL1 drop in arch/vectors.S). FP/SIMD + * access at EL1 is governed by CPACR_EL1.FPEN (0b11 = no trap) and, from + * EL2, by CPTR_EL2 (vectors.S sets it to no-trap before the drop). The + * kernel never saves FP state: no exception handler touches v0-v31 and the + * kernel is built -mgeneral-regs-only. So the unit runs with every exception + * masked (DAIF.DAIF) from the first FP instruction to the last, and FP + * registers are left dirty (nothing else in the kernel reads them; EL0 runs + * with FPEN=01 and traps, never sees them). A future FP-using scheduler must + * save/restore v0-v31/FPSR/FPCR before dropping that restriction. */ +#include + +#include "arch.h" +#include "ck_internal.h" + +#ifdef CK_FPU_PROBE +extern int aienos_fpu_probe(uint32_t *out); + +#define CPACR_FPEN_SHIFT 20 + +/* Set CPACR_EL1.FPEN = 0b11 and synchronise. Idempotent: vectors.S already + * does this at the EL2 -> EL1 drop; doing it here makes the unit not depend + * on that. */ +void ck_fpu_enable(void) +{ + uint64_t v = ck_rd(cpacr_el1); + v |= 3ull << CPACR_FPEN_SHIFT; + ck_wr(cpacr_el1, v); + ck_isb(); +} + +void ck_fpu_run(void) +{ + uint32_t out[6] = {0, 0, 0, 0, 0, 0}; + uint64_t daif; + int rc; + + ck_fpu_enable(); + daif = ck_rd(daif); + __asm__ volatile("msr daifset, #0xf" ::: "memory"); + rc = aienos_fpu_probe(out); + ck_wr(daif, daif); + + int ok = rc == 0 && out[0] == 0x42880000u /* 68.0f */ && out[2] == 0x41300000u /* 11.0f */ && + out[3] == 0x41b00000u /* 22.0f */ && out[4] == 0x42040000u /* 33.0f */ && + out[5] == 0x42300000u /* 44.0f */; + ck_printf("fpu: el1_cpacr=0x%llx rc=%d dot_bits=0x%x exp1_bits=0x%x neon_bits=0x%x,0x%x,0x%x,0x%x\n", + (unsigned long long)ck_rd(cpacr_el1), rc, out[0], out[1], out[2], out[3], out[4], out[5]); + ck_printf("AIENOS_CK_FPU: %s\n", ok ? "PASS" : "FAIL"); +} +#else +void ck_fpu_run(void) {} +#endif diff --git a/native/kernel/core/kmain.c b/native/kernel/core/kmain.c index 64d81223..2d19c7a5 100644 --- a/native/kernel/core/kmain.c +++ b/native/kernel/core/kmain.c @@ -138,6 +138,9 @@ static __attribute__((noreturn)) void ck_el1_main(void *arg) * scripts/qemu_ck_smp_test.sh (CK gate SMP). */ ck_smp_run(); + /* One FP/SIMD-enabled Rust unit (core/fpu.c), probe build only. */ + ck_fpu_run(); + /* M3 isolation checks (core/m3.c), before any stage registers an IRQ. */ ck_m3_run(); diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index da984017..a65db5bf 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -7,11 +7,11 @@ # scripts/qemu_ck_net_test.sh, scripts/qemu_ck_artifact_test.sh, # scripts/qemu_ck_smp_test.sh, scripts/qemu_ck_store_crash_test.sh, # scripts/qemu_ck_disk_layout_test.sh, scripts/qemu_ck_continuity_test.sh, -# scripts/qemu_ck_recovery_test.sh), and prints +# scripts/qemu_ck_recovery_test.sh, scripts/qemu_ck_fpu_test.sh), and prints # one line per gate: # AIENOS_CK_: PASS|FAIL|NOT_RUN [(reason)] # for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH -# M4_CONTINUITY M4_RECOVERY ARGUS1_REVOKE KEYBOARD NET SMP DISK_LAYOUT. Gates with no C +# M4_CONTINUITY M4_RECOVERY ARGUS1_REVOKE KEYBOARD NET SMP DISK_LAYOUT FPU. Gates with no C # implementation print NOT_RUN (MISSING_IMPLEMENTATION: ) and never # run anything. A missing child script, a child that reports NOT_RUN, a child # that prints no verdict line, or a PASS line contradicted by the child's exit @@ -48,6 +48,7 @@ set -uo pipefail # disk -> verdict line from scripts/qemu_ck_disk_layout_test.sh # cont -> verdict line from scripts/qemu_ck_continuity_test.sh # recov -> verdict line from scripts/qemu_ck_recovery_test.sh +# fpu -> verdict line from scripts/qemu_ck_fpu_test.sh (NOT_RUN without the Rust target) # missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS # =========================================================================== CK_GATE_TABLE=' @@ -67,8 +68,9 @@ KEYBOARD|missing|no xHCI/USB HID keyboard driver in the C kernel NET|net|- SMP|smp|- DISK_LAYOUT|disk|- +FPU|fpu|- ' -CHILDREN=(boot store net artifact smp crash disk cont recov) +CHILDREN=(boot store net artifact smp crash disk cont recov fpu) # =========================================================================== repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -115,6 +117,7 @@ set_default_children() { [disk]="${repo_root}/scripts/qemu_ck_disk_layout_test.sh" [cont]="${repo_root}/scripts/qemu_ck_continuity_test.sh" [recov]="${repo_root}/scripts/qemu_ck_recovery_test.sh" + [fpu]="${repo_root}/scripts/qemu_ck_fpu_test.sh" ) } @@ -344,11 +347,11 @@ self_test() { } expect_missing_all() { local g - for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD DISK_LAYOUT; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R; DISK_LAYOUT: no disk child in A..S + for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD DISK_LAYOUT FPU; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R; DISK_LAYOUT: no disk child in A..S } scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] [CRASH_SCRIPT] [DISK_SCRIPT] (absent: missing) scen="$1" - declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh") + declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}" [disk]="${8:-${tmp}/kids/no_disk_child.sh}" [cont]="${tmp}/kids/no_cont_child.sh" [recov]="${tmp}/kids/no_recov_child.sh" [fpu]="${tmp}/kids/no_fpu_child.sh") run_children 2>/dev/null evaluate_table >"${tmp}/${scen}.out" } @@ -364,8 +367,8 @@ self_test() { "$(fake smpA 0 'PASS 3 secondary cores checked in' 'AIENOS_CK_SMP_MUTATION: FAIL' 'AIENOS_CK_SMP: PASS')" expect M1 PASS; expect M3 PASS; expect M4_NVME PASS; expect M4_STORE PASS; expect ARGUS1_REVOKE PASS; expect SMMU PASS; expect NVME_SHUTDOWN PASS; expect NET PASS; expect SMP PASS; expect_missing_all count_rows - [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "9/0/7/16" && ${overall} == NOT_ALL_GATES_PASS ]] \ - && ok "A: counts 9/0/7 of 16, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" + [[ "${n_pass}/${n_fail}/${n_notrun}/${n_total}" == "9/0/8/17" && ${overall} == NOT_ALL_GATES_PASS ]] \ + && ok "A: counts 9/0/8 of 17, verdict NOT_ALL_GATES_PASS" || bad "A: counts ${n_pass}/${n_fail}/${n_notrun}/${n_total} ${overall}" [[ "$(nvme_dma_mode)" == "Confined (QEMU SMMUv3 stage 1, out-of-window DMA faulted); no-SMMU boot denied (NoSmmu); TEST-ONLY bypass image checked separately" ]] && ok "A: confined DMA mode recorded from the store PASS lines" || bad "A: dma mode '$(nvme_dma_mode)'" grep -qxF "AIENOS_CK_M4_NVME: PASS (${M4_NVME_PASS_NOTE})" "${tmp}/A.out" \ && ok "A: M4_NVME PASS line carries the confined-mode note" || bad "A: M4_NVME PASS line lacks the note" @@ -373,7 +376,7 @@ self_test() { && ok "A: NET PASS line carries the QEMU slirp / SMMU fence note" || bad "A: NET PASS line lacks the note" grep -qx 'AIENOS_CK_KEYBOARD: NOT_RUN (MISSING_IMPLEMENTATION: no xHCI/USB HID keyboard driver in the C kernel)' "${tmp}/A.out" \ && ok "A: missing gate prints NOT_RUN (MISSING_IMPLEMENTATION: reason)" || bad "A: KEYBOARD line wrong" - [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 16 ]] && ok "A: exactly 16 verdict lines" || bad "A: verdict line count" + [[ "$(grep -c '^AIENOS_CK_[A-Z0-9_]*: ' "${tmp}/A.out")" == 17 ]] && ok "A: exactly 17 verdict lines" || bad "A: verdict line count" # B: boot FAIL; store script missing -> its three gates NOT_RUN, never PASS. scenario B "$(fake bootB 1 'FAIL kernel: alive' 'AIENOS_CK_M3: FAIL' 'AIENOS_CK_M1: FAIL')" "${tmp}/kids/does_not_exist.sh" @@ -486,10 +489,10 @@ self_test() { # Restore the clean-tree receipt as the one the checks below look at. write_receipt if command -v jq >/dev/null; then - jq -e '.physical == "NOT_RUN" and (.gates | length) == 16 and .verdict == "NOT_ALL_GATES_PASS" + jq -e '.physical == "NOT_RUN" and (.gates | length) == 17 and .verdict == "NOT_ALL_GATES_PASS" and ([.gates[] | select(.id == "M1")][0].verdict == "PASS") and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL") - and (.children | length) == 9 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ + and (.children | length) == 10 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong" else echo "NOT_RUN jq not installed; JSON validity not machine-checked"; jq_skipped=1 @@ -502,18 +505,18 @@ self_test() { bad "an existing receipt was overwritten or the clash was not refused" fi - # The gate table parses: 16 rows, known sources, reasons only on missing rows. + # The gate table parses: 17 rows, known sources, reasons only on missing rows. local g s w rows=0 tbad=0 while IFS='|' read -r g s w; do [[ -n "${g}" ]] || continue rows=$((rows + 1)) case "${s}" in - boot|store|net|artifact|smp|crash|disk|cont|recov) [[ "${w}" == - ]] || tbad=1 ;; + boot|store|net|artifact|smp|crash|disk|cont|recov|fpu) [[ "${w}" == - ]] || tbad=1 ;; missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;; *) tbad=1 ;; esac done <<<"${CK_GATE_TABLE}" - [[ ${tbad} == 0 && ${rows} == 16 ]] && ok "gate table: 16 rows well-formed" || bad "gate table malformed (${rows} rows)" + [[ ${tbad} == 0 && ${rows} == 17 ]] && ok "gate table: 17 rows well-formed" || bad "gate table malformed (${rows} rows)" # Dirty-tree refusal, end to end, on a private clone (never this tree). # Both runs stop before any child script (no QEMU). diff --git a/scripts/qemu_ck_fpu_test.sh b/scripts/qemu_ck_fpu_test.sh new file mode 100644 index 00000000..9c87a4fd --- /dev/null +++ b/scripts/qemu_ck_fpu_test.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# Boot the AIENOS C kernel with the FP/SIMD Rust probe linked in (QEMU AArch64, +# UEFI/AAVMF, EL2 like Machine 1) and check that the kernel, built +# -mgeneral-regs-only, ran ONE floating-point/SIMD-enabled Rust unit +# (crates/aienos-fpu-probe: f32 dot product, exp approximation, NEON 4-lane +# add) and printed its results (aienos#34 lane 0). +# Last line: AIENOS_CK_FPU: PASS|FAIL|NOT_RUN (read by scripts/ck_gates.sh). +# NOT_RUN (never a silent pass) when cargo, the aarch64-unknown-none Rust +# target, qemu or AAVMF is missing, or the machine quiet flag is held. +# QEMU is not hardware: a PASS here qualifies nothing physical. +# +# FAIL when the script re-checks the serial lines itself and finds: no +# "fpu:" line, rc != 0, any of the five result words different from the known +# bit patterns (dot=68.0, NEON 11/22/33/44), exp(1) outside 1e-4 of e, a +# kernel-printed PASS the script does not reproduce, a panic/fault report, a +# missing "kernel: alive", or a non-zero QEMU exit. +# +# Usage: bash scripts/qemu_ck_fpu_test.sh the gate +# bash scripts/qemu_ck_fpu_test.sh --self-test canned logs, no QEMU/build +# Environment: AIENOS_QEMU_TIMEOUT (180 s), AIENOS_QUIET_FLAG / AIENOS_QUIET_TAG, +# AIENOS_LOG_DIR, AIENOS_QEMU_VERBOSE. +set -euo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" +verdict_name=AIENOS_CK_FPU + +# fpu_check_log FILE: PASS/FAIL line per check; sets fpu_failed=1 on failure. +fpu_check_log() { + local f="$1" line re bits dot e rc + ok() { echo "PASS $*"; } + bad() { echo "FAIL $*"; fpu_failed=1; } + re='^fpu: el1_cpacr=0x([0-9a-f]+) rc=(-?[0-9]+) dot_bits=0x([0-9a-f]+) exp1_bits=0x([0-9a-f]+) neon_bits=0x([0-9a-f]+),0x([0-9a-f]+),0x([0-9a-f]+),0x([0-9a-f]+)$' + if [[ "$(grep -cE "${re}" "${f}" || true)" != 1 ]]; then + bad "exactly one well-formed fpu: result line" + else + line="$(grep -E "${re}" "${f}")" + rc="$(sed -E "s/${re}/\\2/" <<<"${line}")" + dot="$(sed -E "s/${re}/\\3/" <<<"${line}")" + e="$(sed -E "s/${re}/\\4/" <<<"${line}")" + bits="$(sed -E "s/${re}/\\5,\\6,\\7,\\8/" <<<"${line}")" + [[ "${rc}" == 0 ]] && ok "probe returned 0" || bad "probe returned ${rc}" + [[ "${dot}" == 42880000 ]] && ok "f32 dot product = 68.0 exactly (0x42880000)" || bad "dot product bits 0x${dot}, expected 0x42880000" + [[ "${bits}" == 41300000,41b00000,42040000,42300000 ]] && ok "NEON vaddq_f32 lanes = 11,22,33,44" || bad "NEON lanes ${bits}" + # exp(1) within 1e-4 of e: e = 0x402df854; ulp near e is 2^-22, so 1e-4 is about 420 ulps. + local d=$(( 0x${e} - 0x402df854 )); (( d < 0 )) && d=$(( -d )) + if (( d <= 420 )); then ok "exp(1) bits 0x${e} within 1e-4 of e"; else bad "exp(1) bits 0x${e} too far from e"; fi + local cp="$(sed -E "s/${re}/\\1/" <<<"${line}")" + [[ $(( 0x${cp} >> 20 & 3 )) == 3 ]] && ok "CPACR_EL1.FPEN = 0b11 at EL1" || bad "CPACR_EL1.FPEN is not 0b11 (0x${cp})" + fi + grep -qx 'AIENOS_CK_FPU: PASS' "${f}" && ok "kernel reported AIENOS_CK_FPU: PASS" || bad "kernel did not report AIENOS_CK_FPU: PASS" + grep -q "kernel: alive" "${f}" && ok "kernel alive" || bad "kernel never reached kernel: alive" + if grep -qE 'report_kind: (panic|fault)' "${f}"; then bad "panic or fault report"; else ok "no panic or fault"; fi +} + +if [[ "${1:-}" == --self-test ]]; then + tmp="$(mktemp -d)"; trap 'rm -rf "${tmp}"' EXIT + good() { printf 'kernel: alive\nfpu: el1_cpacr=0x300000 rc=0 dot_bits=0x42880000 exp1_bits=0x402df84e neon_bits=0x41300000,0x41b00000,0x42040000,0x42300000\nAIENOS_CK_FPU: PASS\nreport_kind: final\n'; } + st=0 + chk() { fpu_failed=0; out="$(fpu_check_log "$3")"; grep -q '^FAIL' <<<"${out}" && fpu_failed=1 + if [[ "$2" == pass && ${fpu_failed} == 0 ]] || [[ "$2" == fail && ${fpu_failed} == 1 ]]; then echo "PASS self-test $1"; else echo "FAIL self-test $1"; st=1; fi; } + good >"${tmp}/g"; chk good pass "${tmp}/g" + sed 's/dot_bits=0x42880000/dot_bits=0x42880001/' "${tmp}/g" >"${tmp}/a"; chk wrong-dot fail "${tmp}/a" + sed 's/exp1_bits=0x402df84e/exp1_bits=0x40300000/' "${tmp}/g" >"${tmp}/b"; chk wrong-exp fail "${tmp}/b" + sed 's/0x42040000,0x42300000/0x42040000,0x42300001/' "${tmp}/g" >"${tmp}/c"; chk wrong-neon fail "${tmp}/c" + sed 's/rc=0/rc=2/' "${tmp}/g" >"${tmp}/d"; chk rc-nonzero fail "${tmp}/d" + sed 's/el1_cpacr=0x300000/el1_cpacr=0x100000/' "${tmp}/g" >"${tmp}/e"; chk fpen-not-3 fail "${tmp}/e" + grep -v '^AIENOS_CK_FPU' "${tmp}/g" >"${tmp}/f"; chk no-kernel-verdict fail "${tmp}/f" + grep -v '^fpu:' "${tmp}/g" >"${tmp}/h"; chk no-result-line fail "${tmp}/h" + { cat "${tmp}/g"; echo "report_kind: fault"; } >"${tmp}/i"; chk fault fail "${tmp}/i" + [[ ${st} == 0 ]] && { echo "AIENOS_CK_FPU_SELF_TEST: PASS"; exit 0; } + echo "AIENOS_CK_FPU_SELF_TEST: FAIL"; exit 1 +fi + +notrun() { echo "NOT_RUN $*"; echo "${verdict_name}: NOT_RUN ($*)"; exit 2; } +command -v qemu-system-aarch64 >/dev/null || notrun "qemu-system-aarch64 not installed" +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" +[[ -r "${code_fd}" && -r "${vars_fd}" ]] || notrun "AAVMF firmware not found" +command -v cargo >/dev/null || notrun "cargo not installed" +if ! rustc --print target-list 2>/dev/null | grep -qx aarch64-unknown-none \ + || ! [[ -d "$(rustc --print sysroot)/lib/rustlib/aarch64-unknown-none/lib" ]]; then + notrun "Rust target aarch64-unknown-none not installed (rustup target add aarch64-unknown-none)" +fi + +cross="" +if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" +out="${repo_root}/target/native-kernel-fpu" +lib="${repo_root}/target/fpu-probe/aarch64-unknown-none/release/libaienos_fpu_probe.a" +CARGO_TARGET_DIR="${repo_root}/target/fpu-probe" cargo build -q --release --target aarch64-unknown-none \ + --manifest-path crates/aienos-fpu-probe/Cargo.toml +make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" CK_RUST_LIBS="${lib}" >/dev/null + +quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" +quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_fpu_test $$}" +if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then + echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" + echo "${verdict_name}: NOT_RUN" + exit 3 +fi +own_flag=1 +release_flag() { + if [[ "${own_flag}" == 1 ]]; then + own_flag=0 + if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi + fi +} +work="$(mktemp -d)" +cleanup() { rm -rf "${work}"; release_flag; } +trap cleanup EXIT +mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" +touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" +cp "${out}/BOOTAA64.EFI" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" +cp "${vars_fd}" "${work}/vars.fd" +log="${work}/serial.log" + +started=$(date +%s) +set +e +timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 \ + -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 2 -m 2048 \ + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ + -drive if=pflash,format=raw,file="${work}/vars.fd" \ + -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \ + -device virtio-blk-pci,drive=esp \ + -device ramfb -display none -nic none \ + -serial file:"${log}" -no-reboot +qemu_status=$? +set -e +elapsed=$(( $(date +%s) - started )) +release_flag + +tr -d '\r' <"${log}" >"${work}/serial.txt" +[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_fpu_serial.log" +echo "qemu exit ${qemu_status} after ${elapsed} s (commit ${commit:0:12})" + +fpu_failed=0 +results="$(fpu_check_log "${work}/serial.txt")" || true +grep -q '^FAIL' <<<"${results}" && fpu_failed=1 +printf '%s\n' "${results}" +if [[ "${qemu_status}" == 0 ]]; then echo "PASS QEMU exit 0 (PSCI reset)"; else echo "FAIL QEMU exit ${qemu_status}"; fpu_failed=1; fi +grep -E '^fpu: ' "${work}/serial.txt" || true +if [[ "${fpu_failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then + echo "---- serial console ----"; cat "${work}/serial.txt" +fi +if [[ "${fpu_failed}" == 0 ]]; then echo "AIENOS_CK_FPU: PASS"; exit 0; fi +echo "AIENOS_CK_FPU: FAIL"; exit 1