-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsysdep-win32.cpp
218 lines (195 loc) · 5.41 KB
/
sysdep-win32.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
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
#include <windows.h>
#include <stdio.h>
#include "OlsApi.h"
#include "OlsDef.h"
#include "sysdep.h"
bool initializeCore(void)
{
int dllStatus;
BYTE verMajor,verMinor,verRevision,verRelease;
InitializeOls ();
dllStatus=GetDllStatus ();
if (dllStatus!=0) {
printf ("Unable to initialize WinRing0 library\n");
switch (dllStatus) {
case OLS_DLL_UNSUPPORTED_PLATFORM:
printf ("Error: unsupported platform\n");
break;
case OLS_DLL_DRIVER_NOT_LOADED:
printf ("Error: driver not loaded\n");
break;
case OLS_DLL_DRIVER_NOT_FOUND:
printf ("Error: driver not found\n");
break;
case OLS_DLL_DRIVER_UNLOADED:
printf ("Error: driver unloaded by other process\n");
break;
case OLS_DLL_DRIVER_NOT_LOADED_ON_NETWORK:
printf ("Error: driver not loaded from network\n");
break;
case OLS_DLL_UNKNOWN_ERROR:
printf ("Error: unknown error\n");
break;
default:
printf ("Error: unknown error\n");
}
return false;
}
GetDriverVersion (&verMajor,&verMinor,&verRevision,&verRelease);
if ((verMajor>=1) && (verMinor>=2)) return true;
return false;
}
bool deinitializeCore(void)
{
DeinitializeOls();
return true;
}
void ClearScreen(unsigned int flags)
{
HANDLE h;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD len;
DWORD dummy;
COORD corner = { 0, 0 };
COORD now;
h = GetStdHandle(STD_OUTPUT_HANDLE);
if (h == INVALID_HANDLE_VALUE) {
return;
}
if (!GetConsoleScreenBufferInfo(h, &csbi)) {
return;
}
if (flags & CLEARSCREEN_FLAG_SMART) {
len = csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X;
} else {
len = csbi.dwSize.X * csbi.dwSize.Y;
}
if (!FillConsoleOutputCharacter(h, (TCHAR) ' ', len, corner, &dummy)) {
return;
}
if (!FillConsoleOutputAttribute(h, csbi.wAttributes, len, corner, &dummy)) {
return;
}
SetConsoleCursorPosition(h, corner);
}
//
// Tests if special PCI Extended Configuration Space access is required.
//
// This is true on Windows XP/2003 and configuration space offsets >= 0x100
// as these operating systems do not provide appropriate API.
//
// More info:
// http://www.osronline.com/showThread.cfm?link=87866
// https://code.google.com/p/turionpowercontrol/issues/detail?id=28
//
bool SpecialEcsAccessRequired(DWORD reg)
{
OSVERSIONINFO versioninfo;
if (reg < 0x100)
return false;
versioninfo.dwOSVersionInfoSize = sizeof(versioninfo);
if (!GetVersionEx(&versioninfo)) {
return false;
}
if (versioninfo.dwMajorVersion >= 6) {
return false;
}
return true;
}
//
// Performs special Extended Configuration Space access.
//
// It takes advantage of AMD-specific IO CF8h/CFCh extension that enables
// accesses to PCI Extended Configuration Space. Access is realized by
// enabling ECS access via IOCF8/IOCFC using EnableCf8ExtCfg from NB_CFG
// MSR (MSRC001_001F) and performing CF8h/CFCh access per specifciation.
// State of NB_CFG and IOCF8 is saved before the operation and gets
// restored afterwards. Calling process is also bound to first CPU
// in the system for the duration of the operation to accommodate
// environments with multiple northbridges.
//
// EnableCf8ExtCfg is supported by all currently supported CPU families,
// that is, 10h, 11h, 12h, 14h, 15h and 16h.
//
// IO CF8h/CFCh method, while racy, is the only feasible method to access
// PCI ECS on Windows XP/2003 (accessing memory mapped configuration space
// is off the table due to lack of physical memory access support
// in WinRing0).
//
// Best effort is put to detect simultaneous users of CF8h/CFCh.
//
bool SpecialEcsAccess(bool write, DWORD devfunc, DWORD reg, DWORD *res)
{
DWORD_PTR mask_save;
DWORD_PTR dummy;
DWORD eax_save;
DWORD edx_save;
DWORD edx;
DWORD addr;
DWORD addr_save;
DWORD addr_check;
DWORD data;
bool result = false;
if (!GetProcessAffinityMask((HANDLE)-1, &mask_save, &dummy)) {
fprintf(stderr, "ERROR getting affinity mask\n");
goto out;
}
if (!SetProcessAffinityMask((HANDLE)-1, 1)) {
fprintf(stderr, "ERROR setting affinity mask\n");
goto out;
}
if (!RdmsrPx(0xC001001F, &eax_save, &edx_save, 1)) {
fprintf(stderr, "ERROR reading NB_CFG\n");
goto out_affinity;
}
edx = edx_save;
edx |= 0x4000;
if (!WrmsrPx(0xC001001F, eax_save, edx, (DWORD_PTR)1)) {
fprintf(stderr, "ERROR writing NB_CFG\n");
goto out_affinity;
}
addr_save = ReadIoPortDword(0xcf8);
addr = 1;
addr <<= 7;
addr |= (reg >> 8) & 0x0F;
addr <<= 16;
addr |= devfunc & 0xFFFF;
addr <<= 8;
addr |= reg & 0xFF;
WriteIoPortDword(0xcf8, addr);
if (write == false) {
data = ReadIoPortDword(0xcfc);
} else {
WriteIoPortDword(0xcfc, *res);
}
addr_check = ReadIoPortDword(0xcf8);
if (addr_check != addr) {
fprintf(stderr, "ERROR: IO CF8h hijacked!\n");
goto out_nbcfg;
}
WriteIoPortDword(0xcf8, addr_save);
if (write == false) {
*res = data;
}
result = true;
out_nbcfg:
WrmsrPx(0xC001001F, eax_save, edx_save, (DWORD_PTR)1);
out_affinity:
SetProcessAffinityMask((HANDLE)-1, mask_save);
out:
return result;
}
BOOL SysReadPciConfigDwordEx(DWORD pciAddress, DWORD regAddress, PDWORD value)
{
if (SpecialEcsAccessRequired(regAddress)) {
return SpecialEcsAccess(false, pciAddress, regAddress, value);
}
return ReadPciConfigDwordEx(pciAddress, regAddress, value);
}
BOOL SysWritePciConfigDwordEx(DWORD pciAddress, DWORD regAddress, DWORD value)
{
if (SpecialEcsAccessRequired(regAddress)) {
return SpecialEcsAccess(true, pciAddress, regAddress, &value);
}
return WritePciConfigDwordEx(pciAddress, regAddress, value);
}