1 // SPDX-License-Identifier: GPL-2.0-only
11 #include "kselftest.h"
13 #define ARBITRARY_IO_PORT 0x2000
15 /* The virtual machine object. */
16 static struct kvm_vm *vm;
18 static void l2_guest_code(void)
20 asm volatile("inb %%dx, %%al"
21 : : [port] "d" (ARBITRARY_IO_PORT) : "rax");
24 #define L2_GUEST_STACK_SIZE 64
25 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
27 void l1_guest_code_vmx(struct vmx_pages *vmx)
30 GUEST_ASSERT(vmx->vmcs_gpa);
31 GUEST_ASSERT(prepare_for_vmx_operation(vmx));
32 GUEST_ASSERT(load_vmcs(vmx));
34 prepare_vmcs(vmx, l2_guest_code,
35 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
37 GUEST_ASSERT(!vmlaunch());
38 /* L2 should triple fault after a triple fault event injected. */
39 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_TRIPLE_FAULT);
43 void l1_guest_code_svm(struct svm_test_data *svm)
45 struct vmcb *vmcb = svm->vmcb;
47 generic_svm_setup(svm, l2_guest_code,
48 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
50 /* don't intercept shutdown to test the case of SVM allowing to do so */
51 vmcb->control.intercept &= ~(BIT(INTERCEPT_SHUTDOWN));
53 run_guest(vmcb, svm->vmcb_gpa);
55 /* should not reach here, L1 should crash */
61 struct kvm_vcpu *vcpu;
63 struct kvm_vcpu_events events;
66 bool has_vmx = kvm_cpu_has(X86_FEATURE_VMX);
67 bool has_svm = kvm_cpu_has(X86_FEATURE_SVM);
69 TEST_REQUIRE(has_vmx || has_svm);
71 TEST_REQUIRE(kvm_has_cap(KVM_CAP_X86_TRIPLE_FAULT_EVENT));
75 vm_vaddr_t vmx_pages_gva;
77 vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code_vmx);
78 vcpu_alloc_vmx(vm, &vmx_pages_gva);
79 vcpu_args_set(vcpu, 1, vmx_pages_gva);
83 vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code_svm);
84 vcpu_alloc_svm(vm, &svm_gva);
85 vcpu_args_set(vcpu, 1, svm_gva);
88 vm_enable_cap(vm, KVM_CAP_X86_TRIPLE_FAULT_EVENT, 1);
92 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
93 TEST_ASSERT(run->io.port == ARBITRARY_IO_PORT,
94 "Expected IN from port %d from L2, got port %d",
95 ARBITRARY_IO_PORT, run->io.port);
96 vcpu_events_get(vcpu, &events);
97 events.flags |= KVM_VCPUEVENT_VALID_TRIPLE_FAULT;
98 events.triple_fault.pending = true;
99 vcpu_events_set(vcpu, &events);
100 run->immediate_exit = true;
101 vcpu_run_complete_io(vcpu);
103 vcpu_events_get(vcpu, &events);
104 TEST_ASSERT(events.flags & KVM_VCPUEVENT_VALID_TRIPLE_FAULT,
105 "Triple fault event invalid");
106 TEST_ASSERT(events.triple_fault.pending,
107 "No triple fault pending");
112 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);
114 switch (get_ucall(vcpu, &uc)) {
118 REPORT_GUEST_ASSERT(uc);
120 TEST_FAIL("Unexpected ucall: %lu", uc.cmd);