xref: /kvm-unit-tests/x86/vmx_tests.c (revision fd056f5b89ac9053eea4ecc9ff555fe016f4ad39)
1 /*
2  * All test cases of nested virtualization should be in this file
3  *
4  * Author : Arthur Chunqi Li <yzt356@gmail.com>
5  */
6 #include "vmx.h"
7 #include "msr.h"
8 #include "processor.h"
9 #include "vm.h"
10 #include "pci.h"
11 #include "fwcfg.h"
12 #include "isr.h"
13 #include "desc.h"
14 #include "apic.h"
15 #include "types.h"
16 #include "vmalloc.h"
17 #include "alloc_page.h"
18 #include "smp.h"
19 #include "delay.h"
20 
21 #define NONCANONICAL            0xaaaaaaaaaaaaaaaaull
22 
23 #define VPID_CAP_INVVPID_TYPES_SHIFT 40
24 
25 u64 ia32_pat;
26 u64 ia32_efer;
27 void *io_bitmap_a, *io_bitmap_b;
28 u16 ioport;
29 
30 unsigned long *pml4;
31 u64 eptp;
32 void *data_page1, *data_page2;
33 
34 phys_addr_t pci_physaddr;
35 
36 void *pml_log;
37 #define PML_INDEX 512
38 
39 static inline unsigned ffs(unsigned x)
40 {
41 	int pos = -1;
42 
43 	__asm__ __volatile__("bsf %1, %%eax; cmovnz %%eax, %0"
44 			     : "+r"(pos) : "rm"(x) : "eax");
45 	return pos + 1;
46 }
47 
48 static inline void vmcall(void)
49 {
50 	asm volatile("vmcall");
51 }
52 
53 static void basic_guest_main(void)
54 {
55 	report("Basic VMX test", 1);
56 }
57 
58 static int basic_exit_handler(void)
59 {
60 	report("Basic VMX test", 0);
61 	print_vmexit_info();
62 	return VMX_TEST_EXIT;
63 }
64 
65 static void vmenter_main(void)
66 {
67 	u64 rax;
68 	u64 rsp, resume_rsp;
69 
70 	report("test vmlaunch", 1);
71 
72 	asm volatile(
73 		"mov %%rsp, %0\n\t"
74 		"mov %3, %%rax\n\t"
75 		"vmcall\n\t"
76 		"mov %%rax, %1\n\t"
77 		"mov %%rsp, %2\n\t"
78 		: "=r"(rsp), "=r"(rax), "=r"(resume_rsp)
79 		: "g"(0xABCD));
80 	report("test vmresume", (rax == 0xFFFF) && (rsp == resume_rsp));
81 }
82 
83 static int vmenter_exit_handler(void)
84 {
85 	u64 guest_rip;
86 	ulong reason;
87 
88 	guest_rip = vmcs_read(GUEST_RIP);
89 	reason = vmcs_read(EXI_REASON) & 0xff;
90 	switch (reason) {
91 	case VMX_VMCALL:
92 		if (regs.rax != 0xABCD) {
93 			report("test vmresume", 0);
94 			return VMX_TEST_VMEXIT;
95 		}
96 		regs.rax = 0xFFFF;
97 		vmcs_write(GUEST_RIP, guest_rip + 3);
98 		return VMX_TEST_RESUME;
99 	default:
100 		report("test vmresume", 0);
101 		print_vmexit_info();
102 	}
103 	return VMX_TEST_VMEXIT;
104 }
105 
106 u32 preempt_scale;
107 volatile unsigned long long tsc_val;
108 volatile u32 preempt_val;
109 u64 saved_rip;
110 
111 static int preemption_timer_init(struct vmcs *vmcs)
112 {
113 	if (!(ctrl_pin_rev.clr & PIN_PREEMPT)) {
114 		printf("\tPreemption timer is not supported\n");
115 		return VMX_TEST_EXIT;
116 	}
117 	vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_PREEMPT);
118 	preempt_val = 10000000;
119 	vmcs_write(PREEMPT_TIMER_VALUE, preempt_val);
120 	preempt_scale = rdmsr(MSR_IA32_VMX_MISC) & 0x1F;
121 
122 	if (!(ctrl_exit_rev.clr & EXI_SAVE_PREEMPT))
123 		printf("\tSave preemption value is not supported\n");
124 
125 	return VMX_TEST_START;
126 }
127 
128 static void preemption_timer_main(void)
129 {
130 	tsc_val = rdtsc();
131 	if (ctrl_exit_rev.clr & EXI_SAVE_PREEMPT) {
132 		vmx_set_test_stage(0);
133 		vmcall();
134 		if (vmx_get_test_stage() == 1)
135 			vmcall();
136 	}
137 	vmx_set_test_stage(1);
138 	while (vmx_get_test_stage() == 1) {
139 		if (((rdtsc() - tsc_val) >> preempt_scale)
140 				> 10 * preempt_val) {
141 			vmx_set_test_stage(2);
142 			vmcall();
143 		}
144 	}
145 	tsc_val = rdtsc();
146 	asm volatile ("hlt");
147 	vmcall();
148 	vmx_set_test_stage(5);
149 	vmcall();
150 }
151 
152 static int preemption_timer_exit_handler(void)
153 {
154 	bool guest_halted;
155 	u64 guest_rip;
156 	ulong reason;
157 	u32 insn_len;
158 	u32 ctrl_exit;
159 
160 	guest_rip = vmcs_read(GUEST_RIP);
161 	reason = vmcs_read(EXI_REASON) & 0xff;
162 	insn_len = vmcs_read(EXI_INST_LEN);
163 	switch (reason) {
164 	case VMX_PREEMPT:
165 		switch (vmx_get_test_stage()) {
166 		case 1:
167 		case 2:
168 			report("busy-wait for preemption timer",
169 			       ((rdtsc() - tsc_val) >> preempt_scale) >=
170 			       preempt_val);
171 			vmx_set_test_stage(3);
172 			vmcs_write(PREEMPT_TIMER_VALUE, preempt_val);
173 			return VMX_TEST_RESUME;
174 		case 3:
175 			guest_halted =
176 				(vmcs_read(GUEST_ACTV_STATE) == ACTV_HLT);
177 			report("preemption timer during hlt",
178 			       ((rdtsc() - tsc_val) >> preempt_scale) >=
179 			       preempt_val && guest_halted);
180 			vmx_set_test_stage(4);
181 			vmcs_write(PIN_CONTROLS,
182 				   vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT);
183 			vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE);
184 			return VMX_TEST_RESUME;
185 		case 4:
186 			report("preemption timer with 0 value",
187 			       saved_rip == guest_rip);
188 			break;
189 		default:
190 			report("Invalid stage.", false);
191 			print_vmexit_info();
192 			break;
193 		}
194 		break;
195 	case VMX_VMCALL:
196 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
197 		switch (vmx_get_test_stage()) {
198 		case 0:
199 			report("Keep preemption value",
200 			       vmcs_read(PREEMPT_TIMER_VALUE) == preempt_val);
201 			vmx_set_test_stage(1);
202 			vmcs_write(PREEMPT_TIMER_VALUE, preempt_val);
203 			ctrl_exit = (vmcs_read(EXI_CONTROLS) |
204 				EXI_SAVE_PREEMPT) & ctrl_exit_rev.clr;
205 			vmcs_write(EXI_CONTROLS, ctrl_exit);
206 			return VMX_TEST_RESUME;
207 		case 1:
208 			report("Save preemption value",
209 			       vmcs_read(PREEMPT_TIMER_VALUE) < preempt_val);
210 			return VMX_TEST_RESUME;
211 		case 2:
212 			report("busy-wait for preemption timer", 0);
213 			vmx_set_test_stage(3);
214 			vmcs_write(PREEMPT_TIMER_VALUE, preempt_val);
215 			return VMX_TEST_RESUME;
216 		case 3:
217 			report("preemption timer during hlt", 0);
218 			vmx_set_test_stage(4);
219 			/* fall through */
220 		case 4:
221 			vmcs_write(PIN_CONTROLS,
222 				   vmcs_read(PIN_CONTROLS) | PIN_PREEMPT);
223 			vmcs_write(PREEMPT_TIMER_VALUE, 0);
224 			saved_rip = guest_rip + insn_len;
225 			return VMX_TEST_RESUME;
226 		case 5:
227 			report("preemption timer with 0 value (vmcall stage 5)", 0);
228 			break;
229 		default:
230 			// Should not reach here
231 			report("unexpected stage, %d", false,
232 			       vmx_get_test_stage());
233 			print_vmexit_info();
234 			return VMX_TEST_VMEXIT;
235 		}
236 		break;
237 	default:
238 		report("Unknown exit reason, %ld", false, reason);
239 		print_vmexit_info();
240 	}
241 	vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_PREEMPT);
242 	return VMX_TEST_VMEXIT;
243 }
244 
245 static void msr_bmp_init(void)
246 {
247 	void *msr_bitmap;
248 	u32 ctrl_cpu0;
249 
250 	msr_bitmap = alloc_page();
251 	memset(msr_bitmap, 0x0, PAGE_SIZE);
252 	ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0);
253 	ctrl_cpu0 |= CPU_MSR_BITMAP;
254 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0);
255 	vmcs_write(MSR_BITMAP, (u64)msr_bitmap);
256 }
257 
258 static int test_ctrl_pat_init(struct vmcs *vmcs)
259 {
260 	u64 ctrl_ent;
261 	u64 ctrl_exi;
262 
263 	msr_bmp_init();
264 	if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT) &&
265 	    !(ctrl_exit_rev.clr & EXI_LOAD_PAT) &&
266 	    !(ctrl_enter_rev.clr & ENT_LOAD_PAT)) {
267 		printf("\tSave/load PAT is not supported\n");
268 		return 1;
269 	}
270 
271 	ctrl_ent = vmcs_read(ENT_CONTROLS);
272 	ctrl_exi = vmcs_read(EXI_CONTROLS);
273 	ctrl_ent |= ctrl_enter_rev.clr & ENT_LOAD_PAT;
274 	ctrl_exi |= ctrl_exit_rev.clr & (EXI_SAVE_PAT | EXI_LOAD_PAT);
275 	vmcs_write(ENT_CONTROLS, ctrl_ent);
276 	vmcs_write(EXI_CONTROLS, ctrl_exi);
277 	ia32_pat = rdmsr(MSR_IA32_CR_PAT);
278 	vmcs_write(GUEST_PAT, 0x0);
279 	vmcs_write(HOST_PAT, ia32_pat);
280 	return VMX_TEST_START;
281 }
282 
283 static void test_ctrl_pat_main(void)
284 {
285 	u64 guest_ia32_pat;
286 
287 	guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT);
288 	if (!(ctrl_enter_rev.clr & ENT_LOAD_PAT))
289 		printf("\tENT_LOAD_PAT is not supported.\n");
290 	else {
291 		if (guest_ia32_pat != 0) {
292 			report("Entry load PAT", 0);
293 			return;
294 		}
295 	}
296 	wrmsr(MSR_IA32_CR_PAT, 0x6);
297 	vmcall();
298 	guest_ia32_pat = rdmsr(MSR_IA32_CR_PAT);
299 	if (ctrl_enter_rev.clr & ENT_LOAD_PAT)
300 		report("Entry load PAT", guest_ia32_pat == ia32_pat);
301 }
302 
303 static int test_ctrl_pat_exit_handler(void)
304 {
305 	u64 guest_rip;
306 	ulong reason;
307 	u64 guest_pat;
308 
309 	guest_rip = vmcs_read(GUEST_RIP);
310 	reason = vmcs_read(EXI_REASON) & 0xff;
311 	switch (reason) {
312 	case VMX_VMCALL:
313 		guest_pat = vmcs_read(GUEST_PAT);
314 		if (!(ctrl_exit_rev.clr & EXI_SAVE_PAT)) {
315 			printf("\tEXI_SAVE_PAT is not supported\n");
316 			vmcs_write(GUEST_PAT, 0x6);
317 		} else {
318 			report("Exit save PAT", guest_pat == 0x6);
319 		}
320 		if (!(ctrl_exit_rev.clr & EXI_LOAD_PAT))
321 			printf("\tEXI_LOAD_PAT is not supported\n");
322 		else
323 			report("Exit load PAT", rdmsr(MSR_IA32_CR_PAT) == ia32_pat);
324 		vmcs_write(GUEST_PAT, ia32_pat);
325 		vmcs_write(GUEST_RIP, guest_rip + 3);
326 		return VMX_TEST_RESUME;
327 	default:
328 		printf("ERROR : Undefined exit reason, reason = %ld.\n", reason);
329 		break;
330 	}
331 	return VMX_TEST_VMEXIT;
332 }
333 
334 static int test_ctrl_efer_init(struct vmcs *vmcs)
335 {
336 	u64 ctrl_ent;
337 	u64 ctrl_exi;
338 
339 	msr_bmp_init();
340 	ctrl_ent = vmcs_read(ENT_CONTROLS) | ENT_LOAD_EFER;
341 	ctrl_exi = vmcs_read(EXI_CONTROLS) | EXI_SAVE_EFER | EXI_LOAD_EFER;
342 	vmcs_write(ENT_CONTROLS, ctrl_ent & ctrl_enter_rev.clr);
343 	vmcs_write(EXI_CONTROLS, ctrl_exi & ctrl_exit_rev.clr);
344 	ia32_efer = rdmsr(MSR_EFER);
345 	vmcs_write(GUEST_EFER, ia32_efer ^ EFER_NX);
346 	vmcs_write(HOST_EFER, ia32_efer ^ EFER_NX);
347 	return VMX_TEST_START;
348 }
349 
350 static void test_ctrl_efer_main(void)
351 {
352 	u64 guest_ia32_efer;
353 
354 	guest_ia32_efer = rdmsr(MSR_EFER);
355 	if (!(ctrl_enter_rev.clr & ENT_LOAD_EFER))
356 		printf("\tENT_LOAD_EFER is not supported.\n");
357 	else {
358 		if (guest_ia32_efer != (ia32_efer ^ EFER_NX)) {
359 			report("Entry load EFER", 0);
360 			return;
361 		}
362 	}
363 	wrmsr(MSR_EFER, ia32_efer);
364 	vmcall();
365 	guest_ia32_efer = rdmsr(MSR_EFER);
366 	if (ctrl_enter_rev.clr & ENT_LOAD_EFER)
367 		report("Entry load EFER", guest_ia32_efer == ia32_efer);
368 }
369 
370 static int test_ctrl_efer_exit_handler(void)
371 {
372 	u64 guest_rip;
373 	ulong reason;
374 	u64 guest_efer;
375 
376 	guest_rip = vmcs_read(GUEST_RIP);
377 	reason = vmcs_read(EXI_REASON) & 0xff;
378 	switch (reason) {
379 	case VMX_VMCALL:
380 		guest_efer = vmcs_read(GUEST_EFER);
381 		if (!(ctrl_exit_rev.clr & EXI_SAVE_EFER)) {
382 			printf("\tEXI_SAVE_EFER is not supported\n");
383 			vmcs_write(GUEST_EFER, ia32_efer);
384 		} else {
385 			report("Exit save EFER", guest_efer == ia32_efer);
386 		}
387 		if (!(ctrl_exit_rev.clr & EXI_LOAD_EFER)) {
388 			printf("\tEXI_LOAD_EFER is not supported\n");
389 			wrmsr(MSR_EFER, ia32_efer ^ EFER_NX);
390 		} else {
391 			report("Exit load EFER", rdmsr(MSR_EFER) == (ia32_efer ^ EFER_NX));
392 		}
393 		vmcs_write(GUEST_PAT, ia32_efer);
394 		vmcs_write(GUEST_RIP, guest_rip + 3);
395 		return VMX_TEST_RESUME;
396 	default:
397 		printf("ERROR : Undefined exit reason, reason = %ld.\n", reason);
398 		break;
399 	}
400 	return VMX_TEST_VMEXIT;
401 }
402 
403 u32 guest_cr0, guest_cr4;
404 
405 static void cr_shadowing_main(void)
406 {
407 	u32 cr0, cr4, tmp;
408 
409 	// Test read through
410 	vmx_set_test_stage(0);
411 	guest_cr0 = read_cr0();
412 	if (vmx_get_test_stage() == 1)
413 		report("Read through CR0", 0);
414 	else
415 		vmcall();
416 	vmx_set_test_stage(1);
417 	guest_cr4 = read_cr4();
418 	if (vmx_get_test_stage() == 2)
419 		report("Read through CR4", 0);
420 	else
421 		vmcall();
422 	// Test write through
423 	guest_cr0 = guest_cr0 ^ (X86_CR0_TS | X86_CR0_MP);
424 	guest_cr4 = guest_cr4 ^ (X86_CR4_TSD | X86_CR4_DE);
425 	vmx_set_test_stage(2);
426 	write_cr0(guest_cr0);
427 	if (vmx_get_test_stage() == 3)
428 		report("Write throuth CR0", 0);
429 	else
430 		vmcall();
431 	vmx_set_test_stage(3);
432 	write_cr4(guest_cr4);
433 	if (vmx_get_test_stage() == 4)
434 		report("Write through CR4", 0);
435 	else
436 		vmcall();
437 	// Test read shadow
438 	vmx_set_test_stage(4);
439 	vmcall();
440 	cr0 = read_cr0();
441 	if (vmx_get_test_stage() != 5)
442 		report("Read shadowing CR0", cr0 == guest_cr0);
443 	vmx_set_test_stage(5);
444 	cr4 = read_cr4();
445 	if (vmx_get_test_stage() != 6)
446 		report("Read shadowing CR4", cr4 == guest_cr4);
447 	// Test write shadow (same value with shadow)
448 	vmx_set_test_stage(6);
449 	write_cr0(guest_cr0);
450 	if (vmx_get_test_stage() == 7)
451 		report("Write shadowing CR0 (same value with shadow)", 0);
452 	else
453 		vmcall();
454 	vmx_set_test_stage(7);
455 	write_cr4(guest_cr4);
456 	if (vmx_get_test_stage() == 8)
457 		report("Write shadowing CR4 (same value with shadow)", 0);
458 	else
459 		vmcall();
460 	// Test write shadow (different value)
461 	vmx_set_test_stage(8);
462 	tmp = guest_cr0 ^ X86_CR0_TS;
463 	asm volatile("mov %0, %%rsi\n\t"
464 		"mov %%rsi, %%cr0\n\t"
465 		::"m"(tmp)
466 		:"rsi", "memory", "cc");
467 	report("Write shadowing different X86_CR0_TS", vmx_get_test_stage() == 9);
468 	vmx_set_test_stage(9);
469 	tmp = guest_cr0 ^ X86_CR0_MP;
470 	asm volatile("mov %0, %%rsi\n\t"
471 		"mov %%rsi, %%cr0\n\t"
472 		::"m"(tmp)
473 		:"rsi", "memory", "cc");
474 	report("Write shadowing different X86_CR0_MP", vmx_get_test_stage() == 10);
475 	vmx_set_test_stage(10);
476 	tmp = guest_cr4 ^ X86_CR4_TSD;
477 	asm volatile("mov %0, %%rsi\n\t"
478 		"mov %%rsi, %%cr4\n\t"
479 		::"m"(tmp)
480 		:"rsi", "memory", "cc");
481 	report("Write shadowing different X86_CR4_TSD", vmx_get_test_stage() == 11);
482 	vmx_set_test_stage(11);
483 	tmp = guest_cr4 ^ X86_CR4_DE;
484 	asm volatile("mov %0, %%rsi\n\t"
485 		"mov %%rsi, %%cr4\n\t"
486 		::"m"(tmp)
487 		:"rsi", "memory", "cc");
488 	report("Write shadowing different X86_CR4_DE", vmx_get_test_stage() == 12);
489 }
490 
491 static int cr_shadowing_exit_handler(void)
492 {
493 	u64 guest_rip;
494 	ulong reason;
495 	u32 insn_len;
496 	u32 exit_qual;
497 
498 	guest_rip = vmcs_read(GUEST_RIP);
499 	reason = vmcs_read(EXI_REASON) & 0xff;
500 	insn_len = vmcs_read(EXI_INST_LEN);
501 	exit_qual = vmcs_read(EXI_QUALIFICATION);
502 	switch (reason) {
503 	case VMX_VMCALL:
504 		switch (vmx_get_test_stage()) {
505 		case 0:
506 			report("Read through CR0", guest_cr0 == vmcs_read(GUEST_CR0));
507 			break;
508 		case 1:
509 			report("Read through CR4", guest_cr4 == vmcs_read(GUEST_CR4));
510 			break;
511 		case 2:
512 			report("Write through CR0", guest_cr0 == vmcs_read(GUEST_CR0));
513 			break;
514 		case 3:
515 			report("Write through CR4", guest_cr4 == vmcs_read(GUEST_CR4));
516 			break;
517 		case 4:
518 			guest_cr0 = vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP);
519 			guest_cr4 = vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE);
520 			vmcs_write(CR0_MASK, X86_CR0_TS | X86_CR0_MP);
521 			vmcs_write(CR0_READ_SHADOW, guest_cr0 & (X86_CR0_TS | X86_CR0_MP));
522 			vmcs_write(CR4_MASK, X86_CR4_TSD | X86_CR4_DE);
523 			vmcs_write(CR4_READ_SHADOW, guest_cr4 & (X86_CR4_TSD | X86_CR4_DE));
524 			break;
525 		case 6:
526 			report("Write shadowing CR0 (same value)",
527 					guest_cr0 == (vmcs_read(GUEST_CR0) ^ (X86_CR0_TS | X86_CR0_MP)));
528 			break;
529 		case 7:
530 			report("Write shadowing CR4 (same value)",
531 					guest_cr4 == (vmcs_read(GUEST_CR4) ^ (X86_CR4_TSD | X86_CR4_DE)));
532 			break;
533 		default:
534 			// Should not reach here
535 			report("unexpected stage, %d", false,
536 			       vmx_get_test_stage());
537 			print_vmexit_info();
538 			return VMX_TEST_VMEXIT;
539 		}
540 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
541 		return VMX_TEST_RESUME;
542 	case VMX_CR:
543 		switch (vmx_get_test_stage()) {
544 		case 4:
545 			report("Read shadowing CR0", 0);
546 			vmx_inc_test_stage();
547 			break;
548 		case 5:
549 			report("Read shadowing CR4", 0);
550 			vmx_inc_test_stage();
551 			break;
552 		case 6:
553 			report("Write shadowing CR0 (same value)", 0);
554 			vmx_inc_test_stage();
555 			break;
556 		case 7:
557 			report("Write shadowing CR4 (same value)", 0);
558 			vmx_inc_test_stage();
559 			break;
560 		case 8:
561 		case 9:
562 			// 0x600 encodes "mov %esi, %cr0"
563 			if (exit_qual == 0x600)
564 				vmx_inc_test_stage();
565 			break;
566 		case 10:
567 		case 11:
568 			// 0x604 encodes "mov %esi, %cr4"
569 			if (exit_qual == 0x604)
570 				vmx_inc_test_stage();
571 			break;
572 		default:
573 			// Should not reach here
574 			report("unexpected stage, %d", false,
575 			       vmx_get_test_stage());
576 			print_vmexit_info();
577 			return VMX_TEST_VMEXIT;
578 		}
579 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
580 		return VMX_TEST_RESUME;
581 	default:
582 		report("Unknown exit reason, %ld", false, reason);
583 		print_vmexit_info();
584 	}
585 	return VMX_TEST_VMEXIT;
586 }
587 
588 static int iobmp_init(struct vmcs *vmcs)
589 {
590 	u32 ctrl_cpu0;
591 
592 	io_bitmap_a = alloc_page();
593 	io_bitmap_b = alloc_page();
594 	memset(io_bitmap_a, 0x0, PAGE_SIZE);
595 	memset(io_bitmap_b, 0x0, PAGE_SIZE);
596 	ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0);
597 	ctrl_cpu0 |= CPU_IO_BITMAP;
598 	ctrl_cpu0 &= (~CPU_IO);
599 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0);
600 	vmcs_write(IO_BITMAP_A, (u64)io_bitmap_a);
601 	vmcs_write(IO_BITMAP_B, (u64)io_bitmap_b);
602 	return VMX_TEST_START;
603 }
604 
605 static void iobmp_main(void)
606 {
607 	// stage 0, test IO pass
608 	vmx_set_test_stage(0);
609 	inb(0x5000);
610 	outb(0x0, 0x5000);
611 	report("I/O bitmap - I/O pass", vmx_get_test_stage() == 0);
612 	// test IO width, in/out
613 	((u8 *)io_bitmap_a)[0] = 0xFF;
614 	vmx_set_test_stage(2);
615 	inb(0x0);
616 	report("I/O bitmap - trap in", vmx_get_test_stage() == 3);
617 	vmx_set_test_stage(3);
618 	outw(0x0, 0x0);
619 	report("I/O bitmap - trap out", vmx_get_test_stage() == 4);
620 	vmx_set_test_stage(4);
621 	inl(0x0);
622 	report("I/O bitmap - I/O width, long", vmx_get_test_stage() == 5);
623 	// test low/high IO port
624 	vmx_set_test_stage(5);
625 	((u8 *)io_bitmap_a)[0x5000 / 8] = (1 << (0x5000 % 8));
626 	inb(0x5000);
627 	report("I/O bitmap - I/O port, low part", vmx_get_test_stage() == 6);
628 	vmx_set_test_stage(6);
629 	((u8 *)io_bitmap_b)[0x1000 / 8] = (1 << (0x1000 % 8));
630 	inb(0x9000);
631 	report("I/O bitmap - I/O port, high part", vmx_get_test_stage() == 7);
632 	// test partial pass
633 	vmx_set_test_stage(7);
634 	inl(0x4FFF);
635 	report("I/O bitmap - partial pass", vmx_get_test_stage() == 8);
636 	// test overrun
637 	vmx_set_test_stage(8);
638 	memset(io_bitmap_a, 0x0, PAGE_SIZE);
639 	memset(io_bitmap_b, 0x0, PAGE_SIZE);
640 	inl(0xFFFF);
641 	report("I/O bitmap - overrun", vmx_get_test_stage() == 9);
642 	vmx_set_test_stage(9);
643 	vmcall();
644 	outb(0x0, 0x0);
645 	report("I/O bitmap - ignore unconditional exiting",
646 	       vmx_get_test_stage() == 9);
647 	vmx_set_test_stage(10);
648 	vmcall();
649 	outb(0x0, 0x0);
650 	report("I/O bitmap - unconditional exiting",
651 	       vmx_get_test_stage() == 11);
652 }
653 
654 static int iobmp_exit_handler(void)
655 {
656 	u64 guest_rip;
657 	ulong reason, exit_qual;
658 	u32 insn_len, ctrl_cpu0;
659 
660 	guest_rip = vmcs_read(GUEST_RIP);
661 	reason = vmcs_read(EXI_REASON) & 0xff;
662 	exit_qual = vmcs_read(EXI_QUALIFICATION);
663 	insn_len = vmcs_read(EXI_INST_LEN);
664 	switch (reason) {
665 	case VMX_IO:
666 		switch (vmx_get_test_stage()) {
667 		case 0:
668 		case 1:
669 			vmx_inc_test_stage();
670 			break;
671 		case 2:
672 			report("I/O bitmap - I/O width, byte",
673 					(exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_BYTE);
674 			report("I/O bitmap - I/O direction, in", exit_qual & VMX_IO_IN);
675 			vmx_inc_test_stage();
676 			break;
677 		case 3:
678 			report("I/O bitmap - I/O width, word",
679 					(exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_WORD);
680 			report("I/O bitmap - I/O direction, out",
681 					!(exit_qual & VMX_IO_IN));
682 			vmx_inc_test_stage();
683 			break;
684 		case 4:
685 			report("I/O bitmap - I/O width, long",
686 					(exit_qual & VMX_IO_SIZE_MASK) == _VMX_IO_LONG);
687 			vmx_inc_test_stage();
688 			break;
689 		case 5:
690 			if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x5000)
691 				vmx_inc_test_stage();
692 			break;
693 		case 6:
694 			if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x9000)
695 				vmx_inc_test_stage();
696 			break;
697 		case 7:
698 			if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0x4FFF)
699 				vmx_inc_test_stage();
700 			break;
701 		case 8:
702 			if (((exit_qual & VMX_IO_PORT_MASK) >> VMX_IO_PORT_SHIFT) == 0xFFFF)
703 				vmx_inc_test_stage();
704 			break;
705 		case 9:
706 		case 10:
707 			ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0);
708 			vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0 & ~CPU_IO);
709 			vmx_inc_test_stage();
710 			break;
711 		default:
712 			// Should not reach here
713 			report("unexpected stage, %d", false,
714 			       vmx_get_test_stage());
715 			print_vmexit_info();
716 			return VMX_TEST_VMEXIT;
717 		}
718 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
719 		return VMX_TEST_RESUME;
720 	case VMX_VMCALL:
721 		switch (vmx_get_test_stage()) {
722 		case 9:
723 			ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0);
724 			ctrl_cpu0 |= CPU_IO | CPU_IO_BITMAP;
725 			vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0);
726 			break;
727 		case 10:
728 			ctrl_cpu0 = vmcs_read(CPU_EXEC_CTRL0);
729 			ctrl_cpu0 = (ctrl_cpu0 & ~CPU_IO_BITMAP) | CPU_IO;
730 			vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu0);
731 			break;
732 		default:
733 			// Should not reach here
734 			report("unexpected stage, %d", false,
735 			       vmx_get_test_stage());
736 			print_vmexit_info();
737 			return VMX_TEST_VMEXIT;
738 		}
739 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
740 		return VMX_TEST_RESUME;
741 	default:
742 		printf("guest_rip = %#lx\n", guest_rip);
743 		printf("\tERROR : Undefined exit reason, reason = %ld.\n", reason);
744 		break;
745 	}
746 	return VMX_TEST_VMEXIT;
747 }
748 
749 #define INSN_CPU0		0
750 #define INSN_CPU1		1
751 #define INSN_ALWAYS_TRAP	2
752 
753 #define FIELD_EXIT_QUAL		(1 << 0)
754 #define FIELD_INSN_INFO		(1 << 1)
755 
756 asm(
757 	"insn_hlt: hlt;ret\n\t"
758 	"insn_invlpg: invlpg 0x12345678;ret\n\t"
759 	"insn_mwait: xor %eax, %eax; xor %ecx, %ecx; mwait;ret\n\t"
760 	"insn_rdpmc: xor %ecx, %ecx; rdpmc;ret\n\t"
761 	"insn_rdtsc: rdtsc;ret\n\t"
762 	"insn_cr3_load: mov cr3,%rax; mov %rax,%cr3;ret\n\t"
763 	"insn_cr3_store: mov %cr3,%rax;ret\n\t"
764 #ifdef __x86_64__
765 	"insn_cr8_load: xor %eax, %eax; mov %rax,%cr8;ret\n\t"
766 	"insn_cr8_store: mov %cr8,%rax;ret\n\t"
767 #endif
768 	"insn_monitor: xor %eax, %eax; xor %ecx, %ecx; xor %edx, %edx; monitor;ret\n\t"
769 	"insn_pause: pause;ret\n\t"
770 	"insn_wbinvd: wbinvd;ret\n\t"
771 	"insn_cpuid: mov $10, %eax; cpuid;ret\n\t"
772 	"insn_invd: invd;ret\n\t"
773 	"insn_sgdt: sgdt gdt64_desc;ret\n\t"
774 	"insn_lgdt: lgdt gdt64_desc;ret\n\t"
775 	"insn_sidt: sidt idt_descr;ret\n\t"
776 	"insn_lidt: lidt idt_descr;ret\n\t"
777 	"insn_sldt: sldt %ax;ret\n\t"
778 	"insn_lldt: xor %eax, %eax; lldt %ax;ret\n\t"
779 	"insn_str: str %ax;ret\n\t"
780 	"insn_rdrand: rdrand %rax;ret\n\t"
781 	"insn_rdseed: rdseed %rax;ret\n\t"
782 );
783 extern void insn_hlt(void);
784 extern void insn_invlpg(void);
785 extern void insn_mwait(void);
786 extern void insn_rdpmc(void);
787 extern void insn_rdtsc(void);
788 extern void insn_cr3_load(void);
789 extern void insn_cr3_store(void);
790 #ifdef __x86_64__
791 extern void insn_cr8_load(void);
792 extern void insn_cr8_store(void);
793 #endif
794 extern void insn_monitor(void);
795 extern void insn_pause(void);
796 extern void insn_wbinvd(void);
797 extern void insn_sgdt(void);
798 extern void insn_lgdt(void);
799 extern void insn_sidt(void);
800 extern void insn_lidt(void);
801 extern void insn_sldt(void);
802 extern void insn_lldt(void);
803 extern void insn_str(void);
804 extern void insn_cpuid(void);
805 extern void insn_invd(void);
806 extern void insn_rdrand(void);
807 extern void insn_rdseed(void);
808 
809 u32 cur_insn;
810 u64 cr3;
811 
812 struct insn_table {
813 	const char *name;
814 	u32 flag;
815 	void (*insn_func)(void);
816 	u32 type;
817 	u32 reason;
818 	ulong exit_qual;
819 	u32 insn_info;
820 	// Use FIELD_EXIT_QUAL and FIELD_INSN_INFO to define
821 	// which field need to be tested, reason is always tested
822 	u32 test_field;
823 };
824 
825 /*
826  * Add more test cases of instruction intercept here. Elements in this
827  * table is:
828  *	name/control flag/insn function/type/exit reason/exit qulification/
829  *	instruction info/field to test
830  * The last field defines which fields (exit_qual and insn_info) need to be
831  * tested in exit handler. If set to 0, only "reason" is checked.
832  */
833 static struct insn_table insn_table[] = {
834 	// Flags for Primary Processor-Based VM-Execution Controls
835 	{"HLT",  CPU_HLT, insn_hlt, INSN_CPU0, 12, 0, 0, 0},
836 	{"INVLPG", CPU_INVLPG, insn_invlpg, INSN_CPU0, 14,
837 		0x12345678, 0, FIELD_EXIT_QUAL},
838 	{"MWAIT", CPU_MWAIT, insn_mwait, INSN_CPU0, 36, 0, 0, 0},
839 	{"RDPMC", CPU_RDPMC, insn_rdpmc, INSN_CPU0, 15, 0, 0, 0},
840 	{"RDTSC", CPU_RDTSC, insn_rdtsc, INSN_CPU0, 16, 0, 0, 0},
841 	{"CR3 load", CPU_CR3_LOAD, insn_cr3_load, INSN_CPU0, 28, 0x3, 0,
842 		FIELD_EXIT_QUAL},
843 	{"CR3 store", CPU_CR3_STORE, insn_cr3_store, INSN_CPU0, 28, 0x13, 0,
844 		FIELD_EXIT_QUAL},
845 #ifdef __x86_64__
846 	{"CR8 load", CPU_CR8_LOAD, insn_cr8_load, INSN_CPU0, 28, 0x8, 0,
847 		FIELD_EXIT_QUAL},
848 	{"CR8 store", CPU_CR8_STORE, insn_cr8_store, INSN_CPU0, 28, 0x18, 0,
849 		FIELD_EXIT_QUAL},
850 #endif
851 	{"MONITOR", CPU_MONITOR, insn_monitor, INSN_CPU0, 39, 0, 0, 0},
852 	{"PAUSE", CPU_PAUSE, insn_pause, INSN_CPU0, 40, 0, 0, 0},
853 	// Flags for Secondary Processor-Based VM-Execution Controls
854 	{"WBINVD", CPU_WBINVD, insn_wbinvd, INSN_CPU1, 54, 0, 0, 0},
855 	{"DESC_TABLE (SGDT)", CPU_DESC_TABLE, insn_sgdt, INSN_CPU1, 46, 0, 0, 0},
856 	{"DESC_TABLE (LGDT)", CPU_DESC_TABLE, insn_lgdt, INSN_CPU1, 46, 0, 0, 0},
857 	{"DESC_TABLE (SIDT)", CPU_DESC_TABLE, insn_sidt, INSN_CPU1, 46, 0, 0, 0},
858 	{"DESC_TABLE (LIDT)", CPU_DESC_TABLE, insn_lidt, INSN_CPU1, 46, 0, 0, 0},
859 	{"DESC_TABLE (SLDT)", CPU_DESC_TABLE, insn_sldt, INSN_CPU1, 47, 0, 0, 0},
860 	{"DESC_TABLE (LLDT)", CPU_DESC_TABLE, insn_lldt, INSN_CPU1, 47, 0, 0, 0},
861 	{"DESC_TABLE (STR)", CPU_DESC_TABLE, insn_str, INSN_CPU1, 47, 0, 0, 0},
862 	/* LTR causes a #GP if done with a busy selector, so it is not tested.  */
863 	{"RDRAND", CPU_RDRAND, insn_rdrand, INSN_CPU1, VMX_RDRAND, 0, 0, 0},
864 	{"RDSEED", CPU_RDSEED, insn_rdseed, INSN_CPU1, VMX_RDSEED, 0, 0, 0},
865 	// Instructions always trap
866 	{"CPUID", 0, insn_cpuid, INSN_ALWAYS_TRAP, 10, 0, 0, 0},
867 	{"INVD", 0, insn_invd, INSN_ALWAYS_TRAP, 13, 0, 0, 0},
868 	// Instructions never trap
869 	{NULL},
870 };
871 
872 static int insn_intercept_init(struct vmcs *vmcs)
873 {
874 	u32 ctrl_cpu;
875 
876 	ctrl_cpu = ctrl_cpu_rev[0].set | CPU_SECONDARY;
877 	ctrl_cpu &= ctrl_cpu_rev[0].clr;
878 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu);
879 	vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu_rev[1].set);
880 	cr3 = read_cr3();
881 	return VMX_TEST_START;
882 }
883 
884 static void insn_intercept_main(void)
885 {
886 	for (cur_insn = 0; insn_table[cur_insn].name != NULL; cur_insn++) {
887 		vmx_set_test_stage(cur_insn * 2);
888 		if ((insn_table[cur_insn].type == INSN_CPU0 &&
889 		     !(ctrl_cpu_rev[0].clr & insn_table[cur_insn].flag)) ||
890 		    (insn_table[cur_insn].type == INSN_CPU1 &&
891 		     !(ctrl_cpu_rev[1].clr & insn_table[cur_insn].flag))) {
892 			printf("\tCPU_CTRL%d.CPU_%s is not supported.\n",
893 			       insn_table[cur_insn].type - INSN_CPU0,
894 			       insn_table[cur_insn].name);
895 			continue;
896 		}
897 
898 		if ((insn_table[cur_insn].type == INSN_CPU0 &&
899 		     !(ctrl_cpu_rev[0].set & insn_table[cur_insn].flag)) ||
900 		    (insn_table[cur_insn].type == INSN_CPU1 &&
901 		     !(ctrl_cpu_rev[1].set & insn_table[cur_insn].flag))) {
902 			/* skip hlt, it stalls the guest and is tested below */
903 			if (insn_table[cur_insn].insn_func != insn_hlt)
904 				insn_table[cur_insn].insn_func();
905 			report("execute %s", vmx_get_test_stage() == cur_insn * 2,
906 					insn_table[cur_insn].name);
907 		} else if (insn_table[cur_insn].type != INSN_ALWAYS_TRAP)
908 			printf("\tCPU_CTRL%d.CPU_%s always traps.\n",
909 			       insn_table[cur_insn].type - INSN_CPU0,
910 			       insn_table[cur_insn].name);
911 
912 		vmcall();
913 
914 		insn_table[cur_insn].insn_func();
915 		report("intercept %s", vmx_get_test_stage() == cur_insn * 2 + 1,
916 				insn_table[cur_insn].name);
917 
918 		vmx_set_test_stage(cur_insn * 2 + 1);
919 		vmcall();
920 	}
921 }
922 
923 static int insn_intercept_exit_handler(void)
924 {
925 	u64 guest_rip;
926 	u32 reason;
927 	ulong exit_qual;
928 	u32 insn_len;
929 	u32 insn_info;
930 	bool pass;
931 
932 	guest_rip = vmcs_read(GUEST_RIP);
933 	reason = vmcs_read(EXI_REASON) & 0xff;
934 	exit_qual = vmcs_read(EXI_QUALIFICATION);
935 	insn_len = vmcs_read(EXI_INST_LEN);
936 	insn_info = vmcs_read(EXI_INST_INFO);
937 
938 	if (reason == VMX_VMCALL) {
939 		u32 val = 0;
940 
941 		if (insn_table[cur_insn].type == INSN_CPU0)
942 			val = vmcs_read(CPU_EXEC_CTRL0);
943 		else if (insn_table[cur_insn].type == INSN_CPU1)
944 			val = vmcs_read(CPU_EXEC_CTRL1);
945 
946 		if (vmx_get_test_stage() & 1)
947 			val &= ~insn_table[cur_insn].flag;
948 		else
949 			val |= insn_table[cur_insn].flag;
950 
951 		if (insn_table[cur_insn].type == INSN_CPU0)
952 			vmcs_write(CPU_EXEC_CTRL0, val | ctrl_cpu_rev[0].set);
953 		else if (insn_table[cur_insn].type == INSN_CPU1)
954 			vmcs_write(CPU_EXEC_CTRL1, val | ctrl_cpu_rev[1].set);
955 	} else {
956 		pass = (cur_insn * 2 == vmx_get_test_stage()) &&
957 			insn_table[cur_insn].reason == reason;
958 		if (insn_table[cur_insn].test_field & FIELD_EXIT_QUAL &&
959 		    insn_table[cur_insn].exit_qual != exit_qual)
960 			pass = false;
961 		if (insn_table[cur_insn].test_field & FIELD_INSN_INFO &&
962 		    insn_table[cur_insn].insn_info != insn_info)
963 			pass = false;
964 		if (pass)
965 			vmx_inc_test_stage();
966 	}
967 	vmcs_write(GUEST_RIP, guest_rip + insn_len);
968 	return VMX_TEST_RESUME;
969 }
970 
971 
972 /* Enables EPT and sets up the identity map. */
973 static int setup_ept(bool enable_ad)
974 {
975 	unsigned long end_of_memory;
976 	u32 ctrl_cpu[2];
977 
978 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
979 	    !(ctrl_cpu_rev[1].clr & CPU_EPT)) {
980 		printf("\tEPT is not supported");
981 		return 1;
982 	}
983 
984 
985 	if (!(ept_vpid.val & EPT_CAP_UC) &&
986 			!(ept_vpid.val & EPT_CAP_WB)) {
987 		printf("\tEPT paging-structure memory type "
988 				"UC&WB are not supported\n");
989 		return 1;
990 	}
991 	if (ept_vpid.val & EPT_CAP_UC)
992 		eptp = EPT_MEM_TYPE_UC;
993 	else
994 		eptp = EPT_MEM_TYPE_WB;
995 	if (!(ept_vpid.val & EPT_CAP_PWL4)) {
996 		printf("\tPWL4 is not supported\n");
997 		return 1;
998 	}
999 	ctrl_cpu[0] = vmcs_read(CPU_EXEC_CTRL0);
1000 	ctrl_cpu[1] = vmcs_read(CPU_EXEC_CTRL1);
1001 	ctrl_cpu[0] = (ctrl_cpu[0] | CPU_SECONDARY)
1002 		& ctrl_cpu_rev[0].clr;
1003 	ctrl_cpu[1] = (ctrl_cpu[1] | CPU_EPT)
1004 		& ctrl_cpu_rev[1].clr;
1005 	vmcs_write(CPU_EXEC_CTRL0, ctrl_cpu[0]);
1006 	vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu[1]);
1007 	eptp |= (3 << EPTP_PG_WALK_LEN_SHIFT);
1008 	pml4 = alloc_page();
1009 	memset(pml4, 0, PAGE_SIZE);
1010 	eptp |= virt_to_phys(pml4);
1011 	if (enable_ad)
1012 		eptp |= EPTP_AD_FLAG;
1013 	vmcs_write(EPTP, eptp);
1014 	end_of_memory = fwcfg_get_u64(FW_CFG_RAM_SIZE);
1015 	if (end_of_memory < (1ul << 32))
1016 		end_of_memory = (1ul << 32);
1017 	/* Cannot use large EPT pages if we need to track EPT
1018 	 * accessed/dirty bits at 4K granularity.
1019 	 */
1020 	setup_ept_range(pml4, 0, end_of_memory, 0,
1021 			!enable_ad && ept_2m_supported(),
1022 			EPT_WA | EPT_RA | EPT_EA);
1023 	return 0;
1024 }
1025 
1026 static void ept_enable_ad_bits(void)
1027 {
1028 	eptp |= EPTP_AD_FLAG;
1029 	vmcs_write(EPTP, eptp);
1030 }
1031 
1032 static void ept_disable_ad_bits(void)
1033 {
1034 	eptp &= ~EPTP_AD_FLAG;
1035 	vmcs_write(EPTP, eptp);
1036 }
1037 
1038 static void ept_enable_ad_bits_or_skip_test(void)
1039 {
1040 	if (!ept_ad_bits_supported())
1041 		test_skip("EPT AD bits not supported.");
1042 	ept_enable_ad_bits();
1043 }
1044 
1045 static int apic_version;
1046 
1047 static int ept_init_common(bool have_ad)
1048 {
1049 	int ret;
1050 	struct pci_dev pcidev;
1051 
1052 	if (setup_ept(have_ad))
1053 		return VMX_TEST_EXIT;
1054 	data_page1 = alloc_page();
1055 	data_page2 = alloc_page();
1056 	memset(data_page1, 0x0, PAGE_SIZE);
1057 	memset(data_page2, 0x0, PAGE_SIZE);
1058 	*((u32 *)data_page1) = MAGIC_VAL_1;
1059 	*((u32 *)data_page2) = MAGIC_VAL_2;
1060 	install_ept(pml4, (unsigned long)data_page1, (unsigned long)data_page2,
1061 			EPT_RA | EPT_WA | EPT_EA);
1062 
1063 	apic_version = apic_read(APIC_LVR);
1064 
1065 	ret = pci_find_dev(PCI_VENDOR_ID_REDHAT, PCI_DEVICE_ID_REDHAT_TEST);
1066 	if (ret != PCIDEVADDR_INVALID) {
1067 		pci_dev_init(&pcidev, ret);
1068 		pci_physaddr = pcidev.resource[PCI_TESTDEV_BAR_MEM];
1069 	}
1070 
1071 	return VMX_TEST_START;
1072 }
1073 
1074 static int ept_init(struct vmcs *vmcs)
1075 {
1076 	return ept_init_common(false);
1077 }
1078 
1079 static void ept_common(void)
1080 {
1081 	vmx_set_test_stage(0);
1082 	if (*((u32 *)data_page2) != MAGIC_VAL_1 ||
1083 			*((u32 *)data_page1) != MAGIC_VAL_1)
1084 		report("EPT basic framework - read", 0);
1085 	else {
1086 		*((u32 *)data_page2) = MAGIC_VAL_3;
1087 		vmcall();
1088 		if (vmx_get_test_stage() == 1) {
1089 			if (*((u32 *)data_page1) == MAGIC_VAL_3 &&
1090 					*((u32 *)data_page2) == MAGIC_VAL_2)
1091 				report("EPT basic framework", 1);
1092 			else
1093 				report("EPT basic framework - remap", 1);
1094 		}
1095 	}
1096 	// Test EPT Misconfigurations
1097 	vmx_set_test_stage(1);
1098 	vmcall();
1099 	*((u32 *)data_page1) = MAGIC_VAL_1;
1100 	if (vmx_get_test_stage() != 2) {
1101 		report("EPT misconfigurations", 0);
1102 		goto t1;
1103 	}
1104 	vmx_set_test_stage(2);
1105 	vmcall();
1106 	*((u32 *)data_page1) = MAGIC_VAL_1;
1107 	report("EPT misconfigurations", vmx_get_test_stage() == 3);
1108 t1:
1109 	// Test EPT violation
1110 	vmx_set_test_stage(3);
1111 	vmcall();
1112 	*((u32 *)data_page1) = MAGIC_VAL_1;
1113 	report("EPT violation - page permission", vmx_get_test_stage() == 4);
1114 	// Violation caused by EPT paging structure
1115 	vmx_set_test_stage(4);
1116 	vmcall();
1117 	*((u32 *)data_page1) = MAGIC_VAL_2;
1118 	report("EPT violation - paging structure", vmx_get_test_stage() == 5);
1119 
1120 	// MMIO Read/Write
1121 	vmx_set_test_stage(5);
1122 	vmcall();
1123 
1124 	*(u32 volatile *)pci_physaddr;
1125 	report("MMIO EPT violation - read", vmx_get_test_stage() == 6);
1126 
1127 	*(u32 volatile *)pci_physaddr = MAGIC_VAL_1;
1128 	report("MMIO EPT violation - write", vmx_get_test_stage() == 7);
1129 }
1130 
1131 static void ept_main(void)
1132 {
1133 	ept_common();
1134 
1135 	// Test EPT access to L1 MMIO
1136 	vmx_set_test_stage(7);
1137 	report("EPT - MMIO access", *((u32 *)0xfee00030UL) == apic_version);
1138 
1139 	// Test invalid operand for INVEPT
1140 	vmcall();
1141 	report("EPT - unsupported INVEPT", vmx_get_test_stage() == 8);
1142 }
1143 
1144 static bool invept_test(int type, u64 eptp)
1145 {
1146 	bool ret, supported;
1147 
1148 	supported = ept_vpid.val & (EPT_CAP_INVEPT_SINGLE >> INVEPT_SINGLE << type);
1149 	ret = invept(type, eptp);
1150 
1151 	if (ret == !supported)
1152 		return false;
1153 
1154 	if (!supported)
1155 		printf("WARNING: unsupported invept passed!\n");
1156 	else
1157 		printf("WARNING: invept failed!\n");
1158 
1159 	return true;
1160 }
1161 
1162 static int pml_exit_handler(void)
1163 {
1164 	u16 index, count;
1165 	ulong reason = vmcs_read(EXI_REASON) & 0xff;
1166 	u64 *pmlbuf = pml_log;
1167 	u64 guest_rip = vmcs_read(GUEST_RIP);;
1168 	u64 guest_cr3 = vmcs_read(GUEST_CR3);
1169 	u32 insn_len = vmcs_read(EXI_INST_LEN);
1170 
1171 	switch (reason) {
1172 	case VMX_VMCALL:
1173 		switch (vmx_get_test_stage()) {
1174 		case 0:
1175 			index = vmcs_read(GUEST_PML_INDEX);
1176 			for (count = index + 1; count < PML_INDEX; count++) {
1177 				if (pmlbuf[count] == (u64)data_page2) {
1178 					vmx_inc_test_stage();
1179 					clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2);
1180 					break;
1181 				}
1182 			}
1183 			break;
1184 		case 1:
1185 			index = vmcs_read(GUEST_PML_INDEX);
1186 			/* Keep clearing the dirty bit till a overflow */
1187 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2);
1188 			break;
1189 		default:
1190 			report("unexpected stage, %d.", false,
1191 			       vmx_get_test_stage());
1192 			print_vmexit_info();
1193 			return VMX_TEST_VMEXIT;
1194 		}
1195 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
1196 		return VMX_TEST_RESUME;
1197 	case VMX_PML_FULL:
1198 		vmx_inc_test_stage();
1199 		vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1);
1200 		return VMX_TEST_RESUME;
1201 	default:
1202 		report("Unknown exit reason, %ld", false, reason);
1203 		print_vmexit_info();
1204 	}
1205 	return VMX_TEST_VMEXIT;
1206 }
1207 
1208 static int ept_exit_handler_common(bool have_ad)
1209 {
1210 	u64 guest_rip;
1211 	u64 guest_cr3;
1212 	ulong reason;
1213 	u32 insn_len;
1214 	u32 exit_qual;
1215 	static unsigned long data_page1_pte, data_page1_pte_pte, memaddr_pte;
1216 
1217 	guest_rip = vmcs_read(GUEST_RIP);
1218 	guest_cr3 = vmcs_read(GUEST_CR3);
1219 	reason = vmcs_read(EXI_REASON) & 0xff;
1220 	insn_len = vmcs_read(EXI_INST_LEN);
1221 	exit_qual = vmcs_read(EXI_QUALIFICATION);
1222 	switch (reason) {
1223 	case VMX_VMCALL:
1224 		switch (vmx_get_test_stage()) {
1225 		case 0:
1226 			check_ept_ad(pml4, guest_cr3,
1227 				     (unsigned long)data_page1,
1228 				     have_ad ? EPT_ACCESS_FLAG : 0,
1229 				     have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0);
1230 			check_ept_ad(pml4, guest_cr3,
1231 				     (unsigned long)data_page2,
1232 				     have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0,
1233 				     have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0);
1234 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1);
1235 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page2);
1236 			if (have_ad)
1237 				ept_sync(INVEPT_SINGLE, eptp);;
1238 			if (*((u32 *)data_page1) == MAGIC_VAL_3 &&
1239 					*((u32 *)data_page2) == MAGIC_VAL_2) {
1240 				vmx_inc_test_stage();
1241 				install_ept(pml4, (unsigned long)data_page2,
1242 						(unsigned long)data_page2,
1243 						EPT_RA | EPT_WA | EPT_EA);
1244 			} else
1245 				report("EPT basic framework - write", 0);
1246 			break;
1247 		case 1:
1248 			install_ept(pml4, (unsigned long)data_page1,
1249  				(unsigned long)data_page1, EPT_WA);
1250 			ept_sync(INVEPT_SINGLE, eptp);
1251 			break;
1252 		case 2:
1253 			install_ept(pml4, (unsigned long)data_page1,
1254  				(unsigned long)data_page1,
1255  				EPT_RA | EPT_WA | EPT_EA |
1256  				(2 << EPT_MEM_TYPE_SHIFT));
1257 			ept_sync(INVEPT_SINGLE, eptp);
1258 			break;
1259 		case 3:
1260 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1);
1261 			TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1,
1262 						1, &data_page1_pte));
1263 			set_ept_pte(pml4, (unsigned long)data_page1,
1264 				1, data_page1_pte & ~EPT_PRESENT);
1265 			ept_sync(INVEPT_SINGLE, eptp);
1266 			break;
1267 		case 4:
1268 			TEST_ASSERT(get_ept_pte(pml4, (unsigned long)data_page1,
1269 						2, &data_page1_pte));
1270 			data_page1_pte &= PAGE_MASK;
1271 			TEST_ASSERT(get_ept_pte(pml4, data_page1_pte,
1272 						2, &data_page1_pte_pte));
1273 			set_ept_pte(pml4, data_page1_pte, 2,
1274 				data_page1_pte_pte & ~EPT_PRESENT);
1275 			ept_sync(INVEPT_SINGLE, eptp);
1276 			break;
1277 		case 5:
1278 			install_ept(pml4, (unsigned long)pci_physaddr,
1279 				(unsigned long)pci_physaddr, 0);
1280 			ept_sync(INVEPT_SINGLE, eptp);
1281 			break;
1282 		case 7:
1283 			if (!invept_test(0, eptp))
1284 				vmx_inc_test_stage();
1285 			break;
1286 		// Should not reach here
1287 		default:
1288 			report("ERROR - unexpected stage, %d.", false,
1289 			       vmx_get_test_stage());
1290 			print_vmexit_info();
1291 			return VMX_TEST_VMEXIT;
1292 		}
1293 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
1294 		return VMX_TEST_RESUME;
1295 	case VMX_EPT_MISCONFIG:
1296 		switch (vmx_get_test_stage()) {
1297 		case 1:
1298 		case 2:
1299 			vmx_inc_test_stage();
1300 			install_ept(pml4, (unsigned long)data_page1,
1301  				(unsigned long)data_page1,
1302  				EPT_RA | EPT_WA | EPT_EA);
1303 			ept_sync(INVEPT_SINGLE, eptp);
1304 			break;
1305 		// Should not reach here
1306 		default:
1307 			report("ERROR - unexpected stage, %d.", false,
1308 			       vmx_get_test_stage());
1309 			print_vmexit_info();
1310 			return VMX_TEST_VMEXIT;
1311 		}
1312 		return VMX_TEST_RESUME;
1313 	case VMX_EPT_VIOLATION:
1314 		switch(vmx_get_test_stage()) {
1315 		case 3:
1316 			check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0,
1317 				     have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0);
1318 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1);
1319 			if (exit_qual == (EPT_VLT_WR | EPT_VLT_LADDR_VLD |
1320 					EPT_VLT_PADDR))
1321 				vmx_inc_test_stage();
1322 			set_ept_pte(pml4, (unsigned long)data_page1,
1323 				1, data_page1_pte | (EPT_PRESENT));
1324 			ept_sync(INVEPT_SINGLE, eptp);
1325 			break;
1326 		case 4:
1327 			check_ept_ad(pml4, guest_cr3, (unsigned long)data_page1, 0,
1328 				     have_ad ? EPT_ACCESS_FLAG | EPT_DIRTY_FLAG : 0);
1329 			clear_ept_ad(pml4, guest_cr3, (unsigned long)data_page1);
1330 			if (exit_qual == (EPT_VLT_RD |
1331 					  (have_ad ? EPT_VLT_WR : 0) |
1332 					  EPT_VLT_LADDR_VLD))
1333 				vmx_inc_test_stage();
1334 			set_ept_pte(pml4, data_page1_pte, 2,
1335 				data_page1_pte_pte | (EPT_PRESENT));
1336 			ept_sync(INVEPT_SINGLE, eptp);
1337 			break;
1338 		case 5:
1339 			if (exit_qual & EPT_VLT_RD)
1340 				vmx_inc_test_stage();
1341 			TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr,
1342 						1, &memaddr_pte));
1343 			set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA);
1344 			ept_sync(INVEPT_SINGLE, eptp);
1345 			break;
1346 		case 6:
1347 			if (exit_qual & EPT_VLT_WR)
1348 				vmx_inc_test_stage();
1349 			TEST_ASSERT(get_ept_pte(pml4, (unsigned long)pci_physaddr,
1350 						1, &memaddr_pte));
1351 			set_ept_pte(pml4, memaddr_pte, 1, memaddr_pte | EPT_RA | EPT_WA);
1352 			ept_sync(INVEPT_SINGLE, eptp);
1353 			break;
1354 		default:
1355 			// Should not reach here
1356 			report("ERROR : unexpected stage, %d", false,
1357 			       vmx_get_test_stage());
1358 			print_vmexit_info();
1359 			return VMX_TEST_VMEXIT;
1360 		}
1361 		return VMX_TEST_RESUME;
1362 	default:
1363 		report("Unknown exit reason, %ld", false, reason);
1364 		print_vmexit_info();
1365 	}
1366 	return VMX_TEST_VMEXIT;
1367 }
1368 
1369 static int ept_exit_handler(void)
1370 {
1371 	return ept_exit_handler_common(false);
1372 }
1373 
1374 static int eptad_init(struct vmcs *vmcs)
1375 {
1376 	int r = ept_init_common(true);
1377 
1378 	if (r == VMX_TEST_EXIT)
1379 		return r;
1380 
1381 	if ((rdmsr(MSR_IA32_VMX_EPT_VPID_CAP) & EPT_CAP_AD_FLAG) == 0) {
1382 		printf("\tEPT A/D bits are not supported");
1383 		return VMX_TEST_EXIT;
1384 	}
1385 
1386 	return r;
1387 }
1388 
1389 static int pml_init(struct vmcs *vmcs)
1390 {
1391 	u32 ctrl_cpu;
1392 	int r = eptad_init(vmcs);
1393 
1394 	if (r == VMX_TEST_EXIT)
1395 		return r;
1396 
1397 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
1398 		!(ctrl_cpu_rev[1].clr & CPU_PML)) {
1399 		printf("\tPML is not supported");
1400 		return VMX_TEST_EXIT;
1401 	}
1402 
1403 	pml_log = alloc_page();
1404 	memset(pml_log, 0x0, PAGE_SIZE);
1405 	vmcs_write(PMLADDR, (u64)pml_log);
1406 	vmcs_write(GUEST_PML_INDEX, PML_INDEX - 1);
1407 
1408 	ctrl_cpu = vmcs_read(CPU_EXEC_CTRL1) | CPU_PML;
1409 	vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu);
1410 
1411 	return VMX_TEST_START;
1412 }
1413 
1414 static void pml_main(void)
1415 {
1416 	int count = 0;
1417 
1418 	vmx_set_test_stage(0);
1419 	*((u32 *)data_page2) = 0x1;
1420 	vmcall();
1421 	report("PML - Dirty GPA Logging", vmx_get_test_stage() == 1);
1422 
1423 	while (vmx_get_test_stage() == 1) {
1424 		vmcall();
1425 		*((u32 *)data_page2) = 0x1;
1426 		if (count++ > PML_INDEX)
1427 			break;
1428 	}
1429 	report("PML Full Event", vmx_get_test_stage() == 2);
1430 }
1431 
1432 static void eptad_main(void)
1433 {
1434 	ept_common();
1435 }
1436 
1437 static int eptad_exit_handler(void)
1438 {
1439 	return ept_exit_handler_common(true);
1440 }
1441 
1442 static bool invvpid_test(int type, u16 vpid)
1443 {
1444 	bool ret, supported;
1445 
1446 	supported = ept_vpid.val &
1447 		(VPID_CAP_INVVPID_ADDR >> INVVPID_ADDR << type);
1448 	ret = invvpid(type, vpid, 0);
1449 
1450 	if (ret == !supported)
1451 		return false;
1452 
1453 	if (!supported)
1454 		printf("WARNING: unsupported invvpid passed!\n");
1455 	else
1456 		printf("WARNING: invvpid failed!\n");
1457 
1458 	return true;
1459 }
1460 
1461 static int vpid_init(struct vmcs *vmcs)
1462 {
1463 	u32 ctrl_cpu1;
1464 
1465 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
1466 		!(ctrl_cpu_rev[1].clr & CPU_VPID)) {
1467 		printf("\tVPID is not supported");
1468 		return VMX_TEST_EXIT;
1469 	}
1470 
1471 	ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1);
1472 	ctrl_cpu1 |= CPU_VPID;
1473 	vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1);
1474 	return VMX_TEST_START;
1475 }
1476 
1477 static void vpid_main(void)
1478 {
1479 	vmx_set_test_stage(0);
1480 	vmcall();
1481 	report("INVVPID SINGLE ADDRESS", vmx_get_test_stage() == 1);
1482 	vmx_set_test_stage(2);
1483 	vmcall();
1484 	report("INVVPID SINGLE", vmx_get_test_stage() == 3);
1485 	vmx_set_test_stage(4);
1486 	vmcall();
1487 	report("INVVPID ALL", vmx_get_test_stage() == 5);
1488 }
1489 
1490 static int vpid_exit_handler(void)
1491 {
1492 	u64 guest_rip;
1493 	ulong reason;
1494 	u32 insn_len;
1495 
1496 	guest_rip = vmcs_read(GUEST_RIP);
1497 	reason = vmcs_read(EXI_REASON) & 0xff;
1498 	insn_len = vmcs_read(EXI_INST_LEN);
1499 
1500 	switch (reason) {
1501 	case VMX_VMCALL:
1502 		switch(vmx_get_test_stage()) {
1503 		case 0:
1504 			if (!invvpid_test(INVVPID_ADDR, 1))
1505 				vmx_inc_test_stage();
1506 			break;
1507 		case 2:
1508 			if (!invvpid_test(INVVPID_CONTEXT_GLOBAL, 1))
1509 				vmx_inc_test_stage();
1510 			break;
1511 		case 4:
1512 			if (!invvpid_test(INVVPID_ALL, 1))
1513 				vmx_inc_test_stage();
1514 			break;
1515 		default:
1516 			report("ERROR: unexpected stage, %d", false,
1517 					vmx_get_test_stage());
1518 			print_vmexit_info();
1519 			return VMX_TEST_VMEXIT;
1520 		}
1521 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
1522 		return VMX_TEST_RESUME;
1523 	default:
1524 		report("Unknown exit reason, %ld", false, reason);
1525 		print_vmexit_info();
1526 	}
1527 	return VMX_TEST_VMEXIT;
1528 }
1529 
1530 #define TIMER_VECTOR	222
1531 
1532 static volatile bool timer_fired;
1533 
1534 static void timer_isr(isr_regs_t *regs)
1535 {
1536 	timer_fired = true;
1537 	apic_write(APIC_EOI, 0);
1538 }
1539 
1540 static int interrupt_init(struct vmcs *vmcs)
1541 {
1542 	msr_bmp_init();
1543 	vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT);
1544 	handle_irq(TIMER_VECTOR, timer_isr);
1545 	return VMX_TEST_START;
1546 }
1547 
1548 static void interrupt_main(void)
1549 {
1550 	long long start, loops;
1551 
1552 	vmx_set_test_stage(0);
1553 
1554 	apic_write(APIC_LVTT, TIMER_VECTOR);
1555 	irq_enable();
1556 
1557 	apic_write(APIC_TMICT, 1);
1558 	for (loops = 0; loops < 10000000 && !timer_fired; loops++)
1559 		asm volatile ("nop");
1560 	report("direct interrupt while running guest", timer_fired);
1561 
1562 	apic_write(APIC_TMICT, 0);
1563 	irq_disable();
1564 	vmcall();
1565 	timer_fired = false;
1566 	apic_write(APIC_TMICT, 1);
1567 	for (loops = 0; loops < 10000000 && !timer_fired; loops++)
1568 		asm volatile ("nop");
1569 	report("intercepted interrupt while running guest", timer_fired);
1570 
1571 	irq_enable();
1572 	apic_write(APIC_TMICT, 0);
1573 	irq_disable();
1574 	vmcall();
1575 	timer_fired = false;
1576 	start = rdtsc();
1577 	apic_write(APIC_TMICT, 1000000);
1578 
1579 	asm volatile ("sti; hlt");
1580 
1581 	report("direct interrupt + hlt",
1582 	       rdtsc() - start > 1000000 && timer_fired);
1583 
1584 	apic_write(APIC_TMICT, 0);
1585 	irq_disable();
1586 	vmcall();
1587 	timer_fired = false;
1588 	start = rdtsc();
1589 	apic_write(APIC_TMICT, 1000000);
1590 
1591 	asm volatile ("sti; hlt");
1592 
1593 	report("intercepted interrupt + hlt",
1594 	       rdtsc() - start > 10000 && timer_fired);
1595 
1596 	apic_write(APIC_TMICT, 0);
1597 	irq_disable();
1598 	vmcall();
1599 	timer_fired = false;
1600 	start = rdtsc();
1601 	apic_write(APIC_TMICT, 1000000);
1602 
1603 	irq_enable();
1604 	asm volatile ("nop");
1605 	vmcall();
1606 
1607 	report("direct interrupt + activity state hlt",
1608 	       rdtsc() - start > 10000 && timer_fired);
1609 
1610 	apic_write(APIC_TMICT, 0);
1611 	irq_disable();
1612 	vmcall();
1613 	timer_fired = false;
1614 	start = rdtsc();
1615 	apic_write(APIC_TMICT, 1000000);
1616 
1617 	irq_enable();
1618 	asm volatile ("nop");
1619 	vmcall();
1620 
1621 	report("intercepted interrupt + activity state hlt",
1622 	       rdtsc() - start > 10000 && timer_fired);
1623 
1624 	apic_write(APIC_TMICT, 0);
1625 	irq_disable();
1626 	vmx_set_test_stage(7);
1627 	vmcall();
1628 	timer_fired = false;
1629 	apic_write(APIC_TMICT, 1);
1630 	for (loops = 0; loops < 10000000 && !timer_fired; loops++)
1631 		asm volatile ("nop");
1632 	report("running a guest with interrupt acknowledgement set", timer_fired);
1633 
1634 	apic_write(APIC_TMICT, 0);
1635 	irq_enable();
1636 	timer_fired = false;
1637 	vmcall();
1638 	report("Inject an event to a halted guest", timer_fired);
1639 }
1640 
1641 static int interrupt_exit_handler(void)
1642 {
1643 	u64 guest_rip = vmcs_read(GUEST_RIP);
1644 	ulong reason = vmcs_read(EXI_REASON) & 0xff;
1645 	u32 insn_len = vmcs_read(EXI_INST_LEN);
1646 
1647 	switch (reason) {
1648 	case VMX_VMCALL:
1649 		switch (vmx_get_test_stage()) {
1650 		case 0:
1651 		case 2:
1652 		case 5:
1653 			vmcs_write(PIN_CONTROLS,
1654 				   vmcs_read(PIN_CONTROLS) | PIN_EXTINT);
1655 			break;
1656 		case 7:
1657 			vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_INTA);
1658 			vmcs_write(PIN_CONTROLS,
1659 				   vmcs_read(PIN_CONTROLS) | PIN_EXTINT);
1660 			break;
1661 		case 1:
1662 		case 3:
1663 			vmcs_write(PIN_CONTROLS,
1664 				   vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT);
1665 			break;
1666 		case 4:
1667 		case 6:
1668 			vmcs_write(GUEST_ACTV_STATE, ACTV_HLT);
1669 			break;
1670 
1671 		case 8:
1672 			vmcs_write(GUEST_ACTV_STATE, ACTV_HLT);
1673 			vmcs_write(ENT_INTR_INFO,
1674 				   TIMER_VECTOR |
1675 				   (VMX_INTR_TYPE_EXT_INTR << INTR_INFO_INTR_TYPE_SHIFT) |
1676 				   INTR_INFO_VALID_MASK);
1677 			break;
1678 		}
1679 		vmx_inc_test_stage();
1680 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
1681 		return VMX_TEST_RESUME;
1682 	case VMX_EXTINT:
1683 		if (vmcs_read(EXI_CONTROLS) & EXI_INTA) {
1684 			int vector = vmcs_read(EXI_INTR_INFO) & 0xff;
1685 			handle_external_interrupt(vector);
1686 		} else {
1687 			irq_enable();
1688 			asm volatile ("nop");
1689 			irq_disable();
1690 		}
1691 		if (vmx_get_test_stage() >= 2)
1692 			vmcs_write(GUEST_ACTV_STATE, ACTV_ACTIVE);
1693 		return VMX_TEST_RESUME;
1694 	default:
1695 		report("Unknown exit reason, %ld", false, reason);
1696 		print_vmexit_info();
1697 	}
1698 
1699 	return VMX_TEST_VMEXIT;
1700 }
1701 
1702 static int dbgctls_init(struct vmcs *vmcs)
1703 {
1704 	u64 dr7 = 0x402;
1705 	u64 zero = 0;
1706 
1707 	msr_bmp_init();
1708 	asm volatile(
1709 		"mov %0,%%dr0\n\t"
1710 		"mov %0,%%dr1\n\t"
1711 		"mov %0,%%dr2\n\t"
1712 		"mov %1,%%dr7\n\t"
1713 		: : "r" (zero), "r" (dr7));
1714 	wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1);
1715 	vmcs_write(GUEST_DR7, 0x404);
1716 	vmcs_write(GUEST_DEBUGCTL, 0x2);
1717 
1718 	vmcs_write(ENT_CONTROLS, vmcs_read(ENT_CONTROLS) | ENT_LOAD_DBGCTLS);
1719 	vmcs_write(EXI_CONTROLS, vmcs_read(EXI_CONTROLS) | EXI_SAVE_DBGCTLS);
1720 
1721 	return VMX_TEST_START;
1722 }
1723 
1724 static void dbgctls_main(void)
1725 {
1726 	u64 dr7, debugctl;
1727 
1728 	asm volatile("mov %%dr7,%0" : "=r" (dr7));
1729 	debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
1730 	/* Commented out: KVM does not support DEBUGCTL so far */
1731 	(void)debugctl;
1732 	report("Load debug controls", dr7 == 0x404 /* && debugctl == 0x2 */);
1733 
1734 	dr7 = 0x408;
1735 	asm volatile("mov %0,%%dr7" : : "r" (dr7));
1736 	wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3);
1737 
1738 	vmx_set_test_stage(0);
1739 	vmcall();
1740 	report("Save debug controls", vmx_get_test_stage() == 1);
1741 
1742 	if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS ||
1743 	    ctrl_exit_rev.set & EXI_SAVE_DBGCTLS) {
1744 		printf("\tDebug controls are always loaded/saved\n");
1745 		return;
1746 	}
1747 	vmx_set_test_stage(2);
1748 	vmcall();
1749 
1750 	asm volatile("mov %%dr7,%0" : "=r" (dr7));
1751 	debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
1752 	/* Commented out: KVM does not support DEBUGCTL so far */
1753 	(void)debugctl;
1754 	report("Guest=host debug controls", dr7 == 0x402 /* && debugctl == 0x1 */);
1755 
1756 	dr7 = 0x408;
1757 	asm volatile("mov %0,%%dr7" : : "r" (dr7));
1758 	wrmsr(MSR_IA32_DEBUGCTLMSR, 0x3);
1759 
1760 	vmx_set_test_stage(3);
1761 	vmcall();
1762 	report("Don't save debug controls", vmx_get_test_stage() == 4);
1763 }
1764 
1765 static int dbgctls_exit_handler(void)
1766 {
1767 	unsigned int reason = vmcs_read(EXI_REASON) & 0xff;
1768 	u32 insn_len = vmcs_read(EXI_INST_LEN);
1769 	u64 guest_rip = vmcs_read(GUEST_RIP);
1770 	u64 dr7, debugctl;
1771 
1772 	asm volatile("mov %%dr7,%0" : "=r" (dr7));
1773 	debugctl = rdmsr(MSR_IA32_DEBUGCTLMSR);
1774 
1775 	switch (reason) {
1776 	case VMX_VMCALL:
1777 		switch (vmx_get_test_stage()) {
1778 		case 0:
1779 			if (dr7 == 0x400 && debugctl == 0 &&
1780 			    vmcs_read(GUEST_DR7) == 0x408 /* &&
1781 			    Commented out: KVM does not support DEBUGCTL so far
1782 			    vmcs_read(GUEST_DEBUGCTL) == 0x3 */)
1783 				vmx_inc_test_stage();
1784 			break;
1785 		case 2:
1786 			dr7 = 0x402;
1787 			asm volatile("mov %0,%%dr7" : : "r" (dr7));
1788 			wrmsr(MSR_IA32_DEBUGCTLMSR, 0x1);
1789 			vmcs_write(GUEST_DR7, 0x404);
1790 			vmcs_write(GUEST_DEBUGCTL, 0x2);
1791 
1792 			vmcs_write(ENT_CONTROLS,
1793 				vmcs_read(ENT_CONTROLS) & ~ENT_LOAD_DBGCTLS);
1794 			vmcs_write(EXI_CONTROLS,
1795 				vmcs_read(EXI_CONTROLS) & ~EXI_SAVE_DBGCTLS);
1796 			break;
1797 		case 3:
1798 			if (dr7 == 0x400 && debugctl == 0 &&
1799 			    vmcs_read(GUEST_DR7) == 0x404 /* &&
1800 			    Commented out: KVM does not support DEBUGCTL so far
1801 			    vmcs_read(GUEST_DEBUGCTL) == 0x2 */)
1802 				vmx_inc_test_stage();
1803 			break;
1804 		}
1805 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
1806 		return VMX_TEST_RESUME;
1807 	default:
1808 		report("Unknown exit reason, %d", false, reason);
1809 		print_vmexit_info();
1810 	}
1811 	return VMX_TEST_VMEXIT;
1812 }
1813 
1814 struct vmx_msr_entry {
1815 	u32 index;
1816 	u32 reserved;
1817 	u64 value;
1818 } __attribute__((packed));
1819 
1820 #define MSR_MAGIC 0x31415926
1821 struct vmx_msr_entry *exit_msr_store, *entry_msr_load, *exit_msr_load;
1822 
1823 static int msr_switch_init(struct vmcs *vmcs)
1824 {
1825 	msr_bmp_init();
1826 	exit_msr_store = alloc_page();
1827 	exit_msr_load = alloc_page();
1828 	entry_msr_load = alloc_page();
1829 	memset(exit_msr_store, 0, PAGE_SIZE);
1830 	memset(exit_msr_load, 0, PAGE_SIZE);
1831 	memset(entry_msr_load, 0, PAGE_SIZE);
1832 	entry_msr_load[0].index = MSR_KERNEL_GS_BASE;
1833 	entry_msr_load[0].value = MSR_MAGIC;
1834 
1835 	vmx_set_test_stage(1);
1836 	vmcs_write(ENT_MSR_LD_CNT, 1);
1837 	vmcs_write(ENTER_MSR_LD_ADDR, (u64)entry_msr_load);
1838 	vmcs_write(EXI_MSR_ST_CNT, 1);
1839 	vmcs_write(EXIT_MSR_ST_ADDR, (u64)exit_msr_store);
1840 	vmcs_write(EXI_MSR_LD_CNT, 1);
1841 	vmcs_write(EXIT_MSR_LD_ADDR, (u64)exit_msr_load);
1842 	return VMX_TEST_START;
1843 }
1844 
1845 static void msr_switch_main(void)
1846 {
1847 	if (vmx_get_test_stage() == 1) {
1848 		report("VM entry MSR load",
1849 			rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC);
1850 		vmx_set_test_stage(2);
1851 		wrmsr(MSR_KERNEL_GS_BASE, MSR_MAGIC + 1);
1852 		exit_msr_store[0].index = MSR_KERNEL_GS_BASE;
1853 		exit_msr_load[0].index = MSR_KERNEL_GS_BASE;
1854 		exit_msr_load[0].value = MSR_MAGIC + 2;
1855 	}
1856 	vmcall();
1857 }
1858 
1859 static int msr_switch_exit_handler(void)
1860 {
1861 	ulong reason;
1862 
1863 	reason = vmcs_read(EXI_REASON);
1864 	if (reason == VMX_VMCALL && vmx_get_test_stage() == 2) {
1865 		report("VM exit MSR store",
1866 			exit_msr_store[0].value == MSR_MAGIC + 1);
1867 		report("VM exit MSR load",
1868 			rdmsr(MSR_KERNEL_GS_BASE) == MSR_MAGIC + 2);
1869 		vmx_set_test_stage(3);
1870 		entry_msr_load[0].index = MSR_FS_BASE;
1871 		return VMX_TEST_RESUME;
1872 	}
1873 	printf("ERROR %s: unexpected stage=%u or reason=%lu\n",
1874 		__func__, vmx_get_test_stage(), reason);
1875 	return VMX_TEST_EXIT;
1876 }
1877 
1878 static int msr_switch_entry_failure(struct vmentry_failure *failure)
1879 {
1880 	ulong reason;
1881 
1882 	if (failure->early) {
1883 		printf("ERROR %s: early exit\n", __func__);
1884 		return VMX_TEST_EXIT;
1885 	}
1886 
1887 	reason = vmcs_read(EXI_REASON);
1888 	if (reason == (VMX_ENTRY_FAILURE | VMX_FAIL_MSR) &&
1889 	    vmx_get_test_stage() == 3) {
1890 		report("VM entry MSR load: try to load FS_BASE",
1891 			vmcs_read(EXI_QUALIFICATION) == 1);
1892 		return VMX_TEST_VMEXIT;
1893 	}
1894 	printf("ERROR %s: unexpected stage=%u or reason=%lu\n",
1895 		__func__, vmx_get_test_stage(), reason);
1896 	return VMX_TEST_EXIT;
1897 }
1898 
1899 static int vmmcall_init(struct vmcs *vmcs)
1900 {
1901 	vmcs_write(EXC_BITMAP, 1 << UD_VECTOR);
1902 	return VMX_TEST_START;
1903 }
1904 
1905 static void vmmcall_main(void)
1906 {
1907 	asm volatile(
1908 		"mov $0xABCD, %%rax\n\t"
1909 		"vmmcall\n\t"
1910 		::: "rax");
1911 
1912 	report("VMMCALL", 0);
1913 }
1914 
1915 static int vmmcall_exit_handler(void)
1916 {
1917 	ulong reason;
1918 
1919 	reason = vmcs_read(EXI_REASON);
1920 	switch (reason) {
1921 	case VMX_VMCALL:
1922 		printf("here\n");
1923 		report("VMMCALL triggers #UD", 0);
1924 		break;
1925 	case VMX_EXC_NMI:
1926 		report("VMMCALL triggers #UD",
1927 		       (vmcs_read(EXI_INTR_INFO) & 0xff) == UD_VECTOR);
1928 		break;
1929 	default:
1930 		report("Unknown exit reason, %ld", false, reason);
1931 		print_vmexit_info();
1932 	}
1933 
1934 	return VMX_TEST_VMEXIT;
1935 }
1936 
1937 static int disable_rdtscp_init(struct vmcs *vmcs)
1938 {
1939 	u32 ctrl_cpu1;
1940 
1941 	if (ctrl_cpu_rev[0].clr & CPU_SECONDARY) {
1942 		ctrl_cpu1 = vmcs_read(CPU_EXEC_CTRL1);
1943 		ctrl_cpu1 &= ~CPU_RDTSCP;
1944 		vmcs_write(CPU_EXEC_CTRL1, ctrl_cpu1);
1945 	}
1946 
1947 	return VMX_TEST_START;
1948 }
1949 
1950 static void disable_rdtscp_ud_handler(struct ex_regs *regs)
1951 {
1952 	switch (vmx_get_test_stage()) {
1953 	case 0:
1954 		report("RDTSCP triggers #UD", true);
1955 		vmx_inc_test_stage();
1956 		regs->rip += 3;
1957 		break;
1958 	case 2:
1959 		report("RDPID triggers #UD", true);
1960 		vmx_inc_test_stage();
1961 		regs->rip += 4;
1962 		break;
1963 	}
1964 	return;
1965 
1966 }
1967 
1968 static void disable_rdtscp_main(void)
1969 {
1970 	/* Test that #UD is properly injected in L2.  */
1971 	handle_exception(UD_VECTOR, disable_rdtscp_ud_handler);
1972 
1973 	vmx_set_test_stage(0);
1974 	asm volatile("rdtscp" : : : "eax", "ecx", "edx");
1975 	vmcall();
1976 	asm volatile(".byte 0xf3, 0x0f, 0xc7, 0xf8" : : : "eax");
1977 
1978 	handle_exception(UD_VECTOR, 0);
1979 	vmcall();
1980 }
1981 
1982 static int disable_rdtscp_exit_handler(void)
1983 {
1984 	unsigned int reason = vmcs_read(EXI_REASON) & 0xff;
1985 
1986 	switch (reason) {
1987 	case VMX_VMCALL:
1988 		switch (vmx_get_test_stage()) {
1989 		case 0:
1990 			report("RDTSCP triggers #UD", false);
1991 			vmx_inc_test_stage();
1992 			/* fallthrough */
1993 		case 1:
1994 			vmx_inc_test_stage();
1995 			vmcs_write(GUEST_RIP, vmcs_read(GUEST_RIP) + 3);
1996 			return VMX_TEST_RESUME;
1997 		case 2:
1998 			report("RDPID triggers #UD", false);
1999 			break;
2000 		}
2001 		break;
2002 
2003 	default:
2004 		report("Unknown exit reason, %d", false, reason);
2005 		print_vmexit_info();
2006 	}
2007 	return VMX_TEST_VMEXIT;
2008 }
2009 
2010 static int int3_init(struct vmcs *vmcs)
2011 {
2012 	vmcs_write(EXC_BITMAP, ~0u);
2013 	return VMX_TEST_START;
2014 }
2015 
2016 static void int3_guest_main(void)
2017 {
2018 	asm volatile ("int3");
2019 }
2020 
2021 static int int3_exit_handler(void)
2022 {
2023 	u32 reason = vmcs_read(EXI_REASON);
2024 	u32 intr_info = vmcs_read(EXI_INTR_INFO);
2025 
2026 	report("L1 intercepts #BP", reason == VMX_EXC_NMI &&
2027 	       (intr_info & INTR_INFO_VALID_MASK) &&
2028 	       (intr_info & INTR_INFO_VECTOR_MASK) == BP_VECTOR &&
2029 	       ((intr_info & INTR_INFO_INTR_TYPE_MASK) >>
2030 		INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION);
2031 
2032 	return VMX_TEST_VMEXIT;
2033 }
2034 
2035 static int into_init(struct vmcs *vmcs)
2036 {
2037 	vmcs_write(EXC_BITMAP, ~0u);
2038 	return VMX_TEST_START;
2039 }
2040 
2041 static void into_guest_main(void)
2042 {
2043 	struct far_pointer32 fp = {
2044 		.offset = (uintptr_t)&&into,
2045 		.selector = KERNEL_CS32,
2046 	};
2047 	register uintptr_t rsp asm("rsp");
2048 
2049 	if (fp.offset != (uintptr_t)&&into) {
2050 		printf("Code address too high.\n");
2051 		return;
2052 	}
2053 	if ((u32)rsp != rsp) {
2054 		printf("Stack address too high.\n");
2055 		return;
2056 	}
2057 
2058 	asm goto ("lcall *%0" : : "m" (fp) : "rax" : into);
2059 	return;
2060 into:
2061 	asm volatile (".code32;"
2062 		      "movl $0x7fffffff, %eax;"
2063 		      "addl %eax, %eax;"
2064 		      "into;"
2065 		      "lret;"
2066 		      ".code64");
2067 	__builtin_unreachable();
2068 }
2069 
2070 static int into_exit_handler(void)
2071 {
2072 	u32 reason = vmcs_read(EXI_REASON);
2073 	u32 intr_info = vmcs_read(EXI_INTR_INFO);
2074 
2075 	report("L1 intercepts #OF", reason == VMX_EXC_NMI &&
2076 	       (intr_info & INTR_INFO_VALID_MASK) &&
2077 	       (intr_info & INTR_INFO_VECTOR_MASK) == OF_VECTOR &&
2078 	       ((intr_info & INTR_INFO_INTR_TYPE_MASK) >>
2079 		INTR_INFO_INTR_TYPE_SHIFT) == VMX_INTR_TYPE_SOFT_EXCEPTION);
2080 
2081 	return VMX_TEST_VMEXIT;
2082 }
2083 
2084 static void exit_monitor_from_l2_main(void)
2085 {
2086 	printf("Calling exit(0) from l2...\n");
2087 	exit(0);
2088 }
2089 
2090 static int exit_monitor_from_l2_handler(void)
2091 {
2092 	report("The guest should have killed the VMM", false);
2093 	return VMX_TEST_EXIT;
2094 }
2095 
2096 static void assert_exit_reason(u64 expected)
2097 {
2098 	u64 actual = vmcs_read(EXI_REASON);
2099 
2100 	TEST_ASSERT_EQ_MSG(expected, actual, "Expected %s, got %s.",
2101 			   exit_reason_description(expected),
2102 			   exit_reason_description(actual));
2103 }
2104 
2105 static void skip_exit_insn(void)
2106 {
2107 	u64 guest_rip = vmcs_read(GUEST_RIP);
2108 	u32 insn_len = vmcs_read(EXI_INST_LEN);
2109 	vmcs_write(GUEST_RIP, guest_rip + insn_len);
2110 }
2111 
2112 static void skip_exit_vmcall(void)
2113 {
2114 	assert_exit_reason(VMX_VMCALL);
2115 	skip_exit_insn();
2116 }
2117 
2118 static void v2_null_test_guest(void)
2119 {
2120 }
2121 
2122 static void v2_null_test(void)
2123 {
2124 	test_set_guest(v2_null_test_guest);
2125 	enter_guest();
2126 	report(__func__, 1);
2127 }
2128 
2129 static void v2_multiple_entries_test_guest(void)
2130 {
2131 	vmx_set_test_stage(1);
2132 	vmcall();
2133 	vmx_set_test_stage(2);
2134 }
2135 
2136 static void v2_multiple_entries_test(void)
2137 {
2138 	test_set_guest(v2_multiple_entries_test_guest);
2139 	enter_guest();
2140 	TEST_ASSERT_EQ(vmx_get_test_stage(), 1);
2141 	skip_exit_vmcall();
2142 	enter_guest();
2143 	TEST_ASSERT_EQ(vmx_get_test_stage(), 2);
2144 	report(__func__, 1);
2145 }
2146 
2147 static int fixture_test_data = 1;
2148 
2149 static void fixture_test_teardown(void *data)
2150 {
2151 	*((int *) data) = 1;
2152 }
2153 
2154 static void fixture_test_guest(void)
2155 {
2156 	fixture_test_data++;
2157 }
2158 
2159 
2160 static void fixture_test_setup(void)
2161 {
2162 	TEST_ASSERT_EQ_MSG(1, fixture_test_data,
2163 			   "fixture_test_teardown didn't run?!");
2164 	fixture_test_data = 2;
2165 	test_add_teardown(fixture_test_teardown, &fixture_test_data);
2166 	test_set_guest(fixture_test_guest);
2167 }
2168 
2169 static void fixture_test_case1(void)
2170 {
2171 	fixture_test_setup();
2172 	TEST_ASSERT_EQ(2, fixture_test_data);
2173 	enter_guest();
2174 	TEST_ASSERT_EQ(3, fixture_test_data);
2175 	report(__func__, 1);
2176 }
2177 
2178 static void fixture_test_case2(void)
2179 {
2180 	fixture_test_setup();
2181 	TEST_ASSERT_EQ(2, fixture_test_data);
2182 	enter_guest();
2183 	TEST_ASSERT_EQ(3, fixture_test_data);
2184 	report(__func__, 1);
2185 }
2186 
2187 enum ept_access_op {
2188 	OP_READ,
2189 	OP_WRITE,
2190 	OP_EXEC,
2191 	OP_FLUSH_TLB,
2192 	OP_EXIT,
2193 };
2194 
2195 static struct ept_access_test_data {
2196 	unsigned long gpa;
2197 	unsigned long *gva;
2198 	unsigned long hpa;
2199 	unsigned long *hva;
2200 	enum ept_access_op op;
2201 } ept_access_test_data;
2202 
2203 extern unsigned char ret42_start;
2204 extern unsigned char ret42_end;
2205 
2206 /* Returns 42. */
2207 asm(
2208 	".align 64\n"
2209 	"ret42_start:\n"
2210 	"mov $42, %eax\n"
2211 	"ret\n"
2212 	"ret42_end:\n"
2213 );
2214 
2215 static void
2216 diagnose_ept_violation_qual(u64 expected, u64 actual)
2217 {
2218 
2219 #define DIAGNOSE(flag)							\
2220 do {									\
2221 	if ((expected & flag) != (actual & flag))			\
2222 		printf(#flag " %sexpected\n",				\
2223 		       (expected & flag) ? "" : "un");			\
2224 } while (0)
2225 
2226 	DIAGNOSE(EPT_VLT_RD);
2227 	DIAGNOSE(EPT_VLT_WR);
2228 	DIAGNOSE(EPT_VLT_FETCH);
2229 	DIAGNOSE(EPT_VLT_PERM_RD);
2230 	DIAGNOSE(EPT_VLT_PERM_WR);
2231 	DIAGNOSE(EPT_VLT_PERM_EX);
2232 	DIAGNOSE(EPT_VLT_LADDR_VLD);
2233 	DIAGNOSE(EPT_VLT_PADDR);
2234 
2235 #undef DIAGNOSE
2236 }
2237 
2238 static void do_ept_access_op(enum ept_access_op op)
2239 {
2240 	ept_access_test_data.op = op;
2241 	enter_guest();
2242 }
2243 
2244 /*
2245  * Force the guest to flush its TLB (i.e., flush gva -> gpa mappings). Only
2246  * needed by tests that modify guest PTEs.
2247  */
2248 static void ept_access_test_guest_flush_tlb(void)
2249 {
2250 	do_ept_access_op(OP_FLUSH_TLB);
2251 	skip_exit_vmcall();
2252 }
2253 
2254 /*
2255  * Modifies the EPT entry at @level in the mapping of @gpa. First clears the
2256  * bits in @clear then sets the bits in @set. @mkhuge transforms the entry into
2257  * a huge page.
2258  */
2259 static unsigned long ept_twiddle(unsigned long gpa, bool mkhuge, int level,
2260 				 unsigned long clear, unsigned long set)
2261 {
2262 	struct ept_access_test_data *data = &ept_access_test_data;
2263 	unsigned long orig_pte;
2264 	unsigned long pte;
2265 
2266 	/* Screw with the mapping at the requested level. */
2267 	TEST_ASSERT(get_ept_pte(pml4, gpa, level, &orig_pte));
2268 	pte = orig_pte;
2269 	if (mkhuge)
2270 		pte = (orig_pte & ~EPT_ADDR_MASK) | data->hpa | EPT_LARGE_PAGE;
2271 	else
2272 		pte = orig_pte;
2273 	pte = (pte & ~clear) | set;
2274 	set_ept_pte(pml4, gpa, level, pte);
2275 	ept_sync(INVEPT_SINGLE, eptp);
2276 
2277 	return orig_pte;
2278 }
2279 
2280 static void ept_untwiddle(unsigned long gpa, int level, unsigned long orig_pte)
2281 {
2282 	set_ept_pte(pml4, gpa, level, orig_pte);
2283 }
2284 
2285 static void do_ept_violation(bool leaf, enum ept_access_op op,
2286 			     u64 expected_qual, u64 expected_paddr)
2287 {
2288 	u64 qual;
2289 
2290 	/* Try the access and observe the violation. */
2291 	do_ept_access_op(op);
2292 
2293 	assert_exit_reason(VMX_EPT_VIOLATION);
2294 
2295 	qual = vmcs_read(EXI_QUALIFICATION);
2296 
2297 	diagnose_ept_violation_qual(expected_qual, qual);
2298 	TEST_EXPECT_EQ(expected_qual, qual);
2299 
2300 	#if 0
2301 	/* Disable for now otherwise every test will fail */
2302 	TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS),
2303 		       (unsigned long) (
2304 			       op == OP_EXEC ? data->gva + 1 : data->gva));
2305 	#endif
2306 	/*
2307 	 * TODO: tests that probe expected_paddr in pages other than the one at
2308 	 * the beginning of the 1g region.
2309 	 */
2310 	TEST_EXPECT_EQ(vmcs_read(INFO_PHYS_ADDR), expected_paddr);
2311 }
2312 
2313 static void
2314 ept_violation_at_level_mkhuge(bool mkhuge, int level, unsigned long clear,
2315 			      unsigned long set, enum ept_access_op op,
2316 			      u64 expected_qual)
2317 {
2318 	struct ept_access_test_data *data = &ept_access_test_data;
2319 	unsigned long orig_pte;
2320 
2321 	orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set);
2322 
2323 	do_ept_violation(level == 1 || mkhuge, op, expected_qual,
2324 			 op == OP_EXEC ? data->gpa + sizeof(unsigned long) :
2325 					 data->gpa);
2326 
2327 	/* Fix the violation and resume the op loop. */
2328 	ept_untwiddle(data->gpa, level, orig_pte);
2329 	enter_guest();
2330 	skip_exit_vmcall();
2331 }
2332 
2333 static void
2334 ept_violation_at_level(int level, unsigned long clear, unsigned long set,
2335 		       enum ept_access_op op, u64 expected_qual)
2336 {
2337 	ept_violation_at_level_mkhuge(false, level, clear, set, op,
2338 				      expected_qual);
2339 	if (ept_huge_pages_supported(level))
2340 		ept_violation_at_level_mkhuge(true, level, clear, set, op,
2341 					      expected_qual);
2342 }
2343 
2344 static void ept_violation(unsigned long clear, unsigned long set,
2345 			  enum ept_access_op op, u64 expected_qual)
2346 {
2347 	ept_violation_at_level(1, clear, set, op, expected_qual);
2348 	ept_violation_at_level(2, clear, set, op, expected_qual);
2349 	ept_violation_at_level(3, clear, set, op, expected_qual);
2350 	ept_violation_at_level(4, clear, set, op, expected_qual);
2351 }
2352 
2353 static void ept_access_violation(unsigned long access, enum ept_access_op op,
2354 				       u64 expected_qual)
2355 {
2356 	ept_violation(EPT_PRESENT, access, op,
2357 		      expected_qual | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
2358 }
2359 
2360 /*
2361  * For translations that don't involve a GVA, that is physical address (paddr)
2362  * accesses, EPT violations don't set the flag EPT_VLT_PADDR.  For a typical
2363  * guest memory access, the hardware does GVA -> GPA -> HPA.  However, certain
2364  * translations don't involve GVAs, such as when the hardware does the guest
2365  * page table walk. For example, in translating GVA_1 -> GPA_1, the guest MMU
2366  * might try to set an A bit on a guest PTE. If the GPA_2 that the PTE resides
2367  * on isn't present in the EPT, then the EPT violation will be for GPA_2 and
2368  * the EPT_VLT_PADDR bit will be clear in the exit qualification.
2369  *
2370  * Note that paddr violations can also be triggered by loading PAE page tables
2371  * with wonky addresses. We don't test that yet.
2372  *
2373  * This function modifies the EPT entry that maps the GPA that the guest page
2374  * table entry mapping ept_access_data.gva resides on.
2375  *
2376  *	@ept_access	EPT permissions to set. Other permissions are cleared.
2377  *
2378  *	@pte_ad		Set the A/D bits on the guest PTE accordingly.
2379  *
2380  *	@op		Guest operation to perform with ept_access_data.gva.
2381  *
2382  *	@expect_violation
2383  *			Is a violation expected during the paddr access?
2384  *
2385  *	@expected_qual	Expected qualification for the EPT violation.
2386  *			EPT_VLT_PADDR should be clear.
2387  */
2388 static void ept_access_paddr(unsigned long ept_access, unsigned long pte_ad,
2389 			     enum ept_access_op op, bool expect_violation,
2390 			     u64 expected_qual)
2391 {
2392 	struct ept_access_test_data *data = &ept_access_test_data;
2393 	unsigned long *ptep;
2394 	unsigned long gpa;
2395 	unsigned long orig_epte;
2396 
2397 	/* Modify the guest PTE mapping data->gva according to @pte_ad.  */
2398 	ptep = get_pte_level(current_page_table(), data->gva, /*level=*/1);
2399 	TEST_ASSERT(ptep);
2400 	TEST_ASSERT_EQ(*ptep & PT_ADDR_MASK, data->gpa);
2401 	*ptep = (*ptep & ~PT_AD_MASK) | pte_ad;
2402 	ept_access_test_guest_flush_tlb();
2403 
2404 	/*
2405 	 * Now modify the access bits on the EPT entry for the GPA that the
2406 	 * guest PTE resides on. Note that by modifying a single EPT entry,
2407 	 * we're potentially affecting 512 guest PTEs. However, we've carefully
2408 	 * constructed our test such that those other 511 PTEs aren't used by
2409 	 * the guest: data->gva is at the beginning of a 1G huge page, thus the
2410 	 * PTE we're modifying is at the beginning of a 4K page and the
2411 	 * following 511 entires are also under our control (and not touched by
2412 	 * the guest).
2413 	 */
2414 	gpa = virt_to_phys(ptep);
2415 	TEST_ASSERT_EQ(gpa & ~PAGE_MASK, 0);
2416 	/*
2417 	 * Make sure the guest page table page is mapped with a 4K EPT entry,
2418 	 * otherwise our level=1 twiddling below will fail. We use the
2419 	 * identity map (gpa = gpa) since page tables are shared with the host.
2420 	 */
2421 	install_ept(pml4, gpa, gpa, EPT_PRESENT);
2422 	orig_epte = ept_twiddle(gpa, /*mkhuge=*/0, /*level=*/1,
2423 				/*clear=*/EPT_PRESENT, /*set=*/ept_access);
2424 
2425 	if (expect_violation) {
2426 		do_ept_violation(/*leaf=*/true, op,
2427 				 expected_qual | EPT_VLT_LADDR_VLD, gpa);
2428 		ept_untwiddle(gpa, /*level=*/1, orig_epte);
2429 		do_ept_access_op(op);
2430 	} else {
2431 		do_ept_access_op(op);
2432 		ept_untwiddle(gpa, /*level=*/1, orig_epte);
2433 	}
2434 
2435 	TEST_ASSERT(*ptep & PT_ACCESSED_MASK);
2436 	if ((pte_ad & PT_DIRTY_MASK) || op == OP_WRITE)
2437 		TEST_ASSERT(*ptep & PT_DIRTY_MASK);
2438 
2439 	skip_exit_vmcall();
2440 }
2441 
2442 static void ept_access_allowed_paddr(unsigned long ept_access,
2443 				     unsigned long pte_ad,
2444 				     enum ept_access_op op)
2445 {
2446 	ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/false,
2447 			 /*expected_qual=*/-1);
2448 }
2449 
2450 static void ept_access_violation_paddr(unsigned long ept_access,
2451 				       unsigned long pte_ad,
2452 				       enum ept_access_op op,
2453 				       u64 expected_qual)
2454 {
2455 	ept_access_paddr(ept_access, pte_ad, op, /*expect_violation=*/true,
2456 			 expected_qual);
2457 }
2458 
2459 
2460 static void ept_allowed_at_level_mkhuge(bool mkhuge, int level,
2461 					unsigned long clear,
2462 					unsigned long set,
2463 					enum ept_access_op op)
2464 {
2465 	struct ept_access_test_data *data = &ept_access_test_data;
2466 	unsigned long orig_pte;
2467 
2468 	orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set);
2469 
2470 	/* No violation. Should proceed to vmcall. */
2471 	do_ept_access_op(op);
2472 	skip_exit_vmcall();
2473 
2474 	ept_untwiddle(data->gpa, level, orig_pte);
2475 }
2476 
2477 static void ept_allowed_at_level(int level, unsigned long clear,
2478 				 unsigned long set, enum ept_access_op op)
2479 {
2480 	ept_allowed_at_level_mkhuge(false, level, clear, set, op);
2481 	if (ept_huge_pages_supported(level))
2482 		ept_allowed_at_level_mkhuge(true, level, clear, set, op);
2483 }
2484 
2485 static void ept_allowed(unsigned long clear, unsigned long set,
2486 			enum ept_access_op op)
2487 {
2488 	ept_allowed_at_level(1, clear, set, op);
2489 	ept_allowed_at_level(2, clear, set, op);
2490 	ept_allowed_at_level(3, clear, set, op);
2491 	ept_allowed_at_level(4, clear, set, op);
2492 }
2493 
2494 static void ept_ignored_bit(int bit)
2495 {
2496 	/* Set the bit. */
2497 	ept_allowed(0, 1ul << bit, OP_READ);
2498 	ept_allowed(0, 1ul << bit, OP_WRITE);
2499 	ept_allowed(0, 1ul << bit, OP_EXEC);
2500 
2501 	/* Clear the bit. */
2502 	ept_allowed(1ul << bit, 0, OP_READ);
2503 	ept_allowed(1ul << bit, 0, OP_WRITE);
2504 	ept_allowed(1ul << bit, 0, OP_EXEC);
2505 }
2506 
2507 static void ept_access_allowed(unsigned long access, enum ept_access_op op)
2508 {
2509 	ept_allowed(EPT_PRESENT, access, op);
2510 }
2511 
2512 
2513 static void ept_misconfig_at_level_mkhuge_op(bool mkhuge, int level,
2514 					     unsigned long clear,
2515 					     unsigned long set,
2516 					     enum ept_access_op op)
2517 {
2518 	struct ept_access_test_data *data = &ept_access_test_data;
2519 	unsigned long orig_pte;
2520 
2521 	orig_pte = ept_twiddle(data->gpa, mkhuge, level, clear, set);
2522 
2523 	do_ept_access_op(op);
2524 	assert_exit_reason(VMX_EPT_MISCONFIG);
2525 
2526 	/* Intel 27.2.1, "For all other VM exits, this field is cleared." */
2527 	#if 0
2528 	/* broken: */
2529 	TEST_EXPECT_EQ_MSG(vmcs_read(EXI_QUALIFICATION), 0);
2530 	#endif
2531 	#if 0
2532 	/*
2533 	 * broken:
2534 	 * According to description of exit qual for EPT violation,
2535 	 * EPT_VLT_LADDR_VLD indicates if GUEST_LINEAR_ADDRESS is valid.
2536 	 * However, I can't find anything that says GUEST_LINEAR_ADDRESS ought
2537 	 * to be set for msiconfig.
2538 	 */
2539 	TEST_EXPECT_EQ(vmcs_read(GUEST_LINEAR_ADDRESS),
2540 		       (unsigned long) (
2541 			       op == OP_EXEC ? data->gva + 1 : data->gva));
2542 	#endif
2543 
2544 	/* Fix the violation and resume the op loop. */
2545 	ept_untwiddle(data->gpa, level, orig_pte);
2546 	enter_guest();
2547 	skip_exit_vmcall();
2548 }
2549 
2550 static void ept_misconfig_at_level_mkhuge(bool mkhuge, int level,
2551 					  unsigned long clear,
2552 					  unsigned long set)
2553 {
2554 	/* The op shouldn't matter (read, write, exec), so try them all! */
2555 	ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_READ);
2556 	ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_WRITE);
2557 	ept_misconfig_at_level_mkhuge_op(mkhuge, level, clear, set, OP_EXEC);
2558 }
2559 
2560 static void ept_misconfig_at_level(int level, unsigned long clear,
2561 				   unsigned long set)
2562 {
2563 	ept_misconfig_at_level_mkhuge(false, level, clear, set);
2564 	if (ept_huge_pages_supported(level))
2565 		ept_misconfig_at_level_mkhuge(true, level, clear, set);
2566 }
2567 
2568 static void ept_misconfig(unsigned long clear, unsigned long set)
2569 {
2570 	ept_misconfig_at_level(1, clear, set);
2571 	ept_misconfig_at_level(2, clear, set);
2572 	ept_misconfig_at_level(3, clear, set);
2573 	ept_misconfig_at_level(4, clear, set);
2574 }
2575 
2576 static void ept_access_misconfig(unsigned long access)
2577 {
2578 	ept_misconfig(EPT_PRESENT, access);
2579 }
2580 
2581 static void ept_reserved_bit_at_level_nohuge(int level, int bit)
2582 {
2583 	/* Setting the bit causes a misconfig. */
2584 	ept_misconfig_at_level_mkhuge(false, level, 0, 1ul << bit);
2585 
2586 	/* Making the entry non-present turns reserved bits into ignored. */
2587 	ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ,
2588 			       EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
2589 }
2590 
2591 static void ept_reserved_bit_at_level_huge(int level, int bit)
2592 {
2593 	/* Setting the bit causes a misconfig. */
2594 	ept_misconfig_at_level_mkhuge(true, level, 0, 1ul << bit);
2595 
2596 	/* Making the entry non-present turns reserved bits into ignored. */
2597 	ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ,
2598 			       EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
2599 }
2600 
2601 static void ept_reserved_bit_at_level(int level, int bit)
2602 {
2603 	/* Setting the bit causes a misconfig. */
2604 	ept_misconfig_at_level(level, 0, 1ul << bit);
2605 
2606 	/* Making the entry non-present turns reserved bits into ignored. */
2607 	ept_violation_at_level(level, EPT_PRESENT, 1ul << bit, OP_READ,
2608 			       EPT_VLT_RD | EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
2609 }
2610 
2611 static void ept_reserved_bit(int bit)
2612 {
2613 	ept_reserved_bit_at_level(1, bit);
2614 	ept_reserved_bit_at_level(2, bit);
2615 	ept_reserved_bit_at_level(3, bit);
2616 	ept_reserved_bit_at_level(4, bit);
2617 }
2618 
2619 #define PAGE_2M_ORDER 9
2620 #define PAGE_1G_ORDER 18
2621 
2622 static void *get_1g_page(void)
2623 {
2624 	static void *alloc;
2625 
2626 	if (!alloc)
2627 		alloc = alloc_pages(PAGE_1G_ORDER);
2628 	return alloc;
2629 }
2630 
2631 static void ept_access_test_teardown(void *unused)
2632 {
2633 	/* Exit the guest cleanly. */
2634 	do_ept_access_op(OP_EXIT);
2635 }
2636 
2637 static void ept_access_test_guest(void)
2638 {
2639 	struct ept_access_test_data *data = &ept_access_test_data;
2640 	int (*code)(void) = (int (*)(void)) &data->gva[1];
2641 
2642 	while (true) {
2643 		switch (data->op) {
2644 		case OP_READ:
2645 			TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_1);
2646 			break;
2647 		case OP_WRITE:
2648 			*data->gva = MAGIC_VAL_2;
2649 			TEST_ASSERT_EQ(*data->gva, MAGIC_VAL_2);
2650 			*data->gva = MAGIC_VAL_1;
2651 			break;
2652 		case OP_EXEC:
2653 			TEST_ASSERT_EQ(42, code());
2654 			break;
2655 		case OP_FLUSH_TLB:
2656 			write_cr3(read_cr3());
2657 			break;
2658 		case OP_EXIT:
2659 			return;
2660 		default:
2661 			TEST_ASSERT_MSG(false, "Unknown op %d", data->op);
2662 		}
2663 		vmcall();
2664 	}
2665 }
2666 
2667 static void ept_access_test_setup(void)
2668 {
2669 	struct ept_access_test_data *data = &ept_access_test_data;
2670 	unsigned long npages = 1ul << PAGE_1G_ORDER;
2671 	unsigned long size = npages * PAGE_SIZE;
2672 	unsigned long *page_table = current_page_table();
2673 	unsigned long pte;
2674 
2675 	if (setup_ept(false))
2676 		test_skip("EPT not supported");
2677 
2678 	/* We use data->gpa = 1 << 39 so that test data has a separate pml4 entry */
2679 	if (cpuid_maxphyaddr() < 40)
2680 		test_skip("Test needs MAXPHYADDR >= 40");
2681 
2682 	test_set_guest(ept_access_test_guest);
2683 	test_add_teardown(ept_access_test_teardown, NULL);
2684 
2685 	data->hva = get_1g_page();
2686 	TEST_ASSERT(data->hva);
2687 	data->hpa = virt_to_phys(data->hva);
2688 
2689 	data->gpa = 1ul << 39;
2690 	data->gva = (void *) ALIGN((unsigned long) alloc_vpages(npages * 2),
2691 				   size);
2692 	TEST_ASSERT(!any_present_pages(page_table, data->gva, size));
2693 	install_pages(page_table, data->gpa, size, data->gva);
2694 
2695 	/*
2696 	 * Make sure nothing's mapped here so the tests that screw with the
2697 	 * pml4 entry don't inadvertently break something.
2698 	 */
2699 	TEST_ASSERT(get_ept_pte(pml4, data->gpa, 4, &pte) && pte == 0);
2700 	TEST_ASSERT(get_ept_pte(pml4, data->gpa + size - 1, 4, &pte) && pte == 0);
2701 	install_ept(pml4, data->hpa, data->gpa, EPT_PRESENT);
2702 
2703 	data->hva[0] = MAGIC_VAL_1;
2704 	memcpy(&data->hva[1], &ret42_start, &ret42_end - &ret42_start);
2705 }
2706 
2707 static void ept_access_test_not_present(void)
2708 {
2709 	ept_access_test_setup();
2710 	/* --- */
2711 	ept_access_violation(0, OP_READ, EPT_VLT_RD);
2712 	ept_access_violation(0, OP_WRITE, EPT_VLT_WR);
2713 	ept_access_violation(0, OP_EXEC, EPT_VLT_FETCH);
2714 }
2715 
2716 static void ept_access_test_read_only(void)
2717 {
2718 	ept_access_test_setup();
2719 
2720 	/* r-- */
2721 	ept_access_allowed(EPT_RA, OP_READ);
2722 	ept_access_violation(EPT_RA, OP_WRITE, EPT_VLT_WR | EPT_VLT_PERM_RD);
2723 	ept_access_violation(EPT_RA, OP_EXEC, EPT_VLT_FETCH | EPT_VLT_PERM_RD);
2724 }
2725 
2726 static void ept_access_test_write_only(void)
2727 {
2728 	ept_access_test_setup();
2729 	/* -w- */
2730 	ept_access_misconfig(EPT_WA);
2731 }
2732 
2733 static void ept_access_test_read_write(void)
2734 {
2735 	ept_access_test_setup();
2736 	/* rw- */
2737 	ept_access_allowed(EPT_RA | EPT_WA, OP_READ);
2738 	ept_access_allowed(EPT_RA | EPT_WA, OP_WRITE);
2739 	ept_access_violation(EPT_RA | EPT_WA, OP_EXEC,
2740 			   EPT_VLT_FETCH | EPT_VLT_PERM_RD | EPT_VLT_PERM_WR);
2741 }
2742 
2743 
2744 static void ept_access_test_execute_only(void)
2745 {
2746 	ept_access_test_setup();
2747 	/* --x */
2748 	if (ept_execute_only_supported()) {
2749 		ept_access_violation(EPT_EA, OP_READ,
2750 				     EPT_VLT_RD | EPT_VLT_PERM_EX);
2751 		ept_access_violation(EPT_EA, OP_WRITE,
2752 				     EPT_VLT_WR | EPT_VLT_PERM_EX);
2753 		ept_access_allowed(EPT_EA, OP_EXEC);
2754 	} else {
2755 		ept_access_misconfig(EPT_EA);
2756 	}
2757 }
2758 
2759 static void ept_access_test_read_execute(void)
2760 {
2761 	ept_access_test_setup();
2762 	/* r-x */
2763 	ept_access_allowed(EPT_RA | EPT_EA, OP_READ);
2764 	ept_access_violation(EPT_RA | EPT_EA, OP_WRITE,
2765 			   EPT_VLT_WR | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX);
2766 	ept_access_allowed(EPT_RA | EPT_EA, OP_EXEC);
2767 }
2768 
2769 static void ept_access_test_write_execute(void)
2770 {
2771 	ept_access_test_setup();
2772 	/* -wx */
2773 	ept_access_misconfig(EPT_WA | EPT_EA);
2774 }
2775 
2776 static void ept_access_test_read_write_execute(void)
2777 {
2778 	ept_access_test_setup();
2779 	/* rwx */
2780 	ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_READ);
2781 	ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_WRITE);
2782 	ept_access_allowed(EPT_RA | EPT_WA | EPT_EA, OP_EXEC);
2783 }
2784 
2785 static void ept_access_test_reserved_bits(void)
2786 {
2787 	int i;
2788 	int maxphyaddr;
2789 
2790 	ept_access_test_setup();
2791 
2792 	/* Reserved bits above maxphyaddr. */
2793 	maxphyaddr = cpuid_maxphyaddr();
2794 	for (i = maxphyaddr; i <= 51; i++) {
2795 		report_prefix_pushf("reserved_bit=%d", i);
2796 		ept_reserved_bit(i);
2797 		report_prefix_pop();
2798 	}
2799 
2800 	/* Level-specific reserved bits. */
2801 	ept_reserved_bit_at_level_nohuge(2, 3);
2802 	ept_reserved_bit_at_level_nohuge(2, 4);
2803 	ept_reserved_bit_at_level_nohuge(2, 5);
2804 	ept_reserved_bit_at_level_nohuge(2, 6);
2805 	/* 2M alignment. */
2806 	for (i = 12; i < 20; i++) {
2807 		report_prefix_pushf("reserved_bit=%d", i);
2808 		ept_reserved_bit_at_level_huge(2, i);
2809 		report_prefix_pop();
2810 	}
2811 	ept_reserved_bit_at_level_nohuge(3, 3);
2812 	ept_reserved_bit_at_level_nohuge(3, 4);
2813 	ept_reserved_bit_at_level_nohuge(3, 5);
2814 	ept_reserved_bit_at_level_nohuge(3, 6);
2815 	/* 1G alignment. */
2816 	for (i = 12; i < 29; i++) {
2817 		report_prefix_pushf("reserved_bit=%d", i);
2818 		ept_reserved_bit_at_level_huge(3, i);
2819 		report_prefix_pop();
2820 	}
2821 	ept_reserved_bit_at_level(4, 3);
2822 	ept_reserved_bit_at_level(4, 4);
2823 	ept_reserved_bit_at_level(4, 5);
2824 	ept_reserved_bit_at_level(4, 6);
2825 	ept_reserved_bit_at_level(4, 7);
2826 }
2827 
2828 static void ept_access_test_ignored_bits(void)
2829 {
2830 	ept_access_test_setup();
2831 	/*
2832 	 * Bits ignored at every level. Bits 8 and 9 (A and D) are ignored as
2833 	 * far as translation is concerned even if AD bits are enabled in the
2834 	 * EPTP. Bit 63 is ignored because "EPT-violation #VE" VM-execution
2835 	 * control is 0.
2836 	 */
2837 	ept_ignored_bit(8);
2838 	ept_ignored_bit(9);
2839 	ept_ignored_bit(10);
2840 	ept_ignored_bit(11);
2841 	ept_ignored_bit(52);
2842 	ept_ignored_bit(53);
2843 	ept_ignored_bit(54);
2844 	ept_ignored_bit(55);
2845 	ept_ignored_bit(56);
2846 	ept_ignored_bit(57);
2847 	ept_ignored_bit(58);
2848 	ept_ignored_bit(59);
2849 	ept_ignored_bit(60);
2850 	ept_ignored_bit(61);
2851 	ept_ignored_bit(62);
2852 	ept_ignored_bit(63);
2853 }
2854 
2855 static void ept_access_test_paddr_not_present_ad_disabled(void)
2856 {
2857 	ept_access_test_setup();
2858 	ept_disable_ad_bits();
2859 
2860 	ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, EPT_VLT_RD);
2861 	ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, EPT_VLT_RD);
2862 	ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, EPT_VLT_RD);
2863 }
2864 
2865 static void ept_access_test_paddr_not_present_ad_enabled(void)
2866 {
2867 	u64 qual = EPT_VLT_RD | EPT_VLT_WR;
2868 
2869 	ept_access_test_setup();
2870 	ept_enable_ad_bits_or_skip_test();
2871 
2872 	ept_access_violation_paddr(0, PT_AD_MASK, OP_READ, qual);
2873 	ept_access_violation_paddr(0, PT_AD_MASK, OP_WRITE, qual);
2874 	ept_access_violation_paddr(0, PT_AD_MASK, OP_EXEC, qual);
2875 }
2876 
2877 static void ept_access_test_paddr_read_only_ad_disabled(void)
2878 {
2879 	/*
2880 	 * When EPT AD bits are disabled, all accesses to guest paging
2881 	 * structures are reported separately as a read and (after
2882 	 * translation of the GPA to host physical address) a read+write
2883 	 * if the A/D bits have to be set.
2884 	 */
2885 	u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD;
2886 
2887 	ept_access_test_setup();
2888 	ept_disable_ad_bits();
2889 
2890 	/* Can't update A bit, so all accesses fail. */
2891 	ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual);
2892 	ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual);
2893 	ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual);
2894 	/* AD bits disabled, so only writes try to update the D bit. */
2895 	ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ);
2896 	ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual);
2897 	ept_access_allowed_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC);
2898 	/* Both A and D already set, so read-only is OK. */
2899 	ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_READ);
2900 	ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_WRITE);
2901 	ept_access_allowed_paddr(EPT_RA, PT_AD_MASK, OP_EXEC);
2902 }
2903 
2904 static void ept_access_test_paddr_read_only_ad_enabled(void)
2905 {
2906 	/*
2907 	 * When EPT AD bits are enabled, all accesses to guest paging
2908 	 * structures are considered writes as far as EPT translation
2909 	 * is concerned.
2910 	 */
2911 	u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD;
2912 
2913 	ept_access_test_setup();
2914 	ept_enable_ad_bits_or_skip_test();
2915 
2916 	ept_access_violation_paddr(EPT_RA, 0, OP_READ, qual);
2917 	ept_access_violation_paddr(EPT_RA, 0, OP_WRITE, qual);
2918 	ept_access_violation_paddr(EPT_RA, 0, OP_EXEC, qual);
2919 	ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_READ, qual);
2920 	ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_WRITE, qual);
2921 	ept_access_violation_paddr(EPT_RA, PT_ACCESSED_MASK, OP_EXEC, qual);
2922 	ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_READ, qual);
2923 	ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_WRITE, qual);
2924 	ept_access_violation_paddr(EPT_RA, PT_AD_MASK, OP_EXEC, qual);
2925 }
2926 
2927 static void ept_access_test_paddr_read_write(void)
2928 {
2929 	ept_access_test_setup();
2930 	/* Read-write access to paging structure. */
2931 	ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_READ);
2932 	ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_WRITE);
2933 	ept_access_allowed_paddr(EPT_RA | EPT_WA, 0, OP_EXEC);
2934 }
2935 
2936 static void ept_access_test_paddr_read_write_execute(void)
2937 {
2938 	ept_access_test_setup();
2939 	/* RWX access to paging structure. */
2940 	ept_access_allowed_paddr(EPT_PRESENT, 0, OP_READ);
2941 	ept_access_allowed_paddr(EPT_PRESENT, 0, OP_WRITE);
2942 	ept_access_allowed_paddr(EPT_PRESENT, 0, OP_EXEC);
2943 }
2944 
2945 static void ept_access_test_paddr_read_execute_ad_disabled(void)
2946 {
2947   	/*
2948 	 * When EPT AD bits are disabled, all accesses to guest paging
2949 	 * structures are reported separately as a read and (after
2950 	 * translation of the GPA to host physical address) a read+write
2951 	 * if the A/D bits have to be set.
2952 	 */
2953 	u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX;
2954 
2955 	ept_access_test_setup();
2956 	ept_disable_ad_bits();
2957 
2958 	/* Can't update A bit, so all accesses fail. */
2959 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual);
2960 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual);
2961 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual);
2962 	/* AD bits disabled, so only writes try to update the D bit. */
2963 	ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ);
2964 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual);
2965 	ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC);
2966 	/* Both A and D already set, so read-only is OK. */
2967 	ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ);
2968 	ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE);
2969 	ept_access_allowed_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC);
2970 }
2971 
2972 static void ept_access_test_paddr_read_execute_ad_enabled(void)
2973 {
2974 	/*
2975 	 * When EPT AD bits are enabled, all accesses to guest paging
2976 	 * structures are considered writes as far as EPT translation
2977 	 * is concerned.
2978 	 */
2979 	u64 qual = EPT_VLT_WR | EPT_VLT_RD | EPT_VLT_PERM_RD | EPT_VLT_PERM_EX;
2980 
2981 	ept_access_test_setup();
2982 	ept_enable_ad_bits_or_skip_test();
2983 
2984 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_READ, qual);
2985 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_WRITE, qual);
2986 	ept_access_violation_paddr(EPT_RA | EPT_EA, 0, OP_EXEC, qual);
2987 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_READ, qual);
2988 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_WRITE, qual);
2989 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_ACCESSED_MASK, OP_EXEC, qual);
2990 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_READ, qual);
2991 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_WRITE, qual);
2992 	ept_access_violation_paddr(EPT_RA | EPT_EA, PT_AD_MASK, OP_EXEC, qual);
2993 }
2994 
2995 static void ept_access_test_paddr_not_present_page_fault(void)
2996 {
2997 	ept_access_test_setup();
2998 	/*
2999 	 * TODO: test no EPT violation as long as guest PF occurs. e.g., GPA is
3000 	 * page is read-only in EPT but GVA is also mapped read only in PT.
3001 	 * Thus guest page fault before host takes EPT violation for trying to
3002 	 * update A bit.
3003 	 */
3004 }
3005 
3006 static void ept_access_test_force_2m_page(void)
3007 {
3008 	ept_access_test_setup();
3009 
3010 	TEST_ASSERT_EQ(ept_2m_supported(), true);
3011 	ept_allowed_at_level_mkhuge(true, 2, 0, 0, OP_READ);
3012 	ept_violation_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_RA, OP_WRITE,
3013 				      EPT_VLT_WR | EPT_VLT_PERM_RD |
3014 				      EPT_VLT_LADDR_VLD | EPT_VLT_PADDR);
3015 	ept_misconfig_at_level_mkhuge(true, 2, EPT_PRESENT, EPT_WA);
3016 }
3017 
3018 static bool invvpid_valid(u64 type, u64 vpid, u64 gla)
3019 {
3020 	u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
3021 
3022 	TEST_ASSERT(msr & VPID_CAP_INVVPID);
3023 
3024 	if (type < INVVPID_ADDR || type > INVVPID_CONTEXT_LOCAL)
3025 		return false;
3026 
3027 	if (!(msr & (1ull << (type + VPID_CAP_INVVPID_TYPES_SHIFT))))
3028 		return false;
3029 
3030 	if (vpid >> 16)
3031 		return false;
3032 
3033 	if (type != INVVPID_ALL && !vpid)
3034 		return false;
3035 
3036 	if (type == INVVPID_ADDR && !is_canonical(gla))
3037 		return false;
3038 
3039 	return true;
3040 }
3041 
3042 static void try_invvpid(u64 type, u64 vpid, u64 gla)
3043 {
3044 	int rc;
3045 	bool valid = invvpid_valid(type, vpid, gla);
3046 	u64 expected = valid ? VMXERR_UNSUPPORTED_VMCS_COMPONENT
3047 		: VMXERR_INVALID_OPERAND_TO_INVEPT_INVVPID;
3048 	/*
3049 	 * Set VMX_INST_ERROR to VMXERR_UNVALID_VMCS_COMPONENT, so
3050 	 * that we can tell if it is updated by INVVPID.
3051 	 */
3052 	vmcs_read(~0);
3053 	rc = invvpid(type, vpid, gla);
3054 	report("INVVPID type %ld VPID %lx GLA %lx %s",
3055 	       !rc == valid, type, vpid, gla,
3056 	       valid ? "passes" : "fails");
3057 	report("After %s INVVPID, VMX_INST_ERR is %ld (actual %ld)",
3058 	       vmcs_read(VMX_INST_ERROR) == expected,
3059 	       rc ? "failed" : "successful",
3060 	       expected, vmcs_read(VMX_INST_ERROR));
3061 }
3062 
3063 static void ds_invvpid(void *data)
3064 {
3065 	u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
3066 	u64 type = ffs(msr >> VPID_CAP_INVVPID_TYPES_SHIFT) - 1;
3067 
3068 	TEST_ASSERT(type >= INVVPID_ADDR && type <= INVVPID_CONTEXT_LOCAL);
3069 	asm volatile("invvpid %0, %1"
3070 		     :
3071 		     : "m"(*(struct invvpid_operand *)data),
3072 		       "r"(type));
3073 }
3074 
3075 /*
3076  * The SS override is ignored in 64-bit mode, so we use an addressing
3077  * mode with %rsp as the base register to generate an implicit SS
3078  * reference.
3079  */
3080 static void ss_invvpid(void *data)
3081 {
3082 	u64 msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
3083 	u64 type = ffs(msr >> VPID_CAP_INVVPID_TYPES_SHIFT) - 1;
3084 
3085 	TEST_ASSERT(type >= INVVPID_ADDR && type <= INVVPID_CONTEXT_LOCAL);
3086 	asm volatile("sub %%rsp,%0; invvpid (%%rsp,%0,1), %1"
3087 		     : "+r"(data)
3088 		     : "r"(type));
3089 }
3090 
3091 static void invvpid_test_gp(void)
3092 {
3093 	bool fault;
3094 
3095 	fault = test_for_exception(GP_VECTOR, &ds_invvpid,
3096 				   (void *)NONCANONICAL);
3097 	report("INVVPID with non-canonical DS operand raises #GP", fault);
3098 }
3099 
3100 static void invvpid_test_ss(void)
3101 {
3102 	bool fault;
3103 
3104 	fault = test_for_exception(SS_VECTOR, &ss_invvpid,
3105 				   (void *)NONCANONICAL);
3106 	report("INVVPID with non-canonical SS operand raises #SS", fault);
3107 }
3108 
3109 static void invvpid_test_pf(void)
3110 {
3111 	void *vpage = alloc_vpage();
3112 	bool fault;
3113 
3114 	fault = test_for_exception(PF_VECTOR, &ds_invvpid, vpage);
3115 	report("INVVPID with unmapped operand raises #PF", fault);
3116 }
3117 
3118 static void try_compat_invvpid(void *unused)
3119 {
3120 	struct far_pointer32 fp = {
3121 		.offset = (uintptr_t)&&invvpid,
3122 		.selector = KERNEL_CS32,
3123 	};
3124 	register uintptr_t rsp asm("rsp");
3125 
3126 	TEST_ASSERT_MSG(fp.offset == (uintptr_t)&&invvpid,
3127 			"Code address too high.");
3128 	TEST_ASSERT_MSG(rsp == (u32)rsp, "Stack address too high.");
3129 
3130 	asm goto ("lcall *%0" : : "m" (fp) : "rax" : invvpid);
3131 	return;
3132 invvpid:
3133 	asm volatile (".code32;"
3134 		      "invvpid (%eax), %eax;"
3135 		      "lret;"
3136 		      ".code64");
3137 	__builtin_unreachable();
3138 }
3139 
3140 static void invvpid_test_compatibility_mode(void)
3141 {
3142 	bool fault;
3143 
3144 	fault = test_for_exception(UD_VECTOR, &try_compat_invvpid, NULL);
3145 	report("Compatibility mode INVVPID raises #UD", fault);
3146 }
3147 
3148 static void invvpid_test_not_in_vmx_operation(void)
3149 {
3150 	bool fault;
3151 
3152 	TEST_ASSERT(!vmx_off());
3153 	fault = test_for_exception(UD_VECTOR, &ds_invvpid, NULL);
3154 	report("INVVPID outside of VMX operation raises #UD", fault);
3155 	TEST_ASSERT(!vmx_on());
3156 }
3157 
3158 /*
3159  * This does not test real-address mode, virtual-8086 mode, protected mode,
3160  * or CPL > 0.
3161  */
3162 static void invvpid_test_v2(void)
3163 {
3164 	u64 msr;
3165 	int i;
3166 	unsigned types = 0;
3167 	unsigned type;
3168 
3169 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY) ||
3170 	    !(ctrl_cpu_rev[1].clr & CPU_VPID))
3171 		test_skip("VPID not supported");
3172 
3173 	msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
3174 
3175 	if (!(msr & VPID_CAP_INVVPID))
3176 		test_skip("INVVPID not supported.\n");
3177 
3178 	if (msr & VPID_CAP_INVVPID_ADDR)
3179 		types |= 1u << INVVPID_ADDR;
3180 	if (msr & VPID_CAP_INVVPID_CXTGLB)
3181 		types |= 1u << INVVPID_CONTEXT_GLOBAL;
3182 	if (msr & VPID_CAP_INVVPID_ALL)
3183 		types |= 1u << INVVPID_ALL;
3184 	if (msr & VPID_CAP_INVVPID_CXTLOC)
3185 		types |= 1u << INVVPID_CONTEXT_LOCAL;
3186 
3187 	if (!types)
3188 		test_skip("No INVVPID types supported.\n");
3189 
3190 	for (i = -127; i < 128; i++)
3191 		try_invvpid(i, 0xffff, 0);
3192 
3193 	/*
3194 	 * VPID must not be more than 16 bits.
3195 	 */
3196 	for (i = 0; i < 64; i++)
3197 		for (type = 0; type < 4; type++)
3198 			if (types & (1u << type))
3199 				try_invvpid(type, 1ul << i, 0);
3200 
3201 	/*
3202 	 * VPID must not be zero, except for "all contexts."
3203 	 */
3204 	for (type = 0; type < 4; type++)
3205 		if (types & (1u << type))
3206 			try_invvpid(type, 0, 0);
3207 
3208 	/*
3209 	 * The gla operand is only validated for single-address INVVPID.
3210 	 */
3211 	if (types & (1u << INVVPID_ADDR))
3212 		try_invvpid(INVVPID_ADDR, 0xffff, NONCANONICAL);
3213 
3214 	invvpid_test_gp();
3215 	invvpid_test_ss();
3216 	invvpid_test_pf();
3217 	invvpid_test_compatibility_mode();
3218 	invvpid_test_not_in_vmx_operation();
3219 }
3220 
3221 /*
3222  * Test for early VMLAUNCH failure. Returns true if VMLAUNCH makes it
3223  * at least as far as the guest-state checks. Returns false if the
3224  * VMLAUNCH fails early and execution falls through to the next
3225  * instruction.
3226  */
3227 static bool vmlaunch_succeeds(void)
3228 {
3229 	/*
3230 	 * Indirectly set VMX_INST_ERR to 12 ("VMREAD/VMWRITE from/to
3231 	 * unsupported VMCS component"). The caller can then check
3232 	 * to see if a failed VM-entry sets VMX_INST_ERR as expected.
3233 	 */
3234 	vmcs_write(~0u, 0);
3235 
3236 	vmcs_write(HOST_RIP, (uintptr_t)&&success);
3237 	__asm__ __volatile__ goto ("vmwrite %%rsp, %0; vmlaunch"
3238 				   :
3239 				   : "r" ((u64)HOST_RSP)
3240 				   : "cc", "memory"
3241 				   : success);
3242 	return false;
3243 success:
3244 	TEST_ASSERT(vmcs_read(EXI_REASON) ==
3245 		    (VMX_FAIL_STATE | VMX_ENTRY_FAILURE));
3246 	return true;
3247 }
3248 
3249 /*
3250  * Try to launch the current VMCS.
3251  */
3252 static void test_vmx_controls(bool controls_valid, bool xfail)
3253 {
3254 	bool success = vmlaunch_succeeds();
3255 	u32 vmx_inst_err;
3256 
3257 	report_xfail("vmlaunch %s", xfail, success == controls_valid,
3258 		     controls_valid ? "succeeds" : "fails");
3259 	if (!success) {
3260 		vmx_inst_err = vmcs_read(VMX_INST_ERROR);
3261 		report("VMX inst error is %d (actual %d)",
3262 		       vmx_inst_err == VMXERR_ENTRY_INVALID_CONTROL_FIELD,
3263 		       VMXERR_ENTRY_INVALID_CONTROL_FIELD, vmx_inst_err);
3264 	}
3265 }
3266 
3267 /*
3268  * Test a particular value of a VM-execution control bit, if the value
3269  * is required or if the value is zero.
3270  */
3271 static void test_rsvd_ctl_bit_value(const char *name, union vmx_ctrl_msr msr,
3272 				    enum Encoding encoding, unsigned bit,
3273 				    unsigned val)
3274 {
3275 	u32 mask = 1u << bit;
3276 	bool expected;
3277 	u32 controls;
3278 
3279 	if (msr.set & mask)
3280 		TEST_ASSERT(msr.clr & mask);
3281 
3282 	/*
3283 	 * We can't arbitrarily turn on a control bit, because it may
3284 	 * introduce dependencies on other VMCS fields. So, we only
3285 	 * test turning on bits that have a required setting.
3286 	 */
3287 	if (val && (msr.clr & mask) && !(msr.set & mask))
3288 		return;
3289 
3290 	report_prefix_pushf("%s %s bit %d",
3291 			    val ? "Set" : "Clear", name, bit);
3292 
3293 	controls = vmcs_read(encoding);
3294 	if (val) {
3295 		vmcs_write(encoding, msr.set | mask);
3296 		expected = (msr.clr & mask);
3297 	} else {
3298 		vmcs_write(encoding, msr.set & ~mask);
3299 		expected = !(msr.set & mask);
3300 	}
3301 	test_vmx_controls(expected, false);
3302 	vmcs_write(encoding, controls);
3303 	report_prefix_pop();
3304 }
3305 
3306 /*
3307  * Test reserved values of a VM-execution control bit, based on the
3308  * allowed bit settings from the corresponding VMX capability MSR.
3309  */
3310 static void test_rsvd_ctl_bit(const char *name, union vmx_ctrl_msr msr,
3311 			      enum Encoding encoding, unsigned bit)
3312 {
3313 	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 0);
3314 	test_rsvd_ctl_bit_value(name, msr, encoding, bit, 1);
3315 }
3316 
3317 /*
3318  * Reserved bits in the pin-based VM-execution controls must be set
3319  * properly. Software may consult the VMX capability MSRs to determine
3320  * the proper settings.
3321  * [Intel SDM]
3322  */
3323 static void test_pin_based_ctls(void)
3324 {
3325 	unsigned bit;
3326 
3327 	printf("%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PIN" :
3328 	       "MSR_IA32_VMX_PINBASED_CTLS", ctrl_pin_rev.val);
3329 	for (bit = 0; bit < 32; bit++)
3330 		test_rsvd_ctl_bit("pin-based controls",
3331 				  ctrl_pin_rev, PIN_CONTROLS, bit);
3332 }
3333 
3334 /*
3335  * Reserved bits in the primary processor-based VM-execution controls
3336  * must be set properly. Software may consult the VMX capability MSRs
3337  * to determine the proper settings.
3338  * [Intel SDM]
3339  */
3340 static void test_primary_processor_based_ctls(void)
3341 {
3342 	unsigned bit;
3343 
3344 	printf("\n%s: %lx\n", basic.ctrl ? "MSR_IA32_VMX_TRUE_PROC" :
3345 	       "MSR_IA32_VMX_PROCBASED_CTLS", ctrl_cpu_rev[0].val);
3346 	for (bit = 0; bit < 32; bit++)
3347 		test_rsvd_ctl_bit("primary processor-based controls",
3348 				  ctrl_cpu_rev[0], CPU_EXEC_CTRL0, bit);
3349 }
3350 
3351 /*
3352  * If the "activate secondary controls" primary processor-based
3353  * VM-execution control is 1, reserved bits in the secondary
3354  * processor-based VM-execution controls must be cleared. Software may
3355  * consult the VMX capability MSRs to determine which bits are
3356  * reserved.
3357  * If the "activate secondary controls" primary processor-based
3358  * VM-execution control is 0 (or if the processor does not support the
3359  * 1-setting of that control), no checks are performed on the
3360  * secondary processor-based VM-execution controls.
3361  * [Intel SDM]
3362  */
3363 static void test_secondary_processor_based_ctls(void)
3364 {
3365 	u32 primary;
3366 	u32 secondary;
3367 	unsigned bit;
3368 
3369 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY))
3370 		return;
3371 
3372 	primary = vmcs_read(CPU_EXEC_CTRL0);
3373 	secondary = vmcs_read(CPU_EXEC_CTRL1);
3374 
3375 	vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY);
3376 	printf("\nMSR_IA32_VMX_PROCBASED_CTLS2: %lx\n", ctrl_cpu_rev[1].val);
3377 	for (bit = 0; bit < 32; bit++)
3378 		test_rsvd_ctl_bit("secondary processor-based controls",
3379 				  ctrl_cpu_rev[1], CPU_EXEC_CTRL1, bit);
3380 
3381 	/*
3382 	 * When the "activate secondary controls" VM-execution control
3383 	 * is clear, there are no checks on the secondary controls.
3384 	 */
3385 	vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY);
3386 	vmcs_write(CPU_EXEC_CTRL1, ~0);
3387 	report("Secondary processor-based controls ignored",
3388 	       vmlaunch_succeeds());
3389 	vmcs_write(CPU_EXEC_CTRL1, secondary);
3390 	vmcs_write(CPU_EXEC_CTRL0, primary);
3391 }
3392 
3393 static void try_cr3_target_count(unsigned i, unsigned max)
3394 {
3395 	report_prefix_pushf("CR3 target count 0x%x", i);
3396 	vmcs_write(CR3_TARGET_COUNT, i);
3397 	test_vmx_controls(i <= max, false);
3398 	report_prefix_pop();
3399 }
3400 
3401 /*
3402  * The CR3-target count must not be greater than 4. Future processors
3403  * may support a different number of CR3-target values. Software
3404  * should read the VMX capability MSR IA32_VMX_MISC to determine the
3405  * number of values supported.
3406  * [Intel SDM]
3407  */
3408 static void test_cr3_targets(void)
3409 {
3410 	unsigned supported_targets = (rdmsr(MSR_IA32_VMX_MISC) >> 16) & 0x1ff;
3411 	u32 cr3_targets = vmcs_read(CR3_TARGET_COUNT);
3412 	unsigned i;
3413 
3414 	printf("\nSupported CR3 targets: %d\n", supported_targets);
3415 	TEST_ASSERT(supported_targets <= 256);
3416 
3417 	try_cr3_target_count(-1u, supported_targets);
3418 	try_cr3_target_count(0x80000000, supported_targets);
3419 	try_cr3_target_count(0x7fffffff, supported_targets);
3420 	for (i = 0; i <= supported_targets + 1; i++)
3421 		try_cr3_target_count(i, supported_targets);
3422 	vmcs_write(CR3_TARGET_COUNT, cr3_targets);
3423 }
3424 
3425 /*
3426  * Test a particular address setting for a physical page reference in
3427  * the VMCS.
3428  */
3429 static void test_vmcs_page_addr(const char *name,
3430 				enum Encoding encoding,
3431 				bool ignored,
3432 				bool xfail_beyond_mapped_ram,
3433 				u64 addr)
3434 {
3435 	bool xfail =
3436 		(xfail_beyond_mapped_ram &&
3437 		 addr > fwcfg_get_u64(FW_CFG_RAM_SIZE) - PAGE_SIZE &&
3438 		 addr < (1ul << cpuid_maxphyaddr()));
3439 
3440 	report_prefix_pushf("%s = %lx", name, addr);
3441 	vmcs_write(encoding, addr);
3442 	test_vmx_controls(ignored || (IS_ALIGNED(addr, PAGE_SIZE) &&
3443 				  addr < (1ul << cpuid_maxphyaddr())),
3444 			  xfail);
3445 	report_prefix_pop();
3446 	xfail = false;
3447 }
3448 
3449 /*
3450  * Test interesting values for a physical page reference in the VMCS.
3451  */
3452 static void test_vmcs_page_values(const char *name,
3453 				  enum Encoding encoding,
3454 				  bool ignored,
3455 				  bool xfail_beyond_mapped_ram)
3456 {
3457 	unsigned i;
3458 	u64 orig_val = vmcs_read(encoding);
3459 
3460 	for (i = 0; i < 64; i++)
3461 		test_vmcs_page_addr(name, encoding, ignored,
3462 				    xfail_beyond_mapped_ram, 1ul << i);
3463 
3464 	test_vmcs_page_addr(name, encoding, ignored,
3465 			    xfail_beyond_mapped_ram, PAGE_SIZE - 1);
3466 	test_vmcs_page_addr(name, encoding, ignored,
3467 			    xfail_beyond_mapped_ram, PAGE_SIZE);
3468 	test_vmcs_page_addr(name, encoding, ignored,
3469 			    xfail_beyond_mapped_ram,
3470 			    (1ul << cpuid_maxphyaddr()) - PAGE_SIZE);
3471 	test_vmcs_page_addr(name, encoding, ignored,
3472 			    xfail_beyond_mapped_ram,
3473 			    -1ul);
3474 
3475 	vmcs_write(encoding, orig_val);
3476 }
3477 
3478 /*
3479  * Test a physical page reference in the VMCS, when the corresponding
3480  * feature is enabled and when the corresponding feature is disabled.
3481  */
3482 static void test_vmcs_page_reference(u32 control_bit, enum Encoding field,
3483 				     const char *field_name,
3484 				     const char *control_name,
3485 				     bool xfail_beyond_mapped_ram,
3486 				     bool control_primary)
3487 {
3488 	u32 primary = vmcs_read(CPU_EXEC_CTRL0);
3489 	u32 secondary = vmcs_read(CPU_EXEC_CTRL1);
3490 	u64 page_addr;
3491 
3492 	if (control_primary) {
3493 		if (!(ctrl_cpu_rev[0].clr & control_bit))
3494 			return;
3495 	} else {
3496 		if (!(ctrl_cpu_rev[1].clr & control_bit))
3497 			return;
3498 	}
3499 
3500 	page_addr = vmcs_read(field);
3501 
3502 	report_prefix_pushf("%s enabled", control_name);
3503 	if (control_primary) {
3504 		vmcs_write(CPU_EXEC_CTRL0, primary | control_bit);
3505 	} else {
3506 		vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY);
3507 		vmcs_write(CPU_EXEC_CTRL1, secondary | control_bit);
3508 	}
3509 	test_vmcs_page_values(field_name, field, false, xfail_beyond_mapped_ram);
3510 	report_prefix_pop();
3511 
3512 	report_prefix_pushf("%s disabled", control_name);
3513 	if (control_primary) {
3514 		vmcs_write(CPU_EXEC_CTRL0, primary & ~control_bit);
3515 	} else {
3516 		vmcs_write(CPU_EXEC_CTRL0, primary & ~CPU_SECONDARY);
3517 		vmcs_write(CPU_EXEC_CTRL1, secondary & ~control_bit);
3518 	}
3519 	test_vmcs_page_values(field_name, field, true, false);
3520 	report_prefix_pop();
3521 
3522 	vmcs_write(field, page_addr);
3523 	vmcs_write(CPU_EXEC_CTRL0, primary);
3524 }
3525 
3526 /*
3527  * If the "use I/O bitmaps" VM-execution control is 1, bits 11:0 of
3528  * each I/O-bitmap address must be 0. Neither address should set any
3529  * bits beyond the processor's physical-address width.
3530  * [Intel SDM]
3531  */
3532 static void test_io_bitmaps(void)
3533 {
3534 	test_vmcs_page_reference(CPU_IO_BITMAP, IO_BITMAP_A,
3535 				 "I/O bitmap A", "Use I/O bitmaps", false,
3536 				 true);
3537 	test_vmcs_page_reference(CPU_IO_BITMAP, IO_BITMAP_B,
3538 				 "I/O bitmap B", "Use I/O bitmaps", false,
3539 				 true);
3540 }
3541 
3542 /*
3543  * If the "use MSR bitmaps" VM-execution control is 1, bits 11:0 of
3544  * the MSR-bitmap address must be 0. The address should not set any
3545  * bits beyond the processor's physical-address width.
3546  * [Intel SDM]
3547  */
3548 static void test_msr_bitmap(void)
3549 {
3550 	test_vmcs_page_reference(CPU_MSR_BITMAP, MSR_BITMAP,
3551 				 "MSR bitmap", "Use MSR bitmaps", false,
3552 				 true);
3553 }
3554 
3555 /*
3556  * If the "use TPR shadow" VM-execution control is 1, the virtual-APIC
3557  * address must satisfy the following checks:
3558  * - Bits 11:0 of the address must be 0.
3559  * - The address should not set any bits beyond the processor's
3560  *   physical-address width.
3561  * [Intel SDM]
3562  */
3563 static void test_apic_virt_addr(void)
3564 {
3565 	test_vmcs_page_reference(CPU_TPR_SHADOW, APIC_VIRT_ADDR,
3566 				 "virtual-APIC address", "Use TPR shadow",
3567 				 true, true);
3568 }
3569 
3570 /*
3571  * If the "virtualize APIC-accesses" VM-execution control is 1, the
3572  * APIC-access address must satisfy the following checks:
3573  *  - Bits 11:0 of the address must be 0.
3574  *  - The address should not set any bits beyond the processor's
3575  *    physical-address width.
3576  * [Intel SDM]
3577  */
3578 static void test_apic_access_addr(void)
3579 {
3580 	void *apic_access_page = alloc_page();
3581 
3582 	vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_page));
3583 
3584 	test_vmcs_page_reference(CPU_VIRT_APIC_ACCESSES, APIC_ACCS_ADDR,
3585 				 "APIC-access address",
3586 				 "virtualize APIC-accesses", false, false);
3587 }
3588 
3589 static bool set_bit_pattern(u8 mask, u32 *secondary)
3590 {
3591 	u8 i;
3592 	bool flag = false;
3593 	u32 test_bits[3] = {
3594 		CPU_VIRT_X2APIC,
3595 		CPU_APIC_REG_VIRT,
3596 		CPU_VINTD
3597 	};
3598 
3599         for (i = 0; i < ARRAY_SIZE(test_bits); i++) {
3600 		if ((mask & (1u << i)) &&
3601 		    (ctrl_cpu_rev[1].clr & test_bits[i])) {
3602 			*secondary |= test_bits[i];
3603 			flag = true;
3604 		}
3605 	}
3606 
3607 	return (flag);
3608 }
3609 
3610 /*
3611  * If the "use TPR shadow" VM-execution control is 0, the following
3612  * VM-execution controls must also be 0:
3613  * 	- virtualize x2APIC mode
3614  *	- APIC-register virtualization
3615  *	- virtual-interrupt delivery
3616  *    [Intel SDM]
3617  *
3618  * 2. If the "virtualize x2APIC mode" VM-execution control is 1, the
3619  *    "virtualize APIC accesses" VM-execution control must be 0.
3620  *    [Intel SDM]
3621  */
3622 static void test_apic_virtual_ctls(void)
3623 {
3624 	u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0);
3625 	u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1);
3626 	u32 primary = saved_primary;
3627 	u32 secondary = saved_secondary;
3628 	bool ctrl = false;
3629 	char str[10] = "disabled";
3630 	u8 i = 0, j;
3631 
3632 	/*
3633 	 * First test
3634 	 */
3635 	if (!((ctrl_cpu_rev[0].clr & (CPU_SECONDARY | CPU_TPR_SHADOW)) ==
3636 	    (CPU_SECONDARY | CPU_TPR_SHADOW)))
3637 		return;
3638 
3639 	primary |= CPU_SECONDARY;
3640 	primary &= ~CPU_TPR_SHADOW;
3641 	vmcs_write(CPU_EXEC_CTRL0, primary);
3642 
3643 	while (1) {
3644 		for (j = 1; j < 8; j++) {
3645 			secondary &= ~(CPU_VIRT_X2APIC | CPU_APIC_REG_VIRT | CPU_VINTD);
3646 			if (primary & CPU_TPR_SHADOW) {
3647 				ctrl = true;
3648 			} else {
3649 				if (! set_bit_pattern(j, &secondary))
3650 					ctrl = true;
3651 				else
3652 					ctrl = false;
3653 			}
3654 
3655 			vmcs_write(CPU_EXEC_CTRL1, secondary);
3656 			report_prefix_pushf("Use TPR shadow %s, virtualize x2APIC mode %s, APIC-register virtualization %s, virtual-interrupt delivery %s",
3657 				str, (secondary & CPU_VIRT_X2APIC) ? "enabled" : "disabled", (secondary & CPU_APIC_REG_VIRT) ? "enabled" : "disabled", (secondary & CPU_VINTD) ? "enabled" : "disabled");
3658 			test_vmx_controls(ctrl, false);
3659 			report_prefix_pop();
3660 		}
3661 
3662 		if (i == 1)
3663 			break;
3664 		i++;
3665 
3666 		primary |= CPU_TPR_SHADOW;
3667 		vmcs_write(CPU_EXEC_CTRL0, primary);
3668 		strcpy(str, "enabled");
3669 	}
3670 
3671 	/*
3672 	 * Second test
3673 	 */
3674 	u32 apic_virt_ctls = (CPU_VIRT_X2APIC | CPU_VIRT_APIC_ACCESSES);
3675 
3676 	primary = saved_primary;
3677 	secondary = saved_secondary;
3678 	if (!((ctrl_cpu_rev[1].clr & apic_virt_ctls) == apic_virt_ctls))
3679 		return;
3680 
3681 	vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY);
3682 	secondary &= ~CPU_VIRT_APIC_ACCESSES;
3683 	vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_X2APIC);
3684 	report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access disabled");
3685 	test_vmx_controls(true, false);
3686 	report_prefix_pop();
3687 
3688 	vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_APIC_ACCESSES);
3689 	report_prefix_pushf("Virtualize x2APIC mode disabled; virtualize APIC access enabled");
3690 	test_vmx_controls(true, false);
3691 	report_prefix_pop();
3692 
3693 	vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VIRT_X2APIC);
3694 	report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access enabled");
3695 	test_vmx_controls(false, false);
3696 	report_prefix_pop();
3697 
3698 	vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VIRT_APIC_ACCESSES);
3699 	report_prefix_pushf("Virtualize x2APIC mode enabled; virtualize APIC access disabled");
3700 	test_vmx_controls(true, false);
3701 	report_prefix_pop();
3702 
3703 	vmcs_write(CPU_EXEC_CTRL0, saved_primary);
3704 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary);
3705 }
3706 
3707 /*
3708  * If the "virtual-interrupt delivery" VM-execution control is 1, the
3709  * "external-interrupt exiting" VM-execution control must be 1.
3710  * [Intel SDM]
3711  */
3712 static void test_virtual_intr_ctls(void)
3713 {
3714 	u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0);
3715 	u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1);
3716 	u32 saved_pin = vmcs_read(PIN_CONTROLS);
3717 	u32 primary = saved_primary;
3718 	u32 secondary = saved_secondary;
3719 	u32 pin = saved_pin;
3720 
3721 	if (!((ctrl_cpu_rev[1].clr & CPU_VINTD) &&
3722 	    (ctrl_pin_rev.clr & PIN_EXTINT)))
3723 		return;
3724 
3725 	vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW);
3726 	vmcs_write(CPU_EXEC_CTRL1, secondary & ~CPU_VINTD);
3727 	vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT);
3728 	report_prefix_pushf("Virtualize interrupt-delivery disabled; external-interrupt exiting disabled");
3729 	test_vmx_controls(true, false);
3730 	report_prefix_pop();
3731 
3732 	vmcs_write(CPU_EXEC_CTRL1, secondary | CPU_VINTD);
3733 	report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled");
3734 	test_vmx_controls(false, false);
3735 	report_prefix_pop();
3736 
3737 	vmcs_write(PIN_CONTROLS, pin | PIN_EXTINT);
3738 	report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting enabled");
3739 	test_vmx_controls(true, false);
3740 	report_prefix_pop();
3741 
3742 	vmcs_write(PIN_CONTROLS, pin & ~PIN_EXTINT);
3743 	report_prefix_pushf("Virtualize interrupt-delivery enabled; external-interrupt exiting disabled");
3744 	test_vmx_controls(false, false);
3745 	report_prefix_pop();
3746 
3747 	vmcs_write(CPU_EXEC_CTRL0, saved_primary);
3748 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary);
3749 	vmcs_write(PIN_CONTROLS, saved_pin);
3750 }
3751 
3752 static void test_pi_desc_addr(u64 addr, bool ctrl)
3753 {
3754 	vmcs_write(POSTED_INTR_DESC_ADDR, addr);
3755 	report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-descriptor-address 0x%lx", addr);
3756 	test_vmx_controls(ctrl, false);
3757 	report_prefix_pop();
3758 }
3759 
3760 /*
3761  * If the “process posted interrupts†VM-execution control is 1, the
3762  * following must be true:
3763  *
3764  *	- The “virtual-interrupt delivery†VM-execution control is 1.
3765  *	- The “acknowledge interrupt on exit†VM-exit control is 1.
3766  *	- The posted-interrupt notification vector has a value in the
3767  *	- range 0–255 (bits 15:8 are all 0).
3768  *	- Bits 5:0 of the posted-interrupt descriptor address are all 0.
3769  *	- The posted-interrupt descriptor address does not set any bits
3770  *	  beyond the processor's physical-address width.
3771  * [Intel SDM]
3772  */
3773 static void test_posted_intr(void)
3774 {
3775 	u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0);
3776 	u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1);
3777 	u32 saved_pin = vmcs_read(PIN_CONTROLS);
3778 	u32 exit_ctl_saved = vmcs_read(EXI_CONTROLS);
3779 	u32 primary = saved_primary;
3780 	u32 secondary = saved_secondary;
3781 	u32 pin = saved_pin;
3782 	u32 exit_ctl = exit_ctl_saved;
3783 	u16 vec;
3784 	int i;
3785 
3786 	if (!((ctrl_pin_rev.clr & PIN_POST_INTR) &&
3787 	    (ctrl_cpu_rev[1].clr & CPU_VINTD) &&
3788 	    (ctrl_exit_rev.clr & EXI_INTA)))
3789 		return;
3790 
3791 	vmcs_write(CPU_EXEC_CTRL0, primary | CPU_SECONDARY | CPU_TPR_SHADOW);
3792 
3793 	/*
3794 	 * Test virtual-interrupt-delivery and acknowledge-interrupt-on-exit
3795 	 */
3796 	pin |= PIN_POST_INTR;
3797 	vmcs_write(PIN_CONTROLS, pin);
3798 	secondary &= ~CPU_VINTD;
3799 	vmcs_write(CPU_EXEC_CTRL1, secondary);
3800 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled");
3801 	test_vmx_controls(false, false);
3802 	report_prefix_pop();
3803 
3804 	secondary |= CPU_VINTD;
3805 	vmcs_write(CPU_EXEC_CTRL1, secondary);
3806 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled");
3807 	test_vmx_controls(false, false);
3808 	report_prefix_pop();
3809 
3810 	exit_ctl &= ~EXI_INTA;
3811 	vmcs_write(EXI_CONTROLS, exit_ctl);
3812 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit disabled");
3813 	test_vmx_controls(false, false);
3814 	report_prefix_pop();
3815 
3816 	exit_ctl |= EXI_INTA;
3817 	vmcs_write(EXI_CONTROLS, exit_ctl);
3818 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled");
3819 	test_vmx_controls(true, false);
3820 	report_prefix_pop();
3821 
3822 	secondary &= ~CPU_VINTD;
3823 	vmcs_write(CPU_EXEC_CTRL1, secondary);
3824 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery disabled; acknowledge-interrupt-on-exit enabled");
3825 	test_vmx_controls(false, false);
3826 	report_prefix_pop();
3827 
3828 	secondary |= CPU_VINTD;
3829 	vmcs_write(CPU_EXEC_CTRL1, secondary);
3830 	report_prefix_pushf("Process-posted-interrupts enabled; virtual-interrupt-delivery enabled; acknowledge-interrupt-on-exit enabled");
3831 	test_vmx_controls(true, false);
3832 	report_prefix_pop();
3833 
3834 	/*
3835 	 * Test posted-interrupt notification vector
3836 	 */
3837 	for (i = 0; i < 8; i++) {
3838 		vec = (1ul << i);
3839 		vmcs_write(PINV, vec);
3840 		report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec);
3841 		test_vmx_controls(true, false);
3842 		report_prefix_pop();
3843 	}
3844 	for (i = 8; i < 16; i++) {
3845 		vec = (1ul << i);
3846 		vmcs_write(PINV, vec);
3847 		report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec);
3848 		test_vmx_controls(false, false);
3849 		report_prefix_pop();
3850 	}
3851 
3852 	vec &= ~(0xff << 8);
3853 	vmcs_write(PINV, vec);
3854 	report_prefix_pushf("Process-posted-interrupts enabled; posted-interrupt-notification-vector %u", vec);
3855 	test_vmx_controls(true, false);
3856 	report_prefix_pop();
3857 
3858 	/*
3859 	 * Test posted-interrupt descriptor addresss
3860 	 */
3861 	for (i = 0; i < 6; i++) {
3862 		test_pi_desc_addr(1ul << i, false);
3863 	}
3864 
3865 	test_pi_desc_addr(0xf0, false);
3866 	test_pi_desc_addr(0xff, false);
3867 	test_pi_desc_addr(0x0f, false);
3868 	test_pi_desc_addr(0x8000, true);
3869 	test_pi_desc_addr(0x00, true);
3870 	test_pi_desc_addr(0xc000, true);
3871 
3872 	test_vmcs_page_values("process-posted interrupts", POSTED_INTR_DESC_ADDR, false, false);
3873 
3874 	vmcs_write(CPU_EXEC_CTRL0, saved_primary);
3875 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary);
3876 	vmcs_write(PIN_CONTROLS, saved_pin);
3877 }
3878 
3879 static void test_apic_ctls(void)
3880 {
3881 	test_apic_virt_addr();
3882 	test_apic_access_addr();
3883 	test_apic_virtual_ctls();
3884 	test_virtual_intr_ctls();
3885 	test_posted_intr();
3886 }
3887 
3888 /*
3889  * If the “enable VPID†VM-execution control is 1, the value of the
3890  * of the VPID VM-execution control field must not be 0000H.
3891  * [Intel SDM]
3892  */
3893 static void test_vpid(void)
3894 {
3895 	u32 saved_primary = vmcs_read(CPU_EXEC_CTRL0);
3896 	u32 saved_secondary = vmcs_read(CPU_EXEC_CTRL1);
3897 	u16 vpid = 0x0000;
3898 	int i;
3899 
3900 	if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) &&
3901 	    (ctrl_cpu_rev[1].clr & CPU_VPID))) {
3902 		test_skip("Secondary controls and/or VPID not supported");
3903 		return;
3904 	}
3905 
3906 	vmcs_write(CPU_EXEC_CTRL0, saved_primary | CPU_SECONDARY);
3907 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary & ~CPU_VPID);
3908 	vmcs_write(VPID, vpid);
3909 	report_prefix_pushf("VPID disabled; VPID value %x", vpid);
3910 	test_vmx_controls(true, false);
3911 	report_prefix_pop();
3912 
3913 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary | CPU_VPID);
3914 	report_prefix_pushf("VPID enabled; VPID value %x", vpid);
3915 	test_vmx_controls(false, false);
3916 	report_prefix_pop();
3917 
3918 	for (i = 0; i < 16; i++) {
3919 		vpid = (short)1 << i;;
3920 		vmcs_write(VPID, vpid);
3921 		report_prefix_pushf("VPID enabled; VPID value %x", vpid);
3922 		test_vmx_controls(true, false);
3923 		report_prefix_pop();
3924 	}
3925 
3926 	vmcs_write(CPU_EXEC_CTRL0, saved_primary);
3927 	vmcs_write(CPU_EXEC_CTRL1, saved_secondary);
3928 }
3929 
3930 static void set_vtpr(unsigned vtpr)
3931 {
3932 	*(u32 *)phys_to_virt(vmcs_read(APIC_VIRT_ADDR) + APIC_TASKPRI) = vtpr;
3933 }
3934 
3935 static void try_tpr_threshold_and_vtpr(unsigned threshold, unsigned vtpr)
3936 {
3937 	bool valid = true;
3938 	u32 primary = vmcs_read(CPU_EXEC_CTRL0);
3939 	u32 secondary = vmcs_read(CPU_EXEC_CTRL1);
3940 
3941 	if ((primary & CPU_TPR_SHADOW) &&
3942 	    (!(primary & CPU_SECONDARY) ||
3943 	     !(secondary & (CPU_VINTD | CPU_VIRT_APIC_ACCESSES))))
3944 		valid = (threshold & 0xf) <= ((vtpr >> 4) & 0xf);
3945 
3946 	set_vtpr(vtpr);
3947 	report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0x%x",
3948 	    threshold, (vtpr >> 4) & 0xf);
3949 	test_vmx_controls(valid, false);
3950 	report_prefix_pop();
3951 }
3952 
3953 static void test_invalid_event_injection(void)
3954 {
3955 	u32 ent_intr_info_save = vmcs_read(ENT_INTR_INFO);
3956 	u32 ent_intr_error_save = vmcs_read(ENT_INTR_ERROR);
3957 	u32 ent_inst_len_save = vmcs_read(ENT_INST_LEN);
3958 	u32 primary_save = vmcs_read(CPU_EXEC_CTRL0);
3959 	u32 secondary_save = vmcs_read(CPU_EXEC_CTRL1);
3960 	u64 guest_cr0_save = vmcs_read(GUEST_CR0);
3961 	u32 ent_intr_info_base = INTR_INFO_VALID_MASK;
3962 	u32 ent_intr_info, ent_intr_err, ent_intr_len;
3963 	u32 cnt;
3964 
3965 	/* Setup */
3966 	report_prefix_push("invalid event injection");
3967 	vmcs_write(ENT_INTR_ERROR, 0x00000000);
3968 	vmcs_write(ENT_INST_LEN, 0x00000001);
3969 
3970 	/* The field’s interruption type is not set to a reserved value. */
3971 	ent_intr_info = ent_intr_info_base | INTR_TYPE_RESERVED | DE_VECTOR;
3972 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
3973 			    "RESERVED interruption type invalid [-]",
3974 			    ent_intr_info);
3975 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
3976 	test_vmx_controls(false, false);
3977 	report_prefix_pop();
3978 
3979 	ent_intr_info = ent_intr_info_base | INTR_TYPE_EXT_INTR |
3980 			DE_VECTOR;
3981 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
3982 			    "RESERVED interruption type invalid [+]",
3983 			    ent_intr_info);
3984 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
3985 	test_vmx_controls(true, false);
3986 	report_prefix_pop();
3987 
3988 	/* If the interruption type is other event, the vector is 0. */
3989 	ent_intr_info = ent_intr_info_base | INTR_TYPE_OTHER_EVENT | DB_VECTOR;
3990 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
3991 			    "(OTHER EVENT && vector != 0) invalid [-]",
3992 			    ent_intr_info);
3993 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
3994 	test_vmx_controls(false, false);
3995 	report_prefix_pop();
3996 
3997 	/* If the interruption type is NMI, the vector is 2 (negative case). */
3998 	ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | DE_VECTOR;
3999 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4000 			    "(NMI && vector != 2) invalid [-]", ent_intr_info);
4001 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4002 	test_vmx_controls(false, false);
4003 	report_prefix_pop();
4004 
4005 	/* If the interruption type is NMI, the vector is 2 (positive case). */
4006 	ent_intr_info = ent_intr_info_base | INTR_TYPE_NMI_INTR | NMI_VECTOR;
4007 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4008 			    "(NMI && vector == 2) valid [+]", ent_intr_info);
4009 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4010 	test_vmx_controls(true, false);
4011 	report_prefix_pop();
4012 
4013 	/*
4014 	 * If the interruption type
4015 	 * is HW exception, the vector is at most 31.
4016 	 */
4017 	ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION | 0x20;
4018 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4019 			    "(HW exception && vector > 31) invalid [-]",
4020 			    ent_intr_info);
4021 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4022 	test_vmx_controls(false, false);
4023 	report_prefix_pop();
4024 
4025 	/*
4026 	 * deliver-error-code is 1 iff either
4027 	 * (a) the "unrestricted guest" VM-execution control is 0
4028 	 * (b) CR0.PE is set.
4029 	 */
4030 	ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION |
4031 			GP_VECTOR;
4032 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4033 			    "error code <-> (!URG || prot_mode) [-]",
4034 			    ent_intr_info);
4035 	disable_unrestricted_guest();
4036 	vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG);
4037 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4038 	test_vmx_controls(false, false);
4039 	report_prefix_pop();
4040 
4041 	ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK |
4042 			INTR_TYPE_HARD_EXCEPTION | GP_VECTOR;
4043 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4044 			    "error code <-> (!URG || prot_mode) [+]",
4045 			    ent_intr_info);
4046 	disable_unrestricted_guest();
4047 	vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG);
4048 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4049 	test_vmx_controls(true, false);
4050 	report_prefix_pop();
4051 
4052 	ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK |
4053 			INTR_TYPE_HARD_EXCEPTION | GP_VECTOR;
4054 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4055 			    "error code <-> (!URG || prot_mode) [-]",
4056 			    ent_intr_info);
4057 	enable_unrestricted_guest();
4058 	vmcs_write(GUEST_CR0, guest_cr0_save & ~X86_CR0_PE & ~X86_CR0_PG);
4059 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4060 	test_vmx_controls(false, false);
4061 	report_prefix_pop();
4062 
4063 	ent_intr_info = ent_intr_info_base | INTR_TYPE_HARD_EXCEPTION |
4064 			GP_VECTOR;
4065 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4066 			    "error code <-> (!URG || prot_mode) [-]",
4067 			    ent_intr_info);
4068 	vmcs_write(GUEST_CR0, guest_cr0_save | X86_CR0_PE);
4069 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4070 	test_vmx_controls(false, false);
4071 	report_prefix_pop();
4072 
4073 	/* deliver-error-code is 1 iff the interruption type is HW exception */
4074 	report_prefix_push("error code <-> HW exception");
4075 	for (cnt = 0; cnt < 8; cnt++) {
4076 		u32 exception_type_mask = cnt << 8;
4077 		u32 deliver_error_code_mask =
4078 			exception_type_mask != INTR_TYPE_HARD_EXCEPTION ?
4079 			INTR_INFO_DELIVER_CODE_MASK : 0;
4080 
4081 		ent_intr_info = ent_intr_info_base | deliver_error_code_mask |
4082 				exception_type_mask | GP_VECTOR;
4083 		report_prefix_pushf("VM-entry intr info=0x%x [-]",
4084 				    ent_intr_info);
4085 		vmcs_write(ENT_INTR_INFO, ent_intr_info);
4086 		test_vmx_controls(false, false);
4087 		report_prefix_pop();
4088 	}
4089 	report_prefix_pop();
4090 
4091 	/*
4092 	 * deliver-error-code is 1 iff the the vector
4093 	 * indicates an exception that would normally deliver an error code
4094 	 */
4095 	report_prefix_push("error code <-> vector delivers error code");
4096 	for (cnt = 0; cnt < 32; cnt++) {
4097 		bool has_error_code = false;
4098 		u32 deliver_error_code_mask;
4099 
4100 		switch (cnt) {
4101 		case DF_VECTOR:
4102 		case TS_VECTOR:
4103 		case NP_VECTOR:
4104 		case SS_VECTOR:
4105 		case GP_VECTOR:
4106 		case PF_VECTOR:
4107 		case AC_VECTOR:
4108 			has_error_code = true;
4109 		}
4110 
4111 		/* Negative case */
4112 		deliver_error_code_mask = has_error_code ?
4113 						0 :
4114 						INTR_INFO_DELIVER_CODE_MASK;
4115 		ent_intr_info = ent_intr_info_base | deliver_error_code_mask |
4116 				INTR_TYPE_HARD_EXCEPTION | cnt;
4117 		report_prefix_pushf("VM-entry intr info=0x%x [-]",
4118 				    ent_intr_info);
4119 		vmcs_write(ENT_INTR_INFO, ent_intr_info);
4120 		test_vmx_controls(false, false);
4121 		report_prefix_pop();
4122 
4123 		/* Positive case */
4124 		deliver_error_code_mask = has_error_code ?
4125 						INTR_INFO_DELIVER_CODE_MASK :
4126 						0;
4127 		ent_intr_info = ent_intr_info_base | deliver_error_code_mask |
4128 				INTR_TYPE_HARD_EXCEPTION | cnt;
4129 		report_prefix_pushf("VM-entry intr info=0x%x [+]",
4130 				    ent_intr_info);
4131 		vmcs_write(ENT_INTR_INFO, ent_intr_info);
4132 		test_vmx_controls(true, false);
4133 		report_prefix_pop();
4134 	}
4135 	report_prefix_pop();
4136 
4137 	/* Reserved bits in the field (30:12) are 0. */
4138 	report_prefix_push("reserved bits clear");
4139 	for (cnt = 12; cnt <= 30; cnt++) {
4140 		ent_intr_info = ent_intr_info_base |
4141 				INTR_INFO_DELIVER_CODE_MASK |
4142 				INTR_TYPE_HARD_EXCEPTION | GP_VECTOR |
4143 				(1U << cnt);
4144 		report_prefix_pushf("VM-entry intr info=0x%x [-]",
4145 				    ent_intr_info);
4146 		vmcs_write(ENT_INTR_INFO, ent_intr_info);
4147 		test_vmx_controls(false, false);
4148 		report_prefix_pop();
4149 	}
4150 	report_prefix_pop();
4151 
4152 	/*
4153 	 * If deliver-error-code is 1
4154 	 * bits 31:15 of the VM-entry exception error-code field are 0.
4155 	 */
4156 	ent_intr_info = ent_intr_info_base | INTR_INFO_DELIVER_CODE_MASK |
4157 			INTR_TYPE_HARD_EXCEPTION | GP_VECTOR;
4158 	report_prefix_pushf("%s, VM-entry intr info=0x%x",
4159 			    "VM-entry exception error code[31:15] clear",
4160 			    ent_intr_info);
4161 	vmcs_write(ENT_INTR_INFO, ent_intr_info);
4162 	for (cnt = 15; cnt <= 31; cnt++) {
4163 		ent_intr_err = 1U << cnt;
4164 		report_prefix_pushf("VM-entry intr error=0x%x [-]",
4165 				    ent_intr_err);
4166 		vmcs_write(ENT_INTR_ERROR, ent_intr_err);
4167 		test_vmx_controls(false, false);
4168 		report_prefix_pop();
4169 	}
4170 	vmcs_write(ENT_INTR_ERROR, 0x00000000);
4171 	report_prefix_pop();
4172 
4173 	/*
4174 	 * If the interruption type is software interrupt, software exception,
4175 	 * or privileged software exception, the VM-entry instruction-length
4176 	 * field is in the range 0–15.
4177 	 */
4178 
4179 	for (cnt = 0; cnt < 3; cnt++) {
4180 		switch (cnt) {
4181 		case 0:
4182 			ent_intr_info = ent_intr_info_base |
4183 					INTR_TYPE_SOFT_INTR;
4184 			break;
4185 		case 1:
4186 			ent_intr_info = ent_intr_info_base |
4187 					INTR_TYPE_SOFT_EXCEPTION;
4188 			break;
4189 		case 2:
4190 			ent_intr_info = ent_intr_info_base |
4191 					INTR_TYPE_PRIV_SW_EXCEPTION;
4192 			break;
4193 		}
4194 		report_prefix_pushf("%s, VM-entry intr info=0x%x",
4195 				    "VM-entry instruction-length check",
4196 				    ent_intr_info);
4197 		vmcs_write(ENT_INTR_INFO, ent_intr_info);
4198 
4199 		/* Instruction length set to -1 (0xFFFFFFFF) should fail */
4200 		ent_intr_len = -1;
4201 		report_prefix_pushf("VM-entry intr length = 0x%x [-]",
4202 				    ent_intr_len);
4203 		vmcs_write(ENT_INST_LEN, ent_intr_len);
4204 		test_vmx_controls(false, false);
4205 		report_prefix_pop();
4206 
4207 		/* Instruction length set to 16 should fail */
4208 		ent_intr_len = 0x00000010;
4209 		report_prefix_pushf("VM-entry intr length = 0x%x [-]",
4210 				    ent_intr_len);
4211 		vmcs_write(ENT_INST_LEN, 0x00000010);
4212 		test_vmx_controls(false, false);
4213 		report_prefix_pop();
4214 
4215 		report_prefix_pop();
4216 	}
4217 
4218 	/* Cleanup */
4219 	vmcs_write(ENT_INTR_INFO, ent_intr_info_save);
4220 	vmcs_write(ENT_INTR_ERROR, ent_intr_error_save);
4221 	vmcs_write(ENT_INST_LEN, ent_inst_len_save);
4222 	vmcs_write(CPU_EXEC_CTRL0, primary_save);
4223 	vmcs_write(CPU_EXEC_CTRL1, secondary_save);
4224 	vmcs_write(GUEST_CR0, guest_cr0_save);
4225 	report_prefix_pop();
4226 }
4227 
4228 /*
4229  * Test interesting vTPR values for a given TPR threshold.
4230  */
4231 static void test_vtpr_values(unsigned threshold)
4232 {
4233 	try_tpr_threshold_and_vtpr(threshold, (threshold - 1) << 4);
4234 	try_tpr_threshold_and_vtpr(threshold, threshold << 4);
4235 	try_tpr_threshold_and_vtpr(threshold, (threshold + 1) << 4);
4236 }
4237 
4238 static void try_tpr_threshold(unsigned threshold)
4239 {
4240 	bool valid = true;
4241 
4242 	u32 primary = vmcs_read(CPU_EXEC_CTRL0);
4243 	u32 secondary = vmcs_read(CPU_EXEC_CTRL1);
4244 
4245 	if ((primary & CPU_TPR_SHADOW) && !((primary & CPU_SECONDARY) &&
4246 	    (secondary & CPU_VINTD)))
4247 		valid = !(threshold >> 4);
4248 
4249 	set_vtpr(-1);
4250 	vmcs_write(TPR_THRESHOLD, threshold);
4251 	report_prefix_pushf("TPR threshold 0x%x, VTPR.class 0xf", threshold);
4252 	test_vmx_controls(valid, false);
4253 	report_prefix_pop();
4254 
4255 	if (valid)
4256 		test_vtpr_values(threshold);
4257 }
4258 
4259 /*
4260  * Test interesting TPR threshold values.
4261  */
4262 static void test_tpr_threshold_values(void)
4263 {
4264 	unsigned i;
4265 
4266 	for (i = 0; i < 0x10; i++)
4267 		try_tpr_threshold(i);
4268 	for (i = 4; i < 32; i++)
4269 		try_tpr_threshold(1u << i);
4270 	try_tpr_threshold(-1u);
4271 	try_tpr_threshold(0x7fffffff);
4272 }
4273 
4274 /*
4275  * This test covers the following two VM entry checks:
4276  *
4277  *      i) If the "use TPR shadow" VM-execution control is 1 and the
4278  *         "virtual-interrupt delivery" VM-execution control is 0, bits
4279  *         31:4 of the TPR threshold VM-execution control field must
4280 	   be 0.
4281  *         [Intel SDM]
4282  *
4283  *      ii) If the "use TPR shadow" VM-execution control is 1, the
4284  *          "virtual-interrupt delivery" VM-execution control is 0
4285  *          and the "virtualize APIC accesses" VM-execution control
4286  *          is 0, the value of bits 3:0 of the TPR threshold VM-execution
4287  *          control field must not be greater than the value of bits
4288  *          7:4 of VTPR.
4289  *          [Intel SDM]
4290  */
4291 static void test_tpr_threshold(void)
4292 {
4293 	u32 primary = vmcs_read(CPU_EXEC_CTRL0);
4294 	void *virtual_apic_page;
4295 
4296 	if (!(ctrl_cpu_rev[0].clr & CPU_TPR_SHADOW))
4297 		return;
4298 
4299 	virtual_apic_page = alloc_page();
4300 	memset(virtual_apic_page, 0xff, PAGE_SIZE);
4301 	vmcs_write(APIC_VIRT_ADDR, virt_to_phys(virtual_apic_page));
4302 
4303 	vmcs_write(CPU_EXEC_CTRL0, primary & ~(CPU_TPR_SHADOW | CPU_SECONDARY));
4304 	report_prefix_pushf("Use TPR shadow disabled, secondary controls disabled");
4305 	test_tpr_threshold_values();
4306 	report_prefix_pop();
4307 	vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | CPU_TPR_SHADOW);
4308 	report_prefix_pushf("Use TPR shadow enabled, secondary controls disabled");
4309 	test_tpr_threshold_values();
4310 	report_prefix_pop();
4311 
4312 	if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) &&
4313 	    (ctrl_cpu_rev[1].clr & (CPU_VINTD  | CPU_VIRT_APIC_ACCESSES)))) {
4314 		vmcs_write(CPU_EXEC_CTRL0, primary);
4315 		return;
4316 	}
4317 
4318 	u32 secondary = vmcs_read(CPU_EXEC_CTRL1);
4319 
4320 	if (ctrl_cpu_rev[1].clr & CPU_VINTD) {
4321 		vmcs_write(CPU_EXEC_CTRL1, CPU_VINTD);
4322 		report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled");
4323 		test_tpr_threshold_values();
4324 		report_prefix_pop();
4325 
4326 		vmcs_write(CPU_EXEC_CTRL0,
4327 			   vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY);
4328 		report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses disabled");
4329 		test_tpr_threshold_values();
4330 		report_prefix_pop();
4331 	}
4332 
4333 	if (ctrl_cpu_rev[1].clr & CPU_VIRT_APIC_ACCESSES) {
4334 		vmcs_write(CPU_EXEC_CTRL0,
4335 			   vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY);
4336 		vmcs_write(CPU_EXEC_CTRL1, CPU_VIRT_APIC_ACCESSES);
4337 		report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled");
4338 		test_tpr_threshold_values();
4339 		report_prefix_pop();
4340 
4341 		vmcs_write(CPU_EXEC_CTRL0,
4342 			   vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY);
4343 		report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled");
4344 		test_tpr_threshold_values();
4345 		report_prefix_pop();
4346 	}
4347 
4348 	if ((ctrl_cpu_rev[1].clr &
4349 	     (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) ==
4350 	    (CPU_VINTD | CPU_VIRT_APIC_ACCESSES)) {
4351 		vmcs_write(CPU_EXEC_CTRL0,
4352 			   vmcs_read(CPU_EXEC_CTRL0) & ~CPU_SECONDARY);
4353 		vmcs_write(CPU_EXEC_CTRL1,
4354 			   CPU_VINTD | CPU_VIRT_APIC_ACCESSES);
4355 		report_prefix_pushf("Use TPR shadow enabled; secondary controls disabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled");
4356 		test_tpr_threshold_values();
4357 		report_prefix_pop();
4358 
4359 		vmcs_write(CPU_EXEC_CTRL0,
4360 			   vmcs_read(CPU_EXEC_CTRL0) | CPU_SECONDARY);
4361 		report_prefix_pushf("Use TPR shadow enabled; secondary controls enabled; virtual-interrupt delivery enabled; virtualize APIC accesses enabled");
4362 		test_tpr_threshold_values();
4363 		report_prefix_pop();
4364 	}
4365 
4366 	vmcs_write(CPU_EXEC_CTRL1, secondary);
4367 	vmcs_write(CPU_EXEC_CTRL0, primary);
4368 }
4369 
4370 /*
4371  * This test verifies the following two vmentry checks:
4372  *
4373  *  If the "NMI exiting" VM-execution control is 0, "Virtual NMIs"
4374  *  VM-execution control must be 0.
4375  *  [Intel SDM]
4376  *
4377  *  If the “virtual NMIs” VM-execution control is 0, the “NMI-window
4378  *  exiting” VM-execution control must be 0.
4379  *  [Intel SDM]
4380  */
4381 static void test_nmi_ctrls(void)
4382 {
4383 	u32 pin_ctrls, cpu_ctrls0, test_pin_ctrls, test_cpu_ctrls0;
4384 
4385 	if ((ctrl_pin_rev.clr & (PIN_NMI | PIN_VIRT_NMI)) !=
4386 	    (PIN_NMI | PIN_VIRT_NMI)) {
4387 		test_skip("NMI exiting and Virtual NMIs are not supported !");
4388 		return;
4389 	}
4390 
4391 	/* Save the controls so that we can restore them after our tests */
4392 	pin_ctrls = vmcs_read(PIN_CONTROLS);
4393 	cpu_ctrls0 = vmcs_read(CPU_EXEC_CTRL0);
4394 
4395 	test_pin_ctrls = pin_ctrls & ~(PIN_NMI | PIN_VIRT_NMI);
4396 	test_cpu_ctrls0 = cpu_ctrls0 & ~CPU_NMI_WINDOW;
4397 
4398 	vmcs_write(PIN_CONTROLS, test_pin_ctrls);
4399 	report_prefix_pushf("NMI-exiting disabled, virtual-NMIs disabled");
4400 	test_vmx_controls(true, false);
4401 	report_prefix_pop();
4402 
4403 	vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_VIRT_NMI);
4404 	report_prefix_pushf("NMI-exiting disabled, virtual-NMIs enabled");
4405 	test_vmx_controls(false, false);
4406 	report_prefix_pop();
4407 
4408 	vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI));
4409 	report_prefix_pushf("NMI-exiting enabled, virtual-NMIs enabled");
4410 	test_vmx_controls(true, false);
4411 	report_prefix_pop();
4412 
4413 	vmcs_write(PIN_CONTROLS, test_pin_ctrls | PIN_NMI);
4414 	report_prefix_pushf("NMI-exiting enabled, virtual-NMIs disabled");
4415 	test_vmx_controls(true, false);
4416 	report_prefix_pop();
4417 
4418 	if (!(ctrl_cpu_rev[0].clr & CPU_NMI_WINDOW)) {
4419 		report_info("NMI-window exiting is not supported, skipping...");
4420 		goto done;
4421 	}
4422 
4423 	vmcs_write(PIN_CONTROLS, test_pin_ctrls);
4424 	vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW);
4425 	report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting enabled");
4426 	test_vmx_controls(false, false);
4427 	report_prefix_pop();
4428 
4429 	vmcs_write(PIN_CONTROLS, test_pin_ctrls);
4430 	vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0);
4431 	report_prefix_pushf("Virtual-NMIs disabled, NMI-window-exiting disabled");
4432 	test_vmx_controls(true, false);
4433 	report_prefix_pop();
4434 
4435 	vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI));
4436 	vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0 | CPU_NMI_WINDOW);
4437 	report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting enabled");
4438 	test_vmx_controls(true, false);
4439 	report_prefix_pop();
4440 
4441 	vmcs_write(PIN_CONTROLS, test_pin_ctrls | (PIN_NMI | PIN_VIRT_NMI));
4442 	vmcs_write(CPU_EXEC_CTRL0, test_cpu_ctrls0);
4443 	report_prefix_pushf("Virtual-NMIs enabled, NMI-window-exiting disabled");
4444 	test_vmx_controls(true, false);
4445 	report_prefix_pop();
4446 
4447 	/* Restore the controls to their original values */
4448 	vmcs_write(CPU_EXEC_CTRL0, cpu_ctrls0);
4449 done:
4450 	vmcs_write(PIN_CONTROLS, pin_ctrls);
4451 }
4452 
4453 static void test_eptp_ad_bit(u64 eptp, bool ctrl)
4454 {
4455 	vmcs_write(EPTP, eptp);
4456 	report_prefix_pushf("Enable-EPT enabled; EPT accessed and dirty flag %s",
4457 	    (eptp & EPTP_AD_FLAG) ? "1": "0");
4458 	test_vmx_controls(ctrl, false);
4459 	report_prefix_pop();
4460 
4461 }
4462 /*
4463  * If the “enable EPT†VM-execution control is 1, the EPTP VM-execution
4464  * control field must satisfy the following checks:
4465  *
4466  *   —  The EPT memory type (bits 2:0) must be a value supported by the
4467  *	processor as indicated in the IA32_VMX_EPT_VPID_CAP MSR.
4468  *   —  Bits 5:3 (1 less than the EPT page-walk length) must be 3,
4469  *	indicating an EPT page-walk length of 4.
4470  *   —  Bit 6 (enable bit for accessed and dirty flags for EPT) must be
4471  *	0 if bit 21 of the IA32_VMX_EPT_VPID_CAP MSR is read as 0,
4472  *	indicating that the processor does not support accessed and dirty
4473  *	dirty flags for EPT.
4474  *   —  Reserved bits 11:7 and 63:N (where N is the processor’s
4475  *	physical-address width) must all be 0.
4476  *
4477  *  [Intel SDM]
4478  */
4479 static void test_eptp(void)
4480 {
4481 	u32 primary_saved = vmcs_read(CPU_EXEC_CTRL0);
4482 	u32 secondary_saved = vmcs_read(CPU_EXEC_CTRL1);
4483 	u64 eptp_saved = vmcs_read(EPTP);
4484 	u32 primary = primary_saved;
4485 	u32 secondary = secondary_saved;
4486 	u64 msr, eptp = eptp_saved;
4487 	bool un_cache = false;
4488 	bool wr_bk = false;
4489 	bool ctrl;
4490 	u32 i, maxphysaddr;
4491 	u64 j, resv_bits_mask = 0;
4492 
4493 	if (!((ctrl_cpu_rev[0].clr & CPU_SECONDARY) &&
4494 	    (ctrl_cpu_rev[1].clr & CPU_EPT))) {
4495 		test_skip("\"CPU secondary\" and/or \"enable EPT\" execution controls are not supported !");
4496 		return;
4497 	}
4498 
4499 	/*
4500 	 * Memory type (bits 2:0)
4501 	 */
4502 	msr = rdmsr(MSR_IA32_VMX_EPT_VPID_CAP);
4503 	if (msr & EPT_CAP_UC)
4504 		un_cache = true;
4505 	if (msr & EPT_CAP_WB)
4506 		wr_bk = true;
4507 
4508 	primary |= CPU_SECONDARY;
4509 	vmcs_write(CPU_EXEC_CTRL0, primary);
4510 	secondary |= CPU_EPT;
4511 	vmcs_write(CPU_EXEC_CTRL1, secondary);
4512 	eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) |
4513 	    (3ul << EPTP_PG_WALK_LEN_SHIFT);
4514 	vmcs_write(EPTP, eptp);
4515 
4516 	for (i = 0; i < 8; i++) {
4517 		if (i == 0) {
4518 			if (un_cache) {
4519 				report_info("EPT paging structure memory-type is Un-cacheable\n");
4520 				ctrl = true;
4521 			} else {
4522 				ctrl = false;
4523 			}
4524 		} else if (i == 6) {
4525 			if (wr_bk) {
4526 				report_info("EPT paging structure memory-type is Write-back\n");
4527 				ctrl = true;
4528 			} else {
4529 				ctrl = false;
4530 			}
4531 		} else {
4532 			ctrl = false;
4533 		}
4534 
4535 		eptp = (eptp & ~EPT_MEM_TYPE_MASK) | i;
4536 		vmcs_write(EPTP, eptp);
4537 		report_prefix_pushf("Enable-EPT enabled; EPT memory type %lu",
4538 		    eptp & EPT_MEM_TYPE_MASK);
4539 		test_vmx_controls(ctrl, false);
4540 		report_prefix_pop();
4541 	}
4542 
4543 	eptp = (eptp & ~EPT_MEM_TYPE_MASK) | 6ul;
4544 
4545 	/*
4546 	 * Page walk length (bits 5:3)
4547 	 */
4548 	for (i = 0; i < 8; i++) {
4549 		eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) |
4550 		    (i << EPTP_PG_WALK_LEN_SHIFT);
4551 		if (i == 3)
4552 			ctrl = true;
4553 		else
4554 			ctrl = false;
4555 
4556 		vmcs_write(EPTP, eptp);
4557 		report_prefix_pushf("Enable-EPT enabled; EPT page walk length %lu",
4558 		    eptp & EPTP_PG_WALK_LEN_MASK);
4559 		test_vmx_controls(ctrl, false);
4560 		report_prefix_pop();
4561 	}
4562 
4563 	eptp = (eptp & ~EPTP_PG_WALK_LEN_MASK) |
4564 	    3ul << EPTP_PG_WALK_LEN_SHIFT;
4565 
4566 	/*
4567 	 * Accessed and dirty flag (bit 6)
4568 	 */
4569 	if (msr & EPT_CAP_AD_FLAG) {
4570 		report_info("Processor supports accessed and dirty flag");
4571 		eptp &= ~EPTP_AD_FLAG;
4572 		test_eptp_ad_bit(eptp, true);
4573 
4574 		eptp |= EPTP_AD_FLAG;
4575 		test_eptp_ad_bit(eptp, true);
4576 	} else {
4577 		report_info("Processor does not supports accessed and dirty flag");
4578 		eptp &= ~EPTP_AD_FLAG;
4579 		test_eptp_ad_bit(eptp, true);
4580 
4581 		eptp |= EPTP_AD_FLAG;
4582 		test_eptp_ad_bit(eptp, false);
4583 	}
4584 
4585 	/*
4586 	 * Reserved bits [11:7] and [63:N]
4587 	 */
4588 	for (i = 0; i < 32; i++) {
4589 		if (i  == 0)
4590 			ctrl = true;
4591 		else
4592 			ctrl = false;
4593 
4594 		eptp = (eptp &
4595 		    ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT)) |
4596 		    (i << EPTP_RESERV_BITS_SHIFT);
4597 		vmcs_write(EPTP, eptp);
4598 		report_prefix_pushf("Enable-EPT enabled; reserved bits [11:7] %lu",
4599 		    (eptp >> EPTP_RESERV_BITS_SHIFT) &
4600 		    EPTP_RESERV_BITS_MASK);
4601 		test_vmx_controls(ctrl, false);
4602 		report_prefix_pop();
4603 	}
4604 
4605 	eptp = (eptp & ~(EPTP_RESERV_BITS_MASK << EPTP_RESERV_BITS_SHIFT));
4606 
4607 	maxphysaddr = cpuid_maxphyaddr();
4608 	for (i = 0; i < (63 - maxphysaddr + 1); i++) {
4609 		resv_bits_mask |= 1ul << i;
4610 	}
4611 
4612 	for (j = 0; j < (63 - maxphysaddr + 1); j++) {
4613 		if (j  == 0)
4614 			ctrl = true;
4615 		else
4616 			ctrl = false;
4617 
4618 		eptp = (eptp & ~(resv_bits_mask << maxphysaddr)) |
4619 		    (j << maxphysaddr);
4620 		vmcs_write(EPTP, eptp);
4621 		report_prefix_pushf("Enable-EPT enabled; reserved bits [63:N] %lu",
4622 		    (eptp >> maxphysaddr) & resv_bits_mask);
4623 		test_vmx_controls(ctrl, false);
4624 		report_prefix_pop();
4625 	}
4626 
4627 	vmcs_write(CPU_EXEC_CTRL0, primary_saved);
4628 	vmcs_write(CPU_EXEC_CTRL1, secondary_saved);
4629 	vmcs_write(EPTP, eptp_saved);
4630 }
4631 
4632 /*
4633  * Check that the virtual CPU checks all of the VMX controls as
4634  * documented in the Intel SDM.
4635  */
4636 static void vmx_controls_test(void)
4637 {
4638 	/*
4639 	 * Bit 1 of the guest's RFLAGS must be 1, or VM-entry will
4640 	 * fail due to invalid guest state, should we make it that
4641 	 * far.
4642 	 */
4643 	vmcs_write(GUEST_RFLAGS, 0);
4644 
4645 	test_pin_based_ctls();
4646 	test_primary_processor_based_ctls();
4647 	test_secondary_processor_based_ctls();
4648 	test_cr3_targets();
4649 	test_io_bitmaps();
4650 	test_msr_bitmap();
4651 	test_apic_ctls();
4652 	test_tpr_threshold();
4653 	test_nmi_ctrls();
4654 	test_invalid_event_injection();
4655 	test_vpid();
4656 	test_eptp();
4657 }
4658 
4659 static bool valid_vmcs_for_vmentry(void)
4660 {
4661 	struct vmcs *current_vmcs = NULL;
4662 
4663 	if (vmcs_save(&current_vmcs))
4664 		return false;
4665 
4666 	return current_vmcs && !current_vmcs->hdr.shadow_vmcs;
4667 }
4668 
4669 static void try_vmentry_in_movss_shadow(void)
4670 {
4671 	u32 vm_inst_err;
4672 	u32 flags;
4673 	bool early_failure = false;
4674 	u32 expected_flags = X86_EFLAGS_FIXED;
4675 	bool valid_vmcs = valid_vmcs_for_vmentry();
4676 
4677 	expected_flags |= valid_vmcs ? X86_EFLAGS_ZF : X86_EFLAGS_CF;
4678 
4679 	/*
4680 	 * Indirectly set VM_INST_ERR to 12 ("VMREAD/VMWRITE from/to
4681 	 * unsupported VMCS component").
4682 	 */
4683 	vmcs_write(~0u, 0);
4684 
4685 	__asm__ __volatile__ ("mov %[host_rsp], %%edx;"
4686 			      "vmwrite %%rsp, %%rdx;"
4687 			      "mov 0f, %%rax;"
4688 			      "mov %[host_rip], %%edx;"
4689 			      "vmwrite %%rax, %%rdx;"
4690 			      "mov $-1, %%ah;"
4691 			      "sahf;"
4692 			      "mov %%ss, %%ax;"
4693 			      "mov %%ax, %%ss;"
4694 			      "vmlaunch;"
4695 			      "mov $1, %[early_failure];"
4696 			      "0: lahf;"
4697 			      "movzbl %%ah, %[flags]"
4698 			      : [early_failure] "+r" (early_failure),
4699 				[flags] "=&a" (flags)
4700 			      : [host_rsp] "i" (HOST_RSP),
4701 				[host_rip] "i" (HOST_RIP)
4702 			      : "rdx", "cc", "memory");
4703 	vm_inst_err = vmcs_read(VMX_INST_ERROR);
4704 
4705 	report("Early VM-entry failure", early_failure);
4706 	report("RFLAGS[8:0] is %x (actual %x)", flags == expected_flags,
4707 	       expected_flags, flags);
4708 	if (valid_vmcs)
4709 		report("VM-instruction error is %d (actual %d)",
4710 		       vm_inst_err == VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS,
4711 		       VMXERR_ENTRY_EVENTS_BLOCKED_BY_MOV_SS, vm_inst_err);
4712 }
4713 
4714 static void vmentry_movss_shadow_test(void)
4715 {
4716 	struct vmcs *orig_vmcs;
4717 
4718 	TEST_ASSERT(!vmcs_save(&orig_vmcs));
4719 
4720 	/*
4721 	 * Set the launched flag on the current VMCS to verify the correct
4722 	 * error priority, below.
4723 	 */
4724 	test_set_guest(v2_null_test_guest);
4725 	enter_guest();
4726 
4727 	/*
4728 	 * With bit 1 of the guest's RFLAGS clear, VM-entry should
4729 	 * fail due to invalid guest state (if we make it that far).
4730 	 */
4731 	vmcs_write(GUEST_RFLAGS, 0);
4732 
4733 	/*
4734 	 * "VM entry with events blocked by MOV SS" takes precedence over
4735 	 * "VMLAUNCH with non-clear VMCS."
4736 	 */
4737 	report_prefix_push("valid current-VMCS");
4738 	try_vmentry_in_movss_shadow();
4739 	report_prefix_pop();
4740 
4741 	/*
4742 	 * VMfailInvalid takes precedence over "VM entry with events
4743 	 * blocked by MOV SS."
4744 	 */
4745 	TEST_ASSERT(!vmcs_clear(orig_vmcs));
4746 	report_prefix_push("no current-VMCS");
4747 	try_vmentry_in_movss_shadow();
4748 	report_prefix_pop();
4749 
4750 	TEST_ASSERT(!make_vmcs_current(orig_vmcs));
4751 	vmcs_write(GUEST_RFLAGS, X86_EFLAGS_FIXED);
4752 }
4753 
4754 #define X86_FEATURE_PCID       (1 << 17)
4755 #define X86_FEATURE_MCE        (1 << 7)
4756 
4757 static int write_cr4_checking(unsigned long val)
4758 {
4759 	asm volatile(ASM_TRY("1f")
4760 		     "mov %0, %%cr4\n\t"
4761 		     "1:": : "r" (val));
4762 	return exception_vector();
4763 }
4764 
4765 static void vmx_cr_load_test(void)
4766 {
4767 	struct cpuid _cpuid = cpuid(1);
4768 	unsigned long cr4 = read_cr4(), cr3 = read_cr3();
4769 
4770 	if (!(_cpuid.c & X86_FEATURE_PCID)) {
4771 		report_skip("PCID not detected");
4772 		return;
4773 	}
4774 	if (!(_cpuid.d & X86_FEATURE_MCE)) {
4775 		report_skip("MCE not detected");
4776 		return;
4777 	}
4778 
4779 	TEST_ASSERT(!(cr4 & (X86_CR4_PCIDE | X86_CR4_MCE)));
4780 	TEST_ASSERT(!(cr3 & X86_CR3_PCID_MASK));
4781 
4782 	/* Enable PCID for L1. */
4783 	cr4 |= X86_CR4_PCIDE;
4784 	cr3 |= 0x1;
4785 	TEST_ASSERT(!write_cr4_checking(cr4));
4786 	write_cr3(cr3);
4787 
4788 	test_set_guest(v2_null_test_guest);
4789 	vmcs_write(HOST_CR4, cr4);
4790 	vmcs_write(HOST_CR3, cr3);
4791 	enter_guest();
4792 
4793 	/*
4794 	 * No exception is expected.
4795 	 *
4796 	 * NB. KVM loads the last guest write to CR4 into CR4 read
4797 	 *     shadow. In order to trigger an exit to KVM, we can set a
4798 	 *     bit that was zero in the above CR4 write and is owned by
4799 	 *     KVM. We choose to set CR4.MCE, which shall have no side
4800 	 *     effect because normally no guest MCE (e.g., as the result
4801 	 *     of bad memory) would happen during this test.
4802 	 */
4803 	TEST_ASSERT(!write_cr4_checking(cr4 | X86_CR4_MCE));
4804 
4805 	/* Cleanup L1 state: disable PCID. */
4806 	write_cr3(cr3 & ~X86_CR3_PCID_MASK);
4807 	TEST_ASSERT(!write_cr4_checking(cr4 & ~X86_CR4_PCIDE));
4808 }
4809 
4810 bool vmx_pending_event_ipi_fired;
4811 static void vmx_pending_event_ipi_isr(isr_regs_t *regs)
4812 {
4813 	vmx_pending_event_ipi_fired = true;
4814 	eoi();
4815 }
4816 
4817 bool vmx_pending_event_guest_run;
4818 static void vmx_pending_event_guest(void)
4819 {
4820 	vmx_pending_event_guest_run = true;
4821 }
4822 
4823 static void vmx_pending_event_test(void)
4824 {
4825 	int ipi_vector = 0xf1;
4826 
4827 	vmx_pending_event_ipi_fired = false;
4828 	handle_irq(ipi_vector, vmx_pending_event_ipi_isr);
4829 
4830 	vmx_pending_event_guest_run = false;
4831 	test_set_guest(vmx_pending_event_guest);
4832 
4833 	vmcs_set_bits(PIN_CONTROLS, PIN_EXTINT);
4834 
4835 	irq_disable();
4836 	apic_icr_write(APIC_DEST_SELF | APIC_DEST_PHYSICAL |
4837 				   APIC_DM_FIXED | ipi_vector,
4838 				   0);
4839 
4840 	enter_guest();
4841 
4842 	assert_exit_reason(VMX_EXTINT);
4843 	report("Guest did not run before host received IPI",
4844 		   !vmx_pending_event_guest_run);
4845 
4846 	irq_enable();
4847 	asm volatile ("nop");
4848 	irq_disable();
4849 	report("Got pending interrupt after IRQ enabled",
4850 		   vmx_pending_event_ipi_fired);
4851 
4852 	enter_guest();
4853 	report("Guest finished running when no interrupt",
4854 		   vmx_pending_event_guest_run);
4855 }
4856 
4857 static bool cpu_has_apicv(void)
4858 {
4859 	return ((ctrl_cpu_rev[1].clr & CPU_APIC_REG_VIRT) &&
4860 		(ctrl_cpu_rev[1].clr & CPU_VINTD) &&
4861 		(ctrl_pin_rev.clr & PIN_POST_INTR));
4862 }
4863 
4864 static void trigger_ioapic_scan_thread(void *data)
4865 {
4866 	/* Wait until other CPU entered L2 */
4867 	while (vmx_get_test_stage() != 1)
4868 		;
4869 
4870 	/* Trigger ioapic scan */
4871 	ioapic_set_redir(0xf, 0x79, TRIGGER_LEVEL);
4872 	vmx_set_test_stage(2);
4873 }
4874 
4875 static void irq_79_handler_guest(isr_regs_t *regs)
4876 {
4877 	eoi();
4878 
4879 	/* L1 expects vmexit on VMX_VMCALL and not VMX_EOI_INDUCED */
4880 	vmcall();
4881 }
4882 
4883 /*
4884  * Constant for num of busy-loop iterations after which
4885  * a timer interrupt should have happened in host
4886  */
4887 #define TIMER_INTERRUPT_DELAY 100000000
4888 
4889 static void vmx_eoi_bitmap_ioapic_scan_test_guest(void)
4890 {
4891 	handle_irq(0x79, irq_79_handler_guest);
4892 	irq_enable();
4893 
4894 	/* Signal to L1 CPU to trigger ioapic scan */
4895 	vmx_set_test_stage(1);
4896 	/* Wait until L1 CPU to trigger ioapic scan */
4897 	while (vmx_get_test_stage() != 2)
4898 		;
4899 
4900 	/*
4901 	 * Wait for L0 timer interrupt to be raised while we run in L2
4902 	 * such that L0 will process the IOAPIC scan request before
4903 	 * resuming L2
4904 	 */
4905 	delay(TIMER_INTERRUPT_DELAY);
4906 
4907 	asm volatile ("int $0x79");
4908 }
4909 
4910 static void vmx_eoi_bitmap_ioapic_scan_test(void)
4911 {
4912 	void *msr_bitmap;
4913 	void *virtual_apic_page;
4914 
4915 	if (!cpu_has_apicv() || (cpu_count() < 2)) {
4916 		report_skip(__func__);
4917 		return;
4918 	}
4919 
4920 	msr_bitmap = alloc_page();
4921 	virtual_apic_page = alloc_page();
4922 
4923 	u64 cpu_ctrl_0 = CPU_SECONDARY | CPU_TPR_SHADOW | CPU_MSR_BITMAP;
4924 	u64 cpu_ctrl_1 = CPU_VINTD | CPU_VIRT_X2APIC;
4925 
4926 	memset(msr_bitmap, 0x0, PAGE_SIZE);
4927 	vmcs_write(MSR_BITMAP, (u64)msr_bitmap);
4928 
4929 	vmcs_write(APIC_VIRT_ADDR, (u64)virtual_apic_page);
4930 	vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) | PIN_EXTINT);
4931 
4932 	vmcs_write(EOI_EXIT_BITMAP0, 0x0);
4933 	vmcs_write(EOI_EXIT_BITMAP1, 0x0);
4934 	vmcs_write(EOI_EXIT_BITMAP2, 0x0);
4935 	vmcs_write(EOI_EXIT_BITMAP3, 0x0);
4936 
4937 	vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | cpu_ctrl_0);
4938 	vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | cpu_ctrl_1);
4939 
4940 	on_cpu_async(1, trigger_ioapic_scan_thread, NULL);
4941 	test_set_guest(vmx_eoi_bitmap_ioapic_scan_test_guest);
4942 
4943 	/*
4944 	 * Launch L2.
4945 	 * We expect the exit reason to be VMX_VMCALL (and not EOI INDUCED).
4946 	 * In case the reason isn't VMX_VMCALL, the asserion inside
4947 	 * skip_exit_vmcall() will fail.
4948 	 */
4949 	enter_guest();
4950 	skip_exit_vmcall();
4951 
4952 	/* Let L2 finish */
4953 	enter_guest();
4954 	report(__func__, 1);
4955 }
4956 
4957 static void set_irq_line_thread(void *data)
4958 {
4959 	/* Wait until other CPU entered L2 */
4960 	while (vmx_get_test_stage() != 1)
4961 		;
4962 
4963 	/* Set irq-line 0xf to raise vector 0x78 for vCPU 0 */
4964 	ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL);
4965 	vmx_set_test_stage(2);
4966 }
4967 
4968 static bool irq_78_handler_vmcall_before_eoi;
4969 static void irq_78_handler_guest(isr_regs_t *regs)
4970 {
4971 	set_irq_line(0xf, 0);
4972 	if (irq_78_handler_vmcall_before_eoi)
4973 		vmcall();
4974 	eoi();
4975 	vmcall();
4976 }
4977 
4978 static void vmx_apic_passthrough_guest(void)
4979 {
4980 	handle_irq(0x78, irq_78_handler_guest);
4981 	irq_enable();
4982 
4983 	/* If requested, wait for other CPU to trigger ioapic scan */
4984 	if (vmx_get_test_stage() < 1) {
4985 		vmx_set_test_stage(1);
4986 		while (vmx_get_test_stage() != 2)
4987 			;
4988 	}
4989 
4990 	set_irq_line(0xf, 1);
4991 }
4992 
4993 static void vmx_apic_passthrough(bool set_irq_line_from_thread)
4994 {
4995 	void *msr_bitmap;
4996 
4997 	if (set_irq_line_from_thread && (cpu_count() < 2)) {
4998 		report_skip(__func__);
4999 		return;
5000 	}
5001 
5002 	msr_bitmap = alloc_page();
5003 
5004 	u64 cpu_ctrl_0 = CPU_SECONDARY | CPU_MSR_BITMAP;
5005 	u64 cpu_ctrl_1 = 0;
5006 
5007 	memset(msr_bitmap, 0x0, PAGE_SIZE);
5008 	vmcs_write(MSR_BITMAP, (u64)msr_bitmap);
5009 
5010 	vmcs_write(PIN_CONTROLS, vmcs_read(PIN_CONTROLS) & ~PIN_EXTINT);
5011 
5012 	vmcs_write(CPU_EXEC_CTRL0, vmcs_read(CPU_EXEC_CTRL0) | cpu_ctrl_0);
5013 	vmcs_write(CPU_EXEC_CTRL1, vmcs_read(CPU_EXEC_CTRL1) | cpu_ctrl_1);
5014 
5015 	if (set_irq_line_from_thread) {
5016 		irq_78_handler_vmcall_before_eoi = false;
5017 		on_cpu_async(1, set_irq_line_thread, NULL);
5018 	} else {
5019 		irq_78_handler_vmcall_before_eoi = true;
5020 		ioapic_set_redir(0xf, 0x78, TRIGGER_LEVEL);
5021 		vmx_set_test_stage(2);
5022 	}
5023 	test_set_guest(vmx_apic_passthrough_guest);
5024 
5025 	if (irq_78_handler_vmcall_before_eoi) {
5026 		/* Before EOI remote_irr should still be set */
5027 		enter_guest();
5028 		skip_exit_vmcall();
5029 		TEST_ASSERT_EQ_MSG(1, (int)ioapic_read_redir(0xf).remote_irr,
5030 			"IOAPIC pass-through: remote_irr=1 before EOI");
5031 	}
5032 
5033 	/* After EOI remote_irr should be cleared */
5034 	enter_guest();
5035 	skip_exit_vmcall();
5036 	TEST_ASSERT_EQ_MSG(0, (int)ioapic_read_redir(0xf).remote_irr,
5037 		"IOAPIC pass-through: remote_irr=0 after EOI");
5038 
5039 	/* Let L2 finish */
5040 	enter_guest();
5041 	report(__func__, 1);
5042 }
5043 
5044 static void vmx_apic_passthrough_test(void)
5045 {
5046 	vmx_apic_passthrough(false);
5047 }
5048 
5049 static void vmx_apic_passthrough_thread_test(void)
5050 {
5051 	vmx_apic_passthrough(true);
5052 }
5053 
5054 enum vmcs_access {
5055 	ACCESS_VMREAD,
5056 	ACCESS_VMWRITE,
5057 	ACCESS_NONE,
5058 };
5059 
5060 struct vmcs_shadow_test_common {
5061 	enum vmcs_access op;
5062 	enum Reason reason;
5063 	u64 field;
5064 	u64 value;
5065 	u64 flags;
5066 	u64 time;
5067 } l1_l2_common;
5068 
5069 static inline u64 vmread_flags(u64 field, u64 *val)
5070 {
5071 	u64 flags;
5072 
5073 	asm volatile ("vmread %2, %1; pushf; pop %0"
5074 		      : "=r" (flags), "=rm" (*val) : "r" (field) : "cc");
5075 	return flags & X86_EFLAGS_ALU;
5076 }
5077 
5078 static inline u64 vmwrite_flags(u64 field, u64 val)
5079 {
5080 	u64 flags;
5081 
5082 	asm volatile ("vmwrite %1, %2; pushf; pop %0"
5083 		      : "=r"(flags) : "rm" (val), "r" (field) : "cc");
5084 	return flags & X86_EFLAGS_ALU;
5085 }
5086 
5087 static void vmx_vmcs_shadow_test_guest(void)
5088 {
5089 	struct vmcs_shadow_test_common *c = &l1_l2_common;
5090 	u64 start;
5091 
5092 	while (c->op != ACCESS_NONE) {
5093 		start = rdtsc();
5094 		switch (c->op) {
5095 		default:
5096 			c->flags = -1ull;
5097 			break;
5098 		case ACCESS_VMREAD:
5099 			c->flags = vmread_flags(c->field, &c->value);
5100 			break;
5101 		case ACCESS_VMWRITE:
5102 			c->flags = vmwrite_flags(c->field, 0);
5103 			break;
5104 		}
5105 		c->time = rdtsc() - start;
5106 		vmcall();
5107 	}
5108 }
5109 
5110 static u64 vmread_from_shadow(u64 field)
5111 {
5112 	struct vmcs *primary;
5113 	struct vmcs *shadow;
5114 	u64 value;
5115 
5116 	TEST_ASSERT(!vmcs_save(&primary));
5117 	shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR);
5118 	TEST_ASSERT(!make_vmcs_current(shadow));
5119 	value = vmcs_read(field);
5120 	TEST_ASSERT(!make_vmcs_current(primary));
5121 	return value;
5122 }
5123 
5124 static u64 vmwrite_to_shadow(u64 field, u64 value)
5125 {
5126 	struct vmcs *primary;
5127 	struct vmcs *shadow;
5128 
5129 	TEST_ASSERT(!vmcs_save(&primary));
5130 	shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR);
5131 	TEST_ASSERT(!make_vmcs_current(shadow));
5132 	vmcs_write(field, value);
5133 	value = vmcs_read(field);
5134 	TEST_ASSERT(!make_vmcs_current(primary));
5135 	return value;
5136 }
5137 
5138 static void vmcs_shadow_test_access(u8 *bitmap[2], enum vmcs_access access)
5139 {
5140 	struct vmcs_shadow_test_common *c = &l1_l2_common;
5141 
5142 	c->op = access;
5143 	vmcs_write(VMX_INST_ERROR, 0);
5144 	enter_guest();
5145 	c->reason = vmcs_read(EXI_REASON) & 0xffff;
5146 	if (c->reason != VMX_VMCALL) {
5147 		skip_exit_insn();
5148 		enter_guest();
5149 	}
5150 	skip_exit_vmcall();
5151 }
5152 
5153 static void vmcs_shadow_test_field(u8 *bitmap[2], u64 field)
5154 {
5155 	struct vmcs_shadow_test_common *c = &l1_l2_common;
5156 	struct vmcs *shadow;
5157 	u64 value;
5158 	uintptr_t flags[2];
5159 	bool good_shadow;
5160 	u32 vmx_inst_error;
5161 
5162 	report_prefix_pushf("field %lx", field);
5163 	c->field = field;
5164 
5165 	shadow = (struct vmcs *)vmcs_read(VMCS_LINK_PTR);
5166 	if (shadow != (struct vmcs *)-1ull) {
5167 		flags[ACCESS_VMREAD] = vmread_flags(field, &value);
5168 		flags[ACCESS_VMWRITE] = vmwrite_flags(field, value);
5169 		good_shadow = !flags[ACCESS_VMREAD] && !flags[ACCESS_VMWRITE];
5170 	} else {
5171 		/*
5172 		 * When VMCS link pointer is -1ull, VMWRITE/VMREAD on
5173 		 * shadowed-fields should fail with setting RFLAGS.CF.
5174 		 */
5175 		flags[ACCESS_VMREAD] = X86_EFLAGS_CF;
5176 		flags[ACCESS_VMWRITE] = X86_EFLAGS_CF;
5177 		good_shadow = false;
5178 	}
5179 
5180 	/* Intercept both VMREAD and VMWRITE. */
5181 	report_prefix_push("no VMREAD/VMWRITE permission");
5182 	/* VMWRITE/VMREAD done on reserved-bit should always intercept */
5183 	if (!(field >> VMCS_FIELD_RESERVED_SHIFT)) {
5184 		set_bit(field, bitmap[ACCESS_VMREAD]);
5185 		set_bit(field, bitmap[ACCESS_VMWRITE]);
5186 	}
5187 	vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE);
5188 	report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE);
5189 	vmcs_shadow_test_access(bitmap, ACCESS_VMREAD);
5190 	report("not shadowed for VMREAD", c->reason == VMX_VMREAD);
5191 	report_prefix_pop();
5192 
5193 	if (field >> VMCS_FIELD_RESERVED_SHIFT)
5194 		goto out;
5195 
5196 	/* Permit shadowed VMREAD. */
5197 	report_prefix_push("VMREAD permission only");
5198 	clear_bit(field, bitmap[ACCESS_VMREAD]);
5199 	set_bit(field, bitmap[ACCESS_VMWRITE]);
5200 	if (good_shadow)
5201 		value = vmwrite_to_shadow(field, MAGIC_VAL_1 + field);
5202 	vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE);
5203 	report("not shadowed for VMWRITE", c->reason == VMX_VMWRITE);
5204 	vmcs_shadow_test_access(bitmap, ACCESS_VMREAD);
5205 	vmx_inst_error = vmcs_read(VMX_INST_ERROR);
5206 	report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL,
5207 	       c->time);
5208 	report("ALU flags after VMREAD (%lx) are as expected (%lx)",
5209 	       c->flags == flags[ACCESS_VMREAD],
5210 	       c->flags, flags[ACCESS_VMREAD]);
5211 	if (good_shadow)
5212 		report("value read from shadow (%lx) is as expected (%lx)",
5213 		       c->value == value, c->value, value);
5214 	else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD])
5215 		report("VMX_INST_ERROR (%d) is as expected (%d)",
5216 		       vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT,
5217 		       vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5218 	report_prefix_pop();
5219 
5220 	/* Permit shadowed VMWRITE. */
5221 	report_prefix_push("VMWRITE permission only");
5222 	set_bit(field, bitmap[ACCESS_VMREAD]);
5223 	clear_bit(field, bitmap[ACCESS_VMWRITE]);
5224 	if (good_shadow)
5225 		vmwrite_to_shadow(field, MAGIC_VAL_1 + field);
5226 	vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE);
5227 	vmx_inst_error = vmcs_read(VMX_INST_ERROR);
5228 	report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL,
5229 		c->time);
5230 	report("ALU flags after VMWRITE (%lx) are as expected (%lx)",
5231 	       c->flags == flags[ACCESS_VMREAD],
5232 	       c->flags, flags[ACCESS_VMREAD]);
5233 	if (good_shadow) {
5234 		value = vmread_from_shadow(field);
5235 		report("shadow VMCS value (%lx) is as expected (%lx)",
5236 		       value == 0, value, 0ul);
5237 	} else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) {
5238 		report("VMX_INST_ERROR (%d) is as expected (%d)",
5239 		       vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT,
5240 		       vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5241 	}
5242 	vmcs_shadow_test_access(bitmap, ACCESS_VMREAD);
5243 	report("not shadowed for VMREAD", c->reason == VMX_VMREAD);
5244 	report_prefix_pop();
5245 
5246 	/* Permit shadowed VMREAD and VMWRITE. */
5247 	report_prefix_push("VMREAD and VMWRITE permission");
5248 	clear_bit(field, bitmap[ACCESS_VMREAD]);
5249 	clear_bit(field, bitmap[ACCESS_VMWRITE]);
5250 	if (good_shadow)
5251 		vmwrite_to_shadow(field, MAGIC_VAL_1 + field);
5252 	vmcs_shadow_test_access(bitmap, ACCESS_VMWRITE);
5253 	vmx_inst_error = vmcs_read(VMX_INST_ERROR);
5254 	report("shadowed for VMWRITE (in %ld cycles)", c->reason == VMX_VMCALL,
5255 		c->time);
5256 	report("ALU flags after VMWRITE (%lx) are as expected (%lx)",
5257 	       c->flags == flags[ACCESS_VMREAD],
5258 	       c->flags, flags[ACCESS_VMREAD]);
5259 	if (good_shadow) {
5260 		value = vmread_from_shadow(field);
5261 		report("shadow VMCS value (%lx) is as expected (%lx)",
5262 		       value == 0, value, 0ul);
5263 	} else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMWRITE]) {
5264 		report("VMX_INST_ERROR (%d) is as expected (%d)",
5265 		       vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT,
5266 		       vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5267 	}
5268 	vmcs_shadow_test_access(bitmap, ACCESS_VMREAD);
5269 	vmx_inst_error = vmcs_read(VMX_INST_ERROR);
5270 	report("shadowed for VMREAD (in %ld cycles)", c->reason == VMX_VMCALL,
5271 	       c->time);
5272 	report("ALU flags after VMREAD (%lx) are as expected (%lx)",
5273 	       c->flags == flags[ACCESS_VMREAD],
5274 	       c->flags, flags[ACCESS_VMREAD]);
5275 	if (good_shadow)
5276 		report("value read from shadow (%lx) is as expected (%lx)",
5277 		       c->value == 0, c->value, 0ul);
5278 	else if (shadow != (struct vmcs *)-1ull && flags[ACCESS_VMREAD])
5279 		report("VMX_INST_ERROR (%d) is as expected (%d)",
5280 		       vmx_inst_error == VMXERR_UNSUPPORTED_VMCS_COMPONENT,
5281 		       vmx_inst_error, VMXERR_UNSUPPORTED_VMCS_COMPONENT);
5282 	report_prefix_pop();
5283 
5284 out:
5285 	report_prefix_pop();
5286 }
5287 
5288 static void vmx_vmcs_shadow_test_body(u8 *bitmap[2])
5289 {
5290 	unsigned base;
5291 	unsigned index;
5292 	unsigned bit;
5293 	unsigned highest_index = rdmsr(MSR_IA32_VMX_VMCS_ENUM);
5294 
5295 	/* Run test on all possible valid VMCS fields */
5296 	for (base = 0;
5297 	     base < (1 << VMCS_FIELD_RESERVED_SHIFT);
5298 	     base += (1 << VMCS_FIELD_TYPE_SHIFT))
5299 		for (index = 0; index <= highest_index; index++)
5300 			vmcs_shadow_test_field(bitmap, base + index);
5301 
5302 	/*
5303 	 * Run tests on some invalid VMCS fields
5304 	 * (Have reserved bit set).
5305 	 */
5306 	for (bit = VMCS_FIELD_RESERVED_SHIFT; bit < VMCS_FIELD_BIT_SIZE; bit++)
5307 		vmcs_shadow_test_field(bitmap, (1ull << bit));
5308 }
5309 
5310 static void vmx_vmcs_shadow_test(void)
5311 {
5312 	u8 *bitmap[2];
5313 	struct vmcs *shadow;
5314 
5315 	if (!(ctrl_cpu_rev[0].clr & CPU_SECONDARY)) {
5316 		printf("\t'Activate secondary controls' not supported.\n");
5317 		return;
5318 	}
5319 
5320 	if (!(ctrl_cpu_rev[1].clr & CPU_SHADOW_VMCS)) {
5321 		printf("\t'VMCS shadowing' not supported.\n");
5322 		return;
5323 	}
5324 
5325 	if (!(rdmsr(MSR_IA32_VMX_MISC) &
5326 	      MSR_IA32_VMX_MISC_VMWRITE_SHADOW_RO_FIELDS)) {
5327 		printf("\tVMWRITE can't modify VM-exit information fields.\n");
5328 		return;
5329 	}
5330 
5331 	test_set_guest(vmx_vmcs_shadow_test_guest);
5332 
5333 	bitmap[ACCESS_VMREAD] = alloc_page();
5334 	bitmap[ACCESS_VMWRITE] = alloc_page();
5335 
5336 	vmcs_write(VMREAD_BITMAP, virt_to_phys(bitmap[ACCESS_VMREAD]));
5337 	vmcs_write(VMWRITE_BITMAP, virt_to_phys(bitmap[ACCESS_VMWRITE]));
5338 
5339 	shadow = alloc_page();
5340 	shadow->hdr.revision_id = basic.revision;
5341 	shadow->hdr.shadow_vmcs = 1;
5342 	TEST_ASSERT(!vmcs_clear(shadow));
5343 
5344 	vmcs_clear_bits(CPU_EXEC_CTRL0, CPU_RDTSC);
5345 	vmcs_set_bits(CPU_EXEC_CTRL0, CPU_SECONDARY);
5346 	vmcs_set_bits(CPU_EXEC_CTRL1, CPU_SHADOW_VMCS);
5347 
5348 	vmcs_write(VMCS_LINK_PTR, virt_to_phys(shadow));
5349 	report_prefix_push("valid link pointer");
5350 	vmx_vmcs_shadow_test_body(bitmap);
5351 	report_prefix_pop();
5352 
5353 	vmcs_write(VMCS_LINK_PTR, -1ull);
5354 	report_prefix_push("invalid link pointer");
5355 	vmx_vmcs_shadow_test_body(bitmap);
5356 	report_prefix_pop();
5357 
5358 	l1_l2_common.op = ACCESS_NONE;
5359 	enter_guest();
5360 }
5361 
5362 #define TEST(name) { #name, .v2 = name }
5363 
5364 /* name/init/guest_main/exit_handler/syscall_handler/guest_regs */
5365 struct vmx_test vmx_tests[] = {
5366 	{ "null", NULL, basic_guest_main, basic_exit_handler, NULL, {0} },
5367 	{ "vmenter", NULL, vmenter_main, vmenter_exit_handler, NULL, {0} },
5368 	{ "preemption timer", preemption_timer_init, preemption_timer_main,
5369 		preemption_timer_exit_handler, NULL, {0} },
5370 	{ "control field PAT", test_ctrl_pat_init, test_ctrl_pat_main,
5371 		test_ctrl_pat_exit_handler, NULL, {0} },
5372 	{ "control field EFER", test_ctrl_efer_init, test_ctrl_efer_main,
5373 		test_ctrl_efer_exit_handler, NULL, {0} },
5374 	{ "CR shadowing", NULL, cr_shadowing_main,
5375 		cr_shadowing_exit_handler, NULL, {0} },
5376 	{ "I/O bitmap", iobmp_init, iobmp_main, iobmp_exit_handler,
5377 		NULL, {0} },
5378 	{ "instruction intercept", insn_intercept_init, insn_intercept_main,
5379 		insn_intercept_exit_handler, NULL, {0} },
5380 	{ "EPT A/D disabled", ept_init, ept_main, ept_exit_handler, NULL, {0} },
5381 	{ "EPT A/D enabled", eptad_init, eptad_main, eptad_exit_handler, NULL, {0} },
5382 	{ "PML", pml_init, pml_main, pml_exit_handler, NULL, {0} },
5383 	{ "VPID", vpid_init, vpid_main, vpid_exit_handler, NULL, {0} },
5384 	{ "interrupt", interrupt_init, interrupt_main,
5385 		interrupt_exit_handler, NULL, {0} },
5386 	{ "debug controls", dbgctls_init, dbgctls_main, dbgctls_exit_handler,
5387 		NULL, {0} },
5388 	{ "MSR switch", msr_switch_init, msr_switch_main,
5389 		msr_switch_exit_handler, NULL, {0}, msr_switch_entry_failure },
5390 	{ "vmmcall", vmmcall_init, vmmcall_main, vmmcall_exit_handler, NULL, {0} },
5391 	{ "disable RDTSCP", disable_rdtscp_init, disable_rdtscp_main,
5392 		disable_rdtscp_exit_handler, NULL, {0} },
5393 	{ "int3", int3_init, int3_guest_main, int3_exit_handler, NULL, {0} },
5394 	{ "into", into_init, into_guest_main, into_exit_handler, NULL, {0} },
5395 	{ "exit_monitor_from_l2_test", NULL, exit_monitor_from_l2_main,
5396 		exit_monitor_from_l2_handler, NULL, {0} },
5397 	/* Basic V2 tests. */
5398 	TEST(v2_null_test),
5399 	TEST(v2_multiple_entries_test),
5400 	TEST(fixture_test_case1),
5401 	TEST(fixture_test_case2),
5402 	/* Opcode tests. */
5403 	TEST(invvpid_test_v2),
5404 	/* VM-entry tests */
5405 	TEST(vmx_controls_test),
5406 	TEST(vmentry_movss_shadow_test),
5407 	/* APICv tests */
5408 	TEST(vmx_eoi_bitmap_ioapic_scan_test),
5409 	/* APIC pass-through tests */
5410 	TEST(vmx_apic_passthrough_test),
5411 	TEST(vmx_apic_passthrough_thread_test),
5412 	/* VMCS Shadowing tests */
5413 	TEST(vmx_vmcs_shadow_test),
5414 	/* Regression tests */
5415 	TEST(vmx_cr_load_test),
5416 	TEST(vmx_pending_event_test),
5417 	/* EPT access tests. */
5418 	TEST(ept_access_test_not_present),
5419 	TEST(ept_access_test_read_only),
5420 	TEST(ept_access_test_write_only),
5421 	TEST(ept_access_test_read_write),
5422 	TEST(ept_access_test_execute_only),
5423 	TEST(ept_access_test_read_execute),
5424 	TEST(ept_access_test_write_execute),
5425 	TEST(ept_access_test_read_write_execute),
5426 	TEST(ept_access_test_reserved_bits),
5427 	TEST(ept_access_test_ignored_bits),
5428 	TEST(ept_access_test_paddr_not_present_ad_disabled),
5429 	TEST(ept_access_test_paddr_not_present_ad_enabled),
5430 	TEST(ept_access_test_paddr_read_only_ad_disabled),
5431 	TEST(ept_access_test_paddr_read_only_ad_enabled),
5432 	TEST(ept_access_test_paddr_read_write),
5433 	TEST(ept_access_test_paddr_read_write_execute),
5434 	TEST(ept_access_test_paddr_read_execute_ad_disabled),
5435 	TEST(ept_access_test_paddr_read_execute_ad_enabled),
5436 	TEST(ept_access_test_paddr_not_present_page_fault),
5437 	TEST(ept_access_test_force_2m_page),
5438 	{ NULL, NULL, NULL, NULL, NULL, {0} },
5439 };
5440