Add #VC exception handling for AMD SEV-ES
Subsystems (3)
v6 cover Add #VC exception handling for AMD SEV-ES
From: Vasant Karasulli <[email protected]>
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]/
v6:
- Rebased the patches on top of the current state of the test suite.
- Rebased the insn decoder on linux kernel e8c39d0f57f.
- Add a line about configuration option --amdsev-efi-vc in the x86/efi/README.md file.
v5:
- Rebased the patches on top of the current state of the test suite.
- Rebased the insn decoder on linux kernel b320441c04.
- Used the definition of struct ghcb and struct vmcb_save_area in the linux kernel b320441c04.
Vasant Karasulli (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 | 140 ++++
lib/x86/amd_sev_vc.c | 500 ++++++++++++
lib/x86/desc.c | 17 +
lib/x86/desc.h | 1 +
lib/x86/insn/README | 23 +
lib/x86/insn/gen-insn-attr-x86.awk | 443 +++++++++++
lib/x86/insn/inat.c | 86 ++
lib/x86/insn/inat.h | 233 ++++++
lib/x86/insn/inat_types.h | 18 +
lib/x86/insn/insn.c | 735 +++++++++++++++++
lib/x86/insn/insn.h | 279 +++++++
lib/x86/insn/insn_glue.h | 32 +
lib/x86/insn/x86-opcode-map.txt | 1191 ++++++++++++++++++++++++++++
lib/x86/msr.h | 1 +
lib/x86/processor.h | 25 +-
lib/x86/setup.c | 8 +
{x86 => lib/x86}/svm.h | 19 +-
lib/x86/xsave.c | 26 +
lib/x86/xsave.h | 15 +
x86/Makefile.common | 16 +-
x86/Makefile.x86_64 | 1 +
x86/efi/README.md | 4 +
x86/kvmclock.c | 4 -
x86/svm.c | 2 +-
x86/svm_tests.c | 2 +-
x86/xsave.c | 17 +-
31 files changed, 3832 insertions(+), 48 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 (97%)
create mode 100644 lib/x86/xsave.c
create mode 100644 lib/x86/xsave.hbase-commit: 7b0147ea57dc29ba844f5b60393a0639e55e88af --
2.34.1
PATCH v6 1/11 x86: AMD SEV-ES: Setup #VC exception handler for AMD SEV-ES
From: Vasant Karasulli <[email protected]>
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 | 15 +++++++++++++++ lib/x86/desc.c | 17 +++++++++++++++++ lib/x86/desc.h | 1 + lib/x86/setup.c | 8 ++++++++ x86/Makefile.common | 1 + x86/efi/README.md | 4 ++++ 10 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 lib/x86/amd_sev_vc.c
Makefile +3 -0
@@ -41,6 +41,9 @@ OBJDIRS += $(LIBFDT_objdir) # EFI App ifeq ($(CONFIG_EFI),y) EFI_CFLAGS := -DCONFIG_EFI -DCONFIG_RELOC +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
@@ -33,6 +33,12 @@ page_size= earlycon= efi= efi_direct= +# 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= # Enable -Werror by default for git repositories only (i.e. developer builds) if [ -e "$srcdir"/.git ]; then
@@ -97,6 +103,8 @@ usage() { system and run from the UEFI shell. Ignored when efi isn't enabled and defaults to enabled when efi is enabled for riscv64. (arm64 and riscv64 only) + --amdsev-efi-vc Use UEFI-provided #VC handlers on AMD SEV/ES. Requires + --enable-efi. EOF exit 1 }
@@ -188,6 +196,9 @@ while [[ "$1" = -* ]]; do --disable-werror) werror= ;; + --amdsev-efi-vc) + amdsev_efi_vc=y + ;; --help) usage ;;
@@ -278,8 +289,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 "$efi" ]; then + echo "--amdsev-efi-vc requires --enable-efi." + usage + fi elif [ "$arch" = "arm" ] || [ "$arch" = "arm64" ]; then testdir=arm if [ "$target" = "qemu" ]; then
@@ -451,6 +471,7 @@ HOST_KEY_DOCUMENT=$host_key_document CONFIG_DUMP=$enable_dump CONFIG_EFI=$efi EFI_DIRECT=$efi_direct +AMDSEV_EFI_VC=$amdsev_efi_vc CONFIG_WERROR=$werror GEN_SE_HEADER=$gen_se_header EOF
lib/x86/amd_sev.c +5 -8
@@ -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
@@ -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 +15 -0
@@ -0,0 +1,15 @@ +// 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 +17 -0
@@ -4,6 +4,9 @@#include "smp.h" #include <setjmp.h> #include "apic-defs.h" +#ifdef CONFIG_EFI +#include "amd_sev.h" +#endif /* Boot-related data structures */
@@ -250,6 +253,9 @@ EX_E(ac, 17);EX (mc,18 );EX (xm,19 );EX_E (cp,21 ); +#ifdef CONFIG_EFI +EX_E (vc,29 ); +#endif asm (".pushsection .text \n\t" "__handle_exception: \n\t"
@@ -316,6 +322,17 @@ void setup_idt(void) } } +#ifdef CONFIG_EFI +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); +} +#endif +void load_idt (void ) {lidt (&idt_descr);
lib/x86/desc.h +1 -0
@@ -252,6 +252,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
@@ -363,6 +363,14 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)save_id ();bsp_rest_init (); +#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
@@ -25,6 +25,7 @@ cflatobjs += lib/x86/delay.o cflatobjs += lib/x86/pmu.o ifeq ($(CONFIG_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
x86/efi/README.md +4 -0
@@ -18,6 +18,10 @@ To build: ./configure --enable-efi make +To make use of UEFI #VC handler for AMD SEV-ES: + + ./configure --enable-efi --amdsev-efi-vc + ### Run test cases with UEFI To run a test case with UEFI:
--
2.34.1
PATCH v6 2/11 x86: Move svm.h to lib/x86/
In reply to [email protected]From: Vasant Karasulli <[email protected]>
This enables sharing 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
x86/svm.c +1 -1
@@ -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
@@ -1,4 +1,4 @@ -#include "svm.h" +#include "x86/svm.h" #include "libcflat.h" #include "processor.h" #include "desc.h"
--
2.34.1
PATCH v6 3/11 lib: Define unlikely()/likely() macros in libcflat.h
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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
@@ -167,4 +167,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
@@ -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 ;
--
2.34.1
Re: [kvm-unit-tests PATCH v6 03/11] lib: Define unlikely()/likely() macros in libcflat.h
Quoted 17 lines · On Thu, Apr 11, 2024 at 07:29:36PM +0200, [email protected] wrote:
From: Vasant Karasulli <[email protected]>
So that they can be shared across testcases and
lib/. Linux's x86 instruction decoder refrences them.Signed-off-by Varad Gautam <[email protected]>Signed-off-by Vasant Karasulli <[email protected]>--- lib/libcflat.h | 3 +++ x86/kvmclock.c | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-)lib/libcflat.h +0
-0
lib/libcflat.h extern void setup_vm(void); · line 171
Quoted 5 lines
lib/libcflat.h +2
-0@@ -167,4 +167,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)
Please put these in lib/linux/compiler.h
Thanks,
drew
Quoted 20 lines
lib/libcflat.h +1
-0+#endif x86/kvmclock.c +0
-5@@ -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 ; --2.34.1
PATCH v6 4/11 lib: x86: Import insn decoder from Linux
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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 e8c39d0f57f358950356a8e44ee5159f57f86ec5
--- .gitignore | 2 + lib/x86/insn/README | 23 + lib/x86/insn/gen-insn-attr-x86.awk | 443 +++++++++++ lib/x86/insn/inat.c | 86 ++ lib/x86/insn/inat.h | 233 ++++++ lib/x86/insn/inat_types.h | 18 + lib/x86/insn/insn.c | 735 +++++++++++++++++ lib/x86/insn/insn.h | 279 +++++++ lib/x86/insn/insn_glue.h | 32 + lib/x86/insn/x86-opcode-map.txt | 1191 ++++++++++++++++++++++++++++ x86/Makefile.common | 14 +- 11 files changed, 3054 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
@@ -16,6 +16,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
@@ -0,0 +1,23 @@ +README +====== + +lib/x86/insn/ contains x86 instruction decoder src from Linux. + +The following files were taken as-is from Linux@e8c39d0f57f 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 +- I: 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 +443 -0
443 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
@@ -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@e8c39d0f57f +* 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 +735 -0
735 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
@@ -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 +1191 -0
1,143 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
--
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH v6 5/11 x86: AMD SEV-ES: Pull related GHCB definitions and helpers from Linux
In reply to [email protected]From: Vasant Karasulli <[email protected]>
Origin: Linux e8c39d0f57f358950356a8e44ee5159f57f86ec5
Suppress -Waddress-of-packed-member to allow taking addresses on struct ghcb / struct vmcb_save_area fields.
--- lib/x86/amd_sev.h | 136 ++++++++++++++++++++++++++++++++++++++++++++ lib/x86/msr.h | 1 + lib/x86/processor.h | 8 +++ lib/x86/svm.h | 19 +++++-- x86/Makefile.x86_64 | 1 + 5 files changed, 159 insertions(+), 6 deletions(-)
lib/x86/amd_sev.h +136 -0
@@ -18,6 +18,90 @@#include "desc.h" #include "asm/page.h" #include "efi.h" +#include "processor.h" +#include "insn/insn.h" +#include "svm.h" + +#define GHCB_SHARED_BUF_SIZE 2032 + +struct ghcb_save_area { +u8 reserved_0x0[203 ]; +u8 cpl; +u8 reserved_0xcc[116 ]; +u64 xss; +u8 reserved_0x148[24 ]; +u64 dr7; +u8 reserved_0x168[16 ]; +u64 rip; +u8 reserved_0x180[88 ]; +u64 rsp; +u8 reserved_0x1e0[24 ]; +u64 rax; +u8 reserved_0x200[264 ]; +u64 rcx; +u64 rdx; +u64 rbx; +u8 reserved_0x320[8 ]; +u64 rbp; +u64 rsi; +u64 rdi; +u64 r8; +u64 r9; +u64 r10; +u64 r11; +u64 r12; +u64 r13; +u64 r14; +u64 r15; +u8 reserved_0x380[16 ]; +u64 sw_exit_code; +u64 sw_exit_info_1; +u64 sw_exit_info_2; +u64 sw_scratch; +u8 reserved_0x3b0[56 ]; +u64 xcr0; +u8 valid_bitmap[16 ]; +u64 x87_state_gpa; +} __packed; + +struct ghcb { +struct ghcb_save_area save; +u8 reserved_save[2048 -sizeof (struct ghcb_save_area )]; + +u8 shared_buffer[GHCB_SHARED_BUF_SIZE ]; + +u8 reserved_0xff0[10 ]; +u16 protocol_version;/* negotiated SEV-ES/GHCB protocol version */ +u32 ghcb_usage; +} __packed; + +#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 +143,58 @@ 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 ghcb_save_area, field) / sizeof(u64)) + +#define DEFINE_GHCB_ACCESSORS (field) \ + static __always_inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \ + { \ + return test_bit(GHCB_BITMAP_IDX(field), \ + (unsigned long *)&ghcb->save.valid_bitmap); \ + } \ + \ + static __always_inline u64 ghcb_get_##field(struct ghcb *ghcb) \ + { \ + return ghcb->save.field; \ + } \ + \ + static __always_inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \ + { \ + return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0; \ + } \ + \ + static __always_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 /* CONFIG_EFI */ #endif /* _X86_AMD_SEV_H_ */
lib/x86/msr.h +1 -0
@@ -151,6 +151,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 +8 -0
@@ -837,6 +837,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 +13 -6
@@ -175,11 +175,13 @@ struct __attribute__ ((__packed__)) vmcb_save_area {struct vmcb_seg ldtr;struct vmcb_seg idtr;struct vmcb_seg tr; -u8 reserved_1[43 ]; +/* Reserved fields are named following their struct offset */ +u8 reserved_0xa0[42 ]; +u8 vmpl;u8 cpl; -u8 reserved_2[4 ]; +u8 reserved_0xcc[4 ];u64 efer; -u8 reserved_3[112 ]; +u8 reserved_0xd8[112 ];u64 cr4;u64 cr3;u64 cr0;
@@ -187,9 +189,11 @@ struct __attribute__ ((__packed__)) vmcb_save_area {u64 dr6;u64 rflags;u64 rip; -u8 reserved_4[88 ]; +u8 reserved_0x180[88 ];u64 rsp; -u8 reserved_5[24 ]; +u64 s_cet; +u64 ssp; +u64 isst_addr;u64 rax;u64 star;u64 lstar;
@@ -200,13 +204,15 @@ struct __attribute__ ((__packed__)) vmcb_save_area {u64 sysenter_esp;u64 sysenter_eip;u64 cr2; -u8 reserved_6[32 ]; +u8 reserved_0x248[32 ];u64 g_pat;u64 dbgctl;u64 br_from;u64 br_to;u64 last_excp_from;u64 last_excp_to; +u8 reserved_0x298[72 ]; +u32 spec_ctrl;/* Guest version of SPEC_CTRL at 0x2E0 */ };struct __attribute__ ((__packed__ ))vmcb {
@@ -307,6 +313,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
@@ -19,6 +19,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
--
2.34.1
PATCH v6 6/11 x86: AMD SEV-ES: Prepare for #VC processing
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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 e8c39d0f57f358950356a8e44ee5159f57f86ec5
--- lib/x86/amd_sev_vc.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+)
lib/x86/amd_sev_vc.c +87 -0
@@ -1,15 +1,102 @@// SPDX-License-Identifier: GPL-2.0 +/* +* AMD SEV-ES #VC exception handling. +* Adapted from Linux@b320441c04: +* - arch/x86/kernel/sev.c +* - arch/x86/kernel/sev-shared.c +*/ #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 ; }
--
2.34.1
PATCH v6 7/11 lib/x86: Move xsave helpers to lib/
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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/processor.h | 10 ---------- lib/x86/xsave.c | 26 ++++++++++++++++++++++++++ lib/x86/xsave.h | 15 +++++++++++++++ x86/Makefile.common | 1 + x86/xsave.c | 17 +---------------- 5 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 lib/x86/xsave.c create mode 100644 lib/x86/xsave.h
lib/x86/processor.h +0 -10
@@ -477,16 +477,6 @@ static inline uint64_t rdpmc(uint32_t index)return val; } -static inline int xgetbv_safe (u32 index,u64 *result) -{ -return rdreg64_safe (".byte 0x0f,0x01,0xd0" , index, result); -} - -static inline int xsetbv_safe (u32 index,u64 value) -{ -return wrreg64_safe (".byte 0x0f,0x01,0xd1" , index, value); -} -static inline int write_cr0_safe (ulong val) {return asm_safe ("mov %0,%%cr0" ,"r" (val));
lib/x86/xsave.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "libcflat.h" +#include "xsave.h" +#include "processor.h" + +int xgetbv_safe (u32 index,u64 *result) +{ +return rdreg64_safe (".byte 0x0f,0x01,0xd0" , index, result); +} + +int xsetbv_safe (u32 index,u64 value) +{ +return wrreg64_safe (".byte 0x0f,0x01,0xd1" , index, value); +} + +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 +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _X86_XSAVE_H_ +#define _X86_XSAVE_H_ + +#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_safe (u32 index,u64 *result); +int xsetbv_safe (u32 index,u64 value); +uint64_t get_supported_xcr0 (void ); +#endif
x86/Makefile.common +1 -0
@@ -23,6 +23,7 @@ cflatobjs += lib/x86/stack.o cflatobjs += lib/x86/fault_test.o cflatobjs += lib/x86/delay.o cflatobjs += lib/x86/pmu.o +cflatobjs += lib/x86/xsave.o ifeq ($(CONFIG_EFI),y) cflatobjs += lib/x86/amd_sev.o cflatobjs += lib/x86/amd_sev_vc.o
x86/xsave.c +1 -16
@@ -1,6 +1,7 @@#include "libcflat.h" #include "desc.h" #include "processor.h" +#include "xsave.h" #ifdef __x86_64__#define uint64_t unsigned long
@@ -8,22 +9,6 @@#define uint64_t unsigned long long#endif -static 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 ); -} - -#define XCR_XFEATURE_ENABLED_MASK 0x00000000 -#define XCR_XFEATURE_ILLEGAL_MASK 0x00000010 - -#define XSTATE_FP 0x1 -#define XSTATE_SSE 0x2 -#define XSTATE_YMM 0x4 -static void test_xsave (void ) {unsigned long cr4;
--
2.34.1
PATCH v6 8/11 x86: AMD SEV-ES: Handle CPUID #VC
In reply to [email protected]From: Vasant Karasulli <[email protected]>
Using Linux's CPUID #VC processing logic.
--- lib/x86/amd_sev.h | 5 ++- lib/x86/amd_sev_vc.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-)
lib/x86/amd_sev.h +4 -1
@@ -71,7 +71,7 @@ struct ghcb {u8 shared_buffer[GHCB_SHARED_BUF_SIZE ];u8 reserved_0xff0[10 ]; -u16 protocol_version;/* negotiated SEV-ES/GHCB protocol version */ +u16 version;/* version of the GHCB data structure */ u32 ghcb_usage; } __packed;
@@ -79,6 +79,9 @@ struct ghcb {#define GHCB_PROTOCOL_MAX 1ULL#define GHCB_DEFAULT_USAGE 0ULL +/* Version of the GHCB data structure */ +#define GHCB_VERSION 1 +#define VMGEXIT () { asm volatile("rep; vmmcall\n\r"); }enum es_result {
lib/x86/amd_sev_vc.c +97 -0
115 lines not on this page · raw message
--
2.34.1
PATCH v6 9/11 x86: AMD SEV-ES: Handle MSR #VC
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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
@@ -152,6 +152,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)
@@ -162,6 +187,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
--
2.34.1
PATCH v6 10/11 x86: AMD SEV-ES: Handle IOIO #VC
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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
@@ -177,6 +177,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)
@@ -190,6 +356,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
@@ -835,6 +835,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;
--
2.34.1
PATCH v6 11/11 x86: AMD SEV-ES: Handle string IO for IOIO #VC
In reply to [email protected]From: Vasant Karasulli <[email protected]>
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
@@ -306,10 +306,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);
@@ -317,7 +353,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 */
--
2.34.1