Guard pages: emulate the faulting access instead of taking the exception #1305
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork#1305
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@liuycsbxbxn on GitHub · 2026-11-26 13:31 UTC · original
Split out of #1273.
Suggested labels: performance / memory
Verification cost:
[GPU][TITLE]What the meta-issue asks
Where it lands
crates/victory-gpu/src/watch.rs—writes_as(the fault classification),failed_asking(the diagnostic on a failed ask),guard/guard_at,protect/protect_here,open/open_here.VICTORY_LOCK_STATS-visible cost today; see also #1238 (hot-block pinning) and #1169.Acceptance
failed_askingpath must still fire).Why not just take it
Trampolining needs instruction-length decoding on x86-64 (and not breaking a partially-executed instruction); that is the part worth discussing before anyone writes it.
@JayFoxRox on GitHub · 2026-11-26 17:04 UTC · original
More hints to communicate the idea:
Current state
This is very expensive due to exception handling (microsecond range) and page lock/unlock (microsecond range) which requires a kernel-round-trip.
Speculation: I assume the game uses the CPU readback to find which resources need to be streamed, so any delay here causes streaming latency.
Proposal
After the first encounter of such code locations (access to GPU resources) the page-exception would not longer happen and there'd be standard control flow (nanosecond operations); everything stays in user-code.
Additionally, it would help identify the root-cause for reads/writes by RIP or stack analysis, so by analyzing the game could, it could be identified and categorized how critical a GPU resource re-upload or download is (as in: why does the game try to access the GPU resource and is it important to be instant).
--
A potential improvement could build a cache of these locations so patching could be done upfront for known locations. Obviously this requires checks that the version of the game code is still the same.
@liuycsbxbxn on GitHub · 2026-11-26 17:04 UTC · original
Per your replies on #1306 and #1307, this is the prerequisite for both: the trampoline is what lets either side drop the fault + re-lock path instead of paying it 1000x over. I will scope this issue as that patch and keep the other two as follow-ups, so the dependency direction is explicit.
@liuycsbxbxn on GitHub · 2026-11-26 17:10 UTC · original
Good - that matches the model in the issue body, and "patch once, then stay in user code" is the part that actually removes the cost; the fault path is not something we can make cheaper, only avoid.
Breaking it down, tagged with what can be built and tested without a card or a game dump:
1. Fault-site recorder (phase 0,
[CPU]only). In the handler, log RIP (module + RVA), the surrounding bytes and the decoded instruction, and patch nothing yet. This is how we learn which instruction forms actually touch resources - the emulator subset should be driven by that data, not guessed.2. Instruction length + operand decoder (
[CPU]only). Exact x86-64 decoding for the subset found in (1). It has to be exact: a wrong length corrupts whatever follows when the instruction is copied into the trampoline. Unit-testable against a corpus; no card needed.3. Trampoline patch + handler table (
[CPU]only). VirtualProtect the code page, write the jump, flush the instruction cache; the per-site handler emulates the original instruction against the full-access view. Testable against a synthetic target (map a page no-access, place known bytes, assert the handler runs once and that the site behaves afterwards).4. Read path (#1306) and write path (#1307),
[GPU]. The handler decides download-on-read and queues the re-upload on write; this is the part that needs the card and the title to measure honestly.5. Patch cache (
[CPU]only). Keyed by (module build id, RVA, hash of the original bytes); verify the bytes still match before pre-patching, otherwise fall back to lazy patching for that site.6. Attribution (
[CPU]plumbing,[GPU][TITLE]for the content). RIP/stack -> which subsystem reads which resource; the report format and aggregation are CPU-only, the interesting conclusions are not.Proposed order: 1 -> 2 -> 3, then 4 on hardware. (1)-(3) are all verifiable here without a card, and they are what #1306 and #1307 would sit on. If you want, I can start with (1)+(2) as a self-contained patch, so the instruction mix from a real capture decides how far the emulator has to go.
One question before I start: should the recorder be always-on but rate-limited, or opt-in per session? Always-on in a shipping path has a real cost, and a rate limit would bias the instruction mix we collect.
@liuycsbxbxn on GitHub · 2026-11-26 17:27 UTC · original
Small update so this is not only design talk: I built the parts that need no card, and they are green on a plain Windows box (26 tests, no warnings).
I am deliberately stopping there and waiting for your call before writing the per-instruction emulator: which operand forms matter should come from the recorder's data, not from me guessing at the subset. It is a self-contained crate under prototypes/, not wired into the workspace, so it costs the normal build nothing.
And thank you for writing the design down in that much detail. Your "patch once, then stay in user code" framing is exactly what made me test the fault path instead of assuming it, and it is the reason the prototype was tractable at all. Happy to keep building in whatever direction you prefer - looking forward to it. 共勉.