Skip to content

common: preserve Windows fastmem protections across partial unmaps - #2361

Merged
capy-ai[bot] merged 1 commit into
nx2from
capy/preserve-windows-fastmem
Nov 22, 2026
Merged

capy-ai[bot] merged 1 commit into
nx2from
capy/preserve-windows-fastmem

Conversation

@zortos293

@zortos293 zortos293 commented Nov 21, 2026 •

Copy link
Copy Markdown
Owner

A legal guest heap shrink or partial UnmapPhysicalMemory can remove part of a Windows fastmem view. Windows tears down the whole view and maps the pages it keeps as read-write. The guest page table, GPU cache counters, and armed read traps still describe those pages as protected, and the unmap path doesn’t re-arm them. A later fastmem store can bypass GPU cache invalidation, leaving a texture or buffer stale. Losing a read trap can also expose stale GPU-written data to the guest CPU.

Before a partial unmap, snapshot the protection runs of the pages that will stay mapped. Restore each run immediately after remapping its piece. The single-view Protect path takes a shared lock so a concurrent protection change can’t get lost between the snapshot and restore. A query or restore failure stops the emulator. Guest accesses can still race the brief interval between mapping a piece read-write and restoring its protections. Full unmaps do not take the new lock, and Linux already keeps the permissions of pages outside the freed range.

Port of dzervas/Lichii@23eb549a71 by Dimitris Zervas. Open Direct NVN PR #1944 contains the same source commit; this PR lets the shared-memory fix land independently. #2108 handles in-flight remap faults but does not restore protections. #2240 does not touch UnmapOnePlaceholder or Protect; this patch applies cleanly to its current head, and its backing-commit path commits the pages before mapping them.

Game-wide rendering/timing change: yes. All titles using Windows fastmem share this path, so this is an AIO candidate.

Built the Linux Release tests target with GCC 13. The new [host_memory] regression and all 19 HostMemory:* cases passed. Both changed files passed AArch64 syntax checks. The Windows Actions job compiled both files and passed the test suite. No game was run.

Open in Capy

Summary by CodeRabbit

  • Bug Fixes
    • Partial memory unmaps now preserve the access protections and contents of the memory that remains mapped.
    • Memory mapping failures are reported more reliably, helping prevent callers from treating an unsuccessful mapping as successful.

A partial unmap recreates the kept Windows views read-write even when the rasterizer still tracks their pages as protected. Save the protections before tearing down a view and restore them on the kept pieces before returning.

Port of dzervas/Lichii@23eb549a71 by Dimitris Zervas:
dzervas@23eb549

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds protection synchronization and protection capture and restoration around partial host-memory unmaps. It also makes mapping failures observable through MapView’s result and adds Windows and Linux tests for retained-page protections and backing data.

Changes: Host memory mapping

Layer / File(s) Summary
Protection synchronization and partial remapping
src/common/host_memory.cpp
Protect uses a shared lock for its fast VirtualProtect attempt. Partial unmaps take an exclusive lock, save retained regions’ non-read-write protections, and restore them after remapping. MapView returns a boolean, which Impl::Map checks.
Protection and backing-data validation
src/tests/common/host_memory.cpp
A Windows- and Linux-gated test checks retained-page protections and data after a middle unmap. It then restores read-write access and verifies a write reaches the backing memory.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related issues

Possibly related PRs

  • zortos293/Lichii#1780 — Both changes modify the Windows HostMemory::Impl::Protect path and its locking behavior.

Suggested reviewers: reinuseslisp, reiixan

Merge Risk: Minimal · up to 7f38c

This change preserves Windows fastmem page protections across partial unmaps and heap shrinks. No concrete defect was found. The Windows-only path should be confirmed by the Windows CI job before merge, but it does not block merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "common: preserve Windows fastmem protections across partial unmaps" matches the main change. The raw summary and PR objectives both describe snapshotting and restoring page protections for …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "common: preserve Windows fastmem protections across partial unmaps" matches the main change. The raw summary and PR objectives both describe snapshotting and restoring page protections for kept pages during a partial unmap on Windows host memory, with a shared lock in Protect. The title is specific, concise, and follows the subsystem-prefix convention. It does not cover the test addition or the MapView error reporting change, which is acceptable for a title.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 08386acd-e555-4d10-acca-32f1bd76542a
Commits

Reviewing files that changed from the base of the PR and between ef51faa and 7f38c10.

Files selected for processing: 2

  • src/common/host_memory.cpp
  • src/tests/common/host_memory.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.

@capy-ai capy-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capy found no issues.

Open in Capy

atmanli added a commit to atmanli/Lichii that referenced this pull request Nov 21, 2026
atmanli added a commit to atmanli/Lichii that referenced this pull request Nov 21, 2026
argotte pushed a commit to argotte/Fritchii that referenced this pull request Nov 21, 2026
A partial Unmap that splits a view unmaps it whole and maps the kept parts again
read-write, which wiped every write trap on them, with FRITCHII_PROTECT_UNLOCKED on or off:
the page table still said RasterizerCachedMemory, so CPU writes there were missed.

UnmapOnePlaceholder now reads the kept parts' committed non-read-write runs (VirtualQuery)
after the first split_unmap_seq increment, while the old view is still mapped, and puts them
back (VirtualProtect) after both remaps, before the sequence turns even. A locked Protect is
ordered by placeholder_mutex; an unlocked one either changed the view before the read (so
it is restored) or sees the sequence odd or changed and is redone under the lock after the
restore, so newer flags win. Pages that are not committed (EDEN_NX2_SEC_RESERVE) are skipped,
and failures are logged rather than fatal. The first 3 splits that restore anything log it,
and the 10 s host protect line counts them. Adds the ported unit test.

Approach of dzervas/Lichii 23eb549 by Dimitris Zervas, as ported by zortos293 in
zortos293/Lichii zortos293#2361 (7f38c10), adapted: no extra protect lock (our split sequence redo
covers that race and keeps the fast path lock-free), the restore after both remaps so the
unmapped window stays as short as before.

Review R3 should-fix 4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
atmanli added a commit to atmanli/Lichii that referenced this pull request Nov 21, 2026
atmanli added a commit to atmanli/Lichii that referenced this pull request Nov 22, 2026
@capy-ai
capy-ai Bot merged commit e0ba425 into nx2 Nov 22, 2026
6 checks passed
conongnang added a commit to conongnang/Lichii that referenced this pull request Nov 22, 2026
Takes the 30 nx2 commits up to zortos293#2428, among them zortos293#2350 (the GPU render
follower from zortos293#2319 is opt-in again: on Ocarina of Time it cost a
cross-thread wake-up per warm draw for no overlap) and zortos293#2361 (partial
unmaps keep the fastmem protections of the kept views on Windows).

- shader_cache_v2 driver blobs: zortos293#2349's store finds this session's
  records at their file offsets; it replaces our in-memory copy
  (fe8ae97) of the same fix.
- vk_pipeline_cache: our CanDiscardCurrentDraw (depth-only and mesh
  draws) asks the texture cache through the render lend like nx2's
  inline version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nitramvi pushed a commit to nitramvi/Lichii-arc that referenced this pull request Nov 22, 2026
…ortos293#2361 Windows fastmem partial unmaps, zortos293#2415 dma_pusher split writes, zortos293#2386 optical flow, zortos293#2425 render follower opt-in, zortos293#2435, zortos293#2436, Mod Store) into prueba

Conflicts: staging pool (prewarm floor + upstream IsFree clock), rasterizer (upstream
ClearImpl split; OoT camera from facts.camera keeps LICHII_OOT_ALLOW_PADDING and the
LICHII_DIAG refusal log), pipeline cache (our BuiltPipeline keeps its place and takes
upstream's GPURender lend for the colour-history lookup), graphics pipeline header and
TickFrame (both sides).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017QULzm8uqSe8KvGLf1q689
dzervas added a commit to dzervas/Lichii that referenced this pull request Nov 22, 2026
Conflicts:
- core.cpp: nx2 sets the application process before the hypervisor check; NvnHook::Install
  stays right before SetupForApplicationProcess.
- nx2_gsv.h / nx2_present.h: the present pacing moved to Nx2PresentBookkeeping here; nx2's
  240 fps cap for unpaced unlocked games (zortos293#2178) is ported into its PaceToVblank.
- host_memory.cpp (Windows): nx2's partial-unmap protection fix (zortos293#2361) replaces this branch's
  equivalent one; Protect keeps returning whether it protected everything, and the backing
  views stay next to nx2's FindBackingRuns. Linux Protect drops AdjustMap as nx2 does.
- renderer_vulkan.cpp: FlyHigh drains and the render lend run before the direct-mode teardown
  and frame pick-up.
- vk_master_semaphore.cpp: nx2's submit watchdog with this branch's timeline wait.
- memory.cpp, vk_rasterizer.cpp, tests: both sides kept.
danieljesus added a commit to danieljesus/Lichii that referenced this pull request Nov 24, 2026
…ortos293#2240)

Refreshes fix/windows-backing-reserve (4e92798, last merged nx2 at
7e7bd04) on current nx2, as asked on zortos293#2240. No textual conflicts. Four
files changed on both sides: src/common/host_memory.cpp (zortos293#2361, Windows
fastmem protections across partial unmaps), src/tests/common/host_memory.cpp,
src/tests/video_core/buffer_cache_ports.cpp and
src/tests/video_core/maxwell_fast_method.cpp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RcHkcvCLwwzA1tzJpyBgjk
Claude-Session: https://claude.ai/code/session_01RcHkcxSN7aBbebPQ17pqcF
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.

1 participant