blob: 4fc1f16505db18a3d7dafb069cb808591c0f5106 [file] [log] [blame]
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001#define pr_fmt(fmt) "SVM: " fmt
2
Avi Kivityedf88412007-12-16 11:02:48 +02003#include <linux/kvm_host.h>
4
Eddie Dong85f455f2007-07-06 12:20:49 +03005#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +08006#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02008#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +01009#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020010#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040011
Avi Kivity6aa8b732006-12-10 02:21:36 -080012#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070013#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020014#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080015#include <linux/vmalloc.h>
16#include <linux/highmem.h>
Joerg Roedelef0f6492020-03-31 12:17:38 -040017#include <linux/amd-iommu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040019#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050021#include <linux/hashtable.h>
Julien Thierry00089c02020-09-04 16:30:25 +010022#include <linux/objtool.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060023#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060024#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060025#include <linux/pagemap.h>
26#include <linux/swap.h>
Tom Lendacky33af3a72019-10-03 21:17:48 +000027#include <linux/rwsem.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050029#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010030#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020031#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010033#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050035#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020036#include <asm/spec-ctrl.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010037#include <asm/cpu_device_id.h>
Tom Lendackyf1c63662020-12-14 10:29:50 -050038#include <asm/traps.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039
Eduardo Habkost63d11422008-11-17 19:03:20 -020040#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020042
Joerg Roedel883b0a92020-03-24 10:41:52 +010043#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080044#include "svm_ops.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010045
Avi Kivity4ecac3f2008-05-13 13:23:38 +030046#define __ex(x) __kvm_handle_fault_on_reboot(x)
47
Avi Kivity6aa8b732006-12-10 02:21:36 -080048MODULE_AUTHOR("Qumranet");
49MODULE_LICENSE("GPL");
50
Valdis Klētnieks575b2552020-02-27 21:49:52 -050051#ifdef MODULE
Josh Triplettae759542012-03-28 11:32:28 -070052static const struct x86_cpu_id svm_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010053 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
Josh Triplettae759542012-03-28 11:32:28 -070054 {}
55};
56MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050057#endif
Josh Triplettae759542012-03-28 11:32:28 -070058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059#define IOPM_ALLOC_ORDER 2
60#define MSRPM_ALLOC_ORDER 1
61
Avi Kivity6aa8b732006-12-10 02:21:36 -080062#define SEG_TYPE_LDT 2
63#define SEG_TYPE_BUSY_TSS16 3
64
Andre Przywara6bc31bd2010-04-11 23:07:28 +020065#define SVM_FEATURE_LBRV (1 << 1)
66#define SVM_FEATURE_SVML (1 << 2)
Andre Przywaraddce97a2010-12-21 11:12:03 +010067#define SVM_FEATURE_TSC_RATE (1 << 4)
68#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
69#define SVM_FEATURE_FLUSH_ASID (1 << 6)
70#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020071#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030072
Joerg Roedel24e09cb2008-02-13 18:58:47 +010073#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
74
Joerg Roedelfbc0db72011-03-25 09:44:46 +010075#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010076#define TSC_RATIO_MIN 0x0000000000000001ULL
77#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010078
Joerg Roedel67ec6602010-05-17 14:43:35 +020079static bool erratum_383_found __read_mostly;
80
Joerg Roedel883b0a92020-03-24 10:41:52 +010081u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
Joerg Roedel323c3d82010-03-01 15:34:37 +010082
Boris Ostrovsky2b036c62012-01-09 14:00:35 -050083/*
84 * Set osvw_len to higher value when updated Revision Guides
85 * are published and we know what the new status bits are
86 */
87static uint64_t osvw_len = 4, osvw_status;
88
Joerg Roedelfbc0db72011-03-25 09:44:46 +010089static DEFINE_PER_CPU(u64, current_tsc_ratio);
90#define TSC_RATIO_DEFAULT 0x0100000000ULL
91
Mathias Krause09941fb2012-08-30 01:30:20 +020092static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +010093 u32 index; /* Index of the MSR */
Tom Lendacky376c6d22020-12-10 11:10:06 -060094 bool always; /* True if intercept is initially cleared */
Alexander Graffd6fa732020-09-25 16:34:19 +020095} direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
Brian Gerst8c065852010-07-17 09:03:26 -040096 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010097 { .index = MSR_IA32_SYSENTER_CS, .always = true },
Maxim Levitskyadc2a232021-04-01 14:19:28 +030098 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
99 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100100#ifdef CONFIG_X86_64
101 { .index = MSR_GS_BASE, .always = true },
102 { .index = MSR_FS_BASE, .always = true },
103 { .index = MSR_KERNEL_GS_BASE, .always = true },
104 { .index = MSR_LSTAR, .always = true },
105 { .index = MSR_CSTAR, .always = true },
106 { .index = MSR_SYSCALL_MASK, .always = true },
107#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100108 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100109 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100110 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
111 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
112 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
113 { .index = MSR_IA32_LASTINTTOIP, .always = false },
Tom Lendacky376c6d22020-12-10 11:10:06 -0600114 { .index = MSR_EFER, .always = false },
115 { .index = MSR_IA32_CR_PAT, .always = false },
116 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100117 { .index = MSR_INVALID, .always = false },
Avi Kivity6c8166a2009-05-31 18:15:37 +0300118};
119
Babu Moger8566ac82018-03-16 16:37:26 -0400120/*
121 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
122 * pause_filter_count: On processors that support Pause filtering(indicated
123 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
124 * count value. On VMRUN this value is loaded into an internal counter.
125 * Each time a pause instruction is executed, this counter is decremented
126 * until it reaches zero at which time a #VMEXIT is generated if pause
127 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
128 * Intercept Filtering for more details.
129 * This also indicate if ple logic enabled.
130 *
131 * pause_filter_thresh: In addition, some processor families support advanced
132 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
133 * the amount of time a guest is allowed to execute in a pause loop.
134 * In this mode, a 16-bit pause filter threshold field is added in the
135 * VMCB. The threshold value is a cycle count that is used to reset the
136 * pause counter. As with simple pause filtering, VMRUN loads the pause
137 * count value from VMCB into an internal counter. Then, on each pause
138 * instruction the hardware checks the elapsed number of cycles since
139 * the most recent pause instruction against the pause filter threshold.
140 * If the elapsed cycle count is greater than the pause filter threshold,
141 * then the internal pause count is reloaded from the VMCB and execution
142 * continues. If the elapsed cycle count is less than the pause filter
143 * threshold, then the internal pause count is decremented. If the count
144 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
145 * triggered. If advanced pause filtering is supported and pause filter
146 * threshold field is set to zero, the filter will operate in the simpler,
147 * count only mode.
148 */
149
150static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
151module_param(pause_filter_thresh, ushort, 0444);
152
153static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
154module_param(pause_filter_count, ushort, 0444);
155
156/* Default doubles per-vcpu window every exit. */
157static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
158module_param(pause_filter_count_grow, ushort, 0444);
159
160/* Default resets per-vcpu window every exit to pause_filter_count. */
161static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
162module_param(pause_filter_count_shrink, ushort, 0444);
163
164/* Default is to compute the maximum so we can never overflow. */
165static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
166module_param(pause_filter_count_max, ushort, 0444);
167
Sean Christopherson99840a72021-03-04 18:16:37 -0800168/*
169 * Use nested page tables by default. Note, NPT may get forced off by
170 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
171 */
172bool npt_enabled = true;
173module_param_named(npt, npt_enabled, bool, 0444);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100174
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100175/* allow nested virtualization in KVM/SVM */
176static int nested = true;
Alexander Graf236de052008-11-25 20:17:10 +0100177module_param(nested, int, S_IRUGO);
178
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200179/* enable/disable Next RIP Save */
180static int nrips = true;
181module_param(nrips, int, 0444);
182
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500183/* enable/disable Virtual VMLOAD VMSAVE */
184static int vls = true;
185module_param(vls, int, 0444);
186
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500187/* enable/disable Virtual GIF */
188static int vgif = true;
189module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500190
Brijesh Singhe9df0942017-12-04 10:57:33 -0600191/* enable/disable SEV support */
Tom Lendacky916391a2020-12-10 11:09:38 -0600192int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
Brijesh Singhe9df0942017-12-04 10:57:33 -0600193module_param(sev, int, 0444);
194
Tom Lendacky916391a2020-12-10 11:09:38 -0600195/* enable/disable SEV-ES support */
196int sev_es = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
197module_param(sev_es, int, 0444);
198
Tom Lendacky291bd202020-12-10 11:09:47 -0600199bool __read_mostly dump_invalid_vmcb;
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200200module_param(dump_invalid_vmcb, bool, 0644);
201
Wei Yongjun2e215212021-02-10 07:59:58 +0000202static bool svm_gp_erratum_intercept = true;
Bandan Das82a11e9c2021-01-26 03:18:29 -0500203
Brijesh Singh7607b712018-02-19 10:14:44 -0600204static u8 rsm_ins_bytes[] = "\x0f\xaa";
205
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800206static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207
208struct kvm_ldttss_desc {
209 u16 limit0;
210 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100211 unsigned base1:8, type:5, dpl:2, p:1;
212 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800213 u32 base3;
214 u32 zero1;
215} __attribute__((packed));
216
Joerg Roedeleaf78262020-03-24 10:41:54 +0100217DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
Mathias Krause09941fb2012-08-30 01:30:20 +0200219static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200221#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222#define MSRS_RANGE_SIZE 2048
223#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
224
Joerg Roedel883b0a92020-03-24 10:41:52 +0100225u32 svm_msrpm_offset(u32 msr)
Joerg Roedel455716f2010-03-01 15:34:35 +0100226{
227 u32 offset;
228 int i;
229
230 for (i = 0; i < NUM_MSR_MAPS; i++) {
231 if (msr < msrpm_ranges[i] ||
232 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
233 continue;
234
235 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
236 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
237
238 /* Now we have the u8 offset - but need the u32 offset */
239 return offset / 4;
240 }
241
242 /* MSR not in any range */
243 return MSR_INVALID;
244}
245
Avi Kivity6aa8b732006-12-10 02:21:36 -0800246#define MAX_INST_SIZE 15
247
Sean Christophersond468d942020-07-15 20:41:20 -0700248static int get_max_npt_level(void)
Joerg Roedel4b161842010-09-10 17:31:03 +0200249{
250#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800251 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200252#else
253 return PT32E_ROOT_LEVEL;
254#endif
255}
256
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300257int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258{
Paolo Bonzinic513f482020-05-18 13:08:37 -0400259 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300260 u64 old_efer = vcpu->arch.efer;
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000261 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100262
263 if (!npt_enabled) {
264 /* Shadow paging assumes NX to be available. */
265 efer |= EFER_NX;
266
267 if (!(efer & EFER_LMA))
268 efer &= ~EFER_LME;
269 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300271 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
272 if (!(efer & EFER_SVME)) {
273 svm_leave_nested(svm);
274 svm_set_gif(svm, true);
Bandan Das82a11e9c2021-01-26 03:18:29 -0500275 /* #GP intercept is still needed for vmware backdoor */
276 if (!enable_vmware_backdoor)
277 clr_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300278
279 /*
280 * Free the nested guest state, unless we are in SMM.
281 * In this case we will return to the nested guest
282 * as soon as we leave SMM.
283 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500284 if (!is_smm(vcpu))
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300285 svm_free_nested(svm);
286
287 } else {
288 int ret = svm_allocate_nested(svm);
289
290 if (ret) {
291 vcpu->arch.efer = old_efer;
292 return ret;
293 }
Bandan Das82a11e9c2021-01-26 03:18:29 -0500294
295 if (svm_gp_erratum_intercept)
296 set_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300297 }
Paolo Bonzinic513f482020-05-18 13:08:37 -0400298 }
299
300 svm->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedel06e78522020-06-25 10:03:23 +0200301 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300302 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303}
304
Avi Kivity6aa8b732006-12-10 02:21:36 -0800305static int is_external_interrupt(u32 info)
306{
307 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
308 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
309}
310
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200311static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400312{
313 struct vcpu_svm *svm = to_svm(vcpu);
314 u32 ret = 0;
315
316 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200317 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
318 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400319}
320
321static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
322{
323 struct vcpu_svm *svm = to_svm(vcpu);
324
325 if (mask == 0)
326 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
327 else
328 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
329
330}
331
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200332static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400334 struct vcpu_svm *svm = to_svm(vcpu);
335
Tom Lendackyf1c63662020-12-14 10:29:50 -0500336 /*
337 * SEV-ES does not expose the next RIP. The RIP update is controlled by
338 * the type of exit and the #VC handler in the guest.
339 */
340 if (sev_es_guest(vcpu->kvm))
341 goto done;
342
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200343 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200344 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200345 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400346 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200347
Sean Christopherson1957aa62019-08-27 14:40:39 -0700348 if (!svm->next_rip) {
349 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
350 return 0;
351 } else {
Sean Christopherson1957aa62019-08-27 14:40:39 -0700352 kvm_rip_write(vcpu, svm->next_rip);
353 }
Tom Lendackyf1c63662020-12-14 10:29:50 -0500354
355done:
Glauber Costa2809f5d2009-05-12 16:21:05 -0400356 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200357
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700358 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800359}
360
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700361static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100362{
363 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700364 unsigned nr = vcpu->arch.exception.nr;
365 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700366 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100367
Paolo Bonzini63129752021-03-02 14:40:39 -0500368 kvm_deliver_exception_payload(vcpu);
Jim Mattsonda998b42018-10-16 14:29:22 -0700369
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200370 if (nr == BP_VECTOR && !nrips) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500371 unsigned long rip, old_rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100372
373 /*
374 * For guest debugging where we have to reinject #BP if some
375 * INT3 is guest-owned:
376 * Emulate nRIP by moving RIP forward. Will fail if injection
377 * raises a fault that is not intercepted. Still better than
378 * failing in all cases.
379 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500380 (void)skip_emulated_instruction(vcpu);
381 rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100382 svm->int3_rip = rip + svm->vmcb->save.cs.base;
383 svm->int3_injected = rip - old_rip;
384 }
385
Jan Kiszka116a4752010-02-23 17:47:54 +0100386 svm->vmcb->control.event_inj = nr
387 | SVM_EVTINJ_VALID
388 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
389 | SVM_EVTINJ_TYPE_EXEPT;
390 svm->vmcb->control.event_inj_err = error_code;
391}
392
Joerg Roedel67ec6602010-05-17 14:43:35 +0200393static void svm_init_erratum_383(void)
394{
395 u32 low, high;
396 int err;
397 u64 val;
398
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100399 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200400 return;
401
402 /* Use _safe variants to not break nested virtualization */
403 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
404 if (err)
405 return;
406
407 val |= (1ULL << 47);
408
409 low = lower_32_bits(val);
410 high = upper_32_bits(val);
411
412 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
413
414 erratum_383_found = true;
415}
416
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500417static void svm_init_osvw(struct kvm_vcpu *vcpu)
418{
419 /*
420 * Guests should see errata 400 and 415 as fixed (assuming that
421 * HLT and IO instructions are intercepted).
422 */
423 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
424 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
425
426 /*
427 * By increasing VCPU's osvw.length to 3 we are telling the guest that
428 * all osvw.status bits inside that length, including bit 0 (which is
429 * reserved for erratum 298), are valid. However, if host processor's
430 * osvw_len is 0 then osvw_status[0] carries no information. We need to
431 * be conservative here and therefore we tell the guest that erratum 298
432 * is present (because we really don't know).
433 */
434 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
435 vcpu->arch.osvw.status |= 1;
436}
437
Avi Kivity6aa8b732006-12-10 02:21:36 -0800438static int has_svm(void)
439{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200440 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Eduardo Habkost63d11422008-11-17 19:03:20 -0200442 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800443 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 return 0;
445 }
446
Sean Christophersonccd85d92021-02-02 13:20:17 -0800447 if (sev_active()) {
448 pr_info("KVM is unsupported when running as an SEV guest\n");
449 return 0;
450 }
451
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452 return 1;
453}
454
Radim Krčmář13a34e02014-08-28 15:13:03 +0200455static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100457 /* Make sure we clean up behind us */
458 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
459 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
460
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200461 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100462
463 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464}
465
Radim Krčmář13a34e02014-08-28 15:13:03 +0200466static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467{
468
Tejun Heo0fe1e002009-10-29 22:34:14 +0900469 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471 struct desc_struct *gdt;
472 int me = raw_smp_processor_id();
473
Alexander Graf10474ae2009-09-15 11:37:46 +0200474 rdmsrl(MSR_EFER, efer);
475 if (efer & EFER_SVME)
476 return -EBUSY;
477
Avi Kivity6aa8b732006-12-10 02:21:36 -0800478 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200479 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200480 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800481 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900482 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900483 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200484 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200485 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486 }
487
Tejun Heo0fe1e002009-10-29 22:34:14 +0900488 sd->asid_generation = 1;
489 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
490 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600491 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492
Thomas Garnier45fc8752017-03-14 10:05:08 -0700493 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900494 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495
Alexander Graf9962d032008-11-25 20:17:02 +0100496 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600498 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
Alexander Graf10474ae2009-09-15 11:37:46 +0200499
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100500 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
501 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500502 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100503 }
504
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500505
506 /*
507 * Get OSVW bits.
508 *
509 * Note that it is possible to have a system with mixed processor
510 * revisions and therefore different OSVW bits. If bits are not the same
511 * on different processors then choose the worst case (i.e. if erratum
512 * is present on one processor and not on another then assume that the
513 * erratum is present everywhere).
514 */
515 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
516 uint64_t len, status = 0;
517 int err;
518
519 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
520 if (!err)
521 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
522 &err);
523
524 if (err)
525 osvw_status = osvw_len = 0;
526 else {
527 if (len < osvw_len)
528 osvw_len = len;
529 osvw_status |= status;
530 osvw_status &= (1ULL << osvw_len) - 1;
531 }
532 } else
533 osvw_status = osvw_len = 0;
534
Joerg Roedel67ec6602010-05-17 14:43:35 +0200535 svm_init_erratum_383();
536
Joerg Roedel1018faa2012-02-29 14:57:32 +0100537 amd_pmu_enable_virt();
538
Alexander Graf10474ae2009-09-15 11:37:46 +0200539 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800540}
541
Joerg Roedel0da1db752008-07-02 16:02:11 +0200542static void svm_cpu_uninit(int cpu)
543{
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800544 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200545
Tejun Heo0fe1e002009-10-29 22:34:14 +0900546 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200547 return;
548
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800549 per_cpu(svm_data, cpu) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600550 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900551 __free_page(sd->save_area);
552 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200553}
554
Avi Kivity6aa8b732006-12-10 02:21:36 -0800555static int svm_cpu_init(int cpu)
556{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900557 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Tejun Heo0fe1e002009-10-29 22:34:14 +0900559 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
560 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800561 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900562 sd->cpu = cpu;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600563 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900564 if (!sd->save_area)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800565 goto free_cpu_data;
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600566 clear_page(page_address(sd->save_area));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600568 if (svm_sev_enabled()) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700569 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
570 sizeof(void *),
571 GFP_KERNEL);
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600572 if (!sd->sev_vmcbs)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800573 goto free_save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600574 }
575
Tejun Heo0fe1e002009-10-29 22:34:14 +0900576 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577
578 return 0;
579
Miaohe Lind80b64f2020-01-04 16:56:49 +0800580free_save_area:
581 __free_page(sd->save_area);
582free_cpu_data:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900583 kfree(sd);
Miaohe Lind80b64f2020-01-04 16:56:49 +0800584 return -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585
586}
587
Alexander Graffd6fa732020-09-25 16:34:19 +0200588static int direct_access_msr_slot(u32 msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589{
Alexander Graffd6fa732020-09-25 16:34:19 +0200590 u32 i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100592 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
Alexander Graffd6fa732020-09-25 16:34:19 +0200593 if (direct_access_msrs[i].index == msr)
594 return i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800595
Alexander Graffd6fa732020-09-25 16:34:19 +0200596 return -ENOENT;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100597}
598
Alexander Graffd6fa732020-09-25 16:34:19 +0200599static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
600 int write)
601{
602 struct vcpu_svm *svm = to_svm(vcpu);
603 int slot = direct_access_msr_slot(msr);
604
605 if (slot == -ENOENT)
606 return;
607
608 /* Set the shadow bitmaps to the desired intercept states */
609 if (read)
610 set_bit(slot, svm->shadow_msr_intercept.read);
611 else
612 clear_bit(slot, svm->shadow_msr_intercept.read);
613
614 if (write)
615 set_bit(slot, svm->shadow_msr_intercept.write);
616 else
617 clear_bit(slot, svm->shadow_msr_intercept.write);
618}
619
620static bool valid_msr_intercept(u32 index)
621{
622 return direct_access_msr_slot(index) != -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800623}
624
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200625static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100626{
627 u8 bit_write;
628 unsigned long tmp;
629 u32 offset;
630 u32 *msrpm;
631
632 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
633 to_svm(vcpu)->msrpm;
634
635 offset = svm_msrpm_offset(msr);
636 bit_write = 2 * (msr & 0x0f) + 1;
637 tmp = msrpm[offset];
638
639 BUG_ON(offset == MSR_INVALID);
640
641 return !!test_bit(bit_write, &tmp);
642}
643
Alexander Graffd6fa732020-09-25 16:34:19 +0200644static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
645 u32 msr, int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646{
Joerg Roedel455716f2010-03-01 15:34:35 +0100647 u8 bit_read, bit_write;
648 unsigned long tmp;
649 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100651 /*
652 * If this warning triggers extend the direct_access_msrs list at the
653 * beginning of the file
654 */
655 WARN_ON(!valid_msr_intercept(msr));
656
Alexander Graffd6fa732020-09-25 16:34:19 +0200657 /* Enforce non allowed MSRs to trap */
658 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
659 read = 0;
660
661 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
662 write = 0;
663
Joerg Roedel455716f2010-03-01 15:34:35 +0100664 offset = svm_msrpm_offset(msr);
665 bit_read = 2 * (msr & 0x0f);
666 bit_write = 2 * (msr & 0x0f) + 1;
667 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668
Joerg Roedel455716f2010-03-01 15:34:35 +0100669 BUG_ON(offset == MSR_INVALID);
670
671 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
672 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
673
674 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800675}
676
Tom Lendacky376c6d22020-12-10 11:10:06 -0600677void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
678 int read, int write)
Alexander Graffd6fa732020-09-25 16:34:19 +0200679{
680 set_shadow_msr_intercept(vcpu, msr, read, write);
681 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
682}
683
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300684u32 *svm_vcpu_alloc_msrpm(void)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100685{
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300686 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200687 u32 *msrpm;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100688
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300689 if (!pages)
690 return NULL;
691
692 msrpm = page_address(pages);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100693 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
694
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200695 return msrpm;
696}
697
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300698void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699{
Joerg Roedelf65c2292008-02-13 18:58:46 +0100700 int i;
701
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100702 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
703 if (!direct_access_msrs[i].always)
704 continue;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200705 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100706 }
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300707}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300709
710void svm_vcpu_free_msrpm(u32 *msrpm)
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300711{
712 __free_pages(virt_to_page(msrpm), MSRPM_ALLOC_ORDER);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800713}
714
Alexander Graffd6fa732020-09-25 16:34:19 +0200715static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
716{
717 struct vcpu_svm *svm = to_svm(vcpu);
718 u32 i;
719
720 /*
721 * Set intercept permissions for all direct access MSRs again. They
722 * will automatically get filtered through the MSR filter, so we are
723 * back in sync after this.
724 */
725 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
726 u32 msr = direct_access_msrs[i].index;
727 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
728 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
729
730 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
Anthony Liguoric8681332007-04-30 09:48:11 +0300731 }
732}
733
Joerg Roedel323c3d82010-03-01 15:34:37 +0100734static void add_msr_offset(u32 offset)
735{
736 int i;
737
738 for (i = 0; i < MSRPM_OFFSETS; ++i) {
739
740 /* Offset already in list? */
741 if (msrpm_offsets[i] == offset)
742 return;
743
744 /* Slot used by another offset? */
745 if (msrpm_offsets[i] != MSR_INVALID)
746 continue;
747
748 /* Add offset to list */
749 msrpm_offsets[i] = offset;
750
751 return;
752 }
753
754 /*
755 * If this BUG triggers the msrpm_offsets table has an overflow. Just
756 * increase MSRPM_OFFSETS in this case.
757 */
758 BUG();
759}
760
761static void init_msrpm_offsets(void)
762{
763 int i;
764
765 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
766
767 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
768 u32 offset;
769
770 offset = svm_msrpm_offset(direct_access_msrs[i].index);
771 BUG_ON(offset == MSR_INVALID);
772
773 add_msr_offset(offset);
774 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775}
776
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200777static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800778{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200779 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500781 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200782 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
783 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
784 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
785 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786}
787
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200788static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800789{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200790 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800791
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500792 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200793 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
794 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
795 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
796 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800797}
798
Joerg Roedel883b0a92020-03-24 10:41:52 +0100799void disable_nmi_singlestep(struct vcpu_svm *svm)
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200800{
801 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500802
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200803 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
804 /* Clear our flags if they were not set by the guest */
805 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
806 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
807 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
808 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
809 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200810}
811
Babu Moger8566ac82018-03-16 16:37:26 -0400812static void grow_ple_window(struct kvm_vcpu *vcpu)
813{
814 struct vcpu_svm *svm = to_svm(vcpu);
815 struct vmcb_control_area *control = &svm->vmcb->control;
816 int old = control->pause_filter_count;
817
818 control->pause_filter_count = __grow_ple_window(old,
819 pause_filter_count,
820 pause_filter_count_grow,
821 pause_filter_count_max);
822
Peter Xu4f75bcc2019-09-06 10:17:22 +0800823 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200824 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800825 trace_kvm_ple_window_update(vcpu->vcpu_id,
826 control->pause_filter_count, old);
827 }
Babu Moger8566ac82018-03-16 16:37:26 -0400828}
829
830static void shrink_ple_window(struct kvm_vcpu *vcpu)
831{
832 struct vcpu_svm *svm = to_svm(vcpu);
833 struct vmcb_control_area *control = &svm->vmcb->control;
834 int old = control->pause_filter_count;
835
836 control->pause_filter_count =
837 __shrink_ple_window(old,
838 pause_filter_count,
839 pause_filter_count_shrink,
840 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800841 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200842 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800843 trace_kvm_ple_window_update(vcpu->vcpu_id,
844 control->pause_filter_count, old);
845 }
Babu Moger8566ac82018-03-16 16:37:26 -0400846}
847
Tom Lendacky52918ed2020-01-09 17:42:16 -0600848/*
849 * The default MMIO mask is a single bit (excluding the present bit),
850 * which could conflict with the memory encryption bit. Check for
851 * memory encryption support and override the default MMIO mask if
852 * memory encryption is enabled.
853 */
854static __init void svm_adjust_mmio_mask(void)
855{
856 unsigned int enc_bit, mask_bit;
857 u64 msr, mask;
858
859 /* If there is no memory encryption support, use existing mask */
860 if (cpuid_eax(0x80000000) < 0x8000001f)
861 return;
862
863 /* If memory encryption is not enabled, use existing mask */
864 rdmsrl(MSR_K8_SYSCFG, msr);
865 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
866 return;
867
868 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
869 mask_bit = boot_cpu_data.x86_phys_bits;
870
871 /* Increment the mask bit if it is the same as the encryption bit */
872 if (enc_bit == mask_bit)
873 mask_bit++;
874
875 /*
876 * If the mask bit location is below 52, then some bits above the
877 * physical addressing limit will always be reserved, so use the
878 * rsvd_bits() function to generate the mask. This mask, along with
879 * the present bit, will be used to generate a page fault with
880 * PFER.RSV = 1.
881 *
882 * If the mask bit location is 52 (or above), then clear the mask.
883 */
884 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
885
Sean Christopherson81203372021-02-25 12:47:35 -0800886 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
Tom Lendacky52918ed2020-01-09 17:42:16 -0600887}
888
Li RongQingdd58f3c2020-02-23 16:13:12 +0800889static void svm_hardware_teardown(void)
890{
891 int cpu;
892
Joerg Roedeleaf78262020-03-24 10:41:54 +0100893 if (svm_sev_enabled())
894 sev_hardware_teardown();
Li RongQingdd58f3c2020-02-23 16:13:12 +0800895
896 for_each_possible_cpu(cpu)
897 svm_cpu_uninit(cpu);
898
899 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
900 iopm_base = 0;
901}
902
Sean Christopherson9b58b982020-03-02 15:56:42 -0800903static __init void svm_set_cpu_caps(void)
904{
905 kvm_set_cpu_caps();
906
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100907 supported_xss = 0;
908
Sean Christophersona50718c2020-03-02 15:57:07 -0800909 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
910 if (nested) {
Sean Christopherson9b58b982020-03-02 15:56:42 -0800911 kvm_cpu_cap_set(X86_FEATURE_SVM);
912
Sean Christopherson4eb87462020-03-02 15:57:08 -0800913 if (nrips)
Sean Christophersona50718c2020-03-02 15:57:07 -0800914 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
915
916 if (npt_enabled)
917 kvm_cpu_cap_set(X86_FEATURE_NPT);
Wei Huang14c2bf82021-01-26 03:18:31 -0500918
919 /* Nested VM can receive #VMEXIT instead of triggering #GP */
920 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
Sean Christophersona50718c2020-03-02 15:57:07 -0800921 }
922
Sean Christopherson93c380e2020-03-02 15:56:54 -0800923 /* CPUID 0x80000008 */
924 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
925 boot_cpu_has(X86_FEATURE_AMD_SSBD))
926 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
Sean Christopherson9b58b982020-03-02 15:56:42 -0800927}
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929static __init int svm_hardware_setup(void)
930{
931 int cpu;
932 struct page *iopm_pages;
933 void *iopm_va;
934 int r;
935
936 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
937
938 if (!iopm_pages)
939 return -ENOMEM;
940
941 iopm_va = page_address(iopm_pages);
Anthony Liguoric8681332007-04-30 09:48:11 +0300942 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
944
Joerg Roedel323c3d82010-03-01 15:34:37 +0100945 init_msrpm_offsets();
946
Sean Christophersoncfc48182020-03-02 15:56:23 -0800947 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
948
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100949 if (boot_cpu_has(X86_FEATURE_NX))
950 kvm_enable_efer_bits(EFER_NX);
951
Alexander Graf1b2fd702009-02-02 16:23:51 +0100952 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
953 kvm_enable_efer_bits(EFER_FFXSR);
954
Joerg Roedel92a1f122011-03-25 09:44:51 +0100955 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100956 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800957 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
958 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100959 }
960
Babu Moger8566ac82018-03-16 16:37:26 -0400961 /* Check for pause filtering support */
962 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
963 pause_filter_count = 0;
964 pause_filter_thresh = 0;
965 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
966 pause_filter_thresh = 0;
967 }
968
Alexander Graf236de052008-11-25 20:17:10 +0100969 if (nested) {
970 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200971 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100972 }
973
Tom Lendacky916391a2020-12-10 11:09:38 -0600974 if (IS_ENABLED(CONFIG_KVM_AMD_SEV) && sev) {
975 sev_hardware_setup();
976 } else {
977 sev = false;
978 sev_es = false;
Brijesh Singhe9df0942017-12-04 10:57:33 -0600979 }
980
Tom Lendacky52918ed2020-01-09 17:42:16 -0600981 svm_adjust_mmio_mask();
982
Zachary Amsden3230bb42009-09-29 11:38:37 -1000983 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 r = svm_cpu_init(cpu);
985 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100986 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800987 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100988
Sean Christopherson99840a72021-03-04 18:16:37 -0800989 /*
990 * KVM's MMU doesn't support using 2-level paging for itself, and thus
991 * NPT isn't supported if the host is using 2-level paging since host
992 * CR4 is unchanged on VMRUN.
993 */
994 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100995 npt_enabled = false;
996
Sean Christopherson99840a72021-03-04 18:16:37 -0800997 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100998 npt_enabled = false;
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100999
Sean Christopherson83013052020-07-15 20:41:22 -07001000 kvm_configure_mmu(npt_enabled, get_max_npt_level(), PG_LEVEL_1G);
Sean Christopherson213e0e12020-03-02 15:57:01 -08001001 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001002
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001003 if (nrips) {
1004 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1005 nrips = false;
1006 }
1007
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001008 if (avic) {
1009 if (!npt_enabled ||
1010 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001011 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001012 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001013 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001014 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001015
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001016 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1017 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001018 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001019
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001020 if (vls) {
1021 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001022 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001023 !IS_ENABLED(CONFIG_X86_64)) {
1024 vls = false;
1025 } else {
1026 pr_info("Virtual VMLOAD VMSAVE supported\n");
1027 }
1028 }
1029
Wei Huang3b9c7232021-01-26 03:18:30 -05001030 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
1031 svm_gp_erratum_intercept = false;
1032
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001033 if (vgif) {
1034 if (!boot_cpu_has(X86_FEATURE_VGIF))
1035 vgif = false;
1036 else
1037 pr_info("Virtual GIF supported\n");
1038 }
1039
Sean Christopherson9b58b982020-03-02 15:56:42 -08001040 svm_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08001041
Mohammed Gamal3edd6832020-07-10 17:48:11 +02001042 /*
1043 * It seems that on AMD processors PTE's accessed bit is
1044 * being set by the CPU hardware before the NPF vmexit.
1045 * This is not expected behaviour and our tests fail because
1046 * of it.
1047 * A workaround here is to disable support for
1048 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1049 * In this case userspace can know if there is support using
1050 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1051 * it
1052 * If future AMD CPU models change the behaviour described above,
1053 * this variable can be changed accordingly
1054 */
1055 allow_smaller_maxphyaddr = !npt_enabled;
1056
Avi Kivity6aa8b732006-12-10 02:21:36 -08001057 return 0;
1058
Joerg Roedelf65c2292008-02-13 18:58:46 +01001059err:
Li RongQingdd58f3c2020-02-23 16:13:12 +08001060 svm_hardware_teardown();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001061 return r;
1062}
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064static void init_seg(struct vmcb_seg *seg)
1065{
1066 seg->selector = 0;
1067 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001068 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069 seg->limit = 0xffff;
1070 seg->base = 0;
1071}
1072
1073static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1074{
1075 seg->selector = 0;
1076 seg->attrib = SVM_SELECTOR_P_MASK | type;
1077 seg->limit = 0xffff;
1078 seg->base = 0;
1079}
1080
Leonid Shatz326e7422018-11-06 12:14:25 +02001081static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001082{
1083 struct vcpu_svm *svm = to_svm(vcpu);
1084 u64 g_tsc_offset = 0;
1085
Joerg Roedel20307532010-11-29 17:51:48 +01001086 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001087 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001088 g_tsc_offset = svm->vmcb->control.tsc_offset -
Cathy Avery4995a362021-01-13 07:07:52 -05001089 svm->vmcb01.ptr->control.tsc_offset;
1090 svm->vmcb01.ptr->control.tsc_offset = offset;
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001091 }
1092
1093 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1094 svm->vmcb->control.tsc_offset - g_tsc_offset,
1095 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001096
1097 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001098
Joerg Roedel06e78522020-06-25 10:03:23 +02001099 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Leonid Shatz326e7422018-11-06 12:14:25 +02001100 return svm->vmcb->control.tsc_offset;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001101}
1102
Babu Moger4407a792020-09-11 14:29:19 -05001103static void svm_check_invpcid(struct vcpu_svm *svm)
1104{
1105 /*
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001106 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1107 * roots, or if INVPCID is disabled in the guest to inject #UD.
Babu Moger4407a792020-09-11 14:29:19 -05001108 */
1109 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001110 if (!npt_enabled ||
1111 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
Babu Moger4407a792020-09-11 14:29:19 -05001112 svm_set_intercept(svm, INTERCEPT_INVPCID);
1113 else
1114 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1115 }
1116}
1117
Paolo Bonzini63129752021-03-02 14:40:39 -05001118static void init_vmcb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119{
Paolo Bonzini63129752021-03-02 14:40:39 -05001120 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedele6101a92008-02-13 18:58:45 +01001121 struct vmcb_control_area *control = &svm->vmcb->control;
1122 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001123
Paolo Bonzini63129752021-03-02 14:40:39 -05001124 vcpu->arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001125
Babu Moger830bd712020-09-11 14:28:50 -05001126 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1127 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1128 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1129 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1130 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1131 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001132 if (!kvm_vcpu_apicv_active(vcpu))
Babu Moger830bd712020-09-11 14:28:50 -05001133 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001134
Paolo Bonzini5315c712014-03-03 13:08:29 +01001135 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001136
Joerg Roedel18c918c2010-11-30 18:03:59 +01001137 set_exception_intercept(svm, PF_VECTOR);
1138 set_exception_intercept(svm, UD_VECTOR);
1139 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001140 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001141 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001142 /*
1143 * Guest access to VMware backdoor ports could legitimately
1144 * trigger #GP because of TSS I/O permission bitmap.
1145 * We intercept those #GP and allow access to them anyway
1146 * as VMware does.
1147 */
1148 if (enable_vmware_backdoor)
1149 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150
Joerg Roedela284ba52020-06-25 10:03:24 +02001151 svm_set_intercept(svm, INTERCEPT_INTR);
1152 svm_set_intercept(svm, INTERCEPT_NMI);
1153 svm_set_intercept(svm, INTERCEPT_SMI);
1154 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1155 svm_set_intercept(svm, INTERCEPT_RDPMC);
1156 svm_set_intercept(svm, INTERCEPT_CPUID);
1157 svm_set_intercept(svm, INTERCEPT_INVD);
1158 svm_set_intercept(svm, INTERCEPT_INVLPG);
1159 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1160 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1161 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1162 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1163 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1164 svm_set_intercept(svm, INTERCEPT_VMRUN);
1165 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1166 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1167 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1168 svm_set_intercept(svm, INTERCEPT_STGI);
1169 svm_set_intercept(svm, INTERCEPT_CLGI);
1170 svm_set_intercept(svm, INTERCEPT_SKINIT);
1171 svm_set_intercept(svm, INTERCEPT_WBINVD);
1172 svm_set_intercept(svm, INTERCEPT_XSETBV);
1173 svm_set_intercept(svm, INTERCEPT_RDPRU);
1174 svm_set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175
Paolo Bonzini63129752021-03-02 14:40:39 -05001176 if (!kvm_mwait_in_guest(vcpu->kvm)) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001177 svm_set_intercept(svm, INTERCEPT_MONITOR);
1178 svm_set_intercept(svm, INTERCEPT_MWAIT);
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001179 }
1180
Paolo Bonzini63129752021-03-02 14:40:39 -05001181 if (!kvm_hlt_in_guest(vcpu->kvm))
Joerg Roedela284ba52020-06-25 10:03:24 +02001182 svm_set_intercept(svm, INTERCEPT_HLT);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001183
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001184 control->iopm_base_pa = __sme_set(iopm_base);
1185 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001186 control->int_ctl = V_INTR_MASKING_MASK;
1187
1188 init_seg(&save->es);
1189 init_seg(&save->ss);
1190 init_seg(&save->ds);
1191 init_seg(&save->fs);
1192 init_seg(&save->gs);
1193
1194 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001195 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 /* Executable/Readable Code Segment */
1197 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1198 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1199 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001200
1201 save->gdtr.limit = 0xffff;
1202 save->idtr.limit = 0xffff;
1203
1204 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1205 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1206
Paolo Bonzini63129752021-03-02 14:40:39 -05001207 svm_set_cr4(vcpu, 0);
1208 svm_set_efer(vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001209 save->dr6 = 0xffff0ff0;
Paolo Bonzini63129752021-03-02 14:40:39 -05001210 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001211 save->rip = 0x0000fff0;
Paolo Bonzini63129752021-03-02 14:40:39 -05001212 vcpu->arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001213
Joerg Roedele0231712010-02-24 18:59:10 +01001214 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001215 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001216 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001217 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001218 svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
1219 kvm_mmu_reset_context(vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001220
Rusty Russell66aee912007-07-17 23:34:16 +10001221 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001223
1224 if (npt_enabled) {
1225 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001226 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedela284ba52020-06-25 10:03:24 +02001227 svm_clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001228 clr_exception_intercept(svm, PF_VECTOR);
Babu Moger830bd712020-09-11 14:28:50 -05001229 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1230 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001231 save->g_pat = vcpu->arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001232 save->cr3 = 0;
1233 save->cr4 = 0;
1234 }
Cathy Avery193015a2021-01-12 11:43:13 -05001235 svm->current_vmcb->asid_generation = 0;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001236 svm->asid = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001237
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03001238 svm->nested.vmcb12_gpa = 0;
Cathy Avery81733962021-03-01 15:08:44 -05001239 svm->nested.last_vmcb12_gpa = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05001240 vcpu->arch.hflags = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001241
Paolo Bonzini63129752021-03-02 14:40:39 -05001242 if (!kvm_pause_in_guest(vcpu->kvm)) {
Babu Moger8566ac82018-03-16 16:37:26 -04001243 control->pause_filter_count = pause_filter_count;
1244 if (pause_filter_thresh)
1245 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedela284ba52020-06-25 10:03:24 +02001246 svm_set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001247 } else {
Joerg Roedela284ba52020-06-25 10:03:24 +02001248 svm_clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001249 }
1250
Babu Moger4407a792020-09-11 14:29:19 -05001251 svm_check_invpcid(svm);
1252
Babu Mogerd00b99c2021-02-17 10:56:04 -05001253 /*
1254 * If the host supports V_SPEC_CTRL then disable the interception
1255 * of MSR_IA32_SPEC_CTRL.
1256 */
1257 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1258 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1259
Paolo Bonzini63129752021-03-02 14:40:39 -05001260 if (kvm_vcpu_apicv_active(vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001261 avic_init_vmcb(svm);
1262
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001263 if (vgif) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001264 svm_clr_intercept(svm, INTERCEPT_STGI);
1265 svm_clr_intercept(svm, INTERCEPT_CLGI);
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001266 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1267 }
1268
Paolo Bonzini63129752021-03-02 14:40:39 -05001269 if (sev_guest(vcpu->kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001270 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001271 clr_exception_intercept(svm, UD_VECTOR);
Tom Lendacky376c6d22020-12-10 11:10:06 -06001272
Paolo Bonzini63129752021-03-02 14:40:39 -05001273 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky376c6d22020-12-10 11:10:06 -06001274 /* Perform SEV-ES specific VMCB updates */
1275 sev_es_init_vmcb(svm);
1276 }
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001277 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001278
Joerg Roedel06e78522020-06-25 10:03:23 +02001279 vmcb_mark_all_dirty(svm->vmcb);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001280
Joerg Roedel2af91942009-08-07 11:49:28 +02001281 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001282
1283}
1284
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001285static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001286{
1287 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001288 u32 dummy;
1289 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001290
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001291 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001292 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001293
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001294 if (!init_event) {
Paolo Bonzini63129752021-03-02 14:40:39 -05001295 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1296 MSR_IA32_APICBASE_ENABLE;
1297 if (kvm_vcpu_is_reset_bsp(vcpu))
1298 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001299 }
Paolo Bonzini63129752021-03-02 14:40:39 -05001300 init_vmcb(vcpu);
Avi Kivity70433382007-11-07 12:57:23 +02001301
Sean Christophersonf91af512020-03-04 17:34:37 -08001302 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001303 kvm_rdx_write(vcpu, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001304
1305 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1306 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001307}
1308
Cathy Avery4995a362021-01-13 07:07:52 -05001309void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1310{
1311 svm->current_vmcb = target_vmcb;
1312 svm->vmcb = target_vmcb->ptr;
1313 svm->vmcb_pa = target_vmcb->pa;
1314
1315 /*
Cathy Averyaf18fa72021-01-12 11:43:12 -05001316 * Track the physical CPU the target_vmcb is running on
1317 * in order to mark the VMCB dirty if the cpu changes at
1318 * its next vmrun.
Cathy Avery4995a362021-01-13 07:07:52 -05001319 */
1320
Cathy Averyaf18fa72021-01-12 11:43:12 -05001321 svm->current_vmcb->cpu = svm->vcpu.cpu;
Cathy Avery4995a362021-01-13 07:07:52 -05001322}
1323
Sean Christopherson987b2592019-12-18 13:54:55 -08001324static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001326 struct vcpu_svm *svm;
Cathy Avery4995a362021-01-13 07:07:52 -05001327 struct page *vmcb01_page;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001328 struct page *vmsa_page = NULL;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001329 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001331 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1332 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001333
Joerg Roedelf65c2292008-02-13 18:58:46 +01001334 err = -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -05001335 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1336 if (!vmcb01_page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001337 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001338
Paolo Bonzini63129752021-03-02 14:40:39 -05001339 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001340 /*
1341 * SEV-ES guests require a separate VMSA page used to contain
1342 * the encrypted register state of the guest.
1343 */
1344 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1345 if (!vmsa_page)
1346 goto error_free_vmcb_page;
Tom Lendackyed02b212020-12-10 11:10:01 -06001347
1348 /*
1349 * SEV-ES guests maintain an encrypted version of their FPU
1350 * state which is restored and saved on VMRUN and VMEXIT.
1351 * Free the fpu structure to prevent KVM from attempting to
1352 * access the FPU state.
1353 */
1354 kvm_free_guest_fpu(vcpu);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001355 }
1356
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001357 err = avic_init_vcpu(svm);
1358 if (err)
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001359 goto error_free_vmsa_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001360
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001361 /* We initialize this flag to true to make sure that the is_running
1362 * bit would be set the first time the vcpu is loaded.
1363 */
Suravee Suthikulpanit6c3e4422019-11-14 14:15:12 -06001364 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1365 svm->avic_is_running = true;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001366
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001367 svm->msrpm = svm_vcpu_alloc_msrpm();
Chen Zhou054409a2020-11-17 10:54:26 +08001368 if (!svm->msrpm) {
1369 err = -ENOMEM;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001370 goto error_free_vmsa_page;
Chen Zhou054409a2020-11-17 10:54:26 +08001371 }
Alexander Grafb286d5d2008-11-25 20:17:05 +01001372
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001373 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001374
Cathy Avery4995a362021-01-13 07:07:52 -05001375 svm->vmcb01.ptr = page_address(vmcb01_page);
1376 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001377
1378 if (vmsa_page)
1379 svm->vmsa = page_address(vmsa_page);
1380
Michael Rotha7fc06d2021-02-02 13:01:26 -06001381 svm->guest_state_loaded = false;
Cathy Avery4995a362021-01-13 07:07:52 -05001382
1383 svm_switch_vmcb(svm, &svm->vmcb01);
Paolo Bonzini63129752021-03-02 14:40:39 -05001384 init_vmcb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385
Sean Christopherson7f271792019-12-18 13:54:51 -08001386 svm_init_osvw(vcpu);
Paolo Bonzinibab0c312020-02-11 18:40:58 +01001387 vcpu->arch.microcode_version = 0x01000065;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001388
Paolo Bonzini63129752021-03-02 14:40:39 -05001389 if (sev_es_guest(vcpu->kvm))
Tom Lendacky376c6d22020-12-10 11:10:06 -06001390 /* Perform SEV-ES specific VMCB creation updates */
1391 sev_es_create_vcpu(svm);
1392
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001393 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001394
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001395error_free_vmsa_page:
1396 if (vmsa_page)
1397 __free_page(vmsa_page);
Maxim Levitsky8d22b902020-08-27 20:11:42 +03001398error_free_vmcb_page:
Cathy Avery4995a362021-01-13 07:07:52 -05001399 __free_page(vmcb01_page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001400out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001401 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402}
1403
Jim Mattsonfd65d312018-05-22 09:54:20 -07001404static void svm_clear_current_vmcb(struct vmcb *vmcb)
1405{
1406 int i;
1407
1408 for_each_online_cpu(i)
1409 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1410}
1411
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1413{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001414 struct vcpu_svm *svm = to_svm(vcpu);
1415
Jim Mattsonfd65d312018-05-22 09:54:20 -07001416 /*
1417 * The vmcb page can be recycled, causing a false negative in
1418 * svm_vcpu_load(). So, ensure that no logical CPU has this
1419 * vmcb page recorded as its current vmcb.
1420 */
1421 svm_clear_current_vmcb(svm->vmcb);
1422
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03001423 svm_free_nested(svm);
1424
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001425 sev_free_vcpu(vcpu);
1426
Cathy Avery4995a362021-01-13 07:07:52 -05001427 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001428 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429}
1430
Michael Rotha7fc06d2021-02-02 13:01:26 -06001431static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001432{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001433 struct vcpu_svm *svm = to_svm(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001434 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
1435 unsigned int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001436
Michael Rotha7fc06d2021-02-02 13:01:26 -06001437 if (svm->guest_state_loaded)
1438 return;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001439
Michael Rotha7fc06d2021-02-02 13:01:26 -06001440 /*
1441 * Certain MSRs are restored on VMEXIT (sev-es), or vmload of host save
1442 * area (non-sev-es). Save ones that aren't so we can restore them
1443 * individually later.
1444 */
1445 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1446 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1447
1448 /*
1449 * Save additional host state that will be restored on VMEXIT (sev-es)
1450 * or subsequent vmload of host save area.
1451 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001452 if (sev_es_guest(vcpu->kvm)) {
Michael Rotha7fc06d2021-02-02 13:01:26 -06001453 sev_es_prepare_guest_switch(svm, vcpu->cpu);
Tom Lendacky86137772020-12-10 11:10:07 -06001454 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06001455 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06001456 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001457
Haozhong Zhangad7218832015-10-20 15:39:02 +08001458 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1459 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1460 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1461 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1462 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1463 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001464 }
Michael Rotha7fc06d2021-02-02 13:01:26 -06001465
Paolo Bonzini46896c72015-11-12 14:49:16 +01001466 /* This assumes that the kernel never uses MSR_TSC_AUX */
1467 if (static_cpu_has(X86_FEATURE_RDTSCP))
1468 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001469
Michael Rotha7fc06d2021-02-02 13:01:26 -06001470 svm->guest_state_loaded = true;
1471}
1472
1473static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1474{
1475 struct vcpu_svm *svm = to_svm(vcpu);
1476 unsigned int i;
1477
1478 if (!svm->guest_state_loaded)
1479 return;
1480
1481 /*
1482 * Certain MSRs are restored on VMEXIT (sev-es), or vmload of host save
1483 * area (non-sev-es). Restore the ones that weren't.
1484 */
1485 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
1486 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
1487
1488 svm->guest_state_loaded = false;
1489}
1490
1491static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1492{
1493 struct vcpu_svm *svm = to_svm(vcpu);
1494 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1495
Ashok Raj15d45072018-02-01 22:59:43 +01001496 if (sd->current_vmcb != svm->vmcb) {
1497 sd->current_vmcb = svm->vmcb;
1498 indirect_branch_prediction_barrier();
1499 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001500 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001501}
1502
1503static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1504{
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001505 avic_vcpu_put(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001506 svm_prepare_host_switch(vcpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001507
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001508 ++vcpu->stat.host_state_reload;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001509}
1510
Avi Kivity6aa8b732006-12-10 02:21:36 -08001511static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1512{
Ladi Prosek9b611742017-06-21 09:06:59 +02001513 struct vcpu_svm *svm = to_svm(vcpu);
1514 unsigned long rflags = svm->vmcb->save.rflags;
1515
1516 if (svm->nmi_singlestep) {
1517 /* Hide our flags if they were not set by the guest */
1518 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1519 rflags &= ~X86_EFLAGS_TF;
1520 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1521 rflags &= ~X86_EFLAGS_RF;
1522 }
1523 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001524}
1525
1526static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1527{
Ladi Prosek9b611742017-06-21 09:06:59 +02001528 if (to_svm(vcpu)->nmi_singlestep)
1529 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1530
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001531 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001532 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001533 * (caused by either a task switch or an inter-privilege IRET),
1534 * so we do not need to update the CPL here.
1535 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001536 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001537}
1538
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001539static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1540{
1541 switch (reg) {
1542 case VCPU_EXREG_PDPTR:
1543 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001544 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001545 break;
1546 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07001547 WARN_ON_ONCE(1);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001548 }
1549}
1550
Suravee Suthikulpanite14b7782020-05-06 08:17:55 -05001551static void svm_set_vintr(struct vcpu_svm *svm)
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001552{
1553 struct vmcb_control_area *control;
1554
1555 /* The following fields are ignored when AVIC is enabled */
1556 WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu));
Joerg Roedela284ba52020-06-25 10:03:24 +02001557 svm_set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001558
1559 /*
1560 * This is just a dummy VINTR to actually cause a vmexit to happen.
1561 * Actual injection of virtual interrupts happens through EVENTINJ.
1562 */
1563 control = &svm->vmcb->control;
1564 control->int_vector = 0x0;
1565 control->int_ctl &= ~V_INTR_PRIO_MASK;
1566 control->int_ctl |= V_IRQ_MASK |
1567 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedel06e78522020-06-25 10:03:23 +02001568 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001569}
1570
Alexander Graff0b85052008-11-25 20:17:01 +01001571static void svm_clear_vintr(struct vcpu_svm *svm)
1572{
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001573 const u32 mask = V_TPR_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK | V_INTR_MASKING_MASK;
Joerg Roedela284ba52020-06-25 10:03:24 +02001574 svm_clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001575
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001576 /* Drop int_ctl fields related to VINTR injection. */
1577 svm->vmcb->control.int_ctl &= mask;
1578 if (is_guest_mode(&svm->vcpu)) {
Cathy Avery4995a362021-01-13 07:07:52 -05001579 svm->vmcb01.ptr->control.int_ctl &= mask;
Paolo Bonzinifb7333d2020-06-08 07:11:47 -04001580
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001581 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1582 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1583 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & ~mask;
1584 }
1585
Joerg Roedel06e78522020-06-25 10:03:23 +02001586 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001587}
1588
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1590{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001591 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001592 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001593
1594 switch (seg) {
1595 case VCPU_SREG_CS: return &save->cs;
1596 case VCPU_SREG_DS: return &save->ds;
1597 case VCPU_SREG_ES: return &save->es;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001598 case VCPU_SREG_FS: return &save01->fs;
1599 case VCPU_SREG_GS: return &save01->gs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001600 case VCPU_SREG_SS: return &save->ss;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001601 case VCPU_SREG_TR: return &save01->tr;
1602 case VCPU_SREG_LDTR: return &save01->ldtr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603 }
1604 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001605 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001606}
1607
1608static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1609{
1610 struct vmcb_seg *s = svm_seg(vcpu, seg);
1611
1612 return s->base;
1613}
1614
1615static void svm_get_segment(struct kvm_vcpu *vcpu,
1616 struct kvm_segment *var, int seg)
1617{
1618 struct vmcb_seg *s = svm_seg(vcpu, seg);
1619
1620 var->base = s->base;
1621 var->limit = s->limit;
1622 var->selector = s->selector;
1623 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1624 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1625 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1626 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1627 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1628 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1629 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301630
1631 /*
1632 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1633 * However, the SVM spec states that the G bit is not observed by the
1634 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1635 * So let's synthesize a legal G bit for all segments, this helps
1636 * running KVM nested. It also helps cross-vendor migration, because
1637 * Intel's vmentry has a check on the 'G' bit.
1638 */
1639 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001640
Joerg Roedele0231712010-02-24 18:59:10 +01001641 /*
1642 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001643 * for cross vendor migration purposes by "not present"
1644 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001645 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001646
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001647 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001648 case VCPU_SREG_TR:
1649 /*
1650 * Work around a bug where the busy flag in the tr selector
1651 * isn't exposed
1652 */
Amit Shahc0d09822008-10-27 09:04:18 +00001653 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001654 break;
1655 case VCPU_SREG_DS:
1656 case VCPU_SREG_ES:
1657 case VCPU_SREG_FS:
1658 case VCPU_SREG_GS:
1659 /*
1660 * The accessed bit must always be set in the segment
1661 * descriptor cache, although it can be cleared in the
1662 * descriptor, the cached bit always remains at 1. Since
1663 * Intel has a check on this, set it here to support
1664 * cross-vendor migration.
1665 */
1666 if (!var->unusable)
1667 var->type |= 0x1;
1668 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001669 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001670 /*
1671 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001672 * descriptor is left as 1, although the whole segment has
1673 * been made unusable. Clear it here to pass an Intel VMX
1674 * entry check when cross vendor migrating.
1675 */
1676 if (var->unusable)
1677 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001678 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001679 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001680 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001681 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682}
1683
Izik Eidus2e4d2652008-03-24 19:38:34 +02001684static int svm_get_cpl(struct kvm_vcpu *vcpu)
1685{
1686 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1687
1688 return save->cpl;
1689}
1690
Gleb Natapov89a27f42010-02-16 10:51:48 +02001691static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001692{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001693 struct vcpu_svm *svm = to_svm(vcpu);
1694
Gleb Natapov89a27f42010-02-16 10:51:48 +02001695 dt->size = svm->vmcb->save.idtr.limit;
1696 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001697}
1698
Gleb Natapov89a27f42010-02-16 10:51:48 +02001699static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001701 struct vcpu_svm *svm = to_svm(vcpu);
1702
Gleb Natapov89a27f42010-02-16 10:51:48 +02001703 svm->vmcb->save.idtr.limit = dt->size;
1704 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001705 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001706}
1707
Gleb Natapov89a27f42010-02-16 10:51:48 +02001708static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001709{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001710 struct vcpu_svm *svm = to_svm(vcpu);
1711
Gleb Natapov89a27f42010-02-16 10:51:48 +02001712 dt->size = svm->vmcb->save.gdtr.limit;
1713 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001714}
1715
Gleb Natapov89a27f42010-02-16 10:51:48 +02001716static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001717{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001718 struct vcpu_svm *svm = to_svm(vcpu);
1719
Gleb Natapov89a27f42010-02-16 10:51:48 +02001720 svm->vmcb->save.gdtr.limit = dt->size;
1721 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001722 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001723}
1724
Joerg Roedel883b0a92020-03-24 10:41:52 +01001725void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001726{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001727 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001728 u64 hcr0 = cr0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001729
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001730#ifdef CONFIG_X86_64
Tom Lendackyf1c63662020-12-14 10:29:50 -05001731 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001732 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001733 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001734 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735 }
1736
Mike Dayd77c26f2007-10-08 09:02:08 -04001737 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001738 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001739 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001740 }
1741 }
1742#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001743 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001744
1745 if (!npt_enabled)
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001746 hcr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001747
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001748 /*
1749 * re-enable caching here because the QEMU bios
1750 * does not do it - this results in some delay at
1751 * reboot
1752 */
1753 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001754 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1755
1756 svm->vmcb->save.cr0 = hcr0;
Joerg Roedel06e78522020-06-25 10:03:23 +02001757 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001758
1759 /*
1760 * SEV-ES guests must always keep the CR intercepts cleared. CR
1761 * tracking is done using the CR write traps.
1762 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001763 if (sev_es_guest(vcpu->kvm))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001764 return;
1765
1766 if (hcr0 == cr0) {
1767 /* Selective CR0 write remains on. */
1768 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1769 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1770 } else {
1771 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1772 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1773 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774}
1775
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001776static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1777{
1778 return true;
1779}
1780
1781void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001782{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001783 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Paolo Bonzinidc924b02020-11-15 09:44:18 -05001784 unsigned long old_cr4 = vcpu->arch.cr4;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001785
1786 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Sean Christophersonf55ac302020-03-20 14:28:12 -07001787 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001788
Joerg Roedelec077262008-04-09 14:15:28 +02001789 vcpu->arch.cr4 = cr4;
1790 if (!npt_enabled)
1791 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001792 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001793 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedel06e78522020-06-25 10:03:23 +02001794 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Jim Mattson2259c172020-10-29 10:06:48 -07001795
1796 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1797 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001798}
1799
1800static void svm_set_segment(struct kvm_vcpu *vcpu,
1801 struct kvm_segment *var, int seg)
1802{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001803 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001804 struct vmcb_seg *s = svm_seg(vcpu, seg);
1805
1806 s->base = var->base;
1807 s->limit = var->limit;
1808 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001809 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1810 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1811 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1812 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1813 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1814 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1815 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1816 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001817
1818 /*
1819 * This is always accurate, except if SYSRET returned to a segment
1820 * with SS.DPL != 3. Intel does not have this quirk, and always
1821 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1822 * would entail passing the CPL to userspace and back.
1823 */
1824 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001825 /* This is symmetric with svm_get_segment() */
1826 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827
Joerg Roedel06e78522020-06-25 10:03:23 +02001828 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829}
1830
Jason Baronb6a7cc32021-01-14 22:27:54 -05001831static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001832{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001833 struct vcpu_svm *svm = to_svm(vcpu);
1834
Joerg Roedel18c918c2010-11-30 18:03:59 +01001835 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001836
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001837 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001838 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001839 set_exception_intercept(svm, BP_VECTOR);
Paolo Bonzini69869822020-07-10 17:48:06 +02001840 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001841}
1842
Tejun Heo0fe1e002009-10-29 22:34:14 +09001843static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001844{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001845 if (sd->next_asid > sd->max_asid) {
1846 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001847 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001848 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001849 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001850 }
1851
Cathy Avery193015a2021-01-12 11:43:13 -05001852 svm->current_vmcb->asid_generation = sd->asid_generation;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001853 svm->asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001854}
1855
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001856static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001857{
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001858 struct vmcb *vmcb = svm->vmcb;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001859
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001860 if (svm->vcpu.arch.guest_state_protected)
1861 return;
1862
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001863 if (unlikely(value != vmcb->save.dr6)) {
1864 vmcb->save.dr6 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001865 vmcb_mark_dirty(vmcb, VMCB_DR);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001866 }
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001867}
1868
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001869static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1870{
1871 struct vcpu_svm *svm = to_svm(vcpu);
1872
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001873 if (vcpu->arch.guest_state_protected)
1874 return;
1875
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001876 get_debugreg(vcpu->arch.db[0], 0);
1877 get_debugreg(vcpu->arch.db[1], 1);
1878 get_debugreg(vcpu->arch.db[2], 2);
1879 get_debugreg(vcpu->arch.db[3], 3);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001880 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001881 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001882 * because db_interception might need it. We can do it before vmentry.
1883 */
Paolo Bonzini5679b802020-05-04 11:28:25 -04001884 vcpu->arch.dr6 = svm->vmcb->save.dr6;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001885 vcpu->arch.dr7 = svm->vmcb->save.dr7;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001886 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1887 set_dr_intercepts(svm);
1888}
1889
Gleb Natapov020df072010-04-13 10:05:23 +03001890static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001891{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001892 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001893
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001894 if (vcpu->arch.guest_state_protected)
1895 return;
1896
Gleb Natapov020df072010-04-13 10:05:23 +03001897 svm->vmcb->save.dr7 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001898 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899}
1900
Paolo Bonzini63129752021-03-02 14:40:39 -05001901static int pf_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902{
Paolo Bonzini63129752021-03-02 14:40:39 -05001903 struct vcpu_svm *svm = to_svm(vcpu);
1904
Sean Christopherson6d1b8672021-03-04 17:10:56 -08001905 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07001906 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907
Paolo Bonzini63129752021-03-02 14:40:39 -05001908 return kvm_handle_page_fault(vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001909 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1910 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001911 svm->vmcb->control.insn_len);
1912}
1913
Paolo Bonzini63129752021-03-02 14:40:39 -05001914static int npf_interception(struct kvm_vcpu *vcpu)
Paolo Bonzinid0006532017-08-11 18:36:43 +02001915{
Paolo Bonzini63129752021-03-02 14:40:39 -05001916 struct vcpu_svm *svm = to_svm(vcpu);
1917
Brijesh Singh0ede79e2017-12-04 10:57:39 -06001918 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02001919 u64 error_code = svm->vmcb->control.exit_info_1;
1920
1921 trace_kvm_page_fault(fault_address, error_code);
Paolo Bonzini63129752021-03-02 14:40:39 -05001922 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001923 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1924 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001925 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001926}
1927
Paolo Bonzini63129752021-03-02 14:40:39 -05001928static int db_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001929{
Paolo Bonzini63129752021-03-02 14:40:39 -05001930 struct kvm_run *kvm_run = vcpu->run;
1931 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001932
Paolo Bonzini63129752021-03-02 14:40:39 -05001933 if (!(vcpu->guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001934 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001935 !svm->nmi_singlestep) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001936 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
Paolo Bonzini63129752021-03-02 14:40:39 -05001937 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001938 return 1;
1939 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001940
Jan Kiszka6be7d302009-10-18 13:24:54 +02001941 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001942 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02001943 /* Make sure we check for pending NMIs upon entry */
1944 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001945 }
1946
Paolo Bonzini63129752021-03-02 14:40:39 -05001947 if (vcpu->guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001948 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001949 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Paolo Bonzinidee919d2020-05-04 09:34:10 -04001950 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1951 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
Gleb Natapov44c11432009-05-11 13:35:52 +03001952 kvm_run->debug.arch.pc =
1953 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1954 kvm_run->debug.arch.exception = DB_VECTOR;
1955 return 0;
1956 }
1957
1958 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001959}
1960
Paolo Bonzini63129752021-03-02 14:40:39 -05001961static int bp_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001962{
Paolo Bonzini63129752021-03-02 14:40:39 -05001963 struct vcpu_svm *svm = to_svm(vcpu);
1964 struct kvm_run *kvm_run = vcpu->run;
Avi Kivity851ba692009-08-24 11:10:17 +03001965
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001966 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1967 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1968 kvm_run->debug.arch.exception = BP_VECTOR;
1969 return 0;
1970}
1971
Paolo Bonzini63129752021-03-02 14:40:39 -05001972static int ud_interception(struct kvm_vcpu *vcpu)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001973{
Paolo Bonzini63129752021-03-02 14:40:39 -05001974 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001975}
1976
Paolo Bonzini63129752021-03-02 14:40:39 -05001977static int ac_interception(struct kvm_vcpu *vcpu)
Eric Northup54a20552015-11-03 18:03:53 +01001978{
Paolo Bonzini63129752021-03-02 14:40:39 -05001979 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
Eric Northup54a20552015-11-03 18:03:53 +01001980 return 1;
1981}
1982
Joerg Roedel67ec6602010-05-17 14:43:35 +02001983static bool is_erratum_383(void)
1984{
1985 int err, i;
1986 u64 value;
1987
1988 if (!erratum_383_found)
1989 return false;
1990
1991 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1992 if (err)
1993 return false;
1994
1995 /* Bit 62 may or may not be set for this mce */
1996 value &= ~(1ULL << 62);
1997
1998 if (value != 0xb600000000010015ULL)
1999 return false;
2000
2001 /* Clear MCi_STATUS registers */
2002 for (i = 0; i < 6; ++i)
2003 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2004
2005 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2006 if (!err) {
2007 u32 low, high;
2008
2009 value &= ~(1ULL << 2);
2010 low = lower_32_bits(value);
2011 high = upper_32_bits(value);
2012
2013 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2014 }
2015
2016 /* Flush tlb to evict multi-match entries */
2017 __flush_tlb_all();
2018
2019 return true;
2020}
2021
Paolo Bonzini63129752021-03-02 14:40:39 -05002022static void svm_handle_mce(struct kvm_vcpu *vcpu)
Joerg Roedel53371b52008-04-09 14:15:30 +02002023{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002024 if (is_erratum_383()) {
2025 /*
2026 * Erratum 383 triggered. Guest state is corrupt so kill the
2027 * guest.
2028 */
2029 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2030
Paolo Bonzini63129752021-03-02 14:40:39 -05002031 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002032
2033 return;
2034 }
2035
Joerg Roedel53371b52008-04-09 14:15:30 +02002036 /*
2037 * On an #MC intercept the MCE handler is not called automatically in
2038 * the host. So do it by hand here.
2039 */
Uros Bizjak1c164cb2020-04-11 17:36:27 +02002040 kvm_machine_check();
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002041}
2042
Paolo Bonzini63129752021-03-02 14:40:39 -05002043static int mc_interception(struct kvm_vcpu *vcpu)
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002044{
Joerg Roedel53371b52008-04-09 14:15:30 +02002045 return 1;
2046}
2047
Paolo Bonzini63129752021-03-02 14:40:39 -05002048static int shutdown_interception(struct kvm_vcpu *vcpu)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002049{
Paolo Bonzini63129752021-03-02 14:40:39 -05002050 struct kvm_run *kvm_run = vcpu->run;
2051 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002052
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002053 /*
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002054 * The VM save area has already been encrypted so it
2055 * cannot be reinitialized - just terminate.
2056 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002057 if (sev_es_guest(vcpu->kvm))
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002058 return -EINVAL;
2059
2060 /*
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002061 * VMCB is undefined after a SHUTDOWN intercept
2062 * so reinitialize it.
2063 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002064 clear_page(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05002065 init_vmcb(vcpu);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002066
2067 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2068 return 0;
2069}
2070
Paolo Bonzini63129752021-03-02 14:40:39 -05002071static int io_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002072{
Paolo Bonzini63129752021-03-02 14:40:39 -05002073 struct vcpu_svm *svm = to_svm(vcpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002074 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002075 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002076 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002077
Paolo Bonzini63129752021-03-02 14:40:39 -05002078 ++vcpu->stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002079 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002080 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2081 port = io_info >> 16;
2082 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002083
2084 if (string) {
2085 if (sev_es_guest(vcpu->kvm))
2086 return sev_es_string_io(svm, size, port, in);
2087 else
2088 return kvm_emulate_instruction(vcpu, 0);
2089 }
2090
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002091 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002092
Paolo Bonzini63129752021-03-02 14:40:39 -05002093 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002094}
2095
Paolo Bonzini63129752021-03-02 14:40:39 -05002096static int nmi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002097{
2098 return 1;
2099}
2100
Paolo Bonzini63129752021-03-02 14:40:39 -05002101static int intr_interception(struct kvm_vcpu *vcpu)
Joerg Roedela0698052008-04-30 17:56:01 +02002102{
Paolo Bonzini63129752021-03-02 14:40:39 -05002103 ++vcpu->stat.irq_exits;
Joerg Roedela0698052008-04-30 17:56:01 +02002104 return 1;
2105}
2106
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002107static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
Alexander Graf55426752008-11-25 20:17:06 +01002108{
Paolo Bonzini63129752021-03-02 14:40:39 -05002109 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002110 struct vmcb *vmcb12;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002111 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002112 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002113
Paolo Bonzini63129752021-03-02 14:40:39 -05002114 if (nested_svm_check_permissions(vcpu))
Alexander Graf55426752008-11-25 20:17:06 +01002115 return 1;
2116
Paolo Bonzini63129752021-03-02 14:40:39 -05002117 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002118 if (ret) {
2119 if (ret == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -05002120 kvm_inject_gp(vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002121 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002122 }
2123
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002124 vmcb12 = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002125
Paolo Bonzini63129752021-03-02 14:40:39 -05002126 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002127
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002128 if (vmload) {
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002129 nested_svm_vmloadsave(vmcb12, svm->vmcb);
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002130 svm->sysenter_eip_hi = 0;
2131 svm->sysenter_esp_hi = 0;
2132 } else
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002133 nested_svm_vmloadsave(svm->vmcb, vmcb12);
2134
Paolo Bonzini63129752021-03-02 14:40:39 -05002135 kvm_vcpu_unmap(vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002136
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002137 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002138}
2139
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002140static int vmload_interception(struct kvm_vcpu *vcpu)
2141{
2142 return vmload_vmsave_interception(vcpu, true);
2143}
2144
Paolo Bonzini63129752021-03-02 14:40:39 -05002145static int vmsave_interception(struct kvm_vcpu *vcpu)
Alexander Graf55426752008-11-25 20:17:06 +01002146{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002147 return vmload_vmsave_interception(vcpu, false);
Alexander Graf55426752008-11-25 20:17:06 +01002148}
2149
Paolo Bonzini63129752021-03-02 14:40:39 -05002150static int vmrun_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002151{
Paolo Bonzini63129752021-03-02 14:40:39 -05002152 if (nested_svm_check_permissions(vcpu))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002153 return 1;
2154
Paolo Bonzini63129752021-03-02 14:40:39 -05002155 return nested_svm_vmrun(vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002156}
2157
Bandan Das82a11e9c2021-01-26 03:18:29 -05002158enum {
2159 NONE_SVM_INSTR,
2160 SVM_INSTR_VMRUN,
2161 SVM_INSTR_VMLOAD,
2162 SVM_INSTR_VMSAVE,
2163};
2164
2165/* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2166static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2167{
2168 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2169
2170 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2171 return NONE_SVM_INSTR;
2172
2173 switch (ctxt->modrm) {
2174 case 0xd8: /* VMRUN */
2175 return SVM_INSTR_VMRUN;
2176 case 0xda: /* VMLOAD */
2177 return SVM_INSTR_VMLOAD;
2178 case 0xdb: /* VMSAVE */
2179 return SVM_INSTR_VMSAVE;
2180 default:
2181 break;
2182 }
2183
2184 return NONE_SVM_INSTR;
2185}
2186
2187static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2188{
Wei Huang14c2bf82021-01-26 03:18:31 -05002189 const int guest_mode_exit_codes[] = {
2190 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2191 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2192 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2193 };
Paolo Bonzini63129752021-03-02 14:40:39 -05002194 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
Bandan Das82a11e9c2021-01-26 03:18:29 -05002195 [SVM_INSTR_VMRUN] = vmrun_interception,
2196 [SVM_INSTR_VMLOAD] = vmload_interception,
2197 [SVM_INSTR_VMSAVE] = vmsave_interception,
2198 };
2199 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002200 int ret;
Bandan Das82a11e9c2021-01-26 03:18:29 -05002201
Wei Huang14c2bf82021-01-26 03:18:31 -05002202 if (is_guest_mode(vcpu)) {
Sean Christopherson2df8d382021-02-23 16:56:26 -08002203 /* Returns '1' or -errno on failure, '0' on success. */
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08002204 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002205 if (ret)
2206 return ret;
2207 return 1;
2208 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002209 return svm_instr_handlers[opcode](vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002210}
2211
2212/*
2213 * #GP handling code. Note that #GP can be triggered under the following two
2214 * cases:
2215 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2216 * some AMD CPUs when EAX of these instructions are in the reserved memory
2217 * regions (e.g. SMM memory on host).
2218 * 2) VMware backdoor
2219 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002220static int gp_interception(struct kvm_vcpu *vcpu)
Bandan Das82a11e9c2021-01-26 03:18:29 -05002221{
Paolo Bonzini63129752021-03-02 14:40:39 -05002222 struct vcpu_svm *svm = to_svm(vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002223 u32 error_code = svm->vmcb->control.exit_info_1;
2224 int opcode;
2225
2226 /* Both #GP cases have zero error_code */
2227 if (error_code)
2228 goto reinject;
2229
2230 /* Decode the instruction for usage later */
2231 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2232 goto reinject;
2233
2234 opcode = svm_instr_opcode(vcpu);
2235
2236 if (opcode == NONE_SVM_INSTR) {
2237 if (!enable_vmware_backdoor)
2238 goto reinject;
2239
2240 /*
2241 * VMware backdoor emulation on #GP interception only handles
2242 * IN{S}, OUT{S}, and RDPMC.
2243 */
Wei Huang14c2bf82021-01-26 03:18:31 -05002244 if (!is_guest_mode(vcpu))
2245 return kvm_emulate_instruction(vcpu,
Bandan Das82a11e9c2021-01-26 03:18:29 -05002246 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2247 } else
2248 return emulate_svm_instr(vcpu, opcode);
2249
2250reinject:
2251 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2252 return 1;
2253}
2254
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002255void svm_set_gif(struct vcpu_svm *svm, bool value)
2256{
2257 if (value) {
2258 /*
2259 * If VGIF is enabled, the STGI intercept is only added to
2260 * detect the opening of the SMI/NMI window; remove it now.
2261 * Likewise, clear the VINTR intercept, we will set it
2262 * again while processing KVM_REQ_EVENT if needed.
2263 */
2264 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02002265 svm_clr_intercept(svm, INTERCEPT_STGI);
2266 if (svm_is_intercept(svm, INTERCEPT_VINTR))
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002267 svm_clear_vintr(svm);
2268
2269 enable_gif(svm);
2270 if (svm->vcpu.arch.smi_pending ||
2271 svm->vcpu.arch.nmi_pending ||
2272 kvm_cpu_has_injectable_intr(&svm->vcpu))
2273 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2274 } else {
2275 disable_gif(svm);
2276
2277 /*
2278 * After a CLGI no interrupts should come. But if vGIF is
2279 * in use, we still rely on the VINTR intercept (rather than
2280 * STGI) to detect an open interrupt window.
2281 */
2282 if (!vgif_enabled(svm))
2283 svm_clear_vintr(svm);
2284 }
2285}
2286
Paolo Bonzini63129752021-03-02 14:40:39 -05002287static int stgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002288{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002289 int ret;
2290
Paolo Bonzini63129752021-03-02 14:40:39 -05002291 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002292 return 1;
2293
Paolo Bonzini63129752021-03-02 14:40:39 -05002294 ret = kvm_skip_emulated_instruction(vcpu);
2295 svm_set_gif(to_svm(vcpu), true);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002296 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002297}
2298
Paolo Bonzini63129752021-03-02 14:40:39 -05002299static int clgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002300{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002301 int ret;
2302
Paolo Bonzini63129752021-03-02 14:40:39 -05002303 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002304 return 1;
2305
Paolo Bonzini63129752021-03-02 14:40:39 -05002306 ret = kvm_skip_emulated_instruction(vcpu);
2307 svm_set_gif(to_svm(vcpu), false);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002308 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002309}
2310
Paolo Bonzini63129752021-03-02 14:40:39 -05002311static int invlpga_interception(struct kvm_vcpu *vcpu)
Alexander Grafff092382009-06-15 15:21:24 +02002312{
Paolo Bonzini63129752021-03-02 14:40:39 -05002313 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, kvm_rcx_read(vcpu),
2314 kvm_rax_read(vcpu));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002315
Alexander Grafff092382009-06-15 15:21:24 +02002316 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Paolo Bonzini63129752021-03-02 14:40:39 -05002317 kvm_mmu_invlpg(vcpu, kvm_rax_read(vcpu));
Alexander Grafff092382009-06-15 15:21:24 +02002318
Paolo Bonzini63129752021-03-02 14:40:39 -05002319 return kvm_skip_emulated_instruction(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002320}
2321
Paolo Bonzini63129752021-03-02 14:40:39 -05002322static int skinit_interception(struct kvm_vcpu *vcpu)
Joerg Roedel532a46b2009-10-09 16:08:32 +02002323{
Paolo Bonzini63129752021-03-02 14:40:39 -05002324 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002325
Paolo Bonzini63129752021-03-02 14:40:39 -05002326 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel532a46b2009-10-09 16:08:32 +02002327 return 1;
2328}
2329
Paolo Bonzini63129752021-03-02 14:40:39 -05002330static int task_switch_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002331{
Paolo Bonzini63129752021-03-02 14:40:39 -05002332 struct vcpu_svm *svm = to_svm(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002333 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002334 int reason;
2335 int int_type = svm->vmcb->control.exit_int_info &
2336 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002337 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002338 uint32_t type =
2339 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2340 uint32_t idt_v =
2341 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002342 bool has_error_code = false;
2343 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002344
2345 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002346
Izik Eidus37817f22008-03-24 23:14:53 +02002347 if (svm->vmcb->control.exit_info_2 &
2348 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002349 reason = TASK_SWITCH_IRET;
2350 else if (svm->vmcb->control.exit_info_2 &
2351 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2352 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002353 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002354 reason = TASK_SWITCH_GATE;
2355 else
2356 reason = TASK_SWITCH_CALL;
2357
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002358 if (reason == TASK_SWITCH_GATE) {
2359 switch (type) {
2360 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05002361 vcpu->arch.nmi_injected = false;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002362 break;
2363 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002364 if (svm->vmcb->control.exit_info_2 &
2365 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2366 has_error_code = true;
2367 error_code =
2368 (u32)svm->vmcb->control.exit_info_2;
2369 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002370 kvm_clear_exception_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002371 break;
2372 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05002373 kvm_clear_interrupt_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002374 break;
2375 default:
2376 break;
2377 }
2378 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002379
Gleb Natapov8317c292009-04-12 13:37:02 +03002380 if (reason != TASK_SWITCH_GATE ||
2381 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2382 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002383 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002384 if (!skip_emulated_instruction(vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002385 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002386 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002387
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002388 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2389 int_vec = -1;
2390
Paolo Bonzini63129752021-03-02 14:40:39 -05002391 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002392 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393}
2394
Paolo Bonzini63129752021-03-02 14:40:39 -05002395static int iret_interception(struct kvm_vcpu *vcpu)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002396{
Paolo Bonzini63129752021-03-02 14:40:39 -05002397 struct vcpu_svm *svm = to_svm(vcpu);
2398
2399 ++vcpu->stat.nmi_window_exits;
2400 vcpu->arch.hflags |= HF_IRET_MASK;
2401 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002402 svm_clr_intercept(svm, INTERCEPT_IRET);
Paolo Bonzini63129752021-03-02 14:40:39 -05002403 svm->nmi_iret_rip = kvm_rip_read(vcpu);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002404 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002405 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002406 return 1;
2407}
2408
Paolo Bonzini63129752021-03-02 14:40:39 -05002409static int invlpg_interception(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002410{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002411 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002412 return kvm_emulate_instruction(vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002413
Paolo Bonzini63129752021-03-02 14:40:39 -05002414 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2415 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002416}
2417
Paolo Bonzini63129752021-03-02 14:40:39 -05002418static int emulate_on_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002419{
Paolo Bonzini63129752021-03-02 14:40:39 -05002420 return kvm_emulate_instruction(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421}
2422
Paolo Bonzini63129752021-03-02 14:40:39 -05002423static int rsm_interception(struct kvm_vcpu *vcpu)
Brijesh Singh7607b712018-02-19 10:14:44 -06002424{
Paolo Bonzini63129752021-03-02 14:40:39 -05002425 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002426}
2427
Paolo Bonzini63129752021-03-02 14:40:39 -05002428static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
Xiubo Li52eb5a62015-03-13 17:39:45 +08002429 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002430{
Paolo Bonzini63129752021-03-02 14:40:39 -05002431 struct vcpu_svm *svm = to_svm(vcpu);
2432 unsigned long cr0 = vcpu->arch.cr0;
Joerg Roedel628afd22011-04-04 12:39:36 +02002433 bool ret = false;
Joerg Roedel628afd22011-04-04 12:39:36 +02002434
Paolo Bonzini63129752021-03-02 14:40:39 -05002435 if (!is_guest_mode(vcpu) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05002436 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
Joerg Roedel628afd22011-04-04 12:39:36 +02002437 return false;
2438
2439 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2440 val &= ~SVM_CR0_SELECTIVE_MASK;
2441
2442 if (cr0 ^ val) {
2443 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2444 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2445 }
2446
2447 return ret;
2448}
2449
Andre Przywara7ff76d52010-12-21 11:12:04 +01002450#define CR_VALID (1ULL << 63)
2451
Paolo Bonzini63129752021-03-02 14:40:39 -05002452static int cr_interception(struct kvm_vcpu *vcpu)
Andre Przywara7ff76d52010-12-21 11:12:04 +01002453{
Paolo Bonzini63129752021-03-02 14:40:39 -05002454 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002455 int reg, cr;
2456 unsigned long val;
2457 int err;
2458
2459 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002460 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002461
2462 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
Paolo Bonzini63129752021-03-02 14:40:39 -05002463 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002464
2465 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002466 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2467 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2468 else
2469 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002470
2471 err = 0;
2472 if (cr >= 16) { /* mov to cr */
2473 cr -= 16;
Paolo Bonzini63129752021-03-02 14:40:39 -05002474 val = kvm_register_read(vcpu, reg);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002475 trace_kvm_cr_write(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002476 switch (cr) {
2477 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002478 if (!check_selective_cr0_intercepted(vcpu, val))
2479 err = kvm_set_cr0(vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002480 else
2481 return 1;
2482
Andre Przywara7ff76d52010-12-21 11:12:04 +01002483 break;
2484 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002485 err = kvm_set_cr3(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002486 break;
2487 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002488 err = kvm_set_cr4(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002489 break;
2490 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002491 err = kvm_set_cr8(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002492 break;
2493 default:
2494 WARN(1, "unhandled write to CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002495 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002496 return 1;
2497 }
2498 } else { /* mov from cr */
2499 switch (cr) {
2500 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002501 val = kvm_read_cr0(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002502 break;
2503 case 2:
Paolo Bonzini63129752021-03-02 14:40:39 -05002504 val = vcpu->arch.cr2;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002505 break;
2506 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002507 val = kvm_read_cr3(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002508 break;
2509 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002510 val = kvm_read_cr4(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002511 break;
2512 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002513 val = kvm_get_cr8(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002514 break;
2515 default:
2516 WARN(1, "unhandled read from CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002517 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002518 return 1;
2519 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002520 kvm_register_write(vcpu, reg, val);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002521 trace_kvm_cr_read(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002522 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002523 return kvm_complete_insn_gp(vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002524}
2525
Paolo Bonzini63129752021-03-02 14:40:39 -05002526static int cr_trap(struct kvm_vcpu *vcpu)
Tom Lendackyf27ad382020-12-10 11:09:56 -06002527{
Paolo Bonzini63129752021-03-02 14:40:39 -05002528 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002529 unsigned long old_value, new_value;
2530 unsigned int cr;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002531 int ret = 0;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002532
2533 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2534
2535 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2536 switch (cr) {
2537 case 0:
2538 old_value = kvm_read_cr0(vcpu);
2539 svm_set_cr0(vcpu, new_value);
2540
2541 kvm_post_set_cr0(vcpu, old_value, new_value);
2542 break;
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002543 case 4:
2544 old_value = kvm_read_cr4(vcpu);
2545 svm_set_cr4(vcpu, new_value);
2546
2547 kvm_post_set_cr4(vcpu, old_value, new_value);
2548 break;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002549 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002550 ret = kvm_set_cr8(vcpu, new_value);
Tom Lendackyd1949b92020-12-10 11:09:58 -06002551 break;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002552 default:
2553 WARN(1, "unhandled CR%d write trap", cr);
2554 kvm_queue_exception(vcpu, UD_VECTOR);
2555 return 1;
2556 }
2557
Tom Lendackyd1949b92020-12-10 11:09:58 -06002558 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002559}
2560
Paolo Bonzini63129752021-03-02 14:40:39 -05002561static int dr_interception(struct kvm_vcpu *vcpu)
Andre Przywaracae37972010-12-21 11:12:05 +01002562{
Paolo Bonzini63129752021-03-02 14:40:39 -05002563 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002564 int reg, dr;
2565 unsigned long val;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002566 int err = 0;
Andre Przywaracae37972010-12-21 11:12:05 +01002567
Paolo Bonzini63129752021-03-02 14:40:39 -05002568 if (vcpu->guest_debug == 0) {
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002569 /*
2570 * No more DR vmexits; force a reload of the debug registers
2571 * and reenter on this instruction. The next vmexit will
2572 * retrieve the full state of the debug registers.
2573 */
2574 clr_dr_intercepts(svm);
Paolo Bonzini63129752021-03-02 14:40:39 -05002575 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002576 return 1;
2577 }
2578
Andre Przywaracae37972010-12-21 11:12:05 +01002579 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002580 return emulate_on_interception(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002581
2582 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2583 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002584 if (dr >= 16) { /* mov to DRn */
2585 dr -= 16;
Paolo Bonzini63129752021-03-02 14:40:39 -05002586 val = kvm_register_read(vcpu, reg);
2587 err = kvm_set_dr(vcpu, dr, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002588 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05002589 kvm_get_dr(vcpu, dr, &val);
2590 kvm_register_write(vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002591 }
2592
Paolo Bonzini63129752021-03-02 14:40:39 -05002593 return kvm_complete_insn_gp(vcpu, err);
Andre Przywaracae37972010-12-21 11:12:05 +01002594}
2595
Paolo Bonzini63129752021-03-02 14:40:39 -05002596static int cr8_write_interception(struct kvm_vcpu *vcpu)
Joerg Roedel1d075432007-12-06 21:02:25 +01002597{
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002598 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002599
Paolo Bonzini63129752021-03-02 14:40:39 -05002600 u8 cr8_prev = kvm_get_cr8(vcpu);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002601 /* instruction emulation calls kvm_set_cr8() */
Paolo Bonzini63129752021-03-02 14:40:39 -05002602 r = cr_interception(vcpu);
2603 if (lapic_in_kernel(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002604 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002605 if (cr8_prev <= kvm_get_cr8(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002606 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002607 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Joerg Roedel1d075432007-12-06 21:02:25 +01002608 return 0;
2609}
2610
Paolo Bonzini63129752021-03-02 14:40:39 -05002611static int efer_trap(struct kvm_vcpu *vcpu)
Tom Lendacky2985afb2020-12-10 11:09:55 -06002612{
2613 struct msr_data msr_info;
2614 int ret;
2615
2616 /*
2617 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2618 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2619 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2620 * the guest doesn't have X86_FEATURE_SVM.
2621 */
2622 msr_info.host_initiated = false;
2623 msr_info.index = MSR_EFER;
Paolo Bonzini63129752021-03-02 14:40:39 -05002624 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2625 ret = kvm_set_msr_common(vcpu, &msr_info);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002626
Paolo Bonzini63129752021-03-02 14:40:39 -05002627 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002628}
2629
Tom Lendacky801e4592018-02-21 13:39:51 -06002630static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2631{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002632 msr->data = 0;
2633
2634 switch (msr->index) {
2635 case MSR_F10H_DECFG:
2636 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2637 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2638 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02002639 case MSR_IA32_PERF_CAPABILITIES:
2640 return 0;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002641 default:
Peter Xu12bc2132020-06-22 18:04:42 -04002642 return KVM_MSR_RET_INVALID;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002643 }
2644
2645 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002646}
2647
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002648static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002649{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002650 struct vcpu_svm *svm = to_svm(vcpu);
2651
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002652 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04002653 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002654 msr_info->data = svm->vmcb01.ptr->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002656#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002657 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002658 msr_info->data = svm->vmcb01.ptr->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 break;
2660 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002661 msr_info->data = svm->vmcb01.ptr->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
2663 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002664 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 break;
2666 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002667 msr_info->data = svm->vmcb01.ptr->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 break;
2669#endif
2670 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002671 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002672 break;
2673 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002674 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2675 if (guest_cpuid_is_intel(vcpu))
2676 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002677 break;
2678 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002679 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2680 if (guest_cpuid_is_intel(vcpu))
2681 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002683 case MSR_TSC_AUX:
2684 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2685 return 1;
2686 msr_info->data = svm->tsc_aux;
2687 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002688 /*
2689 * Nobody will change the following 5 values in the VMCB so we can
2690 * safely return them on rdmsr. They will always be 0 until LBRV is
2691 * implemented.
2692 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002693 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002694 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002695 break;
2696 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002697 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002698 break;
2699 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002700 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002701 break;
2702 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002703 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002704 break;
2705 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002706 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002707 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002708 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002709 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002710 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002711 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002712 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002713 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002714 case MSR_IA32_SPEC_CTRL:
2715 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002716 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002717 return 1;
2718
Babu Mogerd00b99c2021-02-17 10:56:04 -05002719 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2720 msr_info->data = svm->vmcb->save.spec_ctrl;
2721 else
2722 msr_info->data = svm->spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002723 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002724 case MSR_AMD64_VIRT_SPEC_CTRL:
2725 if (!msr_info->host_initiated &&
2726 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2727 return 1;
2728
2729 msr_info->data = svm->virt_spec_ctrl;
2730 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002731 case MSR_F15H_IC_CFG: {
2732
2733 int family, model;
2734
2735 family = guest_cpuid_family(vcpu);
2736 model = guest_cpuid_model(vcpu);
2737
2738 if (family < 0 || model < 0)
2739 return kvm_get_msr_common(vcpu, msr_info);
2740
2741 msr_info->data = 0;
2742
2743 if (family == 0x15 &&
2744 (model >= 0x2 && model < 0x20))
2745 msr_info->data = 0x1E;
2746 }
2747 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002748 case MSR_F10H_DECFG:
2749 msr_info->data = svm->msr_decfg;
2750 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002752 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002753 }
2754 return 0;
2755}
2756
Tom Lendackyf1c63662020-12-14 10:29:50 -05002757static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2758{
2759 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002760 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->ghcb))
Paolo Bonzini63129752021-03-02 14:40:39 -05002761 return kvm_complete_insn_gp(vcpu, err);
Tom Lendackyf1c63662020-12-14 10:29:50 -05002762
2763 ghcb_set_sw_exit_info_1(svm->ghcb, 1);
2764 ghcb_set_sw_exit_info_2(svm->ghcb,
2765 X86_TRAP_GP |
2766 SVM_EVTINJ_TYPE_EXEPT |
2767 SVM_EVTINJ_VALID);
2768 return 1;
2769}
2770
Joerg Roedel4a810182010-02-24 18:59:15 +01002771static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2772{
2773 struct vcpu_svm *svm = to_svm(vcpu);
2774 int svm_dis, chg_mask;
2775
2776 if (data & ~SVM_VM_CR_VALID_MASK)
2777 return 1;
2778
2779 chg_mask = SVM_VM_CR_VALID_MASK;
2780
2781 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2782 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2783
2784 svm->nested.vm_cr_msr &= ~chg_mask;
2785 svm->nested.vm_cr_msr |= (data & chg_mask);
2786
2787 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2788
2789 /* check for svm_disable while efer.svme is set */
2790 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2791 return 1;
2792
2793 return 0;
2794}
2795
Will Auld8fe8ab42012-11-29 12:42:12 -08002796static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002797{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002798 struct vcpu_svm *svm = to_svm(vcpu);
2799
Will Auld8fe8ab42012-11-29 12:42:12 -08002800 u32 ecx = msr->index;
2801 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002802 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02002803 case MSR_IA32_CR_PAT:
2804 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2805 return 1;
2806 vcpu->arch.pat = data;
Cathy Avery4995a362021-01-13 07:07:52 -05002807 svm->vmcb01.ptr->save.g_pat = data;
2808 if (is_guest_mode(vcpu))
2809 nested_vmcb02_compute_g_pat(svm);
Joerg Roedel06e78522020-06-25 10:03:23 +02002810 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Paolo Bonzini15038e12017-10-26 09:13:27 +02002811 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002812 case MSR_IA32_SPEC_CTRL:
2813 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002814 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002815 return 1;
2816
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002817 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002818 return 1;
2819
Babu Mogerd00b99c2021-02-17 10:56:04 -05002820 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2821 svm->vmcb->save.spec_ctrl = data;
2822 else
2823 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002824 if (!data)
2825 break;
2826
2827 /*
2828 * For non-nested:
2829 * When it's written (to non-zero) for the first time, pass
2830 * it through.
2831 *
2832 * For nested:
2833 * The handling of the MSR bitmap for L2 guests is done in
2834 * nested_svm_vmrun_msrpm.
2835 * We update the L1 MSR bit as well since it will end up
2836 * touching the MSR anyway now.
2837 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002838 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002839 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002840 case MSR_IA32_PRED_CMD:
2841 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002842 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002843 return 1;
2844
2845 if (data & ~PRED_CMD_IBPB)
2846 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002847 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002848 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002849 if (!data)
2850 break;
2851
2852 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002853 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
Ashok Raj15d45072018-02-01 22:59:43 +01002854 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002855 case MSR_AMD64_VIRT_SPEC_CTRL:
2856 if (!msr->host_initiated &&
2857 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2858 return 1;
2859
2860 if (data & ~SPEC_CTRL_SSBD)
2861 return 1;
2862
2863 svm->virt_spec_ctrl = data;
2864 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002865 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002866 svm->vmcb01.ptr->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002868#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002869 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002870 svm->vmcb01.ptr->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 break;
2872 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002873 svm->vmcb01.ptr->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002874 break;
2875 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002876 svm->vmcb01.ptr->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 break;
2878 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002879 svm->vmcb01.ptr->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 break;
2881#endif
2882 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002883 svm->vmcb01.ptr->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 break;
2885 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002886 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2887 /*
2888 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2889 * when we spoof an Intel vendor ID (for cross vendor migration).
2890 * In this case we use this intercept to track the high
2891 * 32 bit part of these msrs to support Intel's
2892 * implementation of SYSENTER/SYSEXIT.
2893 */
2894 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895 break;
2896 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002897 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2898 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002900 case MSR_TSC_AUX:
2901 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2902 return 1;
2903
2904 /*
2905 * This is rare, so we update the MSR here instead of using
2906 * direct_access_msrs. Doing that would require a rdmsr in
2907 * svm_vcpu_put.
2908 */
2909 svm->tsc_aux = data;
2910 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
2911 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002912 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002913 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002914 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2915 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002916 break;
2917 }
2918 if (data & DEBUGCTL_RESERVED_BITS)
2919 return 1;
2920
2921 svm->vmcb->save.dbgctl = data;
Joerg Roedel06e78522020-06-25 10:03:23 +02002922 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002923 if (data & (1ULL<<0))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002924 svm_enable_lbrv(vcpu);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002925 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002926 svm_disable_lbrv(vcpu);
Joerg Roedela2938c82008-02-13 16:30:28 +01002927 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002928 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002929 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002930 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002931 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002932 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002933 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03002934 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002935 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002936 case MSR_F10H_DECFG: {
2937 struct kvm_msr_entry msr_entry;
2938
2939 msr_entry.index = msr->index;
2940 if (svm_get_msr_feature(&msr_entry))
2941 return 1;
2942
2943 /* Check the supported bits */
2944 if (data & ~msr_entry.data)
2945 return 1;
2946
2947 /* Don't allow the guest to change a bit, #GP */
2948 if (!msr->host_initiated && (data ^ msr_entry.data))
2949 return 1;
2950
2951 svm->msr_decfg = data;
2952 break;
2953 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002954 case MSR_IA32_APICBASE:
2955 if (kvm_vcpu_apicv_active(vcpu))
2956 avic_update_vapic_bar(to_svm(vcpu), data);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002957 fallthrough;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002958 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08002959 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002960 }
2961 return 0;
2962}
2963
Paolo Bonzini63129752021-03-02 14:40:39 -05002964static int msr_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965{
Paolo Bonzini63129752021-03-02 14:40:39 -05002966 if (to_svm(vcpu)->vmcb->control.exit_info_1)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002967 return kvm_emulate_wrmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002968 else
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002969 return kvm_emulate_rdmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970}
2971
Paolo Bonzini63129752021-03-02 14:40:39 -05002972static int interrupt_window_interception(struct kvm_vcpu *vcpu)
Dor Laorc1150d82007-01-05 16:36:24 -08002973{
Paolo Bonzini63129752021-03-02 14:40:39 -05002974 kvm_make_request(KVM_REQ_EVENT, vcpu);
2975 svm_clear_vintr(to_svm(vcpu));
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002976
2977 /*
2978 * For AVIC, the only reason to end up here is ExtINTs.
2979 * In this case AVIC was temporarily disabled for
2980 * requesting the IRQ window and we have to re-enable it.
2981 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002982 svm_toggle_avic_for_irq_window(vcpu, true);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002983
Paolo Bonzini63129752021-03-02 14:40:39 -05002984 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002985 return 1;
2986}
2987
Paolo Bonzini63129752021-03-02 14:40:39 -05002988static int pause_interception(struct kvm_vcpu *vcpu)
Mark Langsdorf565d0992009-10-06 14:25:02 -05002989{
Tom Lendackyf1c63662020-12-14 10:29:50 -05002990 bool in_kernel;
2991
2992 /*
2993 * CPL is not made available for an SEV-ES guest, therefore
2994 * vcpu->arch.preempted_in_kernel can never be true. Just
2995 * set in_kernel to false as well.
2996 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002997 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002998
Wanpeng Li830f01b2020-07-31 11:12:21 +08002999 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04003000 grow_ple_window(vcpu);
3001
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003002 kvm_vcpu_on_spin(vcpu, in_kernel);
Sean Christophersonc8781fe2021-02-04 16:57:50 -08003003 return kvm_skip_emulated_instruction(vcpu);
Mark Langsdorf565d0992009-10-06 14:25:02 -05003004}
3005
Paolo Bonzini63129752021-03-02 14:40:39 -05003006static int invpcid_interception(struct kvm_vcpu *vcpu)
Babu Moger4407a792020-09-11 14:29:19 -05003007{
Paolo Bonzini63129752021-03-02 14:40:39 -05003008 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger4407a792020-09-11 14:29:19 -05003009 unsigned long type;
3010 gva_t gva;
3011
3012 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3013 kvm_queue_exception(vcpu, UD_VECTOR);
3014 return 1;
3015 }
3016
3017 /*
3018 * For an INVPCID intercept:
3019 * EXITINFO1 provides the linear address of the memory operand.
3020 * EXITINFO2 provides the contents of the register operand.
3021 */
3022 type = svm->vmcb->control.exit_info_2;
3023 gva = svm->vmcb->control.exit_info_1;
3024
3025 if (type > 3) {
3026 kvm_inject_gp(vcpu, 0);
3027 return 1;
3028 }
3029
3030 return kvm_handle_invpcid(vcpu, type, gva);
3031}
3032
Paolo Bonzini63129752021-03-02 14:40:39 -05003033static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003034 [SVM_EXIT_READ_CR0] = cr_interception,
3035 [SVM_EXIT_READ_CR3] = cr_interception,
3036 [SVM_EXIT_READ_CR4] = cr_interception,
3037 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003038 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003039 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003040 [SVM_EXIT_WRITE_CR3] = cr_interception,
3041 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003042 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003043 [SVM_EXIT_READ_DR0] = dr_interception,
3044 [SVM_EXIT_READ_DR1] = dr_interception,
3045 [SVM_EXIT_READ_DR2] = dr_interception,
3046 [SVM_EXIT_READ_DR3] = dr_interception,
3047 [SVM_EXIT_READ_DR4] = dr_interception,
3048 [SVM_EXIT_READ_DR5] = dr_interception,
3049 [SVM_EXIT_READ_DR6] = dr_interception,
3050 [SVM_EXIT_READ_DR7] = dr_interception,
3051 [SVM_EXIT_WRITE_DR0] = dr_interception,
3052 [SVM_EXIT_WRITE_DR1] = dr_interception,
3053 [SVM_EXIT_WRITE_DR2] = dr_interception,
3054 [SVM_EXIT_WRITE_DR3] = dr_interception,
3055 [SVM_EXIT_WRITE_DR4] = dr_interception,
3056 [SVM_EXIT_WRITE_DR5] = dr_interception,
3057 [SVM_EXIT_WRITE_DR6] = dr_interception,
3058 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003059 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3060 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003061 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003062 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003063 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003064 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02003065 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003066 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003067 [SVM_EXIT_NMI] = nmi_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003068 [SVM_EXIT_SMI] = kvm_emulate_as_nop,
3069 [SVM_EXIT_INIT] = kvm_emulate_as_nop,
Dor Laorc1150d82007-01-05 16:36:24 -08003070 [SVM_EXIT_VINTR] = interrupt_window_interception,
Sean Christopherson32c23c72021-02-04 16:57:49 -08003071 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003072 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003073 [SVM_EXIT_IRET] = iret_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003074 [SVM_EXIT_INVD] = kvm_emulate_invd,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003075 [SVM_EXIT_PAUSE] = pause_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003076 [SVM_EXIT_HLT] = kvm_emulate_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003077 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003078 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003079 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003080 [SVM_EXIT_MSR] = msr_interception,
3081 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003082 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003083 [SVM_EXIT_VMRUN] = vmrun_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003084 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
Alexander Graf55426752008-11-25 20:17:06 +01003085 [SVM_EXIT_VMLOAD] = vmload_interception,
3086 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003087 [SVM_EXIT_STGI] = stgi_interception,
3088 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003089 [SVM_EXIT_SKINIT] = skinit_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003090 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3091 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3092 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
Sean Christopherson92f98952021-02-04 16:57:46 -08003093 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003094 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
Tom Lendacky2985afb2020-12-10 11:09:55 -06003095 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
Tom Lendackyf27ad382020-12-10 11:09:56 -06003096 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
Tom Lendacky5b51cb12020-12-10 11:09:57 -06003097 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
Tom Lendackyd1949b92020-12-10 11:09:58 -06003098 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
Babu Moger4407a792020-09-11 14:29:19 -05003099 [SVM_EXIT_INVPCID] = invpcid_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003100 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06003101 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003102 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3103 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Tom Lendacky291bd202020-12-10 11:09:47 -06003104 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003105};
3106
Joe Perchesae8cc052011-04-24 22:00:50 -07003107static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003108{
3109 struct vcpu_svm *svm = to_svm(vcpu);
3110 struct vmcb_control_area *control = &svm->vmcb->control;
3111 struct vmcb_save_area *save = &svm->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003112 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003113
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003114 if (!dump_invalid_vmcb) {
3115 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3116 return;
3117 }
3118
Joerg Roedel3f10c842010-05-05 16:04:42 +02003119 pr_err("VMCB Control Area:\n");
Babu Moger03bfeeb2020-09-11 14:28:05 -05003120 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3121 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
Babu Moger30abaa882020-09-11 14:28:12 -05003122 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3123 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
Babu Moger9780d512020-09-11 14:28:20 -05003124 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
Babu Mogerc62e2e92020-09-11 14:28:28 -05003125 pr_err("%-20s%08x %08x\n", "intercepts:",
3126 control->intercepts[INTERCEPT_WORD3],
3127 control->intercepts[INTERCEPT_WORD4]);
Joe Perchesae8cc052011-04-24 22:00:50 -07003128 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003129 pr_err("%-20s%d\n", "pause filter threshold:",
3130 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003131 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3132 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3133 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3134 pr_err("%-20s%d\n", "asid:", control->asid);
3135 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3136 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3137 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3138 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3139 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3140 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3141 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3142 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3143 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3144 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3145 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003146 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Tom Lendacky291bd202020-12-10 11:09:47 -06003147 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
Joe Perchesae8cc052011-04-24 22:00:50 -07003148 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3149 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003150 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003151 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003152 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3153 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3154 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Tom Lendacky376c6d22020-12-10 11:10:06 -06003155 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003156 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003157 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3158 "es:",
3159 save->es.selector, save->es.attrib,
3160 save->es.limit, save->es.base);
3161 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3162 "cs:",
3163 save->cs.selector, save->cs.attrib,
3164 save->cs.limit, save->cs.base);
3165 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3166 "ss:",
3167 save->ss.selector, save->ss.attrib,
3168 save->ss.limit, save->ss.base);
3169 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3170 "ds:",
3171 save->ds.selector, save->ds.attrib,
3172 save->ds.limit, save->ds.base);
3173 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3174 "fs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003175 save01->fs.selector, save01->fs.attrib,
3176 save01->fs.limit, save01->fs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003177 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3178 "gs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003179 save01->gs.selector, save01->gs.attrib,
3180 save01->gs.limit, save01->gs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003181 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3182 "gdtr:",
3183 save->gdtr.selector, save->gdtr.attrib,
3184 save->gdtr.limit, save->gdtr.base);
3185 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3186 "ldtr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003187 save01->ldtr.selector, save01->ldtr.attrib,
3188 save01->ldtr.limit, save01->ldtr.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003189 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3190 "idtr:",
3191 save->idtr.selector, save->idtr.attrib,
3192 save->idtr.limit, save->idtr.base);
3193 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3194 "tr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003195 save01->tr.selector, save01->tr.attrib,
3196 save01->tr.limit, save01->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003197 pr_err("cpl: %d efer: %016llx\n",
3198 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003199 pr_err("%-15s %016llx %-13s %016llx\n",
3200 "cr0:", save->cr0, "cr2:", save->cr2);
3201 pr_err("%-15s %016llx %-13s %016llx\n",
3202 "cr3:", save->cr3, "cr4:", save->cr4);
3203 pr_err("%-15s %016llx %-13s %016llx\n",
3204 "dr6:", save->dr6, "dr7:", save->dr7);
3205 pr_err("%-15s %016llx %-13s %016llx\n",
3206 "rip:", save->rip, "rflags:", save->rflags);
3207 pr_err("%-15s %016llx %-13s %016llx\n",
3208 "rsp:", save->rsp, "rax:", save->rax);
3209 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003210 "star:", save01->star, "lstar:", save01->lstar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003211 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003212 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
Joe Perchesae8cc052011-04-24 22:00:50 -07003213 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003214 "kernel_gs_base:", save01->kernel_gs_base,
3215 "sysenter_cs:", save01->sysenter_cs);
Joe Perchesae8cc052011-04-24 22:00:50 -07003216 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003217 "sysenter_esp:", save01->sysenter_esp,
3218 "sysenter_eip:", save01->sysenter_eip);
Joe Perchesae8cc052011-04-24 22:00:50 -07003219 pr_err("%-15s %016llx %-13s %016llx\n",
3220 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3221 pr_err("%-15s %016llx %-13s %016llx\n",
3222 "br_from:", save->br_from, "br_to:", save->br_to);
3223 pr_err("%-15s %016llx %-13s %016llx\n",
3224 "excp_from:", save->last_excp_from,
3225 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003226}
3227
Tom Lendackye9093fd42020-12-10 11:09:46 -06003228static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3229{
3230 if (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3231 svm_exit_handlers[exit_code])
3232 return 0;
3233
3234 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3235 dump_vmcb(vcpu);
3236 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3237 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3238 vcpu->run->internal.ndata = 2;
3239 vcpu->run->internal.data[0] = exit_code;
3240 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3241
3242 return -EINVAL;
3243}
3244
Paolo Bonzini63129752021-03-02 14:40:39 -05003245int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
Tom Lendackye9093fd42020-12-10 11:09:46 -06003246{
Paolo Bonzini63129752021-03-02 14:40:39 -05003247 if (svm_handle_invalid_exit(vcpu, exit_code))
Tom Lendackye9093fd42020-12-10 11:09:46 -06003248 return 0;
3249
3250#ifdef CONFIG_RETPOLINE
3251 if (exit_code == SVM_EXIT_MSR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003252 return msr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003253 else if (exit_code == SVM_EXIT_VINTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003254 return interrupt_window_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003255 else if (exit_code == SVM_EXIT_INTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003256 return intr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003257 else if (exit_code == SVM_EXIT_HLT)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003258 return kvm_emulate_halt(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003259 else if (exit_code == SVM_EXIT_NPF)
Paolo Bonzini63129752021-03-02 14:40:39 -05003260 return npf_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003261#endif
Paolo Bonzini63129752021-03-02 14:40:39 -05003262 return svm_exit_handlers[exit_code](vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003263}
3264
Sean Christopherson235ba742020-09-23 13:13:46 -07003265static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3266 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02003267{
3268 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3269
3270 *info1 = control->exit_info_1;
3271 *info2 = control->exit_info_2;
Sean Christopherson235ba742020-09-23 13:13:46 -07003272 *intr_info = control->exit_int_info;
3273 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3274 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3275 *error_code = control->exit_int_info_err;
3276 else
3277 *error_code = 0;
Avi Kivity586f9602010-11-18 13:09:54 +02003278}
3279
Wanpeng Li404d5d72020-04-28 14:23:25 +08003280static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003281{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003282 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003283 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003284 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003285
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003286 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3287
Tom Lendackyf1c63662020-12-14 10:29:50 -05003288 /* SEV-ES guests must use the CR write traps to track CR registers. */
3289 if (!sev_es_guest(vcpu->kvm)) {
3290 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3291 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3292 if (npt_enabled)
3293 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3294 }
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003295
Joerg Roedel20307532010-11-29 17:51:48 +01003296 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003297 int vmexit;
3298
Sean Christophersoncc167bd2020-09-23 13:13:48 -07003299 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003300
Joerg Roedel410e4d52009-08-07 11:49:44 +02003301 vmexit = nested_svm_exit_special(svm);
3302
3303 if (vmexit == NESTED_EXIT_CONTINUE)
3304 vmexit = nested_svm_exit_handled(svm);
3305
3306 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003307 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003308 }
3309
Avi Kivity04d2cc72007-09-10 18:10:54 +03003310 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3311 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3312 kvm_run->fail_entry.hardware_entry_failure_reason
3313 = svm->vmcb->control.exit_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07003314 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003315 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003316 return 0;
3317 }
3318
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003319 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003320 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003321 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3322 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003323 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003325 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003326 exit_code);
3327
Wanpeng Li404d5d72020-04-28 14:23:25 +08003328 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003329 return 1;
Wanpeng Li404d5d72020-04-28 14:23:25 +08003330
Paolo Bonzini63129752021-03-02 14:40:39 -05003331 return svm_invoke_exit_handler(vcpu, exit_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332}
3333
3334static void reload_tss(struct kvm_vcpu *vcpu)
3335{
Jim Mattson73cd6e52020-06-03 16:56:18 -07003336 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003337
Tejun Heo0fe1e002009-10-29 22:34:14 +09003338 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003339 load_TR_desc();
3340}
3341
Paolo Bonzini63129752021-03-02 14:40:39 -05003342static void pre_svm_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003343{
Paolo Bonzini63129752021-03-02 14:40:39 -05003344 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3345 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003346
Cathy Averyaf18fa72021-01-12 11:43:12 -05003347 /*
3348 * If the previous vmrun of the vmcb occurred on
3349 * a different physical cpu then we must mark the vmcb dirty.
Cathy Avery193015a2021-01-12 11:43:13 -05003350 * and assign a new asid.
3351 */
Cathy Averyaf18fa72021-01-12 11:43:12 -05003352
Paolo Bonzini63129752021-03-02 14:40:39 -05003353 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
Cathy Avery193015a2021-01-12 11:43:13 -05003354 svm->current_vmcb->asid_generation = 0;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003355 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05003356 svm->current_vmcb->cpu = vcpu->cpu;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003357 }
3358
Paolo Bonzini63129752021-03-02 14:40:39 -05003359 if (sev_guest(vcpu->kvm))
3360 return pre_sev_run(svm, vcpu->cpu);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003361
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003362 /* FIXME: handle wraparound of asid_generation */
Cathy Avery193015a2021-01-12 11:43:13 -05003363 if (svm->current_vmcb->asid_generation != sd->asid_generation)
Tejun Heo0fe1e002009-10-29 22:34:14 +09003364 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003365}
3366
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003367static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3368{
3369 struct vcpu_svm *svm = to_svm(vcpu);
3370
3371 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3372 vcpu->arch.hflags |= HF_NMI_MASK;
Paolo Bonzini63129752021-03-02 14:40:39 -05003373 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003374 svm_set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003375 ++vcpu->stat.nmi_injections;
3376}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003378static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003379{
3380 struct vcpu_svm *svm = to_svm(vcpu);
3381
Joerg Roedel2af91942009-08-07 11:49:28 +02003382 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003383
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003384 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3385 ++vcpu->stat.irq_injections;
3386
Alexander Graf219b65d2009-06-15 15:21:25 +02003387 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3388 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003389}
3390
Jason Baronb6a7cc32021-01-14 22:27:54 -05003391static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003392{
3393 struct vcpu_svm *svm = to_svm(vcpu);
3394
Tom Lendackyf1c63662020-12-14 10:29:50 -05003395 /*
3396 * SEV-ES guests must always keep the CR intercepts cleared. CR
3397 * tracking is done using the CR write traps.
3398 */
3399 if (sev_es_guest(vcpu->kvm))
3400 return;
3401
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003402 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003403 return;
3404
Babu Moger830bd712020-09-11 14:28:50 -05003405 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
Radim Krčmář596f3142014-03-11 19:11:18 +01003406
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003407 if (irr == -1)
3408 return;
3409
3410 if (tpr >= irr)
Babu Moger830bd712020-09-11 14:28:50 -05003411 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003412}
3413
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003414bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003415{
3416 struct vcpu_svm *svm = to_svm(vcpu);
3417 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson88c604b2020-04-22 19:25:41 -07003418 bool ret;
Cathy Avery9c3d3702020-04-14 16:11:06 -04003419
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003420 if (!gif_set(svm))
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -04003421 return true;
3422
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003423 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3424 return false;
3425
3426 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05003427 (vcpu->arch.hflags & HF_NMI_MASK);
Joerg Roedel924584c2010-04-22 12:33:07 +02003428
3429 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003430}
3431
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003432static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003433{
3434 struct vcpu_svm *svm = to_svm(vcpu);
3435 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003436 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003437
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003438 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3439 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003440 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003441
3442 return !svm_nmi_blocked(vcpu);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003443}
3444
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003445static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3446{
Paolo Bonzini63129752021-03-02 14:40:39 -05003447 return !!(vcpu->arch.hflags & HF_NMI_MASK);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003448}
3449
3450static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3451{
3452 struct vcpu_svm *svm = to_svm(vcpu);
3453
3454 if (masked) {
Paolo Bonzini63129752021-03-02 14:40:39 -05003455 vcpu->arch.hflags |= HF_NMI_MASK;
3456 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003457 svm_set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003458 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05003459 vcpu->arch.hflags &= ~HF_NMI_MASK;
3460 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003461 svm_clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003462 }
3463}
3464
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003465bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
Gleb Natapov78646122009-03-23 12:12:11 +02003466{
3467 struct vcpu_svm *svm = to_svm(vcpu);
3468 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003469
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003470 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003471 return true;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003472
Paolo Bonzini63129752021-03-02 14:40:39 -05003473 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyf1c63662020-12-14 10:29:50 -05003474 /*
3475 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3476 * bit to determine the state of the IF flag.
3477 */
3478 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3479 return true;
3480 } else if (is_guest_mode(vcpu)) {
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003481 /* As long as interrupts are being delivered... */
Paolo Bonzinie9fd7612020-05-13 13:28:23 -04003482 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
Cathy Avery4995a362021-01-13 07:07:52 -05003483 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003484 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3485 return true;
3486
3487 /* ... vmexits aren't blocked by the interrupt shadow */
3488 if (nested_exit_on_intr(svm))
3489 return false;
3490 } else {
3491 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3492 return true;
3493 }
3494
3495 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003496}
3497
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003498static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003499{
3500 struct vcpu_svm *svm = to_svm(vcpu);
3501 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003502 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003503
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003504 /*
3505 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3506 * e.g. if the IRQ arrived asynchronously after checking nested events.
3507 */
3508 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003509 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003510
3511 return !svm_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02003512}
3513
Jason Baronb6a7cc32021-01-14 22:27:54 -05003514static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003515{
Alexander Graf219b65d2009-06-15 15:21:25 +02003516 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003517
Joerg Roedele0231712010-02-24 18:59:10 +01003518 /*
3519 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3520 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3521 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003522 * we'll get the vintr intercept. However, if the vGIF feature is
3523 * enabled, the STGI interception will not occur. Enable the irq
3524 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003525 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003526 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003527 /*
3528 * IRQ window is not needed when AVIC is enabled,
3529 * unless we have pending ExtINT since it cannot be injected
3530 * via AVIC. In such case, we need to temporarily disable AVIC,
3531 * and fallback to injecting IRQ via V_IRQ.
3532 */
3533 svm_toggle_avic_for_irq_window(vcpu, false);
Alexander Graf219b65d2009-06-15 15:21:25 +02003534 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003535 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003536}
3537
Jason Baronb6a7cc32021-01-14 22:27:54 -05003538static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003539{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003540 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003541
Paolo Bonzini63129752021-03-02 14:40:39 -05003542 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003543 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003544
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003545 if (!gif_set(svm)) {
3546 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02003547 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003548 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003549 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003550
Joerg Roedele0231712010-02-24 18:59:10 +01003551 /*
3552 * Something prevents NMI from been injected. Single step over possible
3553 * problem (IRET or exception injection or interrupt shadow)
3554 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003555 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003556 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003557 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003558}
3559
Izik Eiduscbc94022007-10-25 00:29:55 +02003560static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3561{
3562 return 0;
3563}
3564
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003565static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3566{
3567 return 0;
3568}
3569
Sean Christophersonf55ac302020-03-20 14:28:12 -07003570void svm_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003571{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003572 struct vcpu_svm *svm = to_svm(vcpu);
3573
Sean Christopherson4a41e432020-03-20 14:28:17 -07003574 /*
3575 * Flush only the current ASID even if the TLB flush was invoked via
3576 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3577 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3578 * unconditionally does a TLB flush on both nested VM-Enter and nested
3579 * VM-Exit (via kvm_mmu_reset_context()).
3580 */
Joerg Roedel38e5e922010-12-03 15:25:16 +01003581 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3582 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3583 else
Cathy Avery193015a2021-01-12 11:43:13 -05003584 svm->current_vmcb->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003585}
3586
Junaid Shahidfaff8752018-06-29 13:10:05 -07003587static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3588{
3589 struct vcpu_svm *svm = to_svm(vcpu);
3590
3591 invlpga(gva, svm->vmcb->control.asid);
3592}
3593
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003594static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3595{
3596 struct vcpu_svm *svm = to_svm(vcpu);
3597
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003598 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003599 return;
3600
Babu Moger830bd712020-09-11 14:28:50 -05003601 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003602 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003603 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003604 }
3605}
3606
Joerg Roedel649d6862008-04-16 16:51:15 +02003607static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3608{
3609 struct vcpu_svm *svm = to_svm(vcpu);
3610 u64 cr8;
3611
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003612 if (nested_svm_virtualize_tpr(vcpu) ||
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003613 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003614 return;
3615
Joerg Roedel649d6862008-04-16 16:51:15 +02003616 cr8 = kvm_get_cr8(vcpu);
3617 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3618 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3619}
3620
Paolo Bonzini63129752021-03-02 14:40:39 -05003621static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003622{
Paolo Bonzini63129752021-03-02 14:40:39 -05003623 struct vcpu_svm *svm = to_svm(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003624 u8 vector;
3625 int type;
3626 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003627 unsigned int3_injected = svm->int3_injected;
3628
3629 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003630
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003631 /*
3632 * If we've made progress since setting HF_IRET_MASK, we've
3633 * executed an IRET and can allow NMI injection.
3634 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003635 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3636 (sev_es_guest(vcpu->kvm) ||
3637 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3638 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3639 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003640 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003641
Paolo Bonzini63129752021-03-02 14:40:39 -05003642 vcpu->arch.nmi_injected = false;
3643 kvm_clear_exception_queue(vcpu);
3644 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003645
3646 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3647 return;
3648
Paolo Bonzini63129752021-03-02 14:40:39 -05003649 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003650
Gleb Natapov9222be12009-04-23 17:14:37 +03003651 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3652 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3653
3654 switch (type) {
3655 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05003656 vcpu->arch.nmi_injected = true;
Gleb Natapov9222be12009-04-23 17:14:37 +03003657 break;
3658 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003659 /*
Tom Lendackyf1c63662020-12-14 10:29:50 -05003660 * Never re-inject a #VC exception.
3661 */
3662 if (vector == X86_TRAP_VC)
3663 break;
3664
3665 /*
Jan Kiszka66b71382010-02-23 17:47:56 +01003666 * In case of software exceptions, do not reinject the vector,
3667 * but re-execute the instruction instead. Rewind RIP first
3668 * if we emulated INT3 before.
3669 */
3670 if (kvm_exception_is_soft(vector)) {
3671 if (vector == BP_VECTOR && int3_injected &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003672 kvm_is_linear_rip(vcpu, svm->int3_rip))
3673 kvm_rip_write(vcpu,
3674 kvm_rip_read(vcpu) - int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003675 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003676 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003677 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3678 u32 err = svm->vmcb->control.exit_int_info_err;
Paolo Bonzini63129752021-03-02 14:40:39 -05003679 kvm_requeue_exception_e(vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003680
3681 } else
Paolo Bonzini63129752021-03-02 14:40:39 -05003682 kvm_requeue_exception(vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003683 break;
3684 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05003685 kvm_queue_interrupt(vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003686 break;
3687 default:
3688 break;
3689 }
3690}
3691
Avi Kivityb463a6f2010-07-20 15:06:17 +03003692static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3693{
3694 struct vcpu_svm *svm = to_svm(vcpu);
3695 struct vmcb_control_area *control = &svm->vmcb->control;
3696
3697 control->exit_int_info = control->event_inj;
3698 control->exit_int_info_err = control->event_inj_err;
3699 control->event_inj = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003700 svm_complete_interrupts(vcpu);
Avi Kivityb463a6f2010-07-20 15:06:17 +03003701}
3702
Wanpeng Li404d5d72020-04-28 14:23:25 +08003703static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003704{
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003705 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003706 to_svm(vcpu)->vmcb->control.exit_info_1)
3707 return handle_fastpath_set_msr_irqoff(vcpu);
3708
3709 return EXIT_FASTPATH_NONE;
3710}
3711
Paolo Bonzini63129752021-03-02 14:40:39 -05003712static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
Thomas Gleixner135961e2020-07-08 21:51:58 +02003713{
Paolo Bonzini63129752021-03-02 14:40:39 -05003714 struct vcpu_svm *svm = to_svm(vcpu);
3715
Thomas Gleixner135961e2020-07-08 21:51:58 +02003716 /*
3717 * VMENTER enables interrupts (host state), but the kernel state is
3718 * interrupts disabled when this is invoked. Also tell RCU about
3719 * it. This is the same logic as for exit_to_user_mode().
3720 *
3721 * This ensures that e.g. latency analysis on the host observes
3722 * guest mode as interrupt enabled.
3723 *
3724 * guest_enter_irqoff() informs context tracking about the
3725 * transition to guest mode and if enabled adjusts RCU state
3726 * accordingly.
3727 */
3728 instrumentation_begin();
3729 trace_hardirqs_on_prepare();
3730 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
3731 instrumentation_end();
3732
3733 guest_enter_irqoff();
3734 lockdep_hardirqs_on(CALLER_ADDR0);
3735
Paolo Bonzini63129752021-03-02 14:40:39 -05003736 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003737 __svm_sev_es_vcpu_run(svm->vmcb_pa);
3738 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06003739 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3740
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003741 vmload(svm->vmcb01.pa);
Paolo Bonzini63129752021-03-02 14:40:39 -05003742 __svm_vcpu_run(svm->vmcb_pa, (unsigned long *)&vcpu->arch.regs);
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003743 vmsave(svm->vmcb01.pa);
Thomas Gleixner135961e2020-07-08 21:51:58 +02003744
Michael Rothe79b91b2021-02-02 13:01:24 -06003745 vmload(__sme_page_pa(sd->save_area));
Tom Lendacky16809ec2020-12-10 11:10:08 -06003746 }
Thomas Gleixner135961e2020-07-08 21:51:58 +02003747
3748 /*
3749 * VMEXIT disables interrupts (host state), but tracing and lockdep
3750 * have them in state 'on' as recorded before entering guest mode.
3751 * Same as enter_from_user_mode().
3752 *
3753 * guest_exit_irqoff() restores host context and reinstates RCU if
3754 * enabled and required.
3755 *
3756 * This needs to be done before the below as native_read_msr()
3757 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
3758 * into world and some more.
3759 */
3760 lockdep_hardirqs_off(CALLER_ADDR0);
3761 guest_exit_irqoff();
3762
3763 instrumentation_begin();
3764 trace_hardirqs_off_finish();
3765 instrumentation_end();
3766}
3767
Qian Caib95273f2020-04-15 11:37:09 -04003768static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003769{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003770 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003771
Lorenzo Bresciad95df952020-12-23 14:45:07 +00003772 trace_kvm_entry(vcpu);
3773
Joerg Roedel2041a062010-04-22 12:33:08 +02003774 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3775 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3776 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3777
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003778 /*
Ladi Proseka12713c2017-06-21 09:07:00 +02003779 * Disable singlestep if we're injecting an interrupt/exception.
3780 * We don't want our modified rflags to be pushed on the stack where
3781 * we might not be able to easily reset them if we disabled NMI
3782 * singlestep later.
3783 */
3784 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3785 /*
3786 * Event injection happens before external interrupts cause a
3787 * vmexit and interrupts are disabled here, so smp_send_reschedule
3788 * is enough to force an immediate vmexit.
3789 */
3790 disable_nmi_singlestep(svm);
3791 smp_send_reschedule(vcpu->cpu);
3792 }
3793
Paolo Bonzini63129752021-03-02 14:40:39 -05003794 pre_svm_run(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003795
Joerg Roedel649d6862008-04-16 16:51:15 +02003796 sync_lapic_to_cr8(vcpu);
3797
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04003798 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3799 svm->vmcb->control.asid = svm->asid;
3800 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3801 }
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003802 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003803
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003804 /*
3805 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3806 * of a #DB.
3807 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003808 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003809 svm_set_dr6(svm, vcpu->arch.dr6);
3810 else
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08003811 svm_set_dr6(svm, DR6_ACTIVE_LOW);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003812
Avi Kivity04d2cc72007-09-10 18:10:54 +03003813 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003814 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003815
Wanpeng Li010fd372020-09-10 17:50:41 +08003816 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003817
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003818 /*
3819 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3820 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3821 * is no need to worry about the conditional branch over the wrmsr
3822 * being speculatively taken.
3823 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003824 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3825 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003826
Paolo Bonzini63129752021-03-02 14:40:39 -05003827 svm_vcpu_enter_exit(vcpu);
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003828
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003829 /*
3830 * We do not use IBRS in the kernel. If this vCPU has used the
3831 * SPEC_CTRL MSR it may have left it on; save the value and
3832 * turn it off. This is much more efficient than blindly adding
3833 * it to the atomic save/restore list. Especially as the former
3834 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3835 *
3836 * For non-nested case:
3837 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3838 * save it.
3839 *
3840 * For nested case:
3841 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3842 * save it.
3843 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003844 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3845 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003846 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003847
Paolo Bonzini63129752021-03-02 14:40:39 -05003848 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky16809ec2020-12-10 11:10:08 -06003849 reload_tss(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003850
Babu Mogerd00b99c2021-02-17 10:56:04 -05003851 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3852 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003853
Paolo Bonzini63129752021-03-02 14:40:39 -05003854 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003855 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3856 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3857 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3858 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3859 }
Avi Kivity13c34e02010-10-21 12:20:31 +02003860
Joerg Roedel3781c012011-01-14 16:45:02 +01003861 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003862 kvm_before_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003863
Aaron Lewis139a12c2019-10-21 16:30:25 -07003864 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003865 stgi();
3866
3867 /* Any pending NMI will happen here */
3868
3869 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003870 kvm_after_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003871
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003872 sync_cr8_to_lapic(vcpu);
3873
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003874 svm->next_rip = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003875 if (is_guest_mode(vcpu)) {
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05003876 nested_sync_control_from_vmcb02(svm);
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -04003877 svm->nested.nested_run_pending = 0;
3878 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003879
Joerg Roedel38e5e922010-12-03 15:25:16 +01003880 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Wanpeng Lie42c6822020-09-12 02:16:39 -04003881 vmcb_mark_all_clean(svm->vmcb);
Joerg Roedel38e5e922010-12-03 15:25:16 +01003882
Gleb Natapov631bc482010-10-14 11:22:52 +02003883 /* if exit due to PF check for async PF */
3884 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003885 vcpu->arch.apf.host_apf_flags =
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003886 kvm_read_and_reset_apf_flags();
Gleb Natapov631bc482010-10-14 11:22:52 +02003887
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003888 if (npt_enabled) {
3889 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3890 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3891 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003892
3893 /*
3894 * We need to handle MC intercepts here before the vcpu has a chance to
3895 * change the physical cpu
3896 */
3897 if (unlikely(svm->vmcb->control.exit_code ==
3898 SVM_EXIT_EXCP_BASE + MC_VECTOR))
Paolo Bonzini63129752021-03-02 14:40:39 -05003899 svm_handle_mce(vcpu);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003900
Paolo Bonzini63129752021-03-02 14:40:39 -05003901 svm_complete_interrupts(vcpu);
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003902
3903 if (is_guest_mode(vcpu))
3904 return EXIT_FASTPATH_NONE;
3905
3906 return svm_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003907}
3908
Sean Christophersone83bc092021-03-05 10:31:13 -08003909static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
Sean Christopherson2a40b902020-07-15 20:41:18 -07003910 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003912 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003913 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003914
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003915 if (npt_enabled) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003916 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
Joerg Roedel06e78522020-06-25 10:03:23 +02003917 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003918
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003919 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
Paolo Bonzini978ce582020-05-20 08:37:37 -04003920 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3921 return;
3922 cr3 = vcpu->arch.cr3;
Sean Christophersone83bc092021-03-05 10:31:13 -08003923 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003924 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
Sean Christophersone83bc092021-03-05 10:31:13 -08003925 } else {
3926 /* PCID in the guest should be impossible with a 32-bit MMU. */
3927 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3928 cr3 = root_hpa;
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003929 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003930
Paolo Bonzini978ce582020-05-20 08:37:37 -04003931 svm->vmcb->save.cr3 = cr3;
Joerg Roedel06e78522020-06-25 10:03:23 +02003932 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003933}
3934
Avi Kivity6aa8b732006-12-10 02:21:36 -08003935static int is_disabled(void)
3936{
Joerg Roedel6031a612007-06-22 12:29:50 +03003937 u64 vm_cr;
3938
3939 rdmsrl(MSR_VM_CR, vm_cr);
3940 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3941 return 1;
3942
Avi Kivity6aa8b732006-12-10 02:21:36 -08003943 return 0;
3944}
3945
Ingo Molnar102d8322007-02-19 14:37:47 +02003946static void
3947svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3948{
3949 /*
3950 * Patch in the VMMCALL instruction:
3951 */
3952 hypercall[0] = 0x0f;
3953 hypercall[1] = 0x01;
3954 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003955}
3956
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003957static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003958{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003959 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003960}
3961
Avi Kivity774ead32007-12-26 13:57:04 +02003962static bool svm_cpu_has_accelerated_tpr(void)
3963{
3964 return false;
3965}
3966
Tom Lendacky57194552020-12-10 11:10:00 -06003967/*
3968 * The kvm parameter can be NULL (module initialization, or invocation before
3969 * VM creation). Be sure to check the kvm parameter before using it.
3970 */
3971static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003972{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003973 switch (index) {
3974 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02003975 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003976 return false;
Tom Lendacky57194552020-12-10 11:10:00 -06003977 case MSR_IA32_SMBASE:
3978 /* SEV-ES guests do not support SMM, so report false */
3979 if (kvm && sev_es_guest(kvm))
3980 return false;
3981 break;
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003982 default:
3983 break;
3984 }
3985
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003986 return true;
3987}
3988
Paolo Bonzinifc07e762015-10-01 13:20:22 +02003989static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3990{
3991 return 0;
3992}
3993
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08003994static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08003995{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003996 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger96308b02020-11-12 16:18:03 -06003997 struct kvm_cpuid_entry2 *best;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003998
Aaron Lewis72041602019-10-21 16:30:20 -07003999 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08004000 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07004001 boot_cpu_has(X86_FEATURE_XSAVES);
4002
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004003 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08004004 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
Paolo Bonzini63129752021-03-02 14:40:39 -05004005 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004006
Babu Moger4407a792020-09-11 14:29:19 -05004007 /* Check again if INVPCID interception if required */
4008 svm_check_invpcid(svm);
4009
Babu Moger96308b02020-11-12 16:18:03 -06004010 /* For sev guests, the memory encryption bit is not reserved in CR3. */
4011 if (sev_guest(vcpu->kvm)) {
4012 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
4013 if (best)
Sean Christophersonca29e142021-02-03 16:01:12 -08004014 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
Babu Moger96308b02020-11-12 16:18:03 -06004015 }
4016
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004017 if (kvm_vcpu_apicv_active(vcpu)) {
4018 /*
4019 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
4020 * is exposed to the guest, disable AVIC.
4021 */
4022 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
4023 kvm_request_apicv_update(vcpu->kvm, false,
4024 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004025
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004026 /*
4027 * Currently, AVIC does not work with nested virtualization.
4028 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
4029 */
4030 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4031 kvm_request_apicv_update(vcpu->kvm, false,
4032 APICV_INHIBIT_REASON_NESTED);
4033 }
Suravee Suthikulpanit9a0bf052019-11-14 14:15:14 -06004034
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004035 if (guest_cpuid_is_intel(vcpu)) {
4036 /*
4037 * We must intercept SYSENTER_EIP and SYSENTER_ESP
4038 * accesses because the processor only stores 32 bits.
4039 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
4040 */
4041 svm_set_intercept(svm, INTERCEPT_VMLOAD);
4042 svm_set_intercept(svm, INTERCEPT_VMSAVE);
4043 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4044
4045 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
4046 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
4047 } else {
4048 /*
4049 * If hardware supports Virtual VMLOAD VMSAVE then enable it
4050 * in VMCB and clear intercepts to avoid #VMEXIT.
4051 */
4052 if (vls) {
4053 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
4054 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
4055 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4056 }
4057 /* No need to intercept these MSRs */
4058 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
4059 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
4060 }
Sheng Yang0e851882009-12-18 16:48:46 +08004061}
4062
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004063static bool svm_has_wbinvd_exit(void)
4064{
4065 return true;
4066}
4067
Joerg Roedel80612522011-04-04 12:39:33 +02004068#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004069 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004070#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004071 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004072#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004073 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004074
Mathias Krause09941fb2012-08-30 01:30:20 +02004075static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004076 u32 exit_code;
4077 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004078} x86_intercept_map[] = {
4079 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4080 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4081 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4082 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4083 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004084 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4085 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004086 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4087 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4088 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4089 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4090 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4091 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4092 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4093 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004094 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4095 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4096 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4097 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4098 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4099 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4100 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4101 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004102 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4103 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4104 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004105 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4106 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4107 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4108 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4109 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4110 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4111 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4112 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4113 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004114 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4115 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4116 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4117 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4118 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4119 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4120 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004121 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4122 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4123 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4124 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004125 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004126};
4127
Joerg Roedel80612522011-04-04 12:39:33 +02004128#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004129#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004130#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004131
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004132static int svm_check_intercept(struct kvm_vcpu *vcpu,
4133 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08004134 enum x86_intercept_stage stage,
4135 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004136{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004137 struct vcpu_svm *svm = to_svm(vcpu);
4138 int vmexit, ret = X86EMUL_CONTINUE;
4139 struct __x86_intercept icpt_info;
4140 struct vmcb *vmcb = svm->vmcb;
4141
4142 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4143 goto out;
4144
4145 icpt_info = x86_intercept_map[info->intercept];
4146
Avi Kivity40e19b52011-04-21 12:35:41 +03004147 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004148 goto out;
4149
4150 switch (icpt_info.exit_code) {
4151 case SVM_EXIT_READ_CR0:
4152 if (info->intercept == x86_intercept_cr_read)
4153 icpt_info.exit_code += info->modrm_reg;
4154 break;
4155 case SVM_EXIT_WRITE_CR0: {
4156 unsigned long cr0, val;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004157
4158 if (info->intercept == x86_intercept_cr_write)
4159 icpt_info.exit_code += info->modrm_reg;
4160
Jan Kiszka62baf442014-06-29 21:55:53 +02004161 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4162 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004163 break;
4164
Babu Mogerc62e2e92020-09-11 14:28:28 -05004165 if (!(vmcb_is_intercept(&svm->nested.ctl,
4166 INTERCEPT_SELECTIVE_CR0)))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004167 break;
4168
4169 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4170 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4171
4172 if (info->intercept == x86_intercept_lmsw) {
4173 cr0 &= 0xfUL;
4174 val &= 0xfUL;
4175 /* lmsw can't clear PE - catch this here */
4176 if (cr0 & X86_CR0_PE)
4177 val |= X86_CR0_PE;
4178 }
4179
4180 if (cr0 ^ val)
4181 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4182
4183 break;
4184 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004185 case SVM_EXIT_READ_DR0:
4186 case SVM_EXIT_WRITE_DR0:
4187 icpt_info.exit_code += info->modrm_reg;
4188 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004189 case SVM_EXIT_MSR:
4190 if (info->intercept == x86_intercept_wrmsr)
4191 vmcb->control.exit_info_1 = 1;
4192 else
4193 vmcb->control.exit_info_1 = 0;
4194 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004195 case SVM_EXIT_PAUSE:
4196 /*
4197 * We get this for NOP only, but pause
4198 * is rep not, check this here
4199 */
4200 if (info->rep_prefix != REPE_PREFIX)
4201 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004202 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004203 case SVM_EXIT_IOIO: {
4204 u64 exit_info;
4205 u32 bytes;
4206
Joerg Roedelf6511932011-04-04 12:39:35 +02004207 if (info->intercept == x86_intercept_in ||
4208 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004209 exit_info = ((info->src_val & 0xffff) << 16) |
4210 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004211 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004212 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004213 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004214 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004215 }
4216
4217 if (info->intercept == x86_intercept_outs ||
4218 info->intercept == x86_intercept_ins)
4219 exit_info |= SVM_IOIO_STR_MASK;
4220
4221 if (info->rep_prefix)
4222 exit_info |= SVM_IOIO_REP_MASK;
4223
4224 bytes = min(bytes, 4u);
4225
4226 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4227
4228 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4229
4230 vmcb->control.exit_info_1 = exit_info;
4231 vmcb->control.exit_info_2 = info->next_rip;
4232
4233 break;
4234 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004235 default:
4236 break;
4237 }
4238
Bandan Dasf1047652015-06-11 02:05:33 -04004239 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4240 if (static_cpu_has(X86_FEATURE_NRIPS))
4241 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004242 vmcb->control.exit_code = icpt_info.exit_code;
4243 vmexit = nested_svm_exit_handled(svm);
4244
4245 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4246 : X86EMUL_CONTINUE;
4247
4248out:
4249 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004250}
4251
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07004252static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004253{
Yang Zhanga547c6d2013-04-11 19:25:10 +08004254}
4255
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004256static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4257{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004258 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04004259 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004260}
4261
Borislav Petkov74f16902017-03-26 23:51:24 +02004262static void svm_setup_mce(struct kvm_vcpu *vcpu)
4263{
4264 /* [63:9] are reserved. */
4265 vcpu->arch.mcg_cap &= 0x1ff;
4266}
4267
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004268bool svm_smi_blocked(struct kvm_vcpu *vcpu)
Ladi Prosek72d7b372017-10-11 16:54:41 +02004269{
Ladi Prosek05cade72017-10-11 16:54:45 +02004270 struct vcpu_svm *svm = to_svm(vcpu);
4271
4272 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4273 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004274 return true;
4275
4276 return is_smm(vcpu);
4277}
4278
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004279static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004280{
4281 struct vcpu_svm *svm = to_svm(vcpu);
4282 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004283 return -EBUSY;
Ladi Prosek05cade72017-10-11 16:54:45 +02004284
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004285 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4286 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004287 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004288
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004289 return !svm_smi_blocked(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02004290}
4291
Ladi Prosek0234bf82017-10-11 16:54:40 +02004292static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4293{
Ladi Prosek05cade72017-10-11 16:54:45 +02004294 struct vcpu_svm *svm = to_svm(vcpu);
4295 int ret;
4296
4297 if (is_guest_mode(vcpu)) {
4298 /* FED8h - SVM Guest */
4299 put_smstate(u64, smstate, 0x7ed8, 1);
4300 /* FEE0h - SVM Guest VMCB Physical Address */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004301 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
Ladi Prosek05cade72017-10-11 16:54:45 +02004302
4303 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4304 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4305 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4306
4307 ret = nested_svm_vmexit(svm);
4308 if (ret)
4309 return ret;
4310 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02004311 return 0;
4312}
4313
Sean Christophersoned193212019-04-02 08:03:09 -07004314static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004315{
Ladi Prosek05cade72017-10-11 16:54:45 +02004316 struct vcpu_svm *svm = to_svm(vcpu);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01004317 struct kvm_host_map map;
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004318 int ret = 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004319
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004320 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
4321 u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4322 u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004323 u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
Ladi Prosek05cade72017-10-11 16:54:45 +02004324
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004325 if (guest) {
4326 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4327 return 1;
4328
4329 if (!(saved_efer & EFER_SVME))
4330 return 1;
4331
Paolo Bonzini63129752021-03-02 14:40:39 -05004332 if (kvm_vcpu_map(vcpu,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004333 gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004334 return 1;
4335
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03004336 if (svm_allocate_nested(svm))
4337 return 1;
4338
Paolo Bonzini63129752021-03-02 14:40:39 -05004339 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, map.hva);
4340 kvm_vcpu_unmap(vcpu, &map, true);
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004341 }
Ladi Prosek05cade72017-10-11 16:54:45 +02004342 }
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004343
4344 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004345}
4346
Jason Baronb6a7cc32021-01-14 22:27:54 -05004347static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004348{
4349 struct vcpu_svm *svm = to_svm(vcpu);
4350
4351 if (!gif_set(svm)) {
4352 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02004353 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004354 /* STGI will cause a vm exit */
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004355 } else {
4356 /* We must be in SMM; RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004357 }
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004358}
4359
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004360static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004361{
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004362 bool smep, smap, is_user;
4363 unsigned long cr4;
Paolo Bonzinie72436b2020-04-17 12:21:06 -04004364
4365 /*
Tom Lendackybc624d92020-12-10 11:09:44 -06004366 * When the guest is an SEV-ES guest, emulation is not possible.
4367 */
4368 if (sev_es_guest(vcpu->kvm))
4369 return false;
4370
4371 /*
Liran Alon118154b2019-07-17 02:56:58 +03004372 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4373 *
4374 * Errata:
4375 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4376 * possible that CPU microcode implementing DecodeAssist will fail
4377 * to read bytes of instruction which caused #NPF. In this case,
4378 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4379 * return 0 instead of the correct guest instruction bytes.
4380 *
4381 * This happens because CPU microcode reading instruction bytes
4382 * uses a special opcode which attempts to read data using CPL=0
4383 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
4384 * fault, it gives up and returns no instruction bytes.
4385 *
4386 * Detection:
4387 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4388 * returned 0 in GuestIntrBytes field of the VMCB.
4389 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4390 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4391 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4392 * a SMEP fault instead of #NPF).
4393 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4394 * As most guests enable SMAP if they have also enabled SMEP, use above
4395 * logic in order to attempt minimize false-positive of detecting errata
4396 * while still preserving all cases semantic correctness.
4397 *
4398 * Workaround:
4399 * To determine what instruction the guest was executing, the hypervisor
4400 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004401 *
4402 * In non SEV guest, hypervisor will be able to read the guest
4403 * memory to decode the instruction pointer when insn_len is zero
4404 * so we return true to indicate that decoding is possible.
4405 *
4406 * But in the SEV guest, the guest memory is encrypted with the
4407 * guest specific key and hypervisor will not be able to decode the
4408 * instruction pointer so we will not able to workaround it. Lets
4409 * print the error and request to kill the guest.
4410 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004411 if (likely(!insn || insn_len))
4412 return true;
4413
4414 /*
4415 * If RIP is invalid, go ahead with emulation which will cause an
4416 * internal error exit.
4417 */
4418 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4419 return true;
4420
4421 cr4 = kvm_read_cr4(vcpu);
4422 smep = cr4 & X86_CR4_SMEP;
4423 smap = cr4 & X86_CR4_SMAP;
4424 is_user = svm_get_cpl(vcpu) == 3;
Liran Alon118154b2019-07-17 02:56:58 +03004425 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004426 if (!sev_guest(vcpu->kvm))
4427 return true;
4428
Liran Alon118154b2019-07-17 02:56:58 +03004429 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004430 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4431 }
4432
4433 return false;
4434}
4435
Liran Alon4b9852f2019-08-26 13:24:49 +03004436static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4437{
4438 struct vcpu_svm *svm = to_svm(vcpu);
4439
4440 /*
4441 * TODO: Last condition latch INIT signals on vCPU when
4442 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
Paolo Bonzini33b22172020-04-17 10:24:18 -04004443 * To properly emulate the INIT intercept,
4444 * svm_check_nested_events() should call nested_svm_vmexit()
4445 * if an INIT signal is pending.
Liran Alon4b9852f2019-08-26 13:24:49 +03004446 */
4447 return !gif_set(svm) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05004448 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
Liran Alon4b9852f2019-08-26 13:24:49 +03004449}
4450
Tom Lendacky647daca2021-01-04 14:20:01 -06004451static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4452{
4453 if (!sev_es_guest(vcpu->kvm))
4454 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4455
4456 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4457}
4458
Joerg Roedeleaf78262020-03-24 10:41:54 +01004459static void svm_vm_destroy(struct kvm *kvm)
4460{
4461 avic_vm_destroy(kvm);
4462 sev_vm_destroy(kvm);
4463}
4464
4465static int svm_vm_init(struct kvm *kvm)
4466{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004467 if (!pause_filter_count || !pause_filter_thresh)
4468 kvm->arch.pause_in_guest = true;
4469
Joerg Roedeleaf78262020-03-24 10:41:54 +01004470 if (avic) {
4471 int ret = avic_vm_init(kvm);
4472 if (ret)
4473 return ret;
4474 }
4475
4476 kvm_apicv_init(kvm, avic);
4477 return 0;
4478}
4479
Sean Christopherson9c14ee22020-03-21 13:26:03 -07004480static struct kvm_x86_ops svm_x86_ops __initdata = {
Li RongQingdd58f3c2020-02-23 16:13:12 +08004481 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004482 .hardware_enable = svm_hardware_enable,
4483 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004484 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02004485 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004486
4487 .vcpu_create = svm_create_vcpu,
4488 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004489 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004490
Sean Christopherson562b6b02020-01-26 16:41:13 -08004491 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06004492 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004493 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004494
Avi Kivity04d2cc72007-09-10 18:10:54 +03004495 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004496 .vcpu_load = svm_vcpu_load,
4497 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05004498 .vcpu_blocking = svm_vcpu_blocking,
4499 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004500
Jason Baronb6a7cc32021-01-14 22:27:54 -05004501 .update_exception_bitmap = svm_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06004502 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503 .get_msr = svm_get_msr,
4504 .set_msr = svm_set_msr,
4505 .get_segment_base = svm_get_segment_base,
4506 .get_segment = svm_get_segment,
4507 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004508 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004509 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004510 .set_cr0 = svm_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004511 .is_valid_cr4 = svm_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004512 .set_cr4 = svm_set_cr4,
4513 .set_efer = svm_set_efer,
4514 .get_idt = svm_get_idt,
4515 .set_idt = svm_set_idt,
4516 .get_gdt = svm_get_gdt,
4517 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004518 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004519 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004520 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004521 .get_rflags = svm_get_rflags,
4522 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004523
Sean Christopherson77809382020-03-20 14:28:18 -07004524 .tlb_flush_all = svm_flush_tlb,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004525 .tlb_flush_current = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07004526 .tlb_flush_gva = svm_flush_tlb_gva,
Sean Christopherson72b38322020-03-20 14:28:13 -07004527 .tlb_flush_guest = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004528
Avi Kivity6aa8b732006-12-10 02:21:36 -08004529 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004530 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004531 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08004532 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004533 .set_interrupt_shadow = svm_set_interrupt_shadow,
4534 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004535 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004536 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004537 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004538 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004539 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004540 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004541 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004542 .get_nmi_mask = svm_get_nmi_mask,
4543 .set_nmi_mask = svm_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004544 .enable_nmi_window = svm_enable_nmi_window,
4545 .enable_irq_window = svm_enable_irq_window,
4546 .update_cr8_intercept = svm_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04004547 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004548 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06004549 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06004550 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004551 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004552 .hwapic_irr_update = svm_hwapic_irr_update,
4553 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02004554 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004555 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02004556
4557 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004558 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004559 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004560
Avi Kivity586f9602010-11-18 13:09:54 +02004561 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004562
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004563 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004564
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004565 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004566
Leonid Shatz326e7422018-11-06 12:14:25 +02004567 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004568
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004569 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004570
4571 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07004572 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004573
Sean Christophersond264ee02018-08-27 15:21:12 -07004574 .request_immediate_exit = __kvm_request_immediate_exit,
4575
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004576 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004577
4578 .pmu_ops = &amd_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04004579 .nested_ops = &svm_nested_ops,
4580
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004581 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08004582 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004583 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02004584 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004585
Ladi Prosek72d7b372017-10-11 16:54:41 +02004586 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004587 .pre_enter_smm = svm_pre_enter_smm,
4588 .pre_leave_smm = svm_pre_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004589 .enable_smi_window = svm_enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004590
4591 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004592 .mem_enc_reg_region = svm_register_enc_region,
4593 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004594
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004595 .can_emulate_instruction = svm_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03004596
4597 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Alexander Graffd6fa732020-09-25 16:34:19 +02004598
4599 .msr_filter_changed = svm_msr_filter_changed,
Tom Lendackyf1c63662020-12-14 10:29:50 -05004600 .complete_emulated_msr = svm_complete_emulated_msr,
Tom Lendacky647daca2021-01-04 14:20:01 -06004601
4602 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004603};
4604
Sean Christophersond008dfd2020-03-21 13:25:56 -07004605static struct kvm_x86_init_ops svm_init_ops __initdata = {
4606 .cpu_has_kvm_support = has_svm,
4607 .disabled_by_bios = is_disabled,
4608 .hardware_setup = svm_hardware_setup,
4609 .check_processor_compatibility = svm_check_processor_compat,
4610
4611 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612};
4613
4614static int __init svm_init(void)
4615{
Tom Lendackyd07f46f2020-09-07 15:15:03 +02004616 __unused_size_checks();
4617
Sean Christophersond008dfd2020-03-21 13:25:56 -07004618 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004619 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004620}
4621
4622static void __exit svm_exit(void)
4623{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004624 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004625}
4626
4627module_init(svm_init)
4628module_exit(svm_exit)