Skip to content

Commit

Permalink
KVM: SEV: Add support to handle RMP nested page faults
Browse files Browse the repository at this point in the history
When SEV-SNP is enabled in the guest, the hardware places restrictions
on all memory accesses based on the contents of the RMP table. When
hardware encounters RMP check failure caused by the guest memory access
it raises the #NPF. The error code contains additional information on
the access type. See the APM volume 2 for additional information.

When using gmem, RMP faults resulting from mismatches between the state
in the RMP table vs. what the guest expects via its page table result
in KVM_EXIT_MEMORY_FAULTs being forwarded to userspace to handle. This
means the only expected case that needs to be handled in the kernel is
when the page size of the entry in the RMP table is larger than the
mapping in the nested page table, in which case a PSMASH instruction
needs to be issued to split the large RMP entry into individual 4K
entries so that subsequent accesses can succeed.

Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
Co-developed-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Michael Roth <michael.roth@amd.com>
Signed-off-by: Ashish Kalra <ashish.kalra@amd.com>
  • Loading branch information
codomania authored and mdroth committed Apr 11, 2024
1 parent ea594ed commit cf02a57
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
3 changes: 3 additions & 0 deletions arch/x86/include/asm/sev.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
/* RMUPDATE detected 4K page and 2MB page overlap. */
#define RMPUPDATE_FAIL_OVERLAP 4

/* PSMASH failed due to concurrent access by another CPU */
#define PSMASH_FAIL_INUSE 3

/* RMP page size */
#define RMP_PG_SIZE_4K 0
#define RMP_PG_SIZE_2M 1
Expand Down
103 changes: 103 additions & 0 deletions arch/x86/kvm/svm/sev.c
Original file line number Diff line number Diff line change
Expand Up @@ -3414,6 +3414,13 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
svm->vmcb->control.ghcb_gpa = value;
}

static int snp_rmptable_psmash(kvm_pfn_t pfn)
{
pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1);

return psmash(pfn);
}

static int snp_complete_psc_msr(struct kvm_vcpu *vcpu)
{
struct vcpu_svm *svm = to_svm(vcpu);
Expand Down Expand Up @@ -3973,3 +3980,99 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu)

return p;
}

void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code)
{
struct kvm_memory_slot *slot;
struct kvm *kvm = vcpu->kvm;
int order, rmp_level, ret;
bool assigned;
kvm_pfn_t pfn;
gfn_t gfn;

gfn = gpa >> PAGE_SHIFT;

/*
* The only time RMP faults occur for shared pages is when the guest is
* triggering an RMP fault for an implicit page-state change from
* shared->private. Implicit page-state changes are forwarded to
* userspace via KVM_EXIT_MEMORY_FAULT events, however, so RMP faults
* for shared pages should not end up here.
*/
if (!kvm_mem_is_private(kvm, gfn)) {
pr_warn_ratelimited("SEV: Unexpected RMP fault for non-private GPA 0x%llx\n",
gpa);
return;
}

slot = gfn_to_memslot(kvm, gfn);
if (!kvm_slot_can_be_private(slot)) {
pr_warn_ratelimited("SEV: Unexpected RMP fault, non-private slot for GPA 0x%llx\n",
gpa);
return;
}

ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, &order);
if (ret) {
pr_warn_ratelimited("SEV: Unexpected RMP fault, no backing page for private GPA 0x%llx\n",
gpa);
return;
}

ret = snp_lookup_rmpentry(pfn, &assigned, &rmp_level);
if (ret || !assigned) {
pr_warn_ratelimited("SEV: Unexpected RMP fault, no assigned RMP entry found for GPA 0x%llx PFN 0x%llx error %d\n",
gpa, pfn, ret);
goto out;
}

/*
* There are 2 cases where a PSMASH may be needed to resolve an #NPF
* with PFERR_GUEST_RMP_BIT set:
*
* 1) RMPADJUST/PVALIDATE can trigger an #NPF with PFERR_GUEST_SIZEM
* bit set if the guest issues them with a smaller granularity than
* what is indicated by the page-size bit in the 2MB RMP entry for
* the PFN that backs the GPA.
*
* 2) Guest access via NPT can trigger an #NPF if the NPT mapping is
* smaller than what is indicated by the 2MB RMP entry for the PFN
* that backs the GPA.
*
* In both these cases, the corresponding 2M RMP entry needs to
* be PSMASH'd to 512 4K RMP entries. If the RMP entry is already
* split into 4K RMP entries, then this is likely a spurious case which
* can occur when there are concurrent accesses by the guest to a 2MB
* GPA range that is backed by a 2MB-aligned PFN who's RMP entry is in
* the process of being PMASH'd into 4K entries. These cases should
* resolve automatically on subsequent accesses, so just ignore them
* here.
*/
if (rmp_level == PG_LEVEL_4K) {
pr_debug_ratelimited("%s: Spurious RMP fault for GPA 0x%llx, error_code 0x%llx",
__func__, gpa, error_code);
goto out;
}

pr_debug_ratelimited("%s: Splitting 2M RMP entry for GPA 0x%llx, error_code 0x%llx",
__func__, gpa, error_code);
ret = snp_rmptable_psmash(pfn);
if (ret && ret != PSMASH_FAIL_INUSE) {
/*
* Look it up again. If it's 4K now then the PSMASH may have raced with
* another process and the issue has already resolved itself.
*/
if (!snp_lookup_rmpentry(pfn, &assigned, &rmp_level) && assigned &&
rmp_level == PG_LEVEL_4K) {
pr_debug_ratelimited("%s: PSMASH for GPA 0x%llx failed with ret %d due to potential race",
__func__, gpa, ret);
goto out;
}
pr_err_ratelimited("SEV: Unable to split RMP entry for GPA 0x%llx PFN 0x%llx ret %d\n",
gpa, pfn, ret);
}

kvm_zap_gfn_range(kvm, gfn, gfn + PTRS_PER_PMD);
out:
put_page(pfn_to_page(pfn));
}
21 changes: 17 additions & 4 deletions arch/x86/kvm/svm/svm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2043,15 +2043,28 @@ static int pf_interception(struct kvm_vcpu *vcpu)
static int npf_interception(struct kvm_vcpu *vcpu)
{
struct vcpu_svm *svm = to_svm(vcpu);
int rc;

u64 fault_address = svm->vmcb->control.exit_info_2;
u64 error_code = svm->vmcb->control.exit_info_1;

trace_kvm_page_fault(vcpu, fault_address, error_code);
return kvm_mmu_page_fault(vcpu, fault_address, error_code,
static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
svm->vmcb->control.insn_bytes : NULL,
svm->vmcb->control.insn_len);
rc = kvm_mmu_page_fault(vcpu, fault_address, error_code,
static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
svm->vmcb->control.insn_bytes : NULL,
svm->vmcb->control.insn_len);

/*
* rc == 0 indicates a userspace exit is needed to handle page
* transitions, so do that first before updating the RMP table.
*/
if (error_code & PFERR_GUEST_RMP_MASK) {
if (rc == 0)
return rc;
sev_handle_rmp_fault(vcpu, fault_address, error_code);
}

return rc;
}

static int db_interception(struct kvm_vcpu *vcpu)
Expand Down
3 changes: 3 additions & 0 deletions arch/x86/kvm/svm/svm.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ void sev_hardware_unsetup(void);
int sev_cpu_init(struct svm_cpu_data *sd);
int sev_dev_get_attr(u32 group, u64 attr, u64 *val);
extern unsigned int max_sev_asid;
void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code);
#else
static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) {
return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
Expand All @@ -735,6 +736,8 @@ static inline void sev_hardware_unsetup(void) {}
static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; }
static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; }
#define max_sev_asid 0
static inline void sev_handle_rmp_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) {}

#endif

/* vmenter.S */
Expand Down

0 comments on commit cf02a57

Please sign in to comment.