Skip to content

Commit ab3b6a7

Browse files
committed
KVM: selftests: Add a forced emulation variation of KVM_ASM_SAFE()
Add KVM_ASM_SAFE_FEP() to allow forcing emulation on an instruction that might fault. Note, KVM skips RIP past the FEP prefix before injecting an exception, i.e. the fixup needs to be on the instruction itself. Do not check for FEP support, that is firmly the responsibility of whatever code wants to use KVM_ASM_SAFE_FEP(). Sadly, chaining variadic arguments that contain commas doesn't work, thus the unfortunate amount of copy+paste. Tested-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Sean Christopherson <[email protected]>
1 parent cd34fd8 commit ab3b6a7

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,16 +1154,19 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector,
11541154
* r9 = exception vector (non-zero)
11551155
* r10 = error code
11561156
*/
1157-
#define KVM_ASM_SAFE(insn) \
1157+
#define __KVM_ASM_SAFE(insn, fep) \
11581158
"mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \
11591159
"lea 1f(%%rip), %%r10\n\t" \
11601160
"lea 2f(%%rip), %%r11\n\t" \
1161-
"1: " insn "\n\t" \
1161+
fep "1: " insn "\n\t" \
11621162
"xor %%r9, %%r9\n\t" \
11631163
"2:\n\t" \
11641164
"mov %%r9b, %[vector]\n\t" \
11651165
"mov %%r10, %[error_code]\n\t"
11661166

1167+
#define KVM_ASM_SAFE(insn) __KVM_ASM_SAFE(insn, "")
1168+
#define KVM_ASM_SAFE_FEP(insn) __KVM_ASM_SAFE(insn, KVM_FEP)
1169+
11671170
#define KVM_ASM_SAFE_OUTPUTS(v, ec) [vector] "=qm"(v), [error_code] "=rm"(ec)
11681171
#define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11"
11691172

@@ -1190,6 +1193,29 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector,
11901193
vector; \
11911194
})
11921195

1196+
#define kvm_asm_safe_fep(insn, inputs...) \
1197+
({ \
1198+
uint64_t ign_error_code; \
1199+
uint8_t vector; \
1200+
\
1201+
asm volatile(KVM_ASM_SAFE(insn) \
1202+
: KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \
1203+
: inputs \
1204+
: KVM_ASM_SAFE_CLOBBERS); \
1205+
vector; \
1206+
})
1207+
1208+
#define kvm_asm_safe_ec_fep(insn, error_code, inputs...) \
1209+
({ \
1210+
uint8_t vector; \
1211+
\
1212+
asm volatile(KVM_ASM_SAFE_FEP(insn) \
1213+
: KVM_ASM_SAFE_OUTPUTS(vector, error_code) \
1214+
: inputs \
1215+
: KVM_ASM_SAFE_CLOBBERS); \
1216+
vector; \
1217+
})
1218+
11931219
static inline uint8_t rdmsr_safe(uint32_t msr, uint64_t *val)
11941220
{
11951221
uint64_t error_code;

0 commit comments

Comments
 (0)