Skip to content

target/ppc, tcg: faster PowerPC guests (BAT flush, jump cache, FP fast paths) - #32

Open
brunocastello wants to merge 14 commits into
utmapp:utm-editionfrom
brunocastello:pr/ppc-tcg-speed
Open

brunocastello wants to merge 14 commits into
utmapp:utm-editionfrom
brunocastello:pr/ppc-tcg-speed

Conversation

@brunocastello

Copy link
Copy Markdown

PowerPC guests (Mac OS 9 and Mac OS X on mac99) spend a lot of host CPU even when idle, and feel slow under TCG. This series makes them cheaper and faster.

The biggest single win is the first commit: Mac OS 9 rewrites its BATs constantly, and every write flushed the whole TLB, even for invalid BATs. Flushing only the range a BAT covers takes an idle Mac OS 9 desktop from about 57% to 33% host CPU (28% with the jump cache change after it). It also fixes stale TLB entries after a BAT change.

Changes

  • target/ppc: flush BAT ranges by block length, only when the BAT is valid.
  • accel/tcg: bigger TB jump cache retired by a generation counter, and an inline jump cache probe for PowerPC indirect branches (QEMU_TB_INLINE=0 turns it off).
  • target/ppc: lmw/stmw expanded inline; AltiVec permutes on NEON; A-form FP arithmetic on the host FPU when FPSCR allows it (round-to-nearest, no exceptions enabled; NaNs and edge cases fall back to softfloat). PPC_STRICT_FP=1 turns all FP fast paths off.
  • target/ppc: skip the BQL in ppc_maybe_interrupt when nothing can change; data-only flush on DBAT writes (PPC_DBAT_FAST=0 goes back).
  • Opt-in only: page-precise tlbie (PPC_TLBIE=page) and a QEMU TLB per segment register set (PPC_SR_SLOTS=1).
  • macOS host: TCG vCPU threads at user-interactive QoS; remember the JIT write-protect state per thread.

Most of the speed-ups after the first two commits come from linuxkid473/ppc-osx-vgpu (poweremu-qemu, GPL-2.0-or-later); the original authors are kept on those commits. tests/ppc-vmx has the FP/AltiVec comparison tests that came with them.

Testing

  • Mac OS 9.2.2 and Mac OS X Tiger/Leopard on mac99, Apple Silicon host, in my UTM builds for the past weeks (desktop, games, Doom 3 on Leopard).
  • This branch builds on macOS arm64 for ppc, ppc64, x86_64 and aarch64 softmmu; mac99 and q35 start.
  • Not tested: Linux or x86 hosts, and I did not re-run tests/ppc-vmx on this rebase.

It touches the TCG core and adds AArch64 vector ops, so I'm happy to split it (BAT / jump cache / FP / MMU) or make the FP paths opt-in if you prefer.

To be upfront: I built this with Claude (Anthropic) through Claude Code. The code and analysis are largely Claude's work; I directed it and tested it in my own VMs. Please review it with that in mind — happy to change anything.

brunocastello and others added 14 commits November 27, 2026 21:54
When a BAT register is rewritten, its old block has to be dropped from
the softmmu TLB. do_invalidate_BAT() was passed the block length of the
*new* value, so clearing or shrinking a BAT only flushed the first
128 KB of the old block, and stale translations of the rest survived.
Use the length encoded in each BAT value instead.

The flush also did more work than needed, which costs a lot with
classic Mac OS: its NanoKernel rewrites IBAT0/1 and DBAT0/1 (4 MB ROM,
1 MB 68k emulator) every time it switches context, thousands of times
per second even when idle, and each switch flushed ~1700 pages one by
one in all 16 MMU indexes:

- skip BATs with neither Vs nor Vp set: they match no access, so the
  TLB holds nothing from them and they hide no page table translation;
- flush with tlb_flush_range_by_mmuidx() instead of a page loop;
- only flush the MMU indexes with translation enabled: BATs never apply
  in real mode.

Mac OS 9.2.2 on mac99 (G4, TCG, Apple Silicon host), idle desktop:
QEMU host CPU 57% -> 33%.

Default-on; no switch. Fixes a stale-TLB bug as well as saving time.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
tlb_flush_range_by_mmuidx_async_0() clears the jump cache page by page
for ranges below TB_JMP_CACHE_SIZE pages (16 MB with 4 KB pages),
64 entries per page. A page's bucket is its page number bits [0, P) xor
bits [P, 2P) (P = TB_JMP_PAGE_BITS), so any run of 2 << P pages contains
an aligned run of 1 << P pages that hits every bucket: from 512 KB on,
the loop already empties the whole cache, but touches up to 64 times
more entries than tcg_flush_jmp_cache() does.

Lower the threshold to 2 << P pages. The result is the same; only the
cost changes. With classic Mac OS guests flushing 1-4 MB BAT ranges
thousands of times per second, idle host CPU goes from 33% to 28%.

Default-on; no switch.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
A Mac OS X game covers 100k+ translation blocks, so with 4096 entries most
indirect branches -- and every PowerPC return through blr is one -- missed
the jump cache and fell back to the hash table: a quarter of the emulator's
busy time went into lookups.

64K entries fix that but make emptying the cache 16x more expensive, and a
32-bit hash MMU guest empties it constantly; measured on Warcraft III, the
larger cache alone traded lookups for flush stalls.  Retire entries by
bumping a generation counter instead, so a flush is O(1) whatever the cache
size, and walk the array only on the 32-bit wrap.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on; no switch.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
…resses

Every blr, bctr and branch to another page went through
helper_lookup_tb_ptr, whose jump cache probe is a few loads and compares
behind a helper call, curr_cflags() and cpu_get_tb_cpu_state().  In
Geekbench 2's integer tests on Tiger that helper was up to half of the
vCPU thread's time.

translator_lookup_and_goto_ptr() emits the probe itself: hash the pc,
check the entry's pc and generation, and that the TB has this TB's
cs_base, flags and cflags, then goto_ptr to it; a miss calls the helper as
before.  It is only for exits where the CPU state that
cpu_get_tb_cpu_state() returns can't have changed since the TB began, and
only for TBs with ordinary cflags (no icount, single-step, breakpoint
page, plugins or exec logging).  PowerPC uses it for branches to LR, CTR
and TAR and for direct branches off the TB's page.

Geekbench 2 on Tiger 10.4.11 (M1 Max): helper calls 2.6G -> 0.38G,
integer 1173 -> 1308, overall 1249 -> 1314.

QEMU_TB_INLINE=0 turns the inline probe off and always calls
helper_lookup_tb_ptr, for A/B comparisons. The probe is otherwise
default-on, for PowerPC guests only.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
vCPU threads run at QOS_CLASS_USER_INTERACTIVE so macOS keeps them on
performance cores instead of parking them on efficiency cores when the
host is busy or the window is in the background.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on, macOS hosts only.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
Switching the JIT mapping between writable and executable costs an "isb
sy", a full pipeline resynchronisation. qemu_thread_jit_execute() runs
before every translated block is entered, while writes happen only when
code is generated or patched, so the overwhelming majority of those
switches ask for a state the thread is already in.

The cache must be one variable shared by every translation unit. As a
static __thread in the header each file got its own copy, so one file's
idea of the state let another skip a switch it actually needed -- which
did not boot at all. It is declared extern here and defined once in
qemu-thread-posix.c.

Part of a change measured at +5.7% on a paired A/B (52.0 -> 55.0 fps).

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on, Apple JIT hosts only.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
Expand lmw/stmw as word loads and stores instead of a helper that probes
the whole range first. Compilers emit them in most prologues and
epilogues, so the helper call was frequent. A fault part-way through
restarts the instruction with some registers already loaded or some
words already stored, which the architecture allows; the address lives
in a temporary, so rA inside the range is harmless. Little-endian mode
still raises the alignment exception before this code.

Only the lmw/stmw part of the original change is taken; the host-FPU
fast path and the user fault watch from the same commit are not.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on; no switch.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
AltiVec's byte permutes went through out-of-line helpers that loop over
bytes, and QuickTime's decoders lean on them heavily.

tcg: new optional vector op tbl_vec, a two-register byte table lookup
(index 0-15 from a, 16-31 from b, else 0).  The AArch64 backend emits it
as MOVI/EOR/TBL/TBX.  Other hosts don't have it and keep the helpers.

target/ppc: with tbl_vec, vperm, vsldoi, vmrgh/l[bhw], vpku[hw]um, the
saturating packs (VSCR[SAT] kept), vupk[hl]s[bh] are shuffles.  The
even/odd multiplies vmul[eo][us][bh] and the modulo multiply-sums
vmsum{ubm,mbm,uhm,shm} are shifts and vector multiplies.

int_helper: on AArch64 hosts vaddfp, vsubfp, vmaxfp, vminfp, vmaddfp,
vnmsubfp, vrefp, vrsqrtefp, vcf[su]x and vct[su]xs compute with NEON
when every input and result lane is normal or zero (where IEEE and
softfloat agree bit for bit), else fall back to softfloat.

tests/ppc-vmx/run.py runs a bare-metal mac99 image that checks every
converted op against scalar references (188000 checks) and times them:
vperm ~6x, merges/sldoi/packs ~4x and vmaddfp ~3x faster, net of loop
overhead.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on, AArch64 hosts only; other hosts keep the existing helpers.
The lane-wise float helpers fall back to softfloat unless every input
and result lane is normal or zero.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
The guest rewrites MSR constantly, and every write took the BQL. With
nothing pending and CPU_INTERRUPT_HARD already clear there is nothing to
change; a source raising an interrupt calls ppc_maybe_interrupt() itself,
under the BQL, after setting pending_interrupts.

Taken from the mtmsr part of r300 commit 341dd47; the GPU and SR-slot
parts of that commit are not.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).
Default-on; no switch.

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
A-form FP arithmetic (fadd, fsub, fmul, fdiv and the fused multiply-add
family, double and single) goes through one helper that computes on the
host FPU when FPSCR is round-to-nearest with non-IEEE mode off and no
FP exception enabled, and the operands and result are not NaNs. Anything
else takes the softfloat path as before. Results and FPSCR flags are
meant to be bit-identical to softfloat's in that state.

FPRF is also set inline for the case that always happens: nearly every
result is an ordinary normal number, which is two bit tests on the
exponent. Everything else still calls helper_compute_fprf_float64(),
which stays the single definition of the other classes. The inline test
was compared against softfloat's classification over four million bit
patterns (boundaries around zero, denormals, smallest normal, largest
finite, infinity, both NaN kinds) with no mismatches. Measured +9..16%
on a Warcraft III menu on an Apple Silicon host.

Default-on. PPC_STRICT_FP=1 turns every FP fast path off and goes back
to the original softfloat helpers.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
fadd/fsub/fmul/fdiv, fmadd/fmsub/fnmadd/fnmsub and their single forms
now run as host instructions in translated code instead of a call to
helper_fastfp_ab/acb, when the FPSCR is in the state Mac OS X runs
applications in (no FP exception enabled, NI off, round to nearest:
the new HFLAGS_FP_FAST TB flag). Results and FPSCR are bit-identical to
the helper's; where the helper would fall back to softfloat (NaN
results, single ops on non-single inputs) the op branches to an
out-of-line slow path that calls the helper and leaves the TB.

- tcg: new vector-register ops fop_vec, ffma_vec and fcvt_vec for scalar
  IEEE arithmetic in a V64 (AArch64 backend only; TCG_TARGET_HAS_fp_vec).
- FPRF is computed lazily: the inline op stores its result in
  env->fprf_res and ppc_fprf_sync() computes FPRF when something reads
  or partly changes it (mffs, mcrfs, fcmp, mtfsb, gdbstub, migration,
  dumps); full FPRF writers call ppc_fprf_clear(). Each host instruction
  counts here: an eager branchless FPRF cost more than it saved.
- helper_fastfp_acb: fnmadd/fnmsub gave +0 instead of -0 for an exact
  zero sum on AArch64 hosts (clang folds -fma() into FNMADD, which
  negates the operands); flip the sign bit instead.
- tests/ppc-vmx: run.py --fpdiff compares 211,200 scalar FP cases (with
  mffs, fcmpu, mtfsb1 and mcrfs after the op) between the inline path and
  the helper, results and FPSCR; --fpdiff-strict compares result bits
  with softfloat. Also link the test above macio-nvram's
  0xfff04000-0xfff07fff window, which hid code past 16 KiB.

PPC_NATIVE_FP=0 turns the inline path off (PPC_STRICT_FP=1 turns off all
fast paths).

Measured on an M1 Max, Tiger 10.4.11, Geekbench 2.2.0 (AI-assisted,
Claude Code): FP score 654 -> 960, overall 1156 -> 1276 (Mandelbrot
604 -> 1727, dot product 383 -> 584, LU 396 -> 498). Both --fpdiff
modes report 0 mismatches; Tiger and Leopard boot and run with it.

fcmpu and frsp also run inline in this mode (PPC_NATIVE_FCMP=0 and
PPC_NATIVE_FRSP=0 keep their helpers). fcmpu was three helper calls; NaN
inputs branch to the old helper sequence, otherwise the operands'
bits are compared as ordered integer keys. frsp converts to single and
back on the host and takes the old sequence out of line for NaNs, inputs
below 2^-126 and overflow. Geekbench 2 on Tiger, M1 Max, inline vs
helpers: fcmpu +5% overall (Primality +46%), frsp Sharpen/Blur ~2x.

The tests/ppc-vmx --fpdiff run compares the inline ops (including fcmpu
and frsp) against the helpers and, with --fpdiff-strict, against
softfloat: no mismatches over 224,400 cases.

Default-on, AArch64 hosts only (it needs the new vector ops) and not in
user-only emulation. It applies only while the TB flag HFLAGS_FP_FAST is
set (no FP exception enabled, NI off, round to nearest); PPC_NATIVE_FP=0
turns the inline path off, PPC_STRICT_FP=1 turns off all fast paths.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
tlbie marks the whole TLB for flushing, which the architecture allows since
real CPUs invalidate a congruence class.  Remembering the pages tlbie named
and flushing only those is available under PPC_TLBIE=page.

It stays off by default: it measures the same as the faithful behaviour
once emptying the jump cache is O(1) (fork+exec and allocation benchmarks
in a Tiger guest, min of five runs), and there is no reason to deviate from
the architecture for nothing.

Opt-in: PPC_TLBIE=page. Off by default, so the architectural behaviour
is unchanged.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
A 32-bit hash MMU's TLB is tagged with the VSID, so reloading the segment
registers keeps it.  Mac OS X reloads 12 of the 16 on every exception
entry and exit, and QEMU flushed its whole TLB each time, up to 50,000
times a second.  Give each of the 4 most recently used sets of segment
register values its own pair of MMU indexes (index | slot << 2, the
TB's cs_base holding the slot) and evict the least recently used set
instead of flushing.  tlbie and tlbia still flush every set.  About 90%
of switches find their set cached; Geekbench 2 integer on Tiger goes
from 1190 to 1242.  PPC_SR_SLOTS=0 restores the flush.

PPC_TLBIE=page now flushes the named page in all 16 segments, since
tlbie ignores the segment number and a page can be mapped through
another segment register or in another set's TLB.

Opt-in: PPC_SR_SLOTS=1. On Tiger the slots made the Quartz Extreme
desktop much slower (opening a folder took up to 7 s, Dashboard ran at
about 1 fps instead of 7), so by default the segment registers flush the
TLB as before and only the first slot is used.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
…fect

After the previous change, three quarters of the vCPU's time in a file
copy was still do_invalidate_BAT: Tiger's physical-copy window maps
DBAT0/1 thousands of times a second, and each mapping flushed its 32
pages with tlb_flush_page -- every MMU index, plus two jump-cache clears
per page.

A data BAT translates only data accesses made with translation on.
Add tlb_flush_data_pages_by_mmuidx() (cputlb.c), which flushes a run of
pages for the given MMU indexes under one lock and leaves the TB jump
cache alone, and use it for DBATs on the 32-bit hash MMU with the
translated indexes (0x3333; real-mode indexes have bit 1 set).  IBATs
keep the full page flush.  PPC_DBAT_FAST=0 restores it for DBATs too.

Tiger 10.4.11, M1 Max, ditto of a 480 MB file inside the guest
(3 alternating pairs, md5 verified): 14.5 / 14.2 / 14.4 s ->
4.8 / 4.8 / 4.7 s.  Quake III timedemo unchanged (70.7 vs 69.6 fps).
Stress test passes.

Default-on for DBATs on 32-bit hash MMUs; PPC_DBAT_FAST=0 flushes them
like IBATs again.

Based on code from linuxkid473/ppc-osx-vgpu (poweremu-qemu, r300 branch, by
Spartan0285, Vihaan Nathan, Teufelhunden-0311 and linuxkid473;
GPL-2.0-or-later).

Signed-off-by: Bruno Castelló <bfcastello@gmail.com>
Assisted-by: Claude:claude-opus-5-5
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.

3 participants