Skip to content

Commit

Permalink
fix: HI-32 Freeze in IntelInformationProvider.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
L3tum committed Jan 15, 2022
1 parent a4a1437 commit 4a47b45
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions HardwareInformation/Providers/IntelInformationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ private void GatherCacheTopology(ref MachineInformation information)

foreach (var core in information.Cpu.Cores)
{
threads.Add(Util.RunAffinity(1uL << (int) core.Number, () =>
threads.Add(Util.RunAffinity(1uL << (int)core.Number, () =>
{
var ecx = 0u;

while (true)
{
Opcode.Cpuid(out var result, 4, ecx);

var type = (Cache.CacheType) (result.eax & 0xF);
var type = (Cache.CacheType)(result.eax & 0xF);

// Null, no more caches
if (type == Cache.CacheType.NONE)
Expand All @@ -93,7 +93,7 @@ private void GatherCacheTopology(ref MachineInformation information)
var cache = new Cache
{
CoresPerCache = ((result.eax & 0x3FFC000) >> 14) + 1u,
Level = (Cache.CacheLevel) ((result.eax & 0xF0) >> 5),
Level = (Cache.CacheLevel)((result.eax & 0xF0) >> 5),
Type = type,
LineSize = (result.ebx & 0xFFF) + 1u,
WBINVD = (result.edx & 0b1) == 0,
Expand Down Expand Up @@ -135,11 +135,23 @@ private void GatherNumberOfPhysicalCores(ref MachineInformation information)
{
var ecx = 0u;
var apicIds = new Dictionary<uint, uint>();
var currentIteration = 0;
var maxIterations = int.MaxValue;

while (true)
while (currentIteration < maxIterations)
{
currentIteration++;
Opcode.Cpuid(out var result, 0x1f, ecx);

// The CPU doesn't actually support Intel Extended Topology V2
// despite previously saying so.
// Fallback to Legacy Topology (Intel Extended Topology (0BH) is not implemented in this library)
if (Util.ExtractBits(result.ebx, 0, 15) == 0)
{
GatherNumberOfPhysicalCoresLegacy(ref information);
return;
}

var type = Util.ExtractBits(result.ecx, 8, 15);

if (type == 0)
Expand All @@ -153,7 +165,7 @@ private void GatherNumberOfPhysicalCores(ref MachineInformation information)
}

var shift = Util.ExtractBits(result.eax, 0, 4);
var coreApicId = result.edx >> (int) shift;
var coreApicId = result.edx >> (int)shift;

if (apicIds.ContainsKey(coreApicId))
{
Expand All @@ -165,7 +177,7 @@ private void GatherNumberOfPhysicalCores(ref MachineInformation information)
}
}

information.Cpu.PhysicalCores = (uint) apicIds.Count;
information.Cpu.PhysicalCores = (uint)apicIds.Count;
}

private void GatherNumberOfPhysicalCoresLegacy(ref MachineInformation information)
Expand Down Expand Up @@ -196,7 +208,7 @@ private void GatherCpuName(ref MachineInformation information)
Opcode.Cpuid(out var partTwo, 0x80000003, 0);
Opcode.Cpuid(out var partThree, 0x80000004, 0);

var results = new[] {partOne, partTwo, partThree};
var results = new[] { partOne, partTwo, partThree };
var sb = new StringBuilder();

foreach (var res in results)
Expand All @@ -218,25 +230,25 @@ private void GatherFeatureFlags(ref MachineInformation information)
Opcode.Cpuid(out var result, 0x80000001, 0);

information.Cpu.IntelFeatureFlags.ExtendedFeatureFlagsF81One =
(IntelFeatureFlags.ExtendedFeatureFlagsF81ECX) result.ecx;
(IntelFeatureFlags.ExtendedFeatureFlagsF81ECX)result.ecx;
information.Cpu.IntelFeatureFlags.ExtendedFeatureFlagsF81Two =
(IntelFeatureFlags.ExtendedFeatureFlagsF81EDX) result.edx;
(IntelFeatureFlags.ExtendedFeatureFlagsF81EDX)result.edx;
}

if (information.Cpu.MaxCpuIdFeatureLevel >= 6)
{
Opcode.Cpuid(out var result, 6, 0);

information.Cpu.IntelFeatureFlags.TPMFeatureFlags =
(IntelFeatureFlags.TPMFeatureFlagsEAX) result.eax;
(IntelFeatureFlags.TPMFeatureFlagsEAX)result.eax;
}

if (information.Cpu.MaxCpuIdExtendedFeatureLevel >= 7)
{
Opcode.Cpuid(out var result, 0x80000001, 0);

information.Cpu.IntelFeatureFlags.FeatureFlagsApm =
(IntelFeatureFlags.FeatureFlagsAPM) result.edx;
(IntelFeatureFlags.FeatureFlagsAPM)result.edx;
}
}
}
Expand Down

0 comments on commit 4a47b45

Please sign in to comment.