|
25 | 25 | #define VMCS12_REVISION 0x11e57ed0 |
26 | 26 | #define VCPU_ID 5 |
27 | 27 |
|
| 28 | +bool have_evmcs; |
| 29 | + |
28 | 30 | void test_nested_state(struct kvm_vm *vm, struct kvm_nested_state *state) |
29 | 31 | { |
30 | 32 | vcpu_nested_state_set(vm, VCPU_ID, state, false); |
@@ -75,8 +77,9 @@ void set_default_vmx_state(struct kvm_nested_state *state, int size) |
75 | 77 | { |
76 | 78 | memset(state, 0, size); |
77 | 79 | state->flags = KVM_STATE_NESTED_GUEST_MODE | |
78 | | - KVM_STATE_NESTED_RUN_PENDING | |
79 | | - KVM_STATE_NESTED_EVMCS; |
| 80 | + KVM_STATE_NESTED_RUN_PENDING; |
| 81 | + if (have_evmcs) |
| 82 | + state->flags |= KVM_STATE_NESTED_EVMCS; |
80 | 83 | state->format = 0; |
81 | 84 | state->size = size; |
82 | 85 | state->hdr.vmx.vmxon_pa = 0x1000; |
@@ -126,13 +129,19 @@ void test_vmx_nested_state(struct kvm_vm *vm) |
126 | 129 | /* |
127 | 130 | * Setting vmxon_pa == -1ull and vmcs_pa == -1ull exits early without |
128 | 131 | * setting the nested state but flags other than eVMCS must be clear. |
| 132 | + * The eVMCS flag can be set if the enlightened VMCS capability has |
| 133 | + * been enabled. |
129 | 134 | */ |
130 | 135 | set_default_vmx_state(state, state_sz); |
131 | 136 | state->hdr.vmx.vmxon_pa = -1ull; |
132 | 137 | state->hdr.vmx.vmcs12_pa = -1ull; |
133 | 138 | test_nested_state_expect_einval(vm, state); |
134 | 139 |
|
135 | | - state->flags = KVM_STATE_NESTED_EVMCS; |
| 140 | + state->flags &= KVM_STATE_NESTED_EVMCS; |
| 141 | + if (have_evmcs) { |
| 142 | + test_nested_state_expect_einval(vm, state); |
| 143 | + vcpu_enable_evmcs(vm, VCPU_ID); |
| 144 | + } |
136 | 145 | test_nested_state(vm, state); |
137 | 146 |
|
138 | 147 | /* It is invalid to have vmxon_pa == -1ull and SMM flags non-zero. */ |
@@ -217,6 +226,8 @@ int main(int argc, char *argv[]) |
217 | 226 | struct kvm_nested_state state; |
218 | 227 | struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1); |
219 | 228 |
|
| 229 | + have_evmcs = kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS); |
| 230 | + |
220 | 231 | if (!kvm_check_cap(KVM_CAP_NESTED_STATE)) { |
221 | 232 | printf("KVM_CAP_NESTED_STATE not available, skipping test\n"); |
222 | 233 | exit(KSFT_SKIP); |
|
0 commit comments