Fixed the coreId and threadId calculation in the APIC CPU enumeration code and removed some IndexOutOfRangeException problems.
authormoel.mich
Sun, 25 Apr 2010 12:35:16 +0000
changeset 95249d57f82cf8
parent 94 b094329e882d
child 96 364ca73ba463
Fixed the coreId and threadId calculation in the APIC CPU enumeration code and removed some IndexOutOfRangeException problems.
Hardware/CPU/CPUID.cs
     1.1 --- a/Hardware/CPU/CPUID.cs	Sun Apr 25 12:03:43 2010 +0000
     1.2 +++ b/Hardware/CPU/CPUID.cs	Sun Apr 25 12:35:16 2010 +0000
     1.3 @@ -128,8 +128,8 @@
     1.4            out cpuidData[i, 0], out cpuidData[i, 1],
     1.5            out cpuidData[i, 2], out cpuidData[i, 3], mask);
     1.6  
     1.7 -      cpuidExtData = new uint[MaxCpuidExt + 1, 4];
     1.8 -      for (uint i = 0; i < (MaxCpuidExt + 1); i++)
     1.9 +      cpuidExtData = new uint[maxCpuidExt + 1, 4];
    1.10 +      for (uint i = 0; i < (maxCpuidExt + 1); i++)
    1.11          WinRing0.CpuidTx(CPUID_EXT + i, 0, 
    1.12            out cpuidExtData[i, 0], out cpuidExtData[i, 1], 
    1.13            out cpuidExtData[i, 2], out cpuidExtData[i, 3], mask);
    1.14 @@ -155,7 +155,7 @@
    1.15        name = nameBuilder.ToString();
    1.16        if (name.Contains("@"))
    1.17          name = name.Remove(name.LastIndexOf('@'));
    1.18 -      name = name.Trim();
    1.19 +      name = name.Trim();      
    1.20  
    1.21        this.family = ((cpuidData[1, 0] & 0x0FF00000) >> 20) +
    1.22          ((cpuidData[1, 0] & 0x0F00) >> 8);
    1.23 @@ -168,13 +168,21 @@
    1.24        switch (vendor) {
    1.25          case Vendor.Intel:
    1.26            uint maxCoreAndThreadIdPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
    1.27 -          uint maxCoreIdPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    1.28 +          uint maxCoreIdPerPackage;
    1.29 +          if (maxCpuid >= 4)
    1.30 +            maxCoreIdPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
    1.31 +          else
    1.32 +            maxCoreIdPerPackage = 1;
    1.33            threadMaskWith = (uint)Math.Ceiling(Math.Log(
    1.34              maxCoreAndThreadIdPerPackage / maxCoreIdPerPackage, 2));
    1.35            coreMaskWith = (uint)Math.Ceiling(Math.Log(maxCoreIdPerPackage, 2));
    1.36            break;
    1.37          case Vendor.AMD:
    1.38 -          uint corePerPackage = (cpuidExtData[8, 2] & 0xFF) + 1;
    1.39 +          uint corePerPackage;
    1.40 +          if (maxCpuidExt >= 8)
    1.41 +            corePerPackage = (cpuidExtData[8, 2] & 0xFF) + 1;
    1.42 +          else
    1.43 +            corePerPackage = 1;
    1.44            threadMaskWith = 0;
    1.45            coreMaskWith = (uint)Math.Ceiling(Math.Log(corePerPackage, 2));
    1.46            break;
    1.47 @@ -185,8 +193,11 @@
    1.48        }
    1.49  
    1.50        processorId = (uint)(apicId >> (int)(coreMaskWith + threadMaskWith));
    1.51 -      coreId = (uint)((apicId >> (int)(threadMaskWith)) - processorId);
    1.52 -      threadId = apicId - processorId - coreId;
    1.53 +      coreId = (uint)((apicId >> (int)(threadMaskWith)) 
    1.54 +        - (processorId << (int)(coreMaskWith + threadMaskWith)));
    1.55 +      threadId = apicId
    1.56 +        - (processorId << (int)(coreMaskWith + threadMaskWith))
    1.57 +        - (coreId << (int)(threadMaskWith)); 
    1.58      }
    1.59  
    1.60      public string Name {