What is the replacement for undocumeneted Windows Kernel API - KeSetAffinityThread() in Windows 7 and later OS

917 views Asked by At

void KeSetAffinityThread(PKTHREAD pKThread, KAFFINITY cpuAffinityMask) routine is available in Windows Kernel and its documentation can be found in OS/2 Kernel docs. And Windows still retains this API in its kernel. This API can be used to set affinity mask of any thread. I am looking for ways to do this when we have more than 64 processors in the system.

Little background - With the introduction of processor groups feature in Windows 7, we can no longer safely rely on KAFFINITY, thus I cannot use the above API as the code will be unreliable when run on systems with more than 64 processors. I was not able to find appropriate replacement in MSDN, the API's provided by MSDN include KeSetSystemAffinityThread, KeSetSystemGroupAffinityThread but it doesn't serve my purpose as these calls are only used to set affinity of the calling thread.

I also see a function - SetThreadAffinityMask() to set affinity of an arbitrary thread but this is an user level routine. I am looking for something similar in kernel mode.

2

There are 2 answers

1
Evgenii Gostiukhin On
0
ge0rdi On

User mode SetThreadAffinityMask API calls NtSetInformationThread.
You can do the same in kernel mode:

ULONGLONG affinity = <affinity mask>;
ZwSetInformationThread(hThread, ThreadAffinityMask, &affinity, sizeof(affinity));