Hardware/WinRing0.cs
author paulwerelds
Mon, 04 Oct 2010 16:56:42 +0000
changeset 209 dee42c887ab0
parent 196 5e9a8595296c
permissions -rw-r--r--
Fixed the context menu positioning as well, following the previous fix to multimonitor setups.
     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.Runtime.InteropServices;
    40 using System.Threading;
    41 
    42 namespace OpenHardwareMonitor.Hardware {
    43 
    44   internal class WinRing0 {
    45 
    46     private 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;
    59     private static Mutex isaBusMutex;
    60 
    61     private static string GetDllName() {   
    62       int p = (int)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 bool IsCpuidDelegate();
    82     public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
    83       out uint eax, out uint ebx, out uint ecx, out uint edx,
    84       UIntPtr threadAffinityMask);
    85     public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
    86     public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
    87       UIntPtr threadAffinityMask);
    88     public delegate byte ReadIoPortByteDelegate(ushort port);
    89     public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    90     public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    91       uint regAddress, out uint value);
    92     public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
    93       uint regAddress, uint value);    
    94     public delegate bool RdtscDelegate(out uint eax, out uint edx);
    95     public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
    96       UIntPtr threadAffinityMask);    
    97     public delegate bool WrmsrDelegate(uint index, uint eax, uint edx);
    98     public delegate bool WrmsrTxDelegate(uint index, uint eax, uint edx,
    99       UIntPtr threadAffinityMask);
   100 
   101     private static readonly InitializeOlsDelegate InitializeOls = 
   102       CreateDelegate<InitializeOlsDelegate>("InitializeOls");
   103     private static readonly DeinitializeOlsDelegate DeinitializeOls =
   104       CreateDelegate<DeinitializeOlsDelegate>("DeinitializeOls");
   105 
   106     public static readonly IsCpuidDelegate IsCpuid =
   107       CreateDelegate<IsCpuidDelegate>("IsCpuid");
   108     public static readonly CpuidTxDelegate CpuidTx =
   109       CreateDelegate<CpuidTxDelegate>("CpuidTx");
   110     public static readonly RdmsrDelegate Rdmsr =
   111       CreateDelegate<RdmsrDelegate>("Rdmsr");
   112     public static readonly RdmsrTxDelegate RdmsrTx =
   113       CreateDelegate<RdmsrTxDelegate>("RdmsrTx");
   114     public static readonly ReadIoPortByteDelegate ReadIoPortByte =
   115       CreateDelegate<ReadIoPortByteDelegate>("ReadIoPortByte");
   116     public static readonly WriteIoPortByteDelegate WriteIoPortByte =
   117       CreateDelegate<WriteIoPortByteDelegate>("WriteIoPortByte");
   118     public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx =
   119       CreateDelegate<ReadPciConfigDwordExDelegate>("ReadPciConfigDwordEx");
   120     public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx =
   121       CreateDelegate<WritePciConfigDwordExDelegate>("WritePciConfigDwordEx");
   122     public static readonly RdtscDelegate Rdtsc =
   123       CreateDelegate<RdtscDelegate>("Rdtsc");
   124     public static readonly RdtscTxDelegate RdtscTx =
   125       CreateDelegate<RdtscTxDelegate>("RdtscTx");    
   126     public static readonly WrmsrDelegate Wrmsr =
   127       CreateDelegate<WrmsrDelegate>("Wrmsr");
   128     public static readonly WrmsrTxDelegate WrmsrTx =
   129       CreateDelegate<WrmsrTxDelegate>("WrmsrTx");
   130  
   131     private static T CreateDelegate<T>(string entryPoint) where T : class {
   132       DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   133       attribute.CallingConvention = CallingConvention.Winapi;
   134       attribute.PreserveSig = true;
   135       attribute.EntryPoint = entryPoint;
   136       attribute.CharSet = CharSet.Auto;
   137       T result;
   138       PInvokeDelegateFactory.CreateDelegate(attribute, out result);
   139       return result;
   140     }
   141 
   142     public static void Open() {
   143       try {
   144         if (InitializeOls != null && InitializeOls())
   145           available = true;
   146       } catch (DllNotFoundException) { }   
   147       
   148       isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
   149     }
   150 
   151     public static bool IsAvailable {
   152       get { return available; }
   153     }
   154 
   155     public static void Close() {
   156       if (available)
   157         DeinitializeOls();        
   158       isaBusMutex.Close();      
   159     }    
   160 
   161     public static bool WaitIsaBusMutex(int millisecondsTimeout) {
   162       try {
   163         return isaBusMutex.WaitOne(millisecondsTimeout, false);
   164       } catch (AbandonedMutexException) { return false; } 
   165         catch (InvalidOperationException) { return false; }     
   166     }
   167 
   168     public static void ReleaseIsaBusMutex() {
   169       isaBusMutex.ReleaseMutex();
   170     }
   171 
   172     public const uint InvalidPciAddress = 0xFFFFFFFF;
   173 
   174     public static uint GetPciAddress(byte bus, byte device, byte function)	{
   175       return 
   176         (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
   177     }
   178   }
   179 }