From 4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Tue, 13 Oct 2026 08:52:16 -0500 Subject: [PATCH 1/2] AIENOS-PI5: software-only DMA bounce window and descriptor validator (ADR 0020), host-only Co-Authored-By: Claude Sonnet 5.5 --- native/kernel/Makefile | 24 ++- native/kernel/dev/dma_window.c | 197 +++++++++++++++++++++ native/kernel/dev/dma_window.h | 97 +++++++++++ native/kernel/tests/test_dma_window.c | 241 ++++++++++++++++++++++++++ 4 files changed, 558 insertions(+), 1 deletion(-) create mode 100644 native/kernel/dev/dma_window.c create mode 100644 native/kernel/dev/dma_window.h create mode 100644 native/kernel/tests/test_dma_window.c diff --git a/native/kernel/Makefile b/native/kernel/Makefile index 8e3d4d7e..69915867 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -1101,7 +1101,7 @@ HOST_SRCS := $(KDIR)/core/fmt.c $(KDIR)/core/acpi.c $(KDIR)/core/smmu.c $(KDIR)/ $(KDIR)/core/line_rx.c $(KDIR)/core/klog.c $(KDIR)/core/klog_slot.c $(KDIR)/core/cpufeat.c TESTS := test_fmt test_frames test_pt test_acpi test_acpi_dev test_heap test_smmu test_sched test_ipc test_artifact test_osc_unit test_osc_launch test_osc_admit test_entropy test_handoff test_fbcon test_osh_op \ test_continuity_codec test_continuity_resolve test_continuity_commit test_continuity_recovery test_continuity_geometry \ - test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 test_pi5_marker test_pi5_rng test_cpufeat + test_continuity_subject test_continuity_subject_provision test_lifecycle test_console_rx test_klog test_klog_slot test_pi5 test_pi5_marker test_pi5_rng test_cpufeat test_dma_window HCFLAGS := -std=gnu11 -O1 -g -Wall -Wextra -Werror -I$(KDIR)/core -I$(KDIR)/mm -I$(KDIR)/tests -I$(BOOT) SANFLAGS := -fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer @@ -1902,3 +1902,25 @@ test-pi5-marker: $(OUT)/host/test_pi5_marker $(OUT)/host-san/test_pi5_marker: $(KDIR)/tests/test_pi5_marker.c $(PI5_MARKER_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/core/pi5_marker.h @mkdir -p $(dir $@) $(HOSTCC) $(HCFLAGS) $(SANFLAGS) -I$(KDIR)/dev -I$(KDIR)/tools -I$(NATIVE)/disk -o $@ $< $(PI5_MARKER_SRCS) + +# AIENOS-PI5 software-only DMA confinement (ADR 0020): host test only, not linked into any image. +DMAW_SRCS := $(KDIR)/dev/dma_window.c +DMAW_DEPS := $(KDIR)/tests/test_dma_window.c $(DMAW_SRCS) $(KDIR)/tests/ck_test.h $(KDIR)/dev/dma_window.h +$(OUT)/host/test_dma_window: $(DMAW_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) -I$(KDIR)/dev -o $@ $< $(DMAW_SRCS) +$(OUT)/host-san/test_dma_window: $(DMAW_DEPS) + @mkdir -p $(dir $@) + $(HOSTCC) $(HCFLAGS) $(SANFLAGS) -I$(KDIR)/dev -o $@ $< $(DMAW_SRCS) +test-dma-window: $(OUT)/host/test_dma_window + $< +# Each mutant removes one check; the test must FAIL for every one. (The explicit "addr below base" test is not a +# mutant: with unsigned arithmetic the end check already refuses it, so it is kept as defence in depth.) +DMAW_MUTANTS := NO_END WRAP NO_ZERO NO_LATCH NO_STALE NO_ALIGN NO_FRAMECAP NO_GOT NO_INDEX +dma-window-mutants: + @mkdir -p $(OUT)/host + @fail=0; for m in $(DMAW_MUTANTS); do \ + $(HOSTCC) $(HCFLAGS) -DCK_DMAW_MUTANT_$$m -I$(KDIR)/dev -o $(OUT)/host/test_dma_window_mut_$$m $(KDIR)/tests/test_dma_window.c $(DMAW_SRCS) || exit 1; \ + if $(OUT)/host/test_dma_window_mut_$$m >/dev/null; then echo "MUTANT SURVIVED: $$m"; fail=1; else echo "mutant killed: $$m"; fi; \ + done; test $$fail -eq 0 +.PHONY: test-dma-window dma-window-mutants diff --git a/native/kernel/dev/dma_window.c b/native/kernel/dev/dma_window.c new file mode 100644 index 00000000..fc5fae2e --- /dev/null +++ b/native/kernel/dev/dma_window.c @@ -0,0 +1,197 @@ +/* dma_window.c -- see dma_window.h and ADR 0020. + * software-only DMA confinement (Pi 5, weaker than the Spark). */ +#include "dma_window.h" + +/* TEST-only mutants (make dma-window-mutants): each removes one check and + * must make test_dma_window FAIL. Host builds only; never part of an image. */ +#if defined(CK_DMAW_MUTANT_NO_END) || defined(CK_DMAW_MUTANT_WRAP) || \ + defined(CK_DMAW_MUTANT_NO_ZERO) || defined(CK_DMAW_MUTANT_NO_LATCH) || defined(CK_DMAW_MUTANT_NO_STALE) || \ + defined(CK_DMAW_MUTANT_NO_ALIGN) || defined(CK_DMAW_MUTANT_NO_FRAMECAP) || defined(CK_DMAW_MUTANT_NO_GOT) || \ + defined(CK_DMAW_MUTANT_NO_INDEX) +#if !__STDC_HOSTED__ +#error "CK_DMAW_MUTANT_* are host test mutants only" +#endif +#ifdef CK_HARDWARE_STAGING +#error "CK_DMAW_MUTANT_* cannot be combined with CK_HARDWARE_STAGING" +#endif +#endif + +static int pow2(uint64_t n) { return n != 0 && (n & (n - 1)) == 0; } + +int ck_dmaw_init(struct ck_dmaw *w, void *mem, uint64_t bus_base, uint64_t size) +{ + if (!w) + return CK_DMAW_E_PARAM; + w->latched = 1; /* stay halted unless init fully succeeds */ + w->last_err = CK_DMAW_E_PARAM; + w->base = 0; + w->size = 0; + w->next = 0; + if (!mem || !pow2(size) || size < CK_DMAW_MIN_BYTES || size > CK_DMAW_MAX_BYTES) + return CK_DMAW_E_PARAM; + if ((bus_base & (size - 1)) != 0 || bus_base > UINT64_MAX - size) + return CK_DMAW_E_PARAM; /* base must be aligned to size and not wrap */ + volatile uint8_t *p = (volatile uint8_t *)mem; + for (uint64_t i = 0; i < size; i++) + p[i] = 0; + w->base = bus_base; + w->size = size; + w->latched = 0; + w->last_err = CK_DMAW_OK; + return CK_DMAW_OK; +} + +static int latch(struct ck_dmaw *w, int err) +{ +#ifndef CK_DMAW_MUTANT_NO_LATCH + w->latched = 1; +#endif + w->last_err = err; + return err; +} + +int ck_dmaw_halted(const struct ck_dmaw *w) { return !w || w->latched; } + +int ck_dmaw_validate_aligned(struct ck_dmaw *w, uint64_t addr, uint64_t len, uint64_t align) +{ + (void)align; /* unused only in the NO_ALIGN mutant */ + if (!w) + return CK_DMAW_E_PARAM; + if (w->latched) + return CK_DMAW_E_LATCHED; +#ifndef CK_DMAW_MUTANT_NO_ZERO + if (len == 0) + return latch(w, CK_DMAW_E_ZERO); +#endif +#ifndef CK_DMAW_MUTANT_NO_ALIGN + if (!pow2(align) || (addr & (align - 1)) != 0) + return latch(w, CK_DMAW_E_ALIGN); +#endif +#ifdef CK_DMAW_MUTANT_WRAP + /* mutant: the naive end computation that wraps */ + if (addr < w->base || addr + len > w->base + w->size) + return latch(w, CK_DMAW_E_RANGE); +#else + int bad = 0; + bad |= addr < w->base; + bad |= len > w->size; /* also guarantees size - len cannot underflow */ +#ifndef CK_DMAW_MUTANT_NO_END + if (!bad) + bad |= (addr - w->base) > (w->size - len); +#endif + if (bad) + return latch(w, CK_DMAW_E_RANGE); +#endif + return CK_DMAW_OK; +} + +int ck_dmaw_validate(struct ck_dmaw *w, uint64_t addr, uint64_t len) +{ + return ck_dmaw_validate_aligned(w, addr, len, 1); +} + +int ck_dmaw_alloc(struct ck_dmaw *w, uint64_t len, uint64_t align, uint64_t *addr) +{ + if (!w || !addr || len == 0 || !pow2(align)) + return CK_DMAW_E_PARAM; + if (w->latched) + return CK_DMAW_E_LATCHED; + uint64_t off = (w->next + (align - 1)) & ~(align - 1); + if (off < w->next || off > w->size || len > w->size - off) + return CK_DMAW_E_NOSPACE; + w->next = off + len; + *addr = w->base + off; + return CK_DMAW_OK; +} + +int ck_dmaw_ring_init(struct ck_dmaw_ring *r, struct ck_dmaw_slot *slots, uint32_t n) +{ + if (!r || !slots || n == 0 || n > CK_DMAW_RING_MAX || (n & (n - 1)) != 0) + return CK_DMAW_E_PARAM; + r->slots = slots; + r->n = n; + r->head = 0; + r->inflight = 0; + for (uint32_t i = 0; i < n; i++) { + slots[i].d.addr = 0; + slots[i].d.len = 0; + slots[i].d.flags = 0; + slots[i].state = CK_DMAW_SLOT_FREE; + } + return CK_DMAW_OK; +} + +int ck_dmaw_ring_post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, + uint32_t flags, uint32_t *idx) +{ + if (!r || !w || !idx) + return CK_DMAW_E_PARAM; + if (w->latched) + return CK_DMAW_E_LATCHED; +#ifndef CK_DMAW_MUTANT_NO_FRAMECAP + if (len > CK_DMAW_MAX_FRAME) + return latch(w, CK_DMAW_E_BIG); +#endif + int rc = ck_dmaw_validate_aligned(w, addr, len, CK_DMAW_BUF_ALIGN); + if (rc != CK_DMAW_OK) + return rc; + struct ck_dmaw_slot *s = &r->slots[r->head]; + if (s->state != CK_DMAW_SLOT_FREE) + return CK_DMAW_E_NOSPACE; + s->d.addr = addr; + s->d.len = len; + s->d.flags = flags; + s->state = CK_DMAW_SLOT_INFLIGHT; + *idx = r->head; + r->head = (r->head + 1) & (r->n - 1); + r->inflight++; + return CK_DMAW_OK; +} + +int ck_dmaw_ring_emit(const struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, + struct ck_dmaw_desc *out) +{ + if (!r || !w || !out) + return CK_DMAW_E_PARAM; + if (w->latched) + return CK_DMAW_E_LATCHED; + if (idx >= r->n) + return latch(w, CK_DMAW_E_INDEX); + const struct ck_dmaw_slot *s = &r->slots[idx]; +#ifndef CK_DMAW_MUTANT_NO_STALE + if (s->state != CK_DMAW_SLOT_INFLIGHT) + return latch(w, CK_DMAW_E_STALE); +#endif + /* defence in depth: the shadow itself is checked again before it leaves */ + int rc = ck_dmaw_validate_aligned(w, s->d.addr, s->d.len, CK_DMAW_BUF_ALIGN); + if (rc != CK_DMAW_OK) + return rc; + *out = s->d; + return CK_DMAW_OK; +} + +int ck_dmaw_ring_complete(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, uint32_t got) +{ + (void)got; /* unused only in the NO_GOT mutant */ + if (!r || !w) + return CK_DMAW_E_PARAM; + if (w->latched) + return CK_DMAW_E_LATCHED; +#ifndef CK_DMAW_MUTANT_NO_INDEX + if (idx >= r->n) + return latch(w, CK_DMAW_E_INDEX); +#endif + struct ck_dmaw_slot *s = &r->slots[idx & (r->n - 1)]; +#ifndef CK_DMAW_MUTANT_NO_STALE + if (s->state != CK_DMAW_SLOT_INFLIGHT) + return latch(w, CK_DMAW_E_STALE); +#endif +#ifndef CK_DMAW_MUTANT_NO_GOT + if (got > s->d.len) + return latch(w, CK_DMAW_E_RANGE); +#endif + s->state = CK_DMAW_SLOT_FREE; + if (r->inflight) + r->inflight--; + return CK_DMAW_OK; +} diff --git a/native/kernel/dev/dma_window.h b/native/kernel/dev/dma_window.h new file mode 100644 index 00000000..cd8e5100 --- /dev/null +++ b/native/kernel/dev/dma_window.h @@ -0,0 +1,97 @@ +/* dma_window.h -- software-only DMA confinement for the Raspberry Pi 5 + * (ADR 0020). One bounce window, an overflow-safe validator, and descriptor + * ring helpers that only ever emit addresses inside the window. + * + * LABEL: software-only DMA confinement (Pi 5, weaker than the Spark). + * This is an explicit Pi-only exception to ADR 0012 rule 5. The Spark's DMA + * is fenced by SMMU stream windows (core/smmu.c); this file is not part of + * that and is not linked into the Spark, QEMU or any default image. + * + * Pure C, freestanding (stdint only), no allocation, no I/O. The GEM + * descriptor bit layout is NOT modelled here (UNVERIFIED, driver cut): a + * descriptor is just (addr, len, flags) from the driver's own record. */ +#ifndef AIENOS_CK_DMA_WINDOW_H +#define AIENOS_CK_DMA_WINDOW_H +#include + +#define CK_DMAW_MAX_BYTES (2u * 1024u * 1024u) /* ADR 0020 rule 1: <= 2 MiB */ +#define CK_DMAW_MIN_BYTES 4096u +#define CK_DMAW_MAX_FRAME 1536u /* ADR 0020 rule 7: no jumbo frames */ +#define CK_DMAW_BUF_ALIGN 8u /* driver-chosen, not taken from the GEM manual */ +#define CK_DMAW_RING_MAX 256u + +enum { + CK_DMAW_OK = 0, + CK_DMAW_E_LATCHED = 1, /* the NIC is halted until reboot */ + CK_DMAW_E_RANGE = 2, /* not fully inside the window */ + CK_DMAW_E_ZERO = 3, /* zero length */ + CK_DMAW_E_ALIGN = 4, + CK_DMAW_E_PARAM = 5, /* bad init or call arguments */ + CK_DMAW_E_NOSPACE = 6, /* window or ring full (does not latch) */ + CK_DMAW_E_STALE = 7, /* slot not in flight: reuse or double completion */ + CK_DMAW_E_BIG = 8, /* frame larger than CK_DMAW_MAX_FRAME */ + CK_DMAW_E_INDEX = 9 /* device-supplied slot index out of range */ +}; + +struct ck_dmaw { + uint64_t base; /* bus address of the window */ + uint64_t size; /* power of two, 4 KiB..2 MiB, base aligned to size */ + uint64_t next; /* bump offset */ + int latched; + int last_err; +}; + +/* Take a power-of-two, size-aligned block (bus address `bus_base`, CPU + * mapping `mem`) and zero it. The block is used for nothing else. */ +int ck_dmaw_init(struct ck_dmaw *w, void *mem, uint64_t bus_base, uint64_t size); + +/* Is [addr, addr+len) fully inside the window? Never computes addr+len. + * Any violation latches the window; a latched window refuses everything. */ +int ck_dmaw_validate(struct ck_dmaw *w, uint64_t addr, uint64_t len); +int ck_dmaw_validate_aligned(struct ck_dmaw *w, uint64_t addr, uint64_t len, uint64_t align); + +/* Carve `len` bytes at `align` (power of two) out of the window; returns the + * bus address through *addr. Running out of space is E_NOSPACE (no latch). */ +int ck_dmaw_alloc(struct ck_dmaw *w, uint64_t len, uint64_t align, uint64_t *addr); + +/* Operator-visible halt: set when anything went wrong; cleared only by init. */ +int ck_dmaw_halted(const struct ck_dmaw *w); + +/* The descriptor handed to the device. Built only from the driver's record. */ +struct ck_dmaw_desc { + uint64_t addr; + uint32_t len; + uint32_t flags; +}; + +enum { CK_DMAW_SLOT_FREE = 0, CK_DMAW_SLOT_INFLIGHT = 1 }; + +struct ck_dmaw_slot { /* the driver's shadow of one descriptor */ + struct ck_dmaw_desc d; + uint32_t state; +}; + +struct ck_dmaw_ring { + struct ck_dmaw_slot *slots; /* caller storage, kernel memory, NOT in the window */ + uint32_t n; /* power of two, <= CK_DMAW_RING_MAX */ + uint32_t head; /* next slot to post */ + uint32_t inflight; +}; + +int ck_dmaw_ring_init(struct ck_dmaw_ring *r, struct ck_dmaw_slot *slots, uint32_t n); + +/* Post one buffer: validated (range, alignment, frame cap) against the window, + * recorded in the shadow. *idx is the slot used. A full ring is E_NOSPACE. */ +int ck_dmaw_ring_post(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint64_t addr, uint32_t len, + uint32_t flags, uint32_t *idx); + +/* Produce the descriptor for slot idx FROM THE SHADOW ONLY (never from ring + * memory the device can write), re-validated. Slot not in flight: E_STALE. */ +int ck_dmaw_ring_emit(const struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, + struct ck_dmaw_desc *out); + +/* Device says slot idx finished with `got` bytes. Index out of range, slot not + * in flight, or got > posted length: latch. Otherwise the slot is freed. */ +int ck_dmaw_ring_complete(struct ck_dmaw_ring *r, struct ck_dmaw *w, uint32_t idx, uint32_t got); + +#endif diff --git a/native/kernel/tests/test_dma_window.c b/native/kernel/tests/test_dma_window.c new file mode 100644 index 00000000..524d6cba --- /dev/null +++ b/native/kernel/tests/test_dma_window.c @@ -0,0 +1,241 @@ +/* test_dma_window.c -- Pi 5 software-only DMA confinement (dev/dma_window.c, + * ADR 0020). Hosted: this proves the rule's logic, NOT the hardware. Run + * under ASan/UBSan and with each CK_DMAW_MUTANT_* (make dma-window-mutants), + * where every mutant must make this test FAIL. */ +#include +#include +#include +#include "ck_test.h" +#include "dma_window.h" + +#define WSIZE 65536u +#define WBASE 0x40000000ull + +static uint8_t *mem; + +static void fresh(struct ck_dmaw *w) +{ + memset(mem, 0xAA, WSIZE); + CHECK(ck_dmaw_init(w, mem, WBASE, WSIZE) == CK_DMAW_OK); +} + +static void t_init(void) +{ + struct ck_dmaw w; + memset(mem, 0xAA, WSIZE); + CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK); + int zero = 1; + for (uint32_t i = 0; i < WSIZE; i++) zero &= mem[i] == 0; + CHECK(zero); + CHECK(!ck_dmaw_halted(&w)); + CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE + 4096) == CK_DMAW_E_PARAM); /* not pow2 */ + CHECK(ck_dmaw_halted(&w)); /* failed init stays halted */ + CHECK(ck_dmaw_init(&w, mem, WBASE + 4096, WSIZE) == CK_DMAW_E_PARAM); /* misaligned base */ + CHECK(ck_dmaw_init(&w, mem, WBASE, CK_DMAW_MAX_BYTES * 2) == CK_DMAW_E_PARAM); + CHECK(ck_dmaw_init(&w, mem, WBASE, 2048) == CK_DMAW_E_PARAM); + CHECK(ck_dmaw_init(&w, mem, 0 - (uint64_t)WSIZE, WSIZE) == CK_DMAW_E_PARAM); /* end wraps */ + CHECK(ck_dmaw_init(&w, NULL, WBASE, WSIZE) == CK_DMAW_E_PARAM); + CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK); + CHECK(!ck_dmaw_halted(&w)); +} + +static void t_validate(void) +{ + struct ck_dmaw w; + fresh(&w); + CHECK(ck_dmaw_validate(&w, WBASE, 1) == CK_DMAW_OK); + CHECK(ck_dmaw_validate(&w, WBASE, WSIZE) == CK_DMAW_OK); /* whole window */ + CHECK(ck_dmaw_validate(&w, WBASE + WSIZE - 1, 1) == CK_DMAW_OK); /* last byte */ + CHECK(!ck_dmaw_halted(&w)); + + fresh(&w); /* below the window */ + CHECK(ck_dmaw_validate(&w, WBASE - 1, 16) == CK_DMAW_E_RANGE); + CHECK(ck_dmaw_halted(&w)); + CHECK(ck_dmaw_validate(&w, WBASE, 1) == CK_DMAW_E_LATCHED); /* valid call after latch refused */ + + fresh(&w); /* straddles the end */ + CHECK(ck_dmaw_validate(&w, WBASE + WSIZE - 8, 16) == CK_DMAW_E_RANGE); + CHECK(ck_dmaw_halted(&w)); + + fresh(&w); /* starts at the end */ + CHECK(ck_dmaw_validate(&w, WBASE + WSIZE, 1) == CK_DMAW_E_RANGE); + + fresh(&w); /* entirely above */ + CHECK(ck_dmaw_validate(&w, WBASE + WSIZE + 4096, 16) == CK_DMAW_E_RANGE); + + fresh(&w); /* wrapped length: addr + len overflows 2^64 to a small number */ + CHECK(ck_dmaw_validate(&w, 0 - 16ull, 64) == CK_DMAW_E_RANGE); + fresh(&w); + CHECK(ck_dmaw_validate(&w, WBASE + 16, 0 - 8ull) == CK_DMAW_E_RANGE); /* huge len wraps back inside */ + fresh(&w); + CHECK(ck_dmaw_validate(&w, WBASE, UINT64_MAX) == CK_DMAW_E_RANGE); + + fresh(&w); /* zero length */ + CHECK(ck_dmaw_validate(&w, WBASE + 8, 0) == CK_DMAW_E_ZERO); + CHECK(ck_dmaw_halted(&w)); + + fresh(&w); /* misaligned */ + CHECK(ck_dmaw_validate_aligned(&w, WBASE + 4, 16, 8) == CK_DMAW_E_ALIGN); + CHECK(ck_dmaw_halted(&w)); + fresh(&w); + CHECK(ck_dmaw_validate_aligned(&w, WBASE + 8, 16, 8) == CK_DMAW_OK); + CHECK(ck_dmaw_validate_aligned(&w, WBASE + 8, 16, 3) == CK_DMAW_E_ALIGN); /* align not pow2 */ + + CHECK(ck_dmaw_init(&w, mem, WBASE, WSIZE) == CK_DMAW_OK); /* only init clears the latch */ + CHECK(!ck_dmaw_halted(&w)); +} + +static void t_alloc(void) +{ + struct ck_dmaw w; + uint64_t a, b; + fresh(&w); + CHECK(ck_dmaw_alloc(&w, 100, 64, &a) == CK_DMAW_OK && a == WBASE); + CHECK(ck_dmaw_alloc(&w, 8, 64, &b) == CK_DMAW_OK && b == WBASE + 128 && ck_dmaw_validate(&w, b, 8) == CK_DMAW_OK); + CHECK(ck_dmaw_alloc(&w, WSIZE, 8, &a) == CK_DMAW_E_NOSPACE); + CHECK(!ck_dmaw_halted(&w)); /* running out of space is not a violation */ + CHECK(ck_dmaw_alloc(&w, UINT64_MAX, 8, &a) == CK_DMAW_E_NOSPACE); + CHECK(ck_dmaw_alloc(&w, 8, 3, &a) == CK_DMAW_E_PARAM); + CHECK(ck_dmaw_alloc(&w, 0, 8, &a) == CK_DMAW_E_PARAM); +} + +static void t_ring(void) +{ + struct ck_dmaw w; + struct ck_dmaw_ring r; + struct ck_dmaw_slot slots[4]; + struct ck_dmaw_desc d; + uint32_t i, j; + + fresh(&w); + CHECK(ck_dmaw_ring_init(&r, slots, 3) == CK_DMAW_E_PARAM); + CHECK(ck_dmaw_ring_init(&r, slots, 4) == CK_DMAW_OK); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 64, 100, 7, &i) == CK_DMAW_OK && i == 0); + CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_OK && d.addr == WBASE + 64 && d.len == 100 && d.flags == 7); + CHECK(ck_dmaw_ring_complete(&r, &w, i, 100) == CK_DMAW_OK); + CHECK(!ck_dmaw_halted(&w)); + + /* stale: emit and complete of a slot no longer in flight */ + CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_E_STALE); + CHECK(ck_dmaw_halted(&w)); + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i); + CHECK(ck_dmaw_ring_complete(&r, &w, i, 64) == CK_DMAW_OK); + CHECK(ck_dmaw_ring_complete(&r, &w, i, 64) == CK_DMAW_E_STALE); /* double completion */ + CHECK(ck_dmaw_halted(&w)); + + /* device reports more bytes than were posted */ + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i); + CHECK(ck_dmaw_ring_complete(&r, &w, i, 65) == CK_DMAW_E_RANGE); + CHECK(ck_dmaw_halted(&w)); + + /* device-supplied index out of range */ + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i); + CHECK(ck_dmaw_ring_complete(&r, &w, 4, 1) == CK_DMAW_E_INDEX); + CHECK(ck_dmaw_halted(&w)); + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i); + CHECK(ck_dmaw_ring_emit(&r, &w, 99, &d) == CK_DMAW_E_INDEX); + + /* post: out of window, wrapped, zero, misaligned, jumbo; each latches */ + uint64_t bad_addr[] = {WBASE - 8, WBASE + WSIZE, WBASE + WSIZE - 8, 0 - 8ull}; + uint32_t bad_len[] = {64, 64, 64, 64}; + for (j = 0; j < 4; j++) { + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + CHECK(ck_dmaw_ring_post(&r, &w, bad_addr[j], bad_len[j], 0, &i) == CK_DMAW_E_RANGE); + CHECK(ck_dmaw_halted(&w) && r.inflight == 0); + } + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 0, 0, &i) == CK_DMAW_E_ZERO && ck_dmaw_halted(&w)); + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 4, 64, 0, &i) == CK_DMAW_E_ALIGN && ck_dmaw_halted(&w)); + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE, CK_DMAW_MAX_FRAME, 0, &i) == CK_DMAW_OK); /* max standard frame ok */ + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE, CK_DMAW_MAX_FRAME + 1, 0, &i) == CK_DMAW_E_BIG && ck_dmaw_halted(&w)); + + /* ring full is not a violation; a latched window refuses valid posts */ + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + for (j = 0; j < 4; j++) CHECK(ck_dmaw_ring_post(&r, &w, WBASE + 64 * j, 64, 0, &i) == CK_DMAW_OK); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i) == CK_DMAW_E_NOSPACE && !ck_dmaw_halted(&w)); + ck_dmaw_validate(&w, WBASE - 1, 1); + CHECK(ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i) == CK_DMAW_E_LATCHED); + CHECK(ck_dmaw_ring_emit(&r, &w, 0, &d) == CK_DMAW_E_LATCHED); + + /* shadow, not ring memory: corrupting the shadow after post is caught at emit */ + fresh(&w); + ck_dmaw_ring_init(&r, slots, 4); + ck_dmaw_ring_post(&r, &w, WBASE, 64, 0, &i); + slots[i].d.addr = WBASE + WSIZE + 8; + CHECK(ck_dmaw_ring_emit(&r, &w, i, &d) == CK_DMAW_E_RANGE && ck_dmaw_halted(&w)); +} + +/* Property test: for hostile (addr,len), validate agrees with a 128-bit + * reference, and every descriptor the ring emits is fully inside the window. */ +static uint64_t rs = 0x9e3779b97f4a7c15ull; +static uint64_t rnd(void) +{ + rs ^= rs << 13; rs ^= rs >> 7; rs ^= rs << 17; + return rs; +} + +static void t_property(void) +{ + struct ck_dmaw w; + struct ck_dmaw_ring r; + struct ck_dmaw_slot slots[8]; + struct ck_dmaw_desc d; + int mismatch = 0, escaped = 0, emitted = 0; + for (int n = 0; n < 200000; n++) { + uint64_t addr, len; + switch (rnd() % 5) { + case 0: addr = rnd(); len = rnd(); break; + case 1: addr = WBASE + rnd() % (2 * WSIZE); len = rnd() % 4096; break; + case 2: addr = (WBASE - 64) + rnd() % 128; len = 1 + rnd() % 64; break; + case 3: addr = WBASE + WSIZE - 64 + rnd() % 128; len = rnd() % 128; break; + default: addr = 0 - (rnd() % 256); len = rnd() % 512; break; + } + fresh(&w); + int rc = ck_dmaw_validate(&w, addr, len); + unsigned __int128 end = (unsigned __int128)addr + len; + int inside = len != 0 && addr >= WBASE && end <= (unsigned __int128)WBASE + WSIZE; + if ((rc == CK_DMAW_OK) != inside) mismatch++; + ck_dmaw_ring_init(&r, slots, 8); + uint32_t idx; + if (ck_dmaw_ring_post(&r, &w, addr, (uint32_t)len, 0, &idx) == CK_DMAW_OK) { + if (ck_dmaw_ring_emit(&r, &w, idx, &d) == CK_DMAW_OK) { + emitted++; + unsigned __int128 e = (unsigned __int128)d.addr + d.len; + if (d.addr < WBASE || e > (unsigned __int128)WBASE + WSIZE || d.len == 0) escaped++; + } + } + } + CHECK(mismatch == 0); + CHECK(escaped == 0); + CHECK(emitted > 0); +} + +int main(void) +{ + mem = malloc(WSIZE); + if (!mem) return 2; + t_init(); + t_validate(); + t_alloc(); + t_ring(); + t_property(); + free(mem); + return ck_t_verdict("test_dma_window"); +} From b9803cf9a692283e38ea56f87685e38bf54b844f Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Tue, 13 Oct 2026 10:00:52 -0500 Subject: [PATCH 2/2] crumb compile (pinned d0f0b95d) Co-Authored-By: Claude Opus 5.5 --- .crumb | 18 +++++++++--------- native/.crumb | 16 ++++++++-------- native/kernel/.crumb | 22 +++++++++++----------- native/kernel/dev/.crumb | 10 +++++----- native/kernel/tests/.crumb | 10 +++++----- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/.crumb b/.crumb index 69825bd6..39da37e2 100644 --- a/.crumb +++ b/.crumb @@ -33,27 +33,27 @@ "purpose": "Directory docs. Purpose not yet described by a human." }, { - "digest": "4b3233c5f18d4c11224b31eaf4a33151f7cb010b854bb2acc8462ae83a030fcf", - "last_commit": "7dad96f", + "digest": "36d90b1bdc1a7e33efc7d385e66c3301de434c889046cc6855725eabfceb31ff", + "last_commit": "4b74ebf", "name": "native", "purpose": "Directory native. Purpose not yet described by a human." }, { "digest": "7db7f93211cc0ea2a7dab1657474188f3040f71d568d0e41bc21b931cd28e6b5", - "last_commit": "d3fcbcd", + "last_commit": "e059bfe", "name": "scripts", "purpose": "Directory scripts. Purpose not yet described by a human." } ], - "children_root": "sha256:748cb6ace95c1a41f4c32de2271e8d5bd33a33caf1f1ba7e238818a63a8e56ca", - "compiled_at": "2026-10-13T14:40:44Z", + "children_root": "sha256:dcdd12bc141cebd89b36ad1c4caab70017daa3bb6936774e4bf57c688770abf3", + "compiled_at": "2026-10-13T15:00:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:c4247a4ba28e5d57b2b12bb432e9b12e81b1e6d906cb2795525ee8c344c4dc08", + "digest": "sha256:7b1327342af743273c65bd8e9c7c501f5e583d87c030c29cacf7a6a4fe572fc8", "evidence_files": 156, "evidence_root": "sha256:cd53ab119be9de6ce28c4f5949b106c9622f1fe105611de3de21c3bce7932694", - "files": 3149, - "generated_at_commit": "1fd895aef29f2163f075e7b35bb13fa2c9389192", - "source_tree": "sha256:0a4d11d103fefbf939168b2ba6b77a604859ed7c8a8f79d6981d8343faa93ca8" + "files": 3152, + "generated_at_commit": "4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a", + "source_tree": "sha256:4a8e325c0973c835b344bee654dab3a291ac4c948dc95b23a29060c713827e7a" }, "owner_lane": "", "provenance": { diff --git a/native/.crumb b/native/.crumb index e43b170b..dcc5e6f2 100644 --- a/native/.crumb +++ b/native/.crumb @@ -79,8 +79,8 @@ "purpose": "Directory native/disk. Purpose not yet described by a human." }, { - "digest": "d64a164129945f560d4500f05bf71145a6b289b4f2b03b51af3c03c36099a367", - "last_commit": "7dad96f", + "digest": "8fabc55ffb5f66c05299aa5a7eb1f53a7944198f49358f9c032d5a89b9e6227b", + "last_commit": "4b74ebf", "name": "kernel", "purpose": "Directory native/kernel. Purpose not yet described by a human." }, @@ -109,15 +109,15 @@ "purpose": "Directory native/store. Purpose not yet described by a human." } ], - "children_root": "sha256:525a54e64fb825e93dcf221878fc294e343afd1fb951c562834195de3b61f0d1", - "compiled_at": "2026-10-13T14:40:44Z", + "children_root": "sha256:29c901c939f8e26a5f29c603af36860484117befeb833b656f25b69f299c9f08", + "compiled_at": "2026-10-13T15:00:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:4b3233c5f18d4c11224b31eaf4a33151f7cb010b854bb2acc8462ae83a030fcf", + "digest": "sha256:36d90b1bdc1a7e33efc7d385e66c3301de434c889046cc6855725eabfceb31ff", "evidence_files": 4, "evidence_root": "sha256:e410b845507e00568e15b51a5797af32a97e0e4b8ab4e8487bb7c98e7e0cf335", - "files": 515, - "generated_at_commit": "1fd895aef29f2163f075e7b35bb13fa2c9389192", - "source_tree": "sha256:5d12b16d90a1fe7d62557c24e9495a572918d161cb2f1e80feb3130051bce863" + "files": 518, + "generated_at_commit": "4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a", + "source_tree": "sha256:58f95fcaf10ffc25eb6cfdd4c6409d057a97c0bd4dc06980d4f6fe9a45db14e0" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/.crumb b/native/kernel/.crumb index 439ccf93..1f606839 100644 --- a/native/kernel/.crumb +++ b/native/kernel/.crumb @@ -58,13 +58,13 @@ }, { "digest": "59ed62d9cc65c8b4a4fd0a8abef018cc6fc05eba56ead699ad50ae25cbfd4e3a", - "last_commit": "d3fcbcd", + "last_commit": "e059bfe", "name": "core", "purpose": "Directory native/kernel/core. Purpose not yet described by a human." }, { - "digest": "ce05eacbe36c1ae486f78c41446fe810a33aa95e152437b7d95ebf60437802a8", - "last_commit": "2529bc0", + "digest": "f860bfc274faf7b2ae08dbd4c51bf71f57825ed15725c35ae549c20cb01b9489", + "last_commit": "4b74ebf", "name": "dev", "purpose": "Directory native/kernel/dev. Purpose not yet described by a human." }, @@ -87,8 +87,8 @@ "purpose": "Directory native/kernel/svc. Purpose not yet described by a human." }, { - "digest": "5af2f3acb130b4996f485e8ceeb838a3447ced577272c9d455861007537ce6e8", - "last_commit": "65466db", + "digest": "06a5ce6933f29268a72b8617e288985f4335c21c90707a65179a45da070a69c6", + "last_commit": "4b74ebf", "name": "tests", "purpose": "Directory native/kernel/tests. Purpose not yet described by a human." }, @@ -99,15 +99,15 @@ "purpose": "Hosted (Linux-built) checkers for the native kernel, run by `make -C native/kernel test`; nothin" } ], - "children_root": "sha256:fe2c7cc24f5d8315fef2e2029545ed8f6589d0e7a0f9b7aac974cf91530f6ff5", - "compiled_at": "2026-10-13T14:40:44Z", + "children_root": "sha256:cfe8c1e7a598ec42431650dceabc0285b737d3f78a71b44192db2284d12ec30a", + "compiled_at": "2026-10-13T15:00:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:d64a164129945f560d4500f05bf71145a6b289b4f2b03b51af3c03c36099a367", + "digest": "sha256:8fabc55ffb5f66c05299aa5a7eb1f53a7944198f49358f9c032d5a89b9e6227b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 338, - "generated_at_commit": "1fd895aef29f2163f075e7b35bb13fa2c9389192", - "source_tree": "sha256:caa23b6a8c570873c3295a5a811040423d277f34b2ebdd710b0a42b2a77b65b7" + "files": 341, + "generated_at_commit": "4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a", + "source_tree": "sha256:a52fa5dc24c1eea59bbde95be175a7e7d1f4a63e5c970534ef542afbbaed2cef" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/dev/.crumb b/native/kernel/dev/.crumb index 7758279d..332b67cc 100644 --- a/native/kernel/dev/.crumb +++ b/native/kernel/dev/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T12:30:58Z", + "compiled_at": "2026-10-13T15:00:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:ce05eacbe36c1ae486f78c41446fe810a33aa95e152437b7d95ebf60437802a8", + "digest": "sha256:f860bfc274faf7b2ae08dbd4c51bf71f57825ed15725c35ae549c20cb01b9489", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 32, - "generated_at_commit": "a01f365f16ea16bb4843f97b031ee72b4e028389", - "source_tree": "sha256:3c12c30529809e47200d0717160a804e89f7d310a93562d2c55d340b8cb0888b" + "files": 34, + "generated_at_commit": "4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a", + "source_tree": "sha256:db5a9017708120b2a57784caac8fef5f910e560f700fa1f7e4099beec6731b9c" }, "owner_lane": "", "provenance": { diff --git a/native/kernel/tests/.crumb b/native/kernel/tests/.crumb index bc27362b..a0b86413 100644 --- a/native/kernel/tests/.crumb +++ b/native/kernel/tests/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-13T13:46:16Z", + "compiled_at": "2026-10-13T15:00:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:5af2f3acb130b4996f485e8ceeb838a3447ced577272c9d455861007537ce6e8", + "digest": "sha256:06a5ce6933f29268a72b8617e288985f4335c21c90707a65179a45da070a69c6", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 157, - "generated_at_commit": "b46b6ba4c5f031ff1d0d8cf3ea31b5093fbd2f4f", - "source_tree": "sha256:8fce5b9f2f20a9abeaa8d45c12a52f2e8a2a59aeef9b759940246d5ecd04499d" + "files": 158, + "generated_at_commit": "4b74ebf18be00aa0ae3cf512ff1368f1590ddc8a", + "source_tree": "sha256:5e6e65d312be0fcae4c78d1216e269018b1a8b88ad5ec044f254ae208d34b8b1" }, "owner_lane": "", "provenance": {