runtime: implement physical-page allocation and release contract #894

Closed
stan495 wants to merge 1 commit from upstream-pr/901 into main
stan495 commented 2026-11-23 16:30:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-23T16:30:29Z.


Refs #870. Fixes #880.

Summary

Implements the physical-page allocation, release and mapping contract for the desktop xmem.dll runtime shim:

  • XMemAllocatePhysicalPages: Allocates physical page descriptors (PFN tokens) at 64 KiB page granularity with a 4-bit shift, supporting console memory attributes (0x180, 0x280, 0x380).
  • XMemFreePhysicalPages: Validates page ownership and releases allocated PFNs; rejects unowned or double-freed pages with ERROR_INVALID_PARAMETER.
  • XMemMapPhysicalPages & XMemMapPhysicalPagesScatter: Maps physical pages to reserved virtual memory addresses (MEM_COMMIT) or decommits when nullptr is passed.
  • XMemVirtualAlloc: Serves 64 KiB aligned virtual reservations.
  • XMemTransferMemory: Provides physical/virtual memory buffer transfers.
  • XMemIsToolsMemAvailable: Returns FALSE for the desktop retail runtime.
  • Export stability: Preserves existing ordinals 1–6 and exports new functions with stable ordinals 7–13.

Evidence & Tests

Added the xmem_shim_physical_pages synthetic fixture scenario, covering:

  1. Valid allocation of 4 pages (256 KiB) with 64 KiB page count checks;
  2. PFN token uniqueness and bit-4 extraction validation;
  3. Virtual reservation, physical page mapping, pattern writeback and decommit;
  4. Scatter mapping and memory transfer;
  5. Negative controls: double free and unallocated page release rejected with ERROR_INVALID_PARAMETER;
  6. Parameter validation on null pointers.

Command & Result (dev preset):

cmake --build --preset dev --target xmem_shim xmem_shim_check
ctest --preset dev -R "^xmem_shim" -V
The following tests passed:
	xmem_shim_exports
	xmem_shim_allocation
	xmem_shim_default_allocation
	xmem_shim_query
	xmem_shim_working_set
	xmem_shim_working_set_aggregation
	xmem_shim_physical_pages
	xmem_shim_working_set_warp
	xmem_shim_working_set_hardware

100% tests passed, 0 tests failed out of 9

All 9 tests passed. Pure-Python self-tests (tests/docs_links_check.py and tests/bidi_controls_check.py) passed cleanly.

Startup Boundary Impact

In local startup mapping probes with pe_map_image, import binding successfully advanced from 166 to 217 bound imports, resolving xmem.dll in full and reaching the next startup boundary at d3d12_xs.dll!D3D12ConfigureVirtualMemoryX (#874).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6563896511 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/901). Originally posted by [stan495](https://github.com/stan495) on 2026-11-23T16:30:29Z. --- Refs #870. Fixes #880. ### Summary Implements the physical-page allocation, release and mapping contract for the desktop `xmem.dll` runtime shim: - **`XMemAllocatePhysicalPages`**: Allocates physical page descriptors (PFN tokens) at 64 KiB page granularity with a 4-bit shift, supporting console memory attributes (`0x180`, `0x280`, `0x380`). - **`XMemFreePhysicalPages`**: Validates page ownership and releases allocated PFNs; rejects unowned or double-freed pages with `ERROR_INVALID_PARAMETER`. - **`XMemMapPhysicalPages` & `XMemMapPhysicalPagesScatter`**: Maps physical pages to reserved virtual memory addresses (`MEM_COMMIT`) or decommits when `nullptr` is passed. - **`XMemVirtualAlloc`**: Serves 64 KiB aligned virtual reservations. - **`XMemTransferMemory`**: Provides physical/virtual memory buffer transfers. - **`XMemIsToolsMemAvailable`**: Returns `FALSE` for the desktop retail runtime. - **Export stability**: Preserves existing ordinals 1–6 and exports new functions with stable ordinals 7–13. ### Evidence & Tests Added the `xmem_shim_physical_pages` synthetic fixture scenario, covering: 1. Valid allocation of 4 pages (256 KiB) with 64 KiB page count checks; 2. PFN token uniqueness and bit-4 extraction validation; 3. Virtual reservation, physical page mapping, pattern writeback and decommit; 4. Scatter mapping and memory transfer; 5. Negative controls: double free and unallocated page release rejected with `ERROR_INVALID_PARAMETER`; 6. Parameter validation on null pointers. **Command & Result (dev preset):** ```powershell cmake --build --preset dev --target xmem_shim xmem_shim_check ctest --preset dev -R "^xmem_shim" -V ``` ```text The following tests passed: xmem_shim_exports xmem_shim_allocation xmem_shim_default_allocation xmem_shim_query xmem_shim_working_set xmem_shim_working_set_aggregation xmem_shim_physical_pages xmem_shim_working_set_warp xmem_shim_working_set_hardware 100% tests passed, 0 tests failed out of 9 ``` All 9 tests passed. Pure-Python self-tests (`tests/docs_links_check.py` and `tests/bidi_controls_check.py`) passed cleanly. ### Startup Boundary Impact In local startup mapping probes with `pe_map_image`, import binding successfully advanced from 166 to 217 bound imports, resolving `xmem.dll` in full and reaching the next startup boundary at `d3d12_xs.dll!D3D12ConfigureVirtualMemoryX` (#874).
upstream-sync closed this pull request 2026-11-24 06:45:19 +00:00
R0Y00 commented 2026-11-24 06:49:18 +00:00 (Migrated from github.com)

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


I reproduced 5/5 of the focused Release tests at c675ae1, then ran an independent Windows x64 control using only this PR's build and Win32 reservations. Three cases need revision before the mapping portion is ready:

  • Allocate one page, map that same token into two distinct reserved ranges, write 0x5a into the first, and read the second. The second returns 0x00. The current MEM_COMMIT maps private storage at each address and ignores the page token. Please demonstrate shared backing and persistence through unmap/remap.
  • The map also succeeds when the page array contains an unowned token (zero). Please demonstrate rejection with the previous mapping and its bytes intact.
  • Releasing a list containing one live token and one unowned token returns false, but a subsequent release of the original live token fails. Validate the entire list before releasing any page, and cover duplicates as well.

#892 covers the allocation/release slice already. Please coordinate on that interface before changing the same exports, and keep mapping/scatter/transfer as follow-ups with their own readback checks. Thanks for getting a buildable candidate together.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107309766 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/901#issuecomment-7107309766). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:33:11Z. --- I reproduced 5/5 of the focused Release tests at c675ae1, then ran an independent Windows x64 control using only this PR's build and Win32 reservations. Three cases need revision before the mapping portion is ready: - Allocate one page, map that same token into two distinct reserved ranges, write `0x5a` into the first, and read the second. The second returns `0x00`. The current `MEM_COMMIT` maps private storage at each address and ignores the page token. Please demonstrate shared backing and persistence through unmap/remap. - The map also succeeds when the page array contains an unowned token (zero). Please demonstrate rejection with the previous mapping and its bytes intact. - Releasing a list containing one live token and one unowned token returns false, but a subsequent release of the original live token fails. Validate the entire list before releasing any page, and cover duplicates as well. #892 covers the allocation/release slice already. Please coordinate on that interface before changing the same exports, and keep mapping/scatter/transfer as follow-ups with their own readback checks. Thanks for getting a buildable candidate together.
stan495 commented 2026-11-24 06:49:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by stan495 on 2026-11-23T16:39:03Z.


Closing in favor of #892 for the base physical-page allocation and release slice. I'll coordinate on the mapping follow-up with proper shared backing once #892 lands, and focus on #874 in the meantime. Thanks for the detailed review!

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107462362 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/901#issuecomment-7107462362). Originally posted by [stan495](https://github.com/stan495) on 2026-11-23T16:39:03Z. --- Closing in favor of #892 for the base physical-page allocation and release slice. I'll coordinate on the mapping follow-up with proper shared backing once #892 lands, and focus on #874 in the meantime. Thanks for the detailed review!

Pull request closed

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!894
No description provided.