@@ -560,6 +560,8 @@ static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
560560 if (copy_from_user (& params , u64_to_user_ptr (argp -> data ), sizeof (params )))
561561 return - EFAULT ;
562562
563+ sev -> policy = params .policy ;
564+
563565 memset (& start , 0 , sizeof (start ));
564566
565567 dh_blob = NULL ;
@@ -1592,11 +1594,11 @@ static int sev_send_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
15921594
15931595 /* allocate memory for header and transport buffer */
15941596 ret = - ENOMEM ;
1595- hdr = kzalloc (params .hdr_len , GFP_KERNEL_ACCOUNT );
1597+ hdr = kzalloc (params .hdr_len , GFP_KERNEL );
15961598 if (!hdr )
15971599 goto e_unpin ;
15981600
1599- trans_data = kzalloc (params .trans_len , GFP_KERNEL_ACCOUNT );
1601+ trans_data = kzalloc (params .trans_len , GFP_KERNEL );
16001602 if (!trans_data )
16011603 goto e_free_hdr ;
16021604
@@ -2199,6 +2201,8 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
21992201 if (params .policy & SNP_POLICY_MASK_SINGLE_SOCKET )
22002202 return - EINVAL ;
22012203
2204+ sev -> policy = params .policy ;
2205+
22022206 sev -> snp_context = snp_context_create (kvm , argp );
22032207 if (!sev -> snp_context )
22042208 return - ENOTTY ;
@@ -3994,10 +3998,8 @@ static int sev_snp_ap_creation(struct vcpu_svm *svm)
39943998 * Unless Creation is deferred until INIT, signal the vCPU to update
39953999 * its state.
39964000 */
3997- if (request != SVM_VMGEXIT_AP_CREATE_ON_INIT ) {
3998- kvm_make_request (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , target_vcpu );
3999- kvm_vcpu_kick (target_vcpu );
4000- }
4001+ if (request != SVM_VMGEXIT_AP_CREATE_ON_INIT )
4002+ kvm_make_request_and_kick (KVM_REQ_UPDATE_PROTECTED_GUEST_STATE , target_vcpu );
40014003
40024004 return 0 ;
40034005}
@@ -4455,6 +4457,7 @@ void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm)
44554457
44564458static void sev_es_init_vmcb (struct vcpu_svm * svm )
44574459{
4460+ struct kvm_sev_info * sev = to_kvm_sev_info (svm -> vcpu .kvm );
44584461 struct vmcb * vmcb = svm -> vmcb01 .ptr ;
44594462 struct kvm_vcpu * vcpu = & svm -> vcpu ;
44604463
@@ -4470,6 +4473,10 @@ static void sev_es_init_vmcb(struct vcpu_svm *svm)
44704473 if (svm -> sev_es .vmsa && !svm -> sev_es .snp_has_guest_vmsa )
44714474 svm -> vmcb -> control .vmsa_pa = __pa (svm -> sev_es .vmsa );
44724475
4476+ if (cpu_feature_enabled (X86_FEATURE_ALLOWED_SEV_FEATURES ))
4477+ svm -> vmcb -> control .allowed_sev_features = sev -> vmsa_features |
4478+ VMCB_ALLOWED_SEV_FEATURES_VALID ;
4479+
44734480 /* Can't intercept CR register access, HV can't modify CR registers */
44744481 svm_clr_intercept (svm , INTERCEPT_CR0_READ );
44754482 svm_clr_intercept (svm , INTERCEPT_CR4_READ );
@@ -4930,3 +4937,97 @@ int sev_private_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn)
49304937
49314938 return level ;
49324939}
4940+
4941+ struct vmcb_save_area * sev_decrypt_vmsa (struct kvm_vcpu * vcpu )
4942+ {
4943+ struct vcpu_svm * svm = to_svm (vcpu );
4944+ struct vmcb_save_area * vmsa ;
4945+ struct kvm_sev_info * sev ;
4946+ int error = 0 ;
4947+ int ret ;
4948+
4949+ if (!sev_es_guest (vcpu -> kvm ))
4950+ return NULL ;
4951+
4952+ /*
4953+ * If the VMSA has not yet been encrypted, return a pointer to the
4954+ * current un-encrypted VMSA.
4955+ */
4956+ if (!vcpu -> arch .guest_state_protected )
4957+ return (struct vmcb_save_area * )svm -> sev_es .vmsa ;
4958+
4959+ sev = to_kvm_sev_info (vcpu -> kvm );
4960+
4961+ /* Check if the SEV policy allows debugging */
4962+ if (sev_snp_guest (vcpu -> kvm )) {
4963+ if (!(sev -> policy & SNP_POLICY_DEBUG ))
4964+ return NULL ;
4965+ } else {
4966+ if (sev -> policy & SEV_POLICY_NODBG )
4967+ return NULL ;
4968+ }
4969+
4970+ if (sev_snp_guest (vcpu -> kvm )) {
4971+ struct sev_data_snp_dbg dbg = {0 };
4972+
4973+ vmsa = snp_alloc_firmware_page (__GFP_ZERO );
4974+ if (!vmsa )
4975+ return NULL ;
4976+
4977+ dbg .gctx_paddr = __psp_pa (sev -> snp_context );
4978+ dbg .src_addr = svm -> vmcb -> control .vmsa_pa ;
4979+ dbg .dst_addr = __psp_pa (vmsa );
4980+
4981+ ret = sev_do_cmd (SEV_CMD_SNP_DBG_DECRYPT , & dbg , & error );
4982+
4983+ /*
4984+ * Return the target page to a hypervisor page no matter what.
4985+ * If this fails, the page can't be used, so leak it and don't
4986+ * try to use it.
4987+ */
4988+ if (snp_page_reclaim (vcpu -> kvm , PHYS_PFN (__pa (vmsa ))))
4989+ return NULL ;
4990+
4991+ if (ret ) {
4992+ pr_err ("SEV: SNP_DBG_DECRYPT failed ret=%d, fw_error=%d (%#x)\n" ,
4993+ ret , error , error );
4994+ free_page ((unsigned long )vmsa );
4995+
4996+ return NULL ;
4997+ }
4998+ } else {
4999+ struct sev_data_dbg dbg = {0 };
5000+ struct page * vmsa_page ;
5001+
5002+ vmsa_page = alloc_page (GFP_KERNEL );
5003+ if (!vmsa_page )
5004+ return NULL ;
5005+
5006+ vmsa = page_address (vmsa_page );
5007+
5008+ dbg .handle = sev -> handle ;
5009+ dbg .src_addr = svm -> vmcb -> control .vmsa_pa ;
5010+ dbg .dst_addr = __psp_pa (vmsa );
5011+ dbg .len = PAGE_SIZE ;
5012+
5013+ ret = sev_do_cmd (SEV_CMD_DBG_DECRYPT , & dbg , & error );
5014+ if (ret ) {
5015+ pr_err ("SEV: SEV_CMD_DBG_DECRYPT failed ret=%d, fw_error=%d (0x%x)\n" ,
5016+ ret , error , error );
5017+ __free_page (vmsa_page );
5018+
5019+ return NULL ;
5020+ }
5021+ }
5022+
5023+ return vmsa ;
5024+ }
5025+
5026+ void sev_free_decrypted_vmsa (struct kvm_vcpu * vcpu , struct vmcb_save_area * vmsa )
5027+ {
5028+ /* If the VMSA has not yet been encrypted, nothing was allocated */
5029+ if (!vcpu -> arch .guest_state_protected || !vmsa )
5030+ return ;
5031+
5032+ free_page ((unsigned long )vmsa );
5033+ }
0 commit comments