KVM: selftests: Fastops DIV testcases
Sean Christopherson
maintainercover KVM: selftests: Fastops DIV testcases
Add DIV and IDIV support to the so called fastops test to add coverage for #DE (the only exception that can occur during fastops execution), which was broken due to a typo in PeterZ's overhaul[*].
I meant to post this back in July and forgot...
[*] https://lore.kernel.org/all/[email protected]
Sean Christopherson (4):
KVM: selftests: Add support for #DE exception fixup
KVM: selftests: Add coverage for 'b' (byte) sized fastops emulation
KVM: selftests: Dedup the gnarly constraints of the fastops tests
(more macros!)
KVM: selftests: Add support for DIV and IDIV in the fastops test
.../selftests/kvm/include/x86/processor.h | 6 ++ .../testing/selftests/kvm/lib/x86/processor.c | 2 +- .../testing/selftests/kvm/x86/fastops_test.c | 82 ++++++++++++++----- 3 files changed, 70 insertions(+), 20 deletions(-)
base-commit: 33f843444e28920d6e624c6c24637b4bb5d3c8de
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Sean Christopherson
maintainerPATCH 1/4 KVM: selftests: Add support for #DE exception fixup
Add support for handling #DE (divide error) exceptions in KVM selftests so that the fastops test can verify KVM correctly handles #DE when emulating DIV or IDIV on behalf of the guest. Morph #DE to 0xff (i.e. to -1) as a mostly-arbitrary vector to indicate #DE, so that '0' (the real #DE vector) can still be used to indicate "no exception".
--- tools/testing/selftests/kvm/include/x86/processor.h | 6 ++++++ tools/testing/selftests/kvm/lib/x86/processor.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
tools/testing/selftests/kvm/include/x86/processor.h +6 -0
@@ -1179,6 +1179,12 @@ struct idt_entry {void vm_install_exception_handler (struct kvm_vm *vm,int vector,void (*handler)(struct ex_regs *)); +/* +* Exception fixup morphs #DE to an arbitrary magic vector so that '0' can be +* used to signal "no expcetion". +*/ +#define KVM_MAGIC_DE_VECTOR 0xff +/* If a toddler were to say "abracadabra". */ #define KVM_EXCEPTION_MAGIC 0xabacadabaULL
tools/testing/selftests/kvm/lib/x86/processor.c +1 -1
@@ -557,7 +557,7 @@ static bool kvm_fixup_exception(struct ex_regs *regs)return false;if (regs->vector ==DE_VECTOR ) -return false; + regs->vector =KVM_MAGIC_DE_VECTOR ; regs->rip = regs->r11; regs->r9 = regs->vector;
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Sean Christopherson
maintainerPATCH 2/4 KVM: selftests: Add coverage for 'b' (byte) sized fastops emulation
In reply to Sean ChristophersonExtend the fastops test to cover instructions that operate on 8-bit data. Support for 8-bit instructions was omitted from the original commit purely due to complications with BT not having a r/m8 variant. To keep the RFLAGS.CF behavior deterministic and not heavily biased to '0' or '1', continue using BT, but cast and load the to-be-tested value into a dedicated 32-bit constraint.
Supporting 8-bit operations will allow using guest_test_fastops() as-is to provide full coverage for DIV and IDIV. For divide operations, covering all operand sizes _is_ interesting, because KVM needs provide exception fixup for each size (failure to handle a #DE could panic the host).
--- .../testing/selftests/kvm/x86/fastops_test.c | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/x86/fastops_test.c +13 -7
@@ -10,12 +10,13 @@*/ #define guest_execute_fastop_1 (FEP , insn, __val, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[val]\n\t" \ + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ FEP insn " %[val]\n\t" \ "pushfq\n\t" \ "pop %[flags]\n\t" \ : [val]"+r"(__val), [flags]"=r"(__flags) \ - : : "cc", "memory"); \ + : [ro_val]"rm"((uint32_t)__val) \ + : "cc", "memory"); \ })#define guest_test_fastop_1 (insn, type_t, __val) \
@@ -36,12 +37,13 @@#define guest_execute_fastop_2 (FEP , insn, __input, __output, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[output]\n\t" \ + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ FEP insn " %[input], %[output]\n\t" \ "pushfq\n\t" \ "pop %[flags]\n\t" \ : [output]"+r"(__output), [flags]"=r"(__flags) \ - : [input]"r"(__input) : "cc", "memory"); \ + : [input]"r"(__input), [ro_val]"rm"((uint32_t)__output) \ + : "cc", "memory"); \ })#define guest_test_fastop_2 (insn, type_t, __val1, __val2) \
@@ -63,12 +65,13 @@#define guest_execute_fastop_cl (FEP , insn, __shift, __output, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[output]\n\t" \ + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ FEP insn " %%cl, %[output]\n\t" \ "pushfq\n\t" \ "pop %[flags]\n\t" \ : [output]"+r"(__output), [flags]"=r"(__flags) \ - : "c"(__shift) : "cc", "memory"); \ + : "c"(__shift), [ro_val]"rm"((uint32_t)__output) \ + : "cc", "memory"); \ })#define guest_test_fastop_cl (insn, type_t, __val1, __val2) \
@@ -115,14 +118,16 @@ do { \guest_test_fastop_2 ("add" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("adc" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("and" suffix, type_t, vals[i], vals[j]); \ +if (sizeof (type_t) !=1 ) { \guest_test_fastop_2 ("bsf" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("bsr" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("bt" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("btc" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("btr" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("bts" suffix, type_t, vals[i], vals[j]); \ -guest_test_fastop_2 ("cmp" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("imul" suffix, type_t, vals[i], vals[j]); \ +} \ +guest_test_fastop_2 ("cmp" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("or" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("sbb" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_2 ("sub" suffix, type_t, vals[i], vals[j]); \
@@ -142,6 +147,7 @@ do { \static void guest_code (void ) { +guest_test_fastops (uint8_t,"b" );guest_test_fastops (uint16_t,"w" );guest_test_fastops (uint32_t,"l" );guest_test_fastops (uint64_t,"q" );
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Sean Christopherson
maintainerPATCH 3/4 KVM: selftests: Dedup the gnarly constraints of the fastops tests (more macros!)
In reply to Sean ChristophersonAdd a fastop() macro along with macros to define its required constraints, and use the macros to dedup the innermost guts of the fastop testcases.
No functional change intended.
--- .../testing/selftests/kvm/x86/fastops_test.c | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/x86/fastops_test.c +18 -18
@@ -8,14 +8,20 @@* to set RFLAGS.CF based on whether or not the input is even or odd, so that * instructions like ADC and SBB are deterministic. */ +#define fastop (__insn) \ + "bt $0, %[bt_val]\n\t" \ + __insn "\n\t" \ + "pushfq\n\t" \ + "pop %[flags]\n\t" + +#define flags_constraint (flags_val) [flags]"=r"(flags_val) +#define bt_constraint (__bt_val) [bt_val]"rm"((uint32_t)__bt_val) +#define guest_execute_fastop_1 (FEP , insn, __val, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ - FEP insn " %[val]\n\t" \ - "pushfq\n\t" \ - "pop %[flags]\n\t" \ - : [val]"+r"(__val), [flags]"=r"(__flags) \ - : [ro_val]"rm"((uint32_t)__val) \ + __asm__ __volatile__(fastop(FEP insn " %[val]") \ + : [val]"+r"(__val), flags_constraint(__flags) \ + : bt_constraint(__val) \ : "cc", "memory"); \ })
@@ -37,12 +43,9 @@#define guest_execute_fastop_2 (FEP , insn, __input, __output, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ - FEP insn " %[input], %[output]\n\t" \ - "pushfq\n\t" \ - "pop %[flags]\n\t" \ - : [output]"+r"(__output), [flags]"=r"(__flags) \ - : [input]"r"(__input), [ro_val]"rm"((uint32_t)__output) \ + __asm__ __volatile__(fastop(FEP insn " %[input], %[output]") \ + : [output]"+r"(__output), flags_constraint(__flags) \ + : [input]"r"(__input), bt_constraint(__output) \ : "cc", "memory"); \ })
@@ -65,12 +68,9 @@#define guest_execute_fastop_cl (FEP , insn, __shift, __output, __flags) \ ({ \ - __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ - FEP insn " %%cl, %[output]\n\t" \ - "pushfq\n\t" \ - "pop %[flags]\n\t" \ - : [output]"+r"(__output), [flags]"=r"(__flags) \ - : "c"(__shift), [ro_val]"rm"((uint32_t)__output) \ + __asm__ __volatile__(fastop(FEP insn " %%cl, %[output]") \ + : [output]"+r"(__output), flags_constraint(__flags) \ + : "c"(__shift), bt_constraint(__output) \ : "cc", "memory"); \ })
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Sean Christopherson
maintainerPATCH 4/4 KVM: selftests: Add support for DIV and IDIV in the fastops test
In reply to Sean ChristophersonExtend the fastops test coverage to DIV and IDIV, specifically to provide coverage for #DE (divide error) exceptions, as #DE is the only exception that can occur in KVM's fastops path, i.e. that requires exception fixup.
--- .../testing/selftests/kvm/x86/fastops_test.c | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+)
tools/testing/selftests/kvm/x86/fastops_test.c +38 -0
@@ -92,6 +92,42 @@ ex_flags, insn, shift, (uint64_t)input, flags); \ }) +#define guest_execute_fastop_div (__KVM_ASM_SAFE, insn, __a, __d, __rm, __flags) \ +({ \ + uint64_t ign_error_code; \ + uint8_t vector; \ + \ + __asm__ __volatile__(fastop(__KVM_ASM_SAFE(insn " %[denom]")) \ + : "+a"(__a), "+d"(__d), flags_constraint(__flags), \ + KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ + : [denom]"rm"(__rm), bt_constraint(__rm) \ + : "cc", "memory", KVM_ASM_SAFE_CLOBBERS); \ + vector; \ +}) + +#define guest_test_fastop_div (insn, type_t, __val1, __val2) \ +({ \ + type_t _a = __val1, _d = __val1, rm = __val2; \ + type_t a = _a, d = _d, ex_a = _a, ex_d = _d; \ + uint64_t flags, ex_flags; \ + uint8_t v, ex_v; \ + \ + ex_v = guest_execute_fastop_div(KVM_ASM_SAFE, insn, ex_a, ex_d, rm, ex_flags); \ + v = guest_execute_fastop_div(KVM_ASM_SAFE_FEP, insn, a, d, rm, flags); \ + \ + GUEST_ASSERT_EQ(v, ex_v); \ + __GUEST_ASSERT(v == ex_v, \ + "Wanted vector 0x%x for '%s 0x%lx:0x%lx/0x%lx', got 0x%x", \ + ex_v, insn, (uint64_t)_a, (uint64_t)_d, (uint64_t)rm, v); \ + __GUEST_ASSERT(a == ex_a && d == ex_d, \ + "Wanted 0x%lx:0x%lx for '%s 0x%lx:0x%lx/0x%lx', got 0x%lx:0x%lx",\ + (uint64_t)ex_a, (uint64_t)ex_d, insn, (uint64_t)_a, \ + (uint64_t)_d, (uint64_t)rm, (uint64_t)a, (uint64_t)d); \ + __GUEST_ASSERT(v || ex_v || (flags == ex_flags), \ + "Wanted flags 0x%lx for '%s 0x%lx:0x%lx/0x%lx', got 0x%lx", \ + ex_flags, insn, (uint64_t)_a, (uint64_t)_d, (uint64_t)rm, flags);\ +}) +static const uint64_t vals[] = {0 ,1 ,
@@ -141,6 +177,8 @@ if (sizeof(type_t) != 1) { \guest_test_fastop_cl ("sar" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_cl ("shl" suffix, type_t, vals[i], vals[j]); \guest_test_fastop_cl ("shr" suffix, type_t, vals[i], vals[j]); \ + \ +guest_test_fastop_div ("div" suffix, type_t, vals[i], vals[j]); \ } \ } \ }while (0 )
Signature · 1 line
2.51.0.384.g4c02a37b29-goog
Sean Christopherson
maintainerQuoted 9 lines · Sean Christopherson
Add DIV and IDIV support to the so called fastops test to add coverage for #DE (the only exception that can occur during fastops execution), which was broken due to a typo in PeterZ's overhaul[*].
I meant to post this back in July and forgot...
[*] https://lore.kernel.org/all/[email protected]
[...]
Applied to kvm-x86 selftests, thanks!
[1/4] KVM: selftests: Add support for #DE exception fixup
https://github.com/kvm-x86/linux/commit/7b39b6c76942 [2/4] KVM: selftests: Add coverage for 'b' (byte) sized fastops emulation https://github.com/kvm-x86/linux/commit/9bf5da1ca427 [3/4] KVM: selftests: Dedup the gnarly constraints of the fastops tests (more macros!) https://github.com/kvm-x86/linux/commit/fe08478b1d51 [4/4] KVM: selftests: Add support for DIV and IDIV in the fastops test https://github.com/kvm-x86/linux/commit/aebc62b3dedc