KVM: SVM: Add support for SEV LAUNCH_FINISH command
PATCH v9 30/38 KVM: SVM: Add support for SEV LAUNCH_FINISH command
The command is used for finializing the SEV guest launch process.
--- arch/x86/kvm/svm.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
arch/x86/kvm/svm.c +23 -0
@@ -6113,6 +6113,26 @@ static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_launch_finish (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_launch_finish *data; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_FINISH , data, &argp->error); + +kfree (data); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6139,6 +6159,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_LAUNCH_MEASURE : r =sev_launch_measure (kvm, &sev_cmd);break ; +case KVM_SEV_LAUNCH_FINISH : + r =sev_launch_finish (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v9 31/38 KVM: SVM: Add support for SEV GUEST_STATUS command
The command is used for querying the SEV guest information.
--- arch/x86/kvm/svm.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
arch/x86/kvm/svm.c +33 -0
@@ -6133,6 +6133,36 @@ static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int sev_guest_status (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct kvm_sev_guest_status params; +struct sev_data_guest_status *data; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_GUEST_STATUS , data, &argp->error); +if (ret) + goto e_free; + + params.policy = data->policy; + params.state = data->state; + params.handle = data->handle; + +if (copy_to_user ((void __user *)(uintptr_t )argp->data, ¶ms,sizeof (params))) + ret = -EFAULT ; +e_free: +kfree (data); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6162,6 +6192,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_LAUNCH_FINISH : r =sev_launch_finish (kvm, &sev_cmd);break ; +case KVM_SEV_GUEST_STATUS : + r =sev_guest_status (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v9 32/38 KVM: SVM: Add support for SEV DEBUG_DECRYPT command
In reply to Brijesh SinghThe command is used for decrypting a guest memory region for debug purposes.
--- arch/x86/kvm/svm.c | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+)
arch/x86/kvm/svm.c +152 -0
@@ -6163,6 +6163,155 @@ static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)return ret; } +static int __sev_issue_dbg_cmd (struct kvm *kvm,unsigned long src, +unsigned long dst,int size, +int *error,bool enc) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_dbg *data; +int ret; + + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) +return -ENOMEM ; + + data->handle = sev->handle; + data->dst_addr = dst; + data->src_addr = src; + data->len = size; + + ret =sev_issue_cmd (kvm, + enc ?SEV_CMD_DBG_ENCRYPT :SEV_CMD_DBG_DECRYPT , + data, error); +kfree (data); +return ret; +} + +static int __sev_dbg_decrypt (struct kvm *kvm,unsigned long src_paddr, +unsigned long dst_paddr,int sz,int *err) +{ +int offset; + +/* +* Its safe to read more than we are asked, caller should ensure that +* destination has enough space. +*/ + src_paddr =round_down (src_paddr,16 ); + offset = src_paddr &15 ; + sz =round_up (sz + offset,16 ); + +return __sev_issue_dbg_cmd (kvm, src_paddr, dst_paddr, sz, err, false); +} + +static int __sev_dbg_decrypt_user (struct kvm *kvm,unsigned long paddr, +unsigned long __user dst_uaddr, +unsigned long dst_paddr, +int size,int *err) +{ +struct page *tpage =NULL ; +int ret, offset; + +/* if inputs are not 16-byte then use intermediate buffer */ +if (!IS_ALIGNED (dst_paddr,16 ) || + !IS_ALIGNED (paddr,16 ) || + !IS_ALIGNED (size,16 )) { + tpage = (void *)alloc_page (GFP_KERNEL ); +if (!tpage) +return -ENOMEM ; + + dst_paddr =__sme_page_pa (tpage); + } + + ret =__sev_dbg_decrypt (kvm, paddr, dst_paddr, size, err); +if (ret) + goto e_free; + +if (tpage) { + offset = paddr &15 ; +if (copy_to_user ((void __user *)(uintptr_t )dst_uaddr, +page_address (tpage) + offset, size)) + ret = -EFAULT ; + } + +e_free: +if (tpage) +__free_page (tpage); + +return ret; +} + +static int sev_dbg_crypt (struct kvm *kvm,struct kvm_sev_cmd *argp,bool dec) +{ +unsigned long vaddr, vaddr_end, next_vaddr; +unsigned long dst_vaddr, dst_vaddr_end; +struct page **src_p, **dst_p; +struct kvm_sev_dbg debug; +unsigned long n; +int ret, size; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (&debug, (void __user *)(uintptr_t )argp->data,sizeof (debug))) +return -EFAULT ; + + vaddr = debug.src_uaddr; + size = debug.len; + vaddr_end = vaddr + size; + dst_vaddr = debug.dst_uaddr; + dst_vaddr_end = dst_vaddr + size; + +for (; vaddr < vaddr_end; vaddr = next_vaddr) { +int len, s_off, d_off; + +/* lock userspace source and destination page */ + src_p =sev_pin_memory (kvm, vaddr &PAGE_MASK ,PAGE_SIZE , &n,0 ); +if (!src_p) +return -EFAULT ; + + dst_p =sev_pin_memory (kvm, dst_vaddr &PAGE_MASK ,PAGE_SIZE , &n,1 ); +if (!dst_p) { +sev_unpin_memory (kvm, src_p, n); +return -EFAULT ; + } + +/* +* The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the +* memory content (i.e it will write the same memory region with C=1). +* It's possible that the cache may contain the data with C=0, i.e., +* unencrypted so invalidate it first. +*/ +sev_clflush_pages (src_p,1 ); +sev_clflush_pages (dst_p,1 ); + +/* +* Since user buffer may not be page aligned, calculate the +* offset within the page. +*/ + s_off = vaddr & ~PAGE_MASK ; + d_off = dst_vaddr & ~PAGE_MASK ; + len =min_t (size_t, (PAGE_SIZE - s_off), size); + + ret =__sev_dbg_decrypt_user (kvm, +__sme_page_pa (src_p[0 ]) + s_off, + dst_vaddr, +__sme_page_pa (dst_p[0 ]) + d_off, + len, &argp->error); + +sev_unpin_memory (kvm, src_p,1 ); +sev_unpin_memory (kvm, dst_p,1 ); + +if (ret) + goto err; + + next_vaddr = vaddr + len; + dst_vaddr = dst_vaddr + len; + size -= len; + } +err: +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6195,6 +6344,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_GUEST_STATUS : r =sev_guest_status (kvm, &sev_cmd);break ; +case KVM_SEV_DBG_DECRYPT : + r =sev_dbg_crypt (kvm, &sev_cmd, true); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v9 33/38 KVM: SVM: Add support for SEV DEBUG_ENCRYPT command
In reply to Brijesh SinghThe command copies a plaintext into guest memory and encrypts it using the VM encryption key. The command will be used for debug purposes (e.g setting breakpoints through gdbserver)
--- arch/x86/kvm/svm.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 5 deletions(-)
arch/x86/kvm/svm.c +93 -5
@@ -6240,6 +6240,83 @@ static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,return ret; } +static int __sev_dbg_encrypt_user (struct kvm *kvm,unsigned long paddr, +unsigned long __user vaddr, +unsigned long dst_paddr, +unsigned long __user dst_vaddr, +int size,int *error) +{ +struct page *src_tpage =NULL ; +struct page *dst_tpage =NULL ; +int ret, len = size; + +/* If source buffer is not aligned then use an intermediate buffer */ +if (!IS_ALIGNED (vaddr,16 )) { + src_tpage =alloc_page (GFP_KERNEL ); +if (!src_tpage) +return -ENOMEM ; + +if (copy_from_user (page_address (src_tpage), + (void __user *)(uintptr_t )vaddr, size)) { +__free_page (src_tpage); +return -EFAULT ; + } + + paddr =__sme_page_pa (src_tpage); + } + +/* +* If destination buffer or length is not aligned then do read-modify-write: +* - decrypt destination in an intermediate buffer +* - copy the source buffer in an intermediate buffer +* - use the intermediate buffer as source buffer +*/ +if (!IS_ALIGNED (dst_vaddr,16 ) || !IS_ALIGNED (size,16 )) { +int dst_offset; + + dst_tpage =alloc_page (GFP_KERNEL ); +if (!dst_tpage) { + ret = -ENOMEM ; + goto e_free; + } + + ret =__sev_dbg_decrypt (kvm, dst_paddr, +__sme_page_pa (dst_tpage), size, error); +if (ret) + goto e_free; + +/* +* If source is kernel buffer then use memcpy() otherwise +* copy_from_user(). +*/ + dst_offset = dst_paddr &15 ; + +if (src_tpage) +memcpy (page_address (dst_tpage) + dst_offset, +page_address (src_tpage), size); +else { +if (copy_from_user (page_address (dst_tpage) + dst_offset, + (void __user *)(uintptr_t )vaddr, size)) { + ret = -EFAULT ; + goto e_free; + } + } + + paddr =__sme_page_pa (dst_tpage); + dst_paddr =round_down (dst_paddr,16 ); + len =round_up (size,16 ); + } + + ret =__sev_issue_dbg_cmd (kvm, paddr, dst_paddr, len, error, true); + +e_free: +if (src_tpage) +__free_page (src_tpage); +if (dst_tpage) +__free_page (dst_tpage); +return ret; +} +static int sev_dbg_crypt (struct kvm *kvm,struct kvm_sev_cmd *argp,bool dec) {unsigned long vaddr, vaddr_end, next_vaddr;
@@ -6292,11 +6369,19 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec) d_off = dst_vaddr & ~PAGE_MASK ; len =min_t (size_t, (PAGE_SIZE - s_off), size); - ret =__sev_dbg_decrypt_user (kvm, -__sme_page_pa (src_p[0 ]) + s_off, - dst_vaddr, -__sme_page_pa (dst_p[0 ]) + d_off, - len, &argp->error); +if (dec) + ret =__sev_dbg_decrypt_user (kvm, +__sme_page_pa (src_p[0 ]) + s_off, + dst_vaddr, +__sme_page_pa (dst_p[0 ]) + d_off, + len, &argp->error); +else + ret =__sev_dbg_encrypt_user (kvm, +__sme_page_pa (src_p[0 ]) + s_off, + vaddr, +__sme_page_pa (dst_p[0 ]) + d_off, + dst_vaddr, + len, &argp->error);sev_unpin_memory (kvm, src_p,1 );sev_unpin_memory (kvm, dst_p,1 );
@@ -6347,6 +6432,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_DBG_DECRYPT : r =sev_dbg_crypt (kvm, &sev_cmd, true);break ; +case KVM_SEV_DBG_ENCRYPT : + r =sev_dbg_crypt (kvm, &sev_cmd, false); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v9 34/38 KVM: SVM: Add support for SEV LAUNCH_SECRET command
In reply to Brijesh SinghThe command is used for injecting a secret into the guest memory region.
--- arch/x86/kvm/svm.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
arch/x86/kvm/svm.c +68 -0
@@ -6397,6 +6397,71 @@ static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)return ret; } +static int sev_launch_secret (struct kvm *kvm,struct kvm_sev_cmd *argp) +{ +struct kvm_sev_info *sev = &kvm->arch.sev_info; +struct sev_data_launch_secret *data; +struct kvm_sev_launch_secret params; +struct page **pages; +void *blob, *hdr; +unsigned long n; +int ret; + +if (!sev_guest (kvm)) +return -ENOTTY ; + +if (copy_from_user (¶ms, (void __user *)(uintptr_t )argp->data,sizeof (params))) +return -EFAULT ; + + pages =sev_pin_memory (kvm, params.guest_uaddr, params.guest_len, &n,1 ); +if (!pages) +return -ENOMEM ; + +/* +* The secret must be copied into contiguous memory region, lets verify +* that userspace memory pages are contiguous before we issue command. +*/ +if (get_num_contig_pages (0 , pages, n) != n) { + ret = -EINVAL ; + goto e_unpin_memory; + } + + ret = -ENOMEM ; + data =kzalloc (sizeof (*data),GFP_KERNEL ); +if (!data) + goto e_unpin_memory; + + blob =psp_copy_user_blob (params.trans_uaddr, params.trans_len); +if (IS_ERR (blob)) { + ret =PTR_ERR (blob); + goto e_free; + } + + data->trans_address =__psp_pa (blob); + data->trans_len = params.trans_len; + + hdr =psp_copy_user_blob (params.hdr_uaddr, params.hdr_len); +if (IS_ERR (hdr)) { + ret =PTR_ERR (hdr); + goto e_free_blob; + } + data->trans_address =__psp_pa (blob); + data->trans_len = params.trans_len; + + data->handle = sev->handle; + ret =sev_issue_cmd (kvm,SEV_CMD_LAUNCH_UPDATE_SECRET , data, &argp->error); + +kfree (hdr); + +e_free_blob: +kfree (blob); +e_free: +kfree (data); +e_unpin_memory: +sev_unpin_memory (kvm, pages, n); +return ret; +} +static int svm_mem_enc_op (struct kvm *kvm,void __user *argp) {struct kvm_sev_cmd sev_cmd;
@@ -6435,6 +6500,9 @@ static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)case KVM_SEV_DBG_ENCRYPT : r =sev_dbg_crypt (kvm, &sev_cmd, false);break ; +case KVM_SEV_LAUNCH_SECRET : + r =sev_launch_secret (kvm, &sev_cmd); +break ;default : r = -EINVAL ; goto out;
Signature · 1 line
2.9.5
PATCH v9 35/38 KVM: SVM: Pin guest memory when SEV is active
In reply to Brijesh SinghThe SEV memory encryption engine uses a tweak such that two identical plaintext pages at different location will have different ciphertext. So swapping or moving ciphertext of two pages will not result in plaintext being swapped. Relocating (or migrating) physical backing pages for a SEV guest will require some additional steps. The current SEV key management spec does not provide commands to swap or migrate (move) ciphertext pages. For now, we pin the guest memory registered through KVM_MEMORY_ENCRYPT_REG_REGION ioctl.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/svm.c | 132 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -753,6 +753,7 @@ struct kvm_sev_info {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_arch {
arch/x86/kvm/svm.c +132 -0
169 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH v9 36/38 KVM: SVM: Clear C-bit from the page fault address
In reply to Brijesh SinghWhen SEV is active, on #VMEXIT the page fault address will contain the C-bit. We must clear the C-bit before handling the fault.
--- arch/x86/kvm/svm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/svm.c +2 -2
@@ -2430,7 +2430,7 @@ static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)static int pf_interception (struct vcpu_svm *svm) { -u64 fault_address = svm->vmcb->control.exit_info_2; +u64 fault_address =__sme_clr (svm->vmcb->control.exit_info_2);u64 error_code = svm->vmcb->control.exit_info_1; return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
@@ -2440,7 +2440,7 @@ static int pf_interception(struct vcpu_svm *svm)static int npf_interception (struct vcpu_svm *svm) { -u64 fault_address = svm->vmcb->control.exit_info_2; +u64 fault_address =__sme_clr (svm->vmcb->control.exit_info_2);u64 error_code = svm->vmcb->control.exit_info_1;trace_kvm_page_fault (fault_address, error_code);
Signature · 1 line
2.9.5
PATCH v9 37/38 KVM: SVM: Do not install #UD intercept when SEV is enabled
In reply to Brijesh SinghOn #UD, x86_emulate_instruction() fetches the data from guest memory and decodes the instruction bytes to assist further. When SEV is enabled, the instruction bytes will be encrypted using the guest-specific key and the hypervisor will no longer able to fetch the instruction bytes to assist UD handling. By not installing intercept we let the guest receive and handle #UD.
--- arch/x86/kvm/svm.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/svm.c +3 -1
@@ -1443,8 +1443,10 @@ static void init_vmcb(struct vcpu_svm *svm) svm->vmcb->control.int_ctl |=V_GIF_ENABLE_MASK ; } -if (sev_guest (svm->vcpu.kvm)) +if (sev_guest (svm->vcpu.kvm)) { svm->vmcb->control.nested_ctl |=SVM_NESTED_CTL_SEV_ENABLE ; +clr_exception_intercept (svm,UD_VECTOR ); + }mark_all_dirty (svm->vmcb);
Signature · 1 line
2.9.5
PATCH v9 38/38 KVM: X86: Restart the guest when insn_len is zero and SEV is enabled
In reply to Brijesh SinghOn AMD platforms, under certain conditions insn_len may be zero on #NPF. This can happen if a guest gets a page-fault on data access but the HW table walker is not able to read the instruction page (e.g instruction page is not present in memory).
Typically, when insn_len is zero, x86_emulate_instruction() walks the guest page table and fetches the instruction bytes from guest memory. When SEV is enabled, the guest memory is encrypted with guest-specific key hence hypervisor will not able to fetch the instruction bytes. In those cases we simply restart the guest.
I have encountered this issue when running kernbench inside the guest.
--- arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/mmu.c +10 -0
@@ -4950,6 +4950,16 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,if (mmio_info_in_cache (vcpu, cr2, direct)) emulation_type =0 ; emulate: +/* +* On AMD platforms, under certain conditions insn_len may be zero on #NPF. +* This can happen if a guest gets a page-fault on data access but the HW +* table walker is not able to read the instruction page (e.g instruction +* page is not present in memory). In those cases we simply restart the +* guest. +*/ +if (unlikely (insn && !insn_len)) +return 1 ; + er =x86_emulate_instruction (vcpu, cr2, emulation_type, insn, insn_len);switch (er) {
arch/x86/kvm/svm.c +4 -2
@@ -2436,7 +2436,8 @@ static int pf_interception(struct vcpu_svm *svm)u64 error_code = svm->vmcb->control.exit_info_1;return kvm_handle_page_fault (&svm->vcpu, error_code, fault_address, - svm->vmcb->control.insn_bytes, +static_cpu_has (X86_FEATURE_DECODEASSISTS ) ? + svm->vmcb->control.insn_bytes :NULL , svm->vmcb->control.insn_len); }
@@ -2447,7 +2448,8 @@ static int npf_interception(struct vcpu_svm *svm)trace_kvm_page_fault (fault_address, error_code);return kvm_mmu_page_fault (&svm->vcpu, fault_address, error_code, - svm->vmcb->control.insn_bytes, +static_cpu_has (X86_FEATURE_DECODEASSISTS ) ? + svm->vmcb->control.insn_bytes :NULL , svm->vmcb->control.insn_len); }
Signature · 1 line
2.9.5
Re: [Part2 PATCH v9 38/38] KVM: X86: Restart the guest when insn_len is zero and SEV is enabled
Quoted 39 lines · Brijesh Singh
On AMD platforms, under certain conditions
insn_lenmay be zero on #NPF. This can happen if a guest gets a page-fault on data access but the HW table walker is not able to read the instruction page (e.g instruction page is not present in memory).Typically, when
insn_lenis zero,x86_emulate_instruction()walks the guest page table and fetches the instruction bytes from guest memory. When SEV is enabled, the guest memory is encrypted with guest-specific key hence hypervisor will not able to fetch the instruction bytes. In those cases we simply restart the guest.I have encountered this issue when running kernbench inside the guest.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-)arch/x86/kvm/mmu.c +0
-0@@ -4950,6 +4950,16 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,if (mmio_info_in_cache (vcpu, cr2, direct)) emulation_type =0 ; emulate:
arch/x86/kvm/mmu.c int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code, · line 4962
Quoted 10 lines
arch/x86/kvm/mmu.c +10
-0+/* +* On AMD platforms, under certain conditions insn_len may be zero on #NPF. +* This can happen if a guest gets a page-fault on data access but the HW +* table walker is not able to read the instruction page (e.g instruction +* page is not present in memory). In those cases we simply restart the +* guest. +*/ +if (unlikely (insn && !insn_len)) +return 1 ; +
How does this work, for instance, with MMIO at CPL3 with SMAP enabled?
Re: [Part2 PATCH v9 38/38] KVM: X86: Restart the guest when insn_len is zero and SEV is enabled
Quoted 44 lines · Jim Mattson
Quoted 43 lines · Brijesh Singh
On AMD platforms, under certain conditions
insn_lenmay be zero on #NPF. This can happen if a guest gets a page-fault on data access but the HW table walker is not able to read the instruction page (e.g instruction page is not present in memory).Typically, when
insn_lenis zero,x86_emulate_instruction()walks the guest page table and fetches the instruction bytes from guest memory. When SEV is enabled, the guest memory is encrypted with guest-specific key hence hypervisor will not able to fetch the instruction bytes. In those cases we simply restart the guest.I have encountered this issue when running kernbench inside the guest.
Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc "Radim Krčmář" <[email protected]>Cc Joerg Roedel <[email protected]>Cc Borislav Petkov <[email protected]>Cc Tom Lendacky <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/mmu.c | 10 ++++++++++ arch/x86/kvm/svm.c | 6 ++++-- 2 files changed, 14 insertions(+), 2 deletions(-)arch/x86/kvm/mmu.c +4
-0@@ -4950,6 +4950,16 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code,if (mmio_info_in_cache (vcpu, cr2, direct)) emulation_type =0 ; emulate: +/* +* On AMD platforms, under certain conditions insn_len may be zero on #NPF. +* This can happen if a guest gets a page-fault on data access but the HW +* table walker is not able to read the instruction page (e.g instruction
Quoted 9 lines
arch/x86/kvm/mmu.c int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t cr2, u64 error_code, · line 4962
Quoted 6 lines
arch/x86/kvm/mmu.c +6
-0+* page is not present in memory). In those cases we simply restart the +* guest. +*/ +if (unlikely (insn && !insn_len)) +return 1 ; +How does this work, for instance, with MMIO at CPL3 with SMAP enabled?
The processor will still attempt to supply correct instruction bytes in this case (basically it disables SMAP temporarily during this read). *HOWEVER*, looking through Zen docs, there is an errata about this on Zen. See errata 1096 in
https://www.amd.com/system/files/TechDocs/55449_Fam_17h_M_00h0Fh_Rev_Guide.pdf
Looking at errata it seems on Zen processors, the CPU will not supply instruction bytes in this scenario (MMIO at CPL3 with SMAP=1). I will reach out to HW folks to see if there is any plans to fix in upcoming CPU cores.
We probably need to workaround for this errata. In non SEV case we can let the host read the instruction bytes but in SEV case we can disable SMAP temporarily in CR4, then retry the guest, and then restore the CR4.SMAP settings. I will work on patch and post on mailing list.
-Brijesh