-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmio.cpp
280 lines (242 loc) · 8.65 KB
/
mmio.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
278
279
280
/*
Copyright (c) 2009-2019, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// written by Roman Dementiev,
// Patrick Konsor
//
#include <iostream>
#include <string.h>
#ifndef _MSC_VER
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include <fcntl.h>
#include "pci.h"
#include "mmio.h"
#ifndef _MSC_VER
#include <sys/mman.h>
#include <errno.h>
#endif
#ifdef _MSC_VER
#include <windows.h>
#include "utils.h"
#endif
#include <assert.h>
namespace pcm {
#ifdef _MSC_VER
class PCMPmem : public WinPmem {
protected:
virtual int load_driver_()
{
SYSTEM_INFO sys_info;
ZeroMemory(&sys_info, sizeof(sys_info));
GetCurrentDirectory(MAX_PATH - 10, driver_filename);
GetNativeSystemInfo(&sys_info);
switch (sys_info.wProcessorArchitecture)
{
case PROCESSOR_ARCHITECTURE_AMD64:
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x64.sys");
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
{
std::cerr << "ERROR: winpmem_x64.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x64.sys .\n";
std::cerr << "ERROR: Memory bandwidth statistics will not be available.\n";
}
break;
case PROCESSOR_ARCHITECTURE_INTEL:
wcscat_s(driver_filename, MAX_PATH, L"\\winpmem_x86.sys");
if (GetFileAttributes(driver_filename) == INVALID_FILE_ATTRIBUTES)
{
std::cerr << "ERROR: winpmem_x86.sys not found in current directory. Download it from https://github.com/Velocidex/WinPmem/blob/master/kernel/binaries/winpmem_x86.sys .\n";
std::cerr << "ERROR: Memory bandwidth statistics will not be available.\n";
}
break;
default:
return -1;
}
return 1;
}
virtual int write_crashdump_header_(struct PmemMemoryInfo * info)
{
return -1;
}
};
std::shared_ptr<WinPmem> WinPmemMMIORange::pmem;
Mutex WinPmemMMIORange::mutex;
bool WinPmemMMIORange::writeSupported;
WinPmemMMIORange::WinPmemMMIORange(uint64 baseAddr_, uint64 /* size_ */, bool readonly_) : startAddr(baseAddr_), readonly(readonly_)
{
mutex.lock();
if (pmem.get() == NULL)
{
pmem = std::make_shared<PCMPmem>();
pmem->install_driver(false);
pmem->set_acquisition_mode(PMEM_MODE_IOSPACE);
writeSupported = pmem->toggle_write_mode() >= 0; // since it is a global object enable write mode just in case someone needs it
}
mutex.unlock();
}
MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_)
{
auto hDriver = openMSRDriver();
if (hDriver != INVALID_HANDLE_VALUE)
{
DWORD reslength = 0;
uint64 result = 0;
const BOOL status = DeviceIoControl(hDriver, IO_CTL_MMAP_SUPPORT, NULL, 0, &result, sizeof(uint64), &reslength, NULL);
CloseHandle(hDriver);
if (status == TRUE && reslength == sizeof(uint64) && result == 1)
{
impl = std::make_shared<OwnMMIORange>(baseAddr_, size_, readonly_);
return;
}
else
{
std::cerr << "MSR.sys does not support mmap operations\n";
}
}
impl = std::make_shared<WinPmemMMIORange>(baseAddr_, size_, readonly_);
}
OwnMMIORange::OwnMMIORange(uint64 baseAddr_, uint64 size_, bool /* readonly_ */)
{
hDriver = openMSRDriver();
MMAP_Request req{};
uint64 result = 0;
DWORD reslength = 0;
req.address.QuadPart = baseAddr_;
req.size = size_;
const BOOL status = DeviceIoControl(hDriver, IO_CTL_MMAP, &req, sizeof(MMAP_Request), &result, sizeof(uint64), &reslength, NULL);
if (status == FALSE || result == 0)
{
std::cerr << "Error mapping address 0x" << std::hex << req.address.QuadPart << " with size " << std::dec << req.size << "\n";
throw std::runtime_error("OwnMMIORange error");
}
mmapAddr = (char*)result;
}
uint32 OwnMMIORange::read32(uint64 offset)
{
return *((uint32*)(mmapAddr + offset));
}
uint64 OwnMMIORange::read64(uint64 offset)
{
return *((uint64*)(mmapAddr + offset));
}
void OwnMMIORange::write32(uint64 offset, uint32 val)
{
*((uint32*)(mmapAddr + offset)) = val;
}
void OwnMMIORange::write64(uint64 offset, uint64 val)
{
*((uint64*)(mmapAddr + offset)) = val;
}
OwnMMIORange::~OwnMMIORange()
{
MMAP_Request req{};
uint64 result = 0;
DWORD reslength = 0;
req.address.QuadPart = (LONGLONG)mmapAddr;
req.size = 0;
DeviceIoControl(hDriver, IO_CTL_MUNMAP, &req, sizeof(MMAP_Request), &result, sizeof(uint64), &reslength, NULL);
CloseHandle(hDriver);
}
#elif __APPLE__
#include "PCIDriverInterface.h"
MMIORange::MMIORange(uint64 physical_address, uint64 size_, bool readonly_) :
mmapAddr(NULL),
size(size_),
readonly(readonly_)
{
if (size > 4096)
{
std::cerr << "PCM Error: the driver does not support mapping of regions > 4KB\n";
return;
}
if (physical_address) {
PCIDriver_mapMemory((uint32_t)physical_address, (uint8_t **)&mmapAddr);
}
}
uint32 MMIORange::read32(uint64 offset)
{
uint32 val = 0;
PCIDriver_readMemory32((uint8_t *)mmapAddr + offset, &val);
return val;
}
uint64 MMIORange::read64(uint64 offset)
{
uint64 val = 0;
PCIDriver_readMemory64((uint8_t *)mmapAddr + offset, &val);
return val;
}
void MMIORange::write32(uint64 offset, uint32 val)
{
std::cerr << "PCM Error: the driver does not support writing to MMIORange\n";
}
void MMIORange::write64(uint64 offset, uint64 val)
{
std::cerr << "PCM Error: the driver does not support writing to MMIORange\n";
}
MMIORange::~MMIORange()
{
if(mmapAddr) PCIDriver_unmapMemory((uint8_t *)mmapAddr);
}
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_) :
fd(-1),
mmapAddr(NULL),
size(size_),
readonly(readonly_)
{
const int oflag = readonly ? O_RDONLY : O_RDWR;
int handle = ::open("/dev/mem", oflag);
if (handle < 0)
{
std::cerr << "opening /dev/mem failed: errno is " << errno << " (" << strerror(errno) << ")\n";
throw std::exception();
}
fd = handle;
const int prot = readonly ? PROT_READ : (PROT_READ | PROT_WRITE);
mmapAddr = (char *)mmap(NULL, size, prot, MAP_SHARED, fd, baseAddr_);
if (mmapAddr == MAP_FAILED)
{
std::cerr << "mmap failed: errno is " << errno << " (" << strerror(errno) << ")\n";
throw std::exception();
}
}
uint32 MMIORange::read32(uint64 offset)
{
return *((uint32 *)(mmapAddr + offset));
}
uint64 MMIORange::read64(uint64 offset)
{
return *((uint64 *)(mmapAddr + offset));
}
void MMIORange::write32(uint64 offset, uint32 val)
{
if (readonly)
{
std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
return;
}
*((uint32 *)(mmapAddr + offset)) = val;
}
void MMIORange::write64(uint64 offset, uint64 val)
{
if (readonly)
{
std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
return;
}
*((uint64 *)(mmapAddr + offset)) = val;
}
MMIORange::~MMIORange()
{
if (mmapAddr) munmap(mmapAddr, size);
if (fd >= 0) ::close(fd);
}
#endif
} // namespace pcm