forked from sweoggy/AmdMsrTweaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinRing0.cpp
101 lines (78 loc) · 2.13 KB
/
WinRing0.cpp
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
/*
* Copyright (c) Martin Kinkelin
*
* See the "License.txt" file in the root directory for infos
* about permitted and prohibited uses of this code.
*/
#pragma comment(lib, "WinRing0/WinRing0.lib")
#pragma comment(lib, "WinRing0/WinRing0x64.lib")
#include "WinRing0.h"
#include "StringUtils.h"
using std::exception;
using std::string;
DWORD ReadPciConfig(DWORD device, DWORD function, DWORD regAddress)
{
const DWORD pciAddress = ((device & 0x1f) << 3) | (function & 0x7);
DWORD result;
if (!ReadPciConfigDwordEx(pciAddress, regAddress, &result))
{
string msg = "cannot read from PCI configuration space (F";
msg += StringUtils::ToString(function);
msg += "x";
msg += StringUtils::ToHexString(regAddress);
msg += ")";
throw exception(msg.c_str());
}
return result;
}
void WritePciConfig(DWORD device, DWORD function, DWORD regAddress, DWORD value)
{
const DWORD pciAddress = ((device & 0x1f) << 3) | (function & 0x7);
if (!WritePciConfigDwordEx(pciAddress, regAddress, value))
{
string msg = "cannot write to PCI configuration space (F";
msg += StringUtils::ToString(function);
msg += "x";
msg += StringUtils::ToHexString(regAddress);
msg += ")";
throw exception(msg.c_str());
}
}
QWORD Rdmsr(DWORD index)
{
QWORD result;
PDWORD eax = (PDWORD)&result;
PDWORD edx = eax + 1;
if (!Rdmsr(index, eax, edx))
{
string msg = "cannot read from MSR (0x";
msg += StringUtils::ToHexString(index);
msg += ")";
throw exception(msg.c_str());
}
return result;
}
void Wrmsr(DWORD index, const QWORD& value)
{
PDWORD eax = (PDWORD)&value;
PDWORD edx = eax + 1;
if (!Wrmsr(index, *eax, *edx))
{
string msg = "cannot write to MSR (0x";
msg += StringUtils::ToHexString(index);
msg += ")";
throw exception(msg.c_str());
}
}
CpuidRegs Cpuid(DWORD index)
{
CpuidRegs result;
if (!Cpuid(index, &result.eax, &result.ebx, &result.ecx, &result.edx))
{
string msg = "cannot execute CPUID instruction (0x";
msg += StringUtils::ToHexString(index);
msg += ")";
throw exception(msg.c_str());
}
return result;
}