1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018, Red Hat, Inc.
5 * Tests for Enlightened VMCS, including nested guest state.
7 #define _GNU_SOURCE /* for program_invocation_short_name */
12 #include <sys/ioctl.h>
14 #include "test_util.h"
22 void l2_guest_code(void)
28 /* Done, exit to L1 and never come back. */
32 void l1_guest_code(struct vmx_pages
*vmx_pages
)
34 #define L2_GUEST_STACK_SIZE 64
35 unsigned long l2_guest_stack
[L2_GUEST_STACK_SIZE
];
37 enable_vp_assist(vmx_pages
->vp_assist_gpa
, vmx_pages
->vp_assist
);
39 GUEST_ASSERT(vmx_pages
->vmcs_gpa
);
40 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages
));
42 GUEST_ASSERT(load_vmcs(vmx_pages
));
43 GUEST_ASSERT(vmptrstz() == vmx_pages
->enlightened_vmcs_gpa
);
46 GUEST_ASSERT(vmptrstz() == vmx_pages
->enlightened_vmcs_gpa
);
48 prepare_vmcs(vmx_pages
, l2_guest_code
,
49 &l2_guest_stack
[L2_GUEST_STACK_SIZE
]);
52 GUEST_ASSERT(vmptrstz() == vmx_pages
->enlightened_vmcs_gpa
);
53 current_evmcs
->revision_id
= -1u;
54 GUEST_ASSERT(vmlaunch());
55 current_evmcs
->revision_id
= EVMCS_VERSION
;
58 GUEST_ASSERT(!vmlaunch());
59 GUEST_ASSERT(vmptrstz() == vmx_pages
->enlightened_vmcs_gpa
);
61 GUEST_ASSERT(!vmresume());
62 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
66 void guest_code(struct vmx_pages
*vmx_pages
)
72 l1_guest_code(vmx_pages
);
76 /* Try enlightened vmptrld with an incorrect GPA */
77 evmcs_vmptrld(0xdeadbeef, vmx_pages
->enlightened_vmcs
);
78 GUEST_ASSERT(vmlaunch());
81 int main(int argc
, char *argv
[])
83 vm_vaddr_t vmx_pages_gva
= 0;
85 struct kvm_regs regs1
, regs2
;
88 struct kvm_x86_state
*state
;
93 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
95 if (!nested_vmx_supported() ||
96 !kvm_check_cap(KVM_CAP_NESTED_STATE
) ||
97 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS
)) {
98 print_skip("Enlightened VMCS is unsupported");
102 vcpu_enable_evmcs(vm
, VCPU_ID
);
104 run
= vcpu_state(vm
, VCPU_ID
);
106 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
108 vcpu_alloc_vmx(vm
, &vmx_pages_gva
);
109 vcpu_args_set(vm
, VCPU_ID
, 1, vmx_pages_gva
);
111 for (stage
= 1;; stage
++) {
112 _vcpu_run(vm
, VCPU_ID
);
113 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
114 "Stage %d: unexpected exit reason: %u (%s),\n",
115 stage
, run
->exit_reason
,
116 exit_reason_str(run
->exit_reason
));
118 switch (get_ucall(vm
, VCPU_ID
, &uc
)) {
120 TEST_FAIL("%s at %s:%ld", (const char *)uc
.args
[0],
121 __FILE__
, uc
.args
[1]);
128 TEST_FAIL("Unknown ucall %lu", uc
.cmd
);
131 /* UCALL_SYNC is handled here. */
132 TEST_ASSERT(!strcmp((const char *)uc
.args
[0], "hello") &&
133 uc
.args
[1] == stage
, "Stage %d: Unexpected register values vmexit, got %lx",
134 stage
, (ulong
)uc
.args
[1]);
136 state
= vcpu_save_state(vm
, VCPU_ID
);
137 memset(®s1
, 0, sizeof(regs1
));
138 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
142 /* Restore state in a new VM. */
143 kvm_vm_restart(vm
, O_RDWR
);
144 vm_vcpu_add(vm
, VCPU_ID
);
145 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
146 vcpu_enable_evmcs(vm
, VCPU_ID
);
147 vcpu_load_state(vm
, VCPU_ID
, state
);
148 run
= vcpu_state(vm
, VCPU_ID
);
151 memset(®s2
, 0, sizeof(regs2
));
152 vcpu_regs_get(vm
, VCPU_ID
, ®s2
);
153 TEST_ASSERT(!memcmp(®s1
, ®s2
, sizeof(regs2
)),
154 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
155 (ulong
) regs2
.rdi
, (ulong
) regs2
.rsi
);
159 _vcpu_run(vm
, VCPU_ID
);
160 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_SHUTDOWN
,
161 "Unexpected successful VMEnter with invalid eVMCS pointer!");