From 16557a9e72d78472ac57fdb1e7989bbd12be8b94 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Wed, 14 Oct 2026 08:32:32 -0500 Subject: [PATCH 1/4] uci: unfence UCI register reads in src/net/uci; UCI_READ_FENCE=1 restores them The plain UCI adapter put a uci_fence after every register access. Reads need none (the queues advance on the read, DATA_AV/STAT_AV are polled before every byte); measured on the U64E with the fenced 6510 comb build, read fences were 200 s of 363.6 s on the 751,823 B wiki page (#296's fairness measurement). Every WRITE fence stays, and so do the push rule, the shared uci_fence macro and every TOD-bounded wait. The 15 read sites in net.s (5) and uci_cmd.s (10) now use uci_read_fence (or an .ifdef around their jsr uci_settle). The trust store's uci_dos.s reads are left fenced: that path cannot be exercised on the C64U (no SD card), so it is not changed here. UCI_READ_FENCE=1 (BACKEND=uci only, in flags.stamp) assembles the previous images byte for byte: uci-onchip 375f9480, uci-comb 74a7d358, plain uci b4e4e6fb. uci-m3 (44648860) and ip65 are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) --- CLAUDE.md | 8 +++++--- Makefile | 16 ++++++++++++++++ src/net/uci/net.s | 10 +++++----- src/net/uci/uci_cmd.s | 20 +++++++++++++++----- src/net/uci/uci_regs.inc | 13 +++++++++++++ 5 files changed, 54 insertions(+), 13 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 655f6073..80996216 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -368,9 +368,11 @@ can catch a regression here — the guard is `tools/uci/boot_check.py`. ### Firmware quirk — FPGA register timing (delay-loop fence) -The UCI FPGA needs a minimum gap between register accesses regardless of -CPU clock. `uci_fence` (`uci_regs.inc`, OUTER=5, INNER=217, ~85 us at -64 MHz) follows every access to `$DF1C-$DF1F`. INNER=217 covers both the +The UCI FPGA needs a minimum gap after register WRITES regardless of CPU +clock. `uci_fence` (`uci_regs.inc`, OUTER=5, INNER=217, ~85 us at 64 MHz) +follows every write to `$DF1C`/`$DF1D`; reads are unfenced on both UCI +backends (`uci_read_fence`; `UCI_READ_FENCE=1` / `M3_READ_FENCE=1` restore +the old images byte for byte; the trust store's `uci_dos.s` still fences). INNER=217 covers both the U64E (~38 us floor) and the C64 Ultimate (floor bracketed 51.6 FAIL / 62.9 PASS us at 64 MHz; the symptom below the floor is a silently lost TCP_CONNECT push → `NO_SOCKET`). 48 MHz (U64E) and 64 MHz (C64U) verified. diff --git a/Makefile b/Makefile index 1c29d30b..01703e6e 100644 --- a/Makefile +++ b/Makefile @@ -55,6 +55,19 @@ else $(foreach k,M3_READ_FENCE M3_TRACE,$(if $(filter-out 0,$($(k))),$(error $(k)=$($(k)) applies only to BACKEND=uci-m3))) endif +# UCI_READ_FENCE=1 (BACKEND=uci): a uci_fence after every UCI register READ +# in src/net/uci too, as before (src/net/uci/uci_regs.inc, uci_read_fence): +# the A/B arm and the fallback. Refused on the other backends; uci-m3 has +# its own M3_READ_FENCE. +ifneq ($(filter-out 0 1,$(UCI_READ_FENCE)),) +$(error UCI_READ_FENCE must be 1 or 0/unset) +endif +ifneq ($(BACKEND),uci) +ifeq ($(UCI_READ_FENCE),1) +$(error UCI_READ_FENCE=1 applies only to BACKEND=uci) +endif +endif + # X25519 is always the libs/x25519 sibling (issue #245 retired the in-tree # src/crypto/{x25519,fe25519}.s). The old opt-in knob is refused rather than # ignored, so a stale command line cannot believe it selected something. @@ -120,6 +133,9 @@ CA65FLAGS += -D BACKEND_UCI=1 # patience), so the check has the least to do exactly where it does not fit. # Documented in README and tracked on #135 rather than left to be discovered. CA65FLAGS += -D X509_VERIFY_NAME=1 +ifeq ($(UCI_READ_FENCE),1) +CA65FLAGS += -D UCI_READ_FENCE=1 +endif else ifeq ($(BACKEND),uci-m3) # A UCI build (the typed-target prompt, the REU body sink and the viewer # come with BACKEND_UCI) plus the M3 switch. -I src/net/uci after the diff --git a/src/net/uci/net.s b/src/net/uci/net.s index aee30823..5e0204fe 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -116,7 +116,7 @@ net_init: php lda UCI_ID - uci_fence ; settle before comparing ID + uci_read_fence ; settle before comparing ID cmp #UCI_ID_VALUE beq @present @@ -277,13 +277,13 @@ net_poll: ldy #$00 @hdr_loop: lda UCI_STATUS - uci_fence ; settle before testing DATA_AV + uci_read_fence ; settle before testing DATA_AV and #UCI_STAT_DATA_AV bne @hdr_got ; branch past trampoline jmp @hdr_done_short ; long branch: fence too wide for BEQ @hdr_got: lda UCI_RESP_DATA - uci_fence ; settle before storing header byte + uci_read_fence ; settle before storing header byte sta uci_read_hdr,y iny cpy #2 @@ -415,7 +415,7 @@ net_poll: ; FIFO drained mid-record we bail (shouldn't happen if firmware ; honored actual_len but we defend anyway). lda UCI_STATUS - uci_fence ; settle before testing DATA_AV + uci_read_fence ; settle before testing DATA_AV and #UCI_STAT_DATA_AV bne @have_byte jmp @done_data @@ -431,7 +431,7 @@ net_poll: sta @rb_store+2 lda UCI_RESP_DATA - uci_fence ; settle before storing data byte + uci_read_fence ; settle before storing data byte @rb_store: sta $ffff ; SMC: patched each byte diff --git a/src/net/uci/uci_cmd.s b/src/net/uci/uci_cmd.s index d83e9b14..70e262f5 100644 --- a/src/net/uci/uci_cmd.s +++ b/src/net/uci/uci_cmd.s @@ -219,7 +219,7 @@ uci_wait_idle: sta @wi_elapsed @wi_loop: lda UCI_STATUS - uci_fence ; settle read before testing bits + uci_read_fence ; settle read before testing bits and #(UCI_STAT_STATE | UCI_STAT_ABORT_PENDING | UCI_STAT_CMD_BUSY) ; $35 beq @idle_done @@ -269,7 +269,7 @@ uci_wait_not_busy: sta @wnb_elapsed @wnb_loop: lda UCI_STATUS - uci_fence ; settle read before testing bits + uci_read_fence ; settle read before testing bits and #UCI_STAT_CMD_BUSY beq @wnb_done @@ -393,7 +393,7 @@ uci_wait_reply: sta @wr_elapsed @wr_loop: lda UCI_STATUS - uci_fence ; settle read before testing bits + uci_read_fence ; settle read before testing bits and #(UCI_STAT_REPLY_VALID | UCI_STAT_ERROR) ; $28 bne @wr_done @@ -425,7 +425,9 @@ uci_wait_reply: ; ============================================================================= uci_check_err: lda UCI_STATUS +.ifdef UCI_READ_FENCE jsr uci_settle ; settle before testing error bit +.endif and #UCI_STAT_ERROR bne @has_err clc @@ -537,11 +539,11 @@ uci_read_resp_bytes: cpy uci_resp_max bcs @rd_done lda UCI_STATUS - uci_fence ; settle before testing DATA_AV + uci_read_fence ; settle before testing DATA_AV and #UCI_STAT_DATA_AV beq @rd_done ; reply shorter than max: it is final lda UCI_RESP_DATA - uci_fence ; settle before storing/looping + uci_read_fence ; settle before storing/looping @rd_store: sta $FFFF,y ; SMC: dst low/high patched above iny @@ -589,14 +591,18 @@ uci_drain_resp: sta @drn_elapsed @drn_loop: lda UCI_STATUS +.ifdef UCI_READ_FENCE jsr uci_settle ; settle before testing DATA_AV +.endif and #UCI_STAT_DATA_AV bne @drn_have clc rts @drn_have: lda UCI_RESP_DATA +.ifdef UCI_READ_FENCE jsr uci_settle +.endif ; NO PER-BYTE ACK — see the header. The read advanced the FIFO. ; Check TOD for elapsed tenths. Latch (HOUR) then read TENTHS. @@ -671,7 +677,9 @@ uci_drain_status: sta @dst_elapsed @dst_loop: lda UCI_STATUS +.ifdef UCI_READ_FENCE jsr uci_settle ; settle before testing STAT_AV +.endif and #UCI_STAT_STAT_AV bne @dst_have jsr @dst_commit @@ -679,7 +687,9 @@ uci_drain_status: rts @dst_have: lda UCI_STATUS_DATA +.ifdef UCI_READ_FENCE jsr uci_settle +.endif ; Last two bytes seen, non-sticky: net_poll reads the errno of a ; "02,NO DATA: n" line off them (#253). ldx uci_status_tail+1 diff --git a/src/net/uci/uci_regs.inc b/src/net/uci/uci_regs.inc index 3c9128ee..06156128 100644 --- a/src/net/uci/uci_regs.inc +++ b/src/net/uci/uci_regs.inc @@ -114,6 +114,19 @@ UCI_FENCE_INNER = 217 ; inner loop iterations pla ; restore A .endmacro +; uci_read_fence — the fence after a READ of $DF1C-$DF1F in src/net/uci. +; The FPGA's gap is needed after a WRITE (above); reads are unfenced: the +; queues advance on the read itself and DATA_AV / STAT_AV are polled before +; every byte. `make BACKEND=uci UCI_READ_FENCE=1` puts the fence back and +; assembles the previous products byte for byte (the A/B arm and the +; fallback). Sites that fence through `jsr uci_settle` use .ifdef directly. +; uci-m3 has its own knob (M3_READ_FENCE, src/net/uci-m3/m3.inc). +.macro uci_read_fence +.ifdef UCI_READ_FENCE + uci_fence +.endif +.endmacro + ; ============================================================================= ; Command IDs (issued as the first command byte after selecting the target) ; Phase 2+ will use these; Phase 1b keeps them here purely as equates. From fa685cbd9c0ca086fa437deb62c8adfcc347454f Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Wed, 14 Oct 2026 08:36:01 -0500 Subject: [PATCH 2/4] uci: check_uci_read_pacing + mutant runner; label the kept stage fence tools/check_uci_read_pacing.py runs the shipped adapter (net_init, connect, data / idle / EOF / reset polls, send, close) on test_uci_abort_recovery's interpreter and model, and logs every $DF1C-$DF1F access with the uci_fence executions after it: every write must be fenced; on the default image no read is, except the fence net_poll keeps before its header read (now labelled uci_poll_stage_fence; a label, no bytes); on a UCI_READ_FENCE=1 image every read is. --expect flips the expectation to show it red on the other image. tools/mutate_uci_read_pacing.sh: 5 source mutants (two write fences dropped, three read fences re-added); all killed, control 6/6. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci/net.s | 1 + tools/check_uci_read_pacing.py | 169 ++++++++++++++++++++++++++++++++ tools/mutate_uci_read_pacing.sh | 29 ++++++ 3 files changed, 199 insertions(+) create mode 100644 tools/check_uci_read_pacing.py create mode 100755 tools/mutate_uci_read_pacing.sh diff --git a/src/net/uci/net.s b/src/net/uci/net.s index 5e0204fe..47c93ae7 100644 --- a/src/net/uci/net.s +++ b/src/net/uci/net.s @@ -273,6 +273,7 @@ net_poll: ; SMC dst. Loop style matches uci_read_resp_bytes — tight-poll ; DATA_AV and read UCI_RESP_DATA; the firmware FIFO auto-advances ; on read (Phase 2 finding), so NO per-byte DATA_ACC. +uci_poll_stage_fence: ; kept: the one fence after a read that is uci_fence ; give firmware time to stage response ldy #$00 @hdr_loop: diff --git a/tools/check_uci_read_pacing.py b/tools/check_uci_read_pacing.py new file mode 100644 index 00000000..0fbc127f --- /dev/null +++ b/tools/check_uci_read_pacing.py @@ -0,0 +1,169 @@ +#!/usr/bin/env python3 +"""BACKEND=uci read pacing: a uci_fence after every UCI register WRITE, +none after a READ (src/net/uci/uci_regs.inc, uci_read_fence). + +Runs the SHIPPED machine code (build/c64-https.prg at build/labels.txt's +addresses) on test_uci_abort_recovery.py's interpreter and Command +Interface model, through net_init, a connect, data / idle / EOF / reset +polls, a send and a close, and logs every access to $DF1C-$DF1F with the +uci_fence executions that follow it before the next access. A fence is +recognised by its bytes as assembled (that module's FENCE), whether it is +inline or inside uci_settle. + + every image each WRITE to $DF1C/$DF1D is followed by a fence before + the next UCI access. + default no READ is followed by a fence, except the one kept before + net_poll's header read (label uci_poll_stage_fence). + UCI_READ_FENCE=1 (flags.stamp) + every READ is followed by a fence (the previous products). + +What it does not see: a delay that is not a uci_fence (a NOP sled, a +loop), the trust store's uci_dos.s (not on these flows; still fenced), +and anything about the FPGA's real timing -- the hardware A/B in the PR +is that evidence. + + make BACKEND=uci ... && python3 tools/check_uci_read_pacing.py [--expect default|fenced] + +--expect overrides what flags.stamp says, to show the check red on the +other image. Standalone, like tools/check_m3_client.py. Exit 0 pass, +1 fail, 2 cannot run. +""" +from __future__ import annotations + +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parent)) +import test_uci_abort_recovery as A # noqa: E402 +import test_uci_data_acc as base # noqa: E402 + +UCI_REGS = range(0xDF1C, 0xDF20) + + +class Log: + def __init__(self): + self.events = [] # [kind, addr, [fence pcs after it]] + + def access(self, kind, addr): + if addr in UCI_REGS: + self.events.append([kind, addr, []]) + + def fence(self, pc): + if self.events: + self.events[-1][2].append(pc) + + +def _traced(model, log): + read, write = model.read, model.write + + def r(addr): + log.access("r", addr) + return read(addr) + + def w(addr, value): + log.access("w", addr) + return write(addr, value) + model.read, model.write = r, w + return model + + +class PacingCPU(A.FastCPU): + log = None + + def step(self): + pc = self.pc + ram = self.mem.ram + if ram[pc] == 0x48 and ram[pc:pc + len(A.FENCE)] == A.FENCE: + self.log.fence(pc) + super().step() + + +def _machine(replies, log): + model = _traced(A.AbortModel(replies), log) + cpu, mem, labels = A._require(model) + pcpu = PacingCPU(mem) + pcpu.log = log + return pcpu, mem, labels, model + + +def flows(): + """(name, Log) for each traced session.""" + out = [] + log = Log() + cpu, mem, labels, uci = _machine([A.CONNECT_OK], log) + cpu.call(labels["net_init"]) + A._assert_connected(cpu, mem, uci, labels, "pacing: connect") + out.append(("init+connect", log)) + for name, reply in (("poll data", (b"\x40\x00" + bytes(range(64)), A.OK)), + ("poll idle", (b"\xff\xff", b"02,NO DATA: 11")), + ("poll EOF", (b"\x00\x00", b"01,CONNECTION CLOSED BY HOST")), + ("poll reset", (b"\xff\xff", b"02,NO DATA: 104"))): + log = Log() + cpu, mem, labels, uci = _machine([reply], log) + mem.write(labels["uci_socket_id"], 1) + mem.write(labels["net_tcp_state"], A.NET_TCP_CONNECTED) + cpu.call(labels["net_poll"], budget=8_000_000) + out.append((name, log)) + log = Log() + cpu, mem, labels, uci = _machine([(b"\x3a\x00", A.OK), (b"", A.OK)], log) + mem.write(labels["uci_socket_id"], 1) + mem.write(labels["net_tcp_state"], A.NET_TCP_CONNECTED) + mem.write(labels["net_send_len"], 58) + mem.write(labels["net_send_len"] + 1, 0) + cpu.a, cpu.x = 0x00, 0x04 + cpu.call(labels["net_tcp_send"], budget=4_000_000) + cpu.call(labels["net_tcp_close"], budget=4_000_000) + out.append(("send+close", log)) + return out, labels + + +def _fenced_image(): + stamp = base.LABELS.parent / "flags.stamp" + for line in stamp.read_text().splitlines(): + if line.startswith("CA65FLAGS="): + return "-D UCI_READ_FENCE" in line + raise A.Unavailable("%s has no CA65FLAGS line" % stamp) + + +def main(argv) -> int: + expect = None + if len(argv) == 3 and argv[1] == "--expect" and argv[2] in ("default", "fenced"): + expect = argv[2] == "fenced" + elif len(argv) != 1: + print(__doc__) + return 2 + try: + fenced = _fenced_image() if expect is None else expect + traces, labels = flows() + except (A.Unavailable, FileNotFoundError) as exc: + print("COULD NOT RUN: %s" % exc) + return 2 + keep = labels.get("uci_poll_stage_fence") + if keep is None: + print("COULD NOT RUN: uci_poll_stage_fence is not in labels.txt") + return 2 + fails = 0 + for name, log in traces: + ev = log.events + writes = [e for e in ev[:-1] if e[0] == "w"] + reads = [e for e in ev[:-1] if e[0] == "r"] + bare = [e for e in writes if not e[2]] + if fenced: + bad = [e for e in reads if not e[2]] + what = "unfenced read(s) in an UCI_READ_FENCE=1 image" + else: + bad = [e for e in reads if any(pc != keep for pc in e[2])] + what = "fenced read(s) in the default image" + ok = len(writes) > 3 and not bare and not bad + fails += not ok + print("%s %-13s %3d writes, %4d reads; %s%s" % ( + "PASS" if ok else "FAIL", name, len(writes), len(reads), + "%d write(s) with no fence after" % len(bare) if bare else "writes fenced", + "; %d %s, first $%04X" % (len(bad), what, bad[0][1]) if bad else "")) + print("\n%d/%d flows pass (%s image)" % (len(traces) - fails, len(traces), + "fenced" if fenced else "default")) + return 1 if fails else 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/tools/mutate_uci_read_pacing.sh b/tools/mutate_uci_read_pacing.sh new file mode 100755 index 00000000..423039dc --- /dev/null +++ b/tools/mutate_uci_read_pacing.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash +# tools/mutate_uci_read_pacing.sh -- break the BACKEND=uci read-pacing rule +# in the source, one mutant at a time; rebuild (make BACKEND=uci, in build/: +# it OVERWRITES the current build) and require tools/check_uci_read_pacing.py +# to go red (exit 1) on each. The source is restored after every mutant. +# Exit 0 only if every mutant is killed and the control passes. +cd "$(dirname "$0")/.." || exit 1 +fails=0 +mut() { # name file old new + python3 - "$2" "$3" "$4" <<'PY' || { echo "BROKEN $1"; fails=1; return; } +import sys +f, old, new = sys.argv[1], sys.argv[2].encode().decode("unicode_escape"), sys.argv[3].encode().decode("unicode_escape") +s = open(f).read(); assert s.count(old) == 1, (f, old, s.count(old)); open(f + ".orig", "w").write(s); open(f, "w").write(s.replace(old, new)) +PY + rm -rf build; make BACKEND=uci >build-mutant.log 2>&1 || { echo "BROKEN $1 (build)"; fails=1; mv "$2.orig" "$2"; return; } + rm -f build-mutant.log + out=$(python3 tools/check_uci_read_pacing.py 2>&1); rc=$? + mv "$2.orig" "$2" + if [ $rc -eq 1 ]; then echo "KILLED $1: $(echo "$out" | grep -m1 FAIL | cut -c1-120)"; else echo "SURVIVED $1 (rc=$rc)"; fails=1; fi +} +mut put-unfenced src/net/uci/uci_cmd.s "uci_put_byte:\n sta UCI_CMD_DATA\n uci_fence\n" "uci_put_byte:\n sta UCI_CMD_DATA\n" +mut begin-unfenced src/net/uci/uci_cmd.s "uci_begin_cmd:\n sta UCI_CMD_DATA\n uci_fence\n" "uci_begin_cmd:\n sta UCI_CMD_DATA\n" +mut copy-read-fenced src/net/uci/net.s " lda UCI_RESP_DATA\n uci_read_fence ; settle before storing data byte" " lda UCI_RESP_DATA\n uci_fence" +mut status-wait-fenced src/net/uci/uci_cmd.s "@wr_loop:\n lda UCI_STATUS\n uci_read_fence" "@wr_loop:\n lda UCI_STATUS\n uci_fence" +mut statline-fenced src/net/uci/uci_cmd.s " lda UCI_STATUS_DATA\n.ifdef UCI_READ_FENCE\n jsr uci_settle\n.endif" " lda UCI_STATUS_DATA\n jsr uci_settle" +rm -rf build; make BACKEND=uci >/dev/null 2>&1 +python3 tools/check_uci_read_pacing.py >/dev/null || fails=1 +echo "control $(shasum -a 256 build/c64-https.prg | cut -c1-8): $(python3 tools/check_uci_read_pacing.py | tail -1)" +exit $fails From cdc35a3643ef525e7aab381c87eabbe0f520528a Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Wed, 14 Oct 2026 12:42:52 -0500 Subject: [PATCH 3/4] uci: hold the trust store's DOS path to the read-pacing rule; rig prints PRG sha256 adv-299 finding 1: uci_dos.s keeps the fence after its own three reads, but every DOS transaction also runs through the primitives this branch unfenced (uci_wait_idle, uci_wait_reply, uci_check_err, uci_drain_resp, uci_drain_status). So on the default image the trust store's traffic (trust_pre on every 'G' in both shipped products) has unfenced reads too. check_uci_read_pacing.py now runs the store's DOS traffic on a TRUST_STORE=1 image (load on an empty medium, stage + save, reload; on test_trust_store_6502.py's DOS model) and holds it to the same rule: no read fenced on the default image except uci_dos.s's own three, which are labelled (uci_dos_rd_part / _data / _state; labels, no bytes) and must stay fenced on every image. mutate_uci_read_pacing.sh builds the onchip store image and adds dos-own-unfenced and check-err-fenced: 7/7 killed. rig_https_local.py prints the sha256 of the PRG bytes it loads (adv-299 finding 3: a rebuild of build/ during a run must not be mistakable for what ran). Co-Authored-By: Claude Opus 5.5 (1M context) --- src/net/uci/uci_dos.s | 5 +- tools/check_uci_read_pacing.py | 94 ++++++++++++++++++++++++++++++--- tools/mutate_uci_read_pacing.sh | 10 ++-- tools/uci/rig_https_local.py | 4 ++ 4 files changed, 101 insertions(+), 12 deletions(-) diff --git a/src/net/uci/uci_dos.s b/src/net/uci/uci_dos.s index b693184b..359b40da 100644 --- a/src/net/uci/uci_dos.s +++ b/src/net/uci/uci_dos.s @@ -113,7 +113,8 @@ dos_finish: jsr uci_check_err bcs dos_reject ; push rejected (interface not idle) dos_part: - lda UCI_STATUS +uci_dos_rd_part: ; uci_dos.s's own reads keep their fence on + lda UCI_STATUS ; every image (check_uci_read_pacing.py) jsr uci_settle bpl dos_status ; DATA_AV clear lda dos_cap ; not a read (dos_hdr zeroed the cap): @@ -126,6 +127,7 @@ dos_part: cmp dos_cap+1 beq dos_reject ; one byte more than asked for @store: +uci_dos_rd_data: lda UCI_RESP_DATA jsr uci_settle dos_store: @@ -140,6 +142,7 @@ dos_store: dos_status: jsr uci_drain_status bcs dos_rts +uci_dos_rd_state: lda UCI_STATUS jsr uci_settle and #UCI_STAT_STATE diff --git a/tools/check_uci_read_pacing.py b/tools/check_uci_read_pacing.py index 0fbc127f..d6a8e205 100644 --- a/tools/check_uci_read_pacing.py +++ b/tools/check_uci_read_pacing.py @@ -10,6 +10,16 @@ recognised by its bytes as assembled (that module's FENCE), whether it is inline or inside uci_settle. +On a TRUST_STORE=1 image it also runs the trust store's DOS traffic +(trust_store_load on an empty medium, a stage + save, a reload) on +test_trust_store_6502.py's DOS model: the path every 'G' takes in the +shipped products (trust_pre). uci_dos.s keeps the fences after its own +four reads, but it goes through the same primitives as the network path +(uci_wait_idle, uci_wait_reply, uci_check_err, uci_drain_resp, +uci_drain_status), so on the default image those reads are unfenced too, +and they are held to the same rule; uci_dos.s's own four are listed by +address in DOS_FENCED and must stay fenced on every image. + every image each WRITE to $DF1C/$DF1D is followed by a fence before the next UCI access. default no READ is followed by a fence, except the one kept before @@ -18,9 +28,8 @@ every READ is followed by a fence (the previous products). What it does not see: a delay that is not a uci_fence (a NOP sled, a -loop), the trust store's uci_dos.s (not on these flows; still fenced), -and anything about the FPGA's real timing -- the hardware A/B in the PR -is that evidence. +loop), and anything about the FPGA's real timing -- the hardware runs in +the PR are that evidence. make BACKEND=uci ... && python3 tools/check_uci_read_pacing.py [--expect default|fenced] @@ -42,11 +51,15 @@ class Log: def __init__(self): - self.events = [] # [kind, addr, [fence pcs after it]] + self.events = [] # [kind, addr, [fence pcs after it], insn pc] + self.cpu = None # set once the machine exists def access(self, kind, addr): if addr in UCI_REGS: - self.events.append([kind, addr, []]) + # every UCI access is an absolute LDA/STA: 3 bytes, and the + # operand has been fetched when the data cycle runs + pc = (self.cpu.pc - 3) & 0xFFFF if self.cpu else None + self.events.append([kind, addr, [], pc]) def fence(self, pc): if self.events: @@ -83,6 +96,7 @@ def _machine(replies, log): cpu, mem, labels = A._require(model) pcpu = PacingCPU(mem) pcpu.log = log + log.cpu = pcpu return pcpu, mem, labels, model @@ -117,6 +131,59 @@ def flows(): return out, labels +def trust_flows(): + """[(name, Log)] for the trust store's DOS traffic, or None when the + image has no trust store.""" + import test_trust_store_6502 as TS + env = TS.Env() + if "trust_store_load" not in env.labels: + return None + out = [] + for name in ("trust load+save", "trust reload"): + log = Log() + dos = TS.Dos() if name == "trust load+save" else out_dos + m = env.machine(dos) + mem, cpu = m.mem, m.cpu + log.cpu = cpu + r0, w0 = mem.read, mem.write + + def r(addr, r0=r0, log=log): + if (addr & 0xFFFF) in UCI_REGS: + log.access("r", addr & 0xFFFF) + return r0(addr) + + def w(addr, value, w0=w0, log=log): + if (addr & 0xFFFF) in UCI_REGS: + log.access("w", addr & 0xFFFF) + return w0(addr, value) + mem.read, mem.write = r, w + step0 = cpu.step + + def step(cpu=cpu, step0=step0, log=log): + pc = cpu.pc + if cpu.mem.ram[pc] == 0xE9 and cpu.mem.ram[pc:pc + 4] == TS.FENCE: + log.fence(pc) + step0() + cpu.step = step + if name == "trust load+save": + m.load("github.com") + m.stage(TS.rec("anything", TS.SPKI_1).pack()) + if m.save(): + raise A.Unavailable("trust flow: the save failed in the model") + out_dos = dos + else: + c, a = m.load("github.com") + if c: + raise A.Unavailable("trust flow: the reload failed in the model") + out.append((name, log)) + return out + + +#: uci_dos.s's own reads (labels in src/net/uci/uci_dos.s): they keep +#: their `jsr uci_settle` on every image, and must. +DOS_OWN = ("uci_dos_rd_part", "uci_dos_rd_data", "uci_dos_rd_state") + + def _fenced_image(): stamp = base.LABELS.parent / "flags.stamp" for line in stamp.read_text().splitlines(): @@ -135,13 +202,19 @@ def main(argv) -> int: try: fenced = _fenced_image() if expect is None else expect traces, labels = flows() + trust = trust_flows() except (A.Unavailable, FileNotFoundError) as exc: print("COULD NOT RUN: %s" % exc) return 2 + if trust is None: + print("(no trust store in this image: trust flows not applicable)") + else: + traces += trust keep = labels.get("uci_poll_stage_fence") if keep is None: print("COULD NOT RUN: uci_poll_stage_fence is not in labels.txt") return 2 + own = {labels[n] for n in DOS_OWN if n in labels} fails = 0 for name, log in traces: ev = log.events @@ -152,14 +225,19 @@ def main(argv) -> int: bad = [e for e in reads if not e[2]] what = "unfenced read(s) in an UCI_READ_FENCE=1 image" else: - bad = [e for e in reads if any(pc != keep for pc in e[2])] + bad = [e for e in reads if e[3] not in own + and any(pc != keep for pc in e[2])] what = "fenced read(s) in the default image" - ok = len(writes) > 3 and not bare and not bad + own_bare = [e for e in reads if e[3] in own and not e[2]] + dos_missing = name.startswith("trust") and not any(e[3] in own for e in reads) + ok = len(writes) > 3 and not bare and not bad and not own_bare and not dos_missing fails += not ok print("%s %-13s %3d writes, %4d reads; %s%s" % ( "PASS" if ok else "FAIL", name, len(writes), len(reads), "%d write(s) with no fence after" % len(bare) if bare else "writes fenced", - "; %d %s, first $%04X" % (len(bad), what, bad[0][1]) if bad else "")) + "; %d %s, first $%04X" % (len(bad), what, bad[0][1]) if bad else "") + + ("; %d of uci_dos.s's own read(s) unfenced" % len(own_bare) if own_bare else "") + + ("; the DOS path never ran" if dos_missing else "")) print("\n%d/%d flows pass (%s image)" % (len(traces) - fails, len(traces), "fenced" if fenced else "default")) return 1 if fails else 0 diff --git a/tools/mutate_uci_read_pacing.sh b/tools/mutate_uci_read_pacing.sh index 423039dc..d562719e 100755 --- a/tools/mutate_uci_read_pacing.sh +++ b/tools/mutate_uci_read_pacing.sh @@ -1,18 +1,20 @@ #!/usr/bin/env bash # tools/mutate_uci_read_pacing.sh -- break the BACKEND=uci read-pacing rule -# in the source, one mutant at a time; rebuild (make BACKEND=uci, in build/: +# in the source, one mutant at a time; rebuild (make BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1, so the +# trust store's DOS flows run too; in build/: # it OVERWRITES the current build) and require tools/check_uci_read_pacing.py # to go red (exit 1) on each. The source is restored after every mutant. # Exit 0 only if every mutant is killed and the control passes. cd "$(dirname "$0")/.." || exit 1 fails=0 +PROFILE="BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1" mut() { # name file old new python3 - "$2" "$3" "$4" <<'PY' || { echo "BROKEN $1"; fails=1; return; } import sys f, old, new = sys.argv[1], sys.argv[2].encode().decode("unicode_escape"), sys.argv[3].encode().decode("unicode_escape") s = open(f).read(); assert s.count(old) == 1, (f, old, s.count(old)); open(f + ".orig", "w").write(s); open(f, "w").write(s.replace(old, new)) PY - rm -rf build; make BACKEND=uci >build-mutant.log 2>&1 || { echo "BROKEN $1 (build)"; fails=1; mv "$2.orig" "$2"; return; } + rm -rf build; make $PROFILE >build-mutant.log 2>&1 || { echo "BROKEN $1 (build)"; fails=1; mv "$2.orig" "$2"; return; } rm -f build-mutant.log out=$(python3 tools/check_uci_read_pacing.py 2>&1); rc=$? mv "$2.orig" "$2" @@ -23,7 +25,9 @@ mut begin-unfenced src/net/uci/uci_cmd.s "uci_begin_cmd:\n sta UCI_CMD_DA mut copy-read-fenced src/net/uci/net.s " lda UCI_RESP_DATA\n uci_read_fence ; settle before storing data byte" " lda UCI_RESP_DATA\n uci_fence" mut status-wait-fenced src/net/uci/uci_cmd.s "@wr_loop:\n lda UCI_STATUS\n uci_read_fence" "@wr_loop:\n lda UCI_STATUS\n uci_fence" mut statline-fenced src/net/uci/uci_cmd.s " lda UCI_STATUS_DATA\n.ifdef UCI_READ_FENCE\n jsr uci_settle\n.endif" " lda UCI_STATUS_DATA\n jsr uci_settle" -rm -rf build; make BACKEND=uci >/dev/null 2>&1 +mut dos-own-unfenced src/net/uci/uci_dos.s "uci_dos_rd_data:\n lda UCI_RESP_DATA\n jsr uci_settle\n" "uci_dos_rd_data:\n lda UCI_RESP_DATA\n" +mut check-err-fenced src/net/uci/uci_cmd.s "uci_check_err:\n lda UCI_STATUS\n.ifdef UCI_READ_FENCE\n jsr uci_settle ; settle before testing error bit\n.endif" "uci_check_err:\n lda UCI_STATUS\n jsr uci_settle" +rm -rf build; make $PROFILE >/dev/null 2>&1 python3 tools/check_uci_read_pacing.py >/dev/null || fails=1 echo "control $(shasum -a 256 build/c64-https.prg | cut -c1-8): $(python3 tools/check_uci_read_pacing.py | tail -1)" exit $fails diff --git a/tools/uci/rig_https_local.py b/tools/uci/rig_https_local.py index 12f03545..85770dee 100644 --- a/tools/uci/rig_https_local.py +++ b/tools/uci/rig_https_local.py @@ -76,6 +76,7 @@ from __future__ import annotations import base64 +import hashlib import datetime import json import os @@ -1453,6 +1454,9 @@ def main() -> int: return 2 prg = PRG_PATH.read_bytes() + # The provenance of THIS run: the bytes it loads, hashed once here, so a + # later rebuild of build/ cannot be mistaken for what ran. + print(f"PRG sha256 : {hashlib.sha256(prg).hexdigest()} ({PRG_PATH})") lock = DeviceLock(HOST) try: From 78e5aea7705cf6fe794c58449ec1d7d7f0e64967 Mon Sep 17 00:00:00 2001 From: JC-000 <3798556+JC-000@users.noreply.github.com> Date: Wed, 14 Oct 2026 14:10:02 -0500 Subject: [PATCH 4/4] check_uci_read_pacing: reach uci_drain_resp's data read; fix the uci_dos count adv-299 round 2 (low): no flow executed the data read at uci_cmd.s @drn_have, so re-fencing it on the default image or unfencing it on a UCI_READ_FENCE=1 image went unseen. New flow "poll overclaim": a 600 B header over a 512 B request, so the copy stops at the request and uci_drain_resp reads the rest. mutate_uci_read_pacing.sh adds drain-resp-fenced (killed: 49 fenced reads in that flow); the reverse, the site unfenced on the fenced image, is red too (49 unfenced reads). The docstring said uci_dos.s keeps four fenced reads; it is three (DOS_OWN). Co-Authored-By: Claude Opus 5.5 (1M context) --- tools/check_uci_read_pacing.py | 13 +++++++++---- tools/mutate_uci_read_pacing.sh | 1 + 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/check_uci_read_pacing.py b/tools/check_uci_read_pacing.py index d6a8e205..ecbb970f 100644 --- a/tools/check_uci_read_pacing.py +++ b/tools/check_uci_read_pacing.py @@ -14,11 +14,12 @@ (trust_store_load on an empty medium, a stage + save, a reload) on test_trust_store_6502.py's DOS model: the path every 'G' takes in the shipped products (trust_pre). uci_dos.s keeps the fences after its own -four reads, but it goes through the same primitives as the network path +three reads, but it goes through the same primitives as the network path (uci_wait_idle, uci_wait_reply, uci_check_err, uci_drain_resp, uci_drain_status), so on the default image those reads are unfenced too, -and they are held to the same rule; uci_dos.s's own four are listed by -address in DOS_FENCED and must stay fenced on every image. +and they are held to the same rule; uci_dos.s's own three reads are +labelled (uci_dos_rd_part / _data / _state, in DOS_OWN) and must stay +fenced on every image. every image each WRITE to $DF1C/$DF1D is followed by a fence before the next UCI access. @@ -111,7 +112,11 @@ def flows(): for name, reply in (("poll data", (b"\x40\x00" + bytes(range(64)), A.OK)), ("poll idle", (b"\xff\xff", b"02,NO DATA: 11")), ("poll EOF", (b"\x00\x00", b"01,CONNECTION CLOSED BY HOST")), - ("poll reset", (b"\xff\xff", b"02,NO DATA: 104"))): + ("poll reset", (b"\xff\xff", b"02,NO DATA: 104")), + # header 600 over a 512 B request: the copy stops at + # the request and uci_drain_resp reads the other 88 + # (its @drn_have data read; no other flow reaches it) + ("poll overclaim", (b"\x58\x02" + bytes(600), A.OK))): log = Log() cpu, mem, labels, uci = _machine([reply], log) mem.write(labels["uci_socket_id"], 1) diff --git a/tools/mutate_uci_read_pacing.sh b/tools/mutate_uci_read_pacing.sh index d562719e..c958c886 100755 --- a/tools/mutate_uci_read_pacing.sh +++ b/tools/mutate_uci_read_pacing.sh @@ -27,6 +27,7 @@ mut status-wait-fenced src/net/uci/uci_cmd.s "@wr_loop:\n lda UCI_STATUS\ mut statline-fenced src/net/uci/uci_cmd.s " lda UCI_STATUS_DATA\n.ifdef UCI_READ_FENCE\n jsr uci_settle\n.endif" " lda UCI_STATUS_DATA\n jsr uci_settle" mut dos-own-unfenced src/net/uci/uci_dos.s "uci_dos_rd_data:\n lda UCI_RESP_DATA\n jsr uci_settle\n" "uci_dos_rd_data:\n lda UCI_RESP_DATA\n" mut check-err-fenced src/net/uci/uci_cmd.s "uci_check_err:\n lda UCI_STATUS\n.ifdef UCI_READ_FENCE\n jsr uci_settle ; settle before testing error bit\n.endif" "uci_check_err:\n lda UCI_STATUS\n jsr uci_settle" +mut drain-resp-fenced src/net/uci/uci_cmd.s "@drn_have:\n lda UCI_RESP_DATA\n.ifdef UCI_READ_FENCE\n jsr uci_settle\n.endif" "@drn_have:\n lda UCI_RESP_DATA\n jsr uci_settle" rm -rf build; make $PROFILE >/dev/null 2>&1 python3 tools/check_uci_read_pacing.py >/dev/null || fails=1 echo "control $(shasum -a 256 build/c64-https.prg | cut -c1-8): $(python3 tools/check_uci_read_pacing.py | tail -1)"