summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/kvm/x86/svm_nested_efer_test.c
blob: fa0d76a9c390bc84ff7e352163741dafe33047a4 (plain) (blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Tests for KVM's handling of EFER bits whose behavior is tied to nested SVM.
 *
 * Copyright (C) 2026, Google LLC.
 */
#include "test_util.h"
#include "kvm_util.h"
#include "processor.h"
#include "svm_util.h"
#include "kselftest.h"

static bool l2_ran;

static void l2_clear_efer_svme(void)
{
	u64 efer = rdmsr(MSR_EFER);

	/* generic_svm_setup() initializes EFER_SVME set for L2 */
	GUEST_ASSERT(efer & EFER_SVME);
	wrmsr(MSR_EFER, efer & ~EFER_SVME);

	/* Unreachable, L1 should be shutdown */
	GUEST_ASSERT(0);
}

static void l1_clear_efer_svme(struct svm_test_data *svm)
{
	generic_svm_setup(svm, l2_clear_efer_svme);
	run_guest(svm->vmcb, svm->vmcb_gpa);

	/* Unreachable, L1 should be shutdown */
	GUEST_ASSERT(0);
}

static void l2_lmsle(void)
{
	GUEST_ASSERT(rdmsr(MSR_EFER) & EFER_LMSLE);
	l2_ran = true;
	vmmcall();
}

static void l1_lmsle(struct svm_test_data *svm)
{
	bool lmsle_mbz = this_cpu_has(X86_FEATURE_EFER_LMSLE_MBZ);
	struct vmcb *vmcb = svm->vmcb;
	u64 efer = rdmsr(MSR_EFER);

	/*
	 * Selftests' vCPUs are created with EFER.LMSLE clear; the sub-tests
	 * below need to start from a clean slate.
	 */
	GUEST_ASSERT(!(efer & EFER_LMSLE));
	GUEST_ASSERT(!l2_ran);

	/*
	 * Per the APM, if EFER_LMSLE_MBZ is enumerated in CPUID, "64-bit mode
	 * segment limit checking is not supported and attempting to set
	 * EFER.LMSLE = 1 causes a #GP exception".
	 */
	if (lmsle_mbz) {
		GUEST_ASSERT_EQ(wrmsr_safe(MSR_EFER, efer | EFER_LMSLE), GP_VECTOR);
		GUEST_ASSERT(!(rdmsr(MSR_EFER) & EFER_LMSLE));
	} else {
		GUEST_ASSERT_EQ(wrmsr_safe(MSR_EFER, efer | EFER_LMSLE), 0);
		GUEST_ASSERT(rdmsr(MSR_EFER) & EFER_LMSLE);

		/*
		 * Restore EFER so that generic_svm_setup() doesn't propagate
		 * EFER.LMSLE into vmcb12 on its own, i.e. so that the VMRUN
		 * sub-test actually tests what it thinks it's testing.
		 */
		wrmsr(MSR_EFER, efer);
	}

	/*
	 * VMRUN's consistency checks reject "any MBZ bit of EFER", i.e. a
	 * vmcb12 with EFER.LMSLE set must generate VMEXIT_INVALID when the
	 * defeature is enumerated.
	 */
	generic_svm_setup(svm, l2_lmsle);
	vmcb->save.efer |= EFER_LMSLE;
	run_guest(vmcb, svm->vmcb_gpa);

	if (lmsle_mbz) {
		GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_ERR);
		GUEST_ASSERT(!l2_ran);
	} else {
		GUEST_ASSERT_EQ(vmcb->control.exit_code, SVM_EXIT_VMMCALL);
		GUEST_ASSERT(l2_ran);
		GUEST_ASSERT(vmcb->save.efer & EFER_LMSLE);
	}

	GUEST_DONE();
}

static struct kvm_vcpu *create_l1_vcpu(struct kvm_vm **vm, void *l1_guest_code)
{
	struct kvm_vcpu *vcpu;
	gva_t svm_gva;

	*vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code);

	vcpu_alloc_svm(*vm, &svm_gva);
	vcpu_args_set(vcpu, 1, svm_gva);

	return vcpu;
}

static void test_enumeration(void)
{
	bool lmsle_mbz;

	/*
	 * EFER_LMSLE_MBZ, CPUID.80000008H:EBX[bit 20], is a "defeature" bit,
	 * i.e. is set when the CPU does *not* support long mode segment
	 * limits.  KVM enumerates the defeature if and only if KVM refuses to
	 * set EFER.LMSLE, i.e. if the CPU doesn't support LMSLE, or if KVM
	 * doesn't support nested SVM.  Derive the expectation from raw CPUID
	 * and kvm_amd's "nested" module param rather than from
	 * kvm_cpu_has(X86_FEATURE_SVM), so that the assertion doesn't simply
	 * compare KVM's enumeration to itself.
	 */
	lmsle_mbz = this_cpu_has(X86_FEATURE_EFER_LMSLE_MBZ) ||
		    !this_cpu_has(X86_FEATURE_SVM) ||
		    !kvm_is_nested_virtualization_enabled();

	TEST_ASSERT_EQ(kvm_cpu_has(X86_FEATURE_EFER_LMSLE_MBZ), lmsle_mbz);

	ksft_test_result_pass("KVM enumerates EFER_LMSLE_MBZ=%d\n", lmsle_mbz);
}

static void test_clear_efer_svme(void)
{
	struct kvm_vcpu *vcpu;
	struct kvm_vm *vm;

	vcpu = create_l1_vcpu(&vm, l1_clear_efer_svme);

	vcpu_run(vcpu);
	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_SHUTDOWN);

	kvm_vm_free(vm);
	ksft_test_result_pass("L2 clearing EFER.SVME shuts down L1\n");
}

static void test_lmsle(bool lmsle_mbz)
{
	struct kvm_vcpu *vcpu;
	struct kvm_vm *vm;
	struct ucall uc;

	vcpu = create_l1_vcpu(&vm, l1_lmsle);

	vcpu_set_or_clear_cpuid_feature(vcpu, X86_FEATURE_EFER_LMSLE_MBZ,
					lmsle_mbz);

	vcpu_run(vcpu);
	TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);

	switch (get_ucall(vcpu, &uc)) {
	case UCALL_ABORT:
		REPORT_GUEST_ASSERT(uc);
	case UCALL_DONE:
		break;
	default:
		TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
	}

	kvm_vm_free(vm);
	ksft_test_result_pass("Guest EFER_LMSLE_MBZ=%d\n", lmsle_mbz);
}

static void test_host_initiated_lmsle(void)
{
	struct kvm_vcpu *vcpu;
	struct kvm_vm *vm;
	u64 efer;

	vm = vm_create_with_one_vcpu(&vcpu, NULL);
	vcpu_set_cpuid_feature(vcpu, X86_FEATURE_EFER_LMSLE_MBZ);

	/*
	 * EFER_LMSLE_MBZ is a guest CPUID consistency check, not a host
	 * capability, i.e. must not be enforced against host-initiated writes,
	 * so that userspace can set MSRs before it sets guest CPUID.
	 */
	efer = vcpu_get_msr(vcpu, MSR_EFER);
	TEST_ASSERT(!(efer & EFER_LMSLE), "EFER.LMSLE unexpectedly set");

	vcpu_set_msr(vcpu, MSR_EFER, efer | EFER_LMSLE);
	TEST_ASSERT_EQ(vcpu_get_msr(vcpu, MSR_EFER), efer | EFER_LMSLE);

	kvm_vm_free(vm);
	ksft_test_result_pass("Host-initiated EFER.LMSLE=1 is allowed\n");
}

static void test_sregs_lmsle(void)
{
	struct kvm_vcpu *vcpu;
	struct kvm_sregs sregs;
	struct kvm_vm *vm;
	int rc;

	vm = vm_create_with_one_vcpu(&vcpu, NULL);
	vcpu_set_cpuid_feature(vcpu, X86_FEATURE_EFER_LMSLE_MBZ);

	/*
	 * Unlike KVM_SET_MSRS, KVM_SET_SREGS runs the full set of guest CPUID
	 * checks, i.e. rejects EFER.LMSLE even though it's host-initiated.
	 */
	vcpu_sregs_get(vcpu, &sregs);
	TEST_ASSERT(!(sregs.efer & EFER_LMSLE), "EFER.LMSLE unexpectedly set");

	sregs.efer |= EFER_LMSLE;
	rc = _vcpu_sregs_set(vcpu, &sregs);
	TEST_ASSERT(rc, "KVM allowed EFER.LMSLE with EFER_LMSLE_MBZ set");

	kvm_vm_free(vm);
	ksft_test_result_pass("KVM_SET_SREGS rejects EFER.LMSLE=1\n");
}

int main(int argc, char *argv[])
{
	bool has_nested_svm, has_lmsle;

	ksft_print_header();
	ksft_set_plan(6);

	test_enumeration();

	/*
	 * The sub-tests below need to actually run a nested guest, and the
	 * EFER.LMSLE sub-tests additionally need KVM to allow EFER.LMSLE.
	 * It's KVM's view of the world, not raw CPUID, that dictates whether
	 * EFER.LMSLE is allowed, i.e. whether the defeature is emulated.
	 */
	has_nested_svm = kvm_cpu_has(X86_FEATURE_SVM);
	has_lmsle = has_nested_svm &&
		    !kvm_cpu_has(X86_FEATURE_EFER_LMSLE_MBZ);

	if (!has_nested_svm)
		ksft_print_msg("Nested SVM unsupported\n");
	else if (!has_lmsle)
		ksft_print_msg("KVM doesn't support EFER.LMSLE\n");

	if (has_nested_svm) {
		test_clear_efer_svme();
		test_lmsle(true);
	} else {
		ksft_test_result_skip("L2 clearing EFER.SVME shuts down L1\n");
		ksft_test_result_skip("Guest EFER_LMSLE_MBZ=1\n");
	}

	if (has_lmsle) {
		test_lmsle(false);
		test_host_initiated_lmsle();
		test_sregs_lmsle();
	} else {
		ksft_test_result_skip("Guest EFER_LMSLE_MBZ=0\n");
		ksft_test_result_skip("Host-initiated EFER.LMSLE=1 is allowed\n");
		ksft_test_result_skip("KVM_SET_SREGS rejects EFER.LMSLE=1\n");
	}

	ksft_finished();
}