-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.H
299 lines (261 loc) · 23.5 KB
/
protocol.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
#pragma once
#include <cstdint>
#include <vector>
#include <unistd.h>
#include <cstdio>
#include <stdlib.h>
#include <fcntl.h>
static const char* NAME = "shared_mem"; // this file will be create under the directory /dev/shm
static const char* NAME_DOUBLE = "shared_mem_double"; // this file will be create under the directory /dev/shm
// this implementation depends on types being integer for the sake of simplicity.
static constexpr int NUM = 240'000'000;
static constexpr int SIZE = 240'000'000 * 2; // *2 for storing CHAs.
static constexpr int DOUBLE_SIZE = NUM * sizeof(double) * 2; // *2 for storing CHAs.
//static unsigned long CACHE_LINE_SIZE = 64;
struct PagemapEntry
{
uint64_t pfn : 55;
unsigned int soft_dirty : 1;
unsigned int file_page : 1;
unsigned int swapped : 1;
unsigned int present : 1;
};
static const std::vector<int> base_sequence_28_skx {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 1, 16, 11, 18, 18, 19, 16, 17, 22, 23, 20,
21, 26, 27, 24, 25, 18, 3, 16, 9, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0,
3, 2, 12, 13, 6, 7, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 15, 14, 5, 4, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 14, 15, 12, 13, 10,
11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 22, 23, 20, 21, 18, 19, 16, 17, 14, 23, 4, 21, 26, 27, 24, 25, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12,
11, 10, 9, 8, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 21, 20, 23, 22, 17, 16, 19, 18, 21, 12, 23, 6, 25, 24, 27, 26, 27, 26, 25,
24, 3, 2, 9, 8, 19, 18, 17, 16, 23, 22, 21, 20, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0,
3, 2, 5, 4, 7, 6, 24, 25, 26, 27, 0, 1, 10, 11, 16, 17, 18, 19, 20, 21, 22, 23, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0, 1, 10, 11, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 3, 2, 9, 8, 2, 3, 0, 1, 6, 7, 4, 5,
10, 11, 8, 9, 14, 15, 12, 13, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 21, 12, 23, 6, 25, 24, 27, 26, 21, 20, 23, 22, 17, 16, 19,
18, 14, 23, 4, 21, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 23, 22, 21, 20, 19, 18,
17, 16, 15, 14, 5, 4, 27, 26, 25, 24, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9,
8, 11, 10, 20, 21, 22, 23, 16, 17, 18, 19, 12, 13, 6, 7, 24, 25, 26, 27, 26, 27, 24, 25, 18, 3, 16, 9, 18, 19, 16, 17, 22, 23, 20, 21, 11, 10, 9, 8,
15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 25, 24, 27, 26, 1, 16, 11, 18, 17, 16, 19,
18, 21, 20, 23, 22, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 7, 22, 13, 20, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 20, 5, 22, 15, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1, 0, 10, 11, 0, 1, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 9, 16, 3, 18, 25, 24, 27, 26, 21, 20, 23, 22, 17, 16, 19, 18,
12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 20, 21, 22, 23, 16, 17, 18, 19, 8, 17, 2, 19, 24, 25, 26, 27, 5, 4, 7, 6, 1, 0, 3,
2, 13, 12, 15, 14, 9, 8, 11, 10, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 23, 22, 21, 20, 19, 18, 17, 16, 19, 10, 17, 0, 27, 26,
25, 24, 25, 24, 27, 26, 21, 4, 23, 14, 17, 16, 19, 18, 21, 20, 23, 22, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 11, 10, 9, 8, 15,
14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 26, 27, 24, 25, 6, 23, 12, 21, 18, 19, 16, 17, 22, 23, 20, 21, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8,
15, 14, 13, 12, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25, 6, 7, 12, 13, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 21, 4, 23, 14, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 19, 10, 17, 0, 27, 26, 25, 24, 23, 22,
21, 20, 19, 18, 17, 16, 8, 17, 2, 19, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2, 21,
20, 23, 22, 17, 16, 19, 18, 9, 16, 3, 18, 25, 24, 27, 26, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 7, 6, 5, 4, 3, 2, 1, 0,
15, 14, 13, 12, 11, 10, 9, 8, 22, 23, 20, 21, 18, 19, 16, 17, 18, 11, 16, 1, 26, 27, 24, 25, 24, 25, 26, 27, 20, 5, 22, 15, 16, 17, 18, 19, 20, 21, 22,
23, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 26, 25, 24, 7, 22,
13, 20, 19, 18, 17, 16, 23, 22, 21, 20, 5, 4, 15, 14, 25, 24, 27, 26, 21, 20, 23, 22, 17, 16, 19, 18, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0,
1, 2, 3, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 6, 7, 12, 13, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 8, 17, 2, 19, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8,
9, 14, 15, 12, 13, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 19, 10, 17, 0, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 26, 27,
24, 25, 10, 11, 0, 1, 18, 19, 16, 17, 22, 23, 20, 21, 25, 24, 27, 26, 9, 8, 3, 2, 17, 16, 19, 18, 21, 20, 23, 22, 8, 9, 10, 11, 12, 13, 14, 15, 0,
1, 2, 3, 4, 5, 6, 7, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 23, 22, 21, 20, 19, 18, 17, 16, 7, 22, 13, 20, 27, 26, 25, 24,
20, 21, 22, 23, 16, 17, 18, 19, 20, 5, 22, 15, 24, 25, 26, 27, 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, 20, 5, 22, 15, 24, 25, 26,
27, 20, 21, 22, 23, 16, 17, 18, 19, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3,
0, 1, 7, 22, 13, 20, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 9, 8, 3, 2, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25,
10, 11, 0, 1, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 26, 25, 24, 19, 10, 17, 0, 19, 18, 17, 16, 23, 22, 21, 20, 24, 25, 26,
27, 8, 17, 2, 19, 16, 17, 18, 19, 20, 21, 22, 23, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14,
13, 12, 11, 10, 9, 8, 22, 23, 20, 21, 18, 19, 16, 17, 6, 7, 12, 13, 26, 27, 24, 25, 21, 20, 23, 22, 17, 16, 19, 18, 5, 4, 15, 14, 25, 24, 27, 26, 4,
5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 19, 2, 17, 8, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 14, 15, 12, 13, 10, 11, 8, 9,
6, 7, 4, 5, 2, 3, 0, 1, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2, 0, 17, 10, 19, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18,
19, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25, 14, 23, 4, 21, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 21, 12, 23, 6, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5,
4, 7, 6, 24, 25, 26, 27, 20, 13, 22, 7, 16, 17, 18, 19, 20, 21, 22, 23, 27, 26, 25, 24, 15, 14, 5, 4, 19, 18, 17, 16, 23, 22, 21, 20, 10, 11, 8, 9,
14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 21, 20, 23, 22, 17, 16, 19, 18, 1, 16, 11,
18, 25, 24, 27, 26, 22, 23, 20, 21, 18, 19, 16, 17, 18, 3, 16, 9, 26, 27, 24, 25, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 18, 3,
16, 9, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 12, 13, 14, 15, 8, 9, 10, 11, 4,
5, 6, 7, 0, 1, 2, 3, 1, 16, 11, 18, 25, 24, 27, 26, 21, 20, 23, 22, 17, 16, 19, 18, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 15, 22, 5, 20,
2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 20, 13, 22, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 25, 24, 27, 26, 21, 12, 23, 6, 17, 16, 19, 18, 21, 20,
23, 22, 26, 27, 24, 25, 14, 23, 4, 21, 18, 19, 16, 17, 22, 23, 20, 21, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 5, 4, 7, 6, 1,
0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, 20, 21, 22, 23, 16, 17, 18, 19, 0, 17, 10, 19, 24, 25, 26, 27, 23, 22, 21, 20, 19, 18, 17, 16, 3, 2, 9, 8,
27, 26, 25, 24, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 27, 26, 25, 24, 15, 22, 5, 20, 19, 18, 17, 16, 23, 22, 21, 20, 10, 11, 8,
9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, 24, 25, 26, 27, 20, 13, 22, 7, 16, 17,
18, 19, 20, 21, 22, 23, 22, 23, 20, 21, 18, 19, 16, 17, 18, 3, 16, 9, 26, 27, 24, 25, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 4,
5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 21, 20, 23, 22, 17, 16, 19, 18, 1, 16, 11, 18, 25, 24, 27, 26, 13, 12, 15, 14, 9, 8, 11, 10,
5, 4, 7, 6, 1, 0, 3, 2, 0, 17, 10, 19, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 19, 2, 17, 8, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17,
16, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20,
23, 22, 25, 24, 27, 26, 21, 12, 23, 6, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25, 14, 15, 4, 5, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15,
14, 13, 12, 26, 27, 24, 25, 14, 23, 4, 21, 18, 19, 16, 17, 22, 23, 20, 21, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11,
12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 25, 24, 27, 26, 21, 12, 23, 6, 17, 16, 19, 18, 21, 20, 23, 22, 23, 22, 21, 20, 19, 18, 17, 16, 19, 2, 17,
8, 27, 26, 25, 24, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, 20, 21,
22, 23, 16, 17, 18, 19, 0, 17, 10, 19, 24, 25, 26, 27, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 1, 16, 11, 18, 25, 24, 27, 26, 21,
20, 23, 22, 17, 16, 19, 18, 2, 3, 8, 9, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 20, 13, 22, 7, 19, 18, 17, 16, 23, 22, 21,
20, 27, 26, 25, 24, 15, 22, 5, 20, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 25, 24, 27, 26, 9, 16, 3, 18, 17, 16, 19, 18, 21, 20,
23, 22, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 26, 27, 24, 25, 18,
11, 16, 1, 18, 19, 16, 17, 22, 23, 20, 21, 20, 21, 22, 23, 16, 17, 18, 19, 4, 5, 14, 15, 24, 25, 26, 27, 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14,
9, 8, 11, 10, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13, 10, 11, 8, 9, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 13, 12, 27, 26, 25, 24, 15, 14, 13,
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 6, 23, 12, 21, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 21, 4, 23, 14, 25, 24, 27, 26, 21, 20,
23, 22, 17, 16, 19, 18, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 19,
18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 11, 10, 1, 0, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 8, 9, 2, 3, 1, 0, 3, 2, 5, 4, 7, 6,
9, 8, 11, 10, 13, 12, 15, 14, 24, 25, 26, 27, 8, 9, 2, 3, 16, 17, 18, 19, 20, 21, 22, 23, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7,
6, 10, 11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 26, 25, 24, 11, 10, 1, 0, 19, 18, 17, 16, 23, 22, 21, 20, 21, 20, 23, 22, 17, 16,
19, 18, 21, 4, 23, 14, 25, 24, 27, 26, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11,
10, 9, 8, 22, 23, 20, 21, 18, 19, 16, 17, 6, 23, 12, 21, 26, 27, 24, 25, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 7, 6, 13, 12,
27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 4, 5, 14, 15, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7,
6, 1, 0, 3, 2, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25, 18, 11, 16, 1, 17, 16,
19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 9, 16, 3, 18, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 6, 7, 4, 5, 2, 3, 0, 1, 14,
15, 12, 13, 10, 11, 8, 9, 23, 22, 21, 20, 19, 18, 17, 16, 11, 10, 1, 0, 27, 26, 25, 24, 20, 21, 22, 23, 16, 17, 18, 19, 8, 17, 2, 19, 24, 25, 26, 27,
5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 26, 27, 24, 25, 6, 23, 12,
21, 18, 19, 16, 17, 22, 23, 20, 21, 25, 24, 27, 26, 21, 4, 23, 14, 17, 16, 19, 18, 21, 20, 23, 22, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5,
6, 7, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 20, 5, 22, 15, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 2, 3, 0, 1, 6,
7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26, 25, 24, 7, 22, 13, 20, 9, 16, 3, 18, 25, 24, 27, 26, 21, 20, 23, 22,
17, 16, 19, 18, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 18, 11, 16,
1, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 22, 23, 20, 21, 18, 19, 16, 17, 18, 11,
16, 1, 26, 27, 24, 25, 21, 20, 23, 22, 17, 16, 19, 18, 9, 16, 3, 18, 25, 24, 27, 26, 4, 5, 6, 7, 0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 10,
11, 8, 9, 14, 15, 12, 13, 2, 3, 0, 1, 6, 7, 4, 5, 27, 26, 25, 24, 7, 6, 13, 12, 19, 18, 17, 16, 23, 22, 21, 20, 24, 25, 26, 27, 20, 5, 22, 15,
16, 17, 18, 19, 20, 21, 22, 23, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 21, 4, 23,
14, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12, 18, 19, 16, 17, 22, 23,
20, 21, 26, 27, 24, 25, 6, 23, 12, 21, 8, 17, 2, 19, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1,
0, 3, 2, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 19, 10, 17, 0, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 4, 5, 6, 7,
0, 1, 2, 3, 12, 13, 14, 15, 8, 9, 10, 11, 21, 20, 23, 22, 17, 16, 19, 18, 13, 12, 7, 6, 25, 24, 27, 26, 22, 23, 20, 21, 18, 19, 16, 17, 14, 15, 4,
5, 26, 27, 24, 25, 7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8, 9, 8, 11, 10, 13, 12, 15, 14, 1, 0, 3, 2, 5, 4, 7, 6, 24, 25,
26, 27, 0, 17, 10, 19, 16, 17, 18, 19, 20, 21, 22, 23, 27, 26, 25, 24, 19, 2, 17, 8, 19, 18, 17, 16, 23, 22, 21, 20, 10, 11, 8, 9, 14, 15, 12, 13, 2,
3, 0, 1, 6, 7, 4, 5, 18, 19, 16, 17, 22, 23, 20, 21, 26, 27, 24, 25, 2, 3, 8, 9, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 25, 24, 27, 26, 1, 0, 11, 10, 15, 22, 5, 20, 27, 26, 25,
24, 23, 22, 21, 20, 19, 18, 17, 16, 14, 15, 12, 13, 10, 11, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1, 13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0,
3, 2, 20, 13, 22, 7, 24, 25, 26, 27, 20, 21, 22, 23, 16, 17, 18, 19, 5, 4, 7, 6, 1, 0, 3, 2, 13, 12, 15, 14, 9, 8, 11, 10, 20, 21, 22, 23, 16,
17, 18, 19, 20, 13, 22, 7, 24, 25, 26, 27, 23, 22, 21, 20, 19, 18, 17, 16, 15, 22, 5, 20, 27, 26, 25, 24, 6, 7, 4, 5, 2, 3, 0, 1, 14, 15, 12, 13,
10, 11, 8, 9, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 25, 24, 27, 26, 1, 0, 11, 10, 17, 16, 19, 18, 21, 20, 23, 22, 26, 27, 24,
25, 2, 3, 8, 9, 18, 19, 16, 17, 22, 23, 20, 21, 11, 10, 9, 8, 15, 14, 13, 12, 3, 2, 1, 0, 7, 6, 5, 4, 19, 18, 17, 16, 23, 22, 21, 20, 27, 26,
25, 24, 19, 2, 17, 8, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13, 1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 0, 17, 10, 19, 14, 15, 4, 5, 26, 27, 24, 25, 22, 23, 20, 21, 18, 19, 16, 17, 15, 14, 13, 12, 11, 10, 9, 8,
7, 6, 5, 4, 3, 2, 1, 0, 12, 13, 14, 15, 8, 9, 10, 11, 4, 5, 6, 7, 0, 1, 2, 3, 13, 12, 7, 6, 25, 24, 27, 26, 21, 20, 23, 22, 17, 16, 19,
18};
uint64_t compute_perm(uintptr_t physical_address)
{
// SPDLOG_TRACE("0b{:064b} (0x{:016x}) --> physical address, input to compute_perm func.", physical_address, physical_address);
// static const uint64_t SelectorMasks[14] = {0x4c8fc0000, 0x1d05380000, 0x262b8c0000, 0x41f500000, 0x2c6d780000,
// 0x2cd5140000, 0x21d80c0000, 0x3b3f480000, 0x3a03500000, 0x3033280000,
// 0x0, 0x1469b40000, 0x0, 0x0};
static const uint64_t SelectorMasks[14] = {0x32770c0000,
0x3433d40000,
0x39a2900000,
0x3857680000,
0x1ad2880000,
0x1a6ae40000,
0x2b2fc40000,
0x24b6540000,
0x3a03500000,
0xc7b100000,
0xaf7c80000,
0x28218c0000,
0x0,
0x0};
uint64_t computed_perm = 0;
for (int bit = 0; bit < 14; ++bit)
{
auto permutation_selector_mask = SelectorMasks[bit];
// SPDLOG_TRACE("Selector mask: 0b{:b} --> binary; 0x{:x} --> hexadecimal", permutation_selector_mask, permutation_selector_mask);
uint64_t k = permutation_selector_mask & physical_address; // bitwise AND with mask
// SPDLOG_TRACE("will AND below 2 numbers.");
// SPDLOG_TRACE("0b{:064b} (0x{:016x}) -> permutation_selector_mask", permutation_selector_mask, permutation_selector_mask);
// SPDLOG_TRACE("0b{:064b} (0x{:016x}) -> physical_address", physical_address, physical_address);
// SPDLOG_TRACE("0b{:064b} (0x{:016x}) -> AND (&) result.", k, k);
uint64_t j = __builtin_popcountl(k); // count number of bits set
// SPDLOG_TRACE("Number of bits in 0b{:b} : {}", k, j);
uint64_t i = j % 2; // compute parity
// SPDLOG_TRACE("Parity of 0b{:b}: {}", j, i);
computed_perm += (i << bit); // scale and accumulate
// SPDLOG_TRACE("computed permutation += Parity ({}) << {} --> 0b{:b}", i, bit, computed_perm);
}
// SPDLOG_TRACE("Computed permutation for physical address 0x{:x}: {}", physical_address, computed_perm);
return (computed_perm); /// why parentheses around variable here?
}
int pagemap_get_entry(struct PagemapEntry *entry, int pagemap_fd, uintptr_t vaddr)
{
size_t nread;
ssize_t ret;
uint64_t data;
uintptr_t vpn;
vpn = vaddr / sysconf(_SC_PAGE_SIZE);
nread = 0;
while (nread < sizeof(data))
{
ret = pread(pagemap_fd, ((uint8_t *) &data) + nread, sizeof(data) - nread, vpn * sizeof(data) + nread);
nread += ret;
if (ret <= 0)
{
// SPDLOG_ERROR("pread() error: {}", strerror(errno));
return 1;
}
}
entry->pfn = data & (((uint64_t) 1 << 55) - 1);
entry->soft_dirty = (data >> 55) & 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, pid_t pid, uintptr_t vaddr)
{
char pagemap_file[BUFSIZ];
int pagemap_fd;
snprintf(pagemap_file, sizeof(pagemap_file), "/proc/%ju/pagemap", (uintmax_t) pid);
pagemap_fd = open(pagemap_file, O_RDONLY);
if (pagemap_fd < 0)
{
// SPDLOG_ERROR("Could not open() file {}. Error: {}", pagemap_file, strerror(errno));
return 1;
}
struct PagemapEntry entry;
const int pagemap_entry = pagemap_get_entry(&entry, pagemap_fd, vaddr);
// SPDLOG_TRACE("pagemap entry: {}", pagemap_entry);
if (pagemap_entry)
{
return 1;
}
close(pagemap_fd);
*paddr = (entry.pfn * sysconf(_SC_PAGE_SIZE)) + (vaddr % sysconf(_SC_PAGE_SIZE));
return 0;
}
uintptr_t getPhysicalAddress(uintptr_t virtual_address)
{
const pid_t pid = getpid();
uintptr_t physical_address = 0;
// SPDLOG_TRACE("getting physical address for virtual address (0x{:016x})", virtual_address);
if (virt_to_phys_user(&physical_address, pid, virtual_address))
{
// SPDLOG_ERROR("error: virt_to_phys_user");
return EXIT_FAILURE;
};
return physical_address;
}
uint64_t getIndex(uintptr_t physical_address)
{
/// 2^m = baseSequenceLength. m = 12 on SKX with 18 cores.
/// Therefore "index" bits are 17:6. -> 12 bits in total represent index value.
// SPDLOG_TRACE("{} is called for physical address 0x{:x} (0b{:b}).", __PRETTY_FUNCTION__, physical_address, physical_address);
physical_address = physical_address >> 6;
// SPDLOG_TRACE("shifted right to 6 bits: 0b{:b}", physical_address);
int index_mask =
0xFFF; /// this FFF value makes the code non-portable to other machines. it is ideal that this value is constructed using base sequence length.
uint64_t index = physical_address & index_mask;
// SPDLOG_TRACE("index after mask is applied: 0b{:b}", index);
// SPDLOG_TRACE("physi -> 0b{:064b}", physical_address);
// SPDLOG_TRACE("index -> 0b{:064b}, 0x{:x}, {}", index, index, index);
return index;
}
int findCHAByHashing(uintptr_t virtual_address, const std::vector<int> &base_sequence)
{
const pid_t pid = getpid();
uintptr_t physical_address = 0;
//printf("getting physical address for virtual address (0x{:016x}, 0b{:b})\n", virtual_address, virtual_address);
if (virt_to_phys_user(&physical_address, pid, virtual_address))
{
// SPDLOG_ERROR("error: virt_to_phys_user");
return EXIT_FAILURE;
};
const auto computed_perm = compute_perm(physical_address);
const auto physical_address_index = getIndex(physical_address);
const auto base_sequence_index = computed_perm ^ physical_address_index; /// XOR'ing.
// assert(base_sequence_index < 4096 && "Base sequence must be lower than 4096!");
const int cha_by_hashing = base_sequence[base_sequence_index];
//printf("returning cha_by_hashing\n");
return cha_by_hashing;
}