x86: SEV-ES Guest Support
Subsystems (9)
v3 cover x86: SEV-ES Guest Support
Hi,
here is the next version of changes to enable Linux to run as an SEV-ES guest. The code was rebased to v5.7-rc3 and got a fair number of changes since the last version.
What is SEV-ES
==============
SEV-ES is an acronym for 'Secure Encrypted Virtualization - Encrypted State' and means a hardware feature of AMD processors which hides the register state of VCPUs to the hypervisor by encrypting it. The hypervisor can't read or make changes to the guests register state.
Most intercepts set by the hypervisor do not cause a #VMEXIT of the guest anymore, but turn into a VMM Communication Exception (#VC exception, vector 29) inside the guest. The error-code of this exception is the intercept exit-code that caused the exception. The guest handles the #VC exception by communicating with the hypervisor through a shared data structure, the 'Guest-Hypervisor-Communication-Block'.
With SEV-ES an untrusted hypervisor can no longer steal secrets from a guest via inspecting guest memory or guest register contents. A malicious hypervisor can still interfere with guest operations, but the SEV-ES client code does its best to detect such situations and crash the VM if it happens.
More information about the implementation details can be found in the cover letter of the initial post of these patches:
https://lore.kernel.org/lkml/[email protected]/
Current State of the Patches
============================
The patches posted here are considered feature complete and survived a good amount of functional testing:
1) Booting an SEV-ES guest VM to the graphical desktop.
2) Running a 16-vcpu SEV-ES VM with 'perf top' and the x86 selftests shipped with the kernel source in a loop in parallel for 18 hours showed no issues.
3) Various compile testing has been done, including allno/allmod/allyes/defconfig for x86-64 and i386.
4) Compiled every single commit (single .config only) to check if they build and do not introduce new warnings.
5) Boot-tested the changes on various machines, including bare-metal on an AMD (with and without mem_encrypt=on) and Intel machine.
A git-tree with these patches applied can be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/joro/linux.git/log/?h=sev-es-client-v5.7-rc3
Changes to the previous version
===============================
Here is an incomplete list of changes to the previous version of this patch-set. There have been so many small changes that I havn't kept track of all, but the most important ones are listed:
- Rebased to v5.7-rc3
- Changes the #VC exception handler to use an IST stack. This includes a couple of additional patches to set up and map the IST stack, to make dumpstack aware of them and to fix a race with the NMI handler when shifting the #VC handlers IST entry.
- The NMI_Complete message to the hypervisor the re-open the NMI window is now sent at the very beginning of do_nmi().
- The GHCB used in the pre-decompression code is now re-mapped encrypted and flushed from the cache before jumping to the decompressed kernel image. This is needed to make sure the running kernel can safely re-use the page. Actually the page is also unmapped after being re-encrypted to catch any use-after-remap.
- Added CPUID caching.
- Mapped the GHCBs to the EFI page-tables as the UEFI BIOS expects to be able to re-use the OS GHCBs.
- RDTSC and RDTSCP are now also handled in the pre-decompression boot code. These instructions are used by KASLR and some hypervisors might intercept them.
- Re-implemented nested GHCB handling by keeping a backup GHCB around. This supports one level of GHCB nesting, which is sufficient for now.
- Moved all SEV-ES related per-cpu data into 'struct sev_es_runtime_data'. This struct is allocated and initialized at boot per cpu.
- Correctly set the protocol and ghcb_usage information when talking to the hypervisor.
The previous version of the patch-set can be found here:
https://lore.kernel.org/lkml/[email protected]/
Please review.
Thanks,
Joerg
Borislav Petkov (1):
KVM: SVM: Use __packed shorthand
Doug Covelli (1):
x86/vmware: Add VMware specific handling for VMMCALL under SEV-ES
Joerg Roedel (53):
KVM: SVM: Add GHCB Accessor functions
x86/traps: Move some definitions to <asm/trap_defs.h> x86/insn: Make inat-tables.c suitable for pre-decompression code x86/umip: Factor out instruction fetch
x86/umip: Factor out instruction decoding
x86/insn: Add insn_get_modrm_reg_off()
x86/insn: Add insn_rep_prefix() helper x86/boot/compressed/64: Disable red-zone usage x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded x86/boot/compressed/64: Add IDT Infrastructure x86/boot/compressed/64: Rename kaslr_64.c to ident_map_64.c x86/boot/compressed/64: Add page-fault handler x86/boot/compressed/64: Always switch to own page-table x86/boot/compressed/64: Don't pre-map memory in KASLR code x86/boot/compressed/64: Change add_identity_map() to take start and
end
x86/boot/compressed/64: Add stage1 #VC handler x86/boot/compressed/64: Call set_sev_encryption_mask earlier x86/boot/compressed/64: Check return value of
kernel_ident_mapping_init()
x86/boot/compressed/64: Add set_page_en/decrypted() helpers x86/boot/compressed/64: Setup GHCB Based VC Exception handler x86/boot/compressed/64: Unmap GHCB page before booting the kernel x86/fpu: Move xgetbv()/xsetbv() into separate header x86/idt: Move IDT to data segment
x86/idt: Split idt_data setup out of set_intr_gate() x86/idt: Move two function from k/idt.c to i/a/desc.h x86/head/64: Install boot GDT
x86/head/64: Reload GDT after switch to virtual addresses x86/head/64: Load segment registers earlier
x86/head/64: Switch to initial stack earlier
x86/head/64: Build k/head64.c with -fno-stack-protector x86/head/64: Load IDT earlier
x86/head/64: Move early exception dispatch to C code x86/sev-es: Add SEV-ES Feature Detection
x86/sev-es: Print SEV-ES info into kernel log
x86/sev-es: Compile early handler code into kernel image x86/sev-es: Setup early #VC handler
x86/sev-es: Setup GHCB based boot #VC handler
x86/sev-es: Allocate and Map IST stacks for #VC handler x86/dumpstack/64: Handle #VC exception stacks
x86/sev-es: Shift #VC IST Stack in nmi_enter()/nmi_exit() x86/sev-es: Wire up existing #VC exit-code handlers x86/sev-es: Handle instruction fetches from user-space x86/sev-es: Do not crash on #VC exceptions from user-space x86/sev-es: Handle MMIO String Instructions
x86/sev-es: Handle #AC Events
x86/sev-es: Handle #DB Events
x86/paravirt: Allow hypervisor specific VMMCALL handling under SEV-ES x86/realmode: Add SEV-ES specific trampoline entry point x86/head/64: Setup TSS early for secondary CPUs x86/head/64: Don't call verify_cpu() on starting APs x86/head/64: Rename start_cpu0
x86/sev-es: Support CPU offline/online
x86/sev-es: Handle NMI State
Mike Stunes (1):
x86/sev-es: Cache CPUID results for improved performance
Tom Lendacky (19):
KVM: SVM: Add GHCB definitions x86/cpufeatures: Add SEV-ES CPU feature x86/sev-es: Add support for handling IOIO exceptions x86/sev-es: Add CPUID handling to #VC handler x86/sev-es: Setup per-cpu GHCBs for the runtime handler x86/sev-es: Add Runtime #VC Exception Handler x86/sev-es: Handle MMIO events
x86/sev-es: Handle MSR events
x86/sev-es: Handle DR7 read/write events x86/sev-es: Handle WBINVD Events
x86/sev-es: Handle RDTSC(P) Events
x86/sev-es: Handle RDPMC Events
x86/sev-es: Handle INVD Events
x86/sev-es: Handle MONITOR/MONITORX Events x86/sev-es: Handle MWAIT/MWAITX Events x86/sev-es: Handle VMMCALL Events
x86/kvm: Add KVM specific VMMCALL handling under SEV-ES x86/realmode: Setup AP jump table
x86/efi: Add GHCB mappings when SEV-ES is active
arch/x86/Kconfig | 1 + arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/Makefile | 9 +- arch/x86/boot/compressed/head_64.S | 40 +- arch/x86/boot/compressed/ident_map_64.c | 339 +++++ arch/x86/boot/compressed/idt_64.c | 53 + arch/x86/boot/compressed/idt_handlers_64.S | 76 ++ arch/x86/boot/compressed/kaslr.c | 36 +- arch/x86/boot/compressed/kaslr_64.c | 153 --- arch/x86/boot/compressed/misc.c | 7 + arch/x86/boot/compressed/misc.h | 45 +- arch/x86/boot/compressed/sev-es.c | 210 +++ arch/x86/entry/entry_64.S | 4 + arch/x86/include/asm/cpu.h | 2 +- arch/x86/include/asm/cpu_entry_area.h | 62 + arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/desc.h | 30 + arch/x86/include/asm/desc_defs.h | 10 + arch/x86/include/asm/fpu/internal.h | 29 +- arch/x86/include/asm/fpu/xcr.h | 32 + arch/x86/include/asm/hardirq.h | 14 + arch/x86/include/asm/insn-eval.h | 6 + arch/x86/include/asm/mem_encrypt.h | 5 + arch/x86/include/asm/msr-index.h | 3 + arch/x86/include/asm/page_64_types.h | 1 + arch/x86/include/asm/pgtable.h | 2 +- arch/x86/include/asm/processor.h | 1 + arch/x86/include/asm/realmode.h | 4 + arch/x86/include/asm/segment.h | 2 +- arch/x86/include/asm/setup.h | 1 - arch/x86/include/asm/sev-es.h | 107 ++ arch/x86/include/asm/stacktrace.h | 4 + arch/x86/include/asm/svm.h | 115 +- arch/x86/include/asm/trap_defs.h | 50 + arch/x86/include/asm/traps.h | 51 +- arch/x86/include/asm/x86_init.h | 16 +- arch/x86/include/uapi/asm/svm.h | 11 + arch/x86/kernel/Makefile | 5 + arch/x86/kernel/asm-offsets_64.c | 1 + arch/x86/kernel/cpu/amd.c | 3 +- arch/x86/kernel/cpu/common.c | 1 + arch/x86/kernel/cpu/scattered.c | 1 + arch/x86/kernel/cpu/vmware.c | 50 +- arch/x86/kernel/dumpstack_64.c | 47 + arch/x86/kernel/head64.c | 70 +- arch/x86/kernel/head_32.S | 4 +- arch/x86/kernel/head_64.S | 176 ++- arch/x86/kernel/idt.c | 52 +- arch/x86/kernel/kvm.c | 35 +- arch/x86/kernel/nmi.c | 8 + arch/x86/kernel/sev-es-shared.c | 479 +++++++ arch/x86/kernel/sev-es.c | 1428 ++++++++++++++++++++ arch/x86/kernel/smpboot.c | 4 +- arch/x86/kernel/traps.c | 3 + arch/x86/kernel/umip.c | 49 +- arch/x86/lib/insn-eval.c | 130 ++ arch/x86/mm/extable.c | 1 + arch/x86/mm/mem_encrypt.c | 39 +- arch/x86/mm/mem_encrypt_identity.c | 3 + arch/x86/platform/efi/efi_64.c | 10 + arch/x86/realmode/init.c | 12 + arch/x86/realmode/rm/header.S | 3 + arch/x86/realmode/rm/trampoline_64.S | 20 + arch/x86/tools/gen-insn-attr-x86.awk | 50 +- tools/arch/x86/tools/gen-insn-attr-x86.awk | 50 +- 65 files changed, 3842 insertions(+), 426 deletions(-) create mode 100644 arch/x86/boot/compressed/ident_map_64.c create mode 100644 arch/x86/boot/compressed/idt_64.c create mode 100644 arch/x86/boot/compressed/idt_handlers_64.S delete mode 100644 arch/x86/boot/compressed/kaslr_64.c create mode 100644 arch/x86/boot/compressed/sev-es.c create mode 100644 arch/x86/include/asm/fpu/xcr.h create mode 100644 arch/x86/include/asm/sev-es.h create mode 100644 arch/x86/include/asm/trap_defs.h create mode 100644 arch/x86/kernel/sev-es-shared.c create mode 100644 arch/x86/kernel/sev-es.c
Signature · 1 line
2.17.1
PATCH v3 1/75 KVM: SVM: Add GHCB definitions
From: Tom Lendacky <[email protected]>
Extend the vmcb_safe_area with SEV-ES fields and add a new 'struct ghcb' which will be used for guest-hypervisor communication.
--- arch/x86/include/asm/svm.h | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+)
arch/x86/include/asm/svm.h +42 -0
@@ -201,6 +201,48 @@ 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. +*/ +u8 reserved_7[104 ]; +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; +u8 reserved_12[1016 ]; +}; + +struct __attribute__ ((__packed__ ))ghcb { +struct vmcb_save_area save; + +u8 shared_buffer[2032 ]; + +u8 reserved_1[10 ]; +u16 protocol_version;/* negotiated SEV-ES/GHCB protocol version */ +u32 ghcb_usage; };struct __attribute__ ((__packed__ ))vmcb {
Signature · 1 line
2.17.1
PATCH v3 2/75 KVM: SVM: Add GHCB Accessor functions
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Building a correct GHCB for the hypervisor requires setting valid bits in the GHCB. Simplify that process by providing accessor functions to set values and to update the valid bitmap.
--- arch/x86/include/asm/svm.h | 61 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+)
arch/x86/include/asm/svm.h +61 -0
@@ -333,4 +333,65 @@ struct __attribute__ ((__packed__)) vmcb {#define SVM_CR0_SELECTIVE_MASK (X86_CR0_TS | X86_CR0_MP) +/* GHCB Accessor functions */ + +#define DEFINE_GHCB_INDICES (field) \ + u16 idx = offsetof(struct vmcb_save_area, field) / 8; \ + u16 byte_idx = idx / 8; \ + u16 bit_idx = idx % 8; \ + BUILD_BUG_ON(byte_idx > ARRAY_SIZE(ghcb->save.valid_bitmap)); + +#define GHCB_SET_VALID (ghcb, field) \ + { \ + DEFINE_GHCB_INDICES(field) \ + (ghcb)->save.valid_bitmap[byte_idx] |= BIT(bit_idx); \ + } + +#define DEFINE_GHCB_SETTER (field) \ + static inline void \ + ghcb_set_##field(struct ghcb *ghcb, u64 value) \ + { \ + GHCB_SET_VALID(ghcb, field) \ + (ghcb)->save.field = value; \ + } + +#define DEFINE_GHCB_ACCESSORS (field) \ + static inline bool ghcb_is_valid_##field(const struct ghcb *ghcb) \ + { \ + DEFINE_GHCB_INDICES(field) \ + return !!((ghcb)->save.valid_bitmap[byte_idx] \ + & BIT(bit_idx)); \ + } \ + \ + static inline void \ + ghcb_set_##field(struct ghcb *ghcb, u64 value) \ + { \ + GHCB_SET_VALID(ghcb, field) \ + (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
Signature · 1 line
2.17.1
PATCH v3 3/75 KVM: SVM: Use __packed shorthand
In reply to Joerg RoedelFrom: Borislav Petkov <[email protected]>
I guess we can do that ontop.
--- arch/x86/include/asm/svm.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
arch/x86/include/asm/svm.h +8 -8
@@ -151,14 +151,14 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_NP_ENABLE BIT(0)#define SVM_NESTED_CTL_SEV_ENABLE BIT(1) -struct __attribute__ ((__packed__ ))vmcb_seg { +struct vmcb_seg {u16 selector;u16 attrib;u32 limit;u64 base; -}; +} __packed; -struct __attribute__ ((__packed__ ))vmcb_save_area { +struct vmcb_save_area {struct vmcb_seg es;struct vmcb_seg cs;struct vmcb_seg ss;
@@ -233,9 +233,9 @@ struct __attribute__ ((__packed__)) vmcb_save_area {u8 valid_bitmap[16 ];u64 x87_state_gpa;u8 reserved_12[1016 ]; -}; +} __packed; -struct __attribute__ ((__packed__ ))ghcb { +struct ghcb {struct vmcb_save_area save;u8 shared_buffer[2032 ];
@@ -243,12 +243,12 @@ struct __attribute__ ((__packed__)) ghcb {u8 reserved_1[10 ];u16 protocol_version;/* negotiated SEV-ES/GHCB protocol version */ u32 ghcb_usage; -}; +} __packed; -struct __attribute__ ((__packed__ ))vmcb { +struct vmcb {struct vmcb_control_area control;struct vmcb_save_area save; -}; +} __packed;#define SVM_CPUID_FUNC 0x8000000a
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 03/75] KVM: SVM: Use __packed shorthand
Quoted 3 lines · Joerg Roedel
From: Borislav Petkov <[email protected]>
I guess we can do that ontop.
The proper commit message was:
"... to make it more readable.
No functional changes."
:-)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 4/75 x86/cpufeatures: Add SEV-ES CPU feature
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Add CPU feature detection for Secure Encrypted Virtualization with Encrypted State. This feature enhances SEV by also encrypting the guest register state, making it in-accessible to the hypervisor.
--- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- arch/x86/kernel/cpu/scattered.c | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -234,6 +234,7 @@#define X86_FEATURE_EPT_AD ( 8*32+17)/* Intel Extended Page Table access-dirty bit */ #define X86_FEATURE_VMCALL ( 8*32+18)/* "" Hypervisor supports the VMCALL instruction */ #define X86_FEATURE_VMW_VMMCALL ( 8*32+19)/* "" VMware prefers VMMCALL hypercall instruction */ +#define X86_FEATURE_SEV_ES ( 8*32+20)/* AMD Secure Encrypted Virtualization - Encrypted State */ /* Intel-defined CPU features, CPUID level 0x00000007:0 (EBX), word 9 */ #define X86_FEATURE_FSGSBASE ( 9*32+ 0)/* RDFSBASE, WRFSBASE, RDGSBASE, WRGSBASE instructions*/
arch/x86/kernel/cpu/amd.c +2 -1
@@ -610,7 +610,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)* If BIOS has not enabled SME then don't advertise the * SME feature (set in scattered.c). * For SEV: If BIOS has not enabled SEV then don't advertise the -* SEV feature (set in scattered.c). +* SEV and SEV_ES feature (set in scattered.c). * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32.
@@ -641,6 +641,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c)setup_clear_cpu_cap (X86_FEATURE_SME ); clear_sev:setup_clear_cpu_cap (X86_FEATURE_SEV ); +setup_clear_cpu_cap (X86_FEATURE_SEV_ES ); } }
arch/x86/kernel/cpu/scattered.c +1 -0
@@ -41,6 +41,7 @@ static const struct cpuid_bit cpuid_bits[] = { { X86_FEATURE_MBA, CPUID_EBX, 6, 0x80000008, 0 }, { X86_FEATURE_SME, CPUID_EAX, 0, 0x8000001f, 0 }, { X86_FEATURE_SEV, CPUID_EAX, 1, 0x8000001f, 0 }, + { X86_FEATURE_SEV_ES, CPUID_EAX, 3, 0x8000001f, 0 }, { 0, 0, 0, 0, 0 } };
Signature · 1 line
2.17.1
PATCH v3 5/75 x86/traps: Move some definitions to <asm/trap_defs.h>
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Move the definition of x86 trap vector numbers and the page-fault error code bits to the new header file asm/trap_defs.h. This makes it easier to include them into pre-decompression boot code. No functional changes.
--- arch/x86/include/asm/trap_defs.h | 49 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/traps.h | 44 +--------------------------- 2 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 arch/x86/include/asm/trap_defs.h
arch/x86/include/asm/trap_defs.h new file +49 -0
@@ -0,0 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_TRAP_DEFS_H +#define _ASM_X86_TRAP_DEFS_H + +/* Interrupts/Exceptions */ +enum { +X86_TRAP_DE =0 ,/* 0, Divide-by-zero */ +X86_TRAP_DB ,/* 1, Debug */ +X86_TRAP_NMI ,/* 2, Non-maskable Interrupt */ +X86_TRAP_BP ,/* 3, Breakpoint */ +X86_TRAP_OF ,/* 4, Overflow */ +X86_TRAP_BR ,/* 5, Bound Range Exceeded */ +X86_TRAP_UD ,/* 6, Invalid Opcode */ +X86_TRAP_NM ,/* 7, Device Not Available */ +X86_TRAP_DF ,/* 8, Double Fault */ +X86_TRAP_OLD_MF ,/* 9, Coprocessor Segment Overrun */ +X86_TRAP_TS ,/* 10, Invalid TSS */ +X86_TRAP_NP ,/* 11, Segment Not Present */ +X86_TRAP_SS ,/* 12, Stack Segment Fault */ +X86_TRAP_GP ,/* 13, General Protection Fault */ +X86_TRAP_PF ,/* 14, Page Fault */ +X86_TRAP_SPURIOUS ,/* 15, Spurious Interrupt */ +X86_TRAP_MF ,/* 16, x87 Floating-Point Exception */ +X86_TRAP_AC ,/* 17, Alignment Check */ +X86_TRAP_MC ,/* 18, Machine Check */ +X86_TRAP_XF ,/* 19, SIMD Floating-Point Exception */ +X86_TRAP_IRET =32 ,/* 32, IRET Exception */ +}; + +/* +* Page fault error code bits: +* +* bit 0 == 0: no page found 1: protection fault +* bit 1 == 0: read access 1: write access +* bit 2 == 0: kernel-mode access 1: user-mode access +* bit 3 == 1: use of reserved bit detected +* bit 4 == 1: fault was an instruction fetch +* bit 5 == 1: protection keys block access +*/ +enum x86_pf_error_code { +X86_PF_PROT =1 <<0 , +X86_PF_WRITE =1 <<1 , +X86_PF_USER =1 <<2 , +X86_PF_RSVD =1 <<3 , +X86_PF_INSTR =1 <<4 , +X86_PF_PK =1 <<5 , +}; + +#endif /* _ASM_X86_TRAP_DEFS_H */
arch/x86/include/asm/traps.h +1 -43
54 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 6/75 x86/insn: Make inat-tables.c suitable for pre-decompression code
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The inat-tables.c file has some arrays in it that contain pointers to other arrays. These pointers need to be relocated when the kernel image is moved to a different location.
The pre-decompression boot-code has no support for applying ELF relocations, so initialize these arrays at runtime in the pre-decompression code to make sure all pointers are correctly initialized.
--- arch/x86/tools/gen-insn-attr-x86.awk | 50 +++++++++++++++++++++- tools/arch/x86/tools/gen-insn-attr-x86.awk | 50 +++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-)
arch/x86/tools/gen-insn-attr-x86.awk +49 -1
@@ -362,6 +362,9 @@ function convert_operands(count,opnd, i,j,imm,mod) END { if (awkchecked != "") exit 1 + + print "#ifndef __BOOT_COMPRESSED\n" + # print escape opcode map's array print "/* Escape opcode map array */" print "const insn_attr_t * const inat_escape_tables[INAT_ESC_MAX + 1]" \
@@ -388,6 +391,51 @@ END { for (j = 0; j < max_lprefix; j++) if (atable[i,j]) print " ["i"]["j"] = "atable[i,j]"," - print "};" + print "};\n" + + print "#else /* !__BOOT_COMPRESSED */\n" + + print "/* Escape opcode map array */" + print "static const insn_attr_t *inat_escape_tables[INAT_ESC_MAX + 1]" \ + "[INAT_LSTPFX_MAX + 1];" + print "" + + print "/* Group opcode map array */" + print "static const insn_attr_t *inat_group_tables[INAT_GRP_MAX + 1]"\ + "[INAT_LSTPFX_MAX + 1];" + print "" + + print "/* AVX opcode map array */" + print "static const insn_attr_t *inat_avx_tables[X86_VEX_M_MAX + 1]"\ + "[INAT_LSTPFX_MAX + 1];" + print "" + + print "static void inat_init_tables(void)" + print "{" + + # print escape opcode map's array + print "\t/* Print Escape opcode map array */" + for (i = 0; i < geid; i++) + for (j = 0; j < max_lprefix; j++) + if (etable[i,j]) + print "\tinat_escape_tables["i"]["j"] = "etable[i,j]";" + print "" + + # print group opcode map's array + print "\t/* Print Group opcode map array */" + for (i = 0; i < ggid; i++) + for (j = 0; j < max_lprefix; j++) + if (gtable[i,j]) + print "\tinat_group_tables["i"]["j"] = "gtable[i,j]";" + print "" + # print AVX opcode map's array + print "\t/* Print AVX opcode map array */" + for (i = 0; i < gaid; i++) + for (j = 0; j < max_lprefix; j++) + if (atable[i,j]) + print "\tinat_avx_tables["i"]["j"] = "atable[i,j]";" + + print "}" + print "#endif" }
tools/arch/x86/tools/gen-insn-attr-x86.awk +49 -1
61 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 7/75 x86/umip: Factor out instruction fetch
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Factor out the code to fetch the instruction from user-space to a helper function.
No functional changes.
--- arch/x86/include/asm/insn-eval.h | 2 ++ arch/x86/kernel/umip.c | 26 +++++----------------- arch/x86/lib/insn-eval.c | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 20 deletions(-)
arch/x86/include/asm/insn-eval.h +2 -0
@@ -19,5 +19,7 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs); +int insn_fetch_from_user (struct pt_regs *regs, +unsigned char buf[MAX_INSN_SIZE ]);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/kernel/umip.c +6 -20
@@ -317,11 +317,11 @@ static void force_sig_info_umip_fault(void __user *addr, struct pt_regs *regs)*/ bool fixup_umip_exception (struct pt_regs *regs) { -int not_copied, nr_copied, reg_offset, dummy_data_size, umip_inst; -unsigned long seg_base =0 , *reg_addr; +int nr_copied, reg_offset, dummy_data_size, umip_inst;/* 10 bytes is the maximum size of the result of UMIP instructions */ unsigned char dummy_data[10 ] = {0 };unsigned char buf[MAX_INSN_SIZE ]; +unsigned long *reg_addr;void __user *uaddr;struct insn insn;int seg_defs;
@@ -329,26 +329,12 @@ bool fixup_umip_exception(struct pt_regs *regs)if (!regs)return false; -/* -* If not in user-space long mode, a custom code segment could be in -* use. This is true in protected mode (if the process defined a local -* descriptor table), or virtual-8086 mode. In most of the cases -* seg_base will be zero as in USER_CS. -*/ -if (!user_64bit_mode (regs)) - seg_base =insn_get_seg_base (regs,INAT_SEG_REG_CS ); - -if (seg_base ==-1L ) -return false; - - not_copied =copy_from_user (buf, (void __user *)(seg_base + regs->ip), -sizeof (buf)); - nr_copied =sizeof (buf) - not_copied; + nr_copied =insn_fetch_from_user (regs, buf);/* -* The copy_from_user above could have failed if user code is protected -* by a memory protection key. Give up on emulation in such a case. -* Should we issue a page fault? +* The insn_fetch_from_user above could have failed if user code +* is protected by a memory protection key. Give up on emulation +* in such a case. Should we issue a page fault? */ if (!nr_copied)return false;
arch/x86/lib/insn-eval.c +38 -0
@@ -1369,3 +1369,41 @@ void __user *insn_get_addr_ref(struct insn *insn, struct pt_regs *regs)return (void __user *)-1L ; } } + +/** +* insn_fetch_from_user() - Copy instruction bytes from user-space memory +* @regs: Structure with register values as seen when entering kernel mode +* @buf: Array to store the fetched instruction +* +* Gets the linear address of the instruction and copies the instruction bytes +* to the buf. +* +* Returns: +* +* Number of instruction bytes copied. +* +* 0 if nothing was copied. +*/ +int insn_fetch_from_user (struct pt_regs *regs,unsigned char buf[MAX_INSN_SIZE ]) +{ +unsigned long seg_base =0 ; +int not_copied; + +/* +* If not in user-space long mode, a custom code segment could be in +* use. This is true in protected mode (if the process defined a local +* descriptor table), or virtual-8086 mode. In most of the cases +* seg_base will be zero as in USER_CS. +*/ +if (!user_64bit_mode (regs)) { + seg_base =insn_get_seg_base (regs,INAT_SEG_REG_CS ); +if (seg_base ==-1L ) +return 0 ; + } + + + not_copied =copy_from_user (buf, (void __user *)(seg_base + regs->ip), +MAX_INSN_SIZE ); + +return MAX_INSN_SIZE - not_copied; +}
Signature · 1 line
2.17.1
PATCH v3 8/75 x86/umip: Factor out instruction decoding
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Factor out the code used to decode an instruction with the correct address and operand sizes to a helper function.
No functional changes.
--- arch/x86/include/asm/insn-eval.h | 2 ++ arch/x86/kernel/umip.c | 23 +--------------- arch/x86/lib/insn-eval.c | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 22 deletions(-)
arch/x86/include/asm/insn-eval.h +2 -0
@@ -21,5 +21,7 @@ unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs);int insn_fetch_from_user (struct pt_regs *regs,unsigned char buf[MAX_INSN_SIZE ]); +bool insn_decode (struct pt_regs *regs,struct insn *insn, +unsigned char buf[MAX_INSN_SIZE ],int buf_size);#endif /* _ASM_X86_INSN_EVAL_H */
arch/x86/kernel/umip.c +1 -22
@@ -324,7 +324,6 @@ bool fixup_umip_exception(struct pt_regs *regs)unsigned long *reg_addr;void __user *uaddr;struct insn insn; -int seg_defs;if (!regs)return false;
@@ -339,27 +338,7 @@ bool fixup_umip_exception(struct pt_regs *regs)if (!nr_copied)return false; -insn_init (&insn, buf, nr_copied,user_64bit_mode (regs)); - -/* -* Override the default operand and address sizes with what is specified -* in the code segment descriptor. The instruction decoder only sets -* the address size it to either 4 or 8 address bytes and does nothing -* for the operand bytes. This OK for most of the cases, but we could -* have special cases where, for instance, a 16-bit code segment -* descriptor is used. -* If there is an address override prefix, the instruction decoder -* correctly updates these values, even for 16-bit defaults. -*/ - seg_defs =insn_get_code_seg_params (regs); -if (seg_defs == -EINVAL ) -return false; - - insn.addr_bytes =INSN_CODE_SEG_ADDR_SZ (seg_defs); - insn.opnd_bytes =INSN_CODE_SEG_OPND_SZ (seg_defs); - -insn_get_length (&insn); -if (nr_copied < insn.length) +if (!insn_decode (regs, &insn, buf, nr_copied))return false; umip_inst =identify_insn (&insn);
arch/x86/lib/insn-eval.c +45 -0
48 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 08/75] x86/umip: Factor out instruction decoding
Quoted 6 lines · Joerg Roedel
arch/x86/lib/insn-eval.c +6
-0+/** +* insn_decode() - Decode an instruction +* @regs: Structure with register values as seen when entering kernel mode +* @insn: Structure to store decoded instruction +* @buf: Buffer containing the instruction bytes +* @buf_size: Number of instruction bytes available in buf
arch/x86/lib/insn-eval.c int insn_fetch_from_user(struct pt_regs *regs, unsigned char buf[MAX_INSN_SIZE]) · line 1426
Quoted 10 lines
arch/x86/lib/insn-eval.c +10
-0+* +* Decodes the instruction provided in buf and stores the decoding results in +* insn. Also determines the correct address and operand sizes. +* +* Returns: +* +* True if instruction was decoded, False otherwise. +*/ +bool insn_decode (struct pt_regs *regs,struct insn *insn, +unsigned char buf[MAX_INSN_SIZE ],int buf_size)
Right, let's have @insn be the first function argument in all those insn-handling functions.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 9/75 x86/insn: Add insn_get_modrm_reg_off()
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add a function to the instruction decoder which returns the pt_regs offset of the register specified in the reg field of the modrm byte.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -17,6 +17,7 @@void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs); +int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs);unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx);int insn_get_code_seg_params (struct pt_regs *regs); int insn_fetch_from_user(struct pt_regs *regs,
arch/x86/lib/insn-eval.c +23 -0
@@ -20,6 +20,7 @@enum reg_type {REG_TYPE_RM =0 , +REG_TYPE_REG ,REG_TYPE_INDEX ,REG_TYPE_BASE , };
@@ -441,6 +442,13 @@ static int get_reg_offset(struct insn *insn, struct pt_regs *regs, regno +=8 ;break ; +case REG_TYPE_REG : + regno =X86_MODRM_REG (insn->modrm.value); + +if (X86_REX_R (insn->rex_prefix.value)) + regno +=8 ; +break ; +case REG_TYPE_INDEX : regno =X86_SIB_INDEX (insn->sib.value);if (X86_REX_X (insn->rex_prefix.value))
@@ -809,6 +817,21 @@ int insn_get_modrm_rm_off(struct insn *insn, struct pt_regs *regs)return get_reg_offset (insn, regs,REG_TYPE_RM ); } +/** +* insn_get_modrm_reg_off() - Obtain register in reg part of the ModRM byte +* @insn: Instruction containing the ModRM byte +* @regs: Register values as seen when entering kernel mode +* +* Returns: +* +* The register indicated by the reg part of the ModRM byte. The +* register is obtained as an offset from the base of pt_regs. +*/ +int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs) +{ +return get_reg_offset (insn, regs,REG_TYPE_REG ); +} +/** * get_seg_base_limit() - obtain base address and limit of a segment * @insn: Instruction. Must be valid.
Signature · 1 line
2.17.1
PATCH v3 10/75 x86/insn: Add insn_rep_prefix() helper
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add a function to check whether an instruction has a REP prefix.
--- arch/x86/include/asm/insn-eval.h | 1 + arch/x86/lib/insn-eval.c | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+)
arch/x86/include/asm/insn-eval.h +1 -0
@@ -15,6 +15,7 @@#define INSN_CODE_SEG_OPND_SZ (params) (params & 0xf)#define INSN_CODE_SEG_PARAMS (oper_sz, addr_sz) (oper_sz | (addr_sz << 4)) +bool insn_has_rep_prefix (struct insn *insn);void __user *insn_get_addr_ref (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_rm_off (struct insn *insn,struct pt_regs *regs);int insn_get_modrm_reg_off (struct insn *insn,struct pt_regs *regs);
arch/x86/lib/insn-eval.c +24 -0
@@ -53,6 +53,30 @@ static bool is_string_insn(struct insn *insn) } } +/** +* insn_has_rep_prefix() - Determine if instruction has a REP prefix +* @insn: Instruction containing the prefix to inspect +* +* Returns: +* +* true if the instruction has a REP prefix, false if not. +*/ +bool insn_has_rep_prefix (struct insn *insn) +{ +int i; + +insn_get_prefixes (insn); + +for (i =0 ; i < insn->prefixes.nbytes; i++) { +insn_byte_t p = insn->prefixes.bytes[i]; + +if (p ==0xf2 || p ==0xf3 ) +return true; + } + +return false; +} +/** * get_seg_reg_override_idx() - obtain segment register override index * @insn: Valid instruction with segment override prefixes
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 10/75] x86/insn: Add insn_rep_prefix() helper
Quoted 1 line · Joerg Roedel
Subject: Re: [PATCH v3 10/75] x86/insn: Add
insn_rep_prefix()helper
s/insn_rep_prefix/insn_has_rep_prefix/
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 11/75 x86/boot/compressed/64: Disable red-zone usage
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The x86-64 ABI defines a red-zone on the stack:
The 128-byte area beyond the location pointed to by %rsp is considered to be reserved and shall not be modified by signal or interrupt handlers. Therefore, functions may use this area for temporary data that is not needed across function calls. In particular, leaf functions may use this area for their entire stack frame, rather than adjusting the stack pointer in the prologue and epilogue. This area is known as the red zone.
This is not compatible with exception handling, because the IRET frame written by the hardware at the stack pointer and the functions to handle the exception will overwrite the temporary variables of the interrupted function, causing undefined behavior. So disable red-zones for the pre-decompression boot code.
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/Makefile | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/boot/Makefile +1 -1
@@ -65,7 +65,7 @@ clean-files += cpustr.h # --------------------------------------------------------------------------- -KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP +KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP -mno-red-zone KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
arch/x86/boot/compressed/Makefile +1 -1
@@ -30,7 +30,7 @@ KBUILD_CFLAGS := -m$(BITS) -O2 KBUILD_CFLAGS += -fno-strict-aliasing $(call cc-option, -fPIE, -fPIC) KBUILD_CFLAGS += -DDISABLE_BRANCH_PROFILING cflags-$(CONFIG_X86_32) := -march=i386 -cflags-$(CONFIG_X86_64) := -mcmodel=small +cflags-$(CONFIG_X86_64) := -mcmodel=small -mno-red-zone KBUILD_CFLAGS += $(cflags-y) KBUILD_CFLAGS += -mno-mmx -mno-sse KBUILD_CFLAGS += $(call cc-option,-ffreestanding)
Signature · 1 line
2.17.1
PATCH v3 12/75 x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
When the pre-decompression code loads its first GDT in startup_64, it is still running on the CS value of the previous GDT. In the case of SEV-ES this is the EFI GDT.
To make exception handling work (especially IRET) the CPU needs to switch to a CS value in the current GDT, so jump to __KERNEL_CS after the first GDT is loaded.
--- arch/x86/boot/compressed/head_64.S | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/boot/compressed/head_64.S +8 -0
@@ -393,6 +393,14 @@ SYM_CODE_START(startup_64) addq %rax, 2(%rax) lgdt (%rax) + /* Reload CS so IRET returns to a CS actually in the GDT */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + /* * paging_prepare() sets up the trampoline and checks if we need to * enable 5-level paging.
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 12/75] x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded
Quoted 25 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
When the pre-decompression code loads its first GDT in
startup_64, it is still running on the CS value of the previous GDT. In the case of SEV-ES this is the EFI GDT.To make exception handling work (especially IRET) the CPU needs to switch to a CS value in the current GDT, so jump to
__KERNEL_CSafter the first GDT is loaded.Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/boot/compressed/head_64.S | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/boot/compressed/head_64.S +1
-0@@ -393,6 +393,14 @@ SYM_CODE_START(startup_64) addq %rax, 2(%rax) lgdt (%rax) + /* Reload CS so IRET returns to a CS actually in the GDT */
arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 406
Quoted 10 lines
arch/x86/boot/compressed/head_64.S +7
-0+ pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + /* * paging_prepare() sets up the trampoline and checks if we need to * enable 5-level paging.
So I'm thinking I should take this one even now on the grounds that it sanitizes CS to something known-good than what was there before and who knows what set it and loaded the kernel...?
And that is a good thing in itself.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 12/75] x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded
Quoted 18 lines · Borislav Petkov
arch/x86/boot/compressed/head_64.S SYM_CODE_START(startup_64) · line 406
Quoted 12 lines · Joerg Roedel
arch/x86/boot/compressed/head_64.S +8
-0+ /* Reload CS so IRET returns to a CS actually in the GDT */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + /* * paging_prepare() sets up the trampoline and checks if we need to * enable 5-level paging.So I'm thinking I should take this one even now on the grounds that it sanitizes CS to something known-good than what was there before and who knows what set it and loaded the kernel...?
Quoted 1 line
And that is a good thing in itself.
Right, sure. CS is basically undefined at this point and depends on what loaded the kernel (EFI, legacy boot code, some container runtime...), so setting it to something known is definitly good.
Regards,
Joerg
[tip: x86/boot] x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded
In reply to Joerg RoedelThe following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: 34bb49229f19399a5b45c323afb5749f31f7876c Gitweb: https://git.kernel.org/tip/34bb49229f19399a5b45c323afb5749f31f7876c Author: Joerg Roedel <[email protected]> AuthorDate: Tue, 28 Apr 2020 17:16:22 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Mon, 04 May 2020 19:53:08 +02:00
x86/boot/compressed/64: Switch to __KERNEL_CS after GDT is loaded
When the pre-decompression code loads its first GDT in startup_64(), it is still running on the CS value of the previous GDT. In the case of SEV-ES, this is the EFI GDT but it can be anything depending on what has loaded the kernel (boot loader, container runtime, etc.)
To make exception handling work (especially IRET) the CPU needs to switch to a CS value in the current GDT, so jump to __KERNEL_CS after the first GDT is loaded. This is prudent also as a general sanitization of CS to a known good value.
[ bp: Massage commit message. ]
--- arch/x86/boot/compressed/head_64.S | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/boot/compressed/head_64.S +8 -0
@@ -393,6 +393,14 @@ SYM_CODE_START(startup_64) addq %rax, 2(%rax) lgdt (%rax) + /* Reload CS so IRET returns to a CS actually in the GDT */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + /* * paging_prepare() sets up the trampoline and checks if we need to * enable 5-level paging.
PATCH v3 13/75 x86/boot/compressed/64: Add IDT Infrastructure
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add code needed to setup an IDT in the early pre-decompression boot-code. The IDT is loaded first in startup_64, which is after EfiExitBootServices() has been called, and later reloaded when the kernel image has been relocated to the end of the decompression area.
This allows to setup different IDT handlers before and after the relocation.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/head_64.S | 25 +++++++- arch/x86/boot/compressed/idt_64.c | 43 ++++++++++++++ arch/x86/boot/compressed/idt_handlers_64.S | 69 ++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 5 ++ arch/x86/include/asm/desc_defs.h | 3 + 6 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 arch/x86/boot/compressed/idt_64.c create mode 100644 arch/x86/boot/compressed/idt_handlers_64.S
arch/x86/boot/compressed/Makefile +1 -0
@@ -82,6 +82,7 @@ vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o ifdef CONFIG_X86_64 vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr_64.o + vmlinux-objs-y += $(obj)/idt_64.o $(obj)/idt_handlers_64.o vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o endif
arch/x86/boot/compressed/head_64.S +24 -1
@@ -33,6 +33,7 @@ #include <asm/processor-flags.h> #include <asm/asm-offsets.h> #include <asm/bootparam.h> +#include <asm/desc_defs.h> #include "pgtable.h" /*
@@ -401,6 +402,10 @@ SYM_CODE_START(startup_64) .Lon_kernel_cs: + pushq %rsi + call load_stage1_idt + popq %rsi + /* * paging_prepare() sets up the trampoline and checks if we need to * enable 5-level paging.
@@ -528,6 +533,13 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) shrq $3, %rcx rep stosq +/* + * Load stage2 IDT + */ + pushq %rsi + call load_stage2_idt + popq %rsi + /* * Do the extraction, and jump to the new kernel.. */
@@ -681,10 +693,21 @@ SYM_DATA_START_LOCAL(gdt) .quad 0x0000000000000000 /* TS continued */ SYM_DATA_END_LABEL(gdt, SYM_L_LOCAL, gdt_end) +SYM_DATA_START(boot_idt_desc) + .word boot_idt_end - boot_idt + .quad 0 +SYM_DATA_END(boot_idt_desc) + .balign 8 +SYM_DATA_START(boot_idt) + .rept BOOT_IDT_ENTRIES + .quad 0 + .quad 0 + .endr +SYM_DATA_END_LABEL(boot_idt, SYM_L_GLOBAL, boot_idt_end) + #ifdef CONFIG_EFI_STUB SYM_DATA(image_offset, .long 0) #endif - #ifdef CONFIG_EFI_MIXED SYM_DATA_LOCAL(efi32_boot_args, .long 0, 0, 0) SYM_DATA(efi_is64, .byte 1)
arch/x86/boot/compressed/idt_64.c new file +43 -0
43 lines not on this page · raw message
arch/x86/boot/compressed/idt_handlers_64.S new file +69 -0
69 lines not on this page · raw message
arch/x86/boot/compressed/misc.h +5 -0
@@ -23,6 +23,7 @@#include <asm/page.h> #include <asm/boot.h> #include <asm/bootparam.h> +#include <asm/desc_defs.h> #define BOOT_CTYPE_H #include <linux/acpi.h>
@@ -133,4 +134,8 @@ int count_immovable_mem_regions(void);static inline int count_immovable_mem_regions (void ) {return 0 ; }#endif +/* idt_64.c */ +extern gate_desc boot_idt[BOOT_IDT_ENTRIES ]; +extern struct desc_ptr boot_idt_desc; +#endif /* BOOT_COMPRESSED_MISC_H */
arch/x86/include/asm/desc_defs.h +3 -0
@@ -109,6 +109,9 @@ struct desc_ptr {#endif /* !__ASSEMBLY__ */ +/* Boot IDT definitions */ +#define BOOT_IDT_ENTRIES 32 +/* Access rights as returned by LAR */ #define AR_TYPE_RODATA (0 * (1 << 9))#define AR_TYPE_RWDATA (1 * (1 << 9))
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 13/75] x86/boot/compressed/64: Add IDT Infrastructure
Quoted 8 lines · Joerg Roedel
arch/x86/boot/compressed/idt_handlers_64.S new file +2
-0@@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/*
arch/x86/boot/compressed/idt_handlers_64.S line 12
Quoted 10 lines
arch/x86/boot/compressed/idt_handlers_64.S new file +10
-0+ * Early IDT handler entry points + * + * Copyright (C) 2019 SUSE + * + * Author: Joerg Roedel <[email protected]> + */ + +#include <asm/segment.h> + +#include "../../entry/calling.h"
Leftover from something? Commenting it out doesn't break the build here.
If needed, then we need to lift stuff in a separate header and share it or so. I want to include as less as possible crap from kernel proper and eventually untangle arch/x86/boot/ because include/linux/ definitions are a real pain.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 13/75] x86/boot/compressed/64: Add IDT Infrastructure
Quoted 13 lines · Borislav Petkov
Quoted 12 lines · Joerg Roedel
arch/x86/boot/compressed/idt_handlers_64.S new file +6
-0@@ -0,0 +1,69 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Early IDT handler entry points + * + * Copyright (C) 2019 SUSE + *
Quoted 8 lines
arch/x86/boot/compressed/idt_handlers_64.S line 12
Quoted 6 lines
arch/x86/boot/compressed/idt_handlers_64.S new file +6
-0+ * Author: Joerg Roedel <[email protected]> + */ + +#include <asm/segment.h> + +#include "../../entry/calling.h"Leftover from something? Commenting it out doesn't break the build here.
Yes, probably a leftover from when I tried to use the PT_REGS macros there. I'll remove it.
Thanks,
Joerg
Re: [PATCH v3 13/75] x86/boot/compressed/64: Add IDT Infrastructure
Quoted 8 lines · Joerg Roedel
Quoted 4 lines · Borislav Petkov
Quoted 1 line · Joerg Roedel
+#include "
../../entry/calling.h"Leftover from something? Commenting it out doesn't break the build here.
Yes, probably a leftover from when I tried to use the
PT_REGSmacros there. I'll remove it.
Turns out this include is still needed to get ORIG_RAX defined. I'll leave it in place.
Joerg
PATCH v3 14/75 x86/boot/compressed/64: Rename kaslr_64.c to ident_map_64.c
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The file contains only code related to identity mapped page-tables. Rename the file and compile it always in.
---
arch/x86/boot/compressed/Makefile | 2 +-
.../boot/compressed/{kaslr_64.c => ident_map_64.c} | 12 ++++++++++++
arch/x86/boot/compressed/kaslr.c | 9 ---------
arch/x86/boot/compressed/misc.h | 8 ++++++++
4 files changed, 21 insertions(+), 10 deletions(-)
rename arch/x86/boot/compressed/{kaslr_64.c => ident_map_64.c} (93%)arch/x86/boot/compressed/Makefile +1 -1
@@ -81,7 +81,7 @@ vmlinux-objs-y := $(obj)/vmlinux.lds $(obj)/kernel_info.o $(obj)/head_$(BITS).o vmlinux-objs-$(CONFIG_EARLY_PRINTK) += $(obj)/early_serial_console.o vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr.o ifdef CONFIG_X86_64 - vmlinux-objs-$(CONFIG_RANDOMIZE_BASE) += $(obj)/kaslr_64.o + vmlinux-objs-y += $(obj)/ident_map_64.o vmlinux-objs-y += $(obj)/idt_64.o $(obj)/idt_handlers_64.o vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o
arch/x86/boot/compressed/ident_map_64.c renamed from arch/x86/boot/compressed/kaslr_64.c +12 -0
@@ -29,6 +29,18 @@#define __PAGE_OFFSET __PAGE_OFFSET_BASE#include "../../mm/ident_map.c" +#ifdef CONFIG_X86_5LEVEL +unsigned int __pgtable_l5_enabled; +unsigned int pgdir_shift =39 ; +unsigned int ptrs_per_p4d =1 ; +#endif + +/* Used by PAGE_KERN* macros: */ +pteval_t __default_kernel_pte_mask__read_mostly = ~0 ; + +/* Used by pgtable.h asm code to force instruction serialization. */ +unsigned long __force_order; +/* Used to track our page table allocation area. */ struct alloc_pgt_data {unsigned char *pgt_buf;
arch/x86/boot/compressed/kaslr.c +0 -9
@@ -43,17 +43,8 @@#define STATIC #include <linux/decompress/mm.h> -#ifdef CONFIG_X86_5LEVEL -unsigned int __pgtable_l5_enabled; -unsigned int pgdir_shift__ro_after_init =39 ; -unsigned int ptrs_per_p4d__ro_after_init =1 ; -#endif -extern unsigned long get_cmd_line_ptr (void ); -/* Used by PAGE_KERN* macros: */ -pteval_t __default_kernel_pte_mask__read_mostly = ~0 ; -/* Simplified build-specific string for starting entropy. */ static const char build_str[] =UTS_RELEASE " (" LINUX_COMPILE_BY "@" LINUX_COMPILE_HOST ") (" LINUX_COMPILER ") " UTS_VERSION ;
arch/x86/boot/compressed/misc.h +8 -0
@@ -134,6 +134,14 @@ int count_immovable_mem_regions(void);static inline int count_immovable_mem_regions (void ) {return 0 ; }#endif +/* ident_map_64.c */ +#ifdef CONFIG_X86_5LEVEL +extern unsigned int __pgtable_l5_enabled, pgdir_shift, ptrs_per_p4d; +#endif + +/* Used by PAGE_KERN* macros: */ +extern pteval_t __default_kernel_pte_mask; +/* idt_64.c */ extern gate_desc boot_idt[BOOT_IDT_ENTRIES ];extern struct desc_ptr boot_idt_desc;
Signature · 1 line
2.17.1
PATCH v3 15/75 x86/boot/compressed/64: Add page-fault handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Install a page-fault handler to add an identity mapping to addresses not yet mapped. Also do some checking whether the error code is sane.
This makes non SEV-ES machines use the exception handling infrastructure in the pre-decompressions boot code too, making it less likely to break in the future.
--- arch/x86/boot/compressed/ident_map_64.c | 33 ++++++++++++++++++++++ arch/x86/boot/compressed/idt_64.c | 2 ++ arch/x86/boot/compressed/idt_handlers_64.S | 2 ++ arch/x86/boot/compressed/misc.h | 6 ++++ 4 files changed, 43 insertions(+)
arch/x86/boot/compressed/ident_map_64.c +33 -0
@@ -19,11 +19,13 @@/* No PAGE_TABLE_ISOLATION support needed either: */ #undef CONFIG_PAGE_TABLE_ISOLATION +#include "error.h" #include "misc.h" /* These actually do the work of building the kernel identity maps. */ #include <asm/init.h> #include <asm/pgtable.h> +#include <asm/trap_defs.h> /* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET#define __PAGE_OFFSET __PAGE_OFFSET_BASE
@@ -163,3 +165,34 @@ void finalize_identity_maps(void) {write_cr3 (top_level_pgt); } + +void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) +{ +unsigned long address =native_read_cr2 (); + +/* +* Check for unexpected error codes. Unexpected are: +* - Faults on present pages +* - User faults +* - Reserved bits set +*/ +if (error_code & (X86_PF_PROT |X86_PF_USER |X86_PF_RSVD )) { +/* Print some information for debugging */ +error_putstr ("Unexpected page-fault:" ); +error_putstr ("\nError Code: " ); +error_puthex (error_code); +error_putstr ("\nCR2: 0x" ); +error_puthex (address); +error_putstr ("\nRIP relative to _head: 0x" ); +error_puthex (regs->ip - (unsigned long )_head); +error_putstr ("\n" ); + +error ("Stopping.\n" ); + } + +/* +* Error code is sane - now identity map the 2M region around +* the faulting address. +*/ +add_identity_map (address &PMD_MASK ,PMD_SIZE ); +}
arch/x86/boot/compressed/idt_64.c +2 -0
@@ -39,5 +39,7 @@ void load_stage2_idt(void) { boot_idt_desc.address = (unsigned long )boot_idt; +set_idt_entry (X86_TRAP_PF , boot_page_fault); +load_boot_idt (&boot_idt_desc); }
arch/x86/boot/compressed/idt_handlers_64.S +2 -0
@@ -67,3 +67,5 @@ SYM_FUNC_END(\name) .text .code64 + +EXCEPTION_HANDLER boot_page_fault do_boot_page_fault error_code=1
arch/x86/boot/compressed/misc.h +6 -0
@@ -37,6 +37,9 @@#define memptr unsigned#endif +/* boot/compressed/vmlinux start and end markers */ +extern char _head[], _end[]; +/* misc.c */ extern memptr free_mem_ptr;extern memptr free_mem_end_ptr;
@@ -146,4 +149,7 @@ extern pteval_t __default_kernel_pte_mask;extern gate_desc boot_idt[BOOT_IDT_ENTRIES ];extern struct desc_ptr boot_idt_desc; +/* IDT Entry Points */ +void boot_page_fault (void ); +#endif /* BOOT_COMPRESSED_MISC_H */
Signature · 1 line
2.17.1
PATCH v3 16/75 x86/boot/compressed/64: Always switch to own page-table
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
When booted through startup_64 the kernel keeps running on the EFI page-table until the KASLR code sets up its own page-table. Without KASLR the pre-decompression boot code never switches off the EFI page-table. Change that by unconditionally switching to a kernel controlled page-table after relocation.
This makes sure we can make changes to the mapping when necessary, for example map pages unencrypted in SEV and SEV-ES guests.
Also remove the debug_putstr() calls in initialize_identity_maps() because the function now runs before console_init() is called.
--- arch/x86/boot/compressed/head_64.S | 3 +- arch/x86/boot/compressed/ident_map_64.c | 51 +++++++++++++++---------- arch/x86/boot/compressed/kaslr.c | 3 -- 3 files changed, 32 insertions(+), 25 deletions(-)
arch/x86/boot/compressed/head_64.S +2 -1
@@ -534,10 +534,11 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) rep stosq /* - * Load stage2 IDT + * Load stage2 IDT and switch to our own page-table */ pushq %rsi call load_stage2_idt + call initialize_identity_maps popq %rsi /*
arch/x86/boot/compressed/ident_map_64.c +30 -21
76 lines not on this page · raw message
arch/x86/boot/compressed/kaslr.c +0 -3
@@ -903,9 +903,6 @@ void choose_random_location(unsigned long input, boot_params->hdr.loadflags |=KASLR_FLAG ; -/* Prepare to add new identity pagetables on demand. */ -initialize_identity_maps (); -/* Record the various known unsafe memory ranges. */ mem_avoid_init (input, input_size, *output);
Signature · 1 line
2.17.1
PATCH v3 17/75 x86/boot/compressed/64: Don't pre-map memory in KASLR code
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
With the page-fault handler in place the identity mapping can be built on-demand. So remove the code which manually creates the mappings and unexport/remove the functions used for it.
--- arch/x86/boot/compressed/ident_map_64.c | 16 ++-------------- arch/x86/boot/compressed/kaslr.c | 24 +----------------------- arch/x86/boot/compressed/misc.h | 10 ---------- 3 files changed, 3 insertions(+), 47 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +2 -14
@@ -89,11 +89,9 @@ phys_addr_t physical_mask = (1ULL << __PHYSICAL_MASK_SHIFT) - 1;static struct x86_mapping_info mapping_info;/* -* Adds the specified range to what will become the new identity mappings. -* Once all ranges have been added, the new mapping is activated by calling -* finalize_identity_maps() below. +* Adds the specified range to the identity mappings. */ -void add_identity_map (unsigned long start,unsigned long size) +static void add_identity_map (unsigned long start,unsigned long size) {unsigned long end = start + size;
@@ -165,16 +163,6 @@ void initialize_identity_maps(void)write_cr3 (top_level_pgt); } -/* -* This switches the page tables to the new level4 that has been built -* via calls to add_identity_map() above. If booted via startup_32(), -* this is effectively a no-op. -*/ -void finalize_identity_maps (void ) -{ -write_cr3 (top_level_pgt); -} -void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) {unsigned long address =native_read_cr2 ();
arch/x86/boot/compressed/kaslr.c +1 -23
@@ -399,8 +399,6 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,*/ mem_avoid[MEM_AVOID_ZO_RANGE ].start = input; mem_avoid[MEM_AVOID_ZO_RANGE ].size = (output + init_size) - input; -add_identity_map (mem_avoid[MEM_AVOID_ZO_RANGE ].start, - mem_avoid[MEM_AVOID_ZO_RANGE ].size);/* Avoid initrd. */ initrd_start = (u64 )boot_params->ext_ramdisk_image <<32 ;
@@ -420,14 +418,10 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size, ; mem_avoid[MEM_AVOID_CMDLINE ].start = cmd_line; mem_avoid[MEM_AVOID_CMDLINE ].size = cmd_line_size; -add_identity_map (mem_avoid[MEM_AVOID_CMDLINE ].start, - mem_avoid[MEM_AVOID_CMDLINE ].size);/* Avoid boot parameters. */ mem_avoid[MEM_AVOID_BOOTPARAMS ].start = (unsigned long )boot_params; mem_avoid[MEM_AVOID_BOOTPARAMS ].size =sizeof (*boot_params); -add_identity_map (mem_avoid[MEM_AVOID_BOOTPARAMS ].start, - mem_avoid[MEM_AVOID_BOOTPARAMS ].size);/* We don't need to set a mapping for setup_data. */
@@ -436,11 +430,6 @@ static void mem_avoid_init(unsigned long input, unsigned long input_size,/* Enumerate the immovable memory regions */ num_immovable_mem =count_immovable_mem_regions (); - -#ifdef CONFIG_X86_VERBOSE_BOOTUP -/* Make sure video RAM can be used. */ -add_identity_map (0 ,PMD_SIZE ); -#endif }/*
@@ -919,19 +908,8 @@ void choose_random_location(unsigned long input,warn ("Physical KASLR disabled: no suitable memory region!" ); }else {/* Update the new physical address location. */ -if (*output != random_addr) { -add_identity_map (random_addr, output_size); +if (*output != random_addr) *output = random_addr; - } - -/* -* This loads the identity mapping page table. -* This should only be done if a new physical address -* is found for the kernel, otherwise we should keep -* the old page table to make it be like the "nokaslr" -* case. -*/ -finalize_identity_maps (); }
arch/x86/boot/compressed/misc.h +0 -10
@@ -98,17 +98,7 @@ static inline void choose_random_location(unsigned long input,#endif #ifdef CONFIG_X86_64 -void initialize_identity_maps (void ); -void add_identity_map (unsigned long start,unsigned long size); -void finalize_identity_maps (void );extern unsigned char _pgtable[]; -#else -static inline void initialize_identity_maps (void ) -{ } -static inline void add_identity_map (unsigned long start,unsigned long size) -{ } -static inline void finalize_identity_maps (void ) -{ }#endif #ifdef CONFIG_EARLY_PRINTK
Signature · 1 line
2.17.1
PATCH v3 18/75 x86/boot/compressed/64: Change add_identity_map() to take start and end
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Changing the function to take start and end as parameters instead of start and size simplifies the callers, which don't need to calculate the size if they already have start and end.
--- arch/x86/boot/compressed/ident_map_64.c | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +5 -10
@@ -91,10 +91,8 @@ static struct x86_mapping_info mapping_info;/* * Adds the specified range to the identity mappings. */ -static void add_identity_map (unsigned long start,unsigned long size) +static void add_identity_map (unsigned long start,unsigned long end) { -unsigned long end = start + size; -/* Align boundary to 2M. */ start =round_down (start,PMD_SIZE ); end =round_up (end,PMD_SIZE );
@@ -109,8 +107,6 @@ static void add_identity_map(unsigned long start, unsigned long size)/* Locates and clears a region for a new top level page table. */ void initialize_identity_maps (void ) { -unsigned long start, size; -/* If running as an SEV guest, the encryption mask is required. */ set_sev_encryption_mask ();
@@ -157,15 +153,14 @@ void initialize_identity_maps(void)* New page-table is set up - map the kernel image and load it * into cr3. */ - start = (unsigned long )_head; - size = _end - _head; -add_identity_map (start, size); +add_identity_map ((unsigned long )_head, (unsigned long )_end);write_cr3 (top_level_pgt); }void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) { -unsigned long address =native_read_cr2 (); +unsigned long address =native_read_cr2 () &PMD_MASK ; +unsigned long end = address +PMD_SIZE ;/* * Check for unexpected error codes. Unexpected are:
@@ -191,5 +186,5 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)* Error code is sane - now identity map the 2M region around * the faulting address. */ -add_identity_map (address &PMD_MASK ,PMD_SIZE ); +add_identity_map (address, end); }
Signature · 1 line
2.17.1
PATCH v3 19/75 x86/boot/compressed/64: Add stage1 #VC handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add the first handler for #VC exceptions. At stage 1 there is no GHCB yet becaue we might still be on the EFI page table and thus can't map memory unencrypted.
The stage 1 handler is limited to the MSR based protocol to talk to the hypervisor and can only support CPUID exit-codes, but that is enough to get to stage 2.
--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/idt_64.c | 4 ++ arch/x86/boot/compressed/idt_handlers_64.S | 4 ++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/boot/compressed/sev-es.c | 45 +++++++++++++++ arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/sev-es.h | 37 ++++++++++++ arch/x86/include/asm/trap_defs.h | 1 + arch/x86/kernel/sev-es-shared.c | 65 ++++++++++++++++++++++ 9 files changed, 159 insertions(+) create mode 100644 arch/x86/boot/compressed/sev-es.c create mode 100644 arch/x86/include/asm/sev-es.h create mode 100644 arch/x86/kernel/sev-es-shared.c
arch/x86/boot/compressed/Makefile +1 -0
@@ -85,6 +85,7 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/idt_64.o $(obj)/idt_handlers_64.o vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o + vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/sev-es.o endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o
arch/x86/boot/compressed/idt_64.c +4 -0
@@ -31,6 +31,10 @@ void load_stage1_idt(void) { boot_idt_desc.address = (unsigned long )boot_idt; +#ifdef CONFIG_AMD_MEM_ENCRYPT +set_idt_entry (X86_TRAP_VC , boot_stage1_vc); +#endif +load_boot_idt (&boot_idt_desc); }
arch/x86/boot/compressed/idt_handlers_64.S +4 -0
@@ -69,3 +69,7 @@ SYM_FUNC_END(\name) .code64 EXCEPTION_HANDLER boot_page_fault do_boot_page_fault error_code=1 + +#ifdef CONFIG_AMD_MEM_ENCRYPT +EXCEPTION_HANDLER boot_stage1_vc do_vc_no_ghcb error_code=1 +#endif
arch/x86/boot/compressed/misc.h +1 -0
@@ -141,5 +141,6 @@ extern struct desc_ptr boot_idt_desc;/* IDT Entry Points */ void boot_page_fault (void ); +void boot_stage1_vc (void );#endif /* BOOT_COMPRESSED_MISC_H */
arch/x86/boot/compressed/sev-es.c new file +45 -0
45 lines not on this page · raw message
arch/x86/include/asm/msr-index.h +1 -0
@@ -441,6 +441,7 @@#define MSR_AMD64_IBSBRTARGET 0xc001103b#define MSR_AMD64_IBSOPDATA4 0xc001103d#define MSR_AMD64_IBS_REG_COUNT_MAX 8/* includes MSR_AMD64_IBSBRTARGET */ +#define MSR_AMD64_SEV_ES_GHCB 0xc0010130#define MSR_AMD64_SEV 0xc0010131#define MSR_AMD64_SEV_ENABLED_BIT 0#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT)
arch/x86/include/asm/sev-es.h new file +37 -0
@@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* AMD Encrypted Register State Support +* +* Author: Joerg Roedel <[email protected]> +*/ + +#ifndef __ASM_ENCRYPTED_STATE_H +#define __ASM_ENCRYPTED_STATE_H + +#include <linux/types.h> + +#define GHCB_SEV_CPUID_REQ 0x004UL +#define GHCB_CPUID_REQ_EAX 0 +#define GHCB_CPUID_REQ_EBX 1 +#define GHCB_CPUID_REQ_ECX 2 +#define GHCB_CPUID_REQ_EDX 3 +#define GHCB_CPUID_REQ (fn, reg) (GHCB_SEV_CPUID_REQ | \ + (((unsigned long)reg & 3) << 30) | \ + (((unsigned long)fn) << 32)) + +#define GHCB_SEV_CPUID_RESP 0x005UL +#define GHCB_SEV_TERMINATE 0x100UL + +#define GHCB_SEV_GHCB_RESP_CODE (v) ((v) & 0xfff) +#define VMGEXIT () { asm volatile("rep; vmmcall\n\r"); } + +void __init do_vc_no_ghcb (struct pt_regs *regs,unsigned long exit_code); + +static inline u64 lower_bits (u64 val,unsigned int bits) +{ +u64 mask = (1ULL << bits) -1 ; + +return (val & mask); +} + +#endif
arch/x86/include/asm/trap_defs.h +1 -0
@@ -24,6 +24,7 @@ enum {X86_TRAP_AC ,/* 17, Alignment Check */ X86_TRAP_MC ,/* 18, Machine Check */ X86_TRAP_XF ,/* 19, SIMD Floating-Point Exception */ +X86_TRAP_VC =29 ,/* 29, VMM Communication Exception */ X86_TRAP_IRET = 32, /* 32, IRET Exception */ };
arch/x86/kernel/sev-es-shared.c new file +65 -0
65 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 19/75] x86/boot/compressed/64: Add stage1 #VC handler
Quoted 4 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
Add the first handler for #VC exceptions. At stage 1 there is no GHCB yet becaue we might still be on the EFI page table and thus can't map
"... because the kernel might still be running on the EFI page table... "
Quoted 39 lines
memory unencrypted.
The stage 1 handler is limited to the MSR based protocol to talk to the hypervisor and can only support CPUID exit-codes, but that is enough to get to stage 2.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/idt_64.c | 4 ++ arch/x86/boot/compressed/idt_handlers_64.S | 4 ++ arch/x86/boot/compressed/misc.h | 1 + arch/x86/boot/compressed/sev-es.c | 45 +++++++++++++++ arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/sev-es.h | 37 ++++++++++++ arch/x86/include/asm/trap_defs.h | 1 + arch/x86/kernel/sev-es-shared.c | 65 ++++++++++++++++++++++ 9 files changed, 159 insertions(+) create mode 100644 arch/x86/boot/compressed/sev-es.c create mode 100644 arch/x86/include/asm/sev-es.h create mode 100644 arch/x86/kernel/sev-es-shared.carch/x86/boot/compressed/Makefile +1
-0@@ -85,6 +85,7 @@ ifdef CONFIG_X86_64 vmlinux-objs-y += $(obj)/idt_64.o $(obj)/idt_handlers_64.o vmlinux-objs-y += $(obj)/mem_encrypt.o vmlinux-objs-y += $(obj)/pgtable_64.o + vmlinux-objs-$(CONFIG_AMD_MEM_ENCRYPT) += $(obj)/sev-es.o endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.oarch/x86/boot/compressed/idt_64.c +0
-0
arch/x86/boot/compressed/idt_64.c void load_stage1_idt(void) · line 36
Quoted 6 lines
arch/x86/boot/compressed/idt_64.c +3
-0@@ -31,6 +31,10 @@ void load_stage1_idt(void) { boot_idt_desc.address = (unsigned long )boot_idt; +#ifdef CONFIG_AMD_MEM_ENCRYPT +set_idt_entry (X86_TRAP_VC , boot_stage1_vc); +#endif
if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT))
seems to work too and drops the ifdeffery ugliness.
...
Quoted 26 lines
arch/x86/boot/compressed/idt_64.c +26
-0+void __init do_vc_no_ghcb (struct pt_regs *regs,unsigned long exit_code) +{ +unsigned int fn =lower_bits (regs->ax,32 ); +unsigned long val; + +/* Only CPUID is supported via MSR protocol */ +if (exit_code !=SVM_EXIT_CPUID ) + goto fail; + +sev_es_wr_ghcb_msr (GHCB_CPUID_REQ (fn,GHCB_CPUID_REQ_EAX )); +VMGEXIT (); + val =sev_es_rd_ghcb_msr (); +if (GHCB_SEV_GHCB_RESP_CODE (val) !=GHCB_SEV_CPUID_RESP ) + goto fail; + regs->ax = val >>32 ; + +sev_es_wr_ghcb_msr (GHCB_CPUID_REQ (fn,GHCB_CPUID_REQ_EBX )); +VMGEXIT (); + val =sev_es_rd_ghcb_msr (); +if (GHCB_SEV_GHCB_RESP_CODE (val) !=GHCB_SEV_CPUID_RESP ) + goto fail; + regs->bx = val >>32 ; + +sev_es_wr_ghcb_msr (GHCB_CPUID_REQ (fn,GHCB_CPUID_REQ_ECX )); +VMGEXIT (); + val =sev_es_rd_ghcb_msr ();
arch/x86/boot/compressed/idt_64.c void load_stage1_idt(void)
Quoted 10 lines
arch/x86/boot/compressed/idt_64.c +10
-0+if (GHCB_SEV_GHCB_RESP_CODE (val) !=GHCB_SEV_CPUID_RESP ) + goto fail; + regs->cx = val >>32 ; + +sev_es_wr_ghcb_msr (GHCB_CPUID_REQ (fn,GHCB_CPUID_REQ_EDX )); +VMGEXIT (); + val =sev_es_rd_ghcb_msr (); +if (GHCB_SEV_GHCB_RESP_CODE (val) !=GHCB_SEV_CPUID_RESP ) + goto fail; + regs->dx = val >>32 ;
This could use a comment:
/* Skip over the CPUID two-byte opcode */
or so.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 20/75 x86/boot/compressed/64: Call set_sev_encryption_mask earlier
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Call set_sev_encryption_mask() while still on the stage 1 #VC-handler, because the stage 2 handler needs our own page-tables to be set up, to which calling set_sev_encryption_mask() is a prerequisite.
--- arch/x86/boot/compressed/head_64.S | 8 +++++++- arch/x86/boot/compressed/ident_map_64.c | 3 --- 2 files changed, 7 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/head_64.S +7 -1
@@ -534,9 +534,15 @@ SYM_FUNC_START_LOCAL_NOALIGN(.Lrelocated) rep stosq /* - * Load stage2 IDT and switch to our own page-table + * If running as an SEV guest, the encryption mask is required in the + * page-table setup code below. When the guest also has SEV-ES enabled + * set_sev_encryption_mask() will cause #VC exceptions, but the stage2 + * handler can't map its GHCB because the page-table is not set up yet. + * So set up the encryption mask here while still on the stage1 #VC + * handler. Then load stage2 IDT and switch to our own page-table. */ pushq %rsi + call set_sev_encryption_mask call load_stage2_idt call initialize_identity_maps popq %rsi
arch/x86/boot/compressed/ident_map_64.c +0 -3
@@ -107,9 +107,6 @@ static void add_identity_map(unsigned long start, unsigned long end)/* Locates and clears a region for a new top level page table. */ void initialize_identity_maps (void ) { -/* If running as an SEV guest, the encryption mask is required. */ -set_sev_encryption_mask (); -/* Exclude the encryption mask from __PHYSICAL_MASK */ physical_mask &= ~sme_me_mask;
Signature · 1 line
2.17.1
PATCH v3 21/75 x86/boot/compressed/64: Check return value of kernel_ident_mapping_init()
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The function can fail to create an identity mapping, check for that and bail out if it happens.
--- arch/x86/boot/compressed/ident_map_64.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +5 -2
@@ -93,6 +93,8 @@ static struct x86_mapping_info mapping_info;*/ static void add_identity_map (unsigned long start,unsigned long end) { +int ret; +/* Align boundary to 2M. */ start =round_down (start,PMD_SIZE ); end =round_up (end,PMD_SIZE );
@@ -100,8 +102,9 @@ static void add_identity_map(unsigned long start, unsigned long end)return ;/* Build the mapping. */ -kernel_ident_mapping_init (&mapping_info, (pgd_t *)top_level_pgt, - start, end); + ret =kernel_ident_mapping_init (&mapping_info, (pgd_t *)top_level_pgt, start, end); +if (ret) +error ("Error: kernel_ident_mapping_init() failed\n" ); }/* Locates and clears a region for a new top level page table. */
Signature · 1 line
2.17.1
PATCH v3 22/75 x86/boot/compressed/64: Add set_page_en/decrypted() helpers
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The functions are needed to map the GHCB for SEV-ES guests. The GHCB is used for communication with the hypervisor, so its content must not be encrypted. After the GHCB is not needed anymore it must be mapped encrypted again so that the running kernel image can safely re-use the memory.
--- arch/x86/boot/compressed/ident_map_64.c | 134 ++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 2 + 2 files changed, 136 insertions(+)
arch/x86/boot/compressed/ident_map_64.c +134 -0
@@ -26,6 +26,7 @@#include <asm/init.h> #include <asm/pgtable.h> #include <asm/trap_defs.h> +#include <asm/cmpxchg.h> /* Use the static base for this part of the boot process */ #undef __PAGE_OFFSET#define __PAGE_OFFSET __PAGE_OFFSET_BASE
@@ -157,6 +158,139 @@ void initialize_identity_maps(void)write_cr3 (top_level_pgt); } +static pte_t *split_large_pmd (struct x86_mapping_info *info, +pmd_t *pmdp,unsigned long __address) +{ +unsigned long page_flags; +unsigned long address; +pte_t *pte; +pmd_t pmd; +int i; + + pte = (pte_t *)info->alloc_pgt_page (info->context); +if (!pte) +return NULL ; + + address = __address &PMD_MASK ; +/* No large page - clear PSE flag */ + page_flags = info->page_flag & ~_PAGE_PSE; + +/* Populate the PTEs */ +for (i =0 ; i <PTRS_PER_PMD ; i++) { +set_pte (&pte[i],__pte (address | page_flags)); + address +=PAGE_SIZE ; + } + +/* +* Ideally we need to clear the large PMD first and do a TLB +* flush before we write the new PMD. But the 2M range of the +* PMD might contain the code we execute and/or the stack +* we are on, so we can't do that. But that should be safe here +* because we are going from large to small mappings and we are +* also the only user of the page-table, so there is no chance +* of a TLB multihit. +*/ + pmd =__pmd ((unsigned long )pte | info->kernpg_flag); +set_pmd (pmdp, pmd); +/* Flush TLB to establish the new PMD */ +write_cr3 (top_level_pgt); + +return pte +pte_index (__address); +} + +static void clflush_page (unsigned long address) +{ +unsigned int flush_size; +char *cl, *start, *end; + +/* +* Hardcode cl-size to 64 - CPUID can't be used here because that might +* cause another #VC exception and the GHCB is not ready to use yet. +*/ + flush_size =64 ; + start = (char *)(address &PAGE_MASK ); + end = start +PAGE_SIZE ; + +/* +* First make sure there are no pending writes on the cache-lines to +* flush. +*/ + asmvolatile ("mfence" : : :"memory" ); + +for (cl = start; cl != end; cl += flush_size) +clflush (cl); +} + +static int set_clr_page_flags (struct x86_mapping_info *info, +unsigned long address, +pteval_t set,pteval_t clr) +{ +unsigned long scratch, *target; +pgd_t *pgdp = (pgd_t *)top_level_pgt; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep, pte; + +/* +* First make sure there is a PMD mapping for 'address'. +* It should already exist, but keep things generic. +* +* To map the page just read from it and fault it in if there is no +* mapping yet. add_identity_map() can't be called here because that +* would unconditionally map the address on PMD level, destroying any +* PTE-level mappings that might already exist. Also do something +* useless with 'scratch' so the access won't be optimized away. +*/ + target = (unsigned long *)address; + scratch = *target; +arch_cmpxchg (target, scratch, scratch); + +/* +* The page is mapped at least with PMD size - so skip checks and walk +* directly to the PMD. +*/ + p4dp =p4d_offset (pgdp, address); + pudp =pud_offset (p4dp, address); + pmdp =pmd_offset (pudp, address); + +if (pmd_large (*pmdp)) + ptep =split_large_pmd (info, pmdp, address); +else + ptep =pte_offset_kernel (pmdp, address); + +if (!ptep) +return -ENOMEM ; + +/* +* Changing encryption attributes of a page requires to flush it from +* the caches. +*/ +if ((set | clr) & _PAGE_ENC) +clflush_page (address); + +/* Update PTE */ + pte = *ptep; + pte =pte_set_flags (pte, set); + pte =pte_clear_flags (pte, clr); +set_pte (ptep, pte); + +/* Flush TLB after changing encryption attribute */ +write_cr3 (top_level_pgt); + +return 0 ; +} + +int set_page_decrypted (unsigned long address) +{ +return set_clr_page_flags (&mapping_info, address,0 , _PAGE_ENC); +} + +int set_page_encrypted (unsigned long address) +{ +return set_clr_page_flags (&mapping_info, address, _PAGE_ENC,0 ); +} +void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) {unsigned long address =native_read_cr2 () &PMD_MASK ;
arch/x86/boot/compressed/misc.h +2 -0
@@ -98,6 +98,8 @@ static inline void choose_random_location(unsigned long input,#endif #ifdef CONFIG_X86_64 +extern int set_page_decrypted (unsigned long address); +extern int set_page_encrypted (unsigned long address);extern unsigned char _pgtable[];#endif
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 22/75] x86/boot/compressed/64: Add set_page_en/decrypted() helpers
Quoted 13 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
The functions are needed to map the GHCB for SEV-ES guests. The GHCB is used for communication with the hypervisor, so its content must not be encrypted. After the GHCB is not needed anymore it must be mapped encrypted again so that the running kernel image can safely re-use the memory.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/boot/compressed/ident_map_64.c | 134 ++++++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 2 + 2 files changed, 136 insertions(+)
...
Quoted 11 lines
arch/x86/boot/compressed/ident_map_64.c +11
-0+ +static int set_clr_page_flags (struct x86_mapping_info *info, +unsigned long address, +pteval_t set,pteval_t clr) +{ +unsigned long scratch, *target; +pgd_t *pgdp = (pgd_t *)top_level_pgt; +p4d_t *p4dp; +pud_t *pudp; +pmd_t *pmdp; +pte_t *ptep, pte;
arch/x86/boot/compressed/ident_map_64.c void initialize_identity_maps(void)
Quoted 10 lines
arch/x86/boot/compressed/ident_map_64.c +10
-0+ +/* +* First make sure there is a PMD mapping for 'address'. +* It should already exist, but keep things generic. +* +* To map the page just read from it and fault it in if there is no +* mapping yet. add_identity_map() can't be called here because that +* would unconditionally map the address on PMD level, destroying any +* PTE-level mappings that might already exist. Also do something +* useless
You mean something like this?
asm volatile("mov %[address], %%r9"
:: [address] "g" (*(unsigned long *)address)
: "r9", "memory");The "memory" clobber should prevent gcc from optimizing that thing away and r9 is callee-clobbered.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 23/75 x86/boot/compressed/64: Setup GHCB Based VC Exception handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Install an exception handler for #VC exception that uses a GHCB. Also add the infrastructure for handling different exit-codes by decoding the instruction that caused the exception and error handling.
--- arch/x86/Kconfig | 1 + arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/idt_64.c | 4 + arch/x86/boot/compressed/idt_handlers_64.S | 3 +- arch/x86/boot/compressed/misc.c | 7 + arch/x86/boot/compressed/misc.h | 7 + arch/x86/boot/compressed/sev-es.c | 110 +++++++++++++++ arch/x86/include/asm/sev-es.h | 39 ++++++ arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kernel/sev-es-shared.c | 154 +++++++++++++++++++++ 10 files changed, 328 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
@@ -1523,6 +1523,7 @@ config AMD_MEM_ENCRYPT select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT select ARCH_HAS_FORCE_DMA_UNENCRYPTED + select INSTRUCTION_DECODER ---help--- Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory
arch/x86/boot/compressed/Makefile +3 -0
@@ -41,6 +41,9 @@ KBUILD_CFLAGS += -Wno-pointer-sign KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +# sev-es.c inludes generated $(objtree)/arch/x86/lib/inat-tables.c +CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/ + KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n UBSAN_SANITIZE :=n
arch/x86/boot/compressed/idt_64.c +4 -0
@@ -45,5 +45,9 @@ void load_stage2_idt(void)set_idt_entry (X86_TRAP_PF , boot_page_fault); +#ifdef CONFIG_AMD_MEM_ENCRYPT +set_idt_entry (X86_TRAP_VC , boot_stage2_vc); +#endif +load_boot_idt (&boot_idt_desc); }
arch/x86/boot/compressed/idt_handlers_64.S +2 -1
@@ -71,5 +71,6 @@ SYM_FUNC_END(\name) EXCEPTION_HANDLER boot_page_fault do_boot_page_fault error_code=1 #ifdef CONFIG_AMD_MEM_ENCRYPT -EXCEPTION_HANDLER boot_stage1_vc do_vc_no_ghcb error_code=1 +EXCEPTION_HANDLER boot_stage1_vc do_vc_no_ghcb error_code=1 +EXCEPTION_HANDLER boot_stage2_vc do_boot_stage2_vc error_code=1 #endif
arch/x86/boot/compressed/misc.c +7 -0
@@ -441,6 +441,13 @@ asmlinkage __visible void *extract_kernel(void *rmode, memptr heap,parse_elf (output);handle_relocations (output, output_len, virt_addr);debug_putstr ("done.\nBooting the kernel.\n" ); + +/* +* Flush GHCB from cache and map it encrypted again when running as +* SEV-ES guest. +*/ +sev_es_shutdown_ghcb (); +return output; }
arch/x86/boot/compressed/misc.h +7 -0
@@ -115,6 +115,12 @@ static inline void console_init(void)void set_sev_encryption_mask (void ); +#ifdef CONFIG_AMD_MEM_ENCRYPT +void sev_es_shutdown_ghcb (void ); +#else +static inline void sev_es_shutdown_ghcb (void ) { } +#endif +/* acpi.c */ #ifdef CONFIG_ACPI acpi_physical_address get_rsdp_addr (void );
@@ -144,5 +150,6 @@ extern struct desc_ptr boot_idt_desc;/* IDT Entry Points */ void boot_page_fault (void );void boot_stage1_vc (void ); +void boot_stage2_vc (void );#endif /* BOOT_COMPRESSED_MISC_H */
arch/x86/boot/compressed/sev-es.c +110 -0
128 lines not on this page · raw message
arch/x86/include/asm/sev-es.h +39 -0
@@ -9,7 +9,14 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> +#include <asm/insn.h> +#define GHCB_SEV_INFO 0x001UL +#define GHCB_SEV_INFO_REQ 0x002UL +#define GHCB_INFO (v) ((v) & 0xfffUL) +#define GHCB_PROTO_MAX (v) (((v) >> 48) & 0xffffUL) +#define GHCB_PROTO_MIN (v) (((v) >> 32) & 0xffffUL) +#define GHCB_PROTO_OUR 0x0001UL#define GHCB_SEV_CPUID_REQ 0x004UL#define GHCB_CPUID_REQ_EAX 0#define GHCB_CPUID_REQ_EBX 1
@@ -19,12 +26,44 @@ (((unsigned long)reg & 3) << 30) | \ (((unsigned long)fn) << 32)) +#define GHCB_PROTOCOL_MAX 0x0001UL +#define GHCB_DEFAULT_USAGE 0x0000UL +#define GHCB_SEV_CPUID_RESP 0x005UL#define GHCB_SEV_TERMINATE 0x100UL +#define GHCB_SEV_TERMINATE_REASON (reason_set, reason_val) \ + (((((u64)reason_set) & 0x7) << 12) | \ + ((((u64)reason_val) & 0xff) << 16)) +#define GHCB_SEV_ES_REASON_GENERAL_REQUEST 0 +#define GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED 1#define GHCB_SEV_GHCB_RESP_CODE (v) ((v) & 0xfff)#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; +}; + +struct pt_regs ; + +/* ES instruction emulation context */ +struct es_em_ctxt { +struct pt_regs *regs; +struct insn insn; +struct es_fault_info fi; +}; +void __init do_vc_no_ghcb (struct pt_regs *regs,unsigned long exit_code);static inline u64 lower_bits (u64 val,unsigned int bits)
arch/x86/include/uapi/asm/svm.h +1 -0
@@ -29,6 +29,7 @@#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
arch/x86/kernel/sev-es-shared.c +154 -0
163 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 23/75] x86/boot/compressed/64: Setup GHCB Based VC Exception handler
Quoted 7 lines · Joerg Roedel
arch/x86/kernel/sev-es-shared.c +3
-0@@ -63,3 +175,45 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)while (true) asmvolatile ("hlt\n" ); } + +static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt, + void *src, char *buf,
arch/x86/kernel/sev-es-shared.c void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) · line 190
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+unsigned int data_size, + unsigned int count, + bool backwards) +{ + int i, b = backwards ? -1 : 1; +enum es_result ret =ES_OK ; + +for (i =0 ; i < count; i++) { +void *s = src + (i * data_size * b); +char *d = buf + (i * data_size);
From a previous review:
Where are we checking whether that count is not exceeding @buf or similar discrepancies?
Ditto below.
Quoted 32 lines
arch/x86/kernel/sev-es-shared.c +29
-0+ + ret =vc_read_mem (ctxt, s, d, data_size); +if (ret !=ES_OK ) +break ; + } + +return ret; +} + +static enum es_result vc_insn_string_write (struct es_em_ctxt *ctxt, +void *dst,char *buf, +unsigned int data_size, +unsigned int count, +bool backwards) +{ +int i, s = backwards ?-1 :1 ; +enum es_result ret =ES_OK ; + +for (i =0 ; i < count; i++) { +void *d = dst + (i * data_size * s); +char *b = buf + (i * data_size); + + ret =vc_write_mem (ctxt, d, b, data_size); +if (ret !=ES_OK ) +break ; + } + +return ret; +}Signature · 1 line
2.17.1
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 23/75] x86/boot/compressed/64: Setup GHCB Based VC Exception handler
Quoted 19 lines · Borislav Petkov
Quoted 17 lines · Joerg Roedel
arch/x86/kernel/sev-es-shared.c +13
-0@@ -63,3 +175,45 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)while (true) asmvolatile ("hlt\n" ); } + +static enum es_result vc_insn_string_read (struct es_em_ctxt *ctxt, +void *src,char *buf, +unsigned int data_size, +unsigned int count, +bool backwards) +{ +int i, b = backwards ?-1 :1 ; +enum es_result ret =ES_OK ; + +for (i =0 ; i < count; i++) { +void *s = src + (i * data_size * b); +char *d = buf + (i * data_size);
Quoted 4 lines
Quoted 1 line
From a previous review:
Where are we checking whether that count is not exceeding @buf or similar discrepancies?
These two functions are only called from vc_handle_ioio() and buf always points to ghcb->shared_buffer.
In general, the caller has to make sure that sizeof(*buf) is at least data_size*count, and handle_ioio() calculates count based on the size of *buf.
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 23/75] x86/boot/compressed/64: Setup GHCB Based VC Exception handler
In reply to Joerg RoedelQuoted 37 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
Install an exception handler for #VC exception that uses a GHCB. Also add the infrastructure for handling different exit-codes by decoding the instruction that caused the exception and error handling.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/Kconfig | 1 + arch/x86/boot/compressed/Makefile | 3 + arch/x86/boot/compressed/idt_64.c | 4 + arch/x86/boot/compressed/idt_handlers_64.S | 3 +- arch/x86/boot/compressed/misc.c | 7 + arch/x86/boot/compressed/misc.h | 7 + arch/x86/boot/compressed/sev-es.c | 110 +++++++++++++++ arch/x86/include/asm/sev-es.h | 39 ++++++ arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kernel/sev-es-shared.c | 154 +++++++++++++++++++++ 10 files changed, 328 insertions(+), 1 deletion(-)arch/x86/Kconfig +1
-0@@ -1523,6 +1523,7 @@ config AMD_MEM_ENCRYPT select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT select ARCH_HAS_FORCE_DMA_UNENCRYPTED + select INSTRUCTION_DECODER ---help--- Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memoryarch/x86/boot/compressed/Makefile +0
-0
arch/x86/boot/compressed/Makefile KBUILD_CFLAGS += -Wno-pointer-sign · line 44
Quoted 4 lines
arch/x86/boot/compressed/Makefile +1
-0@@ -41,6 +41,9 @@ KBUILD_CFLAGS += -Wno-pointer-sign KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +# sev-es.c inludes generated $(objtree)/arch/x86/lib/inat-tables.c
"includes"
arch/x86/boot/compressed/Makefile KBUILD_CFLAGS += -Wno-pointer-sign · line 45
Quoted 1 line
arch/x86/boot/compressed/Makefile +1
-0+CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/
Does it?
I see
#include "../../lib/inat.c"
#include "../../lib/insn.c"
only and with the above CFLAGS-line removed, it builds still.
Leftover from earlier?
Quoted 9 lines
arch/x86/boot/compressed/Makefile +1
-0+ KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ GCOV_PROFILE := n UBSAN_SANITIZE :=narch/x86/boot/compressed/idt_64.c +0
-0
arch/x86/boot/compressed/idt_64.c void load_stage2_idt(void) · line 50
Quoted 6 lines
arch/x86/boot/compressed/idt_64.c +3
-0@@ -45,5 +45,9 @@ void load_stage2_idt(void)set_idt_entry (X86_TRAP_PF , boot_page_fault); +#ifdef CONFIG_AMD_MEM_ENCRYPT +set_idt_entry (X86_TRAP_VC , boot_stage2_vc); +#endif
if IS_ENABLED()...
...
arch/x86/boot/compressed/idt_64.c void load_stage2_idt(void)
Quoted 11 lines
arch/x86/boot/compressed/idt_64.c +11
-0+static enum es_result vc_decode_insn (struct es_em_ctxt *ctxt) +{ +char buffer[MAX_INSN_SIZE ]; +enum es_result ret; + +memcpy (buffer, (unsigned char *)ctxt->regs->ip,MAX_INSN_SIZE ); + +insn_init (&ctxt->insn, buffer,MAX_INSN_SIZE ,1 ); +insn_get_length (&ctxt->insn); + + ret = ctxt->insn.immediate.got ?ES_OK :ES_DECODE_FAILED ;
Why are we checking whether the immediate? insn_get_length() sets insn->length unconditionally while insn_get_immediate() can error out and not set ->got... ?
Quoted 3 lines
arch/x86/boot/compressed/idt_64.c +3
-0+ +return ret; +}
...
Quoted 5 lines
arch/x86/boot/compressed/idt_64.c +5
-0+static bool sev_es_setup_ghcb (void ) +{ +if (!sev_es_negotiate_protocol ()) +sev_es_terminate (GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED ); +
arch/x86/boot/compressed/idt_64.c
Quoted 10 lines
arch/x86/boot/compressed/idt_64.c +10
-0+if (set_page_decrypted ((unsigned long )&boot_ghcb_page)) +return false; + +/* Page is now mapped decrypted, clear it */ +memset (&boot_ghcb_page,0 ,sizeof (boot_ghcb_page)); + + boot_ghcb = &boot_ghcb_page; + +/* Initialize lookup tables for the instruction decoder */ +inat_init_tables ();
Yeah, that call doesn't logically belong in this function AFAICT as this function should setup the GHCB only. You can move it to the caller.
Quoted 6 lines
arch/x86/boot/compressed/idt_64.c +6
-0+ +return true; +} + +void sev_es_shutdown_ghcb (void ) +{
arch/x86/boot/compressed/idt_64.c
Quoted 10 lines
arch/x86/boot/compressed/idt_64.c +10
-0+if (!boot_ghcb) +return ; + +/* +* GHCB Page must be flushed from the cache and mapped encrypted again. +* Otherwise the running kernel will see strange cache effects when +* trying to use that page. +*/ +if (set_page_encrypted ((unsigned long )&boot_ghcb_page)) +error ("Can't map GHCB page encrypted" );
Is that error() call enough?
Shouldn't we BUG_ON() here or mark that page Reserved or so, so that nothing uses it during the system lifetime and thus avoid the strange cache effects?
...
Quoted 9 lines
arch/x86/boot/compressed/idt_64.c +9
-0+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 ;
arch/x86/boot/compressed/idt_64.c
Quoted 10 lines
arch/x86/boot/compressed/idt_64.c +10
-0+ 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 ) {
^^^^^^^^^^^
(1UL << 32) - 1
I guess.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 23/75] x86/boot/compressed/64: Setup GHCB Based VC Exception handler
Quoted 8 lines · Borislav Petkov
Quoted 1 line
+#
sev-es.cinludes generated $(objtree)/arch/x86/lib/inat-tables.c"includes"
Quoted 1 line
+CFLAGS_sev-es.o += -I$(objtree)/arch/x86/lib/
Does it?
Quoted 8 lines
I see
#include "
../../lib/inat.c"
#include "../../lib/insn.c"only and with the above CFLAGS-line removed, it builds still.
Leftover from earlier?
No, this is a recent addition, otherwise this breaks out-of-tree builds (make O=/some/path ...) because inat-tables.c (included from inat.c) is generated during build and ends up in the $(objtree).
Quoted 8 lines
arch/x86/boot/compressed/sev-es.c static inline void sev_es_wr_ghcb_msr(u64 val) · line 57
Quoted 4 lines
arch/x86/boot/compressed/sev-es.c +4
-0+insn_init (&ctxt->insn, buffer,MAX_INSN_SIZE ,1 ); +insn_get_length (&ctxt->insn); + + ret = ctxt->insn.immediate.got ?ES_OK :ES_DECODE_FAILED ;Why are we checking whether the immediate?
insn_get_length()setsinsn->lengthunconditionally whileinsn_get_immediate()can error out and not set ->got... ?
Because the immediate is the last part of the instruction which is decoded (even if there is no immediate). The .got field is set when either the immediate was decoded successfully or, in case the instruction has no immediate, when the rest of the instruction was decoded successfully. So testing immediate.got is the indicator whether decoding was successful.
Quoted 17 lines
Quoted 4 lines
arch/x86/boot/compressed/sev-es.c +3
-0+ +return ret; +}...
Quoted 10 lines
arch/x86/boot/compressed/sev-es.c +10
-0+static bool sev_es_setup_ghcb (void ) +{ +if (!sev_es_negotiate_protocol ()) +sev_es_terminate (GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED ); + +if (set_page_decrypted ((unsigned long )&boot_ghcb_page)) +return false; + +/* Page is now mapped decrypted, clear it */ +memset (&boot_ghcb_page,0 ,sizeof (boot_ghcb_page));
Quoted 8 lines
arch/x86/boot/compressed/sev-es.c static inline void sev_es_wr_ghcb_msr(u64 val) · line 106
Quoted 5 lines
arch/x86/boot/compressed/sev-es.c +5
-0+ + boot_ghcb = &boot_ghcb_page; + +/* Initialize lookup tables for the instruction decoder */ +inat_init_tables ();Yeah, that call doesn't logically belong in this function AFAICT as this function should setup the GHCB only. You can move it to the caller.
Probably better rename the function, it also does the sev-es protocol version negotiation and all other related setup tasks. Maybe sev_es_setup() is a better name?
Quoted 8 lines
arch/x86/boot/compressed/sev-es.c static inline void sev_es_wr_ghcb_msr(u64 val) · line 122
Quoted 2 lines
arch/x86/boot/compressed/sev-es.c +2
-0+if (set_page_encrypted ((unsigned long )&boot_ghcb_page)) +error ("Can't map GHCB page encrypted" );Is that
error()call enough?Shouldn't we
BUG_ON()here or mark that page Reserved or so, so that nothing uses it during the system lifetime and thus avoid the strange cache effects?
If the above call fails its the end of the systems lifetime, because we can't continue to boot an SEV-ES guest when we have no GHCB.
BUG_ON() and friends are also not available in the pre-decompression boot stage.
Quoted 24 lines
...
Quoted 9 lines
arch/x86/kernel/sev-es-shared.c +9
-0+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 ;arch/x86/kernel/sev-es-shared.c line 99
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+ 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 ) {^^^^^^^^^^^
Quoted 3 lines
(1UL << 32) - 1
I guess.
Or lower_32_bits(), probably. I'll change it.
Thanks,
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 23/75] x86/boot/compressed/64: Setup GHCB Based VC Exception handler
Quoted 3 lines · Joerg Roedel
No, this is a recent addition, otherwise this breaks out-of-tree builds (make O=/some/path ...) because
inat-tables.c(included frominat.c) is generated during build and ends up in the $(objtree).
Please add a blurb about this above it otherwise no one would have a clue why it is there.
Quoted 6 lines
Because the immediate is the last part of the instruction which is decoded (even if there is no immediate). The .got field is set when either the immediate was decoded successfully or, in case the instruction has no immediate, when the rest of the instruction was decoded successfully. So testing immediate.got is the indicator whether decoding was successful.
Hm, whether the immediate was parsed correctly or it wasn't there and using that as the sign whether the instruction was decoded successfully sounds kinda arbitrary.
@Masami: shouldn't that insn_get_length() thing return a value to denote whether the decode was successful or not instead of testing arbitrary fields?
Pasting for you the code sequence again:
...
insn_get_length(&ctxt->insn);
ret = ctxt->insn.immediate.got ? ES_OK : ES_DECODE_FAILED;
...I wonder if one would be able to do instead:
if (insn_get_length(&ctxt->insn)) return ES_OK; return ES_DECODE_FAILED;
to have it straight-forward...
Quoted 3 lines
Probably better rename the function, it also does the sev-es protocol version negotiation and all other related setup tasks. Maybe
sev_es_setup()is a better name?
Sure.
Quoted 5 lines
If the above call fails its the end of the systems lifetime, because we can't continue to boot an SEV-ES guest when we have no GHCB.
BUG_ON()and friends are also not available in the pre-decompression boot stage.
Oh ok, error() does hlt the system.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 24/75 x86/boot/compressed/64: Unmap GHCB page before booting the kernel
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Force a page-fault on any further accesses to the GHCB page when they shouldn't happen anymore. This will catch the bugs where a #VC exception is raised when no one is expected anymore.
--- arch/x86/boot/compressed/ident_map_64.c | 23 +++++++++++++++++++---- arch/x86/boot/compressed/misc.h | 6 ++++++ arch/x86/boot/compressed/sev-es.c | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-)
arch/x86/boot/compressed/ident_map_64.c +19 -4
@@ -291,10 +291,21 @@ int set_page_encrypted(unsigned long address)return set_clr_page_flags (&mapping_info, address, _PAGE_ENC,0 ); } +int set_page_non_present (unsigned long address) +{ +return set_clr_page_flags (&mapping_info, address,0 , _PAGE_PRESENT); +} +void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) { -unsigned long address =native_read_cr2 () &PMD_MASK ; -unsigned long end = address +PMD_SIZE ; +unsigned long address =native_read_cr2 (); +unsigned long end; +bool ghcb_fault; + + ghcb_fault =sev_es_check_ghcb_fault (address); + + address &=PMD_MASK ; + end = address +PMD_SIZE ;/* * Check for unexpected error codes. Unexpected are:
@@ -302,9 +313,13 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)* - User faults * - Reserved bits set */ -if (error_code & (X86_PF_PROT |X86_PF_USER |X86_PF_RSVD )) { +if (ghcb_fault || + error_code & (X86_PF_PROT |X86_PF_USER |X86_PF_RSVD )) {/* Print some information for debugging */ -error_putstr ("Unexpected page-fault:" ); +if (ghcb_fault) +error_putstr ("Page-fault on GHCB page:" ); +else +error_putstr ("Unexpected page-fault:" );error_putstr ("\nError Code: " );error_puthex (error_code);error_putstr ("\nCR2: 0x" );
arch/x86/boot/compressed/misc.h +6 -0
@@ -100,6 +100,7 @@ static inline void choose_random_location(unsigned long input,#ifdef CONFIG_X86_64 extern int set_page_decrypted (unsigned long address);extern int set_page_encrypted (unsigned long address); +extern int set_page_non_present (unsigned long address);extern unsigned char _pgtable[];#endif
@@ -117,8 +118,13 @@ void set_sev_encryption_mask(void);#ifdef CONFIG_AMD_MEM_ENCRYPT void sev_es_shutdown_ghcb (void ); +extern bool sev_es_check_ghcb_fault (unsigned long address);#else static inline void sev_es_shutdown_ghcb (void ) { } +static inline bool sev_es_check_ghcb_fault (unsigned long address) +{ +return false; +}#endif /* acpi.c */
arch/x86/boot/compressed/sev-es.c +14 -0
@@ -120,6 +120,20 @@ void sev_es_shutdown_ghcb(void)*/ if (set_page_encrypted ((unsigned long )&boot_ghcb_page))error ("Can't map GHCB page encrypted" ); + +/* +* GHCB page is mapped encrypted again and flushed from the cache. +* Mark it non-present now to catch bugs when #VC exceptions trigger +* after this point. +*/ +if (set_page_non_present ((unsigned long )&boot_ghcb_page)) +error ("Can't unmap GHCB page" ); +} + +bool sev_es_check_ghcb_fault (unsigned long address) +{ +/* Check whether the fault was on the GHCB page */ +return ((address &PAGE_MASK ) == (unsigned long )&boot_ghcb_page); }void do_boot_stage2_vc (struct pt_regs *regs,unsigned long exit_code)
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 24/75] x86/boot/compressed/64: Unmap GHCB page before booting the kernel
Review void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) · line 322
Quoted 13 lines · Joerg Roedel
(quoted diff) +6
-2@@ -302,9 +313,13 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) * - User faults * - Reserved bits set */ - if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) { + if (ghcb_fault || + error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) { /* Print some information for debugging */ - error_putstr("Unexpected page-fault:"); + if (ghcb_fault) + error_putstr("Page-fault on GHCB page:"); + else + error_putstr("Unexpected page-fault:");
You could carve out the info dumping into a separate function to unclutter this if-statement (diff ontop):
arch/x86/boot/compressed/ident_map_64.c +21 -17
@@ -296,6 +296,22 @@ int set_page_non_present(unsigned long address)return set_clr_page_flags (&mapping_info, address,0 , _PAGE_PRESENT); } +static void do_pf_error (const char *msg,unsigned long error_code, +unsigned long address,unsigned long ip) +{ +error_putstr (msg); + +error_putstr ("\nError Code: " ); +error_puthex (error_code); +error_putstr ("\nCR2: 0x" ); +error_puthex (address); +error_putstr ("\nRIP relative to _head: 0x" ); +error_puthex (ip - (unsigned long )_head); +error_putstr ("\n" ); + +error ("Stopping.\n" ); +} +void do_boot_page_fault (struct pt_regs *regs,unsigned long error_code) {unsigned long address =native_read_cr2 ();
@@ -309,27 +325,15 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)/* * Check for unexpected error codes. Unexpected are: +* - Faults on the GHCB page due to unexpected #VCs * - Faults on present pages * - User faults * - Reserved bits set */ -if (ghcb_fault || - error_code & (X86_PF_PROT |X86_PF_USER |X86_PF_RSVD )) { -/* Print some information for debugging */ -if (ghcb_fault) -error_putstr ("Page-fault on GHCB page:" ); -else -error_putstr ("Unexpected page-fault:" ); -error_putstr ("\nError Code: " ); -error_puthex (error_code); -error_putstr ("\nCR2: 0x" ); -error_puthex (address); -error_putstr ("\nRIP relative to _head: 0x" ); -error_puthex (regs->ip - (unsigned long )_head); -error_putstr ("\n" ); - -error ("Stopping.\n" ); - } +if (ghcb_fault) +do_pf_error ("Page-fault on GHCB page:" , error_code, address, regs->ip); +else if (error_code & (X86_PF_PROT |X86_PF_USER |X86_PF_RSVD )) +do_pf_error ("Unexpected page-fault:" , error_code, address, regs->ip);/* * Error code is sane - now identity map the 2M region around
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 24/75] x86/boot/compressed/64: Unmap GHCB page before booting the kernel
Quoted 17 lines · Borislav Petkov
Review void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) · line 322
Quoted 13 lines · Joerg Roedel
(quoted diff) +6
-2@@ -302,9 +313,13 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code) * - User faults * - Reserved bits set */ - if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) { + if (ghcb_fault || + error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD)) { /* Print some information for debugging */ - error_putstr("Unexpected page-fault:"); + if (ghcb_fault) + error_putstr("Page-fault on GHCB page:"); + else + error_putstr("Unexpected page-fault:");You could carve out the info dumping into a separate function to unclutter this if-statement (diff ontop):
Yeah, I had this this way in v2, but changed it upon you request[1] :)
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 24/75] x86/boot/compressed/64: Unmap GHCB page before booting the kernel
Quoted 1 line · Joerg Roedel
Yeah, I had this this way in v2, but changed it upon you request[1] :)
Yeah, I was wondering why this isn't a separate function - you like them so much. :-P
Quoted 1 line
But that one didn't have the ghcb_fault check. Maybe it was being added later... :)
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 24/75] x86/boot/compressed/64: Unmap GHCB page before booting the kernel
Quoted 10 lines · Borislav Petkov
Quoted 1 line · Joerg Roedel
Yeah, I had this this way in v2, but changed it upon you request[1] :)
Yeah, I was wondering why this isn't a separate function - you like them so much. :-P
Quoted 1 line
But that one didn't have the
ghcb_faultcheck. Maybe it was being added later... :)
Yes, it was :)
I changed it back, first in the patch adding the page-fault handler and also updated this patch.
Joerg
PATCH v3 25/75 x86/sev-es: Add support for handling IOIO exceptions
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Add support for decoding and handling #VC exceptions for IOIO events.
--- arch/x86/boot/compressed/sev-es.c | 32 +++++ arch/x86/kernel/sev-es-shared.c | 202 ++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+)
arch/x86/boot/compressed/sev-es.c +32 -0
@@ -23,6 +23,35 @@struct ghcb boot_ghcb_page__aligned(PAGE_SIZE) ;struct ghcb *boot_ghcb; +/* +* Copy a version of this function here - insn-eval.c can't be used in +* pre-decompression code. +*/ +static bool insn_has_rep_prefix (struct insn *insn) +{ +int i; + +insn_get_prefixes (insn); + +for (i =0 ; i < insn->prefixes.nbytes; i++) { +insn_byte_t p = insn->prefixes.bytes[i]; + +if (p ==0xf2 || p ==0xf3 ) +return true; + } + +return false; +} + +/* +* Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and +* doesn't use segments. +*/ +static unsigned long insn_get_seg_base (struct pt_regs *regs,int seg_reg_idx) +{ +return 0UL ; +} +static inline u64 sev_es_rd_ghcb_msr (void ) {unsigned long low, high;
@@ -150,6 +179,9 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) goto finish;switch (exit_code) { +case SVM_EXIT_IOIO : + result =vc_handle_ioio (boot_ghcb, &ctxt); +break ;default : result =ES_UNSUPPORTED ;break ;
arch/x86/kernel/sev-es-shared.c +202 -0
205 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 4 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Add support for decoding and handling #VC exceptions for IOIO events.
Quoted 8 lines
Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: Adapted code to #VC handling framework ]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/boot/compressed/sev-es.c | 32 +++++ arch/x86/kernel/sev-es-shared.c | 202 ++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+)
Just nitpicks and some more commenting needed:
Quoted 11 lines
arch/x86/kernel/sev-es-shared.c +11
-0+static enum es_result vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +{ +struct pt_regs *regs = ctxt->regs; +u64 exit_info_1, exit_info_2; +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 ) {
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 347
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+int df = (regs->flags &X86_EFLAGS_DF ) ?-1 :1 ; +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 an re-schedule while the
^ and
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 357
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+* 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->cx :1 ; + exit_info_2 =min (op_count, ghcb_count); + exit_bytes = exit_info_2 * io_bytes; + + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES );
In general, I could use some commenting here to find my way around it:
/* Read bytes of OUTS into the shared buffer */
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 366
Quoted 9 lines
arch/x86/kernel/sev-es-shared.c +9
-0+ +if (!(exit_info_1 &IOIO_TYPE_IN )) { + ret =vc_insn_string_read (ctxt, + (void *)(es_base + regs->si), + 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.
*/
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 371
Quoted 5 lines
arch/x86/kernel/sev-es-shared.c +5
-0+ + 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);
Align arguments on the opening brace.
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 375
Quoted 4 lines
arch/x86/kernel/sev-es-shared.c +4
-0+if (ret !=ES_OK ) +return ret; + +/* Everything went well, write back results */
/* Read bytes from shared buffer into the guest's destination. */
Quoted 15 lines
arch/x86/kernel/sev-es-shared.c +15
-0+if (exit_info_1 &IOIO_TYPE_IN ) { + ret =vc_insn_string_write (ctxt, + (void *)(es_base + regs->di), + ghcb->shared_buffer, io_bytes, + exit_info_2, df); +if (ret) +return ret; + +if (df) + regs->di -= exit_bytes; +else + regs->di += exit_bytes; + }else { +if (df) + regs->si -= exit_bytes;
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 400
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+else + regs->si += exit_bytes; + } + +if (exit_info_1 &IOIO_REP ) + regs->cx -= exit_info_2; + + ret = regs->cx ?ES_RETRY :ES_OK ; + + }else {
/* IN/OUT into/from rAX */
Quoted 12 lines
arch/x86/kernel/sev-es-shared.c +11
-0+int bits = (exit_info_1 &0x70 ) >>1 ; +u64 rax =0 ; + +if (!(exit_info_1 &IOIO_TYPE_IN )) + rax =lower_bits (regs->ax, 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;
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 421
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+ +if (exit_info_1 &IOIO_TYPE_IN ) { +if (!ghcb_is_valid_rax (ghcb)) +return ES_VMM_ERROR ; + regs->ax =lower_bits (ghcb->save.rax, bits); + } + } + +return ret; +}
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
In reply to Joerg RoedelJust a reminder so that this doesn't get lost:
Quoted 2 lines · Joerg Roedel
arch/x86/kernel/sev-es-shared.c +2
-0+if (exit_info_1 &IOIO_TYPE_STR ) { +int df = (regs->flags &X86_EFLAGS_DF ) ?-1 :1 ;
...
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 363
Quoted 6 lines
arch/x86/kernel/sev-es-shared.c +6
-0+ +if (!(exit_info_1 &IOIO_TYPE_IN )) { + ret =vc_insn_string_read (ctxt, + (void *)(es_base + regs->si), + ghcb->shared_buffer, io_bytes, + exit_info_2, df);
^^^^
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 380
Quoted 7 lines
arch/x86/kernel/sev-es-shared.c +7
-0+ +/* Everything went well, write back results */ +if (exit_info_1 &IOIO_TYPE_IN ) { + ret =vc_insn_string_write (ctxt, + (void *)(es_base + regs->di), + ghcb->shared_buffer, io_bytes, + exit_info_2, df);
^^^^
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 7 lines · Borislav Petkov
Just a reminder so that this doesn't get lost:
Quoted 2 lines · Joerg Roedel
arch/x86/kernel/sev-es-shared.c +2
-0+if (exit_info_1 &IOIO_TYPE_STR ) { +int df = (regs->flags &X86_EFLAGS_DF ) ?-1 :1 ;...
Thanks, this is fixed now.
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
In reply to Joerg RoedelQuoted 18 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Add support for decoding and handling #VC exceptions for IOIO events.
Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: Adapted code to #VC handling framework ]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/boot/compressed/sev-es.c | 32 +++++ arch/x86/kernel/sev-es-shared.c | 202 ++++++++++++++++++++++++++++++ 2 files changed, 234 insertions(+)arch/x86/boot/compressed/sev-es.c +0
-0@@ -23,6 +23,35 @@
...
Quoted 12 lines
arch/x86/boot/compressed/sev-es.c +12
-0+static enum es_result vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +{ +struct pt_regs *regs = ctxt->regs; +u64 exit_info_1, exit_info_2; +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 ) { +int df = (regs->flags &X86_EFLAGS_DF ) ?-1 :1 ;
arch/x86/boot/compressed/sev-es.c
Quoted 10 lines
arch/x86/boot/compressed/sev-es.c +10
-0+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 an re-schedule while the +* instruction is emulated.
Doesn't this also suppress single-step #DBs?
arch/x86/boot/compressed/sev-es.c
Quoted 13 lines
arch/x86/boot/compressed/sev-es.c +13
-0+*/ + io_bytes = (exit_info_1 >>4 ) &0x7 ; + ghcb_count =sizeof (ghcb->shared_buffer) / io_bytes; + + op_count = (exit_info_1 &IOIO_REP ) ? regs->cx :1 ; + exit_info_2 =min (op_count, ghcb_count); + exit_bytes = exit_info_2 * io_bytes; + + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES ); + +if (!(exit_info_1 &IOIO_TYPE_IN )) { + ret = vc_insn_string_read(ctxt, + (void *)(es_base + regs->si),
SEV(-ES) is 64-bit only, why bother with the es_base charade?
arch/x86/boot/compressed/sev-es.c
Quoted 2 lines
arch/x86/boot/compressed/sev-es.c +2
-0+ ghcb->shared_buffer, io_bytes, + exit_info_2, df);
df handling is busted, it's aways non-zero. Same goes for the SI/DI adjustments below.
arch/x86/boot/compressed/sev-es.c
Quoted 10 lines
arch/x86/boot/compressed/sev-es.c +10
-0+if (ret) +return ret; + } + + 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;
Batching the memory accesses and I/O accesses separately is technically wrong, e.g. a #DB on a memory access will result in bogus data being shown in the debugger. In practice it seems unlikely to matter, but I'm curious as to why string I/O is supported in the first place. I didn't think there was that much string I/O in the kernel?
Quoted 51 lines
arch/x86/boot/compressed/sev-es.c +48
-0+ +/* Everything went well, write back results */ +if (exit_info_1 &IOIO_TYPE_IN ) { + ret =vc_insn_string_write (ctxt, + (void *)(es_base + regs->di), + ghcb->shared_buffer, io_bytes, + exit_info_2, df); +if (ret) +return ret; + +if (df) + regs->di -= exit_bytes; +else + regs->di += exit_bytes; + }else { +if (df) + regs->si -= exit_bytes; +else + regs->si += exit_bytes; + } + +if (exit_info_1 &IOIO_REP ) + regs->cx -= exit_info_2; + + ret = regs->cx ?ES_RETRY :ES_OK ; + + }else { +int bits = (exit_info_1 &0x70 ) >>1 ; +u64 rax =0 ; + +if (!(exit_info_1 &IOIO_TYPE_IN )) + rax =lower_bits (regs->ax, 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_is_valid_rax (ghcb)) +return ES_VMM_ERROR ; + regs->ax =lower_bits (ghcb->save.rax, bits); + } + } + +return ret; +}Signature · 1 line
2.17.1
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Hi Jean,
Quoted 8 lines · Sean Christopherson
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 348
Quoted 5 lines · Joerg Roedel
arch/x86/kernel/sev-es-shared.c +5
-0+/* +* 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 an re-schedule while the +* instruction is emulated. Doesn't this also suppress single-step #DBs?
Yes it does.
Quoted 16 lines
Quoted 4 lines
arch/x86/kernel/sev-es-shared.c +3
-0+*/ + io_bytes = (exit_info_1 >>4 ) &0x7 ; + ghcb_count =sizeof (ghcb->shared_buffer) / io_bytes;arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 361
Quoted 10 lines
arch/x86/kernel/sev-es-shared.c +10
-0+ + op_count = (exit_info_1 &IOIO_REP ) ? regs->cx :1 ; + exit_info_2 =min (op_count, ghcb_count); + exit_bytes = exit_info_2 * io_bytes; + + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES ); + +if (!(exit_info_1 &IOIO_TYPE_IN )) { + ret = vc_insn_string_read(ctxt, + (void *)(es_base + regs->si),SEV(-ES) is 64-bit only, why bother with the
es_basecharade?
User-space can also cause IOIO #VC exceptions, and user-space can be 32-bit legacy code with segments, so es_base has to be taken into account.
Quoted 6 lines
Review
Quoted 3 lines
(quoted diff) +2
-0+ ghcb->shared_buffer, io_bytes, + exit_info_2, df);df handling is busted, it's aways non-zero. Same goes for the SI/DI adjustments below.
Right, this is fixed now.
Quoted 5 lines
Batching the memory accesses and I/O accesses separately is technically wrong, e.g. a #DB on a memory access will result in bogus data being shown in the debugger. In practice it seems unlikely to matter, but I'm curious as to why string I/O is supported in the first place. I didn't think there was that much string I/O in the kernel?
True, #DBs won't be correct anymore. Currently debugging is not supported in SEV-ES guests anyway, but if it is supported the #DB exception would happen in the #VC handler and not on the original instruction.
Regards,
Joerg
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 16 lines · Joerg Roedel
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/sev-es-shared.c +7
-0+*/ + io_bytes = (exit_info_1 >>4 ) &0x7 ; + ghcb_count =sizeof (ghcb->shared_buffer) / io_bytes; + + op_count = (exit_info_1 &IOIO_REP ) ? regs->cx :1 ; + exit_info_2 =min (op_count, ghcb_count); + exit_bytes = exit_info_2 * io_bytes;Quoted 8 lines
arch/x86/kernel/sev-es-shared.c static enum es_result vc_insn_string_write(struct es_em_ctxt *ctxt, · line 361
Quoted 6 lines
arch/x86/kernel/sev-es-shared.c +6
-0+ + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES ); + +if (!(exit_info_1 &IOIO_TYPE_IN )) { + ret = vc_insn_string_read(ctxt, + (void *)(es_base + regs->si),SEV(-ES) is 64-bit only, why bother with the
es_basecharade?
Quoted 3 lines
User-space can also cause IOIO #VC exceptions, and user-space can be 32-bit legacy code with segments, so
es_basehas to be taken into account.
Is there actually a use case for this? Exposing port IO to userspace doesn't exactly improve security.
Given that i386 ABI requires EFLAGS.DF=0 upon function entry/exit, i.e. is the de facto default, the DF bug implies this hasn't been tested. And I don't see how this could possibly have worked for SEV given that the kernel unrolls string I/O because the VMM can't emulate string I/O. Presumably someone would have complained if they "needed" to run legacy crud. The host and guest obviously need major updates, so supporting e.g. DPDK with legacy virtio seems rather silly.
Quoted 14 lines
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ ghcb->shared_buffer, io_bytes, + exit_info_2, df);df handling is busted, it's aways non-zero. Same goes for the SI/DI adjustments below.
Right, this is fixed now.
Quoted 5 lines
Batching the memory accesses and I/O accesses separately is technically wrong, e.g. a #DB on a memory access will result in bogus data being shown in the debugger. In practice it seems unlikely to matter, but I'm curious as to why string I/O is supported in the first place. I didn't think there was that much string I/O in the kernel?
Quoted 4 lines
True, #DBs won't be correct anymore. Currently debugging is not supported in SEV-ES guests anyway, but if it is supported the #DB exception would happen in the #VC handler and not on the original instruction.
As in, the guest can't debug itself? Or the host can't debug the guest?
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Joerg Roedel
User-space can also cause IOIO #VC exceptions, and user-space can be 32-bit legacy code with segments, so
es_basehas to be taken into account.Is there actually a use case for this? Exposing port IO to userspace doesn't exactly improve security.
Might be true, but Linux supports it and this patch-set is not the place to challenge this feature.
Quoted 7 lines
Given that i386 ABI requires EFLAGS.DF=0 upon function entry/exit, i.e. is the de facto default, the DF bug implies this hasn't been tested. And I don't see how this could possibly have worked for SEV given that the kernel unrolls string I/O because the VMM can't emulate string I/O. Presumably someone would have complained if they "needed" to run legacy crud. The host and guest obviously need major updates, so supporting e.g. DPDK with legacy virtio seems rather silly.
With SEV-ES and this unrolling of string-io doesn't need to happen anymore. It is on the list of things to improve, but this patch-set is already pretty big.
Quoted 6 lines
Quoted 4 lines
True, #DBs won't be correct anymore. Currently debugging is not supported in SEV-ES guests anyway, but if it is supported the #DB exception would happen in the #VC handler and not on the original instruction.
As in, the guest can't debug itself? Or the host can't debug the guest?
Both, the guest can't debug itself because writes to DR7 never make it to the hardware DR7 register. And the host obviously can't debug the guest because it has no access to its unencrypted memory and register state.
Regards,
Joerg
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 11 lines · Joerg Roedel
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Joerg Roedel
User-space can also cause IOIO #VC exceptions, and user-space can be 32-bit legacy code with segments, so
es_basehas to be taken into account.Is there actually a use case for this? Exposing port IO to userspace doesn't exactly improve security.
Might be true, but Linux supports it and this patch-set is not the place to challenge this feature.
But SEV already broke it, no?
Re: [PATCH v3 25/75] x86/sev-es: Add support for handling IOIO exceptions
Quoted 1 line · Sean Christopherson
But SEV already broke it, no?
True, but with SEV-ES is can be fixed again, and all SEV hardware also supports SEV-ES.
Regards,
Joerg
PATCH v3 26/75 x86/fpu: Move xgetbv()/xsetbv() into separate header
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The xgetbv() function is needed in pre-decompression boot code, but asm/fpu/internal.h can't be included there directly. Doing so opens the door to include-hell due to various include-magic in boot/compressed/misc.h.
Avoid that by moving xgetbv()/xsetbv() to a separate header file and include this instead.
--- arch/x86/include/asm/fpu/internal.h | 29 +------------------------- arch/x86/include/asm/fpu/xcr.h | 32 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 arch/x86/include/asm/fpu/xcr.h
arch/x86/include/asm/fpu/internal.h +1 -28
@@ -19,6 +19,7 @@#include <asm/user.h> #include <asm/fpu/api.h> #include <asm/fpu/xstate.h> +#include <asm/fpu/xcr.h> #include <asm/cpufeature.h> #include <asm/trace/fpu.h>
@@ -614,32 +615,4 @@ static inline void switch_fpu_finish(struct fpu *new_fpu) }__write_pkru (pkru_val); } - -/* -* MXCSR and XCR definitions: -*/ - -extern unsigned int mxcsr_feature_mask; - -#define XCR_XFEATURE_ENABLED_MASK 0x00000000 - -static inline u64 xgetbv (u32 index) -{ -u32 eax, edx; - - asmvolatile (".byte 0x0f,0x01,0xd0" /* xgetbv */ - :"=a" (eax),"=d" (edx) - :"c" (index)); -return eax + ((u64 )edx <<32 ); -} - -static inline void xsetbv (u32 index,u64 value) -{ -u32 eax = value; -u32 edx = value >>32 ; - - asmvolatile (".byte 0x0f,0x01,0xd1" /* xsetbv */ - : :"a" (eax),"d" (edx),"c" (index)); -} -#endif /* _ASM_X86_FPU_INTERNAL_H */
arch/x86/include/asm/fpu/xcr.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_FPU_XCR_H +#define _ASM_X86_FPU_XCR_H + +/* +* MXCSR and XCR definitions: +*/ + +extern unsigned int mxcsr_feature_mask; + +#define XCR_XFEATURE_ENABLED_MASK 0x00000000 + +static inline u64 xgetbv (u32 index) +{ +u32 eax, edx; + + asmvolatile (".byte 0x0f,0x01,0xd0" /* xgetbv */ + :"=a" (eax),"=d" (edx) + :"c" (index)); +return eax + ((u64 )edx <<32 ); +} + +static inline void xsetbv (u32 index,u64 value) +{ +u32 eax = value; +u32 edx = value >>32 ; + + asmvolatile (".byte 0x0f,0x01,0xd1" /* xsetbv */ + : :"a" (eax),"d" (edx),"c" (index)); +} + +#endif /* _ASM_X86_FPU_XCR_H */
Signature · 1 line
2.17.1
PATCH v3 27/75 x86/sev-es: Add CPUID handling to #VC handler
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Handle #VC exceptions caused by CPUID instructions. These happen in early boot code when the KASLR code checks for RDTSC.
--- arch/x86/boot/compressed/sev-es.c | 4 ++++ arch/x86/kernel/sev-es-shared.c | 35 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+)
arch/x86/boot/compressed/sev-es.c +4 -0
@@ -15,6 +15,7 @@#include <asm/sev-es.h> #include <asm/trap_defs.h> #include <asm/msr-index.h> +#include <asm/fpu/xcr.h> #include <asm/ptrace.h> #include <asm/svm.h>
@@ -182,6 +183,9 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)case SVM_EXIT_IOIO : result =vc_handle_ioio (boot_ghcb, &ctxt);break ; +case SVM_EXIT_CPUID : + result =vc_handle_cpuid (boot_ghcb, &ctxt); +break ;default : result =ES_UNSUPPORTED ;break ;
arch/x86/kernel/sev-es-shared.c +35 -0
@@ -419,3 +419,38 @@ static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)return ret; } + +static enum es_result vc_handle_cpuid (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +struct pt_regs *regs = ctxt->regs; +u32 cr4 =native_read_cr4 (); +enum es_result ret; + +ghcb_set_rax (ghcb, regs->ax); +ghcb_set_rcx (ghcb, regs->cx); + +if (cr4 &X86_CR4_OSXSAVE ) +/* Safe to read xcr0 */ +ghcb_set_xcr0 (ghcb,xgetbv (XCR_XFEATURE_ENABLED_MASK )); +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_is_valid_rax (ghcb) && +ghcb_is_valid_rbx (ghcb) && +ghcb_is_valid_rcx (ghcb) && +ghcb_is_valid_rdx (ghcb))) +return ES_VMM_ERROR ; + + regs->ax = ghcb->save.rax; + regs->bx = ghcb->save.rbx; + regs->cx = ghcb->save.rcx; + regs->dx = ghcb->save.rdx; + +return ES_OK ; +}
Signature · 1 line
2.17.1
PATCH v3 28/75 x86/idt: Move IDT to data segment
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
With SEV-ES, exception handling is needed very early, even before the kernel has cleared the bss segment. In order to prevent clearing the currently used IDT, move the IDT to the data segment.
--- arch/x86/kernel/idt.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
arch/x86/kernel/idt.c +6 -2
@@ -165,8 +165,12 @@ static const __initconst struct idt_data dbg_idts[] = { };#endif -/* Must be page-aligned because the real IDT is used in a fixmap. */ -gate_desc idt_table[IDT_ENTRIES] __page_aligned_bss; +/* +* Must be page-aligned because the real IDT is used in a fixmap. +* Also needs to be in the .data segment, because the idt_table is +* needed before the kernel clears the .bss segment. +*/ +gate_desc idt_table[IDT_ENTRIES] __page_aligned_data;struct desc_ptr idt_descr__ro_after_init = { .size = (IDT_ENTRIES *2 *sizeof (unsigned long )) -1 ,
Signature · 1 line
2.17.1
PATCH v3 29/75 x86/idt: Split idt_data setup out of set_intr_gate()
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The code to setup idt_data is needed for early exception handling, but set_intr_gate() can't be used that early because it has pv-ops in its code path, which don't work that early.
Split out the idt_data initialization part from set_intr_gate() so that it can be used separatly.
--- arch/x86/kernel/idt.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
arch/x86/kernel/idt.c +14 -8
@@ -231,18 +231,24 @@ idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sy } } +static void init_idt_data (struct idt_data *data,unsigned int n, +const void *addr) +{ +BUG_ON (n >0xFF ); + +memset (data,0 ,sizeof (*data)); + data->vector = n; + data->addr = addr; + data->segment = __KERNEL_CS; + data->bits.type =GATE_INTERRUPT ; + data->bits.p =1 ; +} +static void set_intr_gate (unsigned int n,const void *addr) {struct idt_data data; -BUG_ON (n >0xFF ); - -memset (&data,0 ,sizeof (data)); - data.vector = n; - data.addr = addr; - data.segment = __KERNEL_CS; - data.bits.type =GATE_INTERRUPT ; - data.bits.p =1 ; +init_idt_data (&data, n, addr);idt_setup_from_table (idt_table, &data,1 , false); }
Signature · 1 line
2.17.1
PATCH v3 30/75 x86/idt: Move two function from k/idt.c to i/a/desc.h
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Move these two functions from kernel/idt.c to include/asm/desc.h:
* init_idt_data()
* idt_init_desc()
These functions are needed to setup IDT entries very early and need to be called from head64.c. To be usable this early these functions need to be compiled without instrumentation and the stack-protector feature. These features need to be kept enabled for kernel/idt.c, so head64.c must use its own versions.
--- arch/x86/include/asm/desc.h | 27 +++++++++++++++++++++++++ arch/x86/include/asm/desc_defs.h | 7 +++++++ arch/x86/kernel/idt.c | 34 -------------------------------- 3 files changed, 34 insertions(+), 34 deletions(-)
arch/x86/include/asm/desc.h +27 -0
@@ -389,6 +389,33 @@ static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)void update_intr_gate (unsigned int n,const void *addr);void alloc_intr_gate (unsigned int n,const void *addr); +static inline void init_idt_data (struct idt_data *data,unsigned int n, +const void *addr) +{ +BUG_ON (n >0xFF ); + +memset (data,0 ,sizeof (*data)); + data->vector = n; + data->addr = addr; + data->segment = __KERNEL_CS; + data->bits.type =GATE_INTERRUPT ; + data->bits.p =1 ; +} + +static inline void idt_init_desc (gate_desc *gate,const struct idt_data *d) +{ +unsigned long addr = (unsigned long ) d->addr; + + gate->offset_low = (u16 ) addr; + gate->segment = (u16 ) d->segment; + gate->bits = d->bits; + gate->offset_middle = (u16) (addr >>16 ); +#ifdef CONFIG_X86_64 + gate->offset_high = (u32) (addr >>32 ); + gate->reserved =0 ; +#endif +} +extern unsigned long system_vectors[];#ifdef CONFIG_X86_64
arch/x86/include/asm/desc_defs.h +7 -0
@@ -74,6 +74,13 @@ struct idt_bits { p :1 ; } __attribute__((packed)); +struct idt_data { +unsigned int vector; +unsigned int segment; +struct idt_bits bits; +const void *addr; +}; +struct gate_struct {u16 offset_low;u16 segment;
arch/x86/kernel/idt.c +0 -34
@@ -9,13 +9,6 @@#include <asm/desc.h> #include <asm/hw_irq.h> -struct idt_data { -unsigned int vector; -unsigned int segment; -struct idt_bits bits; -const void *addr; -}; -#define DPL0 0x0#define DPL3 0x3
@@ -204,20 +197,6 @@ const struct desc_ptr debug_idt_descr = { };#endif -static inline void idt_init_desc (gate_desc *gate,const struct idt_data *d) -{ -unsigned long addr = (unsigned long ) d->addr; - - gate->offset_low = (u16 ) addr; - gate->segment = (u16 ) d->segment; - gate->bits = d->bits; - gate->offset_middle = (u16) (addr >>16 ); -#ifdef CONFIG_X86_64 - gate->offset_high = (u32) (addr >>32 ); - gate->reserved =0 ; -#endif -} -static void idt_setup_from_table (gate_desc *idt,const struct idt_data *t,int size,bool sys) {
@@ -231,19 +210,6 @@ idt_setup_from_table(gate_desc *idt, const struct idt_data *t, int size, bool sy } } -static void init_idt_data (struct idt_data *data,unsigned int n, -const void *addr) -{ -BUG_ON (n >0xFF ); - -memset (data,0 ,sizeof (*data)); - data->vector = n; - data->addr = addr; - data->segment = __KERNEL_CS; - data->bits.type =GATE_INTERRUPT ; - data->bits.p =1 ; -} -static void set_intr_gate (unsigned int n,const void *addr) {struct idt_data data;
Signature · 1 line
2.17.1
PATCH v3 31/75 x86/head/64: Install boot GDT
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Handling exceptions during boot requires a working GDT. The kernel GDT is not yet ready for use, so install a temporary boot GDT.
--- arch/x86/kernel/head_64.S | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
arch/x86/kernel/head_64.S +36 -0
@@ -72,6 +72,26 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu(), similar to initial_stack below */ leaq (__end_init_task - SIZEOF_PTREGS)(%rip), %rsp + /* Setup boot GDT descriptor and load boot GDT */ + leaq boot_gdt(%rip), %rax + movq %rax, boot_gdt_base(%rip) + lgdt boot_gdt_descr(%rip) + + /* New GDT is live - reload data segment registers */ + movl $__KERNEL_DS, %eax + movl %eax, %ds + movl %eax, %ss + movl %eax, %es + + /* Now switch to __KERNEL_CS so IRET works reliably */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + UNWIND_HINT_EMPTY + /* Sanitize CPU configuration */ call verify_cpu
@@ -480,6 +500,22 @@ SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) SYM_DATA(phys_base, .quad 0x0) EXPORT_SYMBOL(phys_base) +/* Boot GDT used when kernel addresses are not mapped yet */ +SYM_DATA_LOCAL(boot_gdt_descr, .word boot_gdt_end - boot_gdt) +SYM_DATA_LOCAL(boot_gdt_base, .quad 0) +SYM_DATA_START(boot_gdt) + .quad 0 + .quad 0x00cf9a000000ffff /* __KERNEL32_CS */ + .quad 0x00af9a000000ffff /* __KERNEL_CS */ + .quad 0x00cf92000000ffff /* __KERNEL_DS */ + .quad 0 /* __USER32_CS - unused */ + .quad 0 /* __USER_DS - unused */ + .quad 0 /* __USER_CS - unused */ + .quad 0 /* unused */ + .quad 0x0080890000000000 /* TSS descriptor */ + .quad 0x0000000000000000 /* TSS continued */ +SYM_DATA_END_LABEL(boot_gdt, SYM_L_LOCAL, boot_gdt_end) + #include "../../x86/xen/xen-head.S" __PAGE_ALIGNED_BSS
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 31/75] x86/head/64: Install boot GDT
Quoted 8 lines · Joerg Roedel
arch/x86/kernel/head_64.S +4
-0@@ -480,6 +500,22 @@ SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) SYM_DATA(phys_base, .quad 0x0) EXPORT_SYMBOL(phys_base) +/* Boot GDT used when kernel addresses are not mapped yet */ +SYM_DATA_LOCAL(boot_gdt_descr, .word boot_gdt_end - boot_gdt) +SYM_DATA_LOCAL(boot_gdt_base, .quad 0) +SYM_DATA_START(boot_gdt)
arch/x86/kernel/head_64.S SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) · line 516
Quoted 10 lines
arch/x86/kernel/head_64.S +10
-0+ .quad 0 + .quad 0x00cf9a000000ffff /* __KERNEL32_CS */ + .quad 0x00af9a000000ffff /* __KERNEL_CS */ + .quad 0x00cf92000000ffff /* __KERNEL_DS */ + .quad 0 /* __USER32_CS - unused */ + .quad 0 /* __USER_DS - unused */ + .quad 0 /* __USER_CS - unused */ + .quad 0 /* unused */ + .quad 0x0080890000000000 /* TSS descriptor */ + .quad 0x0000000000000000 /* TSS continued */
Any chance you could use macros ala GDT_ENTRY_INIT() for those instead of the naked values?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 31/75] x86/head/64: Install boot GDT
Quoted 14 lines · Borislav Petkov
Quoted 13 lines · Joerg Roedel
(quoted diff) +9
-0@@ -480,6 +500,22 @@ SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) SYM_DATA(phys_base, .quad 0x0) EXPORT_SYMBOL(phys_base) +/* Boot GDT used when kernel addresses are not mapped yet */ +SYM_DATA_LOCAL(boot_gdt_descr, .word boot_gdt_end - boot_gdt) +SYM_DATA_LOCAL(boot_gdt_base, .quad 0) +SYM_DATA_START(boot_gdt) + .quad 0 + .quad 0x00cf9a000000ffff /* __KERNEL32_CS */ + .quad 0x00af9a000000ffff /* __KERNEL_CS */ + .quad 0x00cf92000000ffff /* __KERNEL_DS */ + .quad 0 /* __USER32_CS - unused */
Quoted 8 lines
Review SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) · line 516
Quoted 5 lines
(quoted diff) +5
-0+ .quad 0 /* __USER_DS - unused */ + .quad 0 /* __USER_CS - unused */ + .quad 0 /* unused */ + .quad 0x0080890000000000 /* TSS descriptor */ + .quad 0x0000000000000000 /* TSS continued */Any chance you could use macros ala
GDT_ENTRY_INIT()for those instead of the naked values?
Yeah, seems to work. Updated patch attached.
From 1350746f9063147a33156c0b56a7b12b1794f555 Mon Sep 17 00:00:00 2001
From: Joerg Roedel <[email protected]>
Date: Fri, 15 Nov 2019 13:49:55 +0100
Subject: [PATCH 31/74] x86/head/64: Install boot GDT
Handling exceptions during boot requires a working GDT. The kernel GDT is not yet ready for use, so install a temporary boot GDT.
--- arch/x86/kernel/head64.c | 19 +++++++++++++++++++ arch/x86/kernel/head_64.S | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+)
arch/x86/kernel/head64.c +19 -0
@@ -61,6 +61,25 @@ unsigned long vmemmap_base __ro_after_init = __VMEMMAP_BASE_L4;EXPORT_SYMBOL (vmemmap_base);#endif +/* +* GDT used before %gs is set up and the kernel can use gdt_page. Needed for +* early exception handling. +*/ +struct desc_struct boot_gdt[GDT_ENTRIES ] = { + [GDT_ENTRY_KERNEL32_CS ] =GDT_ENTRY_INIT (0xc09b ,0 ,0xfffff ), + [GDT_ENTRY_KERNEL_CS ] =GDT_ENTRY_INIT (0xa09b ,0 ,0xfffff ), + [GDT_ENTRY_KERNEL_DS ] =GDT_ENTRY_INIT (0xc093 ,0 ,0xfffff ), +}; + +/* +* Address needs to be set at runtime because it references the boot_gdt while +* the kernel still uses a direct mapping. +*/ +struct desc_ptr boot_gdt_descr = { + .size =sizeof (boot_gdt), + .address =0 , +}; +#define __head __section(.head.text) static void __head *fixup_pointer(void *ptr, unsigned long physaddr)
arch/x86/kernel/head_64.S +25 -0
@@ -72,6 +72,26 @@ SYM_CODE_START_NOALIGN(startup_64) /* Set up the stack for verify_cpu(), similar to initial_stack below */ leaq (__end_init_task - SIZEOF_PTREGS)(%rip), %rsp + /* Setup boot GDT descriptor and load boot GDT */ + leaq boot_gdt(%rip), %rax + movq %rax, boot_gdt_descr+2(%rip) + lgdt boot_gdt_descr(%rip) + + /* New GDT is live - reload data segment registers */ + movl $__KERNEL_DS, %eax + movl %eax, %ds + movl %eax, %ss + movl %eax, %es + + /* Now switch to __KERNEL_CS so IRET works reliably */ + pushq $__KERNEL_CS + leaq .Lon_kernel_cs(%rip), %rax + pushq %rax + lretq + +.Lon_kernel_cs: + UNWIND_HINT_EMPTY + /* Sanitize CPU configuration */ call verify_cpu
@@ -143,6 +163,11 @@ SYM_CODE_START(secondary_startup_64) 1: UNWIND_HINT_EMPTY + /* Setup boot GDT descriptor and load boot GDT */ + leaq boot_gdt(%rip), %rax + movq %rax, boot_gdt_descr+2(%rip) + lgdt boot_gdt_descr(%rip) + /* Check if nx is implemented */ movl $0x80000001, %eax cpuid
Signature · 1 line
2.26.2
Borislav Petkov
maintainerRe: [PATCH v3 31/75] x86/head/64: Install boot GDT
Quoted 1 line · Joerg Roedel
Yeah, seems to work. Updated patch attached.
Looks nice, thanks!
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 32/75 x86/head/64: Reload GDT after switch to virtual addresses
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Reload the GDT after switching to virtual addresses to make sure it will not go away when the lower mappings are removed. This will also reload the GDT for booting APs, which will need a working GDT too to handle #VC exceptions.
--- arch/x86/kernel/head_64.S | 5 +++++ 1 file changed, 5 insertions(+)
arch/x86/kernel/head_64.S +5 -0
@@ -163,6 +163,11 @@ SYM_CODE_START(secondary_startup_64) 1: UNWIND_HINT_EMPTY + /* Setup boot GDT descriptor and load boot GDT */ + leaq boot_gdt(%rip), %rax + movq %rax, boot_gdt_base(%rip) + lgdt boot_gdt_descr(%rip) + /* Check if nx is implemented */ movl $0x80000001, %eax cpuid
Signature · 1 line
2.17.1
PATCH v3 33/75 x86/head/64: Load segment registers earlier
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Make sure segments are properly set up before setting up an IDT and doing anything that might cause a #VC exception. This is later needed for early exception handling.
--- arch/x86/kernel/head_64.S | 52 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 26 deletions(-)
arch/x86/kernel/head_64.S +26 -26
@@ -168,6 +168,32 @@ SYM_CODE_START(secondary_startup_64) movq %rax, boot_gdt_base(%rip) lgdt boot_gdt_descr(%rip) + /* set up data segments */ + xorl %eax,%eax + movl %eax,%ds + movl %eax,%ss + movl %eax,%es + + /* + * We don't really need to load %fs or %gs, but load them anyway + * to kill any stale realmode selectors. This allows execution + * under VT hardware. + */ + movl %eax,%fs + movl %eax,%gs + + /* Set up %gs. + * + * The base of %gs always points to fixed_percpu_data. If the + * stack protector canary is enabled, it is located at %gs:40. + * Note that, on SMP, the boot cpu uses init data section until + * the per cpu areas are set up. + */ + movl $MSR_GS_BASE,%ecx + movl initial_gs(%rip),%eax + movl initial_gs+4(%rip),%edx + wrmsr + /* Check if nx is implemented */ movl $0x80000001, %eax cpuid
@@ -203,32 +229,6 @@ SYM_CODE_START(secondary_startup_64) */ lgdt early_gdt_descr(%rip) - /* set up data segments */ - xorl %eax,%eax - movl %eax,%ds - movl %eax,%ss - movl %eax,%es - - /* - * We don't really need to load %fs or %gs, but load them anyway - * to kill any stale realmode selectors. This allows execution - * under VT hardware. - */ - movl %eax,%fs - movl %eax,%gs - - /* Set up %gs. - * - * The base of %gs always points to fixed_percpu_data. If the - * stack protector canary is enabled, it is located at %gs:40. - * Note that, on SMP, the boot cpu uses init data section until - * the per cpu areas are set up. - */ - movl $MSR_GS_BASE,%ecx - movl initial_gs(%rip),%eax - movl initial_gs+4(%rip),%edx - wrmsr - /* rsi is pointer to real mode structure with interesting info. pass it to C */ movq %rsi, %rdi
Signature · 1 line
2.17.1
PATCH v3 34/75 x86/head/64: Switch to initial stack earlier
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Make sure there is a stack once the kernel runs from virual addresses. At this stage any secondary CPU which boots will have lost its stack because the kernel switched to a new page-table which does not map the real-mode stack anymore.
This is needed for handling early #VC exceptions caused by instructions like CPUID.
--- arch/x86/kernel/head_64.S | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/head_64.S +6 -3
@@ -194,6 +194,12 @@ SYM_CODE_START(secondary_startup_64) movl initial_gs+4(%rip),%edx wrmsr + /* + * Setup a boot time stack - Any secondary CPU will have lost its stack + * by now because the cr3-switch above unmaps the real-mode stack + */ + movq initial_stack(%rip), %rsp + /* Check if nx is implemented */ movl $0x80000001, %eax cpuid
@@ -214,9 +220,6 @@ SYM_CODE_START(secondary_startup_64) /* Make changes effective */ movq %rax, %cr0 - /* Setup a boot time stack */ - movq initial_stack(%rip), %rsp - /* zero EFLAGS after setting rsp */ pushq $0 popfq
Signature · 1 line
2.17.1
PATCH v3 35/75 x86/head/64: Build k/head64.c with -fno-stack-protector
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The code inserted by the stack protector does not work in the early boot environment because it uses the GS segment, at least with memory encryption enabled. Make sure the early code is compiled without this feature enabled.
--- arch/x86/kernel/Makefile | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kernel/Makefile +4 -0
@@ -35,6 +35,10 @@ ifdef CONFIG_FRAME_POINTER OBJECT_FILES_NON_STANDARD_ftrace_$(BITS).o := y endif +# make sure head64.c is built without stack protector +nostackp := $(call cc-option, -fno-stack-protector) +CFLAGS_head64.o := $(nostackp) + # If instrumentation of this dir is enabled, boot hangs during first second. # Probably could be more selective here, but note that files related to irqs, # boot, dumpstack/stacktrace, etc are either non-interesting or can lead to
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 35/75] x86/head/64: Build k/head64.c with -fno-stack-protector
Quoted 5 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
The code inserted by the stack protector does not work in the early boot environment because it uses the GS segment, at least with memory encryption enabled.
Can you elaborate on why is that a problem?
The stack cookie is not generated that early yet so it should be comparing %gs:40 to 0.
Also, it generates the checking code here only with
CONFIG_STACKPROTECTOR_STRONG=y
Quoted 1 line
Make sure the early code is compiled without this feature enabled.
If so, then this should be with CONFIG_AMD_MEM_ENCRYPT ifdeffery around it.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 35/75] x86/head/64: Build k/head64.c with -fno-stack-protector
Quoted 11 lines · Borislav Petkov
Quoted 5 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
The code inserted by the stack protector does not work in the early boot environment because it uses the GS segment, at least with memory encryption enabled.
Can you elaborate on why is that a problem?
The stack cookie is not generated that early yet so it should be comparing %gs:40 to 0.
Yes, and when GS_BASE is 0 it will dereference NULL pointer, which generates a page-fault before the kernel is able to handle it.
Joerg
Re: [PATCH v3 35/75] x86/head/64: Build k/head64.c with -fno-stack-protector
In reply to Joerg RoedelQuoted 18 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
The code inserted by the stack protector does not work in the early boot environment because it uses the GS segment, at least with memory encryption enabled. Make sure the early code is compiled without this feature enabled.
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/Makefile | 4 ++++ 1 file changed, 4 insertions(+)arch/x86/kernel/Makefile +0
-0
arch/x86/kernel/Makefile ifdef CONFIG_FRAME_POINTER · line 44
Quoted 10 lines
arch/x86/kernel/Makefile +4
-0@@ -35,6 +35,10 @@ ifdef CONFIG_FRAME_POINTER OBJECT_FILES_NON_STANDARD_ftrace_$(BITS).o := y endif +# make sure head64.c is built without stack protector +nostackp := $(call cc-option, -fno-stack-protector) +CFLAGS_head64.o := $(nostackp) + # If instrumentation of this dir is enabled, boot hangs during first second. # Probably could be more selective here, but note that files related to irqs, # boot, dumpstack/stacktrace, etc are either non-interesting or can lead to
The proper fix would be to initialize MSR_GS_BASE earlier.
--
Brian Gerst
Re: [PATCH v3 35/75] x86/head/64: Build k/head64.c with -fno-stack-protector
Quoted 3 lines · Brian Gerst
On Tue, Apr 28, 2020 at 11:28 AM Joerg Roedel <[email protected]> wrote:
The proper fix would be to initialize
MSR_GS_BASEearlier.
That'll mean to initialize it two times during boot, as the first C function with stack-protection is called before the kernel switches to its high addresses (early_idt_setup call-path). But okay, I can do that.
On the other side, which value does the stack protector have in the early boot code?
Joerg
Re: [PATCH v3 35/75] x86/head/64: Build k/head64.c with -fno-stack-protector
Quoted 9 lines · Joerg Roedel
Quoted 3 lines · Brian Gerst
On Tue, Apr 28, 2020 at 11:28 AM Joerg Roedel <[email protected]> wrote:
The proper fix would be to initialize
MSR_GS_BASEearlier.That'll mean to initialize it two times during boot, as the first C function with stack-protection is called before the kernel switches to its high addresses (
early_idt_setupcall-path). But okay, I can do that.
Good point. Since this is boot code which isn't subject to stack smashing attacks, disabling stack protector is probably the simpler option.
--
Brian Gerst
PATCH v3 36/75 x86/head/64: Load IDT earlier
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Load the IDT right after switching to virtual addresses in head_64.S so that the kernel can handle #VC exceptions.
--- arch/x86/kernel/head64.c | 15 +++++++++++++++ arch/x86/kernel/head_64.S | 17 +++++++++++++++++ 2 files changed, 32 insertions(+)
arch/x86/kernel/head64.c +15 -0
@@ -489,3 +489,18 @@ void __init x86_64_start_reservations(char *real_mode_data)start_kernel (); } + +void __head early_idt_setup_early_handler(unsigned long physaddr) +{ +gate_desc *idt =fixup_pointer (idt_table, physaddr); +int i; + +for (i =0 ; i <NUM_EXCEPTION_VECTORS ; i++) { +struct idt_data data; +gate_desc desc; + +init_idt_data (&data, i, early_idt_handler_array[i]); +idt_init_desc (&desc, &data); +native_write_idt_entry (idt, i, &desc); + } +}
arch/x86/kernel/head_64.S +17 -0
@@ -104,6 +104,20 @@ SYM_CODE_START_NOALIGN(startup_64) leaq _text(%rip), %rdi pushq %rsi call __startup_64 + /* Save return value */ + pushq %rax + + /* + * Load IDT with early handlers - needed for SEV-ES + * Do this here because this must only happen on the boot CPU + * and the code below is shared with secondary CPU bringup. + */ + leaq _text(%rip), %rdi + call early_idt_setup_early_handler + + /* Restore __startup_64 return value*/ + popq %rax + /* Restore pointer to real_mode_data */ popq %rsi /* Form the CR3 value being sure to include the CR3 modifier */
@@ -200,6 +214,9 @@ SYM_CODE_START(secondary_startup_64) */ movq initial_stack(%rip), %rsp + /* Load IDT */ + lidt idt_descr(%rip) + /* Check if nx is implemented */ movl $0x80000001, %eax cpuid
Signature · 1 line
2.17.1
PATCH v3 37/75 x86/head/64: Move early exception dispatch to C code
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Move the assembly coded dispatch between page-faults and all other exceptions to C code to make it easier to maintain and extend.
Also change the return-type of early_make_pgtable() to bool and make it static.
--- arch/x86/include/asm/pgtable.h | 2 +- arch/x86/include/asm/setup.h | 1 - arch/x86/kernel/head64.c | 19 +++++++++++++++---- arch/x86/kernel/head_64.S | 11 +---------- 4 files changed, 17 insertions(+), 16 deletions(-)
arch/x86/include/asm/pgtable.h +1 -1
@@ -28,7 +28,7 @@#include <asm-generic/pgtable_uffd.h> extern pgd_t early_top_pgt[PTRS_PER_PGD ]; -int __init __early_make_pgtable (unsigned long address,pmdval_t pmd); +bool __init __early_make_pgtable (unsigned long address,pmdval_t pmd);void ptdump_walk_pgd_level (struct seq_file *m,struct mm_struct *mm); void ptdump_walk_pgd_level_debugfs(struct seq_file *m, struct mm_struct *mm,
arch/x86/include/asm/setup.h +0 -1
@@ -48,7 +48,6 @@ extern void reserve_standard_io_resources(void);extern void i386_reserve_resources (void );extern unsigned long __startup_64 (unsigned long physaddr,struct boot_params *bp);extern unsigned long __startup_secondary_64 (void ); -extern int early_make_pgtable (unsigned long address);#ifdef CONFIG_X86_INTEL_MID extern void x86_intel_mid_early_setup (void );
arch/x86/kernel/head64.c +15 -4
@@ -36,6 +36,8 @@#include <asm/microcode.h> #include <asm/kasan.h> #include <asm/fixmap.h> +#include <asm/extable.h> +#include <asm/trap_defs.h> /* * Manage page tables very early on.
@@ -297,7 +299,7 @@ static void __init reset_early_page_tables(void) }/* Create a new PMD entry */ -int __init __early_make_pgtable (unsigned long address,pmdval_t pmd) +bool __init __early_make_pgtable (unsigned long address,pmdval_t pmd) {unsigned long physaddr = address - __PAGE_OFFSET;pgdval_t pgd, *pgd_p;
@@ -307,7 +309,7 @@ int __init __early_make_pgtable(unsigned long address, pmdval_t pmd)/* Invalid address or early pgt is done ? */ if (physaddr >=MAXMEM ||read_cr3_pa () !=__pa_nodebug (early_top_pgt)) -return -1 ; +return false; again: pgd_p = &early_top_pgt[pgd_index (address)].pgd;
@@ -364,10 +366,10 @@ int __init __early_make_pgtable(unsigned long address, pmdval_t pmd) } pmd_p[pmd_index (address)] = pmd; -return 0 ; +return true; } -int __init early_make_pgtable (unsigned long address) +static bool __init early_make_pgtable (unsigned long address) {unsigned long physaddr = address - __PAGE_OFFSET;pmdval_t pmd;
@@ -377,6 +379,15 @@ int __init early_make_pgtable(unsigned long address)return __early_make_pgtable (address, pmd); } +void __init do_early_exception (struct pt_regs *regs,int trapnr) +{ +if (trapnr ==X86_TRAP_PF && +early_make_pgtable (native_read_cr2 ())) +return ; + +early_fixup_exception (regs, trapnr); +} +/* Don't add a printk in there. printk relies on the PDA which is not initialized yet. */ static void __init clear_bss (void )
arch/x86/kernel/head_64.S +1 -10
@@ -363,18 +363,9 @@ SYM_CODE_START_LOCAL(early_idt_handler_common) pushq %r15 /* pt_regs->r15 */ UNWIND_HINT_REGS - cmpq $14,%rsi /* Page fault? */ - jnz 10f - GET_CR2_INTO(%rdi) /* can clobber %rax if pv */ - call early_make_pgtable - andl %eax,%eax - jz 20f /* All good */ - -10: movq %rsp,%rdi /* RDI = pt_regs; RSI is already trapnr */ - call early_fixup_exception + call do_early_exception -20: decl early_recursion_flag(%rip) jmp restore_regs_and_return_to_kernel SYM_CODE_END(early_idt_handler_common)
Signature · 1 line
2.17.1
PATCH v3 38/75 x86/sev-es: Add SEV-ES Feature Detection
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add the sev_es_active function for checking whether SEV-ES is enabled. Also cache the value of MSR_AMD64_SEV at boot to speed up the feature checking in the running code.
--- arch/x86/include/asm/mem_encrypt.h | 3 +++ arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/mm/mem_encrypt.c | 10 +++++++++- arch/x86/mm/mem_encrypt_identity.c | 3 +++ 4 files changed, 17 insertions(+), 1 deletion(-)
arch/x86/include/asm/mem_encrypt.h +3 -0
@@ -19,6 +19,7 @@#ifdef CONFIG_AMD_MEM_ENCRYPT extern u64 sme_me_mask; +extern u64 sev_status;extern bool sev_enabled; void sme_encrypt_execute(unsigned long encrypted_kernel_vaddr,
@@ -49,6 +50,7 @@ void __init mem_encrypt_free_decrypted_mem(void);bool sme_active (void );bool sev_active (void ); +bool sev_es_active (void );#define __bss_decrypted __attribute__((__section__(".bss..decrypted")))
@@ -71,6 +73,7 @@ static inline void __init sme_enable(struct boot_params *bp) { }static inline bool sme_active (void ) {return false; }static inline bool sev_active (void ) {return false; } +static inline bool sev_es_active (void ) {return false; }static inline int __init early_set_memory_decrypted (unsigned long vaddr,unsigned long size) {return 0 ; }
arch/x86/include/asm/msr-index.h +2 -0
@@ -444,7 +444,9 @@#define MSR_AMD64_SEV_ES_GHCB 0xc0010130#define MSR_AMD64_SEV 0xc0010131#define MSR_AMD64_SEV_ENABLED_BIT 0 +#define MSR_AMD64_SEV_ES_ENABLED_BIT 1#define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT) +#define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)#define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f
arch/x86/mm/mem_encrypt.c +9 -1
@@ -38,6 +38,7 @@* section is later cleared. */ u64 sme_me_mask __section(.data) = 0; +u64 sev_status __section(.data) = 0;EXPORT_SYMBOL (sme_me_mask);DEFINE_STATIC_KEY_FALSE (sev_enable_key);EXPORT_SYMBOL_GPL (sev_enable_key);
@@ -347,9 +348,16 @@ bool sme_active(void)bool sev_active (void ) { -return sme_me_mask && sev_enabled; +return !!(sev_status &MSR_AMD64_SEV_ENABLED ); } +bool sev_es_active (void ) +{ +return !!(sev_status &MSR_AMD64_SEV_ES_ENABLED ); +} +EXPORT_SYMBOL_GPL (sev_es_active); + +/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted (struct device *dev) {
arch/x86/mm/mem_encrypt_identity.c +3 -0
@@ -540,6 +540,9 @@ void __init sme_enable(struct boot_params *bp)if (!(msr &MSR_AMD64_SEV_ENABLED ))return ; +/* Save SEV_STATUS to avoid reading MSR again */ + sev_status = msr; +/* SEV state cannot be controlled by a command line option */ sme_me_mask = me_mask; sev_enabled = true;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 38/75] x86/sev-es: Add SEV-ES Feature Detection
arch/x86/mm/mem_encrypt.c bool sme_active(void) · line 358
Quoted 5 lines · Joerg Roedel
arch/x86/mm/mem_encrypt.c +5
-0+bool sev_es_active (void ) +{ +return !!(sev_status &MSR_AMD64_SEV_ES_ENABLED ); +} +EXPORT_SYMBOL_GPL (sev_es_active);
I don't see this being used in modules anywhere in the patchset. Or am I missing something?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 38/75] x86/sev-es: Add SEV-ES Feature Detection
Quoted 9 lines · Borislav Petkov
arch/x86/mm/mem_encrypt.c bool sme_active(void) · line 358
Quoted 5 lines · Joerg Roedel
arch/x86/mm/mem_encrypt.c +5
-0+bool sev_es_active (void ) +{ +return !!(sev_status &MSR_AMD64_SEV_ES_ENABLED ); +} +EXPORT_SYMBOL_GPL (sev_es_active);I don't see this being used in modules anywhere in the patchset. Or am I missing something?
It is used in several places, for example do do_nmi() to conditionally re-open the NMI window in SEV-ES guests. But there are other uses too, like int sev_es_efi_map_ghcbs() or in sev_es_init_vc_handling() to opt out if not running as an SEV-ES guest.
Joerg
PATCH v3 39/75 x86/sev-es: Print SEV-ES info into kernel log
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Refactor the message printed to the kernel log which indicates whether SEV or SME is active to print a list of enabled encryption features. This will scale better in the future when more memory encryption features might be added. Also add SEV-ES to the list of features.
--- arch/x86/mm/mem_encrypt.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
arch/x86/mm/mem_encrypt.c +26 -3
@@ -409,6 +409,31 @@ void __init mem_encrypt_free_decrypted_mem(void)free_init_pages ("unused decrypted" , vaddr, vaddr_end); } +static void print_mem_encrypt_feature_info (void ) +{ +pr_info ("AMD Memory Encryption Features active:" ); + +/* Secure Memory Encryption */ +if (sme_active ()) { +/* +* SME is mutually exclusive with any of the SEV +* features below. +*/ +pr_cont (" SME\n" ); +return ; + } + +/* Secure Encrypted Virtualization */ +if (sev_active ()) +pr_cont (" SEV" ); + +/* Encrypted Register State */ +if (sev_es_active ()) +pr_cont (" SEV-ES" ); + +pr_cont ("\n" ); +} +void __init mem_encrypt_init (void ) {if (!sme_me_mask)
@@ -423,8 +448,6 @@ void __init mem_encrypt_init(void)if (sev_active ())static_branch_enable (&sev_enable_key); -pr_info ("AMD %s active\n" , -sev_active () ?"Secure Encrypted Virtualization (SEV)" - :"Secure Memory Encryption (SME)" ); +print_mem_encrypt_feature_info (); }
Signature · 1 line
2.17.1
PATCH v3 40/75 x86/sev-es: Compile early handler code into kernel image
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Setup sev-es.c and include the code from the pre-decompression stage to also build it into the image of the running kernel. Temporarily add __maybe_unused annotations to avoid build warnings until the functions get used.
--- arch/x86/kernel/Makefile | 1 + arch/x86/kernel/sev-es-shared.c | 21 ++-- arch/x86/kernel/sev-es.c | 163 ++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 10 deletions(-) create mode 100644 arch/x86/kernel/sev-es.c
arch/x86/kernel/Makefile +1 -0
@@ -148,6 +148,7 @@ obj-$(CONFIG_UNWINDER_ORC) += unwind_orc.o obj-$(CONFIG_UNWINDER_FRAME_POINTER) += unwind_frame.o obj-$(CONFIG_UNWINDER_GUESS) += unwind_guess.o +obj-$(CONFIG_AMD_MEM_ENCRYPT) += sev-es.o ### # 64 bit specific files ifeq ($(CONFIG_X86_64),y)
arch/x86/kernel/sev-es-shared.c +11 -10
@@ -9,7 +9,7 @@* and is included directly into both code-bases. */ -static void sev_es_terminate (unsigned int reason) +static void __maybe_unused sev_es_terminate (unsigned int reason) {u64 val =GHCB_SEV_TERMINATE ;
@@ -27,7 +27,7 @@ static void sev_es_terminate(unsigned int reason) asmvolatile ("hlt\n" : : :"memory" ); } -static bool sev_es_negotiate_protocol (void ) +static bool __maybe_unused sev_es_negotiate_protocol (void ) {u64 val;
@@ -46,7 +46,7 @@ static bool sev_es_negotiate_protocol(void)return true; } -static void vc_ghcb_invalidate (struct ghcb *ghcb) +static void __maybe_unused vc_ghcb_invalidate (struct ghcb *ghcb) {memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap)); }
@@ -58,9 +58,9 @@ static bool vc_decoding_needed(unsigned long exit_code) exit_code <= SVM_EXIT_LAST_EXCP); } -static enum es_result vc_init_em_ctxt (struct es_em_ctxt *ctxt, -struct pt_regs *regs, -unsigned long exit_code) +static enum es_result __maybe_unused vc_init_em_ctxt (struct es_em_ctxt *ctxt, +struct pt_regs *regs, +unsigned long exit_code) {enum es_result ret =ES_OK ;
@@ -73,7 +73,7 @@ static enum es_result vc_init_em_ctxt(struct es_em_ctxt *ctxt,return ret; } -static void vc_finish_insn (struct es_em_ctxt *ctxt) +static void __maybe_unused vc_finish_insn (struct es_em_ctxt *ctxt) { ctxt->regs->ip += ctxt->insn.length; }
@@ -324,7 +324,8 @@ static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)return ES_OK ; } -static enum es_result vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +static enum es_result __maybe_unused +vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {struct pt_regs *regs = ctxt->regs;u64 exit_info_1, exit_info_2;
@@ -420,8 +421,8 @@ static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)return ret; } -static enum es_result vc_handle_cpuid (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) +static enum es_result __maybe_unused vc_handle_cpuid (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) {struct pt_regs *regs = ctxt->regs;u32 cr4 =native_read_cr4 ();
arch/x86/kernel/sev-es.c new file +163 -0
163 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 40/75] x86/sev-es: Compile early handler code into kernel image
Quoted 4 lines · Joerg Roedel
arch/x86/kernel/sev-es.c new file +4
-0+static inline u64 sev_es_rd_ghcb_msr (void ) +{ +return native_read_msr (MSR_AMD64_SEV_ES_GHCB ); +}
arch/x86/kernel/sev-es.c line 33
Quoted 10 lines
arch/x86/kernel/sev-es.c new file +10
-0+ +static inline void sev_es_wr_ghcb_msr (u64 val) +{ +u32 low, high; + + low = (u32)(val); + high = (u32)(val >>32 ); + +native_write_msr (MSR_AMD64_SEV_ES_GHCB , low, high); +}
Instead of duplicating those two, you can lift the ones in the compressed image into sev-es.h and use them here. I don't care one bit about the MSR tracepoints in native_*_msr().
Quoted 20 lines
arch/x86/kernel/sev-es.c new file +20
-0+static enum es_result vc_write_mem (struct es_em_ctxt *ctxt, +char *dst,char *buf,size_t size) +{ +unsigned long error_code =X86_PF_PROT |X86_PF_WRITE ; +char __user *target = (char __user *)dst; +u64 d8; +u32 d4; +u16 d2; +u8 d1; + +switch (size) { +case 1 : +memcpy (&d1, buf,1 ); +if (put_user (d1, target)) + goto fault; +break ; +case 2 : +memcpy (&d2, buf,2 ); +if (put_user (d2, target)) + goto fault;
arch/x86/kernel/sev-es.c line 93
Quoted 10 lines
arch/x86/kernel/sev-es.c new file +10
-0+break ; +case 4 : +memcpy (&d4, buf,4 ); +if (put_user (d4, target)) + goto fault; +break ; +case 8 : +memcpy (&d8, buf,8 ); +if (put_user (d8, target)) + goto fault;
Ok, those (and below) memcpys get nicely optimized to MOVs by the compiler here.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 40/75] x86/sev-es: Compile early handler code into kernel image
Quoted 11 lines · Borislav Petkov
Quoted 10 lines · Joerg Roedel
arch/x86/kernel/sev-es.c new file +10
-0+static inline u64 sev_es_rd_ghcb_msr (void ) +{ +return native_read_msr (MSR_AMD64_SEV_ES_GHCB ); +} + +static inline void sev_es_wr_ghcb_msr (u64 val) +{ +u32 low, high; + + low = (u32)(val);
Quoted 8 lines
arch/x86/kernel/sev-es.c line 33
Quoted 4 lines
arch/x86/kernel/sev-es.c new file +4
-0+ high = (u32)(val >>32 ); + +native_write_msr (MSR_AMD64_SEV_ES_GHCB , low, high); +}Instead of duplicating those two, you can lift the ones in the compressed image into
sev-es.hand use them here. I don't care one bit about the MSR tracepoints in native_*_msr().
It is not only the trace-point, this would also eliminate exception handling in case the MSR access triggers a #GP. The "Unhandled MSR read/write" messages would turn into a "General Protection Fault" message.
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 40/75] x86/sev-es: Compile early handler code into kernel image
Quoted 4 lines · Joerg Roedel
It is not only the trace-point, this would also eliminate exception handling in case the MSR access triggers a #GP. The "Unhandled MSR read/write" messages would turn into a "General Protection Fault" message.
But the early ones can trigger a #GP too. And there we can't handle those #GPs.
Why would the late ones need exception handling all of a sudden? And for the GHCB MSR, of all MSRs which the SEV-ES guest has used so far to bootstrap?!
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 40/75] x86/sev-es: Compile early handler code into kernel image
Quoted 6 lines · Borislav Petkov
Quoted 4 lines · Joerg Roedel
It is not only the trace-point, this would also eliminate exception handling in case the MSR access triggers a #GP. The "Unhandled MSR read/write" messages would turn into a "General Protection Fault" message.
Quoted 6 lines
But the early ones can trigger a #GP too. And there we can't handle those #GPs.
Why would the late ones need exception handling all of a sudden? And for the GHCB MSR, of all MSRs which the SEV-ES guest has used so far to bootstrap?!
For example when there is a bug in the code which triggers an SEV-ES-only code-path at runtime on bare-metal or in a non-SEV-ES VM. When the MSR is accessed accidentially in that code-path the exception handling will be helpful.
Joerg
PATCH v3 41/75 x86/sev-es: Setup early #VC handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Setup an early handler for #VC exceptions. There is no GHCB mapped yet, so just re-use the vc_no_ghcb_handler. It can only handle CPUID exit-codes, but that should be enough to get the kernel through verify_cpu() and __startup_64() until it runs on virtual addresses.
--- arch/x86/include/asm/desc.h | 1 + arch/x86/include/asm/processor.h | 1 + arch/x86/include/asm/sev-es.h | 2 ++ arch/x86/kernel/head64.c | 17 +++++++++++++++ arch/x86/kernel/head_64.S | 36 ++++++++++++++++++++++++++++++++ arch/x86/kernel/idt.c | 10 +++++++++ 6 files changed, 67 insertions(+)
arch/x86/include/asm/desc.h +1 -0
@@ -388,6 +388,7 @@ static inline void set_desc_limit(struct desc_struct *desc, unsigned long limit)void update_intr_gate (unsigned int n,const void *addr);void alloc_intr_gate (unsigned int n,const void *addr); +void set_early_idt_handler (gate_desc *idt,int n,void *handler);static inline void init_idt_data (struct idt_data *data,unsigned int n,const void *addr)
arch/x86/include/asm/processor.h +1 -0
@@ -757,6 +757,7 @@ extern int sysenter_setup(void);/* Defined in head.S */ extern struct desc_ptr early_gdt_descr; +extern struct desc_ptr early_idt_descr;extern void switch_to_new_gdt (int );extern void load_direct_gdt (int );
arch/x86/include/asm/sev-es.h +2 -0
@@ -73,4 +73,6 @@ static inline u64 lower_bits(u64 val, unsigned int bits) return (val & mask); } +extern void vc_no_ghcb (void ); +#endif
arch/x86/kernel/head64.c +17 -0
@@ -38,6 +38,7 @@#include <asm/fixmap.h> #include <asm/extable.h> #include <asm/trap_defs.h> +#include <asm/sev-es.h> /* * Manage page tables very early on.
@@ -515,3 +516,19 @@ void __head early_idt_setup_early_handler(unsigned long physaddr)native_write_idt_entry (idt, i, &desc); } } + +void __head early_idt_setup(unsigned long physbase) +{ +gate_desc *idt =fixup_pointer (idt_table, physbase); +void __maybe_unused *handler; + +#ifdef CONFIG_AMD_MEM_ENCRYPT +/* VMM Communication Exception */ + handler =fixup_pointer (vc_no_ghcb, physbase); +set_early_idt_handler (idt,X86_TRAP_VC , handler); +#endif + +/* Initialize IDT descriptor and load IDT */ + early_idt_descr.address = (unsigned long )idt; +native_load_idt (&early_idt_descr); +}
arch/x86/kernel/head_64.S +36 -0
@@ -92,6 +92,12 @@ SYM_CODE_START_NOALIGN(startup_64) .Lon_kernel_cs: UNWIND_HINT_EMPTY + /* Setup IDT - Needed for SEV-ES */ + leaq _text(%rip), %rdi + pushq %rsi + call early_idt_setup + popq %rsi + /* Sanitize CPU configuration */ call verify_cpu
@@ -370,6 +376,33 @@ SYM_CODE_START_LOCAL(early_idt_handler_common) jmp restore_regs_and_return_to_kernel SYM_CODE_END(early_idt_handler_common) +#ifdef CONFIG_AMD_MEM_ENCRYPT +/* + * VC Exception handler used during very early boot. The + * early_idt_handler_array can't be used because it returns via the + * paravirtualized INTERRUPT_RETURN and pv-ops don't work that early. + */ +SYM_CODE_START_NOALIGN(vc_no_ghcb) + UNWIND_HINT_IRET_REGS offset=8 + + /* Build pt_regs */ + PUSH_AND_CLEAR_REGS + + /* Call C handler */ + movq %rsp, %rdi + movq ORIG_RAX(%rsp), %rsi + call do_vc_no_ghcb + + /* Unwind pt_regs */ + POP_REGS + + /* Remove Error Code */ + addq $8, %rsp + + /* Pure iret required here - don't use INTERRUPT_RETURN */ + iretq +SYM_CODE_END(vc_no_ghcb) +#endif #define SYM_DATA_START_PAGE_ALIGNED(name) \ SYM_START(name, SYM_L_GLOBAL, .balign PAGE_SIZE)
@@ -511,6 +544,9 @@ SYM_DATA_END(level1_fixmap_pgt) SYM_DATA(early_gdt_descr, .word GDT_ENTRIES*8-1) SYM_DATA_LOCAL(early_gdt_descr_base, .quad INIT_PER_CPU_VAR(gdt_page)) +SYM_DATA(early_idt_descr, .word NUM_EXCEPTION_VECTORS * 16) +SYM_DATA_LOCAL(early_idt_descr_base, .quad 0) + .align 16 /* This must match the first entry in level2_kernel_pgt */ SYM_DATA(phys_base, .quad 0x0)
arch/x86/kernel/idt.c +10 -0
@@ -341,3 +341,13 @@ void alloc_intr_gate(unsigned int n, const void *addr)if (!test_and_set_bit (n, system_vectors))set_intr_gate (n, addr); } + +void set_early_idt_handler (gate_desc *idt,int n,void *handler) +{ +struct idt_data data; +gate_desc desc; + +init_idt_data (&data, n, handler); +idt_init_desc (&desc, &data); +native_write_idt_entry (idt, n, &desc); +}
Signature · 1 line
2.17.1
PATCH v3 42/75 x86/sev-es: Setup GHCB based boot #VC handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add the infrastructure to handle #VC exceptions when the kernel runs on virtual addresses and has a GHCB mapped. This handler will be used until the runtime #VC handler takes over.
--- arch/x86/include/asm/segment.h | 2 +- arch/x86/include/asm/sev-es.h | 1 + arch/x86/kernel/head64.c | 6 ++ arch/x86/kernel/sev-es-shared.c | 14 ++-- arch/x86/kernel/sev-es.c | 116 ++++++++++++++++++++++++++++++++ arch/x86/mm/extable.c | 1 + 6 files changed, 132 insertions(+), 8 deletions(-)
arch/x86/include/asm/segment.h +1 -1
@@ -230,7 +230,7 @@#define NUM_EXCEPTION_VECTORS 32/* Bitmask of exception vectors which push an error code on the stack: */ -#define EXCEPTION_ERRCODE_MASK 0x00027d00 +#define EXCEPTION_ERRCODE_MASK 0x20027d00#define GDT_SIZE (GDT_ENTRIES*8)#define GDT_ENTRY_TLS_ENTRIES 3
arch/x86/include/asm/sev-es.h +1 -0
@@ -74,5 +74,6 @@ static inline u64 lower_bits(u64 val, unsigned int bits) }extern void vc_no_ghcb (void ); +extern bool vc_boot_ghcb (struct pt_regs *regs);#endif
arch/x86/kernel/head64.c +6 -0
@@ -386,6 +386,12 @@ void __init do_early_exception(struct pt_regs *regs, int trapnr) early_make_pgtable(native_read_cr2()))return ; +#ifdef CONFIG_AMD_MEM_ENCRYPT +if (trapnr ==X86_TRAP_VC && +vc_boot_ghcb (regs)) +return ; +#endif +early_fixup_exception (regs, trapnr); }
arch/x86/kernel/sev-es-shared.c +7 -7
@@ -9,7 +9,7 @@* and is included directly into both code-bases. */ -static void __maybe_unused sev_es_terminate (unsigned int reason) +static void sev_es_terminate (unsigned int reason) {u64 val =GHCB_SEV_TERMINATE ;
@@ -27,7 +27,7 @@ static void __maybe_unused sev_es_terminate(unsigned int reason) asmvolatile ("hlt\n" : : :"memory" ); } -static bool __maybe_unused sev_es_negotiate_protocol (void ) +static bool sev_es_negotiate_protocol (void ) {u64 val;
@@ -46,7 +46,7 @@ static bool __maybe_unused sev_es_negotiate_protocol(void)return true; } -static void __maybe_unused vc_ghcb_invalidate (struct ghcb *ghcb) +static void vc_ghcb_invalidate (struct ghcb *ghcb) {memset (ghcb->save.valid_bitmap,0 ,sizeof (ghcb->save.valid_bitmap)); }
@@ -58,9 +58,9 @@ static bool vc_decoding_needed(unsigned long exit_code) exit_code <= SVM_EXIT_LAST_EXCP); } -static enum es_result __maybe_unused vc_init_em_ctxt (struct es_em_ctxt *ctxt, -struct pt_regs *regs, -unsigned long exit_code) +static enum es_result vc_init_em_ctxt (struct es_em_ctxt *ctxt, +struct pt_regs *regs, +unsigned long exit_code) {enum es_result ret =ES_OK ;
@@ -73,7 +73,7 @@ static enum es_result __maybe_unused vc_init_em_ctxt(struct es_em_ctxt *ctxt,return ret; } -static void __maybe_unused vc_finish_insn (struct es_em_ctxt *ctxt) +static void vc_finish_insn (struct es_em_ctxt *ctxt) { ctxt->regs->ip += ctxt->insn.length; }
arch/x86/kernel/sev-es.c +116 -0
134 lines not on this page · raw message
arch/x86/mm/extable.c +1 -0
@@ -5,6 +5,7 @@#include <xen/xen.h> #include <asm/fpu/internal.h> +#include <asm/sev-es.h> #include <asm/traps.h> #include <asm/kdebug.h>
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 42/75] x86/sev-es: Setup GHCB based boot #VC handler
arch/x86/include/asm/sev-es.h static inline u64 lower_bits(u64 val, unsigned int bits) · line 77
Quoted 9 lines · Joerg Roedel
arch/x86/include/asm/sev-es.h +1
-0@@ -74,5 +74,6 @@ static inline u64 lower_bits(u64 val, unsigned int bits) }extern void vc_no_ghcb (void ); +extern bool vc_boot_ghcb (struct pt_regs *regs);
Those function names need verbs:
handle_vc_no_ghcb
handle_vc_boot_ghcb
arch/x86/include/asm/sev-es.h static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 184
Quoted 10 lines
arch/x86/include/asm/sev-es.h +6
-0@@ -161,3 +176,104 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,/* Include code shared with pre-decompression boot stage */ #include "sev-es-shared.c" + +/* +* This function runs on the first #VC exception after the kernel +* switched to virtual addresses. +*/ +static bool __init sev_es_setup_ghcb (void )
There's already another sev_es_setup_ghcb() in compressed/. All those functions with the same name are just confusion waiting to happen. Let's prepend the ones in compressed/ with "early_" or so, so that their names are at least different even if they're in two different files with the same name.
This way you know at least which function is used in which boot stages.
arch/x86/include/asm/sev-es.h static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 188
Quoted 4 lines
arch/x86/include/asm/sev-es.h +4
-0+{ +/* First make sure the hypervisor talks a supported protocol. */ +if (!sev_es_negotiate_protocol ()) +return false;
<---- newline here.
arch/x86/include/asm/sev-es.h static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 201
Quoted 13 lines
arch/x86/include/asm/sev-es.h +13
-0+/* +* Clear the boot_ghcb. The first exception comes in before the bss +* section is cleared. +*/ +memset (&boot_ghcb_page,0 ,PAGE_SIZE ); + +/* Alright - Make the boot-ghcb public */ + boot_ghcb = &boot_ghcb_page; + +return true; +} + +static void __init vc_early_vc_forward_exception (struct es_em_ctxt *ctxt)
That second "vc" looks redundant.
Quoted 28 lines
arch/x86/include/asm/sev-es.h +28
-0+{ +int trapnr = ctxt->fi.vector; + +if (trapnr ==X86_TRAP_PF ) +native_write_cr2 (ctxt->fi.cr2); + + ctxt->regs->orig_ax = ctxt->fi.error_code; +do_early_exception (ctxt->regs, trapnr); +} + +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; +} + +bool __init vc_boot_ghcb (struct pt_regs *regs)
arch/x86/include/asm/sev-es.h static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 239
Quoted 10 lines
arch/x86/include/asm/sev-es.h +10
-0+{ +unsigned long exit_code = regs->orig_ax; +struct es_em_ctxt ctxt; +enum es_result result; + +/* Do initial setup or terminate the guest */ +if (unlikely (boot_ghcb ==NULL && !sev_es_setup_ghcb ())) +sev_es_terminate (GHCB_SEV_ES_REASON_GENERAL_REQUEST ); + +vc_ghcb_invalidate (boot_ghcb);
Newline here...
arch/x86/include/asm/sev-es.h static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 241
Quoted 2 lines
arch/x86/include/asm/sev-es.h +2
-0+ result =vc_init_em_ctxt (&ctxt, regs, exit_code); +
... remove that one here.
Quoted 2 lines
arch/x86/include/asm/sev-es.h +2
-0+if (result ==ES_OK ) + result =vc_handle_exitcode (&ctxt, boot_ghcb, exit_code);
...
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 42/75] x86/sev-es: Setup GHCB based boot #VC handler
Quoted 8 lines · Borislav Petkov
Quoted 7 lines · Joerg Roedel
arch/x86/include/asm/sev-es.h +0
-0@@ -74,5 +74,6 @@ static inline u64 lower_bits(u64 val, unsigned int bits) }
Quoted 7 lines
arch/x86/include/asm/sev-es.h static inline u64 lower_bits(u64 val, unsigned int bits) · line 77
Quoted 2 lines
arch/x86/include/asm/sev-es.h +1
-0extern void vc_no_ghcb (void ); +extern bool vc_boot_ghcb (struct pt_regs *regs);Those function names need verbs:
handle_vc_no_ghcb
handle_vc_boot_ghcb
This are IDT entry points and the names above follow the convention for them, like e.g. 'page_fault', 'nmi' or 'general_protection'. Should I still add the verbs or just add a comment explaining what those symbols are?
Quoted 7 lines
There's already another
sev_es_setup_ghcb()in compressed/. All those functions with the same name are just confusion waiting to happen. Let's prepend the ones in compressed/ with "early_" or so, so that their names are at least different even if they're in two different files with the same name.This way you know at least which function is used in which boot stages.
Okay, will see what can be changed. Some functions are part of the interface for sev-es-shared.c and need to have the same names, but sev_es_setup_ghcb() can be named differently.
Quoted 3 lines
arch/x86/include/asm/sev-es.h static inline u64 lower_bits(u64 val, unsigned int bits) · line 78
Quoted 1 line
arch/x86/include/asm/sev-es.h +1
-0+static void __init vc_early_vc_forward_exception (struct es_em_ctxt *ctxt)That second "vc" looks redundant.
Heh, search and replace artifact :) Fixed now.
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 42/75] x86/sev-es: Setup GHCB based boot #VC handler
Quoted 4 lines · Joerg Roedel
This are IDT entry points and the names above follow the convention for them, like e.g. '
page_fault', 'nmi' or 'general_protection'. Should I still add the verbs or just add a comment explaining what those symbols are?
Hmmkay, I see vc_no_ghcb doing
call do_vc_no_ghcb
and that's setup in early_idt_setup().
vc_boot_ghcb(), OTOH, is called by do_early_exception() only so that one could be called handle_vc_boot_ghcb(), no? I.e., I don't see it being an IDT entry point.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 42/75] x86/sev-es: Setup GHCB based boot #VC handler
Quoted 9 lines · Borislav Petkov
Hmmkay, I see
vc_no_ghcbdoingcall
do_vc_no_ghcband that's setup in
early_idt_setup().
vc_boot_ghcb(), OTOH, is called bydo_early_exception()only so that one could be calledhandle_vc_boot_ghcb(), no? I.e., I don't see it being an IDT entry point.
Right, I renamed it to handle_vc_boot_ghcb().
Joerg
PATCH v3 43/75 x86/sev-es: Setup per-cpu GHCBs for the runtime handler
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
The runtime handler needs a GHCB per CPU. Set them up and map them unencrypted.
--- arch/x86/include/asm/mem_encrypt.h | 2 ++ arch/x86/kernel/sev-es.c | 56 +++++++++++++++++++++++++++++- arch/x86/kernel/traps.c | 3 ++ 3 files changed, 60 insertions(+), 1 deletion(-)
arch/x86/include/asm/mem_encrypt.h +2 -0
@@ -48,6 +48,7 @@ int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size);void __init mem_encrypt_init (void );void __init mem_encrypt_free_decrypted_mem (void ); +void __init sev_es_init_vc_handling (void );bool sme_active (void );bool sev_active (void );bool sev_es_active (void );
@@ -71,6 +72,7 @@ static inline void __init sme_early_init(void) { }static inline void __init sme_encrypt_kernel (struct boot_params *bp) { }static inline void __init sme_enable (struct boot_params *bp) { } +static inline void sev_es_init_vc_handling (void ) { }static inline bool sme_active (void ) {return false; }static inline bool sev_active (void ) {return false; }static inline bool sev_es_active (void ) {return false; }
arch/x86/kernel/sev-es.c +55 -1
75 lines not on this page · raw message
arch/x86/kernel/traps.c +3 -0
@@ -965,6 +965,9 @@ void __init trap_init(void)/* Init cpu_entry_area before IST entries are set up */ setup_cpu_entry_areas (); +/* Init GHCB memory pages when running as an SEV-ES guest */ +sev_es_init_vc_handling (); +idt_setup_traps ();/*
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 43/75] x86/sev-es: Setup per-cpu GHCBs for the runtime handler
Quoted 6 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +2
-0@@ -198,6 +210,48 @@ static bool __init sev_es_setup_ghcb(void)return true; } +static void __init sev_es_alloc_runtime_data (int cpu) +{
arch/x86/kernel/sev-es.c static bool __init sev_es_setup_ghcb(void) · line 224
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+struct sev_es_runtime_data *data; + + data =memblock_alloc (sizeof (*data),PAGE_SIZE ); +if (!data) +panic ("Can't allocate SEV-ES runtime data" ); + +per_cpu (runtime_data, cpu) = data; +} + +static void __init sev_es_init_ghcb (int cpu)
Since those are static functions, I'd drop the "sev_es_" prefix from the name for better readability. Because otherwise the whole file is a sea of "sev_es_"-prefixed identifiers which you need to read until the end to know what they are.
arch/x86/kernel/sev-es.c static bool __init sev_es_setup_ghcb(void) · line 234
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+{ +struct sev_es_runtime_data *data; +int err; + + data =per_cpu (runtime_data, cpu); + + err =early_set_memory_decrypted ((unsigned long )&data->ghcb_page, +sizeof (data->ghcb_page)); +if (err) +panic ("Can not map GHCBs unencrypted" );
"Error mapping ..."
Quoted 3 lines
arch/x86/kernel/sev-es.c +3
-0+ +memset (&data->ghcb_page,0 ,sizeof (data->ghcb_page)); +}
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 44/75 x86/sev-es: Allocate and Map IST stacks for #VC handler
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Allocate and map enough stacks for the #VC handler to support sufficient levels of nesting and the NMI-in-#VC scenario.
Also setup the IST entrys for the #VC handler on all CPUs because #VC needs to work before cpu_init() has set up the per-cpu TSS.
--- arch/x86/include/asm/cpu_entry_area.h | 61 +++++++++++++++++++++++++++ arch/x86/include/asm/page_64_types.h | 1 + arch/x86/kernel/cpu/common.c | 1 + arch/x86/kernel/sev-es.c | 40 ++++++++++++++++++ 4 files changed, 103 insertions(+)
arch/x86/include/asm/cpu_entry_area.h +61 -0
@@ -64,6 +64,61 @@ enum exception_stack_ordering {#define CEA_ESTACK_PAGES \ (sizeof(struct cea_exception_stacks) / PAGE_SIZE) +/* +* VC Handler IST Stacks +* +* The IST stacks for the #VC handler are only allocated when SEV-ES is active, +* so they are not part of 'struct exception_stacks'. +* +* The VC handler uses shift_ist so that #VC can be nested. Nesting happens for +* example when the #VC handler has to call printk in the case of and error or +* when emulating 'movs' instructions. +* +* NMIs are another special case which can cause nesting of #VC handlers. The +* do_nmi() code path can cause #VC, e.g. for RDPMC. An NMI can also hit in +* the time window when the #VC handler is raised but before it has shifted its +* IST entry. To make sure any #VC raised from the NMI code path uses a new +* stack, the NMI handler unconditionally shifts the #VC handlers IST entry. +* This can cause one IST stack for #VC to be omitted. +* +* To support sufficient levels of nesting for the #VC handler, make the number +* of nesting levels configurable. It is currently set to 5 to support this +* scenario: +* +* #VC - IST stack 4, IST entry already shifted to 3 +* +* -> NMI - shifts #VC IST entry to 2 +* +* -> #VC(RDPMC) - shifts #VC IST to 1, something goes wrong, print +* an error message +* +* -> #VC(printk) - shifts #VC IST entry to 0, output driver +* uses 'movs' +* +* -> #VC(movs) - shifts IST to unmapped stack, further #VCs will +* cause #DF +* +*/ +#define N_VC_STACKS 5 + +#define VC_STACK_MEMBERS (guardsize, holesize) \ + char hole[holesize]; \ + struct { \ + char guard[guardsize]; \ + char stack[EXCEPTION_STKSZ]; \ + } stacks[N_VC_STACKS]; \ + char top_guard[guardsize]; \ + +/* Physical storage */ +struct vmm_exception_stacks { + VC_STACK_MEMBERS(0, 0) +}; + +/* Mapping in cpu_entry_area */ +struct cea_vmm_exception_stacks { + VC_STACK_MEMBERS(PAGE_SIZE, EXCEPTION_STKSZ) +}; +#endif #ifdef CONFIG_X86_32
@@ -110,6 +165,12 @@ struct cpu_entry_area {* Exception stacks used for IST entries with guard pages. */ struct cea_exception_stacks estacks; + +/* +* IST Exception stacks for VC handler - Only allocated and mapped when +* SEV-ES is active. +*/ +struct cea_vmm_exception_stacks vc_stacks;#endif /* * Per CPU debug store for Intel performance monitoring. Wastes a
arch/x86/include/asm/page_64_types.h +1 -0
@@ -28,6 +28,7 @@#define IST_INDEX_NMI 1#define IST_INDEX_DB 2#define IST_INDEX_MCE 3 +#define IST_INDEX_VC 4/* * Set __PAGE_OFFSET to the most negative possible address +
arch/x86/kernel/cpu/common.c +1 -0
@@ -1808,6 +1808,7 @@ static inline void tss_setup_ist(struct tss_struct *tss) tss->x86_tss.ist[IST_INDEX_NMI ] =__this_cpu_ist_top_va (NMI ); tss->x86_tss.ist[IST_INDEX_DB ] =__this_cpu_ist_top_va (DB ); tss->x86_tss.ist[IST_INDEX_MCE ] =__this_cpu_ist_top_va (MCE ); + /* IST_INDEX_VC already set up for all CPUs during early boot */ }#else /* CONFIG_X86_64 */
arch/x86/kernel/sev-es.c +40 -0
@@ -17,6 +17,7 @@#include <linux/kernel.h> #include <linux/mm.h> +#include <asm/cpu_entry_area.h> #include <asm/trap_defs.h> #include <asm/sev-es.h> #include <asm/insn-eval.h>
@@ -37,6 +38,9 @@ static struct ghcb __initdata *boot_ghcb;/* #VC handler runtime per-cpu data */ struct sev_es_runtime_data {struct ghcb ghcb_page; + +/* Physical storage for the per-cpu IST stacks of the #VC handler */ +struct vmm_exception_stacks vc_stacks__aligned(PAGE_SIZE) ; }; static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
@@ -236,11 +240,46 @@ static void __init sev_es_init_ghcb(int cpu)memset (&data->ghcb_page,0 ,sizeof (data->ghcb_page)); } +static void __init sev_es_setup_vc_stack (int cpu) +{ +struct vmm_exception_stacks *stack; +struct sev_es_runtime_data *data; +struct cpu_entry_area *cea; +struct tss_struct *tss; +unsigned long size; +char *first_stack; +int i; + + data =per_cpu (runtime_data, cpu); + stack = &data->vc_stacks; + cea =get_cpu_entry_area (cpu); + +/* Map the stacks to the cpu_entry_area */ +for (i =0 ; i <N_VC_STACKS ; i++) { +void *vaddr = cea->vc_stacks.stacks[i].stack; +phys_addr_t pa =__pa (stack->stacks[i].stack); + +cea_set_pte (vaddr, pa,PAGE_KERNEL ); + } + +/* +* The #VC handler IST stack is needed in secondary CPU bringup before +* cpu_init() had a chance to setup the rest of the TSS. So setup the +* #VC handlers stack pointer up here for all CPUs +*/ + first_stack = cea->vc_stacks.stacks[N_VC_STACKS -1 ].stack; + size =sizeof (cea->vc_stacks.stacks[N_VC_STACKS -1 ].stack); + tss =per_cpu_ptr (&cpu_tss_rw, cpu); + + tss->x86_tss.ist[IST_INDEX_VC ] = (unsigned long )first_stack + size; +} +void __init sev_es_init_vc_handling (void ) {int cpu;BUILD_BUG_ON ((offsetof(struct sev_es_runtime_data , ghcb_page) %PAGE_SIZE ) !=0 ); +BUILD_BUG_ON ((offsetof(struct sev_es_runtime_data , vc_stacks) %PAGE_SIZE ) !=0 );if (!sev_es_active ())return ;
@@ -249,6 +288,7 @@ void __init sev_es_init_vc_handling(void)for_each_possible_cpu (cpu) {sev_es_alloc_runtime_data (cpu);sev_es_init_ghcb (cpu); +sev_es_setup_vc_stack (cpu); } }
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 44/75] x86/sev-es: Allocate and Map IST stacks for #VC handler
Dropping [email protected] from Cc from now on because of some microsloth mail rule not delivering my mails.
Quoted 6 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
Allocate and map enough stacks for the #VC handler to support sufficient levels of nesting and the NMI-in-#VC scenario.
Also setup the IST entrys for the #VC handler on all CPUs because #VC
"entries"
Quoted 1 line
needs to work before
cpu_init()has set up the per-cpu TSS.
Add a sentence to the commit message pointing to that "VC Handler IST Stacks" comment in the code explaining the justification for the need for IST stacks and the nesting.
Quoted 15 lines
Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/include/asm/cpu_entry_area.h | 61 +++++++++++++++++++++++++++ arch/x86/include/asm/page_64_types.h | 1 + arch/x86/kernel/cpu/common.c | 1 + arch/x86/kernel/sev-es.c | 40 ++++++++++++++++++ 4 files changed, 103 insertions(+)arch/x86/include/asm/cpu_entry_area.h +0
-0@@ -64,6 +64,61 @@ enum exception_stack_ordering {#define CEA_ESTACK_PAGES \
arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 74
Quoted 10 lines
arch/x86/include/asm/cpu_entry_area.h +8
-0(sizeof (struct cea_exception_stacks ) /PAGE_SIZE ) +/* +* VC Handler IST Stacks +* +* The IST stacks for the #VC handler are only allocated when SEV-ES is active, +* so they are not part of 'struct exception_stacks'. +* +* The VC handler uses shift_ist so that #VC can be nested. Nesting happens for +* example when the #VC handler has to call printk in the case of and error or
"an"
Quoted 36 lines
arch/x86/include/asm/cpu_entry_area.h +36
-0+* when emulating 'movs' instructions. +* +* NMIs are another special case which can cause nesting of #VC handlers. The +* do_nmi() code path can cause #VC, e.g. for RDPMC. An NMI can also hit in +* the time window when the #VC handler is raised but before it has shifted its +* IST entry. To make sure any #VC raised from the NMI code path uses a new +* stack, the NMI handler unconditionally shifts the #VC handlers IST entry. +* This can cause one IST stack for #VC to be omitted. +* +* To support sufficient levels of nesting for the #VC handler, make the number +* of nesting levels configurable. It is currently set to 5 to support this +* scenario: +* +* #VC - IST stack 4, IST entry already shifted to 3 +* +* -> NMI - shifts #VC IST entry to 2 +* +* -> #VC(RDPMC) - shifts #VC IST to 1, something goes wrong, print +* an error message +* +* -> #VC(printk) - shifts #VC IST entry to 0, output driver +* uses 'movs' +* +* -> #VC(movs) - shifts IST to unmapped stack, further #VCs will +* cause #DF +* +*/ +#define N_VC_STACKS 5 + +#define VC_STACK_MEMBERS (guardsize, holesize) \ + char hole[holesize]; \ + struct { \ + char guard[guardsize]; \ + char stack[EXCEPTION_STKSZ]; \ + } stacks[N_VC_STACKS]; \ + char top_guard[guardsize]; \
arch/x86/include/asm/cpu_entry_area.h enum exception_stack_ordering { · line 120
Quoted 10 lines
arch/x86/include/asm/cpu_entry_area.h +10
-0+ +/* Physical storage */ +struct vmm_exception_stacks { + VC_STACK_MEMBERS(0, 0) +}; + +/* Mapping in cpu_entry_area */ +struct cea_vmm_exception_stacks { + VC_STACK_MEMBERS(PAGE_SIZE, EXCEPTION_STKSZ) +};
All those things should be under an CONFIG_AMD_MEM_ENCRYPT ifdeffery.
Quoted 5 lines
arch/x86/include/asm/cpu_entry_area.h +1
-0+#endif #ifdef CONFIG_X86_32
arch/x86/include/asm/cpu_entry_area.h struct cpu_entry_area { · line 173
Quoted 9 lines
arch/x86/include/asm/cpu_entry_area.h +6
-0@@ -110,6 +165,12 @@ struct cpu_entry_area {* Exception stacks used for IST entries with guard pages. */ struct cea_exception_stacks estacks; + +/* +* IST Exception stacks for VC handler - Only allocated and mapped when +* SEV-ES is active. +*/ +struct cea_vmm_exception_stacks vc_stacks;
Ditto.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 45/75 x86/dumpstack/64: Handle #VC exception stacks
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Make the stack unwinder aware of the IST stacks for the #VC exception handler.
--- arch/x86/include/asm/cpu_entry_area.h | 1 + arch/x86/include/asm/sev-es.h | 13 ++++++++ arch/x86/include/asm/stacktrace.h | 4 +++ arch/x86/kernel/dumpstack_64.c | 47 +++++++++++++++++++++++++++ arch/x86/kernel/sev-es.c | 26 +++++++++++++++ 5 files changed, 91 insertions(+)
arch/x86/include/asm/cpu_entry_area.h +1 -0
@@ -192,6 +192,7 @@ struct cpu_entry_area { DECLARE_PER_CPU(struct cpu_entry_area *, cpu_entry_area); DECLARE_PER_CPU(struct cea_exception_stacks *, cea_exception_stacks); +DECLARE_PER_CPU(struct cea_vmm_exception_stacks *, cea_vmm_exception_stacks);extern void setup_cpu_entry_areas (void );extern void cea_set_pte (void *cea_vaddr,phys_addr_t pa,pgprot_t flags);
arch/x86/include/asm/sev-es.h +13 -0
@@ -9,6 +9,8 @@#define __ASM_ENCRYPTED_STATE_H#include <linux/types.h> + +#include <asm/stacktrace.h> #include <asm/insn.h> #define GHCB_SEV_INFO 0x001UL
@@ -76,4 +78,15 @@ static inline u64 lower_bits(u64 val, unsigned int bits)extern void vc_no_ghcb (void );extern bool vc_boot_ghcb (struct pt_regs *regs); +enum stack_type ; + +#ifdef CONFIG_AMD_MEM_ENCRYPT +const char *vc_stack_name (enum stack_type type); +#else /* CONFIG_AMD_MEM_ENCRYPT */ +static inline const char *vc_stack_name (enum stack_type type) +{ +return NULL ; +} +#endif /* CONFIG_AMD_MEM_ENCRYPT*/ +#endif
arch/x86/include/asm/stacktrace.h +4 -0
@@ -21,6 +21,10 @@ enum stack_type {STACK_TYPE_ENTRY ,STACK_TYPE_EXCEPTION ,STACK_TYPE_EXCEPTION_LAST =STACK_TYPE_EXCEPTION +N_EXCEPTION_STACKS -1 , +#ifdef CONFIG_X86_64 +STACK_TYPE_VC , + STACK_TYPE_VC_LAST = STACK_TYPE_VC + N_VC_STACKS - 1, +#endif }; struct stack_info {
arch/x86/kernel/dumpstack_64.c +47 -0
71 lines not on this page · raw message
arch/x86/kernel/sev-es.c +26 -0
@@ -18,6 +18,7 @@#include <linux/mm.h> #include <asm/cpu_entry_area.h> +#include <asm/stacktrace.h> #include <asm/trap_defs.h> #include <asm/sev-es.h> #include <asm/insn-eval.h>
@@ -34,6 +35,9 @@ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);* cleared */ static struct ghcb __initdata *boot_ghcb; +DEFINE_PER_CPU(struct cea_vmm_exception_stacks *, cea_vmm_exception_stacks); + +static char vc_stack_names[N_VC_STACKS ][8 ];/* #VC handler runtime per-cpu data */ struct sev_es_runtime_data {
@@ -240,6 +244,16 @@ static void __init sev_es_init_ghcb(int cpu)memset (&data->ghcb_page,0 ,sizeof (data->ghcb_page)); } +static void __init init_vc_stack_names (void ) +{ +int i; + +for (i =0 ; i <N_VC_STACKS ; i++) { +snprintf (vc_stack_names[i],sizeof (vc_stack_names[i]), +"#VC%d" , i); + } +} +static void __init sev_es_setup_vc_stack (int cpu) {struct vmm_exception_stacks *stack;
@@ -272,6 +286,8 @@ static void __init sev_es_setup_vc_stack(int cpu) tss =per_cpu_ptr (&cpu_tss_rw, cpu); tss->x86_tss.ist[IST_INDEX_VC ] = (unsigned long )first_stack + size; + +per_cpu (cea_vmm_exception_stacks, cpu) = &cea->vc_stacks; }void __init sev_es_init_vc_handling (void )
@@ -290,6 +306,16 @@ void __init sev_es_init_vc_handling(void)sev_es_init_ghcb (cpu);sev_es_setup_vc_stack (cpu); } + +init_vc_stack_names (); +} + +const char *vc_stack_name (enum stack_type type) +{ +if (type <STACK_TYPE_VC || type >STACK_TYPE_VC_LAST ) +return NULL ; + +return vc_stack_names[type -STACK_TYPE_VC ]; }static void __init vc_early_vc_forward_exception (struct es_em_ctxt *ctxt)
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 45/75] x86/dumpstack/64: Handle #VC exception stacks
arch/x86/include/asm/stacktrace.h enum stack_type { · line 24
Quoted 9 lines · Joerg Roedel
arch/x86/include/asm/stacktrace.h +1
-0@@ -21,6 +21,10 @@ enum stack_type {STACK_TYPE_ENTRY ,STACK_TYPE_EXCEPTION ,STACK_TYPE_EXCEPTION_LAST =STACK_TYPE_EXCEPTION +N_EXCEPTION_STACKS -1 , +#ifdef CONFIG_X86_64
CONFIG_AMD_MEM_ENCRYPT
Quoted 19 lines
arch/x86/include/asm/stacktrace.h +3
-0+ STACK_TYPE_VC, + STACK_TYPE_VC_LAST = STACK_TYPE_VC + N_VC_STACKS - 1, +#endif }; struct stack_info {arch/x86/kernel/dumpstack_64.c +1
-0@@ -18,6 +18,7 @@#include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> +#include <asm/sev-es.h> static const char *const exception_stack_names[] = { [ESTACK_DF ] ="#DF" ,
arch/x86/kernel/dumpstack_64.c const char *stack_type_name(enum stack_type type) · line 51
Quoted 4 lines
arch/x86/kernel/dumpstack_64.c +1
-0@@ -47,6 +48,9 @@ const char *stack_type_name(enum stack_type type)if (type >=STACK_TYPE_EXCEPTION && type <=STACK_TYPE_EXCEPTION_LAST )return exception_stack_names[type -STACK_TYPE_EXCEPTION ]; +if (type >=STACK_TYPE_VC && type <=STACK_TYPE_VC_LAST )
That test can be inside vc_stack_name() so that it gets optimized away for !CONFIG_AMD_MEM_ENCRYPT and there's no need for ifdeffery.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 46/75 x86/sev-es: Shift #VC IST Stack in nmi_enter()/nmi_exit()
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
When an NMI hits in the #VC handler entry code before it shifted its IST entry, then any subsequent #VC exception in the NMI code-path will overwrite the interrupted #VC handlers stack.
Make sure this doesn't happen by explicitly shifting the #VC IST entry in the NMI handler for the time in can cause #VC exceptions.
--- arch/x86/include/asm/hardirq.h | 14 ++++++++++++++ arch/x86/include/asm/sev-es.h | 2 ++ arch/x86/kernel/asm-offsets_64.c | 1 + arch/x86/kernel/nmi.c | 1 + arch/x86/kernel/sev-es.c | 21 +++++++++++++++++++++ 5 files changed, 39 insertions(+)
arch/x86/include/asm/hardirq.h +14 -0
@@ -80,4 +80,18 @@ static inline bool kvm_get_cpu_l1tf_flush_l1d(void)static inline void kvm_set_cpu_l1tf_flush_l1d (void ) { }#endif /* IS_ENABLED(CONFIG_KVM_INTEL) */ +#ifdef CONFIG_AMD_MEM_ENCRYPT + +#define arch_nmi_enter () \ + do { \ + sev_es_nmi_enter(); \ + } while (0) + +#define arch_nmi_exit () \ + do { \ + sev_es_nmi_exit(); \ + } while (0) + +#endif +#endif /* _ASM_X86_HARDIRQ_H */
arch/x86/include/asm/sev-es.h +2 -0
@@ -82,6 +82,8 @@ enum stack_type;#ifdef CONFIG_AMD_MEM_ENCRYPT const char *vc_stack_name (enum stack_type type); +void sev_es_nmi_enter (void ); +void sev_es_nmi_exit (void );#else /* CONFIG_AMD_MEM_ENCRYPT */ static inline const char *vc_stack_name (enum stack_type type) {
arch/x86/kernel/asm-offsets_64.c +1 -0
@@ -60,6 +60,7 @@ int main(void)OFFSET (TSS_ist, tss_struct, x86_tss.ist);DEFINE (DB_STACK_OFFSET , offsetof(struct cea_exception_stacks , DB_stack) - offsetof(struct cea_exception_stacks , DB1_stack)); +DEFINE (VC_STACK_OFFSET ,sizeof (((struct cea_vmm_exception_stacks *)0 )->stacks[0 ]));BLANK ();#ifdef CONFIG_STACKPROTECTOR
arch/x86/kernel/nmi.c +1 -0
@@ -37,6 +37,7 @@#include <asm/reboot.h> #include <asm/cache.h> #include <asm/nospec-branch.h> +#include <asm/sev-es.h> #define CREATE_TRACE_POINTS #include <trace/events/nmi.h>
arch/x86/kernel/sev-es.c +21 -0
@@ -17,6 +17,7 @@#include <linux/kernel.h> #include <linux/mm.h> +#include <generated/asm-offsets.h> #include <asm/cpu_entry_area.h> #include <asm/stacktrace.h> #include <asm/trap_defs.h>
@@ -49,6 +50,26 @@ struct sev_es_runtime_data { static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data); +/* +* Shift/Unshift the IST entry for the #VC handler during +* nmi_enter()/nmi_exit(). This is needed when an NMI hits in the #VC handlers +* entry code before it has shifted its IST entry. This way #VC exceptions +* caused by the NMI handler are guaranteed to use a new stack. +*/ +void sev_es_nmi_enter (void ) +{ +struct tss_struct *tss =this_cpu_ptr (&cpu_tss_rw); + + tss->x86_tss.ist[IST_INDEX_VC ] -=VC_STACK_OFFSET ; +} + +void sev_es_nmi_exit (void ) +{ +struct tss_struct *tss =this_cpu_ptr (&cpu_tss_rw); + + tss->x86_tss.ist[IST_INDEX_VC ] +=VC_STACK_OFFSET ; +} +/* Needed in vc_early_vc_forward_exception */ void do_early_exception (struct pt_regs *regs,int trapnr);
Signature · 1 line
2.17.1
PATCH v3 47/75 x86/sev-es: Add Runtime #VC Exception Handler
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Add the handler for #VC exceptions invoked at runtime.
--- arch/x86/entry/entry_64.S | 4 + arch/x86/include/asm/traps.h | 7 ++ arch/x86/kernel/idt.c | 4 +- arch/x86/kernel/sev-es.c | 167 ++++++++++++++++++++++++++++++++++- 4 files changed, 180 insertions(+), 2 deletions(-)
arch/x86/entry/entry_64.S +4 -0
@@ -1210,6 +1210,10 @@ idtentry async_page_fault do_async_page_fault has_error_code=1 read_cr2=1 idtentry machine_check do_mce has_error_code=0 paranoid=1 #endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +idtentry vmm_communication do_vmm_communication has_error_code=1 paranoid=1 shift_ist=IST_INDEX_VC ist_offset=VC_STACK_OFFSET +#endif + /* * Save all registers in pt_regs, and switch gs if needed. * Use slow, but surefire "are we in kernel?" check.
arch/x86/include/asm/traps.h +7 -0
@@ -35,6 +35,9 @@ asmlinkage void alignment_check(void);#ifdef CONFIG_X86_MCE asmlinkage void machine_check(void);#endif /* CONFIG_X86_MCE */ +#ifdef CONFIG_AMD_MEM_ENCRYPT +asmlinkage void vmm_communication(void); +#endif asmlinkage void simd_coprocessor_error(void);#if defined(CONFIG_X86_64 ) && defined(CONFIG_XEN_PV )
@@ -83,6 +86,10 @@ dotraplinkage void do_spurious_interrupt_bug(struct pt_regs *regs, long error_co dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code); dotraplinkage void do_alignment_check(struct pt_regs *regs, long error_code); dotraplinkage void do_simd_coprocessor_error(struct pt_regs *regs, long error_code); +#ifdef CONFIG_AMD_MEM_ENCRYPT +dotraplinkage void do_vmm_communication_error(struct pt_regs *regs, +long error_code); +#endif #ifdef CONFIG_X86_32 dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code);#endif
arch/x86/kernel/idt.c +3 -1
@@ -88,7 +88,6 @@ static const __initconst struct idt_data def_idts[] = {#ifdef CONFIG_X86_MCE INTG(X86_TRAP_MC, &machine_check), #endif - SYSG(X86_TRAP_OF, overflow),#if defined(CONFIG_IA32_EMULATION ) SYSG(IA32_SYSCALL_VECTOR, entry_INT80_compat),
@@ -185,6 +184,9 @@ static const __initconst struct idt_data ist_idts[] = {#ifdef CONFIG_X86_MCE ISTG(X86_TRAP_MC, &machine_check, IST_INDEX_MCE),#endif +#ifdef CONFIG_AMD_MEM_ENCRYPT + ISTG(X86_TRAP_VC, vmm_communication, IST_INDEX_VC), +#endif };/*
arch/x86/kernel/sev-es.c +166 -1
207 lines not on this page · raw message
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
Quoted 22 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +2
-1@@ -10,6 +10,7 @@#include <linux/sched/debug.h> /* For show_regs() */ #include <linux/percpu-defs.h> #include <linux/mem_encrypt.h> +#include <linux/lockdep.h> #include <linux/printk.h> #include <linux/mm_types.h> #include <linux/set_memory.h> @@ -25,7 +26,7 @@#include <asm/insn-eval.h> #include <asm/fpu/internal.h> #include <asm/processor.h> -#include <asm/trap_defs.h> +#include <asm/traps.h> #include <asm/svm.h> /* For early boot hypervisor communication in SEV-ES enabled guests */
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 51
Quoted 5 lines
arch/x86/kernel/sev-es.c +2
-0@@ -46,10 +47,26 @@ struct sev_es_runtime_data {/* Physical storage for the per-cpu IST stacks of the #VC handler */ struct vmm_exception_stacks vc_stacks__aligned(PAGE_SIZE) ; + +/* Reserve on page per CPU as backup storage for the unencrypted GHCB */
one
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 52
Quoted 1 line
arch/x86/kernel/sev-es.c +1
-0+struct ghcb backup_ghcb;
I could use some text explaining what those backups are for?
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 59
Quoted 6 lines
arch/x86/kernel/sev-es.c +6
-0+/* +* Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions. +* There is no need for it to be atomic, because nothing is written to +* the GHCB between the read and the write of ghcb_active. So it is safe +* to use it when a nested #VC exception happens before the write. +*/
Looks liks that is that text... support for nested #VC exceptions. I'm sure this has come up already but why do we even want to support nested #VCs? IOW, can we do without them first or are they absolutely necessary?
I'm guessing VC exceptions inside the VC handler but what are the sensible use cases?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
Quoted 13 lines · Borislav Petkov
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 59
Quoted 6 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +6
-0+/* +* Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions. +* There is no need for it to be atomic, because nothing is written to +* the GHCB between the read and the write of ghcb_active. So it is safe +* to use it when a nested #VC exception happens before the write. +*/ Looks liks that is that text... support for nested #VC exceptions. I'm sure this has come up already but why do we even want to support nested #VCs? IOW, can we do without them first or are they absolutely necessary?
Quoted 2 lines
I'm guessing VC exceptions inside the VC handler but what are the sensible use cases?
The most important use-case is #VC->NMI->#VC. When an NMI hits while the #VC handler uses the GHCB and the NMI handler causes another #VC, then the contents of the GHCB needs to be backed up, so that it doesn't destroy the GHCB contents of the first #VC handling path.
Regards,
Joerg
Re: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
Quoted 14 lines · Joerg Roedel
Quoted 13 lines · Borislav Petkov
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 59
Quoted 6 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +6
-0+/* +* Mark the per-cpu GHCBs as in-use to detect nested #VC exceptions. +* There is no need for it to be atomic, because nothing is written to +* the GHCB between the read and the write of ghcb_active. So it is safe +* to use it when a nested #VC exception happens before the write. +*/ Looks liks that is that text... support for nested #VC exceptions. I'm sure this has come up already but why do we even want to support nested #VCs? IOW, can we do without them first or are they absolutely necessary?
Quoted 7 lines
Quoted 2 lines
I'm guessing VC exceptions inside the VC handler but what are the sensible use cases?
The most important use-case is #VC->NMI->#VC. When an NMI hits while the #VC handler uses the GHCB and the NMI handler causes another #VC, then the contents of the GHCB needs to be backed up, so that it doesn't destroy the GHCB contents of the first #VC handling path.
Isn't it possible for the #VC handler to hit a #PF, e.g. on copy_from_user() in insn_fetch_from_user()? If that happens, what prevents the #PF handler from hitting a #VC? AIUI, do_vmm_communication() panics if the backup GHCB is already in use, e.g. #VC->#PF->#VC->NMI->#VC would be fatal.
Re: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
Quoted 4 lines · Sean Christopherson
Isn't it possible for the #VC handler to hit a #PF, e.g. on
copy_from_user()ininsn_fetch_from_user()? If that happens, what prevents the #PF handler from hitting a #VC? AIUI,do_vmm_communication()panics if the backup GHCB is already in use, e.g. #VC->#PF->#VC->NMI->#VC would be fatal.
This would be possible, if the #PF handler is able to trigger a #VC. But I am not sure how it could, except when it has to call printk.
If #PF can trigger a #VC, then a fix is to further limit the time the GHCB is active, and make sure there are not faults of any kind when it actually is active. Then the only vector to care about is NMI. I will look into that.
Regards,
Joerg
Borislav Petkov
maintainerRe: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
In reply to Joerg RoedelQuoted 4 lines · Joerg Roedel
The most important use-case is #VC->NMI->#VC. When an NMI hits while the #VC handler uses the GHCB and the NMI handler causes another #VC, then the contents of the GHCB needs to be backed up, so that it doesn't destroy the GHCB contents of the first #VC handling path.
That's a good example, please add it to the next version of the patch, preferrably in a comment somewhere.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 47/75] x86/sev-es: Add Runtime #VC Exception Handler
In reply to Borislav PetkovQuoted 4 lines · Borislav Petkov
arch/x86/kernel/sev-es.c struct sev_es_runtime_data { · line 52
Quoted 1 line · Joerg Roedel
arch/x86/kernel/sev-es.c +1
-0+struct ghcb backup_ghcb;I could use some text explaining what those backups are for?
I added another comment above that line to better explain why it is needed.
Joerg
PATCH v3 48/75 x86/sev-es: Wire up existing #VC exit-code handlers
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Re-use the handlers for CPUID and IOIO caused #VC exceptions in the early boot handler.
--- arch/x86/kernel/sev-es-shared.c | 7 +++---- arch/x86/kernel/sev-es.c | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kernel/sev-es-shared.c +3 -4
@@ -324,8 +324,7 @@ static enum es_result vc_ioio_exitinfo(struct es_em_ctxt *ctxt, u64 *exitinfo)return ES_OK ; } -static enum es_result __maybe_unused -vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +static enum es_result vc_handle_ioio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {struct pt_regs *regs = ctxt->regs;u64 exit_info_1, exit_info_2;
@@ -421,8 +420,8 @@ vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)return ret; } -static enum es_result __maybe_unused vc_handle_cpuid (struct ghcb *ghcb, -struct es_em_ctxt *ctxt) +static enum es_result vc_handle_cpuid (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) {struct pt_regs *regs = ctxt->regs;u32 cr4 =native_read_cr4 ();
arch/x86/kernel/sev-es.c +6 -0
@@ -424,6 +424,12 @@ 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 ; +case SVM_EXIT_IOIO : + result =vc_handle_ioio (ghcb, ctxt); +break ;default :/* * Unexpected #VC exception
Signature · 1 line
2.17.1
PATCH v3 49/75 x86/sev-es: Handle instruction fetches from user-space
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
When a #VC exception is triggered by user-space the instruction decoder needs to read the instruction bytes from user addresses. Enhance vc_decode_insn() to safely fetch kernel and user instructions.
--- arch/x86/kernel/sev-es.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-)
arch/x86/kernel/sev-es.c +22 -9
@@ -165,17 +165,30 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)enum es_result ret;int res; - res =vc_fetch_insn_kernel (ctxt, buffer); -if (unlikely (res == -EFAULT )) { - ctxt->fi.vector =X86_TRAP_PF ; - ctxt->fi.error_code =0 ; - ctxt->fi.cr2 = ctxt->regs->ip; -return ES_EXCEPTION ; +if (!user_mode (ctxt->regs)) { + res =vc_fetch_insn_kernel (ctxt, buffer); +if (unlikely (res == -EFAULT )) { + ctxt->fi.vector =X86_TRAP_PF ; + ctxt->fi.error_code =0 ; + ctxt->fi.cr2 = ctxt->regs->ip; +return ES_EXCEPTION ; + } + +insn_init (&ctxt->insn, buffer,MAX_INSN_SIZE - res,1 ); +insn_get_length (&ctxt->insn); + }else { + res =insn_fetch_from_user (ctxt->regs, buffer); +if (res ==0 ) { + ctxt->fi.vector =X86_TRAP_PF ; + ctxt->fi.cr2 = ctxt->regs->ip; + ctxt->fi.error_code =X86_PF_INSTR |X86_PF_USER ; +return ES_EXCEPTION ; + } + +if (!insn_decode (ctxt->regs, &ctxt->insn, buffer, res)) +return ES_DECODE_FAILED ; } -insn_init (&ctxt->insn, buffer,MAX_INSN_SIZE - res,1 ); -insn_get_length (&ctxt->insn); - ret = ctxt->insn.immediate.got ?ES_OK :ES_DECODE_FAILED ;return ret;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 49/75] x86/sev-es: Handle instruction fetches from user-space
Quoted 16 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
When a #VC exception is triggered by user-space the instruction decoder needs to read the instruction bytes from user addresses. Enhance
vc_decode_insn()to safely fetch kernel and user instructions.Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-)arch/x86/kernel/sev-es.c +0
-0
arch/x86/kernel/sev-es.c static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) · line 169
Quoted 5 lines
arch/x86/kernel/sev-es.c +0
-2@@ -165,17 +165,30 @@ static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)enum es_result ret;int res; - res =vc_fetch_insn_kernel (ctxt, buffer); -if (unlikely (res == -EFAULT )) {
Let's also test for 0 in case the probe_read* guts get changed and start returning so other errval besides -EFAULT.
arch/x86/kernel/sev-es.c static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) · line 168
Quoted 5 lines
arch/x86/kernel/sev-es.c +1
-4- ctxt->fi.vector =X86_TRAP_PF ; - ctxt->fi.error_code =0 ; - ctxt->fi.cr2 = ctxt->regs->ip; -return ES_EXCEPTION ; +if (!user_mode (ctxt->regs)) {
Flip that check so that it reads more naturally:
if (user_mode(..)
insn_fetch_from_user()
...
} else {
vc_fetch_insn_kernel()
}Quoted 6 lines
arch/x86/kernel/sev-es.c +3
-0+ res =vc_fetch_insn_kernel (ctxt, buffer); +if (unlikely (res == -EFAULT )) { + ctxt->fi.vector =X86_TRAP_PF ;
arch/x86/kernel/sev-es.c static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) · line 181
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+ ctxt->fi.error_code =0 ; + ctxt->fi.cr2 = ctxt->regs->ip; +return ES_EXCEPTION ; + } + +insn_init (&ctxt->insn, buffer,MAX_INSN_SIZE - res,1 ); +insn_get_length (&ctxt->insn); + }else { + res =insn_fetch_from_user (ctxt->regs, buffer); +if (res ==0 ) {
if (!res)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 50/75 x86/sev-es: Do not crash on #VC exceptions from user-space
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Send SIGBUS to the user-space process that caused the #VC exception instead of killing the machine. Also ratelimit the error messages so that user-space can't flood the kernel log and add a prefix the the messages printed for SEV-ES.
--- arch/x86/kernel/sev-es.c | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-)
arch/x86/kernel/sev-es.c +29 -11
@@ -7,6 +7,8 @@* Author: Joerg Roedel <[email protected]> */ +#define pr_fmt (fmt) "SEV-ES: " fmt +#include <linux/sched/debug.h> /* For show_regs() */ #include <linux/percpu-defs.h> #include <linux/mem_encrypt.h>
@@ -468,7 +470,7 @@ static void vc_forward_exception(struct es_em_ctxt *ctxt)do_invalid_op (ctxt->regs,0 );break ;default : -pr_emerg ("ERROR: Unsupported exception in #VC instruction emulation - can't continue\n" ); +pr_emerg ("Unsupported exception in #VC instruction emulation - can't continue\n" );BUG (); } }
@@ -516,16 +518,16 @@ dotraplinkage void do_vmm_communication(struct pt_regs *regs,vc_finish_insn (&ctxt);break ;case ES_UNSUPPORTED : -pr_emerg ("PANIC: Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n" , - exit_code, regs->ip); +pr_err_ratelimited ("Unsupported exit-code 0x%02lx in early #VC exception (IP: 0x%lx)\n" , + exit_code, regs->ip); goto fail;case ES_VMM_ERROR : -pr_emerg ("PANIC: Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n" , - exit_code, regs->ip); +pr_err_ratelimited ("Failure in communication with VMM (exit-code 0x%02lx IP: 0x%lx)\n" , + exit_code, regs->ip); goto fail;case ES_DECODE_FAILED : -pr_emerg ("PANIC: Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n" , - exit_code, regs->ip); +pr_err_ratelimited ("Failed to decode instruction (exit-code 0x%02lx IP: 0x%lx)\n" , + exit_code, regs->ip); goto fail;case ES_EXCEPTION :vc_forward_exception (&ctxt);
@@ -534,7 +536,7 @@ dotraplinkage void do_vmm_communication(struct pt_regs *regs,/* Nothing to do */ break ;default : -pr_emerg ("PANIC: Unknown result in %s():%d\n" , __func__, result); +pr_emerg ("Unknown result in %s():%d\n" , __func__, result);/* * Emulating the instruction which caused the #VC exception * failed - can't continue so print debug information
@@ -545,10 +547,26 @@ dotraplinkage void do_vmm_communication(struct pt_regs *regs,return ; fail: -show_regs (regs); +if (user_mode (regs)) { +/* +* Do not kill the machine if user-space triggered the +* exception. Send SIGBUS instead and let user-space deal with +* it. +*/ +force_sig_fault (SIGBUS ,BUS_OBJERR , (void __user *)0 ); + }else { +pr_emerg ("PANIC: Unhandled #VC exception in kernel space (result=%d)\n" , + result); -while (true) -halt (); +/* Show some debug info */ +show_regs (regs); + +/* Ask hypervisor to sev_es_terminate */ +sev_es_terminate (GHCB_SEV_ES_REASON_GENERAL_REQUEST ); + +/* If that fails and we get here - just panic */ +panic ("Returned from Terminate-Request to Hypervisor\n" ); + } }bool __init vc_boot_ghcb (struct pt_regs *regs)
Signature · 1 line
2.17.1
PATCH v3 51/75 x86/sev-es: Handle MMIO events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Add handler for VC exceptions caused by MMIO intercepts. These intercepts come along as nested page faults on pages with reserved bits set.
--- arch/x86/include/uapi/asm/svm.h | 5 + arch/x86/kernel/sev-es.c | 188 ++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+)
arch/x86/include/uapi/asm/svm.h +5 -0
@@ -81,6 +81,11 @@#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401#define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402 +/* SEV-ES software-defined VMGEXIT events */ +#define SVM_VMGEXIT_MMIO_READ 0x80000001 +#define SVM_VMGEXIT_MMIO_WRITE 0x80000002 +#define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff +#define SVM_EXIT_ERR -1#define SVM_EXIT_REASONS \
arch/x86/kernel/sev-es.c +188 -0
206 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v3 51/75] x86/sev-es: Handle MMIO events
Quoted 11 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Add handler for VC exceptions caused by MMIO intercepts. These intercepts come along as nested page faults on pages with reserved bits set.
Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: Adapt to VC handling framework ]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>---
...
Quoted 10 lines
arch/x86/kernel/sev-es.c +2
-0@@ -294,6 +294,25 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,return ES_EXCEPTION ; } +static phys_addr_t vc_slow_virt_to_phys (struct ghcb *ghcb,unsigned long vaddr) +{
arch/x86/kernel/sev-es.c static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, · line 308
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+unsigned long va = (unsigned long )vaddr; +unsigned int level; +phys_addr_t pa; +pgd_t *pgd; +pte_t *pte; + + pgd =pgd_offset (current->active_mm, va); + pte =lookup_address_in_pgd (pgd, va, &level); +if (!pte) +return 0 ;
'0' is a valid physical address. It happens to be reserved in the kernel thanks to L1TF, but using '0' as an error code is ugly. Not to mention none of the callers actually check the result.
Quoted 6 lines
arch/x86/kernel/sev-es.c +6
-0+ + pa = (phys_addr_t )pte_pfn (*pte) <<PAGE_SHIFT ; + pa |= va & ~page_level_mask (level); + +return pa; +}
Re: [PATCH v3 51/75] x86/sev-es: Handle MMIO events
Quoted 3 lines · Sean Christopherson
'0' is a valid physical address. It happens to be reserved in the kernel thanks to L1TF, but using '0' as an error code is ugly. Not to mention none of the callers actually check the result.
Right, I changed the function to better handle error cases and added checks to the call-sites. It looks like below now:
static bool vc_slow_virt_to_phys(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
unsigned long vaddr, phys_addr_t *paddr)
{
unsigned long va = (unsigned long)vaddr;
unsigned int level;
phys_addr_t pa;
pgd_t *pgd;
pte_t *pte;
pgd = pgd_offset(current->active_mm, va);
pte = lookup_address_in_pgd(pgd, va, &level);
if (!pte) {
ctxt->fi.vector = X86_TRAP_PF;
ctxt->fi.cr2 = vaddr;
ctxt->fi.error_code = 0;
if (user_mode(ctxt->regs))
ctxt->fi.error_code |= X86_PF_USER;
return false;
}
pa = (phys_addr_t)pte_pfn(*pte) << PAGE_SHIFT;
pa |= va & ~page_level_mask(level);
*paddr = pa;
return true;
}Borislav Petkov
maintainerRe: [PATCH v3 51/75] x86/sev-es: Handle MMIO events
In reply to Joerg RoedelQuoted 5 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +5
-0+static enum es_result vc_do_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt, +unsigned int bytes,bool read) +{ +u64 exit_code, exit_info_1, exit_info_2; +unsigned long ghcb_pa =__pa (ghcb);
arch/x86/kernel/sev-es.c static void __init vc_early_vc_forward_exception(struct es_em_ctxt *ctxt) · line 484
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+void __user *ref; + + ref =insn_get_addr_ref (&ctxt->insn, ctxt->regs); +if (ref == (void __user *)-1L ) +return ES_UNSUPPORTED ; + + exit_code = read ?SVM_VMGEXIT_MMIO_READ :SVM_VMGEXIT_MMIO_WRITE ; + + exit_info_1 =vc_slow_virt_to_phys (ghcb, (unsigned long )ref); + exit_info_2 = bytes;/* Can never be greater than 8 */
No trailing comments pls - put them over the line.
Quoted 9 lines
arch/x86/kernel/sev-es.c +9
-0+ ghcb->save.sw_scratch = ghcb_pa + offsetof(struct ghcb , shared_buffer); + +return sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, exit_info_2); +} + +static enum es_result vc_handle_mmio_twobyte_ops (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +struct insn *insn = &ctxt->insn;
arch/x86/kernel/sev-es.c static void __init vc_early_vc_forward_exception(struct es_em_ctxt *ctxt) · line 504
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+unsigned int bytes =0 ; +enum es_result ret; +int sign_byte; +long *reg_data; + +switch (insn->opcode.bytes[1 ]) { +/* MMIO Read w/ zero-extension */ +case 0xb6 : + bytes =1 ; +/* Fallthrough */
I'm guessing we're supposed to annotate it this way now:
WARNING: Prefer 'fallthrough;' over fallthrough comment #139: FILE: arch/x86/kernel/sev-es.c:504:
+ /* Fallthrough */
arch/x86/kernel/sev-es.c static void __init vc_early_vc_forward_exception(struct es_em_ctxt *ctxt) · line 514
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+case 0xb7 : +if (!bytes) + bytes =2 ; + + ret =vc_do_mmio (ghcb, ctxt, bytes, true); +if (ret) +break ; + +/* Zero extend based on operand size */ + reg_data =vc_insn_get_reg (ctxt);
That function can return NULL - you need to test reg_data. Ditto for all its invocations.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 52/75 x86/sev-es: Handle MMIO String Instructions
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add handling for emulation the MOVS instruction on MMIO regions, as done by the memcpy_toio() and memcpy_fromio() functions.
--- arch/x86/kernel/sev-es.c | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)
arch/x86/kernel/sev-es.c +78 -0
@@ -552,6 +552,74 @@ static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb,return ret; } +/* +* The MOVS instruction has two memory operands, which raises the +* problem that it is not known whether the access to the source or the +* destination caused the #VC exception (and hence whether an MMIO read +* or write operation needs to be emulated). +* +* Instead of playing games with walking page-tables and trying to guess +* whether the source or destination is an MMIO range, this code splits +* the move into two operations, a read and a write with only one +* memory operand. This will cause a nested #VC exception on the MMIO +* address which can then be handled. +* +* This implementation has the benefit that it also supports MOVS where +* source _and_ destination are MMIO regions. +* +* It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a +* rare operation. If it turns out to be a performance problem the split +* operations can be moved to memcpy_fromio() and memcpy_toio(). +*/ +static enum es_result vc_handle_mmio_movs (struct es_em_ctxt *ctxt, +unsigned int bytes) +{ +unsigned long ds_base, es_base; +unsigned char *src, *dst; +unsigned char buffer[8 ]; +enum es_result ret; +bool rep; +int off; + + ds_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_DS ); + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES ); + +if (ds_base ==-1L || es_base ==-1L ) { + ctxt->fi.vector =X86_TRAP_GP ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; + } + + src = ds_base + (unsigned char *)ctxt->regs->si; + dst = es_base + (unsigned char *)ctxt->regs->di; + + ret =vc_read_mem (ctxt, src, buffer, bytes); +if (ret !=ES_OK ) +return ret; + + ret =vc_write_mem (ctxt, dst, buffer, bytes); +if (ret !=ES_OK ) +return ret; + +if (ctxt->regs->flags &X86_EFLAGS_DF ) + off = -bytes; +else + off = bytes; + + ctxt->regs->si += off; + ctxt->regs->di += off; + + rep =insn_has_rep_prefix (&ctxt->insn); + +if (rep) + ctxt->regs->cx -=1 ; + +if (!rep || ctxt->regs->cx ==0 ) +return ES_OK ; +else +return ES_RETRY ; +} +static enum es_result vc_handle_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {
@@ -606,6 +674,16 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,memcpy (reg_data, ghcb->shared_buffer, bytes);break ; +/* MOVS instruction */ +case 0xa4 : + bytes =1 ; +/* Fallthrough */ +case 0xa5 : +if (!bytes) + bytes = insn->opnd_bytes; + + ret =vc_handle_mmio_movs (ctxt, bytes); +break ;/* Two-Byte Opcodes */ case 0x0f : ret =vc_handle_mmio_twobyte_ops (ghcb, ctxt);
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 52/75] x86/sev-es: Handle MMIO String Instructions
Quoted 16 lines · Joerg Roedel
From: Joerg Roedel <[email protected]>
Add handling for emulation the MOVS instruction on MMIO regions, as done by the
memcpy_toio()andmemcpy_fromio()functions.Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es.c | 78 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+)arch/x86/kernel/sev-es.c +0
-0@@ -552,6 +552,74 @@ static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb,return ret;
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb, · line 562
Quoted 10 lines
arch/x86/kernel/sev-es.c +8
-0} +/* +* The MOVS instruction has two memory operands, which raises the +* problem that it is not known whether the access to the source or the +* destination caused the #VC exception (and hence whether an MMIO read +* or write operation needs to be emulated). +* +* Instead of playing games with walking page-tables and trying to guess +* whether the source or destination is an MMIO range, this code splits
s/this code splits/split/
Quoted 41 lines
arch/x86/kernel/sev-es.c +41
-0+* the move into two operations, a read and a write with only one +* memory operand. This will cause a nested #VC exception on the MMIO +* address which can then be handled. +* +* This implementation has the benefit that it also supports MOVS where +* source _and_ destination are MMIO regions. +* +* It will slow MOVS on MMIO down a lot, but in SEV-ES guests it is a +* rare operation. If it turns out to be a performance problem the split +* operations can be moved to memcpy_fromio() and memcpy_toio(). +*/ +static enum es_result vc_handle_mmio_movs (struct es_em_ctxt *ctxt, +unsigned int bytes) +{ +unsigned long ds_base, es_base; +unsigned char *src, *dst; +unsigned char buffer[8 ]; +enum es_result ret; +bool rep; +int off; + + ds_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_DS ); + es_base =insn_get_seg_base (ctxt->regs,INAT_SEG_REG_ES ); + +if (ds_base ==-1L || es_base ==-1L ) { + ctxt->fi.vector =X86_TRAP_GP ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; + } + + src = ds_base + (unsigned char *)ctxt->regs->si; + dst = es_base + (unsigned char *)ctxt->regs->di; + + ret =vc_read_mem (ctxt, src, buffer, bytes); +if (ret !=ES_OK ) +return ret; + + ret =vc_write_mem (ctxt, dst, buffer, bytes); +if (ret !=ES_OK ) +return ret; +
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb, · line 613
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+if (ctxt->regs->flags &X86_EFLAGS_DF ) + off = -bytes; +else + off = bytes; + + ctxt->regs->si += off; + ctxt->regs->di += off; + + rep =insn_has_rep_prefix (&ctxt->insn); +
^ Superfluous newline.
Quoted 13 lines
arch/x86/kernel/sev-es.c +9
-0+if (rep) + ctxt->regs->cx -=1 ; + +if (!rep || ctxt->regs->cx ==0 ) +return ES_OK ; +else +return ES_RETRY ; +} +static enum es_result vc_handle_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 680
Quoted 7 lines
arch/x86/kernel/sev-es.c +4
-0@@ -606,6 +674,16 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,memcpy (reg_data, ghcb->shared_buffer, bytes);break ; +/* MOVS instruction */ +case 0xa4 : + bytes =1 ; +/* Fallthrough */
WARNING: Prefer 'fallthrough;' over fallthrough comment #120: FILE: arch/x86/kernel/sev-es.c:680:
+ /* Fallthrough */
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 53/75 x86/sev-es: Handle MSR events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by RDMSR/WRMSR instructions.
--- arch/x86/kernel/sev-es.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)
arch/x86/kernel/sev-es.c +28 -0
@@ -316,6 +316,31 @@ static phys_addr_t vc_slow_virt_to_phys(struct ghcb *ghcb, unsigned long vaddr)/* Include code shared with pre-decompression boot stage */ #include "sev-es-shared.c" +static enum es_result vc_handle_msr (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +{ +struct pt_regs *regs = ctxt->regs; +enum es_result ret; +u64 exit_info_1; + + exit_info_1 = (ctxt->insn.opcode.bytes[1 ] ==0x30 ) ?1 :0 ; + +ghcb_set_rcx (ghcb, regs->cx); +if (exit_info_1) { +ghcb_set_rax (ghcb, regs->ax); +ghcb_set_rdx (ghcb, regs->dx); + exit_info_1 =1 ; + } + + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MSR , exit_info_1,0 ); + +if ((ret ==ES_OK ) && (!exit_info_1)) { + regs->ax = ghcb->save.rax; + regs->dx = ghcb->save.rdx; + } + +return ret; +} +/* * This function runs on the first #VC exception after the kernel * switched to virtual addresses.
@@ -708,6 +733,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_IOIO : result =vc_handle_ioio (ghcb, ctxt);break ; +case SVM_EXIT_MSR : + result =vc_handle_msr (ghcb, ctxt); +break ;case SVM_EXIT_NPF : result =vc_handle_mmio (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 53/75] x86/sev-es: Handle MSR events
Quoted 18 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by RDMSR/WRMSR instructions.
Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: Adapt to #VC handling infrastructure ]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+)arch/x86/kernel/sev-es.c +0
-0
arch/x86/kernel/sev-es.c static phys_addr_t vc_slow_virt_to_phys(struct ghcb *ghcb, unsigned long vaddr) · line 324
Quoted 9 lines
arch/x86/kernel/sev-es.c +6
-0@@ -316,6 +316,31 @@ static phys_addr_t vc_slow_virt_to_phys(struct ghcb *ghcb, unsigned long vaddr)/* Include code shared with pre-decompression boot stage */ #include "sev-es-shared.c" +static enum es_result vc_handle_msr (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +{ +struct pt_regs *regs = ctxt->regs; +enum es_result ret; +u64 exit_info_1; +
A comment pls:
/* Is it a WRMSR? */
arch/x86/kernel/sev-es.c static phys_addr_t vc_slow_virt_to_phys(struct ghcb *ghcb, unsigned long vaddr) · line 331
Quoted 7 lines
arch/x86/kernel/sev-es.c +7
-0+ exit_info_1 = (ctxt->insn.opcode.bytes[1 ] ==0x30 ) ?1 :0 ; + +ghcb_set_rcx (ghcb, regs->cx); +if (exit_info_1) { +ghcb_set_rax (ghcb, regs->ax); +ghcb_set_rdx (ghcb, regs->dx); + exit_info_1 =1 ;
No need to set it again - you just did above. :)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 54/75 x86/sev-es: Handle DR7 read/write events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Add code to handle #VC exceptions on DR7 register reads and writes. This is needed early because show_regs() reads DR7 to print it out.
Under SEV-ES there is currently no support for saving/restoring the DRx registers, but software expects to be able to write to the DR7 register. For now, cache the value written to DR7 and return it on read attempts, but do not touch the real hardware DR7.
[ [email protected]: - Adapt to #VC handling framework
- Support early usage ]
--- arch/x86/kernel/sev-es.c | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+)
arch/x86/kernel/sev-es.c +85 -0
@@ -31,6 +31,8 @@#include <asm/traps.h> #include <asm/svm.h> +#define DR7_RESET_VALUE 0x400 +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);
@@ -61,6 +63,13 @@ struct sev_es_runtime_data {*/ bool ghcb_active;bool backup_ghcb_active; + +/* +* Cached DR7 value - write it on DR7 writes and return it on reads. +* That value will never make it to the real hardware DR7 as debugging +* is currently unsupported in SEV-ES guests. +*/ +unsigned long dr7; }; static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data);
@@ -492,6 +501,21 @@ static long *vc_insn_get_reg(struct es_em_ctxt *ctxt)return reg_array + offset; } +static long *vc_insn_get_rm (struct es_em_ctxt *ctxt) +{ +long *reg_array; +int offset; + + reg_array = (long *)ctxt->regs; + offset =insn_get_modrm_rm_off (&ctxt->insn, ctxt->regs); + +if (offset <0 ) +return NULL ; + + offset /=sizeof (long ); + +return reg_array + offset; +}static enum es_result vc_do_mmio (struct ghcb *ghcb,struct es_em_ctxt *ctxt,unsigned int bytes,bool read) {
@@ -720,6 +744,61 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,return ret; } +static enum es_result vc_handle_dr7_write (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +struct sev_es_runtime_data *data =this_cpu_read (runtime_data); +long val, *reg =vc_insn_get_rm (ctxt); +enum es_result ret; + +if (!reg) +return ES_DECODE_FAILED ; + + val = *reg; + +/* Upper 32 bits must be written as zeroes */ +if (val >>32 ) { + ctxt->fi.vector =X86_TRAP_GP ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; + } + +/* Clear out other reservered bits and set bit 10 */ + val = (val &0xffff23ffL ) |BIT (10 ); + +/* Early non-zero writes to DR7 are not supported */ +if (!data && (val & ~DR7_RESET_VALUE )) +return ES_UNSUPPORTED ; + +/* Using a value of 0 for ExitInfo1 means RAX holds the value */ +ghcb_set_rax (ghcb, val); + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_WRITE_DR7 ,0 ,0 ); +if (ret !=ES_OK ) +return ret; + +if (data) + data->dr7 = val; + +return ES_OK ; +} + +static enum es_result vc_handle_dr7_read (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +struct sev_es_runtime_data *data =this_cpu_read (runtime_data); +long *reg =vc_insn_get_rm (ctxt); + +if (!reg) +return ES_DECODE_FAILED ; + +if (data) + *reg = data->dr7; +else + *reg =DR7_RESET_VALUE ; + +return ES_OK ; +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -727,6 +806,12 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,enum es_result result;switch (exit_code) { +case SVM_EXIT_READ_DR7 : + result =vc_handle_dr7_read (ghcb, ctxt); +break ; +case SVM_EXIT_WRITE_DR7 : + result =vc_handle_dr7_write (ghcb, ctxt); +break ;case SVM_EXIT_CPUID : result =vc_handle_cpuid (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 54/75] x86/sev-es: Handle DR7 read/write events
Quoted 10 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +10
-0+static enum es_result vc_handle_dr7_write (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +struct sev_es_runtime_data *data =this_cpu_read (runtime_data); +long val, *reg =vc_insn_get_rm (ctxt); +enum es_result ret; + +if (!reg) +return ES_DECODE_FAILED ; +
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 766
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+ val = *reg; + +/* Upper 32 bits must be written as zeroes */ +if (val >>32 ) { + ctxt->fi.vector =X86_TRAP_GP ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; + } + +/* Clear out other reservered bits and set bit 10 */
"reserved"
Quoted 4 lines
arch/x86/kernel/sev-es.c +4
-0+ val = (val &0xffff23ffL ) |BIT (10 ); + +/* Early non-zero writes to DR7 are not supported */ +if (!data && (val & ~DR7_RESET_VALUE ))
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 780
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+return ES_UNSUPPORTED ; + +/* Using a value of 0 for ExitInfo1 means RAX holds the value */ +ghcb_set_rax (ghcb, val); + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_WRITE_DR7 ,0 ,0 ); +if (ret !=ES_OK ) +return ret; + +if (data) + data->dr7 = val;
Are we still returning ES_OK if !data?
Quoted 3 lines
arch/x86/kernel/sev-es.c +3
-0+ +return ES_OK ; +}
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 54/75] x86/sev-es: Handle DR7 read/write events
Quoted 5 lines · Borislav Petkov
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 780
Quoted 2 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +2
-0+if (data) + data->dr7 = val;Are we still returning
ES_OKif !data?
Yes, it just means we ignore DR7 writes when they happen early before runtime_data is allocated. Since the DR7 value never makes it to the hardware register anyway, it doesn't matter.
Joerg
PATCH v3 55/75 x86/sev-es: Handle WBINVD Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by WBINVD instructions.
--- arch/x86/kernel/sev-es.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/x86/kernel/sev-es.c +9 -0
@@ -799,6 +799,12 @@ static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,return ES_OK ; } +static enum es_result vc_handle_wbinvd (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_WBINVD ,0 ,0 ); +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -821,6 +827,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_WBINVD : + result =vc_handle_wbinvd (ghcb, ctxt); +break ;case SVM_EXIT_NPF : result =vc_handle_mmio (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 56/75 x86/sev-es: Handle RDTSC(P) Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by RDTSC and RDTSCP instructions. Also make it available in the pre-decompression stage because the KASLR code used RDTSC/RDTSCP to gather entropy and some hypervisors intercept these instructions.
[ [email protected]: - Adapt to #VC handling infrastructure
- Make it available early ]
--- arch/x86/boot/compressed/sev-es.c | 4 ++++ arch/x86/kernel/sev-es-shared.c | 23 +++++++++++++++++++++++ arch/x86/kernel/sev-es.c | 4 ++++ 3 files changed, 31 insertions(+)
arch/x86/boot/compressed/sev-es.c +4 -0
@@ -180,6 +180,10 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) goto finish;switch (exit_code) { +case SVM_EXIT_RDTSC : +case SVM_EXIT_RDTSCP : + result =vc_handle_rdtsc (boot_ghcb, &ctxt, exit_code); +break ;case SVM_EXIT_IOIO : result =vc_handle_ioio (boot_ghcb, &ctxt);break ;
arch/x86/kernel/sev-es-shared.c +23 -0
@@ -454,3 +454,26 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb,return ES_OK ; } + +static enum es_result vc_handle_rdtsc (struct ghcb *ghcb, +struct es_em_ctxt *ctxt, +unsigned long exit_code) +{ +bool rdtscp = (exit_code ==SVM_EXIT_RDTSCP ); +enum es_result ret; + + ret =sev_es_ghcb_hv_call (ghcb, ctxt, exit_code,0 ,0 ); +if (ret !=ES_OK ) +return ret; + +if (!(ghcb_is_valid_rax (ghcb) &&ghcb_is_valid_rdx (ghcb) && + (!rdtscp ||ghcb_is_valid_rcx (ghcb)))) +return ES_VMM_ERROR ; + + ctxt->regs->ax = ghcb->save.rax; + ctxt->regs->dx = ghcb->save.rdx; +if (rdtscp) + ctxt->regs->cx = ghcb->save.rcx; + +return ES_OK ; +}
arch/x86/kernel/sev-es.c +4 -0
@@ -818,6 +818,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_WRITE_DR7 : result =vc_handle_dr7_write (ghcb, ctxt);break ; +case SVM_EXIT_RDTSC : +case SVM_EXIT_RDTSCP : + result =vc_handle_rdtsc (ghcb, ctxt, exit_code); +break ;case SVM_EXIT_CPUID : result =vc_handle_cpuid (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 57/75 x86/sev-es: Handle RDPMC Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by RDPMC instructions.
--- arch/x86/kernel/sev-es.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
arch/x86/kernel/sev-es.c +22 -0
@@ -805,6 +805,25 @@ static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_WBINVD ,0 ,0 ); } +static enum es_result vc_handle_rdpmc (struct ghcb *ghcb,struct es_em_ctxt *ctxt) +{ +enum es_result ret; + +ghcb_set_rcx (ghcb, ctxt->regs->cx); + + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_RDPMC ,0 ,0 ); +if (ret !=ES_OK ) +return ret; + +if (!(ghcb_is_valid_rax (ghcb) &&ghcb_is_valid_rdx (ghcb))) +return ES_VMM_ERROR ; + + ctxt->regs->ax = ghcb->save.rax; + ctxt->regs->dx = 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)
@@ -822,6 +841,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_RDTSCP : result =vc_handle_rdtsc (ghcb, ctxt, exit_code);break ; +case SVM_EXIT_RDPMC : + result =vc_handle_rdpmc (ghcb, ctxt); +break ;case SVM_EXIT_CPUID : result =vc_handle_cpuid (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 58/75 x86/sev-es: Handle INVD Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by INVD instructions. Since Linux should never use INVD, just mark it as unsupported.
--- arch/x86/kernel/sev-es.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/kernel/sev-es.c +4 -0
@@ -844,6 +844,10 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_RDPMC : result =vc_handle_rdpmc (ghcb, ctxt);break ; +case SVM_EXIT_INVD : +pr_err_ratelimited ("#VC exception for INVD??? Seriously???\n" ); + result =ES_UNSUPPORTED ; +break ;case SVM_EXIT_CPUID : result =vc_handle_cpuid (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 59/75 x86/sev-es: Handle MONITOR/MONITORX Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by MONITOR and MONITORX instructions.
--- arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kernel/sev-es.c +19 -0
@@ -824,6 +824,22 @@ static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxtreturn ES_OK ; } +static enum es_result vc_handle_monitor (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +phys_addr_t monitor_pa; +pgd_t *pgd; + + pgd =__va (read_cr3_pa ()); + monitor_pa =vc_slow_virt_to_phys (ghcb, ctxt->regs->ax); + +ghcb_set_rax (ghcb, monitor_pa); +ghcb_set_rcx (ghcb, ctxt->regs->cx); +ghcb_set_rdx (ghcb, ctxt->regs->dx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 ); +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -860,6 +876,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_WBINVD : result =vc_handle_wbinvd (ghcb, ctxt);break ; +case SVM_EXIT_MONITOR : + result =vc_handle_monitor (ghcb, ctxt); +break ;case SVM_EXIT_NPF : result =vc_handle_mmio (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
Re: [PATCH v3 59/75] x86/sev-es: Handle MONITOR/MONITORX Events
Quoted 25 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by MONITOR and MONITORX instructions.
Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: Adapt to #VC handling infrastructure ]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)arch/x86/kernel/sev-es.c +4
-0@@ -824,6 +824,22 @@ static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxtreturn ES_OK ; } +static enum es_result vc_handle_monitor (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +phys_addr_t monitor_pa;
arch/x86/kernel/sev-es.c static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt · line 840
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+pgd_t *pgd; + + pgd =__va (read_cr3_pa ()); + monitor_pa =vc_slow_virt_to_phys (ghcb, ctxt->regs->ax); + +ghcb_set_rax (ghcb, monitor_pa); +ghcb_set_rcx (ghcb, ctxt->regs->cx); +ghcb_set_rdx (ghcb, ctxt->regs->dx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 );
Why? If SVM has the same behavior as VMX, the MONITOR will be disarmed on VM-Enter, i.e. the VMM can't do anything useful for MONITOR/MWAIT. I assume that's the case given that KVM emulates MONITOR/MWAIT as NOPs on SVM.
Quoted 18 lines
arch/x86/kernel/sev-es.c +5
-0+} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)@@ -860,6 +876,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_WBINVD : result =vc_handle_wbinvd (ghcb, ctxt);break ; +case SVM_EXIT_MONITOR : + result =vc_handle_monitor (ghcb, ctxt); +break ;case SVM_EXIT_NPF : result =vc_handle_mmio (ghcb, ctxt);break ;Signature · 1 line
2.17.1
Re: [PATCH v3 59/75] x86/sev-es: Handle MONITOR/MONITORX Events
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +11
-0+static enum es_result vc_handle_monitor (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +phys_addr_t monitor_pa; +pgd_t *pgd; + + pgd =__va (read_cr3_pa ()); + monitor_pa =vc_slow_virt_to_phys (ghcb, ctxt->regs->ax); + +ghcb_set_rax (ghcb, monitor_pa); +ghcb_set_rcx (ghcb, ctxt->regs->cx);
Quoted 8 lines
arch/x86/kernel/sev-es.c static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt · line 840
Quoted 3 lines
arch/x86/kernel/sev-es.c +3
-0+ghcb_set_rdx (ghcb, ctxt->regs->dx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 );Why? If SVM has the same behavior as VMX, the MONITOR will be disarmed on VM-Enter, i.e. the VMM can't do anything useful for MONITOR/MWAIT. I assume that's the case given that KVM emulates MONITOR/MWAIT as NOPs on SVM.
Not sure if it is disarmed on VMRUN, but the MONITOR/MWAIT instructions are part of the GHCB spec, so they are implemented here.
Joerg
Re: [PATCH v3 59/75] x86/sev-es: Handle MONITOR/MONITORX Events
Quoted 22 lines · Joerg Roedel
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +11
-0+static enum es_result vc_handle_monitor (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +phys_addr_t monitor_pa; +pgd_t *pgd; + + pgd =__va (read_cr3_pa ()); + monitor_pa =vc_slow_virt_to_phys (ghcb, ctxt->regs->ax); + +ghcb_set_rax (ghcb, monitor_pa); +ghcb_set_rcx (ghcb, ctxt->regs->cx);Quoted 8 lines
arch/x86/kernel/sev-es.c static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt · line 840
Quoted 3 lines
arch/x86/kernel/sev-es.c +3
-0+ghcb_set_rdx (ghcb, ctxt->regs->dx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 );Why? If SVM has the same behavior as VMX, the MONITOR will be disarmed on VM-Enter, i.e. the VMM can't do anything useful for MONITOR/MWAIT. I assume that's the case given that KVM emulates MONITOR/MWAIT as NOPs on SVM.
Quoted 2 lines
Not sure if it is disarmed on VMRUN, but the MONITOR/MWAIT instructions are part of the GHCB spec, so they are implemented here.
Even if MONITOR/MWAIT somehow works across VMRUN I'm not sure it's something the guest should enable by default as it leaks GPAs to the untrusted host, with no benefit to the guest except in specific configurations. Yeah, the VMM can muck with page tables to trace guest to the some extent, but the guest shouldn't be unnecessarily volunteering information to the host.
If MONITOR/MWAIT is effectively a NOP then removing this code is a no brainer.
Can someone from AMD chime in?
Re: [PATCH v3 59/75] x86/sev-es: Handle MONITOR/MONITORX Events
Quoted 32 lines · Sean Christopherson
Quoted 22 lines · Joerg Roedel
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Joerg Roedel
arch/x86/kernel/sev-es.c +11
-0+static enum es_result vc_handle_monitor (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +phys_addr_t monitor_pa; +pgd_t *pgd; + + pgd =__va (read_cr3_pa ()); + monitor_pa =vc_slow_virt_to_phys (ghcb, ctxt->regs->ax); + +ghcb_set_rax (ghcb, monitor_pa); +ghcb_set_rcx (ghcb, ctxt->regs->cx);Quoted 8 lines
arch/x86/kernel/sev-es.c static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt · line 840
Quoted 3 lines
arch/x86/kernel/sev-es.c +3
-0+ghcb_set_rdx (ghcb, ctxt->regs->dx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 );Why? If SVM has the same behavior as VMX, the MONITOR will be disarmed on VM-Enter, i.e. the VMM can't do anything useful for MONITOR/MWAIT. I assume that's the case given that KVM emulates MONITOR/MWAIT as NOPs on SVM.
Quoted 2 lines
Not sure if it is disarmed on VMRUN, but the MONITOR/MWAIT instructions are part of the GHCB spec, so they are implemented here.
Even if MONITOR/MWAIT somehow works across VMRUN I'm not sure it's something the guest should enable by default as it leaks GPAs to the untrusted host, with no benefit to the guest except in specific configurations. Yeah, the VMM can muck with page tables to trace guest to the some extent, but the guest shouldn't be unnecessarily volunteering information to the host.
Quoted 4 lines
If MONITOR/MWAIT is effectively a NOP then removing this code is a no brainer.
Can someone from AMD chime in?
I don't think there is any guarantee that MONITOR/MWAIT would work within a guest (I'd have to dig some more on that to get a definitive answer, but probably not necessary to do). As you say, if KVM emulates it as a NOP, there's no sense in exposing the GPA - make it a NOP in the handler. I just need to poke some other hypervisor vendors and hear what they have to say.
Thanks,
Tom
Quoted 1 line
Re: [PATCH v3 59/75] x86/sev-es: Handle MONITOR/MONITORX Events
Quoted 5 lines · Tom Lendacky
I don't think there is any guarantee that MONITOR/MWAIT would work within a guest (I'd have to dig some more on that to get a definitive answer, but probably not necessary to do). As you say, if KVM emulates it as a NOP, there's no sense in exposing the GPA - make it a NOP in the handler. I just need to poke some other hypervisor vendors and hear what they have to say.
Okay, makes sense. I made monitor/mwait nops in the patch-set.
Regards,
Joerg
PATCH v3 60/75 x86/sev-es: Handle MWAIT/MWAITX Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by MWAIT and MWAITX instructions.
--- arch/x86/kernel/sev-es.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
arch/x86/kernel/sev-es.c +12 -0
@@ -840,6 +840,15 @@ static enum es_result vc_handle_monitor(struct ghcb *ghcb,return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MONITOR ,0 ,0 ); } +static enum es_result vc_handle_mwait (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +ghcb_set_rax (ghcb, ctxt->regs->ax); +ghcb_set_rcx (ghcb, ctxt->regs->cx); + +return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MWAIT ,0 ,0 ); +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -879,6 +888,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_MONITOR : result =vc_handle_monitor (ghcb, ctxt);break ; +case SVM_EXIT_MWAIT : + result =vc_handle_mwait (ghcb, ctxt); +break ;case SVM_EXIT_NPF : result =vc_handle_mmio (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 61/75 x86/sev-es: Handle VMMCALL Events
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement a handler for #VC exceptions caused by VMMCALL instructions. This patch is only a starting point, VMMCALL emulation under SEV-ES needs further hypervisor-specific changes to provide additional state.
--- arch/x86/kernel/sev-es.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
arch/x86/kernel/sev-es.c +23 -0
@@ -849,6 +849,26 @@ static enum es_result vc_handle_mwait(struct ghcb *ghcb,return sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_MWAIT ,0 ,0 ); } +static enum es_result vc_handle_vmmcall (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +enum es_result ret; + +ghcb_set_rax (ghcb, ctxt->regs->ax); +ghcb_set_cpl (ghcb,user_mode (ctxt->regs) ?3 :0 ); + + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_VMMCALL ,0 ,0 ); +if (ret !=ES_OK ) +return ret; + +if (!ghcb_is_valid_rax (ghcb)) +return ES_VMM_ERROR ; + + ctxt->regs->ax = ghcb->save.rax; + +return ES_OK ; +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -882,6 +902,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_VMMCALL : + result =vc_handle_vmmcall (ghcb, ctxt); +break ;case SVM_EXIT_WBINVD : result =vc_handle_wbinvd (ghcb, ctxt);break ;
Signature · 1 line
2.17.1
PATCH v3 62/75 x86/sev-es: Handle #AC Events
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Implement a handler for #VC exceptions caused by #AC exceptions. The #AC exception is just forwarded to do_alignment_check() and not pushed down to the hypervisor, as requested by the SEV-ES GHCB Standardization Specification.
--- arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kernel/sev-es.c +19 -0
@@ -869,6 +869,19 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,return ES_OK ; } +static enum es_result vc_handle_trap_ac (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +/* +* Calling do_alignment_check() directly does not work, because it +* enables IRQs and the GHCB is active. Forward the exception and call +* it later from vc_forward_exception(). +*/ + ctxt->fi.vector =X86_TRAP_AC ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -882,6 +895,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_WRITE_DR7 : result =vc_handle_dr7_write (ghcb, ctxt);break ; +case SVM_EXIT_EXCP_BASE +X86_TRAP_AC : + result =vc_handle_trap_ac (ghcb, ctxt); +break ;case SVM_EXIT_RDTSC :case SVM_EXIT_RDTSCP : result =vc_handle_rdtsc (ghcb, ctxt, exit_code);
@@ -941,6 +957,9 @@ static void vc_forward_exception(struct es_em_ctxt *ctxt)case X86_TRAP_UD :do_invalid_op (ctxt->regs,0 );break ; +case X86_TRAP_AC : +do_alignment_check (ctxt->regs, error_code); +break ;default :pr_emerg ("Unsupported exception in #VC instruction emulation - can't continue\n" );BUG ();
Signature · 1 line
2.17.1
PATCH v3 63/75 x86/sev-es: Handle #DB Events
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Handle #VC exceptions caused by #DB exceptions in the guest. Do not forward them to the hypervisor and handle them with do_debug() instead.
--- arch/x86/kernel/sev-es.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
arch/x86/kernel/sev-es.c +19 -0
@@ -882,6 +882,19 @@ static enum es_result vc_handle_trap_ac(struct ghcb *ghcb,return ES_EXCEPTION ; } +static enum es_result vc_handle_trap_db (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +/* +* Calling do_debug() directly does not work, because it might enable +* IRQs and the GHCB is active. Forward the exception and call it later +* from vc_forward_exception(). +*/ + ctxt->fi.vector =X86_TRAP_DB ; + ctxt->fi.error_code =0 ; +return ES_EXCEPTION ; +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)
@@ -895,6 +908,9 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt,case SVM_EXIT_WRITE_DR7 : result =vc_handle_dr7_write (ghcb, ctxt);break ; +case SVM_EXIT_EXCP_BASE +X86_TRAP_DB : + result =vc_handle_trap_db (ghcb, ctxt); +break ;case SVM_EXIT_EXCP_BASE +X86_TRAP_AC : result =vc_handle_trap_ac (ghcb, ctxt);break ;
@@ -960,6 +976,9 @@ static void vc_forward_exception(struct es_em_ctxt *ctxt)case X86_TRAP_AC :do_alignment_check (ctxt->regs, error_code);break ; +case X86_TRAP_DB : +do_debug (ctxt->regs, error_code); +break ;default :pr_emerg ("Unsupported exception in #VC instruction emulation - can't continue\n" );BUG ();
Signature · 1 line
2.17.1
PATCH v3 64/75 x86/sev-es: Cache CPUID results for improved performance
In reply to Joerg RoedelFrom: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
[ [email protected]: - Wrapped cache handling into vc_handle_cpuid_cached()
- Used lower_32_bits() where applicable
- Moved cache_index out of struct es_em_ctxt ]
--- arch/x86/kernel/sev-es-shared.c | 12 ++-- arch/x86/kernel/sev-es.c | 119 +++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 7 deletions(-)
arch/x86/kernel/sev-es-shared.c +6 -6
@@ -427,8 +427,8 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb,u32 cr4 =native_read_cr4 ();enum es_result ret; -ghcb_set_rax (ghcb, regs->ax); -ghcb_set_rcx (ghcb, regs->cx); +ghcb_set_rax (ghcb,lower_32_bits (regs->ax)); +ghcb_set_rcx (ghcb,lower_32_bits (regs->cx));if (cr4 &X86_CR4_OSXSAVE )/* Safe to read xcr0 */
@@ -447,10 +447,10 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb, ghcb_is_valid_rdx(ghcb)))return ES_VMM_ERROR ; - regs->ax = ghcb->save.rax; - regs->bx = ghcb->save.rbx; - regs->cx = ghcb->save.rcx; - regs->dx = ghcb->save.rdx; + regs->ax =lower_32_bits (ghcb->save.rax); + regs->bx =lower_32_bits (ghcb->save.rbx); + regs->cx =lower_32_bits (ghcb->save.rcx); + regs->dx =lower_32_bits (ghcb->save.rdx);return ES_OK ; }
arch/x86/kernel/sev-es.c +118 -1
155 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
Quoted 47 lines · Joerg Roedel
From: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
Signed-off-by Mike Stunes <[email protected]>Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: - Wrapped cache handling into
vc_handle_cpuid_cached()
- Usedlower_32_bits()where applicable
- Movedcache_indexout of structes_em_ctxt]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>---
arch/x86/kernel/sev-es-shared.c| 12 ++--arch/x86/kernel/sev-es.c| 119 +++++++++++++++++++++++++++++++-2 files changed, 124 insertions(+), 7 deletions(-)arch/x86/kernel/sev-es.c +16
-0@@ -744,6 +758,91 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,return ret; } +static unsigned long sev_es_get_cpuid_cache_index (struct es_em_ctxt *ctxt) +{ +unsigned long hi, lo; + +/* Don't attempt to cache until the xarray is initialized */ +if (!sev_es_cpuid_cache_initialized) +return ULONG_MAX ; + + lo =lower_32_bits (ctxt->regs->ax); + +/* +* CPUID 0x0000000d requires both RCX and XCR0, so it can't be +* cached. +*/ +if (lo ==0x0000000d ) +return ULONG_MAX ;
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 786
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+ +/* +* Some callers of CPUID don't always set RCX to zero for CPUID +* functions that don't require RCX, which can result in excessive +* cached values, so RCX needs to be manually zeroed for use as part +* of the cache index. Future CPUID values may need RCX, but since +* they can't be known, they must not be cached. +*/ +if (lo >0x80000020 ) +return ULONG_MAX ;
If the cache is shared across CPUs, do we also need to exclude function 0x1 because it contains the LAPIC ID? (Or is the cache per-CPU?)
Re: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
Quoted 51 lines · Mike Stunes
Quoted 51 lines · Joerg Roedel
From: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
Signed-off-by Mike Stunes <[email protected]>Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: - Wrapped cache handling into
vc_handle_cpuid_cached()
- Usedlower_32_bits()where applicable
- Movedcache_indexout of structes_em_ctxt]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>---
arch/x86/kernel/sev-es-shared.c| 12 ++--arch/x86/kernel/sev-es.c| 119 +++++++++++++++++++++++++++++++-2 files changed, 124 insertions(+), 7 deletions(-)arch/x86/kernel/sev-es.c +20
-0@@ -744,6 +758,91 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,return ret; } +static unsigned long sev_es_get_cpuid_cache_index (struct es_em_ctxt *ctxt) +{ +unsigned long hi, lo; + +/* Don't attempt to cache until the xarray is initialized */ +if (!sev_es_cpuid_cache_initialized) +return ULONG_MAX ; + + lo =lower_32_bits (ctxt->regs->ax); + +/* +* CPUID 0x0000000d requires both RCX and XCR0, so it can't be +* cached. +*/ +if (lo ==0x0000000d ) +return ULONG_MAX ; + +/* +* Some callers of CPUID don't always set RCX to zero for CPUID +* functions that don't require RCX, which can result in excessive
Quoted 8 lines
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 786
Quoted 6 lines
arch/x86/kernel/sev-es.c +6
-0+* cached values, so RCX needs to be manually zeroed for use as part +* of the cache index. Future CPUID values may need RCX, but since +* they can't be known, they must not be cached. +*/ +if (lo >0x80000020 ) +return ULONG_MAX ;If the cache is shared across CPUs, do we also need to exclude function
0x1because it contains the LAPIC ID? (Or is the cache per-CPU?)
It's currently not a per-CPU cache, but given what you pointed out, it should be if we're going to keep function 0x1 in there. The question will be how often is that CPUID issued, as that would determine if (not) caching it matters. Or even how often CPUID is issued and whether the xarray lock could be under contention if the cache is not per-CPU.
Thanks,
Tom
Quoted 1 line
Re: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
In reply to Joerg RoedelQuoted 15 lines · Joerg Roedel
From: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
Signed-off-by Mike Stunes <[email protected]>Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: - Wrapped cache handling into
vc_handle_cpuid_cached()
- Usedlower_32_bits()where applicable
- Movedcache_indexout of structes_em_ctxt]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>---
...
arch/x86/kernel/sev-es.c line 41
Quoted 5 lines
arch/x86/kernel/sev-es.c +5
-0+struct sev_es_cpuid_cache_entry { +unsigned long eax; +unsigned long ebx; +unsigned long ecx; +unsigned long edx;
Why are these unsigned longs? CPUID returns 32-bit values, this wastes 16 bytes per entry.
Quoted 41 lines
arch/x86/kernel/sev-es.c +27
-0+}; + +static struct xarray sev_es_cpuid_cache; +static bool __ro_after_init sev_es_cpuid_cache_initialized; +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -463,6 +474,9 @@ void __init sev_es_init_vc_handling(void)sev_es_setup_vc_stack (cpu); } +xa_init_flags (&sev_es_cpuid_cache,XA_FLAGS_LOCK_IRQ ); + sev_es_cpuid_cache_initialized = true; +init_vc_stack_names (); }@@ -744,6 +758,91 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,return ret; } +static unsigned long sev_es_get_cpuid_cache_index (struct es_em_ctxt *ctxt) +{ +unsigned long hi, lo; + +/* Don't attempt to cache until the xarray is initialized */ +if (!sev_es_cpuid_cache_initialized) +return ULONG_MAX ; + + lo =lower_32_bits (ctxt->regs->ax); + +/* +* CPUID 0x0000000d requires both RCX and XCR0, so it can't be +* cached. +*/ +if (lo ==0x0000000d ) +return ULONG_MAX ; + +/* +* Some callers of CPUID don't always set RCX to zero for CPUID
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 789
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+* functions that don't require RCX, which can result in excessive +* cached values, so RCX needs to be manually zeroed for use as part +* of the cache index. Future CPUID values may need RCX, but since +* they can't be known, they must not be cached. +*/ +if (lo >0x80000020 ) +return ULONG_MAX ; + +switch (lo) { +case 0x00000007 :
OSPKE may or may not be cached correctly depending on when sev_es_cpuid_cache_initialized is set.
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 801
Quoted 12 lines
arch/x86/kernel/sev-es.c +12
-0+case 0x0000000b : +case 0x0000000f : +case 0x00000010 : +case 0x8000001d : +case 0x80000020 : + hi = ctxt->regs->cx <<32 ; +break ; +default : + hi =0 ; + } + +return hi | lo;
This needs to be way more restrictive on what is cached. Unless I've overlooked something, this lets userspace trigger arbitrary, unaccounted kernel memory allocations. E.g.
for (i = 0; i <= 0x80000020; i++) {
for (j = 0; j <= 0xffffffff; j++) {
cpuid(i, j);
if (i != 7 || i != 0xb || i != 0xf || i != 0x10 ||
i != 0x8000001d || i != 0x80000020)
break;
}
}The whole cache on-demand approach seems like overkill. The number of CPUID leaves that are invoked after boot with any regularity can probably be counted
on one hand. IIRC glibc invokes CPUID to gather TLB/cache info, XCR0-based
features, and one or two other leafs. A statically sized global array that's arbitrarily index a la x86_capability would be just as simple and more performant. It would also allow fancier things like emulating CPUID 0xD in the guest if you want to go down that road.
Borislav Petkov
maintainerRe: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
Quoted 7 lines · Sean Christopherson
The whole cache on-demand approach seems like overkill. The number of CPUID leaves that are invoked after boot with any regularity can probably be counted
on one hand. IIRC glibc invokes CPUID to gather TLB/cache info, XCR0-based
features, and one or two other leafs. A statically sized global array that's arbitrarily index a lax86_capabilitywould be just as simple and more performant. It would also allow fancier things like emulating CPUID0xDin the guest if you want to go down that road.
And before we do any of that "caching" or whatnot, I'd like to see numbers justifying its existence. Because if it is only a couple of CPUID invocations and the boot delay is immeasurable, then it's not worth the effort.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
Quoted 9 lines · Borislav Petkov
Quoted 7 lines · Sean Christopherson
The whole cache on-demand approach seems like overkill. The number of CPUID leaves that are invoked after boot with any regularity can probably be counted
on one hand. IIRC glibc invokes CPUID to gather TLB/cache info, XCR0-based
features, and one or two other leafs. A statically sized global array that's arbitrarily index a lax86_capabilitywould be just as simple and more performant. It would also allow fancier things like emulating CPUID0xDin the guest if you want to go down that road.
Quoted 4 lines
And before we do any of that "caching" or whatnot, I'd like to see numbers justifying its existence. Because if it is only a couple of CPUID invocations and the boot delay is immeasurable, then it's not worth the effort.
I added some rudimentary stats code to see how many times there was a CPUID cache hit on a 64-vCPU guest during a kernel build (make clean followed by make with -j 64):
SEV-ES CPUID cache statistics
0x00000000/0x00000000: 220,384
0x00000007/0x00000000: 213,306
0x80000000/0x00000000: 1,054,642
0x80000001/0x00000000: 213,306
0x80000005/0x00000000: 210,334
0x80000006/0x00000000: 420,668
0x80000007/0x00000000: 210,334
0x80000008/0x00000000: 420,684
2,963,658 cache hitsSo it is significant in quantity, but I'm not sure what the overall performance difference is. If I can find some more time I'll try to compare the kernel builds with and without the caching to see if it is noticeable.
Thanks,
Tom
Quoted 1 line
Re: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
In reply to Joerg RoedelQuoted 164 lines · Joerg Roedel
From: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
Signed-off-by Mike Stunes <[email protected]>Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: - Wrapped cache handling into
vc_handle_cpuid_cached()
- Usedlower_32_bits()where applicable
- Movedcache_indexout of structes_em_ctxt]Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es-shared.c | 12 ++-- arch/x86/kernel/sev-es.c | 119 +++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 7 deletions(-)arch/x86/kernel/sev-es-shared.c +6
-6@@ -427,8 +427,8 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb,u32 cr4 =native_read_cr4 ();enum es_result ret; -ghcb_set_rax (ghcb, regs->ax); -ghcb_set_rcx (ghcb, regs->cx); +ghcb_set_rax (ghcb,lower_32_bits (regs->ax)); +ghcb_set_rcx (ghcb,lower_32_bits (regs->cx));if (cr4 &X86_CR4_OSXSAVE )/* Safe to read xcr0 */ @@ -447,10 +447,10 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb, ghcb_is_valid_rdx(ghcb)))return ES_VMM_ERROR ; - regs->ax = ghcb->save.rax; - regs->bx = ghcb->save.rbx; - regs->cx = ghcb->save.rcx; - regs->dx = ghcb->save.rdx; + regs->ax =lower_32_bits (ghcb->save.rax); + regs->bx =lower_32_bits (ghcb->save.rbx); + regs->cx =lower_32_bits (ghcb->save.rcx); + regs->dx =lower_32_bits (ghcb->save.rdx);return ES_OK ; }arch/x86/kernel/sev-es.c +86
-0@@ -19,6 +19,7 @@#include <linux/memblock.h> #include <linux/kernel.h> #include <linux/mm.h> +#include <linux/xarray.h> #include <generated/asm-offsets.h> #include <asm/cpu_entry_area.h> @@ -33,6 +34,16 @@#define DR7_RESET_VALUE 0x400 +struct sev_es_cpuid_cache_entry { +unsigned long eax; +unsigned long ebx; +unsigned long ecx; +unsigned long edx; +}; + +static struct xarray sev_es_cpuid_cache; +static bool __ro_after_init sev_es_cpuid_cache_initialized; +/* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE);@@ -463,6 +474,9 @@ void __init sev_es_init_vc_handling(void)sev_es_setup_vc_stack (cpu); } +xa_init_flags (&sev_es_cpuid_cache,XA_FLAGS_LOCK_IRQ ); + sev_es_cpuid_cache_initialized = true; +init_vc_stack_names (); }@@ -744,6 +758,91 @@ static enum es_result vc_handle_mmio(struct ghcb *ghcb,return ret; } +static unsigned long sev_es_get_cpuid_cache_index (struct es_em_ctxt *ctxt) +{ +unsigned long hi, lo; + +/* Don't attempt to cache until the xarray is initialized */ +if (!sev_es_cpuid_cache_initialized) +return ULONG_MAX ; + + lo =lower_32_bits (ctxt->regs->ax); + +/* +* CPUID 0x0000000d requires both RCX and XCR0, so it can't be +* cached. +*/ +if (lo ==0x0000000d ) +return ULONG_MAX ; + +/* +* Some callers of CPUID don't always set RCX to zero for CPUID +* functions that don't require RCX, which can result in excessive +* cached values, so RCX needs to be manually zeroed for use as part +* of the cache index. Future CPUID values may need RCX, but since +* they can't be known, they must not be cached. +*/ +if (lo >0x80000020 ) +return ULONG_MAX ; + +switch (lo) { +case 0x00000007 : +case 0x0000000b : +case 0x0000000f : +case 0x00000010 : +case 0x8000001d : +case 0x80000020 : + hi = ctxt->regs->cx <<32 ; +break ; +default : + hi =0 ; + } + +return hi | lo; +} + +static bool sev_es_check_cpuid_cache (struct es_em_ctxt *ctxt, +unsigned long cache_index) +{ +struct sev_es_cpuid_cache_entry *cache_entry; + +if (cache_index ==ULONG_MAX ) +return false; + + cache_entry =xa_load (&sev_es_cpuid_cache, cache_index); +if (!cache_entry) +return false; + + ctxt->regs->ax = cache_entry->eax; + ctxt->regs->bx = cache_entry->ebx; + ctxt->regs->cx = cache_entry->ecx; + ctxt->regs->dx = cache_entry->edx; + +return true; +} + +static void sev_es_add_cpuid_cache (struct es_em_ctxt *ctxt, +unsigned long cache_index) +{ +struct sev_es_cpuid_cache_entry *cache_entry; +int ret; + +if (cache_index ==ULONG_MAX ) +return ; +
arch/x86/kernel/sev-es.c static enum es_result vc_handle_mmio(struct ghcb *ghcb, · line 842
Quoted 10 lines
arch/x86/kernel/sev-es.c +10
-0+ cache_entry =kzalloc (sizeof (*cache_entry),GFP_ATOMIC ); +if (cache_entry) { + cache_entry->eax = ctxt->regs->ax; + cache_entry->ebx = ctxt->regs->bx; + cache_entry->ecx = ctxt->regs->cx; + cache_entry->edx = ctxt->regs->dx; + +/* Ignore insertion errors */ + ret =xa_insert (&sev_es_cpuid_cache, cache_index, + cache_entry,GFP_ATOMIC );
Just realized, that on error, the cache_entry should be freed.
Thanks,
Tom
Quoted 41 lines
arch/x86/kernel/sev-es.c +22
-1+ } +} +static enum es_result vc_handle_dr7_write (struct ghcb *ghcb,struct es_em_ctxt *ctxt) {@@ -895,6 +994,24 @@ static enum es_result vc_handle_trap_db(struct ghcb *ghcb,return ES_EXCEPTION ; } +static enum es_result vc_handle_cpuid_cached (struct ghcb *ghcb, +struct es_em_ctxt *ctxt) +{ +unsigned long cache_index; +enum es_result result; + + cache_index =sev_es_get_cpuid_cache_index (ctxt); + +if (sev_es_check_cpuid_cache (ctxt, cache_index)) +return ES_OK ; + + result =vc_handle_cpuid (ghcb, ctxt); +if (result ==ES_OK ) +sev_es_add_cpuid_cache (ctxt, cache_index); + +return result; +} +static enum es_result vc_handle_exitcode (struct es_em_ctxt *ctxt,struct ghcb *ghcb,unsigned long exit_code)@@ -926,7 +1043,7 @@ static enum es_result vc_handle_exitcode(struct es_em_ctxt *ctxt, result =ES_UNSUPPORTED ;break ;case SVM_EXIT_CPUID : - result =vc_handle_cpuid (ghcb, ctxt); + result =vc_handle_cpuid_cached (ghcb, ctxt);break ;case SVM_EXIT_IOIO : result =vc_handle_ioio (ghcb, ctxt);
Re: [PATCH v3 64/75] x86/sev-es: Cache CPUID results for improved performance
In reply to Joerg RoedelQuoted 12 lines · Joerg Roedel
From: Mike Stunes <[email protected]>
To avoid a future VMEXIT for a subsequent CPUID function, cache the results returned by CPUID into an xarray.
[tl: coding standard changes, register zero extension]
Signed-off-by Mike Stunes <[email protected]>Signed-off-by Tom Lendacky <[email protected]>[ [email protected]: - Wrapped cache handling into
vc_handle_cpuid_cached()
- Usedlower_32_bits()where applicable
- Movedcache_indexout of structes_em_ctxt]
Quoted 6 lines
Co-developed-by Joerg Roedel <[email protected]>Signed-off-by Joerg Roedel <[email protected]>--- arch/x86/kernel/sev-es-shared.c | 12 ++-- arch/x86/kernel/sev-es.c | 119 +++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 7 deletions(-)
Okay, following the discussion, I am dropping this patch for now. We can revisit CPUID caching later when we have a better justification.
Regards,
Joerg
PATCH v3 65/75 x86/paravirt: Allow hypervisor specific VMMCALL handling under SEV-ES
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
Add two new paravirt callbacks to provide hypervisor specific processor state in the GHCB and to copy state from the hypervisor back to the processor.
--- arch/x86/include/asm/x86_init.h | 16 +++++++++++++++- arch/x86/kernel/sev-es.c | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-)
arch/x86/include/asm/x86_init.h +15 -1
@@ -4,8 +4,10 @@#include <asm/bootparam.h> +struct ghcb ;struct mpc_bus ;struct mpc_cpu ; +struct pt_regs ;struct mpc_table ;struct cpuinfo_x86 ;
@@ -238,10 +240,22 @@ struct x86_legacy_features {/** * struct x86_hyper_runtime - x86 hypervisor specific runtime callbacks * -* @pin_vcpu: pin current vcpu to specified physical cpu (run rarely) +* @pin_vcpu: pin current vcpu to specified physical +* cpu (run rarely) +* @sev_es_hcall_prepare: Load additional hypervisor-specific +* state into the GHCB when doing a VMMCALL under +* SEV-ES. Called from the #VC exception handler. +* @sev_es_hcall_finish: Copies state from the GHCB back into the +* processor (or pt_regs). Also runs checks on the +* state returned from the hypervisor after a +* VMMCALL under SEV-ES. Needs to return 'false' +* if the checks fail. Called from the #VC +* exception handler. */ struct x86_hyper_runtime {void (*pin_vcpu)(int cpu); +void (*sev_es_hcall_prepare)(struct ghcb *ghcb,struct pt_regs *regs); +bool (*sev_es_hcall_finish)(struct ghcb *ghcb,struct pt_regs *regs); };/**
arch/x86/kernel/sev-es.c +12 -0
@@ -956,6 +956,9 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,ghcb_set_rax (ghcb, ctxt->regs->ax);ghcb_set_cpl (ghcb,user_mode (ctxt->regs) ?3 :0 ); +if (x86_platform.hyper.sev_es_hcall_prepare) + x86_platform.hyper.sev_es_hcall_prepare (ghcb, ctxt->regs); + ret =sev_es_ghcb_hv_call (ghcb, ctxt,SVM_EXIT_VMMCALL ,0 ,0 );if (ret !=ES_OK )return ret;
@@ -965,6 +968,15 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb, ctxt->regs->ax = ghcb->save.rax; +/* +* Call sev_es_hcall_finish() after regs->ax is already set. +* This allows the hypervisor handler to overwrite it again if +* necessary. +*/ +if (x86_platform.hyper.sev_es_hcall_finish && + !x86_platform.hyper.sev_es_hcall_finish (ghcb, ctxt->regs)) +return ES_VMM_ERROR ; +return ES_OK ; }
Signature · 1 line
2.17.1
PATCH v3 66/75 x86/kvm: Add KVM specific VMMCALL handling under SEV-ES
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Implement the callbacks to copy the processor state required by KVM to the GHCB.
[ [email protected]: - Split out of a larger patch
- Adapt to different callback functions ]
--- arch/x86/kernel/kvm.c | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-)
arch/x86/kernel/kvm.c +29 -6
@@ -34,6 +34,8 @@#include <asm/hypervisor.h> #include <asm/tlb.h> #include <asm/cpuidle_haltpoll.h> +#include <asm/ptrace.h> +#include <asm/svm.h> static int kvmapf =1 ;
@@ -729,13 +731,34 @@ static void __init kvm_init_platform(void) x86_platform.apic_post_init = kvm_apic_init; } +#if defined(CONFIG_AMD_MEM_ENCRYPT ) +static void kvm_sev_es_hcall_prepare (struct ghcb *ghcb,struct pt_regs *regs) +{ +/* RAX and CPL are already in the GHCB */ +ghcb_set_rbx (ghcb, regs->bx); +ghcb_set_rcx (ghcb, regs->cx); +ghcb_set_rdx (ghcb, regs->dx); +ghcb_set_rsi (ghcb, regs->si); +} + +static bool kvm_sev_es_hcall_finish (struct ghcb *ghcb,struct pt_regs *regs) +{ +/* No checking of the return state needed */ +return true; +} +#endif +const __initconst struct hypervisor_x86 x86_hyper_kvm = { - .name ="KVM" , - .detect = kvm_detect, - .type =X86_HYPER_KVM , - .init.guest_late_init = kvm_guest_init, - .init.x2apic_available = kvm_para_available, - .init.init_platform = kvm_init_platform, + .name ="KVM" , + .detect = kvm_detect, + .type =X86_HYPER_KVM , + .init.guest_late_init = kvm_guest_init, + .init.x2apic_available = kvm_para_available, + .init.init_platform = kvm_init_platform, +#if defined(CONFIG_AMD_MEM_ENCRYPT) + .runtime.sev_es_hcall_prepare = kvm_sev_es_hcall_prepare, + .runtime.sev_es_hcall_finish = kvm_sev_es_hcall_finish, +#endif };static __init int activate_jump_labels (void )
Signature · 1 line
2.17.1
PATCH v3 67/75 x86/vmware: Add VMware specific handling for VMMCALL under SEV-ES
In reply to Joerg RoedelFrom: Doug Covelli <[email protected]>
This change adds VMware specific handling for #VC faults caused by VMMCALL instructions.
--- arch/x86/kernel/cpu/vmware.c | 50 ++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-)
arch/x86/kernel/cpu/vmware.c +45 -5
@@ -33,6 +33,7 @@#include <asm/timer.h> #include <asm/apic.h> #include <asm/vmware.h> +#include <asm/svm.h> #undef pr_fmt#define pr_fmt (fmt) "vmware: " fmt
@@ -476,10 +477,49 @@ static bool __init vmware_legacy_x2apic_available(void) (eax & (1 <<VMWARE_CMD_LEGACY_X2APIC )) !=0 ; } +#ifdef CONFIG_AMD_MEM_ENCRYPT +static void vmware_sev_es_hcall_prepare (struct ghcb *ghcb, +struct pt_regs *regs) +{ +/* Copy VMWARE specific Hypercall parameters to the GHCB */ +ghcb_set_rip (ghcb, regs->ip); +ghcb_set_rbx (ghcb, regs->bx); +ghcb_set_rcx (ghcb, regs->cx); +ghcb_set_rdx (ghcb, regs->dx); +ghcb_set_rsi (ghcb, regs->si); +ghcb_set_rdi (ghcb, regs->di); +ghcb_set_rbp (ghcb, regs->bp); +} + +static bool vmware_sev_es_hcall_finish (struct ghcb *ghcb,struct pt_regs *regs) +{ +if (!(ghcb_is_valid_rbx (ghcb) && +ghcb_is_valid_rcx (ghcb) && +ghcb_is_valid_rdx (ghcb) && +ghcb_is_valid_rsi (ghcb) && +ghcb_is_valid_rdi (ghcb) && +ghcb_is_valid_rbp (ghcb))) +return false; + + regs->bx = ghcb->save.rbx; + regs->cx = ghcb->save.rcx; + regs->dx = ghcb->save.rdx; + regs->si = ghcb->save.rsi; + regs->di = ghcb->save.rdi; + regs->bp = ghcb->save.rbp; + +return true; +} +#endif +const __initconst struct hypervisor_x86 x86_hyper_vmware = { - .name ="VMware" , - .detect = vmware_platform, - .type =X86_HYPER_VMWARE , - .init.init_platform = vmware_platform_setup, - .init.x2apic_available = vmware_legacy_x2apic_available, + .name ="VMware" , + .detect = vmware_platform, + .type =X86_HYPER_VMWARE , + .init.init_platform = vmware_platform_setup, + .init.x2apic_available = vmware_legacy_x2apic_available, +#ifdef CONFIG_AMD_MEM_ENCRYPT + .runtime.sev_es_hcall_prepare = vmware_sev_es_hcall_prepare, + .runtime.sev_es_hcall_finish = vmware_sev_es_hcall_finish, +#endif };
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 67/75] x86/vmware: Add VMware specific handling for VMMCALL under SEV-ES
Quoted 3 lines · Joerg Roedel
From: Doug Covelli <[email protected]>
This change adds VMware specific handling for #VC faults caused by
s/This change adds/Add/
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH v3 68/75 x86/realmode: Add SEV-ES specific trampoline entry point
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The code at the trampoline entry point is executed in real-mode. In real-mode #VC exceptions can't be handled, so anything that might cause such an exception must be avoided.
In the standard trampoline entry code this is the WBINVD instruction and the call to verify_cpu(), which are both not needed anyway when running as an SEV-ES guest.
--- arch/x86/include/asm/realmode.h | 3 +++ arch/x86/realmode/rm/header.S | 3 +++ arch/x86/realmode/rm/trampoline_64.S | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+)
arch/x86/include/asm/realmode.h +3 -0
@@ -21,6 +21,9 @@ struct real_mode_header {/* SMP trampoline */ u32 trampoline_start;u32 trampoline_header; +#ifdef CONFIG_AMD_MEM_ENCRYPT +u32 sev_es_trampoline_start; +#endif #ifdef CONFIG_X86_64 u32 trampoline_pgd;#endif
arch/x86/realmode/rm/header.S +3 -0
@@ -20,6 +20,9 @@ SYM_DATA_START(real_mode_header) /* SMP trampoline */ .long pa_trampoline_start .long pa_trampoline_header +#ifdef CONFIG_AMD_MEM_ENCRYPT + .long pa_sev_es_trampoline_start +#endif #ifdef CONFIG_X86_64 .long pa_trampoline_pgd; #endif
arch/x86/realmode/rm/trampoline_64.S +20 -0
@@ -56,6 +56,7 @@ SYM_CODE_START(trampoline_start) testl %eax, %eax # Check for return code jnz no_longmode +.Lswitch_to_protected: /* * GDT tables in non default location kernel can be beyond 16MB and * lgdt will not be able to load the address as in real mode default
@@ -80,6 +81,25 @@ no_longmode: jmp no_longmode SYM_CODE_END(trampoline_start) +#ifdef CONFIG_AMD_MEM_ENCRYPT +/* SEV-ES supports non-zero IP for entry points - no alignment needed */ +SYM_CODE_START(sev_es_trampoline_start) + cli # We should be safe anyway + + LJMPW_RM(1f) +1: + mov %cs, %ax # Code and data in the same place + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + + # Setup stack + movl $rm_stack_end, %esp + + jmp .Lswitch_to_protected +SYM_CODE_END(sev_es_trampoline_start) +#endif /* CONFIG_AMD_MEM_ENCRYPT */ + #include "../kernel/verify_cpu.S" .section ".text32","ax"
Signature · 1 line
2.17.1
PATCH v3 69/75 x86/realmode: Setup AP jump table
In reply to Joerg RoedelFrom: Tom Lendacky <[email protected]>
Setup the AP jump table to point to the SEV-ES trampoline code so that the APs can boot.
[ [email protected]: - Adapted to different code base
- Moved AP table setup from SIPI sending path to real-mode setup code
- Fix sparse warnings ]
--- arch/x86/include/asm/sev-es.h | 6 +++ arch/x86/include/uapi/asm/svm.h | 3 ++ arch/x86/kernel/sev-es.c | 66 +++++++++++++++++++++++++++++++++ arch/x86/realmode/init.c | 6 +++ 4 files changed, 81 insertions(+)
arch/x86/include/asm/sev-es.h +6 -0
@@ -78,17 +78,23 @@ static inline u64 lower_bits(u64 val, unsigned int bits)extern void vc_no_ghcb (void );extern bool vc_boot_ghcb (struct pt_regs *regs); +struct real_mode_header ;enum stack_type ;#ifdef CONFIG_AMD_MEM_ENCRYPT const char *vc_stack_name (enum stack_type type);void sev_es_nmi_enter (void );void sev_es_nmi_exit (void ); +int sev_es_setup_ap_jump_table (struct real_mode_header *rmh);#else /* CONFIG_AMD_MEM_ENCRYPT */ static inline const char *vc_stack_name (enum stack_type type) {return NULL ; } +static inline int sev_es_setup_ap_jump_table (struct real_mode_header *rmh) +{ +return 0 ; +}#endif /* CONFIG_AMD_MEM_ENCRYPT*/ #endif
arch/x86/include/uapi/asm/svm.h +3 -0
@@ -84,6 +84,9 @@/* SEV-ES software-defined VMGEXIT events */ #define SVM_VMGEXIT_MMIO_READ 0x80000001#define SVM_VMGEXIT_MMIO_WRITE 0x80000002 +#define SVM_VMGEXIT_AP_JUMP_TABLE 0x80000005 +#define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 +#define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1#define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff#define SVM_EXIT_ERR -1
arch/x86/kernel/sev-es.c +66 -0
84 lines not on this page · raw message
arch/x86/realmode/init.c +6 -0
@@ -9,6 +9,7 @@#include <asm/realmode.h> #include <asm/tlbflush.h> #include <asm/crash.h> +#include <asm/sev-es.h> struct real_mode_header *real_mode_header;u32 *trampoline_cr4_features;
@@ -107,6 +108,11 @@ static void __init setup_real_mode(void)if (sme_active ()) trampoline_header->flags |=TH_FLAGS_SME_ACTIVE ; +if (sev_es_active ()) { +if (sev_es_setup_ap_jump_table (real_mode_header)) +panic ("Failed to update SEV-ES AP Jump Table" ); + } + trampoline_pgd = (u64 *)__va (real_mode_header->trampoline_pgd); trampoline_pgd[0 ] = trampoline_pgd_entry.pgd; trampoline_pgd[511 ] = init_top_pgt[511 ].pgd;
Signature · 1 line
2.17.1
Borislav Petkov
maintainerRe: [PATCH v3 69/75] x86/realmode: Setup AP jump table
Quoted 4 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Setup the AP jump table to point to the SEV-ES trampoline code so that the APs can boot.
Tom, in his laconic way, doesn't want to explain to us why is this even needed...
:)
/me reads the code
/me reads the GHCB spec
aha, it gets it from the HV. And it can be set by the guest too...
So how about expanding that commit message as to why this is done, why needed, etc?
Thx.
Quoted 13 lines
arch/x86/realmode/init.c +1
-0@@ -9,6 +9,7 @@#include <asm/realmode.h> #include <asm/tlbflush.h> #include <asm/crash.h> +#include <asm/sev-es.h> struct real_mode_header *real_mode_header;u32 *trampoline_cr4_features;
arch/x86/realmode/init.c static void __init setup_real_mode(void) · line 115
Quoted 8 lines
arch/x86/realmode/init.c +5
-0@@ -107,6 +108,11 @@ static void __init setup_real_mode(void)if (sme_active ()) trampoline_header->flags |=TH_FLAGS_SME_ACTIVE ; +if (sev_es_active ()) { +if (sev_es_setup_ap_jump_table (real_mode_header)) +panic ("Failed to update SEV-ES AP Jump Table" ); + } +
So this function gets slowly sprinkled with
if (sev-something)
bla
Please wrap at least those last two into a
sev_setup_real_mode()
or so.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v3 69/75] x86/realmode: Setup AP jump table
Quoted 10 lines · Borislav Petkov
Quoted 4 lines · Joerg Roedel
From: Tom Lendacky <[email protected]>
Setup the AP jump table to point to the SEV-ES trampoline code so that the APs can boot.
Tom, in his laconic way, doesn't want to explain to us why is this even needed...
:)
Looks like some of the detail was lost during the patch shuffling. Originally (on GitHub) this was the text:
As part of the GHCB specification, the booting of APs under SEV-ES requires an AP jump table when transitioning from one layer of code to another (e.g. when going from UEFI to the OS). As a result, each layer that parks an AP must provide the physical address of an AP jump table to the next layer using the GHCB MSR.
Upon booting of the kernel, read the GHCB MSR and save the address of the AP jump table. Under SEV-ES, APs are started using the INIT-SIPI-SIPI sequence. Before issuing the first SIPI request for an AP, the start eip is programmed into the AP jump table. Upon issuing the SIPI request, the AP will awaken and jump to the start eip address programmed into the AP jump table.
It needs to change "GHCB MSR" to "VMGEXIT MSR protocol", but should cover what you're looking for.
Thanks,
Tom
Quoted 45 lines
/me reads the code
/me reads the GHCB spec
aha, it gets it from the HV. And it can be set by the guest too...
So how about expanding that commit message as to why this is done, why needed, etc?
Thx.
Quoted 13 lines
arch/x86/realmode/init.c +1
-0@@ -9,6 +9,7 @@#include <asm/realmode.h> #include <asm/tlbflush.h> #include <asm/crash.h> +#include <asm/sev-es.h> struct real_mode_header *real_mode_header;u32 *trampoline_cr4_features;arch/x86/realmode/init.c static void __init setup_real_mode(void) · line 115
Quoted 8 lines
arch/x86/realmode/init.c +5
-0@@ -107,6 +108,11 @@ static void __init setup_real_mode(void)if (sme_active ()) trampoline_header->flags |=TH_FLAGS_SME_ACTIVE ; +if (sev_es_active ()) { +if (sev_es_setup_ap_jump_table (real_mode_header)) +panic ("Failed to update SEV-ES AP Jump Table" ); + } +So this function gets slowly sprinkled with
if (sev-something)
blaPlease wrap at least those last two into a
sev_setup_real_mode()or so.
PATCH v3 70/75 x86/head/64: Setup TSS early for secondary CPUs
In reply to Joerg RoedelFrom: Joerg Roedel <[email protected]>
The #VC exception will trigger very early in head_64.S, when the first CPUID instruction is executed. When secondary CPUs boot, they already load the real system IDT, which has the #VC handler configured to be using an IST stack. IST stacks require a TSS to be loaded, to set up the TSS early for bringing up the secondary CPUs. Use the RW version of early, until cpu_init() switches to the RO mapping.
On the boot CPU the TSS will also be loaded early, but not used as the the early #VC handlers do not use IST stacks.
--- arch/x86/include/asm/desc.h | 2 ++ arch/x86/kernel/head64.c | 13 +++++++++++++ arch/x86/kernel/head_64.S | 3 +++ 3 files changed, 18 insertions(+)
arch/x86/include/asm/desc.h +2 -0
@@ -40,6 +40,8 @@ static inline void fill_ldt(struct desc_struct *desc, const struct user_desc *in desc->l =0 ; } +extern unsigned char boot_gdt[]; +extern struct desc_ptr idt_descr;extern gate_desc idt_table[];extern const struct desc_ptr debug_idt_descr;
arch/x86/kernel/head64.c +13 -0
@@ -523,6 +523,19 @@ void __head early_idt_setup_early_handler(unsigned long physaddr) } } +void __head early_load_tss(void) +{ +struct desc_struct *gdt = (struct desc_struct *)boot_gdt; +struct tss_struct *tss =this_cpu_ptr (&cpu_tss_rw); +tss_desc tss_desc; + +set_tssldt_descriptor (&tss_desc, (unsigned long )tss,DESC_TSS , + __KERNEL_TSS_LIMIT); +native_write_gdt_entry (gdt,GDT_ENTRY_TSS , &tss_desc,DESC_TSS ); + + asmvolatile ("ltr %w0" ::"q" (GDT_ENTRY_TSS *8 )); +} + void __head early_idt_setup(unsigned long physbase) {gate_desc *idt =fixup_pointer (idt_table, physbase);
arch/x86/kernel/head_64.S +3 -0
@@ -220,6 +220,9 @@ SYM_CODE_START(secondary_startup_64) */ movq initial_stack(%rip), %rsp + /* Setup a TSS for early IST handlers - needs %gs to be set up */ + call early_load_tss + /* Load IDT */ lidt idt_descr(%rip)
Signature · 1 line
2.17.1