From 5a9df7ca86474267384fc1a70976fc12e58a26ee Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Wed, 10 Jun 2026 11:13:28 +0200 Subject: [PATCH] Add helper to set full system processor affinity mask --- xtoskrnl/includes/ke/affinity.hh | 1 + xtoskrnl/ke/affinity.cc | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/xtoskrnl/includes/ke/affinity.hh b/xtoskrnl/includes/ke/affinity.hh index 22b3844..44af10a 100644 --- a/xtoskrnl/includes/ke/affinity.hh +++ b/xtoskrnl/includes/ke/affinity.hh @@ -37,6 +37,7 @@ namespace KE IN PKAFFINITY_MAP AffinityMap); STATIC XTAPI ULONG FindNextRightSetProcessor(IN ULONG ThreadSeed, IN PKAFFINITY_MAP AffinityMap); + STATIC XTFASTCALL VOID SetAllProcessorsAffinity(IN OUT PKAFFINITY_MAP AffinityMap); STATIC XTFASTCALL VOID SetProcessorAffinity(IN OUT PKAFFINITY_MAP AffinityMap, IN ULONG CpuNumber); }; diff --git a/xtoskrnl/ke/affinity.cc b/xtoskrnl/ke/affinity.cc index 956ccd5..2d13335 100644 --- a/xtoskrnl/ke/affinity.cc +++ b/xtoskrnl/ke/affinity.cc @@ -404,6 +404,49 @@ KE::Affinity::FindNextRightSetProcessor(IN ULONG ThreadSeed, return 0; } +/** + * Populates the affinity map with all available processors. + * + * @param AffinityMap + * Supplies a pointer to the affinity map to be fully populated. + * + * @return This routine does not return any value. + * + * @since XT 1.0 + */ +XTFASTCALL +VOID +KE::Affinity::SetAllProcessorsAffinity(IN OUT PKAFFINITY_MAP AffinityMap) +{ + ULONG Cpus, Index; + + /* Get the number of available CPUs */ + Cpus = KE::Processor::GetInstalledCpus(); + + /* Iterate through all allocated blocks in the map */ + for(Index = 0; Index < AffinityMap->Size; Index++) + { + /* Evaluate if the remaining logical processors fully saturate the current block */ + if(Cpus >= 64) + { + /* Set all 64 bits and decrement the remaining count */ + AffinityMap->Bitmap[Index] = ~((KAFFINITY)0); + Cpus -= 64; + } + else if(Cpus > 0) + { + /* Generate a partial bitmask for the tail end of the processors */ + AffinityMap->Bitmap[Index] = (((KAFFINITY)1 << Cpus) - 1); + Cpus = 0; + } + else + { + /* Ensure any remaining blocks are safely zeroed */ + AffinityMap->Bitmap[Index] = 0; + } + } +} + /** * Sets the affinity bit for a specified processor. This is a non-atomic operation. *