Skip to contentSkip to conversation

KVM testing

PATCH Superseded Martin Doucha 4 participants 13 messages Mar 31, 2022 – Apr 21, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover KVM testing

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. Basic test for CVE 2021-38198 written using the KVM infrastrucutre (pagetable permission check bypass under software emulated MMU) is included.

Martin Doucha (3):
Add AX_CHECK_COMPILE_FLAG() autoconf macro
KVM test infrastructure
Add test for CVE 2021-38198

 configure.ac                              |   3 +
 include/mk/config.mk.in                   |   2 +
 include/mk/env_post.mk                    |   1 +
 include/mk/rules.mk                       |   8 +
 m4/ax_check_compile_flag.m4               |  53 +++
 runtest/kvm                               |   1 +
 testcases/kernel/Makefile                 |   1 +
 testcases/kernel/kvm/.gitignore           |   1 +
 testcases/kernel/kvm/Makefile             |  62 +++
 testcases/kernel/kvm/bootstrap_x86.S      | 368 ++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S   | 515 ++++++++++++++++++++++
 testcases/kernel/kvm/include/kvm_common.h |  31 ++
 testcases/kernel/kvm/include/kvm_guest.h  |  82 ++++
 testcases/kernel/kvm/include/kvm_host.h   | 115 +++++
 testcases/kernel/kvm/include/kvm_test.h   |  23 +
 testcases/kernel/kvm/include/kvm_x86.h    | 157 +++++++
 testcases/kernel/kvm/kvm_pagefault01.c    | 234 ++++++++++
 testcases/kernel/kvm/lib_guest.c          | 183 ++++++++
 testcases/kernel/kvm/lib_host.c           | 215 +++++++++
 testcases/kernel/kvm/lib_x86.c            | 150 +++++++
 testcases/kernel/kvm/linker/payload.lds   |  11 +
 testcases/kernel/kvm/linker/x86.lds       |  74 ++++
 testcases/kernel/kvm/linker/x86_64.lds    |  74 ++++
 23 files changed, 2364 insertions(+)
 create mode 100644 m4/ax_check_compile_flag.m4
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/Makefile
 create mode 100644 testcases/kernel/kvm/bootstrap_x86.S
 create mode 100644 testcases/kernel/kvm/bootstrap_x86_64.S
 create mode 100644 testcases/kernel/kvm/include/kvm_common.h
 create mode 100644 testcases/kernel/kvm/include/kvm_guest.h
 create mode 100644 testcases/kernel/kvm/include/kvm_host.h
 create mode 100644 testcases/kernel/kvm/include/kvm_test.h
 create mode 100644 testcases/kernel/kvm/include/kvm_x86.h
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
 create mode 100644 testcases/kernel/kvm/lib_guest.c
 create mode 100644 testcases/kernel/kvm/lib_host.c
 create mode 100644 testcases/kernel/kvm/lib_x86.c
 create mode 100644 testcases/kernel/kvm/linker/payload.lds
 create mode 100644 testcases/kernel/kvm/linker/x86.lds
 create mode 100644 testcases/kernel/kvm/linker/x86_64.lds
Signature · 5 lines
2.35.1


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

PATCH v2 1/3 Add AX_CHECK_COMPILE_FLAG() autoconf macro

---

Changes since v1: New patch

 m4/ax_check_compile_flag.m4 | 53 +++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 m4/ax_check_compile_flag.m4
m4/ax_check_compile_flag.m4 new file +53 -0
new file mode 100644
index 000000000..bd753b34d
@@ -0,0 +1,53 @@
+# ===========================================================================
+#  https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT])
+#
+# DESCRIPTION
+#
+#   Check whether the given FLAG works with the current language's compiler
+#   or gives an error.  (Warnings, however, are ignored)
+#
+#   ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
+#   success/failure.
+#
+#   If EXTRA-FLAGS is defined, it is added to the current language's default
+#   flags (e.g. CFLAGS) when the check is done.  The check is thus made with
+#   the flags: "CFLAGS EXTRA-FLAGS FLAG".  This can for example be used to
+#   force the compiler to issue an error when a bad flag is given.
+#
+#   INPUT gives an alternative input source to AC_COMPILE_IFELSE.
+#
+#   NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
+#   macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
+#
+# LICENSE
+#
+#   Copyright (c) 2008 Guido U. Draheim <[email protected]>
+#   Copyright (c) 2011 Maarten Bosmans <[email protected]>
+#
+#   Copying and distribution of this file, with or without modification, are
+#   permitted in any medium without royalty provided the copyright notice
+#   and this notice are preserved.  This file is offered as-is, without any
+#   warranty.
+
+#serial 6
+
+AC_DEFUN([AX_CHECK_COMPILE_FLAG],
+[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
+AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
+AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
+  ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
+  _AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
+  AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
+    [AS_VAR_SET(CACHEVAR,[yes])],
+    [AS_VAR_SET(CACHEVAR,[no])])
+  _AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
+AS_VAR_IF(CACHEVAR,yes,
+  [m4_default([$2], :)],
+  [m4_default([$3], :)])
+AS_VAR_POPDEF([CACHEVAR])dnl
+])dnl AX_CHECK_COMPILE_FLAGS
Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 1/3
#2 lore raw

Re: [LTP] [PATCH v2 1/3] Add AX_CHECK_COMPILE_FLAG() autoconf macro

Hi Martin,

obviously ok.

Kind regards,
Petr

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

PATCH v2 2/3 KVM test infrastructure

In reply to Martin Doucha

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

---

Changes since v1:
- Fixed build on distros which enable -pie by default
- tst_res()/tst_brk() now print file:lineno even from inside KVM payload
- Refactored global GDT symbols in x86 bootstrap code for future use
- Removed unnecessary comments in host library

 configure.ac                              |   3 +
 include/mk/config.mk.in                   |   2 +
 include/mk/env_post.mk                    |   1 +
 include/mk/rules.mk                       |   8 +
 testcases/kernel/Makefile                 |   1 +
 testcases/kernel/kvm/Makefile             |  62 +++
 testcases/kernel/kvm/bootstrap_x86.S      | 368 ++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S   | 515 ++++++++++++++++++++++
 testcases/kernel/kvm/include/kvm_common.h |  31 ++
 testcases/kernel/kvm/include/kvm_guest.h  |  82 ++++
 testcases/kernel/kvm/include/kvm_host.h   | 115 +++++
 testcases/kernel/kvm/include/kvm_test.h   |  23 +
 testcases/kernel/kvm/include/kvm_x86.h    | 157 +++++++
 testcases/kernel/kvm/lib_guest.c          | 183 ++++++++
 testcases/kernel/kvm/lib_host.c           | 215 +++++++++
 testcases/kernel/kvm/lib_x86.c            | 150 +++++++
 testcases/kernel/kvm/linker/payload.lds   |  11 +
 testcases/kernel/kvm/linker/x86.lds       |  74 ++++
 testcases/kernel/kvm/linker/x86_64.lds    |  74 ++++
 19 files changed, 2075 insertions(+)
 create mode 100644 testcases/kernel/kvm/Makefile
 create mode 100644 testcases/kernel/kvm/bootstrap_x86.S
 create mode 100644 testcases/kernel/kvm/bootstrap_x86_64.S
 create mode 100644 testcases/kernel/kvm/include/kvm_common.h
 create mode 100644 testcases/kernel/kvm/include/kvm_guest.h
 create mode 100644 testcases/kernel/kvm/include/kvm_host.h
 create mode 100644 testcases/kernel/kvm/include/kvm_test.h
 create mode 100644 testcases/kernel/kvm/include/kvm_x86.h
 create mode 100644 testcases/kernel/kvm/lib_guest.c
 create mode 100644 testcases/kernel/kvm/lib_host.c
 create mode 100644 testcases/kernel/kvm/lib_x86.c
 create mode 100644 testcases/kernel/kvm/linker/payload.lds
 create mode 100644 testcases/kernel/kvm/linker/x86.lds
 create mode 100644 testcases/kernel/kvm/linker/x86_64.lds
configure.ac +3 -0
index 9b6d01f54..f0ea6acb4 100644
@@ -371,6 +371,9 @@ LTP_CHECK_SYSCALL_EVENTFD
 LTP_CHECK_SYSCALL_FCNTL
 LTP_CHECK_FSVERITY
 
+AX_CHECK_COMPILE_FLAG([-no-pie], [LTP_CFLAGS_NOPIE=1])
+AC_SUBST([LTP_CFLAGS_NOPIE])
+
 if test "x$with_numa" = xyes; then
 	LTP_CHECK_SYSCALL_NUMA
 	numa_error_msg="test requires libnuma development packages with LIBNUMA_API_VERSION >= 2"
include/mk/config.mk.in +2 -0
index 218447ef3..674843563 100644
@@ -83,6 +83,8 @@ WCFLAGS			?= -Wall -W @GCC_WARN_OLDSTYLE@
 LDFLAGS			+= $(WLDFLAGS)
 CFLAGS			+= $(DEBUG_CFLAGS) $(OPT_CFLAGS) $(WCFLAGS)
 
+LTP_CFLAGS_NOPIE	:= @LTP_CFLAGS_NOPIE@
+
 ifeq ($(strip $(HOST_CFLAGS)),)
 HOST_CFLAGS := $(CFLAGS)
 endif
include/mk/env_post.mk +1 -0
index ec045c40d..dc4df41d3 100644
@@ -32,6 +32,7 @@ ENV_POST_LOADED = 1
 # poor software design if you need more than one search directory, and
 # would suggest creating a general purpose static library to that end.
 vpath %.c $(abs_srcdir)
+vpath %.S $(abs_srcdir)
 
 # For config.h, et all.
 CPPFLAGS			+= -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/include/old/
include/mk/rules.mk +8 -0
index a60e6705a..32d8d05a7 100644
@@ -1,5 +1,13 @@
 target_rel_dir := $(if $(cwd_rel_from_top),$(cwd_rel_from_top)/,)
 
+%.o: %.S
+ifdef VERBOSE
+	$(AS) $(ASFLAGS) -c -o $@ $<
+else
+	@$(AS) $(ASFLAGS) -c -o $@ $<
+	@echo AS $(target_rel_dir)$@
+endif
+
 %.o: %.c
 ifdef VERBOSE
 	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
testcases/kernel/Makefile +1 -0
index d44856c57..c0fa24147 100644
@@ -29,6 +29,7 @@ SUBDIRS			+= connectors \
 			   io \
 			   ipc \
 			   irq \
+			   kvm \
 			   logging \
 			   mem \
 			   numa \
testcases/kernel/kvm/Makefile new file +62 -0

62 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86.S new file +368 -0

368 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86_64.S new file +515 -0

515 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_common.h new file +31 -0
new file mode 100644
index 000000000..4e81d8302
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Common definitions for communication between KVM guest and host.
+ */
+
+#ifndef KVM_COMMON_H_
+#define KVM_COMMON_H_
+
+#define KVM_TNONE	-1	/* "No result" status value */
+
+/*
+ * Result value indicating end of test. If the test program exits using
+ * the HLT instruction with any valid result value other than KVM_TEXIT or
+ * TBROK, KVM runner will automatically resume VM execution after printing
+ * the message.
+ */
+#define KVM_TEXIT	0xff
+
+#define KVM_RESULT_BASEADDR 0xfffff000
+#define KVM_RESULT_SIZE 0x1000
+
+struct tst_kvm_result {
+	int32_t result;
+	int32_t lineno;
+	uint64_t file_addr;
+	char message[0];
+};
+
+#endif /* KVM_COMMON_H_ */
testcases/kernel/kvm/include/kvm_guest.h new file +82 -0

82 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_host.h new file +115 -0

115 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_test.h new file +23 -0
new file mode 100644
index 000000000..55989eaa5
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal test library for KVM tests
+ */
+
+#ifndef KVM_TEST_H_
+#define KVM_TEST_H_
+
+#ifdef COMPILE_PAYLOAD
+
+  #include "kvm_guest.h"
+  #include "kvm_common.h"
+
+#else
+
+  #include "tst_test.h"
+  #include "kvm_host.h"
+
+#endif /* COMPILE_PAYLOAD */
+
+#endif /* KVM_TEST_H_ */
testcases/kernel/kvm/include/kvm_x86.h new file +157 -0

157 lines not on this page · raw message

testcases/kernel/kvm/lib_guest.c new file +183 -0

183 lines not on this page · raw message

testcases/kernel/kvm/lib_host.c new file +215 -0

215 lines not on this page · raw message

testcases/kernel/kvm/lib_x86.c new file +150 -0

150 lines not on this page · raw message

testcases/kernel/kvm/linker/payload.lds new file +11 -0
new file mode 100644
index 000000000..a544fd37a
@@ -0,0 +1,11 @@
+TARGET(binary)
+
+SECTIONS
+{
+	.data :
+	{
+		kvm_payload_start = .;
+		KEEP(*(.data))
+		kvm_payload_end = .;
+	}
+}
testcases/kernel/kvm/linker/x86.lds new file +74 -0

74 lines not on this page · raw message

testcases/kernel/kvm/linker/x86_64.lds new file +74 -0

74 lines not on this page · raw message

Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 2/3
#5 lore raw

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

Hello Martin,

Quoted 11 lines · Martin Doucha

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

Quoted 6 lines

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

Signature · 5 lines
Thank you,
Richard.

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

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

In reply to Martin Doucha

Hi!

Quoted 16 lines
testcases/kernel/kvm/include/kvm_guest.h new file +10 -0
new file mode 100644
index 000000000..edb1cb66d
@@ -0,0 +1,82 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal test library for KVM tests
+ */
+
+#ifndef KVM_GUEST_H_
+#define KVM_GUEST_H_
+

testcases/kernel/kvm/include/kvm_guest.h line 20

Quoted 10 lines
testcases/kernel/kvm/include/kvm_guest.h new file +10 -0
+/*
+ * These constants must match the value of corresponding constants defined
+ * in tst_res_flags.h.
+ */
+#define TPASS	0	/* Test passed flag */
+#define TFAIL	1	/* Test failed flag */
+#define TBROK	2	/* Test broken flag */
+#define TWARN	4	/* Test warning flag */
+#define TINFO	16	/* Test information flag */
+#define TCONF	32	/* Test not appropriate for configuration */

The tst_res_flags.h header has been created so that there will be no need to copy these definitions around like this. Why not just include the header?

Quoted 31 lines
testcases/kernel/kvm/include/kvm_guest.h new file +31 -0
+#define TST_TEST_TCONF(message) \
+	void main(void) { tst_brk(TCONF, message); }
+
+#define INTERRUPT_COUNT 32
+
+typedef unsigned long size_t;
+typedef long ssize_t;
+
+typedef signed char int8_t;
+typedef unsigned char uint8_t;
+typedef short int16_t;
+typedef unsigned short uint16_t;
+typedef int int32_t;
+typedef unsigned int uint32_t;
+typedef long long int64_t;
+typedef unsigned long long uint64_t;
+typedef unsigned long uintptr_t;
+
+#define NULL ((void *)0)
+
+void *memset(void *dest, int val, size_t size);
+void *memzero(void *dest, size_t size);
+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);
+
+/* Exit the VM by looping on a HLT instruction forever */
+void kvm_exit(void) __attribute__((noreturn));
+

testcases/kernel/kvm/include/kvm_guest.h line 62

Quoted 10 lines
testcases/kernel/kvm/include/kvm_guest.h new file +10 -0
+/* Exit the VM using the HLT instruction but allow resume */
+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))
+
+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))

I think that it may be slightly better to name these kvm_res() and kvm_brk() to make clear that these are used in the guest context and that the implementation is actually different. I guess that it would make sense to have all the functions called from the guest prefixed with kvm_ just to make the boundary clearer.

Quoted 447 lines
testcases/kernel/kvm/include/kvm_guest.h new file +18 -0
+void *tst_heap_alloc_aligned(size_t size, size_t align);
+void *tst_heap_alloc(size_t size);
 
+/* Arch dependent: */
+
+struct kvm_interrupt_frame;
+
+typedef int (*tst_interrupt_callback)(void *userdata,
+	struct kvm_interrupt_frame *ifrm, unsigned long errcode);
+
+extern const char *tst_interrupt_names[INTERRUPT_COUNT];
+
+void tst_set_interrupt_callback(unsigned int vector,
+	tst_interrupt_callback func, void *userdata);
+
+/* Get the instruction pointer from interrupt frame */
+uintptr_t kvm_get_interrupt_ip(const struct kvm_interrupt_frame *ifrm);
+
+#endif /* KVM_GUEST_H_ */
testcases/kernel/kvm/include/kvm_host.h new file +115 -0
new file mode 100644
index 000000000..52cc3f5e9
@@ -0,0 +1,115 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * KVM host library for setting up and running virtual machine tests. Tests
+ * can either use the default setup/run/host functions or use the advanced
+ * API to create customized VMs.
+ */
+
+/*
+ * Most basic usage:
+ *
+ * #include "kvm_test.h"
+ *
+ * #ifdef COMPILE_PAYLOAD
+ *
+ * void main(void)
+ * {
+ *	[VM guest code goes here]
+ * }
+ *
+ * #else
+ *
+ * [optional VM host setup/run/cleanup code goes here]
+ *
+ * static struct tst_test test = {
+ *	.test_all = tst_kvm_run,
+ *	.setup = tst_kvm_setup,
+ *	.cleanup = tst_kvm_cleanup,
+ * };
+ *
+ * #endif
+ */
+
+#ifndef KVM_HOST_H_
+#define KVM_HOST_H_
+
+#include <inttypes.h>
+#include <linux/kvm.h>
+#include "kvm_common.h"
+
+#define VM_KERNEL_BASEADDR 0x1000
+#define VM_RESET_BASEADDR 0xfffffff0
+#define VM_RESET_CODE_SIZE 8
+
+#define MIN_FREE_RAM (10 * 1024 * 1024)
+#define DEFAULT_RAM_SIZE (16 * 1024 * 1024)
+
+struct tst_kvm_instance {
+	int vm_fd, vcpu_fd;
+	struct kvm_run *vcpu_info;
+	size_t vcpu_info_size;
+	void *ram;
+	struct tst_kvm_result *result;
+};
+
+/* Test binary to be installed into the VM at VM_KERNEL_BASEADDR */
+extern const char kvm_payload_start[], kvm_payload_end[];
+
+/* CPU reset code to be installed into the VM at VM_RESET_BASEADDR */
+extern const unsigned char tst_kvm_reset_code[VM_RESET_CODE_SIZE];
+
+/* Default KVM test functions. */
+void tst_kvm_setup(void);
+void tst_kvm_run(void);
+void tst_kvm_cleanup(void);
+
+/*
+ * Validate KVM guest test result (usually passed via result->result) and
+ * fail with TBROK if the value cannot be safely passed to tst_res() or
+ * tst_brk().
+ */
+void tst_kvm_validate_result(int value);
+
+/*
+ * Allocate memory slot for the VM. The returned pointer is page-aligned
+ * so the actual requested base address is at ret[baseaddr % pagesize].
+ *
+ * The first argument is a VM file descriptor created by ioctl(KVM_CREATE_VM)
+ *
+ * The return value points to a guarded buffer and the user should not attempt
+ * to free() it. Any extra space added at the beginning or end for page
+ * alignment will be writable.
+ */
+void *tst_kvm_alloc_memory(int vm, unsigned int slot, uint64_t baseaddr,
+	size_t size, unsigned int flags);
+
+/*
+ * Find CPUIDs supported by KVM. x86_64 tests must set non-default CPUID,
+ * otherwise bootstrap will fail to initialize 64bit mode.
+ * Returns NULL if ioctl(KVM_GET_SUPPORTED_CPUID) is not supported.
+ *
+ * The argument is a file descriptor created by open("/dev/kvm")
+ */
+struct kvm_cpuid2 *tst_kvm_get_cpuid(int sysfd);
+
+/*
+ * Initialize the given KVM instance structure. Creates new KVM virtual machine
+ * with 1 virtual CPU, allocates VM RAM (max. 4GB minus one page) and
+ * shared result structure. KVM memory slots 0 and 1 will be set by this
+ * function.
+ */
+void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size);
+
+/*
+ * Execute the given KVM instance and print results.
+ */
+void tst_kvm_run_instance(struct tst_kvm_instance *inst);
+
+/*
+ * Close the given KVM instance.
+ */
+void tst_kvm_destroy_instance(struct tst_kvm_instance *inst);
+
+#endif /* KVM_HOST_H_ */
testcases/kernel/kvm/include/kvm_test.h new file +23 -0
new file mode 100644
index 000000000..55989eaa5
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal test library for KVM tests
+ */
+
+#ifndef KVM_TEST_H_
+#define KVM_TEST_H_
+
+#ifdef COMPILE_PAYLOAD
+
+  #include "kvm_guest.h"
+  #include "kvm_common.h"
+
+#else
+
+  #include "tst_test.h"
+  #include "kvm_host.h"
+
+#endif /* COMPILE_PAYLOAD */
+
+#endif /* KVM_TEST_H_ */
testcases/kernel/kvm/include/kvm_x86.h new file +157 -0
new file mode 100644
index 000000000..4f3671135
@@ -0,0 +1,157 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * x86-specific KVM helper functions and structures
+ */
+
+#ifndef KVM_X86_H_
+#define KVM_X86_H_
+
+#include "kvm_test.h"
+
+/* Interrupts */
+#define X86_INTR_COUNT 256
+
+#define INTR_ZERODIV 0
+#define INTR_DEBUG 1
+#define INTR_NMI 2
+#define INTR_BREAKPOINT 3
+#define INTR_OVERFLOW 4
+#define INTR_BOUND_RANGE_EXC 5
+#define INTR_BAD_OPCODE 6
+#define INTR_DEVICE_ERROR 7
+#define INTR_DOUBLE_FAULT 8
+#define INTR_INVALID_TSS 10
+#define INTR_SEGFAULT 11
+#define INTR_STACK_FAULT 12
+#define INTR_GPF 13
+#define INTR_PAGE_FAULT 14
+#define INTR_FPU_ERROR 16
+#define INTR_ALIGNMENT_ERROR 17
+#define INTR_MACHINE_CHECK 18
+#define INTR_SIMD_ERROR 19
+#define INTR_VIRT_ERROR 20
+#define INTR_CPE 21
+#define INTR_HV_INJECTION 28
+#define INTR_VMM_COMM 29
+#define INTR_SECURITY_ERROR 30
+
+
+/* CPUID constants */
+#define CPUID_GET_INPUT_RANGE 0x80000000
+#define CPUID_GET_EXT_FEATURES 0x80000001
+
+
+/* Model-specific CPU register constants */
+#define MSR_EFER 0xc0000080
+
+#define EFER_SCE (1 << 0)	/* SYSCALL/SYSRET instructions enabled */
+#define EFER_LME (1 << 8)	/* CPU is running in 64bit mode */
+#define EFER_LMA (1 << 10)	/* CPU uses 64bit memory paging (read-only) */
+#define EFER_NXE (1 << 11)	/* Execute disable bit active */
+
+
+/* Control register constants */
+#define CR4_VME (1 << 0)
+#define CR4_PVI (1 << 1)
+#define CR4_TSD (1 << 2)
+#define CR4_DE (1 << 3)
+#define CR4_PSE (1 << 4)
+#define CR4_PAE (1 << 5)
+#define CR4_MCE (1 << 6)
+#define CR4_PGE (1 << 7)
+#define CR4_PCE (1 << 8)
+#define CR4_OSFXSR (1 << 9)
+#define CR4_OSXMMEXCPT (1 << 10)
+#define CR4_UMIP (1 << 11)
+#define CR4_LA57 (1 << 12)
+#define CR4_VMXE (1 << 13)
+#define CR4_SMXE (1 << 14)
+#define CR4_FSGSBASE (1 << 16)
+#define CR4_PCIDE (1 << 17)
+#define CR4_OSXSAVE (1 << 18)
+#define CR4_KL (1 << 19)
+#define CR4_SMEP (1 << 20)
+#define CR4_SMAP (1 << 21)
+#define CR4_PKE (1 << 22)
+#define CR4_CET (1 << 23)
+#define CR4_PKS (1 << 24)
+
+struct intr_descriptor {
+	uint16_t offset_lo;
+	uint16_t selector;
+	uint8_t ist;
+	uint8_t flags;
+#if defined(__x86_64__)
+	uint64_t offset_hi; /* top 16 bits must be set to 0 */
+	uint16_t padding;
+#else /* defined(__x86_64__) */
+	uint16_t offset_hi;
+#endif /* defined(__x86_64__) */
+} __attribute__((__packed__));
+
+struct page_table_entry_pae {
+	unsigned int present: 1;
+	unsigned int writable: 1;
+	unsigned int user_access: 1;
+	unsigned int write_through: 1;
+	unsigned int disable_cache: 1;
+	unsigned int accessed: 1;
+	unsigned int dirty: 1;
+	unsigned int page_type: 1;
+	unsigned int global: 1;
+	unsigned int padding: 3;
+	uint64_t address: 40;
+	unsigned int padding2: 7;
+	unsigned int prot_key: 4;
+	unsigned int noexec: 1;
+} __attribute__((__packed__));
+
+struct kvm_cpuid {
+	unsigned int eax, ebx, ecx, edx;
+};
+
+struct kvm_cregs {
+	unsigned long cr0, cr2, cr3, cr4;
+};
+
+extern struct page_table_entry_pae kvm_pagetable[];
+extern struct intr_descriptor kvm_idt[X86_INTR_COUNT];
+
+/* Page table helper functions */
+uintptr_t kvm_get_page_address_pae(const struct page_table_entry_pae *entry);
+
+/* Functions for querying CPU info and status */
+void kvm_get_cpuid(unsigned int eax, unsigned int ecx, struct kvm_cpuid *buf);
+void kvm_read_cregs(struct kvm_cregs *buf);
+uint64_t kvm_rdmsr(unsigned int msr);
+void kvm_wrmsr(unsigned int msr, uint64_t value);
+
+/* Low-level interrupt handlers, DO NOT call directly */
+void kvm_handle_bad_exception(void);
+void kvm_handle_zerodiv(void);
+void kvm_handle_debug(void);
+void kvm_handle_nmi(void);
+void kvm_handle_breakpoint(void);
+void kvm_handle_overflow(void);
+void kvm_handle_bound_range_exc(void);
+void kvm_handle_bad_opcode(void);
+void kvm_handle_device_error(void);
+void kvm_handle_double_fault(void);
+void kvm_handle_invalid_tss(void);
+void kvm_handle_segfault(void);
+void kvm_handle_stack_fault(void);
+void kvm_handle_gpf(void);
+void kvm_handle_page_fault(void);
+void kvm_handle_fpu_error(void);
+void kvm_handle_alignment_error(void);
+void kvm_handle_machine_check(void);
+void kvm_handle_simd_error(void);
+void kvm_handle_virt_error(void);
+void kvm_handle_cpe(void);
+void kvm_handle_hv_injection(void);
+void kvm_handle_vmm_comm(void);
+void kvm_handle_security_error(void);
+
+#endif /* KVM_X86_H_ */
testcases/kernel/kvm/lib_guest.c new file +109 -0
new file mode 100644
index 000000000..f5dfbc46f
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal testing library for KVM tests
+ */
+
+#include "kvm_test.h"
+
+extern char kvm_heap_begin[];
+
+static struct tst_kvm_result *const test_result =
+	(struct tst_kvm_result *)KVM_RESULT_BASEADDR;
+
+static char *heap_end = kvm_heap_begin;
+
+static struct tst_intr_handler {
+	tst_interrupt_callback callback;
+	void *userdata;
+} intr_handlers[INTERRUPT_COUNT];
+
+void *memset(void *dest, int val, size_t size)
+{
+	char *ptr = dest;
+
+	while (size--)
+		*ptr++ = val;
+
+	return dest;
+}
+
+void *memzero(void *dest, size_t size)
+{
+	return memset(dest, 0, size);
+}
+
+void *memcpy(void *dest, const void *src, size_t size)
+{
+	char *dptr = dest;
+	const char *sptr = src;
+
+	while (size--)
+		*dptr++ = *sptr++;
+
+	return dest;
+}
+
+char *strcpy(char *dest, const char *src)
+{
+	char *ret = dest;
+
+	while ((*dest++ = *src++))
+		;
+
+	return ret;
+}
+
+char *strcat(char *dest, const char *src)
+{
+	char *ret = dest;
+
+	for (; *dest; dest++)
+		;
+
+	strcpy(dest, src);
+	return ret;
+}
+
+size_t strlen(const char *str)
+{
+	size_t ret;
+
+	for (ret = 0; str[ret]; ret++)
+		;
+
+	return ret;
+}
+
+char *ptr2hex(char *dest, uintptr_t val)
+{
+	unsigned int i;
+	uintptr_t tmp;
+	char *ret = dest;
+
+	for (i = 4; val >> i; i += 4)
+		;
+
+	do {
+		i -= 4;
+		tmp = (val >> i) & 0xf;
+		*dest++ = tmp + (tmp >= 10 ? 'A' - 10 : '0');
+	} while (i);
+
+	*dest = '\0';
+	return ret;
+}
+
+void *tst_heap_alloc_aligned(size_t size, size_t align)
+{
+	uintptr_t addr = (uintptr_t)heap_end;
+	void *ret;
+
+	addr += align - 1;
+	addr -= addr % align;
+	ret = (void *)addr;
+	addr += size + 3;
+	addr -= addr % 4;
+	heap_end = (char *)addr;
+	return ret;

testcases/kernel/kvm/lib_guest.c line 119

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c new file +10 -0
+}
+
+void *tst_heap_alloc(size_t size)
+{
+	void *ret = heap_end;
+
+	size += 3;
+	size -= size % 4;
+	heap_end += size;
+	return ret;

We do have a few similar pieces scattered around the code, can put the code into a macro that would make the code slightly easier to understand?

We do have LTP_ALIGN() in tst_common.h but I guess that we do not want to include it here in the guest library.

Other than the very minor comments the code looks good.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

Quoted 15 lines · Cyril Hrubis

Hi!

testcases/kernel/kvm/include/kvm_guest.h line 20

Quoted 10 lines
testcases/kernel/kvm/include/kvm_guest.h new file +10 -0
+/*
+ * These constants must match the value of corresponding constants defined
+ * in tst_res_flags.h.
+ */
+#define TPASS	0	/* Test passed flag */
+#define TFAIL	1	/* Test failed flag */
+#define TBROK	2	/* Test broken flag */
+#define TWARN	4	/* Test warning flag */
+#define TINFO	16	/* Test information flag */
+#define TCONF	32	/* Test not appropriate for configuration */

The tst_res_flags.h header has been created so that there will be no need to copy these definitions around like this. Why not just include the header?

I didn't #include the tst_res_flags.h because the main LTP include directory is not passed to the preprocessor when compiling KVM payload. I'd have to do it like this:
#include "../../../../include/tst_res_flags.h"

Quoted 5 lines
Quoted 5 lines
testcases/kernel/kvm/include/kvm_guest.h new file +5 -0
+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))
+
+void tst_brk_(const char *file, const int lineno, int result,
Quoted 8 lines

testcases/kernel/kvm/include/kvm_guest.h line 62

Quoted 2 lines
testcases/kernel/kvm/include/kvm_guest.h new file +2 -0
+	const char *message) __attribute__((noreturn));
+#define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))

I think that it may be slightly better to name these kvm_res() and kvm_brk() to make clear that these are used in the guest context and that the implementation is actually different. I guess that it would make sense to have all the functions called from the guest prefixed with kvm_ just to make the boundary clearer.

My idea was different:
- kvm_* prefix for low-level arch-specific functions
- tst_* prefix for reimplementation of standard LTP library functions and any other platform-independent helper functions

tst_res() and tst_brk() currently lack the printf-like formatting features (and the TERRNO/TTERRNO/TRERRNO flags are not applicable) but otherwise they do the exact same thing as the standard LTP functions. I'm planning to add some limited printf-like formatting later since sprintf() implementation will be needed eventually anyway.

Quoted 27 lines
Quoted 12 lines
testcases/kernel/kvm/lib_guest.c new file +12 -0
+void *tst_heap_alloc_aligned(size_t size, size_t align)
+{
+	uintptr_t addr = (uintptr_t)heap_end;
+	void *ret;
+
+	addr += align - 1;
+	addr -= addr % align;
+	ret = (void *)addr;
+	addr += size + 3;
+	addr -= addr % 4;
+	heap_end = (char *)addr;
+	return ret;

testcases/kernel/kvm/lib_guest.c line 119

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c new file +10 -0
+}
+
+void *tst_heap_alloc(size_t size)
+{
+	void *ret = heap_end;
+
+	size += 3;
+	size -= size % 4;
+	heap_end += size;
+	return ret;

We do have a few similar pieces scattered around the code, can put the code into a macro that would make the code slightly easier to understand?

Quoted 2 lines

We do have LTP_ALIGN() in tst_common.h but I guess that we do not want to include it here in the guest library.

I guess that copying LTP_ALIGN() to the KVM headers would be useful.

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

Hi!

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
testcases/kernel/kvm/include/kvm_guest.h new file +6 -0
+/*
+ * These constants must match the value of corresponding constants defined
+ * in tst_res_flags.h.
+ */
+#define TPASS	0	/* Test passed flag */
+#define TFAIL	1	/* Test failed flag */
Quoted 8 lines

testcases/kernel/kvm/include/kvm_guest.h line 20

Quoted 4 lines
testcases/kernel/kvm/include/kvm_guest.h new file +4 -0
+#define TBROK	2	/* Test broken flag */
+#define TWARN	4	/* Test warning flag */
+#define TINFO	16	/* Test information flag */
+#define TCONF	32	/* Test not appropriate for configuration */

The tst_res_flags.h header has been created so that there will be no need to copy these definitions around like this. Why not just include the header?

Quoted 4 lines

I didn't #include the tst_res_flags.h because the main LTP include directory is not passed to the preprocessor when compiling KVM payload. I'd have to do it like this:
#include "../../../../include/tst_res_flags.h"

And I guess that this is on purpose right?

As we can easily add it by adding -I$(top_src_dir)/include/ to the CFLAGS.

Quoted 19 lines
Quoted 5 lines
Quoted 5 lines
testcases/kernel/kvm/include/kvm_guest.h new file +5 -0
+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))
+
+void tst_brk_(const char *file, const int lineno, int result,
Quoted 8 lines

testcases/kernel/kvm/include/kvm_guest.h line 62

Quoted 2 lines
testcases/kernel/kvm/include/kvm_guest.h new file +2 -0
+	const char *message) __attribute__((noreturn));
+#define tst_brk(result, msg) tst_brk_(__FILE__, __LINE__, (result), (msg))

I think that it may be slightly better to name these kvm_res() and kvm_brk() to make clear that these are used in the guest context and that the implementation is actually different. I guess that it would make sense to have all the functions called from the guest prefixed with kvm_ just to make the boundary clearer.

My idea was different:
- kvm_* prefix for low-level arch-specific functions
- tst_* prefix for reimplementation of standard LTP library functions and any other platform-independent helper functions

Quoted 5 lines

tst_res() and tst_brk() currently lack the printf-like formatting features (and the TERRNO/TTERRNO/TRERRNO flags are not applicable) but otherwise they do the exact same thing as the standard LTP functions. I'm planning to add some limited printf-like formatting later since sprintf() implementation will be needed eventually anyway.

Ack.

Quoted 27 lines
Quoted 27 lines
Quoted 12 lines
testcases/kernel/kvm/lib_guest.c new file +12 -0
+void *tst_heap_alloc_aligned(size_t size, size_t align)
+{
+	uintptr_t addr = (uintptr_t)heap_end;
+	void *ret;
+
+	addr += align - 1;
+	addr -= addr % align;
+	ret = (void *)addr;
+	addr += size + 3;
+	addr -= addr % 4;
+	heap_end = (char *)addr;
+	return ret;

testcases/kernel/kvm/lib_guest.c line 119

Quoted 10 lines
testcases/kernel/kvm/lib_guest.c new file +10 -0
+}
+
+void *tst_heap_alloc(size_t size)
+{
+	void *ret = heap_end;
+
+	size += 3;
+	size -= size % 4;
+	heap_end += size;
+	return ret;

We do have a few similar pieces scattered around the code, can put the code into a macro that would make the code slightly easier to understand?

Quoted 4 lines
Quoted 2 lines

We do have LTP_ALIGN() in tst_common.h but I guess that we do not want to include it here in the guest library.

I guess that copying LTP_ALIGN() to the KVM headers would be useful.

Can you please respin the patchset with this change then?

Signature · 5 lines
Cyril Hrubis
[email protected]

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

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

Quoted 9 lines · Cyril Hrubis
Quoted 4 lines

I didn't #include the tst_res_flags.h because the main LTP include directory is not passed to the preprocessor when compiling KVM payload. I'd have to do it like this:
#include "../../../../include/tst_res_flags.h"

And I guess that this is on purpose right?

As we can easily add it by adding -I$(top_src_dir)/include/ to the CFLAGS.

Yes, this is on purpose. I want any #include of standard LTP headers to trigger error because the vast majority of the functions declared in there are not available in KVM guest library. So should I add the dirty #include or should I keep the duplicate definition of tst_res flags?

Quoted 3 lines
Quoted 1 line

I guess that copying LTP_ALIGN() to the KVM headers would be useful.

Can you please respin the patchset with this change then?

Yes, as soon as we agree on the #includes above.

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

Re: [LTP] [PATCH v2 2/3] KVM test infrastructure

Hi!

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

I didn't #include the tst_res_flags.h because the main LTP include directory is not passed to the preprocessor when compiling KVM payload. I'd have to do it like this:
#include "../../../../include/tst_res_flags.h"

And I guess that this is on purpose right?

Quoted 7 lines
Quoted 2 lines

As we can easily add it by adding -I$(top_src_dir)/include/ to the CFLAGS.

Yes, this is on purpose. I want any #include of standard LTP headers to trigger error because the vast majority of the functions declared in there are not available in KVM guest library. So should I add the dirty #include or should I keep the duplicate definition of tst_res flags?

I wonder what is cleaner solution. I do not like either one that much to be honest, but I guess that I slightly prefer the ugly relative path over copying the definitions over.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH v2 3/3 Add test for CVE 2021-38198

In reply to Martin Doucha

---

Changes since v1:
- Updated .save_restore metadata to the new structure
- Use tst_parse_int() instead of sscanf()

 runtest/kvm                            |   1 +
 testcases/kernel/kvm/.gitignore        |   1 +
 testcases/kernel/kvm/kvm_pagefault01.c | 234 +++++++++++++++++++++++++
 3 files changed, 236 insertions(+)
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
runtest/kvm new file +1 -0
new file mode 100644
index 000000000..16e7c07ff
@@ -0,0 +1 @@
+kvm_pagefault01 kvm_pagefault01
testcases/kernel/kvm/.gitignore new file +1 -0
new file mode 100644
index 000000000..349260359
@@ -0,0 +1 @@
+/kvm_pagefault01
testcases/kernel/kvm/kvm_pagefault01.c new file +234 -0

234 lines not on this page · raw message

Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp