diff --git a/m3/.gitignore b/m3/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/m3/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/m3/atlas_m3.s b/m3/atlas_m3.s new file mode 100644 index 0000000..d2589c3 --- /dev/null +++ b/m3/atlas_m3.s @@ -0,0 +1,155 @@ +/* + * m3/atlas_m3.s - Atlas Bootstrap Seed for Milestone 3 (PHYSICS_EFFECTS) + * Target Platform: QEMU virt AArch64 / Sovereign Machine + * Normative Base: 0x00000000 + * + * Populates Machine Boot Descriptor @ 0x401FE000, verifies physics.bin + * (18,432 bytes) at 0x40200000 with NIST SHA-256 against pinned digest, + * sets up PHYSICS_ENTRY_ABI registers, and executes handoff jump. + */ + +.ifndef DESC_MAGIC +.equ DESC_MAGIC, 0x4D424453435F3031 +.endif +.ifndef DESC_VERSION +.equ DESC_VERSION, 1 +.endif +.ifndef DESC_LENGTH +.equ DESC_LENGTH, 64 +.endif +.ifndef DESC_FLAGS +.equ DESC_FLAGS, 0 +.endif +.ifndef DESC_RAM_BASE +.equ DESC_RAM_BASE, 0x40000000 +.endif +.ifndef DESC_RAM_SIZE +.equ DESC_RAM_SIZE, 0x08000000 +.endif +.ifndef DESC_UART_BASE +.equ DESC_UART_BASE, 0x09000000 +.endif +.ifndef DESC_PHYSICS_BASE +.equ DESC_PHYSICS_BASE, 0x40200000 +.endif +.ifndef DESC_PHYSICS_SIZE +.equ DESC_PHYSICS_SIZE, 18432 +.endif +.ifndef ABI_X0 +.equ ABI_X0, 0x401FE000 +.endif +.ifndef ABI_X1 +.equ ABI_X1, 18432 +.endif +.ifndef ABI_X2 +.equ ABI_X2, 0x5048595349435330 +.endif + + .global _start + .section .text + .balign 64 + +_start: + /* 1. Mask all interrupts (DAIF) */ + msr daifset, #0xf + + /* 2. Initialize Stack Pointer in scratchpad RAM (0x401FC000) */ + ldr x0, =0x401FC000 + mov sp, x0 + + /* 3. Emit Checkpoint 1: AWAKEN */ + adr x21, msg_awaken + bl print_string + + /* 4. Populate Machine Boot Descriptor @ 0x401FE000 */ + ldr x22, =0x401FE000 + ldr x0, =DESC_MAGIC + str x0, [x22, #0] + ldr w0, =DESC_VERSION + str w0, [x22, #8] + ldr w0, =DESC_LENGTH + str w0, [x22, #12] + ldr x0, =DESC_FLAGS + str x0, [x22, #16] + ldr x0, =DESC_RAM_BASE + str x0, [x22, #24] + ldr x0, =DESC_RAM_SIZE + str x0, [x22, #32] + ldr x0, =DESC_UART_BASE + str x0, [x22, #40] + ldr x0, =DESC_PHYSICS_BASE + str x0, [x22, #48] + ldr x0, =DESC_PHYSICS_SIZE + str x0, [x22, #56] + + /* 5. Emit Checkpoint 2: VERIFY */ + adr x21, msg_verify + bl print_string + + /* 6. Verify PHYSICS Integrity using SHA-256 */ + ldr x0, =0x40200000 /* Payload base */ + ldr x1, =18432 /* Payload size */ + ldr x2, =0x401FE100 /* 128-byte scratchpad */ + ldr x3, =0x401FE080 /* 32-byte digest output buffer */ + bl sha256_compute + + /* 7. Compare computed digest with pinned digest */ + ldr x0, =0x401FE080 + adr x1, pinned_physics_sha256 + mov x2, #4 /* 4 pairs of 64-bit words */ +.Lcmp_digest: + cbz x2, .Lverify_pass + ldr x3, [x0], #8 + ldr x4, [x1], #8 + cmp x3, x4 + b.ne .Lverify_fail + sub x2, x2, #1 + b .Lcmp_digest + +.Lverify_pass: + /* 8. Emit Checkpoint 3: HANDOFF */ + adr x21, msg_handoff + bl print_string + + /* 9. Set up PHYSICS_ENTRY_ABI registers */ + ldr x0, =ABI_X0 + ldr x1, =ABI_X1 + ldr x2, =ABI_X2 + ldr x19, =0x40200000 + br x19 /* Transfer physical authority to Physics */ + +.Lverify_fail: + adr x21, msg_panic_verify + bl print_string +.Lpanic_halt: + wfe + b .Lpanic_halt + +/* Diagnostic UART */ +print_string: + ldr x20, =0x09000000 +.Lput_loop: + ldrb w0, [x21], #1 + cbz w0, .Lput_done +1: ldrb w1, [x20, #24] + tst w1, #32 + b.ne 1b + strb w0, [x20] + b .Lput_loop +.Lput_done: + ret + + .section .rodata + .balign 8 +msg_awaken: + .asciz "ATLAS: AWAKEN\n" +msg_verify: + .asciz "ATLAS: VERIFY\n" +msg_handoff: + .asciz "ATLAS: HANDOFF\n" +msg_panic_verify: + .asciz "PANIC: VERIFICATION_FAILED\n" + + .balign 8 +pinned_physics_sha256: + .include "m3_physics_pin.inc" diff --git a/m3/build.sh b/m3/build.sh new file mode 100755 index 0000000..6c8a4d4 --- /dev/null +++ b/m3/build.sh @@ -0,0 +1,117 @@ +#!/bin/sh +# m3/build.sh -- Python-free build system for PHYSICS M3 image and qualification tools. +# +# Outputs under m3/build/ (gitignored). Never touches repo-root M2 files. +# sha256_clean.c is compiled from repo root (audited source, not forked). +# Deterministic: no timestamps, --build-id=none, prefix mapped to "." +set -eu + +M3=$(cd "$(dirname "$0")" && pwd -P) +ROOT=$(cd "$M3/.." && pwd -P) +OUT=$M3/build + +CROSS=${CROSS:-aarch64-linux-gnu-} +CC=${CC:-${CROSS}gcc} +AS=${AS:-${CROSS}as} +LD=${LD:-${CROSS}ld} +OBJCOPY=${OBJCOPY:-${CROSS}objcopy} +NM=${NM:-${CROSS}nm} +SIZE=${SIZE:-${CROSS}size} +HOSTCC=${HOSTCC:-gcc} +QEMU=${QEMU:-qemu-system-aarch64} + +PREFIX_MAP="-ffile-prefix-map=$ROOT=." +CFLAGS_TARGET="-O2 -mstrict-align -mgeneral-regs-only -ffreestanding -nostdlib \ +-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-stack-protector $PREFIX_MAP" +ASFLAGS="" +LDFLAGS="-nostdlib --build-id=none -z noexecstack" +HOSTCFLAGS="-O2 -std=c11 -Wall -Wextra -Werror $PREFIX_MAP" + +M3_KERNEL_SRCS="sha256_be.s capability.s receipt_ledger.s replay_cache.s memory_alloc.s vector_table.s effect_broker.s physics.s" +M3_TEST_SRCS="tests/test_m3_authority.s tests/sha256_kat.s tests/kat_stub.s" + +mkdir -p "$OUT/obj" +cd "$ROOT" + +toolver() { "$@" 2>&1 | head -n 1; } +{ + echo "cc: $(toolver "$CC" --version)" + echo "as: $(toolver "$AS" --version)" + echo "ld: $(toolver "$LD" --version)" + echo "objcopy: $(toolver "$OBJCOPY" --version)" + echo "hostcc: $(toolver "$HOSTCC" --version)" + echo "qemu: $(toolver "$QEMU" --version)" +} > "$OUT/toolchain.txt" + +# 1. Audited SHA-256, compiled from repo root +"$CC" $CFLAGS_TARGET -c sha256_clean.c -o "$OUT/obj/sha256_clean.o" +undef=$("$NM" -u "$OUT/obj/sha256_clean.o") +if [ -n "$undef" ]; then + echo "build.sh: sha256_clean.o has undefined symbols:" >&2 + echo "$undef" >&2; exit 1 +fi + +# 2. Build host tool m3tool +"$HOSTCC" $HOSTCFLAGS -o "$OUT/m3tool" m3/tools/m3tool.c sha256_clean.c + +# 3. Assemble all M3 kernel and test sources +for s in $M3_KERNEL_SRCS $M3_TEST_SRCS; do + o=$OUT/obj/$(basename "$s" .s).o + "$AS" $ASFLAGS "m3/$s" -o "$o" +done +"$AS" $ASFLAGS --defsym KAT_FAULT_PROBE=1 m3/tests/sha256_kat.s -o "$OUT/obj/sha256_kat_fault.o" + +# 4. Link KAT tests (standalone SHA-256 verification) +link_kat() { + "$LD" $LDFLAGS -T m3/tests/kat.ld -o "$OUT/$2.elf" \ + "$1" "$OUT/obj/sha256_be.o" "$OUT/obj/sha256_clean.o" + "$OBJCOPY" -O binary "$OUT/$2.elf" "$OUT/$2.bin" +} +link_kat "$OUT/obj/sha256_kat.o" kat +link_kat "$OUT/obj/sha256_kat_fault.o" kat_fault +"$LD" $LDFLAGS -Ttext=0 -e _start -o "$OUT/kat_stub.elf" "$OUT/obj/kat_stub.o" +"$OBJCOPY" -O binary "$OUT/kat_stub.elf" "$OUT/kat_stub.bin" + +# 5. Link Canonical M3 Physics Kernel: physics.elf and physics.bin +KERNEL_OBJS="$OUT/obj/physics.o \ +$OUT/obj/vector_table.o \ +$OUT/obj/capability.o \ +$OUT/obj/receipt_ledger.o \ +$OUT/obj/replay_cache.o \ +$OUT/obj/memory_alloc.o \ +$OUT/obj/effect_broker.o \ +$OUT/obj/test_m3_authority.o \ +$OUT/obj/sha256_be.o \ +$OUT/obj/sha256_clean.o" + +"$LD" $LDFLAGS -T m3/physics.ld $KERNEL_OBJS -o "$OUT/physics.elf" +"$OBJCOPY" -O binary "$OUT/physics.elf" "$OUT/physics.bin" + +# Check binary size +PHYS_SIZE=$(wc -c < "$OUT/physics.bin") +if [ "$PHYS_SIZE" -ne 18432 ]; then + echo "build.sh: physics.bin size is $PHYS_SIZE (expected 18432 bytes)" >&2 + exit 1 +fi + +# 6. Generate pinned digest include for Atlas +"$OUT/m3tool" pin-inc "$OUT/physics.bin" "$OUT/m3_physics_pin.inc" + +# 7. Assemble and link Atlas M3 bootstrap seed: atlas_m3.bin +"$AS" $ASFLAGS -I"$OUT" m3/atlas_m3.s -o "$OUT/obj/atlas_m3.o" +"$LD" $LDFLAGS -Ttext=0x00000000 "$OUT/obj/atlas_m3.o" "$OUT/obj/sha256_clean.o" -o "$OUT/atlas_m3.elf" +"$OBJCOPY" -O binary "$OUT/atlas_m3.elf" "$OUT/atlas_m3.bin" + +# 8. Produce size and accounting report +{ + echo "=== M3 OBJECT SIZES ===" + ( cd "$OUT/obj" && "$SIZE" -A *.o ) + echo "" + echo "=== M3 ELF IMAGES ===" + ( cd "$OUT" && "$SIZE" physics.elf atlas_m3.elf kat.elf ) + echo "" + echo "=== M3 BINARY SIZES ===" + ls -l "$OUT/physics.bin" "$OUT/atlas_m3.bin" "$OUT/kat.bin" +} > "$OUT/sizes.txt" + +echo "build.sh: OK -> $OUT (physics.bin: $PHYS_SIZE bytes, atlas_m3.bin: $(wc -c < "$OUT/atlas_m3.bin") bytes)" diff --git a/m3/capability.s b/m3/capability.s new file mode 100644 index 0000000..d515e8d --- /dev/null +++ b/m3/capability.s @@ -0,0 +1,533 @@ +/* + * m3/capability.s - Capability Ledger, Genesis Root, Attenuation & Revocation + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Fixed-capacity (32 records) kernel-private capability ledger. + * No heap, no dynamic memory allocation. Checked arithmetic. + * Storage: PHYSICS_CAPABILITY_TABLE @ 0x40209000 (4 KiB) + */ + + .global init_capability_table + .global lookup_capability + .global derive_capability + .global revoke_capability + + .extern sha256_be + .extern print_string + + .section .text, "ax" + .balign 4 + + .equ CAP_TABLE_BASE, 0x40209000 + .equ CAP_TABLE_CAPACITY, 32 + .equ CAP_RECORD_SIZE, 128 + .equ CAP_RECORD_SHIFT, 7 + + .equ RES_UNIVERSAL_ROOT, 1 + .equ RES_MEMORY_FRAME, 2 + .equ RES_CONSOLE, 3 + .equ RES_MEASUREMENT, 4 + + .equ OP_ALL_AUTHORITY, 0xFFFFFFFF + .equ OP_FRAME_GRANT, 1 + .equ OP_FRAME_RELEASE, 2 + .equ OP_CONSOLE_WRITE, 1 + .equ OP_MEASUREMENT_READ, 1 + + .equ STATE_ACTIVE, 1 + .equ STATE_REVOKED, 2 + + .equ DEC_ADMITTED, 1 + .equ DEC_REJECTED_STRUCTURAL, 2 + .equ DEC_REJECTED_UNKNOWN_CAP, 3 + .equ DEC_REJECTED_STALE_GENERATION, 4 + .equ DEC_REJECTED_REVOKED, 5 + .equ DEC_REJECTED_WRONG_PRINCIPAL, 6 + .equ DEC_REJECTED_OPERATION, 7 + .equ DEC_REJECTED_RESOURCE, 8 + .equ DEC_REJECTED_BOUNDS, 9 + .equ DEC_REJECTED_CONSTRAINT, 10 + .equ DEC_REJECTED_REPLAY_CONFLICT, 11 + .equ DEC_REJECTED_EXHAUSTED, 12 + .equ DEC_EXECUTION_FAILED, 13 + +/* + * init_capability_table(): + * Zeros the 4 KiB capability table, then synthesizes CAP_ROOT at slot 0: + * +0x00: slot = 0, generation = 1 + * +0x08: principal_id = 1 (Physics) + * +0x10: resource_type = 1 (RES_UNIVERSAL_ROOT), allowed_ops = 0xFFFFFFFF + * +0x18: bound_base = 0x40210000 (FREE_FRAME_BASE) + * +0x20: bound_size = 0x07DF0000 (DRAM_END - FREE_FRAME_BASE = 32,240 frames) + * +0x28: revocation_state = 1 (ACTIVE), attenuation_depth = 0 + * +0x30: provenance_digest (32 bytes = atlas.sha256) + * +0x50: parent_slot = 0xFFFFFFFF, parent_generation = 0xFFFFFFFF + * +0x58: lifetime = 0 (unbounded) + * +0x60: reserved (32 zero bytes) + */ +init_capability_table: + stp x29, x30, [sp, #-16]! + mov x29, sp + + /* Zero all 4096 bytes of the capability table */ + ldr x0, =CAP_TABLE_BASE + mov x1, #4096 +.Lzero_cap_loop: + cbz x1, .Lzero_cap_done + stp xzr, xzr, [x0], #16 + sub x1, x1, #16 + b .Lzero_cap_loop + +.Lzero_cap_done: + ldr x0, =CAP_TABLE_BASE /* Target: Slot 0 */ + + /* +0x00: slot = 0, generation = 1 */ + mov w1, #0 + str w1, [x0, #0] + mov w1, #1 + str w1, [x0, #4] + + /* +0x08: principal_id = 1 (Physics) */ + mov x1, #1 + str x1, [x0, #8] + + /* +0x10: resource_type = 1 (RES_UNIVERSAL_ROOT), allowed_ops = 0xFFFFFFFF */ + mov w1, #RES_UNIVERSAL_ROOT + str w1, [x0, #16] + mov w1, #-1 + str w1, [x0, #20] + + /* +0x18: bound_base = 0x40210000 (FREE_FRAME_BASE) */ + ldr x1, =0x40210000 + str x1, [x0, #24] + + /* +0x20: bound_size = 0x07DF0000 */ + ldr x1, =0x07DF0000 + str x1, [x0, #32] + + /* +0x28: revocation_state = 1 (ACTIVE), attenuation_depth = 0 */ + mov w1, #STATE_ACTIVE + str w1, [x0, #40] + mov w1, #0 + str w1, [x0, #44] + + /* +0x30: Pinned provenance digest from atlas.sha256 (32 bytes) */ + adr x1, pinned_atlas_sha256 + ldp x2, x3, [x1, #0] + ldp x4, x5, [x1, #16] + stp x2, x3, [x0, #48] + stp x4, x5, [x0, #64] + + /* +0x50: parent_slot = -1, parent_generation = -1 */ + mov w1, #-1 + str w1, [x0, #0x50] + str w1, [x0, #0x54] + + /* +0x58: lifetime = 0 (unbounded) */ + str xzr, [x0, #0x58] + + ldp x29, x30, [sp], #16 + ret + +/* + * lookup_capability(slot, generation, principal_id) + * w0: slot, w1: generation, x2: principal_id + * Returns: + * x0: Pointer to CapabilityRecord (or 0 if invalid/denied) + * w1: Decision / rejection code (DEC_ADMITTED or DEC_REJECTED_*) + */ +lookup_capability: + stp x29, x30, [sp, #-32]! + mov x29, sp + stp x19, x20, [sp, #16] + + /* 1. Slot range check */ + cmp w0, #CAP_TABLE_CAPACITY + b.hs .Llookup_unknown + + /* 2. Compute record address: CAP_TABLE_BASE + (slot << 7) */ + ldr x19, =CAP_TABLE_BASE + ubfiz x3, x0, #CAP_RECORD_SHIFT, #5 + add x19, x19, x3 /* x19 = record ptr */ + + /* 3. Check record allocated (generation > 0) */ + ldr w4, [x19, #4] /* record.generation */ + cbz w4, .Llookup_unknown + + /* 4. Generation check: presented == record */ + cmp w1, w4 + b.ne .Llookup_stale + + /* 5. Immediate revocation check */ + ldr w5, [x19, #40] /* record.revocation_state */ + cmp w5, #STATE_ACTIVE + b.ne .Llookup_revoked + + /* 6. Principal check */ + ldr x6, [x19, #8] /* record.principal_id */ + cmp x2, x6 + b.ne .Llookup_wrong_principal + + /* 7. Ancestor-chain validation on use */ + ldr w7, [x19, #0] /* slot */ + cbz w7, .Llookup_success /* CAP_ROOT has no parent, admitted */ + + mov x20, x19 /* x20 = current node in traversal */ + mov w8, #8 /* loop guard (max depth 8) */ + +.Lancestor_loop: + cbz w8, .Llookup_revoked /* depth exceeded fail-closed */ + sub w8, w8, #1 + + ldr w9, [x20, #0x50] /* parent_slot */ + ldr w10, [x20, #0x54] /* parent_generation */ + + /* If parent_slot is CAP_ROOT (0), check slot 0 */ + cmp w9, #CAP_TABLE_CAPACITY + b.hs .Llookup_revoked + + ldr x11, =CAP_TABLE_BASE + ubfiz x12, x9, #CAP_RECORD_SHIFT, #5 + add x11, x11, x12 /* x11 = parent record ptr */ + + /* Parent generation check */ + ldr w13, [x11, #4] + cmp w10, w13 + b.ne .Llookup_stale + + /* Parent revocation state check */ + ldr w14, [x11, #40] + cmp w14, #STATE_ACTIVE + b.ne .Llookup_revoked + + /* If parent is slot 0 (CAP_ROOT), ancestor chain fully verified! */ + cbz w9, .Llookup_success + + /* Advance to parent */ + mov x20, x11 + b .Lancestor_loop + +.Llookup_success: + mov x0, x19 + mov w1, #DEC_ADMITTED + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 + ret + +.Llookup_unknown: + mov x0, #0 + mov w1, #DEC_REJECTED_UNKNOWN_CAP + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 + ret + +.Llookup_stale: + mov x0, #0 + mov w1, #DEC_REJECTED_STALE_GENERATION + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 + ret + +.Llookup_revoked: + mov x0, #0 + mov w1, #DEC_REJECTED_REVOKED + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 + ret + +.Llookup_wrong_principal: + mov x0, #0 + mov w1, #DEC_REJECTED_WRONG_PRINCIPAL + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #32 + ret + +/* + * derive_capability( + * w0: parent_slot, + * w1: parent_generation, + * x2: caller_principal, + * x3: child_principal, + * w4: requested_resource_type, + * w5: requested_ops, + * x6: requested_base, + * x7: requested_size, + * x8: requested_lifetime [passed in x8] + * ) + * Returns: + * x0: (new_generation << 32) | child_slot, or 0 on error. + */ +derive_capability: + stp x29, x30, [sp, #-192]! + mov x29, sp + stp x19, x20, [sp, #16] + stp x21, x22, [sp, #32] + stp x23, x24, [sp, #48] + stp x25, x26, [sp, #64] + stp x27, x28, [sp, #80] + + /* Save arguments into callee-saved registers */ + mov x22, x3 /* child_principal */ + mov w23, w4 /* rtype */ + mov w24, w5 /* ops */ + mov x25, x6 /* base */ + mov x26, x7 /* size */ + mov x27, x8 /* lifetime */ + + /* 1. Lookup and validate parent (w0, w1, x2 already set) */ + bl lookup_capability + cbz x0, .Lderive_fail + mov x19, x0 /* x19 = validated parent record ptr */ + + /* 2. Child principal cannot be 0 */ + cbz x22, .Lderive_fail + + /* 3. Resource type containment */ + ldr w4, [x19, #16] /* parent.resource_type */ + cmp w4, #RES_UNIVERSAL_ROOT + b.eq .Lcheck_root_narrowing + /* Specific resource: child must match exactly */ + cmp w23, w4 + b.ne .Lderive_fail + b .Lcheck_ops + +.Lcheck_root_narrowing: + /* Narrowing from root: child can be MEMORY_FRAME, CONSOLE, or MEASUREMENT */ + cmp w23, #RES_MEMORY_FRAME + b.eq .Lcheck_ops + cmp w23, #RES_CONSOLE + b.eq .Lcheck_ops + cmp w23, #RES_MEASUREMENT + b.eq .Lcheck_ops + b .Lderive_fail /* Cannot derive ROOT or unknown type */ + +.Lcheck_ops: + /* 4. Rights narrowing: (child_ops & ~parent_ops) == 0 */ + cbz w24, .Lderive_fail /* Cannot have 0 ops */ + ldr w5, [x19, #20] /* parent.allowed_ops */ + mvn w6, w5 + tst w24, w6 + b.ne .Lderive_fail /* Child has forbidden rights */ + + /* 5. Bounds containment with checked arithmetic */ + ldr w4, [x19, #16] /* parent.resource_type */ + cmp w4, #RES_UNIVERSAL_ROOT + b.ne .Lcheck_standard_bounds + + /* Parent is CAP_ROOT: check bounds according to requested concrete resource */ + cmp w23, #RES_CONSOLE + b.eq .Lcheck_root_console_bounds + cmp w23, #RES_MEASUREMENT + b.eq .Lcheck_root_measurement_bounds + + /* Otherwise MEMORY_FRAME: fall through to check standard bounds within DRAM */ + +.Lcheck_standard_bounds: + ldr x5, [x19, #24] /* parent.bound_base */ + ldr x6, [x19, #32] /* parent.bound_size */ + + /* Check child base >= parent base */ + cmp x25, x5 + b.lo .Lderive_fail + + /* Check child base + size overflow */ + adds x7, x25, x26 /* x7 = child end */ + b.cs .Lderive_fail + + /* Check parent base + size overflow */ + adds x8, x5, x6 /* x8 = parent end */ + b.cs .Lderive_fail + + /* Check child end <= parent end */ + cmp x7, x8 + b.hi .Lderive_fail + + /* For memory frame: size must be > 0 and 4 KiB aligned */ + cmp w23, #RES_MEMORY_FRAME + b.ne .Lcheck_lifetime + cbz x26, .Lderive_fail + tst x25, #0xFFF + b.ne .Lderive_fail + tst x26, #0xFFF + b.ne .Lderive_fail + b .Lcheck_lifetime + +.Lcheck_root_console_bounds: + ldr x5, =0x09000000 /* PL011 UART MMIO base */ + cmp x25, x5 + b.ne .Lderive_fail + cmp x26, #0x1000 + b.hi .Lderive_fail + cbz x26, .Lderive_fail + b .Lcheck_lifetime + +.Lcheck_root_measurement_bounds: + cbnz x25, .Lderive_fail + cbnz x26, .Lderive_fail + b .Lcheck_lifetime + +.Lcheck_lifetime: + /* 6. Lifetime narrowing: if parent.lifetime != 0, child <= parent and child != 0 */ + ldr x9, [x19, #0x58] /* parent.lifetime */ + cbz x9, .Lcheck_depth + cbz x27, .Lderive_fail /* Cannot widen to unbounded */ + cmp x27, x9 + b.hi .Lderive_fail + +.Lcheck_depth: + /* 7. Attenuation depth: parent.depth + 1 <= 8 */ + ldr w10, [x19, #44] /* parent.attenuation_depth */ + cmp w10, #8 + b.ge .Lderive_fail + add w20, w10, #1 /* w20 = child depth */ + + /* 8. Find a free slot in [1..31] */ + ldr x11, =CAP_TABLE_BASE + mov w21, #1 /* slot index 1..31 in callee-saved w21 */ +.Lsearch_slot: + cmp w21, #CAP_TABLE_CAPACITY + b.ge .Lderive_fail /* Table full */ + + ubfiz x13, x21, #CAP_RECORD_SHIFT, #5 + add x28, x11, x13 /* x28 = candidate slot ptr (callee-saved) */ + + ldr w15, [x28, #40] /* slot.revocation_state */ + cmp w15, #STATE_ACTIVE + b.ne .Lslot_found + + add w21, w21, #1 + b .Lsearch_slot + +.Lslot_found: + /* Generation bumping */ + ldr w16, [x28, #4] /* old generation */ + add w16, w16, #1 + cbnz w16, 1f + /* Wrapped to 0? Generation wrap fails closed */ + add w21, w21, #1 + b .Lsearch_slot +1: + str w16, [sp, #184] /* Save new_generation on stack */ + + /* 9. Compute Provenance Digest: SHA-256 of parent_prov (32B) + child fields (48B) = 80B */ + /* Construct hash buffer on stack at [sp + 96] */ + add x0, sp, #96 + /* Copy 32 bytes parent provenance */ + add x1, x19, #48 /* parent.provenance */ + ldp x2, x3, [x1, #0] + ldp x4, x5, [x1, #16] + stp x2, x3, [x0, #0] + stp x4, x5, [x0, #16] + + /* Copy child fields: child_principal (8B), rtype (4B), ops (4B), base (8B), size (8B), depth (4B), slot (4B), lifetime (8B) */ + str x22, [x0, #32] /* child_principal */ + str w23, [x0, #40] /* rtype */ + str w24, [x0, #44] /* ops */ + str x25, [x0, #48] /* base */ + str x26, [x0, #56] /* size */ + str w20, [x0, #64] /* child_depth */ + str w21, [x0, #68] /* child_slot (binds identity) */ + str x27, [x0, #72] /* lifetime */ + + /* Call sha256_be(data, len=80, out) */ + /* Target child provenance location: x28 + 48 */ + mov x1, #80 + add x2, x28, #48 /* child.provenance */ + bl sha256_be + cbnz w0, .Lderive_fail + + /* 10. Write child record fields into slot (x28 is preserved!) */ + ldr w16, [sp, #184] /* reload new generation */ + str w21, [x28, #0] /* slot */ + str w16, [x28, #4] /* generation */ + str x22, [x28, #8] /* principal_id */ + str w23, [x28, #16] /* resource_type */ + str w24, [x28, #20] /* allowed_ops */ + str x25, [x28, #24] /* bound_base */ + str x26, [x28, #32] /* bound_size */ + mov w1, #STATE_ACTIVE + str w1, [x28, #40] /* revocation_state */ + str w20, [x28, #44] /* attenuation_depth */ + ldr w2, [x19, #0] /* parent.slot */ + str w2, [x28, #0x50] /* parent_slot */ + ldr w3, [x19, #4] /* parent.generation */ + str w3, [x28, #0x54] /* parent_generation */ + str x27, [x28, #0x58] /* lifetime */ + + /* Zero reserved bytes at +0x60..+0x7F */ + stp xzr, xzr, [x28, #0x60] + stp xzr, xzr, [x28, #0x70] + + /* Return (generation << 32) | slot */ + ubfiz x0, x16, #32, #32 + orr x0, x0, x21 + + ldp x19, x20, [sp, #16] + ldp x21, x22, [sp, #32] + ldp x23, x24, [sp, #48] + ldp x25, x26, [sp, #64] + ldp x27, x28, [sp, #80] + ldp x29, x30, [sp], #192 + ret + +.Lderive_fail: + mov x0, #0 + ldp x19, x20, [sp, #16] + ldp x21, x22, [sp, #32] + ldp x23, x24, [sp, #48] + ldp x25, x26, [sp, #64] + ldp x27, x28, [sp, #80] + ldp x29, x30, [sp], #192 + ret + +/* + * revoke_capability(slot, generation, caller_principal) + * w0: slot, w1: generation, x2: caller_principal + * Returns: + * x0: 1 on success, 0 on failure. + */ +revoke_capability: + stp x29, x30, [sp, #-16]! + mov x29, sp + + /* Cannot revoke CAP_ROOT (slot 0) */ + cbz w0, .Lrevoke_fail + cmp w0, #CAP_TABLE_CAPACITY + b.ge .Lrevoke_fail + + ldr x3, =CAP_TABLE_BASE + ubfiz x4, x0, #CAP_RECORD_SHIFT, #5 + add x3, x3, x4 /* record ptr */ + + /* Check generation matches */ + ldr w5, [x3, #4] + cmp w1, w5 + b.ne .Lrevoke_fail + + /* Check caller is owner OR Physics (principal 1) */ + ldr x6, [x3, #8] + cmp x2, x6 + b.eq 1f + cmp x2, #1 + b.ne .Lrevoke_fail +1: + /* Mark REVOKED */ + mov w7, #STATE_REVOKED + str w7, [x3, #40] + + mov x0, #1 + ldp x29, x30, [sp], #16 + ret + +.Lrevoke_fail: + mov x0, #0 + ldp x29, x30, [sp], #16 + ret + + .section .rodata + .balign 8 +pinned_atlas_sha256: + .byte 0xf7, 0x80, 0x25, 0x01, 0xb4, 0x10, 0xa0, 0xc1 + .byte 0x9e, 0xff, 0x7b, 0x8f, 0xca, 0x88, 0x65, 0xc9 + .byte 0xba, 0x9c, 0x96, 0xbf, 0xa4, 0xf8, 0x06, 0x5c + .byte 0xa8, 0xf5, 0x08, 0xb6, 0x77, 0x48, 0xb9, 0xa5 diff --git a/m3/effect_broker.s b/m3/effect_broker.s new file mode 100644 index 0000000..d540d04 --- /dev/null +++ b/m3/effect_broker.s @@ -0,0 +1,411 @@ +/* + * m3/effect_broker.s - Deterministic Effect Broker & Admission Pipeline + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Single admission path: submit_effect(intent) -> EffectReceipt. + * 10-stage fail-closed validation pipeline. + * Physical execution ONLY occurs after all admission stages pass. + * Every intent attempt produces an immutable cryptographic receipt. + */ + + .global submit_effect + + .extern sha256_be + .extern lookup_capability + .extern lookup_replay + .extern record_replay + .extern is_receipt_ledger_full + .extern commit_receipt + .extern get_receipt_count + .extern allocate_frame_in_range + .extern release_frame + + .section .text, "ax" + .balign 4 + + .equ UART_MMIO_BASE, 0x09000000 + .equ RECEIPT_ENTRIES_BASE, 0x4020A080 + .equ RECEIPT_RECORD_SIZE, 192 + + .equ RES_UNIVERSAL_ROOT, 1 + .equ RES_MEMORY_FRAME, 2 + .equ RES_CONSOLE, 3 + .equ RES_MEASUREMENT, 4 + + .equ OP_FRAME_GRANT, 1 + .equ OP_FRAME_RELEASE, 2 + .equ OP_CONSOLE_WRITE, 1 + .equ OP_MEASUREMENT_READ, 1 + + .equ DEC_ADMITTED, 1 + .equ DEC_REJECTED_STRUCTURAL, 2 + .equ DEC_REJECTED_UNKNOWN_CAP, 3 + .equ DEC_REJECTED_STALE_GENERATION, 4 + .equ DEC_REJECTED_REVOKED, 5 + .equ DEC_REJECTED_WRONG_PRINCIPAL, 6 + .equ DEC_REJECTED_OPERATION, 7 + .equ DEC_REJECTED_RESOURCE, 8 + .equ DEC_REJECTED_BOUNDS, 9 + .equ DEC_REJECTED_CONSTRAINT, 10 + .equ DEC_REJECTED_REPLAY_CONFLICT, 11 + .equ DEC_REJECTED_EXHAUSTED, 12 + .equ DEC_EXECUTION_FAILED, 13 + + .equ REPLAY_MISS, 0 + .equ REPLAY_HIT, 1 + .equ REPLAY_CONFLICT, 2 + +/* + * submit_effect(intent_ptr) -> x0: committed EffectReceipt pointer (or 0) + * x0: intent_ptr + * + * Stack Frame Layout (192 bytes): + * [sp+0] x29, x30 + * [sp+16] x19, x20 (intent_ptr, cap_ptr) + * [sp+32] x21, x22 (decision, rejection_reason) + * [sp+48] x23, x24 (actual_effect, output) + * [sp+64] ReceiptData (96 bytes: 0x00..0x5F) + * [sp+64]: version(u16), length(u16), decision(u32) + * [sp+72]: rejection_reason(u32), reserved0(u32) + * [sp+80]: request_id(u64) + * [sp+88]: intent_digest(32 bytes) + * [sp+120]: actual_effect(u64) + * [sp+128]: output(u64) + * [sp+136]: cap_slot(u32), cap_gen(u32) + * [sp+144]: machine_gen(u64) + * [sp+152]: measurement(u64) + * [sp+160] 32 bytes scratch + */ + .equ FRAME_SIZE, 192 + .equ OFF_RECEIPT, 64 + +submit_effect: + stp x29, x30, [sp, #-FRAME_SIZE]! + mov x29, sp + stp x19, x20, [sp, #16] + stp x21, x22, [sp, #32] + stp x23, x24, [sp, #48] + + mov x19, x0 /* x19 = intent_ptr */ + mov x20, #0 /* x20 = cap_ptr (null initially) */ + mov w21, #DEC_ADMITTED /* w21 = decision */ + mov w22, #0 /* w22 = rejection_reason */ + mov x23, #0 /* x23 = actual_effect */ + mov x24, #0 /* x24 = output */ + + /* Zero ReceiptData scratchpad (96 bytes at sp+64) */ + add x0, sp, #OFF_RECEIPT + mov x1, #96 +.Lzero_rcpt_scratch: + cbz x1, .Lzero_rcpt_scratch_done + stp xzr, xzr, [x0], #16 + sub x1, x1, #16 + b .Lzero_rcpt_scratch +.Lzero_rcpt_scratch_done: + + /* ------------------------------------------------------------- + * Stage 1: Structural Validation + * ------------------------------------------------------------- */ + /* Check alignment of intent_ptr (must be 8-byte aligned) */ + tst x19, #7 + b.ne .Lfail_structural + + /* Check version == 1, length == 64, reserved0 == 0 */ + ldrh w1, [x19, #0] /* version */ + cmp w1, #1 + b.ne .Lfail_structural + + ldrh w2, [x19, #2] /* length */ + cmp w2, #64 + b.ne .Lfail_structural + + ldr w3, [x19, #4] /* reserved0 */ + cbnz w3, .Lfail_structural + b .Lstage2 + +.Lfail_structural: + mov w21, #DEC_REJECTED_STRUCTURAL + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 2: Principal Validation + * ------------------------------------------------------------- */ +.Lstage2: + ldr x7, [x19, #0x10] /* principal_id */ + cbz x7, .Lfail_principal + b .Lstage3 + +.Lfail_principal: + mov w21, #DEC_REJECTED_WRONG_PRINCIPAL + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 3: Digest Intent & Check Replay Cache + * ------------------------------------------------------------- */ +.Lstage3: + /* Compute SHA-256 of the 64-byte EffectIntent into ReceiptData.intent_digest (+88) */ + mov x0, x19 /* data */ + mov x1, #64 /* len */ + add x2, sp, #(OFF_RECEIPT + 24) /* out = sp + 88 */ + bl sha256_be + cbnz w0, .Lfail_structural + + /* Check Replay Cache: lookup_replay(request_id, intent_digest) */ + ldr x0, [x19, #0x08] /* request_id */ + add x1, sp, #(OFF_RECEIPT + 24) /* intent_digest */ + bl lookup_replay + /* w0: REPLAY_MISS (0), REPLAY_HIT (1), REPLAY_CONFLICT (2) */ + cmp w0, #REPLAY_CONFLICT + b.eq .Lfail_replay_conflict + cmp w0, #REPLAY_HIT + b.eq .Lhandle_replay_hit + b .Lstage4 + +.Lfail_replay_conflict: + mov w21, #DEC_REJECTED_REPLAY_CONFLICT + b .Lcommit_rejection + +.Lhandle_replay_hit: + /* Idempotent replay: return existing committed receipt without re-executing */ + ldr x2, =RECEIPT_ENTRIES_BASE + mov x3, #RECEIPT_RECORD_SIZE + madd x0, x1, x3, x2 /* x0 = existing receipt ptr */ + b .Lexit + + /* ------------------------------------------------------------- + * Stage 4: Check Ledger Space (Fail-Closed Exhaustion) + * ------------------------------------------------------------- */ +.Lstage4: + bl is_receipt_ledger_full + cbnz w0, .Lfail_exhausted + b .Lstage5 + +.Lfail_exhausted: + mov w21, #DEC_REJECTED_EXHAUSTED + /* On exhaustion, halt admission fail-closed without modifying memory */ + mov x0, #0 + b .Lexit + + /* ------------------------------------------------------------- + * Stage 5: Capability Lookup & Verification + * ------------------------------------------------------------- */ +.Lstage5: + ldr w0, [x19, #0x18] /* capability_slot */ + ldr w1, [x19, #0x1C] /* capability_generation */ + ldr x2, [x19, #0x10] /* principal_id */ + bl lookup_capability + cbz x0, .Lfail_cap_lookup + mov x20, x0 /* x20 = validated CapabilityRecord ptr */ + b .Lstage6 + +.Lfail_cap_lookup: + /* w1 carries exact rejection code from lookup_capability */ + mov w21, w1 + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 6: Resource Type Check + * ------------------------------------------------------------- */ +.Lstage6: + ldr w3, [x19, #0x20] /* intent.resource_type */ + ldr w4, [x20, #0x10] /* cap.resource_type */ + cmp w3, w4 + b.ne .Lfail_resource + b .Lstage7 + +.Lfail_resource: + mov w21, #DEC_REJECTED_RESOURCE + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 7: Operation Rights Check + * ------------------------------------------------------------- */ +.Lstage7: + ldr w5, [x19, #0x24] /* intent.operation */ + cbz w5, .Lfail_operation + ldr w6, [x20, #0x14] /* cap.allowed_ops */ + mvn w7, w6 + tst w5, w7 + b.ne .Lfail_operation + b .Lstage8 + +.Lfail_operation: + mov w21, #DEC_REJECTED_OPERATION + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 8: Spatial Bounds & Overflow Check + * ------------------------------------------------------------- */ +.Lstage8: + ldr x8, [x19, #0x28] /* target_base */ + ldr x9, [x19, #0x30] /* target_size */ + ldr x10, [x20, #0x18] /* cap.bound_base */ + ldr x11, [x20, #0x20] /* cap.bound_size */ + + /* target_base >= cap.bound_base */ + cmp x8, x10 + b.lo .Lfail_bounds + + /* target_base + target_size overflow */ + adds x12, x8, x9 + b.cs .Lfail_bounds + + /* cap.bound_base + cap.bound_size */ + adds x13, x10, x11 + b.cs .Lfail_bounds + + /* target_base + target_size <= cap.bound_base + cap.bound_size */ + cmp x12, x13 + b.hi .Lfail_bounds + + /* Concrete resource class rules */ + ldr w3, [x19, #0x20] /* resource_type */ + cmp w3, #RES_MEMORY_FRAME + b.eq .Lcheck_memory_bounds + cmp w3, #RES_CONSOLE + b.eq .Lcheck_console_bounds + b .Lstage9_admitted + +.Lcheck_memory_bounds: + /* Frame target must be 4 KiB aligned and size must be 4 KiB */ + cmp x9, #4096 + b.ne .Lfail_bounds + tst x8, #0xFFF + b.ne .Lfail_bounds + b .Lstage9_admitted + +.Lcheck_console_bounds: + /* Console MMIO target must be strictly UART_MMIO_BASE (0x09000000) */ + ldr x14, =UART_MMIO_BASE + cmp x8, x14 + b.ne .Lfail_bounds + b .Lstage9_admitted + +.Lfail_bounds: + mov w21, #DEC_REJECTED_BOUNDS + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 9: PHYSICAL EXECUTION (Admitted Operations Only) + * ------------------------------------------------------------- */ +.Lstage9_admitted: + mov w21, #DEC_ADMITTED + + ldr w3, [x19, #0x20] /* resource_type */ + ldr w5, [x19, #0x24] /* operation */ + ldr x8, [x19, #0x28] /* target_base */ + ldr x9, [x19, #0x30] /* target_size */ + + cmp w3, #RES_MEMORY_FRAME + b.eq .Lexec_memory + cmp w3, #RES_CONSOLE + b.eq .Lexec_console + cmp w3, #RES_MEASUREMENT + b.eq .Lexec_measurement + b .Lfail_exec + +.Lexec_memory: + cmp w5, #OP_FRAME_GRANT + b.eq .Lexec_frame_grant + cmp w5, #OP_FRAME_RELEASE + b.eq .Lexec_frame_release + b .Lfail_exec + +.Lexec_frame_grant: + mov x0, x8 /* target_base */ + mov x1, x9 /* target_size */ + bl allocate_frame_in_range + cbz x0, .Lfail_exec + mov x23, x0 /* actual_effect = granted frame address */ + mov x24, #0 /* output = 0 */ + b .Lcommit_admitted + +.Lexec_frame_release: + mov x0, x8 /* target_base */ + bl release_frame + cbz x0, .Lfail_exec + mov x23, x0 /* actual_effect = released frame address */ + mov x24, #0 + b .Lcommit_admitted + +.Lexec_console: + cmp w5, #OP_CONSOLE_WRITE + b.ne .Lfail_exec + /* Write single character from param0 (+0x38) */ + ldr x1, =UART_MMIO_BASE + ldrb w2, [x19, #0x38] /* param0 */ + strb w2, [x1] + mov x23, x1 /* actual_effect = UART base */ + mov x24, #1 /* output = 1 byte written */ + b .Lcommit_admitted + +.Lexec_measurement: + cmp w5, #OP_MEASUREMENT_READ + b.ne .Lfail_exec + /* Return receipt count as measurement */ + bl get_receipt_count + mov x23, #0 + mov x24, x0 /* output = receipt_count */ + b .Lcommit_admitted + +.Lfail_exec: + mov w21, #DEC_EXECUTION_FAILED + b .Lcommit_rejection + + /* ------------------------------------------------------------- + * Stage 10: Commit Receipt & Update Replay Cache + * ------------------------------------------------------------- */ +.Lcommit_admitted: +.Lcommit_rejection: + /* Populate ReceiptData scratchpad at [sp + OFF_RECEIPT] */ + add x0, sp, #OFF_RECEIPT + mov w1, #1 + strh w1, [x0, #0] /* version = 1 */ + mov w1, #RECEIPT_RECORD_SIZE + strh w1, [x0, #2] /* length = 192 */ + str w21, [x0, #4] /* decision */ + str w22, [x0, #8] /* rejection_reason */ + + ldr x1, [x19, #0x08] /* request_id */ + str x1, [x0, #16] + + /* intent_digest already computed at +24 */ + str x23, [x0, #0x38] /* actual_effect */ + str x24, [x0, #0x40] /* output */ + + /* Bind capability slot and generation */ + ldr w2, [x19, #0x18] + str w2, [x0, #0x48] /* capability_slot */ + ldr w3, [x19, #0x1C] + str w3, [x0, #0x4C] /* capability_generation */ + + /* Commit receipt to ledger */ + add x0, sp, #OFF_RECEIPT + bl commit_receipt + cbz x0, .Lexit /* Ledger was exhausted */ + + mov x19, x0 /* x19 = committed receipt ptr */ + + /* Read assigned receipt_slot from committed receipt offset */ + ldr x1, =RECEIPT_ENTRIES_BASE + sub x2, x19, x1 + mov x3, #RECEIPT_RECORD_SIZE + udiv x2, x2, x3 /* x2 = receipt_slot index */ + + /* Record in Replay Cache: record_replay(request_id, intent_digest, slot, decision, effect, output) */ + ldr x0, [x19, #0x10] /* request_id */ + add x1, x19, #0x18 /* intent_digest */ + mov w2, w2 /* receipt_slot */ + mov w3, w21 /* decision */ + mov x4, x23 /* actual_effect */ + mov x5, x24 /* output */ + bl record_replay + + mov x0, x19 /* Return committed receipt pointer */ + +.Lexit: + ldp x23, x24, [sp, #48] + ldp x21, x22, [sp, #32] + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #FRAME_SIZE + ret diff --git a/m3/m3_qualification_receipt.json b/m3/m3_qualification_receipt.json new file mode 100644 index 0000000..7788d4e --- /dev/null +++ b/m3/m3_qualification_receipt.json @@ -0,0 +1,74 @@ +{ + "milestone": "MILESTONE 3 — PHYSICS_EFFECTS", + "status": "QUALIFIED / QEMU PASS", + "contract_id": "CONTRACT-QEMU-VIRT-AARCH64-M3", + "generated_at": "2026-09-26T07:27:49Z", + "git_commit": "e2d6d7dc5cea31d1ff12428b533723f4e367d365", + "qemu_version": "QEMU emulator version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.18)", + "qemu_flags": "-M virt -cpu cortex-a57 -m 128M -nographic -accel tcg,thread=single", + "artifacts": { + "physics.bin": { + "sha256": "b7afe3306b52d36b8e1ad494aef026a3fb3dda041c2cd11bb3484482cd34068c", + "size_bytes": 18432, + "max_budget_bytes": 65536 + }, + "atlas_m3.bin": { + "sha256": "95b9262c6116a2127ca82cb63d6be1de072fc807a78d6f3852b9ccc38d6b10d2", + "size_bytes": 1568 + }, + "machine_contract.json": { + "sha256": "78bd266579927b2e28e2cff1c18d1e85deb140aea9bcfcedf0f08cf370bf1760" + }, + "milestone-3-spec.md": { + "sha256": "e50a43b55d0263aee32c52c3a79e3d6ca2e92cb1328de0bd023f7b9e1e881572" + }, + "capability.s": { + "sha256": "2893294289efd618ff96f80d81b5970d76989421ed5cd3443027d0ce3ca41820" + }, + "effect_broker.s": { + "sha256": "c52d9ffd2b5af62dc64db9c16add495eac80c7c09bd2fd87eb94522172a8b97f" + }, + "receipt_ledger.s": { + "sha256": "046b63479ad342b2226def8b0ef4a6272b3ae332fbe6f0e317d9ed2e94632045" + }, + "m3tool.c": { + "sha256": "0ce94003c7c3dda7e8010a6b03d20dce70f7abeb8d4a3342063e975eb9259589" + }, + "run_m3_gates.sh": { + "sha256": "d546b1413e6eea0d0ac43729b1d1ad6bd952079992dc622f483f42f50d99ff50" + } + }, + "accounting": { + "physics_elf_text_bytes": 10624, + "max_text_budget_bytes": 32768, + "text_budget_utilization_pct": 32.42, + "physics_bin_total_bytes": 18432, + "max_image_budget_bytes": 65536, + "image_budget_utilization_pct": 28.12 + }, + "qualification_gates": { + "total": 19, + "passed": 19, + "gates": { + "PHYSICS_CAP_LEDGER_PASS": "PASS", + "PHYSICS_CAP_ROOT_PRIVATE_PASS": "PASS", + "PHYSICS_CAP_ATTENUATION_PASS": "PASS", + "PHYSICS_CAP_NO_AMBIENT_AUTHORITY_PASS": "PASS", + "PHYSICS_CAP_GENERATION_PASS": "PASS", + "PHYSICS_CAP_REVOCATION_PASS": "PASS", + "PHYSICS_CAP_DESCENDANT_REVOCATION_PASS": "PASS", + "PHYSICS_EFFECT_ADMISSION_PASS": "PASS", + "PHYSICS_EFFECT_BOUNDS_PASS": "PASS", + "PHYSICS_EFFECT_FORGED_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_STALE_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_REVOKED_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_WRONG_PRINCIPAL_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_REPLAY_PASS": "PASS", + "PHYSICS_EFFECT_REPLAY_CONFLICT_PASS": "PASS", + "PHYSICS_RECEIPT_IDENTITY_PASS": "PASS", + "PHYSICS_RECEIPT_CHAIN_PASS": "PASS", + "PHYSICS_RECEIPT_EXHAUSTION_PASS": "PASS", + "PHYSICS_EFFECTS_QEMU_PASS": "PASS" + } + } +} diff --git a/m3/machine_contract.json b/m3/machine_contract.json new file mode 100644 index 0000000..fb48390 --- /dev/null +++ b/m3/machine_contract.json @@ -0,0 +1,151 @@ +{ + "contract_id": "CONTRACT-QEMU-VIRT-AARCH64-M3", + "version": "3.0.0", + "target_platform": "qemu-virt-aarch64", + "milestone": "PHYSICS_EFFECTS", + "architecture": { + "arch": "aarch64", + "endianness": "little", + "expected_entry_exception_level": "EL1", + "expected_entry_el_value": "0x04", + "physics_target_exception_level": "EL1" + }, + "memory_map": { + "dram_base": "0x40000000", + "dram_size_bytes": 134217728, + "dram_end": "0x48000000", + "atlas_entry_base": "0x00000000", + "boot_descriptor_address": "0x401FE000", + "boot_descriptor_size_bytes": 64, + "physics_payload_base": "0x40200000", + "physics_image_base": "0x40200000", + "physics_image_size_bytes": 16384, + "physics_vector_table_base": "0x40204000", + "physics_vector_table_size_bytes": 2048, + "kernel_stack_base": "0x40204800", + "kernel_stack_top": "0x40208800", + "kernel_stack_size_bytes": 16384, + "boot_state_base": "0x40208800", + "boot_state_size_bytes": 2048, + "capability_table_base": "0x40209000", + "capability_table_size_bytes": 4096, + "capability_capacity": 32, + "capability_record_size_bytes": 128, + "receipt_ledger_base": "0x4020A000", + "receipt_ledger_size_bytes": 8192, + "receipt_capacity": 32, + "receipt_record_size_bytes": 192, + "replay_cache_base": "0x4020C000", + "replay_cache_size_bytes": 4096, + "replay_capacity": 32, + "replay_record_size_bytes": 64, + "static_data_base": "0x4020D000", + "static_data_size_bytes": 12288, + "free_frame_base": "0x40210000", + "total_free_frames": 32240, + "uart_mmio_base": "0x09000000", + "uart_flags_offset": 24, + "uart_txff_mask": 32 + }, + "anti_bloat_bounds": { + "max_text_bytes": 32768, + "max_total_image_bytes": 65536 + }, + "disjoint_regions": [ + { + "name": "PHYSICS_IMAGE", + "start": "0x40200000", + "end": "0x40204000", + "size_bytes": 16384 + }, + { + "name": "PHYSICS_VECTOR_TABLE", + "start": "0x40204000", + "end": "0x40204800", + "size_bytes": 2048 + }, + { + "name": "PHYSICS_KERNEL_STACK", + "start": "0x40204800", + "end": "0x40208800", + "size_bytes": 16384 + }, + { + "name": "PHYSICS_BOOT_STATE", + "start": "0x40208800", + "end": "0x40209000", + "size_bytes": 2048 + }, + { + "name": "PHYSICS_CAPABILITY_TABLE", + "start": "0x40209000", + "end": "0x4020A000", + "size_bytes": 4096 + }, + { + "name": "PHYSICS_RECEIPT_LEDGER", + "start": "0x4020A000", + "end": "0x4020C000", + "size_bytes": 8192 + }, + { + "name": "PHYSICS_REPLAY_CACHE", + "start": "0x4020C000", + "end": "0x4020D000", + "size_bytes": 4096 + }, + { + "name": "PHYSICS_STATIC_DATA", + "start": "0x4020D000", + "end": "0x40210000", + "size_bytes": 12288 + }, + { + "name": "FREE_FRAME_REGION", + "start": "0x40210000", + "end": "0x48000000", + "size_bytes": 132055040 + } + ], + "physics_entry_abi": { + "x0": "0x401FE000", + "x1_payload_size": 18432, + "x2_cookie": "0x5048595349435330", + "entry_point": "0x40200000", + "x1_payload_size_enforced": true + }, + "boot_descriptor_contract": { + "expected_address": "0x401FE000", + "size_bytes": 64, + "magic": "0x4D424453435F3031", + "version": 1, + "length": 64, + "flags_reserved": 0, + "ram_base": "0x40000000", + "ram_size": "0x08000000", + "dram_end": "0x48000000", + "uart_base": "0x09000000", + "physics_base": "0x40200000", + "physics_size": 18432 + }, + "vector_table_spec": { + "base_address": "0x40204000", + "alignment_bytes": 2048, + "table_span_bytes": 2048, + "num_slots": 16, + "slot_stride_bytes": 128 + }, + "root_capability_spec": { + "anchor_address": "0x40209000", + "generation": 1, + "slot": 0, + "principal_id": "0x0000000000000001", + "resource_type": "0x00000001", + "allowed_ops": "0xFFFFFFFF", + "bound_base": "0x40210000", + "bound_size": "0x07DF0000", + "revocation_state": 1, + "attenuation_depth": 0, + "atlas_provenance_digest": "f7802501b410a0c19eff7b8fca8865c9ba9c96bfa4f8065ca8f508b67748b9a5" + } +} diff --git a/m3/memory_alloc.s b/m3/memory_alloc.s new file mode 100644 index 0000000..79033a3 --- /dev/null +++ b/m3/memory_alloc.s @@ -0,0 +1,222 @@ +/* + * m3/memory_alloc.s - Physical Frame Authority for Milestone 3 + * Target Platform: QEMU virt AArch64 / Sovereign Machine + * + * Scope: Physical Frame Authority over [FREE_FRAME_BASE, DRAM_END). + * Excludes all reserved Physics pages: [0x40000000, 0x40210000) permanently reserved. + * Bitmap Anchor: PHYSICS_STATIC_DATA @ 0x4020D000 (12 KiB) + */ + + .global init_frame_authority + .global allocate_frame + .global allocate_frame_in_range + .global release_frame + .global is_frame_reserved + + .section .text, "ax" + .balign 4 + + .equ FREE_FRAME_BASE, 0x40210000 + .equ FRAME_BITMAP_BASE, 0x4020D000 + .equ FRAME_SIZE, 4096 + .equ FRAME_SHIFT, 12 + .equ CONTRACT_DRAM_END, 0x48000000 + .equ BITMAP_OFFSET, 32 + .equ STATIC_DATA_SIZE, 12288 + +/* + * init_frame_authority(ram_base, ram_size) + * x0: ram_base, x1: ram_size + * Returns total managed frames in x0, or 0 on error. + */ +init_frame_authority: + stp x29, x30, [sp, #-16]! + mov x29, sp + + adds x2, x0, x1 /* x2 = DRAM_END */ + b.cs .Lno_frames + ldr x4, =CONTRACT_DRAM_END + cmp x2, x4 + b.ne .Lno_frames + + ldr x4, =FREE_FRAME_BASE + cmp x2, x4 + b.ls .Lno_frames + + sub x5, x2, x4 /* x5 = managed bytes */ + lsr x5, x5, #FRAME_SHIFT /* x5 = total frames */ + + add x6, x5, #7 + lsr x6, x6, #3 /* x6 = bitmap bytes */ + add x7, x6, #BITMAP_OFFSET + ldr x8, =STATIC_DATA_SIZE + cmp x7, x8 + b.hi .Lno_frames + + ldr x3, =FRAME_BITMAP_BASE + str x2, [x3, #0] /* +0: DRAM_END */ + str x5, [x3, #8] /* +8: total_frames */ + str xzr, [x3, #16] /* +16: next_free_cursor = 0 */ + str xzr, [x3, #24] /* +24: reserved */ + add x7, x3, #BITMAP_OFFSET /* x7 = bitmap start */ + +.Lzero_bitmap_loop: + cbz x6, .Lzero_bitmap_done + strb wzr, [x7], #1 + sub x6, x6, #1 + b .Lzero_bitmap_loop + +.Lzero_bitmap_done: + mov x0, x5 + ldp x29, x30, [sp], #16 + ret + +.Lno_frames: + mov x0, #0 + ldp x29, x30, [sp], #16 + ret + +/* + * is_frame_reserved(frame_addr) -> x0: 1 if reserved, 0 if free DRAM + * x0: frame_addr + */ +is_frame_reserved: + ldr x1, =FREE_FRAME_BASE + cmp x0, x1 + b.lo .Lframe_is_res + + ldr x2, =CONTRACT_DRAM_END + cmp x0, x2 + b.hs .Lframe_is_res + + mov x0, #0 + ret +.Lframe_is_res: + mov x0, #1 + ret + +/* + * allocate_frame() -> x0: physical frame address, or 0 if exhausted + */ +allocate_frame: + ldr x1, =FRAME_BITMAP_BASE + ldr x2, [x1, #8] /* total_frames */ + ldr x3, [x1, #16] /* cursor */ + + cmp x3, x2 + b.ge .Lalloc_exhausted + + /* Mark bit in bitmap */ + lsr x4, x3, #3 /* byte offset */ + and x5, x3, #7 /* bit index */ + add x6, x1, #BITMAP_OFFSET + add x6, x6, x4 + ldrb w7, [x6] + mov w8, #1 + lsl w8, w8, w5 + orr w7, w7, w8 + strb w7, [x6] + + /* Compute frame address: FREE_FRAME_BASE + (cursor << 12) */ + ldr x9, =FREE_FRAME_BASE + lsl x10, x3, #FRAME_SHIFT + add x0, x9, x10 + + /* Advance cursor */ + add x3, x3, #1 + str x3, [x1, #16] + ret + +.Lalloc_exhausted: + mov x0, #0 + ret + +/* + * allocate_frame_in_range(target_base, target_size) + * x0: target_base, x1: target_size + * Allocates 4 KiB frame at target_base. + * Returns target_base on success, or 0 on failure. + */ +allocate_frame_in_range: + /* Check 4 KiB alignment */ + tst x0, #0xFFF + b.ne .Lalloc_range_fail + + /* Check bounds: target_base >= FREE_FRAME_BASE, target_base < CONTRACT_DRAM_END */ + ldr x2, =FREE_FRAME_BASE + cmp x0, x2 + b.lo .Lalloc_range_fail + + ldr x3, =CONTRACT_DRAM_END + cmp x0, x3 + b.hs .Lalloc_range_fail + + /* Compute frame index */ + sub x4, x0, x2 + lsr x4, x4, #FRAME_SHIFT /* x4 = frame index */ + + ldr x5, =FRAME_BITMAP_BASE + ldr x6, [x5, #8] /* total_frames */ + cmp x4, x6 + b.ge .Lalloc_range_fail + + /* Check if already allocated */ + lsr x7, x4, #3 /* byte offset */ + and x8, x4, #7 /* bit index */ + add x9, x5, #BITMAP_OFFSET + add x9, x9, x7 + ldrb w10, [x9] + mov w11, #1 + lsl w11, w11, w8 + tst w10, w11 + b.ne .Lalloc_range_fail /* Already allocated */ + + /* Mark allocated */ + orr w10, w10, w11 + strb w10, [x9] + + /* Success: return target_base (in x0) */ + ret + +.Lalloc_range_fail: + mov x0, #0 + ret + +/* + * release_frame(frame_addr) -> x0: frame_addr on success, 0 on failure + * x0: frame_addr + */ +release_frame: + ldr x2, =FREE_FRAME_BASE + cmp x0, x2 + b.lo .Lrel_fail + ldr x3, =CONTRACT_DRAM_END + cmp x0, x3 + b.hs .Lrel_fail + tst x0, #0xFFF + b.ne .Lrel_fail + + sub x4, x0, x2 + lsr x4, x4, #FRAME_SHIFT + ldr x5, =FRAME_BITMAP_BASE + ldr x6, [x5, #8] + cmp x4, x6 + b.ge .Lrel_fail + + lsr x7, x4, #3 + and x8, x4, #7 + add x9, x5, #BITMAP_OFFSET + add x9, x9, x7 + ldrb w10, [x9] + mov w11, #1 + lsl w11, w11, w8 + tst w10, w11 + b.eq .Lrel_fail /* Was not allocated */ + + bic w10, w10, w11 + strb w10, [x9] + ret + +.Lrel_fail: + mov x0, #0 + ret diff --git a/m3/physics.ld b/m3/physics.ld new file mode 100644 index 0000000..a287600 --- /dev/null +++ b/m3/physics.ld @@ -0,0 +1,36 @@ +/* + * Linker script for Physics Effects Nucleus (Milestone 3: PHYSICS_EFFECTS) + * Target Platform: QEMU virt AArch64 / Sovereign Machine + * Normative Base: 0x40200000 + * Normative Vectors: 0x40204000 (2 KiB aligned) + */ + +ENTRY(_start) + +SECTIONS +{ + . = 0x40200000; + + .text : { + *(.text.entry) + *(.text) + *(.text.sha256_be) + . = ALIGN(8); + } + + .rodata : ALIGN(8) { + *(.rodata .rodata.*) + . = ALIGN(8); + } + + . = 0x40204000; + .vectors : AT(0x40204000) { + *(.text.vectors) + } + + /DISCARD/ : { + *(.comment) + *(.note.*) + *(.eh_frame) + } +} diff --git a/m3/physics.s b/m3/physics.s new file mode 100644 index 0000000..6c26117 --- /dev/null +++ b/m3/physics.s @@ -0,0 +1,277 @@ +/* + * m3/physics.s - Bare-Metal Authority Nucleus for Milestone 3 (PHYSICS_EFFECTS) + * Target Platform: QEMU virt AArch64 / Sovereign Machine + * Entry Point: _physics_entry @ 0x40200000 + * ABI: PHYSICS_ENTRY_ABI (x0 = 0x401FE000, x1 = 18432, x2 = 'PHYSICS0') + */ + + .global _physics_entry + .global _start + .global print_string + .global print_char + + .extern vector_table + .extern init_frame_authority + .extern init_capability_table + .extern init_receipt_ledger + .extern init_replay_cache + .extern run_m3_unit_tests + + .section .text.entry, "ax" + .balign 64 + +_physics_entry: +_start: + /* 0. Initialize Physics Kernel Stack @ 0x40208800 (growing downward) */ + ldr x3, =0x40208800 + mov sp, x3 + + /* 1. Validate Verification Cookie in x2: 'PHYSICS0' (0x5048595349435330) */ + ldr x3, =0x5048595349435330 + cmp x2, x3 + b.ne .Lpanic_bad_cookie + + /* 1b. Validate payload size in x1 (18,432 bytes) */ + ldr x3, =18432 + cmp x1, x3 + b.ne .Lpanic_bad_payload_size + + /* 2. Validate Boot Descriptor Pointer in x0: 0x401FE000 */ + ldr x3, =0x401FE000 + cmp x0, x3 + b.ne .Lpanic_bad_descriptor + + /* 3. Validate Descriptor Structural Header */ + ldr x4, [x0, #0] + ldr x5, =0x4D424453435F3031 /* 'MBDSC_01' */ + cmp x4, x5 + b.ne .Lpanic_bad_desc_magic + + ldr w4, [x0, #8] /* version == 1 */ + cmp w4, #1 + b.ne .Lpanic_bad_desc_version + + ldr w4, [x0, #12] /* length == 64 */ + cmp w4, #64 + b.ne .Lpanic_bad_desc_length + + ldr x4, [x0, #16] /* flags_reserved == 0 */ + cbnz x4, .Lpanic_bad_desc_reserved + + /* 4. Validate Machine Profile */ + ldr x19, [x0, #24] /* ram_base == 0x40000000 */ + ldr x5, =0x40000000 + cmp x19, x5 + b.ne .Lpanic_ingress_corrupt + + ldr x20, [x0, #32] /* ram_size == 0x08000000 (128 MiB) */ + ldr x5, =0x08000000 + cmp x20, x5 + b.ne .Lpanic_ingress_corrupt + + ldr x21, [x0, #40] /* uart_base == 0x09000000 */ + ldr x5, =0x09000000 + cmp x21, x5 + b.ne .Lpanic_ingress_corrupt + + ldr x22, [x0, #48] /* physics_base == 0x40200000 */ + ldr x5, =0x40200000 + cmp x22, x5 + b.ne .Lpanic_ingress_corrupt + + ldr x23, [x0, #56] /* physics_size == 18432 */ + ldr x5, =18432 + cmp x23, x5 + b.ne .Lpanic_ingress_corrupt + + /* 5. Copy Validated Ingress Data to PHYSICS_BOOT_STATE @ 0x40208800 */ + ldr x8, =0x40208800 + ldp x4, x5, [x0, #0] + stp x4, x5, [x8, #0] + ldp x4, x5, [x0, #16] + stp x4, x5, [x8, #16] + ldp x4, x5, [x0, #32] + stp x4, x5, [x8, #32] + ldp x4, x5, [x0, #48] + stp x4, x5, [x8, #48] + + /* 6. Verify CurrentEL == EL1 (0x04) */ + mrs x9, CurrentEL + and x9, x9, #0x0C + cmp x9, #0x04 + b.ne .Lpanic_uncontracted_el + str x9, [x8, #0x40] /* Record verified CurrentEL */ + + /* 7. Switch to Dedicated Kernel Stack: SP = 0x40208800 */ + ldr x10, =0x40208800 + mov sp, x10 + mov x0, #0 /* Drop Atlas scratchpad reference */ + + /* 8. Diagnostic Checkpoints */ + adr x0, msg_awaken + bl print_string + + adr x0, msg_ingress_valid + bl print_string + + adr x0, msg_entry_el1_verified + bl print_string + + /* 9. Install Exception Vector Table */ + ldr x1, =vector_table + msr vbar_el1, x1 + isb + + adr x0, msg_vbar_installed + bl print_string + + /* 10. Initialize Physical Frame Authority */ + ldr x8, =0x40208800 + ldr x0, [x8, #24] /* ram_base */ + ldr x1, [x8, #32] /* ram_size */ + bl init_frame_authority + cbz x0, .Lpanic_frame_authority + + adr x0, msg_frame_auth_bound + bl print_string + + /* 11. Initialize Capability Table (CAP_ROOT Genesis) */ + bl init_capability_table + adr x0, msg_cap_root_genesis + bl print_string + + /* 12. Initialize Receipt Ledger */ + bl init_receipt_ledger + adr x0, msg_receipt_ledger_init + bl print_string + + /* 13. Initialize Replay Cache */ + bl init_replay_cache + adr x0, msg_replay_cache_init + bl print_string + + /* 14. Milestone 3 Ready */ + adr x0, msg_quiescent_ready + bl print_string + + /* 15. Execute Milestone 3 Unit Tests */ + bl run_m3_unit_tests + +.Lquiescent_halt: + wfe + b .Lquiescent_halt + +/* Panic handlers */ +.Lpanic_bad_cookie: + adr x0, msg_panic_cookie + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_payload_size: + adr x0, msg_panic_payload_size + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_descriptor: + adr x0, msg_panic_desc + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_desc_magic: + adr x0, msg_panic_desc_magic + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_desc_version: + adr x0, msg_panic_desc_version + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_desc_length: + adr x0, msg_panic_desc_length + bl print_string + b .Lquiescent_halt + +.Lpanic_bad_desc_reserved: + adr x0, msg_panic_desc_reserved + bl print_string + b .Lquiescent_halt + +.Lpanic_ingress_corrupt: + adr x0, msg_panic_ingress + bl print_string + b .Lquiescent_halt + +.Lpanic_uncontracted_el: + adr x0, msg_panic_el + bl print_string + b .Lquiescent_halt + +.Lpanic_frame_authority: + adr x0, msg_panic_frame_auth + bl print_string + b .Lquiescent_halt + +/* UART Polled Telemetry */ +print_string: + ldr x1, =0x09000000 /* PL011 UART */ +.Lputs_loop: + ldrb w2, [x0], #1 + cbz w2, .Lputs_done +1: ldrb w3, [x1, #24] /* UARTFR */ + tst w3, #32 /* TXFF */ + b.ne 1b + strb w2, [x1] + b .Lputs_loop +.Lputs_done: + ret + +print_char: + ldr x1, =0x09000000 +1: ldrb w2, [x1, #24] + tst w2, #32 + b.ne 1b + strb w0, [x1] + ret + + .section .rodata + .balign 8 +msg_awaken: + .asciz "PHYSICS: AWAKEN\n" +msg_ingress_valid: + .asciz "PHYSICS: INGRESS_VALID\n" +msg_entry_el1_verified: + .asciz "PHYSICS: ENTRY_EL1_VERIFIED\n" +msg_vbar_installed: + .asciz "PHYSICS: VBAR_INSTALLED\n" +msg_frame_auth_bound: + .asciz "PHYSICS: FRAME_AUTH_BOUND\n" +msg_cap_root_genesis: + .asciz "PHYSICS: CAP_ROOT_GENESIS\n" +msg_receipt_ledger_init: + .asciz "PHYSICS: RECEIPT_LEDGER_INIT\n" +msg_replay_cache_init: + .asciz "PHYSICS: REPLAY_CACHE_INIT\n" +msg_quiescent_ready: + .asciz "PHYSICS: QUIESCENT_READY\n" + +msg_panic_cookie: + .asciz "PANIC: BAD VERIFICATION COOKIE\n" +msg_panic_payload_size: + .asciz "PANIC: BAD PAYLOAD SIZE\n" +msg_panic_desc: + .asciz "PANIC: BAD BOOT DESCRIPTOR POINTER\n" +msg_panic_desc_magic: + .asciz "PANIC: BAD DESCRIPTOR MAGIC\n" +msg_panic_desc_version: + .asciz "PANIC: UNSUPPORTED DESCRIPTOR VERSION\n" +msg_panic_desc_length: + .asciz "PANIC: TRUNCATED/OVERSIZED DESCRIPTOR\n" +msg_panic_desc_reserved: + .asciz "PANIC: NON-ZERO RESERVED FLAGS\n" +msg_panic_ingress: + .asciz "PANIC: INGRESS MACHINE PROFILE VIOLATION\n" +msg_panic_el: + .asciz "PANIC: UNCONTRACTED ENTRY EXCEPTION LEVEL\n" +msg_panic_frame_auth: + .asciz "PANIC: FRAME AUTHORITY REJECTED\n" diff --git a/m3/receipt_ledger.s b/m3/receipt_ledger.s new file mode 100644 index 0000000..3c5008b --- /dev/null +++ b/m3/receipt_ledger.s @@ -0,0 +1,176 @@ +/* + * m3/receipt_ledger.s - Cryptographic Receipt Chain & Bounded Evidence Ledger + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Append-only bounded receipt ledger. 32 receipts maximum. + * Each receipt binds the previous receipt's SHA-256 digest. + * Full ledger halts admission (fail-closed, never silently overwrite evidence). + * Storage: PHYSICS_RECEIPT_LEDGER @ 0x4020A000 (8 KiB) + */ + + .global init_receipt_ledger + .global get_receipt_count + .global is_receipt_ledger_full + .global commit_receipt + + .extern sha256_be + + .section .text, "ax" + .balign 4 + + .equ RECEIPT_LEDGER_BASE, 0x4020A000 + .equ RECEIPT_LEDGER_CAPACITY, 32 + .equ RECEIPT_HEADER_SIZE, 128 + .equ RECEIPT_RECORD_SIZE, 192 + .equ RECEIPT_ENTRIES_BASE, 0x4020A080 + .equ RECEIPT_MAGIC, 0x524350544C444752 /* 'RCPTLDGR' */ + +/* + * init_receipt_ledger(): + * Zeros the 8 KiB ledger area, then writes the ledger header: + * +0x00: magic (0x524350544C444752) + * +0x08: capacity (u32 = 32), count (u32 = 0) + * +0x10: machine_generation (u64 = 1) + * +0x18: reserved (8 zero bytes) + * +0x20: latest_receipt_digest (32 zero bytes) + */ +init_receipt_ledger: + stp x29, x30, [sp, #-16]! + mov x29, sp + + /* Zero 8192 bytes */ + ldr x0, =RECEIPT_LEDGER_BASE + ldr x1, =8192 +.Lzero_rcpt_loop: + cbz x1, .Lzero_rcpt_done + stp xzr, xzr, [x0], #16 + sub x1, x1, #16 + b .Lzero_rcpt_loop + +.Lzero_rcpt_done: + ldr x0, =RECEIPT_LEDGER_BASE + ldr x1, =RECEIPT_MAGIC + str x1, [x0, #0] /* +0x00: magic */ + + mov w1, #RECEIPT_LEDGER_CAPACITY + str w1, [x0, #8] /* +0x08: capacity = 32 */ + str wzr, [x0, #12] /* +0x0C: count = 0 */ + + mov x1, #1 + str x1, [x0, #16] /* +0x10: machine_generation = 1 */ + + ldp x29, x30, [sp], #16 + ret + +/* + * get_receipt_count() -> w0 + */ +get_receipt_count: + ldr x1, =RECEIPT_LEDGER_BASE + ldr w0, [x1, #12] + ret + +/* + * is_receipt_ledger_full() -> w0 (1 if full, 0 if space available) + */ +is_receipt_ledger_full: + ldr x1, =RECEIPT_LEDGER_BASE + ldr w0, [x1, #12] /* count */ + cmp w0, #RECEIPT_LEDGER_CAPACITY + cset w0, ge + ret + +/* + * commit_receipt(receipt_data_ptr) + * x0: Pointer to a populated ReceiptData block on stack/memory (first 96 bytes: 0x00..0x5F) + * Returns: + * x0: Pointer to committed receipt in PHYSICS_RECEIPT_LEDGER, or 0 if ledger full. + */ +commit_receipt: + stp x29, x30, [sp, #-48]! + mov x29, sp + stp x19, x20, [sp, #16] + stp x21, x22, [sp, #32] + + mov x19, x0 /* x19 = src receipt data */ + + /* 1. Check ledger full */ + ldr x20, =RECEIPT_LEDGER_BASE + ldr w21, [x20, #12] /* w21 = current count */ + cmp w21, #RECEIPT_LEDGER_CAPACITY + b.ge .Lledger_exhausted + + /* 2. Compute destination entry address: RECEIPT_ENTRIES_BASE + count * 192 */ + ldr x22, =RECEIPT_ENTRIES_BASE + mov x1, #RECEIPT_RECORD_SIZE + madd x22, x21, x1, x22 /* x22 = dest receipt ptr */ + + /* 3. Copy first 96 bytes (0x00..0x5F) from src to dst */ + /* 12 x 8-byte words */ + ldp x2, x3, [x19, #0] + stp x2, x3, [x22, #0] + ldp x4, x5, [x19, #16] + stp x4, x5, [x22, #16] + ldp x6, x7, [x19, #32] + stp x6, x7, [x22, #32] + ldp x8, x9, [x19, #48] + stp x8, x9, [x22, #48] + ldp x10, x11, [x19, #64] + stp x10, x11, [x22, #64] + ldp x12, x13, [x19, #80] + stp x12, x13, [x22, #80] + + /* Ensure version = 1 and length = 192 */ + mov w1, #1 + strh w1, [x22, #0] /* version = 1 */ + mov w1, #RECEIPT_RECORD_SIZE + strh w1, [x22, #2] /* length = 192 */ + + /* Bind current machine_generation at +0x50 */ + ldr x14, [x20, #16] /* machine_generation */ + str x14, [x22, #0x50] + + /* 4. Copy previous_receipt_digest (32 bytes) from header.latest_receipt_digest (+0x20) to dst (+0x60) */ + ldp x2, x3, [x20, #0x20] + ldp x4, x5, [x20, #0x30] + stp x2, x3, [x22, #0x60] + stp x4, x5, [x22, #0x70] + + /* 5. Compute SHA-256 seal over bytes 0x00..0x7F (128 bytes) of destination receipt */ + /* Store result into dst + 0x80 (receipt_digest) */ + mov x0, x22 /* data ptr */ + mov x1, #128 /* len = 128 bytes */ + add x2, x22, #0x80 /* out = dst + 0x80 */ + bl sha256_be + cbnz w0, .Lledger_exhausted + + /* Zero trailing padding +0xA0..+0xBF (32 bytes) */ + stp xzr, xzr, [x22, #0xA0] + stp xzr, xzr, [x22, #0xB0] + + /* 6. Update header.latest_receipt_digest with new digest */ + ldp x2, x3, [x22, #0x80] + ldp x4, x5, [x22, #0x90] + stp x2, x3, [x20, #0x20] + stp x4, x5, [x20, #0x30] + + /* 7. Increment count and machine_generation */ + add w21, w21, #1 + str w21, [x20, #12] /* count */ + + ldr x14, [x20, #16] + add x14, x14, #1 + str x14, [x20, #16] /* machine_generation */ + + mov x0, x22 /* Return committed receipt pointer */ + ldp x19, x20, [sp, #16] + ldp x21, x22, [sp, #32] + ldp x29, x30, [sp], #48 + ret + +.Lledger_exhausted: + mov x0, #0 + ldp x19, x20, [sp, #16] + ldp x21, x22, [sp, #32] + ldp x29, x30, [sp], #48 + ret diff --git a/m3/replay_cache.s b/m3/replay_cache.s new file mode 100644 index 0000000..5783ed8 --- /dev/null +++ b/m3/replay_cache.s @@ -0,0 +1,157 @@ +/* + * m3/replay_cache.s - Bounded Replay Cache & Idempotency Engine + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Fixed-capacity 32-entry replay cache. + * Identical replay -> idempotent original result with zero re-execution. + * Conflicting intent with same request_id -> REJECTED_REPLAY_CONFLICT. + * Storage: PHYSICS_REPLAY_CACHE @ 0x4020C000 (4 KiB) + */ + + .global init_replay_cache + .global lookup_replay + .global record_replay + + .section .text, "ax" + .balign 4 + + .equ REPLAY_CACHE_BASE, 0x4020C000 + .equ REPLAY_CACHE_CAPACITY, 32 + .equ REPLAY_ENTRY_SIZE, 64 + .equ REPLAY_ENTRY_SHIFT, 6 + + .equ REPLAY_MISS, 0 + .equ REPLAY_HIT, 1 + .equ REPLAY_CONFLICT, 2 + +/* + * init_replay_cache(): + * Zeros the 4 KiB replay cache area. + */ +init_replay_cache: + ldr x0, =REPLAY_CACHE_BASE + mov x1, #4096 +.Lzero_replay_loop: + cbz x1, .Lzero_replay_done + stp xzr, xzr, [x0], #16 + sub x1, x1, #16 + b .Lzero_replay_loop +.Lzero_replay_done: + ret + +/* + * lookup_replay(request_id, intent_digest_ptr) + * x0: request_id, x1: intent_digest_ptr + * Returns: + * w0: REPLAY_MISS (0), REPLAY_HIT (1), or REPLAY_CONFLICT (2) + * x1: receipt_slot + * w2: decision + * x3: actual_effect + */ +lookup_replay: + stp x29, x30, [sp, #-16]! + mov x29, sp + + ldr x2, =REPLAY_CACHE_BASE + mov w3, #0 /* index 0..31 */ + +.Lsearch_replay: + cmp w3, #REPLAY_CACHE_CAPACITY + b.ge .Lreplay_not_found + + ubfiz x4, x3, #REPLAY_ENTRY_SHIFT, #5 + add x5, x2, x4 /* x5 = entry ptr */ + + /* Check if entry valid */ + ldr x6, [x5, #0x40] /* entry.valid */ + cbz x6, .Lreplay_next + + /* Check request_id match */ + ldr x7, [x5, #0x00] /* entry.request_id */ + cmp x0, x7 + b.ne .Lreplay_next + + /* request_id matches! Compare 32-byte intent_digest */ + add x8, x5, #0x08 /* entry.intent_digest */ + ldp x9, x10, [x8, #0] + ldp x11, x12, [x1, #0] + cmp x9, x11 + b.ne .Lreplay_mismatch + cmp x10, x12 + b.ne .Lreplay_mismatch + + ldp x9, x10, [x8, #16] + ldp x11, x12, [x1, #16] + cmp x9, x11 + b.ne .Lreplay_mismatch + cmp x10, x12 + b.ne .Lreplay_mismatch + + /* Exact match! REPLAY_HIT */ + ldr w1, [x5, #0x28] /* receipt_slot */ + ldr w2, [x5, #0x2C] /* decision */ + ldr x3, [x5, #0x30] /* actual_effect */ + mov w0, #REPLAY_HIT + ldp x29, x30, [sp], #16 + ret + +.Lreplay_mismatch: + /* Same request_id, different intent digest -> REPLAY_CONFLICT */ + mov w0, #REPLAY_CONFLICT + ldp x29, x30, [sp], #16 + ret + +.Lreplay_next: + add w3, w3, #1 + b .Lsearch_replay + +.Lreplay_not_found: + mov w0, #REPLAY_MISS + ldp x29, x30, [sp], #16 + ret + +/* + * record_replay(request_id, intent_digest_ptr, receipt_slot, decision, actual_effect, output) + * x0: request_id, x1: intent_digest_ptr, w2: receipt_slot, w3: decision, x4: actual_effect, x5: output + */ +record_replay: + ldr x6, =REPLAY_CACHE_BASE + mov w7, #0 + +.Lfind_replay_slot: + cmp w7, #REPLAY_CACHE_CAPACITY + b.ge .Lrecord_done /* Cache full */ + + ubfiz x8, x7, #REPLAY_ENTRY_SHIFT, #5 + add x9, x6, x8 /* x9 = entry ptr */ + + ldr x10, [x9, #0x40] /* valid */ + cbz x10, .Lfound_free_replay + + /* Check if already matches this request_id */ + ldr x11, [x9, #0x00] + cmp x0, x11 + b.eq .Lfound_free_replay + + add w7, w7, #1 + b .Lfind_replay_slot + +.Lfound_free_replay: + str x0, [x9, #0x00] /* request_id */ + + /* Copy 32-byte digest */ + ldp x12, x13, [x1, #0] + ldp x14, x15, [x1, #16] + stp x12, x13, [x9, #0x08] + stp x14, x15, [x9, #0x18] + + str w2, [x9, #0x28] /* receipt_slot */ + str w3, [x9, #0x2C] /* decision */ + str x4, [x9, #0x30] /* actual_effect */ + str x5, [x9, #0x38] /* output */ + + mov x12, #1 + str x12, [x9, #0x40] /* valid = 1 */ + +.Lrecord_done: + ret diff --git a/m3/sha256_be.s b/m3/sha256_be.s new file mode 100644 index 0000000..8b090f5 --- /dev/null +++ b/m3/sha256_be.s @@ -0,0 +1,85 @@ +// sha256_be.s -- canonical big-endian SHA-256 wrapper for PHYSICS M3. +// +// int sha256_be(const u8 *data /*x0*/, u64 len /*x1*/, u8 out[32] /*x2*/) +// +// Calls sha256_compute() from the repo-root sha256_clean.c (compiled, never +// copied) with its own 16-byte-aligned stack scratch[128] and a stack u32[8] +// state, then stores the digest to `out` as canonical big-endian bytes +// (REV of each native u32 word), i.e. the same bytes `sha256sum` prints. +// +// Fail closed: every precondition is checked before anything is written. +// On a nonzero return `out` is untouched and sha256_compute is never called. +// 0 SHA256_BE_OK +// 1 SHA256_BE_ERR_LEN len > SHA256_MAX_LEN +// 2 SHA256_BE_ERR_DATA_ALIGN data not 4-byte aligned (sha256_transform does u32 loads) +// 3 SHA256_BE_ERR_OUT_ALIGN out not 4-byte aligned (digest stored as u32 words) +// 4 SHA256_BE_ERR_WRAP data + len wraps the address space +// General registers only (no FP/SIMD). AAPCS64; clobbers x0-x18 as allowed. + + .equ SHA256_MAX_LEN, 0x10000 + .equ SHA256_BE_OK, 0 + .equ SHA256_BE_ERR_LEN, 1 + .equ SHA256_BE_ERR_DATA_ALIGN, 2 + .equ SHA256_BE_ERR_OUT_ALIGN, 3 + .equ SHA256_BE_ERR_WRAP, 4 + + // Stack frame (192 B, 16-aligned): + // [sp+0] x29, x30 + // [sp+16] x19 (+8 pad) + // [sp+32] u32 state[8] (native words from sha256_compute) + // [sp+64] u8 scratch[128] + .equ FRAME, 192 + .equ OFF_STATE, 32 + .equ OFF_SCR, 64 + + .global SHA256_MAX_LEN + .section .text.sha256_be, "ax", %progbits + .balign 4 + .global sha256_be + .type sha256_be, %function +sha256_be: + mov x9, #SHA256_MAX_LEN + cmp x1, x9 + b.hi .Lerr_len + tst x0, #3 + b.ne .Lerr_data + tst x2, #3 + b.ne .Lerr_out + adds x9, x0, x1 + b.cs .Lerr_wrap + + stp x29, x30, [sp, #-FRAME]! + mov x29, sp + str x19, [sp, #16] + mov x19, x2 // out + add x2, sp, #OFF_SCR // scratch[128] + add x3, sp, #OFF_STATE // u32 state[8] + bl sha256_compute + + add x9, sp, #OFF_STATE + mov x10, #4 // 4 pairs of words +1: ldp w11, w12, [x9], #8 + rev w11, w11 + rev w12, w12 + stp w11, w12, [x19], #8 + subs x10, x10, #1 + b.ne 1b + + mov w0, #SHA256_BE_OK + ldr x19, [sp, #16] + ldp x29, x30, [sp], #FRAME + ret + +.Lerr_len: + mov w0, #SHA256_BE_ERR_LEN + ret +.Lerr_data: + mov w0, #SHA256_BE_ERR_DATA_ALIGN + ret +.Lerr_out: + mov w0, #SHA256_BE_ERR_OUT_ALIGN + ret +.Lerr_wrap: + mov w0, #SHA256_BE_ERR_WRAP + ret + .size sha256_be, . - sha256_be diff --git a/m3/tests/kat.ld b/m3/tests/kat.ld new file mode 100644 index 0000000..1f7fa66 --- /dev/null +++ b/m3/tests/kat.ld @@ -0,0 +1,23 @@ +/* kat.ld -- SHA-256 KAT image, loaded raw at 0x40200000 (QEMU puts the DTB at the RAM base 0x40000000 with -bios). */ +OUTPUT_FORMAT("elf64-littleaarch64") +OUTPUT_ARCH(aarch64) +ENTRY(_start) +KAT_BASE = 0x40200000; +KAT_LIMIT = 0x40270000 - 0x4000; /* image must stay below the 16 KiB stack */ +SECTIONS +{ + . = KAT_BASE; + .text : { + KEEP(*(.text.boot)) + *(.text .text.*) + } + .rodata : ALIGN(8) { *(.rodata .rodata.*) } + .data : ALIGN(8) { *(.data .data.*) } + .bss : ALIGN(8) { *(.bss .bss.* COMMON) } + __kat_end = .; + /DISCARD/ : { *(.comment) *(.note*) *(.eh_frame*) } +} +ASSERT(SIZEOF(.data) == 0, "KAT: unexpected .data") +ASSERT(SIZEOF(.bss) == 0, "KAT: unexpected .bss (raw image would not zero it)") +ASSERT(__kat_end <= KAT_LIMIT, "KAT: image overlaps stack") +ASSERT(SHA256_MAX_LEN <= 0x10000, "KAT: SHA256_MAX_LEN exceeds the 64 KiB PAT buffer") diff --git a/m3/tests/kat_stub.s b/m3/tests/kat_stub.s new file mode 100644 index 0000000..2c7d92e --- /dev/null +++ b/m3/tests/kat_stub.s @@ -0,0 +1,7 @@ +// kat_stub.s -- trivial -bios stub for the SHA-256 KAT: runs from the virt +// flash at 0x0 and branches to the KAT image loaded raw at 0x40200000 (above the DTB QEMU places at the RAM base). + .section .text, "ax", %progbits + .global _start +_start: + movz x0, #0x4020, lsl #16 + br x0 diff --git a/m3/tests/run_m3_gates.sh b/m3/tests/run_m3_gates.sh new file mode 100755 index 0000000..8ab307f --- /dev/null +++ b/m3/tests/run_m3_gates.sh @@ -0,0 +1,372 @@ +#!/bin/sh +# m3/tests/run_m3_gates.sh -- Master Qualification Gate Runner for Milestone 3 (PHYSICS_EFFECTS) +# +# Python-free qualification harness. +# Evaluates all 19 canonical qualification gates across: +# - Seam 1: Capability & Ledger Static Proofs (7 gates) +# - Seam 2: Effect Broker & Replay Execution (8 gates) +# - Seam 3: Receipt Evidence & Full System Emulation (4 gates) +# +# Generates canonical m3_qualification_receipt.json binding all artifact digests. +set -eu + +M3_DIR=$(cd "$(dirname "$0")/.." && pwd -P) +PHYSICS_ROOT=$(cd "$M3_DIR/.." && pwd -P) +ARCH_ROOT=$(cd "$PHYSICS_ROOT/../aien-architecture" && pwd -P) +OUT="$M3_DIR/build" +RUN_DIR="/tmp/m3_gate_qualification_$$" + +M3TOOL="$OUT/m3tool" +QEMU_INSPECT="$M3_DIR/tools/qemu_inspect.sh" +KAT_SH="$M3_DIR/tests/run_sha256_kat.sh" + +trap 'rm -rf "$RUN_DIR"' EXIT INT TERM +mkdir -p "$RUN_DIR" + +echo "================================================================================" +echo " AIEN SOVEREIGN MACHINE — MILESTONE 3: PHYSICS_EFFECTS QUALIFICATION" +echo "================================================================================" +echo "Timestamp: $(date -u +'%Y-%m-%dT%H:%M:%SZ')" +echo "Host: $(uname -s) $(uname -m)" +echo "" + +# 1. Build all artifacts +echo "[*] Step 1: Building M3 images and host tools via m3/build.sh..." +"$M3_DIR/build.sh" +echo " Build OK." +echo "" + +# 2. Run NIST SHA-256 KAT Test Suite +echo "[*] Step 2: Running SHA-256 Known-Answer-Test (KAT) harness..." +"$KAT_SH" +echo " SHA-256 KAT suite passed." +echo "" + +# 3. Bare-Metal QEMU Execution & State Inspection +echo "[*] Step 3: Launching QEMU Bare-Metal Execution & State Inspection..." +"$QEMU_INSPECT" \ + --bios "$OUT/atlas_m3.bin" \ + --loader "$OUT/physics.bin@0x40200000" \ + --out "$RUN_DIR" \ + --timeout 10 \ + --marker "PHYSICS: M3_TESTS_DONE" \ + --region "test_record:0x40208A00:0x100" \ + --region "receipt_ledger:0x4020A000:0x2000" \ + --region "replay_cache:0x4020C000:0x1000" \ + --region "cap_table:0x40209000:0x1000" \ + --region "boot_state:0x40208800:0x80" + +echo " QEMU execution succeeded. Memory regions extracted to $RUN_DIR." +echo "" + +# 4. Read Test Summary Record +TEST_MAGIC=$("$M3TOOL" hexfield "$RUN_DIR/test_record.bin" 0 8) +TESTS_RUN=$("$M3TOOL" hexfield "$RUN_DIR/test_record.bin" 8 8) +TESTS_PASSED=$("$M3TOOL" hexfield "$RUN_DIR/test_record.bin" 16 8) +TEST_BITMASK=$("$M3TOOL" hexfield "$RUN_DIR/test_record.bin" 24 8) + +echo "[*] Test Suite Execution Metrics:" +echo " Magic: $TEST_MAGIC (expected 0x4d33544553545355 'M3TESTSU')" +echo " Tests Run: $TESTS_RUN (expected 0x0000000000000015 = 21)" +echo " Tests Passed: $TESTS_PASSED (expected 0x0000000000000015 = 21)" +echo " Bitmask: $TEST_BITMASK (expected 0x00000000001fffff = 21 bits set)" + +if [ "$TEST_MAGIC" != "0x4d33544553545355" ] || \ + [ "$TESTS_RUN" != "0x0000000000000015" ] || \ + [ "$TESTS_PASSED" != "0x0000000000000015" ] || \ + [ "$TEST_BITMASK" != "0x00000000001fffff" ]; then + echo "[-] ERROR: Bare-metal test suite did not pass all 21 tests!" >&2 + exit 1 +fi +echo "" + +GATES_PASSED=0 +GATES_TOTAL=19 + +eval_gate() { + id="$1" + name="$2" + cond="$3" + if [ "$cond" -eq 1 ]; then + echo " [PASS] Gate $id: $name" + GATES_PASSED=$((GATES_PASSED + 1)) + else + echo " [FAIL] Gate $id: $name" >&2 + exit 1 + fi +} + +echo "================================================================================" +echo " SEAM 1: CAPABILITY & LEDGER STATIC PROOFS (7 GATES)" +echo "================================================================================" + +# Gate 1: PHYSICS_CAP_LEDGER_PASS +# Verify Cap table size is 4096 bytes (32 * 128B), and slot 0 is properly formatted CAP_ROOT +CAP_SIZE=$(wc -c < "$RUN_DIR/cap_table.bin") +SLOT0_SLOT=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 0 4) +SLOT0_GEN=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 4 4) +SLOT0_PRINCIPAL=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 8 8) +SLOT0_RTYPE=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 16 4) +SLOT0_OPS=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 20 4) +SLOT0_BASE=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 24 8) +SLOT0_BSIZE=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 32 8) +SLOT0_STATE=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 40 4) +SLOT0_DEPTH=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 44 4) + +G1=0 +if [ "$CAP_SIZE" -eq 4096 ] && \ + [ "$SLOT0_SLOT" = "0x00000000" ] && \ + [ "$SLOT0_GEN" = "0x00000001" ] && \ + [ "$SLOT0_PRINCIPAL" = "0x0000000000000001" ] && \ + [ "$SLOT0_RTYPE" = "0x00000001" ] && \ + [ "$SLOT0_OPS" = "0xffffffff" ] && \ + [ "$SLOT0_BASE" = "0x0000000040210000" ] && \ + [ "$SLOT0_BSIZE" = "0x0000000007df0000" ] && \ + [ "$SLOT0_STATE" = "0x00000001" ] && \ + [ "$SLOT0_DEPTH" = "0x00000000" ]; then + G1=1 +fi +eval_gate 1 "PHYSICS_CAP_LEDGER_PASS" $G1 + +# Gate 2: PHYSICS_CAP_ROOT_PRIVATE_PASS +# Slot 0 principal is 1 (Physics); Test 14 (bit 13) confirms external principal derivation/exercise refused +G2=0 +if [ "$SLOT0_PRINCIPAL" = "0x0000000000000001" ]; then + G2=1 +fi +eval_gate 2 "PHYSICS_CAP_ROOT_PRIVATE_PASS" $G2 + +# Gate 3: PHYSICS_CAP_ATTENUATION_PASS +# Check slot 1 (Cap A) and slot 2 (Cap B). Verify monotonic narrowing and depth increment +SLOT1_DEPTH=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 172 4) # 128 + 44 +SLOT2_DEPTH=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 300 4) # 256 + 44 +SLOT1_PARENT=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 208 4) # 128 + 0x50 +SLOT2_PARENT=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 336 4) # 256 + 0x50 + +G3=0 +if [ "$SLOT1_DEPTH" = "0x00000001" ] && \ + [ "$SLOT2_DEPTH" = "0x00000002" ] && \ + [ "$SLOT1_PARENT" = "0x00000000" ] && \ + [ "$SLOT2_PARENT" = "0x00000001" ]; then + G3=1 +fi +eval_gate 3 "PHYSICS_CAP_ATTENUATION_PASS" $G3 + +# Gate 4: PHYSICS_CAP_NO_AMBIENT_AUTHORITY_PASS +# Slot 15 is unallocated; verify it remains all zeros in cap_table +SLOT15_GEN=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 1924 4) # 15*128 + 4 +SLOT15_STATE=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 1960 4) # 15*128 + 40 +G4=0 +if [ "$SLOT15_GEN" = "0x00000000" ] && [ "$SLOT15_STATE" = "0x00000000" ]; then + G4=1 +fi +eval_gate 4 "PHYSICS_CAP_NO_AMBIENT_AUTHORITY_PASS" $G4 + +# Gate 5: PHYSICS_CAP_GENERATION_PASS +# Slot 1 was reallocated in test 15; verify slot 1 generation bumped to 2 +SLOT1_GEN=$("$M3TOOL" hexfield "$RUN_DIR/cap_table.bin" 132 4) # 128 + 4 +G5=0 +if [ "$SLOT1_GEN" = "0x00000002" ]; then + G5=1 +fi +eval_gate 5 "PHYSICS_CAP_GENERATION_PASS" $G5 + +# Gate 6: PHYSICS_CAP_REVOCATION_PASS +# Direct revocation: verified by Test 5 (bit 4) pass in test suite +G6=1 +eval_gate 6 "PHYSICS_CAP_REVOCATION_PASS" $G6 + +# Gate 7: PHYSICS_CAP_DESCENDANT_REVOCATION_PASS +# Ancestor-chain validation: verified by Test 6 (bit 5) pass in test suite +G7=1 +eval_gate 7 "PHYSICS_CAP_DESCENDANT_REVOCATION_PASS" $G7 + +echo "" +echo "================================================================================" +echo " SEAM 2: EFFECT BROKER & REPLAY EXECUTION (8 GATES)" +echo "================================================================================" + +# Gate 8: PHYSICS_EFFECT_ADMISSION_PASS +# Verified by Test 2 (frame granted, receipt emitted with DEC_ADMITTED) +G8=1 +eval_gate 8 "PHYSICS_EFFECT_ADMISSION_PASS" $G8 + +# Gate 9: PHYSICS_EFFECT_BOUNDS_PASS +# Verified by Test 4 & Test 12 (DEC_REJECTED_BOUNDS on out-of-bounds target / overflow) +G9=1 +eval_gate 9 "PHYSICS_EFFECT_BOUNDS_PASS" $G9 + +# Gate 10: PHYSICS_EFFECT_FORGED_CAP_REFUSAL_PASS +# Verified by Test 10 (DEC_REJECTED_UNKNOWN_CAP on unallocated slot 15) +G10=1 +eval_gate 10 "PHYSICS_EFFECT_FORGED_CAP_REFUSAL_PASS" $G10 + +# Gate 11: PHYSICS_EFFECT_STALE_CAP_REFUSAL_PASS +# Verified by Test 16 (DEC_REJECTED_STALE_GENERATION on stale handle) +G11=1 +eval_gate 11 "PHYSICS_EFFECT_STALE_CAP_REFUSAL_PASS" $G11 + +# Gate 12: PHYSICS_EFFECT_REVOKED_CAP_REFUSAL_PASS +# Verified by Test 5 (DEC_REJECTED_REVOKED on directly revoked cap) +G12=1 +eval_gate 12 "PHYSICS_EFFECT_REVOKED_CAP_REFUSAL_PASS" $G12 + +# Gate 13: PHYSICS_EFFECT_WRONG_PRINCIPAL_REFUSAL_PASS +# Verified by Test 13 (DEC_REJECTED_WRONG_PRINCIPAL on principal mismatch) +G13=1 +eval_gate 13 "PHYSICS_EFFECT_WRONG_PRINCIPAL_REFUSAL_PASS" $G13 + +# Gate 14: PHYSICS_EFFECT_REPLAY_PASS +# Verified by Test 17 (identical request_id returns existing receipt without re-executing) +G14=1 +eval_gate 14 "PHYSICS_EFFECT_REPLAY_PASS" $G14 + +# Gate 15: PHYSICS_EFFECT_REPLAY_CONFLICT_PASS +# Verified by Test 18 (altered intent on duplicate request_id refused with DEC_REJECTED_REPLAY_CONFLICT) +G15=1 +eval_gate 15 "PHYSICS_EFFECT_REPLAY_CONFLICT_PASS" $G15 + +echo "" +echo "================================================================================" +echo " SEAM 3: RECEIPT EVIDENCE & FULL SYSTEM EMULATION (4 GATES)" +echo "================================================================================" + +# Gate 16: PHYSICS_RECEIPT_IDENTITY_PASS +# Inspect receipt 0 in receipt_ledger.bin using m3tool verify-receipt +VR_OUT=$("$M3TOOL" verify-receipt "$RUN_DIR/receipt_ledger.bin" 128) +G16=0 +case "$VR_OUT" in + PASS*) G16=1 ;; +esac +eval_gate 16 "PHYSICS_RECEIPT_IDENTITY_PASS" $G16 + +# Gate 17: PHYSICS_RECEIPT_CHAIN_PASS +# Verify entire rolling SHA-256 hash chain of receipts +VC_OUT=$("$M3TOOL" verify-chain "$RUN_DIR/receipt_ledger.bin") +G17=0 +case "$VC_OUT" in + PASS*) G17=1 ;; +esac +eval_gate 17 "PHYSICS_RECEIPT_CHAIN_PASS" $G17 + +# Gate 18: PHYSICS_RECEIPT_EXHAUSTION_PASS +# Verified by Test 19 (receipt ledger full halts admission fail-closed with zero effect) +G18=1 +eval_gate 18 "PHYSICS_RECEIPT_EXHAUSTION_PASS" $G18 + +# Gate 19: PHYSICS_EFFECTS_QEMU_PASS +# Clean Atlas -> Physics M3 end-to-end boot and test run verified +G19=0 +if grep -q "ATLAS: AWAKEN" "$RUN_DIR/serial.txt" && \ + grep -q "ATLAS: VERIFY" "$RUN_DIR/serial.txt" && \ + grep -q "ATLAS: HANDOFF" "$RUN_DIR/serial.txt" && \ + grep -q "PHYSICS: AWAKEN" "$RUN_DIR/serial.txt" && \ + grep -q "PHYSICS: QUIESCENT_READY" "$RUN_DIR/serial.txt" && \ + grep -q "PHYSICS: M3_TESTS_DONE" "$RUN_DIR/serial.txt"; then + G19=1 +fi +eval_gate 19 "PHYSICS_EFFECTS_QEMU_PASS" $G19 + +echo "" +echo "================================================================================" +echo " QUALIFICATION SUMMARY: $GATES_PASSED / $GATES_TOTAL GATES PASSING" +echo "================================================================================" + +# Compute SHA-256 digests for qualification receipt +PHYSICS_BIN_SHA=$("$M3TOOL" sha256 "$OUT/physics.bin") +ATLAS_BIN_SHA=$("$M3TOOL" sha256 "$OUT/atlas_m3.bin") +CONTRACT_SHA=$("$M3TOOL" sha256 "$M3_DIR/machine_contract.json") +SPEC_SHA=$("$M3TOOL" sha256 "$ARCH_ROOT/docs/milestone-3-spec.md") +CAP_SRC_SHA=$("$M3TOOL" sha256 "$M3_DIR/capability.s") +BROKER_SRC_SHA=$("$M3TOOL" sha256 "$M3_DIR/effect_broker.s") +RECEIPT_SRC_SHA=$("$M3TOOL" sha256 "$M3_DIR/receipt_ledger.s") +M3TOOL_SRC_SHA=$("$M3TOOL" sha256 "$M3_DIR/tools/m3tool.c") +GATE_RUNNER_SHA=$("$M3TOOL" sha256 "$M3_DIR/tests/run_m3_gates.sh") +QEMU_VER=$(qemu-system-aarch64 --version | head -n 1) +GIT_COMMIT=$(cd "$PHYSICS_ROOT" && git rev-parse HEAD 2>/dev/null || echo "uncommitted") + +RECEIPT_FILE="$M3_DIR/m3_qualification_receipt.json" + +cat < "$RECEIPT_FILE" +{ + "milestone": "MILESTONE 3 — PHYSICS_EFFECTS", + "status": "QUALIFIED / QEMU PASS", + "contract_id": "CONTRACT-QEMU-VIRT-AARCH64-M3", + "generated_at": "$(date -u +'%Y-%m-%dT%H:%M:%SZ')", + "git_commit": "$GIT_COMMIT", + "qemu_version": "$QEMU_VER", + "qemu_flags": "-M virt -cpu cortex-a57 -m 128M -nographic -accel tcg,thread=single", + "artifacts": { + "physics.bin": { + "sha256": "$PHYSICS_BIN_SHA", + "size_bytes": 18432, + "max_budget_bytes": 65536 + }, + "atlas_m3.bin": { + "sha256": "$ATLAS_BIN_SHA", + "size_bytes": $(wc -c < "$OUT/atlas_m3.bin") + }, + "machine_contract.json": { + "sha256": "$CONTRACT_SHA" + }, + "milestone-3-spec.md": { + "sha256": "$SPEC_SHA" + }, + "capability.s": { + "sha256": "$CAP_SRC_SHA" + }, + "effect_broker.s": { + "sha256": "$BROKER_SRC_SHA" + }, + "receipt_ledger.s": { + "sha256": "$RECEIPT_SRC_SHA" + }, + "m3tool.c": { + "sha256": "$M3TOOL_SRC_SHA" + }, + "run_m3_gates.sh": { + "sha256": "$GATE_RUNNER_SHA" + } + }, + "accounting": { + "physics_elf_text_bytes": 10624, + "max_text_budget_bytes": 32768, + "text_budget_utilization_pct": 32.42, + "physics_bin_total_bytes": 18432, + "max_image_budget_bytes": 65536, + "image_budget_utilization_pct": 28.12 + }, + "qualification_gates": { + "total": 19, + "passed": 19, + "gates": { + "PHYSICS_CAP_LEDGER_PASS": "PASS", + "PHYSICS_CAP_ROOT_PRIVATE_PASS": "PASS", + "PHYSICS_CAP_ATTENUATION_PASS": "PASS", + "PHYSICS_CAP_NO_AMBIENT_AUTHORITY_PASS": "PASS", + "PHYSICS_CAP_GENERATION_PASS": "PASS", + "PHYSICS_CAP_REVOCATION_PASS": "PASS", + "PHYSICS_CAP_DESCENDANT_REVOCATION_PASS": "PASS", + "PHYSICS_EFFECT_ADMISSION_PASS": "PASS", + "PHYSICS_EFFECT_BOUNDS_PASS": "PASS", + "PHYSICS_EFFECT_FORGED_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_STALE_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_REVOKED_CAP_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_WRONG_PRINCIPAL_REFUSAL_PASS": "PASS", + "PHYSICS_EFFECT_REPLAY_PASS": "PASS", + "PHYSICS_EFFECT_REPLAY_CONFLICT_PASS": "PASS", + "PHYSICS_RECEIPT_IDENTITY_PASS": "PASS", + "PHYSICS_RECEIPT_CHAIN_PASS": "PASS", + "PHYSICS_RECEIPT_EXHAUSTION_PASS": "PASS", + "PHYSICS_EFFECTS_QEMU_PASS": "PASS" + } + } +} +EOF + +cp "$RECEIPT_FILE" "$OUT/m3_qualification_receipt.json" + +echo "Canonical qualification receipt emitted at:" +echo " $RECEIPT_FILE" +echo "" +echo "=== MILESTONE 3: ALL GATES PASS ===" diff --git a/m3/tests/run_sha256_kat.sh b/m3/tests/run_sha256_kat.sh new file mode 100755 index 0000000..b0991db --- /dev/null +++ b/m3/tests/run_sha256_kat.sh @@ -0,0 +1,92 @@ +#!/bin/sh +# m3/tests/run_sha256_kat.sh -- end-to-end SHA-256 KAT for m3/sha256_be.s. +# +# 1. Builds everything (m3/build.sh). +# 2. Cross-checks the host tool (m3tool sha256) against coreutils sha256sum. +# 3. Boots the bare-metal KAT in QEMU (SCTLR_EL1.A on), dumps RES and PAT by +# QMP pmemsave, and checks by STATE INSPECTION: every digest equals host +# sha256sum of the same bytes, every refusal returns its code and leaves +# its 0xA5-prefilled out buffer untouched. +# 4. Boots the fault-probe variant: a deliberate misaligned load must take a +# synchronous alignment fault (ESR EC 0x25, DFSC 0x21) and never finish. +# Exits 0 only if every check passes. Evidence lands in m3/build/kat/. +set -eu + +M3=$(cd "$(dirname "$0")/.." && pwd -P) +B=$M3/build +W=$B/kat +FAILS=0 + +"$M3/build.sh" +T=$B/m3tool +rm -rf "$W"; mkdir -p "$W/vec" +LOG=$W/results.txt; : > "$LOG" + +ok() { echo "PASS $*" | tee -a "$LOG"; } +bad() { echo "FAIL $*" | tee -a "$LOG"; FAILS=$((FAILS + 1)); } +check() { if [ "$2" = "$3" ]; then ok "$1 ($2)"; else bad "$1: got '$2' want '$3'"; fi; } +hsha() { sha256sum < "$1" | cut -d' ' -f1; } +pattern() { LC_ALL=C awk -v n="$1" 'BEGIN { for (i = 0; i < n; i++) printf "%c", i % 256 }'; } + +# ---- host vectors --------------------------------------------------------- +: > "$W/vec/0_empty" +printf 'abc' > "$W/vec/1_abc" +printf 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' > "$W/vec/2_nist56" +pattern 1000 > "$W/vec/3_pat1000" +pattern 65536 > "$W/vec/4_pat65536" +check "host vector nist56 length" "$(wc -c < "$W/vec/2_nist56" | tr -d ' ')" 56 +check "host vector pat65536 length" "$(wc -c < "$W/vec/4_pat65536" | tr -d ' ')" 65536 + +# ---- m3tool sha256 vs sha256sum ------------------------------------------- +for n in 55 56 63 64 65 119 120 200000; do pattern "$n" > "$W/vec/x_pat$n"; done +for f in "$W"/vec/* "$B/kat.bin" "$B/m3tool" "$M3/../sha256_clean.c"; do + check "m3tool sha256 $(basename "$f") ($(wc -c < "$f" | tr -d ' ') B)" "$("$T" sha256 "$f")" "$(hsha "$f")" +done +check "known abc digest" "$(hsha "$W/vec/1_abc")" ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad + +# ---- QEMU: main KAT ------------------------------------------------------- +RES=0x40280000 +"$M3/tools/qemu_inspect.sh" --out "$W/run" --marker 'M3_KAT_DONE' --fail-marker 'M3_KAT_FAULT' \ + --timeout 20 --bios "$B/kat_stub.bin" --loader "$B/kat.bin@0x40200000" \ + --region "res:$RES:0x240" --region "pat:0x40290000:0x10000" \ + || { bad "qemu_inspect main KAT (rc=$?)"; echo "RESULT: FAIL ($FAILS)"; exit 1; } +R=$W/run/res.bin +check "RES magic" "$("$T" hexfield "$R" 0 8)" 0x534552544b41334d +check "guest PAT == host pattern" "$(hsha "$W/run/pat.bin")" "$(hsha "$W/vec/4_pat65536")" +sctlr=$("$T" hexfield "$R" 8 8) +check "SCTLR_EL1.A set" "$(( sctlr & 2 ))" 2 +check "CurrentEL == EL1" "$("$T" hexfield "$R" 16 8)" 0x0000000000000004 +i=0 +for v in 0_empty 1_abc 2_nist56 3_pat1000 4_pat65536; do + check "vector $i rc" "$("$T" hexfield "$R" $((0x20 + 4 * i)) 4)" 0x00000000 + check "vector $i $v digest == sha256sum" "$("$T" bytes "$R" $((0x80 + 32 * i)) 32)" "$(hsha "$W/vec/$v")" + i=$((i + 1)) +done +check "refusal len=SHA256_MAX_LEN+1 rc" "$("$T" hexfield "$R" $((0x20 + 4 * 5)) 4)" 0x00000001 +check "refusal misaligned data rc" "$("$T" hexfield "$R" $((0x20 + 4 * 6)) 4)" 0x00000002 +check "refusal misaligned out rc" "$("$T" hexfield "$R" $((0x20 + 4 * 7)) 4)" 0x00000003 +check "refusal data+len wrap rc" "$("$T" hexfield "$R" $((0x20 + 4 * 8)) 4)" 0x00000004 +a5=$(printf 'a5%.0s' $(seq 1 $((0x200 - 0x140)))) +if [ "$("$T" bytes "$R" $((0x140)) $((0x200 - 0x140)))" = "$a5" ]; then + ok "refusal out buffers untouched (0x140..0x200: 192 B, all 0xA5)" +else bad "refusal out buffers modified (0x140..0x200 not all 0xA5)"; fi +check "no fault recorded" "$("$T" hexfield "$R" $((0x200)) 8)" 0x0000000000000000 + +# ---- QEMU: alignment fault probe ----------------------------------------- +if "$M3/tools/qemu_inspect.sh" --out "$W/fault" --marker 'M3_KAT_FAULT' --fail-marker 'M3_KAT_DONE' \ + --timeout 20 --bios "$B/kat_stub.bin" --loader "$B/kat_fault.bin@0x40200000" \ + --region "res:$RES:0x240"; then + F=$W/fault/res.bin + check "fault probe: fault magic" "$("$T" hexfield "$F" $((0x200)) 8)" 0x21544c554146334d + esr=$("$T" hexfield "$F" $((0x208)) 8) + check "fault probe: ESR EC = 0x25 (data abort, same EL)" "$(( (esr >> 26) & 0x3f ))" 37 + check "fault probe: DFSC = 0x21 (alignment fault)" "$(( esr & 0x3f ))" 33 + check "fault probe: FAR = PAT+1" "$("$T" hexfield "$F" $((0x218)) 8)" 0x0000000040290001 + check "fault probe: vector = current EL SPx sync (0x200)" "$("$T" hexfield "$F" $((0x220)) 8)" 0x0000000000000200 + check "fault probe: run never completed (no RES magic)" "$("$T" hexfield "$F" 0 8)" 0x0000000000000000 +else + bad "qemu_inspect fault probe (rc=$?)" +fi + +if [ "$FAILS" -eq 0 ]; then echo "RESULT: PASS" | tee -a "$LOG"; exit 0; fi +echo "RESULT: FAIL ($FAILS)" | tee -a "$LOG"; exit 1 diff --git a/m3/tests/sha256_kat.s b/m3/tests/sha256_kat.s new file mode 100644 index 0000000..767a745 --- /dev/null +++ b/m3/tests/sha256_kat.s @@ -0,0 +1,163 @@ +// sha256_kat.s -- bare-metal SHA-256 known-answer test for m3/sha256_be.s. +// +// Loaded raw at 0x40200000 (entered from tests/kat_stub.s in flash) on +// qemu-system-aarch64 -M virt -cpu cortex-a57, EL1, MMU off. +// Enables SCTLR_EL1.A, runs the vectors below, writes results to RES, prints +// "M3_KAT_DONE" (or "M3_KAT_FAULT" from any exception) on the PL011, then wfe. +// The host checks results by pmemsave state inspection, not by UART text. +// +// RES layout (0x40280000, 0x240 bytes, all little-endian): +// +0x000 u64 magic "M3KATRES" (written last; absent => incomplete run) +// +0x008 u64 SCTLR_EL1 read back after enabling A (bit 1) +// +0x010 u64 CurrentEL +// +0x020 u32 rc[9] return code of each call below +// +0x080 u8 digest[5][32] vectors 0..4 (canonical big-endian bytes) +// +0x140 u8 out buffers of refusal vectors 5..8, 0x30 B each, prefilled 0xA5 +// +0x200 u64 fault magic "M3FAULT!", ESR_EL1, ELR_EL1, FAR_EL1, vector offset +// Vectors: +// 0 "" 1 "abc" +// 2 NIST 56-byte message 3 PAT[0..1000) +// 4 PAT[0..SHA256_MAX_LEN) 5 len = SHA256_MAX_LEN+1 -> rc 1 +// 6 data = "abc"+1 (misalign) -> rc 2 7 out misaligned -> rc 3 +// 8 data+len wraps -> rc 4 +// PAT (0x40290000, 0x10000 bytes): byte i = i & 0xff. + + .equ RES, 0x40280000 + .equ PAT, 0x40290000 + .equ PAT_LEN, 0x10000 + .equ STACK_TOP, 0x40270000 + .equ UART_DR, 0x09000000 + // SHA256_MAX_LEN is the absolute symbol exported by m3/sha256_be.s; + // kat.ld asserts it fits in PAT. + + .equ R_MAGIC, 0x000 + .equ R_SCTLR, 0x008 + .equ R_EL, 0x010 + .equ R_RC, 0x020 + .equ R_DIG, 0x080 + .equ R_REF, 0x140 + .equ R_REFSZ, 0x30 + .equ R_FAULT, 0x200 + .equ R_END, 0x240 + + .section .text.boot, "ax", %progbits + .global _start +_start: + msr daifset, #0xf + ldr x0, =STACK_TOP + mov sp, x0 + adr x0, vectors + msr vbar_el1, x0 + isb + mrs x0, sctlr_el1 + orr x0, x0, #(1 << 1) // SCTLR_EL1.A: alignment checking + msr sctlr_el1, x0 + isb + + // Clear RES, then prefill the refusal out buffers with 0xA5. + ldr x20, =RES + mov x0, x20 + mov x1, #(R_END / 8) +1: str xzr, [x0], #8 + subs x1, x1, #1 + b.ne 1b + add x0, x20, #R_REF + mov x1, #((R_FAULT - R_REF) / 8) + ldr x2, =0xa5a5a5a5a5a5a5a5 +2: str x2, [x0], #8 + subs x1, x1, #1 + b.ne 2b + + mrs x0, sctlr_el1 + str x0, [x20, #R_SCTLR] + mrs x0, CurrentEL + str x0, [x20, #R_EL] + + // PAT[i] = i & 0xff + ldr x0, =PAT + mov x1, #0 +3: strb w1, [x0, x1] + add x1, x1, #1 + cmp x1, #PAT_LEN + b.ne 3b + +.ifdef KAT_FAULT_PROBE + // Deliberate misaligned load: must trap to the vector table. + ldr x0, =PAT + 1 + ldr w0, [x0] +.endif + + .macro KAT idx, data, len, out + ldr x0, =\data + ldr x1, =\len + ldr x2, =\out + bl sha256_be + str w0, [x20, #(R_RC + 4 * \idx)] + .endm + + KAT 0, msg_empty, 0, RES + R_DIG + 0 * 32 + KAT 1, msg_abc, 3, RES + R_DIG + 1 * 32 + KAT 2, msg_nist, 56, RES + R_DIG + 2 * 32 + KAT 3, PAT, 1000, RES + R_DIG + 3 * 32 + KAT 4, PAT, SHA256_MAX_LEN, RES + R_DIG + 4 * 32 + KAT 5, PAT, SHA256_MAX_LEN+1, RES + R_REF + 0 * R_REFSZ + KAT 6, msg_abc+1, 3, RES + R_REF + 1 * R_REFSZ + KAT 7, msg_abc, 3, RES + R_REF + 2 * R_REFSZ + 1 + KAT 8, 0xffffffffffffff00, 0x200, RES + R_REF + 3 * R_REFSZ + + ldr x0, =0x534552544b41334d // "M3KATRES" + str x0, [x20, #R_MAGIC] + dsb sy + adr x0, str_done + bl puts +halt: wfe + b halt + +// puts(x0 = NUL-terminated string) -- byte writes to the PL011 data register. +puts: ldr x1, =UART_DR +1: ldrb w2, [x0], #1 + cbz w2, 2f + strb w2, [x1] + b 1b +2: ret + +fault_common: // x9 = vector offset + ldr x20, =RES + R_FAULT + ldr x0, =0x21544c554146334d // "M3FAULT!" + str x0, [x20, #0] + mrs x0, esr_el1 + str x0, [x20, #8] + mrs x0, elr_el1 + str x0, [x20, #16] + mrs x0, far_el1 + str x0, [x20, #24] + str x9, [x20, #32] + dsb sy + adr x0, str_fault + bl puts + b halt + + .ltorg + + .balign 2048 +vectors: + .irp n, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + .balign 0x80 + mov x9, #(\n * 0x80) + b fault_common + .endr + + .section .rodata, "a", %progbits + .balign 8 +msg_empty: + .balign 4 +msg_abc: + .ascii "abc" + .balign 4 +msg_nist: + .ascii "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + .balign 4 +str_done: + .asciz "M3_KAT_DONE\n" +str_fault: + .asciz "M3_KAT_FAULT\n" diff --git a/m3/tests/test_m3_authority.s b/m3/tests/test_m3_authority.s new file mode 100644 index 0000000..a1dc957 --- /dev/null +++ b/m3/tests/test_m3_authority.s @@ -0,0 +1,843 @@ +/* + * m3/tests/test_m3_authority.s - Bare-Metal Dynamic Qualification Test Suite + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Runs end-to-end derivation, attenuation, execution, rejection, revocation, + * receipt chaining, replay idempotency, replay conflict, and adversarial matrix. + * Records results in PHYSICS_BOOT_STATE + 0x200 (0x40208A00). + */ + + .global run_m3_unit_tests + + .extern submit_effect + .extern derive_capability + .extern revoke_capability + .extern lookup_capability + .extern get_receipt_count + .extern print_string + + .section .text, "ax" + .balign 4 + + .equ TEST_RECORD_BASE, 0x40208A00 + .equ TEST_MAGIC, 0x4D33544553545355 /* 'M3TESTSU' */ + + .equ RES_UNIVERSAL_ROOT, 1 + .equ RES_MEMORY_FRAME, 2 + .equ RES_CONSOLE, 3 + .equ RES_MEASUREMENT, 4 + + .equ OP_FRAME_GRANT, 1 + .equ OP_FRAME_RELEASE, 2 + .equ OP_CONSOLE_WRITE, 1 + .equ OP_MEASUREMENT_READ, 1 + + .equ DEC_ADMITTED, 1 + .equ DEC_REJECTED_STRUCTURAL, 2 + .equ DEC_REJECTED_UNKNOWN_CAP, 3 + .equ DEC_REJECTED_STALE_GENERATION, 4 + .equ DEC_REJECTED_REVOKED, 5 + .equ DEC_REJECTED_WRONG_PRINCIPAL, 6 + .equ DEC_REJECTED_OPERATION, 7 + .equ DEC_REJECTED_RESOURCE, 8 + .equ DEC_REJECTED_BOUNDS, 9 + .equ DEC_REJECTED_CONSTRAINT, 10 + .equ DEC_REJECTED_REPLAY_CONFLICT, 11 + .equ DEC_REJECTED_EXHAUSTED, 12 + .equ DEC_EXECUTION_FAILED, 13 + +/* + * run_m3_unit_tests(): + * Executes all M3 test scenarios. + */ +run_m3_unit_tests: + stp x29, x30, [sp, #-160]! + mov x29, sp + stp x19, x20, [sp, #16] + stp x21, x22, [sp, #32] + stp x23, x24, [sp, #48] + + /* x19 = pass count, x20 = test count, x21 = bitmask */ + mov x19, #0 + mov x20, #0 + mov x21, #0 + + adr x0, msg_unit_tests_start + bl print_string + + /* ------------------------------------------------------------- + * TEST 1: End-to-End Scenario — Derive Memory Cap A from CAP_ROOT + * Parent: Slot 0, Gen 1, caller=1. Child: principal=2, rtype=FRAME, + * ops=FRAME_GRANT, bounds=[0x40210000, 0x40220000) (64 KiB), lifetime=0. + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #0 /* parent_slot = 0 */ + mov w1, #1 /* parent_gen = 1 */ + mov x2, #1 /* caller_principal = 1 */ + mov x3, #2 /* child_principal = 2 */ + mov w4, #RES_MEMORY_FRAME /* rtype */ + mov w5, #OP_FRAME_GRANT /* ops */ + ldr x6, =0x40210000 /* base */ + ldr x7, =0x00010000 /* size = 64 KiB */ + mov x8, #0 /* lifetime = 0 */ + bl derive_capability + /* Expected: Cap A in Slot 1, Gen 1 -> (1 << 32) | 1 */ + ldr x1, =(1 << 32) | 1 + cmp x0, x1 + b.ne 1f + add x19, x19, #1 + orr x21, x21, #(1 << 0) +1: + mov x22, x0 /* x22 = Cap A handle */ + + /* ------------------------------------------------------------- + * TEST 2: Valid Intent on Cap A -> ADMITTED, Frame Granted + * Intent: req_id=101, principal=2, slot=1, gen=1, rtype=FRAME, + * op=FRAME_GRANT, target=0x40210000, size=4096. + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 /* Intent buffer (64 bytes) */ + mov w1, #1 + strh w1, [x0, #0] /* version = 1 */ + mov w1, #64 + strh w1, [x0, #2] /* length = 64 */ + str wzr, [x0, #4] /* reserved0 = 0 */ + mov x1, #101 + str x1, [x0, #8] /* request_id = 101 */ + mov x1, #2 + str x1, [x0, #16] /* principal_id = 2 */ + mov w1, #1 + str w1, [x0, #24] /* slot = 1 */ + mov w1, #1 + str w1, [x0, #28] /* generation = 1 */ + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] /* rtype */ + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] /* op */ + ldr x1, =0x40210000 + str x1, [x0, #40] /* target_base = 0x40210000 */ + mov x1, #4096 + str x1, [x0, #48] /* target_size = 4096 */ + str xzr, [x0, #56] /* reserved1 */ + + bl submit_effect + cbz x0, 2f + ldr w1, [x0, #4] /* decision */ + cmp w1, #DEC_ADMITTED + b.ne 2f + ldr x2, [x0, #0x38] /* actual_effect */ + ldr x3, =0x40210000 + cmp x2, x3 + b.ne 2f + add x19, x19, #1 + orr x21, x21, #(1 << 1) +2: + + /* ------------------------------------------------------------- + * TEST 3: Derive Cap B from Cap A (Strict subset) + * Parent: Slot 1, Gen 1, caller=2. Child: principal=3, rtype=FRAME, + * ops=FRAME_GRANT, bounds=[0x40210000, 0x40218000) (32 KiB, subset). + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #1 /* parent_slot = 1 */ + mov w1, #1 /* parent_gen = 1 */ + mov x2, #2 /* caller_principal = 2 */ + mov x3, #3 /* child_principal = 3 */ + mov w4, #RES_MEMORY_FRAME + mov w5, #OP_FRAME_GRANT + ldr x6, =0x40210000 + ldr x7, =0x00008000 /* size = 32 KiB */ + mov x8, #0 + bl derive_capability + /* Expected: Cap B in Slot 2, Gen 1 */ + ldr x1, =(1 << 32) | 2 + cmp x0, x1 + b.ne 3f + add x19, x19, #1 + orr x21, x21, #(1 << 2) +3: + mov x23, x0 /* x23 = Cap B handle */ + + /* ------------------------------------------------------------- + * TEST 4: Intent on Cap B Targeting Outside B's Bounds -> REJECTED_BOUNDS + * Target: 0x40219000 (outside [0x40210000, 0x40218000)) + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #102 + str x1, [x0, #8] /* request_id = 102 */ + mov x1, #3 + str x1, [x0, #16] /* principal_id = 3 */ + mov w1, #2 + str w1, [x0, #24] /* slot = 2 */ + mov w1, #1 + str w1, [x0, #28] /* generation = 1 */ + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40219000 /* OUTSIDE B */ + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 4f + ldr w1, [x0, #4] /* decision */ + cmp w1, #DEC_REJECTED_BOUNDS + b.ne 4f + add x19, x19, #1 + orr x21, x21, #(1 << 3) +4: + + /* ------------------------------------------------------------- + * TEST 5: Revoke Cap A (Slot 1) + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #1 /* slot = 1 */ + mov w1, #1 /* gen = 1 */ + mov x2, #2 /* principal = 2 */ + bl revoke_capability + cmp x0, #1 + b.ne 5f + add x19, x19, #1 + orr x21, x21, #(1 << 4) +5: + + /* ------------------------------------------------------------- + * TEST 6: Exercising Directly Revoked Cap A -> REJECTED_REVOKED + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #103 + str x1, [x0, #8] + mov x1, #2 + str x1, [x0, #16] + mov w1, #1 + str w1, [x0, #24] /* slot = 1 (revoked) */ + mov w1, #1 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40211000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 6f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_REVOKED + b.ne 6f + add x19, x19, #1 + orr x21, x21, #(1 << 5) +6: + + /* ------------------------------------------------------------- + * TEST 7: Exercising Descendant Cap B (Parent A Revoked) -> REJECTED_REVOKED + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #104 + str x1, [x0, #8] + mov x1, #3 + str x1, [x0, #16] + mov w1, #2 + str w1, [x0, #24] /* slot = 2 (descendant of 1) */ + mov w1, #1 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 7f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_REVOKED + b.ne 7f + add x19, x19, #1 + orr x21, x21, #(1 << 6) +7: + + /* ------------------------------------------------------------- + * TEST 8: Adversarial Matrix — Forged Slot (Slot 15 unallocated) + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #105 + str x1, [x0, #8] + mov x1, #2 + str x1, [x0, #16] + mov w1, #15 /* Forged slot */ + str w1, [x0, #24] + mov w1, #1 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 8f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_UNKNOWN_CAP + b.ne 8f + add x19, x19, #1 + orr x21, x21, #(1 << 7) +8: + + /* ------------------------------------------------------------- + * TEST 9: Adversarial Matrix — Out-of-Range Slot (Slot 40 >= 32) + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #106 + str x1, [x0, #8] + mov x1, #2 + str x1, [x0, #16] + mov w1, #40 /* Out of range */ + str w1, [x0, #24] + mov w1, #1 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 9f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_UNKNOWN_CAP + b.ne 9f + add x19, x19, #1 + orr x21, x21, #(1 << 8) +9: + + /* ------------------------------------------------------------- + * TEST 10: Adversarial Matrix — Stale Generation + * Re-allocate slot 1. Slot 1 becomes gen 2. Present gen 1. + * ------------------------------------------------------------- */ + add x20, x20, #1 + /* Derive fresh cap in slot 1 from root */ + mov w0, #0 + mov w1, #1 + mov x2, #1 + mov x3, #4 /* principal 4 */ + mov w4, #RES_MEMORY_FRAME + mov w5, #OP_FRAME_GRANT + ldr x6, =0x40210000 + ldr x7, =0x00010000 + mov x8, #0 + bl derive_capability + /* Should be slot 1, gen 2 -> (2 << 32) | 1 */ + ldr x1, =(2 << 32) | 1 + cmp x0, x1 + b.ne 10f + + /* Now submit intent presenting gen 1 (stale) */ + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #107 + str x1, [x0, #8] + mov x1, #4 + str x1, [x0, #16] + mov w1, #1 /* slot 1 */ + str w1, [x0, #24] + mov w1, #1 /* STALE generation 1 (current is 2) */ + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 10f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_STALE_GENERATION + b.ne 10f + add x19, x19, #1 + orr x21, x21, #(1 << 9) +10: + + /* ------------------------------------------------------------- + * TEST 11: Adversarial Matrix — Wrong Principal + * Slot 1 gen 2 owned by principal 4. Caller presents principal 99. + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #108 + str x1, [x0, #8] + mov x1, #99 /* Wrong principal */ + str x1, [x0, #16] + mov w1, #1 + str w1, [x0, #24] + mov w1, #2 /* Current gen = 2 */ + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 11f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_WRONG_PRINCIPAL + b.ne 11f + add x19, x19, #1 + orr x21, x21, #(1 << 10) +11: + + /* ------------------------------------------------------------- + * TEST 12: Adversarial Matrix — Forbidden Operation + * Slot 1 gen 2 allows only FRAME_GRANT. Request FRAME_RELEASE. + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #109 + str x1, [x0, #8] + mov x1, #4 /* correct principal */ + str x1, [x0, #16] + mov w1, #1 + str w1, [x0, #24] + mov w1, #2 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_RELEASE /* Forbidden operation */ + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 12f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_OPERATION + b.ne 12f + add x19, x19, #1 + orr x21, x21, #(1 << 11) +12: + + /* ------------------------------------------------------------- + * TEST 13: Adversarial Matrix — Wrong Resource Type + * Slot 1 gen 2 is RES_MEMORY_FRAME. Request RES_CONSOLE. + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #110 + str x1, [x0, #8] + mov x1, #4 + str x1, [x0, #16] + mov w1, #1 + str w1, [x0, #24] + mov w1, #2 + str w1, [x0, #28] + mov w1, #RES_CONSOLE /* Wrong resource */ + str w1, [x0, #32] + mov w1, #OP_CONSOLE_WRITE + str w1, [x0, #36] + ldr x1, =0x09000000 + str x1, [x0, #40] + mov x1, #1 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 13f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_RESOURCE + b.ne 13f + add x19, x19, #1 + orr x21, x21, #(1 << 12) +13: + + /* ------------------------------------------------------------- + * TEST 14: Adversarial Matrix — Child Rights Widening Refused + * Parent has only FRAME_GRANT. Attempt to derive child with FRAME_RELEASE. + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #1 /* parent_slot = 1 (gen 2) */ + mov w1, #2 + mov x2, #4 /* caller = 4 */ + mov x3, #5 /* child = 5 */ + mov w4, #RES_MEMORY_FRAME + mov w5, #(OP_FRAME_GRANT | OP_FRAME_RELEASE) /* Widened ops! */ + ldr x6, =0x40210000 + ldr x7, =0x00010000 + mov x8, #0 + bl derive_capability + /* Must return 0 (refusal) */ + cbnz x0, 14f + add x19, x19, #1 + orr x21, x21, #(1 << 13) +14: + + /* ------------------------------------------------------------- + * TEST 15: Adversarial Matrix — Child Bounds Widening Refused + * Parent bound is [0x40210000, 0x40220000). Child requests 0x40230000 end. + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #1 + mov w1, #2 + mov x2, #4 + mov x3, #5 + mov w4, #RES_MEMORY_FRAME + mov w5, #OP_FRAME_GRANT + ldr x6, =0x40210000 + ldr x7, =0x00020000 /* Widened bound: 128 KiB > 64 KiB! */ + mov x8, #0 + bl derive_capability + cbnz x0, 15f + add x19, x19, #1 + orr x21, x21, #(1 << 14) +15: + + /* ------------------------------------------------------------- + * TEST 16: Adversarial Matrix — Integer Overflow in Child Bounds Refused + * base = 0xFFFFFFFFFFFFF000, size = 0x2000 (wraps around 0) + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #1 + mov w1, #2 + mov x2, #4 + mov x3, #5 + mov w4, #RES_MEMORY_FRAME + mov w5, #OP_FRAME_GRANT + ldr x6, =0xFFFFFFFFFFFFF000 + ldr x7, =0x2000 + mov x8, #0 + bl derive_capability + cbnz x0, 16f + add x19, x19, #1 + orr x21, x21, #(1 << 15) +16: + + /* ------------------------------------------------------------- + * TEST 17: Adversarial Matrix — Root Capability Export Attempt + * External principal (principal 9) attempts to exercise slot 0. + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #111 + str x1, [x0, #8] + mov x1, #9 /* External caller */ + str x1, [x0, #16] + mov w1, #0 /* CAP_ROOT slot 0 */ + str w1, [x0, #24] + mov w1, #1 + str w1, [x0, #28] + mov w1, #RES_UNIVERSAL_ROOT + str w1, [x0, #32] + mov w1, #1 + str w1, [x0, #36] + ldr x1, =0x40210000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 17f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_WRONG_PRINCIPAL + b.ne 17f + add x19, x19, #1 + orr x21, x21, #(1 << 16) +17: + + /* ------------------------------------------------------------- + * TEST 18: Replay Semantics — Identical Intent Returns Cached Result + * Submit intent with request_id=200 on slot 1 gen 2 (principal 4). + * Then submit duplicate request_id=200 with identical intent. + * ------------------------------------------------------------- */ + add x20, x20, #1 + /* First submission */ + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #200 + str x1, [x0, #8] /* request_id = 200 */ + mov x1, #4 /* principal 4 */ + str x1, [x0, #16] + mov w1, #1 /* slot 1 */ + str w1, [x0, #24] + mov w1, #2 /* gen 2 */ + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40212000 + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 18f + mov x24, x0 /* x24 = first receipt */ + + /* Second submission (identical request_id 200 and intent) */ + add x0, sp, #64 + bl submit_effect + cmp x0, x24 /* Must return identical receipt pointer! */ + b.ne 18f + add x19, x19, #1 + orr x21, x21, #(1 << 17) +18: + + /* ------------------------------------------------------------- + * TEST 19: Replay Semantics — Replay Conflict (Same ID, altered intent) + * Submit request_id=200 with target=0x40213000 (altered!) + * ------------------------------------------------------------- */ + add x20, x20, #1 + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #200 /* Same request_id = 200 */ + str x1, [x0, #8] + mov x1, #4 + str x1, [x0, #16] + mov w1, #1 + str w1, [x0, #24] + mov w1, #2 + str w1, [x0, #28] + mov w1, #RES_MEMORY_FRAME + str w1, [x0, #32] + mov w1, #OP_FRAME_GRANT + str w1, [x0, #36] + ldr x1, =0x40213000 /* ALTERED TARGET! */ + str x1, [x0, #40] + mov x1, #4096 + str x1, [x0, #48] + str xzr, [x0, #56] + + bl submit_effect + cbz x0, 19f + ldr w1, [x0, #4] + cmp w1, #DEC_REJECTED_REPLAY_CONFLICT + b.ne 19f + add x19, x19, #1 + orr x21, x21, #(1 << 18) +19: + + /* ------------------------------------------------------------- + * TEST 20: Console Resource Test (CONSOLE_WRITE to UART) + * Derive Console Cap C (slot 3) from CAP_ROOT (caller 1, child 6) + * Submit CONSOLE_WRITE intent. + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #0 /* parent = 0 */ + mov w1, #1 + mov x2, #1 + mov x3, #6 /* child = 6 */ + mov w4, #RES_CONSOLE + mov w5, #OP_CONSOLE_WRITE + ldr x6, =0x09000000 /* base = UART MMIO */ + ldr x7, =0x1000 /* size = 4 KiB */ + mov x8, #0 + bl derive_capability + cbz x0, 20f + + /* Submit CONSOLE_WRITE intent with character '*' (0x2A) */ + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #300 + str x1, [x0, #8] + mov x1, #6 /* principal 6 */ + str x1, [x0, #16] + mov w1, #3 /* slot 3 */ + str w1, [x0, #24] + mov w1, #1 /* gen 1 */ + str w1, [x0, #28] + mov w1, #RES_CONSOLE + str w1, [x0, #32] + mov w1, #OP_CONSOLE_WRITE + str w1, [x0, #36] + ldr x1, =0x09000000 + str x1, [x0, #40] + mov x1, #1 + str x1, [x0, #48] + mov x1, #0x2A /* character '*' in param0 (+0x38 = 56) */ + str x1, [x0, #56] + + bl submit_effect + cbz x0, 20f + ldr w1, [x0, #4] + cmp w1, #DEC_ADMITTED + b.ne 20f + add x19, x19, #1 + orr x21, x21, #(1 << 19) +20: + + /* ------------------------------------------------------------- + * TEST 21: Measurement Resource Test (MEASUREMENT_READ) + * Derive Measurement Cap D (slot 4) from CAP_ROOT (caller 1, child 7) + * Submit MEASUREMENT_READ intent. + * ------------------------------------------------------------- */ + add x20, x20, #1 + mov w0, #0 + mov w1, #1 + mov x2, #1 + mov x3, #7 /* child 7 */ + mov w4, #RES_MEASUREMENT + mov w5, #OP_MEASUREMENT_READ + mov x6, #0 + mov x7, #0 + mov x8, #0 + bl derive_capability + cbz x0, 21f + + add x0, sp, #64 + mov w1, #1 + strh w1, [x0, #0] + mov w1, #64 + strh w1, [x0, #2] + str wzr, [x0, #4] + mov x1, #400 + str x1, [x0, #8] + mov x1, #7 + str x1, [x0, #16] + mov w1, #4 /* slot 4 */ + str w1, [x0, #24] + mov w1, #1 /* gen 1 */ + str w1, [x0, #28] + mov w1, #RES_MEASUREMENT + str w1, [x0, #32] + mov w1, #OP_MEASUREMENT_READ + str w1, [x0, #36] + mov x1, #0 + str x1, [x0, #40] + str x1, [x0, #48] + str x1, [x0, #56] + + bl submit_effect + cbz x0, 21f + ldr w1, [x0, #4] + cmp w1, #DEC_ADMITTED + b.ne 21f + add x19, x19, #1 + orr x21, x21, #(1 << 20) +21: + + /* ------------------------------------------------------------- + * Write Test Summary Record to PHYSICS_BOOT_STATE + 0x200 (0x40208A00) + * +0x00: magic (0x4D33544553545355 'M3TESTSU') + * +0x08: tests_run (u64) + * +0x10: tests_passed (u64) + * +0x18: bitmask_results (u64) + * ------------------------------------------------------------- */ + ldr x0, =TEST_RECORD_BASE + ldr x1, =TEST_MAGIC + str x1, [x0, #0] + str x20, [x0, #8] /* tests_run */ + str x19, [x0, #16] /* tests_passed */ + str x21, [x0, #24] /* bitmask_results */ + + /* Emit telemetry */ + adr x0, msg_unit_tests_done + bl print_string + + ldp x23, x24, [sp, #48] + ldp x21, x22, [sp, #32] + ldp x19, x20, [sp, #16] + ldp x29, x30, [sp], #160 + ret + + .section .rodata + .balign 8 +msg_unit_tests_start: + .asciz "PHYSICS: M3_TESTS_START\n" +msg_unit_tests_done: + .asciz "PHYSICS: M3_TESTS_DONE\n" diff --git a/m3/tools/m3tool.c b/m3/tools/m3tool.c new file mode 100644 index 0000000..99fc12d --- /dev/null +++ b/m3/tools/m3tool.c @@ -0,0 +1,260 @@ +/* + * m3tool -- PHYSICS M3 host tool (no Python). Host: aarch64 little-endian. + * + * Built by m3/build.sh with the host gcc, linking the repo-root + * sha256_clean.c (the same audited source the image uses). Results must + * still be cross-checked against coreutils sha256sum so a bug shared by the + * image and this tool cannot hide. + * + * Subcommands: + * sha256 canonical SHA-256 hex (as sha256sum) + * hexfield LE unsigned field, width 1/2/4/8 -> 0x... + * bytes raw bytes as contiguous lowercase hex + * + * Adding a subcommand: write `static int cmd_x(int argc, char **argv)` (argv[0] + * is the subcommand name; return 0 on success, nonzero on failure) and add a + * row to CMDS[]. Shared helpers: read_file(), parse_u64(), sha256_file_buf(), + * put_hex(). Every failure prints "m3tool: ..." to stderr and exits nonzero. + */ +#include +#include +#include +#include +#include + +/* From the repo-root sha256_clean.c. data must be 4-byte aligned. */ +void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], + uint32_t out_digest[8]); + +#define M3TOOL_MAX_FILE (1ull << 30) /* 1 GiB: keeps len*8 far from overflow */ + +#include + +static int fail(const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "m3tool: "); + vfprintf(stderr, fmt, ap); + fputc('\n', stderr); + va_end(ap); + return 1; +} + +/* Read a whole file into a malloc'd (16-byte aligned) buffer. */ +static int read_file(const char *path, uint8_t **out, uint64_t *out_len) { + FILE *f = fopen(path, "rb"); + if (!f) return fail("cannot open %s", path); + uint64_t cap = 65536, len = 0; + uint8_t *buf = malloc(cap); + if (!buf) { fclose(f); return fail("out of memory reading %s", path); } + for (;;) { + if (len == cap) { + if (cap >= M3TOOL_MAX_FILE) { free(buf); fclose(f); return fail("file too large: %s", path); } + uint8_t *nb = realloc(buf, cap * 2); + if (!nb) { free(buf); fclose(f); return fail("out of memory reading %s", path); } + buf = nb; cap *= 2; + } + size_t n = fread(buf + len, 1, cap - len, f); + len += n; + if (n == 0) break; + } + int err = ferror(f); + fclose(f); + if (err) { free(buf); return fail("read error on %s", path); } + *out = buf; *out_len = len; + return 0; +} + +/* Strict unsigned parse: decimal or 0x hex, whole string, no sign. */ +static int parse_u64(const char *s, uint64_t *v) { + if (!s || !*s || *s == '-' || *s == '+' || *s == ' ') return fail("bad number: %s", s ? s : "(null)"); + char *end; errno = 0; + unsigned long long x = strtoull(s, &end, 0); + if (errno || *end) return fail("bad number: %s", s); + *v = x; + return 0; +} + +static void put_hex(const uint8_t *p, uint64_t n) { + for (uint64_t i = 0; i < n; i++) printf("%02x", p[i]); +} + +/* Canonical big-endian digest bytes. */ +static void sha256_file_buf(const uint8_t *buf, uint64_t len, uint8_t out[32]) { + uint8_t scratch[128] __attribute__((aligned(16))); + uint32_t st[8]; + sha256_compute(buf, len, scratch, st); + for (int i = 0; i < 8; i++) { + out[4 * i + 0] = (uint8_t)(st[i] >> 24); + out[4 * i + 1] = (uint8_t)(st[i] >> 16); + out[4 * i + 2] = (uint8_t)(st[i] >> 8); + out[4 * i + 3] = (uint8_t)(st[i]); + } +} + +/* Load file and bounds-check [off, off+n) without overflow. */ +static int load_range(const char *path, const char *off_s, uint64_t n, + uint8_t **buf, uint64_t *off) { + uint64_t len; + if (parse_u64(off_s, off)) return 1; + if (read_file(path, buf, &len)) return 1; + if (*off > len || n > len - *off) { free(*buf); return fail("range outside file %s", path); } + return 0; +} + +static int cmd_sha256(int argc, char **argv) { + if (argc != 2) return fail("usage: %s ", argv[0]); + uint8_t *buf; uint64_t len; uint8_t d[32]; + if (read_file(argv[1], &buf, &len)) return 1; + sha256_file_buf(buf, len, d); + free(buf); + put_hex(d, 32); putchar('\n'); + return 0; +} + +static int cmd_hexfield(int argc, char **argv) { + if (argc != 4) return fail("usage: %s ", argv[0]); + uint64_t w, off, v = 0; uint8_t *buf; + if (parse_u64(argv[3], &w)) return 1; + if (w != 1 && w != 2 && w != 4 && w != 8) return fail("width must be 1, 2, 4 or 8: %s", argv[3]); + if (load_range(argv[1], argv[2], w, &buf, &off)) return 1; + for (uint64_t i = 0; i < w; i++) v |= (uint64_t)buf[off + i] << (8 * i); + free(buf); + printf("0x%0*llx\n", (int)(2 * w), (unsigned long long)v); + return 0; +} + +static int cmd_bytes(int argc, char **argv) { + if (argc != 4) return fail("usage: %s ", argv[0]); + uint64_t n, off; uint8_t *buf; + if (parse_u64(argv[3], &n)) return 1; + if (load_range(argv[1], argv[2], n, &buf, &off)) return 1; + put_hex(buf + off, n); putchar('\n'); + free(buf); + return 0; +} + +static int cmd_pin_inc(int argc, char **argv) { + if (argc != 3) return fail("usage: %s ", argv[0]); + uint8_t *buf; uint64_t len; uint8_t d[32]; + if (read_file(argv[1], &buf, &len)) return 1; + sha256_file_buf(buf, len, d); + free(buf); + + FILE *f = fopen(argv[2], "w"); + if (!f) return fail("cannot open %s for writing", argv[2]); + fprintf(f, "/* Generated by m3tool pin-inc */\n"); + fprintf(f, " .word "); + for (int i = 0; i < 4; i++) { + uint32_t w = ((uint32_t)d[4*i] << 24) | ((uint32_t)d[4*i+1] << 16) | + ((uint32_t)d[4*i+2] << 8) | ((uint32_t)d[4*i+3]); + fprintf(f, "0x%08x%s", w, i < 3 ? ", " : "\n"); + } + fprintf(f, " .word "); + for (int i = 4; i < 8; i++) { + uint32_t w = ((uint32_t)d[4*i] << 24) | ((uint32_t)d[4*i+1] << 16) | + ((uint32_t)d[4*i+2] << 8) | ((uint32_t)d[4*i+3]); + fprintf(f, "0x%08x%s", w, i < 7 ? ", " : "\n"); + } + fclose(f); + return 0; +} + +static int cmd_verify_receipt(int argc, char **argv) { + if (argc != 3) return fail("usage: %s ", argv[0]); + uint64_t off; uint8_t *buf; + if (load_range(argv[1], argv[2], 192, &buf, &off)) return 1; + uint8_t *rcpt = buf + off; + + uint8_t computed[32]; + sha256_file_buf(rcpt, 128, computed); + + if (memcmp(computed, rcpt + 0x80, 32) != 0) { + free(buf); + return fail("receipt digest mismatch at offset %s", argv[2]); + } + uint32_t decision = *(uint32_t *)(rcpt + 4); + uint64_t effect = *(uint64_t *)(rcpt + 0x38); + printf("PASS receipt at %s decision=0x%08x effect=0x%016llx digest=", argv[2], decision, (unsigned long long)effect); + put_hex(computed, 32); + putchar('\n'); + free(buf); + return 0; +} + +static int cmd_verify_chain(int argc, char **argv) { + if (argc != 2) return fail("usage: %s ", argv[0]); + uint8_t *buf; uint64_t len; + if (read_file(argv[1], &buf, &len)) return 1; + if (len < 128) { free(buf); return fail("dump too small for ledger header: %s", argv[1]); } + + uint64_t magic = *(uint64_t *)(buf + 0); + if (magic != 0x524350544C444752ULL) { free(buf); return fail("bad ledger magic 0x%llx", (unsigned long long)magic); } + + uint32_t cap = *(uint32_t *)(buf + 8); + uint32_t count = *(uint32_t *)(buf + 12); + if (cap != 32 || count > 32) { free(buf); return fail("invalid cap=%u or count=%u", (unsigned)cap, (unsigned)count); } + + uint8_t prev_digest[32]; + memset(prev_digest, 0, 32); + + for (uint32_t i = 0; i < count; i++) { + uint64_t roff = 128 + i * 192; + if (roff + 192 > len) { free(buf); return fail("truncated receipt %u at offset %llu", i, (unsigned long long)roff); } + uint8_t *rcpt = buf + roff; + + /* Verify previous_receipt_digest */ + if (memcmp(rcpt + 0x60, prev_digest, 32) != 0) { + free(buf); + return fail("broken chain at receipt %u: previous digest mismatch", (unsigned)i); + } + + /* Verify receipt_digest */ + uint8_t comp[32]; + sha256_file_buf(rcpt, 128, comp); + if (memcmp(comp, rcpt + 0x80, 32) != 0) { + free(buf); + return fail("tampered receipt %u: computed digest does not match seal", (unsigned)i); + } + memcpy(prev_digest, comp, 32); + } + + if (count > 0) { + if (memcmp(buf + 0x20, prev_digest, 32) != 0) { + free(buf); + return fail("%s", "header.latest_receipt_digest does not match last receipt seal"); + } + } + + printf("PASS receipt chain: %u receipts verified (chain intact)\n", count); + free(buf); + return 0; +} + +static const struct { + const char *name; + int (*fn)(int, char **); + const char *help; +} CMDS[] = { + { "sha256", cmd_sha256, "sha256 " }, + { "hexfield", cmd_hexfield, "hexfield " }, + { "bytes", cmd_bytes, "bytes " }, + { "pin-inc", cmd_pin_inc, "pin-inc " }, + { "verify-receipt", cmd_verify_receipt, "verify-receipt " }, + { "verify-chain", cmd_verify_chain, "verify-chain " }, +}; + +int main(int argc, char **argv) { + if (argc >= 2) { + for (size_t i = 0; i < sizeof CMDS / sizeof CMDS[0]; i++) { + if (strcmp(argv[1], CMDS[i].name) == 0) { + int rc = CMDS[i].fn(argc - 1, argv + 1); + if (fflush(stdout) != 0 || ferror(stdout)) return fail("%s", "write error on stdout"); + return rc ? 1 : 0; + } + } + } + fprintf(stderr, "usage: m3tool ...\n"); + for (size_t i = 0; i < sizeof CMDS / sizeof CMDS[0]; i++) fprintf(stderr, " m3tool %s\n", CMDS[i].help); + return 2; +} diff --git a/m3/tools/qemu_inspect.sh b/m3/tools/qemu_inspect.sh new file mode 100755 index 0000000..24ea740 --- /dev/null +++ b/m3/tools/qemu_inspect.sh @@ -0,0 +1,165 @@ +#!/bin/sh +# m3/tools/qemu_inspect.sh -- Python-free QEMU state inspection for M3 gates. +# +# Boots qemu-system-aarch64 (-M virt -cpu cortex-a57 -m 128M, TCG single +# thread), waits for a UART marker in the serial file, stops the guest, dumps +# named guest-physical regions with QMP pmemsave, then quits. Fails closed: +# exit nonzero on timeout, fail-marker, QMP error, or any short/missing dump. +# +# Usage: +# qemu_inspect.sh --out DIR --marker ERE [--fail-marker ERE] [--timeout SECS] +# [--bios FILE] [--loader FILE@ADDR]... [--region NAME:ADDR:LEN]... +# [--qemu-arg ARG]... +# ADDR/LEN accept 0x hex or decimal. Region dumps land in DIR/NAME.bin. +# Regions must lie inside guest RAM [0x40000000, +QEMU_MEM): pmemsave of +# unmapped space returns zeros silently, so it is refused up front. +# Env overrides: QEMU (binary), QEMU_MACHINE, QEMU_CPU, QEMU_MEM. +# Outputs in DIR: serial.txt, qmp.log, qemu-cmd.txt, qemu-version.txt, NAME.bin. +# Exit codes: 0 ok, 1 usage/setup/QMP/dump failure, 2 marker timeout, +# 3 fail-marker seen, 4 QEMU exited before the marker. +set -eu + +QEMU=${QEMU:-qemu-system-aarch64} +QEMU_MACHINE=${QEMU_MACHINE:-virt} +QEMU_CPU=${QEMU_CPU:-cortex-a57} +QEMU_MEM=${QEMU_MEM:-128M} + +die() { echo "qemu_inspect: $*" >&2; exit "${RC:-1}"; } + +OUT= MARKER= FAIL_MARKER= TIMEOUT=10 BIOS= +LOADERS= REGIONS= EXTRA= +nl=' +' +while [ $# -gt 0 ]; do + case $1 in + --out) OUT=$2; shift 2 ;; + --marker) MARKER=$2; shift 2 ;; + --fail-marker) FAIL_MARKER=$2; shift 2 ;; + --timeout) TIMEOUT=$2; shift 2 ;; + --bios) BIOS=$2; shift 2 ;; + --loader) LOADERS="$LOADERS$2$nl"; shift 2 ;; + --region) REGIONS="$REGIONS$2$nl"; shift 2 ;; + --qemu-arg) EXTRA="$EXTRA$2$nl"; shift 2 ;; + *) die "unknown argument: $1" ;; + esac +done +[ -n "$OUT" ] || die "--out required" +[ -n "$MARKER" ] || die "--marker required" +case $TIMEOUT in ''|*[!0-9]*) die "--timeout must be whole seconds" ;; esac +[ "$TIMEOUT" -ge 1 ] || die "--timeout must be >= 1" +command -v "$QEMU" >/dev/null || die "$QEMU not found" +command -v socat >/dev/null || die "socat not found" + +to_dec() { # strict unsigned number -> decimal; empty on error + case $1 in + 0x*|0X*) h=${1#0?}; case $h in ''|*[!0-9a-fA-F]*) return 1 ;; esac ;; + *) case $1 in ''|*[!0-9]*) return 1 ;; esac ;; + esac + printf '%d' "$1" +} + +mkdir -p "$OUT" +OUT=$(cd "$OUT" && pwd -P) +SOCK=$OUT/qmp.sock +rm -f "$OUT/serial.txt" "$OUT/qmp.log" "$SOCK" + +# Regions must lie wholly inside guest RAM: QEMU pmemsave of unmapped space +# silently returns zeros, which would fake "no effect" evidence. +RAM_BASE=$((0x40000000)) +case $QEMU_MEM in + *[!0-9]*[MG]|[MG]) die "QEMU_MEM must be NNM or NNG: $QEMU_MEM" ;; + *M) RAM_SIZE=$(( ${QEMU_MEM%M} * 1048576 )) ;; + *G) RAM_SIZE=$(( ${QEMU_MEM%G} * 1073741824 )) ;; + *) die "QEMU_MEM must be NNM or NNG: $QEMU_MEM" ;; +esac +RAM_END=$((RAM_BASE + RAM_SIZE)) + +# Validate regions up front; remove stale dumps. +IFS_SAVE=$IFS +IFS=$nl +for r in $REGIONS; do + IFS=: read -r name addr len </dev/null || die "bad region addr in '$r'" + to_dec "$len" >/dev/null || die "bad region len in '$r'" + a=$(to_dec "$addr"); n=$(to_dec "$len") + [ "$n" -gt 0 ] || die "zero-length region '$r'" + [ "$a" -ge "$RAM_BASE" ] && [ "$n" -le "$RAM_SIZE" ] && [ "$a" -le $((RAM_END - n)) ] \ + || die "region '$r' is not inside guest RAM [0x40000000, +$QEMU_MEM)" + rm -f "$OUT/$name.bin" +done + +# Build the QEMU argument list as positional parameters. +set -- "$QEMU" -M "$QEMU_MACHINE" -cpu "$QEMU_CPU" -m "$QEMU_MEM" \ + -accel tcg,thread=single -display none -monitor none \ + -serial "file:$OUT/serial.txt" -qmp "unix:$SOCK,server=on,wait=off" +[ -z "$BIOS" ] || { [ -f "$BIOS" ] || die "bios not found: $BIOS"; set -- "$@" -bios "$BIOS"; } +for l in $LOADERS; do + f=${l%@*}; a=${l##*@} + [ "$f" != "$l" ] || die "loader must be FILE@ADDR: $l" + [ -f "$f" ] || die "loader file not found: $f" + to_dec "$a" >/dev/null || die "bad loader addr: $l" + set -- "$@" -device "loader,file=$f,addr=$a,force-raw=on" +done +for e in $EXTRA; do set -- "$@" "$e"; done +IFS=$IFS_SAVE + +"$QEMU" --version > "$OUT/qemu-version.txt" +{ for a in "$@"; do printf "'%s' " "$a"; done; echo; } > "$OUT/qemu-cmd.txt" + +"$@" & +QPID=$! +cleanup() { kill "$QPID" 2>/dev/null || true; wait "$QPID" 2>/dev/null || true; rm -f "$SOCK"; } +trap cleanup EXIT INT TERM + +# Wait for the marker (bounded; 50 ms steps). +steps=$((TIMEOUT * 20)); i=0 +while :; do + if [ -n "$FAIL_MARKER" ] && grep -qE "$FAIL_MARKER" "$OUT/serial.txt" 2>/dev/null; then + RC=3 die "fail marker /$FAIL_MARKER/ seen in serial output" + fi + grep -qE "$MARKER" "$OUT/serial.txt" 2>/dev/null && break + kill -0 "$QPID" 2>/dev/null || RC=4 die "QEMU exited before marker /$MARKER/" + i=$((i + 1)) + [ "$i" -le "$steps" ] || RC=2 die "timeout (${TIMEOUT}s) waiting for marker /$MARKER/" + sleep 0.05 +done +[ -S "$SOCK" ] || die "QMP socket missing: $SOCK" + +# One QMP session: capabilities, stop, pmemsave each region, quit. +n=0 +{ + printf '{"execute":"qmp_capabilities"}\n' + printf '{"execute":"stop"}\n' + IFS=$nl + for r in $REGIONS; do + IFS=: read -r name addr len < "$OUT/qmp.log" 2>"$OUT/socat.err" || true +IFS=$IFS_SAVE +wait "$QPID" 2>/dev/null || true +trap - EXIT INT TERM +rm -f "$SOCK" + +grep -q '"error"' "$OUT/qmp.log" && die "QMP error (see $OUT/qmp.log)" +IFS=$nl +for r in $REGIONS; do + IFS=: read -r name addr len < $OUT" diff --git a/m3/vector_table.s b/m3/vector_table.s new file mode 100644 index 0000000..618cf2e --- /dev/null +++ b/m3/vector_table.s @@ -0,0 +1,205 @@ +/* + * m3/vector_table.s - 2,048-Byte Aligned AArch64 Exception Vector Table + * Milestone 3: PHYSICS_EFFECTS (Sovereign Machine) + * + * Vector Table Geometry: + * Base Alignment: 2,048 bytes (0x800) + * Total Span: exactly 2,048 bytes (0x000..0x7FF) + * 16 architectural vector slots spaced exactly 128 bytes (0x80) apart: + * 0x000: Current EL with SP0 (Sync) + * 0x080: Current EL with SP0 (IRQ) + * 0x100: Current EL with SP0 (FIQ) + * 0x180: Current EL with SP0 (SError) + * 0x200: Current EL with SPx (Sync -> Main Trap) + * 0x280: Current EL with SPx (IRQ) + * 0x300: Current EL with SPx (FIQ) + * 0x380: Current EL with SPx (SError) + * 0x400: Lower EL AArch64 (Sync) + * 0x480: Lower EL AArch64 (IRQ) + * 0x500: Lower EL AArch64 (FIQ) + * 0x580: Lower EL AArch64 (SError) + * 0x600: Lower EL AArch32 (Sync) + * 0x680: Lower EL AArch32 (IRQ) + * 0x700: Lower EL AArch32 (FIQ) + * 0x780: Lower EL AArch32 (SError) + */ + + .global vector_table + + .section .text.vectors, "ax" + .balign 2048 + +vector_table: +/* Slot 0 (0x000): Current EL with SP0 - Sync */ + mov x0, #0 + b common_trap + .balign 128 + +/* Slot 1 (0x080): Current EL with SP0 - IRQ */ + mov x0, #1 + b common_trap + .balign 128 + +/* Slot 2 (0x100): Current EL with SP0 - FIQ */ + mov x0, #2 + b common_trap + .balign 128 + +/* Slot 3 (0x180): Current EL with SP0 - SError */ + mov x0, #3 + b common_trap + .balign 128 + +/* Slot 4 (0x200): Current EL with SPx - Sync */ + mov x0, #4 + b common_trap + .balign 128 + +/* Slot 5 (0x280): Current EL with SPx - IRQ */ + mov x0, #5 + b common_trap + .balign 128 + +/* Slot 6 (0x300): Current EL with SPx - FIQ */ + mov x0, #6 + b common_trap + .balign 128 + +/* Slot 7 (0x380): Current EL with SPx - SError */ + mov x0, #7 + b common_trap + .balign 128 + +/* Slot 8 (0x400): Lower EL AArch64 - Sync */ + mov x0, #8 + b common_trap + .balign 128 + +/* Slot 9 (0x480): Lower EL AArch64 - IRQ */ + mov x0, #9 + b common_trap + .balign 128 + +/* Slot 10 (0x500): Lower EL AArch64 - FIQ */ + mov x0, #10 + b common_trap + .balign 128 + +/* Slot 11 (0x580): Lower EL AArch64 - SError */ + mov x0, #11 + b common_trap + .balign 128 + +/* Slot 12 (0x600): Lower EL AArch32 - Sync */ + mov x0, #12 + b common_trap + .balign 128 + +/* Slot 13 (0x680): Lower EL AArch32 - IRQ */ + mov x0, #13 + b common_trap + .balign 128 + +/* Slot 14 (0x700): Lower EL AArch32 - FIQ */ + mov x0, #14 + b common_trap + .balign 128 + +/* Slot 15 (0x780): Lower EL AArch32 - SError */ + mov x0, #15 + b common_trap + .balign 128 + +/* Common Trap Handler in general .text section */ + .section .text, "ax" + .balign 4 +common_trap: + /* Capture trap state to PHYSICS_BOOT_STATE + 0x80 = 0x40208880 */ + ldr x1, =0x40208880 + + /* Save x2..x30 first */ + stp x2, x3, [x1, #16] + stp x4, x5, [x1, #32] + stp x6, x7, [x1, #48] + stp x8, x9, [x1, #64] + stp x10, x11, [x1, #80] + stp x12, x13, [x1, #96] + stp x14, x15, [x1, #112] + stp x16, x17, [x1, #128] + stp x18, x19, [x1, #144] + stp x20, x21, [x1, #160] + stp x22, x23, [x1, #176] + stp x24, x25, [x1, #192] + stp x26, x27, [x1, #208] + stp x28, x29, [x1, #224] + str x30, [x1, #240] + + /* Save slot index to +0x100 */ + str x0, [x1, #0x100] + + /* Save SP to +0x0F8 */ + mov x2, sp + str x2, [x1, #0x0F8] + + /* Save CurrentEL */ + mrs x3, CurrentEL + str x3, [x1, #0x108] + + /* Save ESR, ELR, SPSR */ + mrs x4, esr_el1 + str x4, [x1, #0x110] + mrs x5, elr_el1 + str x5, [x1, #0x118] + mrs x6, spsr_el1 + str x6, [x1, #0x120] + + /* Evaluate ESR.EC to check FAR validity */ + lsr x7, x4, #26 /* x7 = EC */ + cmp x7, #0x20 /* Instruction abort lower EL */ + b.eq .Lfar_valid + cmp x7, #0x21 /* Instruction abort same EL */ + b.eq .Lfar_valid + cmp x7, #0x22 /* PC alignment */ + b.eq .Lfar_valid + cmp x7, #0x24 /* Data abort lower EL */ + b.eq .Lfar_valid + cmp x7, #0x25 /* Data abort same EL */ + b.eq .Lfar_valid + cmp x7, #0x26 /* SP alignment */ + b.eq .Lfar_valid + + /* FAR not valid */ + str xzr, [x1, #0x128] + str xzr, [x1, #0x130] + b .Lwrite_magic + +.Lfar_valid: + mrs x8, far_el1 + str x8, [x1, #0x128] + mov x9, #1 + str x9, [x1, #0x130] + +.Lwrite_magic: + /* Write magic TRAPFRM1 (0x314D524650415254) last */ + ldr x10, =0x314D524650415254 + str x10, [x1, #0x138] + + /* Emit diagnostic trap notification to UART */ + ldr x1, =0x09000000 + adr x2, .Lmsg_trap +.Lput_trap_loop: + ldrb w3, [x2], #1 + cbz w3, .Lput_trap_done +1: ldrb w4, [x1, #24] + tst w4, #32 + b.ne 1b + strb w3, [x1] + b .Lput_trap_loop +.Lput_trap_done: + + /* Quiesce into terminal loop */ +2: wfe + b 2b + +.Lmsg_trap: + .asciz "PHYSICS: TRAP_EXCEPTION\n"