Added CPUID support for Linux.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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) 2010
20 the Initial Developer. All Rights Reserved.
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.
40 using System.Reflection;
41 using System.Runtime.InteropServices;
42 using System.Threading;
44 namespace OpenHardwareMonitor.Hardware {
45 internal static class Ring0 {
47 private static KernelDriver driver;
48 private static Mutex isaBusMutex;
50 private const uint OLS_TYPE = 40000;
51 private static IOControlCode
52 IOCTL_OLS_GET_REFCOUNT = new IOControlCode(OLS_TYPE, 0x801,
53 IOControlCode.Access.Any),
54 IOCTL_OLS_GET_DRIVER_VERSION = new IOControlCode(OLS_TYPE, 0x800,
55 IOControlCode.Access.Any),
56 IOCTL_OLS_READ_MSR = new IOControlCode(OLS_TYPE, 0x821,
57 IOControlCode.Access.Any),
58 IOCTL_OLS_WRITE_MSR = new IOControlCode(OLS_TYPE, 0x822,
59 IOControlCode.Access.Any),
60 IOCTL_OLS_READ_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x833,
61 IOControlCode.Access.Read),
62 IOCTL_OLS_WRITE_IO_PORT_BYTE = new IOControlCode(OLS_TYPE, 0x836,
63 IOControlCode.Access.Write),
64 IOCTL_OLS_READ_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x851,
65 IOControlCode.Access.Read),
66 IOCTL_OLS_WRITE_PCI_CONFIG = new IOControlCode(OLS_TYPE, 0x852,
67 IOControlCode.Access.Write);
69 private static bool ExtractDriver(string fileName) {
70 string resourceName = "OpenHardwareMonitor.Hardware." +
71 (IntPtr.Size == 4 ? "WinRing0.sys" : "WinRing0x64.sys");
74 Assembly.GetExecutingAssembly().GetManifestResourceNames();
76 for (int i = 0; i < names.Length; i++) {
77 if (names[i].Replace('\\', '.') == resourceName) {
78 using (Stream stream = Assembly.GetExecutingAssembly().
79 GetManifestResourceStream(names[i]))
81 buffer = new byte[stream.Length];
82 stream.Read(buffer, 0, buffer.Length);
90 using (FileStream target = new FileStream(fileName, FileMode.Create)) {
91 target.Write(buffer, 0, buffer.Length);
97 public static void Open() {
98 // No implementation for Unix systems
99 int p = (int)Environment.OSVersion.Platform;
100 if ((p == 4) || (p == 128))
106 driver = new KernelDriver("WinRing0_1_2_0");
109 if (!driver.IsOpen) {
110 string fileName = Path.GetTempFileName();
111 if (ExtractDriver(fileName)) {
113 driver.Install(fileName);
114 File.Delete(fileName);
126 isaBusMutex = new Mutex(false, "Access_ISABUS.HTP.Method");
129 public static bool IsOpen {
130 get { return driver != null; }
133 public static void Close() {
138 driver.DeviceIOControl(IOCTL_OLS_GET_REFCOUNT, null, ref refCount);
150 public static bool WaitIsaBusMutex(int millisecondsTimeout) {
152 return isaBusMutex.WaitOne(millisecondsTimeout, false);
153 } catch (AbandonedMutexException) { return false; }
154 catch (InvalidOperationException) { return false; }
157 public static void ReleaseIsaBusMutex() {
158 isaBusMutex.ReleaseMutex();
161 public static bool Rdmsr(uint index, out uint eax, out uint edx) {
162 if (driver == null) {
169 bool result = driver.DeviceIOControl(IOCTL_OLS_READ_MSR, index,
172 edx = (uint)((buffer >> 32) & 0xFFFFFFFF);
173 eax = (uint)(buffer & 0xFFFFFFFF);
177 public static bool RdmsrTx(uint index, out uint eax, out uint edx,
178 ulong threadAffinityMask)
180 ulong mask = ThreadAffinity.Set(threadAffinityMask);
182 bool result = Rdmsr(index, out eax, out edx);
184 ThreadAffinity.Set(mask);
188 [StructLayout(LayoutKind.Sequential, Pack = 1)]
189 private struct WrmsrInput {
190 public uint Register;
194 public static bool Wrmsr(uint index, uint eax, uint edx) {
198 WrmsrInput input = new WrmsrInput();
199 input.Register = index;
200 input.Value = ((ulong)edx << 32) | eax;
202 return driver.DeviceIOControl(IOCTL_OLS_WRITE_MSR, input);
205 public static byte ReadIoPort(uint port) {
210 driver.DeviceIOControl(IOCTL_OLS_READ_IO_PORT_BYTE, port, ref value);
212 return (byte)(value & 0xFF);
215 [StructLayout(LayoutKind.Sequential, Pack = 1)]
216 private struct WriteIoPortInput {
217 public uint PortNumber;
221 public static void WriteIoPort(uint port, byte value) {
225 WriteIoPortInput input = new WriteIoPortInput();
226 input.PortNumber = port;
229 driver.DeviceIOControl(IOCTL_OLS_WRITE_IO_PORT_BYTE, input);
232 public const uint InvalidPciAddress = 0xFFFFFFFF;
234 public static uint GetPciAddress(byte bus, byte device, byte function) {
236 (uint)(((bus & 0xFF) << 8) | ((device & 0x1F) << 3) | (function & 7));
239 [StructLayout(LayoutKind.Sequential, Pack = 1)]
240 private struct ReadPciConfigInput {
241 public uint PciAddress;
242 public uint RegAddress;
245 public static bool ReadPciConfig(uint pciAddress, uint regAddress,
248 if (driver == null || (regAddress & 3) != 0) {
253 ReadPciConfigInput input = new ReadPciConfigInput();
254 input.PciAddress = pciAddress;
255 input.RegAddress = regAddress;
258 return driver.DeviceIOControl(IOCTL_OLS_READ_PCI_CONFIG, input,
262 [StructLayout(LayoutKind.Sequential, Pack = 1)]
263 private struct WritePciConfigInput {
264 public uint PciAddress;
265 public uint RegAddress;
269 public static bool WritePciConfig(uint pciAddress, uint regAddress,
272 if (driver == null || (regAddress & 3) != 0)
275 WritePciConfigInput input = new WritePciConfigInput();
276 input.PciAddress = pciAddress;
277 input.RegAddress = regAddress;
280 return driver.DeviceIOControl(IOCTL_OLS_WRITE_PCI_CONFIG, input);