1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2026, Google LLC.
*/
#include "kvm_util.h"
#include "vmx.h"
#include "svm_util.h"
#include "kselftest.h"
#include "kvm_test_harness.h"
#include "test_util.h"
#define L2_GUEST_STACK_SIZE 64
#define SYNC_GP 101
#define SYNC_L2_STARTED 102
static unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
static void guest_gp_handler(struct ex_regs *regs)
{
GUEST_SYNC(SYNC_GP);
}
static void l2_code(void)
{
GUEST_SYNC(SYNC_L2_STARTED);
vmcall();
}
static void l1_vmrun(struct svm_test_data *svm, gpa_t gpa)
{
generic_svm_setup(svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
asm volatile ("vmrun %[gpa]" : : [gpa] "a" (gpa) : "memory");
}
static void l1_vmload(struct svm_test_data *svm, gpa_t gpa)
{
generic_svm_setup(svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
asm volatile ("vmload %[gpa]" : : [gpa] "a" (gpa) : "memory");
}
static void l1_vmsave(struct svm_test_data *svm, gpa_t gpa)
{
generic_svm_setup(svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
asm volatile ("vmsave %[gpa]" : : [gpa] "a" (gpa) : "memory");
}
static void l1_vmexit(struct svm_test_data *svm, gpa_t gpa)
{
generic_svm_setup(svm, l2_code, &l2_guest_stack[L2_GUEST_STACK_SIZE]);
run_guest(svm->vmcb, svm->vmcb_gpa);
GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL);
GUEST_DONE();
}
static u64 unmappable_gpa(struct kvm_vcpu *vcpu)
{
struct userspace_mem_region *region;
u64 region_gpa_end, vm_gpa_end = 0;
int i;
hash_for_each(vcpu->vm->regions.slot_hash, i, region, slot_node) {
region_gpa_end = region->region.guest_phys_addr + region->region.memory_size;
vm_gpa_end = max(vm_gpa_end, region_gpa_end);
}
return vm_gpa_end;
}
static void test_invalid_vmcb12(struct kvm_vcpu *vcpu)
{
gva_t nested_gva = 0;
struct ucall uc;
vm_install_exception_handler(vcpu->vm, GP_VECTOR, guest_gp_handler);
vcpu_alloc_svm(vcpu->vm, &nested_gva);
vcpu_args_set(vcpu, 2, nested_gva, -1ULL);
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC);
TEST_ASSERT_EQ(uc.args[1], SYNC_GP);
}
static void test_unmappable_vmcb12(struct kvm_vcpu *vcpu)
{
gva_t nested_gva = 0;
vcpu_alloc_svm(vcpu->vm, &nested_gva);
vcpu_args_set(vcpu, 2, nested_gva, unmappable_gpa(vcpu));
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_INTERNAL_ERROR);
TEST_ASSERT_EQ(vcpu->run->emulation_failure.suberror, KVM_INTERNAL_ERROR_EMULATION);
}
static void test_unmappable_vmcb12_vmexit(struct kvm_vcpu *vcpu)
{
struct kvm_x86_state *state;
gva_t nested_gva = 0;
struct ucall uc;
/*
* Enter L2 (with a legit vmcb12 GPA), then overwrite vmcb12 GPA with an
* unmappable GPA. KVM will fail to map vmcb12 on nested VM-Exit and
* cause a shutdown.
*/
vcpu_alloc_svm(vcpu->vm, &nested_gva);
vcpu_args_set(vcpu, 2, nested_gva, unmappable_gpa(vcpu));
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
TEST_ASSERT_EQ(get_ucall(vcpu, &uc), UCALL_SYNC);
TEST_ASSERT_EQ(uc.args[1], SYNC_L2_STARTED);
state = vcpu_save_state(vcpu);
state->nested.hdr.svm.vmcb_pa = unmappable_gpa(vcpu);
vcpu_load_state(vcpu, state);
vcpu_run(vcpu);
TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);
kvm_x86_state_cleanup(state);
}
KVM_ONE_VCPU_TEST_SUITE(vmcb12_gpa);
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmrun_invalid, l1_vmrun)
{
test_invalid_vmcb12(vcpu);
}
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmload_invalid, l1_vmload)
{
test_invalid_vmcb12(vcpu);
}
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmsave_invalid, l1_vmsave)
{
test_invalid_vmcb12(vcpu);
}
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmrun_unmappable, l1_vmrun)
{
test_unmappable_vmcb12(vcpu);
}
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmload_unmappable, l1_vmload)
{
test_unmappable_vmcb12(vcpu);
}
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmsave_unmappable, l1_vmsave)
{
test_unmappable_vmcb12(vcpu);
}
/*
* Invalid vmcb12_gpa cannot be test for #VMEXIT as KVM_SET_NESTED_STATE will
* reject it.
*/
KVM_ONE_VCPU_TEST(vmcb12_gpa, vmexit_unmappable, l1_vmexit)
{
test_unmappable_vmcb12_vmexit(vcpu);
}
int main(int argc, char *argv[])
{
TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SVM));
return test_harness_run(argc, argv);
}
|