-
Notifications
You must be signed in to change notification settings - Fork 8
/
regaccess.cpp
285 lines (253 loc) · 8.99 KB
/
regaccess.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
281
282
283
284
285
//----------------------------------------------------------------------------
//
// Arm64 CPU system registers tools
// Copyright (c) 2023, Thierry Lelegard
// BSD-2-Clause license, see the LICENSE file.
//
// A class to access Arm64 system registers.
//
//----------------------------------------------------------------------------
#include "regaccess.h"
#include "strutils.h"
#include <cstddef>
#include <cstdlib>
#if defined(__linux__)
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#elif defined(__APPLE__)
#include <unistd.h>
#include <sys/socket.h>
#include <sys/sys_domain.h>
#include <sys/kern_control.h>
#include <sys/ioctl.h>
#endif
//----------------------------------------------------------------------------
// Constructor and destructor.
//----------------------------------------------------------------------------
RegAccess::RegAccess(bool print_errors, bool exit_on_open_error) :
_fd(CSR_INVALID_SYSHANDLE),
_print_errors(print_errors),
_error(CSR_SUCCESS),
_error_ref()
{
#if defined(__linux__)
// Open the pseudo-device for the kernel module.
if ((_fd = ::open(CSR_DEVICE_PATH, O_RDONLY)) < 0) {
setError(errno, "Error opening " CSR_DEVICE_PATH ", kernel module probably not loaded", false, exit_on_open_error);
return;
}
#elif defined(__APPLE__)
// We use a system socket to communicate with the kernel extension.
if ((_fd = ::socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL)) < 0) {
setError(errno, "socket()", false, exit_on_open_error);
return;
}
// Get the control id of the cpusysregs kernel extension.
struct ctl_info info;
Zero(&info, sizeof(info));
::strncpy(info.ctl_name, CSR_SOCKET_NAME, sizeof(info.ctl_name));
if (::ioctl(_fd, CTLIOCGINFO, &info) < 0) {
setError(errno, "Error accessing system socket " CSR_SOCKET_NAME ", kernel extension probably not loaded", true, exit_on_open_error);
return;
}
// Connect to the kernel extension using its id.
struct sockaddr_ctl addr;
Zero(&addr, sizeof(addr));
addr.sc_len = sizeof(addr);
addr.sc_family = AF_SYSTEM;
addr.ss_sysaddr = AF_SYS_CONTROL;
addr.sc_id = info.ctl_id;
if (::connect(_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
setError(errno, "connect()", true, exit_on_open_error);
return;
}
#elif defined(WINDOWS)
// Open the pseudo-device for the kernel driver.
_fd = ::CreateFileA(CSR_DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (_fd == INVALID_HANDLE_VALUE) {
setError(::GetLastError(), "Error opening " CSR_DEVICE_NAME ", kernel driver probably not loaded", false, exit_on_open_error);
return;
}
#endif
}
//----------------------------------------------------------------------------
// Close the kernel module, check if open.
//----------------------------------------------------------------------------
bool RegAccess::isOpen() const
{
return _fd != CSR_INVALID_SYSHANDLE;
}
void RegAccess::close()
{
if (_fd != CSR_INVALID_SYSHANDLE) {
#if defined(__linux__) || defined(__APPLE__)
if (::close(_fd) < 0) {
setError(errno, "close");
}
#elif defined(WINDOWS)
if (!::CloseHandle(_fd)) {
setError(::GetLastError(), "CloseHandle");
}
#endif
}
_fd = CSR_INVALID_SYSHANDLE;
}
//----------------------------------------------------------------------------
// Error reporting.
//----------------------------------------------------------------------------
void RegAccess::printLastError(const std::string& label, std::ostream& file) const
{
if (_error != CSR_SUCCESS) {
if (!label.empty()) {
file << label << ": ";
}
if (!_error_ref.empty()) {
file << _error_ref << ": ";
}
file << Error(_error) << std::endl;
}
}
bool RegAccess::setError(SysError code, const std::string& ref, bool close_fd, bool exit_on_error)
{
_error = code;
_error_ref = ref;
if (close_fd) {
close();
}
if (_print_errors || exit_on_error) {
printLastError();
}
if (exit_on_error) {
::exit(EXIT_FAILURE);
}
return false;
}
//----------------------------------------------------------------------------
// Read CPU registers.
//----------------------------------------------------------------------------
bool RegAccess::read(int regid, csr_u64_t& reg)
{
if (!csr_regid_is_single(regid)) {
return setError(EINVAL, "invalid register id");
}
#if defined(__linux__)
if (::ioctl(_fd, CSR_IOC_GET_REG(regid), ®) < 0) {
return setError(errno, "ioctl(GET_REG)");
}
#elif defined(__APPLE__)
::socklen_t len = sizeof(reg);
if (::getsockopt(_fd, SYSPROTO_CONTROL, CSR_SOCKOPT_REG(regid), ®, &len) < 0) {
return setError(errno, "getsockopt(GET_REG)");
}
#elif defined(WINDOWS)
::ULONG retsize = 0;
if (!::DeviceIoControl(_fd, CSR_IOC_GET_REG(regid), nullptr, 0, ®, sizeof(reg), &retsize, nullptr)) {
return setError(::GetLastError(), "DeviceIoControl(GET_REG)");
}
if (retsize < sizeof(reg)) {
return setError(ERROR_INVALID_DATA, Format("DeviceIoControl(GET_REG) returned size too short: ", retsize));
}
#endif
return true;
}
bool RegAccess::read(int regid, csr_pair_t& reg)
{
if (csr_regid_is_single(regid)) {
reg.high = 0;
return read(regid, reg.low);
}
if (!csr_regid_is_pair(regid)) {
return setError(EINVAL, "invalid register pair id");
}
#if defined(__linux__)
if (::ioctl(_fd, CSR_IOC_GET_REG2(regid), ®) < 0) {
return setError(errno, "ioctl(GET_REG2)");
}
#elif defined(__APPLE__)
::socklen_t len = sizeof(reg);
if (::getsockopt(_fd, SYSPROTO_CONTROL, CSR_SOCKOPT_REG(regid), ®, &len) < 0) {
return setError(errno, "getsockopt(GET_REG2)");
}
#elif defined(WINDOWS)
::ULONG retsize = 0;
if (!::DeviceIoControl(_fd, CSR_IOC_GET_REG(regid), nullptr, 0, ®, sizeof(reg), &retsize, nullptr)) {
return setError(::GetLastError(), "DeviceIoControl(GET_REG)");
}
if (retsize < sizeof(reg)) {
return setError(ERROR_INVALID_DATA, Format("DeviceIoControl(GET_REG) returned size too short: ", retsize));
}
#endif
return true;
}
//----------------------------------------------------------------------------
// Write CPU registers.
//----------------------------------------------------------------------------
bool RegAccess::write(int regid, csr_u64_t reg)
{
if (!csr_regid_is_single(regid)) {
return setError(EINVAL, "invalid register id");
}
#if defined(__linux__)
if (::ioctl(_fd, CSR_IOC_SET_REG(regid), ®) < 0) {
return setError(errno, "ioctl(SET_REG)");
}
#elif defined(__APPLE__)
if (::setsockopt(_fd, SYSPROTO_CONTROL, CSR_SOCKOPT_REG(regid), ®, sizeof(reg)) < 0) {
return setError(errno, "setsockopt(SET_REG)");
}
#elif defined(WINDOWS)
if (!::DeviceIoControl(_fd, CSR_IOC_SET_REG(regid), const_cast<csr_u64_t*>(®), sizeof(reg), nullptr, 0, nullptr, nullptr)) {
return setError(::GetLastError(), "DeviceIoControl(SET_REG)");
}
#endif
return true;
}
bool RegAccess::write(int regid, const csr_pair_t& reg)
{
if (csr_regid_is_single(regid)) {
return write(regid, reg.low);
}
if (!csr_regid_is_pair(regid)) {
return setError(EINVAL, "invalid register pair id");
}
#if defined(__linux__)
if (::ioctl(_fd, CSR_IOC_SET_REG2(regid), ®) < 0) {
return setError(errno, "ioctl(SET_REG2)");
}
#elif defined(__APPLE__)
if (::setsockopt(_fd, SYSPROTO_CONTROL, CSR_SOCKOPT_REG(regid), ®, sizeof(reg)) < 0) {
return setError(errno, "setsockopt(SET_REG2)");
}
#elif defined(WINDOWS)
if (!::DeviceIoControl(_fd, CSR_IOC_SET_REG(regid), const_cast<csr_pair_t*>(®), sizeof(reg), nullptr, 0, nullptr, nullptr)) {
return setError(::GetLastError(), "DeviceIoControl(SET_REG)");
}
#endif
return true;
}
//----------------------------------------------------------------------------
// Execute a PACxx or AUTxx in kernel mode.
//----------------------------------------------------------------------------
bool RegAccess::executeInstr(int instr, csr_instr_t& args)
{
#if defined(__linux__)
if (::ioctl(_fd, CSR_IOC_INSTR(instr), &args) < 0) {
return setError(errno, "ioctl(INSTR)");
}
#elif defined(__APPLE__)
::socklen_t len = sizeof(args);
if (::getsockopt(_fd, SYSPROTO_CONTROL, CSR_SOCKOPT_INSTR(instr), &args, &len) < 0) {
return setError(errno, "getsockopt(INSTR)");
}
#elif defined(WINDOWS)
::ULONG retsize = 0;
if (!::DeviceIoControl(_fd, CSR_IOC_INSTR(instr), &args, sizeof(args), &args, sizeof(args), &retsize, nullptr)) {
return setError(::GetLastError(), "DeviceIoControl(INSTR)");
}
if (retsize < sizeof(args)) {
return setError(ERROR_INVALID_DATA, Format("DeviceIoControl(INSTR) returned size too short: ", retsize));
}
#endif
return true;
}