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 GUEST_ASSERT(!vmlaunch());
54 GUEST_ASSERT(vmptrstz() == vmx_pages
->enlightened_vmcs_gpa
);
56 GUEST_ASSERT(!vmresume());
57 GUEST_ASSERT(vmreadz(VM_EXIT_REASON
) == EXIT_REASON_VMCALL
);
61 void guest_code(struct vmx_pages
*vmx_pages
)
67 l1_guest_code(vmx_pages
);
72 int main(int argc
, char *argv
[])
74 vm_vaddr_t vmx_pages_gva
= 0;
76 struct kvm_regs regs1
, regs2
;
79 struct kvm_x86_state
*state
;
84 vm
= vm_create_default(VCPU_ID
, 0, guest_code
);
86 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
88 if (!kvm_check_cap(KVM_CAP_NESTED_STATE
) ||
89 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS
)) {
90 printf("capabilities not available, skipping test\n");
94 vcpu_enable_evmcs(vm
, VCPU_ID
);
96 run
= vcpu_state(vm
, VCPU_ID
);
98 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
100 vcpu_alloc_vmx(vm
, &vmx_pages_gva
);
101 vcpu_args_set(vm
, VCPU_ID
, 1, vmx_pages_gva
);
103 for (stage
= 1;; stage
++) {
104 _vcpu_run(vm
, VCPU_ID
);
105 TEST_ASSERT(run
->exit_reason
== KVM_EXIT_IO
,
106 "Stage %d: unexpected exit reason: %u (%s),\n",
107 stage
, run
->exit_reason
,
108 exit_reason_str(run
->exit_reason
));
110 switch (get_ucall(vm
, VCPU_ID
, &uc
)) {
112 TEST_ASSERT(false, "%s at %s:%d", (const char *)uc
.args
[0],
113 __FILE__
, uc
.args
[1]);
120 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc
.cmd
);
123 /* UCALL_SYNC is handled here. */
124 TEST_ASSERT(!strcmp((const char *)uc
.args
[0], "hello") &&
125 uc
.args
[1] == stage
, "Unexpected register values vmexit #%lx, got %lx",
126 stage
, (ulong
)uc
.args
[1]);
128 state
= vcpu_save_state(vm
, VCPU_ID
);
129 memset(®s1
, 0, sizeof(regs1
));
130 vcpu_regs_get(vm
, VCPU_ID
, ®s1
);
134 /* Restore state in a new VM. */
135 kvm_vm_restart(vm
, O_RDWR
);
136 vm_vcpu_add(vm
, VCPU_ID
);
137 vcpu_set_cpuid(vm
, VCPU_ID
, kvm_get_supported_cpuid());
138 vcpu_enable_evmcs(vm
, VCPU_ID
);
139 vcpu_load_state(vm
, VCPU_ID
, state
);
140 run
= vcpu_state(vm
, VCPU_ID
);
143 memset(®s2
, 0, sizeof(regs2
));
144 vcpu_regs_get(vm
, VCPU_ID
, ®s2
);
145 TEST_ASSERT(!memcmp(®s1
, ®s2
, sizeof(regs2
)),
146 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
147 (ulong
) regs2
.rdi
, (ulong
) regs2
.rsi
);