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