KVM: SVM: move the SEV command handling into a separate file
cover KVM: SVM: move the SEV command handling into a separate file
svm.c is pretty huge, before we add more SEV specific commands (e.g SEV-ES, SEV-Migration etc) lets move the SEV command handling into a separate file. There is no logical changes in this series.
The patch is based on motivation from this thread:
https://marc.info/?l=linux-kernel&m=153910240507399&w=2
Brijesh Singh (2):
KVM: SVM: move common struct definitions to header file
KVM: SVM: move the SEV specific function in a separate file
arch/x86/include/asm/svm.h | 155 +++++ arch/x86/kvm/Makefile | 2 + arch/x86/kvm/sev.c | 1076 +++++++++++++++++++++++++++++++ arch/x86/kvm/sev.h | 40 ++ arch/x86/kvm/svm.c | 1234 +----------------------------------- 5 files changed, 1296 insertions(+), 1211 deletions(-) create mode 100644 arch/x86/kvm/sev.c create mode 100644 arch/x86/kvm/sev.h
Signature · 1 line
2.17.1
PATCH 1/2 KVM: SVM: move common struct definitions to header file
Some of the structure will be used by sev.c in next patch, lets move the common structure definition in svm.h so that both the sev.c and svm.c can use it.
--- arch/x86/include/asm/svm.h | 155 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/svm.c | 155 ------------------------------------- 2 files changed, 155 insertions(+), 155 deletions(-)
arch/x86/include/asm/svm.h +155 -0
@@ -2,6 +2,7 @@#ifndef __SVM_H#define __SVM_H +#include <linux/kvm_host.h> #include <uapi/asm/svm.h>
@@ -207,6 +208,160 @@ struct __attribute__ ((__packed__)) vmcb {struct vmcb_save_area save; }; +static const u32 host_save_user_msrs[] = { +#ifdef CONFIG_X86_64 +MSR_STAR ,MSR_LSTAR ,MSR_CSTAR ,MSR_SYSCALL_MASK ,MSR_KERNEL_GS_BASE , +MSR_FS_BASE , +#endif +MSR_IA32_SYSENTER_CS ,MSR_IA32_SYSENTER_ESP ,MSR_IA32_SYSENTER_EIP , +MSR_TSC_AUX , +}; + +#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) + +struct kvm_sev_info { +bool active;/* SEV enabled guest */ +unsigned int asid;/* ASID used for this guest */ +unsigned int handle;/* SEV firmware handle */ +int fd;/* SEV device fd */ +unsigned long pages_locked;/* Number of pages locked */ +struct list_head regions_list;/* List of registered regions */ +}; + +struct kvm_svm { +struct kvm kvm; + +/* Struct members for AVIC */ +u32 avic_vm_id; +u32 ldr_mode; +struct page *avic_logical_id_table_page; +struct page *avic_physical_id_table_page; +struct hlist_node hnode; + +struct kvm_sev_info sev_info; +}; + +struct nested_state { +struct vmcb *hsave; +u64 hsave_msr; +u64 vm_cr_msr; +u64 vmcb; + +/* These are the merged vectors */ +u32 *msrpm; + +/* gpa pointers to the real vectors */ +u64 vmcb_msrpm; +u64 vmcb_iopm; + +/* A VMEXIT is required but not yet emulated */ +bool exit_required; + +/* cache for intercepts of the guest */ +u32 intercept_cr; +u32 intercept_dr; +u32 intercept_exceptions; +u64 intercept; + +/* Nested Paging related state */ +u64 nested_cr3; +}; + +struct vcpu_svm { +struct kvm_vcpu vcpu; +struct vmcb *vmcb; +unsigned long vmcb_pa; +struct svm_cpu_data *svm_data; +uint64_t asid_generation; +uint64_t sysenter_esp; +uint64_t sysenter_eip; +uint64_t tsc_aux; + +u64 msr_decfg; + +u64 next_rip; + +u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS ]; +struct { +u16 fs; +u16 gs; +u16 ldt; +u64 gs_base; + } host; + +u64 spec_ctrl; +/* +* Contains guest-controlled bits of VIRT_SPEC_CTRL, which will be +* translated into the appropriate L2_CFG bits on the host to +* perform speculative control. +*/ +u64 virt_spec_ctrl; + +u32 *msrpm; + +ulong nmi_iret_rip; + +struct nested_state nested; + +bool nmi_singlestep; +u64 nmi_singlestep_guest_rflags; + +unsigned int3_injected; +unsigned long int3_rip; + +/* cached guest cpuid flags for faster access */ +bool nrips_enabled :1 ; + +u32 ldr_reg; +struct page *avic_backing_page; +u64 *avic_physical_id_cache; +bool avic_is_running; + +/* +* Per-vcpu list of struct amd_svm_iommu_ir: +* This is used mainly to store interrupt remapping information used +* when update the vcpu affinity. This avoids the need to scan for +* IRTE and try to match ga_tag in the IOMMU driver. +*/ +struct list_head ir_list; +spinlock_t ir_list_lock; + +/* which host CPU was used for running this vcpu */ +unsigned int last_cpu; +}; + +#define __sme_page_pa (x) __sme_set(page_to_pfn(x) << PAGE_SHIFT) +struct enc_region { +struct list_head list; +unsigned long npages; +struct page **pages; +unsigned long uaddr; +unsigned long size; +}; + +static inline struct kvm_svm *to_kvm_svm (struct kvm *kvm) +{ + return container_of(kvm, struct kvm_svm, kvm); +} + +static inline bool sev_guest (struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; + +return sev->active; +#else +return false; +#endif +} + +static inline int sev_get_asid (struct kvm *kvm) +{ +struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info; + +return sev->asid; +} +#define SVM_CPUID_FUNC 0x8000000a#define SVM_VM_CR_SVM_DISABLE 4
arch/x86/kvm/svm.c +0 -155
181 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 2/2 KVM: SVM: move the SEV specific function in a separate file
In reply to Singh, Brijeshsvm.c was pretty huge, and the recent addition of SEV feature grew it further. Before we add more SEV command handling lets split the SEV bits into a separate file. The sev.c will be compiled only when CONFIG_KVM_AMD_SEV is selected.
--- arch/x86/kvm/Makefile | 2 + arch/x86/kvm/sev.c | 1076 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/sev.h | 40 ++ arch/x86/kvm/svm.c | 1079 +---------------------------------------- 4 files changed, 1141 insertions(+), 1056 deletions(-) create mode 100644 arch/x86/kvm/sev.c create mode 100644 arch/x86/kvm/sev.h
arch/x86/kvm/Makefile +2 -0
@@ -19,6 +19,8 @@ kvm-y += x86.o mmu.o emulate.o i8259.o irq.o lapic.o \ kvm-intel-y += vmx/vmx.o vmx/vmenter.o vmx/pmu_intel.o vmx/vmcs12.o vmx/evmcs.o vmx/nested.o kvm-amd-y += svm.o pmu_amd.o +kvm-amd-$(CONFIG_KVM_AMD_SEV) += sev.o + obj-$(CONFIG_KVM) += kvm.o obj-$(CONFIG_KVM_INTEL) += kvm-intel.o obj-$(CONFIG_KVM_AMD) += kvm-amd.o
arch/x86/kvm/sev.c new file +1076 -0
1,076 lines not on this page · raw message
arch/x86/kvm/sev.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef ARCH_X86_KVM_SEV_H +#define ARCH_X86_KVM_SEV_H + +#ifdef CONFIG_KVM_AMD_SEV + +bool svm_sev_enabled (void ); +unsigned int sev_get_max_asid (void ); +__init int sev_hardware_setup (void ); +__exit void sev_hardware_unsetup(void); +int svm_mem_enc_op (struct kvm *kvm,void __user *argp); +int svm_register_enc_region (struct kvm *kvm,struct kvm_enc_region *range); +int svm_unregister_enc_region (struct kvm *kvm,struct kvm_enc_region *range); +void sev_vm_destroy (struct kvm *kvm); + +#else + +static inline unsigned int sev_get_max_asid (void ) {return 0 ; } +static inline bool svm_sev_enabled (void ) {return false; } +static inline __init int sev_hardware_setup (void ) {return -ENOTTY ; } +static inline __exit void sev_hardware_unsetup(void) { } +static inline void sev_vm_destroy (struct kvm *kvm) { } + +static inline int svm_mem_enc_op (struct kvm *kvm,void __user *argp) +{ +return -ENOTTY ; +} +static int svm_register_enc_region (struct kvm *kvm,struct kvm_enc_region *range) +{ +return -ENOTTY ; +} +static inline int svm_unregister_enc_region (struct kvm *kvm, +struct kvm_enc_region *range) +{ +return -ENOTTY ; +} + +#endif /* CONFIG_KVM_AMD_SEV */ +#endif /* ARCH_X86_KVM_SEV_H */
arch/x86/kvm/svm.c +23 -1056
1,142 lines not on this page · raw message
Signature · 1 line
2.17.1
Quoted 3 lines · Brijesh
svm.cis pretty huge, before we add more SEV specific commands (e.g SEV-ES, SEV-Migration etc) lets move the SEV command handling into a separate file. There is no logical changes in this series.
Any reason not to create arch/x86/kvm/svm/ before introducing sev.c?
Quoted 25 lines
The patch is based on motivation from this thread:
https://marc.info/?l=linux-kernel&m=153910240507399&w=2Cc Borislav Petkov <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Tom Lendacky <[email protected]>Brijesh Singh (2):
KVM: SVM: move common struct definitions to header file
KVM: SVM: move the SEV specific function in a separate filearch/x86/include/asm/svm.h | 155 +++++ arch/x86/kvm/Makefile | 2 + arch/x86/kvm/sev.c | 1076 +++++++++++++++++++++++++++++++ arch/x86/kvm/sev.h | 40 ++ arch/x86/kvm/svm.c | 1234 +----------------------------------- 5 files changed, 1296 insertions(+), 1211 deletions(-) create mode 100644 arch/x86/kvm/sev.c create mode 100644 arch/x86/kvm/sev.hSignature · 1 line
2.17.1
Quoted 7 lines · Sean Christopherson
Quoted 3 lines · Brijesh
svm.cis pretty huge, before we add more SEV specific commands (e.g SEV-ES, SEV-Migration etc) lets move the SEV command handling into a separate file. There is no logical changes in this series.Any reason not to create
arch/x86/kvm/svm/before introducingsev.c?
There are several features in svm.c that can be moved to different files, SEV bits are just the start. I was thinking that we create svm directory after features are separated in different files rather than creating the directory first. Having said so, I am not oppose to creating directory first then moving the code in a separate file.
Paolo, Radim
thoughts ?
thanks
Hi Paolo and Radim,
Quoted 4 lines · Brijesh
svm.cis pretty huge, before we add more SEV specific commands (e.g SEV-ES, SEV-Migration etc) lets move the SEV command handling into a separate file. There is no logical changes in this series.
Any thoughts on this series ?
Quoted 13 lines
The patch is based on motivation from this thread:
https://marc.info/?l=linux-kernel&m=153910240507399&w=2Cc Borislav Petkov <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Tom Lendacky <[email protected]>Brijesh Singh (2):
KVM: SVM: move common struct definitions to header file
KVM: SVM: move the SEV specific function in a separate file
Quoted 9 lines
arch/x86/include/asm/svm.h | 155 +++++ arch/x86/kvm/Makefile | 2 + arch/x86/kvm/sev.c | 1076 +++++++++++++++++++++++++++++++ arch/x86/kvm/sev.h | 40 ++ arch/x86/kvm/svm.c | 1234 +----------------------------------- 5 files changed, 1296 insertions(+), 1211 deletions(-)create mode 100644
arch/x86/kvm/sev.c
create mode 100644arch/x86/kvm/sev.h
-Brijesh