-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmsr.cpp
More file actions
386 lines (330 loc) · 9.49 KB
/
msr.cpp
File metadata and controls
386 lines (330 loc) · 9.49 KB
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
#include <string.h>
#include "uint256.h"
#include "RandomX/src/randomx.h"
#if !defined(_MSC_VER)
#include <sched.h>
#include <unistd.h>
#endif
#if defined(__GNUC__)
#include <stdint.h>
#elif defined(_MSC_VER)
#include <intrin.h>
#include <algorithm>
#include <iostream>
#include <thread>
#endif
#if defined(__cplusplus)
extern "C"
{
#include "miner.h"
}
#endif
using namespace std;
#define MAX_INTEL_TOP_LVL 4
#define SERVICE_NAME L"WinRing0_1_2_0"
#define IOCTL_WRITE_MSR CTL_CODE(40000, 0x822, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define b(val, base, end) ((val << (32-end-1)) >> (32-end+base-1))
class CPUID {
uint32_t regs[4];
public:
explicit CPUID(unsigned funcId, unsigned subFuncId) {
#ifdef _MSC_VER
__cpuidex((int*)regs, (int)funcId, (int)subFuncId);
#else
asm volatile
("cpuid" : "=a" (regs[0]), "=b" (regs[1]), "=c" (regs[2]), "=d" (regs[3])
: "a" (funcId), "c" (subFuncId));
// ECX is set to zero for CPUID function 4
#endif
}
const uint32_t& EAX() const { return regs[0]; }
const uint32_t& EBX() const { return regs[1]; }
const uint32_t& ECX() const { return regs[2]; }
const uint32_t& EDX() const { return regs[3]; }
};
class CPUInfo {
public:
CPUInfo();
string vendor() const { return mVendorId; }
string model() const { return mModelName; }
int cores() const { return mNumCores; }
float cpuSpeedInMHz() const { return mCPUMHz; }
bool isSSE() const { return mIsSSE; }
bool isSSE2() const { return mIsSSE2; }
bool isSSE3() const { return mIsSSE3; }
bool isSSE41() const { return mIsSSE41; }
bool isSSE42() const { return mIsSSE42; }
bool isAVX() const { return mIsAVX; }
bool isAVX2() const { return mIsAVX2; }
bool isHyperThreaded() const { return mIsHTT; }
bool isIntel() const { return boolIntel; }
bool isAMD() const { return boolAMD; }
int logicalCpus() const { return mNumLogCpus; }
private:
// Bit positions for data extractions
static const uint32_t SSE_POS = 0x02000000;
static const uint32_t SSE2_POS = 0x04000000;
static const uint32_t SSE3_POS = 0x00000001;
static const uint32_t SSE41_POS = 0x00080000;
static const uint32_t SSE42_POS = 0x00100000;
static const uint32_t AVX_POS = 0x10000000;
static const uint32_t AVX2_POS = 0x00000020;
static const uint32_t LVL_NUM = 0x000000FF;
static const uint32_t LVL_TYPE = 0x0000FF00;
static const uint32_t LVL_CORES = 0x0000FFFF;
// Attributes
string mVendorId;
string mModelName;
int mNumSMT;
int mNumCores;
int mNumLogCpus;
float mCPUMHz;
bool boolIntel;
bool boolAMD;
bool mIsHTT;
bool mIsSSE;
bool mIsSSE2;
bool mIsSSE3;
bool mIsSSE41;
bool mIsSSE42;
bool mIsAVX;
bool mIsAVX2;
};
CPUInfo::CPUInfo()
{
// Get vendor name EAX=0
CPUID cpuID0(0, 0);
uint32_t HFS = cpuID0.EAX();
mVendorId += string((const char*)&cpuID0.EBX(), 4);
mVendorId += string((const char*)&cpuID0.EDX(), 4);
mVendorId += string((const char*)&cpuID0.ECX(), 4);
// Get SSE instructions availability
CPUID cpuID1(1, 0);
mIsHTT = cpuID1.EDX() & AVX_POS;
mIsSSE = cpuID1.EDX() & SSE_POS;
mIsSSE2 = cpuID1.EDX() & SSE2_POS;
mIsSSE3 = cpuID1.ECX() & SSE3_POS;
mIsSSE41 = cpuID1.ECX() & SSE41_POS;
mIsSSE42 = cpuID1.ECX() & SSE41_POS;
mIsAVX = cpuID1.ECX() & AVX_POS;
// Get AVX2 instructions availability
CPUID cpuID7(7, 0);
mIsAVX2 = cpuID7.EBX() & AVX2_POS;
string upVId = mVendorId;
for_each(upVId.begin(), upVId.end(), [](char& in) { in = ::toupper(in); });
// Get num of cores
if (upVId.find("INTEL") != std::string::npos) {
boolIntel = true;
boolAMD = false;
if (HFS >= 11) {
for (int lvl = 0; lvl < MAX_INTEL_TOP_LVL; ++lvl) {
CPUID cpuID4(0x0B, lvl);
uint32_t currLevel = (LVL_TYPE & cpuID4.ECX()) >> 8;
switch (currLevel) {
case 0x01: mNumSMT = LVL_CORES & cpuID4.EBX(); break;
case 0x02: mNumLogCpus = LVL_CORES & cpuID4.EBX(); break;
default: break;
}
}
mNumCores = mNumLogCpus / mNumSMT;
}
else {
if (HFS >= 1) {
mNumLogCpus = (cpuID1.EBX() >> 16) & 0xFF;
if (HFS >= 4) {
mNumCores = 1 + (CPUID(4, 0).EAX() >> 26) & 0x3F;
}
}
if (mIsHTT) {
if (!(mNumCores > 1)) {
mNumCores = 1;
mNumLogCpus = (mNumLogCpus >= 2 ? mNumLogCpus : 2);
}
}
else {
mNumCores = mNumLogCpus = 1;
}
}
}
else if (upVId.find("AMD") != std::string::npos) {
boolIntel = false;
boolAMD = true;
if (HFS >= 1) {
mNumLogCpus = (cpuID1.EBX() >> 16) & 0xFF;
if (CPUID(0x80000000, 0).EAX() >= 8) {
mNumCores = 1 + (CPUID(0x80000008, 0).ECX() & 0xFF);
}
}
if (mIsHTT) {
if (!(mNumCores > 1)) {
mNumCores = 1;
mNumLogCpus = (mNumLogCpus >= 2 ? mNumLogCpus : 2);
}
}
else {
mNumCores = mNumLogCpus = 1;
}
}
else {
cout << "Unexpected vendor id" << endl;
boolIntel = false;
boolAMD = false;
}
// Get processor brand string
// This seems to be working for both Intel & AMD vendors
for (int i = 0x80000002; i < 0x80000005; ++i) {
CPUID cpuID(i, 0);
mModelName += string((const char*)&cpuID.EAX(), 4);
mModelName += string((const char*)&cpuID.EBX(), 4);
mModelName += string((const char*)&cpuID.ECX(), 4);
mModelName += string((const char*)&cpuID.EDX(), 4);
}
}
static int wrmsr_uninstall_driver()
{
if (!hService) {
return true;
}
bool result = true;
SERVICE_STATUS serviceStatus;
if (!ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus)) {
printf("failed to stop WinRing0 driver, error %u\n", GetLastError());
result = false;
}
if (!DeleteService(hService)) {
printf("failed to remove WinRing0 driver, error %u\n", GetLastError());
result = false;
}
CloseServiceHandle(hService);
hService = nullptr;
return result;
}
static HANDLE wrmsr_install_driver()
{
DWORD err = 0;
hManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS);
if (!hManager) {
err = GetLastError();
if (err == ERROR_ACCESS_DENIED) {
printf("to write MSR registers Administrator privileges required.\n");
}
else {
printf("failed to open service control manager, error %u\n", err);
}
return nullptr;
}
std::vector<wchar_t> dir;
dir.resize(MAX_PATH);
do {
dir.resize(dir.size() * 2);
GetModuleFileNameW(nullptr, dir.data(), dir.size());
err = GetLastError();
} while (err == ERROR_INSUFFICIENT_BUFFER);
if (err != ERROR_SUCCESS) {
printf("failed to get path to driver, error %u\n", err);
return nullptr;
}
for (auto it = dir.end(); it != dir.begin(); --it) {
if ((*it == L'\\') || (*it == L'/')) {
++it;
*it = L'\0';
break;
}
}
std::wstring driverPath = dir.data();
driverPath += L"WinRing0x64.sys";
hService = OpenServiceW(hManager, SERVICE_NAME, SERVICE_ALL_ACCESS);
if (hService && !wrmsr_uninstall_driver()) {
return nullptr;
}
hService = CreateServiceW(hManager, SERVICE_NAME, SERVICE_NAME, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, driverPath.c_str(), nullptr, nullptr, nullptr, nullptr, nullptr);
if (!hService) {
printf("failed to install WinRing0 driver, error %u", GetLastError());
return nullptr;
}
if (!StartService(hService, 0, nullptr)) {
err = GetLastError();
if (err != ERROR_SERVICE_ALREADY_RUNNING) {
printf("failed to start WinRing0 driver, error %u", err);
CloseServiceHandle(hService);
hService = nullptr;
return nullptr;
}
}
HANDLE hDriver = CreateFileW(L"\\\\.\\" SERVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (!hDriver) {
printf("failed to connect to WinRing0 driver, error %u\n", GetLastError());
return nullptr;
}
return hDriver;
}
static bool wrmsr(HANDLE hDriver, uint32_t reg, uint64_t value) {
struct {
uint32_t reg = 0;
uint32_t value[2]{};
} input;
static_assert(sizeof(input) == 12, "Invalid struct size for WinRing0 driver");
input.reg = reg;
*((uint64_t*)input.value) = value;
DWORD output;
DWORD k;
if (!DeviceIoControl(hDriver, IOCTL_WRITE_MSR, &input, sizeof(input), &output, sizeof(output), &k, nullptr)) {
printf("cannot set MSR 0x%08 to 0x%08", reg, value);
return false;
}
return true;
}
void MSRStart()
{
hDriver = wrmsr_install_driver();
if (!hDriver) {
printf("can't find driver\n");
wrmsr_uninstall_driver();
if (hManager) {
CloseServiceHandle(hManager);
}
return;
}
// return hDriver;
}
void MSRSTOP()
{
CloseHandle(hDriver);
wrmsr_uninstall_driver();
CloseServiceHandle(hManager);
}
void MSRInit()
{
CPUInfo cinfo;
CPUID cpuID(1, 0);
// cout << "mining on " << cinfo.model() << endl;
// printf("Again Family %ld\n", b(cpuID.EAX(), 8, 11) + b(cpuID.EAX(), 20, 27));
// cout << "isIntel " << cinfo.isIntel() << " isAMD " << cinfo.isAMD() << endl;
if (!cinfo.isIntel() && cinfo.isAMD())
{
int FamilyNumber = b(cpuID.EAX(), 8, 11) + b(cpuID.EAX(), 20, 27);
if (FamilyNumber >= 23 && FamilyNumber <= 24) // zen, zen+, zen2, epyc (Hygon Dhyana)
{
wrmsr(hDriver, 0xC0011020, 0);
wrmsr(hDriver, 0xC0011021, 0x40);
wrmsr(hDriver, 0xC0011022, 0x1510000);
wrmsr(hDriver, 0xC001102b, 0x2000cc16);
}
else if (FamilyNumber == 25) // zen3
{
wrmsr(hDriver, 0xC0011020, 0x4480000000000);
wrmsr(hDriver, 0xC0011021, 0x1c000200000040);
wrmsr(hDriver, 0xC0011022, 0xc000000401500000);
wrmsr(hDriver, 0xC001102b, 0x2000cc14);
}
// printf("this is AMD family %d", b(cpuID.EAX(), 8, 11) + b(cpuID.EAX(), 20, 27));
}
else if (cinfo.isIntel() && !cinfo.isAMD())
{
wrmsr(hDriver, 0x1a4, 0xf);
}
else
{
printf("unknown cpu vendor\n");
}
}