Hardware/WinRing0.cs
changeset 1 361e324a0ed4
child 14 51c2f209da6d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/WinRing0.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,149 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.IO;
    1.44 +using System.Runtime.InteropServices;
    1.45 +using System.Text;
    1.46 +
    1.47 +namespace OpenHardwareMonitor.Hardware {
    1.48 +
    1.49 +  public class WinRing0 {
    1.50 +    
    1.51 +    public enum OlsDllStatus{
    1.52 +      OLS_DLL_NO_ERROR                        = 0,
    1.53 +      OLS_DLL_UNSUPPORTED_PLATFORM            = 1,
    1.54 +      OLS_DLL_DRIVER_NOT_LOADED               = 2,
    1.55 +      OLS_DLL_DRIVER_NOT_FOUND                = 3,
    1.56 +      OLS_DLL_DRIVER_UNLOADED                 = 4,
    1.57 +      OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK    = 5,
    1.58 +      OLS_DLL_UNKNOWN_ERROR                   = 9
    1.59 +    }
    1.60 +
    1.61 +    private static bool available = false;
    1.62 +
    1.63 +    private static string GetDllName() {   
    1.64 +      int p = (int)System.Environment.OSVersion.Platform;
    1.65 +      if ((p == 4) || (p == 128)) {
    1.66 +        if (IntPtr.Size == 4) {
    1.67 +          return "libring0.so";
    1.68 +        } else {
    1.69 +          return "libring0x64.so";
    1.70 +        }
    1.71 +      } else {
    1.72 +        if (IntPtr.Size == 4) {
    1.73 +          return "WinRing0.dll";
    1.74 +        } else {
    1.75 +          return "WinRing0x64.dll";
    1.76 +        }
    1.77 +      }                       
    1.78 +    }
    1.79 +    
    1.80 +    private delegate bool InitializeOlsDelegate();
    1.81 +    private delegate void DeinitializeOlsDelegate();
    1.82 +    
    1.83 +    public delegate uint GetDllStatusDelegate();
    1.84 +    public delegate bool IsCpuidDelegate();
    1.85 +    public delegate bool CpuidDelegate(uint index, out uint eax, out uint ebx, 
    1.86 +      out uint ecx, out uint edx);
    1.87 +    public delegate bool RdmsrPxDelegate(uint index, ref uint eax, ref uint edx, 
    1.88 +      UIntPtr processAffinityMask);
    1.89 +    public delegate byte ReadIoPortByteDelegate(ushort port);
    1.90 +    public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    1.91 +    public delegate void SetPciMaxBusIndexDelegate(byte max);
    1.92 +    public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
    1.93 +      ushort deviceId, byte index);
    1.94 +    public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    1.95 +      uint regAddress, out uint value);
    1.96 +
    1.97 +    private static InitializeOlsDelegate InitializeOls;
    1.98 +    private static DeinitializeOlsDelegate DeinitializeOls;
    1.99 +
   1.100 +    public static GetDllStatusDelegate GetDllStatus;
   1.101 +    public static IsCpuidDelegate IsCpuid;
   1.102 +    public static CpuidDelegate Cpuid;
   1.103 +    public static RdmsrPxDelegate RdmsrPx;
   1.104 +    public static ReadIoPortByteDelegate ReadIoPortByte;
   1.105 +    public static WriteIoPortByteDelegate WriteIoPortByte;
   1.106 +    public static SetPciMaxBusIndexDelegate SetPciMaxBusIndex;
   1.107 +    public static FindPciDeviceByIdDelegate FindPciDeviceById;
   1.108 +    public static ReadPciConfigDwordExDelegate ReadPciConfigDwordEx;
   1.109 +
   1.110 +    private static void GetDelegate<T>(string entryPoint, out T newDelegate) 
   1.111 +      where T : class 
   1.112 +    {
   1.113 +      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   1.114 +      attribute.CallingConvention = CallingConvention.Winapi;
   1.115 +      attribute.PreserveSig = true;
   1.116 +      attribute.EntryPoint = entryPoint;
   1.117 +      attribute.CharSet = CharSet.Auto;
   1.118 +      PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   1.119 +    }
   1.120 +
   1.121 +    static WinRing0() {
   1.122 +      GetDelegate("InitializeOls", out InitializeOls);
   1.123 +      GetDelegate("DeinitializeOls", out DeinitializeOls);
   1.124 +      GetDelegate("GetDllStatus", out GetDllStatus);
   1.125 +      GetDelegate("IsCpuid", out IsCpuid);
   1.126 +      GetDelegate("Cpuid", out Cpuid);
   1.127 +      GetDelegate("RdmsrPx", out  RdmsrPx);
   1.128 +      GetDelegate("ReadIoPortByte", out ReadIoPortByte);
   1.129 +      GetDelegate("WriteIoPortByte", out WriteIoPortByte);
   1.130 +      GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex);
   1.131 +      GetDelegate("FindPciDeviceById", out FindPciDeviceById);
   1.132 +      GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx);
   1.133 +
   1.134 +      try {
   1.135 +        if (InitializeOls != null && InitializeOls())
   1.136 +          available = true;
   1.137 +      } catch (DllNotFoundException) { }       
   1.138 +    }
   1.139 +    
   1.140 +    public static bool IsAvailable {
   1.141 +      get { return available; }
   1.142 +    }
   1.143 +
   1.144 +    private static Deinitializer deinitializer = new Deinitializer();
   1.145 +    private class Deinitializer {
   1.146 +      ~Deinitializer() {
   1.147 +        if (available)
   1.148 +          DeinitializeOls();
   1.149 +      }
   1.150 +    }
   1.151 +  }
   1.152 +}