[PATCH Part2 RFC v3 30/37] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT

From: Brijesh Singh
Date: Wed Jun 02 2021 - 10:14:24 EST


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 MSR protocol
as defined in the GHCB specification.

Before changing the page state in the RMP entry, we lookup the page in
the TDP to make sure that there is a valid mapping for it. If the mapping
exist then try to find a workable page level between the TDP and RMP for
the page. If the page is not mapped in the TDP, then create a fault such
that it gets mapped before we change the page state in the RMP entry.

Signed-off-by: Brijesh Singh <brijesh.singh@xxxxxxx>
---
arch/x86/include/asm/sev-common.h | 3 +
arch/x86/kvm/svm/sev.c | 141 ++++++++++++++++++++++++++++++
2 files changed, 144 insertions(+)

diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h
index e7c6ce2ce45e..ed417340ed42 100644
--- a/arch/x86/include/asm/sev-common.h
+++ b/arch/x86/include/asm/sev-common.h
@@ -82,6 +82,9 @@

#define GHCB_MSR_PSC_RESP 0x015
#define GHCB_MSR_PSC_ERROR_POS 32
+#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0)
+#define GHCB_MSR_PSC_RSVD_POS 12
+#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0)
#define GHCB_MSR_PSC_RESP_VAL(val) ((val) >> GHCB_MSR_PSC_ERROR_POS)

/* GHCB GPA Register */
diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c
index 81c0fc883261..dac7042464be 100644
--- a/arch/x86/kvm/svm/sev.c
+++ b/arch/x86/kvm/svm/sev.c
@@ -28,6 +28,7 @@
#include "svm_ops.h"
#include "cpuid.h"
#include "trace.h"
+#include "mmu.h"

#define __ex(x) __kvm_handle_fault_on_reboot(x)

@@ -2821,6 +2822,127 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
svm->vmcb->control.ghcb_gpa = value;
}

+static int snp_rmptable_psmash(struct kvm_vcpu *vcpu, kvm_pfn_t pfn)
+{
+ pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);
+
+ return psmash(pfn_to_page(pfn));
+}
+
+static int snp_make_page_shared(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level)
+{
+ struct rmpupdate val;
+ int rc, rmp_level;
+ struct rmpentry *e;
+
+ e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level);
+ if (!e)
+ return -EINVAL;
+
+ if (!rmpentry_assigned(e))
+ return 0;
+
+ /* Log if the entry is validated */
+ if (rmpentry_validated(e))
+ pr_warn_ratelimited("Remove RMP entry for a validated gpa 0x%llx\n", gpa);
+
+ /*
+ * Is the page part of an existing 2M RMP entry ? Split the 2MB into multiple
+ * of 4K-page before making the memory shared.
+ */
+ if ((level == PG_LEVEL_4K) && (rmp_level == PG_LEVEL_2M)) {
+ rc = snp_rmptable_psmash(vcpu, pfn);
+ if (rc)
+ return rc;
+ }
+
+ memset(&val, 0, sizeof(val));
+ val.pagesize = X86_TO_RMP_PG_LEVEL(level);
+ return rmpupdate(pfn_to_page(pfn), &val);
+}
+
+static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level)
+{
+ struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
+ struct rmpupdate val;
+ struct rmpentry *e;
+ int rmp_level;
+
+ e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level);
+ if (!e)
+ return -EINVAL;
+
+ /* Log if the entry is validated */
+ if (rmpentry_validated(e))
+ pr_warn_ratelimited("Asked to make a pre-validated gpa %llx private\n", gpa);
+
+ memset(&val, 0, sizeof(val));
+ val.gpa = gpa;
+ val.asid = sev->asid;
+ val.pagesize = X86_TO_RMP_PG_LEVEL(level);
+ val.assigned = true;
+
+ return rmpupdate(pfn_to_page(pfn), &val);
+}
+
+static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t gpa, int level)
+{
+ struct kvm *kvm = vcpu->kvm;
+ int rc, tdp_level;
+ kvm_pfn_t pfn;
+ gpa_t gpa_end;
+
+ gpa_end = gpa + page_level_size(level);
+
+ while (gpa < gpa_end) {
+ /*
+ * Get the pfn and level for the gpa from the nested page table.
+ *
+ * If the TDP walk failed, then its safe to say that we don't have a valid
+ * mapping for the gpa in the nested page table. Create a fault to map the
+ * page is nested page table.
+ */
+ if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level)) {
+ pfn = kvm_mmu_map_tdp_page(vcpu, gpa, PFERR_USER_MASK, level);
+ if (is_error_noslot_pfn(pfn))
+ goto out;
+
+ if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level))
+ goto out;
+ }
+
+ /* Adjust the level so that we don't go higher than the backing page level */
+ level = min_t(size_t, level, tdp_level);
+
+ write_lock(&kvm->mmu_lock);
+
+ switch (op) {
+ case SNP_PAGE_STATE_SHARED:
+ rc = snp_make_page_shared(vcpu, gpa, pfn, level);
+ break;
+ case SNP_PAGE_STATE_PRIVATE:
+ rc = snp_make_page_private(vcpu, gpa, pfn, level);
+ break;
+ default:
+ rc = -EINVAL;
+ break;
+ }
+
+ write_unlock(&kvm->mmu_lock);
+
+ if (rc) {
+ pr_err_ratelimited("Error op %d gpa %llx pfn %llx level %d rc %d\n",
+ op, gpa, pfn, level, rc);
+ goto out;
+ }
+
+ gpa = gpa + page_level_size(level);
+ }
+
+out:
+ return rc;
+}
+
static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
{
struct vmcb_control_area *control = &svm->vmcb->control;
@@ -2919,6 +3041,25 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
GHCB_MSR_INFO_POS);
break;
}
+ case GHCB_MSR_PSC_REQ: {
+ gfn_t gfn;
+ int ret;
+ u8 op;
+
+ gfn = get_ghcb_msr_bits(svm, GHCB_MSR_PSC_GFN_MASK, GHCB_MSR_PSC_GFN_POS);
+ op = get_ghcb_msr_bits(svm, GHCB_MSR_PSC_OP_MASK, GHCB_MSR_PSC_OP_POS);
+
+ ret = __snp_handle_page_state_change(vcpu, op, gfn_to_gpa(gfn), PG_LEVEL_4K);
+
+ /* If failed to change the state then spec requires to return all F's */
+ if (ret)
+ ret = -1;
+
+ set_ghcb_msr_bits(svm, ret, GHCB_MSR_PSC_ERROR_MASK, GHCB_MSR_PSC_ERROR_POS);
+ set_ghcb_msr_bits(svm, 0, GHCB_MSR_PSC_RSVD_MASK, GHCB_MSR_PSC_RSVD_POS);
+ set_ghcb_msr_bits(svm, GHCB_MSR_PSC_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS);
+ break;
+ }
case GHCB_MSR_TERM_REQ: {
u64 reason_set, reason_code;

--
2.17.1