[PATCH] KVM: improve definition of KVM_GET/SET_NESTED_STATE structs

From: Paolo Bonzini
Date: Fri Jun 14 2019 - 20:18:15 EST


Liran sent me a patch with a large overhaul of the
KVM_GET/SET_NESTED_STATE structs. However, I am wary of changing the
userspace ABI in backwards-incompatible ways, so here is the bare minimum
that is needed to achieve the same functionality.

Namely, the format of VMX nested state is detailed in a struct, and is
accessible through struct kvm_vmx_nested_state. Unfortunately, to avoid
changing the size of the structs it has to be accessed as "vmx.data[0]"
rather than just "vmx.data".

Also, the values of the "format" field are defined as macros, which is
easy and not controversial.

Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/include/uapi/asm/kvm.h | 11 +++++++++++
arch/x86/kvm/vmx/nested.c | 3 +++
2 files changed, 14 insertions(+)

diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h
index 7a0e64ccd6ff..06b8727a3b58 100644
--- a/arch/x86/include/uapi/asm/kvm.h
+++ b/arch/x86/include/uapi/asm/kvm.h
@@ -383,6 +383,9 @@ struct kvm_sync_regs {
#define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2)
#define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3)

+#define KVM_STATE_NESTED_FORMAT_VMX 0
+#define KVM_STATE_NESTED_FORMAT_SVM 1
+
#define KVM_STATE_NESTED_GUEST_MODE 0x00000001
#define KVM_STATE_NESTED_RUN_PENDING 0x00000002
#define KVM_STATE_NESTED_EVMCS 0x00000004
@@ -390,6 +393,11 @@ struct kvm_sync_regs {
#define KVM_STATE_NESTED_SMM_GUEST_MODE 0x00000001
#define KVM_STATE_NESTED_SMM_VMXON 0x00000002

+struct kvm_vmx_nested_state_data {
+ __u8 vmcs12[0x1000];
+ __u8 shadow_vmcs12[0x1000];
+};
+
struct kvm_vmx_nested_state {
__u64 vmxon_pa;
__u64 vmcs_pa;
@@ -397,6 +405,9 @@ struct kvm_vmx_nested_state {
struct {
__u16 flags;
} smm;
+
+ __u8 pad[120 - 18];
+ struct kvm_vmx_nested_state_data data[0];
};

/* for KVM_CAP_NESTED_STATE */
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 0c601d079cd2..d97b87ef209b 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -5269,6 +5269,9 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu,
.vmx.vmcs_pa = -1ull,
};

+ BUILD_BUG_ON(sizeof(struct kvm_vmx_nested_state)
+ != sizeof_field(struct kvm_nested_state, pad));
+
if (!vcpu)
return kvm_state.size + 2 * VMCS12_SIZE;

--
2.21.0