upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/process.cpp
diff options
context:
space:
mode:
authortedin7 <tommasoderosa94@gmail.com>2026-11-23 16:57:53 +0100
committerubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com>2026-11-23 19:43:03 -0500
commitf35d19cb861d00154dbeadd16035af7a79095d4c (patch)
tree37f6077694c61c3ff8cf5ca38de8113afbeaaea8 /runtime/shims/process.cpp
parente7f17716f1b3a02018cd4f0e03591d601eb9c068 (diff)
fix: SetThreadGroupAffinity answers a GROUP_AFFINITY of absent cores or groups with the thread's own affinity, served under kernel32, kernelbase and every processtopology API set (retail exe imports it; reported by dadsmallbuk1 on #6942)
(cherry picked from commit fc4de7d2272a43dcdb0cc8eed83d6821c36f117e) (cherry picked from commit bbee83abfc1afe5b44ef9bc56c52ea517f6afa86)
Diffstat (limited to 'runtime/shims/process.cpp')
-rw-r--r--runtime/shims/process.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/runtime/shims/process.cpp b/runtime/shims/process.cpp
index b66c2e590..407cea8ad 100644
--- a/runtime/shims/process.cpp
+++ b/runtime/shims/process.cpp
@@ -172,6 +172,66 @@ void __cdecl game_cexit() {
172} 172}
173void __cdecl game_c_exit() {} 173void __cdecl game_c_exit() {}
174void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); } 174void __stdcall game_exit_process(UINT code) { end_run(static_cast<int>(code)); }
175
176// ---- threads and processors
177// The console has cores a PC may lack, and a title pins its threads to them with masks the host rejects: Windows
178// answers a mask with no CPU of the process (or one outside it) with 0 and ERROR_INVALID_PARAMETER, and a title that
179// checks the previous mask then aborts. The call succeeds on the console, so it does here: the thread stays where it
180// is and the answer is the mask the thread had (the process mask for a thread never pinned). A bad handle and an empty
181// mask still fail, as on Windows. The fact (answer instead of failing when the host call returns 0) is from sammwyy/xvalkyrie src/runtime/modules/kernel.cpp (commit
182// 53af7df77c10, GPL-3.0), reimplemented here in our own words and checked against the Win32 behaviour.
183DWORD_PTR __stdcall game_set_thread_affinity_mask(HANDLE thread, DWORD_PTR mask) {
184 DWORD_PTR previous = SetThreadAffinityMask(thread, mask);
185 if (previous != 0) return previous;
186 // An empty mask names no CPU on any machine: it fails as on Windows (the host's own answer, ERROR_INVALID_PARAMETER).
187 if (mask == 0) {
188 SetLastError(ERROR_INVALID_PARAMETER);
189 return 0;
190 }
191 DWORD_PTR process = 0, system = 0;
192 if (GetLastError() != ERROR_INVALID_PARAMETER || !GetProcessAffinityMask(GetCurrentProcess(), &process, &system))
193 return 0;
194 // The caller expects the mask the thread had, which may be narrower than the process mask (it may restore it
195 // later). The host reports it only on a successful call, so widen the thread to the process mask, read the old
196 // mask from that call, and put the old mask back: the thread ends where it started.
197 const DWORD_PTR actual = SetThreadAffinityMask(thread, process);
198 if (actual == 0) return 0; // the host's error stands (a bad handle)
199 if (actual != process) SetThreadAffinityMask(thread, actual);
200 // The first boot shows which masks the title asks for: the first few are named, the rest only counted.
201 static std::atomic<int> substituted{0};
202 const int n = ++substituted;
203 if (n <= 8)
204 std::fprintf(stderr, "[rt] process: SetThreadAffinityMask(0x%llx) names cores the host lacks; the thread stays, "
205 "answered its mask 0x%llx (call %d)\n",
206 static_cast<unsigned long long>(mask), static_cast<unsigned long long>(actual), n);
207 // The log may have failed (a closed stderr pipe leaves ERROR_NO_DATA); the call itself succeeded.
208 SetLastError(ERROR_SUCCESS);
209 return actual;
210}
211// The retail title imports the group form (processtopology API sets), not SetThreadAffinityMask. The same rule holds: a
212// GROUP_AFFINITY naming cores or a processor group the PC lacks is answered, not failed. The thread stays where it is,
213// the call returns TRUE and the out parameter gets the affinity the thread had. An empty mask names no CPU on any
214// machine and a bad handle is the host's own error: both are passed to the host and fail as on Windows.
215BOOL __stdcall game_set_thread_group_affinity(HANDLE thread, const GROUP_AFFINITY* requested, PGROUP_AFFINITY previous) {
216 if (SetThreadGroupAffinity(thread, requested, previous)) return TRUE;
217 if (!requested || requested->Mask == 0 || GetLastError() != ERROR_INVALID_PARAMETER) return FALSE;
218 GROUP_AFFINITY actual = {};
219 if (!GetThreadGroupAffinity(thread, &actual)) return FALSE; // the host's error stands
220 if (previous) *previous = actual;
221 static std::atomic<int> substituted{0};
222 const int n = ++substituted;
223 if (n <= 8)
224 std::fprintf(stderr, "[rt] process: SetThreadGroupAffinity(group %u, 0x%llx) names cores or a group the host lacks; "
225 "the thread stays, answered group %u mask 0x%llx (call %d)\n",
226 static_cast<unsigned>(requested->Group), static_cast<unsigned long long>(requested->Mask),
227 static_cast<unsigned>(actual.Group), static_cast<unsigned long long>(actual.Mask), n);
228 // The log may have failed (a closed stderr pipe leaves ERROR_NO_DATA); the call itself succeeded.
229 SetLastError(ERROR_SUCCESS);
230 return TRUE;
231}
232// A console-only kernel32 export that pins a thread pool to cores. The host has no such call, and a pool with the
233// host's own affinity runs the same work, so it succeeds and changes nothing.
234BOOL __stdcall game_set_threadpool_affinity_mask(PTP_POOL, DWORD_PTR) { return TRUE; }
175void __cdecl game_abort() { 235void __cdecl game_abort() {
176 std::fprintf(stderr, "[rt] process: the game called abort()\n"); 236 std::fprintf(stderr, "[rt] process: the game called abort()\n");
177 // The game's signal() import is not shimmed: its handler goes into the host CRT's process-wide table, which 237 // The game's signal() import is not shimmed: its handler goes into the host CRT's process-wide table, which
@@ -436,6 +496,11 @@ using namespace rt::process;
436 {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \ 496 {dll, "_get_narrow_winmain_command_line", reinterpret_cast<void*>(&game_get_narrow_winmain_command_line)}, \
437 {dll, "_get_wide_winmain_command_line", reinterpret_cast<void*>(&game_get_wide_winmain_command_line)} 497 {dll, "_get_wide_winmain_command_line", reinterpret_cast<void*>(&game_get_wide_winmain_command_line)}
438#define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast<void*>(&game_exit_process)} 498#define EXIT_PROCESS(dll) {dll, "ExitProcess", reinterpret_cast<void*>(&game_exit_process)}
499#define THREAD_AFFINITY(dll) {dll, "SetThreadAffinityMask", reinterpret_cast<void*>(&game_set_thread_affinity_mask)}
500#define THREAD_GROUP_AFFINITY(dll) \
501 {dll, "SetThreadGroupAffinity", reinterpret_cast<void*>(&game_set_thread_group_affinity)}
502#define POOL_AFFINITY(dll) \
503 {dll, "SetThreadpoolAffinityMask", reinterpret_cast<void*>(&game_set_threadpool_affinity_mask)}
439#define COMMAND_LINE(dll) \ 504#define COMMAND_LINE(dll) \
440 {dll, "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, \ 505 {dll, "GetCommandLineA", reinterpret_cast<void*>(&game_get_command_line_a)}, \
441 {dll, "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)} 506 {dll, "GetCommandLineW", reinterpret_cast<void*>(&game_get_command_line_w)}
@@ -457,6 +522,19 @@ SHIM_TABLE(process,
457 EXIT_PROCESS("kernel32.dll"), 522 EXIT_PROCESS("kernel32.dll"),
458 EXIT_PROCESS("kernelbase.dll"), 523 EXIT_PROCESS("kernelbase.dll"),
459 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"), 524 EXIT_PROCESS("api-ms-win-core-processthreads-l1-1-0.dll"),
525 THREAD_AFFINITY("kernel32.dll"),
526 THREAD_AFFINITY("kernelbase.dll"),
527 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-0.dll"),
528 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-1.dll"),
529 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-2.dll"),
530 THREAD_AFFINITY("api-ms-win-core-processthreads-l1-1-3.dll"),
531 THREAD_GROUP_AFFINITY("kernel32.dll"),
532 THREAD_GROUP_AFFINITY("kernelbase.dll"),
533 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-0.dll"),
534 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-1.dll"),
535 THREAD_GROUP_AFFINITY("api-ms-win-core-processtopology-l1-1-2.dll"),
536 POOL_AFFINITY("kernel32.dll"),
537 POOL_AFFINITY("kernelbase.dll"),
460 COMMAND_LINE("kernel32.dll"), 538 COMMAND_LINE("kernel32.dll"),
461 COMMAND_LINE("kernelbase.dll"), 539 COMMAND_LINE("kernelbase.dll"),
462 COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"), 540 COMMAND_LINE("api-ms-win-core-processenvironment-l1-1-0.dll"),