Skip to contentSkip to conversation

Add functional test for AMD VMSAVE/VMLOAD instructions

PATCH In review Martin Doucha 2 participants 6 messages May 14, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover Add functional test for AMD VMSAVE/VMLOAD instructions

Thanks to a minor bug in LTP KVM library, LTP test kvm_svm02 found a kernel bug in emulation of VMSAVE and VMLOAD instructions in nested VMs. Add a thorough functional test for both instructions which can detect and pinpoint emulation bugs.

Also implement basic printf-like formatting for tst_res() and tst_brk() so that the test can print incorrect register values to simplify result analysis. Only standard integer and character conversions are supported. Floating point number conversions, field alignment and padding are not implemented.

Martin Doucha (4):
KVM: Disable EBP register use in 32bit code
KVM: Implement strchr() and basic sprintf()
KVM: Implement printf-like formatting for tst_res() and tst_brk()
KVM: Add functional test for VMSAVE/VMLOAD instructions

 configure.ac                             |   2 +
 include/mk/config.mk.in                  |   1 +
 runtest/kvm                              |   1 +
 testcases/kernel/kvm/.gitignore          |   1 +
 testcases/kernel/kvm/Makefile            |   4 +
 testcases/kernel/kvm/include/kvm_guest.h |  19 +-
 testcases/kernel/kvm/kvm_svm04.c         | 307 ++++++++++++++++++++
 testcases/kernel/kvm/lib_guest.c         | 348 ++++++++++++++++++++++-
 8 files changed, 675 insertions(+), 8 deletions(-)
 create mode 100644 testcases/kernel/kvm/kvm_svm04.c
Signature · 5 lines
2.44.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v2 1/4 KVM: Disable EBP register use in 32bit code

The EBP register points to the stack segment by default but GCC uses it to access data segment without the proper prefix. This works fine on most systems because the stack and data segments are usually identical. However, KVM environment intentionally enforces strict limits on the stack segment and access to the data segment using unprefixed EBP would trigger stack segment fault exception in 32bit LTP builds (stack segment limits are ignored in 64bit mode).

---

Changes since v1:
- Detect -ffixed-ebp support in configure script and disable EBP conditionally

 configure.ac                  | 2 ++
 include/mk/config.mk.in       | 1 +
 testcases/kernel/kvm/Makefile | 4 ++++
 3 files changed, 7 insertions(+)
configure.ac +2 -0
index 1f7aa70bd..6d7009763 100644
@@ -393,7 +393,9 @@ LTP_CHECK_SYSCALL_FCNTL
 LTP_CHECK_FSVERITY
 
 AX_CHECK_COMPILE_FLAG([-no-pie], [LTP_CFLAGS_NOPIE=1])
+AX_CHECK_COMPILE_FLAG([-ffixed-ebp], [LTP_CFLAGS_FFIXED_EBP=1])
 AC_SUBST([LTP_CFLAGS_NOPIE])
+AC_SUBST([LTP_CFLAGS_FFIXED_EBP])
 
 if test "x$with_numa" = xyes; then
 	LTP_CHECK_SYSCALL_NUMA
include/mk/config.mk.in +1 -0
index 145b887fa..f6e02eaeb 100644
@@ -86,6 +86,7 @@ LDFLAGS			+= $(WLDFLAGS)
 CFLAGS			+= $(DEBUG_CFLAGS) $(OPT_CFLAGS) $(WCFLAGS) $(STDCFLAGS)
 
 LTP_CFLAGS_NOPIE	:= @LTP_CFLAGS_NOPIE@
+LTP_CFLAGS_FFIXED_EBP	:= @LTP_CFLAGS_FFIXED_EBP@
 
 ifeq ($(strip $(HOST_CFLAGS)),)
 HOST_CFLAGS := $(CFLAGS)
testcases/kernel/kvm/Makefile +4 -0
index ce4a5ede2..07bdd9705 100644
@@ -24,6 +24,10 @@ endif
 ifeq ($(HOST_CPU),x86)
 	GUEST_CFLAGS += -m32
 	ASFLAGS += --32
+
+	ifdef LTP_CFLAGS_FFIXED_EBP
+		GUEST_CFLAGS += -ffixed-ebp
+	endif
 endif
 
 # Some distros enable -pie by default. That breaks KVM payload linking.
Signature · 5 lines
2.44.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

PATCH v2 2/4 KVM: Implement strchr() and basic sprintf()

In reply to Martin Doucha

Add basic implementation of sprintf() that supports string, pointer and integer arguments but without advanced formatting options like field alignment and padding.

---

Changes since v1:
- #include <stdarg.h> instead of defining custom va_list macros

The C standard requires that <stdarg.h> must support freestanding environment so it's safe to #include it in KVM guest code.

 testcases/kernel/kvm/include/kvm_guest.h |   7 +
 testcases/kernel/kvm/lib_guest.c         | 312 +++++++++++++++++++++++
 2 files changed, 319 insertions(+)
testcases/kernel/kvm/include/kvm_guest.h +7 -0
index 96f246155..3cfafa313 100644
@@ -8,6 +8,8 @@
 #ifndef KVM_GUEST_H_
 #define KVM_GUEST_H_
 
+#include <stdarg.h>
+
 /* The main LTP include dir is intentionally excluded during payload build */
 #include "../../../../include/tst_res_flags.h"
 #undef TERRNO
@@ -49,6 +51,11 @@ void *memcpy(void *dest, const void *src, size_t size);
 char *strcpy(char *dest, const char *src);
 char *strcat(char *dest, const char *src);
 size_t strlen(const char *str);
+char *strchr(const char *s, int c);
+char *strrchr(const char *s, int c);
+
+int vsprintf(char *dest, const char *fmt, va_list ap);
+int sprintf(char *dest, const char *fmt, ...);
 
 /* Exit the VM by looping on a HLT instruction forever */
 void kvm_exit(void) __attribute__((noreturn));
testcases/kernel/kvm/lib_guest.c +312 -0

324 lines not on this page · raw message

Signature · 5 lines
2.44.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH v2 3/4 KVM: Implement printf-like formatting for tst_res() and tst_brk()

In reply to Martin Doucha

---

Changes since v1: None

 testcases/kernel/kvm/include/kvm_guest.h | 12 +++++---
 testcases/kernel/kvm/lib_guest.c         | 36 +++++++++++++++++++++---
 2 files changed, 40 insertions(+), 8 deletions(-)
testcases/kernel/kvm/include/kvm_guest.h +8 -4
index 3cfafa313..0eabfb9a0 100644
@@ -64,12 +64,16 @@ void kvm_exit(void) __attribute__((noreturn));
 void kvm_yield(void);
 
 void tst_res_(const char *file, const int lineno, int result,
-	const char *message);
-#define tst_res(result, msg) tst_res_(__FILE__, __LINE__, (result), (msg))
+	const char *fmt, ...)
+	__attribute__ ((format (printf, 4, 5)));
+#define tst_res(result, fmt, ...) \
+	tst_res_(__FILE__, __LINE__, (result), (fmt), ##__VA_ARGS__)
 
 void tst_brk_(const char *file, const int lineno, int result,
-	const char *message) __attribute__((noreturn));
-#define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))
+	const char *fmt, ...) __attribute__((noreturn))
+	__attribute__ ((format (printf, 4, 5)));
+#define tst_brk(result, fmt, ...) \
+	tst_brk_(__FILE__, __LINE__, (result), (fmt), ##__VA_ARGS__)
 
 /*
  * Send asynchronous notification to host without stopping VM execution and
testcases/kernel/kvm/lib_guest.c +32 -4
index 73a76ccb1..2e3e9cb6e 100644
@@ -443,6 +443,7 @@ static void tst_fatal_error(const char *file, const int lineno,
 	test_result->result = TBROK;
 	test_result->lineno = lineno;
 	test_result->file_addr = (uintptr_t)file;
+	/* Avoid sprintf() here in case of bugs */
 	strcpy(test_result->message, message);
 	strcat(test_result->message, " at address 0x");
 	ptr2hex(test_result->message + strlen(test_result->message), ip);
@@ -451,19 +452,46 @@ static void tst_fatal_error(const char *file, const int lineno,
 }
 
 void tst_res_(const char *file, const int lineno, int result,
-	const char *message)
+	const char *fmt, ...)
 {
+	va_list args;
+	int ret;
+
+	va_start(args, fmt);
 	test_result->result = result;
 	test_result->lineno = lineno;
 	test_result->file_addr = (uintptr_t)file;
-	strcpy(test_result->message, message);
+	ret = vsprintf(test_result->message, fmt, args);
+	va_end(args);
+
+	if (ret < 0) {
+		tst_brk_(file, lineno, TBROK, "Invalid tst_res() format: %s",
+			fmt);
+	}
+
 	kvm_yield();
 }
 
 void tst_brk_(const char *file, const int lineno, int result,
-	const char *message)
+	const char *fmt, ...)
 {
-	tst_res_(file, lineno, result, message);
+	va_list args;
+	int ret;
+
+	va_start(args, fmt);
+	test_result->result = result;
+	test_result->lineno = lineno;
+	test_result->file_addr = (uintptr_t)file;
+	ret = vsprintf(test_result->message, fmt, args);
+	va_end(args);
+
+	if (ret < 0) {
+		test_result->result = TBROK;
+		strcpy(test_result->message, "Invalid tst_brk() format: ");
+		strcat(test_result->message, fmt);
+	}
+
+	kvm_yield();
 	kvm_exit();
 }
 
Signature · 5 lines
2.44.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v2 4/4 KVM: Add functional test for VMSAVE/VMLOAD instructions

In reply to Martin Doucha

---

Changes since v1: None

 runtest/kvm                      |   1 +
 testcases/kernel/kvm/.gitignore  |   1 +
 testcases/kernel/kvm/kvm_svm04.c | 307 +++++++++++++++++++++++++++++++
 3 files changed, 309 insertions(+)
 create mode 100644 testcases/kernel/kvm/kvm_svm04.c
runtest/kvm +1 -0
index 9de846a09..74a517add 100644
@@ -1,5 +1,6 @@
 kvm_svm01 kvm_svm01
 kvm_svm02 kvm_svm02
 kvm_svm03 kvm_svm03
+kvm_svm04 kvm_svm04
 # Tests below may interfere with bug reproducibility
 kvm_pagefault01 kvm_pagefault01
testcases/kernel/kvm/.gitignore +1 -0
index 9638a6fc7..661472cae 100644
@@ -2,3 +2,4 @@
 /kvm_svm01
 /kvm_svm02
 /kvm_svm03
+/kvm_svm04
testcases/kernel/kvm/kvm_svm04.c new file +307 -0

307 lines not on this page · raw message

Signature · 5 lines
2.44.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH v2 4/4] KVM: Add functional test for VMSAVE/VMLOAD instructions

Hi Martin,

thanks a lot merged!

I particularly like the previous commit printf-like formatting implementation.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp