Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ hashes are given where a change is easiest to understand by reading the commit.

## November 2026

### jitv2 interrupts

- Under jitv2, software interrupts are taken as soon as they are enabled, not
at the next hardware interrupt, on every machine. On the Tezro, quake's
timedemo no longer freezes for up to a second at a time.

### jitv2 flushes

- Under jitv2, a single-processor machine no longer hangs when the CPU runs out
Expand Down
1 change: 1 addition & 0 deletions clanker/INDEX.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ The MIPS JIT (`src/cpu/jitv2/`): design, Cranelift codegen, delay slots, invalid
| [inlined-slot-pc-bd-bracket.md](jitv2/inlined-slot-pc-bd-bracket.md) | rule | The inlined delay slot's `core.pc` / `in_delay_slot` bracket |
| [instructions-jitv2-still-interprets.md](jitv2/instructions-jitv2-still-interprets.md) | history | What jitv2 still interprets, and which of it is worth compiling |
| [gpr-cache.md](jitv2/gpr-cache.md) | rule | Guest registers are cached in host registers only inside loops, and written through: the rule for callouts, the load/store join, suite and IRIX measurements, what was dropped, and compile time as the bigger lever |
| [software-interrupts-must-show-in-the-pending-word.md](jitv2/software-interrupts-must-show-in-the-pending-word.md) | rule | Cause IP0/IP1 must be mirrored into `hot.interrupts`: compiled code polls only that word, so a pending software interrupt waited for the next hardware interrupt; Tezro quake froze up to 1.3 s on IRIX's deferred wake-up |
| [interrupt-check-frequency-gates-gpr-forwarding.md](jitv2/interrupt-check-frequency-gates-gpr-forwarding.md) | measure | The pending-interrupt check, not block structure, is what blocks GPR forwarding |
| [j2_per_instruction_category_toggle.md](jitv2/j2_per_instruction_category_toggle.md) | design | j2 per-instruction/category enable toggle |
| [jit-page-probe-must-be-retired-on-drop.md](jitv2/jit-page-probe-must-be-retired-on-drop.md) | history | The jitv2 dirty-page probe must be retired when its cache dies |
Expand Down
37 changes: 37 additions & 0 deletions clanker/jitv2/software-interrupts-must-show-in-the-pending-word.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Software interrupts must show in the pending word

Status: fixed 2026-11-22 (`MipsCore::sync_soft_interrupts`).

**Symptom.** On the Tezro (4 x R16000, jitv2), quake's `timedemo demo1`
froze for 0.3 to 1.3 s several times a run, at random; scores varied
16 to 29 fps. The guest's clock was fine (UST never went back, no system
gap). Quake was asleep in the kernel outside any system call, at a
libGLcore PC, on a wait address inside `rrm_pane` (the Rendering Resource
Manager). The VPro board was idle for the whole stall: swap armed, latched
at the next retrace, retrace interrupt delivered.

**What the trace showed.** Perfetto's per-CPU `interrupt taken` events:
for the whole stall one CPU took `ip 0x81` (IP7 clock plus software
interrupt IP0) every 10 ms, and the stall ended the moment that CPU took
`ip 0x1` alone. IRIX had posted deferred work (the wake-up) as a software
interrupt; its clock handler services IP7 and returns.

**Cause.** Compiled code polls only `core.hot.interrupts` for a pending
interrupt (`emit_pending_interrupt_preamble`, `emit_entry_interrupt_bail`).
Software interrupts lived only in `cp0_cause`, which the interpreter's
`step_preamble!` reads but compiled code does not. A software interrupt
left pending across an `eret` into compiled code was taken only when a
hardware interrupt next sent the CPU through the interpreter's check, and
when that was the clock, the handler left IP0 pending again.

**Rule.** Every write of Cause IP0/IP1 (guest `mtc0`, snapshot and state
restores) mirrors them into `hot.interrupts` through
`MipsCore::sync_soft_interrupts`. The interpreter merges only the external
bits (IP2-IP7) back into Cause, so the mirror only makes the word nonzero;
delivery still reads Cause. Anything new that sets Cause's software bits
must call it.

**Tools that found it.** A small guest watcher sampling the process from `/proc` every 50 ms
(state, wait channel, `pr_syscall`, user PC; `pr_syscall` reads one above
`sys.s`), kernel symbols from `/unix` for the wait address, and a perfetto
trace (`all,-prim,-jit`) with frame gaps taken from `swap arm` slices.
2 changes: 1 addition & 1 deletion debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ controller's accesses and values nothing establishes.
|---|---|
| `perf snapshot` | Performance counters (Indy, Indigo2 and the Bedrock machines) |
| `locks` | Every registered lock, whether it is held, and by which thread and call site (for `SpinLock`s); Indy and Indigo2 |
| `perfetto start FILE [cats=LIST] [clock=host\|guest]` | Start a Perfetto trace (`--features perfetto`; Indy, Indigo2, O2 and Octane) |
| `perfetto start FILE [cats=LIST] [clock=host\|guest]` | Start a Perfetto trace (`--features perfetto`; Indy, Indigo2, O2, Octane, Origin 200/2000, Fuel, Tezro and Origin 3000) |
| `perfetto cats LIST` / `perfetto status` / `perfetto stop` | Change categories / show / stop |
| `idleprof ...` | See [history and tracing](#history-undo-and-tracing) |

Expand Down
8 changes: 5 additions & 3 deletions docs/perfetto.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ event that belongs to several categories is recorded when any of them is on.
|---|---|
| `cpu` | Interrupts taken by the CPU (also `irq`). |
| `irq` | IOC/INT2 interrupt lines, the CPU's IP2-IP7 pins, interrupts taken. |
| `timer` | The 8254 slow and fast ticks and Count/Compare (IP7); these are also `irq`. |
| `timer` | The 8254 slow and fast ticks, Count/Compare (IP7) and the IP35 PI real-time clock; these are also `irq`. |
| `dma` | HPC3 PDMA channels, VDMA (also `gfx`), IMPACT host DMA (also `gfx`). |
| `scsi` | WD33C93A commands, CDBs and chip interrupts. |
| `net` | Ethernet frames sent, received, refused and filtered. |
Expand Down Expand Up @@ -71,7 +71,8 @@ host thread's name.
| Where | Track / thread | Events |
|---|---|---|
| Interrupts | `L0 *`, `L1 *`, `map *` | One slice per assertion of each IOC/INT2 status bit (raw, before masking), from raise to clear. `slow tick (8254 timer 0)` and `fast tick (8254 timer 1)` are latches, so their slices measure tick-to-acknowledge latency. |
| CPU interrupt pins | `IP2`…`IP6`, `IP7 (count/compare)` | The CPU's interrupt inputs. IP7 runs from the Count/Compare match to the guest's Compare write. `IP7 masked (Status)` is a slice while the guest has the clock masked (Status IE or IM7 clear); `IP7 pending, held` while IP7 is pending and the CPU cannot take it (arg at the start: Status << 32 \| PC). |
| CPU interrupt pins | `IP2`…`IP6`, `IP7 (count/compare) cpu N` | The CPU's interrupt inputs. Each processor (0 to 7) has its own IP7 tracks. IP7 runs from the Count/Compare match to the guest's Compare write. `IP7 masked (Status)` is a slice while the guest has the clock masked (Status IE or IM7 clear); `IP7 pending, held` while IP7 is pending and the CPU cannot take it (arg at the start: Status << 32 \| PC). |
| IP35 clock | `RT clock cpu 0`…`3` | The Fuel, Tezro and Origin 3000 schedule from each processor's PI real-time comparator, not Count/Compare. A `rt tick` slice runs from the comparator latching to the guest clearing its pending bit: its length is the tick-to-acknowledge latency, and a gap is a missed tick. Both nodes of a two-node machine share the tracks. |
| `MIPS-CPU` | thread | `interrupt taken` (arg: pending & enabled IP bits); jitv2 `compile request` and `hot request` (arg: PC); `pool flush (CPU thread)` and `track page (CPU thread)`, the jitv2 work that runs on the CPU thread with no guest code running. |
| DMA | `PDMA pbus0-7`, `PDMA scsi0/1`, `PDMA enet rx/tx` | HPC3 channel `active` slices (arg: first descriptor), `desc` per data descriptor (arg: byte count/flags), `int` on completion interrupts. PBUS channels carry HAL2 audio. |
| DMA | `VDMA` | MC graphics DMA: `mem -> gio`, `gio -> mem`, `fill` (arg: bytes). |
Expand All @@ -87,8 +88,9 @@ host thread's name.
| Graphics | `IMPACT status`, `IMPACT flags`, `IMPACT vc3` | Every STATUS read with its value (bit 2 is vertical blank); flags raised (arg: raster entries queued << 32 \| bits) and cleared by the host; VC3 register and SRAM writes (the window-ID tables) and current-line reads. |
| Graphics | `IMPACT irq` | The board's three interrupt lines (arg: line << 1 \| level: 0 FIFO, 1 general, 2 retrace). On the Octane, the interrupt packets sent to HEART (arg: vector << 1 \| set) and the kernel's acknowledgements (arg: offset << 32 \| bits). |
| Graphics | `VPro cfifo level` | The command-FIFO level STATUS0 is built from (words queued, not yet run), sampled at slice start and end and at STATUS0 reads. |
| Graphics | `VPro irq` | `cfifo low water` (the level fell below the mark as a store retired), `dma done`, and an `interrupt packet` per packet sent to HEART (arg: routed-source shift << 9 \| vector << 1 \| set). |
| Graphics | `VPro irq` | `cfifo high water` (the level reached the driver's mark as a store was queued, arg: level), `enable/flags write` (the driver's writes to the interrupt enables and flags, arg: enables << 32 \| flags), `cfifo low water` (the low-water flag latched: the level fell below the mark as a store retired, with the flag clear), `dma done`, and an `interrupt packet` per packet sent to HEART (arg: routed-source shift << 9 \| vector << 1 \| set). |
| Graphics | `Display vblank`, `Display frame` | The display refresh thread, for every board: a `vblank` slice from vblank start to vblank end (arg: field), then on the frame track `capture` (the board taking the frame from its live state), `compose` and `present`, or `capture refused` when the board is not ready (Newport's fence). |
| Graphics | `VPro irq line`, `VPro flow control` | `flag interrupt` while an enabled flag is set (arg: flags & enables); `low-water enabled` while the driver has the low-water interrupt enabled, that is, while its flow control waits for the command FIFO to drain. |
| Graphics | `VPro display`, `VPro capture`, `VPro present` | `swap arm` (the driver's flip request, arg: mask), `retrace` (flips latched, arg: displayed-buffer bits); the refresh thread resolving the frame and handing it to the window. Inside a capture, `frame planes` on `Display-Refresh` is the part that composes the whole frame from board memory, with no lock and no wait for the raster unit. |
| Graphics | `VPro access busy` | One processor access answered busy, from its first refusal until it completes (arg: aperture offset). The processor takes its enabled interrupts in between. |
| `odyssey-work` | thread | `slice` per run of queued stores on the front-end thread (arg: stores waiting), `dma descriptor` per page-table transfer (arg: table address). A processor access that drains the queue shows `drain` on its own thread. |
Expand Down
64 changes: 54 additions & 10 deletions src/cpu/mips_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub const NS_PER_GUEST_CYCLE: u64 = 10;
/// next Compare write re-arms it via `MipsCore::arm_ip7_sequence`, which is
/// exactly the guest's acknowledgement.
#[inline]
fn claim_ip7(seq: &AtomicU64, ticket: u64, irq: &AtomicU64, fasttick: &AtomicU64) -> bool {
fn claim_ip7(seq: &AtomicU64, ticket: u64, irq: &AtomicU64, fasttick: &AtomicU64, _cpu: u8) -> bool {
if ticket == IP7_SEQ_CONSUMED {
return false;
}
Expand All @@ -117,7 +117,11 @@ fn claim_ip7(seq: &AtomicU64, ticket: u64, irq: &AtomicU64, fasttick: &AtomicU64
{
irq.fetch_or(CAUSE_IP7 as u64, Ordering::SeqCst);
seq.store(IP7_SEQ_CONSUMED, Ordering::SeqCst);
crate::pf_level!(crate::perfetto::events::IP7_LEVEL, crate::perfetto::events::IP7_COMPARE, true);
crate::pf_level!(
crate::perfetto::events::IP7_LEVEL[_cpu as usize % crate::perfetto::events::IP7_CPUS],
crate::perfetto::events::IP7_COMPARE[_cpu as usize % crate::perfetto::events::IP7_CPUS],
true
);
#[cfg(feature = "idle-pause")]
crate::cpu::idle_park::wake();
fasttick.fetch_add(1, Ordering::Relaxed);
Expand Down Expand Up @@ -1856,6 +1860,7 @@ impl MipsCore {
self.ip7_seq,
&self.hot.interrupts,
&self.fasttick_count,
self.cpu_id,
);
#[cfg(feature = "developer_ip7")]
if won {
Expand Down Expand Up @@ -1968,6 +1973,7 @@ impl MipsCore {
let fasttick = self.fasttick_count.clone();
let ticket = self.ip7_seq;
let seq_ref = self.ip7_seq_shared.clone();
let cpu = self.cpu_id;
// After firing, hardware would next match Compare again after a
// full 32-bit Count wrap — self-reschedule that far out. In
// practice the guest's interrupt handler writes Compare long
Expand All @@ -1977,8 +1983,8 @@ impl MipsCore {
);
self.timer_id = Some(tm.add_one_shot(
std::time::Duration::from_nanos(ns),
(irq, fasttick, wrap, ticket, seq_ref),
|(irq, fasttick, wrap, ticket, seq_ref)| {
(irq, fasttick, wrap, ticket, seq_ref, cpu),
|(irq, fasttick, wrap, ticket, seq_ref, cpu)| {
// SAFETY: points into the MipsCore owned by the executor's
// top-level Arc<Mutex<..>>, which outlives the armed timer
// (Drop for MipsCore removes it). Same contract as
Expand All @@ -1988,7 +1994,7 @@ impl MipsCore {
// Compare write since this one-shot was scheduled has
// already issued a new ticket, so this claim fails and the
// interrupt the guest just acked is not re-asserted.
let won = claim_ip7(seq_ref, *ticket, irq_ref, fasttick);
let won = claim_ip7(seq_ref, *ticket, irq_ref, fasttick, *cpu);
if !won {
return crate::hptimer::TimerReturn::Delete;
}
Expand All @@ -1998,6 +2004,25 @@ impl MipsCore {
}
}

/// Mirror the software interrupts (Cause IP0 and IP1) into
/// `hot.interrupts`. Compiled code polls only that word for a pending
/// interrupt, so a software interrupt kept in Cause alone was not taken
/// under the JIT until a hardware interrupt happened to send the CPU
/// through the interpreter's check: IRIX posts deferred work (such as
/// waking a process blocked on the graphics pipe) as a software
/// interrupt, and the Tezro's quake froze for most of a second at a time
/// while it sat pending behind clock ticks the handler serviced alone.
/// The interpreter merges only the external bits back into Cause, so the
/// mirror only makes the word nonzero; delivery still reads Cause.
pub fn sync_soft_interrupts(&self) {
let mask = (CAUSE_IP0 | CAUSE_IP1) as u64;
let soft = self.cp0_cause as u64 & mask;
self.hot.interrupts.fetch_and(!(mask & !soft), Ordering::SeqCst);
if soft != 0 {
self.hot.interrupts.fetch_or(soft, Ordering::SeqCst);
}
}

/// 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.
Expand All @@ -2010,7 +2035,7 @@ impl MipsCore {
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, crate::perfetto::events::IP7_COMPARE, true);
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);
}
}
Expand Down Expand Up @@ -2063,7 +2088,7 @@ impl MipsCore {
self.arm_ip7_sequence();
self.reanchor_count_and_reschedule();
if self.cp0_cause & CAUSE_IP7 != 0 {
claim_ip7(&self.ip7_seq_shared, self.ip7_seq, &self.hot.interrupts, &self.fasttick_count);
claim_ip7(&self.ip7_seq_shared, self.ip7_seq, &self.hot.interrupts, &self.fasttick_count, self.cpu_id);
} else {
self.hot.interrupts.fetch_and(!(CAUSE_IP7 as u64), Ordering::SeqCst);
}
Expand Down Expand Up @@ -2191,7 +2216,7 @@ impl MipsCore {
// re-raise it).
self.cp0_cause &= !CAUSE_IP7;
self.hot.interrupts.fetch_and(!(CAUSE_IP7 as u64), Ordering::SeqCst);
crate::pf_level!(crate::perfetto::events::IP7_LEVEL, crate::perfetto::events::IP7_COMPARE, false);
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], false);

// Now classify what the guest actually asked for. Count here
// is virtual and materialized in lumps from the wall clock,
Expand Down Expand Up @@ -2250,6 +2275,7 @@ impl MipsCore {
ticket,
&self.hot.interrupts,
&self.fasttick_count,
self.cpu_id,
);
#[cfg(feature = "developer_ip7")]
if won { self.ip7_late_fires += 1; }
Expand Down Expand Up @@ -2329,6 +2355,7 @@ impl MipsCore {
// Cause register: Only IP0 and IP1 are writable software interrupts
let mask = CAUSE_IP0 | CAUSE_IP1;
self.cp0_cause = (self.cp0_cause & !mask) | ((value as u32) & mask);
self.sync_soft_interrupts();
}
14 => {
// A guest write of a 32-bit value here is where a 64-bit
Expand Down Expand Up @@ -2950,10 +2977,27 @@ mod ip7_ticket_tests {
let shared = core.ip7_seq_shared.load(Ordering::SeqCst);
assert_ne!(shared, IP7_SEQ_CONSUMED, "the restored deadline was armed with the delivered sentinel");
assert_eq!(shared, core.ip7_seq, "the armed one-shot's ticket must be the live sequence");
assert!(claim_ip7(&core.ip7_seq_shared, core.ip7_seq, &core.hot.interrupts, &core.fasttick_count),
assert!(claim_ip7(&core.ip7_seq_shared, core.ip7_seq, &core.hot.interrupts, &core.fasttick_count, core.cpu_id),
"the restored deadline's one-shot could not deliver IP7");
}

/// A software interrupt the guest posts in Cause shows in the pending
/// word compiled code polls, and clearing it there leaves the hardware
/// lines alone. Kept only in Cause, it went untaken under the JIT until
/// a hardware interrupt passed through the interpreter's check.
#[test]
fn a_software_interrupt_shows_in_the_pending_word() {
let mut core = MipsCore::default();
core.hot.interrupts.store(CAUSE_IP7 as u64, Ordering::SeqCst);
core.write_cp0(13, CAUSE_IP0 as u64);
assert_eq!(core.hot.interrupts.load(Ordering::SeqCst), (CAUSE_IP7 | CAUSE_IP0) as u64);
core.write_cp0(13, CAUSE_IP1 as u64);
assert_eq!(core.hot.interrupts.load(Ordering::SeqCst), (CAUSE_IP7 | CAUSE_IP1) as u64);
core.write_cp0(13, 0);
assert_eq!(core.hot.interrupts.load(Ordering::SeqCst), CAUSE_IP7 as u64);
assert_eq!(core.cp0_cause & (CAUSE_IP0 | CAUSE_IP1), 0);
}

fn ip7_pending(core: &MipsCore) -> bool {
core.hot.interrupts.load(Ordering::SeqCst) as u32 & CAUSE_IP7 != 0
}
Expand Down Expand Up @@ -3006,7 +3050,7 @@ mod ip7_ticket_tests {
}
if ticket != tried.load(Ordering::SeqCst) {
// SAFETY: the core outlives the scope, as it does the armed timer.
claim_ip7(&seq, ticket, unsafe { &*irq.0 }, &fasttick);
claim_ip7(&seq, ticket, unsafe { &*irq.0 }, &fasttick, 0);
tried.store(ticket, Ordering::SeqCst);
}
std::thread::yield_now();
Expand Down
Loading