Skip to contentSkip to conversation

Add #VC exception handling for AMD SEV-ES

PATCH In review Varad Gautam 5 participants 26 messages Feb 24, 2022 – Mar 29, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover Add #VC exception handling for AMD SEV-ES

For AMD SEV-ES, kvm-unit-tests currently rely on UEFI to set up a #VC exception handler. This leads to the following problems:

1) The test's page table needs to map the firmware and the shared GHCB used by the firmware.
2) The firmware needs to keep its #VC handler in the current IDT so that kvm-unit-tests can copy the #VC entry into its own IDT.
3) The firmware #VC handler might use state which is not available anymore after ExitBootServices.
4) After ExitBootServices, the firmware needs to get the GHCB address from the GHCB MSR if it needs to use the kvm-unit-test GHCB. This requires keeping an identity mapping, and the GHCB address must be in the MSR at all times where a #VC could happen.

Problems 1) and 2) were temporarily mitigated via commits b114aa57ab ("x86 AMD SEV-ES: Set up GHCB page") and 706ede1833 ("x86 AMD SEV-ES:
Copy UEFI #VC IDT entry") respectively.

However, to make kvm-unit-tests reliable against 3) and 4), the tests must supply their own #VC handler [1][2].

This series adds #VC exception processing from Linux into kvm-unit-tests, and makes it the default way of handling #VC exceptions.

If --amdsev-efi-vc is passed during ./configure, the tests will continue using the UEFI #VC handler.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/YSA%[email protected]/

Git branch: https://github.com/varadgautam/kvm-unit-tests/commits/vc-handler-v3

v3:
- Reduce the diff between insn decoder code imported into kvm-unit-tests and the original code in Linux; cleanup #VC handling.

v2:
- Drop #VC processing code for RDTSC/RDTSCP and WBINVD (seanjc). KVM does not trap RDTSC/RDTSCP, and the tests do not produce a WBINVD exit to be handled.
- Clarify the rationale for tests needing their own #VC handler (marcorr).

Varad Gautam (11):
x86: AMD SEV-ES: Setup #VC exception handler for AMD SEV-ES
x86: Move svm.h to lib/x86/
lib: Define unlikely()/likely() macros in libcflat.h
lib: x86: Import insn decoder from Linux
x86: AMD SEV-ES: Pull related GHCB definitions and helpers from Linux
x86: AMD SEV-ES: Prepare for #VC processing
lib/x86: Move xsave helpers to lib/
x86: AMD SEV-ES: Handle CPUID #VC
x86: AMD SEV-ES: Handle MSR #VC
x86: AMD SEV-ES: Handle IOIO #VC
x86: AMD SEV-ES: Handle string IO for IOIO #VC

 .gitignore                         |    2 +
 Makefile                           |    3 +
 configure                          |   21 +
 lib/libcflat.h                     |    3 +
 lib/x86/amd_sev.c                  |   13 +-
 lib/x86/amd_sev.h                  |   96 +++
 lib/x86/amd_sev_vc.c               |  494 +++++++++++++
 lib/x86/desc.c                     |   15 +
 lib/x86/desc.h                     |    1 +
 lib/x86/insn/README                |   23 +
 lib/x86/insn/gen-insn-attr-x86.awk |  444 +++++++++++
 lib/x86/insn/inat.c                |   86 +++
 lib/x86/insn/inat.h                |  233 ++++++
 lib/x86/insn/inat_types.h          |   18 +
 lib/x86/insn/insn.c                |  749 +++++++++++++++++++
 lib/x86/insn/insn.h                |  279 +++++++
 lib/x86/insn/insn_glue.h           |   32 +
 lib/x86/insn/x86-opcode-map.txt    | 1106 ++++++++++++++++++++++++++++
 lib/x86/msr.h                      |    1 +
 lib/x86/processor.h                |   16 +
 lib/x86/setup.c                    |    8 +
 {x86 => lib/x86}/svm.h             |   40 +-
 lib/x86/xsave.c                    |   37 +
 lib/x86/xsave.h                    |   16 +
 x86/Makefile.common                |   16 +-
 x86/Makefile.x86_64                |    1 +
 x86/kvmclock.c                     |    4 -
 x86/svm.c                          |    2 +-
 x86/svm_tests.c                    |    2 +-
 x86/xsave.c                        |   43 +-
 30 files changed, 3745 insertions(+), 59 deletions(-)
 create mode 100644 lib/x86/amd_sev_vc.c
 create mode 100644 lib/x86/insn/README
 create mode 100644 lib/x86/insn/gen-insn-attr-x86.awk
 create mode 100644 lib/x86/insn/inat.c
 create mode 100644 lib/x86/insn/inat.h
 create mode 100644 lib/x86/insn/inat_types.h
 create mode 100644 lib/x86/insn/insn.c
 create mode 100644 lib/x86/insn/insn.h
 create mode 100644 lib/x86/insn/insn_glue.h
 create mode 100644 lib/x86/insn/x86-opcode-map.txt
 rename {x86 => lib/x86}/svm.h (93%)
 create mode 100644 lib/x86/xsave.c
 create mode 100644 lib/x86/xsave.h
Signature · 1 line
2.32.0
#1 lore raw

PATCH v3 1/11 x86: AMD SEV-ES: Setup #VC exception handler for AMD SEV-ES

AMD SEV-ES defines a new guest exception that gets triggered on some vmexits to allow the guest to control what state gets shared with the host. kvm-unit-tests currently relies on UEFI to provide this #VC exception handler. This leads to the following problems:

1) The test's page table needs to map the firmware and the shared GHCB used by the firmware.
2) The firmware needs to keep its #VC handler in the current IDT so that kvm-unit-tests can copy the #VC entry into its own IDT.
3) The firmware #VC handler might use state which is not available anymore after ExitBootServices.
4) After ExitBootServices, the firmware needs to get the GHCB address from the GHCB MSR if it needs to use the kvm-unit-test GHCB. This requires keeping an identity mapping, and the GHCB address must be in the MSR at all times where a #VC could happen.

Problems 1) and 2) were temporarily mitigated via commits b114aa57ab ("x86 AMD SEV-ES: Set up GHCB page") and 706ede1833 ("x86 AMD SEV-ES:
Copy UEFI #VC IDT entry") respectively.

However, to make kvm-unit-tests reliable against 3) and 4), the tests must supply their own #VC handler [1][2].

Switch the tests to install a #VC handler on early bootup, just after GHCB has been mapped. The tests will use this handler by default. If --amdsev-efi-vc is passed during ./configure, the tests will continue using the UEFI #VC handler.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/YSA%[email protected]/

---
 Makefile             |  3 +++
 configure            | 21 +++++++++++++++++++++
 lib/x86/amd_sev.c    | 13 +++++--------
 lib/x86/amd_sev.h    |  1 +
 lib/x86/amd_sev_vc.c | 14 ++++++++++++++
 lib/x86/desc.c       | 15 +++++++++++++++
 lib/x86/desc.h       |  1 +
 lib/x86/setup.c      |  8 ++++++++
 x86/Makefile.common  |  1 +
 9 files changed, 69 insertions(+), 8 deletions(-)
 create mode 100644 lib/x86/amd_sev_vc.c
Makefile +3 -0
index 4f4ad23..94a0162 100644
@@ -46,6 +46,9 @@ else
 $(error Cannot build $(ARCH_NAME) tests as EFI apps)
 endif
 EFI_CFLAGS := -DTARGET_EFI
+ifeq ($(AMDSEV_EFI_VC),y)
+EFI_CFLAGS += -DAMDSEV_EFI_VC
+endif
 # The following CFLAGS and LDFLAGS come from:
 #   - GNU-EFI/Makefile.defaults
 #   - GNU-EFI/apps/Makefile
configure +21 -0
index 2d9c3e0..148d051 100755
@@ -30,6 +30,12 @@ gen_se_header=
 page_size=
 earlycon=
 target_efi=
+# For AMD SEV-ES, the tests build to use their own #VC exception handler
+# by default, instead of using the one installed by UEFI. This ensures
+# that the tests do not depend on UEFI state after ExitBootServices.
+# To continue using the UEFI #VC handler, ./configure can be run with
+# --amdsev-efi-vc.
+amdsev_efi_vc=
 
 usage() {
     cat <<-EOF
@@ -75,6 +81,8 @@ usage() {
 	                           Specify a PL011 compatible UART at address ADDR. Supported
 	                           register stride is 32 bit only.
 	    --target-efi           Boot and run from UEFI
+	    --amdsev-efi-vc        Use UEFI-provided #VC handlers on AMD SEV/ES. Requires
+	                           --target-efi.
 EOF
     exit 1
 }
@@ -145,6 +153,9 @@ while [[ "$1" = -* ]]; do
 	--target-efi)
 	    target_efi=y
 	    ;;
+	--amdsev-efi-vc)
+	    amdsev_efi_vc=y
+	    ;;
 	--help)
 	    usage
 	    ;;
@@ -204,8 +215,17 @@ elif [ "$processor" = "arm" ]; then
     processor="cortex-a15"
 fi
 
+if [ "$amdsev_efi_vc" ] && [ "$arch" != "x86_64" ]; then
+    echo "--amdsev-efi-vc requires arch x86_64."
+    usage
+fi
+
 if [ "$arch" = "i386" ] || [ "$arch" = "x86_64" ]; then
     testdir=x86
+    if [ "$amdsev_efi_vc" ] && [ -z "$target_efi" ]; then
+        echo "--amdsev-efi-vc requires --target-efi."
+        usage
+    fi
 elif [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then
     testdir=arm
     if [ "$target" = "qemu" ]; then
@@ -363,6 +383,7 @@ WA_DIVIDE=$wa_divide
 GENPROTIMG=${GENPROTIMG-genprotimg}
 HOST_KEY_DOCUMENT=$host_key_document
 TARGET_EFI=$target_efi
+AMDSEV_EFI_VC=$amdsev_efi_vc
 GEN_SE_HEADER=$gen_se_header
 EOF
 if [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then
lib/x86/amd_sev.c +5 -8
index 6672214..987b59f 100644
@@ -14,6 +14,7 @@
 #include "x86/vm.h"
 
 static unsigned short amd_sev_c_bit_pos;
+phys_addr_t ghcb_addr;
 
 bool amd_sev_enabled(void)
 {
@@ -100,14 +101,10 @@ efi_status_t setup_amd_sev_es(void)
 
 	/*
 	 * Copy UEFI's #VC IDT entry, so KVM-Unit-Tests can reuse it and does
-	 * not have to re-implement a #VC handler. Also update the #VC IDT code
-	 * segment to use KVM-Unit-Tests segments, KERNEL_CS, so that we do not
+	 * not have to re-implement a #VC handler for #VC exceptions before
+	 * GHCB is mapped. Also update the #VC IDT code segment to use
+	 * KVM-Unit-Tests segments, KERNEL_CS, so that we do not
 	 * have to copy the UEFI GDT entries into KVM-Unit-Tests GDT.
-	 *
-	 * TODO: Reusing UEFI #VC handler is a temporary workaround to simplify
-	 * the boot up process, the long-term solution is to implement a #VC
-	 * handler in kvm-unit-tests and load it, so that kvm-unit-tests does
-	 * not depend on specific UEFI #VC handler implementation.
 	 */
 	sidt(&idtr);
 	idt = (idt_entry_t *)idtr.base;
@@ -126,7 +123,7 @@ void setup_ghcb_pte(pgd_t *page_table)
 	 * function searches GHCB's L1 pte, creates corresponding L1 ptes if not
 	 * found, and unsets the c-bit of GHCB's L1 pte.
 	 */
-	phys_addr_t ghcb_addr, ghcb_base_addr;
+	phys_addr_t ghcb_base_addr;
 	pteval_t *pte;
 
 	/* Read the current GHCB page addr */
lib/x86/amd_sev.h +1 -0
index 6a10f84..afbacf3 100644
@@ -54,6 +54,7 @@ efi_status_t setup_amd_sev(void);
 bool amd_sev_es_enabled(void);
 efi_status_t setup_amd_sev_es(void);
 void setup_ghcb_pte(pgd_t *page_table);
+void handle_sev_es_vc(struct ex_regs *regs);
 
 unsigned long long get_amd_sev_c_bit_mask(void);
 unsigned long long get_amd_sev_addr_upperbound(void);
lib/x86/amd_sev_vc.c new file +14 -0
new file mode 100644
index 0000000..8226121
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#include "amd_sev.h"
+
+extern phys_addr_t ghcb_addr;
+
+void handle_sev_es_vc(struct ex_regs *regs)
+{
+	struct ghcb *ghcb = (struct ghcb *) ghcb_addr;
+	if (!ghcb) {
+		/* TODO: kill guest */
+		return;
+	}
+}
lib/x86/desc.c +15 -0
index c2eb16e..564efb0 100644
@@ -3,6 +3,9 @@
 #include "processor.h"
 #include <setjmp.h>
 #include "apic-defs.h"
+#ifdef TARGET_EFI
+#include "amd_sev.h"
+#endif
 
 /* Boot-related data structures */
 
@@ -228,6 +231,9 @@ EX_E(ac, 17);
 EX(mc, 18);
 EX(xm, 19);
 EX_E(cp, 21);
+#ifdef TARGET_EFI
+EX_E(vc, 29);
+#endif
 
 asm (".pushsection .text \n\t"
      "__handle_exception: \n\t"
@@ -293,6 +299,15 @@ void setup_idt(void)
     handle_exception(13, check_exception_table);
 }
 
+void setup_amd_sev_es_vc(void)
+{
+	if (!amd_sev_es_enabled())
+		return;
+
+	set_idt_entry(29, &vc_fault, 0);
+	handle_exception(29, handle_sev_es_vc);
+}
+
 unsigned exception_vector(void)
 {
     unsigned char vector;
lib/x86/desc.h +1 -0
index ad6277b..6f8f213 100644
@@ -225,6 +225,7 @@ void print_current_tss_info(void);
 handler handle_exception(u8 v, handler fn);
 void unhandled_exception(struct ex_regs *regs, bool cpu);
 const char* exception_mnemonic(int vector);
+void setup_amd_sev_es_vc(void);
 
 bool test_for_exception(unsigned int ex, void (*trigger_func)(void *data),
 			void *data);
lib/x86/setup.c +8 -0
index bbd3468..9de946b 100644
@@ -327,6 +327,14 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
 	smp_init();
 	setup_page_table();
 
+#ifndef AMDSEV_EFI_VC
+	/*
+	 * Switch away from the UEFI-installed #VC handler.
+	 * GHCB has already been mapped at this point.
+	 */
+	setup_amd_sev_es_vc();
+#endif /* AMDSEV_EFI_VC */
+
 	return EFI_SUCCESS;
 }
 
x86/Makefile.common +1 -0
index ff02d98..ae426aa 100644
@@ -24,6 +24,7 @@ cflatobjs += lib/x86/fault_test.o
 cflatobjs += lib/x86/delay.o
 ifeq ($(TARGET_EFI),y)
 cflatobjs += lib/x86/amd_sev.o
+cflatobjs += lib/x86/amd_sev_vc.o
 cflatobjs += lib/efi.o
 cflatobjs += x86/efi/reloc_x86_64.o
 endif
Signature · 1 line
2.32.0
#2 lore raw

PATCH v3 2/11 x86: Move svm.h to lib/x86/

In reply to Varad Gautam

to share common definitions across testcases and lib/.

---
 {x86 => lib/x86}/svm.h | 0
 x86/svm.c              | 2 +-
 x86/svm_tests.c        | 2 +-
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename {x86 => lib/x86}/svm.h (100%)
lib/x86/svm.h renamed from x86/svm.h +0 -0
similarity index 100%
rename from x86/svm.h
rename to lib/x86/svm.h
x86/svm.c +1 -1
index 3f94b2a..7cfef9e 100644
@@ -2,7 +2,7 @@
  * Framework for testing nested virtualization
  */
 
-#include "svm.h"
+#include "x86/svm.h"
 #include "libcflat.h"
 #include "processor.h"
 #include "desc.h"
x86/svm_tests.c +1 -1
index 0707786..7756296 100644
@@ -1,4 +1,4 @@
-#include "svm.h"
+#include "x86/svm.h"
 #include "libcflat.h"
 #include "processor.h"
 #include "desc.h"
Signature · 1 line
2.32.0
#3 lore raw

PATCH v3 3/11 lib: Define unlikely()/likely() macros in libcflat.h

In reply to Varad Gautam

So that they can be shared across testcases and lib/. Linux's x86 instruction decoder refrences them.

---
 lib/libcflat.h | 3 +++
 x86/kvmclock.c | 4 ----
 2 files changed, 3 insertions(+), 4 deletions(-)
lib/libcflat.h +3 -0
index c1fd31f..0cfd1bc 100644
@@ -166,4 +166,7 @@ extern void setup_vm(void);
 #define SZ_1G			(1 << 30)
 #define SZ_2G			(1ul << 31)
 
+#define unlikely(x)	__builtin_expect(!!(x), 0)
+#define likely(x)	__builtin_expect(!!(x), 1)
+
 #endif
x86/kvmclock.c +0 -4
index de30a5e..cae867f 100644
@@ -5,10 +5,6 @@
 #include "kvmclock.h"
 #include "asm/barrier.h"
 
-#define unlikely(x)	__builtin_expect(!!(x), 0)
-#define likely(x)	__builtin_expect(!!(x), 1)
-
-
 struct pvclock_vcpu_time_info __attribute__((aligned(4))) hv_clock[MAX_CPU];
 struct pvclock_wall_clock wall_clock;
 static unsigned char valid_flags = 0;
Signature · 1 line
2.32.0
#4 lore raw

PATCH v3 4/11 lib: x86: Import insn decoder from Linux

In reply to Varad Gautam

Processing #VC exceptions on AMD SEV-ES requires instruction decoding logic to set up the right GHCB state before exiting to the host.

Pull in the instruction decoder from Linux for this purpose.

Origin: Linux 64222515138e43da1fcf288f0289ef1020427b87

---
 .gitignore                         |    2 +
 lib/x86/insn/README                |   23 +
 lib/x86/insn/gen-insn-attr-x86.awk |  444 +++++++++++
 lib/x86/insn/inat.c                |   86 +++
 lib/x86/insn/inat.h                |  233 ++++++
 lib/x86/insn/inat_types.h          |   18 +
 lib/x86/insn/insn.c                |  749 +++++++++++++++++++
 lib/x86/insn/insn.h                |  279 +++++++
 lib/x86/insn/insn_glue.h           |   32 +
 lib/x86/insn/x86-opcode-map.txt    | 1106 ++++++++++++++++++++++++++++
 x86/Makefile.common                |   14 +-
 11 files changed, 2984 insertions(+), 2 deletions(-)
 create mode 100644 lib/x86/insn/README
 create mode 100644 lib/x86/insn/gen-insn-attr-x86.awk
 create mode 100644 lib/x86/insn/inat.c
 create mode 100644 lib/x86/insn/inat.h
 create mode 100644 lib/x86/insn/inat_types.h
 create mode 100644 lib/x86/insn/insn.c
 create mode 100644 lib/x86/insn/insn.h
 create mode 100644 lib/x86/insn/insn_glue.h
 create mode 100644 lib/x86/insn/x86-opcode-map.txt
.gitignore +2 -0
index 33529b6..3508379 100644
@@ -15,6 +15,8 @@ cscope.*
 *.swp
 /lib/asm
 /lib/config.h
+/lib/x86/insn/inat-tables.c
+/lib/x86/insn/*.d
 /config.mak
 /*-run
 /msr.out
lib/x86/insn/README new file +23 -0
new file mode 100644
index 0000000..988897c
@@ -0,0 +1,23 @@
+README
+======
+
+lib/x86/insn/ contains x86 instruction decoder src from Linux.
+
+The following files were taken as-is from Linux@6422251513 and
+adapted to build with kvm-unit-tests source:
+- U: lib/x86/insn/gen-insn-attr-x86.awk
+- I: lib/x86/insn/inat.c
+- I: lib/x86/insn/inat.h
+- U: lib/x86/insn/inat_types.h
+- I: lib/x86/insn/insn.c
+- I: lib/x86/insn/insn.h
+- U: lib/x86/insn/x86-opcode-map.txt
+
+U: Unmodified, except source attribution.
+I: Modified for #include path fixup.
+
+lib/x86/insn/insn_glue.h contains additional code from Linux that
+is relevant to the insn decoder, and not required elsewhere by
+kvm-unit-tests. These definitions are placed in a separate file to
+keep the diff between Linux and kvm-unit-tests's insn decoder copy
+minimal.
lib/x86/insn/gen-insn-attr-x86.awk new file +444 -0

444 lines not on this page · raw message

lib/x86/insn/inat.c new file +86 -0

86 lines not on this page · raw message

lib/x86/insn/inat.h new file +233 -0

233 lines not on this page · raw message

lib/x86/insn/inat_types.h new file +18 -0
new file mode 100644
index 0000000..74a76ef
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _ASM_X86_INAT_TYPES_H
+#define _ASM_X86_INAT_TYPES_H
+/*
+ * x86 instruction attributes
+ *
+ * Written by Masami Hiramatsu <[email protected]>
+ *
+ * kvm-unit-tests origin: Linux@6422251513
+ *   tools/arch/x86/include/asm/inat_types.h
+ */
+
+/* Instruction attributes */
+typedef unsigned int insn_attr_t;
+typedef unsigned char insn_byte_t;
+typedef signed int insn_value_t;
+
+#endif
lib/x86/insn/insn.c new file +749 -0

749 lines not on this page · raw message

lib/x86/insn/insn.h new file +279 -0

279 lines not on this page · raw message

lib/x86/insn/insn_glue.h new file +32 -0
new file mode 100644
index 0000000..bbbbcab
@@ -0,0 +1,32 @@
+/*
+ * Necessary definitions from Linux to adapt the insn decoder for
+ * kvm-unit-tests.
+ *
+ * SPDX-License-Identifier: GPL-2.0
+ */
+
+/**
+ * BUILD_BUG - no-op.
+ */
+#define BUILD_BUG()
+
+/*
+ * Virt escape sequences to trigger instruction emulation;
+ * ideally these would decode to 'whole' instruction and not destroy
+ * the instruction stream; sadly this is not true for the 'kvm' one :/
+ */
+#define __XEN_EMULATE_PREFIX  0x0f,0x0b,0x78,0x65,0x6e  /* ud2 ; .ascii "xen" */
+#define __KVM_EMULATE_PREFIX  0x0f,0x0b,0x6b,0x76,0x6d	/* ud2 ; .ascii "kvm" */
+
+# define __packed		__attribute__((__packed__))
+#define __get_unaligned_t(type, ptr) ({						\
+	const struct { type x; } __packed *__pptr = (typeof(__pptr))(ptr);	\
+	__pptr->x;								\
+})
+#define get_unaligned(ptr)	__get_unaligned_t(typeof(*(ptr)), (ptr))
+
+#define	EINVAL		22	/* Invalid argument */
+#define	ENODATA		61	/* No data available */
+
+#define CONFIG_X86_64		1
+#define IS_ENABLED(option)	1
lib/x86/insn/x86-opcode-map.txt new file +1106 -0

1,106 lines not on this page · raw message

x86/Makefile.common +12 -2
index ae426aa..ae19314 100644
@@ -25,6 +25,8 @@ cflatobjs += lib/x86/delay.o
 ifeq ($(TARGET_EFI),y)
 cflatobjs += lib/x86/amd_sev.o
 cflatobjs += lib/x86/amd_sev_vc.o
+cflatobjs += lib/x86/insn/insn.o
+cflatobjs += lib/x86/insn/inat.o
 cflatobjs += lib/efi.o
 cflatobjs += x86/efi/reloc_x86_64.o
 endif
@@ -48,7 +50,14 @@ FLATLIBS = lib/libcflat.a
 ifeq ($(TARGET_EFI),y)
 .PRECIOUS: %.efi %.so
 
-%.so: %.o $(FLATLIBS) $(SRCDIR)/x86/efi/elf_x86_64_efi.lds $(cstart.o)
+inat_tables_script = $(SRCDIR)/lib/x86/insn/gen-insn-attr-x86.awk
+inat_tables_maps = $(SRCDIR)/lib/x86/insn/x86-opcode-map.txt
+inat_tables_c = $(SRCDIR)/lib/x86/insn/inat-tables.c
+
+$(inat_tables_c): $(inat_tables_script) $(inat_tables_maps)
+	awk -f $(inat_tables_script) $(inat_tables_maps) > $@
+
+%.so: $(inat_tables_c) %.o $(FLATLIBS) $(SRCDIR)/x86/efi/elf_x86_64_efi.lds $(cstart.o)
 	$(LD) -T $(SRCDIR)/x86/efi/elf_x86_64_efi.lds $(EFI_LDFLAGS) -o $@ \
 		$(filter %.o, $^) $(FLATLIBS)
[… more lines in the raw message]

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.32.0

Part of the diff is not shown here. View the raw message.

#5 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

Quoted 4 lines · Varad Gautam

Processing #VC exceptions on AMD SEV-ES requires instruction decoding logic to set up the right GHCB state before exiting to the host.

Pull in the instruction decoder from Linux for this purpose.

Do we really need Linux's decoder for this? Linux needs a more robust decoder because it has to deal with userspace crud, but KUT should have full control over what code it encounters in a #VC handler, e.g. we should never have to worry about ignore prefixes on a WRMSR. And looking at future patches, KUT is still looking at raw opcode bytes, e.g.

	/* Is it a WRMSR? */
	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;

and the giant switch in vc_ioio_exitinfo().

The decoder does bring a bit of cleanliness, but 2k+ lines of code that's likely to get stale fairly quickly is going to be a maintenance burden. And we certainly don't need things like VEX prefix handling :-)

Do you happen to have data on how often each flavors of instructions is encountered? E.g. can we get away with a truly minimal "decoder" by modifying select tests to avoid hard-to-decode instructions? Or even patch them to do VMGEXIT directly?

#6 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

Quoted 11 lines · Sean Christopherson

Do we really need Linux's decoder for this? Linux needs a more robust decoder because it has to deal with userspace crud, but KUT should have full control over what code it encounters in a #VC handler, e.g. we should never have to worry about ignore prefixes on a WRMSR. And looking at future patches, KUT is still looking at raw opcode bytes, e.g.

	/* Is it a WRMSR? */
	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;

and the giant switch in vc_ioio_exitinfo().

Quoted 7 lines

The decoder does bring a bit of cleanliness, but 2k+ lines of code that's likely to get stale fairly quickly is going to be a maintenance burden. And we certainly don't need things like VEX prefix handling :-)

Do you happen to have data on how often each flavors of instructions is encountered? E.g. can we get away with a truly minimal "decoder" by modifying select tests to avoid hard-to-decode instructions? Or even patch them to do VMGEXIT directly?

Is it really less pain to have this code in KUT than not having it? The code for the instruction decoder is maintained in the kernel source tree, and KUT just can pull in a new version if needed.

I think it is much better to include this code than to work around its absence every time it is needed, even when it is capable of doing more than is needed in this context.

Regards,

Signature · 10 lines
Jörg Rödel
[email protected]

SUSE Software Solutions Germany GmbH
Maxfeldstr. 5
90409 Nürnberg
Germany
 
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#7 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

Quoted 16 lines · Joerg Roedel
Quoted 15 lines · Sean Christopherson

Do we really need Linux's decoder for this? Linux needs a more robust decoder because it has to deal with userspace crud, but KUT should have full control over what code it encounters in a #VC handler, e.g. we should never have to worry about ignore prefixes on a WRMSR. And looking at future patches, KUT is still looking at raw opcode bytes, e.g.

	/* Is it a WRMSR? */
	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;

and the giant switch in vc_ioio_exitinfo().

The decoder does bring a bit of cleanliness, but 2k+ lines of code that's likely to get stale fairly quickly is going to be a maintenance burden. And we certainly don't need things like VEX prefix handling :-)

Quoted 7 lines
Quoted 3 lines

Do you happen to have data on how often each flavors of instructions is encountered? E.g. can we get away with a truly minimal "decoder" by modifying select tests to avoid hard-to-decode instructions? Or even patch them to do VMGEXIT directly?

Is it really less pain to have this code in KUT than not having it? The code for the instruction decoder is maintained in the kernel source tree, and KUT just can pull in a new version if needed.

But how will we know when a new version is needed? I agree that the actual update will likely be trivial, but if something does go awry, debugging will be painful.

If we drop string I/O decoding, this boils down to CPUID, MSR, and port I/O.

Very unsurprisingly, objdump+grep shows that CPUID and {RD,WR}MSR never have any prefixes, i.e. they can be hardcoded to expect the exact 2-byte opcode.

Port I/O does use the operand size prefix, but that's trivial to handle. At that point, the only decoding beyond the base opcode is the imm8 for the non-DX versions of IN and OUT.

Compile tested only, and I've no idea if the exception forwarding will actually work, but IMO this shows that for at least basic support, the kernel's full decoder is overkill.

If/when we get to MMIO, then 100% agree we need a more formal decoder. But even then, I would prefer to more judiciously pull in the bits we need, e.g. it's not like insn_decode_mmio() is doing anything clever. The pieces we really need are for decoding ModR/M and SIB.

---
 lib/x86/amd_sev.h    |  23 ----
 lib/x86/amd_sev_vc.c | 300 +++++++++++++------------------------------
 2 files changed, 89 insertions(+), 234 deletions(-)
lib/x86/amd_sev.h +0 -23
index 17d0957..66abc75 100644
@@ -19,7 +19,6 @@
 #include "asm/page.h"
 #include "efi.h"
 #include "processor.h"
-#include "insn/insn.h"
 #include "svm.h"
 
 struct __attribute__ ((__packed__)) ghcb {
@@ -39,28 +38,6 @@ struct __attribute__ ((__packed__)) ghcb {
 
 #define	VMGEXIT()			{ asm volatile("rep; vmmcall\n\r"); }
 
-enum es_result {
-	ES_OK,			/* All good */
-	ES_UNSUPPORTED,		/* Requested operation not supported */
-	ES_VMM_ERROR,		/* Unexpected state from the VMM */
-	ES_DECODE_FAILED,	/* Instruction decoding failed */
-	ES_EXCEPTION,		/* Instruction caused exception */
-	ES_RETRY,		/* Retry instruction emulation */
-};
-
-struct es_fault_info {
-	unsigned long vector;
-	unsigned long error_code;
-	unsigned long cr2;
-};
-
-/* ES instruction emulation context */
-struct es_em_ctxt {
-	struct ex_regs *regs;
-	struct insn insn;
-	struct es_fault_info fi;
-};
-
 /*
  * AMD Programmer's Manual Volume 3
  *   - Section "Function 8000_0000h - Maximum Extended Function Number and Vendor String"
lib/x86/amd_sev_vc.c +89 -211

440 lines not on this page · raw message

base-commit: 845549433d850e408968d4bd94892593cf4a324c

#8 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

There is at least one test that I know uses string IO i.e. x86/amd_sev.c. I will check if there are more.

#9 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

Quoted 1 line · Vasant Karasulli

There is at least one test that I know uses string IO i.e. x86/amd_sev.c.

That one should be easy to convert to directly do #VMGEXIT when SEV-ES is enabled. Or maybe delete it an enlighten emulator.c's version?

Quoted 1 line

I will check if there are more.

Tests that explicitly validate string I/O aren't all that interesting, we really only need one string I/O test for SEV-ES. I supposed one could make the argument that it'd be useful to ensure that string I/O generates #VC as expected, but that can be done without actually decoding the I/O.

Hmm, or cheat. E.g. write up a dedicated #VC handler in x86/amd_sev.c for the string I/O test to forward the #VMGEXIT, but rely on the existing checks in test_stringio() to ensure it was done correctly. That way there's no need to do fancy decoding, but the "spirit" of the test is kept intact.

#10 lore raw

Re: [kvm-unit-tests PATCH v3 04/11] lib: x86: Import insn decoder from Linux

In reply to Sean Christopherson

Hi,

trying to get some tracktion here again.

Quoted 5 lines · Sean Christopherson

Do we really need Linux's decoder for this? Linux needs a more robust decoder because it has to deal with userspace crud, but KUT should have full control over what code it encounters in a #VC handler, e.g. we should never have to worry about ignore prefixes on a WRMSR. And looking at future patches, KUT is still looking at raw opcode bytes, e.g.

I think just importing Linux' instruction decoder is the most robust solution for kvm-unit-tests. The code does MMIO today and who knows what the future will bring. To minimize the risk that changes unrelated to SEV-ES will break it all the time, having a full instruction decoder is the best option.

The maintenance cost for this should be pretty low, as the Linux code has no dependencies and new versions can be ported over as needed with little effort.

Regards,

Signature · 10 lines
Jörg Rödel
[email protected]

SUSE Software Solutions Germany GmbH
Frankenstraße 146
90461 Nürnberg
Germany

(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
#11 lore raw

PATCH v3 5/11 x86: AMD SEV-ES: Pull related GHCB definitions and helpers from Linux

In reply to Varad Gautam

Origin: Linux 64222515138e43da1fcf288f0289ef1020427b87

Suppress -Waddress-of-packed-member to allow taking addresses on struct ghcb / struct vmcb_save_area fields.

---
 lib/x86/amd_sev.h   | 95 +++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/msr.h       |  1 +
 lib/x86/processor.h |  9 +++++
 lib/x86/svm.h       | 40 ++++++++++++++++++-
 x86/Makefile.x86_64 |  1 +
 5 files changed, 145 insertions(+), 1 deletion(-)
lib/x86/amd_sev.h +95 -0
index afbacf3..17d0957 100644
@@ -18,6 +18,48 @@
 #include "desc.h"
 #include "asm/page.h"
 #include "efi.h"
+#include "processor.h"
+#include "insn/insn.h"
+#include "svm.h"
+
+struct __attribute__ ((__packed__)) ghcb {
+	struct vmcb_save_area save;
+	u8 reserved_save[2048 - sizeof(struct vmcb_save_area)];
+
+	u8 shared_buffer[2032];
+
+	u8 reserved_1[10];
+	u16 protocol_version;	/* negotiated SEV-ES/GHCB protocol version */
+	u32 ghcb_usage;
+};
+
+#define GHCB_PROTO_OUR		0x0001UL
+#define GHCB_PROTOCOL_MAX	1ULL
+#define GHCB_DEFAULT_USAGE	0ULL
+
+#define	VMGEXIT()			{ asm volatile("rep; vmmcall\n\r"); }
+
+enum es_result {
+	ES_OK,			/* All good */
+	ES_UNSUPPORTED,		/* Requested operation not supported */
+	ES_VMM_ERROR,		/* Unexpected state from the VMM */
+	ES_DECODE_FAILED,	/* Instruction decoding failed */
+	ES_EXCEPTION,		/* Instruction caused exception */
+	ES_RETRY,		/* Retry instruction emulation */
+};
+
+struct es_fault_info {
+	unsigned long vector;
+	unsigned long error_code;
+	unsigned long cr2;
+};
+
+/* ES instruction emulation context */
+struct es_em_ctxt {
+	struct ex_regs *regs;
+	struct insn insn;
+	struct es_fault_info fi;
+};
 
 /*
  * AMD Programmer's Manual Volume 3
@@ -59,6 +101,59 @@ void handle_sev_es_vc(struct ex_regs *regs);
 unsigned long long get_amd_sev_c_bit_mask(void);
 unsigned long long get_amd_sev_addr_upperbound(void);
 
+/* GHCB Accessor functions from Linux's include/asm/svm.h */
+
+#define GHCB_BITMAP_IDX(field)							\
+	(offsetof(struct vmcb_save_area, field) / sizeof(u64))
+
+#define DEFINE_GHCB_ACCESSORS(field)						\
+	static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb)	\
+	{									\
+		return test_bit(GHCB_BITMAP_IDX(field),				\
+				(unsigned long *)&ghcb->save.valid_bitmap);	\
+	}									\
+										\
+	static inline u64 ghcb_get_##field(struct ghcb *ghcb)			\
+	{									\
+		return ghcb->save.field;					\
+	}									\
+										\
+	static inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb)	\
+	{									\
+		return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0;	\
+	}									\
+										\
+	static inline void ghcb_set_##field(struct ghcb *ghcb, u64 value)	\
+	{									\
+		set_bit(GHCB_BITMAP_IDX(field),				\
+			  (u8 *)&ghcb->save.valid_bitmap);		\
+		ghcb->save.field = value;					\
+	}
+
+DEFINE_GHCB_ACCESSORS(cpl)
+DEFINE_GHCB_ACCESSORS(rip)
+DEFINE_GHCB_ACCESSORS(rsp)
+DEFINE_GHCB_ACCESSORS(rax)
+DEFINE_GHCB_ACCESSORS(rcx)
+DEFINE_GHCB_ACCESSORS(rdx)
+DEFINE_GHCB_ACCESSORS(rbx)
+DEFINE_GHCB_ACCESSORS(rbp)
+DEFINE_GHCB_ACCESSORS(rsi)
+DEFINE_GHCB_ACCESSORS(rdi)
+DEFINE_GHCB_ACCESSORS(r8)
+DEFINE_GHCB_ACCESSORS(r9)
+DEFINE_GHCB_ACCESSORS(r10)
+DEFINE_GHCB_ACCESSORS(r11)
+DEFINE_GHCB_ACCESSORS(r12)
+DEFINE_GHCB_ACCESSORS(r13)
+DEFINE_GHCB_ACCESSORS(r14)
+DEFINE_GHCB_ACCESSORS(r15)
+DEFINE_GHCB_ACCESSORS(sw_exit_code)
+DEFINE_GHCB_ACCESSORS(sw_exit_info_1)
+DEFINE_GHCB_ACCESSORS(sw_exit_info_2)
+DEFINE_GHCB_ACCESSORS(sw_scratch)
+DEFINE_GHCB_ACCESSORS(xcr0)
+
 #endif /* TARGET_EFI */
 
 #endif /* _X86_AMD_SEV_H_ */
lib/x86/msr.h +1 -0
index 5001b16..ed440b6 100644
@@ -128,6 +128,7 @@
 #define MSR_AMD64_IBSDCLINAD		0xc0011038
 #define MSR_AMD64_IBSDCPHYSAD		0xc0011039
 #define MSR_AMD64_IBSCTL		0xc001103a
+#define MSR_AMD64_SEV_ES_GHCB		0xc0010130
 
 /* Fam 10h MSRs */
 #define MSR_FAM10H_MMIO_CONF_BASE	0xc0010058
lib/x86/processor.h +9 -0
index 117032a..a4fa227 100644
@@ -2,6 +2,7 @@
 #define _X86_PROCESSOR_H_
 
 #include "libcflat.h"
+#include "bitops.h"
 #include "desc.h"
 #include "msr.h"
 #include <stdint.h>
@@ -662,6 +663,14 @@ static inline void set_bit(int bit, u8 *addr)
 			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
 }
 
+static inline int test_bit(int nr, const volatile unsigned long *addr)
+{
+	const volatile unsigned long *word = addr + BIT_WORD(nr);
+	unsigned long mask = BIT_MASK(nr);
+
+	return (*word & mask) != 0;
+}
+
 static inline void flush_tlb(void)
 {
 	ulong cr4;
lib/x86/svm.h +39 -1
index f74b13a..bc032fa 100644
@@ -169,7 +169,8 @@ struct __attribute__ ((__packed__)) vmcb_save_area {
 	u8 cpl;
 	u8 reserved_2[4];
 	u64 efer;
-	u8 reserved_3[112];
+	u8 reserved_3[104];
+	u64 xss;		/* Valid for SEV-ES only */
 	u64 cr4;
 	u64 cr3;
 	u64 cr0;
@@ -197,6 +198,42 @@ struct __attribute__ ((__packed__)) vmcb_save_area {
 	u64 br_to;
 	u64 last_excp_from;
 	u64 last_excp_to;
+
+	/*
+	 * The following part of the save area is valid only for
+	 * SEV-ES guests when referenced through the GHCB or for
+	 * saving to the host save area.
+	 */
+	u8 reserved_7[72];
+	u32 spec_ctrl;		/* Guest version of SPEC_CTRL at 0x2E0 */
+	u8 reserved_7b[4];
+	u32 pkru;
+	u8 reserved_7a[20];
+	u64 reserved_8;		/* rax already available at 0x01f8 */
+	u64 rcx;
+	u64 rdx;
+	u64 rbx;
+	u64 reserved_9;		/* rsp already available at 0x01d8 */
+	u64 rbp;
+	u64 rsi;
+	u64 rdi;
+	u64 r8;
+	u64 r9;
+	u64 r10;
+	u64 r11;
+	u64 r12;
+	u64 r13;
+	u64 r14;
+	u64 r15;
+	u8 reserved_10[16];
+	u64 sw_exit_code;
+	u64 sw_exit_info_1;
+	u64 sw_exit_info_2;
+	u64 sw_scratch;
+	u8 reserved_11[56];
+	u64 xcr0;
+	u8 valid_bitmap[16];
+	u64 x87_state_gpa;
 };
 
 struct __attribute__ ((__packed__)) vmcb {
@@ -297,6 +334,7 @@ struct __attribute__ ((__packed__)) vmcb {
 #define	SVM_EXIT_WRITE_DR6 	0x036
 #define	SVM_EXIT_WRITE_DR7 	0x037
 #define SVM_EXIT_EXCP_BASE      0x040
+#define SVM_EXIT_LAST_EXCP	0x05f
 #define SVM_EXIT_INTR		0x060
 #define SVM_EXIT_NMI		0x061
 #define SVM_EXIT_SMI		0x062
x86/Makefile.x86_64 +1 -0
index a3cb75a..7d3eb53 100644
@@ -13,6 +13,7 @@ endif
 
 fcf_protection_full := $(call cc-option, -fcf-protection=full,)
 COMMON_CFLAGS += -mno-red-zone -mno-sse -mno-sse2 $(fcf_protection_full)
+COMMON_CFLAGS += -Wno-address-of-packed-member
 
 cflatobjs += lib/x86/setjmp64.o
 cflatobjs += lib/x86/intel-iommu.o
Signature · 1 line
2.32.0
#12 lore raw

PATCH v3 6/11 x86: AMD SEV-ES: Prepare for #VC processing

In reply to Varad Gautam

Lay the groundwork for processing #VC exceptions in the handler. This includes clearing the GHCB, decoding the insn that triggered this #VC, and continuing execution after the exception has been processed.

Based on Linux 64222515138e43da1fcf288f0289ef1020427b87.

---
 lib/x86/amd_sev_vc.c | 90 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 89 insertions(+), 1 deletion(-)
lib/x86/amd_sev_vc.c +89 -1
index 8226121..cbaf0c8 100644
@@ -1,14 +1,102 @@
-/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * AMD SEV-ES #VC exception handling.
+ * Adapted from Linux@6422251513:
+ * - arch/x86/kernel/sev.c
+ * - arch/x86/kernel/sev-shared.c
+ *
+ * SPDX-License-Identifier: GPL-2.0 */
 
 #include "amd_sev.h"
+#include "svm.h"
 
 extern phys_addr_t ghcb_addr;
 
+static void vc_ghcb_invalidate(struct ghcb *ghcb)
+{
+	ghcb->save.sw_exit_code = 0;
+	memset(ghcb->save.valid_bitmap, 0, sizeof(ghcb->save.valid_bitmap));
+}
+
+static bool vc_decoding_needed(unsigned long exit_code)
+{
+	/* Exceptions don't require to decode the instruction */
+	return !(exit_code >= SVM_EXIT_EXCP_BASE &&
+		 exit_code <= SVM_EXIT_LAST_EXCP);
+}
+
+static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
+{
+	unsigned char buffer[MAX_INSN_SIZE];
+	int ret;
+
+	memcpy(buffer, (unsigned char *)ctxt->regs->rip, MAX_INSN_SIZE);
+
+	ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
+	if (ret < 0)
+		return ES_DECODE_FAILED;
+	else
+		return ES_OK;
+}
+
+static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,
+				      struct ex_regs *regs,
+				      unsigned long exit_code)
+{
+	enum es_result ret = ES_OK;
+
+	memset(ctxt, 0, sizeof(*ctxt));
+	ctxt->regs = regs;
+
+	if (vc_decoding_needed(exit_code))
+		ret = vc_decode_insn(ctxt);
+
+	return ret;
+}
+
+static void vc_finish_insn(struct es_em_ctxt *ctxt)
+{
+	ctxt->regs->rip += ctxt->insn.length;
+}
+
+static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
+					 struct ghcb *ghcb,
+					 unsigned long exit_code)
+{
+	enum es_result result;
+
+	switch (exit_code) {
+	default:
+		/*
+		 * Unexpected #VC exception
+		 */
+		result = ES_UNSUPPORTED;
+	}
+
+	return result;
+}
+
 void handle_sev_es_vc(struct ex_regs *regs)
 {
 	struct ghcb *ghcb = (struct ghcb *) ghcb_addr;
+	unsigned long exit_code = regs->error_code;
+	struct es_em_ctxt ctxt;
+	enum es_result result;
+
 	if (!ghcb) {
 		/* TODO: kill guest */
 		return;
 	}
+
+	vc_ghcb_invalidate(ghcb);
+	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
+	if (result == ES_OK)
+		result = vc_handle_exitcode(&ctxt, ghcb, exit_code);
+	if (result == ES_OK) {
+		vc_finish_insn(&ctxt);
+	} else {
+		printf("Unable to handle #VC exitcode, exit_code=%lx result=%x\n",
+		       exit_code, result);
+	}
+
+	return;
 }
Signature · 1 line
2.32.0
#13 lore raw

PATCH v3 7/11 lib/x86: Move xsave helpers to lib/

In reply to Varad Gautam

Processing CPUID #VC for AMD SEV-ES requires copying xcr0 into GHCB. Move the xsave read/write helpers used by xsave testcase to lib/x86 to share as common code.

---
 lib/x86/xsave.c     | 37 +++++++++++++++++++++++++++++++++++++
 lib/x86/xsave.h     | 16 ++++++++++++++++
 x86/Makefile.common |  1 +
 x86/xsave.c         | 43 +------------------------------------------
 4 files changed, 55 insertions(+), 42 deletions(-)
 create mode 100644 lib/x86/xsave.c
 create mode 100644 lib/x86/xsave.h
lib/x86/xsave.c new file +37 -0
new file mode 100644
index 0000000..1c0f16e
@@ -0,0 +1,37 @@
+#include "libcflat.h"
+#include "xsave.h"
+#include "processor.h"
+
+int xgetbv_checking(u32 index, u64 *result)
+{
+    u32 eax, edx;
+
+    asm volatile(ASM_TRY("1f")
+            ".byte 0x0f,0x01,0xd0\n\t" /* xgetbv */
+            "1:"
+            : "=a" (eax), "=d" (edx)
+            : "c" (index));
+    *result = eax + ((u64)edx << 32);
+    return exception_vector();
+}
+
+int xsetbv_checking(u32 index, u64 value)
+{
+    u32 eax = value;
+    u32 edx = value >> 32;
+
+    asm volatile(ASM_TRY("1f")
+            ".byte 0x0f,0x01,0xd1\n\t" /* xsetbv */
+            "1:"
+            : : "a" (eax), "d" (edx), "c" (index));
+    return exception_vector();
+}
+
+uint64_t get_supported_xcr0(void)
+{
+    struct cpuid r;
+    r = cpuid_indexed(0xd, 0);
+    printf("eax %x, ebx %x, ecx %x, edx %x\n",
+            r.a, r.b, r.c, r.d);
+    return r.a + ((u64)r.d << 32);
+}
lib/x86/xsave.h new file +16 -0
new file mode 100644
index 0000000..f1851a3
@@ -0,0 +1,16 @@
+#ifndef _X86_XSAVE_H_
+#define _X86_XSAVE_H_
+
+#define X86_CR4_OSXSAVE			0x00040000
+#define XCR_XFEATURE_ENABLED_MASK       0x00000000
+#define XCR_XFEATURE_ILLEGAL_MASK       0x00000010
+
+#define XSTATE_FP       0x1
+#define XSTATE_SSE      0x2
+#define XSTATE_YMM      0x4
+
+int xgetbv_checking(u32 index, u64 *result);
+int xsetbv_checking(u32 index, u64 value);
+uint64_t get_supported_xcr0(void);
+
+#endif
x86/Makefile.common +1 -0
index ae19314..bf44e91 100644
@@ -22,6 +22,7 @@ cflatobjs += lib/x86/acpi.o
 cflatobjs += lib/x86/stack.o
 cflatobjs += lib/x86/fault_test.o
 cflatobjs += lib/x86/delay.o
+cflatobjs += lib/x86/xsave.o
 ifeq ($(TARGET_EFI),y)
 cflatobjs += lib/x86/amd_sev.o
 cflatobjs += lib/x86/amd_sev_vc.o
x86/xsave.c +1 -42

55 lines not on this page · raw message

Signature · 1 line
2.32.0
#14 lore raw

PATCH v3 8/11 x86: AMD SEV-ES: Handle CPUID #VC

In reply to Varad Gautam

Using Linux's CPUID #VC processing logic.

---
 lib/x86/amd_sev_vc.c | 91 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 91 insertions(+)
lib/x86/amd_sev_vc.c +91 -0
index cbaf0c8..8d13319 100644
@@ -8,6 +8,7 @@
 
 #include "amd_sev.h"
 #include "svm.h"
+#include "x86/xsave.h"
 
 extern phys_addr_t ghcb_addr;
 
@@ -58,6 +59,93 @@ static void vc_finish_insn(struct es_em_ctxt *ctxt)
 	ctxt->regs->rip += ctxt->insn.length;
 }
 
+static inline void sev_es_wr_ghcb_msr(u64 val)
+{
+	wrmsr(MSR_AMD64_SEV_ES_GHCB, val);
+}
+
+static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
+					  struct es_em_ctxt *ctxt,
+					  u64 exit_code, u64 exit_info_1,
+					  u64 exit_info_2)
+{
+	enum es_result ret;
+
+	/* Fill in protocol and format specifiers */
+	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
+	ghcb->ghcb_usage       = GHCB_DEFAULT_USAGE;
+
+	ghcb_set_sw_exit_code(ghcb, exit_code);
+	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
+	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
+
+	sev_es_wr_ghcb_msr(__pa(ghcb));
+	VMGEXIT();
+
+	if ((ghcb->save.sw_exit_info_1 & 0xffffffff) == 1) {
+		u64 info = ghcb->save.sw_exit_info_2;
+		unsigned long v;
+
+		v = info & SVM_EVTINJ_VEC_MASK;
+
+		/* Check if exception information from hypervisor is sane. */
+		if ((info & SVM_EVTINJ_VALID) &&
+		    ((v == GP_VECTOR) || (v == UD_VECTOR)) &&
+		    ((info & SVM_EVTINJ_TYPE_MASK) == SVM_EVTINJ_TYPE_EXEPT)) {
+			ctxt->fi.vector = v;
+			if (info & SVM_EVTINJ_VALID_ERR)
+				ctxt->fi.error_code = info >> 32;
+			ret = ES_EXCEPTION;
+		} else {
+			ret = ES_VMM_ERROR;
+		}
+	} else if (ghcb->save.sw_exit_info_1 & 0xffffffff) {
+		ret = ES_VMM_ERROR;
+	} else {
+		ret = ES_OK;
+	}
+
+	return ret;
+}
+
+static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
+				      struct es_em_ctxt *ctxt)
+{
+	struct ex_regs *regs = ctxt->regs;
+	u32 cr4 = read_cr4();
+	enum es_result ret;
+
+	ghcb_set_rax(ghcb, regs->rax);
+	ghcb_set_rcx(ghcb, regs->rcx);
+
+	if (cr4 & X86_CR4_OSXSAVE) {
+		/* Safe to read xcr0 */
+		u64 xcr0;
+		xgetbv_checking(XCR_XFEATURE_ENABLED_MASK, &xcr0);
+		ghcb_set_xcr0(ghcb, xcr0);
+	} else {
+		/* xgetbv will cause #GP - use reset value for xcr0 */
+		ghcb_set_xcr0(ghcb, 1);
+	}
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
+	if (ret != ES_OK)
+		return ret;
+
+	if (!(ghcb_rax_is_valid(ghcb) &&
+	      ghcb_rbx_is_valid(ghcb) &&
+	      ghcb_rcx_is_valid(ghcb) &&
+	      ghcb_rdx_is_valid(ghcb)))
+		return ES_VMM_ERROR;
+
+	regs->rax = ghcb->save.rax;
+	regs->rbx = ghcb->save.rbx;
+	regs->rcx = ghcb->save.rcx;
+	regs->rdx = ghcb->save.rdx;
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -65,6 +153,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	enum es_result result;
 
 	switch (exit_code) {
+	case SVM_EXIT_CPUID:
+		result = vc_handle_cpuid(ghcb, ctxt);
+		break;
 	default:
 		/*
 		 * Unexpected #VC exception
Signature · 1 line
2.32.0
#15 lore raw

PATCH v3 9/11 x86: AMD SEV-ES: Handle MSR #VC

In reply to Varad Gautam

Using Linux's MSR #VC processing logic.

---
 lib/x86/amd_sev_vc.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
lib/x86/amd_sev_vc.c +28 -0
index 8d13319..8efc6db 100644
@@ -146,6 +146,31 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
 	return ES_OK;
 }
 
+static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+{
+	struct ex_regs *regs = ctxt->regs;
+	enum es_result ret;
+	u64 exit_info_1;
+
+	/* Is it a WRMSR? */
+	exit_info_1 = (ctxt->insn.opcode.bytes[1] == 0x30) ? 1 : 0;
+
+	ghcb_set_rcx(ghcb, regs->rcx);
+	if (exit_info_1) {
+		ghcb_set_rax(ghcb, regs->rax);
+		ghcb_set_rdx(ghcb, regs->rdx);
+	}
+
+	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, exit_info_1, 0);
+
+	if ((ret == ES_OK) && (!exit_info_1)) {
+		regs->rax = ghcb->save.rax;
+		regs->rdx = ghcb->save.rdx;
+	}
+
+	return ret;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -156,6 +181,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_CPUID:
 		result = vc_handle_cpuid(ghcb, ctxt);
 		break;
+	case SVM_EXIT_MSR:
+		result = vc_handle_msr(ghcb, ctxt);
+		break;
 	default:
 		/*
 		 * Unexpected #VC exception
Signature · 1 line
2.32.0
#16 lore raw

PATCH v3 10/11 x86: AMD SEV-ES: Handle IOIO #VC

In reply to Varad Gautam

Using Linux's IOIO #VC processing logic.

---
 lib/x86/amd_sev_vc.c | 169 +++++++++++++++++++++++++++++++++++++++++++
 lib/x86/processor.h  |   7 ++
 2 files changed, 176 insertions(+)
lib/x86/amd_sev_vc.c +169 -0
index 8efc6db..e8285f2 100644
@@ -171,6 +171,172 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	return ret;
 }
 
+#define IOIO_TYPE_STR  BIT(2)
+#define IOIO_TYPE_IN   1
+#define IOIO_TYPE_INS  (IOIO_TYPE_IN | IOIO_TYPE_STR)
+#define IOIO_TYPE_OUT  0
+#define IOIO_TYPE_OUTS (IOIO_TYPE_OUT | IOIO_TYPE_STR)
+
+#define IOIO_REP       BIT(3)
+
+#define IOIO_ADDR_64   BIT(9)
+#define IOIO_ADDR_32   BIT(8)
+#define IOIO_ADDR_16   BIT(7)
+
+#define IOIO_DATA_32   BIT(6)
+#define IOIO_DATA_16   BIT(5)
+#define IOIO_DATA_8    BIT(4)
+
+#define IOIO_SEG_ES    (0 << 10)
+#define IOIO_SEG_DS    (3 << 10)
+
+/**
+ * insn_has_rep_prefix() - Determine if instruction has a REP prefix
+ * @insn:       Instruction containing the prefix to inspect
+ *
+ * Returns:
+ *
+ * 1 if the instruction has a REP prefix, 0 if not.
+ */
+static int insn_has_rep_prefix(struct insn *insn)
+{
+        insn_byte_t p;
+        int i;
+
+        insn_get_prefixes(insn);
+
+        for_each_insn_prefix(insn, i, p) {
+                if (p == 0xf2 || p == 0xf3)
+                        return 1;
+        }
+
+        return 0;
+}
+
+static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
+{
+	struct insn *insn = &ctxt->insn;
+	*exitinfo = 0;
+
+	switch (insn->opcode.bytes[0]) {
+	/* INS opcodes */
+	case 0x6c:
+	case 0x6d:
+		*exitinfo |= IOIO_TYPE_INS;
+		*exitinfo |= IOIO_SEG_ES;
+		*exitinfo |= (ctxt->regs->rdx & 0xffff) << 16;
+		break;
+
+	/* OUTS opcodes */
+	case 0x6e:
+	case 0x6f:
+		*exitinfo |= IOIO_TYPE_OUTS;
+		*exitinfo |= IOIO_SEG_DS;
+		*exitinfo |= (ctxt->regs->rdx & 0xffff) << 16;
+		break;
+
+	/* IN immediate opcodes */
+	case 0xe4:
+	case 0xe5:
+		*exitinfo |= IOIO_TYPE_IN;
+		*exitinfo |= (u8)insn->immediate.value << 16;
+		break;
+
+	/* OUT immediate opcodes */
+	case 0xe6:
+	case 0xe7:
+		*exitinfo |= IOIO_TYPE_OUT;
+		*exitinfo |= (u8)insn->immediate.value << 16;
+		break;
+
+	/* IN register opcodes */
+	case 0xec:
+	case 0xed:
+		*exitinfo |= IOIO_TYPE_IN;
+		*exitinfo |= (ctxt->regs->rdx & 0xffff) << 16;
+		break;
+
+	/* OUT register opcodes */
+	case 0xee:
+	case 0xef:
+		*exitinfo |= IOIO_TYPE_OUT;
+		*exitinfo |= (ctxt->regs->rdx & 0xffff) << 16;
+		break;
+
+	default:
+		return ES_DECODE_FAILED;
+	}
+
+	switch (insn->opcode.bytes[0]) {
+	case 0x6c:
+	case 0x6e:
+	case 0xe4:
+	case 0xe6:
+	case 0xec:
+	case 0xee:
+		/* Single byte opcodes */
+		*exitinfo |= IOIO_DATA_8;
+		break;
+	default:
+		/* Length determined by instruction parsing */
+		*exitinfo |= (insn->opnd_bytes == 2) ? IOIO_DATA_16
+						     : IOIO_DATA_32;
+	}
+	switch (insn->addr_bytes) {
+	case 2:
+		*exitinfo |= IOIO_ADDR_16;
+		break;
+	case 4:
+		*exitinfo |= IOIO_ADDR_32;
+		break;
+	case 8:
+		*exitinfo |= IOIO_ADDR_64;
+		break;
+	}
+
+	if (insn_has_rep_prefix(insn))
+		*exitinfo |= IOIO_REP;
+
+	return ES_OK;
+}
+
+static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
+{
+	struct ex_regs *regs = ctxt->regs;
+	u64 exit_info_1;
+	enum es_result ret;
+
+	ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
+	if (ret != ES_OK)
+		return ret;
+
+	if (exit_info_1 & IOIO_TYPE_STR) {
+		ret = ES_VMM_ERROR;
+	} else {
+		/* IN/OUT into/from rAX */
+
+		int bits = (exit_info_1 & 0x70) >> 1;
+		u64 rax = 0;
+
+		if (!(exit_info_1 & IOIO_TYPE_IN))
+			rax = lower_bits(regs->rax, bits);
+
+		ghcb_set_rax(ghcb, rax);
+
+		ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
+		if (ret != ES_OK)
+			return ret;
+
+		if (exit_info_1 & IOIO_TYPE_IN) {
+			if (!ghcb_rax_is_valid(ghcb))
+				return ES_VMM_ERROR;
+			regs->rax = lower_bits(ghcb->save.rax, bits);
+		}
+	}
+
+	return ret;
+}
+
 static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 					 struct ghcb *ghcb,
 					 unsigned long exit_code)
@@ -184,6 +350,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,
 	case SVM_EXIT_MSR:
 		result = vc_handle_msr(ghcb, ctxt);
 		break;
+	case SVM_EXIT_IOIO:
+		result = vc_handle_ioio(ghcb, ctxt);
+		break;
 	default:
 		/*
 		 * Unexpected #VC exception
lib/x86/processor.h +7 -0
index a4fa227..7284061 100644
@@ -671,6 +671,13 @@ static inline int test_bit(int nr, const volatile unsigned long *addr)
 	return (*word & mask) != 0;
 }
 
+static inline u64 lower_bits(u64 val, unsigned int bits)
+{
+	u64 mask = (1ULL << bits) - 1;
+
+	return (val & mask);
+}
+
 static inline void flush_tlb(void)
 {
 	ulong cr4;
Signature · 1 line
2.32.0
#17 lore raw

PATCH v3 11/11 x86: AMD SEV-ES: Handle string IO for IOIO #VC

In reply to Varad Gautam

Using Linux's IOIO #VC processing logic.

---
 lib/x86/amd_sev_vc.c | 108 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 106 insertions(+), 2 deletions(-)
lib/x86/amd_sev_vc.c +106 -2
index e8285f2..4fdf596 100644
@@ -300,10 +300,46 @@ static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)
 	return ES_OK;
 }
 
+static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt,
+					  void *src, unsigned char *buf,
+					  unsigned int data_size,
+					  unsigned int count,
+					  bool backwards)
+{
+	int i, b = backwards ? -1 : 1;
+
+	for (i = 0; i < count; i++) {
+		void *s = src + (i * data_size * b);
+		unsigned char *d = buf + (i * data_size);
+
+		memcpy(d, s, data_size);
+	}
+
+	return ES_OK;
+}
+
+static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt,
+					   void *dst, unsigned char *buf,
+					   unsigned int data_size,
+					   unsigned int count,
+					   bool backwards)
+{
+	int i, s = backwards ? -1 : 1;
+
+	for (i = 0; i < count; i++) {
+		void *d = dst + (i * data_size * s);
+		unsigned char *b = buf + (i * data_size);
+
+		memcpy(d, b, data_size);
+	}
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct ex_regs *regs = ctxt->regs;
-	u64 exit_info_1;
+	u64 exit_info_1, exit_info_2;
 	enum es_result ret;
 
 	ret = vc_ioio_exitinfo(ctxt, &exit_info_1);
@@ -311,7 +347,75 @@ static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 		return ret;
 
 	if (exit_info_1 & IOIO_TYPE_STR) {
-		ret = ES_VMM_ERROR;
+		/* (REP) INS/OUTS */
+
+		bool df = ((regs->rflags & X86_EFLAGS_DF) == X86_EFLAGS_DF);
+		unsigned int io_bytes, exit_bytes;
+		unsigned int ghcb_count, op_count;
+		unsigned long es_base;
+		u64 sw_scratch;
+
+		/*
+		 * For the string variants with rep prefix the amount of in/out
+		 * operations per #VC exception is limited so that the kernel
+		 * has a chance to take interrupts and re-schedule while the
+		 * instruction is emulated.
+		 */
+		io_bytes   = (exit_info_1 >> 4) & 0x7;
+		ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;
+
+		op_count    = (exit_info_1 & IOIO_REP) ? regs->rcx : 1;
+		exit_info_2 = op_count < ghcb_count ? op_count : ghcb_count;
+		exit_bytes  = exit_info_2 * io_bytes;
+
+		es_base = 0;
+
+		/* Read bytes of OUTS into the shared buffer */
+		if (!(exit_info_1 & IOIO_TYPE_IN)) {
+			ret = vc_insn_string_read(ctxt,
+					       (void *)(es_base + regs->rsi),
+					       ghcb->shared_buffer, io_bytes,
+					       exit_info_2, df);
+			if (ret)
+				return ret;
+		}
+
+		/*
+		 * Issue an VMGEXIT to the HV to consume the bytes from the
+		 * shared buffer or to have it write them into the shared buffer
+		 * depending on the instruction: OUTS or INS.
+		 */
+		sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
+		ghcb_set_sw_scratch(ghcb, sw_scratch);
+		ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
+					  exit_info_1, exit_info_2);
+		if (ret != ES_OK)
+			return ret;
+
+		/* Read bytes from shared buffer into the guest's destination. */
+		if (exit_info_1 & IOIO_TYPE_IN) {
+			ret = vc_insn_string_write(ctxt,
+						   (void *)(es_base + regs->rdi),
+						   ghcb->shared_buffer, io_bytes,
+						   exit_info_2, df);
+			if (ret)
+				return ret;
+
+			if (df)
+				regs->rdi -= exit_bytes;
+			else
+				regs->rdi += exit_bytes;
+		} else {
+			if (df)
+				regs->rsi -= exit_bytes;
+			else
+				regs->rsi += exit_bytes;
+		}
+
+		if (exit_info_1 & IOIO_REP)
+			regs->rcx -= exit_info_2;
+
+		ret = regs->rcx ? ES_RETRY : ES_OK;
 	} else {
 		/* IN/OUT into/from rAX */
 
Signature · 1 line
2.32.0
#18 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

#19 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

Regards,

Signature · 10 lines
Jörg Rödel
[email protected]

SUSE Software Solutions Germany GmbH
Maxfeldstr. 5
90409 Nürnberg
Germany
 
(HRB 36809, AG Nürnberg)
Geschäftsführer: Ivo Totev
#20 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 9 lines · Joerg Roedel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

But IMO that is extremely unlikely to happen. objdump + grep shows that the only string I/O in KUT comes from the explicit asm in emulator.c and amd_sev.c. And the existence of amd_sev.c's version suggests that emulator.c isn't supported. I.e. this is being added purely for an SEV specific test, which is silly.

And it's not like we're getting validation coverage of the exit_info, that also comes from software in vc_ioio_exitinfo().

Burying this in the #VC handler makes it so much harder to understand what is actually be tested, and will make it difficult to test the more interesting edge cases. E.g. I'd really like to see a test that requests string I/O emulation for a buffer that's beyond the allowed size, straddles multiple pages, walks into non-existent memory, etc.., and doing those with a direct #VMGEXIT will be a lot easier to write and read then bouncing through the #VC handler.

#21 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Joerg Roedel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

But IMO that is extremely unlikely to happen. objdump + grep shows that the only string I/O in KUT comes from the explicit asm in emulator.c and amd_sev.c. And the existence of amd_sev.c's version suggests that emulator.c isn't supported. I.e. this is being added purely for an SEV specific test, which is silly.

And it's not like we're getting validation coverage of the exit_info, that also comes from software in vc_ioio_exitinfo().

Quoted 6 lines

Burying this in the #VC handler makes it so much harder to understand what is actually be tested, and will make it difficult to test the more interesting edge cases. E.g. I'd really like to see a test that requests string I/O emulation for a buffer that's beyond the allowed size, straddles multiple pages, walks into non-existent memory, etc.., and doing those with a direct #VMGEXIT will be a lot easier to write and read then bouncing through the #VC handler.

For the record, I like the current approach of implementing a #VC handler within kvm-unit-tests itself for the string IO.

Rationale:
- Makes writing string IO tests easy.
- We get some level of testing of the #VC handler in the guest kernel in the sense that this #VC handler is based on that one. So if we find an issue in this handler we know we probably need to fix that same issue in the guest kernel #VC handler.
- I don't follow the argument that having a direct #VMGEXIT in the test itself makes the test easerit to write and read. It's going to add a lot of extra code to the test that makes it hard to parse the actual string IO operations and expectations IMHO.
- I agree that writing test cases to straddle multiple pages, walk into non-existent memory, etc. is an excellent idea. But I don't follow how exposing the test itself to the #VC exit makes this easier. Worst case, the kvm-unit-tests can be extended with some sort of helper to expose to the test the scratch buffer size and whether it's embedded in the GHCB or external.

#22 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 32 lines · Marc Orr
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Joerg Roedel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

But IMO that is extremely unlikely to happen. objdump + grep shows that the only string I/O in KUT comes from the explicit asm in emulator.c and amd_sev.c. And the existence of amd_sev.c's version suggests that emulator.c isn't supported. I.e. this is being added purely for an SEV specific test, which is silly.

And it's not like we're getting validation coverage of the exit_info, that also comes from software in vc_ioio_exitinfo().

Quoted 6 lines

Burying this in the #VC handler makes it so much harder to understand what is actually be tested, and will make it difficult to test the more interesting edge cases. E.g. I'd really like to see a test that requests string I/O emulation for a buffer that's beyond the allowed size, straddles multiple pages, walks into non-existent memory, etc.., and doing those with a direct #VMGEXIT will be a lot easier to write and read then bouncing through the #VC handler.

For the record, I like the current approach of implementing a #VC handler within kvm-unit-tests itself for the string IO.

Quoted 16 lines

Rationale:
- Makes writing string IO tests easy.
- We get some level of testing of the #VC handler in the guest kernel in the sense that this #VC handler is based on that one. So if we find an issue in this handler we know we probably need to fix that same issue in the guest kernel #VC handler.
- I don't follow the argument that having a direct #VMGEXIT in the test itself makes the test easerit to write and read. It's going to add a lot of extra code to the test that makes it hard to parse the actual string IO operations and expectations IMHO.
- I agree that writing test cases to straddle multiple pages, walk into non-existent memory, etc. is an excellent idea. But I don't follow how exposing the test itself to the #VC exit makes this easier. Worst case, the kvm-unit-tests can be extended with some sort of helper to expose to the test the scratch buffer size and whether it's embedded in the GHCB or external.

Also, having this handler does not stop anyone from contributing a more elaborate test that turns out to need to implement its own #VC handling.

#23 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

In reply to Marc Orr
Quoted 28 lines · Marc Orr
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Joerg Roedel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

But IMO that is extremely unlikely to happen. objdump + grep shows that the only string I/O in KUT comes from the explicit asm in emulator.c and amd_sev.c. And the existence of amd_sev.c's version suggests that emulator.c isn't supported. I.e. this is being added purely for an SEV specific test, which is silly.

And it's not like we're getting validation coverage of the exit_info, that also comes from software in vc_ioio_exitinfo().

Quoted 6 lines

Burying this in the #VC handler makes it so much harder to understand what is actually be tested, and will make it difficult to test the more interesting edge cases. E.g. I'd really like to see a test that requests string I/O emulation for a buffer that's beyond the allowed size, straddles multiple pages, walks into non-existent memory, etc.., and doing those with a direct #VMGEXIT will be a lot easier to write and read then bouncing through the #VC handler.

Quoted 5 lines

For the record, I like the current approach of implementing a #VC handler within kvm-unit-tests itself for the string IO.

Rationale:
- Makes writing string IO tests easy.

(a) that's debatable, (b) it's a moot point because we can and should add a helper to do the dirty work. E.g.

static void sev_es_do_string_io(..., int port, int size, int count, void *data);

I say it's debatable because it's not like this is the most pleasant code to read:

	asm volatile("cld \n\t"
		     "movw %0, %%dx \n\t"
		     "rep outsb \n\t"
		     : : "i"((short)TESTDEV_IO_PORT),
		       "S"(st1), "c"(sizeof(st1) - 1));
Quoted 8 lines

- We get some level of testing of the #VC handler in the guest kernel in the sense that this #VC handler is based on that one. So if we find an issue in this handler we know we probably need to fix that same issue in the guest kernel #VC handler.
- I don't follow the argument that having a direct #VMGEXIT in the test itself makes the test easerit to write and read. It's going to add a lot of extra code to the test that makes it hard to parse the actual string IO operations and expectations IMHO.

I strongly disagree. This

	static char st1[] = "abcdefghijklmnop";

	static void test_stringio(void)
	{
		unsigned char r = 0;
		asm volatile("cld \n\t"
			"movw %0, %%dx \n\t"
			"rep outsb \n\t"
			: : "i"((short)TESTDEV_IO_PORT),
			"S"(st1), "c"(sizeof(st1) - 1));
		asm volatile("inb %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
		report(r == st1[sizeof(st1) - 2], "outsb up"); /* last char */

		asm volatile("std \n\t"
			"movw %0, %%dx \n\t"
			"rep outsb \n\t"
			: : "i"((short)TESTDEV_IO_PORT),
			"S"(st1 + sizeof(st1) - 2), "c"(sizeof(st1) - 1));
		asm volatile("cld \n\t" : : );
		asm volatile("in %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
		report(r == st1[0], "outsb down");
	}

is not easy to write or read.

I'm envisioning SEV-ES string I/O tests will be things like:

	sev_es_outsb(..., TESTDEV_IO_PORT, sizeof(st1) - 1, st1);

	sev_es_outsb_backwards(..., TESTDEV_IO_PORT, sizeof(st1) - 1,
			       st1 + sizeof(st1) - 2));

where sev_es_outsb() is a wrapper to sev_es_do_string_io() or whatever and fills in all the appropriate SEV-ES IOIO_* constants.

Yes, we can and probably should add wrappers for the raw string I/O tests too. But, no matter what, somehwere there has to be a helper to translate raw string I/O into SEV-ES string I/O. I don't see why doing that in the #VC handler is any easier than doing it in a helper.

Quoted 3 lines

- I agree that writing test cases to straddle multiple pages, walk into non-existent memory, etc. is an excellent idea. But I don't follow how exposing the test itself to the #VC exit makes this easier.

The #VC handler does things like:

	ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;

to explicitly not mess up the _guest_ kernel. The proposed #VC handler literally cannot generate:

- a string I/O request larger than 2032 bytes
- does not reside inside the GHCB's internal buffer
- spans multiple pages
- points at illegal memory

And so on an so forth. And if we add helpers to allow that, then what value does the #VC handler provide since adding a wrapper to follow the Linux guest approach would be trivial?

Quoted 3 lines

Worst case, the kvm-unit-tests can be extended with some sort of helper to expose to the test the scratch buffer size and whether it's embedded in the GHCB or external.

#24 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 41 lines · Sean Christopherson
Quoted 28 lines · Marc Orr
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Joerg Roedel
Quoted 5 lines · Sean Christopherson
Quoted 1 line · Varad Gautam

Using Linux's IOIO #VC processing logic.

How much string I/O is there in KUT? I assume it's rare, i.e. avoiding it entirely is probably less work in the long run.

The problem is that SEV-ES support will silently break if someone adds it unnoticed and without testing changes on SEV-ES.

But IMO that is extremely unlikely to happen. objdump + grep shows that the only string I/O in KUT comes from the explicit asm in emulator.c and amd_sev.c. And the existence of amd_sev.c's version suggests that emulator.c isn't supported. I.e. this is being added purely for an SEV specific test, which is silly.

And it's not like we're getting validation coverage of the exit_info, that also comes from software in vc_ioio_exitinfo().

Quoted 6 lines

Burying this in the #VC handler makes it so much harder to understand what is actually be tested, and will make it difficult to test the more interesting edge cases. E.g. I'd really like to see a test that requests string I/O emulation for a buffer that's beyond the allowed size, straddles multiple pages, walks into non-existent memory, etc.., and doing those with a direct #VMGEXIT will be a lot easier to write and read then bouncing through the #VC handler.

Quoted 5 lines

For the record, I like the current approach of implementing a #VC handler within kvm-unit-tests itself for the string IO.

Rationale:
- Makes writing string IO tests easy.

(a) that's debatable, (b) it's a moot point because we can and should add a helper to do the dirty work. E.g.

static void sev_es_do_string_io(..., int port, int size, int count, void *data);

Quoted 7 lines

I say it's debatable because it's not like this is the most pleasant code to read:

        asm volatile("cld \n\t"
                     "movw %0, %%dx \n\t"
                     "rep outsb \n\t"
                     : : "i"((short)TESTDEV_IO_PORT),
                       "S"(st1), "c"(sizeof(st1) - 1));

Yeah, if we have a helper that resolves most of my concerns. (More on this below.)

Quoted 34 lines
Quoted 8 lines

- We get some level of testing of the #VC handler in the guest kernel in the sense that this #VC handler is based on that one. So if we find an issue in this handler we know we probably need to fix that same issue in the guest kernel #VC handler.
- I don't follow the argument that having a direct #VMGEXIT in the test itself makes the test easerit to write and read. It's going to add a lot of extra code to the test that makes it hard to parse the actual string IO operations and expectations IMHO.

I strongly disagree. This

        static char st1[] = "abcdefghijklmnop";

        static void test_stringio(void)
        {
                unsigned char r = 0;
                asm volatile("cld \n\t"
                        "movw %0, %%dx \n\t"
                        "rep outsb \n\t"
                        : : "i"((short)TESTDEV_IO_PORT),
                        "S"(st1), "c"(sizeof(st1) - 1));
                asm volatile("inb %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
                report(r == st1[sizeof(st1) - 2], "outsb up"); /* last char */

                asm volatile("std \n\t"
                        "movw %0, %%dx \n\t"
                        "rep outsb \n\t"
                        : : "i"((short)TESTDEV_IO_PORT),
                        "S"(st1 + sizeof(st1) - 2), "c"(sizeof(st1) - 1));
                asm volatile("cld \n\t" : : );
                asm volatile("in %1, %0\n\t" : "=a"(r) : "i"((short)TESTDEV_IO_PORT));
                report(r == st1[0], "outsb down");
        }
Quoted 1 line

is not easy to write or read.

Agreed. But having to also "Bring Your Own #VC Handler" makes it even harder. Which is my point.

If we have helpers to load a #VC handler, then that resolves most of my concerns. Though, I still think having a default #VC handler for string IO is better than not having one. (More on that below.)

Quoted 7 lines

I'm envisioning SEV-ES string I/O tests will be things like:

        sev_es_outsb(..., TESTDEV_IO_PORT, sizeof(st1) - 1, st1);

        sev_es_outsb_backwards(..., TESTDEV_IO_PORT, sizeof(st1) - 1,
                               st1 + sizeof(st1) - 2));
Quoted 7 lines

where sev_es_outsb() is a wrapper to sev_es_do_string_io() or whatever and fills in all the appropriate SEV-ES IOIO_* constants.

Yes, we can and probably should add wrappers for the raw string I/O tests too. But, no matter what, somehwere there has to be a helper to translate raw string I/O into SEV-ES string I/O. I don't see why doing that in the #VC handler is any easier than doing it in a helper.

Hmmm... yeah, if this patch really does get vetoed then rather than throw it away maybe we can convert it to be loaded with a helper now.

Note: I hear your arguments, but I still don't agree with throwing away this patch. At least not based on the arguments made in this email thread. I think having a default #VC handler to handle string IO is better than not having one. Individual tests can always override it. From reading the other email thread on the decoder, I get the sense that the real reason you're opposed to this patch is because you're opposed to pulling in the Linux decoder. I don't follow that patch as well as this one. So that may or may not be a valid reason to nuke this patch. I'll leave that for others to discuss.

Quoted 11 lines
Quoted 3 lines

- I agree that writing test cases to straddle multiple pages, walk into non-existent memory, etc. is an excellent idea. But I don't follow how exposing the test itself to the #VC exit makes this easier.

The #VC handler does things like:

        ghcb_count = sizeof(ghcb->shared_buffer) / io_bytes;

to explicitly not mess up the _guest_ kernel. The proposed #VC handler literally cannot generate:

Quoted 8 lines

- a string I/O request larger than 2032 bytes
- does not reside inside the GHCB's internal buffer
- spans multiple pages
- points at illegal memory

And so on an so forth. And if we add helpers to allow that, then what value does the #VC handler provide since adding a wrapper to follow the Linux guest approach would be trivial?

Fair point. But having the #VC handler doesn't prevent tests from pivoting to their own handler when needed.

#25 lore raw

Re: [kvm-unit-tests PATCH v3 11/11] x86: AMD SEV-ES: Handle string IO for IOIO #VC

Quoted 6 lines · Marc Orr
Quoted 4 lines · Sean Christopherson

Yes, we can and probably should add wrappers for the raw string I/O tests too. But, no matter what, somehwere there has to be a helper to translate raw string I/O into SEV-ES string I/O. I don't see why doing that in the #VC handler is any easier than doing it in a helper.

Quoted 8 lines

Hmmm... yeah, if this patch really does get vetoed then rather than throw it away maybe we can convert it to be loaded with a helper now.

Note: I hear your arguments, but I still don't agree with throwing away this patch. At least not based on the arguments made in this email thread. I think having a default #VC handler to handle string IO is better than not having one. Individual tests can always override it.

What test is ever going to do its own string port I/O? String MMIO is a different beast because REP MOVS and REP STOS can be generated by the compiler almost at will, e.g. memcpy(), memset(), struct initialization, random for-loops, etc...

Port I/O on the other requires very deliberate code. I doubt it's even possible to generate string port I/O without resorting to assembly.

Outside of emulator.c, the next closest instance is the use of KVM's "force emulation prefix", which happens to sometimes decode as INSL due to using byte 0x6d :-)

realmode.c's print_serial() has string I/O, but (a) it's #ifdef'd out by default and (b) would be trivial to convert to a common helper.

In other words, any test that does string I/O is going to have to either open code it in inline asm or call a helper. I'd much prefer we enable the latter.

Quoted 5 lines

From reading the other email thread on the decoder, I get the sense that the real reason you're opposed to this patch is because you're opposed to pulling in the Linux decoder. I don't follow that patch as well as this one. So that may or may not be a valid reason to nuke this patch. I'll leave that for others to discuss.

Yeah, they're very intertwined, not having to pull in a massive decoder is a big motivation for not wanting string I/O support in the #VC handler. But, even were that not the case, IMO bouncing through the #VC handler for string I/O is asinine because the source of the #VC _knows_ that it wants to do string I/O. Just call a helper.