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

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

Cc: Andrew Jones <andrew.jones@linux.dev>
Cc: Anup Patel <anup@brainfault.org>
Cc: Atish Patra <atishp@atishpatra.org>
Cc: Christian Borntraeger <borntraeger@linux.ibm.com>
Cc: Janosch Frank <frankja@linux.ibm.com>
Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20220928233652.783504-2-seanjc@google.com>
Reviewed-by: Andrew Jones <andrew.jones@linux.dev>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Sean Christopherson and committed by
Paolo Bonzini
6b6f7148 aae2e722

+49 -1
+10 -1
tools/testing/selftests/kvm/Makefile
··· 48 48 LIBKVM += lib/sparsebit.c 49 49 LIBKVM += lib/test_util.c 50 50 51 + LIBKVM_STRING += lib/string_override.c 52 + 51 53 LIBKVM_x86_64 += lib/x86_64/apic.c 52 54 LIBKVM_x86_64 += lib/x86_64/handlers.S 53 55 LIBKVM_x86_64 += lib/x86_64/perf_test_util.c ··· 222 220 LIBKVM_S := $(filter %.S,$(LIBKVM)) 223 221 LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) 224 222 LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) 225 - LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) 223 + LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) 224 + LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) 226 225 227 226 EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.* 228 227 ··· 233 230 234 231 $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S 235 232 $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ 233 + 234 + # Compile the string overrides as freestanding to prevent the compiler from 235 + # generating self-referential code, e.g. without "freestanding" the compiler may 236 + # "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion. 237 + $(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c 238 + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ 236 239 237 240 x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) 238 241 $(TEST_GEN_PROGS): $(LIBKVM_OBJS)
+39
tools/testing/selftests/kvm/lib/string_override.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + #include <stddef.h> 3 + 4 + /* 5 + * Override the "basic" built-in string helpers so that they can be used in 6 + * guest code. KVM selftests don't support dynamic loading in guest code and 7 + * will jump into the weeds if the compiler decides to insert an out-of-line 8 + * call via the PLT. 9 + */ 10 + int memcmp(const void *cs, const void *ct, size_t count) 11 + { 12 + const unsigned char *su1, *su2; 13 + int res = 0; 14 + 15 + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) { 16 + if ((res = *su1 - *su2) != 0) 17 + break; 18 + } 19 + return res; 20 + } 21 + 22 + void *memcpy(void *dest, const void *src, size_t count) 23 + { 24 + char *tmp = dest; 25 + const char *s = src; 26 + 27 + while (count--) 28 + *tmp++ = *s++; 29 + return dest; 30 + } 31 + 32 + void *memset(void *s, int c, size_t count) 33 + { 34 + char *xs = s; 35 + 36 + while (count--) 37 + *xs++ = c; 38 + return s; 39 + }