Add AMD Secure Nested Paging (SEV-SNP)
Subsystems (12)
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Peter Gonda[Public]
Hello Peter,
-----Original Message-----
From: Peter Gonda <[email protected]>
Sent: Friday, June 24, 2022 11:25 AM
To: Kalra, Ashish <[email protected]>
Cc: the arch/x86 maintainers <[email protected]>; LKML <[email protected]>; kvm list <[email protected]>; [email protected]; [email protected]; Linux Crypto Mailing List <[email protected]>; Thomas Gleixner <[email protected]>; Ingo Molnar <[email protected]>; Joerg Roedel <[email protected]>; Lendacky, Thomas <[email protected]>; H. Peter Anvin <[email protected]>; Ard Biesheuvel <[email protected]>; Paolo Bonzini <[email protected]>; Sean Christopherson <[email protected]>; Vitaly Kuznetsov <[email protected]>; Jim Mattson <[email protected]>; Andy Lutomirski <[email protected]>; Dave Hansen <[email protected]>; Sergio Lopez <[email protected]>; Peter Zijlstra <[email protected]>; Srinivas Pandruvada <[email protected]>; David Rientjes <[email protected]>; Dov Murik <[email protected]>; Tobin Feldman-Fitzthum <[email protected]>; Borislav Petkov <[email protected]>; Roth, Michael <[email protected]>; Vlastimil Babka <[email protected]>; Kirill A . Shutemov <[email protected]>; Andi Kleen <[email protected]>; Tony Luck <[email protected]>; Marc Orr <[email protected]>; Sathyanarayanan Kuppuswamy <[email protected]>; Alper Gun <[email protected]>; Dr. David Alan Gilbert <[email protected]>; [email protected]
Subject: Re: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
Quoted 40 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 196 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 192 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c index renamed from arch/x86/kvm/svm/sev.c +0
-0
7fc0fad87054..089af21a4efe100644arch/x86/kvm/svm/sev.c +1
-0@@ -343,6 +343,7 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) spin_lock_init(&sev->psc_lock); ret =sev_snp_init (&argp->error); +mutex_init (&sev->guest_req_lock); }else { ret =sev_platform_init (&argp->error);}arch/x86/kvm/svm/sev.c +1
-0@@ -1884,23 +1885,39 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)static void *snp_context_create (struct kvm *kvm,struct kvm_sev_cmd *argp) { +void *context =NULL , *certs_data =NULL , *resp_page =NULL ;
Quoted 2 lines
Is the NULL setting here unnecessary since all of these are set via functions
snp_alloc_firmware_page(),kmalloc(), andsnp_alloc_firmware_page()respectively?
Yes, they don't need to be set to NULL.
Quoted 11 lines
arch/x86/kvm/svm/sev.c +5
-1+struct kvm_sev_info *sev = &to_kvm_svm (kvm)->sev_info;struct sev_data_snp_gctx_create data = {}; -void *context;int rc; +/* Allocate memory used for the certs data in SNP guest request */ + certs_data =kmalloc (SEV_FW_BLOB_MAX_SIZE ,GFP_KERNEL_ACCOUNT ); +if (!certs_data) +return NULL ;I think we want to use
kzalloc()here to ensure we never give the guest uninitialized kernel memory.
Yes.
Quoted 67 lines
arch/x86/kvm/svm/sev.c +7
-1+/* Allocate memory for context page */ context =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT );if (!context) -return NULL ; + goto e_free; + +/* Allocate a firmware buffer used during the guest command handling. */ + resp_page =snp_alloc_firmware_page (GFP_KERNEL_ACCOUNT ); +if (!resp_page) + goto e_free;|resp_page| doesn't appear to be used anywhere?
data.gctx_paddr = __psp_pa(context); rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error);arch/x86/kvm/svm/sev.c +14
-4-if (rc) { -snp_free_firmware_page (context); -return NULL ; - } +if (rc) + goto e_free; + + sev->snp_certs_data = certs_data;return context; + +e_free: +snp_free_firmware_page (context); +kfree (certs_data); +return NULL ; } static int snp_bind_asid(struct kvm *kvm, int *error) @@ -2565,6 +2582,8 @@ static int snp_decommission_context(struct kvm *kvm) snp_free_firmware_page(sev->snp_context); sev->snp_context =NULL ; +kfree (sev->snp_certs_data); +return 0 ; }@@ -3077,6 +3096,8 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)case SVM_VMGEXIT_UNSUPPORTED_EVENT :case SVM_VMGEXIT_HV_FEATURES :case SVM_VMGEXIT_PSC : +case SVM_VMGEXIT_GUEST_REQUEST : +case SVM_VMGEXIT_EXT_GUEST_REQUEST :break ;default : reason = GHCB_ERR_INVALID_EVENT; @@ -3502,6 +3523,155@@ static unsigned long
snp_handle_page_state_change(struct vcpu_svm *svm)return rc ? map_to_psc_vmgexit_code(rc) : 0; }arch/x86/kvm/svm/sev.c +10
-0+static unsigned long snp_setup_guest_buf (struct vcpu_svm *svm, +struct sev_data_snp_guest_request *data, +gpa_t req_gpa,gpa_t +resp_gpa) { +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +kvm_pfn_t req_pfn, resp_pfn; +struct kvm_sev_info *sev; + + sev = &to_kvm_svm (kvm)->sev_info;
Quoted 3 lines
This is normally done at declaration in this file. Why not here?
struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Ok.
Quoted 31 lines
arch/x86/kvm/svm/sev.c +30
-0+ +if (!IS_ALIGNED (req_gpa,PAGE_SIZE ) || !IS_ALIGNED (resp_gpa,PAGE_SIZE )) +return SEV_RET_INVALID_PARAM ; + + req_pfn =gfn_to_pfn (kvm,gpa_to_gfn (req_gpa)); +if (is_error_noslot_pfn (req_pfn)) +return SEV_RET_INVALID_ADDRESS ; + + resp_pfn =gfn_to_pfn (kvm,gpa_to_gfn (resp_gpa)); +if (is_error_noslot_pfn (resp_pfn)) +return SEV_RET_INVALID_ADDRESS ; + +if (rmp_make_private (resp_pfn,0 ,PG_LEVEL_4K ,0 , true)) +return SEV_RET_INVALID_ADDRESS ; + + data->gctx_paddr =__psp_pa (sev->snp_context); + data->req_paddr =__sme_set (req_pfn <<PAGE_SHIFT ); + data->res_paddr =__sme_set (resp_pfn <<PAGE_SHIFT ); + +return 0 ; +} + +static void snp_cleanup_guest_buf (struct sev_data_snp_guest_request +*data,unsigned long *rc) { +u64 pfn =__sme_clr (data->res_paddr) >>PAGE_SHIFT ; +int ret; + + ret =snp_page_reclaim (pfn); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ;
Quoted 1 line
Do we need a diff error code here? This means the page the guest gives us is now "stuck" in the FW owned state. How would the guest know this is the case? We return the exact same error in
snp_setup_guest_buf()if theresp_gpaisn't page aligned >so now if the guest ever sees aSEV_RET_INVALID_ADDRESSI think its only safe option is to either try andpage_state_changeit to a know state or mark it as unusable memory.
If snp_page_reclaim() fails, it will invoke snp_leak_pages() which will indicate memory failure and trigger memory recovery mechanisms and that should drop the pages or mark them unusable.
Quoted 8 lines
arch/x86/kvm/svm/sev.c +4
-0+ + ret =rmp_make_shared (pfn,PG_LEVEL_4K ); +if (ret) + *rc =SEV_RET_INVALID_ADDRESS ;Ditto here I think we need some way to signal to the guest what state this page is on return to guest execution.
Also these errors shadow over FW successes, this means the guest's guest-request-sequence-numbers are now out of sync meaning this VMPCK is unusable less the guest risk reusing the AES IV (which would break the confidentiality/integrity). >Should we have a way to signal to the guest the FW has successfully run your command but we could not change the page states back correctly, so the guest should increment their sequence numbers.
Yes, that is an important observation, and the sequence numbers are now out of sync.
But as this is an error path, so what's the guarantee that the next guest message request will succeed completely, isn’t it better to let the FW reject any subsequent guest messages once it has detected that the sequence numbers are out of sync ?
Quoted 18 lines
arch/x86/kvm/svm/sev.c +17
-0+} + +static void snp_handle_guest_request (struct vcpu_svm *svm,gpa_t +req_gpa,gpa_t resp_gpa) { +struct sev_data_snp_guest_request data = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +struct kvm_sev_info *sev; +unsigned long rc; +int err; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info;
Quoted 1 line
Ditto why not due this above?
Ok.
Quoted 77 lines
arch/x86/kvm/svm/sev.c +76
-0+ +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &data, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =sev_issue_cmd (kvm,SEV_CMD_SNP_GUEST_REQUEST , &data, &err); +if (rc) +/* use the firmware error code */ + rc = err; + +snp_cleanup_guest_buf (&data, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); } + +static void snp_handle_ext_guest_request (struct vcpu_svm *svm,gpa_t +req_gpa,gpa_t resp_gpa) { +struct sev_data_snp_guest_request req = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX ]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX ]; + +if (!IS_ALIGNED (data_gpa,PAGE_SIZE )) { + rc =SEV_RET_INVALID_ADDRESS ; + goto e_fail; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + goto e_fail; + } + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &req, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ;
Quoted 1 line
Since at this point the PSP FW has correctly executed the command and incremented the VMPCK sequence number I think we need another error signal here since this will tell the guest the PSP had an error so it will not know if the VMPCK sequence >number should be incremented.
Similarly as above, as this is an error path, so what's the guarantee that the next guest message request will succeed completely, isn’t it better to let the FW reject any subsequent guest messages once it has detected that the sequence numbers are out of sync ?
Quoted 51 lines
arch/x86/kvm/svm/sev.c +25
-1+ +cleanup: +snp_cleanup_guest_buf (&req, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); } +static int sev_handle_vmgexit_msr_protocol (struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3753,6 +3923,20 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu)svm_set_ghcb_sw_exit_info_2 (vcpu, rc);break ; } +case SVM_VMGEXIT_GUEST_REQUEST : { +snp_handle_guest_request (svm, control->exit_info_1, + control->exit_info_2); + + ret =1 ; +break ; + } +case SVM_VMGEXIT_EXT_GUEST_REQUEST : { +snp_handle_ext_guest_request (svm, + control->exit_info_1, + control->exit_info_2); + + ret =1 ; +break ; + }case SVM_VMGEXIT_UNSUPPORTED_EVENT : vcpu_unimpl(vcpu, "vmgexit: unsupported event -exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git
a/arch/x86/kvm/svm/svm.hb/arch/x86/kvm/svm/svm.hindex3fd95193ed8d..3be24da1a743100644arch/x86/kvm/svm/svm.h +2
-1@@ -98,6 +98,8 @@ struct kvm_sev_info {u64 snp_init_flags;void *snp_context;/* SNP guest context page */ spinlock_t psc_lock; +void *snp_certs_data; +struct mutex guest_req_lock; };struct kvm_svm { --2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
[Public]
Quoted 57 lines
Quoted 56 lines
arch/x86/kvm/svm/sev.c +56
-0+static void snp_handle_ext_guest_request (struct vcpu_svm *svm,gpa_t +req_gpa,gpa_t resp_gpa) { +struct sev_data_snp_guest_request req = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX ]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX ]; + +if (!IS_ALIGNED (data_gpa,PAGE_SIZE )) { + rc =SEV_RET_INVALID_ADDRESS ; + goto e_fail; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + goto e_fail; + } + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &req, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ;
Quoted 4 lines
Quoted 1 line
Since at this point the PSP FW has correctly executed the command and incremented the VMPCK sequence number I think we need another error signal here since this will tell the guest the PSP had an error so it will not know if the VMPCK sequence >number should be incremented.
Similarly as above, as this is an error path, so what's the guarantee that the next guest message request will succeed completely, isn’t it better to let the FW reject any subsequent guest messages once it has detected that the sequence numbers are out of sync ?
Alternately, we probably can return SEV_RET_INVALID_PAGE_STATE/SEV_RET_INVALID_PAGE_OWNER here, but that still does not indicate to the guest
that the FW has successfully executed the command and the error occurred during cleanup/result phase and it needs to increment the VMPCK sequence number. There is nothing as such defined in SNP FW API specs to indicate such kind of failures to guest. As I mentioned earlier, this is probably indicative of a bigger system failure and it is better to let the FW reject subsequent guest messages/requests once it has detected that the sequence numbers are out of sync.
Thanks,
Ashish
Re: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
Quoted 61 lines · Ashish
[Public]
Quoted 56 lines
Quoted 56 lines
arch/x86/kvm/svm/sev.c +56
-0+static void snp_handle_ext_guest_request (struct vcpu_svm *svm,gpa_t +req_gpa,gpa_t resp_gpa) { +struct sev_data_snp_guest_request req = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX ]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX ]; + +if (!IS_ALIGNED (data_gpa,PAGE_SIZE )) { + rc =SEV_RET_INVALID_ADDRESS ; + goto e_fail; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) { + rc =SEV_RET_INVALID_PARAM ; + goto e_fail; + } + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &req, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Since at this point the PSP FW has correctly executed the command and incremented the VMPCK sequence number I think we need another error signal here since this will tell the guest the PSP had an error so it will not know if the VMPCK sequence >number should be incremented.
Similarly as above, as this is an error path, so what's the guarantee that the next guest message request will succeed completely, isn’t it better to let the FW reject any subsequent guest messages once it has detected that the sequence numbers are out of sync ?
Alternately, we probably can return SEV_RET_INVALID_PAGE_STATE/SEV_RET_INVALID_PAGE_OWNER here, but that still does not indicate to the guest
that the FW has successfully executed the command and the error occurred during cleanup/result phase and it needs to increment the VMPCK sequence number. There is nothing as such defined in SNP FW API specs to indicate such kind of failures to guest. As I mentioned earlier, this is probably indicative of a bigger system failure and it is better to let the FW reject subsequent guest messages/requests once it has detected that the sequence numbers are out of sync.
Hmm I think the guest must be careful here because the guest could not trust the hypervisor here to be truthful about the sequence numbers incrementing. That's unfortunate since this means if these operations do fail with a well behaved hypervisor the guest cannot use that VMPCK again. But there is no harm in the guest re-issuing the SNP_GUEST_REQUEST (or extended version) with the exact same request just in at a different address. The GHCB spec actually calls this out " It is recommended that the hypervisor validate the guest physical address of the response page before invoking the SNP_GUEST_REQUEST API so that the sequence numbers do not get out of sync for the guest, possibly resulting in all successive requests failing".
Currently SVM_VMGEXIT_GUEST_REQUEST and SVM_VMGEXIT_EXT_GUEST_REQUEST have different hypervisor -> guest usage for SW_EXITINFO2. I think they both should be defined as what SVM_VMGEXIT_EXT_GUEST_REQUEST is
now: the high 32bits are the hypervisor error code, the low 32bits are the FW error code. This would allow for both NAEs to have some signal to the guest say SEV_RET_INVALID_REQ_ADDRESS. The hypervisor can use this error code when doing the validation on the request and response regions, if some is wrong with them the guest can retry with the exact same request (so no IV reuse) in a corrected region.
But another reason I think SVM_VMGEXIT_GUEST_REQUEST SW_EXITINFO2 hypervisor->guest state should include this change is because in this patch we are currently overloading the lower 32bits with hypervisor error codes. In snp_handle_guest_request() if sev_snp_guest(), snp_setup_guest_buf(), or snp_cleanup_guest_buf() fails we use the low 32bits of SW_EXITINFO2 to return hypervisor errors to the guest.
Quoted 3 lines
Thanks,
Ashish
Tom Lendacky
maintainerRe: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
In reply to Ashish KalraQuoted 41 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>--- arch/x86/kvm/svm/sev.c | 196 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 2 + 2 files changed, 192 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +17
-0+static void snp_handle_ext_guest_request (struct vcpu_svm *svm,gpa_t req_gpa,gpa_t resp_gpa) +{ +struct sev_data_snp_guest_request req = {0 }; +struct kvm_vcpu *vcpu = &svm->vcpu; +struct kvm *kvm = vcpu->kvm; +unsigned long data_npages; +struct kvm_sev_info *sev; +unsigned long rc, err; +u64 data_gpa; + +if (!sev_snp_guest (vcpu->kvm)) { + rc =SEV_RET_INVALID_GUEST ; + goto e_fail; + } + + sev = &to_kvm_svm (kvm)->sev_info; +
arch/x86/kvm/svm/sev.c static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm) · line 3634
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ data_gpa = vcpu->arch.regs[VCPU_REGS_RAX ]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX ]; + +if (!IS_ALIGNED (data_gpa,PAGE_SIZE )) { + rc =SEV_RET_INVALID_ADDRESS ; + goto e_fail; + } + +/* Verify that requested blob will fit in certificate buffer */ +if ((data_npages <<PAGE_SHIFT ) >SEV_FW_BLOB_MAX_SIZE ) {
Not sure this is a valid check... Isn't it OK if the guest has supplied more room than is required? If the guest supplies 8 pages and the hypervisor only needs to copy 1 page of data (or the SEV_FW_BLOB_MAX_SIZE number of pages) that shouldn't be an error. I think this check can go, right?
Thanks,
Tom
Quoted 40 lines
arch/x86/kvm/svm/sev.c +40
-0+ rc =SEV_RET_INVALID_PARAM ; + goto e_fail; + } + +mutex_lock (&sev->guest_req_lock); + + rc =snp_setup_guest_buf (svm, &req, req_gpa, resp_gpa); +if (rc) + goto unlock; + + rc =snp_guest_ext_guest_request (&req, (unsigned long )sev->snp_certs_data, + &data_npages, &err); +if (rc) { +/* +* If buffer length is small then return the expected +* length in rbx. +*/ +if (err ==SNP_GUEST_REQ_INVALID_LEN ) + vcpu->arch.regs[VCPU_REGS_RBX ] = data_npages; + +/* pass the firmware error code */ + rc = err; + goto cleanup; + } + +/* Copy the certificate blob in the guest memory */ +if (data_npages && +kvm_write_guest (kvm, data_gpa, sev->snp_certs_data, data_npages <<PAGE_SHIFT )) + rc =SEV_RET_INVALID_ADDRESS ; + +cleanup: +snp_cleanup_guest_buf (&req, &rc); + +unlock: +mutex_unlock (&sev->guest_req_lock); + +e_fail: +svm_set_ghcb_sw_exit_info_2 (vcpu, rc); +} +
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
Hello Tom,
Quoted 47 lines · Tom Lendacky
Quoted 46 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>---
arch/x86/kvm/svm/sev.c| 196 +++++++++++++++++++++++++++++++++++++++--arch/x86/kvm/svm/svm.h| 2 +2 files changed, 192 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +1
-0+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t
req_gpa,gpa_tresp_gpa)arch/x86/kvm/svm/sev.c +20
-0+{ + struct sev_data_snp_guest_request req = {0}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long rc, err; + u64 data_gpa; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {
Quoted 9 lines
Quoted 6 lines
+ rc =
SEV_RET_INVALID_ADDRESS;
+ gotoe_fail;
+ }
+
+ /* Verify that requested blob will fit in certificate buffer */ + if ((data_npages<<PAGE_SHIFT) >SEV_FW_BLOB_MAX_SIZE) {Not sure this is a valid check... Isn't it OK if the guest has supplied more room than is required? If the guest supplies 8 pages and the hypervisor only needs to copy 1 page of data (or the
SEV_FW_BLOB_MAX_SIZEnumber of pages) that shouldn't be an error. I think this check can go, right?
Agreed.
The check should probably be
if ((data_npages << PAGE_SHIFT) < SEV_FW_BLOB_MAX_SIZE)
and that check already exists in:
snp_guest_ext_guest_request(...)
{
...
...
/*
* Check if there is enough space to copy the certificate
chain. Otherwise
* return ERROR code defined in the GHCB specification.
*/
expected_npages = sev->snp_certs_len >> PAGE_SHIFT;
if (*npages < expected_npages) {
*npages = expected_npages;
*fw_err = SNP_GUEST_REQ_INVALID_LEN;
return -EINVAL;
}...
Thanks,
Ashish
Quoted 45 lines
Thanks,
TomQuoted 42 lines
+ rc =
SEV_RET_INVALID_PARAM;
+ gotoe_fail;
+ }arch/x86/kvm/svm/sev.c +8
-0+ + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa); + if (rc) + goto unlock; + + rc = snp_guest_ext_guest_request(&req, (unsignedlong)sev->snp_certs_data,
+&data_npages, &err);
+ if (rc) {
+ /*
+ * If buffer length is small then return the expected + * length in rbx.
+ */
+ if (err ==SNP_GUEST_REQ_INVALID_LEN)
+ vcpu->arch.regs[VCPU_REGS_RBX] =data_npages;arch/x86/kvm/svm/sev.c +9
-0+ + /* pass the firmware error code */ + rc = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (data_npages && + kvm_write_guest(kvm, data_gpa, sev->snp_certs_data,
data_npages<<PAGE_SHIFT))
+ rc =SEV_RET_INVALID_ADDRESS;arch/x86/kvm/svm/sev.c +11
-0+ +cleanup: + snp_cleanup_guest_buf(&req, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + svm_set_ghcb_sw_exit_info_2(vcpu, rc); +} +
Tom Lendacky
maintainerRe: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
Quoted 60 lines · Ashish
Hello Tom,
Quoted 47 lines · Tom Lendacky
Quoted 46 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>---
arch/x86/kvm/svm/sev.c| 196 +++++++++++++++++++++++++++++++++++++++--arch/x86/kvm/svm/svm.h| 2 +2 files changed, 192 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +1
-0+static void snp_handle_ext_guest_request(struct vcpu_svm *svm, gpa_t
req_gpa,gpa_tresp_gpa)arch/x86/kvm/svm/sev.c +20
-0+{ + struct sev_data_snp_guest_request req = {0}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long rc, err; + u64 data_gpa; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) {Quoted 9 lines
Quoted 6 lines
+ rc =
SEV_RET_INVALID_ADDRESS;
+ gotoe_fail;
+ }
+
+ /* Verify that requested blob will fit in certificate buffer */ + if ((data_npages<<PAGE_SHIFT) >SEV_FW_BLOB_MAX_SIZE) {Not sure this is a valid check... Isn't it OK if the guest has supplied more room than is required? If the guest supplies 8 pages and the hypervisor only needs to copy 1 page of data (or the
SEV_FW_BLOB_MAX_SIZEnumber of pages) that shouldn't be an error. I think this check can go, right?
Quoted 4 lines
Agreed.
The check should probably be
if ((data_npages<<PAGE_SHIFT) <SEV_FW_BLOB_MAX_SIZE)
No, the check should just be removed. If the number of pages required to hold the cert data is only 1, then a data_npages value of 1 is just fine (see below).
Quoted 14 lines
and that check already exists in:
snp_guest_ext_guest_request(...)
{
...
...
/*
* Check if there is enough space to copy the certificate chain.
Otherwise
* return ERROR code defined in the GHCB specification. */
expected_npages=sev->snp_certs_len>>PAGE_SHIFT; if (*npages <expected_npages) {
If expected_npages is 1, then an *npages value of 1 is OK. But if you put the check in above that you want, you would never get here with an *npages value of 1.
Thanks,
Tom
Quoted 54 lines
*npages =
expected_npages;*fw_err=SNP_GUEST_REQ_INVALID_LEN; return-EINVAL;
}
...Thanks,
AshishQuoted 45 lines
Thanks,
TomQuoted 42 lines
+ rc =
SEV_RET_INVALID_PARAM;
+ gotoe_fail;
+ }arch/x86/kvm/svm/sev.c +8
-0+ + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa); + if (rc) + goto unlock; + + rc = snp_guest_ext_guest_request(&req, (unsignedlong)sev->snp_certs_data,
+&data_npages, &err);
+ if (rc) {
+ /*
+ * If buffer length is small then return the expected + * length in rbx.
+ */
+ if (err ==SNP_GUEST_REQ_INVALID_LEN)
+ vcpu->arch.regs[VCPU_REGS_RBX] =data_npages;arch/x86/kvm/svm/sev.c +9
-0+ + /* pass the firmware error code */ + rc = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (data_npages && + kvm_write_guest(kvm, data_gpa, sev->snp_certs_data,
data_npages<<PAGE_SHIFT))
+ rc =SEV_RET_INVALID_ADDRESS;arch/x86/kvm/svm/sev.c +11
-0+ +cleanup: + snp_cleanup_guest_buf(&req, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + svm_set_ghcb_sw_exit_info_2(vcpu, rc); +} +
Kalra, Ashish
maintainerRe: [PATCH Part2 v6 42/49] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event
Quoted 62 lines · Tom Lendacky
Quoted 61 lines · Ashish
Hello Tom,
Quoted 57 lines · Tom Lendacky
Quoted 43 lines · Ashish Kalra
From: Brijesh Singh <[email protected]>
Version 2 of GHCB specification added the support for two SNP Guest Request Message NAE events. The events allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the
SNP_GUEST_REQUESTAPI define in the SEV-SNP firmware specification.The
SNP_EXT_GUEST_REQUESTis similar toSNP_GUEST_REQUESTwith the difference of an additional certificate blob that can be passed through theSNP_SET_CONFIGioctl defined in the CCP driver. The CCP driver providessnp_guest_ext_guest_request()that is used by the KVM to get both the report and certificate data at once.Signed-off-by Brijesh Singh <[email protected]>---
arch/x86/kvm/svm/sev.c| 196
+++++++++++++++++++++++++++++++++++++++--
arch/x86/kvm/svm/svm.h| 2 +2 files changed, 192 insertions(+), 6 deletions(-)arch/x86/kvm/svm/sev.c +1
-0+static void snp_handle_ext_guest_request(struct vcpu_svm *svm,
gpa_treq_gpa,gpa_tresp_gpa)arch/x86/kvm/svm/sev.c +16
-0+{ + struct sev_data_snp_guest_request req = {0}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + unsigned long data_npages; + struct kvm_sev_info *sev; + unsigned long rc, err; + u64 data_gpa; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = SEV_RET_INVALID_GUEST; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; +arch/x86/kvm/svm/sev.c static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm) · line 3634
Quoted 10 lines
arch/x86/kvm/svm/sev.c +10
-0+ data_gpa = vcpu->arch.regs[VCPU_REGS_RAX]; + data_npages = vcpu->arch.regs[VCPU_REGS_RBX]; + + if (!IS_ALIGNED(data_gpa, PAGE_SIZE)) { + rc = SEV_RET_INVALID_ADDRESS; + goto e_fail; + } + + /* Verify that requested blob will fit in certificate buffer */ + if ((data_npages << PAGE_SHIFT) > SEV_FW_BLOB_MAX_SIZE) {Not sure this is a valid check... Isn't it OK if the guest has supplied more room than is required? If the guest supplies 8 pages and the hypervisor only needs to copy 1 page of data (or the
SEV_FW_BLOB_MAX_SIZEnumber of pages) that shouldn't be an error. I think this check can go, right?
Quoted 7 lines
Quoted 4 lines
Agreed.
The check should probably be
if ((data_npages<<PAGE_SHIFT) <SEV_FW_BLOB_MAX_SIZE)No, the check should just be removed. If the number of pages required to hold the cert data is only 1, then a
data_npagesvalue of 1 is just fine (see below).
Ok.
Quoted 14 lines
Quoted 13 lines
and that check already exists in:
snp_guest_ext_guest_request(...)
{
...
...
/*
* Check if there is enough space to copy the certificate chain. Otherwise
* return ERROR code defined in the GHCB specification.
*/
expected_npages=sev->snp_certs_len>>PAGE_SHIFT;
if (*npages <expected_npages) {
Quoted 1 line
If
expected_npagesis 1, then an *npages value of 1 is OK. But if you put the check in above that you want, you would never get here with an *npages value of 1.
Yes that is correct, i incorrectly assumed thatsev->snp_certs_len == SEV_FW_BLOB_MAX_SIZE
Thanks,
Ashish
Quoted 58 lines
Thanks,
TomQuoted 54 lines
*npages =
expected_npages;*fw_err=SNP_GUEST_REQ_INVALID_LEN; return-EINVAL;
}
...Thanks,
AshishQuoted 45 lines
Thanks,
TomQuoted 42 lines
+ rc =
SEV_RET_INVALID_PARAM;
+ gotoe_fail;
+ }arch/x86/kvm/svm/sev.c +8
-0+ + mutex_lock(&sev->guest_req_lock); + + rc = snp_setup_guest_buf(svm, &req, req_gpa, resp_gpa); + if (rc) + goto unlock; + + rc = snp_guest_ext_guest_request(&req, (unsignedlong)sev->snp_certs_data,
+&data_npages, &err);
+ if (rc) {
+ /*
+ * If buffer length is small then return the expected + * length in rbx.
+ */
+ if (err ==SNP_GUEST_REQ_INVALID_LEN)
+ vcpu->arch.regs[VCPU_REGS_RBX] =data_npages;arch/x86/kvm/svm/sev.c +9
-0+ + /* pass the firmware error code */ + rc = err; + goto cleanup; + } + + /* Copy the certificate blob in the guest memory */ + if (data_npages && + kvm_write_guest(kvm, data_gpa, sev->snp_certs_data,
data_npages<<PAGE_SHIFT))
+ rc =SEV_RET_INVALID_ADDRESS;arch/x86/kvm/svm/sev.c +11
-0+ +cleanup: + snp_cleanup_guest_buf(&req, &rc); + +unlock: + mutex_unlock(&sev->guest_req_lock); + +e_fail: + svm_set_ghcb_sw_exit_info_2(vcpu, rc); +} +
Ashish Kalra
maintainerPATCH v6 43/49 KVM: SVM: Use a VMSA physical address variable for populating VMCB
In reply to Ashish KalraFrom: Tom Lendacky <[email protected]>
In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken.
--- arch/x86/kvm/svm/sev.c | 5 ++--- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +2 -3
@@ -3980,10 +3980,9 @@ void sev_es_init_vmcb(struct vcpu_svm *svm)/* * An SEV-ES guest requires a VMSA area that is a separate from the -* VMCB page. Do not include the encryption mask on the VMSA physical -* address since hardware will access it using the guest key. +* VMCB page. */ - svm->vmcb->control.vmsa_pa =__pa (svm->sev_es.vmsa); + svm->vmcb->control.vmsa_pa = svm->sev_es.vmsa_pa;/* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept (svm,INTERCEPT_CR0_READ );
arch/x86/kvm/svm/svm.c +8 -1
@@ -1296,9 +1296,16 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu) svm->vmcb01.pa =__sme_set (page_to_pfn (vmcb01_page) <<PAGE_SHIFT );svm_switch_vmcb (svm, &svm->vmcb01); -if (vmsa_page) +if (vmsa_page) { svm->sev_es.vmsa =page_address (vmsa_page); +/* +* Do not include the encryption mask on the VMSA physical +* address since hardware will access it using the guest key. +*/ + svm->sev_es.vmsa_pa =__pa (svm->sev_es.vmsa); + } + svm->guest_state_loaded = false;return 0 ;
arch/x86/kvm/svm/svm.h +1 -0
@@ -197,6 +197,7 @@ struct svm_nested_state {struct vcpu_sev_es_state {/* SEV-ES support */ struct sev_es_save_area *vmsa; +hpa_t vmsa_pa;bool ghcb_in_use;bool received_first_sipi;unsigned int ap_reset_hold_type;
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 44/49 KVM: SVM: Support SEV-SNP AP Creation NAE event
In reply to Ashish KalraFrom: Tom Lendacky <[email protected]>
Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to alter the register state of the APs on their own. This allows the guest a way of simulating INIT-SIPI.
A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running.
For CREATE
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked.
For CREATE_ON_INIT:
The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU.
For DESTROY:
The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked.
The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run.
--- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 3 +- arch/x86/include/asm/svm.h | 7 +- arch/x86/kvm/svm/sev.c | 197 +++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 5 +- arch/x86/kvm/svm/svm.h | 6 + arch/x86/kvm/x86.c | 9 +- 7 files changed, 221 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm-x86-ops.h +0 -1
@@ -130,7 +130,6 @@ KVM_X86_OP(vcpu_deliver_sipi_vector)KVM_X86_OP_OPTIONAL_RET0 (vcpu_get_apicv_inhibit_reasons); KVM_X86_OP(alloc_apic_backing_page) KVM_X86_OP_OPTIONAL(rmp_page_level_adjust) -KVM_X86_OP(update_protected_guest_state)#undef KVM_X86_OP#undef KVM_X86_OP_OPTIONAL
arch/x86/include/asm/kvm_host.h +1 -2
@@ -105,6 +105,7 @@ KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)#define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \ KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(32)#define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -1524,8 +1525,6 @@ struct kvm_x86_ops {void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu);void (*rmp_page_level_adjust)(struct kvm *kvm,kvm_pfn_t pfn,int *level); - -int (*update_protected_guest_state)(struct kvm_vcpu *vcpu); };struct kvm_x86_nested_ops {
arch/x86/include/asm/svm.h +6 -1
@@ -263,7 +263,12 @@ enum avic_ipi_failure_cause {#define AVIC_HPA_MASK ~((0xFFFULL << 52) | 0xFFF)#define VMCB_AVIC_APIC_BAR_MASK 0xFFFFFFFFFF000ULL -#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEAT_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEAT_INT_INJ_MODES \ + (SVM_SEV_FEAT_RESTRICTED_INJECTION | \ + SVM_SEV_FEAT_ALTERNATE_INJECTION)struct vmcb_seg {u16 selector;
arch/x86/kvm/svm/sev.c +197 -0
233 lines not on this page · raw message
arch/x86/kvm/svm/svm.c +3 -2
@@ -1237,6 +1237,9 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl =0 ; svm->virt_spec_ctrl =0 ; +if (init_event) +sev_snp_init_protected_guest_state (vcpu); +init_vmcb (vcpu);if (!init_event)
@@ -4749,8 +4752,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .rmp_page_level_adjust = sev_rmp_page_level_adjust, - - .update_protected_guest_state = sev_snp_update_protected_guest_state, };/*
arch/x86/kvm/svm/svm.h +6 -0
@@ -100,6 +100,8 @@ struct kvm_sev_info {spinlock_t psc_lock;void *snp_certs_data;struct mutex guest_req_lock; + +u64 sev_features;/* Features set at VMSA creation */ };struct kvm_svm {
@@ -217,6 +219,10 @@ struct vcpu_sev_es_state {u64 ghcb_sw_exit_info_2;u64 ghcb_registered_gpa; + +struct mutex snp_vmsa_mutex; +gpa_t snp_vmsa_gpa; +bool snp_ap_create; };struct vcpu_svm {
arch/x86/kvm/x86.c +8 -1
@@ -10095,6 +10095,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_UPDATE_CPU_DIRTY_LOGGING , vcpu))static_call (kvm_x86_update_cpu_dirty_logging)(vcpu); + +if (kvm_check_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu)) { +kvm_vcpu_reset (vcpu, true); +if (vcpu->arch.mp_state !=KVM_MP_STATE_RUNNABLE ) + goto out; + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win ||
@@ -12219,7 +12225,8 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)if (!list_empty_careful (&vcpu->async_pf.done))return true; -if (kvm_apic_has_events (vcpu)) +if (kvm_apic_has_events (vcpu) || +kvm_test_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , vcpu))return true;if (vcpu->arch.pv.pv_unhalted)
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 45/49 KVM: SVM: Add module parameter to enable the SEV-SNP
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Add a module parameter than can be used to enable or disable the SEV-SNP feature. Now that KVM contains the support for the SNP set the GHCB hypervisor feature flag to indicate that SNP is supported.
--- arch/x86/kvm/svm/sev.c | 7 ++++--- arch/x86/kvm/svm/svm.h | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/kvm/svm/sev.c +4 -3
@@ -57,14 +57,15 @@ module_param_named(sev, sev_enabled, bool, 0444);/* enable/disable SEV-ES support */ static bool sev_es_enabled = true;module_param_named (sev_es, sev_es_enabled, bool,0444 ); + +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled = true; +module_param_named (sev_snp, sev_snp_enabled, bool,0444 );#else #define sev_enabled false#define sev_es_enabled false#endif /* CONFIG_KVM_AMD_SEV */ -/* enable/disable SEV-SNP support */ -static bool sev_snp_enabled; -#define AP_RESET_HOLD_NONE 0#define AP_RESET_HOLD_NAE_EVENT 1#define AP_RESET_HOLD_MSR_PROTO 2
arch/x86/kvm/svm/svm.h +1 -1
@@ -688,7 +688,7 @@ unsigned long avic_vcpu_get_apicv_inhibit_reasons(struct kvm_vcpu *vcpu);#define GHCB_VERSION_MAX 2ULL#define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED 0 +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION)extern unsigned int max_sev_asid;
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 46/49 ccp: add support to decrypt the page
In reply to Ashish KalraFrom: Brijesh Singh <[email protected]>
Add support to decrypt guest encrypted memory, these API interfaces can be used for example to dump VMCBs on SNP guest exit.
--- drivers/crypto/ccp/sev-dev.c | 33 ++++++++++++++++++++++++++++++--- include/linux/psp-sev.h | 6 +++--- 2 files changed, 33 insertions(+), 6 deletions(-)
drivers/crypto/ccp/sev-dev.c +30 -3
@@ -1852,11 +1852,38 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error) }EXPORT_SYMBOL_GPL (snp_guest_page_reclaim); -int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) { -return sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , data, error); +struct sev_data_snp_dbg data = {0 }; +struct sev_device *sev; +int ret; + +if (!psp_master || !psp_master->sev_data) +return -ENODEV ; + + sev = psp_master->sev_data; + +if (!sev->snp_inited) +return -EINVAL ; + + data.gctx_paddr = sme_me_mask | (gctx_pfn <<PAGE_SHIFT ); + data.src_addr = sme_me_mask | (src_pfn <<PAGE_SHIFT ); + data.dst_addr = sme_me_mask | (dst_pfn <<PAGE_SHIFT ); + data.len =PAGE_SIZE ; + +/* The destination page must be in the firmware state. */ +if (snp_set_rmp_state (data.dst_addr,1 , true, false, false)) +return -EIO ; + + ret =sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , &data, error); + +/* Restore the page state */ +if (snp_set_rmp_state (data.dst_addr,1 , false, false, true)) + ret = -EIO ; + +return ret; } -EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt); +EXPORT_SYMBOL_GPL (snp_guest_dbg_decrypt_page);int snp_guest_ext_guest_request (struct sev_data_snp_guest_request *data,unsigned long vaddr,unsigned long *npages,unsigned long *fw_err)
include/linux/psp-sev.h +3 -3
@@ -928,7 +928,7 @@ int snp_guest_decommission(struct sev_data_snp_decommission *data, int *error);int snp_guest_page_reclaim (struct sev_data_snp_page_reclaim *data,int *error);/** -* snp_guest_dbg_decrypt - perform SEV SNP_DBG_DECRYPT command +* snp_guest_dbg_decrypt_page - perform SEV SNP_DBG_DECRYPT command * * @sev_ret: sev command return code *
@@ -939,7 +939,7 @@ int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error);* -%ETIMEDOUT if the sev command timed out * -%EIO if the sev returned a non-zero return code */ -int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error); +int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error);void *psp_copy_user_blob (u64 uaddr,u32 len);void *snp_alloc_firmware_page (gfp_t mask);
@@ -997,7 +997,7 @@ static inline int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data,return -ENODEV ; } -static inline int snp_guest_dbg_decrypt (struct sev_data_snp_dbg *data,int *error) +static inline int snp_guest_dbg_decrypt_page (u64 gctx_pfn,u64 src_pfn,u64 dst_pfn,int *error) {return -ENODEV ; }
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 47/49 *fix for stale per-cpu pointer due to cond_resched during ghcb mapping
In reply to Ashish KalraFrom: Michael Roth <[email protected]>
--- arch/x86/kvm/svm/svm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/kvm/svm/svm.c +5 -1
@@ -1352,7 +1352,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)static void svm_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct svm_cpu_data *sd =per_cpu (svm_data, vcpu->cpu); +struct svm_cpu_data *sd;if (sev_es_guest (vcpu->kvm))sev_es_unmap_ghcb (svm);
@@ -1360,6 +1360,10 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)if (svm->guest_state_loaded)return ; +/* sev_es_unmap_ghcb() can resched, so grab per-cpu pointer afterward. */ +barrier (); + sd =per_cpu (svm_data, vcpu->cpu); +/* * Save additional host state that will be restored on VMEXIT (sev-es) * or subsequent vmload of host save area.
Signature · 1 line
2.25.1
Re: [PATCH Part2 v6 47/49] *fix for stale per-cpu pointer due to cond_resched during ghcb mapping
Quoted 4 lines · Ashish Kalra
From: Michael Roth <[email protected]>
Signed-off-by Michael Roth <[email protected]>
Can you add a commit description here? Is this a fix for existing SEV-ES support or should this be incorporated into a patch in this series which adds this issue?
Quoted 31 lines
--- arch/x86/kvm/svm/svm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c +5
-2@@ -1352,7 +1352,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)static void svm_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct svm_cpu_data *sd =per_cpu (svm_data, vcpu->cpu); +struct svm_cpu_data *sd;if (sev_es_guest (vcpu->kvm))sev_es_unmap_ghcb (svm);@@ -1360,6 +1360,10 @@ static void svm_prepare_switch_to_guest(struct kvm_vcpu *vcpu)if (svm->guest_state_loaded)return ; +/* sev_es_unmap_ghcb() can resched, so grab per-cpu pointer afterward. */ +barrier (); + sd =per_cpu (svm_data, vcpu->cpu); +/* * Save additional host state that will be restored on VMEXIT (sev-es) * or subsequent vmload of host save area. -- 2.25.1
Kalra, Ashish
maintainerRE: [PATCH Part2 v6 47/49] *fix for stale per-cpu pointer due to cond_resched during ghcb mapping
[AMD Official Use Only - General]
Hello Peter,
Quoted 6 lines
Quoted 4 lines
From: Michael Roth <[email protected]>
Signed-off-by Michael Roth <[email protected]>Can you add a commit description here? Is this a fix for existing SEV-ES support or should this be incorporated into a patch in this series which adds this issue?
This actually fixes issues caused due to preemption happening in svm_prepare_switch_to_guest() when kvm_vcpu_map() is called to map in the GHCB before entering the guest.
This is a temporary fix and what we need to do is to prevent getting preempted after vcpu_enter_guest() has disabled preemption, have some ideas about using gfn_to_pfn_cache() infrastructure to re-use the already mapped GHCB at guest exit, so that we can avoid calling kvm_vcpu_map() to re-map the GHCB.
Thanks,
Ashish
Quoted 30 lines
--- arch/x86/kvm/svm/svm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/x86/kvm/svm/svm.c index renamed from arch/x86/kvm/svm/svm.c +0
-0
fced6ea423ad..f78e3b1bde0e100644arch/x86/kvm/svm/svm.c +1
-1@@ -1352,7 +1352,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu)static void svm_prepare_switch_to_guest (struct kvm_vcpu *vcpu) {struct vcpu_svm *svm =to_svm (vcpu); -struct svm_cpu_data *sd =per_cpu (svm_data, vcpu->cpu); +struct svm_cpu_data *sd;if (sev_es_guest (vcpu->kvm)) sev_es_unmap_ghcb(svm); @@ -1360,6 +1360,10 @@ staticvoid svm_prepare_switch_to_guest (struct kvm_vcpu *vcpu)if (svm->guest_state_loaded) return;arch/x86/kvm/svm/svm.c +4
-1+/* sev_es_unmap_ghcb() can resched, so grab per-cpu pointer afterward. */ +barrier (); + sd =per_cpu (svm_data, vcpu->cpu); +/* * Save additional host state that will be restored on VMEXIT (sev-es) * or subsequent vmload of host save area. -- 2.25.1
Ashish Kalra
maintainerPATCH v6 48/49 *debug: warn and retry failed rmpupdates
In reply to Ashish KalraFrom: Michael Roth <[email protected]>
In some cases on B0 hardware exhibits something like the following behavior (where M < 512):
Guest A | Guest B |-------------------------------|----------------------------------| | | rc = rmpupdate pfn=N*512,4K,priv | rmpupdate pfn=N*512+M,4K,priv |
| rc = FAIL_OVERLAP | rc = SUCCESS
The FAIL_OVERLAP might possible be the result of hardware temporarily treating Guest B's rmpupdate for pfn=N*512 as a 2M update, causing the subsequent update from Guest A for pfn=N*512+M to report FAIL_OVERLAP at that particular instance. Retrying the update for N*512+M immediately afterward seems to resolve the FAIL_OVERLAP issue reliably however.
A similar failure has also been observed when transitioning pages back to shared during VM destroy. In this case repeating the rmpupdate does not always seem to resolve the failure immediately.
Both situations are much more likely to occur if THP is disabled, or if it is enabled/disabled while guests are actively being started/stopped.
Include some debug/error information to get a better idea of the behavior on different hardware, and add the rmpupdate retry as a workaround for Milan B0 testing.
--- arch/x86/kernel/sev.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/x86/kernel/sev.c +15 -0
@@ -2530,6 +2530,7 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val) {unsigned long paddr = pfn <<PAGE_SHIFT ;int ret, level, npages; +int retries =0 ;if (!pfn_valid (pfn))return -EINVAL ;
@@ -2552,12 +2553,26 @@ static int rmpupdate(u64 pfn, struct rmpupdate *val) } } +retry:/* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ asmvolatile (".byte 0xF2, 0x0F, 0x01, 0xFE" :"=a" (ret) :"a" (paddr),"c" ((unsigned long )val) :"memory" ,"cc" ); +if (ret) { +if (!retries) { +pr_err ("rmpupdate failed, ret: %d, pfn: %llx, npages: %d, level: %d, retrying (max: %d)...\n" , + ret, pfn, npages, level,2 *num_present_cpus ()); +dump_stack (); + } + retries++; +if (retries <2 *num_present_cpus ()) + goto retry; + }else if (retries >0 ) { +pr_err ("rmpupdate for pfn %llx succeeded after %d retries\n" , pfn, retries); + } +/* * Restore the direct map after the page is removed from the RMP table. */
Signature · 1 line
2.25.1
Ashish Kalra
maintainerPATCH v6 49/49 KVM: SVM: Sync the GHCB scratch buffer using already mapped ghcb
In reply to Ashish KalraFrom: Ashish Kalra <[email protected]>
Using kvm_write_guest() to sync the GHCB scratch buffer can fail due to host mapping being 2M, but RMP being 4K. The page fault handling in do_user_addr_fault() fails to split the 2M page to handle RMP fault due to it being called here in a non-preemptible context. Instead use the already kernel mapped ghcb to sync the scratch buffer when the scratch buffer is contained within the GHCB.
--- arch/x86/kvm/svm/sev.c | 29 +++++++++++++++++++++-------- arch/x86/kvm/svm/svm.h | 2 ++ 2 files changed, 23 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/sev.c +21 -8
@@ -2944,6 +2944,24 @@ static bool sev_es_sync_to_ghcb(struct vcpu_svm *svm)ghcb_set_sw_exit_info_1 (ghcb, svm->sev_es.ghcb_sw_exit_info_1);ghcb_set_sw_exit_info_2 (ghcb, svm->sev_es.ghcb_sw_exit_info_2); +/* Sync the scratch buffer area. */ +if (svm->sev_es.ghcb_sa_sync) { +if (svm->sev_es.ghcb_sa_contained) { +memcpy (ghcb->shared_buffer + svm->sev_es.ghcb_sa_offset, + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); + }else { +int ret; + + ret =kvm_write_guest (svm->vcpu.kvm, + svm->sev_es.ghcb_sa_gpa, + svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); +if (ret) +pr_warn_ratelimited ("unmap_ghcb: kvm_write_guest failed while syncing scratch area, gpa: %llx, ret: %d\n" , + svm->sev_es.ghcb_sa_gpa, ret); + } + svm->sev_es.ghcb_sa_sync = false; + } +trace_kvm_vmgexit_exit (svm->vcpu.vcpu_id, ghcb);svm_unmap_ghcb (svm, &map);
@@ -3156,14 +3174,6 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm)if (!svm->sev_es.ghcb_in_use)return ; -/* Sync the scratch buffer area. */ -if (svm->sev_es.ghcb_sa_sync) { -kvm_write_guest (svm->vcpu.kvm, - svm->sev_es.ghcb_sa_gpa, - svm->sev_es.ghcb_sa, svm->sev_es.ghcb_sa_len); - svm->sev_es.ghcb_sa_sync = false; - } -sev_es_sync_to_ghcb (svm); svm->sev_es.ghcb_in_use = false;
@@ -3229,6 +3239,8 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) scratch_gpa_beg, scratch_gpa_end); goto e_scratch; } + svm->sev_es.ghcb_sa_contained = true; + svm->sev_es.ghcb_sa_offset = scratch_gpa_beg - ghcb_scratch_beg; }else {/* * The guest memory must be read into a kernel buffer, so
@@ -3239,6 +3251,7 @@ static int setup_vmgexit_scratch(struct vcpu_svm *svm, bool sync, u64 len) len, GHCB_SCRATCH_AREA_LIMIT); goto e_scratch; } + svm->sev_es.ghcb_sa_contained = false; }if (svm->sev_es.ghcb_sa_alloc_len < len) {
arch/x86/kvm/svm/svm.h +2 -0
@@ -210,6 +210,8 @@ struct vcpu_sev_es_state {u64 ghcb_sa_gpa;u32 ghcb_sa_alloc_len;bool ghcb_sa_sync; +bool ghcb_sa_contained; +u32 ghcb_sa_offset;/* * SEV-ES support to hold the sw_exit_info return values to be
Signature · 1 line
2.25.1