]>
Commit | Line | Data |
---|---|---|
4fc427d9 TL |
1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
2 | From: Maxim Levitsky <mlevitsk@redhat.com> | |
3 | Date: Wed, 3 Aug 2022 18:50:06 +0300 | |
4 | Subject: [PATCH] KVM: x86: emulator/smm: use smram structs in the common code | |
826eb0ff FG |
5 | MIME-Version: 1.0 |
6 | Content-Type: text/plain; charset=UTF-8 | |
7 | Content-Transfer-Encoding: 8bit | |
4fc427d9 TL |
8 | |
9 | Switch from using a raw array to 'union kvm_smram'. | |
10 | ||
11 | Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> | |
12 | Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com> | |
826eb0ff | 13 | Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com> |
4fc427d9 TL |
14 | --- |
15 | arch/x86/include/asm/kvm_host.h | 5 +++-- | |
16 | arch/x86/kvm/emulate.c | 12 +++++++----- | |
17 | arch/x86/kvm/kvm_emulate.h | 3 ++- | |
18 | arch/x86/kvm/svm/svm.c | 8 ++++++-- | |
19 | arch/x86/kvm/vmx/vmx.c | 4 ++-- | |
20 | arch/x86/kvm/x86.c | 16 ++++++++-------- | |
21 | 6 files changed, 28 insertions(+), 20 deletions(-) | |
22 | ||
23 | diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h | |
12247ad0 | 24 | index f05ebaa26f0f..6885f3839e25 100644 |
4fc427d9 TL |
25 | --- a/arch/x86/include/asm/kvm_host.h |
26 | +++ b/arch/x86/include/asm/kvm_host.h | |
12247ad0 | 27 | @@ -204,6 +204,7 @@ typedef enum exit_fastpath_completion fastpath_t; |
4fc427d9 TL |
28 | |
29 | struct x86_emulate_ctxt; | |
30 | struct x86_exception; | |
31 | +union kvm_smram; | |
32 | enum x86_intercept; | |
33 | enum x86_intercept_stage; | |
34 | ||
12247ad0 | 35 | @@ -1613,8 +1614,8 @@ struct kvm_x86_ops { |
4fc427d9 TL |
36 | void (*setup_mce)(struct kvm_vcpu *vcpu); |
37 | ||
38 | int (*smi_allowed)(struct kvm_vcpu *vcpu, bool for_injection); | |
39 | - int (*enter_smm)(struct kvm_vcpu *vcpu, char *smstate); | |
40 | - int (*leave_smm)(struct kvm_vcpu *vcpu, const char *smstate); | |
41 | + int (*enter_smm)(struct kvm_vcpu *vcpu, union kvm_smram *smram); | |
42 | + int (*leave_smm)(struct kvm_vcpu *vcpu, const union kvm_smram *smram); | |
43 | void (*enable_smi_window)(struct kvm_vcpu *vcpu); | |
44 | ||
45 | int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp); | |
46 | diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c | |
12247ad0 | 47 | index 470dd4453b01..7294dffa794a 100644 |
4fc427d9 TL |
48 | --- a/arch/x86/kvm/emulate.c |
49 | +++ b/arch/x86/kvm/emulate.c | |
50 | @@ -2582,16 +2582,18 @@ static int rsm_load_state_64(struct x86_emulate_ctxt *ctxt, | |
51 | static int em_rsm(struct x86_emulate_ctxt *ctxt) | |
52 | { | |
53 | unsigned long cr0, cr4, efer; | |
54 | - char buf[512]; | |
55 | + const union kvm_smram smram; | |
56 | u64 smbase; | |
57 | int ret; | |
58 | ||
59 | + BUILD_BUG_ON(sizeof(smram) != 512); | |
60 | + | |
61 | if ((ctxt->ops->get_hflags(ctxt) & X86EMUL_SMM_MASK) == 0) | |
62 | return emulate_ud(ctxt); | |
63 | ||
64 | smbase = ctxt->ops->get_smbase(ctxt); | |
65 | ||
66 | - ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, buf, sizeof(buf)); | |
67 | + ret = ctxt->ops->read_phys(ctxt, smbase + 0xfe00, (void *)&smram, sizeof(smram)); | |
68 | if (ret != X86EMUL_CONTINUE) | |
69 | return X86EMUL_UNHANDLEABLE; | |
70 | ||
71 | @@ -2641,15 +2643,15 @@ static int em_rsm(struct x86_emulate_ctxt *ctxt) | |
72 | * state (e.g. enter guest mode) before loading state from the SMM | |
73 | * state-save area. | |
74 | */ | |
75 | - if (ctxt->ops->leave_smm(ctxt, buf)) | |
76 | + if (ctxt->ops->leave_smm(ctxt, &smram)) | |
77 | goto emulate_shutdown; | |
78 | ||
79 | #ifdef CONFIG_X86_64 | |
80 | if (emulator_has_longmode(ctxt)) | |
81 | - ret = rsm_load_state_64(ctxt, buf); | |
82 | + ret = rsm_load_state_64(ctxt, (const char *)&smram); | |
83 | else | |
84 | #endif | |
85 | - ret = rsm_load_state_32(ctxt, buf); | |
86 | + ret = rsm_load_state_32(ctxt, (const char *)&smram); | |
87 | ||
88 | if (ret != X86EMUL_CONTINUE) | |
89 | goto emulate_shutdown; | |
90 | diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h | |
12247ad0 | 91 | index dd0ae61e44a1..76c0b8e7890b 100644 |
4fc427d9 TL |
92 | --- a/arch/x86/kvm/kvm_emulate.h |
93 | +++ b/arch/x86/kvm/kvm_emulate.h | |
94 | @@ -19,6 +19,7 @@ | |
95 | struct x86_emulate_ctxt; | |
96 | enum x86_intercept; | |
97 | enum x86_intercept_stage; | |
98 | +union kvm_smram; | |
99 | ||
100 | struct x86_exception { | |
101 | u8 vector; | |
12247ad0 | 102 | @@ -236,7 +237,7 @@ struct x86_emulate_ops { |
4fc427d9 TL |
103 | |
104 | unsigned (*get_hflags)(struct x86_emulate_ctxt *ctxt); | |
105 | void (*exiting_smm)(struct x86_emulate_ctxt *ctxt); | |
106 | - int (*leave_smm)(struct x86_emulate_ctxt *ctxt, const char *smstate); | |
107 | + int (*leave_smm)(struct x86_emulate_ctxt *ctxt, const union kvm_smram *smram); | |
108 | void (*triple_fault)(struct x86_emulate_ctxt *ctxt); | |
109 | int (*set_xcr)(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr); | |
110 | }; | |
111 | diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c | |
fc2b61b1 | 112 | index 0434bb7b456b..feb48828124e 100644 |
4fc427d9 TL |
113 | --- a/arch/x86/kvm/svm/svm.c |
114 | +++ b/arch/x86/kvm/svm/svm.c | |
fc2b61b1 | 115 | @@ -4391,12 +4391,14 @@ static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) |
4fc427d9 TL |
116 | return 1; |
117 | } | |
118 | ||
119 | -static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate) | |
120 | +static int svm_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) | |
121 | { | |
122 | struct vcpu_svm *svm = to_svm(vcpu); | |
123 | struct kvm_host_map map_save; | |
124 | int ret; | |
125 | ||
126 | + char *smstate = (char *)smram; | |
127 | + | |
128 | if (!is_guest_mode(vcpu)) | |
129 | return 0; | |
130 | ||
fc2b61b1 | 131 | @@ -4438,7 +4440,7 @@ static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
4fc427d9 TL |
132 | return 0; |
133 | } | |
134 | ||
135 | -static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) | |
136 | +static int svm_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) | |
137 | { | |
138 | struct vcpu_svm *svm = to_svm(vcpu); | |
139 | struct kvm_host_map map, map_save; | |
fc2b61b1 | 140 | @@ -4446,6 +4448,8 @@ static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) |
4fc427d9 TL |
141 | struct vmcb *vmcb12; |
142 | int ret; | |
143 | ||
144 | + const char *smstate = (const char *)smram; | |
145 | + | |
146 | if (!guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
147 | return 0; | |
148 | ||
149 | diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c | |
fc2b61b1 | 150 | index 95ed874fbbcc..d12f341df52e 100644 |
4fc427d9 TL |
151 | --- a/arch/x86/kvm/vmx/vmx.c |
152 | +++ b/arch/x86/kvm/vmx/vmx.c | |
fc2b61b1 | 153 | @@ -7913,7 +7913,7 @@ static int vmx_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection) |
4fc427d9 TL |
154 | return !is_smm(vcpu); |
155 | } | |
156 | ||
157 | -static int vmx_enter_smm(struct kvm_vcpu *vcpu, char *smstate) | |
158 | +static int vmx_enter_smm(struct kvm_vcpu *vcpu, union kvm_smram *smram) | |
159 | { | |
160 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
161 | ||
fc2b61b1 | 162 | @@ -7934,7 +7934,7 @@ static int vmx_enter_smm(struct kvm_vcpu *vcpu, char *smstate) |
4fc427d9 TL |
163 | return 0; |
164 | } | |
165 | ||
166 | -static int vmx_leave_smm(struct kvm_vcpu *vcpu, const char *smstate) | |
167 | +static int vmx_leave_smm(struct kvm_vcpu *vcpu, const union kvm_smram *smram) | |
168 | { | |
169 | struct vcpu_vmx *vmx = to_vmx(vcpu); | |
170 | int ret; | |
171 | diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c | |
fc2b61b1 | 172 | index 8bdcba503f35..928b3c21b4cb 100644 |
4fc427d9 TL |
173 | --- a/arch/x86/kvm/x86.c |
174 | +++ b/arch/x86/kvm/x86.c | |
fc2b61b1 | 175 | @@ -8205,9 +8205,9 @@ static void emulator_exiting_smm(struct x86_emulate_ctxt *ctxt) |
4fc427d9 TL |
176 | } |
177 | ||
178 | static int emulator_leave_smm(struct x86_emulate_ctxt *ctxt, | |
179 | - const char *smstate) | |
180 | + const union kvm_smram *smram) | |
181 | { | |
182 | - return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smstate); | |
183 | + return static_call(kvm_x86_leave_smm)(emul_to_vcpu(ctxt), smram); | |
184 | } | |
185 | ||
186 | static void emulator_triple_fault(struct x86_emulate_ctxt *ctxt) | |
fc2b61b1 | 187 | @@ -10267,25 +10267,25 @@ static void enter_smm(struct kvm_vcpu *vcpu) |
4fc427d9 TL |
188 | struct kvm_segment cs, ds; |
189 | struct desc_ptr dt; | |
190 | unsigned long cr0; | |
191 | - char buf[512]; | |
192 | + union kvm_smram smram; | |
193 | ||
194 | - memset(buf, 0, 512); | |
195 | + memset(smram.bytes, 0, sizeof(smram.bytes)); | |
196 | #ifdef CONFIG_X86_64 | |
197 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
198 | - enter_smm_save_state_64(vcpu, buf); | |
199 | + enter_smm_save_state_64(vcpu, (char *)&smram); | |
200 | else | |
201 | #endif | |
202 | - enter_smm_save_state_32(vcpu, buf); | |
203 | + enter_smm_save_state_32(vcpu, (char *)&smram); | |
204 | ||
205 | /* | |
206 | * Give enter_smm() a chance to make ISA-specific changes to the vCPU | |
207 | * state (e.g. leave guest mode) after we've saved the state into the | |
208 | * SMM state-save area. | |
209 | */ | |
210 | - static_call(kvm_x86_enter_smm)(vcpu, buf); | |
211 | + static_call(kvm_x86_enter_smm)(vcpu, &smram); | |
212 | ||
213 | kvm_smm_changed(vcpu, true); | |
214 | - kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); | |
215 | + kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, &smram, sizeof(smram)); | |
216 | ||
217 | if (static_call(kvm_x86_get_nmi_mask)(vcpu)) | |
218 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |