Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Add coverage for 'b' (byte) sized fastops emulation

Extend 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).

Link: https://lore.kernel.org/all/aIF7ZhWZxlkcpm4y@google.com
Link: https://lore.kernel.org/r/20250909202835.333554-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

+13 -7
+13 -7
tools/testing/selftests/kvm/x86/fastops_test.c
··· 10 10 */ 11 11 #define guest_execute_fastop_1(FEP, insn, __val, __flags) \ 12 12 ({ \ 13 - __asm__ __volatile__("bt $0, %[val]\n\t" \ 13 + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ 14 14 FEP insn " %[val]\n\t" \ 15 15 "pushfq\n\t" \ 16 16 "pop %[flags]\n\t" \ 17 17 : [val]"+r"(__val), [flags]"=r"(__flags) \ 18 - : : "cc", "memory"); \ 18 + : [ro_val]"rm"((uint32_t)__val) \ 19 + : "cc", "memory"); \ 19 20 }) 20 21 21 22 #define guest_test_fastop_1(insn, type_t, __val) \ ··· 37 36 38 37 #define guest_execute_fastop_2(FEP, insn, __input, __output, __flags) \ 39 38 ({ \ 40 - __asm__ __volatile__("bt $0, %[output]\n\t" \ 39 + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ 41 40 FEP insn " %[input], %[output]\n\t" \ 42 41 "pushfq\n\t" \ 43 42 "pop %[flags]\n\t" \ 44 43 : [output]"+r"(__output), [flags]"=r"(__flags) \ 45 - : [input]"r"(__input) : "cc", "memory"); \ 44 + : [input]"r"(__input), [ro_val]"rm"((uint32_t)__output) \ 45 + : "cc", "memory"); \ 46 46 }) 47 47 48 48 #define guest_test_fastop_2(insn, type_t, __val1, __val2) \ ··· 65 63 66 64 #define guest_execute_fastop_cl(FEP, insn, __shift, __output, __flags) \ 67 65 ({ \ 68 - __asm__ __volatile__("bt $0, %[output]\n\t" \ 66 + __asm__ __volatile__("bt $0, %[ro_val]\n\t" \ 69 67 FEP insn " %%cl, %[output]\n\t" \ 70 68 "pushfq\n\t" \ 71 69 "pop %[flags]\n\t" \ 72 70 : [output]"+r"(__output), [flags]"=r"(__flags) \ 73 - : "c"(__shift) : "cc", "memory"); \ 71 + : "c"(__shift), [ro_val]"rm"((uint32_t)__output) \ 72 + : "cc", "memory"); \ 74 73 }) 75 74 76 75 #define guest_test_fastop_cl(insn, type_t, __val1, __val2) \ ··· 118 115 guest_test_fastop_2("add" suffix, type_t, vals[i], vals[j]); \ 119 116 guest_test_fastop_2("adc" suffix, type_t, vals[i], vals[j]); \ 120 117 guest_test_fastop_2("and" suffix, type_t, vals[i], vals[j]); \ 118 + if (sizeof(type_t) != 1) { \ 121 119 guest_test_fastop_2("bsf" suffix, type_t, vals[i], vals[j]); \ 122 120 guest_test_fastop_2("bsr" suffix, type_t, vals[i], vals[j]); \ 123 121 guest_test_fastop_2("bt" suffix, type_t, vals[i], vals[j]); \ 124 122 guest_test_fastop_2("btc" suffix, type_t, vals[i], vals[j]); \ 125 123 guest_test_fastop_2("btr" suffix, type_t, vals[i], vals[j]); \ 126 124 guest_test_fastop_2("bts" suffix, type_t, vals[i], vals[j]); \ 127 - guest_test_fastop_2("cmp" suffix, type_t, vals[i], vals[j]); \ 128 125 guest_test_fastop_2("imul" suffix, type_t, vals[i], vals[j]); \ 126 + } \ 127 + guest_test_fastop_2("cmp" suffix, type_t, vals[i], vals[j]); \ 129 128 guest_test_fastop_2("or" suffix, type_t, vals[i], vals[j]); \ 130 129 guest_test_fastop_2("sbb" suffix, type_t, vals[i], vals[j]); \ 131 130 guest_test_fastop_2("sub" suffix, type_t, vals[i], vals[j]); \ ··· 147 142 148 143 static void guest_code(void) 149 144 { 145 + guest_test_fastops(uint8_t, "b"); 150 146 guest_test_fastops(uint16_t, "w"); 151 147 guest_test_fastops(uint32_t, "l"); 152 148 guest_test_fastops(uint64_t, "q");