Hardware/WinRing0.cs
author moel.mich
Sun, 31 Jan 2010 20:57:18 +0000
changeset 14 51c2f209da6d
parent 1 361e324a0ed4
child 22 e6832d4b89d2
permissions -rw-r--r--
Release version 0.1.5. Added support for AMD family 0Fh CPUs. Fixed saving of wrong window size values for minimized forms (by not saving them).
     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.Text;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   public 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 
    60     private static string GetDllName() {   
    61       int p = (int)System.Environment.OSVersion.Platform;
    62       if ((p == 4) || (p == 128)) {
    63         if (IntPtr.Size == 4) {
    64           return "libring0.so";
    65         } else {
    66           return "libring0x64.so";
    67         }
    68       } else {
    69         if (IntPtr.Size == 4) {
    70           return "WinRing0.dll";
    71         } else {
    72           return "WinRing0x64.dll";
    73         }
    74       }                       
    75     }
    76     
    77     private delegate bool InitializeOlsDelegate();
    78     private delegate void DeinitializeOlsDelegate();
    79     
    80     public delegate uint GetDllStatusDelegate();
    81     public delegate bool IsCpuidDelegate();
    82     public delegate bool CpuidDelegate(uint index, out uint eax, out uint ebx, 
    83       out uint ecx, out uint edx);
    84     public delegate bool RdmsrPxDelegate(uint index, ref uint eax, ref uint edx, 
    85       UIntPtr processAffinityMask);
    86     public delegate byte ReadIoPortByteDelegate(ushort port);
    87     public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    88     public delegate void SetPciMaxBusIndexDelegate(byte max);
    89     public delegate uint FindPciDeviceByIdDelegate(ushort vendorId, 
    90       ushort deviceId, byte index);
    91     public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    92       uint regAddress, out uint value);
    93     public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
    94       uint regAddress, uint value);
    95 
    96     private static InitializeOlsDelegate InitializeOls;
    97     private static DeinitializeOlsDelegate DeinitializeOls;
    98 
    99     public static GetDllStatusDelegate GetDllStatus;
   100     public static IsCpuidDelegate IsCpuid;
   101     public static CpuidDelegate Cpuid;
   102     public static RdmsrPxDelegate RdmsrPx;
   103     public static ReadIoPortByteDelegate ReadIoPortByte;
   104     public static WriteIoPortByteDelegate WriteIoPortByte;
   105     public static SetPciMaxBusIndexDelegate SetPciMaxBusIndex;
   106     public static FindPciDeviceByIdDelegate FindPciDeviceById;
   107     public static ReadPciConfigDwordExDelegate ReadPciConfigDwordEx;
   108     public static WritePciConfigDwordExDelegate WritePciConfigDwordEx;
   109 
   110 
   111     private static void GetDelegate<T>(string entryPoint, out T newDelegate) 
   112       where T : class 
   113     {
   114       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   115       attribute.CallingConvention = CallingConvention.Winapi;
   116       attribute.PreserveSig = true;
   117       attribute.EntryPoint = entryPoint;
   118       attribute.CharSet = CharSet.Auto;
   119       PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate);
   120     }
   121 
   122     static WinRing0() {
   123       GetDelegate("InitializeOls", out InitializeOls);
   124       GetDelegate("DeinitializeOls", out DeinitializeOls);
   125       GetDelegate("GetDllStatus", out GetDllStatus);
   126       GetDelegate("IsCpuid", out IsCpuid);
   127       GetDelegate("Cpuid", out Cpuid);
   128       GetDelegate("RdmsrPx", out  RdmsrPx);
   129       GetDelegate("ReadIoPortByte", out ReadIoPortByte);
   130       GetDelegate("WriteIoPortByte", out WriteIoPortByte);
   131       GetDelegate("SetPciMaxBusIndex", out SetPciMaxBusIndex);
   132       GetDelegate("FindPciDeviceById", out FindPciDeviceById);
   133       GetDelegate("ReadPciConfigDwordEx", out ReadPciConfigDwordEx);
   134       GetDelegate("WritePciConfigDwordEx", out WritePciConfigDwordEx);
   135 
   136       try {
   137         if (InitializeOls != null && InitializeOls())
   138           available = true;
   139       } catch (DllNotFoundException) { }       
   140     }
   141     
   142     public static bool IsAvailable {
   143       get { return available; }
   144     }
   145 
   146     private static Deinitializer deinitializer = new Deinitializer();
   147     private class Deinitializer {
   148       ~Deinitializer() {
   149         if (available)
   150           DeinitializeOls();
   151       }
   152     }
   153   }
   154 }