runtime: define the physical-page mapping contracts for startup #895

Merged
Holastor merged 4 commits from upstream-pr/902 into main 2026-11-23 17:29:54 +00:00
Holastor commented 2026-11-23 16:33:04 +00:00 (Migrated from github.com)

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


Refs #870 and follow-up to #880.

Overview

Following physical-page allocation (#880, #892) and virtual memory reservation, titles map physical pages into the reserved virtual address space via xmem.dll!XMemMapPhysicalPages and xmem.dll!XMemMapPhysicalPagesScatter.

This PR establishes the contract through an independent, source-built caller and controlled recording-provider fixture without changing the production XMem shim:

  • cpu_map_pages_caller.exe (tests/cpu/map_pages_caller.asm): standalone MASM x64 executable importing both functions from xmem.dll by name and executing 7 contract validation steps:
    1. Contiguous mapping to a 64 KiB aligned virtual address, returning the base address.
    2. Contiguous unmapping (NULL page array), returning the base address.
    3. Scatter mapping across an array of 64 KiB aligned virtual addresses, returning TRUE (1).
    4. Scatter unmapping (NULL page array), returning TRUE (1).
    5. Refusal on an unaligned virtual address (returns NULL).
    6. Refusal on a null virtual address (returns NULL).
    7. Refusal on a null scatter address array (returns FALSE).
      Exits with milestone status 0x870 on success.
  • cpu_map_pages_provider (tests/cpu/map_pages_provider.cpp / .def): recording provider validating the 64 KiB alignment requirement (address % 65536 == 0), nonzero counts, unmapping via null page array, and recording call and unmap counts for both contiguous and scatter paths.
  • pe_map comparison and negative controls (tests/cpu/map_pages_check.py):
    • Native Windows loader and pe_map (in-process loader) produce identical exit code 0x870.
    • When xmem.dll is missing, pe_map halts at map time before entry with STATUS["unresolved"] and diagnostic unresolved import xmem.dll!XMemMapPhysicalPages.

Verification

Environment: Windows 11 x64, MSVC 19.51, CMake/Ninja, Python 3.11 (out/pe-env).

Dev preset

cmake --build --preset dev --target cpu_map_pages_caller cpu_map_pages_provider pe_map
ctest --preset dev --no-tests=error -R '^cpu_native_map_pages$' -V

Output:

caller imports: {'xmem.dll': ['XMemMapPhysicalPages', 'XMemMapPhysicalPagesScatter']}
contiguous and scatter mapping: native and mapped exit 0x870, calls recorded
negative control: map time refusal before entry: unresolved import xmem.dll!XMemMapPhysicalPagesScatter
1/1 Test #275: cpu_native_map_pages .............   Passed    0.15 sec

Release preset

cmake --build --preset release --target cpu_map_pages_caller cpu_map_pages_provider pe_map
ctest --preset release --no-tests=error -R '^cpu_native_map_pages$' -V

Output:

caller imports: {'xmem.dll': ['XMemMapPhysicalPages', 'XMemMapPhysicalPagesScatter']}
contiguous and scatter mapping: native and mapped exit 0x870, calls recorded
negative control: map time refusal before entry: unresolved import xmem.dll!XMemMapPhysicalPagesScatter
1/1 Test #275: cpu_native_map_pages .............   Passed    0.15 sec

Documentation & pure-Python checks

python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
python -B tests/form_coverage_check.py
git diff --check

All passed:

  • docs_links_check.py: Checked 13 Markdown files and 180 links.
  • bidi_controls_check.py: Checked 723 tracked source, script and build/CI files: no bidirectional controls.
  • form_coverage_check.py: form coverage checks passed.
  • git diff --check: 0 issues.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact test command and result for dev and release presets.
  • I considered a negative control or failure case for the new fixture.
  • I updated docs/experiments.md with the new capability and reproduction commands.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6563944516 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-23T16:33:04Z. --- Refs #870 and follow-up to #880. ## Overview Following physical-page allocation (#880, #892) and virtual memory reservation, titles map physical pages into the reserved virtual address space via `xmem.dll!XMemMapPhysicalPages` and `xmem.dll!XMemMapPhysicalPagesScatter`. This PR establishes the contract through an independent, source-built caller and controlled recording-provider fixture without changing the production XMem shim: - **`cpu_map_pages_caller.exe`** (`tests/cpu/map_pages_caller.asm`): standalone MASM x64 executable importing both functions from `xmem.dll` by name and executing 7 contract validation steps: 1. Contiguous mapping to a 64 KiB aligned virtual address, returning the base address. 2. Contiguous unmapping (`NULL` page array), returning the base address. 3. Scatter mapping across an array of 64 KiB aligned virtual addresses, returning `TRUE` (`1`). 4. Scatter unmapping (`NULL` page array), returning `TRUE` (`1`). 5. Refusal on an unaligned virtual address (returns `NULL`). 6. Refusal on a null virtual address (returns `NULL`). 7. Refusal on a null scatter address array (returns `FALSE`). Exits with milestone status `0x870` on success. - **`cpu_map_pages_provider`** (`tests/cpu/map_pages_provider.cpp` / `.def`): recording provider validating the 64 KiB alignment requirement (`address % 65536 == 0`), nonzero counts, unmapping via null page array, and recording call and unmap counts for both contiguous and scatter paths. - **`pe_map` comparison and negative controls** (`tests/cpu/map_pages_check.py`): - Native Windows loader and `pe_map` (in-process loader) produce identical exit code `0x870`. - When `xmem.dll` is missing, `pe_map` halts at map time before entry with `STATUS["unresolved"]` and diagnostic `unresolved import xmem.dll!XMemMapPhysicalPages`. ## Verification Environment: Windows 11 x64, MSVC 19.51, CMake/Ninja, Python 3.11 (`out/pe-env`). ### Dev preset ```powershell cmake --build --preset dev --target cpu_map_pages_caller cpu_map_pages_provider pe_map ctest --preset dev --no-tests=error -R '^cpu_native_map_pages$' -V ``` Output: ``` caller imports: {'xmem.dll': ['XMemMapPhysicalPages', 'XMemMapPhysicalPagesScatter']} contiguous and scatter mapping: native and mapped exit 0x870, calls recorded negative control: map time refusal before entry: unresolved import xmem.dll!XMemMapPhysicalPagesScatter 1/1 Test #275: cpu_native_map_pages ............. Passed 0.15 sec ``` ### Release preset ```powershell cmake --build --preset release --target cpu_map_pages_caller cpu_map_pages_provider pe_map ctest --preset release --no-tests=error -R '^cpu_native_map_pages$' -V ``` Output: ``` caller imports: {'xmem.dll': ['XMemMapPhysicalPages', 'XMemMapPhysicalPagesScatter']} contiguous and scatter mapping: native and mapped exit 0x870, calls recorded negative control: map time refusal before entry: unresolved import xmem.dll!XMemMapPhysicalPagesScatter 1/1 Test #275: cpu_native_map_pages ............. Passed 0.15 sec ``` ### Documentation & pure-Python checks ```powershell python -B tests/docs_links_check.py python -B tests/bidi_controls_check.py python -B tests/form_coverage_check.py git diff --check ``` All passed: - `docs_links_check.py`: Checked 13 Markdown files and 180 links. - `bidi_controls_check.py`: Checked 723 tracked source, script and build/CI files: no bidirectional controls. - `form_coverage_check.py`: form coverage checks passed. - `git diff --check`: 0 issues. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact test command and result for dev and release presets. - [x] I considered a negative control or failure case for the new fixture. - [x] I updated `docs/experiments.md` with the new capability and reproduction commands. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-24 06:45:20 +00:00
R0Y00 commented 2026-11-24 06:49:20 +00:00 (Migrated from github.com)

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


The source-built caller is a useful way to record proposed call shapes. Before this fixture can land, please scope the README/docs to the controlled provider's choices. The two loaders consuming the same stub cannot establish the console return types or that NULL unmaps, alignment is required, or the reservation survives. Please cite the independent ABI basis where known, and label the rest as hypotheses. Reuse exe_startup.asm and cpu_exe_io!finish_exe with the live-worker control and mapped return. The current four counters are read only from the mapped process. Please record or check the actual call arguments and outcome in both runs, and withhold each named mapping export independently. Keep #892's physical registry and the shared-backing/readback follow-up separate from this recorder.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107746726 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902#issuecomment-7107746726). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T16:50:15Z. --- The source-built caller is a useful way to record proposed call shapes. Before this fixture can land, please scope the README/docs to the controlled provider's choices. The two loaders consuming the same stub cannot establish the console return types or that NULL unmaps, alignment is required, or the reservation survives. Please cite the independent ABI basis where known, and label the rest as hypotheses. Reuse exe_startup.asm and cpu_exe_io!finish_exe with the live-worker control and mapped return. The current four counters are read only from the mapped process. Please record or check the actual call arguments and outcome in both runs, and withhold each named mapping export independently. Keep #892's physical registry and the shared-backing/readback follow-up separate from this recorder.
Holastor commented 2026-11-24 06:49:21 +00:00 (Migrated from github.com)

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


Updated in 24e58eb:

  1. Startup wrapper and lifetime control:

    • cpu_map_pages_caller now starts through tests/cpu/exe_startup.asm and calls finish_exe from cpu_exe_io.dll.
    • On the native path, CPU_CRC_TEST_LIVE_WORKER=1 is exercised to ensure clean process exit under finish_exe even with a live worker thread.
    • Under pe_map, finish_exe preserves the host return without exiting the host process.
  2. Per-process verification of call counters:

    • map_pages_caller.asm now queries the provider's call counters (get_map_pages_call_count, get_map_pages_unmap_count, get_scatter_map_call_count, get_scatter_unmap_count) directly within step 8 of native_entry before returning 0x870. Both the native child process and pe_map host-mapped run verify the counters internally.
  3. Independent negative controls:

    • map_pages_check.py now independently withholds each named mapping export:
      • Withholding XMemMapPhysicalPages while providing XMemMapPhysicalPagesScatter halts at map time with unresolved import xmem.dll!XMemMapPhysicalPages.
      • Withholding XMemMapPhysicalPagesScatter while providing XMemMapPhysicalPages halts at map time with unresolved import xmem.dll!XMemMapPhysicalPagesScatter.
  4. Documentation scoping & ABI basis:

    • docs/experiments.md now explicitly cites the public declarations (SlimEra era.h, WinDurango kernelx.h) and clarifies that return types, 64 KiB alignment requirement, and NULL unmapping remain controlled provider policies and open hypotheses until physical registry tracking and readback are proven.
    • Physical page registry tracking and production XMem shim remain untouched.

Passes cpu_native_map_pages under both dev and release presets, plus all pure-Python checks (docs_links_check.py, bidi_controls_check.py, form_coverage_check.py, and git diff --check).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7107915404 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902#issuecomment-7107915404). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-23T16:56:46Z. --- Updated in `24e58eb`: 1. **Startup wrapper and lifetime control**: - `cpu_map_pages_caller` now starts through `tests/cpu/exe_startup.asm` and calls `finish_exe` from `cpu_exe_io.dll`. - On the native path, `CPU_CRC_TEST_LIVE_WORKER=1` is exercised to ensure clean process exit under `finish_exe` even with a live worker thread. - Under `pe_map`, `finish_exe` preserves the host return without exiting the host process. 2. **Per-process verification of call counters**: - `map_pages_caller.asm` now queries the provider's call counters (`get_map_pages_call_count`, `get_map_pages_unmap_count`, `get_scatter_map_call_count`, `get_scatter_unmap_count`) directly within step 8 of `native_entry` before returning `0x870`. Both the native child process and `pe_map` host-mapped run verify the counters internally. 3. **Independent negative controls**: - `map_pages_check.py` now independently withholds each named mapping export: - Withholding `XMemMapPhysicalPages` while providing `XMemMapPhysicalPagesScatter` halts at map time with `unresolved import xmem.dll!XMemMapPhysicalPages`. - Withholding `XMemMapPhysicalPagesScatter` while providing `XMemMapPhysicalPages` halts at map time with `unresolved import xmem.dll!XMemMapPhysicalPagesScatter`. 4. **Documentation scoping & ABI basis**: - `docs/experiments.md` now explicitly cites the public declarations (SlimEra `era.h`, WinDurango `kernelx.h`) and clarifies that return types, 64 KiB alignment requirement, and NULL unmapping remain controlled provider policies and open hypotheses until physical registry tracking and readback are proven. - Physical page registry tracking and production XMem shim remain untouched. Passes `cpu_native_map_pages` under both `dev` and `release` presets, plus all pure-Python checks (`docs_links_check.py`, `bidi_controls_check.py`, `form_coverage_check.py`, and `git diff --check`).
R0Y00 commented 2026-11-24 06:49:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:02:28Z.


24e58eb addresses the startup wrapper, in-process counters and the two missing-export cases. One acceptance gap remains before resolving the docs conflict: the recorder currently checks the shape and tally, but a wrong page number or swapped scatter destinations can still satisfy the caller. Please make the trace or assertions distinguish the expected count, page IDs and ordered addresses, with a changed-argument control. Keep those exact values labeled as fixture inputs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108058770 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902#issuecomment-7108058770). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:02:28Z. --- 24e58eb addresses the startup wrapper, in-process counters and the two missing-export cases. One acceptance gap remains before resolving the docs conflict: the recorder currently checks the shape and tally, but a wrong page number or swapped scatter destinations can still satisfy the caller. Please make the trace or assertions distinguish the expected count, page IDs and ordered addresses, with a changed-argument control. Keep those exact values labeled as fixture inputs.
Holastor commented 2026-11-24 06:49:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-23T17:11:48Z.


Updated in 379b0d6:

  1. Distinguish expected count, page IDs, and ordered addresses:

    • The recording provider now validates and captures the full call trace (recorded_map_address, recorded_map_count, recorded_map_pages, recorded_scatter_addresses, recorded_scatter_count, recorded_scatter_pages).
    • map_pages_check.py asserts the recorded trace against the expected fixture inputs:
      • Contiguous address: 0x10000000, page count: 2, page IDs: [1, 2].
      • Scatter ordered addresses: [0x10000000, 0x10010000], page count: 2, page IDs: [1, 2].
  2. Changed-argument controls:

    • The provider now rejects mismatched parameters and records them in argument_mismatches:
      • Contiguous mapping rejects unexpected page numbers ([999, 888]), returning NULL.
      • Scatter mapping rejects swapped/out-of-order addresses ([0x10010000, 0x10000000]), returning 0.
    • map_pages_caller.asm exercises both changed-argument controls (steps 8 and 9) and verifies both rejections.
    • Step 10 verifies within the caller process that exactly two argument mismatches occurred, along with the expected successful call counts.
  3. Fixture input labeling:

    • docs/experiments.md, map_pages_check.py, and map_pages_provider.cpp explicitly label 0x10000000, cPages = 2, [1, 2], and [0x10000000, 0x10010000] as synthetic fixture inputs chosen for this test.

Independently passed cpu_native_map_pages under dev and release presets, plus all pure-Python checks (docs_links_check.py, bidi_controls_check.py, form_coverage_check.py, and git diff --check).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108294899 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902#issuecomment-7108294899). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-23T17:11:48Z. --- Updated in `379b0d6`: 1. **Distinguish expected count, page IDs, and ordered addresses**: - The recording provider now validates and captures the full call trace (`recorded_map_address`, `recorded_map_count`, `recorded_map_pages`, `recorded_scatter_addresses`, `recorded_scatter_count`, `recorded_scatter_pages`). - `map_pages_check.py` asserts the recorded trace against the expected fixture inputs: - Contiguous address: `0x10000000`, page count: `2`, page IDs: `[1, 2]`. - Scatter ordered addresses: `[0x10000000, 0x10010000]`, page count: `2`, page IDs: `[1, 2]`. 2. **Changed-argument controls**: - The provider now rejects mismatched parameters and records them in `argument_mismatches`: - Contiguous mapping rejects unexpected page numbers (`[999, 888]`), returning `NULL`. - Scatter mapping rejects swapped/out-of-order addresses (`[0x10010000, 0x10000000]`), returning `0`. - `map_pages_caller.asm` exercises both changed-argument controls (steps 8 and 9) and verifies both rejections. - Step 10 verifies within the caller process that exactly two argument mismatches occurred, along with the expected successful call counts. 3. **Fixture input labeling**: - `docs/experiments.md`, `map_pages_check.py`, and `map_pages_provider.cpp` explicitly label `0x10000000`, `cPages = 2`, `[1, 2]`, and `[0x10000000, 0x10010000]` as synthetic fixture inputs chosen for this test. Independently passed `cpu_native_map_pages` under `dev` and `release` presets, plus all pure-Python checks (`docs_links_check.py`, `bidi_controls_check.py`, `form_coverage_check.py`, and `git diff --check`).
R0Y00 commented 2026-11-24 06:49:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T17:14:54Z.


Thanks for adding the exact arguments and the two changed-argument cases. I resolved the CMake and documentation conflicts with the landed XMem fixtures in f9ecbb5. The contiguous/scatter recorder and tools-memory fixture passed 2/2 in Debug and Release. Both loaders returned the fixture's expected result, the recorder checked the ordered addresses and page values, and withholding either import stopped before entry with its exact name. Fresh CI is running. This PR covers the recorder; shared backing, aliasing, and map/unmap policy remain follow-up work.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7108372337 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/902#issuecomment-7108372337). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T17:14:54Z. --- Thanks for adding the exact arguments and the two changed-argument cases. I resolved the CMake and documentation conflicts with the landed XMem fixtures in f9ecbb5. The contiguous/scatter recorder and tools-memory fixture passed 2/2 in Debug and Release. Both loaders returned the fixture's expected result, the recorder checked the ordered addresses and page values, and withholding either import stopped before entry with its exact name. Fresh CI is running. This PR covers the recorder; shared backing, aliasing, and map/unmap policy remain follow-up work.
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!895
No description provided.