Hardware/Ring0.cs
author moel.mich
Tue, 17 Jul 2012 16:10:59 +0000
changeset 364 25ef2c489ce8
parent 361 0a386ef7d5bb
child 367 45215572a774
permissions -rw-r--r--
Attempt at fixing Issue 253 without breaking Issue 159 once more.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2010-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.IO;
    13 using System.Reflection;
    14 using System.Runtime.InteropServices;
    15 using System.Security.AccessControl;
    16 using System.Threading;
    17 using System.Text;
    18 
    19 namespace OpenHardwareMonitor.Hardware {
    20   internal static class Ring0 {
    21 
    22     private static KernelDriver driver;
    23     private static string fileName;
    24     private static Mutex isaBusMutex;
    25     private static readonly StringBuilder report = new StringBuilder();
    26 
    27     private const uint OLS_TYPE = 40000;
    28     private static IOControlCode
    29       IOCTL_OLS_GET_REFCOUNT = new IOControlCode(OLS_TYPE, 0x801,
    30         IOControlCode.Access.Any),
    31       IOCTL_OLS_GET_DRIVER_VERSION = new IOControlCode(OLS_TYPE, 0x800,
    32         IOControlCode.Access.Any),
    33       IOCTL_OLS_READ_MSR = new IOControlCode(OLS_TYPE, 0x821,
    34         IOControlCode.Access.Any),
    35       IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822, 
    36         IOControlCode.Access.Any),
    37       IOCTL_OLS_READ_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x833,
    38         IOControlCode.Access.Read),
    39       IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836, 
    40         IOControlCode.Access.Write),
    41       IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851, 
    42         IOControlCode.Access.Read),
    43       IOCTL_OLS_WRITE_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x852,
    44         IOControlCode.Access.Write),
    45       IOCTL_OLS_READ_MEMORY = new IOControlCode(OLS_TYPE, 0x841,
    46         IOControlCode.Access.Read);
    47 
    48     private static string GetTempFileName() {
    49 
    50       // try to get a file in the temporary folder
    51       try {
    52         return Path.GetTempFileName();        
    53       } catch (IOException) { 
    54           // some I/O exception
    55         } 
    56         catch (UnauthorizedAccessException) { 
    57           // we do not have the right to create a file in the temp folder
    58         }
    59         catch (NotSupportedException) {
    60           // invalid path format of the TMP system environment variable
    61         }
    62 
    63       // if this failed, we try to create one in the application folder
    64       string fileName = Path.ChangeExtension(
    65         Assembly.GetExecutingAssembly().Location, ".sys");
    66       try {
    67         using (FileStream stream = File.Create(fileName)) {
    68           return fileName;
    69         }        
    70       } catch (IOException) { } 
    71         catch (UnauthorizedAccessException) { }
    72      
    73       return null;
    74     }
    75 
    76     private static bool ExtractDriver(string fileName) {
    77       string resourceName = "OpenHardwareMonitor.Hardware." +
    78         (OperatingSystem.Is64BitOperatingSystem() ? "WinRing0x64.sys" : 
    79         "WinRing0.sys");
    80 
    81       string[] names =
    82         Assembly.GetExecutingAssembly().GetManifestResourceNames();
    83       byte[] buffer = null;
    84       for (int i = 0; i < names.Length; i++) {
    85         if (names[i].Replace('\\', '.') == resourceName) {
    86           using (Stream stream = Assembly.GetExecutingAssembly().
    87             GetManifestResourceStream(names[i])) 
    88           {
    89               buffer = new byte[stream.Length];
    90               stream.Read(buffer, 0, buffer.Length);
    91           }
    92         }
    93       }
    94 
    95       if (buffer == null)
    96         return false;
    97 
    98       try {
    99         using (FileStream target = new FileStream(fileName, FileMode.Create)) {
   100           target.Write(buffer, 0, buffer.Length);
   101         }
   102       } catch (IOException) { 
   103         // for example there is not enough space on the disk
   104         return false; 
   105       }
   106 
   107       return true;
   108     }
   109 
   110     public static void Open() {
   111       // no implementation for unix systems
   112       int p = (int)Environment.OSVersion.Platform;
   113       if ((p == 4) || (p == 128))
   114         return;  
   115       
   116       if (driver != null)
   117         return;
   118 
   119       // clear the current report
   120       report.Length = 0;
   121      
   122       driver = new KernelDriver("WinRing0_1_2_0");
   123       driver.Open();
   124 
   125       if (!driver.IsOpen) {
   126         // driver is not loaded, try to install and open
   127 
   128         fileName = GetTempFileName();
   129         if (fileName != null && ExtractDriver(fileName)) {
   130           if (driver.Install(fileName)) {
   131             driver.Open();
   132 
   133             if (!driver.IsOpen) {
   134               driver.Delete();
   135               report.AppendLine("Status: Opening driver failed after install");
   136             }
   137           } else {
   138             // install failed, try to delete and reinstall
   139             driver.Delete();
   140 
   141             if (driver.Install(fileName)) {
   142               driver.Open();
   143 
   144               if (!driver.IsOpen) {
   145                 driver.Delete();
   146                 report.AppendLine(
   147                   "Status: Opening driver failed after reinstall");
   148               }
   149             } else {
   150               report.AppendLine("Status: Installing driver \"" +
   151                 fileName + "\" failed" +
   152                 (File.Exists(fileName) ? " and file exists" : ""));
   153               report.AppendLine();
   154               report.Append("Exception: " + Marshal.GetExceptionForHR(
   155                 Marshal.GetHRForLastWin32Error()).Message);
   156             }
   157           }
   158         } else {
   159           report.AppendLine("Status: Extracting driver failed");
   160         }
   161 
   162         try {
   163           // try to delte the driver file
   164           if (File.Exists(fileName))
   165             File.Delete(fileName);
   166           fileName = null;
   167         } catch (IOException) { } 
   168           catch (UnauthorizedAccessException) { }
   169       }
   170 
   171       if (!driver.IsOpen) 
   172         driver = null;
   173 
   174       string mutexName = "Global\\Access_ISABUS.HTP.Method";
   175       try {
   176         isaBusMutex = new Mutex(false, mutexName);
   177       } catch (UnauthorizedAccessException) {
   178         try {
   179           isaBusMutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize);
   180         } catch { }
   181       }
   182     }
   183 
   184     public static bool IsOpen {
   185       get { return driver != null; }
   186     }
   187 
   188     public static void Close() {
   189       if (driver == null)
   190         return;
   191 
   192       uint refCount = 0;
   193       driver.DeviceIOControl(IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
   194 
   195       driver.Close();
   196 
   197       if (refCount <= 1)
   198         driver.Delete();
   199 
   200       driver = null;
   201 
   202       if (isaBusMutex != null) {
   203         isaBusMutex.Close();
   204         isaBusMutex = null;
   205       }
   206 
   207       // try to delete temporary driver file again if failed during open
   208       if (fileName != null && File.Exists(fileName)) {
   209         try {
   210           File.Delete(fileName);
   211           fileName = null;
   212         } catch (IOException) { } 
   213           catch (UnauthorizedAccessException) { }
   214       }
   215     }
   216 
   217     public static string GetReport() {
   218       if (report.Length > 0) {
   219         StringBuilder r = new StringBuilder();
   220         r.AppendLine("Ring0");
   221         r.AppendLine();
   222         r.Append(report);
   223         r.AppendLine();
   224         return r.ToString();
   225       } else
   226         return null;
   227     }
   228 
   229     public static bool WaitIsaBusMutex(int millisecondsTimeout) {
   230       if (isaBusMutex == null)
   231         return true;
   232       try {
   233         return isaBusMutex.WaitOne(millisecondsTimeout, false);
   234       } catch (AbandonedMutexException) { return false; } 
   235         catch (InvalidOperationException) { return false; }
   236     }
   237 
   238     public static void ReleaseIsaBusMutex() {
   239       if (isaBusMutex == null)
   240         return;
   241       isaBusMutex.ReleaseMutex();
   242     }
   243 
   244     public static bool Rdmsr(uint index, out uint eax, out uint edx) {
   245       if (driver == null) {
   246         eax = 0;
   247         edx = 0;
   248         return false;
   249       }
   250 
   251       ulong buffer = 0;
   252       bool result = driver.DeviceIOControl(IOCTL_OLS_READ_MSR, index,
   253         ref buffer);
   254 
   255       edx = (uint)((buffer >> 32) & 0xFFFFFFFF);
   256       eax = (uint)(buffer & 0xFFFFFFFF);
   257       return result;
   258     }
   259 
   260     public static bool RdmsrTx(uint index, out uint eax, out uint edx,
   261       ulong threadAffinityMask) 
   262     {
   263       ulong mask = ThreadAffinity.Set(threadAffinityMask);
   264 
   265       bool result = Rdmsr(index, out eax, out edx);
   266 
   267       ThreadAffinity.Set(mask);
   268       return result;
   269     }
   270 
   271     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   272     private struct WrmsrInput {
   273       public uint Register;
   274       public ulong Value;
   275     }
   276 
   277     public static bool Wrmsr(uint index, uint eax, uint edx) {
   278       if (driver == null)
   279         return false;
   280 
   281       WrmsrInput input = new WrmsrInput();
   282       input.Register = index;
   283       input.Value = ((ulong)edx << 32) | eax;
   284 
   285       return driver.DeviceIOControl(IOCTL_OLS_WRITE_MSR, input);
   286     }
   287 
   288     public static byte ReadIoPort(uint port) {
   289       if (driver == null)
   290         return 0;
   291 
   292       uint value = 0;
   293       driver.DeviceIOControl(IOCTL_OLS_READ_IO_PORT_BYTE, port, ref value);
   294 
   295       return (byte)(value & 0xFF);
   296     }
   297 
   298     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   299     private struct WriteIoPortInput {
   300       public uint PortNumber;
   301       public byte Value;
   302     }
   303 
   304     public static void WriteIoPort(uint port, byte value) {
   305       if (driver == null)
   306         return;
   307 
   308       WriteIoPortInput input = new WriteIoPortInput();
   309       input.PortNumber = port;
   310       input.Value = value;
   311 
   312       driver.DeviceIOControl(IOCTL_OLS_WRITE_IO_PORT_BYTE, input);
   313     }
   314 
   315     public const uint InvalidPciAddress = 0xFFFFFFFF;
   316 
   317     public static uint GetPciAddress(byte bus, byte device, byte function) {
   318       return
   319         (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
   320     }
   321 
   322     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   323     private struct ReadPciConfigInput {
   324       public uint PciAddress;
   325       public uint RegAddress;
   326     }
   327 
   328     public static bool ReadPciConfig(uint pciAddress, uint regAddress, 
   329       out uint value) 
   330     {
   331       if (driver == null || (regAddress & 3) != 0) {
   332         value = 0;
   333         return false;
   334       }
   335 
   336       ReadPciConfigInput input = new ReadPciConfigInput();
   337       input.PciAddress = pciAddress;
   338       input.RegAddress = regAddress;
   339 
   340       value = 0;
   341       return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input, 
   342         ref value);
   343     }
   344 
   345     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   346     private struct WritePciConfigInput {
   347       public uint PciAddress;
   348       public uint RegAddress;
   349       public uint Value;
   350     }
   351 
   352     public static bool WritePciConfig(uint pciAddress, uint regAddress, 
   353       uint value) 
   354     {
   355       if (driver == null || (regAddress & 3) != 0)
   356         return false;
   357 
   358       WritePciConfigInput input = new WritePciConfigInput();
   359       input.PciAddress = pciAddress;
   360       input.RegAddress = regAddress;
   361       input.Value = value;
   362 
   363       return driver.DeviceIOControl(IOCTL_OLS_WRITE_PCI_CONFIG, input);
   364     }
   365 
   366     [StructLayout(LayoutKind.Sequential, Pack = 1)]
   367     private struct ReadMemoryInput {
   368       public ulong address;
   369       public uint unitSize;
   370       public uint count;
   371     }
   372 
   373     public static bool ReadMemory<T>(ulong address, ref T buffer) {
   374       if (driver == null) {
   375         return false;
   376       }
   377 
   378       ReadMemoryInput input = new ReadMemoryInput();
   379       input.address = address;
   380       input.unitSize = 1;
   381       input.count = (uint)Marshal.SizeOf(buffer);
   382 
   383       return driver.DeviceIOControl(IOCTL_OLS_READ_MEMORY, input,
   384         ref buffer);
   385     }
   386   }
   387 }