Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ ci-test = "ci-nextest --profile ci"
# The default features alone: the only run of the real-cache path that tcache
# (implied by jitv2) compiles out.
ci-test-default = "nextest run --workspace --profile ci"
# The nightly instruction-driven clock tests, which need their own build.
# The nightly instruction-driven clock tests, which need their own build. It also builds jitv2,
# whose compiles are async by default: a test that compares runs sets `jitv2.compile` to Sync.
ci-clock = "ci-nextest --profile ci-clock --features ci_clock,idle-pause"
# The loom concurrency models, built under --cfg loom. A flag, not RUSTFLAGS, so
# no environment is needed; cargo keeps the loom build beside the normal one in
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ hashes are given where a change is easiest to understand by reading the commit.

## November 2026

### Repeatable runs with `ci_clock`

- On the Indy and the O2, a `ci_clock` build now runs the timers, the display
refresh and the device engines (MC DMA, REX3, CRIME's engine, both SCSI
controllers) on CPU 0's instructions, so two runs of one PROM that use no
network give the same registers, Count, serial output and NVRAM. Every
battery clock starts at 2026-01-01.
- The Ethernet is not covered: its thread and the host network backend still
run on host time, so a run whose guest sends or receives packets can differ.
- A build with the CPU JIT needs `--jitv2-compile sync` for this; compiles
that finish at host-chosen moments move where a timer lands.
- Sound follows machine time there, so a chime takes about ten times the
guest instructions it does on host time: `chime_tests` is slow under
`ci_clock` (about 110 s for the O2's in a dev build, against 9 s).
- A driver that starts an HPC3 SCSI transfer before it arms the channel (the
BSD manual flow) is paused at once under `ci_clock`, not after 100 ms.
- In every build, HAL2's read-ahead window and REX3's BACKBUSY count machine
time, so they hold still while the machine is stopped.

### Front ends

- A native Windows front end, `iris-win32`, over the same controllers as the
Expand Down
2 changes: 1 addition & 1 deletion FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Select with `cargo build --release --features <comma-separated-list>`; add
| `perfetto` | Perfetto trace recording (`--perfetto FILE`, monitor `perfetto`); see [docs/perfetto.md](docs/perfetto.md). Lightning-compatible. | — |
| `rexdiag` | REX3 per-GO activity and dispatch counters. Default on; removable in a core build. | — |
| `fetchverify` | Compare executed instruction bytes with memory in interpreter/JIT; expensive stale-code detector. | — |
| `ci_clock` | Synthetic deterministic Count clock for CI/snapshot validation; replaces real-time Count timing. Also times the Indy's DS1386 and 8254 on the cycle count. | — |
| `ci_clock` | Synthetic deterministic Count clock for CI/snapshot validation; replaces real-time Count timing. Also times the Indy's DS1386 and 8254 on the cycle count, starts every battery clock at 2026-01-01T00:00:00Z, and on the Indy and the O2 runs the timers, the device engines and the display's retrace on CPU 0's instructions: two runs of one PROM that use no network agree on registers, Count, serial output and NVRAM (with `--jitv2-compile sync` when the CPU JIT is built). The Ethernet's thread and the host network backend still run on host time. A driver that arms an HPC3 SCSI channel only after it has started a transfer sees the chip's pause interrupt at once instead of after a 100 ms wait. Not repeatable with `idle-pause` (which it turns off on those machines), after a CPU hard reset, which sets the clock back, or on the O2's CRIME cold reset and power-off, which the service thread still performs at a host-chosen instruction. | — |
| `opcodefusion` | Interpreter instruction fusion; breakpoints on a fused second instruction cannot fire. | — |
| `lightning` | Strip breakpoints/traceback for speed; hides GUI Debug controls. | `opcodefusion` |
| `tlbvmap` | O(1) TLB fast path (the vmap); without it lookups use the MRU scan. On by default. | — |
Expand Down
5 changes: 4 additions & 1 deletion src/chime_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ fn boot(profile: MachineProfile, cpu: &str, quiet: usize, edit: fn(&mut MachineC
m.start();
m.cpu_start();
let deadline = Instant::now() + Duration::from_secs(1700);
// Sound follows machine time on a counting clock, which a debug build under load
// advances slowly: five host seconds of nothing new is then not the end.
let patience = if crate::clock::COUNTING { 400 } else { 20 };
let (mut seen, mut still) = (0, 0);
let played = loop {
std::thread::sleep(Duration::from_millis(250));
Expand All @@ -39,7 +42,7 @@ fn boot(profile: MachineProfile, cpu: &str, quiet: usize, edit: fn(&mut MachineC
still = if all.len() == seen { still + 1 } else { 0 };
seen = all.len();
let tail = all.iter().rev().take_while(|&&f| f == (0, 0)).count();
if all.iter().any(|&f| f != (0, 0)) && (tail >= quiet || still >= 20) {
if all.iter().any(|&f| f != (0, 0)) && (tail >= quiet || still >= patience) {
let rate = recs[0].0;
assert!(recs.iter().all(|r| r.0 == rate), "the card changed rate");
break (rate, all.into_iter().filter(|&f| f != (0, 0)).collect());
Expand Down
29 changes: 27 additions & 2 deletions src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

use std::sync::{atomic::{AtomicBool, AtomicU64, Ordering::{Acquire, Relaxed, Release}}, Arc};

/// Whether this build runs machines on instruction counts, not host time (`ci_clock`). The one
/// switch the devices test; a machine whose clock counts says so with [`Clock::is_counting`].
pub const COUNTING: bool = cfg!(feature = "ci_clock");

#[derive(Default)]
struct Inner {
/// Source minus machine nanoseconds, while running.
shift: AtomicU64,
/// The reading while stopped.
held: AtomicU64,
stopped: AtomicBool,
/// ci_clock, the Indy's clock only: the source is the first CPU's retired instructions.
/// ci_clock, a machine whose timers run on its CPU: the source is the first CPU's retired instructions.
#[cfg(feature = "ci_clock")]
cycles: Option<std::sync::OnceLock<crate::cpu::mips_core::CyclesPtr>>,
#[cfg(test)]
Expand Down Expand Up @@ -50,9 +54,30 @@ impl Clock {
Clock(Arc::new(Inner { cycles: Some(Default::default()), ..Default::default() }))
}

#[cfg(feature = "ci_clock")]
/// The clock of a machine whose timers can run on its one CPU: that CPU's instructions when
/// the build counts, else host time.
pub fn cpu_or_host() -> Self {
#[cfg(feature = "ci_clock")]
return Self::counting();
#[cfg(not(feature = "ci_clock"))]
Self::new()
}

/// Whether the clock counts the CPU's instructions: its timers then run on that CPU.
#[inline]
pub fn is_counting(&self) -> bool {
#[cfg(feature = "ci_clock")]
return self.0.cycles.is_some();
#[cfg(not(feature = "ci_clock"))]
false
}

/// Give a counting clock its counter; any other clock ignores it.
pub fn set_cpu_cycles(&self, cycles: crate::cpu::mips_core::CyclesPtr) {
#[cfg(feature = "ci_clock")]
let _ = self.0.cycles.as_ref().map(|o| o.set(cycles));
#[cfg(not(feature = "ci_clock"))]
let _ = cycles;
}

pub fn now(&self) -> u64 {
Expand Down
116 changes: 94 additions & 22 deletions src/cpu/mips_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub const DEFAULT_COUNT_HZ: u64 = 33_000_000;
/// ci_clock: synthetic nanoseconds per retired instruction (~100 MIPS R4400).
/// Virtual time is derived from `hot.cycles` instead of the wall clock so the
/// snapshot determinism validator stays reproducible at any host speed.
#[cfg(feature = "ci_clock")]
pub const NS_PER_GUEST_CYCLE: u64 = 10;

/// Claim the right to deliver IP7 for the armed deadline `ticket`.
Expand Down Expand Up @@ -129,7 +128,7 @@ const IP7_SEQ_CLAIMING: u64 = u64::MAX;
/// `Ioc::set_interrupts`'s stored pointer: it points into the core owned by
/// the executor's top-level `Arc<Mutex<..>>`, and `Drop for MipsCore` removes
/// the timer before the core goes away.
struct TimerIrqPtr(*const AtomicU64);
pub(crate) struct TimerIrqPtr(pub(crate) *const AtomicU64);
unsafe impl Send for TimerIrqPtr {}
pub const CAUSE_CE_MASK: u32 = 0x3 << 28; // Coprocessor Error mask
pub const CAUSE_CE_SHIFT: u32 = 28; // Coprocessor Error shift
Expand Down Expand Up @@ -966,10 +965,19 @@ pub struct MipsCore {
/// stay deterministic).
#[cfg(feature = "ci_clock")]
pub(crate) count_anchor_cycle: u64,
/// ci_clock: `hot.cycles` value at which IP7 fires (checked in step()'s
/// preamble instead of an hptimer). u64::MAX = disarmed.
/// ci_clock: the part of a Count tick the anchor has run, in ns times Hz.
#[cfg(feature = "ci_clock")]
pub(crate) count_frac: u64,
/// ci_clock: `hot.cycles` of the next deadline, Compare's or the timers' (checked in step()
/// and at compiled code's cycle flushes). u64::MAX = none; only the CPU thread writes it.
#[cfg(feature = "ci_clock")]
pub count_fire_cycle: u64,
/// ci_clock: the part of `count_fire_cycle` that is Compare's, and the part that is the
/// timer manager's next deadline. u64::MAX = none.
#[cfg(feature = "ci_clock")]
count_compare_cycle: u64,
#[cfg(feature = "ci_clock")]
count_timer_cycle: u64,
/// Interrupt exceptions this CPU has taken for a line other than the CP0
/// timer (IP7) alone. The idle parker reads it to tell whether the CPU
/// ran a device or inter-processor handler since its last park: on a
Expand Down Expand Up @@ -1308,6 +1316,10 @@ impl Drop for MipsCore {
// core (`TimerIrqPtr`) — take it out of the manager before the
// pointee goes away.
self.disarm_compare_timer();
// So is the counting manager's pointer to the interrupt word, which only CPU 0 hands over.
if let Some(tm) = self.timer_mgr.as_ref().filter(|_| self.cpu_id == 0) {
tm.set_interrupts(std::ptr::null());
}
}
}

Expand Down Expand Up @@ -1468,7 +1480,13 @@ impl MipsCore {
#[cfg(feature = "ci_clock")]
count_anchor_cycle: 0,
#[cfg(feature = "ci_clock")]
count_frac: 0,
#[cfg(feature = "ci_clock")]
count_fire_cycle: u64::MAX,
#[cfg(feature = "ci_clock")]
count_compare_cycle: u64::MAX,
#[cfg(feature = "ci_clock")]
count_timer_cycle: u64::MAX,
irqs_taken: 0,
count_last_guest_read: 0,
count_write_samples: false,
Expand Down Expand Up @@ -1513,7 +1531,8 @@ impl MipsCore {
#[cfg(feature = "ci_clock")]
{
self.count_anchor_cycle = 0;
self.count_fire_cycle = u64::MAX;
self.count_frac = 0;
self.count_timer_cycle = u64::MAX;
}
self.disarm_compare_timer();
self.tick_rate.reset(0);
Expand Down Expand Up @@ -1562,6 +1581,18 @@ impl MipsCore {
self.reset_registers(soft);
// A reset does not release a CPU another thread is holding.
self.hot.interrupts.fetch_and(crate::cpu::smp::HOST_PARK_BIT, Ordering::SeqCst);
self.ask_for_timers();
}

/// Have a counting manager's timers run at the next batch end: a reset or a restart loses the
/// deadline this CPU was watching.
fn ask_for_timers(&self) {
if crate::clock::COUNTING
&& self.cpu_id == 0
&& self.timer_mgr.as_ref().is_some_and(|tm| tm.clock().is_counting())
{
self.hot.interrupts.fetch_or(crate::cpu::smp::TIMERS_BIT, Ordering::SeqCst);
}
}

/// Read a GPR by index. gpr\[0\] is always kept at zero.
Expand Down Expand Up @@ -1734,13 +1765,22 @@ impl MipsCore {
let elapsed_ns = cycles.wrapping_sub(self.count_anchor_cycle).saturating_mul(NS_PER_GUEST_CYCLE);
#[cfg(not(feature = "ci_clock"))]
let elapsed_ns = self.count_anchor_instant.elapsed().as_nanos() as u64;
let ticks = ((elapsed_ns as u128 * self.count_hz as u128) / 1_000_000_000) as u64;
#[cfg_attr(not(feature = "ci_clock"), allow(unused_mut))]
let mut acc = elapsed_ns as u128 * self.count_hz as u128;
#[cfg(feature = "ci_clock")]
{
acc += self.count_frac as u128;
// A tick is not a whole number of cycles: keep the remainder exactly.
self.count_anchor_cycle = cycles;
self.count_frac = (acc % 1_000_000_000) as u64;
}
let ticks = (acc / 1_000_000_000) as u64;
if ticks != 0 {
let consumed_ns = ((ticks as u128 * 1_000_000_000) / self.count_hz as u128) as u64;
#[cfg(feature = "ci_clock")]
{ self.count_anchor_cycle = self.count_anchor_cycle.wrapping_add(consumed_ns / NS_PER_GUEST_CYCLE); }
#[cfg(not(feature = "ci_clock"))]
{ self.count_anchor_instant += std::time::Duration::from_nanos(consumed_ns); }
{
let consumed_ns = ((ticks as u128 * 1_000_000_000) / self.count_hz as u128) as u64;
self.count_anchor_instant += std::time::Duration::from_nanos(consumed_ns);
}
let before = self.cp0_count as u32;
let after = before.wrapping_add(ticks as u32);
self.cp0_count = after as u64;
Expand Down Expand Up @@ -1807,7 +1847,11 @@ impl MipsCore {
let elapsed_ns = self.hot.cycles.wrapping_sub(self.count_anchor_cycle).saturating_mul(NS_PER_GUEST_CYCLE);
#[cfg(not(feature = "ci_clock"))]
let elapsed_ns = self.count_anchor_instant.elapsed().as_nanos() as u64;
let ticks = ((elapsed_ns as u128 * self.count_hz as u128) / 1_000_000_000) as u64;
#[cfg(feature = "ci_clock")]
let frac = self.count_frac as u128;
#[cfg(not(feature = "ci_clock"))]
let frac = 0;
let ticks = ((elapsed_ns as u128 * self.count_hz as u128 + frac) / 1_000_000_000) as u64;
(self.cp0_count as u32).wrapping_add(ticks as u32)
}

Expand Down Expand Up @@ -1874,7 +1918,8 @@ impl MipsCore {
let ns = compare_delay_ns(self.cp0_compare as u32, self.cp0_count as u32, self.count_hz);
#[cfg(feature = "ci_clock")]
{
self.count_fire_cycle = self.hot.cycles.saturating_add(ns / NS_PER_GUEST_CYCLE);
self.count_compare_cycle = self.hot.cycles.saturating_add(ns / NS_PER_GUEST_CYCLE);
self.count_fire_cycle = self.count_compare_cycle.min(self.count_timer_cycle);
}
#[cfg(not(feature = "ci_clock"))]
{
Expand Down Expand Up @@ -1941,27 +1986,50 @@ impl MipsCore {
}
}

/// ci_clock: raise IP7 once `hot.cycles` has reached the Compare
/// deadline. A threshold, not an edge, so a caller that looks late still
/// delivers it, once.
/// ci_clock: act on the deadline `hot.cycles` has reached: Compare's raises IP7, the timers'
/// set `TIMERS_BIT`. A threshold, not an edge, so a late caller still delivers each once.
#[cfg(feature = "ci_clock")]
#[inline(always)]
pub(crate) fn fire_compare_if_due(&mut self) {
if self.hot.cycles >= self.count_fire_cycle {
if self.hot.cycles < self.count_fire_cycle {
return;
}
if self.hot.cycles >= self.count_compare_cycle {
// Next architectural match is a full 32-bit Count wrap away;
// normally a Compare write re-arms much sooner.
let wrap_ns = ((1u128 << 32) * 1_000_000_000) / self.count_hz as u128;
self.count_fire_cycle = self.hot.cycles.saturating_add(wrap_ns as u64 / NS_PER_GUEST_CYCLE);
self.hot.interrupts.fetch_or(CAUSE_IP7 as u64, Ordering::SeqCst);
crate::pf_level!(crate::perfetto::events::IP7_LEVEL[self.cpu_id as usize % crate::perfetto::events::IP7_CPUS], crate::perfetto::events::IP7_COMPARE[self.cpu_id as usize % crate::perfetto::events::IP7_CPUS], true);
self.fasttick_count.fetch_add(1, Ordering::Relaxed);
self.count_compare_cycle = self.hot.cycles.saturating_add(wrap_ns as u64 / NS_PER_GUEST_CYCLE);
// Claim the armed ticket, as the host timer does, so a Count read crossing Compare does not deliver it again.
claim_ip7(&self.ip7_seq_shared, self.ip7_seq, &self.hot.interrupts, &self.fasttick_count, self.cpu_id);
}
if self.hot.cycles >= self.count_timer_cycle {
// The CPU loop runs them and sets the next deadline.
self.count_timer_cycle = u64::MAX;
self.hot.interrupts.fetch_or(crate::cpu::smp::TIMERS_BIT, Ordering::SeqCst);
}
self.count_fire_cycle = self.count_compare_cycle.min(self.count_timer_cycle);
}

/// The timers have run, `now` machine nanoseconds in, and the next is due at `next`: set the
/// deadline `fire_compare_if_due` watches for them.
pub(crate) fn set_timer_deadline(&mut self, next: Option<u64>, now: u64) {
#[cfg(feature = "ci_clock")]
{
let wait = next.map_or(u64::MAX, |due| due.saturating_sub(now).div_ceil(NS_PER_GUEST_CYCLE));
self.count_timer_cycle = self.hot.cycles.saturating_add(wait);
self.count_fire_cycle = self.count_compare_cycle.min(self.count_timer_cycle);
}
#[cfg(not(feature = "ci_clock"))]
let _ = (next, now);
}

/// Cancel any armed Count==Compare interrupt source.
fn disarm_compare_timer(&mut self) {
#[cfg(feature = "ci_clock")]
{ self.count_fire_cycle = u64::MAX; }
{
self.count_compare_cycle = u64::MAX;
self.count_fire_cycle = self.count_timer_cycle;
}
if let (Some(tm), Some(id)) = (self.timer_mgr.as_ref(), self.timer_id.take()) {
tm.remove(id);
}
Expand All @@ -1982,7 +2050,10 @@ impl MipsCore {
pub fn reanchor_count_and_reschedule(&mut self) {
self.count_read_cycle = self.hot.cycles;
#[cfg(feature = "ci_clock")]
{ self.count_anchor_cycle = self.hot.cycles; }
{
self.count_anchor_cycle = self.hot.cycles;
self.count_frac = 0;
}
#[cfg(not(feature = "ci_clock"))]
{ self.count_anchor_instant = std::time::Instant::now(); }
self.schedule_compare_timer();
Expand Down Expand Up @@ -2056,6 +2127,7 @@ impl MipsCore {
pub fn on_cpu_start(&mut self) {
self.count_paused = false;
self.reanchor_count_and_reschedule();
self.ask_for_timers();
}

/// Write CP0 register by index.
Expand Down
Loading