2022-08-08 16:08:52 +03:00
|
|
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
|
From: Maxim Levitsky <mlevitsk@redhat.com>
|
|
|
|
Date: Wed, 3 Aug 2022 18:50:06 +0300
|
|
|
|
Subject: [PATCH] KVM: x86: emulator/smm: use smram structs in the common code
|
|
|
|
|
|
|
|
Switch from using a raw array to 'union kvm_smram'.
|
|
|
|
|
|
|
|
Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
|
|
|
|
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
|
|
|
|
---
|
|
|
|
arch/x86/include/asm/kvm_host.h | 5 +++--
|
|
|
|
arch/x86/kvm/emulate.c | 12 +++++++-----
|
|
|
|
arch/x86/kvm/kvm_emulate.h | 3 ++-
|
|
|
|
arch/x86/kvm/svm/svm.c | 8 ++++++--
|
|
|
|
arch/x86/kvm/vmx/vmx.c | 4 ++--
|
|
|
|
arch/x86/kvm/x86.c | 16 ++++++++--------
|
|
|
|
6 files changed, 28 insertions(+), 20 deletions(-)
|
|
|
|
|
|
|
|
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
|
2022-11-14 22:05:01 +03:00
|
|
|
index 1172a201d851..c4e382af1853 100644
|
2022-08-08 16:08:52 +03:00
|
|
|
--- a/arch/x86/include/asm/kvm_host.h
|
|
|
|
+++ b/arch/x86/include/asm/kvm_host.h
|
|
|
|
@@ -200,6 +200,7 @@ typedef enum exit_fastpath_completion fastpath_t;
|
|
|
|
|
|
|
|
struct x86_emulate_ctxt;
|
|
|
|
struct x86_exception;
|
|
|
|
+union kvm_smram;
|
|
|
|
enum x86_intercept;
|
|
|
|
enum x86_intercept_stage;
|
|
|
|
|
2022-10-13 11:28:51 +03:00
|
|
|
@@ -1467,8 +1468,8 @@ struct kvm_x86_ops {
|
2022-08-08 16:08:52 +03:00
|
|
|
void (*setup_mce)(struct kvm_vcpu *vcpu);
|
|
|
|
|
|
|
|
int (*smi_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
|
|
|
|
- int (*enter_smm)(struct kvm_vcpu *vcpu, char *smstate);
|
|
|
|
- int (*leave_smm)(struct kvm_vcpu *vcpu, const char *smstate);
|
|
|
|
+ int (*enter_smm)(struct kvm_vcpu *vcpu, union kvm_smram *smram);
|
|
|
|
+ int (*leave_smm)(struct kvm_vcpu *vcpu, const union kvm_smram *smram);
|
|
|
|
void (*enable_smi_window)(struct kvm_vcpu *vcpu);
|
|
|
|
|
|
|
|
int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
|
|
|
|
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
|
2022-12-14 16:07:12 +03:00
|
|
|
index f169be004aab..d3cc1b8e2ea6 100644
|
2022-08-08 16:08:52 +03:00
|
|
|
--- a/arch/x86/kvm/emulate.c
|
|
|
|
+++ b/arch/x86/kvm/emulate.c
|
2022-10-13 11:28:51 +03:00
|
|
|
@@ -2566,16 +2566,18 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt,
|
2022-08-08 16:08:52 +03:00
|
|
|
static int em_rsm(struct x86_emulate_ctxt *ctxt)
|
|
|
|
{
|
|
|
|
unsigned long cr0, cr4, efer;
|
|
|
|
- char buf[512];
|
|
|
|
+ const union kvm_smram smram;
|
|
|
|
u64 smbase;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
+ BUILD_BUG_ON(sizeof(smram) != 512);
|
|
|
|
+
|
|
|
|
if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0)
|
|
|
|
return emulate_ud(ctxt);
|
|
|
|
|
|
|
|
smbase = ctxt->ops->get_smbase(ctxt);
|
|
|
|
|
|
|
|
- ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf));
|
|
|
|
+ ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, (void *)&smram, sizeof(smram));
|
|
|
|
if (ret != X86EMUL_CONTINUE)
|
|
|
|
return X86EMUL_UNHANDLEABLE;
|
|
|
|
|
2022-10-13 11:28:51 +03:00
|
|
|
@@ -2625,15 +2627,15 @@ static int em_rsm(struct x86_emulate_ctxt *ctxt)
|
2022-08-08 16:08:52 +03:00
|
|
|
* state (e.g. enter guest mode) before loading state from the SMM
|
|
|
|
* state-save area.
|
|
|
|
*/
|
|
|
|
- if (ctxt->ops->leave_smm(ctxt, buf))
|
|
|
|
+ if (ctxt->ops->leave_smm(ctxt, &smram))
|
|
|
|
goto emulate_shutdown;
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
if (emulator_has_longmode(ctxt))
|
|
|
|
- ret = rsm_load_state_64(ctxt, buf);
|
|
|
|
+ ret = rsm_load_state_64(ctxt, (const char *)&smram);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
- ret = rsm_load_state_32(ctxt, buf);
|
|
|
|
+ ret = rsm_load_state_32(ctxt, (const char *)&smram);
|
|
|
|
|
|
|
|
if (ret != X86EMUL_CONTINUE)
|
|
|
|
goto emulate_shutdown;
|
|
|
|
diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h
|
|
|
|
index 0b2bbcce321a..3b37b3e17379 100644
|
|
|
|
--- a/arch/x86/kvm/kvm_emulate.h
|
|
|
|
+++ b/arch/x86/kvm/kvm_emulate.h
|
|
|
|
@@ -19,6 +19,7 @@
|
|
|
|
struct x86_emulate_ctxt;
|
|
|
|
enum x86_intercept;
|
|
|
|
enum x86_intercept_stage;
|
|
|
|
+union kvm_smram;
|
|
|
|
|
|
|
|
struct x86_exception {
|
|
|
|
u8 vector;
|
|
|
|
@@ -233,7 +234,7 @@ struct x86_emulate_ops {
|
|
|
|
|
|
|
|
unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt);
|
|
|
|
void (*exiting_smm)(struct x86_emulate_ctxt *ctxt);
|
|
|
|
- int (*leave_smm)(struct x86_emulate_ctxt *ctxt, const char *smstate);
|
|
|
|
+ int (*leave_smm)(struct x86_emulate_ctxt *ctxt, const union kvm_smram *smram);
|
|
|
|
void (*triple_fault)(struct x86_emulate_ctxt *ctxt);
|
|
|
|
int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr);
|
|
|
|
};
|
|
|
|
diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
|
2022-12-14 16:07:12 +03:00
|
|
|
index 6deb0553ff01..703d63ea1398 100644
|
2022-08-08 16:08:52 +03:00
|
|
|
--- a/arch/x86/kvm/svm/svm.c
|
|
|
|
+++ b/arch/x86/kvm/svm/svm.c
|
2022-12-14 16:07:12 +03:00
|
|
|
@@ -4299,12 +4299,14 @@ static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
|
2022-08-08 16:08:52 +03:00
|
|
|
return !svm_smi_blocked(vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
-static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
|
|
|
|
+static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram)
|
|
|
|
{
|
|
|
|
struct vcpu_svm *svm = to_svm(vcpu);
|
|
|
|
struct kvm_host_map map_save;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
+ char *smstate = (char *)smram;
|
|
|
|
+
|
|
|
|
if (!is_guest_mode(vcpu))
|
|
|
|
return 0;
|
|
|
|
|
2022-12-14 16:07:12 +03:00
|
|
|
@@ -4346,7 +4348,7 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
|
2022-08-08 16:08:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
-static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
|
|
|
|
+static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)
|
|
|
|
{
|
|
|
|
struct vcpu_svm *svm = to_svm(vcpu);
|
|
|
|
struct kvm_host_map map, map_save;
|
2022-12-14 16:07:12 +03:00
|
|
|
@@ -4354,6 +4356,8 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
|
2022-08-08 16:08:52 +03:00
|
|
|
struct vmcb *vmcb12;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
+ const char *smstate = (const char *)smram;
|
|
|
|
+
|
|
|
|
if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
|
2023-03-14 12:44:40 +03:00
|
|
|
index f22a8c24de83..6e858effb3e4 100644
|
2022-08-08 16:08:52 +03:00
|
|
|
--- a/arch/x86/kvm/vmx/vmx.c
|
|
|
|
+++ b/arch/x86/kvm/vmx/vmx.c
|
2023-03-14 12:44:40 +03:00
|
|
|
@@ -7606,7 +7606,7 @@ static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
|
2022-08-08 16:08:52 +03:00
|
|
|
return !is_smm(vcpu);
|
|
|
|
}
|
|
|
|
|
|
|
|
-static int vmx_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
|
|
|
|
+static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram)
|
|
|
|
{
|
|
|
|
struct vcpu_vmx *vmx = to_vmx(vcpu);
|
|
|
|
|
2023-03-14 12:44:40 +03:00
|
|
|
@@ -7620,7 +7620,7 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
|
2022-08-08 16:08:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
-static int vmx_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
|
|
|
|
+static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram)
|
|
|
|
{
|
|
|
|
struct vcpu_vmx *vmx = to_vmx(vcpu);
|
|
|
|
int ret;
|
|
|
|
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
|
2022-12-14 16:07:12 +03:00
|
|
|
index 6b3c5e4df3e8..dd496c99d984 100644
|
2022-08-08 16:08:52 +03:00
|
|
|
--- a/arch/x86/kvm/x86.c
|
|
|
|
+++ b/arch/x86/kvm/x86.c
|
2022-12-14 16:07:12 +03:00
|
|
|
@@ -7421,9 +7421,9 @@ static void emulator_exiting_smm(struct x86_emulate_ctxt *ctxt)
|
2022-08-08 16:08:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int emulator_leave_smm(struct x86_emulate_ctxt *ctxt,
|
|
|
|
- const char *smstate)
|
|
|
|
+ const union kvm_smram *smram)
|
|
|
|
{
|
|
|
|
- return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smstate);
|
|
|
|
+ return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smram);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt)
|
2022-12-14 16:07:12 +03:00
|
|
|
@@ -9300,25 +9300,25 @@ static void enter_smm(struct kvm_vcpu *vcpu)
|
2022-08-08 16:08:52 +03:00
|
|
|
struct kvm_segment cs, ds;
|
|
|
|
struct desc_ptr dt;
|
|
|
|
unsigned long cr0;
|
|
|
|
- char buf[512];
|
|
|
|
+ union kvm_smram smram;
|
|
|
|
|
|
|
|
- memset(buf, 0, 512);
|
|
|
|
+ memset(smram.bytes, 0, sizeof(smram.bytes));
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
|
|
|
|
- enter_smm_save_state_64(vcpu, buf);
|
|
|
|
+ enter_smm_save_state_64(vcpu, (char *)&smram);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
- enter_smm_save_state_32(vcpu, buf);
|
|
|
|
+ enter_smm_save_state_32(vcpu, (char *)&smram);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give enter_smm() a chance to make ISA-specific changes to the vCPU
|
|
|
|
* state (e.g. leave guest mode) after we've saved the state into the
|
|
|
|
* SMM state-save area.
|
|
|
|
*/
|
|
|
|
- static_call(kvm_x86_enter_smm)(vcpu, buf);
|
|
|
|
+ static_call(kvm_x86_enter_smm)(vcpu, &smram);
|
|
|
|
|
|
|
|
kvm_smm_changed(vcpu, true);
|
|
|
|
- kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
|
|
|
|
+ kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, &smram, sizeof(smram));
|
|
|
|
|
|
|
|
if (static_call(kvm_x86_get_nmi_mask)(vcpu))
|
|
|
|
vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
|