Skip to content

Commit b5e66df

Browse files
committed
KVM: selftests: Add helpers for safe and safe+forced RDMSR, RDPMC, and XGETBV
Add helpers for safe and safe-with-forced-emulations versions of RDMSR, RDPMC, and XGETBV. Use macro shenanigans to eliminate the rather large amount of boilerplate needed to get values in and out of registers. Tested-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent ab3b6a7 commit b5e66df

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,20 +1216,34 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector,
12161216
vector; \
12171217
})
12181218

1219-
static inline uint8_t rdmsr_safe(uint32_t msr, uint64_t *val)
1220-
{
1221-
uint64_t error_code;
1222-
uint8_t vector;
1223-
uint32_t a, d;
1219+
#define BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \
1220+
static inline uint8_t insn##_safe ##_fep(uint32_t idx, uint64_t *val) \
1221+
{ \
1222+
uint64_t error_code; \
1223+
uint8_t vector; \
1224+
uint32_t a, d; \
1225+
\
1226+
asm volatile(KVM_ASM_SAFE##_FEP(#insn) \
1227+
: "=a"(a), "=d"(d), \
1228+
KVM_ASM_SAFE_OUTPUTS(vector, error_code) \
1229+
: "c"(idx) \
1230+
: KVM_ASM_SAFE_CLOBBERS); \
1231+
\
1232+
*val = (uint64_t)a | ((uint64_t)d << 32); \
1233+
return vector; \
1234+
}
12241235

1225-
asm volatile(KVM_ASM_SAFE("rdmsr")
1226-
: "=a"(a), "=d"(d), KVM_ASM_SAFE_OUTPUTS(vector, error_code)
1227-
: "c"(msr)
1228-
: KVM_ASM_SAFE_CLOBBERS);
1236+
/*
1237+
* Generate {insn}_safe() and {insn}_safe_fep() helpers for instructions that
1238+
* use ECX as in input index, and EDX:EAX as a 64-bit output.
1239+
*/
1240+
#define BUILD_READ_U64_SAFE_HELPERS(insn) \
1241+
BUILD_READ_U64_SAFE_HELPER(insn, , ) \
1242+
BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \
12291243

1230-
*val = (uint64_t)a | ((uint64_t)d << 32);
1231-
return vector;
1232-
}
1244+
BUILD_READ_U64_SAFE_HELPERS(rdmsr)
1245+
BUILD_READ_U64_SAFE_HELPERS(rdpmc)
1246+
BUILD_READ_U64_SAFE_HELPERS(xgetbv)
12331247

12341248
static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val)
12351249
{

0 commit comments

Comments
 (0)