-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscan.c
184 lines (159 loc) · 4.74 KB
/
scan.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
/* ----------------------------------------------------------------------- *
*
* Copyright 2016 Clémentine Maurice
* Copyright 2021 Guillaume Didier
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston MA 02110-1301, USA; either version 2 of the License, or
* (at your option) any later version. *
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ----------------------------------------------------------------------- */
#define _GNU_SOURCE
#include <cpuid.h>
#include <getopt.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "arch.h"
#include "global_variables.h"
#include "monitoring.h"
#include "poke.h"
#include "rdmsr.h"
#include "scan.h"
#include "util.h"
#include "wrmsr.h"
#define HUGE_PAGE_SIZE (1 * 1024 * 1024 * 1024)
void print_help() {
fprintf(stderr,
" >> Usage: sudo ./scan -c nb_cores -a (sdb|ivb|hsw|core)\n");
}
/*
* Declare architecture- and core-dependent variables
*/
int verbose = 0;
int clflush = 0;
int main(int argc, char **argv) {
/*
* Verify CPU is Intel
*/
if (!is_intel()) {
fprintf(stderr,"CPU is not Intel\n");
exit(EXIT_FAILURE);
}
/*
* Pin to core
*/
int cpu_mask = 0;
cpu_set_t my_set;
CPU_ZERO(&my_set);
CPU_SET(cpu_mask, &my_set);
sched_setaffinity(0, sizeof(cpu_set_t), &my_set);
/*
* Extract CPU information: micro-arch name and number of cores
* https://en.wikichip.org/wiki/intel/cpuid
*/
nb_cores = cores_per_package();
int cpu_model = get_cpu_model();
/*
* Options
*/
int opt;
while ((opt = getopt(argc, argv, "hfv")) != -1) {
switch (opt) {
case 'h':
print_help();
exit(1);
case 'f':
clflush = 1;
break;
case 'v':
verbose = 1;
break;
default:
print_help();
exit(1);
}
}
if (determine_class_uarch(cpu_model) < 0 && !clflush) {
exit(EXIT_FAILURE);
}
/*
* Initialize architecture-dependent variables
*/
if (setup_perf_counters(class, archi, nb_cores) < 0 && !clflush) {
exit(EXIT_FAILURE);
}
/*
* Verify number of cores is coherent with micro-architecture
*/
if (nb_cores > max_slices) {
fprintf(
stderr,
"Specified number of cores (%d) incoherent with maximum number of "
"core of specified micro-architecure (%d for %s). \n",
nb_cores, max_slices, uarch_names[archi]);
print_help();
exit(1);
}
printf("Micro-architecture: %s\n", uarch_names[archi]);
printf("Number of cores: %d\n", nb_cores);
/*
* Allocate and initialize memory for monitoring addresses
*/
unsigned long long i;
unsigned long long const stride = 64;
unsigned long long const nb_loops = 100;
// For 4kB pages
char *mem = (char *)malloc(nb_loops * stride);
if (mem == NULL) {
printf("Malloc has failed \n");
return -1;
} else {
printf("[+] Allocated memory\n");
}
// For huge pages ()
/*char * mem = (char*)mmap(NULL, HUGE_PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE | MAP_HUGETLB, - 1, 0); if (mem
== NULL) { printf("Malloc huge page has failed \n"); } else { printf("[+]
Yay, allocated 1GB\n");
}*/
for (i = 0; i < nb_loops * stride; i++) {
mem[i] = 12;
}
printf("[+] Initialized memory\n");
/*
* Monitor addresses
*/
if (clflush) {
for (i = 0; i < nb_loops; i++) {
monitor_single_address_clflush((uintptr_t)mem + (i * stride), 1);
}
} else if (class == INTEL_CORE) {
for (i = 0; i < nb_loops; i++) {
monitor_single_address_core((uintptr_t)mem + (i * stride), 1);
}
} else {
for (i = 0; i < nb_loops; i++) {
monitor_single_address_fast((uintptr_t)mem + (i * stride));
}
}
// munmap(mem, HUGE_PAGE_SIZE);
free(mem);
return 0;
}