Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / testing / selftests / kvm / x86_64 / triple_fault_event_test.c
blob56306a19144a7db90a7f0906aede46e75ee346a1
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include "test_util.h"
3 #include "kvm_util.h"
4 #include "processor.h"
5 #include "vmx.h"
6 #include "svm_util.h"
8 #include <string.h>
9 #include <sys/ioctl.h>
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);
40 GUEST_DONE();
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 */
56 GUEST_ASSERT(0);
59 int main(void)
61 struct kvm_vcpu *vcpu;
62 struct kvm_run *run;
63 struct kvm_vcpu_events events;
64 struct ucall uc;
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));
74 if (has_vmx) {
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);
80 } else {
81 vm_vaddr_t svm_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);
89 run = vcpu->run;
90 vcpu_run(vcpu);
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");
108 vcpu_run(vcpu);
111 if (has_svm) {
112 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);
113 } else {
114 switch (get_ucall(vcpu, &uc)) {
115 case UCALL_DONE:
116 break;
117 case UCALL_ABORT:
118 REPORT_GUEST_ASSERT(uc);
119 default:
120 TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
123 return 0;