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

Merged
yous0serious merged 1 commit from upstream-pr/221 into main 2026-11-23 21:24:02 +00:00
yous0serious commented 2026-11-23 21:15:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by yous0serious on 2026-11-23T21:15:23Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:6568983621 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/221). Originally posted by [yous0serious](https://github.com/yous0serious) on 2026-11-23T21:15:23Z. --- 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.
upstream-sync closed this pull request 2026-11-24 06:57:43 +00:00
ivall commented 2026-11-24 07:04:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T21:24:26Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7114418441 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/221#issuecomment-7114418441). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T21:24:26Z. --- 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).
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
xcl-dev/xcl!218
No description provided.