KVM: selftests: Fix "fix hypercall test" build errors
Sean Christopherson
reviewerv2 cover KVM: selftests: Fix "fix hypercall test" build errors
After a toolchain upgrade, the x86 fix_hypercall_test started throwing warnings due to -Werror=array-bounds rightly complaining that the test is generating an out-of-bounds array access.
The "obvious" fix is to replace the memcpy() with a memcmp() and compare only the exact size of the hypercall instruction. That worked, until I fiddled with the code a bit more and suddenly the test started jumping into the weeds due to gcc generating a call to the external memcmp() through the PLT, which isn't supported in the selftests.
To fix that mess, which has been a pitfall for quite some time, provide implementations of memcmp(), memcpy(), and memset() to effectively override the compiler built-ins. My thought is to start with the helpers that are most likely to be used in guest code, and then add more as needed.
v2:
- Revert a patch that avoided memset(). [Jim]
- Collect reviews. [Oliver]
- Rename kvm_string.c => string_overrides.c. [David]
- Tweak the "fix hypercall" test to verify that the original opcode is preserved exactly. [Oliver]
- Further dedup code in the "fix hypercall" test.
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (7):
KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest
use
KVM: selftests: Compare insn opcodes directly in fix_hypercall_test
KVM: selftests: Remove unnecessary register shuffling in
fix_hypercall_test
KVM: selftests: Hardcode VMCALL/VMMCALL opcodes in "fix hypercall"
test
KVM: selftests: Explicitly verify KVM doesn't patch hypercall if
quirk==off
KVM: selftests: Dedup subtests of fix_hypercall_test Revert "KVM: selftests: Fix nested SVM tests when built with clang"
tools/testing/selftests/kvm/Makefile | 11 +- .../selftests/kvm/lib/string_override.c | 39 +++++ tools/testing/selftests/kvm/lib/x86_64/svm.c | 14 +- .../selftests/kvm/x86_64/fix_hypercall_test.c | 134 +++++++----------- 4 files changed, 103 insertions(+), 95 deletions(-) create mode 100644 tools/testing/selftests/kvm/lib/string_override.c
base-commit: c59fb127583869350256656b7ed848c398bef879
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 1/7 KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use
Implement memcmp(), memcpy(), and memset() to override the compiler's built-in versions in order to guarantee that the compiler won't generate out-of-line calls to external functions via the PLT. This allows the helpers to be safely used in guest code, as KVM selftests don't support dynamic loading of guest code.
Steal the implementations from the kernel's generic versions, sans the optimizations in memcmp() for unaligned accesses.
Put the utilities in a separate compilation unit and build with -ffreestanding to fudge around a gcc "feature" where it will optimize memset(), memcpy(), etc... by generating a recursive call. I.e. the compiler optimizes itself into infinite recursion. Alternatively, the individual functions could be tagged with optimize("no-tree-loop-distribute-patterns"), but using "optimize" for anything but debug is discouraged, and Linus NAK'd the use of the flag in the kernel proper[*].
https://lore.kernel.org/lkml/CAHk-=wik-oXnUpfZ6Hw37uLykc-_P0Apyn2XuX-odh-3Nzop8w@mail.gmail.com
--- tools/testing/selftests/kvm/Makefile | 11 +++++- .../selftests/kvm/lib/string_override.c | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/lib/string_override.c
tools/testing/selftests/kvm/Makefile +10 -1
@@ -48,6 +48,8 @@ LIBKVM += lib/rbtree.c LIBKVM += lib/sparsebit.c LIBKVM += lib/test_util.c +LIBKVM_STRING += lib/string_override.c + LIBKVM_x86_64 += lib/x86_64/apic.c LIBKVM_x86_64 += lib/x86_64/handlers.S LIBKVM_x86_64 += lib/x86_64/perf_test_util.c
@@ -221,7 +223,8 @@ LIBKVM_C := $(filter %.c,$(LIBKVM)) LIBKVM_S := $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) -LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) +LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) +LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*
@@ -232,6 +235,12 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ +# Compile the string overrides as freestanding to prevent the compiler from +# generating self-referential code, e.g. with "freestanding" the compiler may +# "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion. +$(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ + x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) $(TEST_GEN_PROGS): $(LIBKVM_OBJS) $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)
tools/testing/selftests/kvm/lib/string_override.c new file +39 -0
@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stddef.h> + +/* +* Override the "basic" built-in string helpers so that they can be used in +* guest code. KVM selftests don't support dynamic loading in guest code and +* will jump into the weeds if the compiler decides to insert an out-of-line +* call via the PLT. +*/ +int memcmp (const void *cs,const void *ct,size_t count) +{ +const unsigned char *su1, *su2; +int res =0 ; + +for (su1 = cs, su2 = ct;0 < count; ++su1, ++su2, count--) { +if ((res = *su1 - *su2) !=0 ) +break ; + } +return res; +} + +void *memcpy (void *dest,const void *src,size_t count) +{ +char *tmp = dest; +const char *s = src; + +while (count--) + *tmp++ = *s++; +return dest; +} + +void *memset (void *s,int c,size_t count) +{ +char *xs = s; + +while (count--) + *xs++ = c; +return s; +}
Signature · 1 line
2.37.3.998.g577e59143f-goog
Re: [PATCH v2 1/7] KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use
Quoted 57 lines · Sean Christopherson
Implement
memcmp(),memcpy(), andmemset()to override the compiler's built-in versions in order to guarantee that the compiler won't generate out-of-line calls to external functions via the PLT. This allows the helpers to be safely used in guest code, as KVM selftests don't support dynamic loading of guest code.Steal the implementations from the kernel's generic versions, sans the optimizations in
memcmp()for unaligned accesses.Put the utilities in a separate compilation unit and build with -ffreestanding to fudge around a gcc "feature" where it will optimize
memset(),memcpy(), etc... by generating a recursive call. I.e. the compiler optimizes itself into infinite recursion. Alternatively, the individual functions could be tagged withoptimize("no-tree-loop-distribute-patterns"), but using "optimize" for anything but debug is discouraged, and Linus NAK'd the use of the flag in the kernel proper[*].https://lore.kernel.org/lkml/CAHk-=wik-oXnUpfZ6Hw37uLykc-_P0Apyn2XuX-odh-3Nzop8w@mail.gmail.com
Cc Andrew Jones <[email protected]>Cc Anup Patel <[email protected]>Cc Atish Patra <[email protected]>Cc Christian Borntraeger <[email protected]>Cc Janosch Frank <[email protected]>Cc Claudio Imbrenda <[email protected]>Signed-off-by Sean Christopherson <[email protected]>--- tools/testing/selftests/kvm/Makefile | 11 +++++- .../selftests/kvm/lib/string_override.c | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/kvm/lib/string_override.ctools/testing/selftests/kvm/Makefile +4
-1@@ -48,6 +48,8 @@ LIBKVM += lib/rbtree.c LIBKVM += lib/sparsebit.c LIBKVM += lib/test_util.c +LIBKVM_STRING += lib/string_override.c + LIBKVM_x86_64 += lib/x86_64/apic.c LIBKVM_x86_64 += lib/x86_64/handlers.S LIBKVM_x86_64 += lib/x86_64/perf_test_util.c@@ -221,7 +223,8 @@ LIBKVM_C := $(filter %.c,$(LIBKVM)) LIBKVM_S := $(filter %.S,$(LIBKVM)) LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) -LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) +LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) +LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.*
tools/testing/selftests/kvm/Makefile $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c · line 239
Quoted 5 lines
tools/testing/selftests/kvm/Makefile +2
-0@@ -232,6 +235,12 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ +# Compile the string overrides as freestanding to prevent the compiler from +# generating self-referential code, e.g. with "freestanding" the compiler may
^ without
Quoted 55 lines
tools/testing/selftests/kvm/Makefile +4
-0+# "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion. +$(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ + x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) $(TEST_GEN_PROGS): $(LIBKVM_OBJS) $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS)tools/testing/selftests/kvm/lib/string_override.c new file +39
-0@@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <stddef.h> + +/* +* Override the "basic" built-in string helpers so that they can be used in +* guest code. KVM selftests don't support dynamic loading in guest code and +* will jump into the weeds if the compiler decides to insert an out-of-line +* call via the PLT. +*/ +int memcmp (const void *cs,const void *ct,size_t count) +{ +const unsigned char *su1, *su2; +int res =0 ; + +for (su1 = cs, su2 = ct;0 < count; ++su1, ++su2, count--) { +if ((res = *su1 - *su2) !=0 ) +break ; + } +return res; +} + +void *memcpy (void *dest,const void *src,size_t count) +{ +char *tmp = dest; +const char *s = src; + +while (count--) + *tmp++ = *s++; +return dest; +} + +void *memset (void *s,int c,size_t count) +{ +char *xs = s; + +while (count--) + *xs++ = c; +return s; +}Signature · 1 line
2.37.3.998.g577e59143f-goog
Otherwise
Sean Christopherson
reviewerRe: [PATCH v2 1/7] KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use
Quoted 8 lines · Andrew Jones
tools/testing/selftests/kvm/Makefile $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c · line 239
Quoted 6 lines · Sean Christopherson
tools/testing/selftests/kvm/Makefile +2
-0@@ -232,6 +235,12 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ +# Compile the string overrides as freestanding to prevent the compiler from +# generating self-referential code, e.g. with "freestanding" the compiler may^ without
Oof, good eyes. Thanks!
Sean Christopherson
reviewerPATCH v2 2/7 KVM: selftests: Compare insn opcodes directly in fix_hypercall_test
In reply to Sean ChristophersonDirectly compare the expected versus observed hypercall instructions when verifying that KVM patched in the native hypercall (FIX_HYPERCALL_INSN quirk enabled). gcc rightly complains that doing a 4-byte memcpy() with an "unsigned char" as the source generates an out-of-bounds accesses.
Alternatively, "exp" and "obs" could be declared as 3-byte arrays, but there's no known reason to copy locally instead of comparing directly.
In function ‘assert_hypercall_insn’,
inlined from ‘guest_main’ at x86_64/fix_hypercall_test.c:91:2:x86_64/fix_hypercall_test.c:63:9: error: array subscript ‘unsigned int[0]’
is partly outside array bounds of ‘unsigned char[1]’ [-Werror=array-bounds]
63 | memcpy(&exp, exp_insn, sizeof(exp));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x86_64/fix_hypercall_test.c: In function ‘guest_main’:
x86_64/fix_hypercall_test.c:42:22: note: object ‘vmx_hypercall_insn’ of size 1
42 | extern unsigned char vmx_hypercall_insn;
| ^~~~~~~~~~~~~~~~~~
x86_64/fix_hypercall_test.c:25:22: note: object ‘svm_hypercall_insn’ of size 1
25 | extern unsigned char svm_hypercall_insn;
| ^~~~~~~~~~~~~~~~~~In function ‘assert_hypercall_insn’,
inlined from ‘guest_main’ at x86_64/fix_hypercall_test.c:91:2:x86_64/fix_hypercall_test.c:64:9: error: array subscript ‘unsigned int[0]’
is partly outside array bounds of ‘unsigned char[1]’ [-Werror=array-bounds]
64 | memcpy(&obs, obs_insn, sizeof(obs));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x86_64/fix_hypercall_test.c: In function ‘guest_main’:
x86_64/fix_hypercall_test.c:25:22: note: object ‘svm_hypercall_insn’ of size 1
25 | extern unsigned char svm_hypercall_insn;
| ^~~~~~~~~~~~~~~~~~
x86_64/fix_hypercall_test.c:42:22: note: object ‘vmx_hypercall_insn’ of size 1
42 | extern unsigned char vmx_hypercall_insn;
| ^~~~~~~~~~~~~~~~~~cc1: all warnings being treated as errors
make: *** [../lib.mk:135: tools/testing/selftests/kvm/x86_64/fix_hypercall_test] Error 1
--- .../selftests/kvm/x86_64/fix_hypercall_test.c | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-)
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +16 -18
@@ -14,6 +14,9 @@#include "kvm_util.h" #include "processor.h" +/* VMCALL and VMMCALL are both 3-byte opcodes. */ +#define HYPERCALL_INSN_SIZE 3 +static bool ud_expected;static void guest_ud_handler (struct ex_regs *regs)
@@ -22,7 +25,7 @@ static void guest_ud_handler(struct ex_regs *regs)GUEST_DONE (); } -extern unsigned char svm_hypercall_insn; +extern uint8_t svm_hypercall_insn[HYPERCALL_INSN_SIZE ];static uint64_t svm_do_sched_yield (uint8_t apic_id) {uint64_t ret;
@@ -39,7 +42,7 @@ static uint64_t svm_do_sched_yield(uint8_t apic_id)return ret; } -extern unsigned char vmx_hypercall_insn; +extern uint8_t vmx_hypercall_insn[HYPERCALL_INSN_SIZE ];static uint64_t vmx_do_sched_yield (uint8_t apic_id) {uint64_t ret;
@@ -56,30 +59,20 @@ static uint64_t vmx_do_sched_yield(uint8_t apic_id)return ret; } -static void assert_hypercall_insn (unsigned char *exp_insn,unsigned char *obs_insn) -{ -uint32_t exp =0 , obs =0 ; - -memcpy (&exp, exp_insn,sizeof (exp)); -memcpy (&obs, obs_insn,sizeof (obs)); - -GUEST_ASSERT_EQ (exp, obs); -} -static void guest_main (void ) { -unsigned char *native_hypercall_insn, *hypercall_insn; +uint8_t *native_hypercall_insn, *hypercall_insn;uint8_t apic_id; apic_id =GET_APIC_ID_FIELD (xapic_read_reg (APIC_ID ));if (is_intel_cpu ()) { - native_hypercall_insn = &vmx_hypercall_insn; - hypercall_insn = &svm_hypercall_insn; + native_hypercall_insn = vmx_hypercall_insn; + hypercall_insn = svm_hypercall_insn;svm_do_sched_yield (apic_id); }else if (is_amd_cpu ()) { - native_hypercall_insn = &svm_hypercall_insn; - hypercall_insn = &vmx_hypercall_insn; + native_hypercall_insn = svm_hypercall_insn; + hypercall_insn = vmx_hypercall_insn;vmx_do_sched_yield (apic_id); }else {GUEST_ASSERT (0 );
@@ -87,8 +80,13 @@ static void guest_main(void)return ; } +/* +* The hypercall didn't #UD (guest_ud_handler() signals "done" if a #UD +* occurs). Verify that a #UD is NOT expected and that KVM patched in +* the native hypercall. +*/ GUEST_ASSERT (!ud_expected); -assert_hypercall_insn (native_hypercall_insn, hypercall_insn); +GUEST_ASSERT (!memcmp (native_hypercall_insn, hypercall_insn,HYPERCALL_INSN_SIZE ));GUEST_DONE (); }
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 3/7 KVM: selftests: Remove unnecessary register shuffling in fix_hypercall_test
In reply to Sean ChristophersonUse input constraints to load RAX and RBX when testing that KVM correctly does/doesn't patch the "wrong" hypercall. There's no need to manually load RAX and RBX, and no reason to clobber them either (KVM is not supposed to modify anything other than RAX).
--- .../selftests/kvm/x86_64/fix_hypercall_test.c | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-)
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +8 -14
@@ -30,14 +30,11 @@ static uint64_t svm_do_sched_yield(uint8_t apic_id) {uint64_t ret; - asmvolatile ("mov %1, %%rax\n\t" -"mov %2, %%rbx\n\t" -"svm_hypercall_insn:\n\t" + asmvolatile ("svm_hypercall_insn:\n\t" "vmmcall\n\t" -"mov %%rax, %0\n\t" - :"=r" (ret) - :"r" ((uint64_t )KVM_HC_SCHED_YIELD ),"r" ((uint64_t )apic_id) - :"rax" ,"rbx" ,"memory" ); + :"=a" (ret) + :"a" ((uint64_t )KVM_HC_SCHED_YIELD ),"b" ((uint64_t )apic_id) + :"memory" );return ret; }
@@ -47,14 +44,11 @@ static uint64_t vmx_do_sched_yield(uint8_t apic_id) {uint64_t ret; - asmvolatile ("mov %1, %%rax\n\t" -"mov %2, %%rbx\n\t" -"vmx_hypercall_insn:\n\t" + asmvolatile ("vmx_hypercall_insn:\n\t" "vmcall\n\t" -"mov %%rax, %0\n\t" - :"=r" (ret) - :"r" ((uint64_t )KVM_HC_SCHED_YIELD ),"r" ((uint64_t )apic_id) - :"rax" ,"rbx" ,"memory" ); + :"=a" (ret) + :"a" ((uint64_t )KVM_HC_SCHED_YIELD ),"b" ((uint64_t )apic_id) + :"memory" );return ret; }
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 4/7 KVM: selftests: Hardcode VMCALL/VMMCALL opcodes in "fix hypercall" test
In reply to Sean ChristophersonHardcode the VMCALL/VMMCALL opcodes in dedicated arrays instead of extracting the opcodes from inline asm, and patch in the "other" opcode so as to preserve the original opcode, i.e. the opcode that the test executes in the guest.
Preserving the original opcode (by not patching the source), will make it easier to implement a check that KVM doesn't modify the opcode (the test currently only verifies that a #UD occurred).
Use INT3 (0xcc) as the placeholder so that the guest will likely die a horrible death if the test's patching goes awry.
As a bonus, patching from within the test dedups a decent chunk of code.
--- .../selftests/kvm/x86_64/fix_hypercall_test.c | 43 +++++++------------ 1 file changed, 16 insertions(+), 27 deletions(-)
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +16 -27
@@ -25,27 +25,16 @@ static void guest_ud_handler(struct ex_regs *regs)GUEST_DONE (); } -extern uint8_t svm_hypercall_insn[HYPERCALL_INSN_SIZE ]; -static uint64_t svm_do_sched_yield (uint8_t apic_id) -{ -uint64_t ret; - - asmvolatile ("svm_hypercall_insn:\n\t" -"vmmcall\n\t" - :"=a" (ret) - :"a" ((uint64_t )KVM_HC_SCHED_YIELD ),"b" ((uint64_t )apic_id) - :"memory" ); - -return ret; -} +static const uint8_t vmx_vmcall[HYPERCALL_INSN_SIZE ] = {0x0f ,0x01 ,0xc1 }; +static const uint8_t svm_vmmcall[HYPERCALL_INSN_SIZE ] = {0x0f ,0x01 ,0xd9 }; -extern uint8_t vmx_hypercall_insn[HYPERCALL_INSN_SIZE ]; -static uint64_t vmx_do_sched_yield (uint8_t apic_id) +extern uint8_t hypercall_insn[HYPERCALL_INSN_SIZE ]; +static uint64_t do_sched_yield (uint8_t apic_id) {uint64_t ret; - asmvolatile ("vmx_hypercall_insn:\n\t" -"vmcall\n\t" + asmvolatile ("hypercall_insn:\n\t" +".byte 0xcc,0xcc,0xcc\n\t" :"=a" (ret) :"a" ((uint64_t )KVM_HC_SCHED_YIELD ),"b" ((uint64_t )apic_id) :"memory" );
@@ -55,25 +44,25 @@ static uint64_t vmx_do_sched_yield(uint8_t apic_id)static void guest_main (void ) { -uint8_t *native_hypercall_insn, *hypercall_insn; -uint8_t apic_id; - - apic_id =GET_APIC_ID_FIELD (xapic_read_reg (APIC_ID )); +const uint8_t *native_hypercall_insn; +const uint8_t *other_hypercall_insn;if (is_intel_cpu ()) { - native_hypercall_insn = vmx_hypercall_insn; - hypercall_insn = svm_hypercall_insn; -svm_do_sched_yield (apic_id); + native_hypercall_insn = vmx_vmcall; + other_hypercall_insn = svm_vmmcall; }else if (is_amd_cpu ()) { - native_hypercall_insn = svm_hypercall_insn; - hypercall_insn = vmx_hypercall_insn; -vmx_do_sched_yield (apic_id); + native_hypercall_insn = svm_vmmcall; + other_hypercall_insn = vmx_vmcall; }else {GUEST_ASSERT (0 );/* unreachable */ return ; } +memcpy (hypercall_insn, other_hypercall_insn,HYPERCALL_INSN_SIZE ); + +do_sched_yield (GET_APIC_ID_FIELD (xapic_read_reg (APIC_ID ))); +/* * The hypercall didn't #UD (guest_ud_handler() signals "done" if a #UD * occurs). Verify that a #UD is NOT expected and that KVM patched in
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 5/7 KVM: selftests: Explicitly verify KVM doesn't patch hypercall if quirk==off
In reply to Sean ChristophersonExplicitly verify that KVM doesn't patch in the native hypercall if the FIX_HYPERCALL_INSN quirk is disabled. The test currently verifies that a #UD occurred, but doesn't actually verify that no patching occurred.
--- .../selftests/kvm/x86_64/fix_hypercall_test.c | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-)
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +18 -8
@@ -21,8 +21,8 @@ static bool ud_expected;static void guest_ud_handler (struct ex_regs *regs) { -GUEST_ASSERT (ud_expected); -GUEST_DONE (); + regs->rax = -EFAULT ; + regs->rip +=HYPERCALL_INSN_SIZE ; }static const uint8_t vmx_vmcall[HYPERCALL_INSN_SIZE ] = {0x0f ,0x01 ,0xc1 };
@@ -46,6 +46,7 @@ static void guest_main(void) {const uint8_t *native_hypercall_insn;const uint8_t *other_hypercall_insn; +uint64_t ret;if (is_intel_cpu ()) { native_hypercall_insn = vmx_vmcall;
@@ -61,15 +62,24 @@ static void guest_main(void)memcpy (hypercall_insn, other_hypercall_insn,HYPERCALL_INSN_SIZE ); -do_sched_yield (GET_APIC_ID_FIELD (xapic_read_reg (APIC_ID ))); + ret =do_sched_yield (GET_APIC_ID_FIELD (xapic_read_reg (APIC_ID )));/* -* The hypercall didn't #UD (guest_ud_handler() signals "done" if a #UD -* occurs). Verify that a #UD is NOT expected and that KVM patched in -* the native hypercall. +* If the quirk is disabled, verify that guest_ud_handler() "returned" +* -EFAULT and that KVM did NOT patch the hypercall. If the quirk is +* enabled, verify that the hypercall succeeded and that KVM patched in +* the "right" hypercall. */ -GUEST_ASSERT (!ud_expected); -GUEST_ASSERT (!memcmp (native_hypercall_insn, hypercall_insn,HYPERCALL_INSN_SIZE )); +if (ud_expected) { +GUEST_ASSERT (ret == (uint64_t )-EFAULT ); +GUEST_ASSERT (!memcmp (other_hypercall_insn, hypercall_insn, +HYPERCALL_INSN_SIZE )); + }else { +GUEST_ASSERT (!ret); +GUEST_ASSERT (!memcmp (native_hypercall_insn, hypercall_insn, +HYPERCALL_INSN_SIZE )); + } +GUEST_DONE (); }
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 6/7 KVM: selftests: Dedup subtests of fix_hypercall_test
In reply to Sean ChristophersonCombine fix_hypercall_test's two subtests into a common routine, the only difference between the two is whether or not the quirk is disabled. Passing a boolean is a little gross, but using an enum to make it super obvious that the callers are enabling/disabling the quirk seems like overkill.
No functional change intended.
--- .../selftests/kvm/x86_64/fix_hypercall_test.c | 45 ++++++------------- 1 file changed, 13 insertions(+), 32 deletions(-)
tools/testing/selftests/kvm/x86_64/fix_hypercall_test.c +13 -32
@@ -17,7 +17,7 @@/* VMCALL and VMMCALL are both 3-byte opcodes. */ #define HYPERCALL_INSN_SIZE 3 -static bool ud_expected; +static bool quirk_disabled;static void guest_ud_handler (struct ex_regs *regs) {
@@ -70,7 +70,7 @@ static void guest_main(void)* enabled, verify that the hypercall succeeded and that KVM patched in * the "right" hypercall. */ -if (ud_expected) { +if (quirk_disabled) {GUEST_ASSERT (ret == (uint64_t )-EFAULT );GUEST_ASSERT (!memcmp (other_hypercall_insn, hypercall_insn,HYPERCALL_INSN_SIZE ));
@@ -83,13 +83,6 @@ static void guest_main(void)GUEST_DONE (); } -static void setup_ud_vector (struct kvm_vcpu *vcpu) -{ -vm_init_descriptor_tables (vcpu->vm); -vcpu_init_descriptor_tables (vcpu); -vm_install_exception_handler (vcpu->vm,UD_VECTOR , guest_ud_handler); -} -static void enter_guest (struct kvm_vcpu *vcpu) {struct kvm_run *run = vcpu->run;
@@ -110,35 +103,23 @@ static void enter_guest(struct kvm_vcpu *vcpu) } } -static void test_fix_hypercall (void ) +static void test_fix_hypercall (bool disable_quirk) {struct kvm_vcpu *vcpu;struct kvm_vm *vm; vm =vm_create_with_one_vcpu (&vcpu, guest_main); -setup_ud_vector (vcpu); - ud_expected = false; -sync_global_to_guest (vm, ud_expected); +vm_init_descriptor_tables (vcpu->vm); +vcpu_init_descriptor_tables (vcpu); +vm_install_exception_handler (vcpu->vm,UD_VECTOR , guest_ud_handler); -virt_pg_map (vm,APIC_DEFAULT_GPA ,APIC_DEFAULT_GPA ); +if (disable_quirk) +vm_enable_cap (vm,KVM_CAP_DISABLE_QUIRKS2 , +KVM_X86_QUIRK_FIX_HYPERCALL_INSN ); -enter_guest (vcpu); -} - -static void test_fix_hypercall_disabled (void ) -{ -struct kvm_vcpu *vcpu; -struct kvm_vm *vm; - - vm =vm_create_with_one_vcpu (&vcpu, guest_main); -setup_ud_vector (vcpu); - -vm_enable_cap (vm,KVM_CAP_DISABLE_QUIRKS2 , -KVM_X86_QUIRK_FIX_HYPERCALL_INSN ); - - ud_expected = true; -sync_global_to_guest (vm, ud_expected); + quirk_disabled = disable_quirk; +sync_global_to_guest (vm, quirk_disabled);virt_pg_map (vm,APIC_DEFAULT_GPA ,APIC_DEFAULT_GPA );
@@ -149,6 +130,6 @@ int main(void) {TEST_REQUIRE (kvm_check_cap (KVM_CAP_DISABLE_QUIRKS2 ) &KVM_X86_QUIRK_FIX_HYPERCALL_INSN ); -test_fix_hypercall (); -test_fix_hypercall_disabled (); +test_fix_hypercall (false); +test_fix_hypercall (true); }
Signature · 1 line
2.37.3.998.g577e59143f-goog
Sean Christopherson
reviewerPATCH v2 7/7 Revert "KVM: selftests: Fix nested SVM tests when built with clang"
In reply to Sean ChristophersonRevert back to using memset() in generic_svm_setup() now that KVM selftests override memset() and friends specifically to prevent the compiler from generating fancy code and/or linking to the libc implementation.
This reverts commit ed290e1c20da19fa100a3e0f421aa31b65984960.
--- tools/testing/selftests/kvm/lib/x86_64/svm.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-)
tools/testing/selftests/kvm/lib/x86_64/svm.c +1 -13
@@ -60,18 +60,6 @@ static void vmcb_set_seg(struct vmcb_seg *seg, u16 selector, seg->base = base; } -/* -* Avoid using memset to clear the vmcb, since libc may not be -* available in L1 (and, even if it is, features that libc memset may -* want to use, like AVX, may not be enabled). -*/ -static void clear_vmcb (struct vmcb *vmcb) -{ -int n =sizeof (*vmcb) /sizeof (u32); - - asmvolatile ("rep stosl" :"+c" (n),"+D" (vmcb) :"a" (0 ) :"memory" ); -} -void generic_svm_setup (struct svm_test_data *svm,void *guest_rip,void *guest_rsp) {struct vmcb *vmcb = svm->vmcb;
@@ -88,7 +76,7 @@ void generic_svm_setup(struct svm_test_data *svm, void *guest_rip, void *guest_rwrmsr (MSR_EFER , efer |EFER_SVME );wrmsr (MSR_VM_HSAVE_PA , svm->save_area_gpa); -clear_vmcb (vmcb); +memset (vmcb,0 ,sizeof (*vmcb)); asmvolatile ("vmsave %0\n\t" : :"a" (vmcb_gpa) :"memory" );vmcb_set_seg (&save->es,get_es (),0 ,-1U , data_seg_attr);vmcb_set_seg (&save->cs,get_cs (),0 ,-1U , code_seg_attr);
Signature · 1 line
2.37.3.998.g577e59143f-goog
Re: [PATCH v2 7/7] Revert "KVM: selftests: Fix nested SVM tests when built with clang"
Quoted 10 lines · Sean Christopherson
Revert back to using
memset()ingeneric_svm_setup()now that KVM selftests overridememset()and friends specifically to prevent the compiler from generating fancy code and/or linking to the libc implementation.This reverts commit
ed290e1c20da19fa100a3e0f421aa31b65984960.Suggested-by Jim Mattson <[email protected]>Signed-off-by Sean Christopherson <[email protected]>
I love it!