Hardware/WinRing0.cs
author moel.mich
Sat, 20 Feb 2010 19:51:10 +0000
changeset 56 5cb7eb5bf628
parent 44 c150de283ca0
child 79 9cdbe1d8d12a
permissions -rw-r--r--
Improved Winbond temperature reading. Temperatures create by adding PECI Agent values (delta to TCC Activation Temperature) to a (possibly uncalibrated) TBase are not read. Direct reading temperatures from sensor report register if available. Added lower bound for temperatures on Winbond chips. Nvidia GPUs are now displayed even if they do not have any sensors.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.IO;
moel@1
    41
using System.Runtime.InteropServices;
moel@1
    42
using System.Text;
moel@1
    43
moel@1
    44
namespace OpenHardwareMonitor.Hardware {
moel@1
    45
moel@1
    46
  public class WinRing0 {
moel@1
    47
    
moel@1
    48
    public enum OlsDllStatus{
moel@1
    49
      OLS_DLL_NO_ERROR                        = 0,
moel@1
    50
      OLS_DLL_UNSUPPORTED_PLATFORM            = 1,
moel@1
    51
      OLS_DLL_DRIVER_NOT_LOADED               = 2,
moel@1
    52
      OLS_DLL_DRIVER_NOT_FOUND                = 3,
moel@1
    53
      OLS_DLL_DRIVER_UNLOADED                 = 4,
moel@1
    54
      OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK    = 5,
moel@1
    55
      OLS_DLL_UNKNOWN_ERROR                   = 9
moel@1
    56
    }
moel@1
    57
moel@1
    58
    private static bool available = false;
moel@1
    59
moel@1
    60
    private static string GetDllName() {   
moel@1
    61
      int p = (int)System.Environment.OSVersion.Platform;
moel@1
    62
      if ((p == 4) || (p == 128)) {
moel@1
    63
        if (IntPtr.Size == 4) {
moel@1
    64
          return "libring0.so";
moel@1
    65
        } else {
moel@1
    66
          return "libring0x64.so";
moel@1
    67
        }
moel@1
    68
      } else {
moel@1
    69
        if (IntPtr.Size == 4) {
moel@1
    70
          return "WinRing0.dll";
moel@1
    71
        } else {
moel@1
    72
          return "WinRing0x64.dll";
moel@1
    73
        }
moel@1
    74
      }                       
moel@1
    75
    }
moel@1
    76
    
moel@1
    77
    private delegate bool InitializeOlsDelegate();
moel@1
    78
    private delegate void DeinitializeOlsDelegate();
moel@1
    79
    
moel@1
    80
    public delegate uint GetDllStatusDelegate();
moel@1
    81
    public delegate bool IsCpuidDelegate();
moel@22
    82
    public delegate bool CpuidDelegate(uint index, 
moel@22
    83
      out uint eax, out uint ebx, out uint ecx, out uint edx);
moel@22
    84
    public delegate bool CpuidExDelegate(uint index, uint ecxValue, 
moel@22
    85
      out uint eax, out uint ebx, out uint ecx, out uint edx);
moel@46
    86
    public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
moel@46
    87
    public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
moel@46
    88
      UIntPtr threadAffinityMask);
moel@1
    89
    public delegate byte ReadIoPortByteDelegate(ushort port);
moel@1
    90
    public delegate void WriteIoPortByteDelegate(ushort port, byte value);
moel@1
    91
    public delegate void SetPciMaxBusIndexDelegate(byte max);
moel@1
    92
    public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
moel@1
    93
      ushort deviceId, byte index);
moel@1
    94
    public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
moel@1
    95
      uint regAddress, out uint value);
moel@14
    96
    public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
moel@14
    97
      uint regAddress, uint value);
moel@46
    98
    public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
moel@46
    99
      UIntPtr threadAffinityMask);
moel@1
   100
moel@1
   101
    private static InitializeOlsDelegate InitializeOls;
moel@1
   102
    private static DeinitializeOlsDelegate DeinitializeOls;
moel@1
   103
moel@1
   104
    public static GetDllStatusDelegate GetDllStatus;
moel@1
   105
    public static IsCpuidDelegate IsCpuid;
moel@1
   106
    public static CpuidDelegate Cpuid;
moel@22
   107
    public static CpuidExDelegate CpuidEx;
moel@46
   108
    public static RdmsrDelegate Rdmsr;
moel@46
   109
    public static RdmsrTxDelegate RdmsrTx;
moel@1
   110
    public static ReadIoPortByteDelegate ReadIoPortByte;
moel@1
   111
    public static WriteIoPortByteDelegate WriteIoPortByte;
moel@1
   112
    public static SetPciMaxBusIndexDelegate SetPciMaxBusIndex;
moel@1
   113
    public static FindPciDeviceByIdDelegate FindPciDeviceById;
moel@1
   114
    public static ReadPciConfigDwordExDelegate ReadPciConfigDwordEx;
moel@44
   115
    public static WritePciConfigDwordExDelegate WritePciConfigDwordEx;
moel@46
   116
    public static RdtscTxDelegate RdtscTx;
moel@1
   117
moel@1
   118
    private static void GetDelegate<T>(string entryPoint, out T newDelegate) 
moel@1
   119
      where T : class 
moel@1
   120
    {
moel@1
   121
      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
moel@1
   122
      attribute.CallingConvention = CallingConvention.Winapi;
moel@1
   123
      attribute.PreserveSig = true;
moel@1
   124
      attribute.EntryPoint = entryPoint;
moel@1
   125
      attribute.CharSet = CharSet.Auto;
moel@1
   126
      PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
moel@1
   127
    }
moel@1
   128
moel@1
   129
    static WinRing0() {
moel@1
   130
      GetDelegate("InitializeOls", out InitializeOls);
moel@1
   131
      GetDelegate("DeinitializeOls", out DeinitializeOls);
moel@1
   132
      GetDelegate("GetDllStatus", out GetDllStatus);
moel@1
   133
      GetDelegate("IsCpuid", out IsCpuid);
moel@1
   134
      GetDelegate("Cpuid", out Cpuid);
moel@22
   135
      GetDelegate("CpuidEx", out CpuidEx);
moel@46
   136
      GetDelegate("Rdmsr", out  Rdmsr);
moel@46
   137
      GetDelegate("RdmsrTx", out  RdmsrTx);
moel@1
   138
      GetDelegate("ReadIoPortByte", out ReadIoPortByte);
moel@1
   139
      GetDelegate("WriteIoPortByte", out WriteIoPortByte);
moel@1
   140
      GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex);
moel@1
   141
      GetDelegate("FindPciDeviceById", out FindPciDeviceById);
moel@1
   142
      GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx);
moel@14
   143
      GetDelegate("WritePciConfigDwordEx", out WritePciConfigDwordEx);
moel@46
   144
      GetDelegate("RdtscTx", out RdtscTx);
moel@1
   145
moel@1
   146
      try {
moel@1
   147
        if (InitializeOls != null && InitializeOls())
moel@1
   148
          available = true;
moel@1
   149
      } catch (DllNotFoundException) { }       
moel@1
   150
    }
moel@1
   151
    
moel@1
   152
    public static bool IsAvailable {
moel@1
   153
      get { return available; }
moel@1
   154
    }
moel@1
   155
moel@1
   156
    private static Deinitializer deinitializer = new Deinitializer();
moel@1
   157
    private class Deinitializer {
moel@1
   158
      ~Deinitializer() {
moel@1
   159
        if (available)
moel@1
   160
          DeinitializeOls();
moel@1
   161
      }
moel@1
   162
    }
moel@1
   163
  }
moel@1
   164
}