moel@236: /* moel@236: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@236: moel@344: Copyright (C) 2010-2011 Michael Möller moel@344: 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@285: using System.Security.AccessControl; 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@281: private static string fileName; 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@279: IOControlCode.Access.Write), moel@279: IOCTL_OLS_READ_MEMORY = new IOControlCode(OLS_TYPE, 0x841, moel@279: IOControlCode.Access.Read); moel@236: moel@283: private static string GetTempFileName() { moel@283: moel@283: // try to get a file in the temporary folder moel@283: try { moel@283: return Path.GetTempFileName(); moel@293: } catch (IOException) { moel@293: // some I/O exception moel@293: } moel@293: catch (UnauthorizedAccessException) { moel@293: // we do not have the right to create a file in the temp folder moel@293: } moel@293: catch (NotSupportedException) { moel@293: // invalid path format of the TMP system environment variable moel@293: } moel@283: moel@283: // if this failed, we try to create one in the application folder moel@283: string fileName = Path.ChangeExtension( moel@283: Assembly.GetExecutingAssembly().Location, ".sys"); moel@283: try { moel@283: using (FileStream stream = File.Create(fileName)) { moel@283: return fileName; moel@283: } moel@283: } catch (IOException) { } moel@283: catch (UnauthorizedAccessException) { } moel@283: moel@283: return null; moel@283: } moel@283: 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@294: try { moel@294: using (FileStream target = new FileStream(fileName, FileMode.Create)) { moel@294: target.Write(buffer, 0, buffer.Length); moel@294: } moel@294: } catch (IOException) { moel@294: // for example there is not enough space on the disk moel@294: return false; 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@283: fileName = GetTempFileName(); moel@283: if (fileName != null && ExtractDriver(fileName)) { moel@254: if (driver.Install(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@283: report.AppendLine("Status: Installing driver \"" + moel@283: 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@283: report.AppendLine("Status: Extracting driver failed"); moel@236: } moel@281: moel@281: try { moel@281: // try to delte the driver file moel@281: if (File.Exists(fileName)) moel@281: File.Delete(fileName); moel@281: fileName = null; moel@281: } catch (IOException) { } moel@281: catch (UnauthorizedAccessException) { } moel@236: } moel@236: moel@236: if (!driver.IsOpen) moel@236: driver = null; moel@236: moel@285: string mutexName = "Global\\Access_ISABUS.HTP.Method"; moel@285: try { moel@285: isaBusMutex = new Mutex(false, mutexName); moel@285: } catch (UnauthorizedAccessException) { moel@285: try { moel@285: isaBusMutex = Mutex.OpenExisting(mutexName, MutexRights.Synchronize); moel@285: } catch { } moel@285: } 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@285: if (isaBusMutex != null) { moel@285: isaBusMutex.Close(); moel@285: isaBusMutex = null; moel@285: } moel@281: moel@281: // try to delete temporary driver file again if failed during open moel@281: if (fileName != null && File.Exists(fileName)) { moel@281: try { moel@281: File.Delete(fileName); moel@281: fileName = null; moel@281: } catch (IOException) { } moel@281: catch (UnauthorizedAccessException) { } moel@281: } 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@285: if (isaBusMutex == null) moel@285: return true; 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@285: if (isaBusMutex == null) moel@285: return; 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@279: moel@279: [StructLayout(LayoutKind.Sequential, Pack = 1)] moel@279: private struct ReadMemoryInput { moel@279: public ulong address; moel@279: public uint unitSize; moel@279: public uint count; moel@279: } moel@279: moel@279: public static bool ReadMemory(ulong address, ref T buffer) { moel@279: if (driver == null) { moel@279: return false; moel@279: } moel@279: moel@279: ReadMemoryInput input = new ReadMemoryInput(); moel@279: input.address = address; moel@279: input.unitSize = 1; moel@279: input.count = (uint)Marshal.SizeOf(buffer); moel@279: moel@279: return driver.DeviceIOControl(IOCTL_OLS_READ_MEMORY, input, moel@279: ref buffer); moel@279: } moel@236: } moel@236: }