Hardware/CPU/CPUID.cs
author moel.mich
Mon, 02 Jan 2012 20:45:18 +0000
changeset 329 756af5ee409e
parent 238 bddc6e01840a
child 344 3145aadca3d2
permissions -rw-r--r--
Small corrections for AMD family 15h CPUs (based on documentation).
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Text;
    40 
    41 namespace OpenHardwareMonitor.Hardware.CPU {
    42 
    43   internal enum Vendor {
    44     Unknown,
    45     Intel,
    46     AMD,
    47   }
    48 
    49   internal class CPUID {
    50 
    51     private readonly int thread;
    52 
    53     private readonly Vendor vendor = Vendor.Unknown;
    54 
    55     private readonly string cpuBrandString = "";
    56     private readonly string name = "";
    57 
    58     private readonly uint[,] cpuidData = new uint[0, 0];
    59     private readonly uint[,] cpuidExtData = new uint[0, 0];
    60 
    61     private readonly uint family;
    62     private readonly uint model;
    63     private readonly uint stepping;
    64 
    65     private readonly uint apicId;
    66 
    67     private readonly uint threadMaskWith;
    68     private readonly uint coreMaskWith;
    69 
    70     private readonly uint processorId;
    71     private readonly uint coreId;
    72     private readonly uint threadId;
    73 
    74     public const uint CPUID_0 = 0;
    75     public const uint CPUID_EXT = 0x80000000;
    76 
    77     private static void AppendRegister(StringBuilder b, uint value) {
    78       b.Append((char)((value) & 0xff));
    79       b.Append((char)((value >> 8) & 0xff));
    80       b.Append((char)((value >> 16) & 0xff));
    81       b.Append((char)((value >> 24) & 0xff));
    82     }
    83 
    84     private static uint NextLog2(long x) {
    85       if (x <= 0)
    86         return 0;
    87 
    88       x--;
    89       uint count = 0;
    90       while (x > 0) {
    91         x >>= 1;
    92         count++;
    93       }
    94 
    95       return count;
    96     }
    97 
    98     public CPUID(int thread) {
    99       this.thread = thread;
   100 
   101       uint maxCpuid = 0;
   102       uint maxCpuidExt = 0;
   103 
   104       uint eax, ebx, ecx, edx;
   105 
   106       if (thread >= 32)
   107         throw new ArgumentOutOfRangeException("thread");
   108       ulong mask = 1UL << thread;
   109 
   110       if (Opcode.CpuidTx(CPUID_0, 0,
   111           out eax, out ebx, out ecx, out edx, mask)) {
   112         if (eax > 0)
   113           maxCpuid = eax;
   114         else
   115           return;
   116 
   117         StringBuilder vendorBuilder = new StringBuilder();
   118         AppendRegister(vendorBuilder, ebx);
   119         AppendRegister(vendorBuilder, edx);
   120         AppendRegister(vendorBuilder, ecx);
   121         string cpuVendor = vendorBuilder.ToString();
   122         switch (cpuVendor) {
   123           case "GenuineIntel":
   124             vendor = Vendor.Intel;
   125             break;
   126           case "AuthenticAMD":
   127             vendor = Vendor.AMD;
   128             break;
   129           default:
   130             vendor = Vendor.Unknown;
   131             break;
   132         }
   133         eax = ebx = ecx = edx = 0;
   134         if (Opcode.CpuidTx(CPUID_EXT, 0,
   135           out eax, out ebx, out ecx, out edx, mask)) {
   136           if (eax > CPUID_EXT)
   137             maxCpuidExt = eax - CPUID_EXT;
   138           else
   139             return;
   140         } else {
   141           throw new ArgumentOutOfRangeException("thread");
   142         }
   143       } else {
   144         throw new ArgumentOutOfRangeException("thread");
   145       }
   146 
   147       maxCpuid = Math.Min(maxCpuid, 1024);
   148       maxCpuidExt = Math.Min(maxCpuidExt, 1024);   
   149 
   150       cpuidData = new uint[maxCpuid + 1, 4];
   151       for (uint i = 0; i < (maxCpuid + 1); i++)
   152         Opcode.CpuidTx(CPUID_0 + i, 0, 
   153           out cpuidData[i, 0], out cpuidData[i, 1],
   154           out cpuidData[i, 2], out cpuidData[i, 3], mask);
   155 
   156       cpuidExtData = new uint[maxCpuidExt + 1, 4];
   157       for (uint i = 0; i < (maxCpuidExt + 1); i++)
   158         Opcode.CpuidTx(CPUID_EXT + i, 0, 
   159           out cpuidExtData[i, 0], out cpuidExtData[i, 1], 
   160           out cpuidExtData[i, 2], out cpuidExtData[i, 3], mask);
   161 
   162       StringBuilder nameBuilder = new StringBuilder();
   163       for (uint i = 2; i <= 4; i++) {
   164         if (Opcode.CpuidTx(CPUID_EXT + i, 0, 
   165           out eax, out ebx, out ecx, out edx, mask)) 
   166         {
   167           AppendRegister(nameBuilder, eax);
   168           AppendRegister(nameBuilder, ebx);
   169           AppendRegister(nameBuilder, ecx);
   170           AppendRegister(nameBuilder, edx);
   171         }
   172       }
   173       nameBuilder.Replace('\0', ' ');
   174       cpuBrandString = nameBuilder.ToString().Trim();
   175       nameBuilder.Replace("(R)", " ");
   176       nameBuilder.Replace("(TM)", " ");
   177       nameBuilder.Replace("(tm)", "");
   178       nameBuilder.Replace("CPU", "");
   179       nameBuilder.Replace("Quad-Core Processor", "");
   180       nameBuilder.Replace("Six-Core Processor", "");
   181       nameBuilder.Replace("Eight-Core Processor", "");
   182       for (int i = 0; i < 10; i++) nameBuilder.Replace("  ", " ");
   183       name = nameBuilder.ToString();
   184       if (name.Contains("@"))
   185         name = name.Remove(name.LastIndexOf('@'));
   186       name = name.Trim();      
   187 
   188       this.family = ((cpuidData[1, 0] & 0x0FF00000) >> 20) +
   189         ((cpuidData[1, 0] & 0x0F00) >> 8);
   190       this.model = ((cpuidData[1, 0] & 0x0F0000) >> 12) +
   191         ((cpuidData[1, 0] & 0xF0) >> 4);
   192       this.stepping = (cpuidData[1, 0] & 0x0F);
   193 
   194       this.apicId = (cpuidData[1, 1] >> 24) & 0xFF;
   195 
   196       switch (vendor) {
   197         case Vendor.Intel:
   198           uint maxCoreAndThreadIdPerPackage = (cpuidData[1, 1] >> 16) & 0xFF;
   199           uint maxCoreIdPerPackage;
   200           if (maxCpuid >= 4)
   201             maxCoreIdPerPackage = ((cpuidData[4, 0] >> 26) & 0x3F) + 1;
   202           else
   203             maxCoreIdPerPackage = 1;
   204           threadMaskWith = 
   205             NextLog2(maxCoreAndThreadIdPerPackage / maxCoreIdPerPackage);
   206           coreMaskWith = NextLog2(maxCoreIdPerPackage);
   207           break;
   208         case Vendor.AMD:
   209           uint corePerPackage;
   210           if (maxCpuidExt >= 8)
   211             corePerPackage = (cpuidExtData[8, 2] & 0xFF) + 1;
   212           else
   213             corePerPackage = 1;
   214           threadMaskWith = 0;
   215           coreMaskWith = NextLog2(corePerPackage);
   216           break;
   217         default:
   218           threadMaskWith = 0;
   219           coreMaskWith = 0;
   220           break;
   221       }
   222 
   223       processorId = (apicId >> (int)(coreMaskWith + threadMaskWith));
   224       coreId = ((apicId >> (int)(threadMaskWith)) 
   225         - (processorId << (int)(coreMaskWith)));
   226       threadId = apicId
   227         - (processorId << (int)(coreMaskWith + threadMaskWith))
   228         - (coreId << (int)(threadMaskWith)); 
   229     }
   230 
   231     public string Name {
   232       get { return name; }
   233     }
   234 
   235     public string BrandString {
   236       get { return cpuBrandString; }
   237     }
   238 
   239     public int Thread {
   240       get { return thread; }
   241     }
   242 
   243     public Vendor Vendor {
   244       get { return vendor; }
   245     }
   246 
   247     public uint Family {
   248       get { return family; }
   249     }
   250 
   251     public uint Model {
   252       get { return model; }
   253     }
   254 
   255     public uint Stepping {
   256       get { return stepping; }
   257     }
   258 
   259     public uint ApicId {
   260       get { return apicId; }
   261     }
   262 
   263     public uint ProcessorId {
   264       get { return processorId; }
   265     }
   266 
   267     public uint CoreId {
   268       get { return coreId; }
   269     }
   270 
   271     public uint ThreadId {
   272       get { return threadId; }
   273     }
   274 
   275     public uint[,] Data {
   276       get { return cpuidData; }
   277     }
   278 
   279     public uint[,] ExtData {
   280       get { return cpuidExtData; }
   281     }
   282   }
   283 }