Skip to content

Commit ac5c480

Browse files
committed
KVM: SEV: publish supported VMSA features
Compute the set of features to be stored in the VMSA when KVM is initialized; move it from there into kvm_sev_info when SEV is initialized, and then into the initial VMSA. The new variable can then be used to return the set of supported features to userspace, via the KVM_GET_DEVICE_ATTR ioctl. Signed-off-by: Paolo Bonzini <[email protected]> Reviewed-by: Isaku Yamahata <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 546d714 commit ac5c480

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,18 @@ issued by the hypervisor to make the guest ready for execution.
425425

426426
Returns: 0 on success, -negative on error
427427

428+
Device attribute API
429+
====================
430+
431+
Attributes of the SEV implementation can be retrieved through the
432+
``KVM_HAS_DEVICE_ATTR`` and ``KVM_GET_DEVICE_ATTR`` ioctls on the ``/dev/kvm``
433+
device node, using group ``KVM_X86_GRP_SEV``.
434+
435+
Currently only one attribute is implemented:
436+
437+
* ``KVM_X86_SEV_VMSA_FEATURES``: return the set of all bits that
438+
are accepted in the ``vmsa_features`` of ``KVM_SEV_INIT2``.
439+
428440
Firmware Management
429441
===================
430442

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,13 @@ struct kvm_sync_regs {
457457

458458
#define KVM_STATE_VMX_PREEMPTION_TIMER_DEADLINE 0x00000001
459459

460-
/* attributes for system fd (group 0) */
461-
#define KVM_X86_XCOMP_GUEST_SUPP 0
460+
/* vendor-independent attributes for system fd (group 0) */
461+
#define KVM_X86_GRP_SYSTEM 0
462+
# define KVM_X86_XCOMP_GUEST_SUPP 0
463+
464+
/* vendor-specific groups and attributes for system fd */
465+
#define KVM_X86_GRP_SEV 1
466+
# define KVM_X86_SEV_VMSA_FEATURES 0
462467

463468
struct kvm_vmx_nested_state_data {
464469
__u8 vmcs12[KVM_STATE_NESTED_VMX_VMCS_SIZE];

arch/x86/kvm/svm/sev.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444);
4646
/* enable/disable SEV-ES DebugSwap support */
4747
static bool sev_es_debug_swap_enabled = false;
4848
module_param_named(debug_swap, sev_es_debug_swap_enabled, bool, 0444);
49+
static u64 sev_supported_vmsa_features;
4950

5051
static u8 sev_enc_bit;
5152
static DECLARE_RWSEM(sev_deactivate_lock);
@@ -603,8 +604,8 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm)
603604
save->xss = svm->vcpu.arch.ia32_xss;
604605
save->dr6 = svm->vcpu.arch.dr6;
605606

606-
if (sev_es_debug_swap_enabled) {
607-
save->sev_features |= SVM_SEV_FEAT_DEBUG_SWAP;
607+
if (sev_supported_vmsa_features) {
608+
save->sev_features = sev_supported_vmsa_features;
608609
pr_warn_once("Enabling DebugSwap with KVM_SEV_ES_INIT. "
609610
"This will not work starting with Linux 6.10\n");
610611
}
@@ -1843,6 +1844,21 @@ int sev_vm_move_enc_context_from(struct kvm *kvm, unsigned int source_fd)
18431844
return ret;
18441845
}
18451846

1847+
int sev_dev_get_attr(u32 group, u64 attr, u64 *val)
1848+
{
1849+
if (group != KVM_X86_GRP_SEV)
1850+
return -ENXIO;
1851+
1852+
switch (attr) {
1853+
case KVM_X86_SEV_VMSA_FEATURES:
1854+
*val = sev_supported_vmsa_features;
1855+
return 0;
1856+
1857+
default:
1858+
return -ENXIO;
1859+
}
1860+
}
1861+
18461862
int sev_mem_enc_ioctl(struct kvm *kvm, void __user *argp)
18471863
{
18481864
struct kvm_sev_cmd sev_cmd;
@@ -2275,6 +2291,10 @@ void __init sev_hardware_setup(void)
22752291
if (!sev_es_enabled || !cpu_feature_enabled(X86_FEATURE_DEBUG_SWAP) ||
22762292
!cpu_feature_enabled(X86_FEATURE_NO_NESTED_DATA_BP))
22772293
sev_es_debug_swap_enabled = false;
2294+
2295+
sev_supported_vmsa_features = 0;
2296+
if (sev_es_debug_swap_enabled)
2297+
sev_supported_vmsa_features |= SVM_SEV_FEAT_DEBUG_SWAP;
22782298
}
22792299

22802300
void sev_hardware_unsetup(void)

arch/x86/kvm/svm/svm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5026,6 +5026,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = {
50265026
#endif
50275027

50285028
#ifdef CONFIG_KVM_AMD_SEV
5029+
.dev_get_attr = sev_dev_get_attr,
50295030
.mem_enc_ioctl = sev_mem_enc_ioctl,
50305031
.mem_enc_register_region = sev_mem_enc_register_region,
50315032
.mem_enc_unregister_region = sev_mem_enc_unregister_region,

arch/x86/kvm/svm/svm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ void __init sev_set_cpu_caps(void);
692692
void __init sev_hardware_setup(void);
693693
void sev_hardware_unsetup(void);
694694
int sev_cpu_init(struct svm_cpu_data *sd);
695+
int sev_dev_get_attr(u32 group, u64 attr, u64 *val);
695696
extern unsigned int max_sev_asid;
696697
#else
697698
static inline struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) {
@@ -704,6 +705,7 @@ static inline void __init sev_set_cpu_caps(void) {}
704705
static inline void __init sev_hardware_setup(void) {}
705706
static inline void sev_hardware_unsetup(void) {}
706707
static inline int sev_cpu_init(struct svm_cpu_data *sd) { return 0; }
708+
static inline int sev_dev_get_attr(u32 group, u64 attr, u64 *val) { return -ENXIO; }
707709
#define max_sev_asid 0
708710
#endif
709711

0 commit comments

Comments
 (0)