Skip to content

Add shared address-region management and MapMemory source ownership - #151

Closed
b4f8abbb24dbf56a7d0280f296e16eeb wants to merge 1 commit into
seedds:mainfrom
b4f8abbb24dbf56a7d0280f296e16eeb:feature/native-memory-management
Closed

b4f8abbb24dbf56a7d0280f296e16eeb wants to merge 1 commit into
seedds:mainfrom
b4f8abbb24dbf56a7d0280f296e16eeb:feature/native-memory-management

Conversation

@b4f8abbb24dbf56a7d0280f296e16eeb

Copy link
Copy Markdown
Contributor

Why

Mapping state, memory budgets and QueryMemory need a consistent view of guest memory. Separate bookkeeping can disagree after overlapping mappings, and a stack mapping's source can otherwise be borrowed twice or released before copyback.

This group introduces shared native memory bookkeeping and uses it in the active SVC paths, while providing the reservation core needed for a later SDK allocator replacement.

Shared address-region management

A host-owned manager tracks the actual native alias range. MapPhysicalMemory, UnmapPhysicalMemory, QueryMemory and used-memory accounting share its committed-page state. Overlapping maps charge only newly committed 4 KiB pages; repeated unmaps cannot subtract them twice.

Heap resizing, mapping changes and application-pool checks are serialized. Failed host protection changes preserve accounting and the previous heap size. Committed alias runs are reported as Heap/RW, with the existing profile-controlled memory-limit policy retained.

The manager also provides exact-size aligned reservations, free-gap reuse and bounded host metadata. Production currently keeps reservation ownership with the SDK and rejects host reserve/release calls in that mode. This prevents two allocators from assigning the same address range during a partial transition.

Recommit without damaging neighbors or forcing large first allocations resident

Newly committed logical slices are initialized without clearing live bytes in an overlapping request. A host-page exposure bitmap distinguishes fresh anonymous backing from pages previously made writable.

Fresh backing retains the OS's lazy zero-fill, so a multi-GiB first map does not touch every page just to clear it. Reused logical slices are explicitly zeroed, including slices that shared an earlier writable host page. Live 4 KiB neighbors retain access to their enclosing host page, and affected host protection changes notify the GPU dirty tracker.

MapMemory source ownership

MapMemory exclusively borrows a validated source range from opened heap memory, committed alias pages or writable static-module data. Read-only module seals are respected.

Conflicting mappings, backing release, heap shrink and permission/attribute requests cannot invalidate a borrowed source. Ownership is published only after destination setup succeeds. Exact unmap copies contents back before releasing the source.

QueryMemory splits runs at ownership boundaries, preserves the original memory state, and reports the borrowed source with the borrowed/kernel-Locked attribute (bit 0) and no user permissions. It initializes the complete 40-byte MemoryInfo output, including reference counts and the reserved field.

This preserves the native copy/move model. Ownership is tracked logically while hardware protections remain host-page-granular.

Runtime benefit and patch reduction

The shared SVC paths now report committed alias pages accurately, enforce memory budgets against the same state, and keep borrowed stack sources valid until copyback. Lazy first exposure avoids touching a large anonymous mapping solely to zero it, while explicit zeroing on reuse prevents stale data from appearing in newly committed logical pages.

The next step is moving SDK address-region metadata out of scarce guest virtual-address space. Current per-build cuts shrink its fixed reservation from approximately 1 GiB to 62 MiB. Host-owned metadata could eliminate that guest reservation once SDK allocator takeover is complete, making the 38 metadata-size patch entries across 19 SDK builds candidates for removal. This concerns virtual-address capacity, not 1 GiB of physical RAM.

This PR supplies the reservation core. SDK initialization, region allocation and freeing still use the SDK's allocator; routing them through the core requires verified ABI and internal-call coverage before the existing cuts can be retired. The separate 12–62 GB CPU-arena assumptions require allocator adaptation beyond metadata management.

Design references

These behavioral and interface references informed the memory implementation:

Host-owned metadata, 4 KiB logical accounting over host-page boundaries, and the native copy/move backend are nxnative-specific adaptations of these contracts.

Validation

CI passes

Combine host-owned address metadata, alias commitment accounting and source ownership tracking. Preserve profile budgets, rollback and exact copyback. Retain all game patches and add portable allocator, SVC, concurrency and profile regressions.
seedds added a commit that referenced this pull request Oct 25, 2026
Incorporate the active SVC improvements from PR #151 around the existing
alias bitmap and mapping registry. Serialize heap changes, physical maps,
stack ownership and memory queries with one lock; report committed alias
runs and borrowed sources; initialize reused logical pages while preserving
live neighbors and lazy first exposure. Retain PR #127's fail-fast host
protection policy and the log formats parsed by the NSP heap probe.

Game smoke tests exposed two integration requirements: Raging Loop's SDK
extends committed windows across borrowed stack sources, so allow recommit
that preserves existing bytes; Tears of the Kingdom's audio helper needs
guest-backed stacks, so allocate host-created guest-thread stacks through
SDK malloc and free them after thread destruction and copyback.

Keep SDK reservation ownership and existing patches. The unused host
reservation allocator from #151 is not included.

Validation: full host suite; focused UBSan, ASan and ThreadSanitizer tests;
real dirty-tracker concurrency tests; heap-probe parser compatibility; Mac
host builds; iOS arm64 syntax checks; all 60 profile layout checks.
Raging Loop and Tears of the Kingdom passed guest-thread self-checks and
ran to deliberate timeouts after the startup fixes. Their shader caches
are stale for this checkout, so rendering/performance are not validated.
No IPA build or iOS device validation was performed.

Co-authored-by: b4f8abbb24dbf56a7d0280f296e16eeb <51352400+b4f8abbb24dbf56a7d0280f296e16eeb@users.noreply.github.com>
@seedds

seedds commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Applied the active SVC improvements to main in cae5ec2, with Co-authored-by credit to b4f8abbb24dbf56a7d0280f296e16eeb. Thank you for the contribution.

The incorporated change uses the existing alias bitmap and mapping registry with one memory-state lock. QueryMemory now recognizes committed alias pages and borrowed sources; heap changes and physical maps share serialized budget checks; stack sources remain owned through copyback; and recommit clears newly committed logical slices while preserving live neighbors and lazy first exposure.

Changes made during integration:

  • Retained the fail-fast protection-error policy from Fail fast when SetHeapSize cannot change host page protections #127 (already applied in 9ba44da). Preserving counters alone does not establish safe recovery from a potentially partial host protection change.
  • Preserved the SetHeapSize diagnostic formats consumed by the NSP heap-profile probe, and added a regression check using actual dispatcher output.
  • Allowed physical recommit across borrowed sources when existing bytes are preserved. Raging Loop's SDK extends committed windows this way; rejecting every intersecting map caused a startup abort. Decommit remains rejected while the source is borrowed.
  • Moved host-created guest-thread stack backing to SDK malloc, retaining it until SDK thread destruction/copyback. The former host allocation failed source validation when Tears of the Kingdom created its audio worker.
  • Deferred the unused host reservation allocator. SDK reservation ownership, game profiles and existing SDK patches remain in place.

Validation passed: the full host suite; focused UBSan, ASan and ThreadSanitizer tests; real GPU dirty-tracker concurrency tests; heap-profile parser compatibility; Mac host builds; iOS arm64 syntax checks; and address-layout checks for all 60 profiles.

Raging Loop and Tears of the Kingdom passed their previous startup failures and guest-thread self-checks, then ran until deliberate 30/40-second timeouts. Their available shader caches are stale for this checkout, so these runs do not validate rendering or performance. No IPA build or iOS device test was performed.

Closing this PR because the scoped, corrected SVC work has been incorporated into main. SDK allocator takeover can be considered separately once its ABI and call-path coverage are established.

@seedds seedds closed this Oct 25, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
Incorporate the active SVC improvements from PR seedds#151 around the existing
alias bitmap and mapping registry. Serialize heap changes, physical maps,
stack ownership and memory queries with one lock; report committed alias
runs and borrowed sources; initialize reused logical pages while preserving
live neighbors and lazy first exposure. Retain PR seedds#127's fail-fast host
protection policy and the log formats parsed by the NSP heap probe.

Game smoke tests exposed two integration requirements: Raging Loop's SDK
extends committed windows across borrowed stack sources, so allow recommit
that preserves existing bytes; Tears of the Kingdom's audio helper needs
guest-backed stacks, so allocate host-created guest-thread stacks through
SDK malloc and free them after thread destruction and copyback.

Keep SDK reservation ownership and existing patches. The unused host
reservation allocator from seedds#151 is not included.

Validation: full host suite; focused UBSan, ASan and ThreadSanitizer tests;
real dirty-tracker concurrency tests; heap-probe parser compatibility; Mac
host builds; iOS arm64 syntax checks; all 60 profile layout checks.
Raging Loop and Tears of the Kingdom passed guest-thread self-checks and
ran to deliberate timeouts after the startup fixes. Their shader caches
are stale for this checkout, so rendering/performance are not validated.
No IPA build or iOS device validation was performed.

Co-authored-by: b4f8abbb24dbf56a7d0280f296e16eeb <51352400+b4f8abbb24dbf56a7d0280f296e16eeb@users.noreply.github.com>
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 12, 2026
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 12, 2026
…e-43only

seedds#163's Speedhack setting takes field 21 of the profile row (skipLayersOf,
Profile.skipLayers); the 4:3 variants move to field 22.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 12, 2026
…settings, Ocarina of Time on Qualcomm

- Game language: Polish (pl) and Thai (th), from SDK 21's own settings table (sdk+0xc62b5c in Ocarina of Time lists pt-BR, pl, th); the launcher list, the host's DM_LANG mapping and GetAvailableLanguageCodes. Ocarina of Time (`0400486024DEC000`) carries EUpl messages, Donkey Kong Bananza (`040074A01BF12000`) 1.1 EUpl and THth.
- Launcher: one full page per game with tabs Performance (Picture, Speed, Advanced), Game (options, Game language, Controls), Saves and Storage; rows wrap and say what they do; profile options keep their description and say when they need Prepare again.
- Recommended settings per game and phone (game.tsv recommend rows, Recommend.java): a card with Recommended / Custom and tags on the rows; facts from GLES at launcher start before the first readiness check. Ocarina of Time on Qualcomm: system driver, Performance preset, Full vegetation off.
- Resolution presets (res_preset, scene_output rows) and Scene resolution overlays from a game's own dynamic resolution palette (scene_res.py, DM_ROMFS_OVERLAY).
- Qualcomm's own driver: mesh draws through compute by default (full Ocarina of Time foliage; DM_VK_MESH=1 keeps native mesh shading).
- Mesh draws through compute: a draw takes its own record's entry, and prewarm drops mesh / pixel pairings that do not feed each other (see-through on Adreno 830, black-line flicker on Turnip, issue seedds#180).
- Texture serial map: probes past tombstones, live and tombstone counts apart (the table no longer grows with every serial ever registered).
- pack_android.py: a profile with a selected shader set builds that set on the Mac and Windows (issue seedds#178).
- PRs seedds#150, seedds#151, seedds#171 (FrenklinFish) and seedds#156, seedds#163 (Omnicrash) were merged on Forgejo before this update.
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 14, 2026
The profile gets a second recommend row, "recommend any upscaler=fsr3": on every phone the DLSS stand-in's Upscaler row reads FSR 3.1 (DM_VK_DLSS=4) unless the player chose another for the game. The value is computed at each read and never stored, so a player's own choice is kept, and other games on the stand-in keep GSR 2. The existing row (an installed Turnip on Qualcomm's driver before 512.800) sets only the driver, so the two rows apply independently.

FSR 3.1 is recommended for its picture against GSR 2 (c2), as measured in seedds#151: finer detail kept (the planks and railing in Dagsion, the same scene), about 30-40 % less unexplained flicker in camera pans and dashes, and still-edge flicker equal to GSR 2's at the default sharpness 0.5. The cost is more GPU time: about 2 ms on Turnip and about 34 against 26 ms on an older stock driver (seedds#151), and about 3 ms on the OnePlus Pad 3's stock 512.800. It also runs on Turnip and on Qualcomm's own driver, switches live from the in-game panel, and gives clean frames with frame generation wherever GSR 2 does. The game's note records these facts, and its Upscaler table now lists FSR 3.1. test_android_recommend and RecommendJvmTest check the profile's rows: FSR 3.1 on the stock driver, the old Qualcomm driver (which also gets the Turnip row), Turnip and a driver not yet checked.

With the help of Claude
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