-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptlcalls.h
405 lines (335 loc) · 12.4 KB
/
ptlcalls.h
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// -*- c++ -*-
//
// PTLsim: Cycle Accurate x86-64 Simulator
// PTLCALL support for user code running inside the virtual machine
//
// Copyright 2004-2009 Matt T. Yourst <[email protected]>
//
#ifndef __PTLCALLS_H__
#define __PTLCALLS_H__
//
// We exclude some definitions if we're compiling the PTLsim hypervisor itself
// (i.e. __INSIDE_PTLSIM__ is defined) orQEMU (i.e. __INSIDE_MARSS_QEMU__),
// since these definitions would collide with ptlsim-kvm.h otherwise.
//
#define _GNU_SOURCE
#if (!defined(__INSIDE_PTLSIM__)) && (!defined(__INSIDE_MARSS_QEMU__))
#define PTLCALLS_USERSPACE
#define _GNU_SOURCE
#endif
#ifdef PTLCALLS_USERSPACE
#include <unistd.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
typedef unsigned char byte;
typedef unsigned short W16;
typedef unsigned int W32;
typedef unsigned long long W64;
#ifdef __x86_64__
typedef W64 Waddr;
#else
typedef W32 Waddr;
#endif
// Read timestamp counter
static inline W64 ptlcall_rdtsc() {
W32 lo, hi;
asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
return ((W64)lo) | (((W64)hi) << 32);
}
#endif // PTLCALLS_USERSPACE
//
// CPUID may be executed with the magic value 0x404d5459 ("@MTY")
// in %rax to detect if we're running under PTLsim. If so, %rax
// is changed to 0x59455321 ("YES!"), and %rbx/%rcx/%rdx contain
// information on how to invoke PTLCALLs (see below).
//
// If we are NOT running under PTLsim, Intel and AMD chips will
// return the value for the highest basic information leaf instead
// of the magic value above.
//
// The 0x4xxxxxxx CPUID index range has been architecturally reserved
// specifically for user defined purposes like this.
//
#define PTLSIM_CPUID_MAGIC 0x404d5459
#define PTLSIM_CPUID_FOUND 0x59455321
//
// Supported methods of invoking a PTLCALL:
//
// These form a bitmap returned in %rbx after successfully executing
// CPUID with PTLSIM_CPUID_MAGIC and checking %rax == PTLSIM_CPUID_FOUND.
//
// If a future version of PTLsim defines a new type of PTLCALL
// with different calling conventions than these existing methods,
// a new PTLCALL_METHOD_xxx bit will be defined for it.
//
#define PTLCALL_METHOD_OPCODE 0x1 // x86 opcode 0x0f37
#define PTLCALL_METHOD_MMIO 0x2 // MMIO store to physical address in %rdx[15:0] : %rcx[31:0]
#define PTLCALL_METHOD_IOPORT 0x4 // OUT to I/O port number in %rdx[31:16]
#ifdef PTLCALLS_USERSPACE
//
// ptlsim_check_status meanings:
// -4 = running under PTLsim but but permission denied while adjusting I/O port permissions
// -3 = running under PTLsim but but unable to map MMIO page
// -2 = running under PTLsim but but unable to open device for MMIO
// -1 = not running under PTLsim
// 0 = currently unknown before first call to is_running_under_ptlsim()
// +1 = running under PTLsim with least one available invocation method
//
#define bits(x, i, l) (((x) >> (i)) & bitmask(l))
#define LO32(x) (W32)((x) & 0xffffffffLL)
#define bitmask(l) (((l) == 64) ? (W64)(-1LL) : ((1LL << (l))-1LL))
static int ptlsim_check_status __attribute__((common)) = 0;
static W64 supported_ptlcall_methods __attribute__((common)) = 0;
static int selected_ptlcall_method __attribute__((common)) = -1;
static W64 ptlcall_mmio_page_physaddr __attribute__((common)) = 0;
static W64* ptlcall_mmio_page_virtaddr __attribute__((common)) = NULL;
static W16 ptlcall_io_port __attribute__((common)) = 0;
static int ptlsim_ptlcall_init() {
W32 rax = PTLSIM_CPUID_MAGIC;
W32 rbx = 0;
W32 rcx = 0;
W32 rdx = 0;
int ptlcall_mmio_page_offset;
static const char* mmap_filename = "/dev/mem";
// a.k.a. cpuid(PTLSIM_CPUID_MAGIC, rax, rbx, rcx, rdx);
asm volatile("cpuid" : "+a" (rax), "+b" (rbx), "+c" (rcx), "+d" (rdx) : : "memory");
// cout << "rax = 0x", hexstring(rax, 32), endl;
// cout << "rbx = 0x", hexstring(rbx, 32), endl;
// cout << "rcx = 0x", hexstring(rcx, 32), endl;
// cout << "rdx = 0x", hexstring(rdx, 32), endl;
if (rax != PTLSIM_CPUID_FOUND) {
ptlsim_check_status = -1;
return 0;
}
supported_ptlcall_methods = rbx;
ptlcall_mmio_page_physaddr = (bits(rdx, 0, 16) << 32) | LO32(rcx);
ptlcall_mmio_page_offset = (ptlcall_mmio_page_physaddr & 0xfff);
ptlcall_mmio_page_physaddr &= ~0xfff;
ptlcall_io_port = bits(rdx, 16, 16);
if (supported_ptlcall_methods & PTLCALL_METHOD_MMIO) {
// We use O_SYNC to guarantee uncached accesses:
// int fd = open(mmap_filename, O_RDWR|O_LARGEFILE|O_SYNC, 0);
int fd = open(mmap_filename, O_RDWR|O_SYNC, 0);
if (fd < 0) {
fprintf(stderr, "ptlsim_ptlcall_init: cannot open %s for MMIO to physaddr %p (%s)\n",
mmap_filename, (void*)ptlcall_mmio_page_physaddr, strerror(errno));
supported_ptlcall_methods &= ~PTLCALL_METHOD_MMIO;
ptlsim_check_status = -2;
return 0;
}
ptlcall_mmio_page_virtaddr = (W64*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, ptlcall_mmio_page_physaddr);
if (((int)(Waddr)ptlcall_mmio_page_virtaddr) == -1) {
fprintf(stderr, "ptlsim_ptlcall_init: cannot mmap %s (fd %d) for MMIO to physaddr %p (%s)\n",
mmap_filename, fd, (void*)ptlcall_mmio_page_physaddr, strerror(errno));
supported_ptlcall_methods &= ~PTLCALL_METHOD_MMIO;
ptlsim_check_status = -3;
close(fd);
return 0;
}
// Adjust the pointer to the actual trigger word within the page (usually always offset 0)
ptlcall_mmio_page_virtaddr = (W64*)(((Waddr)ptlcall_mmio_page_virtaddr) + ptlcall_mmio_page_offset);
close(fd);
selected_ptlcall_method = PTLCALL_METHOD_MMIO;
fprintf(stderr, "ptlsim_ptlcall_init: mapped PTLcall MMIO page at phys %p, virt %p\n",
(void*)ptlcall_mmio_page_physaddr, (void*)ptlcall_mmio_page_virtaddr);
}
ptlsim_check_status = +1;
return 1;
}
static inline int is_running_under_ptlsim() {
if (!ptlsim_check_status)
ptlsim_ptlcall_init();
return (ptlsim_check_status > 0);
}
#ifdef __x86_64__
static inline W64 do_ptlcall_mmio(W64 callid, W64 arg1, W64 arg2, W64 arg3, W64 arg4, W64 arg5, W64 arg6) {
W64 rc;
asm volatile ("movq %[arg4],%%r10\n"
"movq %[arg5],%%r8\n"
"movq %[arg6],%%r9\n"
"mfence\n"
"smsw %[target]\n"
: "=a" (rc),
[target] "=m" (*ptlcall_mmio_page_virtaddr)
: [callid] "a" (callid),
[arg1] "D" ((W64)(arg1)),
[arg2] "S" ((W64)(arg2)),
[arg3] "d" ((W64)(arg3)),
[arg4] "g" ((W64)(arg4)),
[arg5] "g" ((W64)(arg5)),
[arg6] "g" ((W64)(arg6))
: "r11","rcx","memory" ,"r8", "r10", "r9");
return rc;
}
#else
static inline W64 do_ptlcall_mmio(W64 callid, W64 arg1, W64 arg2, W64 arg3, W64 arg4, W64 arg5, W64 arg6) {
#error TODO: Define a 32-bit PTLCALL calling convention so we can use this from 32-bit userspace apps
}
#endif
static inline W64 ptlcall(W64 op, W64 arg1, W64 arg2, W64 arg3, W64 arg4, W64 arg5, W64 arg6) {
if (!is_running_under_ptlsim())
return (W64)(-ENOSYS);
if (selected_ptlcall_method == PTLCALL_METHOD_MMIO) {
return do_ptlcall_mmio(op, arg1, arg2, arg3, arg4, arg5, arg6);
} else {
// assert(false);
return (W64)(-ENOSYS);
}
}
#endif // PTLCALLS_USERSPACE
//
// Get PTLsim version information (assuming CPUID indicates PTLsim is active)
//
// The highest bit (bit 63) is set when running under simulation, and clear
// when running in native mode.
//
#define PTLCALL_VERSION 0
#ifdef PTLCALLS_USERSPACE
static inline W64 ptlcall_version() {
return ptlcall(PTLCALL_VERSION, 0, 0, 0, 0, 0, 0);
}
#endif
//
// Log this call with a marker and attached performance counter data
//
#define PTLCALL_MARKER 1
#ifdef PTLCALLS_USERSPACE
static inline W64 ptlcall_marker(W64 marker) {
return ptlcall(PTLCALL_MARKER, marker, 0, 0, 0, 0, 0);
}
//
// For utility usage in benchmarks:
//
static W64 ptlsim_marker_id = 0;
#endif // !PTLCALLS_USERSPACE
//
// Enqueue a command list for the PTLsim hypervisor to process.
//
// If the caller is running in native mode, this will force
// all VCPUs to context switch into the PTLsim simulated model.
//
#define PTLCALL_ENQUEUE 2
//
// Descriptor in list of commands passed to PTLCALL_ENQUEUE
//
struct PTLsimCommandDescriptor {
W64 command; // pointer to command string
W64 length; // length of command string
};
#ifdef PTLCALLS_USERSPACE
//
// Enqueue a list of commands, to be executed in sequence.
//
// Queueing is required to implement a fixed-length simulation run
// followed by a switch back to native mode (or another core).
// Otherwise, PTLsim would halt after completing the first command,
// but it would never know about the next command since it needs
// to actually execute another ptlcall instruction to get that
// command. Hence, we allow multiple commands to be atomically
// queued and processed in sequence.
//
static inline W64 ptlcall_multi(char* const list[], size_t length, int flush) {
struct PTLsimCommandDescriptor* desc = (struct PTLsimCommandDescriptor*)malloc(length * sizeof(struct PTLsimCommandDescriptor));
W64 rc;
int i;
for (i = 0; i < length; i++) {
desc[i].command = (W64)list[i];
desc[i].length = strlen(list[i]);
}
rc = ptlcall(PTLCALL_ENQUEUE, (W64)desc, length, flush, 0, 0, 0);
free(desc);
return rc;
}
static inline W64 ptlcall_multi_enqueue(char* const list[], size_t length) {
return ptlcall_multi(list, length, 0);
}
static inline W64 ptlcall_multi_flush(char* const list[], size_t length) {
return ptlcall_multi(list, length, 1);
}
static inline W64 ptlcall_single(const char* command, int flush) {
struct PTLsimCommandDescriptor desc;
desc.command = (W64)command;
desc.length = strlen(command);
return ptlcall(PTLCALL_ENQUEUE, (W64)&desc, 1, flush, 0, 0, 0);
}
static inline W64 ptlcall_single_enqueue(const char* command) {
return ptlcall_single(command, 0);
}
static inline W64 ptlcall_single_flush(const char* command) {
return ptlcall_single(command, 1);
}
//
// Convenience functions
//
static inline W64 ptlcall_nop() {
return ptlcall_single_flush("-run");
}
static inline W64 ptlcall_switch_to_sim() {
return ptlcall_single_flush("-run");
}
static inline W64 ptlcall_switch_to_native() {
return ptlcall_single_flush("-native");
}
static inline W64 ptlcall_kill() {
return ptlcall_single_flush("-kill");
}
static inline W64 ptlcall_capture_stats(const char* snapshot) {
char buf[128];
char runcmd[] = "-run";
char* commands[2] = {buf, runcmd};
if (!snapshot) snapshot = "forced";
snprintf(buf, sizeof(buf), "-snapshot-now %s", snapshot);
return ptlcall_multi_flush(commands, 2);
}
#endif // !PTLCALLS_USERSPACE
//
// Create a checkpoint of the entire virtual machine at a precise
// point in time (i.e. at the instant the PTLCALL is executed),
// such that the checkpoint can restart execution at the next
// instruction following the PTLCALL.
//
// This should only be used in native mode; it will be ignored
// when running in simulation mode.
//
#define PTLCALL_CHECKPOINT 3
//
// PTLCALL_CHECKPOINT callers can optionally ask the hypervisor to
// pause, shut down or reboot (back into native mode) immediately
// after creating the checkpoint:
//
#define PTLCALL_CHECKPOINT_AND_CONTINUE 0
#define PTLCALL_CHECKPOINT_AND_SHUTDOWN 1
#define PTLCALL_CHECKPOINT_AND_REBOOT 2
#define PTLCALL_CHECKPOINT_AND_PAUSE 3
#define PTLCALL_CHECKPOINT_DUMMY 4
#ifdef PTLCALLS_USERSPACE
static inline W64 ptlcall_checkpoint_generic(const char* name, int action) {
return ptlcall(PTLCALL_CHECKPOINT, (W64)name, strlen(name), action, 0, 0, 0);
}
static inline W64 ptlcall_checkpoint_and_continue(const char* name) {
return ptlcall_checkpoint_generic(name, PTLCALL_CHECKPOINT_AND_CONTINUE);
}
static inline W64 ptlcall_checkpoint_and_shutdown(const char* name) {
return ptlcall_checkpoint_generic(name, PTLCALL_CHECKPOINT_AND_SHUTDOWN);
}
static inline W64 ptlcall_checkpoint_and_reboot(const char* name) {
return ptlcall_checkpoint_generic(name, PTLCALL_CHECKPOINT_AND_REBOOT);
}
static inline W64 ptlcall_checkpoint_and_pause(const char* name) {
return ptlcall_checkpoint_generic(name, PTLCALL_CHECKPOINT_AND_PAUSE);
}
static inline W64 ptlcall_checkpoint() {
static const char* checkpoint_name = "default";
return ptlcall_checkpoint_and_shutdown(checkpoint_name);
}
// This function will only make QEMU to pause
static inline W64 ptlcall_checkpoint_dummy() {
static const char* name= "default";
return ptlcall_checkpoint_generic(name, PTLCALL_CHECKPOINT_DUMMY);
}
#endif // PTLCALLS_USERSPACE
#endif // __PTLCALLS_H__