-
Notifications
You must be signed in to change notification settings - Fork 0
/
conflict_thresh.c
876 lines (701 loc) · 33.5 KB
/
conflict_thresh.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
#include "rowhammer_utils.h"
#define _GNU_SOURCE
#include <sys/mman.h>
#define ROW_CONFLICT_TH 530
// #include "allocator.h"
// #include "include/params.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
// #include "utils.h"
#define POSIX_ALIGN (1<<22)
#define MEM_SIZE (1<<21)
#define HUGE_SIZE (1<<21)
#define HASH_FN_CNT 4
#define ROW_SIZE (1<<13)
typedef struct {
uint64_t lst[HASH_FN_CNT];
uint64_t len;
}AddrFns;
typedef struct {
AddrFns h_fns;
uint64_t row_mask;
uint64_t col_mask;
}DRAMLayout;
typedef struct{
uint64_t num_conf;
char** conflicts;
}contig_rows;
typedef struct {
char* base;
contig_rows* bank_contig_rows; // array of conflict array (e.g. conflict_bank[0] is an array of all the addresses that conflict in bank 0. Each 4 addresses in that row will belong to a single row)
uint64_t virt_offset; // the virtual addr offset from eg. a virtual address that thas 21 trailing zeroes has a virtual offset of 0. Helps with virt2dram but not abs necessary
}contig_chunk;
typedef struct{
char* aggressors[8];
char* victims[4];
}ds_pair; // this struct will hold all the relevant base addresses for a double-sided hammer
typedef struct{
ds_pair* pairs[16];
int num_pairs[16];
}bank_pairs;
typedef struct{
char** addresses[16];
int num_addr[16];
}contiguous_bank;
DRAMLayout g_mem_layout = {{{0x2040,0x24000,0x48000,0x90000}, 4}, 0x3fffe0000, ROW_SIZE-1};
bank_pairs bank_pairs_arr;
contiguous_bank bank_rows;
int num_blocks = 2;
// only works with 2MB aligned pages because the first 21 bits of the virtual addresses are identical to the first in phys
u_int64_t get_bank(uint64_t v_addr){
uint64_t bank=0;
for (int i = 0; i < g_mem_layout.h_fns.len; i++) {
bank |= (__builtin_parityl(v_addr & g_mem_layout.h_fns.lst[i]) <<i);
}
return bank;
}
///thp code
uint64_t get_dram_row_thp(uint64_t v_addr)
{
uint64_t row_mask = (HUGE_SIZE -1) & g_mem_layout.row_mask;
return (v_addr & row_mask) >> __builtin_ctzl(row_mask);
}
int alloc_buffer(contig_chunk* mem_chunks, int num_blocks)
{
// if (mem->buffer[0] != NULL) {
// fprintf(stderr, "[ERROR] - Memory already allocated\n");
// }s
// if (mem->align < _SC_PAGE_SIZE) {
// mem->align = 0;
// }
//madvise(mem->buffer, mem->size, MADV_HUGEPAGE);
//uint64_t alloc_size = mem->align ? mem->size + mem->align : mem->size;
// uint64_t alloc_size = mem->size;
//uint64_t alloc_flags = MAP_PRIVATE | MAP_POPULATE | MAP_NORESERVE | MAP_HUGETLB | (21 << MAP_HUGE_SHIFT) | MAP_ANONYMOUS;
//uint64_t alloc_flags = MAP_PRIVATE | MAP_POPULATE | MAP_NORESERVE | MAP_ANONYMOUS;
// uint64_t alloc_flags = MAP_PRIVATE | MAP_POPULATE;
// madvise alloc
for (int i = 0; i < num_blocks; i++) {
posix_memalign((void **)(&(mem_chunks[i].base)), POSIX_ALIGN, MEM_SIZE);
if (mem_chunks[i].base == NULL) {
fprintf(stderr, "wtf\n");
}
//fprintf(stderr, "iter %d: ", i);
if (madvise(mem_chunks[i].base, POSIX_ALIGN, MADV_HUGEPAGE) == -1)
{
fprintf(stderr, "MADV %d Failed: %d\n", i, errno);
if(errno == ENOMEM){
fprintf(stderr, "no mem");
}
}
// *(mem[i]) = 10;
memset(mem_chunks[i].base,0xFF,MEM_SIZE);
//else
//{
// fprintf(stderr, "MADV Success\n");
//}
if(check_consecutive((uint64_t) mem_chunks[i].base,MEM_SIZE)){
printf("cons\n");
}else{printf("not cons \n");}
mem_chunks[i].virt_offset = ((uint64_t) mem_chunks[i].base) & (MEM_SIZE - 1);
}
if (mem_chunks[0].base != NULL) {
fprintf(stderr, "Buffer allocated\n");
}
// fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
// fprintf(stderr, "[ MEM ] - Buffer: %p\n", mem->buffer);
// fprintf(stderr, "[ MEM ] - Size: %ld\n", alloc_size);
// fprintf(stderr, "[ MEM ] - Alignment: %ld\n", mem->align);
// fprintf(stderr, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
// exit(0);
return 0;
}
// finds all pages that row conflict with the base_page
void find_bank_conflict(contig_chunk* mem_chunk, int target_bank){
// find a base address in the target bank
u_int64_t base;
u_int64_t curr;
uint64_t phys;
int ndx=0;
for(int i=0; i< MEM_SIZE/PAGE_SIZE; i++){
int found_base=0;
for(int j=0; j<2; j++){
base = (uint64_t)mem_chunk->base + i*PAGE_SIZE + 64*j;
if(get_bank(base) == target_bank){
// printf("Found base\n");
found_base=1;
break;
}
}
if(found_base){break;}
}
mem_chunk->bank_contig_rows[target_bank].conflicts = (char**) malloc(sizeof(char*) * 300);
DramAddr* dram = (DramAddr*) malloc(sizeof(DramAddr));
phys = get_physical_addr(base);
dram_address(phys, dram);
// printf("Base page memory\n");
// printf("BA0:%lx BA1:%lx BA2:%lx BA3:%lx row:%lx\n", dram->BA0,dram->BA1, dram->BA2, dram->BA3, dram->row_num);
// printf("%lx\n",phys);
// printf("----------------------------\n");
// printf("starting row conflict\n");
for(int i=0; i<MEM_SIZE/PAGE_SIZE; i++){
for (int v=0; v< 2; v++){
curr = (uint64_t) mem_chunk->base + (i*PAGE_SIZE) + 64*v;
if(curr == base){continue;}
phys = get_physical_addr(curr);
// u_int64_t acc_time = 0;
// u_int64_t time;
// int j=0;
// while(j<8000){
// time = row_conflict_time((u_int64_t) (base), curr);
// if(time > 1000){continue;}
// j++;
// acc_time+=time;
// }
// acc_time = acc_time/8000;
// // for visibility
// if(acc_time > ROW_CONFLICT_TH){
//
// dram_address(phys, dram);
// printf("%d\n", ndx);
// // printf("BA0:%lx BA1:%lx BA2:%lx BA3:%lx row:%lx\n", dram->BA0,dram->BA1, dram->BA2, dram->BA3, dram->row_num);
// printf("Get DRAM row: %lx\n",get_dram_row_thp((u_int64_t)curr));
// printf("Get BANK: %lx\n",get_bank((u_int64_t)curr));
// printf("(%lx, %lx)\n",curr,phys);
// printf("%lu\n",acc_time);
// printf("----------------------------\n");
// // add to array
// mem_chunk->bank_contig_rows[target_bank].conflicts[ndx] = (char*)curr;
// ndx++;
// }
if(get_bank(base) == get_bank(curr)){
mem_chunk->bank_contig_rows[target_bank].conflicts[ndx] = (char*)curr;
// printf("(%lx, %lx)\t",curr,phys);
// printf("%lu\n",acc_time);
// printf("Get DRAM row: %lx\n",get_dram_row_thp((u_int64_t)curr));
// printf("Get BANK: %lx\n",get_bank((u_int64_t)curr));
ndx++;
}
}
// if(ndx >=100){break;}
}
free(dram);
// if(ndx < 100){printf("less than 100 found\n"); exit(0);}
mem_chunk->bank_contig_rows[target_bank].num_conf =ndx;
return ;
}
// on stalin (ddr4) each row has data from 4 pages/
// so, in conflict array, every 4 entries correspond to same row
// In the conflict array, the first two entries are pages with data in that row from odd blocks (1,3,5...)
// the second two entries are pages with data in even blocks (0,2,4,...)
int memory_template(char** conflicts, int num_conf){
// victim index will step by 2 from 2 to n-4
int victim_ndx =36;
char** agg_list = malloc(5*sizeof(char*));
while(victim_ndx <= (num_conf - 8) ){
// set the aggressors
printf("--------------------------------------------------------------------\n");
memset(conflicts[victim_ndx -4], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx -3], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx -2], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx -1], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx+4], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx+5], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx+6], 0xFF, PAGE_SIZE);
memset(conflicts[victim_ndx+7], 0xFF, PAGE_SIZE);
agg_list[0] = conflicts[victim_ndx -4];
agg_list[1] = conflicts[victim_ndx +4];
agg_list[2] = conflicts[victim_ndx +8];
// printf("Aggressors:\n");
// for(int a=0; a< 3; a++){
// printf("%lx\t", get_physical_addr((u_int64_t)agg_list[a]));
// }
// printf("\n");
// check for correct flips
// flips in only the first 32 bytes
int flippy;
int bad = 0;
int num_flips=0;
for(int i=0; i<4;i++){
memset(conflicts[victim_ndx +i], 0x00, PAGE_SIZE);
flush_chunck(conflicts[victim_ndx+i],PAGE_SIZE);
hammer(conflicts[victim_ndx -4],conflicts[victim_ndx+4] , 1000000);
// multi_sided(agg_list, 3, 900000);
flippy = 0;
bad = 0;
num_flips=0;
for(int j=0; j<PAGE_SIZE; j++){
if((uint8_t) conflicts[victim_ndx+i][j] != 0x00 ){
printf("FLIP in addr (%lx, %lx): %hhx\n", get_physical_addr((u_int64_t)(conflicts[victim_ndx+i] + j)),(u_int64_t)(conflicts[victim_ndx+i] + j), conflicts[victim_ndx+i][j]);
}
}
}
victim_ndx+=4;
}
return -1;
}
void print_pairs(ds_pair* pairs_array, int num_pairs){
for(int i=0; i< num_pairs; i++){
printf("(%lx, %lx)\n",(uint64_t) pairs_array[i].aggressors[0], get_physical_addr((u_int64_t)pairs_array[i].aggressors[0]));
}
}
// takes in all the memory chunks and returns a pointer to a list of hammering pairs
// the list is to be used in templating
// so this function is just for organizing things
int create_hammer_pairs(contig_chunk* chunks, int target_bank, ds_pair* pairs_array){
// ds_pair* pairs_array = (ds_pair*) malloc(200*sizeof(ds_pair));
int pair_ndx=0;
for(int i=0; i< num_blocks; i++){ // go through the conflict array of the target bank of each chunk. The conflict array is basically a list of contig memory rows
if(chunks[i].bank_contig_rows[target_bank].num_conf < 12){continue;} // if the chunk doesn't have a full 3 rows for double sided, skip it
int ndx=0;
int agg_parity=0;
while((ndx*4) <= chunks[i].bank_contig_rows[target_bank].num_conf -4){
if((ndx%2) == 0){
// aggressors
pairs_array[pair_ndx].aggressors[0 + 4*agg_parity] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4];
pairs_array[pair_ndx].aggressors[1 + 4*agg_parity] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+1];
pairs_array[pair_ndx].aggressors[2 + 4*agg_parity] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+2];
pairs_array[pair_ndx].aggressors[3 + 4*agg_parity] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+3];
if(agg_parity == 1){
// completed an agg pair
pair_ndx++;
}
agg_parity = agg_parity ^ 1;
}else{
pairs_array[pair_ndx].victims[0] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4];
pairs_array[pair_ndx].victims[1] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+1];
pairs_array[pair_ndx].victims[2] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+2];
pairs_array[pair_ndx].victims[3] = chunks[i].bank_contig_rows[target_bank].conflicts[ndx*4+3];
}
ndx++;
}
}
// print_pairs(pairs_array, pair_ndx);
return pair_ndx;
}
void fill_victims(ds_pair* pairs_array, int num_pairs){
for(int i=0; i< num_pairs; i++){
char* addr = (char*) (((uint64_t) pairs_array[i].victims[0]) &(~(PAGE_SIZE-1)) );
char* addr2 = (char*) (((uint64_t) pairs_array[i].victims[1]) &(~(PAGE_SIZE-1)) );
char* addr3 = (char*) (((uint64_t) pairs_array[i].victims[2]) &(~(PAGE_SIZE-1)) );
char* addr4 = (char*) (((uint64_t) pairs_array[i].victims[3]) &(~(PAGE_SIZE-1)) );
memset(addr, 0x00, PAGE_SIZE);
memset(addr2, 0x00, PAGE_SIZE);
memset(addr3, 0x00, PAGE_SIZE);
memset(addr4, 0x00, PAGE_SIZE);
flush_chunck(addr,PAGE_SIZE);
flush_chunck(addr2,PAGE_SIZE);
flush_chunck(addr3,PAGE_SIZE);
flush_chunck(addr4,PAGE_SIZE);
}
return;
}
int check_victims(ds_pair* pairs_array, int num_pairs, uint8_t expected){
for(int i=0; i< num_pairs; i++){
for(int j=0; j < 4; j++){
char* addr = (char*) (((uint64_t) pairs_array[i].victims[j]) &(~(PAGE_SIZE-1)) );
// printf("checking (%lx, %lx)\n", get_physical_addr((u_int64_t)(conflicts[ndx*4 +i])),(u_int64_t)(conflicts[ndx*4 +i]));
for(int n=0; n<PAGE_SIZE; n++){
if((uint8_t) addr[n] != expected ){
printf("FLIP in addr (%lx, %lx): %hhx\n", get_physical_addr((u_int64_t)(addr + n)),(u_int64_t)(addr + n), addr[n]);
if(n<=31){
// return 1;
}
if(n==2121){
// return 1;
}
if(n == 2122){
// return 1;
}
}
}
}
}
return 0 ;
}
// TODO: FIX BUG
// the pairs has the two aggressors in a single array
int ten_sided_temp(ds_pair* pairs_array, int num_pairs){
if(num_pairs < 5){return 0;}
// for every pair, fill the victims with data. Agg already flled at the first allocation
char** agg_list = malloc(10*sizeof(char*));
// go through the aggressor pairs in slidiing window
int agg_window_start=0;
int agg_window_end=1;
agg_list[0] = pairs_array[agg_window_start].aggressors[0]; // add top and bottom agg
agg_list[1] = pairs_array[agg_window_start].aggressors[4];
int ndx=2;
while(agg_window_end < num_pairs){
agg_list[ndx] = pairs_array[agg_window_end].aggressors[0];
agg_list[ndx+1] = pairs_array[agg_window_end].aggressors[4];
ndx+=2;
agg_window_end++;
if(ndx>=10){ // got a list of 10 aggressors
// fill victims and hammer
printf("%d aggressors\n", ndx);
for(int i=0; i<ndx; i++){
printf("%lx\n", get_physical_addr((u_int64_t) agg_list[i]));
}
for(int i=0; i<10; i++){
fill_victims(pairs_array, num_pairs);
int x = hammer_thp_prehammer(agg_list, i, 10);
check_victims(pairs_array, num_pairs, 0x00);
}
// reset
agg_window_start++;
agg_window_end = agg_window_start+1;
ndx=0;
agg_list[ndx] = pairs_array[agg_window_start].aggressors[0];
agg_list[ndx+1] = pairs_array[agg_window_start].aggressors[4];
ndx+=2;
}
}
return 0;
// while((ndx*4) <= num_conf -4){
// if((ndx%2) == 0){
// printf("agg ndx %d\n", ndx*4);
// // set the aggressors to 1
// // memset( (char*) (((uint64_t) conflicts[ndx*4]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// // memset((char*) (((uint64_t) conflicts[ndx*4 +1]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// // memset((char*) (((uint64_t) conflicts[ndx*4 +2]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// // memset((char*) (((uint64_t) conflicts[ndx*4 +3]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// printf("Entering ndx %d into list \n", ndx*4);
// printf("(%lx, %lx) \n", get_physical_addr((u_int64_t)( conflicts[ndx*4])),(u_int64_t)( conflicts[ndx*4]));
// agg_list[num_agg] = conflicts[ndx*4];
// num_agg++;
// }else{
// char* addr = (char*) (((uint64_t) conflicts[ndx*4 ]) &(~(PAGE_SIZE-1)) );
// char* addr2 = (char*) (((uint64_t) conflicts[ndx*4 +1]) &(~(PAGE_SIZE-1)) );
// char* addr3 = (char*) (((uint64_t) conflicts[ndx*4 +2]) &(~(PAGE_SIZE-1)) );
// char* addr4 = (char*) (((uint64_t) conflicts[ndx*4 +3]) &(~(PAGE_SIZE-1)) );
// // set the victiim to 0
// memset(addr, 0x00, PAGE_SIZE);
// memset(addr2, 0x00, PAGE_SIZE);
// memset(addr3, 0x00, PAGE_SIZE);
// memset(addr4, 0x00, PAGE_SIZE);
// flush_chunck(conflicts[ndx*4],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+1],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+2],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+3],PAGE_SIZE);
// }
// ndx++;
// }
// printf("%d - aggressors\n", num_agg);
// for(int i=0; i<num_agg; i++){
// printf("%lx\n", get_physical_addr((u_int64_t) agg_list[i]));
// }
// // exit(0);
// if(num_agg>10){num_agg=10;}
// for(int i=0; i < num_agg; i++){
// int x = hammer_thp_prehammer(agg_list, i, num_agg);
// // check for flips
// ndx=0;
// while((ndx*4) <= num_conf -4){
// // printf("%d --------\n", ndx);
// if((ndx%2) == 0){ndx++; continue;}
// for(int i=0; i < 4; i++){
// char* addr = (char*) (((uint64_t) conflicts[ndx*4 +i]) &(~(PAGE_SIZE-1)) );
// // printf("checking (%lx, %lx)\n", get_physical_addr((u_int64_t)(conflicts[ndx*4 +i])),(u_int64_t)(conflicts[ndx*4 +i]));
// for(int j=0; j<PAGE_SIZE; j++){
// if((uint8_t) addr[j] != 0x00 ){
// printf("FLIP in addr (%lx, %lx): %hhx\n", get_physical_addr((u_int64_t)(addr + j)),(u_int64_t)(addr + j), addr[j]);
// }
// }
// }
// memset( (char*) (((uint64_t) conflicts[ndx*4]) &(~(PAGE_SIZE-1)) ), 0x00, PAGE_SIZE);
// memset((char*) (((uint64_t) conflicts[ndx*4 +1]) &(~(PAGE_SIZE-1)) ), 0x00, PAGE_SIZE);
// memset((char*) (((uint64_t) conflicts[ndx*4 +2]) &(~(PAGE_SIZE-1)) ), 0x00, PAGE_SIZE);
// memset((char*) (((uint64_t) conflicts[ndx*4 +3]) &(~(PAGE_SIZE-1)) ), 0x00, PAGE_SIZE);
// flush_chunck(conflicts[ndx*4],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+1],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+2],PAGE_SIZE);
// flush_chunck(conflicts[ndx*4+3],PAGE_SIZE);
// ndx++;
// }
// }
}
void setup_bank_rows(contig_chunk* chunks){
for(int n=0; n< 16; n++){
bank_rows.addresses[n] = (char**)malloc(10000*sizeof(char*));
int ndx=0;
for(int i=0; i< num_blocks; i++){
for(int j=0; j< chunks[i].bank_contig_rows[n].num_conf; j++){
bank_rows.addresses[n][ndx] = chunks[i].bank_contig_rows[n].conflicts[j];
ndx++;
}
}
bank_rows.num_addr[n] = ndx;
}
}
void print_bank_rows(){
for(int i=0; i<2; i++){
printf("Bank %d\n", i);
for(int j=0; j<bank_rows.num_addr[i]; j++){
printf(("(%lx, %lx)\n"),(uint64_t) bank_rows.addresses[i][j], get_physical_addr((uint64_t) bank_rows.addresses[i][j]) );
}
}
}
void fill(char** addr, int num, uint8_t val){
for(int i=0; i<num; i++){
char* curr = (char*) (((uint64_t) addr[i]) &(~(PAGE_SIZE-1)) );
memset(curr,val,PAGE_SIZE);
flush_chunck(curr,PAGE_SIZE);
}
}
int check(char** victims, int num, uint8_t expected){
for(int i=0; i<num; i++){
char* addr = (char*) (((uint64_t) victims[i]) &(~(PAGE_SIZE-1)) );
for(int n=0; n<PAGE_SIZE; n++){
if((uint8_t) addr[n] != expected ){
printf("FLIP in addr (%lx, %lx): %hhx\n", get_physical_addr((u_int64_t)(addr + n)),(u_int64_t)(addr + n), addr[n]);
if(n<=31){
return 1;
}
}
}
}
return 0;
}
// int multi_bank(){
// printf("MULTI..........\n");
// // print_bank_rows();
// printf("done\n");
// char** agg_list = malloc(20*sizeof(char*));
// char** victim_list = malloc(80*sizeof(char*));
// int agg_ndx=0;
// int conf_ndx=0;
// int victim_ndx=0;
// int bank1_start=0;
// int bank2_start=0;
// // int bank1_end=0;
// // int bank2_end=0;
// int ndx=0;
// for(int bank1=0; bank1< 16; bank1++){
// for(int bank2=(bank1+2); bank2<16; bank2+=1){
// int bank1_base=0;
// int bank2_base=0;
// int bank1_start=0;
// int bank2_start=0;
// while((bank1_start < bank_rows.num_addr[bank1] - 12) && (bank2_start < bank_rows.num_addr[bank2] - 12)){
// agg_list[ndx] = bank_rows.addresses[bank1][bank1_start];
// agg_list[ndx+1] = bank_rows.addresses[bank2][bank2_start]; // aggressors
// ndx+=2;
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+1]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+2]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+3]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+1]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+2]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+3]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// victim_list[victim_ndx]=bank_rows.addresses[bank1][bank1_start+4];
// victim_list[victim_ndx+1]=bank_rows.addresses[bank1][bank1_start+5];
// victim_list[victim_ndx+2]=bank_rows.addresses[bank1][bank1_start+6];
// victim_list[victim_ndx+3]=bank_rows.addresses[bank1][bank1_start+7];
// victim_list[victim_ndx+4]=bank_rows.addresses[bank2][bank2_start+4];
// victim_list[victim_ndx+5]=bank_rows.addresses[bank2][bank2_start+5];
// victim_list[victim_ndx+6]=bank_rows.addresses[bank2][bank2_start+6];
// victim_list[victim_ndx+7]=bank_rows.addresses[bank2][bank2_start+7];
// victim_ndx+=8;
// agg_list[ndx] = bank_rows.addresses[bank1][bank1_start+8];
// agg_list[ndx+1] = bank_rows.addresses[bank2][bank2_start+8]; // aggressors
// ndx+=2;
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+1+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+2+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank1][bank1_start+3+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+1+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+2+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// memset( (char*) (((uint64_t) bank_rows.addresses[bank2][bank2_start+3+8]) &(~(PAGE_SIZE-1)) ), 0xFF, PAGE_SIZE);
// if(ndx>=20){
// // printf("%d aggressors\n", ndx);
// // for(int i=0; i<ndx; i++){
// // printf("%lx\t", (u_int64_t) agg_list[i]);
// // // printf("%lx\t", get_physical_addr((u_int64_t) agg_list[i]));
// // printf("Bank %ld:\n", get_bank((u_int64_t) agg_list[i]));
// // }
// fill(victim_list,victim_ndx, 0x00);
// int x = hammer_thp_prehammer(agg_list, 4, 20);
// if(check(victim_list,victim_ndx, 0x00)){
// return 1;
// }
// ndx=0;
// victim_ndx=0;
// bank1_base+=4;
// bank2_base+=4;
// }else[
// ]
// bank1_start+=12;
// bank2_start+=12;
// }
// }
// }
// return 0 ;
// }
// void multi_bank(){
// char** agg_list = malloc(20*sizeof(char*));
// for(int bank1=0; bank1<16; bank1++){
// for(int bank2=(bank1+1); bank2<16; bank2++){
// int bank1_start=0;
// int bank2_start=0;
// int bank1_end=0;
// int bank2_end=0;
// int ndx=0;
// while((bank1_end < bank_pairs_arr.num_pairs[bank1]) && (bank2_end < bank_pairs_arr.num_pairs[bank2])){
// // agg_list[ndx] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[0];
// // agg_list[ndx+1] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
// // agg_list[ndx+2] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[0];
// // agg_list[ndx+3] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[4];
// agg_list[ndx] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[0];
// // agg_list[ndx+1] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
// agg_list[ndx+1] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[0];
// agg_list[ndx+2] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
// agg_list[ndx+3] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[4];
// ndx+=4;
// bank1_end++;
// bank2_end++;
// if(ndx>=20){
// printf("%d aggressors\n", ndx);
// for(int i=0; i<ndx; i++){
// printf("%lx\t", get_physical_addr((u_int64_t) agg_list[i]));
// printf("Bank %ld:\n", get_bank((u_int64_t) agg_list[i]));
// }
// // fill the victims
// fill_victims(bank_pairs_arr.pairs[bank1], bank_pairs_arr.num_pairs[bank1]);
// fill_victims(bank_pairs_arr.pairs[bank2], bank_pairs_arr.num_pairs[bank2]);
// // hammer
// int x = hammer_thp_prehammer(agg_list, 4, 20);
// // scan the victims
// check_victims(bank_pairs_arr.pairs[bank1], bank_pairs_arr.num_pairs[bank1], 0x00);
// check_victims(bank_pairs_arr.pairs[bank2], bank_pairs_arr.num_pairs[bank2], 0x00);
// // reset
// ndx=0;
// // bank1_end = bank1_start+3;
// // bank1_start+=3;
// // bank2_end = bank2_start+3;
// // bank2_start+=3;
// }
// }
// }
// }
// }
int multi_bank(){
char** agg_list = malloc(20*sizeof(char*));
for(int bank1=0; bank1<16; bank1++){
for(int bank2=(bank1+1); bank2<16; bank2++){
int bank1_start=0;
int bank2_start=0;
int bank1_end=0;
int bank2_end=0;
int ndx=0;
while((bank1_end < bank_pairs_arr.num_pairs[bank1]) && (bank2_end < bank_pairs_arr.num_pairs[bank2])){
// agg_list[ndx] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[0];
// agg_list[ndx+1] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
// agg_list[ndx+2] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[0];
// agg_list[ndx+3] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[4];
agg_list[ndx] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[0];
// agg_list[ndx+1] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
agg_list[ndx+1] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[0];
agg_list[ndx+2] = bank_pairs_arr.pairs[bank1][bank1_end].aggressors[4];
agg_list[ndx+3] = bank_pairs_arr.pairs[bank2][bank2_end].aggressors[4];
ndx+=4;
bank1_end++;
bank2_end++;
if(ndx>=20){
// printf("%d aggressors\n", ndx);
// for(int i=0; i<ndx; i++){
// printf("Bank %ld:\n", get_bank((u_int64_t) agg_list[i]));
// }
// fill the victims
fill_victims(bank_pairs_arr.pairs[bank1], bank_pairs_arr.num_pairs[bank1]);
fill_victims(bank_pairs_arr.pairs[bank2], bank_pairs_arr.num_pairs[bank2]);
// hammer
int x = hammer_thp_prehammer(agg_list, 0, 20);
// scan the victims
if(check_victims(bank_pairs_arr.pairs[bank1], bank_pairs_arr.num_pairs[bank1], 0x00)){
return 1;
}
if(check_victims(bank_pairs_arr.pairs[bank2], bank_pairs_arr.num_pairs[bank2], 0x00)){
return 1;
}
// reset
ndx=0;
bank1_end = bank1_start+3;
bank1_start+=3;
bank2_end = bank2_start+3;
bank2_start+=3;
}
}
}
}
return 0;
}
void main(void){
contig_chunk* chunks = malloc( num_blocks* sizeof(contig_chunk));
for(int i=0; i<num_blocks; i++){
chunks[i].bank_contig_rows = malloc(16*sizeof(contig_rows));
}
/// madvise memory///////////////////////////
int alloc = alloc_buffer(chunks, num_blocks);
for(int i=0; i < num_blocks; i++){
printf("%d - (%lx, %lx)\n",i,(uint64_t)chunks[i].base, get_physical_addr((uint64_t)chunks[i].base));
printf("offset %lx\n", chunks[i].virt_offset);
}
///////////////////////////////////
// exit(0);
///////////////////////////// HUGE Page 1GB//////////////////////// --- make sure MEM_SIZE is 1gb
// mem[0] = (char *) mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE| MAP_ANONYMOUS | MAP_HUGETLB| (30 << MAP_HUGE_SHIFT) , -1, 0); //cons 1gb
// memset(mem[0], 0xFF, MEM_SIZE);
// if(check_consecutive(mem[0],MEM_SIZE)){
// printf("cons 1GB\n");
// }else{printf("not cons \n");}
///////////////////////////////////////////////////
// For every bank, for every chunk, create a list of contiguous rows
int tries = 0;
while(tries < 5){
for(int j=0; j < 16; j++){
ds_pair* pairs_array = (ds_pair*) malloc(200*sizeof(ds_pair));
// printf("//////////////////////Conflict for Bank %d////////////////////////////\n", j);
for(int i=0; i < num_blocks; i++){
// printf("-------------Chunk %d------------------------\n", i);
find_bank_conflict(&chunks[i], j);
}
// printf("//////////////////////Create Pairs.....................\n");
int num_pairs = create_hammer_pairs(chunks, j, pairs_array); //create a liist of double sided pairs
bank_pairs_arr.pairs[j] = pairs_array;
bank_pairs_arr.num_pairs[j] = num_pairs;
// int flippy = ten_sided_temp(pairs_array, num_pairs);
}
// setup_bank_rows(chunks);
if(multi_bank()){
exit(0);
}
alloc = alloc_buffer(chunks, num_blocks);
for(int i=0; i<num_blocks; i++){
chunks[i].bank_contig_rows = malloc(16*sizeof(contig_rows));
}
for(int i=0; i < num_blocks; i++){
printf("%d - (%lx, %lx)\n",i,(uint64_t)chunks[i].base, get_physical_addr((uint64_t)chunks[i].base));
printf("offset %lx\n", chunks[i].virt_offset);
}
tries++;
}
// char** conflicts = (char**) malloc(sizeof(char*) * 500);
// printf("################################################################################\n");
// for(int i =0; i< num_blocks; i++){ // this number depends on the memory configuration (how many dimms, ((interleaving)))
// printf("-------------------------BASE Page: %d----------------------\n",i);
// int num_conf = find_bank_conflict((u_int64_t)chunks[i].base ,MEM_SIZE/PAGE_SIZE,0 ,conflicts, chunks[i].virt_offset);
// printf("num conf %d\n", num_conf);
// n_sided_temp(conflicts, num_conf);
// printf("################################################################################\n");
// }
}