From 046af755d0baf974fb05cb7c017d59413de6a333 Mon Sep 17 00:00:00 2001 From: Pawel Dziepak Date: Mon, 30 Dec 2013 04:03:28 +0100 Subject: [PATCH] x86: Fix stack corruption in cache topology detection --- src/system/kernel/arch/x86/arch_cpu.cpp | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/system/kernel/arch/x86/arch_cpu.cpp b/src/system/kernel/arch/x86/arch_cpu.cpp index f4cc5ffba3..3de6d53835 100644 --- a/src/system/kernel/arch/x86/arch_cpu.cpp +++ b/src/system/kernel/arch/x86/arch_cpu.cpp @@ -15,6 +15,8 @@ #include #include +#include + #include #include @@ -578,7 +580,7 @@ detect_amd_cache_topology(uint32 maxExtendedLeaf) return; uint8 hierarchyLevels[CPU_MAX_CACHE_LEVEL]; - uint8 maxCacheLevel = 0; + int maxCacheLevel = 0; int currentLevel = 0; int cacheType; @@ -587,17 +589,17 @@ detect_amd_cache_topology(uint32 maxExtendedLeaf) get_current_cpuid(&cpuid, 0x8000001d, currentLevel); cacheType = cpuid.regs.eax & 0x1f; - int cacheLevel = (cpuid.regs.eax >> 5) & 0x7; + if (cacheType == 0) + break; + int cacheLevel = (cpuid.regs.eax >> 5) & 0x7; int coresCount = next_power_of_2(((cpuid.regs.eax >> 14) & 0x3f) + 1); hierarchyLevels[cacheLevel - 1] = coresCount * (sHierarchyMask[CPU_TOPOLOGY_SMT] + 1); - - if (cacheType != 0) - maxCacheLevel = max_c(maxCacheLevel, cacheLevel); + maxCacheLevel = std::max(maxCacheLevel, cacheLevel); currentLevel++; - } while (cacheType != 0); + } while (true); for (int i = 0; i < maxCacheLevel; i++) sCacheSharingMask[i] = ~uint32(hierarchyLevels[i] - 1); @@ -692,7 +694,7 @@ detect_intel_cache_topology(uint32 maxBasicLeaf) return; uint8 hierarchyLevels[CPU_MAX_CACHE_LEVEL]; - uint8 maxCacheLevel = 0; + int maxCacheLevel = 0; int currentLevel = 0; int cacheType; @@ -701,16 +703,16 @@ detect_intel_cache_topology(uint32 maxBasicLeaf) get_current_cpuid(&cpuid, 4, currentLevel); cacheType = cpuid.regs.eax & 0x1f; - int cacheLevel = (cpuid.regs.eax >> 5) & 0x7; + if (cacheType == 0) + break; + int cacheLevel = (cpuid.regs.eax >> 5) & 0x7; hierarchyLevels[cacheLevel - 1] = next_power_of_2(((cpuid.regs.eax >> 14) & 0x3f) + 1); - - if (cacheType != 0) - maxCacheLevel = max_c(maxCacheLevel, cacheLevel); + maxCacheLevel = std::max(maxCacheLevel, cacheLevel); currentLevel++; - } while (cacheType != 0); + } while (true); for (int i = 0; i < maxCacheLevel; i++) sCacheSharingMask[i] = ~uint32(hierarchyLevels[i] - 1);