Hardware/WinRing0.cs
author paulwerelds
Tue, 21 Sep 2010 10:18:07 +0000
changeset 193 52ef1cf6b8e5
parent 167 b7cc9d09aefe
child 195 0ee888c485d5
permissions -rw-r--r--
Added thermal and voltage/frequency MSRs for K8 CPUs, fixed chars-per-line limit
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.Runtime.InteropServices;
moel@162
    40
using System.Threading;
moel@1
    41
moel@1
    42
namespace OpenHardwareMonitor.Hardware {
moel@1
    43
moel@165
    44
  internal class WinRing0 {
moel@167
    45
moel@167
    46
    private 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@167
    59
    private static Mutex isaBusMutex;
moel@1
    60
moel@1
    61
    private static string GetDllName() {   
moel@1
    62
      int p = (int)System.Environment.OSVersion.Platform;
moel@1
    63
      if ((p == 4) || (p == 128)) {
moel@1
    64
        if (IntPtr.Size == 4) {
moel@1
    65
          return "libring0.so";
moel@1
    66
        } else {
moel@1
    67
          return "libring0x64.so";
moel@1
    68
        }
moel@1
    69
      } else {
moel@1
    70
        if (IntPtr.Size == 4) {
moel@1
    71
          return "WinRing0.dll";
moel@1
    72
        } else {
moel@1
    73
          return "WinRing0x64.dll";
moel@1
    74
        }
moel@1
    75
      }                       
moel@1
    76
    }
moel@1
    77
    
moel@1
    78
    private delegate bool InitializeOlsDelegate();
moel@1
    79
    private delegate void DeinitializeOlsDelegate();
moel@1
    80
    
moel@1
    81
    public delegate bool IsCpuidDelegate();
moel@90
    82
    public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
moel@90
    83
      out uint eax, out uint ebx, out uint ecx, out uint edx,
moel@90
    84
      UIntPtr threadAffinityMask);
moel@46
    85
    public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
moel@46
    86
    public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
moel@46
    87
      UIntPtr threadAffinityMask);
moel@1
    88
    public delegate byte ReadIoPortByteDelegate(ushort port);
moel@1
    89
    public delegate void WriteIoPortByteDelegate(ushort port, byte value);
moel@1
    90
    public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
moel@1
    91
      ushort deviceId, byte index);
moel@1
    92
    public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
moel@1
    93
      uint regAddress, out uint value);
moel@14
    94
    public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
moel@14
    95
      uint regAddress, uint value);
moel@46
    96
    public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
moel@46
    97
      UIntPtr threadAffinityMask);
moel@79
    98
    public delegate bool RdtscDelegate(out uint eax, out uint edx);
moel@1
    99
moel@167
   100
    private static InitializeOlsDelegate InitializeOls = 
moel@167
   101
      CreateDelegate<InitializeOlsDelegate>("InitializeOls");
moel@167
   102
    private static DeinitializeOlsDelegate DeinitializeOls =
moel@167
   103
      CreateDelegate<DeinitializeOlsDelegate>("DeinitializeOls");
moel@1
   104
moel@167
   105
    public static readonly IsCpuidDelegate IsCpuid =
moel@167
   106
      CreateDelegate<IsCpuidDelegate>("IsCpuid");
moel@167
   107
    public static readonly CpuidTxDelegate CpuidTx =
moel@167
   108
      CreateDelegate<CpuidTxDelegate>("CpuidTx");
moel@167
   109
    public static readonly RdmsrDelegate Rdmsr =
moel@167
   110
      CreateDelegate<RdmsrDelegate>("Rdmsr");
moel@167
   111
    public static readonly RdmsrTxDelegate RdmsrTx =
moel@167
   112
      CreateDelegate<RdmsrTxDelegate>("RdmsrTx");
moel@167
   113
    public static readonly ReadIoPortByteDelegate ReadIoPortByte =
moel@167
   114
      CreateDelegate<ReadIoPortByteDelegate>("ReadIoPortByte");
moel@167
   115
    public static readonly WriteIoPortByteDelegate WriteIoPortByte =
moel@167
   116
      CreateDelegate<WriteIoPortByteDelegate>("WriteIoPortByte");
moel@167
   117
    public static readonly FindPciDeviceByIdDelegate FindPciDeviceById =
moel@167
   118
      CreateDelegate<FindPciDeviceByIdDelegate>("FindPciDeviceById");
moel@167
   119
    public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx =
moel@167
   120
      CreateDelegate<ReadPciConfigDwordExDelegate>("ReadPciConfigDwordEx");
moel@167
   121
    public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx =
moel@167
   122
      CreateDelegate<WritePciConfigDwordExDelegate>("WritePciConfigDwordEx");
moel@167
   123
    public static readonly RdtscTxDelegate RdtscTx =
moel@167
   124
      CreateDelegate<RdtscTxDelegate>("RdtscTx");
moel@167
   125
    public static readonly RdtscDelegate Rdtsc =
moel@167
   126
      CreateDelegate<RdtscDelegate>("Rdtsc");
moel@167
   127
 
moel@167
   128
    private static T CreateDelegate<T>(string entryPoint) where T : class {
moel@1
   129
      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
moel@1
   130
      attribute.CallingConvention = CallingConvention.Winapi;
moel@1
   131
      attribute.PreserveSig = true;
moel@1
   132
      attribute.EntryPoint = entryPoint;
moel@1
   133
      attribute.CharSet = CharSet.Auto;
moel@167
   134
      T result;
moel@167
   135
      PInvokeDelegateFactory.CreateDelegate(attribute, out result);
moel@167
   136
      return result;
moel@1
   137
    }
moel@1
   138
moel@167
   139
    public static void Open() {
moel@1
   140
      try {
moel@1
   141
        if (InitializeOls != null && InitializeOls())
moel@1
   142
          available = true;
moel@167
   143
      } catch (DllNotFoundException) { }   
moel@167
   144
      
moel@162
   145
      isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
moel@1
   146
    }
moel@167
   147
moel@1
   148
    public static bool IsAvailable {
moel@1
   149
      get { return available; }
moel@1
   150
    }
moel@1
   151
moel@167
   152
    public static void Close() {
moel@167
   153
      if (available)
moel@167
   154
        DeinitializeOls();        
moel@167
   155
      isaBusMutex.Close();      
moel@167
   156
    }    
moel@167
   157
moel@163
   158
    public static bool WaitIsaBusMutex(int millisecondsTimeout) {
moel@162
   159
      try {
moel@167
   160
        return isaBusMutex.WaitOne(millisecondsTimeout, false);
moel@167
   161
      } catch (AbandonedMutexException) { return false; } 
moel@167
   162
        catch (InvalidOperationException) { return false; }     
moel@162
   163
    }
moel@162
   164
moel@162
   165
    public static void ReleaseIsaBusMutex() {
moel@162
   166
      isaBusMutex.ReleaseMutex();
moel@167
   167
    }    
moel@1
   168
  }
moel@1
   169
}