-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathmsr.cpp
277 lines (229 loc) · 6.2 KB
/
msr.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
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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2009-2022, Intel Corporation
// written by Roman Dementiev
// Austen Ott
// Jim Harris (FreeBSD)
#include <sys/types.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef _MSC_VER
#include <unistd.h>
#endif
#include "types.h"
#include "msr.h"
#include "utils.h"
#include <assert.h>
#ifdef _MSC_VER
#include <windows.h>
#include "utils.h"
#include "Winmsrdriver\msrstruct.h"
#include "winring0/OlsApiInitExt.h"
#endif
#if defined(__FreeBSD__) || defined(__DragonFly__)
#include <sys/ioccom.h>
#include <sys/cpuctl.h>
#endif
#include <mutex>
namespace pcm {
#ifdef _MSC_VER
extern HMODULE hOpenLibSys;
// here comes an implementation for Windows
MsrHandle::MsrHandle(uint32 cpu) : cpu_id(cpu)
{
hDriver = openMSRDriver();
if (hDriver == INVALID_HANDLE_VALUE && hOpenLibSys == NULL)
throw std::exception();
}
MsrHandle::~MsrHandle()
{
if (hDriver != INVALID_HANDLE_VALUE) CloseHandle(hDriver);
}
int32 MsrHandle::write(uint64 msr_number, uint64 value)
{
if (hDriver != INVALID_HANDLE_VALUE)
{
MSR_Request req;
ULONG64 result;
DWORD reslength = 0;
req.core_id = cpu_id;
req.msr_address = msr_number;
req.write_value = value;
BOOL status = DeviceIoControl(hDriver, IO_CTL_MSR_WRITE, &req, sizeof(MSR_Request), &result, sizeof(uint64), &reslength, NULL);
assert(status && "Error in DeviceIoControl");
return status ? sizeof(uint64) : 0;
}
cvt_ds cvt;
cvt.ui64 = value;
ThreadGroupTempAffinity affinity(cpu_id);
DWORD status = Wrmsr((DWORD)msr_number, cvt.ui32.low, cvt.ui32.high);
return status ? sizeof(uint64) : 0;
}
int32 MsrHandle::read(uint64 msr_number, uint64 * value)
{
if (hDriver != INVALID_HANDLE_VALUE)
{
MSR_Request req;
// ULONG64 result;
DWORD reslength = 0;
req.core_id = cpu_id;
req.msr_address = msr_number;
BOOL status = DeviceIoControl(hDriver, IO_CTL_MSR_READ, &req, sizeof(MSR_Request), value, sizeof(uint64), &reslength, NULL);
assert(status && "Error in DeviceIoControl");
return (int32)reslength;
}
cvt_ds cvt;
cvt.ui64 = 0;
ThreadGroupTempAffinity affinity(cpu_id);
DWORD status = Rdmsr((DWORD)msr_number, &(cvt.ui32.low), &(cvt.ui32.high));
if (status) *value = cvt.ui64;
return status ? sizeof(uint64) : 0;
}
#elif __APPLE__
// OSX Version
MSRAccessor * MsrHandle::driver = NULL;
int MsrHandle::num_handles = 0;
MsrHandle::MsrHandle(uint32 cpu)
{
cpu_id = cpu;
if (!driver)
{
driver = new MSRAccessor();
MsrHandle::num_handles = 1;
}
else
{
MsrHandle::num_handles++;
}
}
MsrHandle::~MsrHandle()
{
MsrHandle::num_handles--;
if (MsrHandle::num_handles == 0)
{
deleteAndNullify(driver);
}
}
int32 MsrHandle::write(uint64 msr_number, uint64 value)
{
return driver->write(cpu_id, msr_number, value);
}
int32 MsrHandle::read(uint64 msr_number, uint64 * value)
{
return driver->read(cpu_id, msr_number, value);
}
int32 MsrHandle::buildTopology(uint32 num_cores, void * ptr)
{
return driver->buildTopology(num_cores, ptr);
}
uint32 MsrHandle::getNumInstances()
{
return driver->getNumInstances();
}
uint32 MsrHandle::incrementNumInstances()
{
return driver->incrementNumInstances();
}
uint32 MsrHandle::decrementNumInstances()
{
return driver->decrementNumInstances();
}
#elif defined(__FreeBSD__) || defined(__DragonFly__)
MsrHandle::MsrHandle(uint32 cpu) : fd(-1), cpu_id(cpu)
{
char path[200];
snprintf(path, 200, "/dev/cpuctl%d", cpu);
int handle = ::open(path, O_RDWR);
if (handle < 0) throw std::exception();
fd = handle;
}
MsrHandle::~MsrHandle()
{
if (fd >= 0) ::close(fd);
}
int32 MsrHandle::write(uint64 msr_number, uint64 value)
{
cpuctl_msr_args_t args;
int ret;
args.msr = msr_number;
args.data = value;
ret = ::ioctl(fd, CPUCTL_WRMSR, &args);
if (ret) return ret;
return sizeof(value);
}
int32 MsrHandle::read(uint64 msr_number, uint64 * value)
{
cpuctl_msr_args_t args;
int32 ret;
args.msr = msr_number;
ret = ::ioctl(fd, CPUCTL_RDMSR, &args);
if (ret) return ret;
*value = args.data;
return sizeof(*value);
}
#else
// here comes a Linux version
bool noMSRMode()
{
static int noMSR = -1;
if (noMSR < 0)
{
noMSR = (safe_getenv("PCM_NO_MSR") == std::string("1")) ? 1 : 0;
}
return 1 == noMSR;
}
MsrHandle::MsrHandle(uint32 cpu) : fd(-1), cpu_id(cpu)
{
if (noMSRMode()) return;
constexpr auto allowWritesPath = "/sys/module/msr/parameters/allow_writes";
static bool writesEnabled = false;
if (writesEnabled == false)
{
if (readSysFS(allowWritesPath, true).length() > 0)
{
writeSysFS(allowWritesPath, "on", false);
}
writesEnabled = true;
}
char * path = new char[200];
if (!path) throw std::runtime_error("Allocation of 200 bytes failed.");
snprintf(path, 200, "/dev/cpu/%d/msr", cpu);
int handle = ::open(path, O_RDWR);
if (handle < 0)
{ // try Android msr device path
snprintf(path, 200, "/dev/msr%d", cpu);
handle = ::open(path, O_RDWR);
}
deleteAndNullifyArray(path);
if (handle < 0)
{
std::cerr << "PCM Error: can't open MSR handle for core " << cpu << " (" << strerror(errno) << ")\n";
std::cerr << "Try no-MSR mode by setting env variable PCM_NO_MSR=1\n";
throw std::exception();
}
fd = handle;
}
MsrHandle::~MsrHandle()
{
if (fd >= 0) ::close(fd);
}
int32 MsrHandle::write(uint64 msr_number, uint64 value)
{
#if 0
static std::mutex m;
std::lock_guard<std::mutex> g(m);
std::cout << "DEBUG: writing MSR 0x" << std::hex << msr_number << " value 0x" << value << " on cpu " << std::dec << cpu_id << std::endl;
#endif
if (fd < 0) return 0;
return ::pwrite(fd, (const void *)&value, sizeof(uint64), msr_number);
}
int32 MsrHandle::read(uint64 msr_number, uint64 * value)
{
if (fd < 0) return 0;
return ::pread(fd, (void *)value, sizeof(uint64), msr_number);
}
#endif
#ifndef __linux__
bool noMSRMode() { return false; }
#endif
} // namespace pcm