Skip to content

Commit d46b7b6

Browse files
mdrothbonzini
authored andcommitted
KVM: SEV: Add support to handle MSR based Page State Change VMGEXIT
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. When using gmem, private/shared memory is allocated through separate pools, and KVM relies on userspace issuing a KVM_SET_MEMORY_ATTRIBUTES KVM ioctl to tell the KVM MMU whether or not a particular GFN should be backed by private memory or not. Forward these page state change requests to userspace so that it can issue the expected KVM ioctls. The KVM MMU will handle updating the RMP entries when it is ready to map a private page into a guest. Use the existing KVM_HC_MAP_GPA_RANGE hypercall format to deliver these requests to userspace via KVM_EXIT_HYPERCALL. Signed-off-by: Michael Roth <michael.roth@amd.com> Co-developed-by: Brijesh Singh <brijesh.singh@amd.com> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> Signed-off-by: Ashish Kalra <ashish.kalra@amd.com> Message-ID: <20240501085210.2213060-10-michael.roth@amd.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 0c76b1d commit d46b7b6

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

arch/x86/include/asm/sev-common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,17 @@ enum psc_op {
101101
/* GHCBData[11:0] */ \
102102
GHCB_MSR_PSC_REQ)
103103

104+
#define GHCB_MSR_PSC_REQ_TO_GFN(msr) (((msr) & GENMASK_ULL(51, 12)) >> 12)
105+
#define GHCB_MSR_PSC_REQ_TO_OP(msr) (((msr) & GENMASK_ULL(55, 52)) >> 52)
106+
104107
#define GHCB_MSR_PSC_RESP 0x015
105108
#define GHCB_MSR_PSC_RESP_VAL(val) \
106109
/* GHCBData[63:32] */ \
107110
(((u64)(val) & GENMASK_ULL(63, 32)) >> 32)
108111

112+
/* Set highest bit as a generic error response */
113+
#define GHCB_MSR_PSC_RESP_ERROR (BIT_ULL(63) | GHCB_MSR_PSC_RESP)
114+
109115
/* GHCB Hypervisor Feature Request/Response */
110116
#define GHCB_MSR_HV_FT_REQ 0x080
111117
#define GHCB_MSR_HV_FT_RESP 0x081

arch/x86/kvm/svm/sev.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3461,6 +3461,48 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value)
34613461
svm->vmcb->control.ghcb_gpa = value;
34623462
}
34633463

3464+
static int snp_complete_psc_msr(struct kvm_vcpu *vcpu)
3465+
{
3466+
struct vcpu_svm *svm = to_svm(vcpu);
3467+
3468+
if (vcpu->run->hypercall.ret)
3469+
set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3470+
else
3471+
set_ghcb_msr(svm, GHCB_MSR_PSC_RESP);
3472+
3473+
return 1; /* resume guest */
3474+
}
3475+
3476+
static int snp_begin_psc_msr(struct vcpu_svm *svm, u64 ghcb_msr)
3477+
{
3478+
u64 gpa = gfn_to_gpa(GHCB_MSR_PSC_REQ_TO_GFN(ghcb_msr));
3479+
u8 op = GHCB_MSR_PSC_REQ_TO_OP(ghcb_msr);
3480+
struct kvm_vcpu *vcpu = &svm->vcpu;
3481+
3482+
if (op != SNP_PAGE_STATE_PRIVATE && op != SNP_PAGE_STATE_SHARED) {
3483+
set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3484+
return 1; /* resume guest */
3485+
}
3486+
3487+
if (!(vcpu->kvm->arch.hypercall_exit_enabled & (1 << KVM_HC_MAP_GPA_RANGE))) {
3488+
set_ghcb_msr(svm, GHCB_MSR_PSC_RESP_ERROR);
3489+
return 1; /* resume guest */
3490+
}
3491+
3492+
vcpu->run->exit_reason = KVM_EXIT_HYPERCALL;
3493+
vcpu->run->hypercall.nr = KVM_HC_MAP_GPA_RANGE;
3494+
vcpu->run->hypercall.args[0] = gpa;
3495+
vcpu->run->hypercall.args[1] = 1;
3496+
vcpu->run->hypercall.args[2] = (op == SNP_PAGE_STATE_PRIVATE)
3497+
? KVM_MAP_GPA_RANGE_ENCRYPTED
3498+
: KVM_MAP_GPA_RANGE_DECRYPTED;
3499+
vcpu->run->hypercall.args[2] |= KVM_MAP_GPA_RANGE_PAGE_SZ_4K;
3500+
3501+
vcpu->arch.complete_userspace_io = snp_complete_psc_msr;
3502+
3503+
return 0; /* forward request to userspace */
3504+
}
3505+
34643506
static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
34653507
{
34663508
struct vmcb_control_area *control = &svm->vmcb->control;
@@ -3566,6 +3608,12 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
35663608
GHCB_MSR_INFO_POS);
35673609
break;
35683610
}
3611+
case GHCB_MSR_PSC_REQ:
3612+
if (!sev_snp_guest(vcpu->kvm))
3613+
goto out_terminate;
3614+
3615+
ret = snp_begin_psc_msr(svm, control->ghcb_gpa);
3616+
break;
35693617
case GHCB_MSR_TERM_REQ: {
35703618
u64 reason_set, reason_code;
35713619

0 commit comments

Comments
 (0)