runtime: define the XMem physical-page allocation pair #885

Merged
rasptum merged 2 commits from upstream-pr/892 into main 2026-11-23 16:42:46 +00:00
rasptum commented 2026-11-23 16:14:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-23T16:14:34Z.


Refs #880 and #870.

Desktop xmem.dll exports XMemAllocatePhysicalPages (ordinal 7) and XMemFreePhysicalPages (ordinal 8), backed by a new page-number registry, src/era_physical.{h,cpp}. There are no pe_map changes. Mapping, scatter mapping and transfer remain follow-ups.

Source-built comparison and negative controls

Test What it shows
cpu_native_xmem_physical_pair tests/cpu/xmem_physical_pair.asm imports the pair by name from xmem.dll and starts through exe_startup.asm/finish_exe. The same EXE runs under the Windows loader and under pe_map_run. It returns 0x880 when all 10 steps pass, or 0xF00 plus the failing step. The steps cover a valid two-page grant (count unchanged, distinct nonzero numbers, canary intact), a refused short-count release, a valid release afterwards, a refused double free, a refused bad flag (count reset to 0, three canaries intact) and a refused null-array release. The negative controls full_count and skip_release give 0xf04 and 0xf06 on both loaders. A provider missing either export stops the mapping before entry with unresolved import xmem.dll!<name>.
era_physical_controls The registry is linked next to a test-only build with ERA_PHYSICAL_LENIENT_RELEASE, a SlimEra-style release that clears any listed page. Each of 7 controls (short count, long count, single page of a grant, double free, never-issued page, forged mix, duplicate entry) is refused by the policy with the granted total unchanged, and accepted by the lenient build. This shows each control detects wrong ownership or page-count handling.
xmem_shim_physical_pages Through the named imports: three valid grants, 14 refused releases and 10 refused allocations. It checks last-error values, count reset and canary-filled arrays. A valid release still succeeds after the refusals. Exhaustion at 2^18 - 1 pages is followed by cleanup and a full 2^18-page grant.
era_physical_lifetime Exact granted totals across grants, a mixed-order release, exhaustion and cleanup.
xmem_shim_exports Also resolves the two new names.

Companion fixture and credit: triggered02's draft #887 holds their independent fixture and negative controls (CTest cpu_native_xmem_physical, branch xmem-physical-pages, 7dcc36d/3d206a1). This PR does not copy that code and does not reuse its CTest name; the test here is cpu_native_xmem_physical_pair. The EXE comparison here follows its design: Windows loader vs pe_map, a canary-filled page array and a missing-provider control. This version adds the finish_exe wrapper, so success is the nonzero code 0x880 rather than a bare return of 0, plus the two mutation variants. Native-start tests found that a bare entry return can end the process with a worker thread's code 0. Co-authored-by is in the commit.

Evidence and proposed desktop policy

The Microsoft Xbox-GDK-Samples (MIT, b1e77b6) establish the 64 KiB allocate/free pairing, the in/out count with a PULONG_PTR page array, and the BOOL success check. See AdvancedESRAM/PageAllocator.cpp L193-232 and FSR3FrameInterpolation.cpp L146-167.

The following is proposed desktop policy, labeled as such in the code comments and in docs/experiments.md:

  • Only MEM_64K_PAGES (0x20400000) is accepted. Any other bit, including the upper 32, returns FALSE.
  • Allocation is all or nothing. Page numbers are opaque, nonzero and unique.
  • On failure: *count = 0, the array is untouched, and the error is ERROR_INVALID_PARAMETER or ERROR_NOT_ENOUGH_MEMORY. The samples do not settle count or array mutation on failure.
  • Release: whole live grants only, each page exactly once, with several grants allowed in one call. Anything else returns FALSE/ERROR_INVALID_PARAMETER and changes nothing. The samples do not settle whether partial release is permitted.
  • No memory is committed. The 2^18-page (16 GiB) capacity matches SlimEra and is not a console figure.

Results

Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, CMake/Ninja, commit 729bfa1 (rebased on origin/main 406e14a). CPU-only tests, so no adapter or debug layer is involved.

cmake --preset dev
cmake --build --preset dev --target xmem_shim xmem_shim_check era_heap_check memory_query_check era_physical_check pe_map cpu_exe_io cpu_xmem_physical_original cpu_xmem_physical_full_count cpu_xmem_physical_skip_release
ctest --preset dev --no-tests=error -R 'era_heap|era_physical|desktop_memory_query|^xmem_shim_(exports|allocation|default_allocation|physical_pages|query|working_set|working_set_aggregation)$|^cpu_native_xmem_physical_pair$'
  • dev: 19/19 passed.
  • release (same three commands with release): 19/19 passed.
  • tests/form_coverage_check.py, tests/docs_links_check.py, tests/bidi_controls_check.py and tests/xmem_attribute_scan_check.py pass.
  • No capability skips.
  • Not run: the GPU working-set cases, since they are unaffected, and cpu_native_shim_startup. triggered02 reports it passing in Release with their shim.

Open contract questions (for #870)

  • Flag width. Is the flag ULONG_PTR or DWORD on the console? ULONG_PTR is inferred from one sample's cast.
  • 2 MiB clusters. What is the value of XMEM_2MB_CLUSTERS, and in which units are the count and array then given? The flag is refused here.
  • Failure state. What happens to the count and array on failure? Can the console grant fewer pages than requested?
  • Page numbers and partial release. What do console page numbers encode? Is partial release allowed?
  • Limits and errors. What error codes does the console return, and what is the title's physical-page budget?

No game files, dumps or binaries. All inputs are synthetic.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6563593813 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/892). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-23T16:14:34Z. --- Refs #880 and #870. Desktop `xmem.dll` exports `XMemAllocatePhysicalPages` (ordinal 7) and `XMemFreePhysicalPages` (ordinal 8), backed by a new page-number registry, `src/era_physical.{h,cpp}`. There are no `pe_map` changes. Mapping, scatter mapping and transfer remain follow-ups. ## Source-built comparison and negative controls | Test | What it shows | |---|---| | `cpu_native_xmem_physical_pair` | `tests/cpu/xmem_physical_pair.asm` imports the pair by name from `xmem.dll` and starts through `exe_startup.asm`/`finish_exe`. The same EXE runs under the Windows loader and under `pe_map_run`. It returns `0x880` when all 10 steps pass, or `0xF00` plus the failing step. The steps cover a valid two-page grant (count unchanged, distinct nonzero numbers, canary intact), a refused short-count release, a valid release afterwards, a refused double free, a refused bad flag (count reset to 0, three canaries intact) and a refused null-array release. The negative controls `full_count` and `skip_release` give `0xf04` and `0xf06` on both loaders. A provider missing either export stops the mapping before entry with `unresolved import xmem.dll!<name>`. | | `era_physical_controls` | The registry is linked next to a test-only build with `ERA_PHYSICAL_LENIENT_RELEASE`, a SlimEra-style release that clears any listed page. Each of 7 controls (short count, long count, single page of a grant, double free, never-issued page, forged mix, duplicate entry) is refused by the policy with the granted total unchanged, and accepted by the lenient build. This shows each control detects wrong ownership or page-count handling. | | `xmem_shim_physical_pages` | Through the named imports: three valid grants, 14 refused releases and 10 refused allocations. It checks last-error values, count reset and canary-filled arrays. A valid release still succeeds after the refusals. Exhaustion at 2^18 - 1 pages is followed by cleanup and a full 2^18-page grant. | | `era_physical_lifetime` | Exact granted totals across grants, a mixed-order release, exhaustion and cleanup. | | `xmem_shim_exports` | Also resolves the two new names. | **Companion fixture and credit:** triggered02's draft #887 holds their independent fixture and negative controls (CTest `cpu_native_xmem_physical`, branch `xmem-physical-pages`, `7dcc36d`/`3d206a1`). This PR does not copy that code and does not reuse its CTest name; the test here is `cpu_native_xmem_physical_pair`. The EXE comparison here follows its design: Windows loader vs `pe_map`, a canary-filled page array and a missing-provider control. This version adds the `finish_exe` wrapper, so success is the nonzero code `0x880` rather than a bare return of 0, plus the two mutation variants. Native-start tests found that a bare entry return can end the process with a worker thread's code 0. Co-authored-by is in the commit. ## Evidence and proposed desktop policy The Microsoft Xbox-GDK-Samples (MIT, `b1e77b6`) establish the 64 KiB allocate/free pairing, the in/out count with a `PULONG_PTR` page array, and the `BOOL` success check. See [`AdvancedESRAM/PageAllocator.cpp` L193-232](https://github.com/microsoft/Xbox-GDK-Samples/blob/b1e77b6ac10f95b07296025fda171ca2ddc42dbe/Samples/Graphics/AdvancedESRAM/PageAllocator.cpp#L193-L232) and [`FSR3FrameInterpolation.cpp` L146-167](https://github.com/microsoft/Xbox-GDK-Samples/blob/b1e77b6ac10f95b07296025fda171ca2ddc42dbe/Samples/Graphics/AMDFidelityFX_FSR3FrameInterpolation/FSR3FrameInterpolation.cpp#L146-L167). The following is **proposed desktop policy**, labeled as such in the code comments and in `docs/experiments.md`: - Only `MEM_64K_PAGES` (`0x20400000`) is accepted. Any other bit, including the upper 32, returns `FALSE`. - Allocation is all or nothing. Page numbers are opaque, nonzero and unique. - **On failure:** `*count = 0`, the array is untouched, and the error is `ERROR_INVALID_PARAMETER` or `ERROR_NOT_ENOUGH_MEMORY`. The samples do not settle count or array mutation on failure. - **Release:** whole live grants only, each page exactly once, with several grants allowed in one call. Anything else returns `FALSE`/`ERROR_INVALID_PARAMETER` and changes nothing. The samples do not settle whether partial release is permitted. - No memory is committed. The 2^18-page (16 GiB) capacity matches SlimEra and is not a console figure. ## Results Windows 10 Pro 10.0.19045.6466, MSVC 19.44.35207.1 x64, CMake/Ninja, commit `729bfa1` (rebased on `origin/main` `406e14a`). CPU-only tests, so no adapter or debug layer is involved. ```powershell cmake --preset dev cmake --build --preset dev --target xmem_shim xmem_shim_check era_heap_check memory_query_check era_physical_check pe_map cpu_exe_io cpu_xmem_physical_original cpu_xmem_physical_full_count cpu_xmem_physical_skip_release ctest --preset dev --no-tests=error -R 'era_heap|era_physical|desktop_memory_query|^xmem_shim_(exports|allocation|default_allocation|physical_pages|query|working_set|working_set_aggregation)$|^cpu_native_xmem_physical_pair$' ``` - dev: 19/19 passed. - release (same three commands with `release`): 19/19 passed. - `tests/form_coverage_check.py`, `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `tests/xmem_attribute_scan_check.py` pass. - No capability skips. - Not run: the GPU working-set cases, since they are unaffected, and `cpu_native_shim_startup`. triggered02 reports it passing in Release with their shim. ## Open contract questions (for #870) - **Flag width.** Is the flag `ULONG_PTR` or `DWORD` on the console? `ULONG_PTR` is inferred from one sample's cast. - **2 MiB clusters.** What is the value of `XMEM_2MB_CLUSTERS`, and in which units are the count and array then given? The flag is refused here. - **Failure state.** What happens to the count and array on failure? Can the console grant fewer pages than requested? - **Page numbers and partial release.** What do console page numbers encode? Is partial release allowed? - **Limits and errors.** What error codes does the console return, and what is the title's physical-page budget? No game files, dumps or binaries. All inputs are synthetic. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-24 06:45:16 +00:00
R0Y00 commented 2026-11-24 06:49:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T16:26:57Z.


Review update at d0bf29a. The desktop contract now says the count and output buffer are separate, and page numbers can be reused after release. The same-EXE, missing-provider, grant-lifetime and existing XMem regression checks passed 9/9 in Debug and 9/9 in Release. The comparison validates the proposed 64 KiB bookkeeping policy. Actual backing, mapping and console parity remain separate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107150109 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/892#issuecomment-7107150109). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:26:57Z. --- Review update at `d0bf29a`. The desktop contract now says the count and output buffer are separate, and page numbers can be reused after release. The same-EXE, missing-provider, grant-lifetime and existing XMem regression checks passed 9/9 in Debug and 9/9 in Release. The comparison validates the proposed 64 KiB bookkeeping policy. Actual backing, mapping and console parity remain separate.
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
R0Y00/Xbox2Windows!885
No description provided.