# 2026-10-29 — kido-barrier design deliberation (builder crew)

Goal: the next module the tree can actually carry. Session 34's
remains are, again, all blocked or fenced: the QEMU virt PSCI
proof and the QEMU virt -smp proof need the toolchain (absent
an 18th time this session — `which` finds no
aarch64-linux-gnu-gcc, no qemu-system-aarch64); the Gate
wirings (vec into the boot path, GIC enable(30) + timer arm
into the scheduler tick, serial-irq init into the boot path,
HAL wiring, MMU call-site wiring, psci-init gating policy,
secondary entry stub, first kido-sync consumer) are Gate
items, not things a scaffold session takes unilaterally;
the destroy bare-metal proof has its own design (session
22, not swallowed here); D1 KIDO_ERR_NO_TASKS is N8toriousAi's
call; the BUBO peer is Phase 4. Per the session-22
precedent (degrade honestly to the next draftable module),
this session drafts kido-barrier: the rendezvous primitive
session 33's secondaries need before they can agree on a
shared "starting line." SMP brought CPUs up (session 33)
and gave them a lock (session 34); nothing yet lets N
CPUs all arrive and all proceed together. Zero existing
files touched (the session-24/25/26/33 fence pattern);
the module-map row and the ledger entry are the only
other edits.

## Positions

- P1 (rendezvous realist): a barrier is a counter plus a
  generation, nothing more. N participants arrive; the
  last arriver releases everyone; the barrier is
  immediately reusable. No per-participant state, no
  scheduler, no timer, no IRQ involvement — the leaf
  shape the tree keeps rewarding.
- P2 (ordering auditor): the release must order memory,
  not just count heads. The chain is: last arriver
  writes generation under the spinlock, then unlocks
  (STLR release); waiters spin on an acquire load of
  generation (LDAR on ARM64, __atomic acquire on
  hosted). A waiter that observes the new generation
  therefore observes everything every arriver wrote
  before its arrival — the sync C6 acquire/release
  chain, reused, not re-invented. No DMB is claimed or
  needed: the lock's release plus the acquire load IS
  the ordering, and the header states the chain
  exactly so a reader can audit it.
- P3 (static-tables keeper): the barrier is a
  caller-embedded struct — one spinlock, two counters,
  a participant count, a magic. No allocator, no
  module-global table, no per-CPU slot array. A
  barrier must exist before any allocator policy, and
  a global barrier registry would be a second
  invention.
- P4 (fence keeper): no scheduler, entry, boot, HAL,
  smp, psci, or sync file moves. Barrier is a leaf: it
  depends on kido-sync's public API and kido-boot.h
  status codes only, and nothing yet depends on it.
  The scheduler-tick rendezvous that might one day
  USE it is a Gate item, not this session's.
- P5 (test keeper): the hosted self-test must prove
  rendezvous, not just count. A single-threaded test
  cannot prove that — wait() blocks until all arrive,
  so one thread can only ever test N=1 and the guards.
  The test therefore uses pthreads (4 threads, 2
  rounds, a pre-barrier write each thread must see
  after the barrier). Pthreads live in the TEST TU
  ONLY, never in the module: the kernel code stays
  pthread-free, and the TESTING line carries -pthread
  so the separation is visible.
- P6 (portability auditor): the ARM64 spinner is one
  LDAR in inline asm, WRITTEN NOT ASSEMBLED (toolchain
  absent, 18th data point). The hosted spinner is a
  real __atomic acquire load, proving the generation
  logic exactly; it does not prove the LDAR spelling,
  and the header says so.

## Challenges, answered

- C1 (generation vs sense-reversing): the classic
  sense-reversing barrier gives each participant a
  local sense flag it flips per round. Overruled in
  favor of a generation counter: the caller keeps
  nothing, the struct holds one more word, and the
  semantics are identical (a waiter spins until the
  shared value differs from the value it saw on
  arrival). Local sense is a storage burden with no
  behavioral payoff here; the generation counter is
  the simpler shape, and simpler is auditable.
- C2 (the release race): the last arriver resets
  arrived to 0 and bumps generation UNDER the lock,
  then unlocks. A waiter spins on generation, never
  on arrived — so the reset can never release a
  waiter early, and a waiter that starts spinning
  after the release sees the new generation on its
  first load and returns at once. The order inside
  the lock (reset, then bump) is load-bearing and is
  stated in the source.
- C3 (deadlock / no timeout): a barrier whose
  participant count is wrong, or whose participant
  never arrives, spins forever — by definition, and
  by the sync D1 reasoning: a timeout would release
  a partial group past the starting line, which is
  silent corruption (half the CPUs proceed on shared
  state the other half never finished writing), not
  a recovery. No timeout, no try_wait: a barrier
  "try" is meaningless — arrival without waiting is
  just a counter. Callers that cannot wait do not
  use a barrier.
- C4 (IRQ context): wait() spins, so calling it from
  an IRQ handler that preempted a participant
  deadlocks that CPU (the handler spins for an
  arrival that cannot happen until the handler
  returns) — the sync C3 shape, restated briefly as
  law. Barriers are CPU-to-CPU rendezvous, not IRQ
  discipline.
- C5 (double-arrive / caller contract): arrival
  counts, it does not identify. A participant that
  calls wait() twice in one round counts twice and
  releases the barrier early. Detecting that would
  need per-CPU arrival slots plus owner tracking —
  a second invention on a leaf. The contract is
  stated in the header: one call per participant per
  round. The count is honest about what it is.
- C6 (re-init rules): init() refuses EARLY_ENV while
  a round is in flight (arrived != 0), mirroring
  sync's init-refuses-held-lock law — re-initializing
  under arriving CPUs would silently free the round.
  Init on an idle barrier succeeds and PRESERVES the
  generation counter (monotonic, never reset): a
  stale waiter from a previous life spinning on an
  old generation must never be released by a reset
  to 0. Init is construction-time, not concurrent —
  the sync init contract, reused.
- C7 (participant cap): 8, matching KIDO_SMP_MAX_CPUS
  — the only CPU census the tree has. A rendezvous
  larger than the machine's CPU table is a guess, not
  a design. 0 participants is EARLY_ENV (a barrier
  nobody arrives at releases nobody, forever).
- C8 (pthread in the test, a first): no module self-
  test has used threads before. Considered and kept:
  the alternative (N=1 + guards only) proves the
  guards and proves nothing about rendezvous — the
  module's entire purpose. The test TU links
  -pthread; the module TU does not include
  <pthread.h> and compiles clean without it (the
  plain -c check proves the separation). If a future
  session dislikes the dependency, the pthread
  section is one fenced block to replace — the
  module is untouched either way.

## Convergence

- API, three functions: init / wait / arrived
  (diagnostic snapshot, the sync is_locked precedent:
  one read, honestly labeled, never a
  synchronization point).
- Struct: { kido_spinlock_t lock; volatile uint32_t
  arrived; volatile uint32_t generation; uint32_t
  participants; uint32_t magic } — caller-embedded,
  no allocator, no globals.
- wait() returns KIDO_OK on release; EARLY_ENV only
  on a NULL or uninitialized barrier. No new header
  codes (sched-D1 precedent).
- Hosted proof proves rendezvous with real threads
  and real acquire loads; the ARM64 LDAR sequence is
  WRITTEN, NOT ASSEMBLED, NEVER EXECUTED until the
  toolchain returns — stated in the header, as every
  session since 22 has stated it.
- No barrier is wired anywhere this session. The
  first consumer (scheduler-tick rendezvous, smp
  shared-state guard, or a Gate wiring) is a Gate
  item with its own design.

## Preserved dissent

- D1 (bounded wait with a timeout): overruled. A
  timeout on wait() converts a missing participant
  into a partial release — silent corruption instead
  of a loud hang. The spin is the honest shape; the
  caller that cannot wait uses a different primitive.
- D2 (sense-reversing local sense): overruled for
  this version. Per-participant storage buys nothing
  the generation counter does not already give, and
  the caller-keeping-nothing shape is strictly
  simpler to audit.
- D3 (per-CPU arrival slots / double-arrive
  detection): overruled. Detection needs an 8-slot
  table plus identity tracking — a second invention
  that duplicates smp's table. The one-call-per-
  round contract is stated, not hidden.
- D4 (barrier inside the scheduler tick): overruled
  as this session's scope. The scheduler tick wiring
  is a Gate item; this module is the leaf it would
  one day use, not the wiring itself.

## Proof gate (queued, toolchain)

- Hosted self-test this session (pthread rendezvous,
  real acquire loads, in-flight re-init refusal
  proven with a blocked thread); plain hosted -c
  clean (no pthread in the module TU); full hosted
  battery re-run; collision check (zero kido_barrier
  references outside the two new files before
  writing).
- ARM64 proof (queued): assemble the LDAR spinner,
  disassemble the acquire-load sequence,
  cross-compile -ffreestanding -Werror clean, then a
  QEMU virt -smp 2 run where CPU 1 arrives and CPU 0
  releases (generation 0 -> 1, both proceed) with
  zero exceptions. That run needs the toolchain AND
  the Gate secondary stub, so it waits on both —
  stated, not hidden.
