forked from flyrain/system_map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPgd.c
435 lines (379 loc) · 11.8 KB
/
getPgd.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
/*
============================================================================
Name : getPgd.c
Author : YufeiGu
Version :
Copyright : Copyright 2012 by UTD. all rights reserved. This material may
be freely copied and distributed subject to inclusion of this
copyright notice and our World Wide Web URL http://www.utdallas.edu
Description : get pgd address from any mem
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <sys/times.h>
#include <stdint.h>
int potential_pgd;
int real_pgd;
int potential_pgd_time;
int real_pgd_time;
//determine whether two memory match each other,
//if match rate less and equal to 0.02 then return 0 (Match)
//else return 1 (No match)
int isEqual(char *mem, unsigned paddr1, unsigned paddr2, int length) {
int i;
int noMatchCount = 0;
unsigned value1s[512];
unsigned value2s[512];
int indexs[512];
for (i = 0; i < length; i = i + 4) {
unsigned value1 = *(unsigned *) ((unsigned) mem + paddr1 + i);
unsigned value2 = *(unsigned *) ((unsigned) mem + paddr2 + i);
if (value1 != value2) {
value1s[noMatchCount] = value1;
value2s[noMatchCount] = value2;
indexs[noMatchCount] = i;
noMatchCount++;
}
}
float matchRate = (float) noMatchCount / (float) (length / 4);
//rate less than 0.02
if (matchRate <= 0.02) {
// printf("paddr1:0x%x vs paddr2:0x%x noMatchcount:%d rate:%4.2f\n", paddr1, paddr2,
// noMatchCount, (float) noMatchCount / (float) (length / 4));
// for (i = 0; i < noMatchCount; i++) {
// printf("0x%x: %x %x \n", indexs[i]+2048, value1s[i], value2s[i]);
// }
return 0;
} else
return 1;
}
//print pde item in the pgd
void printPdeItem(unsigned startAddr, char *mem, int mem_size) {
int j;
//scan high 2k, from 0x800
j = 2 * 1024;
int pdeItemNo = 0;
for (; j < 4 * 1024; j = j + 4) {
unsigned pgdCandidate = *(unsigned *) ((unsigned) mem + startAddr + j);
if ((pgdCandidate & ~0xfff) > mem_size)
continue;
//present,first bit;readonly, second bit, accessed, sixth bit; global, ninth bit.
//linux 1e3, winxp 163, win7 63, freebsd 63,so get their common part: 63
//but, solaris 27
// if ((pgdCandidate & 0xf) == 0x3 && (pgdCandidate & 0x60) == 0x60) {
if ((pgdCandidate & 0x3) == 0x3 && (pgdCandidate & 0x20) == 0x20
&& (pgdCandidate & 0x618) == 0) {
pdeItemNo++;
}
}
printf("pde item no: %d\n", pdeItemNo);
FILE *out_data;
out_data = fopen("PGDData", "a+");
extern char * snapshot;
fprintf(out_data, "%d\t%s\n", pdeItemNo, snapshot);
fclose(out_data);
}
void printAllRealPgd(int pgdcount, int countedpages[pgdcount], int maxIndex,
int potengtialPdgArray[pgdcount], int pageSize) {
int i;
//printf all real pgds
for (i = 0; i < pgdcount; i++) {
if (countedpages[i] == maxIndex) {
printf("pgd physical address:0x%x\n",
potengtialPdgArray[i] * pageSize);
}
}
}
//get real pgd from potential pgds
unsigned getPgdReal(int pgdcount, int potengtialPdgArray[pgdcount], int pageSize, char *mem) {
//get real pgd page by compare all field of 3*1024 to 4*1024
unsigned pgdPhyAddr = 0;
//array to record match number,initiate all to 0
int matchNumber[pgdcount];
int i;
for (i = 0; i < pgdcount; i++) {
matchNumber[i] = 0;
}
//array to record the page which have been count as one of the same pages
//if i is handled, set countedpages[i] by the first same index, else countedpages[i] is -1
int countedpages[pgdcount];
for (i = 0; i < pgdcount; i++) {
countedpages[i] = -1;
}
//find the max match, and the physical address of pgd
for (i = 0; i < pgdcount; i++) {
//if page i is handled, do nothing
if (countedpages[i] >= 0)
continue;
int j;
//compare i and i+1,i+2...pgdcount-1
for (j = i + 1; j < pgdcount; j++) {
//if page j is handled, do nothing
if (countedpages[j] >= 0)
continue;
//compare i and j
int startIndex = 2;
unsigned paddr1 = potengtialPdgArray[i] * pageSize + startIndex * 1024;
unsigned paddr2 = potengtialPdgArray[j] * pageSize + startIndex * 1024;
int ret = isEqual(mem, paddr1, paddr2, (4 - startIndex) * 1024);
if (ret == 0) {
matchNumber[i]++;
countedpages[j] = i;
}
}
}
//if current match number is bigger than max Match number,
//then update max Match number, and record the phycial address
int maxMatch = 0;
int maxIndex = 0;
for (i = 0; i < pgdcount; i++) {
if (matchNumber[i] > maxMatch) {
maxMatch = matchNumber[i];
maxIndex = i;
}
}
pgdPhyAddr = potengtialPdgArray[maxIndex] * pageSize;
//printf all real pgds
// printAllRealPgd(pgdcount, countedpages, maxIndex, potengtialPdgArray, pageSize);
real_pgd = maxMatch + 1;
printf("Real PGD Number is %d, PGD physical address:0x%x\n", maxMatch + 1, pgdPhyAddr);
return pgdPhyAddr;
}
//check whether pte is valid, a real pte
int isPteValid(unsigned pteStart, char *mem, int mem_size) {
int isValid = 1;
// printf("pdeItem:%x, pte Start: %x\n", pdeItem, pteStart);
int k = 0;
int matchCount = 0;
for (; k < 4 * 1024; k += 4) {
unsigned pte = *(unsigned *) ((unsigned) mem + pteStart + k);
// printf("pte:%x\n",pte);
if ((pte & 0x1) == 0x1 && (pte & ~0xfff) > mem_size) {
continue;
}
if (pte == 0)
continue;
int us = pte & (1 << 2);
int g = pte & (1 << 8);
//if find one match, then get this page break;
// if ((pte & 0x1) == 0x1 && (pte & 0x20) == 0x20 && (pte & 0x618) == 0) {
// if ((pte & 0x1) == 0x1 && (pte & 0x618) == 0) {
if ((pte & 0x1) == 0x1 && us == 0 && g == 256) {
matchCount++;
} else {
isValid = 0;
break;
}
}
if (matchCount == 0)
isValid = 0;
return isValid;
}
/* get potential pgd page
* return number of potential pgd*/
int getPotentialPgd(int totalPageNumber, int pageSize, char *mem, int pageIndex[totalPageNumber],
int mem_size) {
int pgdcount = 0;
int i;
for (i = 0; i < totalPageNumber; i++) {
int startAddr = i * pageSize;
int j;
int isPgd = 1;
for (j = 0; j < 2 * 1024; j = j + 4) {
unsigned pdeItem = *(unsigned *) ((unsigned) mem + startAddr + j);
if ((pdeItem & 0x418) != 0) {
isPgd = 0;
break;
}
}
if (isPgd == 0)
continue;
//scan high 2k, from 0x800
j = 2 * 1024;
int matchCount = 0;
for (; j < 4 * 1024; j = j + 4) {
unsigned pdeItem = *(unsigned *) ((unsigned) mem + startAddr + j);
if (pdeItem == 0 || (pdeItem & ~0xfff) > mem_size)
continue;
if ((pdeItem & 0x418) != 0) {
break;
}
#ifdef OLD_PGD_MATCH
//present,first bit;readonly, second bit, accessed, sixth bit; global, ninth bit.
//linux 1e3, winxp 163, win7 63, freebsd 63,so get their common part: 63
//but, solaris 27
if ((pdeItem & 0x3) == 0x3 && (pdeItem & 0x20) == 0x20 && (pdeItem & 0x618) == 0) {
proCount++;
//if at least 30 value is match,then put it in potential pgd
if (proCount > 30) {
//this page is potential pgd
pageIndex[i] = 1;
pgdcount++;
break;
}
}
#endif
if ((pdeItem & 0x3) == 0x3 && (pdeItem & 0x20) == 0x20 && (pdeItem & 0x418) == 0) {
//check pte
int isValid = isPteValid(pdeItem & ~0xfff, mem, mem_size);
if (isValid == 1)
matchCount++;
} else {
//too strong
// isPgd = 0;
// break;
}
}
//detemine whether this page is a pgd
if (isPgd == 1 && matchCount >= 1) {
pageIndex[i] = 1;
pgdcount++;
}
}
printf("potential pdg count: %d\n", pgdcount);
return pgdcount;
}
//abundant
int getMorePotentialPgd(int totalPageNumber, int pageSize, char *mem, int pageIndex[],
int pageIndex2[], int mem_size) {
int pgdcount = 0;
int i;
for (i = 0; i < totalPageNumber; i++) {
if (pageIndex[i] != 1)
continue;
int startAddr = i * pageSize;
int j;
j = 2 * 1024;
int proCount = 0;
for (; j < 4 * 1024; j = j + 4) {
unsigned pgdCandidate = *(unsigned *) ((unsigned) mem + startAddr + j);
if ((pgdCandidate & ~0xfff) > mem_size)
continue;
int pdeStart = pgdCandidate & ~0xfff;
int k = 0;
int isRealPde = 1;
for (; k < 4 * 1024; k += 4) {
unsigned pde = *(unsigned *) ((unsigned) mem + pdeStart + k);
if ((pde & 0x1) == 0x1 && (pde & ~0xfff) > mem_size) {
isRealPde = 0;
break;
}
// if ((pde & 0x3) == 0x3 && (pde & ~0xfff) < mem_size) {
// //check pte
// int index = 0;
// unsigned pteStart = (pde & ~0xfff);
// for (; index < 4 * 1024; index += 4) {
// unsigned pte = *(unsigned *) ((unsigned) mem + pteStart
// + index);
// if ((pte & 0x1) == 0x1) {
// if (!((pte & 0x100) == 0x100
// && (pte & ~0xfff) < mem_size)) {
// isRealPde = 0;
// break;
// }
// }
// }
// if (isRealPde == 0)
// break;
// }
}
if (isRealPde == 0)
continue;
//present,first bit;readonly, second bit, accessed, sixth bit; global, ninth bit.
//linux 1e3, winxp 163, win7 63, freebsd 63,so get their common part: 63
//but, solaris 27
// if ((pgdCandidate & 0xf) == 0x3 && (pgdCandidate & 0x60) == 0x60) {
if ((pgdCandidate & 0x3) == 0x3 && (pgdCandidate & 0x20) == 0x20
&& (pgdCandidate & 0x618) == 0) { //for solaris
proCount++;
//if at least 30 value is match,then put it in potential pgd
if (proCount > 30) {
//this page is potential pgd
pageIndex2[i] = 1;
pgdcount++;
break;
}
}
}
}
printf("potential pdg count2: %d\n", pgdcount);
return pgdcount;
}
long long timeval_diff(struct timeval *difference, struct timeval *end_time,
struct timeval *start_time) {
struct timeval temp_diff;
if (difference == NULL)
{
difference = &temp_diff;
}
// printf("start_time tv_sec %ld,tv_usec %ld\n",start_time->tv_sec,start_time->tv_usec);
// printf("end_time tv_sec %ld,tv_usec %ld\n",end_time->tv_sec,end_time->tv_usec);
difference->tv_sec = end_time->tv_sec - start_time->tv_sec;
difference->tv_usec = end_time->tv_usec - start_time->tv_usec;
/* Using while instead of if below makes the code slightly more robust. */
while (difference->tv_usec < 0) {
difference->tv_usec += 1000000;
difference->tv_sec -= 1;
}
// printf("tv_sec %ld,tv_usec %ld\n",difference->tv_sec,difference->tv_usec);
return 1000000LL * difference->tv_sec + difference->tv_usec;
} /* timeval_diff() */
unsigned getPgd(char * mem, int mem_size) {
int pageSize = 4 * 1024;
int totalPageNumber = mem_size / (4 * 1024); //assume that every page has 4k
int pageIndex[totalPageNumber];
struct timeval earlier;
struct timeval later;
if (gettimeofday(&earlier, NULL)) {
perror("gettimeofday() error");
exit(1);
}
int pgdcount = getPotentialPgd(totalPageNumber, pageSize, mem, pageIndex, mem_size);
potential_pgd = pgdcount;
if (gettimeofday(&later, NULL)) {
perror("gettimeofday() error");
exit(1);
}
// pgdcount = getMorePotentialPgd(totalPageNumber, pageSize, mem, pageIndex,
// pageIndex2, mem_size);
//generate the potential pgd index array
int potengtialPdgArray[pgdcount];
int i, j = 0;
for (i = 0; i < totalPageNumber; i++) {
if (pageIndex[i] == 1) {
potengtialPdgArray[j] = i;
j++;
}
}
potential_pgd_time = timeval_diff(NULL, &later, &earlier) / 1000;
printf("difference is %d milliseconds\n", potential_pgd_time);
//print the potential pgd physical address
// for (i = 0; i < pgdcount; i++) {
// printf("pgd physical address:0x%x\n", potengtialPdgArray[i] * pageSize);
// }
//get real pgd page by compare all field of 3*1024 to 4*1024
if (gettimeofday(&earlier, NULL)) {
perror("gettimeofday() error");
exit(1);
}
unsigned pgd = getPgdReal(pgdcount, potengtialPdgArray, pageSize, mem);
if (gettimeofday(&later, NULL)) {
perror("gettimeofday() error");
exit(1);
}
real_pgd_time = timeval_diff(NULL, &later, &earlier) / 1000;
printf("difference is %d milliseconds\n", real_pgd_time);
//record data;
FILE *out_data;
out_data = fopen("outDataPgd", "a+");
extern char *snapshot;
fprintf(out_data, "%d\t%d\t%s\n", potential_pgd, real_pgd, snapshot);
fclose(out_data);
//end record data
// printPdeItem(pgd,mem,mem_size);
return pgd;
}