Skip to content

Commit f138258

Browse files
committed
KVM: selftests: Verify KVM preserves userspace writes to "durable" MSRs
Assert that KVM provides "read what you wrote" semantics for all "durable" MSRs (for lack of a better name). The extra coverage is cheap from a runtime performance perspective, and verifying the behavior in the common helper avoids gratuitous copy+paste in individual tests. Note, this affects all tests that set MSRs from userspace! Link: https://lore.kernel.org/r/20230311004618.920745-13-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 22234c2 commit f138258

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

tools/testing/selftests/kvm/include/x86_64/processor.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,12 +945,27 @@ do { \
945945
} \
946946
} while (0)
947947

948+
/*
949+
* Returns true if KVM should return the last written value when reading an MSR
950+
* from userspace, e.g. the MSR isn't a command MSR, doesn't emulate state that
951+
* is changing, etc. This is NOT an exhaustive list! The intent is to filter
952+
* out MSRs that are not durable _and_ that a selftest wants to write.
953+
*/
954+
static inline bool is_durable_msr(uint32_t msr)
955+
{
956+
return msr != MSR_IA32_TSC;
957+
}
958+
948959
#define vcpu_set_msr(vcpu, msr, val) \
949960
do { \
950-
uint64_t v = val; \
961+
uint64_t r, v = val; \
951962
\
952963
TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) == 1, \
953964
"KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \
965+
if (!is_durable_msr(msr)) \
966+
break; \
967+
r = vcpu_get_msr(vcpu, msr); \
968+
TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\
954969
} while (0)
955970

956971
void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);

0 commit comments

Comments
 (0)