Hardware/WinRing0.cs
changeset 236 763675f19ff4
parent 235 99b6bab765f8
child 237 9bf70d316cea
     1.1 --- a/Hardware/WinRing0.cs	Mon Oct 18 07:18:14 2010 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,179 +0,0 @@
     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.Runtime.InteropServices;
    1.43 -using System.Threading;
    1.44 -
    1.45 -namespace OpenHardwareMonitor.Hardware {
    1.46 -
    1.47 -  internal class WinRing0 {
    1.48 -
    1.49 -    private 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;
    1.62 -    private static Mutex isaBusMutex;
    1.63 -
    1.64 -    private static string GetDllName() {   
    1.65 -      int p = (int)Environment.OSVersion.Platform;
    1.66 -      if ((p == 4) || (p == 128)) {
    1.67 -        if (IntPtr.Size == 4) {
    1.68 -          return "libring0.so";
    1.69 -        } else {
    1.70 -          return "libring0x64.so";
    1.71 -        }
    1.72 -      } else {
    1.73 -        if (IntPtr.Size == 4) {
    1.74 -          return "WinRing0.dll";
    1.75 -        } else {
    1.76 -          return "WinRing0x64.dll";
    1.77 -        }
    1.78 -      }                       
    1.79 -    }
    1.80 -    
    1.81 -    private delegate bool InitializeOlsDelegate();
    1.82 -    private delegate void DeinitializeOlsDelegate();
    1.83 -    
    1.84 -    public delegate bool IsCpuidDelegate();
    1.85 -    public delegate bool CpuidTxDelegate(uint index, uint ecxValue,
    1.86 -      out uint eax, out uint ebx, out uint ecx, out uint edx,
    1.87 -      UIntPtr threadAffinityMask);
    1.88 -    public delegate bool RdmsrDelegate(uint index, out uint eax, out uint edx);
    1.89 -    public delegate bool RdmsrTxDelegate(uint index, out uint eax, out uint edx,
    1.90 -      UIntPtr threadAffinityMask);
    1.91 -    public delegate byte ReadIoPortByteDelegate(ushort port);
    1.92 -    public delegate void WriteIoPortByteDelegate(ushort port, byte value);
    1.93 -    public delegate bool ReadPciConfigDwordExDelegate(uint pciAddress, 
    1.94 -      uint regAddress, out uint value);
    1.95 -    public delegate bool WritePciConfigDwordExDelegate(uint pciAddress, 
    1.96 -      uint regAddress, uint value);    
    1.97 -    public delegate bool RdtscDelegate(out uint eax, out uint edx);
    1.98 -    public delegate bool RdtscTxDelegate(out uint eax, out uint edx,
    1.99 -      UIntPtr threadAffinityMask);    
   1.100 -    public delegate bool WrmsrDelegate(uint index, uint eax, uint edx);
   1.101 -    public delegate bool WrmsrTxDelegate(uint index, uint eax, uint edx,
   1.102 -      UIntPtr threadAffinityMask);
   1.103 -
   1.104 -    private static readonly InitializeOlsDelegate InitializeOls = 
   1.105 -      CreateDelegate<InitializeOlsDelegate>("InitializeOls");
   1.106 -    private static readonly DeinitializeOlsDelegate DeinitializeOls =
   1.107 -      CreateDelegate<DeinitializeOlsDelegate>("DeinitializeOls");
   1.108 -
   1.109 -    public static readonly IsCpuidDelegate IsCpuid =
   1.110 -      CreateDelegate<IsCpuidDelegate>("IsCpuid");
   1.111 -    public static readonly CpuidTxDelegate CpuidTx =
   1.112 -      CreateDelegate<CpuidTxDelegate>("CpuidTx");
   1.113 -    public static readonly RdmsrDelegate Rdmsr =
   1.114 -      CreateDelegate<RdmsrDelegate>("Rdmsr");
   1.115 -    public static readonly RdmsrTxDelegate RdmsrTx =
   1.116 -      CreateDelegate<RdmsrTxDelegate>("RdmsrTx");
   1.117 -    public static readonly ReadIoPortByteDelegate ReadIoPortByte =
   1.118 -      CreateDelegate<ReadIoPortByteDelegate>("ReadIoPortByte");
   1.119 -    public static readonly WriteIoPortByteDelegate WriteIoPortByte =
   1.120 -      CreateDelegate<WriteIoPortByteDelegate>("WriteIoPortByte");
   1.121 -    public static readonly ReadPciConfigDwordExDelegate ReadPciConfigDwordEx =
   1.122 -      CreateDelegate<ReadPciConfigDwordExDelegate>("ReadPciConfigDwordEx");
   1.123 -    public static readonly WritePciConfigDwordExDelegate WritePciConfigDwordEx =
   1.124 -      CreateDelegate<WritePciConfigDwordExDelegate>("WritePciConfigDwordEx");
   1.125 -    public static readonly RdtscDelegate Rdtsc =
   1.126 -      CreateDelegate<RdtscDelegate>("Rdtsc");
   1.127 -    public static readonly RdtscTxDelegate RdtscTx =
   1.128 -      CreateDelegate<RdtscTxDelegate>("RdtscTx");    
   1.129 -    public static readonly WrmsrDelegate Wrmsr =
   1.130 -      CreateDelegate<WrmsrDelegate>("Wrmsr");
   1.131 -    public static readonly WrmsrTxDelegate WrmsrTx =
   1.132 -      CreateDelegate<WrmsrTxDelegate>("WrmsrTx");
   1.133 - 
   1.134 -    private static T CreateDelegate<T>(string entryPoint) where T : class {
   1.135 -      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   1.136 -      attribute.CallingConvention = CallingConvention.Winapi;
   1.137 -      attribute.PreserveSig = true;
   1.138 -      attribute.EntryPoint = entryPoint;
   1.139 -      attribute.CharSet = CharSet.Auto;
   1.140 -      T result;
   1.141 -      PInvokeDelegateFactory.CreateDelegate(attribute, out result);
   1.142 -      return result;
   1.143 -    }
   1.144 -
   1.145 -    public static void Open() {
   1.146 -      try {
   1.147 -        if (InitializeOls != null && InitializeOls())
   1.148 -          available = true;
   1.149 -      } catch (DllNotFoundException) { }   
   1.150 -      
   1.151 -      isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
   1.152 -    }
   1.153 -
   1.154 -    public static bool IsAvailable {
   1.155 -      get { return available; }
   1.156 -    }
   1.157 -
   1.158 -    public static void Close() {
   1.159 -      if (available)
   1.160 -        DeinitializeOls();        
   1.161 -      isaBusMutex.Close();      
   1.162 -    }    
   1.163 -
   1.164 -    public static bool WaitIsaBusMutex(int millisecondsTimeout) {
   1.165 -      try {
   1.166 -        return isaBusMutex.WaitOne(millisecondsTimeout, false);
   1.167 -      } catch (AbandonedMutexException) { return false; } 
   1.168 -        catch (InvalidOperationException) { return false; }     
   1.169 -    }
   1.170 -
   1.171 -    public static void ReleaseIsaBusMutex() {
   1.172 -      isaBusMutex.ReleaseMutex();
   1.173 -    }
   1.174 -
   1.175 -    public const uint InvalidPciAddress = 0xFFFFFFFF;
   1.176 -
   1.177 -    public static uint GetPciAddress(byte bus, byte device, byte function)	{
   1.178 -      return 
   1.179 -        (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
   1.180 -    }
   1.181 -  }
   1.182 -}