Skip to content

ci_clock: make two runs of one PROM byte-identical, with the choice confined to the clock - #892

Open
jamesbraid wants to merge 5 commits into
techomancer:mainfrom
jamesbraid:det-minimal
Open

jamesbraid wants to merge 5 commits into
techomancer:mainfrom
jamesbraid:det-minimal

Conversation

@jamesbraid

@jamesbraid jamesbraid commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

A ci_clock build already counts Count and Compare in retired instructions, but timers, the display refresh and the device engines still ran on host time or their own threads. Two runs of one PROM diverged within a few thousand instructions, so a snapshot check could not tell a change from scheduling noise, and a PROM run could not be diffed against another emulator's.

Now two runs of the Indy's or the O2's PROM that use no network agree on registers, Count, serial output and NVRAM, and in the two-run checks below the screen hash too. Only the uniprocessor Indy and O2 count; every other machine keeps host-time timers.

The commits, in order:

  • Count, Compare and WAIT under ci_clock get the guest's own rates: Count keeps its sub-tick remainder, the Compare check claims its delivery ticket, and WAIT counts idle time per instruction instead of stalling on host time.
  • A counting clock's timers run on CPU 0 between batches (TimerManager::run_expired), with no timer thread.
  • HPC3's read-ahead window and REX3's BACKBUSY count machine time in every build. These two change default builds, so they are separate commits with their own reasons.
  • The MC DMA engine, REX3, CRIME's engine and both SCSI controllers run in the access that wakes them when the clock counts. This commit changes nothing in a default build but dead COUNTING branches, and adds the two-run test.

Developer impact

A device author does nothing. Timer, add_one_shot and add_recurring take the same callback and run on the CPU on a counting machine, on the timer thread elsewhere. The one switch is clock::COUNTING (constant false by default) or Clock::is_counting: a device with its own thread tests it and runs inline instead. add_recurring takes its first delay as a Duration, as add_one_shot does.

Default builds

The timer loop is generic over its time source, add_recurring takes a Duration, and each HPC3 channel holds a clock. Two behaviour changes, each in its own commit:

  • The 50 ms read-ahead window now reads the machine clock at a CBP poll (it read Instant::now()), so it holds still while the machine is stopped.
  • BACKBUSY lasts 1 ms of the display head's clock. A STATUS read clears the deadline once it has passed, so the head is asked for the time only inside that 1 ms.

Known limits

  • The SEEQ Ethernet runs on its own thread, fed by the host network backend's thread, on host time. Runs whose guest sends or receives packets can differ, and "repeatable" here means runs that use no network. This is the likeliest source of divergence in long PROM runs; making it inline is a separate change.
  • With the CPU JIT built, compiles must be synchronous (--jitv2-compile sync): compiled code sees a deadline only at its cycle flushes, so which regions are compiled by when moves where a timer lands. The two-run test sets it.
  • Engines finish before the access that started them returns. Timers run at the first dispatch past their deadline. WAIT with interrupts enabled retries once per dispatch until an interrupt is pending.
  • The WD33C93A raises deferred interrupts at once. A driver that arms its channel after starting a transfer (BSD) is paused immediately, not after 100 ms.
  • A requested display frame is composed on the caller's thread while the CPU is stopped.
  • Battery clocks start at 2026-01-01; idle-pause is off; the O2's service pass runs every 100 us with no idle backoff.
  • The O2's CRIME reset, power-off and flash save still use the service thread, so runs that reach them are not repeatable.
  • Sound follows machine time, so chime_tests::o2_mace_chime is slow in a dev build.

Review

  • Lock order in run_expired: callbacks run with the manager unlocked, on the CPU thread, so a slow callback now stalls the CPU (ci_clock only).
  • The CPU's interrupt word has a new bit, TIMERS_BIT, kept out of Cause, WAIT and park wake like the park bit. Only the CPU thread writes the cycle threshold.

Verification

  • cargo check --all-targets clean on each commit, default (workspace) and --features ci_clock.
  • nextest: default 5,592 passed; jitv2,rex-jit,gputrace 6,247 passed; cargo ci-clock 23 passed. The new ci_clock_two_runs_of_the_indy_prom_end_in_the_same_state builds the Indy, runs its built-in PROM for 40 ms of machine time twice, and compares registers, Count, console log and NVRAM.
  • Also checked outside the test suite, in a release ci_clock build: two runs of the Indy PROM stopped at the 30,001st hit of one PROM address, and of the O2 PROM at the 50,001st hit of another, gave identical serial log, NVRAM, Count, screen hash and monitor status. The same Indy pair on a default build differs in NVRAM and status, and sometimes in screen hash. A 10-minute Indy run did not stay identical (NVRAM differed), which is the Ethernet limit above or a host-chosen stop; it is not claimed here.

Three things in the ci_clock build made the guest's time depend on how
it read the clock, or on the host.

Count kept no sub-tick remainder. Each read moved the cycle anchor on by
the nanoseconds its whole ticks used, divided down to whole instructions.
At 90 MHz a tick is 11.1 ns, so the anchor moved a full 10 ns
instruction per tick and the rest was counted again on the next read. A
guest that reads Count every other instruction, as a delay calibration
does, saw one tick per instruction (100 MHz) instead of the R5000's
90 MHz. The remainder is now kept exactly, in nanoseconds times Hz, and
the anchor advances to the current cycle on every read. A Count write, a
re-anchor and a reset start it over. The host-clock path is unchanged.

The Compare deadline check raised IP7 and bumped the tick counter
without claiming the deadline's delivery ticket. The handler's next
Count read then crossed Compare, found the ticket still armed and
delivered it a second time, so the tick counter ran at 2000 Hz against
the 1000 Hz the guest programmed. The check now claims the ticket, as
the host timer does. Two tests armed the deadline without one, which no
guest can do, and now arm it through a Compare write.

A WAIT with interrupts enabled stalled inside one instruction, advancing
time by host time until an interrupt arrived. Idle time was then a
function of the host, and no deadline check ran while it waited. Under
ci_clock WAIT now answers retry, as the masked case already does, until
an interrupt the IM bits pass is pending; each retry is a dispatch, so
time is counted per instruction. The stall loop is unreachable under
ci_clock, so its Compare check goes, and the two tests of the stall's
pacing apply only to the host-clock build.

Production lines: +34 -14
@jamesbraid
jamesbraid marked this pull request as draft November 27, 2026 23:24
With ci_clock the Indy's and the O2's machine time is CPU 0's retired
instructions, but the timers still ran on the host timer thread. Their
callbacks (the 8254, the UARTs, the display's retrace and line events,
the O2's device service pass) then fired at host-chosen instructions,
so two runs of one PROM diverged.

A manager on a counting clock now spawns no thread. CPU 0 runs
TimerManager::run_expired between batches with the executor unlocked,
so a callback that needs the executor cannot deadlock. It is the host
thread's own loop, made generic over the time it reads and over having
a waiter, so there is one scheduler. Deadlines are machine time, which
is why add_recurring now takes a first delay as a Duration, as
add_one_shot already does, in place of an Instant that could not be
machine time.

A batch is not the only place time must be checked. Compiled code stays
in a region until the interrupt word is nonzero, and Compare is the
only deadline it sees. A guest that never arms Compare (the IP28 kernel
leaves it at zero) would spin in a compiled loop for a whole Count
wrap. The cycle threshold that already ends such a spin for Compare is
now the earlier of Compare and the manager's next deadline. Reaching a
timer's deadline claims no IP7: it sets a free high bit in the interrupt
word, kept out of Cause, WAIT and park wake like the park bit. Arming a
timer from any thread sets the same bit, so the loop leaves its batch,
runs the timers, clears the bit and sets the next deadline. Only the CPU
thread writes the threshold.

Machine setup shares one clock constructor, Clock::cpu_or_host, and
set_cpu_cycles is now a no-op on a host clock, which removes the cfg
pairs at those sites. The display refresh runs from the timers on a
counting clock, composes a requested frame on the caller's thread while
the CPU is stopped, and the O2 service pass is a recurring timer. CRIME
reset, power-off and the flash save stay on the service thread because
they stop and start the CPU. Battery clocks start at 2026-01-01 so the
date does not depend on when the run is made, and idle-pause stays off
on a counting machine because it paces the CPU on host time.

The manager finds the CPU's interrupt word through a pointer it keeps
under its own lock, set when the machine is built and cleared by the
core's Drop before the word goes. The manager can outlive the core, as
the display holds it too, so a late timer then wakes nothing. O2Machine
now stops the machine clock when it drops, as Machine does, so nothing
reads the CPU's cycle counter afterwards.

Default builds only change in shape: add_recurring's argument, the
generic timer loop and an unused run_expired.

Production lines: +276 -64
@jamesbraid jamesbraid changed the title ci_clock: make two runs of one PROM byte-identical ci_clock: make two runs of one PROM byte-identical, with the choice confined to the clock Nov 28, 2026
@jamesbraid
jamesbraid force-pushed the det-minimal branch 2 times, most recently from 3a8d9bd to 6c125cb Compare November 30, 2026 11:05
A SCSI channel caps how far the disk may read ahead of the CPU's last
look at CBP, and forgets the look after 50 ms. That window was host
time, so it kept running while the machine was stopped in the monitor
or a debugger, and a guest that polled CBP just before a stop found the
cap gone on resume. It now reads the machine clock, which holds still
while the machine is stopped, and the window is exactly 50 ms of what
the guest saw.

It also removes a host-time read from the path a counting clock must
keep out of, which the instruction-driven clock needs. A CBP poll now
calls Clock::now() where it called Instant::now(); both are a few
atomic loads and a clock read, and neither is per instruction. Each
channel takes the clock at construction.

Production lines: +9 -9
A DCB address-12 command holds STATUS.BACKBUSY up for 1 ms, which was
1 ms of host time. A machine stopped in the monitor, or a guest that ran
slower than the host, then saw BACKBUSY end at a different point of its
own execution each time. It now lasts 1 ms of the head's clock, which is
machine time, so a run reads the same STATUS however long the host took.
The instruction-driven clock needs this for repeatable runs.

BACKBUSY was never cleared when it ran out, so once any DCB command had
set it every later STATUS read compared against it. That read is now a
weak upgrade and a boxed clock call, not an Instant::now(), so the read
drops the deadline as soon as it has passed and the cost is paid only
inside the 1 ms window after a command.

Production lines: +10 -6
The MC's DMA engine, REX3's GFIFO processor, CRIME's rendering engine,
the AIC-7880 and the WD33C93A each ran on a thread of their own. When
such a thread finishes a transfer, or what a status read sees of it,
depends on the host's scheduler, so two runs of one PROM diverged even
with the timers already on CPU 0's instructions.

Where the build counts instructions the five now run in the access that
wakes them: no worker, and a kick runs the loop until nothing is left,
as the AIC-7880 already does when it has no worker. REX3 and CRIME do
so once started, and queue as before until then. They read
clock::COUNTING, which is a constant false in a default build. The MC
leaves RUN latched for one read after a trigger, because the Indy PROM
wants it set on its first look and clear on its second. The machine
constructors assert that a counting build gives these engines a
counting manager.

The WD33C93A cannot wait for its HPC3 channel inline. A driver that
writes TRANSFER_INFO before it arms the channel used to be waited for
up to 100 ms of host time, but inline the waiter is the CPU that would
arm it. A counting chip skips the wait and takes the pause path it
already has, which keeps the data and raises the unexpected-phase
interrupt at once. A one-byte transfer pauses the same way instead of
retrying. IRIX and the PROM arm first, so neither is affected. The
chip's deferred status interrupt, which the worker raised after
spinning 10000 cycles, is raised with the command, as
--no-scsi-deferred-int does. The status reads of REX3 and CRIME report
an engine that has finished, which an inline engine always has.

A test runs the Indy's built-in PROM for 40 ms of machine time twice and
compares the registers, Count, console log and NVRAM. It sets the JIT's
compile mode to sync, as any run that compares runs must: compiled code
sees a deadline only at its cycle flushes, so which regions are compiled
by when decides where a timer lands. The Ethernet is not covered. The
SEEQ's thread and the host network backend run on host time, so a run
whose guest sends or receives packets can still differ; the PROM sends
none in this window.

In a default build this changes nothing but dead COUNTING branches.

Production lines: +83 -12
@jamesbraid
jamesbraid marked this pull request as ready for review November 30, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant