Skip to content

Commit 4af663c

Browse files
mdrothbonzini
authored andcommitted
KVM: SEV: Allow per-guest configuration of GHCB protocol version
The GHCB protocol version may be different from one guest to the next. Add a field to track it for each KVM instance and extend KVM_SEV_INIT2 to allow it to be configured by userspace. Now that all SEV-ES support for GHCB protocol version 2 is in place, go ahead and default to it when creating SEV-ES guests through the new KVM_SEV_INIT2 interface. Keep the older KVM_SEV_ES_INIT interface restricted to GHCB protocol version 1. Suggested-by: Sean Christopherson <[email protected]> Signed-off-by: Michael Roth <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8d1a36e commit 4af663c

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

Documentation/virt/kvm/x86/amd-memory-encryption.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,19 @@ Returns: 0 on success, -negative on error
9595
struct kvm_sev_init {
9696
__u64 vmsa_features; /* initial value of features field in VMSA */
9797
__u32 flags; /* must be 0 */
98-
__u32 pad[9];
98+
__u16 ghcb_version; /* maximum guest GHCB version allowed */
99+
__u16 pad1;
100+
__u32 pad2[8];
99101
};
100102

101103
It is an error if the hypervisor does not support any of the bits that
102104
are set in ``flags`` or ``vmsa_features``. ``vmsa_features`` must be
103105
0 for SEV virtual machines, as they do not have a VMSA.
104106

107+
``ghcb_version`` must be 0 for SEV virtual machines, as they do not issue GHCB
108+
requests. If ``ghcb_version`` is 0 for any other guest type, then the maximum
109+
allowed guest GHCB protocol will default to version 2.
110+
105111
This command replaces the deprecated KVM_SEV_INIT and KVM_SEV_ES_INIT commands.
106112
The commands did not have any parameters (the ```data``` field was unused) and
107113
only work for the KVM_X86_DEFAULT_VM machine type (0).
@@ -112,7 +118,8 @@ They behave as if:
112118
KVM_SEV_ES_INIT
113119

114120
* the ``flags`` and ``vmsa_features`` fields of ``struct kvm_sev_init`` are
115-
set to zero
121+
set to zero, and ``ghcb_version`` is set to 0 for KVM_SEV_INIT and 1 for
122+
KVM_SEV_ES_INIT.
116123

117124
If the ``KVM_X86_SEV_VMSA_FEATURES`` attribute does not exist, the hypervisor only
118125
supports KVM_SEV_INIT and KVM_SEV_ES_INIT. In that case, note that KVM_SEV_ES_INIT

arch/x86/include/uapi/asm/kvm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,9 @@ struct kvm_sev_cmd {
711711
struct kvm_sev_init {
712712
__u64 vmsa_features;
713713
__u32 flags;
714-
__u32 pad[9];
714+
__u16 ghcb_version;
715+
__u16 pad1;
716+
__u32 pad2[8];
715717
};
716718

717719
struct kvm_sev_launch_start {

arch/x86/kvm/svm/sev.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
#include "cpuid.h"
3434
#include "trace.h"
3535

36-
#define GHCB_VERSION_MAX 1ULL
36+
#define GHCB_VERSION_MAX 2ULL
37+
#define GHCB_VERSION_DEFAULT 2ULL
3738
#define GHCB_VERSION_MIN 1ULL
3839

3940
#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP
@@ -268,12 +269,24 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp,
268269
if (data->vmsa_features & ~valid_vmsa_features)
269270
return -EINVAL;
270271

272+
if (data->ghcb_version > GHCB_VERSION_MAX || (!es_active && data->ghcb_version))
273+
return -EINVAL;
274+
271275
if (unlikely(sev->active))
272276
return -EINVAL;
273277

274278
sev->active = true;
275279
sev->es_active = es_active;
276280
sev->vmsa_features = data->vmsa_features;
281+
sev->ghcb_version = data->ghcb_version;
282+
283+
/*
284+
* Currently KVM supports the full range of mandatory features defined
285+
* by version 2 of the GHCB protocol, so default to that for SEV-ES
286+
* guests created via KVM_SEV_INIT2.
287+
*/
288+
if (sev->es_active && !sev->ghcb_version)
289+
sev->ghcb_version = GHCB_VERSION_DEFAULT;
277290

278291
ret = sev_asid_new(sev);
279292
if (ret)
@@ -307,13 +320,22 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
307320
{
308321
struct kvm_sev_init data = {
309322
.vmsa_features = 0,
323+
.ghcb_version = 0,
310324
};
311325
unsigned long vm_type;
312326

313327
if (kvm->arch.vm_type != KVM_X86_DEFAULT_VM)
314328
return -EINVAL;
315329

316330
vm_type = (argp->id == KVM_SEV_INIT ? KVM_X86_SEV_VM : KVM_X86_SEV_ES_VM);
331+
332+
/*
333+
* KVM_SEV_ES_INIT has been deprecated by KVM_SEV_INIT2, so it will
334+
* continue to only ever support the minimal GHCB protocol version.
335+
*/
336+
if (vm_type == KVM_X86_SEV_ES_VM)
337+
data.ghcb_version = GHCB_VERSION_MIN;
338+
317339
return __sev_guest_init(kvm, argp, &data, vm_type);
318340
}
319341

@@ -2897,6 +2919,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
28972919
{
28982920
struct vmcb_control_area *control = &svm->vmcb->control;
28992921
struct kvm_vcpu *vcpu = &svm->vcpu;
2922+
struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
29002923
u64 ghcb_info;
29012924
int ret = 1;
29022925

@@ -2907,7 +2930,7 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm)
29072930

29082931
switch (ghcb_info) {
29092932
case GHCB_MSR_SEV_INFO_REQ:
2910-
set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
2933+
set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version,
29112934
GHCB_VERSION_MIN,
29122935
sev_enc_bit));
29132936
break;
@@ -3268,11 +3291,14 @@ void sev_init_vmcb(struct vcpu_svm *svm)
32683291

32693292
void sev_es_vcpu_reset(struct vcpu_svm *svm)
32703293
{
3294+
struct kvm_vcpu *vcpu = &svm->vcpu;
3295+
struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info;
3296+
32713297
/*
32723298
* Set the GHCB MSR value as per the GHCB specification when emulating
32733299
* vCPU RESET for an SEV-ES guest.
32743300
*/
3275-
set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX,
3301+
set_ghcb_msr(svm, GHCB_MSR_SEV_INFO((__u64)sev->ghcb_version,
32763302
GHCB_VERSION_MIN,
32773303
sev_enc_bit));
32783304
}

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ struct kvm_sev_info {
8787
struct list_head regions_list; /* List of registered regions */
8888
u64 ap_jump_table; /* SEV-ES AP Jump Table address */
8989
u64 vmsa_features;
90+
u16 ghcb_version; /* Highest guest GHCB protocol version allowed */
9091
struct kvm *enc_context_owner; /* Owner of copied encryption context */
9192
struct list_head mirror_vms; /* List of VMs mirroring */
9293
struct list_head mirror_entry; /* Use as a list entry of mirrors */

0 commit comments

Comments
 (0)