This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_4.c
executable file
·74 lines (65 loc) · 2.3 KB
/
test_4.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
#include "param.h"
#include "types.h"
#include "stat.h"
#include "user.h"
#include "ptentry.h"
#define PGSIZE 4096
#define PGROUNDDOWN(a) (((a)) & ~(PGSIZE-1))
static int
err(char *msg, ...) {
printf(1, "XV6_TEST_OUTPUT %s\n", msg);
exit();
}
int
main(void){
const uint PAGES_NUM = 1;
char *buffer = sbrk(PGSIZE * sizeof(char));
while ((uint)buffer != 0x6000)
buffer = sbrk(PGSIZE * sizeof(char));
// Allocate one pages of space
char *ptr = sbrk(PAGES_NUM * PGSIZE);
struct pt_entry pt_entries[PAGES_NUM];
// Initialize the pages
for (int i = 0; i < PAGES_NUM * PGSIZE; i++)
ptr[i] = 0xAA;
// Call the mencrypt
if (mencrypt(ptr, PAGES_NUM) != 0)
err("mencrypt return non-zero value when encrypting a valid page\n");
if (getpgtable(pt_entries, PAGES_NUM) >= 0){
for (int i = 0; i < PAGES_NUM; i++) {
printf(1, "XV6_TEST_OUTPUT Index %d: pdx: 0x%x, ptx: 0x%x, present: %d, writable: %d, encrypted: %d\n",
i,
pt_entries[i].pdx,
pt_entries[i].ptx,
pt_entries[i].present,
pt_entries[i].writable,
pt_entries[i].encrypted
);
if (dump_rawphymem((uint)(pt_entries[i].ppage * PGSIZE), buffer) != 0)
err("dump_rawphymem return non-zero value\n");
uint expected = ~0xAA;
uint is_failed = 0;
for (int j = 0; j < PGSIZE; j ++) {
if (buffer[j] != (char)expected) {
is_failed = 1;
break;
}
}
if (is_failed) {
printf(1, "XV6_TEST_OUTPUT wrong content at physical page 0x%x\n", pt_entries[i].ppage * PGSIZE);
for (int j = 0; j < PGSIZE; j +=64) {
printf(1, "XV6_TEST_OUTPUT ");
for (int k = 0; k < 64; k ++) {
if (k < 63) {
printf(1, "0x%x ", (uint)buffer[j + k] & 0xFF);
} else {
printf(1, "0x%x\n", (uint)buffer[j + k] & 0xFF);
}
}
}
err("physical memory is encrypted incorrectly\n");
}
}
}
exit();
}