-
Notifications
You must be signed in to change notification settings - Fork 1
/
translation_tests.c
435 lines (363 loc) · 11.4 KB
/
translation_tests.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
#include <rvh_test.h>
#include <page_tables.h>
static inline void touchread(uintptr_t addr){
asm volatile("" ::: "memory");
volatile uint64_t x = *(volatile uint64_t *)addr;
}
static inline void touchwrite(uintptr_t addr){
*(volatile uint64_t *)addr = 0xdeadbeef;
}
static inline void touch(uintptr_t addr){
touchwrite(addr);
}
bool two_stage_translation(){
TEST_START();
uintptr_t addr1 = phys_page_base(SWITCH1);
uintptr_t addr2 = phys_page_base(SWITCH2);
uintptr_t vaddr1 = vs_page_base(SWITCH1);
uintptr_t vaddr2 = vs_page_base(SWITCH2);
write64(addr1, 0x11);
write64(addr2, 0x22);
/**
* Setup hyp page_tables.
*/
goto_priv(PRIV_HS);
hspt_init();
hpt_init();
/**
* Setup guest page tables.
*/
goto_priv(PRIV_VS);
vspt_init();
bool check1 = read64(vaddr1) == 0x11;
bool check2 = read64(vaddr2) == 0x22;
TEST_ASSERT("vs gets right values", check1 && check2);
goto_priv(PRIV_HS);
hpt_switch();
hfence();
goto_priv(PRIV_VS);
check1 = read64(vaddr1) == 0x22;
check2 = read64(vaddr2) == 0x11;
// INFO("0%lx 0x%lx", read64(vaddr1), read64(vaddr2));
TEST_ASSERT("vs gets right values after changing 2nd stage pt", check1 && check2);
vspt_switch();
sfence();
check1 = read64(vaddr1) == 0x11;
check2 = read64(vaddr2) == 0x22;
TEST_ASSERT("vs gets right values after changing 1st stage pt", check1 && check2);
goto_priv(PRIV_M);
CSRS(medeleg, 1ull << CAUSE_LGPF);
goto_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
read64(vs_page_base(VSRWX_GI));
TEST_ASSERT(
"load guest page fault on unmapped address",
excpt.triggered == true &&
excpt.cause == CAUSE_LGPF &&
excpt.tval2 == (vs_page_base(VSRWX_GI) >> 2) &&
excpt.priv == PRIV_HS &&
excpt.gva == true &&
excpt.xpv == true
);
TEST_SETUP_EXCEPT();
TEST_EXEC_EXCEPT(vs_page_base(VSRWX_GI));
TEST_ASSERT(
"instruction guest page fault on unmapped 2-stage address",
excpt.triggered == true &&
excpt.cause == CAUSE_IGPF &&
excpt.tval2 == (vs_page_base(VSRWX_GI) >> 2) &&
excpt.priv == PRIV_M &&
excpt.gva == true &&
excpt.xpv == true
);
goto_priv(PRIV_M);
CSRS(medeleg, 1 << CAUSE_LPF | 1 << CAUSE_LGPF);
goto_priv(PRIV_HS);
CSRS(CSR_HEDELEG, 1 << CAUSE_LPF);
goto_priv(PRIV_VS);
sfence();
TEST_SETUP_EXCEPT();
touchread(vs_page_base(VSI_GI));
TEST_ASSERT(
"invalid pte in both stages leads to s1 page fault",
excpt.triggered == true &&
excpt.cause == CAUSE_LPF &&
excpt.priv == PRIV_VS &&
excpt.gva == false
);
TEST_END();
}
bool second_stage_only_translation(){
/**
* Test only 2nd stage translation.
*/
TEST_START();
uintptr_t addr1 = phys_page_base(SWITCH1);
uintptr_t addr2 = phys_page_base(SWITCH2);
uintptr_t vaddr1 = vs_page_base(SWITCH1);
uintptr_t vaddr2 = vs_page_base(SWITCH2);
write64(addr1, 0x11);
write64(addr2, 0x22);
CSRS(medeleg, (1 << CAUSE_LGPF) | (1 << CAUSE_SGPF));
/**
* Setup hyp page_tables.
*/
goto_priv(PRIV_HS);
hspt_init();
hpt_init();
goto_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
bool check1 = read64(vaddr1) == 0x11;
bool check2 = read64(vaddr2) == 0x22;
TEST_ASSERT("vs gets right values", excpt.triggered == false && check1 && check2);
hpt_switch();
sfence();
TEST_SETUP_EXCEPT();
check1 = read64(vaddr1) == 0x22;
check2 = read64(vaddr2) == 0x11;
TEST_ASSERT("vs gets right values after changing pt", excpt.triggered == false && check1 && check2);
TEST_SETUP_EXCEPT();
(void) read64(vs_page_base(VSRWX_GI));
TEST_ASSERT(
"vs access to unmapped -> load gpf",
excpt.triggered == true &&
excpt.cause == CAUSE_LGPF &&
excpt.priv == PRIV_HS
);
TEST_SETUP_EXCEPT();
read64(vs_page_base_limit(TOP));
TEST_ASSERT(
"access top of guest pa space with high bits == 0",
excpt.triggered == false
);
TEST_SETUP_EXCEPT();
read64(vs_page_base_limit(TOP) | (1ULL << 41));
TEST_ASSERT(
"access top of guest pa space with high bits =/= 0",
excpt.triggered == true &&
excpt.cause == CAUSE_LGPF
);
TEST_END();
}
static inline uint64_t read64_mprv(unsigned priv, uintptr_t addr){
if(curr_priv != PRIV_M){
ERROR("trying to read as mprv from low privilege");
}
uint64_t value = 0;
set_prev_priv(priv);
asm volatile(
"csrs mstatus, %2\n\t"
"ld %0, %1\n\t"
"csrc mstatus, %2\n\t"
: "=&r"(value) : "m"(*(uint64_t*)addr), "r"(MSTATUS_MPRV)
);
return value;
}
static inline void write64_mprv(unsigned priv, uintptr_t addr, uint64_t value){
if(curr_priv != PRIV_M){
ERROR("trying to write as mprv from low privilege");
}
set_prev_priv(priv);
asm volatile(
"csrs mstatus, %2\n\t"
"sd %0, %1\n\t"
"csrc mstatus, %2\n\t"
:: "r"(value), "m"(*(uint64_t*)addr), "r"(MSTATUS_MPRV)
);
}
bool m_and_hs_using_vs_access(){
uint64_t val, valu;
uintptr_t vaddr = vs_page_base(SCRATCHPAD);
uintptr_t addr;
TEST_START();
hspt_init();
hpt_init();
vspt_init();
TEST_SETUP_EXCEPT();
write64_mprv(PRIV_VS, vaddr, 0x1107ec0ffee);
val = read64_mprv(PRIV_VS, vaddr);
TEST_ASSERT("machine sets mprv to access vs space",
excpt.triggered == false && val == 0x1107ec0ffee
);
//TODO: test mprv to access hs space
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
hsvd(vaddr, 0xdeadbeef);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd",
excpt.triggered == false && val == 0xdeadbeef
);
TEST_SETUP_EXCEPT();
hsvb(vaddr, ((uint8_t)-1));
val = hlvb(vaddr);
valu = hlvbu(vaddr);
TEST_ASSERT("hs hlvb vs hlvbu",
excpt.triggered == false && val == (-1) && valu == ((uint8_t)-1)
);
TEST_SETUP_EXCEPT();
hsvh(vaddr, ((uint16_t)-1));
val = hlvh(vaddr);
valu = hlvhu(vaddr);
TEST_ASSERT("hs hlvh vs hlvhu",
excpt.triggered == false && val == (-1) && valu == ((uint16_t)-1)
);
TEST_SETUP_EXCEPT();
hsvw(vaddr, ((uint32_t)-1));
val = hlvw(vaddr);
valu = hlvwu(vaddr);
printf("val 0x%llx, valu 0x%llx\n", val, valu);
TEST_ASSERT("hs hlvw vs hlvwu",
excpt.triggered == false && val == (-1) && valu == ((uint32_t)-1)
);
/**
* Some tests are commented out because qemu behaves in a weird way if you
* try to use a hlvx instruction on a page without read permissions. It
* gives you a load access fault, with the mepc not even pointing to the
* hlvx instruction but on a previous lui instruction.
* TODO: find out why
*/
TEST_SETUP_EXCEPT();
val = hlvxwu(vs_page_base(VSX_GUX));
TEST_ASSERT("hs hlvxwu accesses on only execute page",
excpt.triggered == false
);
TEST_SETUP_EXCEPT();
val = hlvxwu(vs_page_base(VSRWX_GURWX));
TEST_ASSERT("hs hlvxwu accesses page with all permissions",
excpt.triggered == false
);
TEST_SETUP_EXCEPT();
val = hlvxwu(vs_page_base(VSRWX_GURW));
TEST_ASSERT("hs hlvxwu on hs-level non-exec page leads to lgpf",
excpt.triggered == true &&
excpt.cause == CAUSE_LGPF &&
excpt.gva == true &&
excpt.xpv == false
);
TEST_SETUP_EXCEPT();
val = hlvxwu(vs_page_base(VSRW_GURWX));
TEST_ASSERT("hs hlvxwu on vs-level non-exec page leads to lpf",
excpt.triggered == true &&
excpt.cause == CAUSE_LPF &&
excpt.gva == false &&
excpt.xpv == false
);
vaddr = vs_page_base(VSURWX_GURWX);
addr = phys_page_base(VSURWX_GURWX);
goto_priv(PRIV_M);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = read64_mprv(PRIV_VS, vaddr);
TEST_ASSERT("machine mprv vs access to vu leads to exception",
excpt.triggered == true
);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = read64_mprv(PRIV_VU, vaddr);
TEST_ASSERT("machine mprv vu access to vu successful",
excpt.triggered == false
);
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VU);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd to vu page successful when spvp = 0",
excpt.triggered == false
);
set_prev_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd to vu page leads to exception when spvp = 1",
excpt.triggered == true
);
CSRS(CSR_VSSTATUS, SSTATUS_SUM);
goto_priv(PRIV_M);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = read64_mprv(PRIV_VS, vaddr);
TEST_ASSERT("machine mprv access vs user page successful when vsstatus.sum set",
excpt.triggered == false && val == 0x1107ec0ffee
);
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
write64(addr, 0x1107ec0ffee);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd to user page successful when vsstatus.sum set",
excpt.triggered == false && val == 0x1107ec0ffee
);
CSRC(CSR_VSSTATUS, SSTATUS_SUM);
vaddr = vs_page_base(VSX_GUX);
addr = phys_page_base(VSX_GUX);
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VS);
TEST_SETUP_EXCEPT();
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd of xo vs page leads to exception",
excpt.triggered == true
);
TEST_SETUP_EXCEPT();
CSRS(sstatus, SSTATUS_MXR);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd of xo vs page succsseful",
excpt.triggered == false
);
CSRC(sstatus, SSTATUS_MXR);
vaddr = vs_page_base(VSX_GUR);
addr = phys_page_base(VSX_GUR);
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VS);
CSRW(sscratch, 0x1111111);
TEST_SETUP_EXCEPT();
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd of xo vs page leads to load page fault",
excpt.triggered == true &&
excpt.cause == CAUSE_LPF
);
TEST_SETUP_EXCEPT();
CSRS(CSR_VSSTATUS, SSTATUS_MXR);
val = hlvd(vaddr);
TEST_ASSERT("hs hlvd of xo vs page succsseful with sstatus.mxr set",
excpt.triggered == false
);
reset_state();
goto_priv(PRIV_HS);
set_prev_priv(PRIV_VS);
hpt_init();
vaddr = vs_page_base(VSI_GUR) + 1;
TEST_SETUP_EXCEPT();\
hsvb(vaddr, 0xdeadbeef);
TEST_ASSERT("hs hsvb on ro 2-stage page leads to store guest page fault",
excpt.triggered == true &&
excpt.cause == CAUSE_SGPF &&
excpt.tval2 == vaddr >> 2
);
vaddr = vs_page_base(VSI_GUR);
INFO_PRINT(vaddr);
TEST_SETUP_EXCEPT();
val = hlvb(vaddr);
TEST_ASSERT("hs hlvb on ro 2-stage page successfull",
excpt.triggered == false
);
vspt_init();
vaddr = vs_page_base(VSR_GUR);
TEST_SETUP_EXCEPT();
CSRW(sscratch, 0x911);
hsvb(vaddr, 0xdeadbeef);
TEST_ASSERT("hs hsvb on ro both stage page leads to store page fault",
excpt.triggered == true &&
excpt.cause == CAUSE_SPF
);
vaddr = vs_page_base(VSRW_GI);
TEST_SETUP_EXCEPT();
CSRW(sscratch, 0x112);
hsvb(vaddr, 0xdeadbeef);
TEST_ASSERT("hs hsvb on invalid 2 stage page leads to store guest page fault",
excpt.triggered == true &&
excpt.cause == CAUSE_SGPF
);
TEST_END();
}