-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
165 lines (145 loc) · 5.45 KB
/
main.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
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mergeable-pages.h"
int main() {
rmpinit(0UL, 4096UL);
cur_asid = 0;
hcr3 = -1;
const uint64_t HCR3 = 4096UL * 4;
const uint64_t NCR3_1 = 4096UL * 5;
const uint64_t NCR3_2 = 4096UL * 6;
const uint64_t GCR3 = 4096UL * 0;
// set up host page table
pte_t *hpt = (pte_t *)HCR3;
for (int i = 0; i < 512; i++)
WRITE(&hpt[i], (uint64_t)i << 12 | PTE_PRESENT | PTE_WRITE);
hcr3 = HCR3;
// set up nested page tables
pte_t *npt1 = (pte_t *)NCR3_1;
WRITE(&npt1[0], 7 * 4096 | PTE_PRESENT | PTE_PRIVATE | PTE_WRITE);
WRITE(&npt1[1], 8 * 4096 | PTE_PRESENT | PTE_MERGEABLE | PTE_WRITE);
WRITE(&npt1[2], 9 * 4096 | PTE_PRESENT | PTE_MERGEABLE);
WRITE(&npt1[3], 10 * 4096 | PTE_PRESENT | PTE_MERGEABLE);
rmpupdate(7UL * 4096, 0UL * 4096, 1, RMPE_PRIVATE);
rmpupdate(8UL * 4096, 1UL * 4096, 1, RMPE_MERGEABLE);
rmpupdate(9UL * 4096, 2UL * 4096, 1, RMPE_MERGEABLE);
rmpupdate(10UL * 4096, 3UL * 4096, 1, RMPE_MERGEABLE);
pte_t *npt2 = (pte_t *)NCR3_2;
WRITE(&npt2[0], 11 * 4096 | PTE_PRESENT | PTE_PRIVATE | PTE_WRITE);
WRITE(&npt2[1], 12 * 4096 | PTE_PRESENT | PTE_MERGEABLE | PTE_WRITE);
WRITE(&npt2[2], 13 * 4096 | PTE_PRESENT | PTE_MERGEABLE);
WRITE(&npt2[3], 14 * 4096 | PTE_PRESENT | PTE_MERGEABLE);
rmpupdate(11UL * 4096, 0UL * 4096, 2, RMPE_PRIVATE);
rmpupdate(12UL * 4096, 1UL * 4096, 2, RMPE_MERGEABLE);
rmpupdate(13UL * 4096, 2UL * 4096, 2, RMPE_MERGEABLE);
rmpupdate(14UL * 4096, 3UL * 4096, 2, RMPE_MERGEABLE);
// switch into VM1
cur_asid = 1;
ncr3 = NCR3_1;
gcr3 = -1;
// in VM1
// set up guest page table
pte_t *gpt1 = (pte_t *)GCR3;
pvalidate((uint64_t)&gpt1[0], RMPE_PRIVATE);
WRITE(&gpt1[0], 0UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_PRIVATE);
WRITE(&gpt1[1], 1UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
WRITE(&gpt1[2], 2UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
WRITE(&gpt1[3], 3UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
pvalidate(1UL * 4096, RMPE_MERGEABLE);
pvalidate(2UL * 4096, RMPE_MERGEABLE);
pvalidate(3UL * 4096, RMPE_MERGEABLE);
gcr3 = GCR3;
// switch into VM2
cur_asid = 2;
ncr3 = NCR3_2;
gcr3 = -1;
// in VM2
// set up guest page table
pte_t *gpt2 = (pte_t *)GCR3;
pvalidate((uint64_t)&gpt2[0], RMPE_PRIVATE);
WRITE(&gpt2[0], 0UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_PRIVATE);
WRITE(&gpt2[1], 1UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
WRITE(&gpt2[2], 2UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
WRITE(&gpt2[3], 3UL * 4096 | PTE_PRESENT | PTE_WRITE | PTE_MERGEABLE);
pvalidate(1UL * 4096, RMPE_MERGEABLE);
pvalidate(2UL * 4096, RMPE_MERGEABLE);
pvalidate(3UL * 4096, RMPE_MERGEABLE);
gcr3 = GCR3;
// switch into VM1
cur_asid = 1;
ncr3 = NCR3_1;
gcr3 = GCR3;
// in VM1
printf("%lx\n", translate(0x0000UL, false));
// switch into VM2
cur_asid = 2;
ncr3 = NCR3_2;
gcr3 = GCR3;
// in VM2
printf("%lx\n", translate(0x0000UL, false));
// Merge VM1's "1"st page and VM2's "1"st page
// switch into VM1
cur_asid = 1;
ncr3 = NCR3_1;
gcr3 = GCR3;
// in VM1
memset((void *)REF_MUT(1UL * 4096), 0, 4096); // at 8*4096 in hPA
strcpy((void *)REF_MUT(1UL * 4096), "hello");
// switch into VM2
cur_asid = 2;
ncr3 = NCR3_2;
gcr3 = GCR3;
// in VM2
memset((void *)REF_MUT(1UL * 4096), 0, 4096); // at 12*4096 in hPA
strcpy((void *)REF_MUT(1UL * 4096), "hello");
// merged page must be the same
// strcpy((void *)REF_MUT(1UL * 4096), "hell");
// switch into VMM
cur_asid = 0;
hcr3 = HCR3;
// in VMM
rmpupdate(1UL * 4096, -1L, 0, RMPE_LEAF); // prepare RMP leaf page
pfix(8UL * 4096, 1UL * 4096); // fix VM1's "1"st page (at 8*4096 in hPA)
pmerge(8UL * 4096, 12UL * 4096); // merge VM2's "1"st page into VM1's "1"st page
WRITE(&npt2[1], 8UL * 4096 | PTE_PRESENT | PTE_MERGEABLE | PTE_WRITE); // update corresponding NPT
// switch into VM1
cur_asid = 1;
ncr3 = NCR3_1;
gcr3 = GCR3;
// in VM1
// REF_MUT(1UL * 4096); // this access should be invalid cuz the page is now fixed
puts((char *)REF(1UL * 4096)); // this is ok
// switch into VM2
cur_asid = 2;
ncr3 = NCR3_2;
gcr3 = GCR3;
// in VM2
// REF_MUT(1UL * 4096); // this access should be invalid cuz the page is now fixed
puts((char *)REF(1UL * 4096)); // this is ok
// switch into VMM
cur_asid = 0;
hcr3 = HCR3;
// in VMM
punmerge(8UL * 4096, 12UL * 4096, 2); // unmerge page at 8*4096 into 12*4096
punfix(8UL * 4096); // unfix page at 8*4096
WRITE(&npt2[1], 12UL * 4096 | PTE_PRESENT | PTE_MERGEABLE | PTE_WRITE); // update corresponding NPT
// switch into VM1
cur_asid = 1;
ncr3 = NCR3_1;
gcr3 = GCR3;
// in VM1
strcpy((void *)REF_MUT(1UL * 4096), "foobar"); // this is ok
puts((char *)REF(1UL * 4096));
// switch into VM2
cur_asid = 2;
ncr3 = NCR3_2;
gcr3 = GCR3;
// in VM2
strcpy((void *)REF_MUT(1UL * 4096), "deadbeef"); // this is ok
puts((char *)REF(1UL * 4096));
return 0;
}