-
Notifications
You must be signed in to change notification settings - Fork 7
/
CpuAccelerator.cpp
347 lines (288 loc) · 9.96 KB
/
CpuAccelerator.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
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
// Copyright (C) 2014 The Android Open Source Project
//
// This software is licensed under the terms of the GNU General Public
// License version 2, as published by the Free Software Foundation, and
// may be copied, distributed, and modified under those terms.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
#include "android/emulation/CpuAccelerator.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <winioctl.h>
#else
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include "android/utils/path.h"
#include "android/base/Compiler.h"
#include "android/base/files/ScopedFd.h"
#ifdef _WIN32
#include "android/base/files/ScopedHandle.h"
#endif
#include "android/base/Log.h"
#include "android/base/StringFormat.h"
// NOTE: This source file must be independent of the rest of QEMU, as such
// it should not include / reuse any QEMU source file or function
// related to KVM or HAX.
#ifdef __linux__
# define HAVE_KVM 1
# define HAVE_HAX 0
#elif defined(_WIN32) || defined(__APPLE__)
# define HAVE_KVM 0
# define HAVE_HAX 1
#else
# error "Unsupported host platform!"
#endif
namespace android {
using base::String;
using base::StringAppendFormat;
using base::ScopedFd;
namespace {
struct GlobalState {
bool probed;
bool testing;
CpuAccelerator accel;
char status[256];
};
GlobalState gGlobals = { false, false, CPU_ACCELERATOR_NONE, { '\0' } };
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////
///// Linux KVM support.
/////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
#if HAVE_KVM
#include <linux/kvm.h>
// Return true iff KVM is installed and usable on this machine.
// |*status| will be set to a small status string explaining the
// status of KVM on success or failure.
bool ProbeKVM(String *status) {
// 1) Check that /dev/kvm exists.
if (::access("/dev/kvm", F_OK)) {
status->assign(
"KVM is not installed on this machine (/dev/kvm is missing).");
return false;
}
// 2) Check that /dev/kvm can be opened.
if (::access("/dev/kvm", R_OK)) {
status->assign(
"This user doesn't have permissions to use KVM (/dev/kvm).");
return false;
}
// 3) Open the file.
ScopedFd fd(TEMP_FAILURE_RETRY(open("/dev/kvm", O_RDWR)));
if (!fd.valid()) {
status->assign("Could not open /dev/kvm :");
status->append(strerror(errno));
return false;
}
// 4) Extract KVM version number.
int version = ::ioctl(fd.get(), KVM_GET_API_VERSION, 0);
if (version < 0) {
status->assign("Could not extract KVM version: ");
status->append(strerror(errno));
return false;
}
// 5) Compare to minimum supported version
status->clear();
if (version < KVM_API_VERSION) {
StringAppendFormat(status,
"KVM version too old: %d (expected at least %d)\n",
version,
KVM_API_VERSION);
return false;
}
// 6) Profit!
StringAppendFormat(status,
"KVM (version %d) is installed and usable.",
version);
return true;
}
#endif // HAVE_KVM
#if HAVE_HAX
// Version numbers for the HAX kernel module.
// |compat_version| is the minimum API version supported by the module.
// |current_version| is its current API version.
struct HaxModuleVersion {
uint32_t compat_version;
uint32_t current_version;
};
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////
///// Windows HAX support.
/////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
#if defined(_WIN32)
using base::ScopedHandle;
// Windows IOCTL code to extract HAX kernel module version.
#define HAX_DEVICE_TYPE 0x4000
#define HAX_IOCTL_VERSION \
CTL_CODE(HAX_DEVICE_TYPE, 0x900, METHOD_BUFFERED, FILE_ANY_ACCESS)
// The minimum API version supported by the Android emulator.
#define HAX_MIN_VERSION 1
bool ProbeHAX(String* status) {
status->clear();
// 1) Try to find the HAX kernel module.
ScopedHandle hax(CreateFile("\\\\.\\HAX",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL));
if (!hax.valid()) {
DWORD err = GetLastError();
if (err == ERROR_FILE_NOT_FOUND) {
status->assign("HAX kernel module is not installed!");
} else {
StringAppendFormat(status,
"Opening HAX kernel module failed: %u",
err);
}
return false;
}
// 2) Extract the module's version.
HaxModuleVersion hax_version;
DWORD dSize = 0;
BOOL ret = DeviceIoControl(hax.get(),
HAX_IOCTL_VERSION,
NULL, 0,
&hax_version, sizeof(hax_version),
&dSize,
(LPOVERLAPPED) NULL);
if (!ret) {
DWORD err = GetLastError();
StringAppendFormat(status,
"Could not extract HAX module version: %u",
err);
return false;
}
// 3) Check that it is the right version.
if (hax_version.current_version < HAX_MIN_VERSION) {
StringAppendFormat(status,
"HAX version (%d) is too old (need at least %d).",
hax_version.current_version,
HAX_MIN_VERSION);
return false;
}
// 4) Profit!
StringAppendFormat(status,
"HAX (version %d) is installed and usable.",
hax_version.current_version);
return true;
}
#elif defined(__APPLE__)
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////
///// Darwin HAX support.
/////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// An IOCTL command number used to retrieve the HAX kernel module version.
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, HaxModuleVersion)
// The minimum API version supported by the Android emulator.
#define HAX_MIN_VERSION 1
bool ProbeHAX(String* status) {
// 1) Check that /dev/HAX exists.
if (::access("/dev/HAX", F_OK)) {
status->assign(
"HAX is not installed on this machine (/dev/HAX is missing).");
return false;
}
// 2) Check that /dev/HAX can be opened.
if (::access("/dev/HAX", R_OK)) {
status->assign(
"This user doesn't have permission to use HAX (/dev/HAX).");
return false;
}
// 3) Open the file.
ScopedFd fd(open("/dev/HAX", O_RDWR));
if (!fd.valid()) {
status->assign("Could not open /dev/HAX: ");
status->append(strerror(errno));
return false;
}
// 4) Extract HAX version number.
status->clear();
HaxModuleVersion hax_version;
if (::ioctl(fd.get(), HAX_IOCTL_VERSION, &hax_version) < 0) {
StringAppendFormat(status,
"Could not extract HAX version: %s",
strerror(errno));
return false;
}
if (hax_version.current_version < hax_version.compat_version) {
StringAppendFormat(
status,
"Malformed HAX version numbers (current=%d, compat=%d)\n",
hax_version.current_version,
hax_version.compat_version);
return false;
}
// 5) Compare to minimum supported version.
if (hax_version.current_version < HAX_MIN_VERSION) {
StringAppendFormat(status,
"HAX version too old: %d (expected at least %d)\n",
hax_version.current_version,
HAX_MIN_VERSION);
return false;
}
// 6) Profit!
StringAppendFormat(status,
"HAX (version %d) is installed and usable.",
hax_version.current_version);
return true;
}
#else // !_WIN32 && !__APPLE__
#error "Unsupported HAX host platform"
#endif // !_WIN32 && !__APPLE__
#endif // HAVE_HAX
} // namespace
CpuAccelerator GetCurrentCpuAccelerator() {
GlobalState* g = &gGlobals;
if (g->probed || g->testing) {
return g->accel;
}
String status;
#if HAVE_KVM
if (ProbeKVM(&status)) {
g->accel = CPU_ACCELERATOR_KVM;
}
#elif HAVE_HAX
if (ProbeHAX(&status)) {
g->accel = CPU_ACCELERATOR_HAX;
}
#else
status = "This system does not support CPU acceleration.";
#endif
::snprintf(g->status, sizeof(g->status), "%s", status.c_str());
g->probed = true;
return g->accel;
}
String GetCurrentCpuAcceleratorStatus() {
GlobalState *g = &gGlobals;
if (!g->probed && !g->testing) {
// Force detection of the current CPU accelerator.
GetCurrentCpuAccelerator();
}
return String(g->status);
}
void SetCurrentCpuAcceleratorForTesting(CpuAccelerator accel,
const char* status) {
GlobalState *g = &gGlobals;
g->testing = true;
g->accel = accel;
::snprintf(g->status, sizeof(g->status), "%s", status);
}
} // namespace android