Skip to content

cpu: narrow the untagged-TLB barrier at ASID-preserving writes and nested exceptions - #945

Open
Samueltgg12 wants to merge 3 commits into
techomancer:mainfrom
Samueltgg12:perf/nutlb-flush-narrow
Open

Samueltgg12 wants to merge 3 commits into
techomancer:mainfrom
Samueltgg12:perf/nutlb-flush-narrow

Conversation

@Samueltgg12

Copy link
Copy Markdown
Contributor

Summary

Next TLB-path PR after #943 and #944. Narrow nanotlb_invalidate() to the events that can actually change a cached translation. nanotlb_invalidate() clears nanotlb[0..3], both ways of nutlb_valid (now two-way after #944), and the jitv2 pcp; several hotspots fired it when nothing translation-relevant moved.

Narrowed sites

  1. MTC0/DMTC0 EntryHi (reg 10): handle_cp0_side_effects takes the previous cp0_entryhi and flushes only when (old ^ new) & 0xFF != 0. VPN2/region bits are not in a nutlb tag. IRIX writes EntryHi on every TLB refill, so this is hot.
  2. TLBR: captures the old EntryHi before overwriting it; flushes only if the ASID changed (TLBR does not write the TLB).
  3. All three exception paths: capture cp0_status before deliver_exception[_at], then call resync_privilege_state_filtered((old ^ new) & TRANSLATION_STATUS_MASK != 0). A nested exception arriving with EXL already set changes no EXL/ERL/privilege/mode bit (EPC/Cause/vector do not feed translation) so the per-CPU nutlb working set survives. A first exception (EXL 0→1) or a cache error (ERL) still flushes.

Unchanged: exec_eret, TLBWI/TLBWR, K0 config writes, jitv2 layout/geometry change, snapshot/restore, soft reset.

Why correct / SMP

The nutlb is valid only while the VA→PA meaning of its tags holds; that meaning changes on privilege/mode, ASID, a TLB write, or a memory remap. None of the narrowed sites changes those. translate_fn/FPR mode are still always re-derived in the exception paths; jitv2 pcp stays valid because the exception vector is a different page, so the fetch nanotlb misses and re-derives pcp. All touched state is per-CPU — no shared state or ordering changes; safe for 4-vCPU Fuel/Tezro (please verify on the Tezro).

Verification

cargo check -p iris --features jitv2 clean. mips_exec_test 218 passed, jitv2 523 passed, and the new test_mtc0_entryhi_with_same_asid_keeps_the_nutlb (tlbstats) passed; existing ASID-change/TLBR regression tests still pass. Not guest-measured — please run iris-bench and an SMP IRIX boot on the Tezro.

Docs: docs/nutlb-design.md §1a; CHANGELOG.md; clanker/cpu/narrow-nutlb-flush-sites.md + clanker/INDEX.md.

A jitv2 build paid ~10% on every guest store from a shared
gen[phys >> 12].fetch_add(1, Release), which also ping-ponged the cache
line between guest CPUs and compile workers. Each 4KB page now has 16
counters: slot 0 for bus/DMA/monitor writes, slots 1..8 for the guest
CPUs. A CPU store bumps only its own slot with a plain Release store
(no atomic RMW, single writer); PhysicalCodePage::current_gen() takes
the max of all slots with Acquire, so any CPU's or a device's write is
observed before code is published. CodePage::gen_slots bounds the read
(16 for RAM, 1 for ROM/MMIO); NEVER_COMPILABLE_GEN is an array.

ppMem GEN_RATIO 512 -> 32 (16 counters per page), GEN_WINDOW_SIZE
128MB; all window map/scrub/mark sites and the JIT inline store's gen
index scaled by GEN_SLOTS; MipsCore::cpu_id selects the slot. bump_gen_all
bumps every slot; install_jit_mem_ptrs stamps each cache's CPU id.

SMP-safe: single-writer slots + Release/Acquire + max over all CPUs.
cargo check (jitv2, +rex-jit/gr4-jit/gr2-jit/wgpu/gputrace) clean;
ppmem 51 passed; jitv2 523 passed. Not yet guest-measured.
docs/nutlb-design.md §8/§9 measured 95-100% of nutlb misses as same-set
conflicts and §10 listed 2-way as the obvious next move. Each set now
holds two entries: MipsCore::nutlb/nutlb_valid gain a [way] dimension and
nutlb_lru records the next victim bit, flipped only on a fill so the hot
hit path pays no LRU store.

nutlb_translate probes both ways with a branchless AND of tag compare and
valid bit, ORs the hits, and selects the matching phys; on a miss it fills
the victim way. The JIT inline probe in codegen.rs mirrors this exactly:
way 1 is one array past way 0, both valid words are read, hits OR-ed and
e_phys selected. No victim-bit write in compiled code (fills are Rust-only).
tlbstats conflict checking looks at either way.

Flushing at all the existing barriers still clears both ways; nutlb is
per-CPU, so SMP is unaffected. Arrays grow 8 KiB -> 16 KiB.

docs/nutlb-design.md updated (§10 no longer open, §11.1 shape).
cargo check (+jitv2_lockstep) clean; jitv2 523, mips_exec_test 218,
nutlb 2, lockstep 15 tests passed. Not yet guest-measured.
…sted exceptions

nanotlb_invalidate cleared nanotlb[0..3], both nutlb ways and the jitv2
pcp even when no cached translation could have changed.

- MTC0/DMTC0 EntryHi (reg 10): pass the previous cp0_entryhi into
  handle_cp0_side_effects; flush only if the ASID bits [7:0] changed.
  VPN2/region writes are not a barrier. IRIX writes EntryHi on every
  TLB refill, so this is hot.
- TLBR: capture old EntryHi before the read; flush only on an ASID
  change (TLBR does not write the TLB).
- All three exception paths: capture cp0_status before
  deliver_exception[_at] and call resync_privilege_state_filtered with
  (old ^ new) & TRANSLATION_STATUS_MASK != 0. A nested exception with
  EXL already set changes no translation bit, so the nutlb survives;
  a first exception or cache error (ERL) still flushes. translate_fn and
  FPR mode are always re-derived.

ERET, TLBWI/TLBWR, K0 config writes, layout changes, restore/reset are
unchanged. per-CPU state only; SMP-safe. New regression test
test_mtc0_entryhi_with_same_asid_keeps_the_nutlb.

cargo check clean; mips_exec_test 218, jitv2 523, new tlbstats test passed.
@techomancer

Copy link
Copy Markdown
Owner

ok this looks promising, will check later

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.

2 participants