Hardware/WinRing0.cs
author moel.mich
Tue, 17 Aug 2010 21:44:02 +0000
changeset 170 31858ba46e9c
parent 165 813d8bc3192f
child 182 4801e9eaf979
permissions -rw-r--r--
Fixed Issue 97.
     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     private WinRing0() { }
    49     
    50     public enum OlsDllStatus{
    51       OLS_DLL_NO_ERROR                        = 0,
    52       OLS_DLL_UNSUPPORTED_PLATFORM            = 1,
    53       OLS_DLL_DRIVER_NOT_LOADED               = 2,
    54       OLS_DLL_DRIVER_NOT_FOUND                = 3,
    55       OLS_DLL_DRIVER_UNLOADED                 = 4,
    56       OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK    = 5,
    57       OLS_DLL_UNKNOWN_ERROR                   = 9
    58     }
    59 
    60     private static bool available = false;
    61     private static Mutex isaBusMutex;
    62 
    63     private static string GetDllName() {   
    64       int p = (int)System.Environment.OSVersion.Platform;
    65       if ((p == 4) || (p == 128)) {
    66         if (IntPtr.Size == 4) {
    67           return "libring0.so";
    68         } else {
    69           return "libring0x64.so";
    70         }
    71       } else {
    72         if (IntPtr.Size == 4) {
    73           return "WinRing0.dll";
    74         } else {
    75           return "WinRing0x64.dll";
    76         }
    77       }                       
    78     }
    79     
    80     private delegate bool InitializeOlsDelegate();
    81     private delegate void DeinitializeOlsDelegate();
    82     
    83     public delegate bool IsCpuidDelegate();
    84     public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
    85       out uint eax, out uint ebx, out uint ecx, out uint edx,
    86       UIntPtr threadAffinityMask);
    87     public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
    88     public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
    89       UIntPtr threadAffinityMask);
    90     public delegate byte ReadIoPortByteDelegate(ushort port);
    91     public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    92     public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
    93       ushort deviceId, byte index);
    94     public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    95       uint regAddress, out uint value);
    96     public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
    97       uint regAddress, uint value);
    98     public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
    99       UIntPtr threadAffinityMask);
   100     public delegate bool RdtscDelegate(out uint eax, out uint edx);
   101 
   102     private static InitializeOlsDelegate InitializeOls = 
   103       CreateDelegate<InitializeOlsDelegate>("InitializeOls");
   104     private static DeinitializeOlsDelegate DeinitializeOls =
   105       CreateDelegate<DeinitializeOlsDelegate>("DeinitializeOls");
   106 
   107     public static readonly IsCpuidDelegate IsCpuid =
   108       CreateDelegate<IsCpuidDelegate>("IsCpuid");
   109     public static readonly CpuidTxDelegate CpuidTx =
   110       CreateDelegate<CpuidTxDelegate>("CpuidTx");
   111     public static readonly RdmsrDelegate Rdmsr =
   112       CreateDelegate<RdmsrDelegate>("Rdmsr");
   113     public static readonly RdmsrTxDelegate RdmsrTx =
   114       CreateDelegate<RdmsrTxDelegate>("RdmsrTx");
   115     public static readonly ReadIoPortByteDelegate ReadIoPortByte =
   116       CreateDelegate<ReadIoPortByteDelegate>("ReadIoPortByte");
   117     public static readonly WriteIoPortByteDelegate WriteIoPortByte =
   118       CreateDelegate<WriteIoPortByteDelegate>("WriteIoPortByte");
   119     public static readonly FindPciDeviceByIdDelegate FindPciDeviceById =
   120       CreateDelegate<FindPciDeviceByIdDelegate>("FindPciDeviceById");
   121     public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx =
   122       CreateDelegate<ReadPciConfigDwordExDelegate>("ReadPciConfigDwordEx");
   123     public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx =
   124       CreateDelegate<WritePciConfigDwordExDelegate>("WritePciConfigDwordEx");
   125     public static readonly RdtscTxDelegate RdtscTx =
   126       CreateDelegate<RdtscTxDelegate>("RdtscTx");
   127     public static readonly RdtscDelegate Rdtsc =
   128       CreateDelegate<RdtscDelegate>("Rdtsc");
   129  
   130     private static T CreateDelegate<T>(string entryPoint) where T : class {
   131       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   132       attribute.CallingConvention = CallingConvention.Winapi;
   133       attribute.PreserveSig = true;
   134       attribute.EntryPoint = entryPoint;
   135       attribute.CharSet = CharSet.Auto;
   136       T result;
   137       PInvokeDelegateFactory.CreateDelegate(attribute, out result);
   138       return result;
   139     }
   140 
   141     public static void Open() {
   142       try {
   143         if (InitializeOls != null && InitializeOls())
   144           available = true;
   145       } catch (DllNotFoundException) { }   
   146       
   147       isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
   148     }
   149 
   150     public static bool IsAvailable {
   151       get { return available; }
   152     }
   153 
   154     public static void Close() {
   155       if (available)
   156         DeinitializeOls();        
   157       isaBusMutex.Close();      
   158     }    
   159 
   160     public static bool WaitIsaBusMutex(int millisecondsTimeout) {
   161       try {
   162         return isaBusMutex.WaitOne(millisecondsTimeout, false);
   163       } catch (AbandonedMutexException) { return false; } 
   164         catch (InvalidOperationException) { return false; }     
   165     }
   166 
   167     public static void ReleaseIsaBusMutex() {
   168       isaBusMutex.ReleaseMutex();
   169     }    
   170   }
   171 }