Hardware/WinRing0.cs
author moel.mich
Thu, 12 Aug 2010 20:53:27 +0000
changeset 166 fa9dfbfc4145
parent 163 67be1c62f950
child 167 b7cc9d09aefe
permissions -rw-r--r--
Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
     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.Collections.Generic;
    40 using System.IO;
    41 using System.Runtime.InteropServices;
    42 using System.Threading;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   internal class WinRing0 {
    47     
    48     public enum OlsDllStatus{
    49       OLS_DLL_NO_ERROR                        = 0,
    50       OLS_DLL_UNSUPPORTED_PLATFORM            = 1,
    51       OLS_DLL_DRIVER_NOT_LOADED               = 2,
    52       OLS_DLL_DRIVER_NOT_FOUND                = 3,
    53       OLS_DLL_DRIVER_UNLOADED                 = 4,
    54       OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK    = 5,
    55       OLS_DLL_UNKNOWN_ERROR                   = 9
    56     }
    57 
    58     private static bool available = false;
    59     public static Mutex isaBusMutex;
    60 
    61     private static string GetDllName() {   
    62       int p = (int)System.Environment.OSVersion.Platform;
    63       if ((p == 4) || (p == 128)) {
    64         if (IntPtr.Size == 4) {
    65           return "libring0.so";
    66         } else {
    67           return "libring0x64.so";
    68         }
    69       } else {
    70         if (IntPtr.Size == 4) {
    71           return "WinRing0.dll";
    72         } else {
    73           return "WinRing0x64.dll";
    74         }
    75       }                       
    76     }
    77     
    78     private delegate bool InitializeOlsDelegate();
    79     private delegate void DeinitializeOlsDelegate();
    80     
    81     public delegate uint GetDllStatusDelegate();
    82     public delegate bool IsCpuidDelegate();
    83     public delegate bool CpuidDelegate(uint index, uint ecxValue, 
    84       out uint eax, out uint ebx, out uint ecx, out uint edx);
    85     public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
    86       out uint eax, out uint ebx, out uint ecx, out uint edx,
    87       UIntPtr threadAffinityMask);
    88     public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
    89     public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
    90       UIntPtr threadAffinityMask);
    91     public delegate byte ReadIoPortByteDelegate(ushort port);
    92     public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    93     public delegate void SetPciMaxBusIndexDelegate(byte max);
    94     public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
    95       ushort deviceId, byte index);
    96     public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    97       uint regAddress, out uint value);
    98     public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
    99       uint regAddress, uint value);
   100     public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
   101       UIntPtr threadAffinityMask);
   102     public delegate bool RdtscDelegate(out uint eax, out uint edx);
   103 
   104     private static InitializeOlsDelegate InitializeOls;
   105     private static DeinitializeOlsDelegate DeinitializeOls;
   106 
   107     public static readonly GetDllStatusDelegate GetDllStatus;
   108     public static readonly IsCpuidDelegate IsCpuid;
   109     public static readonly CpuidDelegate Cpuid;
   110     public static readonly CpuidTxDelegate CpuidTx;
   111     public static readonly RdmsrDelegate Rdmsr;
   112     public static readonly RdmsrTxDelegate RdmsrTx;
   113     public static readonly ReadIoPortByteDelegate ReadIoPortByte;
   114     public static readonly WriteIoPortByteDelegate WriteIoPortByte;
   115     public static readonly SetPciMaxBusIndexDelegate SetPciMaxBusIndex;
   116     public static readonly FindPciDeviceByIdDelegate FindPciDeviceById;
   117     public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx;
   118     public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx;
   119     public static readonly RdtscTxDelegate RdtscTx;
   120     public static readonly RdtscDelegate Rdtsc;
   121 
   122     
   123 
   124     private static void GetDelegate<T>(string entryPoint, out T newDelegate) 
   125       where T : class 
   126     {
   127       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   128       attribute.CallingConvention = CallingConvention.Winapi;
   129       attribute.PreserveSig = true;
   130       attribute.EntryPoint = entryPoint;
   131       attribute.CharSet = CharSet.Auto;
   132       PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   133     }
   134 
   135     static WinRing0() {
   136       GetDelegate("InitializeOls", out InitializeOls);
   137       GetDelegate("DeinitializeOls", out DeinitializeOls);
   138       GetDelegate("GetDllStatus", out GetDllStatus);
   139       GetDelegate("IsCpuid", out IsCpuid);
   140       GetDelegate("Cpuid", out Cpuid);
   141       GetDelegate("CpuidTx", out CpuidTx);
   142       GetDelegate("Rdmsr", out  Rdmsr);
   143       GetDelegate("RdmsrTx", out  RdmsrTx);
   144       GetDelegate("ReadIoPortByte", out ReadIoPortByte);
   145       GetDelegate("WriteIoPortByte", out WriteIoPortByte);
   146       GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex);
   147       GetDelegate("FindPciDeviceById", out FindPciDeviceById);
   148       GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx);
   149       GetDelegate("WritePciConfigDwordEx", out WritePciConfigDwordEx);
   150       GetDelegate("RdtscTx", out RdtscTx);
   151       GetDelegate("Rdtsc", out Rdtsc);
   152 
   153       try {
   154         if (InitializeOls != null && InitializeOls())
   155           available = true;
   156       } catch (DllNotFoundException) { }
   157 
   158       isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
   159     }
   160     
   161     public static bool IsAvailable {
   162       get { return available; }
   163     }
   164 
   165     public static bool WaitIsaBusMutex(int millisecondsTimeout) {
   166       try {
   167         return isaBusMutex.WaitOne(millisecondsTimeout);
   168       } catch { return false; }
   169     }
   170 
   171     public static void ReleaseIsaBusMutex() {
   172       isaBusMutex.ReleaseMutex();
   173     }
   174 
   175     private static Deinitializer deinitializer = new Deinitializer();
   176     private class Deinitializer {
   177       ~Deinitializer() {
   178         if (available)
   179           DeinitializeOls();
   180         
   181         isaBusMutex.Close();
   182       }
   183     }
   184   }
   185 }