moel@236: /*
moel@236:   
moel@236:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@236: 
moel@236:   The contents of this file are subject to the Mozilla Public License Version
moel@236:   1.1 (the "License"); you may not use this file except in compliance with
moel@236:   the License. You may obtain a copy of the License at
moel@236:  
moel@236:   http://www.mozilla.org/MPL/
moel@236: 
moel@236:   Software distributed under the License is distributed on an "AS IS" basis,
moel@236:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@236:   for the specific language governing rights and limitations under the License.
moel@236: 
moel@236:   The Original Code is the Open Hardware Monitor code.
moel@236: 
moel@236:   The Initial Developer of the Original Code is 
moel@236:   Michael Möller <m.moeller@gmx.ch>.
moel@254:   Portions created by the Initial Developer are Copyright (C) 2010-2011
moel@236:   the Initial Developer. All Rights Reserved.
moel@236: 
moel@236:   Contributor(s):
moel@236: 
moel@236:   Alternatively, the contents of this file may be used under the terms of
moel@236:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@236:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@236:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@236:   of those above. If you wish to allow use of your version of this file only
moel@236:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@236:   use your version of this file under the terms of the MPL, indicate your
moel@236:   decision by deleting the provisions above and replace them with the notice
moel@236:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@236:   the provisions above, a recipient may use your version of this file under
moel@236:   the terms of any one of the MPL, the GPL or the LGPL.
moel@236:  
moel@236: */
moel@236: 
moel@236: using System;
moel@236: using System.IO;
moel@236: using System.Reflection;
moel@236: using System.Runtime.InteropServices;
moel@236: using System.Threading;
moel@254: using System.Text;
moel@236: 
moel@236: namespace OpenHardwareMonitor.Hardware {
moel@236:   internal static class Ring0 {
moel@236: 
moel@236:     private static KernelDriver driver;
moel@236:     private static Mutex isaBusMutex;
moel@254:     private static readonly StringBuilder report = new StringBuilder();
moel@236: 
moel@236:     private const uint OLS_TYPE = 40000;
moel@236:     private static IOControlCode
moel@236:       IOCTL_OLS_GET_REFCOUNT = new IOControlCode(OLS_TYPE, 0x801,
moel@236:         IOControlCode.Access.Any),
moel@236:       IOCTL_OLS_GET_DRIVER_VERSION = new IOControlCode(OLS_TYPE, 0x800,
moel@236:         IOControlCode.Access.Any),
moel@236:       IOCTL_OLS_READ_MSR = new IOControlCode(OLS_TYPE, 0x821,
moel@236:         IOControlCode.Access.Any),
moel@236:       IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822, 
moel@236:         IOControlCode.Access.Any),
moel@236:       IOCTL_OLS_READ_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x833,
moel@236:         IOControlCode.Access.Read),
moel@236:       IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836, 
moel@236:         IOControlCode.Access.Write),
moel@236:       IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851, 
moel@236:         IOControlCode.Access.Read),
moel@236:       IOCTL_OLS_WRITE_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x852,
moel@236:         IOControlCode.Access.Write);
moel@236: 
moel@236:     private static bool ExtractDriver(string fileName) {
moel@236:       string resourceName = "OpenHardwareMonitor.Hardware." +
moel@236:         (IntPtr.Size == 4 ? "WinRing0.sys" : "WinRing0x64.sys");
moel@236: 
moel@236:       string[] names =
moel@236:         Assembly.GetExecutingAssembly().GetManifestResourceNames();
moel@236:       byte[] buffer = null;
moel@236:       for (int i = 0; i < names.Length; i++) {
moel@236:         if (names[i].Replace('\\', '.') == resourceName) {
moel@236:           using (Stream stream = Assembly.GetExecutingAssembly().
moel@236:             GetManifestResourceStream(names[i])) 
moel@236:           {
moel@236:               buffer = new byte[stream.Length];
moel@236:               stream.Read(buffer, 0, buffer.Length);
moel@236:           }
moel@236:         }
moel@236:       }
moel@236: 
moel@236:       if (buffer == null)
moel@236:         return false;
moel@236: 
moel@236:       using (FileStream target = new FileStream(fileName, FileMode.Create)) {
moel@236:         target.Write(buffer, 0, buffer.Length);
moel@236:       }
moel@236: 
moel@236:       return true;
moel@236:     }
moel@236: 
moel@236:     public static void Open() {
moel@254:       // no implementation for unix systems
moel@238:       int p = (int)Environment.OSVersion.Platform;
moel@238:       if ((p == 4) || (p == 128))
moel@238:         return;  
moel@238:       
moel@236:       if (driver != null)
moel@236:         return;
moel@254: 
moel@254:       // clear the current report
moel@254:       report.Length = 0;
moel@236:      
moel@236:       driver = new KernelDriver("WinRing0_1_2_0");
moel@236:       driver.Open();
moel@236: 
moel@236:       if (!driver.IsOpen) {
moel@255:         // driver is not loaded, try to reinstall and open
moel@255: 
moel@255:         driver.Delete();
moel@236:         string fileName = Path.GetTempFileName();
moel@236:         if (ExtractDriver(fileName)) {
moel@254:           if (driver.Install(fileName)) {
moel@254:             File.Delete(fileName);
moel@254:             driver.Open();
moel@236: 
moel@254:             if (!driver.IsOpen) {
moel@254:               driver.Delete();
moel@254:               report.AppendLine("Status: Opening driver failed");
moel@254:             }
moel@254:           } else {
moel@255:             report.AppendLine("Status: Installing driver \"" + 
moel@255:               fileName + "\" failed" + 
moel@255:               (File.Exists(fileName) ? " and file exists" : ""));
moel@254:             report.AppendLine();
moel@254:             report.Append("Exception: " + Marshal.GetExceptionForHR(
moel@254:               Marshal.GetHRForLastWin32Error()).Message);
moel@254:           }
moel@254:         } else {
moel@254:           report.AppendLine(
moel@254:             "Status: Extracting driver to \"" + fileName + "\" failed");
moel@236:         }
moel@236:       }
moel@236: 
moel@236:       if (!driver.IsOpen) 
moel@236:         driver = null;
moel@236: 
moel@259:       isaBusMutex = new Mutex(false, "Global\\Access_ISABUS.HTP.Method");
moel@236:     }
moel@236: 
moel@236:     public static bool IsOpen {
moel@236:       get { return driver != null; }
moel@236:     }
moel@236: 
moel@236:     public static void Close() {
moel@236:       if (driver == null)
moel@236:         return;
moel@236: 
moel@236:       uint refCount = 0;
moel@236:       driver.DeviceIOControl(IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
moel@236: 
moel@236:       driver.Close();
moel@236: 
moel@236:       if (refCount <= 1)
moel@236:         driver.Delete();
moel@236: 
moel@236:       driver = null;
moel@236: 
moel@236:       isaBusMutex.Close(); 
moel@236:     }
moel@236: 
moel@254:     public static string GetReport() {
moel@254:       if (report.Length > 0) {
moel@256:         StringBuilder r = new StringBuilder();
moel@256:         r.AppendLine("Ring0");
moel@256:         r.AppendLine();
moel@256:         r.Append(report);
moel@256:         r.AppendLine();
moel@256:         return r.ToString();
moel@254:       } else
moel@254:         return null;
moel@254:     }
moel@254: 
moel@236:     public static bool WaitIsaBusMutex(int millisecondsTimeout) {
moel@236:       try {
moel@236:         return isaBusMutex.WaitOne(millisecondsTimeout, false);
moel@236:       } catch (AbandonedMutexException) { return false; } 
moel@236:         catch (InvalidOperationException) { return false; }
moel@236:     }
moel@236: 
moel@236:     public static void ReleaseIsaBusMutex() {
moel@236:       isaBusMutex.ReleaseMutex();
moel@236:     }
moel@236: 
moel@236:     public static bool Rdmsr(uint index, out uint eax, out uint edx) {
moel@236:       if (driver == null) {
moel@236:         eax = 0;
moel@236:         edx = 0;
moel@236:         return false;
moel@236:       }
moel@236: 
moel@236:       ulong buffer = 0;
moel@236:       bool result = driver.DeviceIOControl(IOCTL_OLS_READ_MSR, index,
moel@236:         ref buffer);
moel@236: 
moel@236:       edx = (uint)((buffer >> 32) & 0xFFFFFFFF);
moel@236:       eax = (uint)(buffer & 0xFFFFFFFF);
moel@236:       return result;
moel@236:     }
moel@236: 
moel@236:     public static bool RdmsrTx(uint index, out uint eax, out uint edx,
moel@238:       ulong threadAffinityMask) 
moel@236:     {
moel@238:       ulong mask = ThreadAffinity.Set(threadAffinityMask);
moel@236: 
moel@236:       bool result = Rdmsr(index, out eax, out edx);
moel@236: 
moel@238:       ThreadAffinity.Set(mask);
moel@236:       return result;
moel@236:     }
moel@236: 
moel@236:     [StructLayout(LayoutKind.Sequential, Pack = 1)]
moel@236:     private struct WrmsrInput {
moel@236:       public uint Register;
moel@236:       public ulong Value;
moel@236:     }
moel@236: 
moel@236:     public static bool Wrmsr(uint index, uint eax, uint edx) {
moel@236:       if (driver == null)
moel@236:         return false;
moel@236: 
moel@236:       WrmsrInput input = new WrmsrInput();
moel@236:       input.Register = index;
moel@236:       input.Value = ((ulong)edx << 32) | eax;
moel@236: 
moel@236:       return driver.DeviceIOControl(IOCTL_OLS_WRITE_MSR, input);
moel@236:     }
moel@236: 
moel@236:     public static byte ReadIoPort(uint port) {
moel@236:       if (driver == null)
moel@236:         return 0;
moel@236: 
moel@236:       uint value = 0;
moel@236:       driver.DeviceIOControl(IOCTL_OLS_READ_IO_PORT_BYTE, port, ref value);
moel@236: 
moel@236:       return (byte)(value & 0xFF);
moel@236:     }
moel@236: 
moel@236:     [StructLayout(LayoutKind.Sequential, Pack = 1)]
moel@236:     private struct WriteIoPortInput {
moel@236:       public uint PortNumber;
moel@236:       public byte Value;
moel@236:     }
moel@236: 
moel@236:     public static void WriteIoPort(uint port, byte value) {
moel@236:       if (driver == null)
moel@236:         return;
moel@236: 
moel@236:       WriteIoPortInput input = new WriteIoPortInput();
moel@236:       input.PortNumber = port;
moel@236:       input.Value = value;
moel@236: 
moel@236:       driver.DeviceIOControl(IOCTL_OLS_WRITE_IO_PORT_BYTE, input);
moel@236:     }
moel@236: 
moel@236:     public const uint InvalidPciAddress = 0xFFFFFFFF;
moel@236: 
moel@236:     public static uint GetPciAddress(byte bus, byte device, byte function) {
moel@236:       return
moel@236:         (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
moel@236:     }
moel@236: 
moel@236:     [StructLayout(LayoutKind.Sequential, Pack = 1)]
moel@236:     private struct ReadPciConfigInput {
moel@236:       public uint PciAddress;
moel@236:       public uint RegAddress;
moel@236:     }
moel@236: 
moel@236:     public static bool ReadPciConfig(uint pciAddress, uint regAddress, 
moel@236:       out uint value) 
moel@236:     {
moel@236:       if (driver == null || (regAddress & 3) != 0) {
moel@236:         value = 0;
moel@236:         return false;
moel@236:       }
moel@236: 
moel@236:       ReadPciConfigInput input = new ReadPciConfigInput();
moel@236:       input.PciAddress = pciAddress;
moel@236:       input.RegAddress = regAddress;
moel@236: 
moel@236:       value = 0;
moel@236:       return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input, 
moel@236:         ref value);
moel@236:     }
moel@236: 
moel@236:     [StructLayout(LayoutKind.Sequential, Pack = 1)]
moel@236:     private struct WritePciConfigInput {
moel@236:       public uint PciAddress;
moel@236:       public uint RegAddress;
moel@236:       public uint Value;
moel@236:     }
moel@236: 
moel@236:     public static bool WritePciConfig(uint pciAddress, uint regAddress, 
moel@236:       uint value) 
moel@236:     {
moel@236:       if (driver == null || (regAddress & 3) != 0)
moel@236:         return false;
moel@236: 
moel@236:       WritePciConfigInput input = new WritePciConfigInput();
moel@236:       input.PciAddress = pciAddress;
moel@236:       input.RegAddress = regAddress;
moel@236:       input.Value = value;
moel@236: 
moel@236:       return driver.DeviceIOControl(IOCTL_OLS_WRITE_PCI_CONFIG, input);
moel@236:     }
moel@236:   }
moel@236: }