SEV-ES hypervisor support
Subsystems (4)
v2 cover SEV-ES hypervisor support
From: Tom Lendacky <[email protected]>
This patch series provides support for running SEV-ES guests under KVM.
Secure Encrypted Virtualization - Encrypted State (SEV-ES) expands on the SEV support to protect the guest register state from the hypervisor. See "AMD64 Architecture Programmer's Manual Volume 2: System Programming", section "15.35 Encrypted State (SEV-ES)" [1].
In order to allow a hypervisor to perform functions on behalf of a guest, there is architectural support for notifying a guest's operating system when certain types of VMEXITs are about to occur. This allows the guest to selectively share information with the hypervisor to satisfy the requested function. The notification is performed using a new exception, the VMM Communication exception (#VC). The information is shared through the Guest-Hypervisor Communication Block (GHCB) using the VMGEXIT instruction. The GHCB format and the protocol for using it is documented in "SEV-ES Guest-Hypervisor Communication Block Standardization" [2].
Under SEV-ES, a vCPU save area (VMSA) must be encrypted. SVM is updated to build the initial VMSA and then encrypt it before running the guest. Once encrypted, it must not be modified by the hypervisor. Modification of the VMSA will result in the VMRUN instruction failing with a SHUTDOWN exit code. KVM must support the VMGEXIT exit code in order to perform the necessary functions required of the guest. The GHCB is used to exchange the information needed by both the hypervisor and the guest.
To simplify access to the VMSA and the GHCB, SVM uses an accessor function to obtain the address of the either the VMSA or the GHCB, depending on the stage of execution of the guest.
There are changes to some of the intercepts that are needed under SEV-ES. For example, CR0 writes cannot be intercepted, so the code needs to ensure that the intercept is not enabled during execution or that the hypervisor does not try to read the register as part of exit processing. Another example is shutdown processing, where the vCPU cannot be directly reset.
Support is added to handle VMGEXIT events and implement the GHCB protocol. This includes supporting standard exit events, like a CPUID instruction intercept, to new support, for things like AP processor booting. Much of the existing SVM intercept support can be re-used by setting the exit code information from the VMGEXIT and calling the appropriate intercept handlers.
Finally, to launch and run an SEV-ES guest requires changes to the vCPU initialization, loading and execution.
[1] https://www.amd.com/system/files/TechDocs/24593.pdf
[2] https://developer.amd.com/wp-content/resources/56421.pdf
---
These patches are based on a commit of the KVM next branch. However, I had to backport recent SEV-ES guest patches (a previous series to the actual patches that are now in the tip tree) into my development branch, since there are prereq patches needed by this series. As a result, this patch series will not successfully build or apply to the KVM next branch as is.
A version of the tree can be found at:
https://github.com/AMDESE/linux/tree/sev-es-5.9-v1
Changes from v1:
- Removed the VMSA indirection support:
- On LAUNCH_UPDATE_VMSA, sync traditional VMSA over to the new SEV-ES VMSA area to be encrypted.
- On VMGEXIT VMEXIT, directly copy valid registers into vCPU arch register array from GHCB. On VMRUN (following a VMGEXIT), directly copy dirty vCPU arch registers to GHCB.
- Removed reg_read_override()/reg_write_override() KVM ops.
- Added VMGEXIT exit-reason validation.
- Changed kvm_vcpu_arch variable vmsa_encrypted to guest_state_protected
- Updated the tracking support for EFER/CR0/CR4/CR8 to minimize changes to the x86.c code
- Updated __set_sregs to not set any register values (previously supported setting the tracked values of EFER/CR0/CR4/CR8)
- Added support for reporting SMM capability at the VM-level. This allows an SEV-ES guest to indicate SMM is not supported
- Updated FPU support to check for a guest FPU save area before using it. Updated SVM to free guest FPU for an SEV-ES guest during KVM create_vcpu op.
- Removed changes to the kvm_skip_emulated_instruction()
- Added VMSA validity checks before invoking LAUNCH_UPDATE_VMSA
- Minor code restructuring in areas for better readability
Tom Lendacky (33):
KVM: SVM: Remove the call to sev_platform_status() during setup
KVM: SVM: Add support for SEV-ES capability in KVM
KVM: SVM: Add GHCB accessor functions for retrieving fields
KVM: SVM: Add support for the SEV-ES VMSA
KVM: x86: Mark GPRs dirty when written
KVM: SVM: Add required changes to support intercepts under SEV-ES
KVM: SVM: Prevent debugging under SEV-ES
KVM: SVM: Do not allow instruction emulation under SEV-ES
KVM: SVM: Cannot re-initialize the VMCB after shutdown with SEV-ES
KVM: SVM: Prepare for SEV-ES exit handling in the sev.c file
KVM: SVM: Add initial support for a VMGEXIT VMEXIT
KVM: SVM: Create trace events for VMGEXIT processing
KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x002
KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x004
KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x100
KVM: SVM: Create trace events for VMGEXIT MSR protocol processing
KVM: SVM: Support MMIO for an SEV-ES guest
KVM: SVM: Support port IO operations for an SEV-ES guest
KVM: SVM: Add support for EFER write traps for an SEV-ES guest
KVM: SVM: Add support for CR0 write traps for an SEV-ES guest
KVM: SVM: Add support for CR4 write traps for an SEV-ES guest
KVM: SVM: Add support for CR8 write traps for an SEV-ES guest
KVM: x86: Update __get_sregs() / __set_sregs() to support SEV-ES
KVM: SVM: Do not report support for SMM for an SEV-ES guest
KVM: SVM: Guest FPU state save/restore not needed for SEV-ES guest
KVM: SVM: Add support for booting APs for an SEV-ES guest
KVM: SVM: Add NMI support for an SEV-ES guest
KVM: SVM: Set the encryption mask for the SVM host save area
KVM: SVM: Update ASID allocation to support SEV-ES guests
KVM: SVM: Provide support for SEV-ES vCPU creation/loading
KVM: SVM: Provide support for SEV-ES vCPU loading
KVM: SVM: Provide an updated VMRUN invocation for SEV-ES guests
KVM: SVM: Provide support to launch and run an SEV-ES guest
arch/x86/include/asm/kvm_host.h | 12 +- arch/x86/include/asm/msr-index.h | 1 + arch/x86/include/asm/svm.h | 40 +- arch/x86/include/uapi/asm/svm.h | 28 ++ arch/x86/kernel/cpu/vmware.c | 12 +- arch/x86/kvm/Kconfig | 3 +- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/kvm_cache_regs.h | 51 +- arch/x86/kvm/svm/sev.c | 837 +++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.c | 465 +++++++++++++---- arch/x86/kvm/svm/svm.h | 165 ++++-- arch/x86/kvm/svm/vmenter.S | 50 ++ arch/x86/kvm/trace.h | 97 ++++ arch/x86/kvm/vmx/vmx.c | 6 +- arch/x86/kvm/x86.c | 364 ++++++++++++-- arch/x86/kvm/x86.h | 9 + 16 files changed, 1892 insertions(+), 249 deletions(-)
Signature · 1 line
2.28.0
RFC PATCH v2 1/33 KVM: SVM: Remove the call to sev_platform_status() during setup
From: Tom Lendacky <[email protected]>
When both KVM support and the CCP driver are built into the kernel instead of as modules, KVM initialization happens before CCP initialization. As a result, sev_platform_status() will return a failure when it is called from sev_hardware_setup(), when this isn't really an error condition.
Since sev_platform_status() doesn't need to be called at this time anyway, remove the invocation from sev_hardware_setup().
--- arch/x86/kvm/svm/sev.c | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-)
arch/x86/kvm/svm/sev.c +1 -21
@@ -1126,9 +1126,6 @@ void sev_vm_destroy(struct kvm *kvm)int __init sev_hardware_setup (void ) { -struct sev_user_data_status *status; -int rc; -/* Maximum number of encrypted guests supported simultaneously */ max_sev_asid =cpuid_ecx (0x8000001F );
@@ -1147,26 +1144,9 @@ int __init sev_hardware_setup(void)if (!sev_reclaim_asid_bitmap)return 1 ; - status =kmalloc (sizeof (*status),GFP_KERNEL ); -if (!status) -return 1 ; - -/* -* Check SEV platform status. -* -* PLATFORM_STATUS can be called in any state, if we failed to query -* the PLATFORM status then either PSP firmware does not support SEV -* feature or SEV firmware is dead. -*/ - rc =sev_platform_status (status,NULL ); -if (rc) - goto err; -pr_info ("SEV supported\n" ); -err: -kfree (status); -return rc; +return 0 ; }void sev_hardware_teardown (void )
Signature · 1 line
2.28.0
RFC PATCH v2 2/33 KVM: SVM: Add support for SEV-ES capability in KVM
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Add support to KVM for determining if a system is capable of supporting SEV-ES as well as determining if a guest is an SEV-ES guest.
--- arch/x86/kvm/Kconfig | 3 ++- arch/x86/kvm/svm/sev.c | 47 ++++++++++++++++++++++++++++++++++-------- arch/x86/kvm/svm/svm.c | 20 +++++++++--------- arch/x86/kvm/svm/svm.h | 17 ++++++++++++++- 4 files changed, 66 insertions(+), 21 deletions(-)
arch/x86/kvm/Kconfig +2 -1
@@ -99,7 +99,8 @@ config KVM_AMD_SEV depends on KVM_AMD && X86_64 depends on CRYPTO_DEV_SP_PSP && !(KVM_AMD=y && CRYPTO_DEV_CCP_DD=m) help - Provides support for launching Encrypted VMs on AMD processors. + Provides support for launching Encrypted VMs (SEV) and Encrypted VMs + with Encrypted State (SEV-ES) on AMD processors. config KVM_MMU_AUDIT bool "Audit KVM MMU"
arch/x86/kvm/svm/sev.c +38 -9
74 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +10 -10
@@ -187,9 +187,13 @@ static int vgif = true;module_param (vgif, int,0444 );/* enable/disable SEV support */ -static int sev =IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT ); +int sev =IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT );module_param (sev, int,0444 ); +/* enable/disable SEV-ES support */ +int sev_es =IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT ); +module_param (sev_es, int,0444 ); +static bool __read_mostly dump_invalid_vmcb =0 ;module_param (dump_invalid_vmcb, bool,0644 );
@@ -938,15 +942,11 @@ static __init int svm_hardware_setup(void)kvm_enable_efer_bits (EFER_SVME |EFER_LMSLE ); } -if (sev) { -if (boot_cpu_has (X86_FEATURE_SEV ) && -IS_ENABLED (CONFIG_KVM_AMD_SEV )) { - r =sev_hardware_setup (); -if (r) - sev = false; - }else { - sev = false; - } +if (IS_ENABLED (CONFIG_KVM_AMD_SEV ) && sev) { +sev_hardware_setup (); + }else { + sev = false; + sev_es = false; }svm_adjust_mmio_mask ();
arch/x86/kvm/svm/svm.h +16 -1
@@ -61,6 +61,7 @@ enum {struct kvm_sev_info {bool active;/* SEV enabled guest */ +bool es_active;/* SEV-ES enabled guest */ unsigned int asid;/* ASID used for this guest */ unsigned int handle;/* SEV firmware handle */ int fd;/* SEV device fd */
@@ -349,6 +350,9 @@ static inline bool gif_set(struct vcpu_svm *svm)#define MSR_CR3_LONG_MBZ_MASK 0xfff0000000000000U#define MSR_INVALID 0xffffffffU +extern int sev; +extern int sev_es; +u32 svm_msrpm_offset (u32 msr);void svm_set_efer (struct kvm_vcpu *vcpu,u64 efer);void svm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);
@@ -475,6 +479,17 @@ static inline bool sev_guest(struct kvm *kvm)#endif } +static inline bool sev_es_guest (struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; + +return sev_guest (kvm) && sev->es_active; +#else +return false; +#endif +} +static inline bool svm_sev_enabled (void ) {return IS_ENABLED (CONFIG_KVM_AMD_SEV ) ? max_sev_asid :0 ;
@@ -487,7 +502,7 @@ int svm_register_enc_region(struct kvm *kvm,int svm_unregister_enc_region (struct kvm *kvm,struct kvm_enc_region *range);void pre_sev_run (struct vcpu_svm *svm,int cpu); -int __init sev_hardware_setup (void ); +void __init sev_hardware_setup (void );void sev_hardware_teardown (void );#endif
Signature · 1 line
2.28.0
RFC PATCH v2 3/33 KVM: SVM: Add GHCB accessor functions for retrieving fields
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Update the GHCB accessor functions to add functions for retrieve GHCB fields by name. Update existing code to use the new accessor functions.
--- arch/x86/include/asm/svm.h | 10 ++++++++++ arch/x86/kernel/cpu/vmware.c | 12 ++++++------ 2 files changed, 16 insertions(+), 6 deletions(-)
arch/x86/include/asm/svm.h +10 -0
@@ -379,6 +379,16 @@ struct vmcb { (unsigned long *)&ghcb->save.valid_bitmap); \ } \ \ + static inline u64 ghcb_get_##field(struct ghcb *ghcb) \ + { \ + return ghcb->save.field; \ + } \ + \ + static inline u64 ghcb_get_##field##_if_valid(struct ghcb *ghcb) \ + { \ + return ghcb_##field##_is_valid(ghcb) ? ghcb->save.field : 0; \ + } \ + \ static inline void ghcb_set_##field(struct ghcb *ghcb, u64 value) \ { \ __set_bit(GHCB_BITMAP_IDX(field), \
arch/x86/kernel/cpu/vmware.c +6 -6
@@ -501,12 +501,12 @@ static bool vmware_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs) ghcb_rbp_is_valid(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; + regs->bx =ghcb_get_rbx (ghcb); + regs->cx =ghcb_get_rcx (ghcb); + regs->dx =ghcb_get_rdx (ghcb); + regs->si =ghcb_get_rsi (ghcb); + regs->di =ghcb_get_rdi (ghcb); + regs->bp =ghcb_get_rbp (ghcb);return true; }
Signature · 1 line
2.28.0
RFC PATCH v2 4/33 KVM: SVM: Add support for the SEV-ES VMSA
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Allocate a page during vCPU creation to be used as the encrypted VM save area (VMSA) for the SEV-ES guest. Provide a flag in the kvm_vcpu_arch structure that indicates whether the guest state is protected.
When freeing a VMSA page that has been encrypted, the cache contents must be flushed using the MSR_AMD64_VM_PAGE_FLUSH before freeing the page.
--- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/svm/svm.c | 42 ++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 4 +++ 4 files changed, 48 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +3 -0
@@ -789,6 +789,9 @@ struct kvm_vcpu_arch {/* AMD MSRC001_0015 Hardware Configuration */ u64 msr_hwcr; + +/* Protected Guests */ +bool guest_state_protected; };struct kvm_lpage_info {
arch/x86/include/asm/msr-index.h +1 -0
@@ -466,6 +466,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_VM_PAGE_FLUSH 0xc001011e#define MSR_AMD64_SEV_ES_GHCB 0xc0010130#define MSR_AMD64_SEV 0xc0010131#define MSR_AMD64_SEV_ENABLED_BIT 0
arch/x86/kvm/svm/svm.c +40 -2
80 lines not on this page · raw message
arch/x86/kvm/svm/svm.h +4 -0
@@ -165,6 +165,10 @@ struct vcpu_svm {DECLARE_BITMAP (read,MAX_DIRECT_ACCESS_MSRS );DECLARE_BITMAP (write,MAX_DIRECT_ACCESS_MSRS ); } shadow_msr_intercept; + +/* SEV-ES support */ +struct vmcb_save_area *vmsa; +struct ghcb *ghcb; };struct svm_cpu_data {
Signature · 1 line
2.28.0
RFC PATCH v2 5/33 KVM: x86: Mark GPRs dirty when written
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
When performing VMGEXIT processing for an SEV-ES guest, register values will be synced between KVM and the GHCB. Prepare for detecting when a GPR has been updated (marked dirty) in order to determine whether to sync the register to the GHCB.
--- arch/x86/kvm/kvm_cache_regs.h | 51 ++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 25 deletions(-)
arch/x86/kvm/kvm_cache_regs.h +26 -25
@@ -9,6 +9,31 @@ (X86_CR4_PVI |X86_CR4_DE |X86_CR4_PCE |X86_CR4_OSFXSR \ | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_PGE | X86_CR4_TSD) +static inline bool kvm_register_is_available (struct kvm_vcpu *vcpu, +enum kvm_reg reg) +{ +return test_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); +} + +static inline bool kvm_register_is_dirty (struct kvm_vcpu *vcpu, +enum kvm_reg reg) +{ +return test_bit (reg, (unsigned long *)&vcpu->arch.regs_dirty); +} + +static inline void kvm_register_mark_available (struct kvm_vcpu *vcpu, +enum kvm_reg reg) +{ +__set_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); +} + +static inline void kvm_register_mark_dirty (struct kvm_vcpu *vcpu, +enum kvm_reg reg) +{ +__set_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); +__set_bit (reg, (unsigned long *)&vcpu->arch.regs_dirty); +} +#define BUILD_KVM_GPR_ACCESSORS (lname, uname) \ static __always_inline unsigned long kvm_##lname##_read(struct kvm_vcpu *vcpu)\ { \
@@ -18,6 +43,7 @@ static __always_inline void kvm_##lname##_write(struct kvm_vcpu *vcpu, \ unsigned long val) \ { \ vcpu->arch.regs[VCPU_REGS_##uname] = val; \ + kvm_register_mark_dirty(vcpu, VCPU_REGS_##uname); \ } BUILD_KVM_GPR_ACCESSORS(rax, RAX)BUILD_KVM_GPR_ACCESSORS (rbx,RBX )
@@ -37,31 +63,6 @@ BUILD_KVM_GPR_ACCESSORS(r14, R14) BUILD_KVM_GPR_ACCESSORS(r15, R15)#endif -static inline bool kvm_register_is_available (struct kvm_vcpu *vcpu, -enum kvm_reg reg) -{ -return test_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); -} - -static inline bool kvm_register_is_dirty (struct kvm_vcpu *vcpu, -enum kvm_reg reg) -{ -return test_bit (reg, (unsigned long *)&vcpu->arch.regs_dirty); -} - -static inline void kvm_register_mark_available (struct kvm_vcpu *vcpu, -enum kvm_reg reg) -{ -__set_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); -} - -static inline void kvm_register_mark_dirty (struct kvm_vcpu *vcpu, -enum kvm_reg reg) -{ -__set_bit (reg, (unsigned long *)&vcpu->arch.regs_avail); -__set_bit (reg, (unsigned long *)&vcpu->arch.regs_dirty); -} -static inline unsigned long kvm_register_read (struct kvm_vcpu *vcpu,int reg) {if (WARN_ON_ONCE ((unsigned int )reg >=NR_VCPU_REGS ))
Signature · 1 line
2.28.0
RFC PATCH v2 6/33 KVM: SVM: Add required changes to support intercepts under SEV-ES
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
When a guest is running under SEV-ES, the hypervisor cannot access the guest register state. There are numerous places in the KVM code where certain registers are accessed that are not allowed to be accessed (e.g. RIP, CR0, etc). Add checks to prevent register accesses and add intercept update support at various points within the KVM code.
Also, when handling a VMGEXIT, exceptions are passed back through the GHCB. Since the RDMSR/WRMSR intercepts (may) inject a #GP on error, update the SVM intercepts to handle this for SEV-ES guests.
--- arch/x86/include/asm/svm.h | 3 +- arch/x86/kvm/svm/svm.c | 111 +++++++++++++++++++++++++++++++++---- arch/x86/kvm/x86.c | 6 +- 3 files changed, 107 insertions(+), 13 deletions(-)
arch/x86/include/asm/svm.h +2 -1
@@ -178,7 +178,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define LBR_CTL_ENABLE_MASK BIT_ULL(0)#define VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK BIT_ULL(1) -#define SVM_INTERRUPT_SHADOW_MASK 1 +#define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0) +#define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1)#define SVM_IOIO_STR_SHIFT 2#define SVM_IOIO_REP_SHIFT 3
arch/x86/kvm/svm/svm.c +101 -10
184 lines not on this page · raw message
arch/x86/kvm/x86.c +4 -2
@@ -3909,7 +3909,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) {int idx; -if (vcpu->preempted) +if (vcpu->preempted && !vcpu->arch.guest_state_protected) vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl (vcpu);/*
@@ -8043,7 +8043,9 @@ static void post_kvm_run_save(struct kvm_vcpu *vcpu) {struct kvm_run *kvm_run = vcpu->run; - kvm_run->if_flag = (kvm_get_rflags (vcpu) &X86_EFLAGS_IF ) !=0 ; + kvm_run->if_flag = (vcpu->arch.guest_state_protected) + ?kvm_arch_interrupt_allowed (vcpu) + : (kvm_get_rflags (vcpu) &X86_EFLAGS_IF ) !=0 ; kvm_run->flags =is_smm (vcpu) ?KVM_RUN_X86_SMM :0 ; kvm_run->cr8 =kvm_get_cr8 (vcpu); kvm_run->apic_base =kvm_get_apic_base (vcpu);
Signature · 1 line
2.28.0
RFC PATCH v2 7/33 KVM: SVM: Prevent debugging under SEV-ES
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Since the guest register state of an SEV-ES guest is encrypted, debugging is not supported. Update the code to prevent guest debugging when the guest has protected state.
Additionally, an SEV-ES guest must only and always intercept DR7 reads and writes. Update set_dr_intercepts() and clr_dr_intercepts() to account for this.
--- arch/x86/kvm/svm/svm.c | 9 +++++ arch/x86/kvm/svm/svm.h | 81 +++++++++++++++++++++++------------------- arch/x86/kvm/x86.c | 3 ++ 3 files changed, 57 insertions(+), 36 deletions(-)
arch/x86/kvm/svm/svm.c +9 -0
@@ -1817,6 +1817,9 @@ static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value) {struct vmcb *vmcb = svm->vmcb; +if (svm->vcpu.arch.guest_state_protected) +return ; +if (unlikely (value != vmcb->save.dr6)) { vmcb->save.dr6 = value;vmcb_mark_dirty (vmcb,VMCB_DR );
@@ -1827,6 +1830,9 @@ static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); +if (vcpu->arch.guest_state_protected) +return ; +get_debugreg (vcpu->arch.db[0 ],0 );get_debugreg (vcpu->arch.db[1 ],1 );get_debugreg (vcpu->arch.db[2 ],2 );
@@ -1845,6 +1851,9 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) {struct vcpu_svm *svm =to_svm (vcpu); +if (vcpu->arch.guest_state_protected) +return ; + svm->vmcb->save.dr7 = value;vmcb_mark_dirty (svm->vmcb,VMCB_DR ); }
arch/x86/kvm/svm/svm.h +45 -36
106 lines not on this page · raw message
arch/x86/kvm/x86.c +3 -0
@@ -9550,6 +9550,9 @@ int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,unsigned long rflags;int i, r; +if (vcpu->arch.guest_state_protected) +return -EINVAL ; +vcpu_load (vcpu);if (dbg->control & (KVM_GUESTDBG_INJECT_DB |KVM_GUESTDBG_INJECT_BP )) {
Signature · 1 line
2.28.0
RFC PATCH v2 8/33 KVM: SVM: Do not allow instruction emulation under SEV-ES
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
When a guest is running as an SEV-ES guest, it is not possible to emulate instructions. Add support to prevent instruction emulation.
--- arch/x86/kvm/svm/svm.c | 6 ++++++ 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/svm.c +6 -0
@@ -4201,6 +4201,12 @@ static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int ibool smep, smap, is_user;unsigned long cr4; +/* +* When the guest is an SEV-ES guest, emulation is not possible. +*/ +if (sev_es_guest (vcpu->kvm)) +return false; +/* * Detect and workaround Errata 1096 Fam_17h_00_0Fh. *
Signature · 1 line
2.28.0
RFC PATCH v2 9/33 KVM: SVM: Cannot re-initialize the VMCB after shutdown with SEV-ES
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
When a SHUTDOWN VMEXIT is encountered, normally the VMCB is re-initialized so that the guest can be re-launched. But when a guest is running as an SEV-ES guest, the VMSA cannot be re-initialized because it has been encrypted. For now, just return -EINVAL to prevent a possible attempt at a guest reset.
--- arch/x86/kvm/svm/svm.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kvm/svm/svm.c +7 -0
@@ -2041,6 +2041,13 @@ static int shutdown_interception(struct vcpu_svm *svm) {struct kvm_run *kvm_run = svm->vcpu.run; +/* +* The VM save area has already been encrypted so it +* cannot be reinitialized - just terminate. +*/ +if (sev_es_guest (svm->vcpu.kvm)) +return -EINVAL ; +/* * VMCB is undefined after a SHUTDOWN intercept * so reinitialize it.
Signature · 1 line
2.28.0
RFC PATCH v2 10/33 KVM: SVM: Prepare for SEV-ES exit handling in the sev.c file
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
This is a pre-patch to consolidate some exit handling code into callable functions. Follow-on patches for SEV-ES exit handling will then be able to use them from the sev.c file.
--- arch/x86/kvm/svm/svm.c | 64 +++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 26 deletions(-)
arch/x86/kvm/svm/svm.c +38 -26
@@ -3156,6 +3156,43 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) "excp_to:", save->last_excp_to); } +static int svm_handle_invalid_exit (struct kvm_vcpu *vcpu,u64 exit_code) +{ +if (exit_code <ARRAY_SIZE (svm_exit_handlers) && + svm_exit_handlers[exit_code]) +return 0 ; + +vcpu_unimpl (vcpu,"svm: unexpected exit reason 0x%llx\n" , exit_code); +dump_vmcb (vcpu); + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON ; + vcpu->run->internal.ndata =2 ; + vcpu->run->internal.data[0 ] = exit_code; + vcpu->run->internal.data[1 ] = vcpu->arch.last_vmentry_cpu; + +return -EINVAL ; +} + +static int svm_invoke_exit_handler (struct vcpu_svm *svm,u64 exit_code) +{ +if (svm_handle_invalid_exit (&svm->vcpu, exit_code)) +return 0 ; + +#ifdef CONFIG_RETPOLINE +if (exit_code ==SVM_EXIT_MSR ) +return msr_interception (svm); +else if (exit_code ==SVM_EXIT_VINTR ) +return interrupt_window_interception (svm); +else if (exit_code ==SVM_EXIT_INTR ) +return intr_interception (svm); +else if (exit_code ==SVM_EXIT_HLT ) +return halt_interception (svm); +else if (exit_code ==SVM_EXIT_NPF ) +return npf_interception (svm); +#endif +return svm_exit_handlers[exit_code](svm); +} +static void svm_get_exit_info (struct kvm_vcpu *vcpu,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {
@@ -3222,32 +3259,7 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)if (exit_fastpath !=EXIT_FASTPATH_NONE )return 1 ; -if (exit_code >=ARRAY_SIZE (svm_exit_handlers) - || !svm_exit_handlers[exit_code]) { -vcpu_unimpl (vcpu,"svm: unexpected exit reason 0x%x\n" , exit_code); -dump_vmcb (vcpu); - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror = -KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON ; - vcpu->run->internal.ndata =2 ; - vcpu->run->internal.data[0 ] = exit_code; - vcpu->run->internal.data[1 ] = vcpu->arch.last_vmentry_cpu; -return 0 ; - } - -#ifdef CONFIG_RETPOLINE -if (exit_code ==SVM_EXIT_MSR ) -return msr_interception (svm); -else if (exit_code ==SVM_EXIT_VINTR ) -return interrupt_window_interception (svm); -else if (exit_code ==SVM_EXIT_INTR ) -return intr_interception (svm); -else if (exit_code ==SVM_EXIT_HLT ) -return halt_interception (svm); -else if (exit_code ==SVM_EXIT_NPF ) -return npf_interception (svm); -#endif -return svm_exit_handlers[exit_code](svm); +return svm_invoke_exit_handler (svm, exit_code); }static void reload_tss (struct kvm_vcpu *vcpu)
Signature · 1 line
2.28.0
RFC PATCH v2 11/33 KVM: SVM: Add initial support for a VMGEXIT VMEXIT
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
SEV-ES adds a new VMEXIT reason code, VMGEXIT. Initial support for a VMGEXIT includes mapping the GHCB based on the guest GPA, which is obtained from a new VMCB field, and then validating the required inputs for the VMGEXIT exit reason.
Since many of the VMGEXIT exit reasons correspond to existing VMEXIT reasons, the information from the GHCB is copied into the VMCB control exit code areas and KVM register areas. The standard exit handlers are invoked, similar to standard VMEXIT processing. Before restarting the vCPU, the GHCB is updated with any registers that have been updated by the hypervisor.
--- arch/x86/include/asm/svm.h | 2 +- arch/x86/include/uapi/asm/svm.h | 7 + arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/svm/sev.c | 247 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 8 +- arch/x86/kvm/svm/svm.h | 8 ++ 6 files changed, 270 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +1 -1
@@ -130,7 +130,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {u32 exit_int_info_err;u64 nested_ctl;u64 avic_vapic_bar; -u8 reserved_4[8 ]; +u64 ghcb_gpa;u32 event_inj;u32 event_inj_err;u64 nested_cr3;
arch/x86/include/uapi/asm/svm.h +7 -0
@@ -81,6 +81,7 @@#define SVM_EXIT_NPF 0x400#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401#define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402 +#define SVM_EXIT_VMGEXIT 0x403/* SEV-ES software-defined VMGEXIT events */ #define SVM_VMGEXIT_MMIO_READ 0x80000001
@@ -187,6 +188,12 @@ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \ { SVM_EXIT_AVIC_UNACCELERATED_ACCESS, "avic_unaccelerated_access" }, \ + { SVM_EXIT_VMGEXIT, "vmgexit" }, \ + { SVM_VMGEXIT_MMIO_READ, "vmgexit_mmio_read" }, \ + { SVM_VMGEXIT_MMIO_WRITE, "vmgexit_mmio_write" }, \ + { SVM_VMGEXIT_NMI_COMPLETE, "vmgexit_nmi_complete" }, \ + { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ + { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ { SVM_EXIT_ERR, "invalid_guest_state" }
arch/x86/kvm/cpuid.c +1 -0
@@ -115,6 +115,7 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) MSR_IA32_MISC_ENABLE_MWAIT); } } +EXPORT_SYMBOL_GPL (kvm_update_cpuid_runtime);static void kvm_vcpu_after_set_cpuid (struct kvm_vcpu *vcpu) {
arch/x86/kvm/svm/sev.c +247 -0
267 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +6 -2
@@ -195,7 +195,7 @@ module_param(sev, int, 0444);int sev_es =IS_ENABLED (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT );module_param (sev_es, int,0444 ); -static bool __read_mostly dump_invalid_vmcb =0 ; +bool __read_mostly dump_invalid_vmcb =0 ;module_param (dump_invalid_vmcb, bool,0644 );static u8 rsm_ins_bytes[] ="\x0f\xaa" ;
@@ -3036,6 +3036,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_RSM] = rsm_interception, [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception, [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception, + [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit, };static void dump_vmcb (struct kvm_vcpu *vcpu)
@@ -3077,6 +3078,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)pr_err ("%-20s%lld\n" ,"nested_ctl:" , control->nested_ctl);pr_err ("%-20s%016llx\n" ,"nested_cr3:" , control->nested_cr3);pr_err ("%-20s%016llx\n" ,"avic_vapic_bar:" , control->avic_vapic_bar); +pr_err ("%-20s%016llx\n" ,"ghcb:" , control->ghcb_gpa);pr_err ("%-20s%08x\n" ,"event_inj:" , control->event_inj);pr_err ("%-20s%08x\n" ,"event_inj_err:" , control->event_inj_err);pr_err ("%-20s%lld\n" ,"virt_ext:" , control->virt_ext);
@@ -3173,7 +3175,7 @@ static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)return -EINVAL ; } -static int svm_invoke_exit_handler (struct vcpu_svm *svm,u64 exit_code) +int svm_invoke_exit_handler (struct vcpu_svm *svm,u64 exit_code) {if (svm_handle_invalid_exit (&svm->vcpu, exit_code))return 0 ;
@@ -3189,6 +3191,8 @@ static int svm_invoke_exit_handler(struct vcpu_svm *svm, u64 exit_code)return halt_interception (svm); else if (exit_code == SVM_EXIT_NPF) return npf_interception(svm); + else if (exit_code == SVM_EXIT_VMGEXIT) + return sev_handle_vmgexit(svm);#endif return svm_exit_handlers[exit_code](svm); }
arch/x86/kvm/svm/svm.h +8 -0
@@ -17,6 +17,7 @@#include <linux/kvm_types.h> #include <linux/kvm_host.h> +#include <linux/bits.h> #include <asm/svm.h>
@@ -169,6 +170,7 @@ struct vcpu_svm {/* SEV-ES support */ struct vmcb_save_area *vmsa;struct ghcb *ghcb; +struct kvm_host_map ghcb_map; };struct svm_cpu_data {
@@ -387,6 +389,7 @@ static inline bool gif_set(struct vcpu_svm *svm)extern int sev;extern int sev_es; +extern bool dump_invalid_vmcb;u32 svm_msrpm_offset (u32 msr);void svm_set_efer (struct kvm_vcpu *vcpu,u64 efer);
@@ -398,6 +401,7 @@ bool svm_smi_blocked(struct kvm_vcpu *vcpu);bool svm_nmi_blocked (struct kvm_vcpu *vcpu);bool svm_interrupt_blocked (struct kvm_vcpu *vcpu);void svm_set_gif (struct vcpu_svm *svm,bool value); +int svm_invoke_exit_handler (struct vcpu_svm *svm,u64 exit_code);/* nested.c */
@@ -501,6 +505,9 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);/* sev.c */ +#define GHCB_MSR_INFO_POS 0 +#define GHCB_MSR_INFO_MASK (BIT_ULL(12) - 1) +extern unsigned int max_sev_asid;static inline bool svm_sev_enabled (void )
@@ -517,5 +524,6 @@ int svm_unregister_enc_region(struct kvm *kvm,void pre_sev_run (struct vcpu_svm *svm,int cpu);void __init sev_hardware_setup (void );void sev_hardware_teardown (void ); +int sev_handle_vmgexit (struct vcpu_svm *svm);#endif
Signature · 1 line
2.28.0
RFC PATCH v2 12/33 KVM: SVM: Create trace events for VMGEXIT processing
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Add trace events for entry to and exit from VMGEXIT processing. The vCPU id and the exit reason will be common for the trace events. The exit info fields will represent the input and output values for the entry and exit events, respectively.
--- arch/x86/kvm/svm/sev.c | 6 +++++ arch/x86/kvm/trace.h | 53 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 2 ++ 3 files changed, 61 insertions(+)
arch/x86/kvm/svm/sev.c +6 -0
@@ -14,10 +14,12 @@#include <linux/psp-sev.h> #include <linux/pagemap.h> #include <linux/swap.h> +#include <linux/trace_events.h> #include "x86.h" #include "svm.h" #include "cpuid.h" +#include "trace.h" static int sev_flush_asids (void );static DECLARE_RWSEM (sev_deactivate_lock);
@@ -1372,6 +1374,8 @@ static void pre_sev_es_run(struct vcpu_svm *svm)if (!svm->ghcb)return ; +trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, svm->ghcb); +sev_es_sync_to_ghcb (svm);kvm_vcpu_unmap (&svm->vcpu, &svm->ghcb_map, true);
@@ -1436,6 +1440,8 @@ int sev_handle_vmgexit(struct vcpu_svm *svm) svm->ghcb = svm->ghcb_map.hva; ghcb = svm->ghcb_map.hva; +trace_kvm_vmgexit_enter (svm->vcpu.vcpu_id, ghcb); + exit_code =ghcb_get_sw_exit_code (ghcb); ret =sev_es_validate_vmgexit (svm);
arch/x86/kvm/trace.h +53 -0
59 lines not on this page · raw message
arch/x86/kvm/x86.c +2 -0
@@ -11174,3 +11174,5 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_avic_incomplete_ipi);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_avic_ga_log);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_apicv_update_request); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_enter); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_exit);
Signature · 1 line
2.28.0
RFC PATCH v2 13/33 KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x002
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The GHCB specification defines a GHCB MSR protocol using the lower 12-bits of the GHCB MSR (in the hypervisor this corresponds to the GHCB GPA field in the VMCB).
Function 0x002 is a request to set the GHCB MSR value to the SEV INFO as per the specification via the VMCB GHCB GPA field.
--- arch/x86/kvm/svm/sev.c | 26 +++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 17 +++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/sev.c +25 -1
@@ -21,6 +21,7 @@#include "cpuid.h" #include "trace.h" +static u8 sev_enc_bit;static int sev_flush_asids (void );static DECLARE_RWSEM (sev_deactivate_lock);static DEFINE_MUTEX (sev_bitmap_lock);
@@ -1140,6 +1141,9 @@ void __init sev_hardware_setup(void)/* Retrieve SEV CPUID information */ cpuid (0x8000001f , &eax, &ebx, &ecx, &edx); +/* Set encryption bit location for SEV-ES guests */ + sev_enc_bit = ebx &0x3f ; +/* Maximum number of encrypted guests supported simultaneously */ max_sev_asid = ecx;
@@ -1408,9 +1412,29 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)vmcb_mark_dirty (svm->vmcb,VMCB_ASID ); } +static void set_ghcb_msr (struct vcpu_svm *svm,u64 value) +{ + svm->vmcb->control.ghcb_gpa = value; +} +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) { -return -EINVAL ; +struct vmcb_control_area *control = &svm->vmcb->control; +u64 ghcb_info; + + ghcb_info = control->ghcb_gpa &GHCB_MSR_INFO_MASK ; + +switch (ghcb_info) { +case GHCB_MSR_SEV_INFO_REQ : +set_ghcb_msr (svm,GHCB_MSR_SEV_INFO (GHCB_VERSION_MAX , +GHCB_VERSION_MIN , + sev_enc_bit)); +break ; +default : +return -EINVAL ; + } + +return 1 ; }int sev_handle_vmgexit (struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.h +17 -0
@@ -505,9 +505,26 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);/* sev.c */ +#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MIN 1ULL +#define GHCB_MSR_INFO_POS 0#define GHCB_MSR_INFO_MASK (BIT_ULL(12) - 1) +#define GHCB_MSR_SEV_INFO_RESP 0x001 +#define GHCB_MSR_SEV_INFO_REQ 0x002 +#define GHCB_MSR_VER_MAX_POS 48 +#define GHCB_MSR_VER_MAX_MASK 0xffff +#define GHCB_MSR_VER_MIN_POS 32 +#define GHCB_MSR_VER_MIN_MASK 0xffff +#define GHCB_MSR_CBIT_POS 24 +#define GHCB_MSR_CBIT_MASK 0xff +#define GHCB_MSR_SEV_INFO (_max, _min, _cbit) \ + ((((_max) & GHCB_MSR_VER_MAX_MASK) << GHCB_MSR_VER_MAX_POS) | \ + (((_min) & GHCB_MSR_VER_MIN_MASK) << GHCB_MSR_VER_MIN_POS) | \ + (((_cbit) & GHCB_MSR_CBIT_MASK) << GHCB_MSR_CBIT_POS) | \ + GHCB_MSR_SEV_INFO_RESP) +extern unsigned int max_sev_asid;static inline bool svm_sev_enabled (void )
Signature · 1 line
2.28.0
RFC PATCH v2 14/33 KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x004
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The GHCB specification defines a GHCB MSR protocol using the lower 12-bits of the GHCB MSR (in the hypervisor this corresponds to the GHCB GPA field in the VMCB).
Function 0x004 is a request for CPUID information. Only a single CPUID result register can be sent per invocation, so the protocol defines the register that is requested. The GHCB MSR value is set to the CPUID register value as per the specification via the VMCB GHCB GPA field.
--- arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 9 +++++++ 2 files changed, 63 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +54 -2
@@ -1412,6 +1412,18 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)vmcb_mark_dirty (svm->vmcb,VMCB_ASID ); } +static void set_ghcb_msr_bits (struct vcpu_svm *svm,u64 value,u64 mask, +unsigned int pos) +{ + svm->vmcb->control.ghcb_gpa &= ~(mask << pos); + svm->vmcb->control.ghcb_gpa |= (value & mask) << pos; +} + +static u64 get_ghcb_msr_bits (struct vcpu_svm *svm,u64 mask,unsigned int pos) +{ +return (svm->vmcb->control.ghcb_gpa >> pos) & mask; +} +static void set_ghcb_msr (struct vcpu_svm *svm,u64 value) { svm->vmcb->control.ghcb_gpa = value;
@@ -1420,7 +1432,9 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) {struct vmcb_control_area *control = &svm->vmcb->control; +struct kvm_vcpu *vcpu = &svm->vcpu;u64 ghcb_info; +int ret =1 ; ghcb_info = control->ghcb_gpa &GHCB_MSR_INFO_MASK ;
@@ -1430,11 +1444,49 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)GHCB_VERSION_MIN , sev_enc_bit));break ; +case GHCB_MSR_CPUID_REQ : { +u64 cpuid_fn, cpuid_reg, cpuid_value; + + cpuid_fn =get_ghcb_msr_bits (svm, +GHCB_MSR_CPUID_FUNC_MASK , +GHCB_MSR_CPUID_FUNC_POS ); + +/* Initialize the registers needed by the CPUID intercept */ + vcpu->arch.regs[VCPU_REGS_RAX ] = cpuid_fn; + vcpu->arch.regs[VCPU_REGS_RCX ] =0 ; + + ret =svm_invoke_exit_handler (svm,SVM_EXIT_CPUID ); +if (!ret) { + ret = -EINVAL ; +break ; + } + + cpuid_reg =get_ghcb_msr_bits (svm, +GHCB_MSR_CPUID_REG_MASK , +GHCB_MSR_CPUID_REG_POS ); +if (cpuid_reg ==0 ) + cpuid_value = vcpu->arch.regs[VCPU_REGS_RAX ]; +else if (cpuid_reg ==1 ) + cpuid_value = vcpu->arch.regs[VCPU_REGS_RBX ]; +else if (cpuid_reg ==2 ) + cpuid_value = vcpu->arch.regs[VCPU_REGS_RCX ]; +else + cpuid_value = vcpu->arch.regs[VCPU_REGS_RDX ]; + +set_ghcb_msr_bits (svm, cpuid_value, +GHCB_MSR_CPUID_VALUE_MASK , +GHCB_MSR_CPUID_VALUE_POS ); + +set_ghcb_msr_bits (svm,GHCB_MSR_CPUID_RESP , +GHCB_MSR_INFO_MASK , +GHCB_MSR_INFO_POS ); +break ; + }default : -return -EINVAL ; + ret = -EINVAL ; } -return 1 ; +return ret; }int sev_handle_vmgexit (struct vcpu_svm *svm)
arch/x86/kvm/svm/svm.h +9 -0
@@ -525,6 +525,15 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); (((_cbit) & GHCB_MSR_CBIT_MASK) << GHCB_MSR_CBIT_POS) | \ GHCB_MSR_SEV_INFO_RESP) +#define GHCB_MSR_CPUID_REQ 0x004 +#define GHCB_MSR_CPUID_RESP 0x005 +#define GHCB_MSR_CPUID_FUNC_POS 32 +#define GHCB_MSR_CPUID_FUNC_MASK 0xffffffff +#define GHCB_MSR_CPUID_VALUE_POS 32 +#define GHCB_MSR_CPUID_VALUE_MASK 0xffffffff +#define GHCB_MSR_CPUID_REG_POS 30 +#define GHCB_MSR_CPUID_REG_MASK 0x3 +extern unsigned int max_sev_asid;static inline bool svm_sev_enabled (void )
Signature · 1 line
2.28.0
RFC PATCH v2 15/33 KVM: SVM: Add support for SEV-ES GHCB MSR protocol function 0x100
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The GHCB specification defines a GHCB MSR protocol using the lower 12-bits of the GHCB MSR (in the hypervisor this corresponds to the GHCB GPA field in the VMCB).
Function 0x100 is a request for termination of the guest. The guest has encountered some situation for which it has requested to be terminated. The GHCB MSR value contains the reason for the request.
--- arch/x86/kvm/svm/sev.c | 13 +++++++++++++ arch/x86/kvm/svm/svm.h | 6 ++++++ 2 files changed, 19 insertions(+)
arch/x86/kvm/svm/sev.c +13 -0
@@ -1482,6 +1482,19 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS);break ; } +case GHCB_MSR_TERM_REQ : { +u64 reason_set, reason_code; + + reason_set =get_ghcb_msr_bits (svm, +GHCB_MSR_TERM_REASON_SET_MASK , +GHCB_MSR_TERM_REASON_SET_POS ); + reason_code =get_ghcb_msr_bits (svm, +GHCB_MSR_TERM_REASON_MASK , +GHCB_MSR_TERM_REASON_POS ); +pr_info ("SEV-ES guest requested termination: %#llx:%#llx\n" , + reason_set, reason_code); + fallthrough; + }default : ret = -EINVAL ; }
arch/x86/kvm/svm/svm.h +6 -0
@@ -534,6 +534,12 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu);#define GHCB_MSR_CPUID_REG_POS 30#define GHCB_MSR_CPUID_REG_MASK 0x3 +#define GHCB_MSR_TERM_REQ 0x100 +#define GHCB_MSR_TERM_REASON_SET_POS 12 +#define GHCB_MSR_TERM_REASON_SET_MASK 0xf +#define GHCB_MSR_TERM_REASON_POS 16 +#define GHCB_MSR_TERM_REASON_MASK 0xff +extern unsigned int max_sev_asid;static inline bool svm_sev_enabled (void )
Signature · 1 line
2.28.0
RFC PATCH v2 16/33 KVM: SVM: Create trace events for VMGEXIT MSR protocol processing
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Add trace events for entry to and exit from VMGEXIT MSR protocol processing. The vCPU will be common for the trace events. The MSR protocol processing is guided by the GHCB GPA in the VMCB, so the GHCB GPA will represent the input and output values for the entry and exit events, respectively. Additionally, the exit event will contain the return code for the event.
--- arch/x86/kvm/svm/sev.c | 6 ++++++ arch/x86/kvm/trace.h | 44 ++++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 2 ++ 3 files changed, 52 insertions(+)
arch/x86/kvm/svm/sev.c +6 -0
@@ -1438,6 +1438,9 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) ghcb_info = control->ghcb_gpa &GHCB_MSR_INFO_MASK ; +trace_kvm_vmgexit_msr_protocol_enter (svm->vcpu.vcpu_id, + control->ghcb_gpa); +switch (ghcb_info) {case GHCB_MSR_SEV_INFO_REQ : set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
@@ -1499,6 +1502,9 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) ret = -EINVAL ; } +trace_kvm_vmgexit_msr_protocol_exit (svm->vcpu.vcpu_id, + control->ghcb_gpa, ret); +return ret; }
arch/x86/kvm/trace.h +44 -0
50 lines not on this page · raw message
arch/x86/kvm/x86.c +2 -0
@@ -11176,3 +11176,5 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_apicv_update_request);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_enter);EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_enter); +EXPORT_TRACEPOINT_SYMBOL_GPL (kvm_vmgexit_msr_protocol_exit);
Signature · 1 line
2.28.0
RFC PATCH v2 17/33 KVM: SVM: Support MMIO for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For an SEV-ES guest, MMIO is performed to a shared (un-encrypted) page so that both the hypervisor and guest can read or write to it and each see the contents.
The GHCB specification provides software-defined VMGEXIT exit codes to indicate a request for an MMIO read or an MMIO write. Add support to recognize the MMIO requests and invoke SEV-ES specific routines that can complete the MMIO operation. These routines use common KVM support to complete the MMIO operation.
--- arch/x86/kvm/svm/sev.c | 121 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 3 + arch/x86/kvm/svm/svm.h | 6 ++ arch/x86/kvm/x86.c | 123 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 5 ++ 5 files changed, 258 insertions(+)
arch/x86/kvm/svm/sev.c +121 -0
@@ -1350,6 +1350,11 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) !ghcb_rcx_is_valid(ghcb)) goto vmgexit_err;break ; +case SVM_VMGEXIT_MMIO_READ : +case SVM_VMGEXIT_MMIO_WRITE : +if (!ghcb_sw_scratch_is_valid (ghcb)) + goto vmgexit_err; +break ;case SVM_VMGEXIT_UNSUPPORTED_EVENT :break ;default :
@@ -1378,6 +1383,24 @@ static void pre_sev_es_run(struct vcpu_svm *svm)if (!svm->ghcb)return ; +if (svm->ghcb_sa_free) { +/* +* The scratch area lives outside the GHCB, so there is a +* buffer that, depending on the operation performed, may +* need to be synced, then freed. +*/ +if (svm->ghcb_sa_sync) { +kvm_write_guest (svm->vcpu.kvm, +ghcb_get_sw_scratch (svm->ghcb), + svm->ghcb_sa, svm->ghcb_sa_len); + svm->ghcb_sa_sync = false; + } + +kfree (svm->ghcb_sa); + svm->ghcb_sa =NULL ; + svm->ghcb_sa_free = false; + } +trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, svm->ghcb);sev_es_sync_to_ghcb (svm);
@@ -1412,6 +1435,86 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu)vmcb_mark_dirty (svm->vmcb,VMCB_ASID ); } +#define GHCB_SCRATCH_AREA_LIMIT (16ULL * PAGE_SIZE) +static bool setup_vmgexit_scratch (struct vcpu_svm *svm,bool sync,u64 len) +{ +struct vmcb_control_area *control = &svm->vmcb->control; +struct ghcb *ghcb = svm->ghcb; +u64 ghcb_scratch_beg, ghcb_scratch_end; +u64 scratch_gpa_beg, scratch_gpa_end; +void *scratch_va; + + scratch_gpa_beg =ghcb_get_sw_scratch (ghcb); +if (!scratch_gpa_beg) { +pr_err ("vmgexit: scratch gpa not provided\n" ); +return false; + } + + scratch_gpa_end = scratch_gpa_beg + len; +if (scratch_gpa_end < scratch_gpa_beg) { +pr_err ("vmgexit: scratch length (%#llx) not valid for scratch address (%#llx)\n" , + len, scratch_gpa_beg); +return false; + } + +if ((scratch_gpa_beg &PAGE_MASK ) == control->ghcb_gpa) { +/* Scratch area begins within GHCB */ + ghcb_scratch_beg = control->ghcb_gpa + + offsetof(struct ghcb , shared_buffer); + ghcb_scratch_end = control->ghcb_gpa + + offsetof(struct ghcb , reserved_1); + +/* +* If the scratch area begins within the GHCB, it must be +* completely contained in the GHCB shared buffer area. +*/ +if (scratch_gpa_beg < ghcb_scratch_beg || + scratch_gpa_end > ghcb_scratch_end) { +pr_err ("vmgexit: scratch area is outside of GHCB shared buffer area (%#llx - %#llx)\n" , + scratch_gpa_beg, scratch_gpa_end); +return false; + } + + scratch_va = (void *)svm->ghcb; + scratch_va += (scratch_gpa_beg - control->ghcb_gpa); + }else { +/* +* The guest memory must be read into a kernel buffer, so +* limit the size +*/ +if (len >GHCB_SCRATCH_AREA_LIMIT ) { +pr_err ("vmgexit: scratch area exceeds KVM limits (%#llx requested, %#llx limit)\n" , + len,GHCB_SCRATCH_AREA_LIMIT ); +return false; + } + scratch_va =kzalloc (len,GFP_KERNEL ); +if (!scratch_va) +return false; + +if (kvm_read_guest (svm->vcpu.kvm, scratch_gpa_beg, scratch_va, len)) { +/* Unable to copy scratch area from guest */ +pr_err ("vmgexit: kvm_read_guest for scratch area failed\n" ); + +kfree (scratch_va); +return false; + } + +/* +* The scratch area is outside the GHCB. The operation will +* dictate whether the buffer needs to be synced before running +* the vCPU next time (i.e. a read was requested so the data +* must be written back to the guest memory). +*/ + svm->ghcb_sa_sync = sync; + svm->ghcb_sa_free = true; + } + + svm->ghcb_sa = scratch_va; + svm->ghcb_sa_len = len; + +return true; +} +static void set_ghcb_msr_bits (struct vcpu_svm *svm,u64 value,u64 mask,unsigned int pos) {
@@ -1549,6 +1652,24 @@ int sev_handle_vmgexit(struct vcpu_svm *svm) ret = -EINVAL ;switch (exit_code) { +case SVM_VMGEXIT_MMIO_READ : +if (!setup_vmgexit_scratch (svm, true, control->exit_info_2)) +break ; + + ret =kvm_sev_es_mmio_read (&svm->vcpu, + control->exit_info_1, + control->exit_info_2, + svm->ghcb_sa); +break ; +case SVM_VMGEXIT_MMIO_WRITE : +if (!setup_vmgexit_scratch (svm, false, control->exit_info_2)) +break ; + + ret =kvm_sev_es_mmio_write (&svm->vcpu, + control->exit_info_1, + control->exit_info_2, + svm->ghcb_sa); +break ;case SVM_VMGEXIT_UNSUPPORTED_EVENT :vcpu_unimpl (&svm->vcpu,"vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n" , control->exit_info_1, control->exit_info_2);
arch/x86/kvm/svm/svm.c +3 -0
@@ -1392,6 +1392,9 @@ static void svm_free_vcpu(struct kvm_vcpu *vcpu) }__free_page (virt_to_page (svm->vmsa)); + +if (svm->ghcb_sa_free) +kfree (svm->ghcb_sa); }__free_page (pfn_to_page (__sme_clr (svm->vmcb_pa) >>PAGE_SHIFT ));
arch/x86/kvm/svm/svm.h +6 -0
@@ -171,6 +171,12 @@ struct vcpu_svm {struct vmcb_save_area *vmsa;struct ghcb *ghcb;struct kvm_host_map ghcb_map; + +/* SEV-ES scratch area support */ +void *ghcb_sa; +u64 ghcb_sa_len; +bool ghcb_sa_sync; +bool ghcb_sa_free; };struct svm_cpu_data {
arch/x86/kvm/x86.c +123 -0
129 lines not on this page · raw message
arch/x86/kvm/x86.h +5 -0
@@ -401,4 +401,9 @@ bool kvm_msr_allowed(struct kvm_vcpu *vcpu, u32 index, u32 type); __reserved_bits; \ }) +int kvm_sev_es_mmio_write (struct kvm_vcpu *vcpu,gpa_t src,unsigned int bytes, +void *dst); +int kvm_sev_es_mmio_read (struct kvm_vcpu *vcpu,gpa_t src,unsigned int bytes, +void *dst); +#endif
Signature · 1 line
2.28.0
RFC PATCH v2 18/33 KVM: SVM: Support port IO operations for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For an SEV-ES guest, string-based port IO is performed to a shared (un-encrypted) page so that both the hypervisor and guest can read or write to it and each see the contents.
For string-based port IO operations, invoke SEV-ES specific routines that can complete the operation using common KVM port IO support.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm/sev.c | 13 +++++++++ arch/x86/kvm/svm/svm.c | 11 +++++-- arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/x86.c | 51 +++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 3 ++ 6 files changed, 77 insertions(+), 3 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -614,6 +614,7 @@ struct kvm_vcpu_arch {struct kvm_pio_request pio;void *pio_data; +void *guest_ins_data;u8 event_exit_inst_len;
arch/x86/kvm/svm/sev.c +13 -0
@@ -1320,6 +1320,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)if (!(ghcb_get_sw_exit_info_1 (ghcb) &SVM_IOIO_TYPE_MASK ))if (!ghcb_rax_is_valid (ghcb)) goto vmgexit_err; + +if (ghcb_get_sw_exit_info_1 (ghcb) &SVM_IOIO_STR_MASK ) +if (!ghcb_sw_scratch_is_valid (ghcb)) + goto vmgexit_err;break ;case SVM_EXIT_MSR :if (!ghcb_rcx_is_valid (ghcb))
@@ -1680,3 +1684,12 @@ int sev_handle_vmgexit(struct vcpu_svm *svm)return ret; } + +int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in) +{ +if (!setup_vmgexit_scratch (svm, in, svm->vmcb->control.exit_info_2)) +return -EINVAL ; + +return kvm_sev_es_string_io (&svm->vcpu, size, port, + svm->ghcb_sa, svm->ghcb_sa_len, in); +}
arch/x86/kvm/svm/svm.c +8 -3
@@ -2072,11 +2072,16 @@ static int io_interception(struct vcpu_svm *svm) ++svm->vcpu.stat.io_exits; string = (io_info &SVM_IOIO_STR_MASK ) !=0 ; in = (io_info &SVM_IOIO_TYPE_MASK ) !=0 ; -if (string) -return kvm_emulate_instruction (vcpu,0 ); - port = io_info >>16 ; size = (io_info &SVM_IOIO_SIZE_MASK ) >>SVM_IOIO_SIZE_SHIFT ; + +if (string) { +if (sev_es_guest (vcpu->kvm)) +return sev_es_string_io (svm, size, port, in); +else +return kvm_emulate_instruction (vcpu,0 ); + } + svm->next_rip = svm->vmcb->control.exit_info_2;return kvm_fast_pio (&svm->vcpu, size, port, in);
arch/x86/kvm/svm/svm.h +1 -0
@@ -563,5 +563,6 @@ void pre_sev_run(struct vcpu_svm *svm, int cpu);void __init sev_hardware_setup (void );void sev_hardware_teardown (void );int sev_handle_vmgexit (struct vcpu_svm *svm); +int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in);#endif
arch/x86/kvm/x86.c +51 -0
63 lines not on this page · raw message
arch/x86/kvm/x86.h +3 -0
@@ -405,5 +405,8 @@ int kvm_sev_es_mmio_write(struct kvm_vcpu *vcpu, gpa_t src, unsigned int bytes, void *dst);int kvm_sev_es_mmio_read (struct kvm_vcpu *vcpu,gpa_t src,unsigned int bytes,void *dst); +int kvm_sev_es_string_io (struct kvm_vcpu *vcpu,unsigned int size, +unsigned int port,void *data,unsigned int count, +int in);#endif
Signature · 1 line
2.28.0
RFC PATCH v2 19/33 KVM: SVM: Add support for EFER write traps for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For SEV-ES guests, the interception of EFER write access is not recommended. EFER interception occurs prior to EFER being modified and the hypervisor is unable to modify EFER itself because the register is located in the encrypted register state.
SEV-ES support introduces a new EFER write trap. This trap provides intercept support of an EFER write after it has been modified. The new EFER value is provided in the VMCB EXITINFO1 field, allowing the hypervisor to track the setting of the guest EFER.
Add support to track the value of the guest EFER value using the EFER write trap so that the hypervisor understands the guest operating mode.
--- arch/x86/include/uapi/asm/svm.h | 2 ++ arch/x86/kvm/svm/svm.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+)
arch/x86/include/uapi/asm/svm.h +2 -0
@@ -77,6 +77,7 @@#define SVM_EXIT_MWAIT_COND 0x08c#define SVM_EXIT_XSETBV 0x08d#define SVM_EXIT_RDPRU 0x08e +#define SVM_EXIT_EFER_WRITE_TRAP 0x08f#define SVM_EXIT_INVPCID 0x0a2#define SVM_EXIT_NPF 0x400#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401
@@ -184,6 +185,7 @@ { SVM_EXIT_MONITOR, "monitor" }, \ { SVM_EXIT_MWAIT, "mwait" }, \ { SVM_EXIT_XSETBV, "xsetbv" }, \ + { SVM_EXIT_EFER_WRITE_TRAP, "write_efer_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
arch/x86/kvm/svm/svm.c +20 -0
@@ -2531,6 +2531,25 @@ static int cr8_write_interception(struct vcpu_svm *svm)return 0 ; } +static int efer_trap (struct vcpu_svm *svm) +{ +struct msr_data msr_info; +int ret; + +/* +* Clear the EFER_SVME bit from EFER. The SVM code always sets this +* bit in svm_set_efer(), but __kvm_valid_efer() checks it against +* whether the guest has X86_FEATURE_SVM - this avoids a failure if +* the guest doesn't have X86_FEATURE_SVM. +*/ + msr_info.host_initiated = false; + msr_info.index =MSR_EFER ; + msr_info.data = svm->vmcb->control.exit_info_1 & ~EFER_SVME ; + ret =kvm_set_msr_common (&svm->vcpu, &msr_info); + +return kvm_complete_insn_gp (&svm->vcpu, ret); +} +static int svm_get_msr_feature (struct kvm_msr_entry *msr) { msr->data =0 ;
@@ -3039,6 +3058,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_MWAIT] = mwait_interception, [SVM_EXIT_XSETBV] = xsetbv_interception, [SVM_EXIT_RDPRU] = rdpru_interception, + [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception,
Signature · 1 line
2.28.0
RFC PATCH v2 20/33 KVM: SVM: Add support for CR0 write traps for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For SEV-ES guests, the interception of control register write access is not recommended. Control register interception occurs prior to the control register being modified and the hypervisor is unable to modify the control register itself because the register is located in the encrypted register state.
SEV-ES support introduces new control register write traps. These traps provide intercept support of a control register write after the control register has been modified. The new control register value is provided in the VMCB EXITINFO1 field, allowing the hypervisor to track the setting of the guest control registers.
Add support to track the value of the guest CR0 register using the control register write trap so that the hypervisor understands the guest operating mode.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/svm.h | 17 ++++++++++++++ arch/x86/kvm/svm/svm.c | 24 +++++++++++++++++++ arch/x86/kvm/x86.c | 41 +++++++++++++++++++-------------- 4 files changed, 66 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1444,6 +1444,7 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); +int __kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3);int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4);
arch/x86/include/uapi/asm/svm.h +17 -0
@@ -78,6 +78,22 @@#define SVM_EXIT_XSETBV 0x08d#define SVM_EXIT_RDPRU 0x08e#define SVM_EXIT_EFER_WRITE_TRAP 0x08f +#define SVM_EXIT_CR0_WRITE_TRAP 0x090 +#define SVM_EXIT_CR1_WRITE_TRAP 0x091 +#define SVM_EXIT_CR2_WRITE_TRAP 0x092 +#define SVM_EXIT_CR3_WRITE_TRAP 0x093 +#define SVM_EXIT_CR4_WRITE_TRAP 0x094 +#define SVM_EXIT_CR5_WRITE_TRAP 0x095 +#define SVM_EXIT_CR6_WRITE_TRAP 0x096 +#define SVM_EXIT_CR7_WRITE_TRAP 0x097 +#define SVM_EXIT_CR8_WRITE_TRAP 0x098 +#define SVM_EXIT_CR9_WRITE_TRAP 0x099 +#define SVM_EXIT_CR10_WRITE_TRAP 0x09a +#define SVM_EXIT_CR11_WRITE_TRAP 0x09b +#define SVM_EXIT_CR12_WRITE_TRAP 0x09c +#define SVM_EXIT_CR13_WRITE_TRAP 0x09d +#define SVM_EXIT_CR14_WRITE_TRAP 0x09e +#define SVM_EXIT_CR15_WRITE_TRAP 0x09f#define SVM_EXIT_INVPCID 0x0a2#define SVM_EXIT_NPF 0x400#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401
@@ -186,6 +202,7 @@ { SVM_EXIT_MWAIT, "mwait" }, \ { SVM_EXIT_XSETBV, "xsetbv" }, \ { SVM_EXIT_EFER_WRITE_TRAP, "write_efer_trap" }, \ + { SVM_EXIT_CR0_WRITE_TRAP, "write_cr0_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
arch/x86/kvm/svm/svm.c +24 -0
@@ -2478,6 +2478,29 @@ static int cr_interception(struct vcpu_svm *svm)return kvm_complete_insn_gp (&svm->vcpu, err); } +static int cr_trap (struct vcpu_svm *svm) +{ +unsigned long old_value, new_value; +unsigned int cr; +int ret; + + new_value = (unsigned long )svm->vmcb->control.exit_info_1; + + cr = svm->vmcb->control.exit_code -SVM_EXIT_CR0_WRITE_TRAP ; +switch (cr) { +case 0 : + old_value =kvm_read_cr0 (&svm->vcpu); + + ret =__kvm_set_cr0 (&svm->vcpu, old_value, new_value); +break ; +default : +WARN (1 ,"unhandled CR%d write trap" , cr); + ret =1 ; + } + +return kvm_complete_insn_gp (&svm->vcpu, ret); +} +static int dr_interception (struct vcpu_svm *svm) {int reg, dr;
@@ -3059,6 +3082,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_XSETBV] = xsetbv_interception, [SVM_EXIT_RDPRU] = rdpru_interception, [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, + [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception,
arch/x86/kvm/x86.c +24 -17
57 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH v2 21/33 KVM: SVM: Add support for CR4 write traps for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For SEV-ES guests, the interception of control register write access is not recommended. Control register interception occurs prior to the control register being modified and the hypervisor is unable to modify the control register itself because the register is located in the encrypted register state.
SEV-ES guests introduce new control register write traps. These traps provide intercept support of a control register write after the control register has been modified. The new control register value is provided in the VMCB EXITINFO1 field, allowing the hypervisor to track the setting of the guest control registers.
Add support to track the value of the guest CR4 register using the control register write trap so that the hypervisor understands the guest operating mode.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kvm/svm/svm.c | 6 ++++++ arch/x86/kvm/x86.c | 31 ++++++++++++++++++++----------- 4 files changed, 28 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -1447,6 +1447,7 @@ int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,int __kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3); +int __kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4);int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4);int kvm_set_cr8 (struct kvm_vcpu *vcpu,unsigned long cr8);int kvm_set_dr (struct kvm_vcpu *vcpu,int dr,unsigned long val);
arch/x86/include/uapi/asm/svm.h +1 -0
@@ -203,6 +203,7 @@ { SVM_EXIT_XSETBV, "xsetbv" }, \ { SVM_EXIT_EFER_WRITE_TRAP, "write_efer_trap" }, \ { SVM_EXIT_CR0_WRITE_TRAP, "write_cr0_trap" }, \ + { SVM_EXIT_CR4_WRITE_TRAP, "write_cr4_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
arch/x86/kvm/svm/svm.c +6 -0
@@ -2493,6 +2493,11 @@ static int cr_trap(struct vcpu_svm *svm) ret =__kvm_set_cr0 (&svm->vcpu, old_value, new_value);break ; +case 4 : + old_value =kvm_read_cr4 (&svm->vcpu); + + ret =__kvm_set_cr4 (&svm->vcpu, old_value, new_value); +break ;default :WARN (1 ,"unhandled CR%d write trap" , cr); ret =1 ;
@@ -3083,6 +3088,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_RDPRU] = rdpru_interception, [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, + [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception,
arch/x86/kvm/x86.c +20 -11
@@ -984,6 +984,25 @@ int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) }EXPORT_SYMBOL_GPL (kvm_valid_cr4); +int __kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long old_cr4,unsigned long cr4) +{ +unsigned long pdptr_bits =X86_CR4_PGE |X86_CR4_PSE |X86_CR4_PAE | +X86_CR4_SMEP |X86_CR4_SMAP |X86_CR4_PKE ; + +if (kvm_x86_ops.set_cr4 (vcpu, cr4)) +return 1 ; + +if (((cr4 ^ old_cr4) & pdptr_bits) || + (!(cr4 &X86_CR4_PCIDE ) && (old_cr4 &X86_CR4_PCIDE ))) +kvm_mmu_reset_context (vcpu); + +if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) +kvm_update_cpuid_runtime (vcpu); + +return 0 ; +} +EXPORT_SYMBOL_GPL (__kvm_set_cr4); +int kvm_set_cr4 (struct kvm_vcpu *vcpu,unsigned long cr4) {unsigned long old_cr4 =kvm_read_cr4 (vcpu);
@@ -1013,17 +1032,7 @@ int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)return 1 ; } -if (kvm_x86_ops.set_cr4 (vcpu, cr4)) -return 1 ; - -if (((cr4 ^ old_cr4) & pdptr_bits) || - (!(cr4 &X86_CR4_PCIDE ) && (old_cr4 &X86_CR4_PCIDE ))) -kvm_mmu_reset_context (vcpu); - -if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE |X86_CR4_PKE )) -kvm_update_cpuid_runtime (vcpu); - -return 0 ; +return __kvm_set_cr4 (vcpu, old_cr4, cr4); }EXPORT_SYMBOL_GPL (kvm_set_cr4);
Signature · 1 line
2.28.0
RFC PATCH v2 22/33 KVM: SVM: Add support for CR8 write traps for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
For SEV-ES guests, the interception of control register write access is not recommended. Control register interception occurs prior to the control register being modified and the hypervisor is unable to modify the control register itself because the register is located in the encrypted register state.
SEV-ES guests introduce new control register write traps. These traps provide intercept support of a control register write after the control register has been modified. The new control register value is provided in the VMCB EXITINFO1 field, allowing the hypervisor to track the setting of the guest control registers.
Add support to track the value of the guest CR8 register using the control register write trap so that the hypervisor understands the guest operating mode.
--- arch/x86/include/uapi/asm/svm.h | 1 + arch/x86/kvm/svm/svm.c | 4 ++++ 2 files changed, 5 insertions(+)
arch/x86/include/uapi/asm/svm.h +1 -0
@@ -204,6 +204,7 @@ { SVM_EXIT_EFER_WRITE_TRAP, "write_efer_trap" }, \ { SVM_EXIT_CR0_WRITE_TRAP, "write_cr0_trap" }, \ { SVM_EXIT_CR4_WRITE_TRAP, "write_cr4_trap" }, \ + { SVM_EXIT_CR8_WRITE_TRAP, "write_cr8_trap" }, \ { SVM_EXIT_INVPCID, "invpcid" }, \ { SVM_EXIT_NPF, "npf" }, \ { SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
arch/x86/kvm/svm/svm.c +4 -0
@@ -2498,6 +2498,9 @@ static int cr_trap(struct vcpu_svm *svm) ret =__kvm_set_cr4 (&svm->vcpu, old_value, new_value);break ; +case 8 : + ret =kvm_set_cr8 (&svm->vcpu, new_value); +break ;default :WARN (1 ,"unhandled CR%d write trap" , cr); ret =1 ;
@@ -3089,6 +3092,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = { [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap, [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap, [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap, + [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap, [SVM_EXIT_INVPCID] = invpcid_interception, [SVM_EXIT_NPF] = npf_interception, [SVM_EXIT_RSM] = rsm_interception,
Signature · 1 line
2.28.0
RFC PATCH v2 23/33 KVM: x86: Update __get_sregs() / __set_sregs() to support SEV-ES
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Since many of the registers used by the SEV-ES are encrypted and cannot be read or written, adjust the __get_sregs() / __set_sregs() to take into account whether the VMSA/guest state is encrypted.
For __get_sregs(), return the actual value that is in use by the guest for all registers being tracked using the write trap support.
For __set_sregs(), skip setting of all guest registers values.
--- arch/x86/kvm/x86.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-)
arch/x86/kvm/x86.c +18 -9
@@ -9320,6 +9320,9 @@ static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) {struct desc_ptr dt; +if (vcpu->arch.guest_state_protected) + goto skip_protected_regs; +kvm_get_segment (vcpu, &sregs->cs,VCPU_SREG_CS );kvm_get_segment (vcpu, &sregs->ds,VCPU_SREG_DS );kvm_get_segment (vcpu, &sregs->es,VCPU_SREG_ES );
@@ -9337,9 +9340,11 @@ static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) sregs->gdt.limit = dt.size; sregs->gdt.base = dt.address; - sregs->cr0 =kvm_read_cr0 (vcpu); sregs->cr2 = vcpu->arch.cr2; sregs->cr3 =kvm_read_cr3 (vcpu); + +skip_protected_regs: + sregs->cr0 =kvm_read_cr0 (vcpu); sregs->cr4 =kvm_read_cr4 (vcpu); sregs->cr8 =kvm_get_cr8 (vcpu); sregs->efer = vcpu->arch.efer;
@@ -9478,6 +9483,9 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)if (kvm_set_apic_base (vcpu, &apic_base_msr)) goto out; +if (vcpu->arch.guest_state_protected) + goto skip_protected_regs; + dt.size = sregs->idt.limit; dt.address = sregs->idt.base; kvm_x86_ops.set_idt (vcpu, &dt);
@@ -9516,14 +9524,6 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)if (mmu_reset_needed)kvm_mmu_reset_context (vcpu); - max_bits =KVM_NR_INTERRUPTS ; - pending_vec =find_first_bit ( - (const unsigned long *)sregs->interrupt_bitmap, max_bits); -if (pending_vec < max_bits) { -kvm_queue_interrupt (vcpu, pending_vec, false); -pr_debug ("Set back pending irq %d\n" , pending_vec); - } -kvm_set_segment (vcpu, &sregs->cs,VCPU_SREG_CS );kvm_set_segment (vcpu, &sregs->ds,VCPU_SREG_DS );kvm_set_segment (vcpu, &sregs->es,VCPU_SREG_ES );
@@ -9542,6 +9542,15 @@ static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) !is_protmode(vcpu)) vcpu->arch.mp_state =KVM_MP_STATE_RUNNABLE ; +skip_protected_regs: + max_bits =KVM_NR_INTERRUPTS ; + pending_vec =find_first_bit ( + (const unsigned long *)sregs->interrupt_bitmap, max_bits); +if (pending_vec < max_bits) { +kvm_queue_interrupt (vcpu, pending_vec, false); +pr_debug ("Set back pending irq %d\n" , pending_vec); + } +kvm_make_request (KVM_REQ_EVENT , vcpu); ret =0 ;
Signature · 1 line
2.28.0
RFC PATCH v2 24/33 KVM: SVM: Do not report support for SMM for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
SEV-ES guests do not currently support SMM. Update the has_emulated_msr() kvm_x86_ops function to take a struct kvm parameter so that the capability can be reported at a VM level.
Since this op is also called during KVM initialization and before a struct kvm instance is available, comments will be added to each implementation of has_emulated_msr() to indicate the kvm parameter can be null.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/svm/svm.c | 11 ++++++++++- arch/x86/kvm/vmx/vmx.c | 6 +++++- arch/x86/kvm/x86.c | 4 ++-- 4 files changed, 18 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -1062,7 +1062,7 @@ struct kvm_x86_ops {void (*hardware_disable)(void );void (*hardware_unsetup)(void );bool (*cpu_has_accelerated_tpr)(void ); -bool (*has_emulated_msr)(u32 index); +bool (*has_emulated_msr)(struct kvm *kvm,u32 index);void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);unsigned int vm_size;
arch/x86/kvm/svm/svm.c +10 -1
@@ -3934,12 +3934,21 @@ static bool svm_cpu_has_accelerated_tpr(void)return false; } -static bool svm_has_emulated_msr (u32 index) +/* +* The kvm parameter can be NULL (module initialization, or invocation before +* VM creation). Be sure to check the kvm parameter before using it. +*/ +static bool svm_has_emulated_msr (struct kvm *kvm,u32 index) {switch (index) {case MSR_IA32_MCG_EXT_CTL : case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:return false; +case MSR_IA32_SMBASE : +/* SEV-ES guests do not support SMM, so report false */ +if (kvm &&sev_es_guest (kvm)) +return false; +break ;default :break ; }
arch/x86/kvm/vmx/vmx.c +5 -1
@@ -6372,7 +6372,11 @@ static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu)handle_exception_nmi_irqoff (vmx); } -static bool vmx_has_emulated_msr (u32 index) +/* +* The kvm parameter can be NULL (module initialization, or invocation before +* VM creation). Be sure to check the kvm parameter before using it. +*/ +static bool vmx_has_emulated_msr (struct kvm *kvm,u32 index) {switch (index) {case MSR_IA32_SMBASE :
arch/x86/kvm/x86.c +2 -2
@@ -3690,7 +3690,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)* fringe case that is not enabled except via specific settings * of the module parameters. */ - r = kvm_x86_ops.has_emulated_msr (MSR_IA32_SMBASE ); + r = kvm_x86_ops.has_emulated_msr (kvm,MSR_IA32_SMBASE );break ;case KVM_CAP_VAPIC : r = !kvm_x86_ops.cpu_has_accelerated_tpr ();
@@ -5688,7 +5688,7 @@ static void kvm_init_msr_list(void) }for (i =0 ; i <ARRAY_SIZE (emulated_msrs_all); i++) { -if (!kvm_x86_ops.has_emulated_msr (emulated_msrs_all[i])) +if (!kvm_x86_ops.has_emulated_msr (NULL , emulated_msrs_all[i]))continue ; emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
Signature · 1 line
2.28.0
RFC PATCH v2 25/33 KVM: SVM: Guest FPU state save/restore not needed for SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The guest FPU state is automatically restored on VMRUN and saved on VMEXIT by the hardware, so there is no reason to do this in KVM. Eliminate the allocation of the guest_fpu save area and key off that to skip operations related to the guest FPU state.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm/svm.c | 10 ++++++ arch/x86/kvm/x86.c | 56 +++++++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 10 deletions(-)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1444,6 +1444,8 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector);int kvm_task_switch (struct kvm_vcpu *vcpu,u16 tss_selector,int idt_index,int reason,bool has_error_code,u32 error_code); +void kvm_free_guest_fpu (struct kvm_vcpu *vcpu); +int __kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long old_cr0,unsigned long cr0);int kvm_set_cr0 (struct kvm_vcpu *vcpu,unsigned long cr0);int kvm_set_cr3 (struct kvm_vcpu *vcpu,unsigned long cr3);
arch/x86/kvm/svm/svm.c +10 -0
@@ -1301,6 +1301,14 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) vmsa_page =alloc_page (GFP_KERNEL_ACCOUNT | __GFP_ZERO);if (!vmsa_page) goto error_free_hsave_page; + +/* +* SEV-ES guests maintain an encrypted version of their FPU +* state which is restored and saved on VMRUN and VMEXIT. +* Free the fpu structure to prevent KVM from attempting to +* access the FPU state. +*/ +kvm_free_guest_fpu (vcpu); } err =avic_init_vcpu (svm);
@@ -3792,6 +3800,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)svm_set_dr6 (svm,DR6_FIXED_1 |DR6_RTM );clgi (); +kvm_load_guest_xsave_state (vcpu);kvm_wait_lapic_expire (vcpu);
@@ -3837,6 +3846,7 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)kvm_before_interrupt (&svm->vcpu);kvm_load_host_xsave_state (vcpu); +stgi ();/* Any pending NMI will happen here */
arch/x86/kvm/x86.c +46 -10
122 lines not on this page · raw message
Signature · 1 line
2.28.0
RFC PATCH v2 26/33 KVM: SVM: Add support for booting APs for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
Typically under KVM, an AP is booted using the INIT-SIPI-SIPI sequence, where the guest vCPU register state is updated and then the vCPU is VMRUN to begin execution of the AP. For an SEV-ES guest, this won't work because the guest register state is encrypted.
Following the GHCB specification, the hypervisor must not alter the guest register state, so KVM must track an AP/vCPU boot. Should the guest want to park the AP, it must use the AP Reset Hold exit event in place of, for example, a HLT loop.
First AP boot (first INIT-SIPI-SIPI sequence):
Execute the AP (vCPU) as it was initialized and measured by the SEV-ES support. It is up to the guest to transfer control of the AP to the proper location.
Subsequent AP boot:
KVM will expect to receive an AP Reset Hold exit event indicating that the vCPU is being parked and will require an INIT-SIPI-SIPI sequence to awaken it. When the AP Reset Hold exit event is received, KVM will place the vCPU into a simulated HLT mode. Upon receiving the INIT-SIPI-SIPI sequence, KVM will make the vCPU runnable. It is again up to the guest to then transfer control of the AP to the proper location.
The GHCB specification also requires the hypervisor to save the address of an AP Jump Table so that, for example, vCPUs that have been parked by UEFI can be started by the OS. Provide support for the AP Jump Table set/get exit code.
--- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/svm/sev.c | 50 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 7 +++++ arch/x86/kvm/svm/svm.h | 3 ++ arch/x86/kvm/x86.c | 9 ++++++ 5 files changed, 71 insertions(+)
arch/x86/include/asm/kvm_host.h +2 -0
@@ -1254,6 +1254,8 @@ struct kvm_x86_ops {void (*migrate_timers)(struct kvm_vcpu *vcpu);void (*msr_filter_changed)(struct kvm_vcpu *vcpu); + +void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu,u8 vector); };struct kvm_x86_nested_ops {
arch/x86/kvm/svm/sev.c +50 -0
71 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +7 -0
@@ -4391,6 +4391,11 @@ static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu) (vmcb_is_intercept (&svm->vmcb->control,INTERCEPT_INIT )); } +static void svm_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector) +{ +sev_vcpu_deliver_sipi_vector (vcpu, vector); +} +static void svm_vm_destroy (struct kvm *kvm) {avic_vm_destroy (kvm);
@@ -4531,6 +4536,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .apic_init_signal_blocked = svm_apic_init_signal_blocked, .msr_filter_changed = svm_msr_filter_changed, + + .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, };static struct kvm_x86_init_ops svm_init_ops__initdata = {
arch/x86/kvm/svm/svm.h +3 -0
@@ -68,6 +68,7 @@ struct kvm_sev_info {int fd;/* SEV device fd */ unsigned long pages_locked;/* Number of pages locked */ struct list_head regions_list;/* List of registered regions */ +u64 ap_jump_table;/* SEV-ES AP Jump Table address */ };struct kvm_svm {
@@ -171,6 +172,7 @@ struct vcpu_svm {struct vmcb_save_area *vmsa;struct ghcb *ghcb;struct kvm_host_map ghcb_map; +bool ap_hlt_loop;/* SEV-ES scratch area support */ void *ghcb_sa;
@@ -564,5 +566,6 @@ void __init sev_hardware_setup(void);void sev_hardware_teardown (void );int sev_handle_vmgexit (struct vcpu_svm *svm);int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in); +void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);#endif
arch/x86/kvm/x86.c +9 -0
@@ -10023,6 +10023,15 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) {struct kvm_segment cs; +/* +* Guests with protected state can't have their state altered by KVM, +* call the vcpu_deliver_sipi_vector() x86 op for processing. +*/ +if (vcpu->arch.guest_state_protected) { + kvm_x86_ops.vcpu_deliver_sipi_vector (vcpu, vector); +return ; + } +kvm_get_segment (vcpu, &cs,VCPU_SREG_CS ); cs.selector = vector <<8 ; cs.base = vector <<12 ;
Signature · 1 line
2.28.0
RFC PATCH v2 27/33 KVM: SVM: Add NMI support for an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The GHCB specification defines how NMIs are to be handled for an SEV-ES guest. To detect the completion of an NMI the hypervisor must not intercept the IRET instruction (because a #VC while running the NMI will issue an IRET) and, instead, must receive an NMI Complete exit event from the guest.
Update the KVM support for detecting the completion of NMIs in the guest to follow the GHCB specification. When an SEV-ES guest is active, the IRET instruction will no longer be intercepted. Now, when the NMI Complete exit event is received, the iret_interception() function will be called to simulate the completion of the NMI.
--- arch/x86/kvm/svm/sev.c | 4 ++++ arch/x86/kvm/svm/svm.c | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/sev.c +4 -0
@@ -1361,6 +1361,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm)if (!ghcb_sw_scratch_is_valid (ghcb)) goto vmgexit_err;break ; +case SVM_VMGEXIT_NMI_COMPLETE :case SVM_VMGEXIT_AP_HLT_LOOP :case SVM_VMGEXIT_AP_JUMP_TABLE :case SVM_VMGEXIT_UNSUPPORTED_EVENT :
@@ -1678,6 +1679,9 @@ int sev_handle_vmgexit(struct vcpu_svm *svm) control->exit_info_2, svm->ghcb_sa);break ; +case SVM_VMGEXIT_NMI_COMPLETE : + ret =svm_invoke_exit_handler (svm,SVM_EXIT_IRET ); +break ;case SVM_VMGEXIT_AP_HLT_LOOP : svm->ap_hlt_loop = true; ret =kvm_emulate_halt (&svm->vcpu);
arch/x86/kvm/svm/svm.c +13 -7
@@ -2353,9 +2353,11 @@ static int cpuid_interception(struct vcpu_svm *svm)static int iret_interception (struct vcpu_svm *svm) { ++svm->vcpu.stat.nmi_window_exits; -svm_clr_intercept (svm,INTERCEPT_IRET ); svm->vcpu.arch.hflags |=HF_IRET_MASK ; - svm->nmi_iret_rip =kvm_rip_read (&svm->vcpu); +if (!sev_es_guest (svm->vcpu.kvm)) { +svm_clr_intercept (svm,INTERCEPT_IRET ); + svm->nmi_iret_rip =kvm_rip_read (&svm->vcpu); + }kvm_make_request (KVM_REQ_EVENT , &svm->vcpu);return 1 ; }
@@ -3362,7 +3364,8 @@ static void svm_inject_nmi(struct kvm_vcpu *vcpu) svm->vmcb->control.event_inj =SVM_EVTINJ_VALID |SVM_EVTINJ_TYPE_NMI ; vcpu->arch.hflags |=HF_NMI_MASK ; -svm_set_intercept (svm,INTERCEPT_IRET ); +if (!sev_es_guest (svm->vcpu.kvm)) +svm_set_intercept (svm,INTERCEPT_IRET ); ++vcpu->stat.nmi_injections; }
@@ -3446,10 +3449,12 @@ static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)if (masked) { svm->vcpu.arch.hflags |=HF_NMI_MASK ; -svm_set_intercept (svm,INTERCEPT_IRET ); +if (!sev_es_guest (svm->vcpu.kvm)) +svm_set_intercept (svm,INTERCEPT_IRET ); }else { svm->vcpu.arch.hflags &= ~HF_NMI_MASK ; -svm_clr_intercept (svm,INTERCEPT_IRET ); +if (!sev_es_guest (svm->vcpu.kvm)) +svm_clr_intercept (svm,INTERCEPT_IRET ); } }
@@ -3627,8 +3632,9 @@ static void svm_complete_interrupts(struct vcpu_svm *svm)* If we've made progress since setting HF_IRET_MASK, we've * executed an IRET and can allow NMI injection. */ -if ((svm->vcpu.arch.hflags &HF_IRET_MASK ) - &&kvm_rip_read (&svm->vcpu) != svm->nmi_iret_rip) { +if ((svm->vcpu.arch.hflags &HF_IRET_MASK ) && + (sev_es_guest (svm->vcpu.kvm) || +kvm_rip_read (&svm->vcpu) != svm->nmi_iret_rip)) { svm->vcpu.arch.hflags &= ~(HF_NMI_MASK |HF_IRET_MASK );kvm_make_request (KVM_REQ_EVENT , &svm->vcpu); }
Signature · 1 line
2.28.0
RFC PATCH v2 28/33 KVM: SVM: Set the encryption mask for the SVM host save area
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The SVM host save area is used to restore some host state on VMEXIT of an SEV-ES guest. After allocating the save area, clear it and add the encryption mask to the SVM host save area physical address that is programmed into the VM_HSAVE_PA MSR.
--- arch/x86/kvm/svm/sev.c | 1 - arch/x86/kvm/svm/svm.c | 3 ++- arch/x86/kvm/svm/svm.h | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/sev.c +0 -1
@@ -31,7 +31,6 @@ unsigned int max_sev_asid;static unsigned int min_sev_asid;static unsigned long *sev_asid_bitmap;static unsigned long *sev_reclaim_asid_bitmap; -#define __sme_page_pa (x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)struct enc_region {struct list_head list;
arch/x86/kvm/svm/svm.c +2 -1
@@ -478,7 +478,7 @@ static int svm_hardware_enable(void)wrmsrl (MSR_EFER , efer |EFER_SVME ); -wrmsrl (MSR_VM_HSAVE_PA ,page_to_pfn (sd->save_area) <<PAGE_SHIFT ); +wrmsrl (MSR_VM_HSAVE_PA ,__sme_page_pa (sd->save_area));if (static_cpu_has (X86_FEATURE_TSCRATEMSR )) {wrmsrl (MSR_AMD64_TSC_RATIO ,TSC_RATIO_DEFAULT );
@@ -546,6 +546,7 @@ static int svm_cpu_init(int cpu) sd->save_area =alloc_page (GFP_KERNEL );if (!sd->save_area) goto free_cpu_data; +clear_page (page_address (sd->save_area));if (svm_sev_enabled ()) { sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
arch/x86/kvm/svm/svm.h +2 -0
@@ -21,6 +21,8 @@#include <asm/svm.h> +#define __sme_page_pa (x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) +static const u32 host_save_user_msrs[] = { #ifdef CONFIG_X86_64 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
Signature · 1 line
2.28.0
RFC PATCH v2 29/33 KVM: SVM: Update ASID allocation to support SEV-ES guests
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
SEV and SEV-ES guests each have dedicated ASID ranges. Update the ASID allocation routine to return an ASID in the respective range.
--- arch/x86/kvm/svm/sev.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
arch/x86/kvm/svm/sev.c +14 -11
@@ -62,19 +62,19 @@ static int sev_flush_asids(void) }/* Must be called with the sev_bitmap_lock held */ -static bool __sev_recycle_asids (void ) +static bool __sev_recycle_asids (int min_asid,int max_asid) {int pos;/* Check if there are any ASIDs to reclaim before performing a flush */ - pos =find_next_bit (sev_reclaim_asid_bitmap, - max_sev_asid, min_sev_asid -1 ); -if (pos >= max_sev_asid) + pos =find_next_bit (sev_reclaim_asid_bitmap, max_sev_asid, min_asid); +if (pos >= max_asid)return false;if (sev_flush_asids ())return false; +/* The flush process will flush all reclaimable SEV and SEV-ES ASIDs */ bitmap_xor (sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap, max_sev_asid);bitmap_zero (sev_reclaim_asid_bitmap, max_sev_asid);
@@ -82,20 +82,23 @@ static bool __sev_recycle_asids(void)return true; } -static int sev_asid_new (void ) +static int sev_asid_new (struct kvm_sev_info *sev) { +int pos, min_asid, max_asid;bool retry = true; -int pos;mutex_lock (&sev_bitmap_lock);/* -* SEV-enabled guest must use asid from min_sev_asid to max_sev_asid. +* SEV-enabled guests must use asid from min_sev_asid to max_sev_asid. +* SEV-ES-enabled guest can use from 1 to min_sev_asid - 1. */ + min_asid = sev->es_active ?0 : min_sev_asid -1 ; + max_asid = sev->es_active ? min_sev_asid -1 : max_sev_asid; again: - pos =find_next_zero_bit (sev_asid_bitmap, max_sev_asid, min_sev_asid -1 ); -if (pos >= max_sev_asid) { -if (retry &&__sev_recycle_asids ()) { + pos =find_next_zero_bit (sev_asid_bitmap, max_sev_asid, min_asid); +if (pos >= max_asid) { +if (retry &&__sev_recycle_asids (min_asid, max_asid)) { retry = false; goto again; }
@@ -177,7 +180,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)if (unlikely (sev->active))return ret; - asid =sev_asid_new (); + asid =sev_asid_new (sev);if (asid <0 )return ret;
Signature · 1 line
2.28.0
RFC PATCH v2 30/33 KVM: SVM: Provide support for SEV-ES vCPU creation/loading
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
An SEV-ES vCPU requires additional VMCB initialization requirements for vCPU creation and vCPU load/put requirements. This includes:
General VMCB initialization changes:
- Set a VMCB control bit to enable SEV-ES support on the vCPU.
- Set the VMCB encrypted VM save area address.
- CRx registers are part of the encrypted register state and cannot be updated. Remove the CRx register read and write intercepts and replace them with CRx register write traps to track the CRx register values.
- Certain MSR values are part of the encrypted register state and cannot be updated. Remove certain MSR intercepts (EFER, CR_PAT, etc.).
- Remove the #GP intercept (no support for "enable_vmware_backdoor").
- Remove the XSETBV intercept since the hypervisor cannot modify XCR0.
General vCPU creation changes:
- Set the initial GHCB gpa value as per the GHCB specification.
--- arch/x86/include/asm/svm.h | 15 +++++++++- arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 20 ++++++++++++-- arch/x86/kvm/svm/svm.h | 6 +++- 4 files changed, 92 insertions(+), 5 deletions(-)
arch/x86/include/asm/svm.h +14 -1
@@ -98,6 +98,16 @@ enum {INTERCEPT_MWAIT_COND ,INTERCEPT_XSETBV ,INTERCEPT_RDPRU , +TRAP_EFER_WRITE , +TRAP_CR0_WRITE , +TRAP_CR1_WRITE , +TRAP_CR2_WRITE , +TRAP_CR3_WRITE , +TRAP_CR4_WRITE , +TRAP_CR5_WRITE , +TRAP_CR6_WRITE , +TRAP_CR7_WRITE , +TRAP_CR8_WRITE ,/* Byte offset 014h (word 5) */ INTERCEPT_INVLPGB =160 , INTERCEPT_INVLPGB_ILLEGAL,
@@ -144,6 +154,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area {u8 reserved_6[8 ];/* Offset 0xe8 */ u64 avic_logical_id;/* Offset 0xf0 */ u64 avic_physical_id;/* Offset 0xf8 */ +u8 reserved_7[8 ]; +u64 vmsa_pa;/* Used for an SEV-ES guest */ };
@@ -198,6 +210,7 @@ struct __attribute__ ((__packed__)) vmcb_control_area {#define SVM_NESTED_CTL_NP_ENABLE BIT(0)#define SVM_NESTED_CTL_SEV_ENABLE BIT(1) +#define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2)struct vmcb_seg {u16 selector;
@@ -295,7 +308,7 @@ struct ghcb {#define EXPECTED_VMCB_SAVE_AREA_SIZE 1032 -#define EXPECTED_VMCB_CONTROL_AREA_SIZE 256 +#define EXPECTED_VMCB_CONTROL_AREA_SIZE 272#define EXPECTED_GHCB_SIZE PAGE_SIZEstatic inline void __unused_size_checks (void )
arch/x86/kvm/svm/sev.c +56 -0
59 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +17 -3
@@ -91,7 +91,7 @@ static DEFINE_PER_CPU(u64, current_tsc_ratio);static const struct svm_direct_access_msrs {u32 index;/* Index of the MSR */ -bool always;/* True if intercept is always on */ +bool always;/* True if intercept is initially cleared */ } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS ] = { { .index =MSR_STAR , .always = true }, { .index =MSR_IA32_SYSENTER_CS , .always = true },
@@ -109,6 +109,9 @@ static const struct svm_direct_access_msrs { { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, { .index = MSR_IA32_LASTINTFROMIP, .always = false }, { .index = MSR_IA32_LASTINTTOIP, .always = false }, + { .index = MSR_EFER, .always = false }, + { .index = MSR_IA32_CR_PAT, .always = false }, + { .index = MSR_AMD64_SEV_ES_GHCB, .always = true }, { .index = MSR_INVALID, .always = false }, };
@@ -657,8 +660,8 @@ static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm, msrpm[offset] = tmp; } -static void set_msr_interception (struct kvm_vcpu *vcpu,u32 *msrpm,u32 msr, -int read,int write) +void set_msr_interception (struct kvm_vcpu *vcpu,u32 *msrpm,u32 msr, +int read,int write) {set_shadow_msr_intercept (vcpu, msr, read, write);set_msr_interception_bitmap (vcpu, msrpm, msr, read, write);
@@ -1242,6 +1245,11 @@ static void init_vmcb(struct vcpu_svm *svm)if (sev_guest (svm->vcpu.kvm)) { svm->vmcb->control.nested_ctl |=SVM_NESTED_CTL_SEV_ENABLE ;clr_exception_intercept (svm,UD_VECTOR ); + +if (sev_es_guest (svm->vcpu.kvm)) { +/* Perform SEV-ES specific VMCB updates */ +sev_es_init_vmcb (svm); + } }vmcb_mark_all_dirty (svm->vmcb);
@@ -1349,6 +1357,10 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)svm_init_osvw (vcpu); vcpu->arch.microcode_version =0x01000065 ; +if (sev_es_guest (svm->vcpu.kvm)) +/* Perform SEV-ES specific VMCB creation updates */ +sev_es_create_vcpu (svm); +return 0 ; error_free_msrpm:
@@ -1469,6 +1481,7 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)loadsegment (gs, svm->host.gs);#endif #endif +for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++)wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); }
@@ -3159,6 +3172,7 @@ static void dump_vmcb(struct kvm_vcpu *vcpu)pr_err ("%-20s%016llx\n" ,"avic_backing_page:" , control->avic_backing_page);pr_err ("%-20s%016llx\n" ,"avic_logical_id:" , control->avic_logical_id);pr_err ("%-20s%016llx\n" ,"avic_physical_id:" , control->avic_physical_id); +pr_err ("%-20s%016llx\n" ,"vmsa_pa:" , control->vmsa_pa);pr_err ("VMCB State Save Area:\n" ); pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", "es:",
arch/x86/kvm/svm/svm.h +5 -1
@@ -34,7 +34,7 @@ static const u32 host_save_user_msrs[] = {#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) -#define MAX_DIRECT_ACCESS_MSRS 15 +#define MAX_DIRECT_ACCESS_MSRS 18#define MSRPM_OFFSETS 16extern u32 msrpm_offsets[MSRPM_OFFSETS ]__read_mostly ;extern bool npt_enabled;
@@ -412,6 +412,8 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu);bool svm_interrupt_blocked (struct kvm_vcpu *vcpu);void svm_set_gif (struct vcpu_svm *svm,bool value);int svm_invoke_exit_handler (struct vcpu_svm *svm,u64 exit_code); +void set_msr_interception (struct kvm_vcpu *vcpu,u32 *msrpm,u32 msr, +int read,int write);/* nested.c */
@@ -569,5 +571,7 @@ void sev_hardware_teardown(void);int sev_handle_vmgexit (struct vcpu_svm *svm);int sev_es_string_io (struct vcpu_svm *svm,int size,unsigned int port,int in);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector); +void sev_es_init_vmcb (struct vcpu_svm *svm); +void sev_es_create_vcpu (struct vcpu_svm *svm);#endif
Signature · 1 line
2.28.0
RFC PATCH v2 31/33 KVM: SVM: Provide support for SEV-ES vCPU loading
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
An SEV-ES vCPU requires additional VMCB vCPU load/put requirements. SEV-ES hardware will restore certain registers on VMEXIT, but not save them on VMRUM (see Table B-3 and Table B-4 of the AMD64 APM Volume 2), so make the following changes:
General vCPU load changes:
- During vCPU loading, perform a VMSAVE to the per-CPU SVM save area and save the current values of XCR0, XSS and PKRU to the per-CPU SVM save area as these registers will be restored on VMEXIT.
General vCPU put changes:
- Do not attempt to restore registers that SEV-ES hardware has already restored on VMEXIT.
--- arch/x86/include/asm/svm.h | 10 ++++--- arch/x86/kvm/svm/sev.c | 54 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 36 ++++++++++++++++--------- arch/x86/kvm/svm/svm.h | 22 +++++++++++----- arch/x86/kvm/x86.c | 3 ++- arch/x86/kvm/x86.h | 1 + 6 files changed, 103 insertions(+), 23 deletions(-)
arch/x86/include/asm/svm.h +7 -3
@@ -234,7 +234,8 @@ struct vmcb_save_area {u8 cpl;u8 reserved_2[4 ];u64 efer; -u8 reserved_3[112 ]; +u8 reserved_3[104 ]; +u64 xss;/* Valid for SEV-ES only */ u64 cr4;u64 cr3;u64 cr0;
@@ -265,9 +266,12 @@ struct vmcb_save_area {/* * The following part of the save area is valid only for -* SEV-ES guests when referenced through the GHCB. +* SEV-ES guests when referenced through the GHCB or for +* saving to the host save area. */ -u8 reserved_7[104 ]; +u8 reserved_7[80 ]; +u32 pkru; +u8 reserved_7a[20 ];u64 reserved_8;/* rax already available at 0x01f8 */ u64 rcx;u64 rdx;
arch/x86/kvm/svm/sev.c +54 -0
69 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +23 -13
@@ -1435,15 +1435,20 @@ static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)vmcb_mark_all_dirty (svm->vmcb); } +if (sev_es_guest (svm->vcpu.kvm)) { +sev_es_vcpu_load (svm, cpu); + }else {#ifdef CONFIG_X86_64 -rdmsrl (MSR_GS_BASE ,to_svm (vcpu)->host.gs_base); +rdmsrl (MSR_GS_BASE ,to_svm (vcpu)->host.gs_base);#endif -savesegment (fs, svm->host.fs); -savesegment (gs, svm->host.gs); - svm->host.ldt =kvm_read_ldt (); +savesegment (fs, svm->host.fs); +savesegment (gs, svm->host.gs); + svm->host.ldt =kvm_read_ldt (); -for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++) -rdmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); +for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++) +rdmsrl (host_save_user_msrs[i].index, + svm->host_user_msrs[i]); + }if (static_cpu_has (X86_FEATURE_TSCRATEMSR )) {u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
@@ -1471,19 +1476,24 @@ static void svm_vcpu_put(struct kvm_vcpu *vcpu)avic_vcpu_put (vcpu); ++vcpu->stat.host_state_reload; -kvm_load_ldt (svm->host.ldt); +if (sev_es_guest (svm->vcpu.kvm)) { +sev_es_vcpu_put (svm); + }else { +kvm_load_ldt (svm->host.ldt);#ifdef CONFIG_X86_64 -loadsegment (fs, svm->host.fs); -wrmsrl (MSR_KERNEL_GS_BASE , current->thread.gsbase); -load_gs_index (svm->host.gs); +loadsegment (fs, svm->host.fs); +wrmsrl (MSR_KERNEL_GS_BASE , current->thread.gsbase); +load_gs_index (svm->host.gs);#else #ifdef CONFIG_X86_32_LAZY_GS -loadsegment (gs, svm->host.gs); +loadsegment (gs, svm->host.gs);#endif #endif -for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++) -wrmsrl (host_save_user_msrs[i], svm->host_user_msrs[i]); +for (i =0 ; i <NR_HOST_SAVE_USER_MSRS ; i++) +wrmsrl (host_save_user_msrs[i].index, + svm->host_user_msrs[i]); + } }static unsigned long svm_get_rflags (struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.h +16 -6
@@ -23,15 +23,23 @@#define __sme_page_pa (x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) -static const u32 host_save_user_msrs[] = { +static const struct svm_host_save_msrs { +u32 index;/* Index of the MSR */ +bool sev_es_restored;/* True if MSR is restored on SEV-ES VMEXIT */ +} host_save_user_msrs[] = { #ifdef CONFIG_X86_64 -MSR_STAR ,MSR_LSTAR ,MSR_CSTAR ,MSR_SYSCALL_MASK ,MSR_KERNEL_GS_BASE , -MSR_FS_BASE , + { .index =MSR_STAR , .sev_es_restored = true }, + { .index =MSR_LSTAR , .sev_es_restored = true }, + { .index =MSR_CSTAR , .sev_es_restored = true }, + { .index =MSR_SYSCALL_MASK , .sev_es_restored = true }, + { .index =MSR_KERNEL_GS_BASE , .sev_es_restored = true }, + { .index =MSR_FS_BASE , .sev_es_restored = true }, #endif -MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP , -MSR_TSC_AUX , + { .index =MSR_IA32_SYSENTER_CS , .sev_es_restored = true }, + { .index =MSR_IA32_SYSENTER_ESP , .sev_es_restored = true }, + { .index =MSR_IA32_SYSENTER_EIP , .sev_es_restored = true }, + { .index =MSR_TSC_AUX , .sev_es_restored = false }, }; -#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)#define MAX_DIRECT_ACCESS_MSRS 18
@@ -573,5 +581,7 @@ int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in);void sev_vcpu_deliver_sipi_vector (struct kvm_vcpu *vcpu,u8 vector);void sev_es_init_vmcb (struct vcpu_svm *svm);void sev_es_create_vcpu (struct vcpu_svm *svm); +void sev_es_vcpu_load (struct vcpu_svm *svm,int cpu); +void sev_es_vcpu_put (struct vcpu_svm *svm);#endif
arch/x86/kvm/x86.c +2 -1
@@ -197,7 +197,8 @@ EXPORT_SYMBOL_GPL(host_efer);bool __read_mostly allow_smaller_maxphyaddr;EXPORT_SYMBOL_GPL (allow_smaller_maxphyaddr); -static u64 __read_mostly host_xss; +u64 __read_mostly host_xss; +EXPORT_SYMBOL_GPL (host_xss);u64 __read_mostly supported_xss;EXPORT_SYMBOL_GPL (supported_xss);
arch/x86/kvm/x86.h +1 -0
@@ -278,6 +278,7 @@ fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);extern u64 host_xcr0;extern u64 supported_xcr0; +extern u64 host_xss;extern u64 supported_xss;static inline bool kvm_mpx_supported (void )
Signature · 1 line
2.28.0
RFC PATCH v2 32/33 KVM: SVM: Provide an updated VMRUN invocation for SEV-ES guests
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
The run sequence is different for an SEV-ES guest compared to a legacy or even an SEV guest. The guest vCPU register state of an SEV-ES guest will be restored on VMRUN and saved on VMEXIT. There is no need to restore the guest registers directly and through VMLOAD before VMRUN and no need to save the guest registers directly and through VMSAVE on VMEXIT.
Update the svm_vcpu_run() function to skip register state saving and restoring and provide an alternative function for running an SEV-ES guest in vmenter.S
Additionally, certain host state is restored across an SEV-ES VMRUN. As a result certain register states are not required to be restored upon VMEXIT (e.g. FS, GS, etc.), so only do that if the guest is not an SEV-ES guest.
--- arch/x86/kvm/svm/svm.c | 25 ++++++++++++------- arch/x86/kvm/svm/svm.h | 5 ++++ arch/x86/kvm/svm/vmenter.S | 50 ++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 6 +++++ 4 files changed, 77 insertions(+), 9 deletions(-)
arch/x86/kvm/svm/svm.c +16 -9
@@ -3760,16 +3760,20 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu,guest_enter_irqoff ();lockdep_hardirqs_on (CALLER_ADDR0 ); -__svm_vcpu_run (svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs); +if (sev_es_guest (svm->vcpu.kvm)) { +__svm_sev_es_vcpu_run (svm->vmcb_pa); + }else { +__svm_vcpu_run (svm->vmcb_pa, (unsigned long *)&svm->vcpu.arch.regs);#ifdef CONFIG_X86_64 -native_wrmsrl (MSR_GS_BASE , svm->host.gs_base); +native_wrmsrl (MSR_GS_BASE , svm->host.gs_base);#else -loadsegment (fs, svm->host.fs); +loadsegment (fs, svm->host.fs);#ifndef CONFIG_X86_32_LAZY_GS -loadsegment (gs, svm->host.gs); +loadsegment (gs, svm->host.gs);#endif #endif + }/* * VMEXIT disables interrupts (host state), but tracing and lockdep
@@ -3864,14 +3868,17 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)if (unlikely (!msr_write_intercepted (vcpu,MSR_IA32_SPEC_CTRL ))) svm->spec_ctrl =native_read_msr (MSR_IA32_SPEC_CTRL ); -reload_tss (vcpu); +if (!sev_es_guest (svm->vcpu.kvm)) +reload_tss (vcpu);x86_spec_ctrl_restore_host (svm->spec_ctrl, svm->virt_spec_ctrl); - vcpu->arch.cr2 = svm->vmcb->save.cr2; - vcpu->arch.regs[VCPU_REGS_RAX ] = svm->vmcb->save.rax; - vcpu->arch.regs[VCPU_REGS_RSP ] = svm->vmcb->save.rsp; - vcpu->arch.regs[VCPU_REGS_RIP ] = svm->vmcb->save.rip; +if (!sev_es_guest (svm->vcpu.kvm)) { + vcpu->arch.cr2 = svm->vmcb->save.cr2; + vcpu->arch.regs[VCPU_REGS_RAX ] = svm->vmcb->save.rax; + vcpu->arch.regs[VCPU_REGS_RSP ] = svm->vmcb->save.rsp; + vcpu->arch.regs[VCPU_REGS_RIP ] = svm->vmcb->save.rip; + }if (unlikely (svm->vmcb->control.exit_code ==SVM_EXIT_NMI ))kvm_before_interrupt (&svm->vcpu);
arch/x86/kvm/svm/svm.h +5 -0
@@ -584,4 +584,9 @@ void sev_es_create_vcpu(struct vcpu_svm *svm);void sev_es_vcpu_load (struct vcpu_svm *svm,int cpu);void sev_es_vcpu_put (struct vcpu_svm *svm); +/* vmenter.S */ + +void __svm_sev_es_vcpu_run (unsigned long vmcb_pa); +void __svm_vcpu_run (unsigned long vmcb_pa,unsigned long *regs); +#endif
arch/x86/kvm/svm/vmenter.S +50 -0
53 lines not on this page · raw message
arch/x86/kvm/x86.c +6 -0
@@ -881,6 +881,9 @@ EXPORT_SYMBOL_GPL(kvm_lmsw);void kvm_load_guest_xsave_state (struct kvm_vcpu *vcpu) { +if (vcpu->arch.guest_state_protected) +return ; +if (kvm_read_cr4_bits (vcpu,X86_CR4_OSXSAVE )) {if (vcpu->arch.xcr0 != host_xcr0)
@@ -901,6 +904,9 @@ EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);void kvm_load_host_xsave_state (struct kvm_vcpu *vcpu) { +if (vcpu->arch.guest_state_protected) +return ; +if (static_cpu_has (X86_FEATURE_PKU ) && (kvm_read_cr4_bits (vcpu,X86_CR4_PKE ) || (vcpu->arch.xcr0 &XFEATURE_MASK_PKRU ))) {
Signature · 1 line
2.28.0
RFC PATCH v2 33/33 KVM: SVM: Provide support to launch and run an SEV-ES guest
In reply to Tom LendackyFrom: Tom Lendacky <[email protected]>
An SEV-ES guest is started by invoking a new SEV initialization ioctl, KVM_SEV_ES_INIT. This identifies the guest as an SEV-ES guest, which is used to drive the appropriate ASID allocation, VMSA encryption, etc.
Before being able to run an SEV-ES vCPU, the vCPU VMSA must be encrypted and measured. This is done using the LAUNCH_UPDATE_VMSA command after all calls to LAUNCH_UPDATE_DATA have been performed, but before LAUNCH_MEASURE has been performed. In order to establish the encrypted VMSA, the current (traditional) VMSA and the GPRs are synced to the page that will hold the encrypted VMSA and then LAUNCH_UPDATE_VMSA is invoked. The vCPU is then marked as having protected guest state.
--- arch/x86/kvm/svm/sev.c | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+)
arch/x86/kvm/svm/sev.c +104 -0
@@ -202,6 +202,16 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_es_guest_init (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +if (!sev_es) +return -ENOTTY ; + +to_kvm_svm (kvm)->sev_info.es_active = true; + +return sev_guest_init (kvm, argp); +} +static int sev_bind_asid (struct kvm *kvm,unsigned int handle,int *error) {struct sev_data_activate *data;
@@ -500,6 +510,94 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_es_sync_vmsa (struct vcpu_svm *svm) +{ +struct vmcb_save_area *save = &svm->vmcb->save; + +/* Check some debug related fields before encrypting the VMSA */ +if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1 )) +return -EINVAL ; + +/* Sync registgers */ + save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX ]; + save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX ]; + save->rcx = svm->vcpu.arch.regs[VCPU_REGS_RCX ]; + save->rdx = svm->vcpu.arch.regs[VCPU_REGS_RDX ]; + save->rsp = svm->vcpu.arch.regs[VCPU_REGS_RSP ]; + save->rbp = svm->vcpu.arch.regs[VCPU_REGS_RBP ]; + save->rsi = svm->vcpu.arch.regs[VCPU_REGS_RSI ]; + save->rdi = svm->vcpu.arch.regs[VCPU_REGS_RDI ]; + save->r8 = svm->vcpu.arch.regs[VCPU_REGS_R8 ]; + save->r9 = svm->vcpu.arch.regs[VCPU_REGS_R9 ]; + save->r10 = svm->vcpu.arch.regs[VCPU_REGS_R10 ]; + save->r11 = svm->vcpu.arch.regs[VCPU_REGS_R11 ]; + save->r12 = svm->vcpu.arch.regs[VCPU_REGS_R12 ]; + save->r13 = svm->vcpu.arch.regs[VCPU_REGS_R13 ]; + save->r14 = svm->vcpu.arch.regs[VCPU_REGS_R14 ]; + save->r15 = svm->vcpu.arch.regs[VCPU_REGS_R15 ]; + save->rip = svm->vcpu.arch.regs[VCPU_REGS_RIP ]; + +/* Sync some non-GPR registers before encrypting */ + save->xcr0 = svm->vcpu.arch.xcr0; + save->pkru = svm->vcpu.arch.pkru; + save->xss = svm->vcpu.arch.ia32_xss; + +/* +* SEV-ES will use a VMSA that is pointed to by the VMCB, not +* the traditional VMSA that is part of the VMCB. Copy the +* traditional VMSA as it has been built so far (in prep +* for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state. +*/ +memcpy (svm->vmsa, save,sizeof (*save)); + +return 0 ; +} + +static int sev_launch_update_vmsa (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; +struct sev_data_launch_update_vmsa *vmsa; +int i, ret; + +if (!sev_es_guest (kvm)) +return -ENOTTY ; + + vmsa =kzalloc (sizeof (*vmsa),GFP_KERNEL ); +if (!vmsa) +return -ENOMEM ; + +for (i =0 ; i < kvm->created_vcpus; i++) { +struct vcpu_svm *svm =to_svm (kvm->vcpus[i]); + +/* Perform some pre-encryption checks against the VMSA */ + ret =sev_es_sync_vmsa (svm); +if (ret) + goto e_free; + +/* +* The LAUNCH_UPDATE_VMSA command will perform in-place +* encryption of the VMSA memory content (i.e it will write +* the same memory region with the guest's key), so invalidate +* it first. +*/ +clflush_cache_range (svm->vmsa,PAGE_SIZE ); + + vmsa->handle = sev->handle; + vmsa->address =__sme_pa (svm->vmsa); + vmsa->len =PAGE_SIZE ; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_VMSA , vmsa, + &argp->error); +if (ret) + goto e_free; + + svm->vcpu.arch.guest_state_protected = true; + } + +e_free: +kfree (vmsa); +return ret; +} +static int sev_launch_measure (struct kvm *kvm,struct kvm_sev_cmd *argp) {void __user *measure = (void __user *)(uintptr_t )argp->data;
@@ -957,12 +1055,18 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_INIT : r =sev_guest_init (kvm, &sev_cmd);break ; +case KVM_SEV_ES_INIT : + r =sev_es_guest_init (kvm, &sev_cmd); +break ;case KVM_SEV_LAUNCH_START : r =sev_launch_start (kvm, &sev_cmd);break ;case KVM_SEV_LAUNCH_UPDATE_DATA : r =sev_launch_update_data (kvm, &sev_cmd);break ; +case KVM_SEV_LAUNCH_UPDATE_VMSA : + r =sev_launch_update_vmsa (kvm, &sev_cmd); +break ;case KVM_SEV_LAUNCH_MEASURE : r =sev_launch_measure (kvm, &sev_cmd);break ;
Signature · 1 line
2.28.0