Re: [PATCH Part2 v5 38/45] KVM: SVM: Add support to handle Page State Change VMGEXIT
From: Brijesh Singh
Date: Tue Sep 28 2021 - 19:21:36 EST
On 9/28/21 5:17 AM, Dr. David Alan Gilbert wrote:
> * Brijesh Singh (brijesh.singh@xxxxxxx) wrote:
>> SEV-SNP VMs can ask the hypervisor to change the page state in the RMP
>> table to be private or shared using the Page State Change NAE event
>> as defined in the GHCB specification version 2.
>>
>> Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
>> ---
>> arch/x86/include/asm/sev-common.h | 7 +++
>> arch/x86/kvm/svm/sev.c | 82 +++++++++++++++++++++++++++++--
>> 2 files changed, 84 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
>> index 4980f77aa1d5..5ee30bb2cdb8 100644
>> --- a/arch/x86/include/asm/sev-common.h
>> +++ b/arch/x86/include/asm/sev-common.h
>> @@ -126,6 +126,13 @@ enum psc_op {
>> /* SNP Page State Change NAE event */
>> #define VMGEXIT_PSC_MAX_ENTRY 253
>>
>> +/* The page state change hdr structure in not valid */
>> +#define PSC_INVALID_HDR 1
>> +/* The hdr.cur_entry or hdr.end_entry is not valid */
>> +#define PSC_INVALID_ENTRY 2
>> +/* Page state change encountered undefined error */
>> +#define PSC_UNDEF_ERR 3
>> +
>> struct psc_hdr {
>> u16 cur_entry;
>> u16 end_entry;
>> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
>> index 6d9483ec91ab..0de85ed63e9b 100644
>> --- a/arch/x86/kvm/svm/sev.c
>> +++ b/arch/x86/kvm/svm/sev.c
>> @@ -2731,6 +2731,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm, u64 *exit_code)
>> case SVM_VMGEXIT_AP_JUMP_TABLE:
>> case SVM_VMGEXIT_UNSUPPORTED_EVENT:
>> case SVM_VMGEXIT_HV_FEATURES:
>> + case SVM_VMGEXIT_PSC:
>> break;
>> default:
>> goto vmgexit_err;
>> @@ -3004,13 +3005,13 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,
>> */
>> rc = snp_check_and_build_npt(vcpu, gpa, level);
>> if (rc)
>> - return -EINVAL;
>> + return PSC_UNDEF_ERR;
>>
>> if (op == SNP_PAGE_STATE_PRIVATE) {
>> hva_t hva;
>>
>> if (snp_gpa_to_hva(kvm, gpa, &hva))
>> - return -EINVAL;
>> + return PSC_UNDEF_ERR;
>>
>> /*
>> * Verify that the hva range is registered. This enforcement is
>> @@ -3022,7 +3023,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,
>> rc = is_hva_registered(kvm, hva, page_level_size(level));
>> mutex_unlock(&kvm->lock);
>> if (!rc)
>> - return -EINVAL;
>> + return PSC_UNDEF_ERR;
>>
>> /*
>> * Mark the userspace range unmerable before adding the pages
>> @@ -3032,7 +3033,7 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,
>> rc = snp_mark_unmergable(kvm, hva, page_level_size(level));
>> mmap_write_unlock(kvm->mm);
>> if (rc)
>> - return -EINVAL;
>> + return PSC_UNDEF_ERR;
>> }
>>
>> write_lock(&kvm->mmu_lock);
>> @@ -3062,8 +3063,11 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,
>> case SNP_PAGE_STATE_PRIVATE:
>> rc = rmp_make_private(pfn, gpa, level, sev->asid, false);
>> break;
>> + case SNP_PAGE_STATE_PSMASH:
>> + case SNP_PAGE_STATE_UNSMASH:
>> + /* TODO: Add support to handle it */
>> default:
>> - rc = -EINVAL;
>> + rc = PSC_INVALID_ENTRY;
>> break;
>> }
>>
>> @@ -3081,6 +3085,65 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, enum psc_op op,
>> return 0;
>> }
>>
>> +static inline unsigned long map_to_psc_vmgexit_code(int rc)
>> +{
>> + switch (rc) {
>> + case PSC_INVALID_HDR:
>> + return ((1ul << 32) | 1);
>> + case PSC_INVALID_ENTRY:
>> + return ((1ul << 32) | 2);
>> + case RMPUPDATE_FAIL_OVERLAP:
>> + return ((3ul << 32) | 2);
>> + default: return (4ul << 32);
>> + }
> Are these the values defined in 56421 section 4.1.6 ?
> If so, that says:
> SW_EXITINFO2[63:32] == 0x00000100
> The hypervisor encountered some other error situation and was not able to complete the
> request identified by page_state_change_header.cur_entry. It is left to the guest to decide how
> to proceed in this situation.
>
> so it looks like the default should be 0x100 rather than 4?
Ah good catch, it should be 0x100. I will fix it.
> (It's a shame they're all magical constants, it would be nice if the
> standard have them names)
In early RFC's I had a macros to build the error code but based on
feedback's went with this function with open coded values.
thanks