Skip to content

Commit a8b9cca

Browse files
committed
KVM: selftests: Extend MSRs test to validate vCPUs without supported features
Add a third vCPUs to the MSRs test that runs with all features disabled in the vCPU's CPUID model, to verify that KVM does the right thing with respect to emulating accesses to MSRs that shouldn't exist. Use the same VM to verify that KVM is honoring the vCPU model, e.g. isn't looking at per-VM state when emulating MSR accesses. Link: https://lore.kernel.org/r/20250919223258.1604852-48-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 27c4135 commit a8b9cca

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

tools/testing/selftests/kvm/x86/msrs_test.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,17 @@ static void test_msrs(void)
303303
MSR_TEST(MSR_IA32_PL3_SSP, canonical_val, canonical_val | 1, SHSTK),
304304
};
305305

306+
const struct kvm_x86_cpu_feature feat_none = X86_FEATURE_NONE;
307+
const struct kvm_x86_cpu_feature feat_lm = X86_FEATURE_LM;
308+
306309
/*
307-
* Create two vCPUs, but run them on the same task, to validate KVM's
310+
* Create three vCPUs, but run them on the same task, to validate KVM's
308311
* context switching of MSR state. Don't pin the task to a pCPU to
309-
* also validate KVM's handling of cross-pCPU migration.
312+
* also validate KVM's handling of cross-pCPU migration. Use the full
313+
* set of features for the first two vCPUs, but clear all features in
314+
* third vCPU in order to test both positive and negative paths.
310315
*/
311-
const int NR_VCPUS = 2;
316+
const int NR_VCPUS = 3;
312317
struct kvm_vcpu *vcpus[NR_VCPUS];
313318
struct kvm_vm *vm;
314319

@@ -323,6 +328,23 @@ static void test_msrs(void)
323328
sync_global_to_guest(vm, msrs);
324329
sync_global_to_guest(vm, ignore_unsupported_msrs);
325330

331+
/*
332+
* Clear features in the "unsupported features" vCPU. This needs to be
333+
* done before the first vCPU run as KVM's ABI is that guest CPUID is
334+
* immutable once the vCPU has been run.
335+
*/
336+
for (idx = 0; idx < ARRAY_SIZE(__msrs); idx++) {
337+
/*
338+
* Don't clear LM; selftests are 64-bit only, and KVM doesn't
339+
* honor LM=0 for MSRs that are supposed to exist if and only
340+
* if the vCPU is a 64-bit model. Ditto for NONE; clearing a
341+
* fake feature flag will result in false failures.
342+
*/
343+
if (memcmp(&msrs[idx].feature, &feat_lm, sizeof(feat_lm)) &&
344+
memcmp(&msrs[idx].feature, &feat_none, sizeof(feat_none)))
345+
vcpu_clear_cpuid_feature(vcpus[2], msrs[idx].feature);
346+
}
347+
326348
for (idx = 0; idx < ARRAY_SIZE(__msrs); idx++) {
327349
sync_global_to_guest(vm, idx);
328350

0 commit comments

Comments
 (0)