Skip to content

Commit 22234c2

Browse files
committed
KVM: selftests: Print out failing MSR and value in vcpu_set_msr()
Reimplement vcpu_set_msr() as a macro and pretty print the failing MSR (when possible) and the value if KVM_SET_MSRS fails instead of using the using the standard KVM_IOCTL_ERROR(). KVM_SET_MSRS is somewhat odd in that it returns the index of the last successful write, i.e. will be '0' on failure barring an entirely different KVM bug. And for writing MSRs, the MSR being written and the value being written are almost always relevant to the failure, i.e. just saying "failed!" doesn't help debug. Place the string goo in a separate macro in anticipation of using it to further expand MSR testing. Link: https://lore.kernel.org/r/20230311004618.920745-12-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent b1b7056 commit 22234c2

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -928,14 +928,30 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu,
928928
uint64_t vcpu_get_msr(struct kvm_vcpu *vcpu, uint64_t msr_index);
929929
int _vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index, uint64_t msr_value);
930930

931-
static inline void vcpu_set_msr(struct kvm_vcpu *vcpu, uint64_t msr_index,
932-
uint64_t msr_value)
933-
{
934-
int r = _vcpu_set_msr(vcpu, msr_index, msr_value);
935-
936-
TEST_ASSERT(r == 1, KVM_IOCTL_ERROR(KVM_SET_MSRS, r));
937-
}
938-
931+
/*
932+
* Assert on an MSR access(es) and pretty print the MSR name when possible.
933+
* Note, the caller provides the stringified name so that the name of macro is
934+
* printed, not the value the macro resolves to (due to macro expansion).
935+
*/
936+
#define TEST_ASSERT_MSR(cond, fmt, msr, str, args...) \
937+
do { \
938+
if (__builtin_constant_p(msr)) { \
939+
TEST_ASSERT(cond, fmt, str, args); \
940+
} else if (!(cond)) { \
941+
char buf[16]; \
942+
\
943+
snprintf(buf, sizeof(buf), "MSR 0x%x", msr); \
944+
TEST_ASSERT(cond, fmt, buf, args); \
945+
} \
946+
} while (0)
947+
948+
#define vcpu_set_msr(vcpu, msr, val) \
949+
do { \
950+
uint64_t v = val; \
951+
\
952+
TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) == 1, \
953+
"KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \
954+
} while (0)
939955

940956
void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);
941957
bool vm_is_unrestricted_guest(struct kvm_vm *vm);

0 commit comments

Comments
 (0)