macOS: the safe copies' masked stores as plain stores under Rosetta 2 (KLBF_SAFE_PLAIN_STORES) #436

Open
vakaobr wants to merge 1 commit from upstream-pr/436 into v26123
vakaobr commented 2026-12-01 15:19:58 +00:00 (Migrated from github.com)

Imported from PastiiJay/klbf-lab. Originally posted by vakaobr on 2026-12-01T15:19:58Z.


What this changes

victory_safe_put and victory_safe_merge (crates/victory-gpu/src/safecopy.rs) each store the dwords that change with one AVX2 masked store (vpmaskmovd to memory). This adds a twin of each, victory_safe_put_plain and victory_safe_merge_plain, that stores the same dwords with plain movs, and uses the twins when KLBF_SAFE_PLAIN_STORES says so:

  • unset: on when Wine's host is macOS (wine_get_host_version says Darwin), off everywhere else;
  • 1 forces the twins anywhere, 0 keeps the masked stores even on macOS.

The masked routines are untouched, so nothing changes on Windows or Linux unless the option is set.

Details:

  • The twins take the same arguments, return the same value, write the same counts and leave through a failing exit of their own. They use only registers that are free at that point (r10/ymm0 in put; eax, ymm1, ymm2 in merge) and do not touch the stack, as the originals.
  • Every store of a twin lies between its own _at and _after labels, and failing_exit knows the two new ranges, so the handler in watch.rs treats a fault in a twin as it treats one in the original.
  • watch::wine_host becomes pub(crate) so safecopy can ask it.
  • docs/klbf/launch-options.json has the new option (risk: experimental), and LAUNCH-OPTIONS.md is regenerated from it with scripts/launch-options-doc.ps1.

Why (issue, mission or report)

#435 (macOS test report). Under Rosetta 2 a masked store that faults cannot be resumed: rosetta error: unexpectedly need to EmulateForward on a synchronous exception, and the process hangs. On an M5 Pro with fault tracking on, a run stopped about 68 s in at victory.exe+0x57e2b2, bytes c4 e2 5d 8e 19 = vpmaskmovd [rcx], ymm4, ymm3: the store of victory_safe_merge.

KLBF_NO_FAULT_TRACKING removes the faults the layer makes itself, but title memory can still fault under these two routines (pages the title made read-only or unmapped: the reason they have a failing exit), so I think the twins are wanted with the Mac mode as well.

How it was tested

  • Graphics card and driver: Apple M5 Pro (MacBook Pro Mac17,8, 48 GB), macOS 27.2.1, CrossOver 26.3.0 with D3DMetal.
  • KLBF Lab version: this change is made on v26123 at 065ee66. The run on the Mac was on stable at 4c79ac5, with the same plain stores put in place of the masked ones without a switch.
  • Before / after (fps, screenshot, log line):
    • Before: the hang above, about 68 s into the run.
    • After: no Rosetta error in two more runs of about 2 min each; menus at 30 fps, the title loaded into the world (the runs then ended for other reasons: D3DMetal pipeline failures and the title's watchdog, see the issue).

What was checked off the Mac:

  • The assembly text of the originals and the twins, taken from this branch's safecopy.rs, assembled and run side by side on an x86-64 CPU (Linux, extern "win64"): 200,000 randomized runs each of put and merge, with the title winning on every other merge. Return values, the three counts, title memory and the held copy were equal every time.
  • In the same harness, with the destination page read-only and a handler that works as failing_exit does: 127,346 faults opened and resumed with the same result, 7,660 sent to the failing exit with nothing stored, and none outside the twins' labels.
  • cargo clippy -p victory-gpu --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warnings: clean for these files (cross-checked from Linux with a newer toolchain, so with -A deprecated for fetch_update in files this does not touch).
  • tests/launch-options.ps1: all passed (616 options). cargo test -p xtask (repository_passes_policy): passed.

What was not done, so you know what to look at:

  • The three new tests in safecopy.rs (plain_stores_are_for_macos_unless_asked, the_twins_of_plain_stores_do_as_the_masked_ones, faults_in_the_twins_of_plain_stores_are_failures) are compiled but have not been run: they need Windows. CI will be their first run.
  • The switch itself (the twins chosen at run time by the Wine host) has not run on a Mac yet; the same instructions have, unconditionally. I will run this branch on the Mac and add the result.
  • No run on Windows hardware by me.

Checklist

  • No game files, and no name from the game or its publisher (characters, places, missions)
  • New behaviour behind a KLBF_... launch option, off by default (unless it's a plain fix) — off everywhere except under macOS, where the masked stores cannot work
  • Code from another project: compatible licence, and credited — none; all of it is written for this change (GPL-3.0)
<!-- greyfinch-sync:PastiiJay/klbf-lab:issue:5588699746 --> Imported from [PastiiJay/klbf-lab](https://github.com/PastiiJay/klbf-lab/pull/436). Originally posted by [vakaobr](https://github.com/vakaobr) on 2026-12-01T15:19:58Z. --- ## What this changes `victory_safe_put` and `victory_safe_merge` (`crates/victory-gpu/src/safecopy.rs`) each store the dwords that change with one AVX2 masked store (`vpmaskmovd` to memory). This adds a twin of each, `victory_safe_put_plain` and `victory_safe_merge_plain`, that stores the same dwords with plain `mov`s, and uses the twins when `KLBF_SAFE_PLAIN_STORES` says so: - unset: on when Wine's host is macOS (`wine_get_host_version` says `Darwin`), off everywhere else; - `1` forces the twins anywhere, `0` keeps the masked stores even on macOS. The masked routines are untouched, so nothing changes on Windows or Linux unless the option is set. Details: - The twins take the same arguments, return the same value, write the same counts and leave through a failing exit of their own. They use only registers that are free at that point (`r10`/`ymm0` in put; `eax`, `ymm1`, `ymm2` in merge) and do not touch the stack, as the originals. - Every store of a twin lies between its own `_at` and `_after` labels, and `failing_exit` knows the two new ranges, so the handler in `watch.rs` treats a fault in a twin as it treats one in the original. - `watch::wine_host` becomes `pub(crate)` so `safecopy` can ask it. - `docs/klbf/launch-options.json` has the new option (risk: experimental), and `LAUNCH-OPTIONS.md` is regenerated from it with `scripts/launch-options-doc.ps1`. ## Why (issue, mission or report) #435 (macOS test report). Under Rosetta 2 a masked store that faults cannot be resumed: `rosetta error: unexpectedly need to EmulateForward on a synchronous exception`, and the process hangs. On an M5 Pro with fault tracking on, a run stopped about 68 s in at `victory.exe+0x57e2b2`, bytes `c4 e2 5d 8e 19` = `vpmaskmovd [rcx], ymm4, ymm3`: the store of `victory_safe_merge`. `KLBF_NO_FAULT_TRACKING` removes the faults the layer makes itself, but title memory can still fault under these two routines (pages the title made read-only or unmapped: the reason they have a failing exit), so I think the twins are wanted with the Mac mode as well. ## How it was tested - Graphics card and driver: Apple M5 Pro (MacBook Pro Mac17,8, 48 GB), macOS 27.2.1, CrossOver 26.3.0 with D3DMetal. - KLBF Lab version: this change is made on `v26123` at `065ee66`. The run on the Mac was on `stable` at `4c79ac5`, with the same plain stores put in place of the masked ones without a switch. - Before / after (fps, screenshot, log line): - Before: the hang above, about 68 s into the run. - After: no Rosetta error in two more runs of about 2 min each; menus at 30 fps, the title loaded into the world (the runs then ended for other reasons: D3DMetal pipeline failures and the title's watchdog, see the issue). What was checked off the Mac: - The assembly text of the originals and the twins, taken from this branch's `safecopy.rs`, assembled and run side by side on an x86-64 CPU (Linux, `extern "win64"`): 200,000 randomized runs each of put and merge, with the title winning on every other merge. Return values, the three counts, title memory and the held copy were equal every time. - In the same harness, with the destination page read-only and a handler that works as `failing_exit` does: 127,346 faults opened and resumed with the same result, 7,660 sent to the failing exit with nothing stored, and none outside the twins' labels. - `cargo clippy -p victory-gpu --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warnings`: clean for these files (cross-checked from Linux with a newer toolchain, so with `-A deprecated` for `fetch_update` in files this does not touch). - `tests/launch-options.ps1`: all passed (616 options). `cargo test -p xtask` (`repository_passes_policy`): passed. What was **not** done, so you know what to look at: - The three new tests in `safecopy.rs` (`plain_stores_are_for_macos_unless_asked`, `the_twins_of_plain_stores_do_as_the_masked_ones`, `faults_in_the_twins_of_plain_stores_are_failures`) are compiled but have not been run: they need Windows. CI will be their first run. - The switch itself (the twins chosen at run time by the Wine host) has not run on a Mac yet; the same instructions have, unconditionally. I will run this branch on the Mac and add the result. - No run on Windows hardware by me. ## Checklist - [x] No game files, and no name from the game or its publisher (characters, places, missions) - [x] New behaviour behind a `KLBF_...` launch option, off by default (unless it's a plain fix) — off everywhere except under macOS, where the masked stores cannot work - [x] Code from another project: compatible licence, and credited — none; all of it is written for this change (GPL-3.0)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/436:upstream-pr/436'
git switch 'upstream-pr/436'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'v26123'
git merge --no-ff 'upstream-pr/436'
git switch 'upstream-pr/436'
git rebase 'v26123'
git switch 'v26123'
git merge --ff-only 'upstream-pr/436'
git switch 'upstream-pr/436'
git rebase 'v26123'
git switch 'v26123'
git merge --no-ff 'upstream-pr/436'
git switch 'v26123'
git merge --squash 'upstream-pr/436'
git switch 'v26123'
git merge --ff-only 'upstream-pr/436'
git switch 'v26123'
git merge 'upstream-pr/436'
git push origin 'v26123'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
PastiiJay/klbf-lab!436
No description provided.