Thread affinity: console masks with processors the PC lacks are fitted to the process's (#106) #221

Manually merged
github merged 1 commit from gh-pr/221/head into gh-pr/221/base 2026-11-26 04:16:10 +00:00
Owner

@⁠yous0serious on GitHub · 2026-11-23 21:15 UTC · original
yous0serious:hybrid-cpu-affinity → main · +96 −0 · 2 files · 1 commits · merged 2026-11-23 21:24 UTC

Addresses the "Hybrid CPU affinity masks & core counts" item of #106 – the part @⁠ivall's triage there marked as wanted: "masks for > available cores are worth clamping". The design decision stays as it is: the title sees the PC's processors (HANDOFF, "MicrosoftGame.Config"), so nothing is remapped for masks a title builds from them.

Relates to #106 (only this item; the other items of that issue are untouched, so it should stay open).

Problem

SetThreadAffinityMask / SetThreadGroupAffinity from kernelx / kernel32legacy / the api-ms-win-core-* sets went straight to Windows. A console title can pin threads with masks for the console's 7 cores / 14 threads (e.g. 0x40, 0x3000, 0x7F). Windows answers ERROR_INVALID_PARAMETER (87) and does not change the affinity when such a mask

  • names none of the PC's processors (e.g. 1 << 12 on a 4c / 8t CPU), or
  • names any processor outside the process's affinity mask (a process limited with start /affinity, Process Lasso, a job object, …).

A title that checks the result (most console code does: the console never fails this) asserts or takes an error path.

Measured with SetThreadAffinityMask directly on an i7-13700KF (24 logical processors, Raptor Lake hybrid):

process affinity mask Windows
0xFFFFFF (all) 0x7F ok
0xFFFFFF 0x1000000FF (one bit past the last processor) ok – bits past the processors are ignored
0xFFFFFF 0xFF000000 (only processors that do not exist) fails, 87
0xF (start /affinity F) 0x7F, 0x3F, 0x40 all fail, 87

Change (hle/kernelx.rs)

  • New HLE exports SetThreadAffinityMask and SetThreadGroupAffinity (group 0).
  • fit_affinity_mask(mask, allowed) with allowed = the process's affinity mask:
    • mask ⊆ allowed (every mask made from the PC's processors), 0, or allowed == 0 → unchanged;
    • some bits allowed → cut to those (0x7F on a 4-processor process → 0xF);
    • none allowed → each console processor goes to the PC's processor at its index modulo their count (1 << 6 on 4 processors → 1 << 2), so threads pinned to different console cores stay spread instead of piling onto one.
  • Logged once per distinct mask (SetThreadAffinityMask: mask 0x10 names processors outside the process's 0xf – 0x1).
  • More than one processor group (> 64 logical processors) → masks pass unchanged (as before).
  • The real function is still called, so the return value (the previous mask) and GetLastError are Windows'.

About the E-core part of #106

#106 also suggested console masks get pinned onto E-cores on hybrid Intel CPUs. On this i7-13700KF that is not the case: Windows numbers the P-core threads first (0–15), the E-cores last (16–23), so the console's masks (bits 0–13) land on P-cores. Nothing is changed for that here.

Tests

  • hle::kernelx::tests::affinity_masks_outside_the_process_are_fitted – the mask arithmetic (unchanged / cut / dealt round-robin, non-contiguous process masks).
  • hle::kernelx::tests::set_thread_affinity_mask_takes_console_masks – on a fresh thread, the hooked SetThreadAffinityMask with a processor the process lacks succeeds and the thread's affinity is the fitted mask; SetThreadGroupAffinity with a partly-outside mask succeeds. Only XCL's answer is asserted (Wine's own may differ, cf. #213).
  • resolver::tests::console_user32_and_legacy_dlls_reach_the_pc_functions – kernel32legacy!SetThreadAffinityMask (Fortnite's import) now resolves to kernelx; GetProcessAffinityMask still to Windows.

Run on Windows 11, i7-13700KF, RTX 4090 (driver 32.0.16.2101), Rust 1.100 MSVC:

  • cargo test --workspace --release --no-fail-fast -- --test-threads=4: 624 passed, 0 failed (2 SKIPPED … no wave64, expected on NVIDIA), after rebasing on 0cd0a53.
  • The two new kernelx tests also inside a process limited to 4 processors (start /affinity F <test exe> affinity): pass, with the log lines above.
  • Not run with a retail game (I have no game files); no title is known to fail on this yet – it removes a failure mode on small / affinity-limited PCs.

Overlaps

None in content. Other open PRs touch hle/kernelx.rs in different hunks: #187, #194, #197, #203, #213 (#213 adds tests near the end of mod tests; this PR adds its tests at the start). Rebased on current main (0cd0a53).

AI-assisted (GitHub Copilot), reviewed and tested locally.

**@⁠yous0serious** on GitHub · 2026-11-23 21:15 UTC · [original](https://github.com/xcl-dev/xcl/pull/221) `yous0serious:hybrid-cpu-affinity` → `main` · +96 −0 · 2 files · 1 commits · merged 2026-11-23 21:24 UTC Addresses the **"Hybrid CPU affinity masks & core counts"** item of #106 – the part @⁠ivall's triage there marked as wanted: *"masks for > available cores are worth clamping"*. The design decision stays as it is: **the title sees the PC's processors** (HANDOFF, "MicrosoftGame.Config"), so nothing is remapped for masks a title builds from them. Relates to #106 (only this item; the other items of that issue are untouched, so it should stay open). ## Problem `SetThreadAffinityMask` / `SetThreadGroupAffinity` from `kernelx` / `kernel32legacy` / the `api-ms-win-core-*` sets went straight to Windows. A console title can pin threads with masks for the console's 7 cores / 14 threads (e.g. `0x40`, `0x3000`, `0x7F`). Windows answers `ERROR_INVALID_PARAMETER` (87) and **does not change the affinity** when such a mask - names **none** of the PC's processors (e.g. `1 << 12` on a 4c / 8t CPU), or - names **any** processor outside the process's affinity mask (a process limited with `start /affinity`, Process Lasso, a job object, …). A title that checks the result (most console code does: the console never fails this) asserts or takes an error path. Measured with `SetThreadAffinityMask` directly on an **i7-13700KF** (24 logical processors, Raptor Lake hybrid): | process affinity | mask | Windows | |---|---|---| | `0xFFFFFF` (all) | `0x7F` | ok | | `0xFFFFFF` | `0x1000000FF` (one bit past the last processor) | ok – bits past the processors are ignored | | `0xFFFFFF` | `0xFF000000` (only processors that do not exist) | **fails, 87** | | `0xF` (`start /affinity F`) | `0x7F`, `0x3F`, `0x40` | **all fail, 87** | ## Change (`hle/kernelx.rs`) - New HLE exports `SetThreadAffinityMask` and `SetThreadGroupAffinity` (group 0). - `fit_affinity_mask(mask, allowed)` with `allowed` = the process's affinity mask: - mask ⊆ allowed (every mask made from the PC's processors), `0`, or `allowed == 0` → **unchanged**; - some bits allowed → cut to those (`0x7F` on a 4-processor process → `0xF`); - none allowed → each console processor goes to the PC's processor at its index modulo their count (`1 << 6` on 4 processors → `1 << 2`), so threads pinned to different console cores stay spread instead of piling onto one. - Logged once per distinct mask (`SetThreadAffinityMask: mask 0x10 names processors outside the process's 0xf – 0x1`). - More than one processor group (> 64 logical processors) → masks pass unchanged (as before). - The real function is still called, so the return value (the previous mask) and `GetLastError` are Windows'. ## About the E-core part of #106 #106 also suggested console masks get pinned onto E-cores on hybrid Intel CPUs. On this i7-13700KF that is not the case: Windows numbers the P-core threads first (0–15), the E-cores last (16–23), so the console's masks (bits 0–13) land on P-cores. Nothing is changed for that here. ## Tests - `hle::kernelx::tests::affinity_masks_outside_the_process_are_fitted` – the mask arithmetic (unchanged / cut / dealt round-robin, non-contiguous process masks). - `hle::kernelx::tests::set_thread_affinity_mask_takes_console_masks` – on a fresh thread, the hooked `SetThreadAffinityMask` with a processor the process lacks succeeds and the thread's affinity is the fitted mask; `SetThreadGroupAffinity` with a partly-outside mask succeeds. Only XCL's answer is asserted (Wine's own may differ, cf. #213). - `resolver::tests::console_user32_and_legacy_dlls_reach_the_pc_functions` – `kernel32legacy!SetThreadAffinityMask` (Fortnite's import) now resolves to kernelx; `GetProcessAffinityMask` still to Windows. Run on Windows 11, i7-13700KF, RTX 4090 (driver 32.0.16.2101), Rust 1.100 MSVC: - `cargo test --workspace --release --no-fail-fast -- --test-threads=4`: **624 passed, 0 failed** (2 `SKIPPED … no wave64`, expected on NVIDIA), after rebasing on `0cd0a53`. - The two new kernelx tests also inside a process limited to 4 processors (`start /affinity F <test exe> affinity`): pass, with the log lines above. - Not run with a retail game (I have no game files); no title is known to fail on this yet – it removes a failure mode on small / affinity-limited PCs. ## Overlaps None in content. Other open PRs touch `hle/kernelx.rs` in different hunks: #187, #194, #197, #203, #213 (#213 adds tests near the end of `mod tests`; this PR adds its tests at the start). Rebased on current `main` (`0cd0a53`). AI-assisted (GitHub Copilot), reviewed and tested locally.
The title sees the PC's processors, so SetThreadAffinityMask / SetThreadGroupAffinity masks
made from them pass unchanged. A console mask (its 7 cores / 14 threads) naming processors
outside the process's affinity – a PC with fewer processors, or a process limited to some –
made Windows fail the call with ERROR_INVALID_PARAMETER: when none of its processors is there,
or any is outside the process's mask. Such a mask is now cut to the processors there are; when
none is left, each console processor goes to the PC's processor at its index modulo their
count. Logged once per mask. Systems with more than one processor group pass masks unchanged.

Measured on an i7-13700KF (24 logical processors, P-cores 0-15 first): 0x7F passes, 0xFF000000
fails (87); restricted to 0xF (start /affinity F) 0x7F, 0x3F and 0x40 all fail (87). The tests
check XCL's answer only (Wine's own may differ), also run under start /affinity F.

Co-authored-by: Copilot App <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:16:09 +00:00
github manually merged commit 7b029e22d7 into gh-pr/221/base 2026-11-26 04:16:10 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 21:24 UTC · original

Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 605 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed; and the retail game still reaches its first menu at 30 fps (720 draws per 2 s).

**@⁠ivall** on GitHub · 2026-11-23 21:24 UTC · [original](https://github.com/xcl-dev/xcl/pull/221#issuecomment-7114418441) Merged (main d4f17ca). Checked on an RX 9070 (RDNA4) with 11 PRs merged together on main af20b4b: `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 605 unit tests passed, 0 failed (20 ignored), the integration and e2e tests passed; and the retail game still reaches its first menu at 30 fps (720 draws per 2 s).
This discussion has been locked. Commenting is limited to contributors.
No description provided.