-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
296 lines (250 loc) · 8.24 KB
/
main.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
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
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>
#include <emmintrin.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <x86intrin.h> /* for rdtscp and clflush */
#define ADDR_POOL_SIZE 15000
#define ACCESS_PRBE_NUM 5000
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE )
#define PROTECTION (PROT_READ | PROT_WRITE)
#define HUGE_PAGE_SIZE 2*1024*1024
typedef struct {
uint64_t pfn : 54;
unsigned int soft_dirty : 1;
unsigned int file_page : 1;
unsigned int swapped : 1;
unsigned int present : 1;
} PagemapEntry;
int pagemap_get_entry(PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
{
size_t nread;
ssize_t ret;
uint64_t data;
nread = 0;
while (nread < sizeof(data)) {
ret = pread(
pagemap_fd,
&data,
sizeof(data),
(vaddr / sysconf(_SC_PAGESIZE)) * sizeof(data) + nread
);
nread += ret;
if (ret <= 0) {
return 1;
}
}
entry->pfn = data & (((uint64_t)1 << 54) - 1);
entry->soft_dirty = (data >> 54) & 1;
entry->file_page = (data >> 61) & 1;
entry->swapped = (data >> 62) & 1;
entry->present = (data >> 63) & 1;
return 0;
}
int virt_to_phys_user(uintptr_t *paddr, uintptr_t vaddr)
{
int pagemap_fd;
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
if (pagemap_fd < 0) {
printf("RET 1\n");
return 1;
}
PagemapEntry entry;
if (pagemap_get_entry(&entry, pagemap_fd, vaddr)) {
printf("RET 2\n");
return 1;
}
close(pagemap_fd);
long pg_size = sysconf(_SC_PAGESIZE);
// printf("entry.pfn: %lu\t vaddr: %p\n", entry.pfn, vaddr);
*paddr = (entry.pfn * pg_size) + (vaddr % pg_size);
return 0;
}
char * check_if_memory_continous(char *original_mem_addr){
uintptr_t pfn_prev;
virt_to_phys_user(&pfn_prev, (uintptr_t)original_mem_addr);
printf("+ Checking if memory is continous\n");
size_t num_consequtive_pfn = 0;
/* I check each 4KB page up to 512MB */
size_t i = 1;
for(; i < 512*256; i++) {
uintptr_t pfn_tmp;
uintptr_t addrv = (((uintptr_t)original_mem_addr)) + i*1024*4; // 4KB page
virt_to_phys_user(&pfn_tmp, addrv);
// printf("Physical addr: %p, Virt: %lu\n", (char*)pfn_tmp, addrv);
if(pfn_tmp != pfn_prev + 4*1024){ // if pfn is not consecutive
num_consequtive_pfn = 0;
// printf("pfn_tmp: %llu != %llu\n", pfn_tmp, pfn_prev);
pfn_prev = pfn_tmp;
continue;
}
pfn_prev = pfn_tmp;
num_consequtive_pfn += 1;
// printf("num_consequtive_pfn: %d", num_consequtive_pfn);
if(num_consequtive_pfn == 512){ // 2MB/4KB
break;
}
}
if(num_consequtive_pfn != 512){
printf("!!! The memory is not continous!\n");
exit(1);
}
uintptr_t continous_mem_adr = ((unsigned long)original_mem_addr) + (i-512)*1024*4;
printf("+ The memory chunk is continous for the next 2MB starting at: %p\n", (char*)continous_mem_adr);
return (char*)continous_mem_adr;
}
unsigned int arg_save_to_csv_flag = 0;
unsigned int arg_find_bits_flag = 0;
unsigned int arg_find_bits_threshold = 0;
void check_arguments(int argc, char *argv[]){
for(size_t i = 1; i < argc; i++)
{
char *arg = argv[i];
if(!strcmp(arg, "-e")){
arg_save_to_csv_flag = 1;
}
else if(!strcmp(arg, "-t")){
arg_find_bits_flag = 1;
arg_find_bits_threshold = atoi(argv[i+1]);
i+=1;
}
}
}
uint64_t rdtsc2() {
uint64_t a, d;
asm volatile ("rdtscp" : "=a" (a), "=d" (d) : : "rcx");
asm volatile ("cpuid"::: "rax", "rbx", "rcx", "rdx");
a = (d << 32) | a;
return a;
}
uint64_t get_timing(char* first, char* second) {
size_t min_res = (-1ull);
for (int i = 0; i < 4; i++) {
size_t number_of_reads = ACCESS_PRBE_NUM;
volatile size_t *base_addr = (volatile size_t *) first;
volatile size_t *probe_addr = (volatile size_t *) second;
size_t t0 = rdtsc2();
while (number_of_reads-- > 0) {
*base_addr; *base_addr;
*probe_addr; *probe_addr;
asm volatile("clflush (%0)" : : "r" (base_addr) : "memory");
asm volatile("clflush (%0)" : : "r" (probe_addr) : "memory");
}
uint64_t res = (rdtsc2() - t0) / (ACCESS_PRBE_NUM);
if (res < min_res)
min_res = res;
}
return min_res;
}
char* change_bit(void* addr, int bit){
return (char*)((uint64_t)addr ^ (1 << bit));
}
void find_bits(char** conflict_pool, size_t conflict_pool_size, char* base_addr){
unsigned int bits_arr[20];
for(size_t i = 0; i < 20; i++){
bits_arr[i] = 0;
}
for(size_t i = 0; i < conflict_pool_size; i++) {
char *probe_addr = conflict_pool[i];
for(size_t bit_i = 0; bit_i <= 20; bit_i++){
char *probe_addr_shifted = change_bit(probe_addr, bit_i);
uint64_t time = get_timing(base_addr, probe_addr_shifted);
if (time < arg_find_bits_threshold) {
// printf("bit:%lu\n", bit_i);
bits_arr[bit_i] = bits_arr[bit_i] + 1;
}
}
}
printf("Bits histogram:\n");
FILE *file_bits_csv = fopen("jsi510-bits.csv", "w");
unsigned int max_val = 0;
//find max value
for(size_t i = 0; i < 20; i++){
if(max_val < bits_arr[i]) max_val = bits_arr[i];
}
for(size_t i = 0; i < 20; i++){
printf("bit[%lu]: %u\n", i, bits_arr[i]);
if(bits_arr[i] > (max_val*0.8)){ // TODO: max value from bins * 80%
fprintf(file_bits_csv, "%lu\n", i);
}
}
fclose(file_bits_csv);
}
int main(int argc, char *argv[])
{
check_arguments(argc, argv);
if(arg_find_bits_flag){
printf("+ Chosen threshold = %u\n", arg_find_bits_threshold);
}
printf("+ Allocating 512MB...\n");
// char *continous_mem_addr = calloc(1024*1024*512, 1); // 512 MB
char *continous_mem_addr = mmap(NULL, 1024*1024*512, PROTECTION, FLAGS, -1, 0);
if(continous_mem_addr == MAP_FAILED){
printf("map failed\n");
perror("sdf");
return -1;
}
if(continous_mem_addr == NULL){
printf("! Cannot alloc 512MB!\n");
exit(1);
}
printf("+ Allocated 512MB at %p\n", continous_mem_addr);
/* Update continous mem address */
continous_mem_addr = check_if_memory_continous(continous_mem_addr);
unsigned int random_constraint = HUGE_PAGE_SIZE >> 6;
/* Create address pool */
char **addr_pool = malloc(sizeof(char*) * ADDR_POOL_SIZE);
for(size_t i = 0; i < ADDR_POOL_SIZE; i++) {
char *addr_i = ((rand() % (random_constraint+1)) << 6 ) + continous_mem_addr;
// printf("rand addr: %p\n", addr_i);
addr_pool[i] = addr_i;
// printf("saved to pool: %p", addr_pool[i]);
}
/* Create pool for conflicts */
char **conflict_addr_pool = malloc(sizeof(char*) * ADDR_POOL_SIZE);
unsigned int conflict_addr_pool_size = 0;
uintptr_t base_phys;
virt_to_phys_user(&base_phys, (uintptr_t)addr_pool[0]);
int junk=0;
FILE *file_csv = NULL;
if(arg_save_to_csv_flag){
file_csv = fopen("jsi510-time.csv", "w");
fprintf(file_csv, "baseVirt,probeVirt,basePhys,probePhys,time\n");
}
char *base_addr = addr_pool[0];
for(size_t probe_addr_i = 1; probe_addr_i < ADDR_POOL_SIZE; probe_addr_i++)
{
uint64_t time_sum = get_timing(base_addr, addr_pool[probe_addr_i]);
uintptr_t probe_phys;
virt_to_phys_user(&probe_phys, (uintptr_t)addr_pool[probe_addr_i]);
if(time_sum > arg_find_bits_threshold){
conflict_addr_pool[conflict_addr_pool_size++] = addr_pool[probe_addr_i];
}
if(arg_save_to_csv_flag){
fprintf(file_csv, "%p,%p,%p,%p,%lu\n",
base_addr,
addr_pool[probe_addr_i],
(char*)base_phys,
(char*)probe_phys,
time_sum
);
}
}
if(arg_find_bits_flag){
find_bits(conflict_addr_pool, conflict_addr_pool_size, base_addr);
}
if(arg_save_to_csv_flag){
fclose(file_csv);
}
return 0;
}