blob: 907ba85609a2f6b832e9f74f70e4a9113697e069 [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>
Tom Lendacky4d96f912021-09-08 17:58:37 -050028#include <linux/cc_platform.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080029
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050030#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010031#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010034#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020035#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050036#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020037#include <asm/spec-ctrl.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010038#include <asm/cpu_device_id.h>
Tom Lendackyf1c63662020-12-14 10:29:50 -050039#include <asm/traps.h>
Thomas Gleixnerd69c1382021-10-22 20:55:53 +020040#include <asm/fpu/api.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080041
Eduardo Habkost63d11422008-11-17 19:03:20 -020042#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030043#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020044
Joerg Roedel883b0a92020-03-24 10:41:52 +010045#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080046#include "svm_ops.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010047
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000048#include "kvm_onhyperv.h"
49#include "svm_onhyperv.h"
50
Avi Kivity6aa8b732006-12-10 02:21:36 -080051MODULE_AUTHOR("Qumranet");
52MODULE_LICENSE("GPL");
53
Valdis Klētnieks575b2552020-02-27 21:49:52 -050054#ifdef MODULE
Josh Triplettae759542012-03-28 11:32:28 -070055static const struct x86_cpu_id svm_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010056 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
Josh Triplettae759542012-03-28 11:32:28 -070057 {}
58};
59MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050060#endif
Josh Triplettae759542012-03-28 11:32:28 -070061
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
Maxim Levitsky4c849262021-09-14 18:48:19 +0300191/* enable/disable LBR virtualization */
192static int lbrv = true;
193module_param(lbrv, int, 0444);
194
Maxim Levitskyf8006502021-09-14 18:48:23 +0300195static int tsc_scaling = true;
196module_param(tsc_scaling, int, 0444);
197
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200198/*
199 * enable / disable AVIC. Because the defaults differ for APICv
200 * support between VMX and SVM we cannot use module_param_named.
201 */
202static bool avic;
203module_param(avic, bool, 0444);
204
Tom Lendacky291bd202020-12-10 11:09:47 -0600205bool __read_mostly dump_invalid_vmcb;
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200206module_param(dump_invalid_vmcb, bool, 0644);
207
Maxim Levitsky4b639a92021-07-07 15:51:00 +0300208
209bool intercept_smi = true;
210module_param(intercept_smi, bool, 0444);
211
212
Wei Yongjun2e215212021-02-10 07:59:58 +0000213static bool svm_gp_erratum_intercept = true;
Bandan Das82a11e9c2021-01-26 03:18:29 -0500214
Brijesh Singh7607b712018-02-19 10:14:44 -0600215static u8 rsm_ins_bytes[] = "\x0f\xaa";
216
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800217static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
219struct kvm_ldttss_desc {
220 u16 limit0;
221 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100222 unsigned base1:8, type:5, dpl:2, p:1;
223 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224 u32 base3;
225 u32 zero1;
226} __attribute__((packed));
227
Joerg Roedeleaf78262020-03-24 10:41:54 +0100228DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800229
Sean Christopherson844d69c2021-04-23 15:34:04 -0700230/*
231 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
232 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
233 *
234 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
235 * defer the restoration of TSC_AUX until the CPU returns to userspace.
236 */
Sean Christopherson0caa0a72021-05-04 10:17:25 -0700237static int tsc_aux_uret_slot __read_mostly = -1;
Sean Christopherson844d69c2021-04-23 15:34:04 -0700238
Mathias Krause09941fb2012-08-30 01:30:20 +0200239static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200241#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242#define MSRS_RANGE_SIZE 2048
243#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
244
Joerg Roedel883b0a92020-03-24 10:41:52 +0100245u32 svm_msrpm_offset(u32 msr)
Joerg Roedel455716f2010-03-01 15:34:35 +0100246{
247 u32 offset;
248 int i;
249
250 for (i = 0; i < NUM_MSR_MAPS; i++) {
251 if (msr < msrpm_ranges[i] ||
252 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
253 continue;
254
255 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
256 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
257
258 /* Now we have the u8 offset - but need the u32 offset */
259 return offset / 4;
260 }
261
262 /* MSR not in any range */
263 return MSR_INVALID;
264}
265
Avi Kivity6aa8b732006-12-10 02:21:36 -0800266#define MAX_INST_SIZE 15
267
Sean Christophersond468d942020-07-15 20:41:20 -0700268static int get_max_npt_level(void)
Joerg Roedel4b161842010-09-10 17:31:03 +0200269{
270#ifdef CONFIG_X86_64
Wei Huang43e540c2021-08-18 11:55:49 -0500271 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200272#else
273 return PT32E_ROOT_LEVEL;
274#endif
275}
276
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300277int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800278{
Paolo Bonzinic513f482020-05-18 13:08:37 -0400279 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300280 u64 old_efer = vcpu->arch.efer;
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000281 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100282
283 if (!npt_enabled) {
284 /* Shadow paging assumes NX to be available. */
285 efer |= EFER_NX;
286
287 if (!(efer & EFER_LMA))
288 efer &= ~EFER_LME;
289 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300291 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
292 if (!(efer & EFER_SVME)) {
293 svm_leave_nested(svm);
294 svm_set_gif(svm, true);
Bandan Das82a11e9c2021-01-26 03:18:29 -0500295 /* #GP intercept is still needed for vmware backdoor */
296 if (!enable_vmware_backdoor)
297 clr_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300298
299 /*
300 * Free the nested guest state, unless we are in SMM.
301 * In this case we will return to the nested guest
302 * as soon as we leave SMM.
303 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500304 if (!is_smm(vcpu))
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300305 svm_free_nested(svm);
306
307 } else {
308 int ret = svm_allocate_nested(svm);
309
310 if (ret) {
311 vcpu->arch.efer = old_efer;
312 return ret;
313 }
Bandan Das82a11e9c2021-01-26 03:18:29 -0500314
315 if (svm_gp_erratum_intercept)
316 set_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300317 }
Paolo Bonzinic513f482020-05-18 13:08:37 -0400318 }
319
320 svm->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedel06e78522020-06-25 10:03:23 +0200321 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300322 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323}
324
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325static int is_external_interrupt(u32 info)
326{
327 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
328 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
329}
330
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200331static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400332{
333 struct vcpu_svm *svm = to_svm(vcpu);
334 u32 ret = 0;
335
336 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200337 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
338 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400339}
340
341static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
342{
343 struct vcpu_svm *svm = to_svm(vcpu);
344
345 if (mask == 0)
346 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
347 else
348 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
349
350}
351
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200352static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800353{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400354 struct vcpu_svm *svm = to_svm(vcpu);
355
Tom Lendackyf1c63662020-12-14 10:29:50 -0500356 /*
357 * SEV-ES does not expose the next RIP. The RIP update is controlled by
358 * the type of exit and the #VC handler in the guest.
359 */
360 if (sev_es_guest(vcpu->kvm))
361 goto done;
362
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200363 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200364 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200365 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400366 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200367
Sean Christopherson1957aa62019-08-27 14:40:39 -0700368 if (!svm->next_rip) {
369 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
370 return 0;
371 } else {
Sean Christopherson1957aa62019-08-27 14:40:39 -0700372 kvm_rip_write(vcpu, svm->next_rip);
373 }
Tom Lendackyf1c63662020-12-14 10:29:50 -0500374
375done:
Glauber Costa2809f5d2009-05-12 16:21:05 -0400376 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200377
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700378 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800379}
380
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700381static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100382{
383 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700384 unsigned nr = vcpu->arch.exception.nr;
385 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700386 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100387
Paolo Bonzini63129752021-03-02 14:40:39 -0500388 kvm_deliver_exception_payload(vcpu);
Jim Mattsonda998b42018-10-16 14:29:22 -0700389
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200390 if (nr == BP_VECTOR && !nrips) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500391 unsigned long rip, old_rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100392
393 /*
394 * For guest debugging where we have to reinject #BP if some
395 * INT3 is guest-owned:
396 * Emulate nRIP by moving RIP forward. Will fail if injection
397 * raises a fault that is not intercepted. Still better than
398 * failing in all cases.
399 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500400 (void)skip_emulated_instruction(vcpu);
401 rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100402 svm->int3_rip = rip + svm->vmcb->save.cs.base;
403 svm->int3_injected = rip - old_rip;
404 }
405
Jan Kiszka116a4752010-02-23 17:47:54 +0100406 svm->vmcb->control.event_inj = nr
407 | SVM_EVTINJ_VALID
408 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
409 | SVM_EVTINJ_TYPE_EXEPT;
410 svm->vmcb->control.event_inj_err = error_code;
411}
412
Joerg Roedel67ec6602010-05-17 14:43:35 +0200413static void svm_init_erratum_383(void)
414{
415 u32 low, high;
416 int err;
417 u64 val;
418
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100419 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200420 return;
421
422 /* Use _safe variants to not break nested virtualization */
423 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
424 if (err)
425 return;
426
427 val |= (1ULL << 47);
428
429 low = lower_32_bits(val);
430 high = upper_32_bits(val);
431
432 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
433
434 erratum_383_found = true;
435}
436
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500437static void svm_init_osvw(struct kvm_vcpu *vcpu)
438{
439 /*
440 * Guests should see errata 400 and 415 as fixed (assuming that
441 * HLT and IO instructions are intercepted).
442 */
443 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
444 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
445
446 /*
447 * By increasing VCPU's osvw.length to 3 we are telling the guest that
448 * all osvw.status bits inside that length, including bit 0 (which is
449 * reserved for erratum 298), are valid. However, if host processor's
450 * osvw_len is 0 then osvw_status[0] carries no information. We need to
451 * be conservative here and therefore we tell the guest that erratum 298
452 * is present (because we really don't know).
453 */
454 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
455 vcpu->arch.osvw.status |= 1;
456}
457
Avi Kivity6aa8b732006-12-10 02:21:36 -0800458static int has_svm(void)
459{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200460 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800461
Eduardo Habkost63d11422008-11-17 19:03:20 -0200462 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800463 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 return 0;
465 }
466
Tom Lendacky4d96f912021-09-08 17:58:37 -0500467 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
Sean Christophersonccd85d92021-02-02 13:20:17 -0800468 pr_info("KVM is unsupported when running as an SEV guest\n");
469 return 0;
470 }
471
Avi Kivity6aa8b732006-12-10 02:21:36 -0800472 return 1;
473}
474
Radim Krčmář13a34e02014-08-28 15:13:03 +0200475static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100477 /* Make sure we clean up behind us */
Maxim Levitskyf8006502021-09-14 18:48:23 +0300478 if (tsc_scaling)
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100479 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
480
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200481 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100482
483 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484}
485
Radim Krčmář13a34e02014-08-28 15:13:03 +0200486static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487{
488
Tejun Heo0fe1e002009-10-29 22:34:14 +0900489 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800491 struct desc_struct *gdt;
492 int me = raw_smp_processor_id();
493
Alexander Graf10474ae2009-09-15 11:37:46 +0200494 rdmsrl(MSR_EFER, efer);
495 if (efer & EFER_SVME)
496 return -EBUSY;
497
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200499 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200500 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900502 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900503 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200504 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200505 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506 }
507
Tejun Heo0fe1e002009-10-29 22:34:14 +0900508 sd->asid_generation = 1;
509 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
510 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600511 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512
Thomas Garnier45fc8752017-03-14 10:05:08 -0700513 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900514 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515
Alexander Graf9962d032008-11-25 20:17:02 +0100516 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600518 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
Alexander Graf10474ae2009-09-15 11:37:46 +0200519
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100520 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Maxim Levitskyf8006502021-09-14 18:48:23 +0300521 /*
522 * Set the default value, even if we don't use TSC scaling
523 * to avoid having stale value in the msr
524 */
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100525 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500526 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100527 }
528
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500529
530 /*
531 * Get OSVW bits.
532 *
533 * Note that it is possible to have a system with mixed processor
534 * revisions and therefore different OSVW bits. If bits are not the same
535 * on different processors then choose the worst case (i.e. if erratum
536 * is present on one processor and not on another then assume that the
537 * erratum is present everywhere).
538 */
539 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
540 uint64_t len, status = 0;
541 int err;
542
543 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
544 if (!err)
545 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
546 &err);
547
548 if (err)
549 osvw_status = osvw_len = 0;
550 else {
551 if (len < osvw_len)
552 osvw_len = len;
553 osvw_status |= status;
554 osvw_status &= (1ULL << osvw_len) - 1;
555 }
556 } else
557 osvw_status = osvw_len = 0;
558
Joerg Roedel67ec6602010-05-17 14:43:35 +0200559 svm_init_erratum_383();
560
Joerg Roedel1018faa2012-02-29 14:57:32 +0100561 amd_pmu_enable_virt();
562
Alexander Graf10474ae2009-09-15 11:37:46 +0200563 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800564}
565
Joerg Roedel0da1db752008-07-02 16:02:11 +0200566static void svm_cpu_uninit(int cpu)
567{
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800568 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200569
Tejun Heo0fe1e002009-10-29 22:34:14 +0900570 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200571 return;
572
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800573 per_cpu(svm_data, cpu) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600574 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900575 __free_page(sd->save_area);
576 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200577}
578
Avi Kivity6aa8b732006-12-10 02:21:36 -0800579static int svm_cpu_init(int cpu)
580{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900581 struct svm_cpu_data *sd;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700582 int ret = -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800583
Tejun Heo0fe1e002009-10-29 22:34:14 +0900584 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
585 if (!sd)
Sean Christophersonb95c2212021-04-21 19:11:22 -0700586 return ret;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900587 sd->cpu = cpu;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600588 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900589 if (!sd->save_area)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800590 goto free_cpu_data;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700591
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600592 clear_page(page_address(sd->save_area));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593
Sean Christophersonb95c2212021-04-21 19:11:22 -0700594 ret = sev_cpu_init(sd);
595 if (ret)
596 goto free_save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600597
Tejun Heo0fe1e002009-10-29 22:34:14 +0900598 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800599
600 return 0;
601
Miaohe Lind80b64f2020-01-04 16:56:49 +0800602free_save_area:
603 __free_page(sd->save_area);
604free_cpu_data:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900605 kfree(sd);
Sean Christophersonb95c2212021-04-21 19:11:22 -0700606 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607
608}
609
Alexander Graffd6fa732020-09-25 16:34:19 +0200610static int direct_access_msr_slot(u32 msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611{
Alexander Graffd6fa732020-09-25 16:34:19 +0200612 u32 i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800613
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100614 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
Alexander Graffd6fa732020-09-25 16:34:19 +0200615 if (direct_access_msrs[i].index == msr)
616 return i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617
Alexander Graffd6fa732020-09-25 16:34:19 +0200618 return -ENOENT;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100619}
620
Alexander Graffd6fa732020-09-25 16:34:19 +0200621static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
622 int write)
623{
624 struct vcpu_svm *svm = to_svm(vcpu);
625 int slot = direct_access_msr_slot(msr);
626
627 if (slot == -ENOENT)
628 return;
629
630 /* Set the shadow bitmaps to the desired intercept states */
631 if (read)
632 set_bit(slot, svm->shadow_msr_intercept.read);
633 else
634 clear_bit(slot, svm->shadow_msr_intercept.read);
635
636 if (write)
637 set_bit(slot, svm->shadow_msr_intercept.write);
638 else
639 clear_bit(slot, svm->shadow_msr_intercept.write);
640}
641
642static bool valid_msr_intercept(u32 index)
643{
644 return direct_access_msr_slot(index) != -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800645}
646
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200647static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100648{
649 u8 bit_write;
650 unsigned long tmp;
651 u32 offset;
652 u32 *msrpm;
653
654 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
655 to_svm(vcpu)->msrpm;
656
657 offset = svm_msrpm_offset(msr);
658 bit_write = 2 * (msr & 0x0f) + 1;
659 tmp = msrpm[offset];
660
661 BUG_ON(offset == MSR_INVALID);
662
663 return !!test_bit(bit_write, &tmp);
664}
665
Alexander Graffd6fa732020-09-25 16:34:19 +0200666static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
667 u32 msr, int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668{
Joerg Roedel455716f2010-03-01 15:34:35 +0100669 u8 bit_read, bit_write;
670 unsigned long tmp;
671 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100673 /*
674 * If this warning triggers extend the direct_access_msrs list at the
675 * beginning of the file
676 */
677 WARN_ON(!valid_msr_intercept(msr));
678
Alexander Graffd6fa732020-09-25 16:34:19 +0200679 /* Enforce non allowed MSRs to trap */
680 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
681 read = 0;
682
683 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
684 write = 0;
685
Joerg Roedel455716f2010-03-01 15:34:35 +0100686 offset = svm_msrpm_offset(msr);
687 bit_read = 2 * (msr & 0x0f);
688 bit_write = 2 * (msr & 0x0f) + 1;
689 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800690
Joerg Roedel455716f2010-03-01 15:34:35 +0100691 BUG_ON(offset == MSR_INVALID);
692
693 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
694 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
695
696 msrpm[offset] = tmp;
Vineeth Pillaic4327f12021-06-03 15:14:39 +0000697
698 svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
699
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700}
701
Tom Lendacky376c6d22020-12-10 11:10:06 -0600702void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
703 int read, int write)
Alexander Graffd6fa732020-09-25 16:34:19 +0200704{
705 set_shadow_msr_intercept(vcpu, msr, read, write);
706 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
707}
708
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300709u32 *svm_vcpu_alloc_msrpm(void)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100710{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400711 unsigned int order = get_order(MSRPM_SIZE);
712 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200713 u32 *msrpm;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100714
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300715 if (!pages)
716 return NULL;
717
718 msrpm = page_address(pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400719 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100720
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200721 return msrpm;
722}
723
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300724void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800725{
Joerg Roedelf65c2292008-02-13 18:58:46 +0100726 int i;
727
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100728 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
729 if (!direct_access_msrs[i].always)
730 continue;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200731 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100732 }
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300733}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300735
736void svm_vcpu_free_msrpm(u32 *msrpm)
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300737{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400738 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800739}
740
Alexander Graffd6fa732020-09-25 16:34:19 +0200741static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
742{
743 struct vcpu_svm *svm = to_svm(vcpu);
744 u32 i;
745
746 /*
747 * Set intercept permissions for all direct access MSRs again. They
748 * will automatically get filtered through the MSR filter, so we are
749 * back in sync after this.
750 */
751 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
752 u32 msr = direct_access_msrs[i].index;
753 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
754 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
755
756 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
Anthony Liguoric8681332007-04-30 09:48:11 +0300757 }
758}
759
Joerg Roedel323c3d82010-03-01 15:34:37 +0100760static void add_msr_offset(u32 offset)
761{
762 int i;
763
764 for (i = 0; i < MSRPM_OFFSETS; ++i) {
765
766 /* Offset already in list? */
767 if (msrpm_offsets[i] == offset)
768 return;
769
770 /* Slot used by another offset? */
771 if (msrpm_offsets[i] != MSR_INVALID)
772 continue;
773
774 /* Add offset to list */
775 msrpm_offsets[i] = offset;
776
777 return;
778 }
779
780 /*
781 * If this BUG triggers the msrpm_offsets table has an overflow. Just
782 * increase MSRPM_OFFSETS in this case.
783 */
784 BUG();
785}
786
787static void init_msrpm_offsets(void)
788{
789 int i;
790
791 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
792
793 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
794 u32 offset;
795
796 offset = svm_msrpm_offset(direct_access_msrs[i].index);
797 BUG_ON(offset == MSR_INVALID);
798
799 add_msr_offset(offset);
800 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801}
802
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200803static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200805 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800806
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500807 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200808 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
809 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
810 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
811 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812}
813
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200814static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200816 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800817
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500818 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200819 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
820 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
821 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
822 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800823}
824
Joerg Roedel883b0a92020-03-24 10:41:52 +0100825void disable_nmi_singlestep(struct vcpu_svm *svm)
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200826{
827 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500828
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200829 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
830 /* Clear our flags if they were not set by the guest */
831 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
832 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
833 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
834 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
835 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200836}
837
Babu Moger8566ac82018-03-16 16:37:26 -0400838static void grow_ple_window(struct kvm_vcpu *vcpu)
839{
840 struct vcpu_svm *svm = to_svm(vcpu);
841 struct vmcb_control_area *control = &svm->vmcb->control;
842 int old = control->pause_filter_count;
843
844 control->pause_filter_count = __grow_ple_window(old,
845 pause_filter_count,
846 pause_filter_count_grow,
847 pause_filter_count_max);
848
Peter Xu4f75bcc2019-09-06 10:17:22 +0800849 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200850 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800851 trace_kvm_ple_window_update(vcpu->vcpu_id,
852 control->pause_filter_count, old);
853 }
Babu Moger8566ac82018-03-16 16:37:26 -0400854}
855
856static void shrink_ple_window(struct kvm_vcpu *vcpu)
857{
858 struct vcpu_svm *svm = to_svm(vcpu);
859 struct vmcb_control_area *control = &svm->vmcb->control;
860 int old = control->pause_filter_count;
861
862 control->pause_filter_count =
863 __shrink_ple_window(old,
864 pause_filter_count,
865 pause_filter_count_shrink,
866 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800867 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200868 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800869 trace_kvm_ple_window_update(vcpu->vcpu_id,
870 control->pause_filter_count, old);
871 }
Babu Moger8566ac82018-03-16 16:37:26 -0400872}
873
Tom Lendacky52918ed2020-01-09 17:42:16 -0600874/*
875 * The default MMIO mask is a single bit (excluding the present bit),
876 * which could conflict with the memory encryption bit. Check for
877 * memory encryption support and override the default MMIO mask if
878 * memory encryption is enabled.
879 */
880static __init void svm_adjust_mmio_mask(void)
881{
882 unsigned int enc_bit, mask_bit;
883 u64 msr, mask;
884
885 /* If there is no memory encryption support, use existing mask */
886 if (cpuid_eax(0x80000000) < 0x8000001f)
887 return;
888
889 /* If memory encryption is not enabled, use existing mask */
Brijesh Singh059e5c32021-04-27 06:16:36 -0500890 rdmsrl(MSR_AMD64_SYSCFG, msr);
891 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
Tom Lendacky52918ed2020-01-09 17:42:16 -0600892 return;
893
894 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
895 mask_bit = boot_cpu_data.x86_phys_bits;
896
897 /* Increment the mask bit if it is the same as the encryption bit */
898 if (enc_bit == mask_bit)
899 mask_bit++;
900
901 /*
902 * If the mask bit location is below 52, then some bits above the
903 * physical addressing limit will always be reserved, so use the
904 * rsvd_bits() function to generate the mask. This mask, along with
905 * the present bit, will be used to generate a page fault with
906 * PFER.RSV = 1.
907 *
908 * If the mask bit location is 52 (or above), then clear the mask.
909 */
910 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
911
Sean Christopherson81203372021-02-25 12:47:35 -0800912 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
Tom Lendacky52918ed2020-01-09 17:42:16 -0600913}
914
Li RongQingdd58f3c2020-02-23 16:13:12 +0800915static void svm_hardware_teardown(void)
916{
917 int cpu;
918
Sean Christopherson4cafd0c2021-04-21 19:11:20 -0700919 sev_hardware_teardown();
Li RongQingdd58f3c2020-02-23 16:13:12 +0800920
921 for_each_possible_cpu(cpu)
922 svm_cpu_uninit(cpu);
923
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400924 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
925 get_order(IOPM_SIZE));
Li RongQingdd58f3c2020-02-23 16:13:12 +0800926 iopm_base = 0;
927}
928
Sean Christopherson9b58b982020-03-02 15:56:42 -0800929static __init void svm_set_cpu_caps(void)
930{
931 kvm_set_cpu_caps();
932
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100933 supported_xss = 0;
934
Sean Christophersona50718c2020-03-02 15:57:07 -0800935 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
936 if (nested) {
Sean Christopherson9b58b982020-03-02 15:56:42 -0800937 kvm_cpu_cap_set(X86_FEATURE_SVM);
938
Sean Christopherson4eb87462020-03-02 15:57:08 -0800939 if (nrips)
Sean Christophersona50718c2020-03-02 15:57:07 -0800940 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
941
942 if (npt_enabled)
943 kvm_cpu_cap_set(X86_FEATURE_NPT);
Wei Huang14c2bf82021-01-26 03:18:31 -0500944
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300945 if (tsc_scaling)
946 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
947
Wei Huang14c2bf82021-01-26 03:18:31 -0500948 /* Nested VM can receive #VMEXIT instead of triggering #GP */
949 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
Sean Christophersona50718c2020-03-02 15:57:07 -0800950 }
951
Sean Christopherson93c380e2020-03-02 15:56:54 -0800952 /* CPUID 0x80000008 */
953 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
954 boot_cpu_has(X86_FEATURE_AMD_SSBD))
955 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700956
957 /* CPUID 0x8000001F (SME/SEV features) */
958 sev_set_cpu_caps();
Sean Christopherson9b58b982020-03-02 15:56:42 -0800959}
960
Avi Kivity6aa8b732006-12-10 02:21:36 -0800961static __init int svm_hardware_setup(void)
962{
963 int cpu;
964 struct page *iopm_pages;
965 void *iopm_va;
966 int r;
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400967 unsigned int order = get_order(IOPM_SIZE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800968
Sean Christophersonb26a71a2021-06-15 09:45:33 -0700969 /*
970 * NX is required for shadow paging and for NPT if the NX huge pages
971 * mitigation is enabled.
972 */
973 if (!boot_cpu_has(X86_FEATURE_NX)) {
974 pr_err_ratelimited("NX (Execute Disable) not supported\n");
975 return -EOPNOTSUPP;
976 }
977 kvm_enable_efer_bits(EFER_NX);
978
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400979 iopm_pages = alloc_pages(GFP_KERNEL, order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800980
981 if (!iopm_pages)
982 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300983
984 iopm_va = page_address(iopm_pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400985 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
987
Joerg Roedel323c3d82010-03-01 15:34:37 +0100988 init_msrpm_offsets();
989
Sean Christophersoncfc48182020-03-02 15:56:23 -0800990 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
991
Alexander Graf1b2fd702009-02-02 16:23:51 +0100992 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
993 kvm_enable_efer_bits(EFER_FFXSR);
994
Maxim Levitskyf8006502021-09-14 18:48:23 +0300995 if (tsc_scaling) {
996 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
997 tsc_scaling = false;
998 } else {
999 pr_info("TSC scaling supported\n");
1000 kvm_has_tsc_control = true;
1001 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1002 kvm_tsc_scaling_ratio_frac_bits = 32;
1003 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01001004 }
1005
Sean Christophersone5fda4b2021-05-04 10:17:32 -07001006 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
Sean Christopherson844d69c2021-04-23 15:34:04 -07001007
Babu Moger8566ac82018-03-16 16:37:26 -04001008 /* Check for pause filtering support */
1009 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1010 pause_filter_count = 0;
1011 pause_filter_thresh = 0;
1012 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1013 pause_filter_thresh = 0;
1014 }
1015
Alexander Graf236de052008-11-25 20:17:10 +01001016 if (nested) {
1017 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001018 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001019 }
1020
Sean Christopherson99840a72021-03-04 18:16:37 -08001021 /*
1022 * KVM's MMU doesn't support using 2-level paging for itself, and thus
1023 * NPT isn't supported if the host is using 2-level paging since host
1024 * CR4 is unchanged on VMRUN.
1025 */
1026 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001027 npt_enabled = false;
1028
Sean Christopherson99840a72021-03-04 18:16:37 -08001029 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001030 npt_enabled = false;
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001031
Wei Huang746700d2021-08-18 11:55:47 -05001032 /* Force VM NPT level equal to the host's max NPT level */
1033 kvm_configure_mmu(npt_enabled, get_max_npt_level(),
1034 get_max_npt_level(), PG_LEVEL_1G);
Sean Christopherson213e0e12020-03-02 15:57:01 -08001035 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001036
Sean Christophersone8126bd2021-04-21 19:11:14 -07001037 /* Note, SEV setup consumes npt_enabled. */
1038 sev_hardware_setup();
Sean Christophersonfa136802021-04-21 19:11:13 -07001039
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001040 svm_hv_hardware_setup();
1041
Sean Christophersonfa136802021-04-21 19:11:13 -07001042 svm_adjust_mmio_mask();
1043
1044 for_each_possible_cpu(cpu) {
1045 r = svm_cpu_init(cpu);
1046 if (r)
1047 goto err;
1048 }
1049
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001050 if (nrips) {
1051 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1052 nrips = false;
1053 }
1054
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02001055 enable_apicv = avic = avic && npt_enabled && boot_cpu_has(X86_FEATURE_AVIC);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001056
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02001057 if (enable_apicv) {
1058 pr_info("AVIC enabled\n");
1059
1060 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001061 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001062
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001063 if (vls) {
1064 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001065 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001066 !IS_ENABLED(CONFIG_X86_64)) {
1067 vls = false;
1068 } else {
1069 pr_info("Virtual VMLOAD VMSAVE supported\n");
1070 }
1071 }
1072
Wei Huang3b9c7232021-01-26 03:18:30 -05001073 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
1074 svm_gp_erratum_intercept = false;
1075
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001076 if (vgif) {
1077 if (!boot_cpu_has(X86_FEATURE_VGIF))
1078 vgif = false;
1079 else
1080 pr_info("Virtual GIF supported\n");
1081 }
1082
Maxim Levitsky4c849262021-09-14 18:48:19 +03001083 if (lbrv) {
1084 if (!boot_cpu_has(X86_FEATURE_LBRV))
1085 lbrv = false;
1086 else
1087 pr_info("LBR virtualization supported\n");
1088 }
1089
Sean Christopherson9b58b982020-03-02 15:56:42 -08001090 svm_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08001091
Mohammed Gamal3edd6832020-07-10 17:48:11 +02001092 /*
1093 * It seems that on AMD processors PTE's accessed bit is
1094 * being set by the CPU hardware before the NPF vmexit.
1095 * This is not expected behaviour and our tests fail because
1096 * of it.
1097 * A workaround here is to disable support for
1098 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1099 * In this case userspace can know if there is support using
1100 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1101 * it
1102 * If future AMD CPU models change the behaviour described above,
1103 * this variable can be changed accordingly
1104 */
1105 allow_smaller_maxphyaddr = !npt_enabled;
1106
Avi Kivity6aa8b732006-12-10 02:21:36 -08001107 return 0;
1108
Joerg Roedelf65c2292008-02-13 18:58:46 +01001109err:
Li RongQingdd58f3c2020-02-23 16:13:12 +08001110 svm_hardware_teardown();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111 return r;
1112}
1113
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114static void init_seg(struct vmcb_seg *seg)
1115{
1116 seg->selector = 0;
1117 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001118 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001119 seg->limit = 0xffff;
1120 seg->base = 0;
1121}
1122
1123static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1124{
1125 seg->selector = 0;
1126 seg->attrib = SVM_SELECTOR_P_MASK | type;
1127 seg->limit = 0xffff;
1128 seg->base = 0;
1129}
1130
Ilias Stamatis307a94c2021-05-26 19:44:13 +01001131static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
1132{
1133 struct vcpu_svm *svm = to_svm(vcpu);
1134
1135 return svm->nested.ctl.tsc_offset;
1136}
1137
1138static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
1139{
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001140 struct vcpu_svm *svm = to_svm(vcpu);
1141
1142 return svm->tsc_ratio_msr;
Ilias Stamatis307a94c2021-05-26 19:44:13 +01001143}
1144
Ilias Stamatisedcfe542021-05-26 19:44:15 +01001145static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001146{
1147 struct vcpu_svm *svm = to_svm(vcpu);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001148
Ilias Stamatisedcfe542021-05-26 19:44:15 +01001149 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
1150 svm->vmcb->control.tsc_offset = offset;
Joerg Roedel06e78522020-06-25 10:03:23 +02001151 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001152}
1153
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001154void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01001155{
1156 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
1157}
1158
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001159/* Evaluate instruction intercepts that depend on guest CPUID features. */
1160static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
1161 struct vcpu_svm *svm)
Babu Moger4407a792020-09-11 14:29:19 -05001162{
1163 /*
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001164 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1165 * roots, or if INVPCID is disabled in the guest to inject #UD.
Babu Moger4407a792020-09-11 14:29:19 -05001166 */
1167 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001168 if (!npt_enabled ||
1169 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
Babu Moger4407a792020-09-11 14:29:19 -05001170 svm_set_intercept(svm, INTERCEPT_INVPCID);
1171 else
1172 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1173 }
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001174
1175 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
1176 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
1177 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
1178 else
1179 svm_set_intercept(svm, INTERCEPT_RDTSCP);
1180 }
Babu Moger4407a792020-09-11 14:29:19 -05001181}
1182
Paolo Bonzini36e81942021-09-23 12:46:07 -04001183static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
1184{
1185 struct vcpu_svm *svm = to_svm(vcpu);
1186
1187 if (guest_cpuid_is_intel(vcpu)) {
1188 /*
1189 * We must intercept SYSENTER_EIP and SYSENTER_ESP
1190 * accesses because the processor only stores 32 bits.
1191 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
1192 */
1193 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1194 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1195 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1196
1197 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
1198 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
1199 } else {
1200 /*
1201 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1202 * in VMCB and clear intercepts to avoid #VMEXIT.
1203 */
1204 if (vls) {
1205 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
1206 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
1207 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1208 }
1209 /* No need to intercept these MSRs */
1210 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
1211 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
1212 }
1213}
1214
Paolo Bonzini63129752021-03-02 14:40:39 -05001215static void init_vmcb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001216{
Paolo Bonzini63129752021-03-02 14:40:39 -05001217 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedele6101a92008-02-13 18:58:45 +01001218 struct vmcb_control_area *control = &svm->vmcb->control;
1219 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001220
Babu Moger830bd712020-09-11 14:28:50 -05001221 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1222 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1223 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1224 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1225 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1226 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001227 if (!kvm_vcpu_apicv_active(vcpu))
Babu Moger830bd712020-09-11 14:28:50 -05001228 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229
Paolo Bonzini5315c712014-03-03 13:08:29 +01001230 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001231
Joerg Roedel18c918c2010-11-30 18:03:59 +01001232 set_exception_intercept(svm, PF_VECTOR);
1233 set_exception_intercept(svm, UD_VECTOR);
1234 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001235 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001236 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001237 /*
1238 * Guest access to VMware backdoor ports could legitimately
1239 * trigger #GP because of TSS I/O permission bitmap.
1240 * We intercept those #GP and allow access to them anyway
1241 * as VMware does.
1242 */
1243 if (enable_vmware_backdoor)
1244 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001245
Joerg Roedela284ba52020-06-25 10:03:24 +02001246 svm_set_intercept(svm, INTERCEPT_INTR);
1247 svm_set_intercept(svm, INTERCEPT_NMI);
Maxim Levitsky4b639a92021-07-07 15:51:00 +03001248
1249 if (intercept_smi)
1250 svm_set_intercept(svm, INTERCEPT_SMI);
1251
Joerg Roedela284ba52020-06-25 10:03:24 +02001252 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1253 svm_set_intercept(svm, INTERCEPT_RDPMC);
1254 svm_set_intercept(svm, INTERCEPT_CPUID);
1255 svm_set_intercept(svm, INTERCEPT_INVD);
1256 svm_set_intercept(svm, INTERCEPT_INVLPG);
1257 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1258 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1259 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1260 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1261 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1262 svm_set_intercept(svm, INTERCEPT_VMRUN);
1263 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1264 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1265 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1266 svm_set_intercept(svm, INTERCEPT_STGI);
1267 svm_set_intercept(svm, INTERCEPT_CLGI);
1268 svm_set_intercept(svm, INTERCEPT_SKINIT);
1269 svm_set_intercept(svm, INTERCEPT_WBINVD);
1270 svm_set_intercept(svm, INTERCEPT_XSETBV);
1271 svm_set_intercept(svm, INTERCEPT_RDPRU);
1272 svm_set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001273
Paolo Bonzini63129752021-03-02 14:40:39 -05001274 if (!kvm_mwait_in_guest(vcpu->kvm)) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001275 svm_set_intercept(svm, INTERCEPT_MONITOR);
1276 svm_set_intercept(svm, INTERCEPT_MWAIT);
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001277 }
1278
Paolo Bonzini63129752021-03-02 14:40:39 -05001279 if (!kvm_hlt_in_guest(vcpu->kvm))
Joerg Roedela284ba52020-06-25 10:03:24 +02001280 svm_set_intercept(svm, INTERCEPT_HLT);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001281
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001282 control->iopm_base_pa = __sme_set(iopm_base);
1283 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284 control->int_ctl = V_INTR_MASKING_MASK;
1285
1286 init_seg(&save->es);
1287 init_seg(&save->ss);
1288 init_seg(&save->ds);
1289 init_seg(&save->fs);
1290 init_seg(&save->gs);
1291
1292 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001293 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001294 /* Executable/Readable Code Segment */
1295 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1296 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1297 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001299 save->gdtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 save->gdtr.limit = 0xffff;
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001301 save->idtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001302 save->idtr.limit = 0xffff;
1303
1304 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1305 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1306
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001307 if (npt_enabled) {
1308 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001309 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedela284ba52020-06-25 10:03:24 +02001310 svm_clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001311 clr_exception_intercept(svm, PF_VECTOR);
Babu Moger830bd712020-09-11 14:28:50 -05001312 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1313 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001314 save->g_pat = vcpu->arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001315 save->cr3 = 0;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001316 }
Cathy Avery193015a2021-01-12 11:43:13 -05001317 svm->current_vmcb->asid_generation = 0;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001318 svm->asid = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001319
Maxim Levitskyc74ad082021-05-03 15:54:43 +03001320 svm->nested.vmcb12_gpa = INVALID_GPA;
1321 svm->nested.last_vmcb12_gpa = INVALID_GPA;
Joerg Roedel2af91942009-08-07 11:49:28 +02001322
Paolo Bonzini63129752021-03-02 14:40:39 -05001323 if (!kvm_pause_in_guest(vcpu->kvm)) {
Babu Moger8566ac82018-03-16 16:37:26 -04001324 control->pause_filter_count = pause_filter_count;
1325 if (pause_filter_thresh)
1326 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedela284ba52020-06-25 10:03:24 +02001327 svm_set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001328 } else {
Joerg Roedela284ba52020-06-25 10:03:24 +02001329 svm_clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001330 }
1331
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001332 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05001333
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001334 /*
Babu Mogerd00b99c2021-02-17 10:56:04 -05001335 * If the host supports V_SPEC_CTRL then disable the interception
1336 * of MSR_IA32_SPEC_CTRL.
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001337 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05001338 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1339 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1340
Paolo Bonzini63129752021-03-02 14:40:39 -05001341 if (kvm_vcpu_apicv_active(vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001342 avic_init_vmcb(svm);
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001343
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001344 if (vgif) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001345 svm_clr_intercept(svm, INTERCEPT_STGI);
1346 svm_clr_intercept(svm, INTERCEPT_CLGI);
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001347 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1348 }
1349
Paolo Bonzini63129752021-03-02 14:40:39 -05001350 if (sev_guest(vcpu->kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001351 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001352 clr_exception_intercept(svm, UD_VECTOR);
Tom Lendacky376c6d22020-12-10 11:10:06 -06001353
Paolo Bonzini63129752021-03-02 14:40:39 -05001354 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky376c6d22020-12-10 11:10:06 -06001355 /* Perform SEV-ES specific VMCB updates */
1356 sev_es_init_vmcb(svm);
1357 }
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001358 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001359
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001360 svm_hv_init_vmcb(svm->vmcb);
Paolo Bonzini36e81942021-09-23 12:46:07 -04001361 init_vmcb_after_set_cpuid(vcpu);
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001362
Joerg Roedel06e78522020-06-25 10:03:23 +02001363 vmcb_mark_all_dirty(svm->vmcb);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001364
Joerg Roedel2af91942009-08-07 11:49:28 +02001365 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001366}
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001367
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001368static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
1369{
1370 struct vcpu_svm *svm = to_svm(vcpu);
1371
1372 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1373
1374 svm_init_osvw(vcpu);
1375 vcpu->arch.microcode_version = 0x01000065;
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001376 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio;
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001377
1378 if (sev_es_guest(vcpu->kvm))
1379 sev_es_vcpu_reset(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001380}
1381
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001382static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001383{
1384 struct vcpu_svm *svm = to_svm(vcpu);
1385
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001386 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001387 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001388
Paolo Bonzini63129752021-03-02 14:40:39 -05001389 init_vmcb(vcpu);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001390
1391 if (!init_event)
1392 __svm_vcpu_reset(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001393}
1394
Cathy Avery4995a362021-01-13 07:07:52 -05001395void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1396{
1397 svm->current_vmcb = target_vmcb;
1398 svm->vmcb = target_vmcb->ptr;
Cathy Avery4995a362021-01-13 07:07:52 -05001399}
1400
Sean Christopherson987b2592019-12-18 13:54:55 -08001401static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001402{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001403 struct vcpu_svm *svm;
Cathy Avery4995a362021-01-13 07:07:52 -05001404 struct page *vmcb01_page;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001405 struct page *vmsa_page = NULL;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001406 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001407
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001408 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1409 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001410
Joerg Roedelf65c2292008-02-13 18:58:46 +01001411 err = -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -05001412 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1413 if (!vmcb01_page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001414 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001415
Paolo Bonzini63129752021-03-02 14:40:39 -05001416 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001417 /*
1418 * SEV-ES guests require a separate VMSA page used to contain
1419 * the encrypted register state of the guest.
1420 */
1421 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1422 if (!vmsa_page)
1423 goto error_free_vmcb_page;
Tom Lendackyed02b212020-12-10 11:10:01 -06001424
1425 /*
1426 * SEV-ES guests maintain an encrypted version of their FPU
1427 * state which is restored and saved on VMRUN and VMEXIT.
Thomas Gleixnerd69c1382021-10-22 20:55:53 +02001428 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
1429 * do xsave/xrstor on it.
Tom Lendackyed02b212020-12-10 11:10:01 -06001430 */
Thomas Gleixnerd69c1382021-10-22 20:55:53 +02001431 fpstate_set_confidential(&vcpu->arch.guest_fpu);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001432 }
1433
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001434 err = avic_init_vcpu(svm);
1435 if (err)
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001436 goto error_free_vmsa_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001437
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001438 /* We initialize this flag to true to make sure that the is_running
1439 * bit would be set the first time the vcpu is loaded.
1440 */
Suravee Suthikulpanit6c3e4422019-11-14 14:15:12 -06001441 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1442 svm->avic_is_running = true;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001443
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001444 svm->msrpm = svm_vcpu_alloc_msrpm();
Chen Zhou054409a2020-11-17 10:54:26 +08001445 if (!svm->msrpm) {
1446 err = -ENOMEM;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001447 goto error_free_vmsa_page;
Chen Zhou054409a2020-11-17 10:54:26 +08001448 }
Alexander Grafb286d5d2008-11-25 20:17:05 +01001449
Cathy Avery4995a362021-01-13 07:07:52 -05001450 svm->vmcb01.ptr = page_address(vmcb01_page);
1451 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001452 svm_switch_vmcb(svm, &svm->vmcb01);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001453
1454 if (vmsa_page)
Peter Gondab67a4cc2021-10-21 10:42:59 -07001455 svm->sev_es.vmsa = page_address(vmsa_page);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001456
Michael Rotha7fc06d2021-02-02 13:01:26 -06001457 svm->guest_state_loaded = false;
Cathy Avery4995a362021-01-13 07:07:52 -05001458
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001459 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001460
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001461error_free_vmsa_page:
1462 if (vmsa_page)
1463 __free_page(vmsa_page);
Maxim Levitsky8d22b902020-08-27 20:11:42 +03001464error_free_vmcb_page:
Cathy Avery4995a362021-01-13 07:07:52 -05001465 __free_page(vmcb01_page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001466out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001467 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001468}
1469
Jim Mattsonfd65d312018-05-22 09:54:20 -07001470static void svm_clear_current_vmcb(struct vmcb *vmcb)
1471{
1472 int i;
1473
1474 for_each_online_cpu(i)
1475 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1476}
1477
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1479{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001480 struct vcpu_svm *svm = to_svm(vcpu);
1481
Jim Mattsonfd65d312018-05-22 09:54:20 -07001482 /*
1483 * The vmcb page can be recycled, causing a false negative in
1484 * svm_vcpu_load(). So, ensure that no logical CPU has this
1485 * vmcb page recorded as its current vmcb.
1486 */
1487 svm_clear_current_vmcb(svm->vmcb);
1488
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03001489 svm_free_nested(svm);
1490
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001491 sev_free_vcpu(vcpu);
1492
Cathy Avery4995a362021-01-13 07:07:52 -05001493 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
Krish Sadhukhan47903dc2021-04-12 17:56:05 -04001494 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001495}
1496
Michael Rotha7fc06d2021-02-02 13:01:26 -06001497static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001498{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001499 struct vcpu_svm *svm = to_svm(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001500 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity0cc50642007-03-25 12:07:27 +02001501
Tom Lendackyce7ea0c2021-05-06 15:14:41 -05001502 if (sev_es_guest(vcpu->kvm))
1503 sev_es_unmap_ghcb(svm);
1504
Michael Rotha7fc06d2021-02-02 13:01:26 -06001505 if (svm->guest_state_loaded)
1506 return;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001507
Michael Rotha7fc06d2021-02-02 13:01:26 -06001508 /*
Michael Rotha7fc06d2021-02-02 13:01:26 -06001509 * Save additional host state that will be restored on VMEXIT (sev-es)
1510 * or subsequent vmload of host save area.
1511 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001512 if (sev_es_guest(vcpu->kvm)) {
Michael Rotha7fc06d2021-02-02 13:01:26 -06001513 sev_es_prepare_guest_switch(svm, vcpu->cpu);
Tom Lendacky86137772020-12-10 11:10:07 -06001514 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06001515 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06001516 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001517
Maxim Levitskyf8006502021-09-14 18:48:23 +03001518 if (tsc_scaling) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001519 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1520 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1521 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1522 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1523 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001524 }
Michael Rotha7fc06d2021-02-02 13:01:26 -06001525
Sean Christopherson0caa0a72021-05-04 10:17:25 -07001526 if (likely(tsc_aux_uret_slot >= 0))
1527 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001528
Michael Rotha7fc06d2021-02-02 13:01:26 -06001529 svm->guest_state_loaded = true;
1530}
1531
1532static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1533{
Sean Christopherson844d69c2021-04-23 15:34:04 -07001534 to_svm(vcpu)->guest_state_loaded = false;
Michael Rotha7fc06d2021-02-02 13:01:26 -06001535}
1536
1537static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1538{
1539 struct vcpu_svm *svm = to_svm(vcpu);
1540 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1541
Ashok Raj15d45072018-02-01 22:59:43 +01001542 if (sd->current_vmcb != svm->vmcb) {
1543 sd->current_vmcb = svm->vmcb;
1544 indirect_branch_prediction_barrier();
1545 }
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001546 if (kvm_vcpu_apicv_active(vcpu))
1547 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548}
1549
1550static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1551{
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001552 if (kvm_vcpu_apicv_active(vcpu))
1553 avic_vcpu_put(vcpu);
1554
Michael Rotha7fc06d2021-02-02 13:01:26 -06001555 svm_prepare_host_switch(vcpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001556
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001557 ++vcpu->stat.host_state_reload;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558}
1559
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1561{
Ladi Prosek9b611742017-06-21 09:06:59 +02001562 struct vcpu_svm *svm = to_svm(vcpu);
1563 unsigned long rflags = svm->vmcb->save.rflags;
1564
1565 if (svm->nmi_singlestep) {
1566 /* Hide our flags if they were not set by the guest */
1567 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1568 rflags &= ~X86_EFLAGS_TF;
1569 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1570 rflags &= ~X86_EFLAGS_RF;
1571 }
1572 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001573}
1574
1575static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1576{
Ladi Prosek9b611742017-06-21 09:06:59 +02001577 if (to_svm(vcpu)->nmi_singlestep)
1578 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1579
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001580 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001581 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001582 * (caused by either a task switch or an inter-privilege IRET),
1583 * so we do not need to update the CPL here.
1584 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001585 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001586}
1587
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001588static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1589{
1590 switch (reg) {
1591 case VCPU_EXREG_PDPTR:
1592 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001593 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001594 break;
1595 default:
Sean Christopherson67369272021-07-02 15:04:25 -07001596 KVM_BUG_ON(1, vcpu->kvm);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001597 }
1598}
1599
Suravee Suthikulpanite14b7782020-05-06 08:17:55 -05001600static void svm_set_vintr(struct vcpu_svm *svm)
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001601{
1602 struct vmcb_control_area *control;
1603
Maxim Levitskyf1577ab2021-07-13 17:20:16 +03001604 /*
1605 * The following fields are ignored when AVIC is enabled
1606 */
1607 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
1608
Joerg Roedela284ba52020-06-25 10:03:24 +02001609 svm_set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001610
1611 /*
1612 * This is just a dummy VINTR to actually cause a vmexit to happen.
1613 * Actual injection of virtual interrupts happens through EVENTINJ.
1614 */
1615 control = &svm->vmcb->control;
1616 control->int_vector = 0x0;
1617 control->int_ctl &= ~V_INTR_PRIO_MASK;
1618 control->int_ctl |= V_IRQ_MASK |
1619 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedel06e78522020-06-25 10:03:23 +02001620 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001621}
1622
Alexander Graff0b85052008-11-25 20:17:01 +01001623static void svm_clear_vintr(struct vcpu_svm *svm)
1624{
Joerg Roedela284ba52020-06-25 10:03:24 +02001625 svm_clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001626
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001627 /* Drop int_ctl fields related to VINTR injection. */
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001628 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001629 if (is_guest_mode(&svm->vcpu)) {
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001630 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinifb7333d2020-06-08 07:11:47 -04001631
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001632 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1633 (svm->nested.ctl.int_ctl & V_TPR_MASK));
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001634
1635 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1636 V_IRQ_INJECTION_BITS_MASK;
Maxim Levitskyaee77e12021-09-14 18:48:12 +03001637
1638 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001639 }
1640
Joerg Roedel06e78522020-06-25 10:03:23 +02001641 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001642}
1643
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1645{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001646 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001647 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001648
1649 switch (seg) {
1650 case VCPU_SREG_CS: return &save->cs;
1651 case VCPU_SREG_DS: return &save->ds;
1652 case VCPU_SREG_ES: return &save->es;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001653 case VCPU_SREG_FS: return &save01->fs;
1654 case VCPU_SREG_GS: return &save01->gs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001655 case VCPU_SREG_SS: return &save->ss;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001656 case VCPU_SREG_TR: return &save01->tr;
1657 case VCPU_SREG_LDTR: return &save01->ldtr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 }
1659 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001660 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001661}
1662
1663static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1664{
1665 struct vmcb_seg *s = svm_seg(vcpu, seg);
1666
1667 return s->base;
1668}
1669
1670static void svm_get_segment(struct kvm_vcpu *vcpu,
1671 struct kvm_segment *var, int seg)
1672{
1673 struct vmcb_seg *s = svm_seg(vcpu, seg);
1674
1675 var->base = s->base;
1676 var->limit = s->limit;
1677 var->selector = s->selector;
1678 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1679 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1680 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1681 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1682 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1683 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1684 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301685
1686 /*
1687 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1688 * However, the SVM spec states that the G bit is not observed by the
1689 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1690 * So let's synthesize a legal G bit for all segments, this helps
1691 * running KVM nested. It also helps cross-vendor migration, because
1692 * Intel's vmentry has a check on the 'G' bit.
1693 */
1694 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001695
Joerg Roedele0231712010-02-24 18:59:10 +01001696 /*
1697 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001698 * for cross vendor migration purposes by "not present"
1699 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001700 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001701
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001702 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001703 case VCPU_SREG_TR:
1704 /*
1705 * Work around a bug where the busy flag in the tr selector
1706 * isn't exposed
1707 */
Amit Shahc0d09822008-10-27 09:04:18 +00001708 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001709 break;
1710 case VCPU_SREG_DS:
1711 case VCPU_SREG_ES:
1712 case VCPU_SREG_FS:
1713 case VCPU_SREG_GS:
1714 /*
1715 * The accessed bit must always be set in the segment
1716 * descriptor cache, although it can be cleared in the
1717 * descriptor, the cached bit always remains at 1. Since
1718 * Intel has a check on this, set it here to support
1719 * cross-vendor migration.
1720 */
1721 if (!var->unusable)
1722 var->type |= 0x1;
1723 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001724 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001725 /*
1726 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001727 * descriptor is left as 1, although the whole segment has
1728 * been made unusable. Clear it here to pass an Intel VMX
1729 * entry check when cross vendor migrating.
1730 */
1731 if (var->unusable)
1732 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001733 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001734 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001735 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001736 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001737}
1738
Izik Eidus2e4d2652008-03-24 19:38:34 +02001739static int svm_get_cpl(struct kvm_vcpu *vcpu)
1740{
1741 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1742
1743 return save->cpl;
1744}
1745
Gleb Natapov89a27f42010-02-16 10:51:48 +02001746static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001747{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001748 struct vcpu_svm *svm = to_svm(vcpu);
1749
Gleb Natapov89a27f42010-02-16 10:51:48 +02001750 dt->size = svm->vmcb->save.idtr.limit;
1751 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001752}
1753
Gleb Natapov89a27f42010-02-16 10:51:48 +02001754static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001755{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001756 struct vcpu_svm *svm = to_svm(vcpu);
1757
Gleb Natapov89a27f42010-02-16 10:51:48 +02001758 svm->vmcb->save.idtr.limit = dt->size;
1759 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001760 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761}
1762
Gleb Natapov89a27f42010-02-16 10:51:48 +02001763static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001764{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001765 struct vcpu_svm *svm = to_svm(vcpu);
1766
Gleb Natapov89a27f42010-02-16 10:51:48 +02001767 dt->size = svm->vmcb->save.gdtr.limit;
1768 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001769}
1770
Gleb Natapov89a27f42010-02-16 10:51:48 +02001771static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001772{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001773 struct vcpu_svm *svm = to_svm(vcpu);
1774
Gleb Natapov89a27f42010-02-16 10:51:48 +02001775 svm->vmcb->save.gdtr.limit = dt->size;
1776 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001777 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001778}
1779
Joerg Roedel883b0a92020-03-24 10:41:52 +01001780void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001781{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001782 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001783 u64 hcr0 = cr0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001784
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001785#ifdef CONFIG_X86_64
Tom Lendackyf1c63662020-12-14 10:29:50 -05001786 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001787 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001788 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001789 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001790 }
1791
Mike Dayd77c26f2007-10-08 09:02:08 -04001792 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001793 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001794 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001795 }
1796 }
1797#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001798 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001799
1800 if (!npt_enabled)
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001801 hcr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001802
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001803 /*
1804 * re-enable caching here because the QEMU bios
1805 * does not do it - this results in some delay at
1806 * reboot
1807 */
1808 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001809 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1810
1811 svm->vmcb->save.cr0 = hcr0;
Joerg Roedel06e78522020-06-25 10:03:23 +02001812 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001813
1814 /*
1815 * SEV-ES guests must always keep the CR intercepts cleared. CR
1816 * tracking is done using the CR write traps.
1817 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001818 if (sev_es_guest(vcpu->kvm))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001819 return;
1820
1821 if (hcr0 == cr0) {
1822 /* Selective CR0 write remains on. */
1823 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1824 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1825 } else {
1826 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1827 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1828 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001829}
1830
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001831static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1832{
1833 return true;
1834}
1835
1836void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001837{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001838 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Paolo Bonzinidc924b02020-11-15 09:44:18 -05001839 unsigned long old_cr4 = vcpu->arch.cr4;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001840
1841 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Sean Christophersonf55ac302020-03-20 14:28:12 -07001842 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001843
Joerg Roedelec077262008-04-09 14:15:28 +02001844 vcpu->arch.cr4 = cr4;
1845 if (!npt_enabled)
1846 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001847 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001848 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedel06e78522020-06-25 10:03:23 +02001849 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Jim Mattson2259c172020-10-29 10:06:48 -07001850
1851 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1852 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001853}
1854
1855static void svm_set_segment(struct kvm_vcpu *vcpu,
1856 struct kvm_segment *var, int seg)
1857{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001858 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001859 struct vmcb_seg *s = svm_seg(vcpu, seg);
1860
1861 s->base = var->base;
1862 s->limit = var->limit;
1863 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001864 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1865 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1866 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1867 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1868 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1869 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1870 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1871 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001872
1873 /*
1874 * This is always accurate, except if SYSRET returned to a segment
1875 * with SS.DPL != 3. Intel does not have this quirk, and always
1876 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1877 * would entail passing the CPL to userspace and back.
1878 */
1879 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001880 /* This is symmetric with svm_get_segment() */
1881 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882
Joerg Roedel06e78522020-06-25 10:03:23 +02001883 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001884}
1885
Jason Baronb6a7cc32021-01-14 22:27:54 -05001886static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001887{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001888 struct vcpu_svm *svm = to_svm(vcpu);
1889
Joerg Roedel18c918c2010-11-30 18:03:59 +01001890 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001891
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001892 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001893 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001894 set_exception_intercept(svm, BP_VECTOR);
Paolo Bonzini69869822020-07-10 17:48:06 +02001895 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001896}
1897
Tejun Heo0fe1e002009-10-29 22:34:14 +09001898static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001899{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001900 if (sd->next_asid > sd->max_asid) {
1901 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001902 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001903 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001904 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001905 }
1906
Cathy Avery193015a2021-01-12 11:43:13 -05001907 svm->current_vmcb->asid_generation = sd->asid_generation;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001908 svm->asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001909}
1910
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001911static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001912{
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001913 struct vmcb *vmcb = svm->vmcb;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001914
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001915 if (svm->vcpu.arch.guest_state_protected)
1916 return;
1917
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001918 if (unlikely(value != vmcb->save.dr6)) {
1919 vmcb->save.dr6 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001920 vmcb_mark_dirty(vmcb, VMCB_DR);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001921 }
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001922}
1923
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001924static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1925{
1926 struct vcpu_svm *svm = to_svm(vcpu);
1927
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001928 if (vcpu->arch.guest_state_protected)
1929 return;
1930
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001931 get_debugreg(vcpu->arch.db[0], 0);
1932 get_debugreg(vcpu->arch.db[1], 1);
1933 get_debugreg(vcpu->arch.db[2], 2);
1934 get_debugreg(vcpu->arch.db[3], 3);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001935 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001936 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001937 * because db_interception might need it. We can do it before vmentry.
1938 */
Paolo Bonzini5679b802020-05-04 11:28:25 -04001939 vcpu->arch.dr6 = svm->vmcb->save.dr6;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001940 vcpu->arch.dr7 = svm->vmcb->save.dr7;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001941 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1942 set_dr_intercepts(svm);
1943}
1944
Gleb Natapov020df072010-04-13 10:05:23 +03001945static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001946{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001947 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001948
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001949 if (vcpu->arch.guest_state_protected)
1950 return;
1951
Gleb Natapov020df072010-04-13 10:05:23 +03001952 svm->vmcb->save.dr7 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001953 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001954}
1955
Paolo Bonzini63129752021-03-02 14:40:39 -05001956static int pf_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001957{
Paolo Bonzini63129752021-03-02 14:40:39 -05001958 struct vcpu_svm *svm = to_svm(vcpu);
1959
Sean Christopherson6d1b8672021-03-04 17:10:56 -08001960 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07001961 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001962
Paolo Bonzini63129752021-03-02 14:40:39 -05001963 return kvm_handle_page_fault(vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001964 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1965 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001966 svm->vmcb->control.insn_len);
1967}
1968
Paolo Bonzini63129752021-03-02 14:40:39 -05001969static int npf_interception(struct kvm_vcpu *vcpu)
Paolo Bonzinid0006532017-08-11 18:36:43 +02001970{
Paolo Bonzini63129752021-03-02 14:40:39 -05001971 struct vcpu_svm *svm = to_svm(vcpu);
1972
Sean Christopherson76ff3712021-06-24 19:03:54 -07001973 u64 fault_address = svm->vmcb->control.exit_info_2;
Paolo Bonzinid0006532017-08-11 18:36:43 +02001974 u64 error_code = svm->vmcb->control.exit_info_1;
1975
1976 trace_kvm_page_fault(fault_address, error_code);
Paolo Bonzini63129752021-03-02 14:40:39 -05001977 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001978 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1979 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001980 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001981}
1982
Paolo Bonzini63129752021-03-02 14:40:39 -05001983static int db_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001984{
Paolo Bonzini63129752021-03-02 14:40:39 -05001985 struct kvm_run *kvm_run = vcpu->run;
1986 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001987
Paolo Bonzini63129752021-03-02 14:40:39 -05001988 if (!(vcpu->guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001989 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001990 !svm->nmi_singlestep) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001991 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
Paolo Bonzini63129752021-03-02 14:40:39 -05001992 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001993 return 1;
1994 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001995
Jan Kiszka6be7d302009-10-18 13:24:54 +02001996 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001997 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02001998 /* Make sure we check for pending NMIs upon entry */
1999 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03002000 }
2001
Paolo Bonzini63129752021-03-02 14:40:39 -05002002 if (vcpu->guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002003 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002004 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Paolo Bonzinidee919d2020-05-04 09:34:10 -04002005 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
2006 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
Gleb Natapov44c11432009-05-11 13:35:52 +03002007 kvm_run->debug.arch.pc =
2008 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2009 kvm_run->debug.arch.exception = DB_VECTOR;
2010 return 0;
2011 }
2012
2013 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002014}
2015
Paolo Bonzini63129752021-03-02 14:40:39 -05002016static int bp_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002017{
Paolo Bonzini63129752021-03-02 14:40:39 -05002018 struct vcpu_svm *svm = to_svm(vcpu);
2019 struct kvm_run *kvm_run = vcpu->run;
Avi Kivity851ba692009-08-24 11:10:17 +03002020
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002021 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2022 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2023 kvm_run->debug.arch.exception = BP_VECTOR;
2024 return 0;
2025}
2026
Paolo Bonzini63129752021-03-02 14:40:39 -05002027static int ud_interception(struct kvm_vcpu *vcpu)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002028{
Paolo Bonzini63129752021-03-02 14:40:39 -05002029 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002030}
2031
Paolo Bonzini63129752021-03-02 14:40:39 -05002032static int ac_interception(struct kvm_vcpu *vcpu)
Eric Northup54a20552015-11-03 18:03:53 +01002033{
Paolo Bonzini63129752021-03-02 14:40:39 -05002034 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
Eric Northup54a20552015-11-03 18:03:53 +01002035 return 1;
2036}
2037
Joerg Roedel67ec6602010-05-17 14:43:35 +02002038static bool is_erratum_383(void)
2039{
2040 int err, i;
2041 u64 value;
2042
2043 if (!erratum_383_found)
2044 return false;
2045
2046 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2047 if (err)
2048 return false;
2049
2050 /* Bit 62 may or may not be set for this mce */
2051 value &= ~(1ULL << 62);
2052
2053 if (value != 0xb600000000010015ULL)
2054 return false;
2055
2056 /* Clear MCi_STATUS registers */
2057 for (i = 0; i < 6; ++i)
2058 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2059
2060 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2061 if (!err) {
2062 u32 low, high;
2063
2064 value &= ~(1ULL << 2);
2065 low = lower_32_bits(value);
2066 high = upper_32_bits(value);
2067
2068 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2069 }
2070
2071 /* Flush tlb to evict multi-match entries */
2072 __flush_tlb_all();
2073
2074 return true;
2075}
2076
Paolo Bonzini63129752021-03-02 14:40:39 -05002077static void svm_handle_mce(struct kvm_vcpu *vcpu)
Joerg Roedel53371b52008-04-09 14:15:30 +02002078{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002079 if (is_erratum_383()) {
2080 /*
2081 * Erratum 383 triggered. Guest state is corrupt so kill the
2082 * guest.
2083 */
2084 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2085
Paolo Bonzini63129752021-03-02 14:40:39 -05002086 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002087
2088 return;
2089 }
2090
Joerg Roedel53371b52008-04-09 14:15:30 +02002091 /*
2092 * On an #MC intercept the MCE handler is not called automatically in
2093 * the host. So do it by hand here.
2094 */
Uros Bizjak1c164cb2020-04-11 17:36:27 +02002095 kvm_machine_check();
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002096}
2097
Paolo Bonzini63129752021-03-02 14:40:39 -05002098static int mc_interception(struct kvm_vcpu *vcpu)
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002099{
Joerg Roedel53371b52008-04-09 14:15:30 +02002100 return 1;
2101}
2102
Paolo Bonzini63129752021-03-02 14:40:39 -05002103static int shutdown_interception(struct kvm_vcpu *vcpu)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002104{
Paolo Bonzini63129752021-03-02 14:40:39 -05002105 struct kvm_run *kvm_run = vcpu->run;
2106 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002107
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002108 /*
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002109 * The VM save area has already been encrypted so it
2110 * cannot be reinitialized - just terminate.
2111 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002112 if (sev_es_guest(vcpu->kvm))
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002113 return -EINVAL;
2114
2115 /*
Sean Christopherson265e4352021-07-13 09:33:22 -07002116 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
2117 * the VMCB in a known good state. Unfortuately, KVM doesn't have
2118 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
2119 * userspace. At a platform view, INIT is acceptable behavior as
2120 * there exist bare metal platforms that automatically INIT the CPU
2121 * in response to shutdown.
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002122 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002123 clear_page(svm->vmcb);
Sean Christopherson265e4352021-07-13 09:33:22 -07002124 kvm_vcpu_reset(vcpu, true);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002125
2126 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2127 return 0;
2128}
2129
Paolo Bonzini63129752021-03-02 14:40:39 -05002130static int io_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002131{
Paolo Bonzini63129752021-03-02 14:40:39 -05002132 struct vcpu_svm *svm = to_svm(vcpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002133 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002134 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002135 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002136
Paolo Bonzini63129752021-03-02 14:40:39 -05002137 ++vcpu->stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002138 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002139 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2140 port = io_info >> 16;
2141 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002142
2143 if (string) {
2144 if (sev_es_guest(vcpu->kvm))
2145 return sev_es_string_io(svm, size, port, in);
2146 else
2147 return kvm_emulate_instruction(vcpu, 0);
2148 }
2149
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002150 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002151
Paolo Bonzini63129752021-03-02 14:40:39 -05002152 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002153}
2154
Paolo Bonzini63129752021-03-02 14:40:39 -05002155static int nmi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002156{
2157 return 1;
2158}
2159
Maxim Levitsky991afbb2021-07-07 15:50:58 +03002160static int smi_interception(struct kvm_vcpu *vcpu)
2161{
2162 return 1;
2163}
2164
Paolo Bonzini63129752021-03-02 14:40:39 -05002165static int intr_interception(struct kvm_vcpu *vcpu)
Joerg Roedela0698052008-04-30 17:56:01 +02002166{
Paolo Bonzini63129752021-03-02 14:40:39 -05002167 ++vcpu->stat.irq_exits;
Joerg Roedela0698052008-04-30 17:56:01 +02002168 return 1;
2169}
2170
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002171static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002172{
Paolo Bonzini63129752021-03-02 14:40:39 -05002173 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002174 struct vmcb *vmcb12;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002175 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002176 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002177
Paolo Bonzini63129752021-03-02 14:40:39 -05002178 if (nested_svm_check_permissions(vcpu))
Alexander Graf55426752008-11-25 20:17:06 +01002179 return 1;
2180
Paolo Bonzini63129752021-03-02 14:40:39 -05002181 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002182 if (ret) {
2183 if (ret == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -05002184 kvm_inject_gp(vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002185 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002186 }
2187
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002188 vmcb12 = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002189
Paolo Bonzini63129752021-03-02 14:40:39 -05002190 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002191
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002192 if (vmload) {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02002193 svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002194 svm->sysenter_eip_hi = 0;
2195 svm->sysenter_esp_hi = 0;
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02002196 } else {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02002197 svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02002198 }
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002199
Paolo Bonzini63129752021-03-02 14:40:39 -05002200 kvm_vcpu_unmap(vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002201
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002202 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002203}
2204
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002205static int vmload_interception(struct kvm_vcpu *vcpu)
Alexander Graf55426752008-11-25 20:17:06 +01002206{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002207 return vmload_vmsave_interception(vcpu, true);
Alexander Graf55426752008-11-25 20:17:06 +01002208}
2209
Paolo Bonzini63129752021-03-02 14:40:39 -05002210static int vmsave_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002211{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002212 return vmload_vmsave_interception(vcpu, false);
Alexander Grafc0725422008-11-25 20:17:03 +01002213}
2214
Paolo Bonzini63129752021-03-02 14:40:39 -05002215static int vmrun_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002216{
Paolo Bonzini63129752021-03-02 14:40:39 -05002217 if (nested_svm_check_permissions(vcpu))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002218 return 1;
2219
Paolo Bonzini63129752021-03-02 14:40:39 -05002220 return nested_svm_vmrun(vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002221}
2222
Bandan Das82a11e9c2021-01-26 03:18:29 -05002223enum {
2224 NONE_SVM_INSTR,
2225 SVM_INSTR_VMRUN,
2226 SVM_INSTR_VMLOAD,
2227 SVM_INSTR_VMSAVE,
2228};
2229
2230/* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2231static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2232{
2233 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2234
2235 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2236 return NONE_SVM_INSTR;
2237
2238 switch (ctxt->modrm) {
2239 case 0xd8: /* VMRUN */
2240 return SVM_INSTR_VMRUN;
2241 case 0xda: /* VMLOAD */
2242 return SVM_INSTR_VMLOAD;
2243 case 0xdb: /* VMSAVE */
2244 return SVM_INSTR_VMSAVE;
2245 default:
2246 break;
2247 }
2248
2249 return NONE_SVM_INSTR;
2250}
2251
2252static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2253{
Wei Huang14c2bf82021-01-26 03:18:31 -05002254 const int guest_mode_exit_codes[] = {
2255 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2256 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2257 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2258 };
Paolo Bonzini63129752021-03-02 14:40:39 -05002259 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
Bandan Das82a11e9c2021-01-26 03:18:29 -05002260 [SVM_INSTR_VMRUN] = vmrun_interception,
2261 [SVM_INSTR_VMLOAD] = vmload_interception,
2262 [SVM_INSTR_VMSAVE] = vmsave_interception,
2263 };
2264 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002265 int ret;
Bandan Das82a11e9c2021-01-26 03:18:29 -05002266
Wei Huang14c2bf82021-01-26 03:18:31 -05002267 if (is_guest_mode(vcpu)) {
Sean Christopherson2df8d382021-02-23 16:56:26 -08002268 /* Returns '1' or -errno on failure, '0' on success. */
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08002269 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002270 if (ret)
2271 return ret;
2272 return 1;
2273 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002274 return svm_instr_handlers[opcode](vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002275}
2276
2277/*
2278 * #GP handling code. Note that #GP can be triggered under the following two
2279 * cases:
2280 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2281 * some AMD CPUs when EAX of these instructions are in the reserved memory
2282 * regions (e.g. SMM memory on host).
2283 * 2) VMware backdoor
2284 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002285static int gp_interception(struct kvm_vcpu *vcpu)
Bandan Das82a11e9c2021-01-26 03:18:29 -05002286{
Paolo Bonzini63129752021-03-02 14:40:39 -05002287 struct vcpu_svm *svm = to_svm(vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002288 u32 error_code = svm->vmcb->control.exit_info_1;
2289 int opcode;
2290
2291 /* Both #GP cases have zero error_code */
2292 if (error_code)
2293 goto reinject;
2294
Maxim Levitskyd1cba6c2021-09-14 18:48:14 +03002295 /* All SVM instructions expect page aligned RAX */
2296 if (svm->vmcb->save.rax & ~PAGE_MASK)
2297 goto reinject;
2298
Bandan Das82a11e9c2021-01-26 03:18:29 -05002299 /* Decode the instruction for usage later */
2300 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2301 goto reinject;
2302
2303 opcode = svm_instr_opcode(vcpu);
2304
2305 if (opcode == NONE_SVM_INSTR) {
2306 if (!enable_vmware_backdoor)
2307 goto reinject;
2308
2309 /*
2310 * VMware backdoor emulation on #GP interception only handles
2311 * IN{S}, OUT{S}, and RDPMC.
2312 */
Wei Huang14c2bf82021-01-26 03:18:31 -05002313 if (!is_guest_mode(vcpu))
2314 return kvm_emulate_instruction(vcpu,
Bandan Das82a11e9c2021-01-26 03:18:29 -05002315 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2316 } else
2317 return emulate_svm_instr(vcpu, opcode);
2318
2319reinject:
2320 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2321 return 1;
2322}
2323
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002324void svm_set_gif(struct vcpu_svm *svm, bool value)
2325{
2326 if (value) {
2327 /*
2328 * If VGIF is enabled, the STGI intercept is only added to
2329 * detect the opening of the SMI/NMI window; remove it now.
2330 * Likewise, clear the VINTR intercept, we will set it
2331 * again while processing KVM_REQ_EVENT if needed.
2332 */
2333 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02002334 svm_clr_intercept(svm, INTERCEPT_STGI);
2335 if (svm_is_intercept(svm, INTERCEPT_VINTR))
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002336 svm_clear_vintr(svm);
2337
2338 enable_gif(svm);
2339 if (svm->vcpu.arch.smi_pending ||
2340 svm->vcpu.arch.nmi_pending ||
2341 kvm_cpu_has_injectable_intr(&svm->vcpu))
2342 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2343 } else {
2344 disable_gif(svm);
2345
2346 /*
2347 * After a CLGI no interrupts should come. But if vGIF is
2348 * in use, we still rely on the VINTR intercept (rather than
2349 * STGI) to detect an open interrupt window.
2350 */
2351 if (!vgif_enabled(svm))
2352 svm_clear_vintr(svm);
2353 }
2354}
2355
Paolo Bonzini63129752021-03-02 14:40:39 -05002356static int stgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002357{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002358 int ret;
2359
Paolo Bonzini63129752021-03-02 14:40:39 -05002360 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002361 return 1;
2362
Paolo Bonzini63129752021-03-02 14:40:39 -05002363 ret = kvm_skip_emulated_instruction(vcpu);
2364 svm_set_gif(to_svm(vcpu), true);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002365 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002366}
2367
Paolo Bonzini63129752021-03-02 14:40:39 -05002368static int clgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002369{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002370 int ret;
2371
Paolo Bonzini63129752021-03-02 14:40:39 -05002372 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002373 return 1;
2374
Paolo Bonzini63129752021-03-02 14:40:39 -05002375 ret = kvm_skip_emulated_instruction(vcpu);
2376 svm_set_gif(to_svm(vcpu), false);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002377 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002378}
2379
Paolo Bonzini63129752021-03-02 14:40:39 -05002380static int invlpga_interception(struct kvm_vcpu *vcpu)
Alexander Grafff092382009-06-15 15:21:24 +02002381{
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002382 gva_t gva = kvm_rax_read(vcpu);
2383 u32 asid = kvm_rcx_read(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002384
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002385 /* FIXME: Handle an address size prefix. */
2386 if (!is_long_mode(vcpu))
2387 gva = (u32)gva;
2388
2389 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
Joerg Roedelec1ff792009-10-09 16:08:31 +02002390
Alexander Grafff092382009-06-15 15:21:24 +02002391 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002392 kvm_mmu_invlpg(vcpu, gva);
Alexander Grafff092382009-06-15 15:21:24 +02002393
Paolo Bonzini63129752021-03-02 14:40:39 -05002394 return kvm_skip_emulated_instruction(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002395}
2396
Paolo Bonzini63129752021-03-02 14:40:39 -05002397static int skinit_interception(struct kvm_vcpu *vcpu)
Joerg Roedel532a46b2009-10-09 16:08:32 +02002398{
Paolo Bonzini63129752021-03-02 14:40:39 -05002399 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002400
Paolo Bonzini63129752021-03-02 14:40:39 -05002401 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel532a46b2009-10-09 16:08:32 +02002402 return 1;
2403}
2404
Paolo Bonzini63129752021-03-02 14:40:39 -05002405static int task_switch_interception(struct kvm_vcpu *vcpu)
David Kaplandab429a2015-03-02 13:43:37 -06002406{
Paolo Bonzini63129752021-03-02 14:40:39 -05002407 struct vcpu_svm *svm = to_svm(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002408 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002409 int reason;
2410 int int_type = svm->vmcb->control.exit_int_info &
2411 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002412 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002413 uint32_t type =
2414 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2415 uint32_t idt_v =
2416 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002417 bool has_error_code = false;
2418 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002419
2420 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002421
Izik Eidus37817f22008-03-24 23:14:53 +02002422 if (svm->vmcb->control.exit_info_2 &
2423 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002424 reason = TASK_SWITCH_IRET;
2425 else if (svm->vmcb->control.exit_info_2 &
2426 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2427 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002428 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002429 reason = TASK_SWITCH_GATE;
2430 else
2431 reason = TASK_SWITCH_CALL;
2432
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002433 if (reason == TASK_SWITCH_GATE) {
2434 switch (type) {
2435 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05002436 vcpu->arch.nmi_injected = false;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002437 break;
2438 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002439 if (svm->vmcb->control.exit_info_2 &
2440 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2441 has_error_code = true;
2442 error_code =
2443 (u32)svm->vmcb->control.exit_info_2;
2444 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002445 kvm_clear_exception_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002446 break;
2447 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05002448 kvm_clear_interrupt_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002449 break;
2450 default:
2451 break;
2452 }
2453 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002454
Gleb Natapov8317c292009-04-12 13:37:02 +03002455 if (reason != TASK_SWITCH_GATE ||
2456 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2457 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002458 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002459 if (!skip_emulated_instruction(vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002460 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002461 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002462
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002463 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2464 int_vec = -1;
2465
Paolo Bonzini63129752021-03-02 14:40:39 -05002466 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002467 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002468}
2469
Paolo Bonzini63129752021-03-02 14:40:39 -05002470static int iret_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471{
Paolo Bonzini63129752021-03-02 14:40:39 -05002472 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002473
Paolo Bonzini63129752021-03-02 14:40:39 -05002474 ++vcpu->stat.nmi_window_exits;
2475 vcpu->arch.hflags |= HF_IRET_MASK;
2476 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002477 svm_clr_intercept(svm, INTERCEPT_IRET);
Paolo Bonzini63129752021-03-02 14:40:39 -05002478 svm->nmi_iret_rip = kvm_rip_read(vcpu);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002479 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002480 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002481 return 1;
2482}
2483
Paolo Bonzini63129752021-03-02 14:40:39 -05002484static int invlpg_interception(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002485{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002486 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002487 return kvm_emulate_instruction(vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002488
Paolo Bonzini63129752021-03-02 14:40:39 -05002489 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2490 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002491}
2492
Paolo Bonzini63129752021-03-02 14:40:39 -05002493static int emulate_on_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494{
Paolo Bonzini63129752021-03-02 14:40:39 -05002495 return kvm_emulate_instruction(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002496}
2497
Paolo Bonzini63129752021-03-02 14:40:39 -05002498static int rsm_interception(struct kvm_vcpu *vcpu)
Brijesh Singh7607b712018-02-19 10:14:44 -06002499{
Paolo Bonzini63129752021-03-02 14:40:39 -05002500 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002501}
2502
Paolo Bonzini63129752021-03-02 14:40:39 -05002503static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
Xiubo Li52eb5a62015-03-13 17:39:45 +08002504 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002505{
Paolo Bonzini63129752021-03-02 14:40:39 -05002506 struct vcpu_svm *svm = to_svm(vcpu);
2507 unsigned long cr0 = vcpu->arch.cr0;
Joerg Roedel628afd22011-04-04 12:39:36 +02002508 bool ret = false;
Joerg Roedel628afd22011-04-04 12:39:36 +02002509
Paolo Bonzini63129752021-03-02 14:40:39 -05002510 if (!is_guest_mode(vcpu) ||
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04002511 (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
Joerg Roedel628afd22011-04-04 12:39:36 +02002512 return false;
2513
2514 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2515 val &= ~SVM_CR0_SELECTIVE_MASK;
2516
2517 if (cr0 ^ val) {
2518 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2519 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2520 }
2521
2522 return ret;
2523}
2524
Andre Przywara7ff76d52010-12-21 11:12:04 +01002525#define CR_VALID (1ULL << 63)
2526
Paolo Bonzini63129752021-03-02 14:40:39 -05002527static int cr_interception(struct kvm_vcpu *vcpu)
Andre Przywara7ff76d52010-12-21 11:12:04 +01002528{
Paolo Bonzini63129752021-03-02 14:40:39 -05002529 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002530 int reg, cr;
2531 unsigned long val;
2532 int err;
2533
2534 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002535 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002536
2537 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
Paolo Bonzini63129752021-03-02 14:40:39 -05002538 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002539
2540 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002541 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2542 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2543 else
2544 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002545
2546 err = 0;
2547 if (cr >= 16) { /* mov to cr */
2548 cr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002549 val = kvm_register_read(vcpu, reg);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002550 trace_kvm_cr_write(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002551 switch (cr) {
2552 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002553 if (!check_selective_cr0_intercepted(vcpu, val))
2554 err = kvm_set_cr0(vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002555 else
2556 return 1;
2557
Andre Przywara7ff76d52010-12-21 11:12:04 +01002558 break;
2559 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002560 err = kvm_set_cr3(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002561 break;
2562 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002563 err = kvm_set_cr4(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002564 break;
2565 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002566 err = kvm_set_cr8(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002567 break;
2568 default:
2569 WARN(1, "unhandled write to CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002570 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002571 return 1;
2572 }
2573 } else { /* mov from cr */
2574 switch (cr) {
2575 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002576 val = kvm_read_cr0(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002577 break;
2578 case 2:
Paolo Bonzini63129752021-03-02 14:40:39 -05002579 val = vcpu->arch.cr2;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002580 break;
2581 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002582 val = kvm_read_cr3(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002583 break;
2584 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002585 val = kvm_read_cr4(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002586 break;
2587 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002588 val = kvm_get_cr8(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002589 break;
2590 default:
2591 WARN(1, "unhandled read from CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002592 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002593 return 1;
2594 }
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002595 kvm_register_write(vcpu, reg, val);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002596 trace_kvm_cr_read(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002597 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002598 return kvm_complete_insn_gp(vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002599}
2600
Paolo Bonzini63129752021-03-02 14:40:39 -05002601static int cr_trap(struct kvm_vcpu *vcpu)
Tom Lendackyf27ad382020-12-10 11:09:56 -06002602{
Paolo Bonzini63129752021-03-02 14:40:39 -05002603 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002604 unsigned long old_value, new_value;
2605 unsigned int cr;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002606 int ret = 0;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002607
2608 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2609
2610 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2611 switch (cr) {
2612 case 0:
2613 old_value = kvm_read_cr0(vcpu);
2614 svm_set_cr0(vcpu, new_value);
2615
2616 kvm_post_set_cr0(vcpu, old_value, new_value);
2617 break;
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002618 case 4:
2619 old_value = kvm_read_cr4(vcpu);
2620 svm_set_cr4(vcpu, new_value);
2621
2622 kvm_post_set_cr4(vcpu, old_value, new_value);
2623 break;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002624 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002625 ret = kvm_set_cr8(vcpu, new_value);
Tom Lendackyd1949b92020-12-10 11:09:58 -06002626 break;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002627 default:
2628 WARN(1, "unhandled CR%d write trap", cr);
2629 kvm_queue_exception(vcpu, UD_VECTOR);
2630 return 1;
2631 }
2632
Tom Lendackyd1949b92020-12-10 11:09:58 -06002633 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002634}
2635
Paolo Bonzini63129752021-03-02 14:40:39 -05002636static int dr_interception(struct kvm_vcpu *vcpu)
Andre Przywaracae37972010-12-21 11:12:05 +01002637{
Paolo Bonzini63129752021-03-02 14:40:39 -05002638 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002639 int reg, dr;
2640 unsigned long val;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002641 int err = 0;
Andre Przywaracae37972010-12-21 11:12:05 +01002642
Paolo Bonzini63129752021-03-02 14:40:39 -05002643 if (vcpu->guest_debug == 0) {
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002644 /*
2645 * No more DR vmexits; force a reload of the debug registers
2646 * and reenter on this instruction. The next vmexit will
2647 * retrieve the full state of the debug registers.
2648 */
2649 clr_dr_intercepts(svm);
Paolo Bonzini63129752021-03-02 14:40:39 -05002650 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002651 return 1;
2652 }
2653
Andre Przywaracae37972010-12-21 11:12:05 +01002654 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002655 return emulate_on_interception(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002656
2657 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2658 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002659 if (dr >= 16) { /* mov to DRn */
2660 dr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002661 val = kvm_register_read(vcpu, reg);
Paolo Bonzini63129752021-03-02 14:40:39 -05002662 err = kvm_set_dr(vcpu, dr, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002663 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05002664 kvm_get_dr(vcpu, dr, &val);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002665 kvm_register_write(vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002666 }
2667
Paolo Bonzini63129752021-03-02 14:40:39 -05002668 return kvm_complete_insn_gp(vcpu, err);
Andre Przywaracae37972010-12-21 11:12:05 +01002669}
2670
Paolo Bonzini63129752021-03-02 14:40:39 -05002671static int cr8_write_interception(struct kvm_vcpu *vcpu)
Joerg Roedel1d075432007-12-06 21:02:25 +01002672{
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002673 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002674
Paolo Bonzini63129752021-03-02 14:40:39 -05002675 u8 cr8_prev = kvm_get_cr8(vcpu);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002676 /* instruction emulation calls kvm_set_cr8() */
Paolo Bonzini63129752021-03-02 14:40:39 -05002677 r = cr_interception(vcpu);
2678 if (lapic_in_kernel(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002679 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002680 if (cr8_prev <= kvm_get_cr8(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002681 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002682 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Joerg Roedel1d075432007-12-06 21:02:25 +01002683 return 0;
2684}
2685
Paolo Bonzini63129752021-03-02 14:40:39 -05002686static int efer_trap(struct kvm_vcpu *vcpu)
Tom Lendacky2985afb2020-12-10 11:09:55 -06002687{
2688 struct msr_data msr_info;
2689 int ret;
2690
2691 /*
2692 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2693 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2694 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2695 * the guest doesn't have X86_FEATURE_SVM.
2696 */
2697 msr_info.host_initiated = false;
2698 msr_info.index = MSR_EFER;
Paolo Bonzini63129752021-03-02 14:40:39 -05002699 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2700 ret = kvm_set_msr_common(vcpu, &msr_info);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002701
Paolo Bonzini63129752021-03-02 14:40:39 -05002702 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002703}
2704
Tom Lendacky801e4592018-02-21 13:39:51 -06002705static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2706{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002707 msr->data = 0;
2708
2709 switch (msr->index) {
2710 case MSR_F10H_DECFG:
2711 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2712 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2713 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02002714 case MSR_IA32_PERF_CAPABILITIES:
2715 return 0;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002716 default:
Peter Xu12bc2132020-06-22 18:04:42 -04002717 return KVM_MSR_RET_INVALID;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002718 }
2719
2720 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002721}
2722
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002723static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002725 struct vcpu_svm *svm = to_svm(vcpu);
2726
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002727 switch (msr_info->index) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002728 case MSR_AMD64_TSC_RATIO:
2729 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2730 return 1;
2731 msr_info->data = svm->tsc_ratio_msr;
2732 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002733 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002734 msr_info->data = svm->vmcb01.ptr->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002736#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002738 msr_info->data = svm->vmcb01.ptr->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 break;
2740 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002741 msr_info->data = svm->vmcb01.ptr->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002742 break;
2743 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002744 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002745 break;
2746 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002747 msr_info->data = svm->vmcb01.ptr->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002748 break;
2749#endif
2750 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002751 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 break;
2753 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002754 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2755 if (guest_cpuid_is_intel(vcpu))
2756 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757 break;
2758 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002759 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2760 if (guest_cpuid_is_intel(vcpu))
2761 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002763 case MSR_TSC_AUX:
Paolo Bonzini46896c72015-11-12 14:49:16 +01002764 msr_info->data = svm->tsc_aux;
2765 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002766 /*
2767 * Nobody will change the following 5 values in the VMCB so we can
2768 * safely return them on rdmsr. They will always be 0 until LBRV is
2769 * implemented.
2770 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002771 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002772 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002773 break;
2774 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002775 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002776 break;
2777 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002778 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002779 break;
2780 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002781 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002782 break;
2783 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002784 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002785 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002786 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002787 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002788 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002789 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002790 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002791 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002792 case MSR_IA32_SPEC_CTRL:
2793 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002794 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002795 return 1;
2796
Babu Mogerd00b99c2021-02-17 10:56:04 -05002797 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2798 msr_info->data = svm->vmcb->save.spec_ctrl;
2799 else
2800 msr_info->data = svm->spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002801 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002802 case MSR_AMD64_VIRT_SPEC_CTRL:
2803 if (!msr_info->host_initiated &&
2804 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2805 return 1;
2806
2807 msr_info->data = svm->virt_spec_ctrl;
2808 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002809 case MSR_F15H_IC_CFG: {
2810
2811 int family, model;
2812
2813 family = guest_cpuid_family(vcpu);
2814 model = guest_cpuid_model(vcpu);
2815
2816 if (family < 0 || model < 0)
2817 return kvm_get_msr_common(vcpu, msr_info);
2818
2819 msr_info->data = 0;
2820
2821 if (family == 0x15 &&
2822 (model >= 0x2 && model < 0x20))
2823 msr_info->data = 0x1E;
2824 }
2825 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002826 case MSR_F10H_DECFG:
2827 msr_info->data = svm->msr_decfg;
2828 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002830 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002831 }
2832 return 0;
2833}
2834
Tom Lendackyf1c63662020-12-14 10:29:50 -05002835static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2836{
2837 struct vcpu_svm *svm = to_svm(vcpu);
Peter Gondab67a4cc2021-10-21 10:42:59 -07002838 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
Paolo Bonzini63129752021-03-02 14:40:39 -05002839 return kvm_complete_insn_gp(vcpu, err);
Tom Lendackyf1c63662020-12-14 10:29:50 -05002840
Peter Gondab67a4cc2021-10-21 10:42:59 -07002841 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
2842 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
Tom Lendackyf1c63662020-12-14 10:29:50 -05002843 X86_TRAP_GP |
2844 SVM_EVTINJ_TYPE_EXEPT |
2845 SVM_EVTINJ_VALID);
2846 return 1;
2847}
2848
Joerg Roedel4a810182010-02-24 18:59:15 +01002849static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2850{
2851 struct vcpu_svm *svm = to_svm(vcpu);
2852 int svm_dis, chg_mask;
2853
2854 if (data & ~SVM_VM_CR_VALID_MASK)
2855 return 1;
2856
2857 chg_mask = SVM_VM_CR_VALID_MASK;
2858
2859 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2860 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2861
2862 svm->nested.vm_cr_msr &= ~chg_mask;
2863 svm->nested.vm_cr_msr |= (data & chg_mask);
2864
2865 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2866
2867 /* check for svm_disable while efer.svme is set */
2868 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2869 return 1;
2870
2871 return 0;
2872}
2873
Will Auld8fe8ab42012-11-29 12:42:12 -08002874static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002875{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002876 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002877 int r;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002878
Will Auld8fe8ab42012-11-29 12:42:12 -08002879 u32 ecx = msr->index;
2880 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881 switch (ecx) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002882 case MSR_AMD64_TSC_RATIO:
2883 if (!msr->host_initiated && !svm->tsc_scaling_enabled)
2884 return 1;
2885
2886 if (data & TSC_RATIO_RSVD)
2887 return 1;
2888
2889 svm->tsc_ratio_msr = data;
2890
2891 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2892 nested_svm_update_tsc_ratio_msr(vcpu);
2893
2894 break;
Paolo Bonzini15038e12017-10-26 09:13:27 +02002895 case MSR_IA32_CR_PAT:
2896 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2897 return 1;
2898 vcpu->arch.pat = data;
Cathy Avery4995a362021-01-13 07:07:52 -05002899 svm->vmcb01.ptr->save.g_pat = data;
2900 if (is_guest_mode(vcpu))
2901 nested_vmcb02_compute_g_pat(svm);
Joerg Roedel06e78522020-06-25 10:03:23 +02002902 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Paolo Bonzini15038e12017-10-26 09:13:27 +02002903 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002904 case MSR_IA32_SPEC_CTRL:
2905 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002906 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002907 return 1;
2908
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002909 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002910 return 1;
2911
Babu Mogerd00b99c2021-02-17 10:56:04 -05002912 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2913 svm->vmcb->save.spec_ctrl = data;
2914 else
2915 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002916 if (!data)
2917 break;
2918
2919 /*
2920 * For non-nested:
2921 * When it's written (to non-zero) for the first time, pass
2922 * it through.
2923 *
2924 * For nested:
2925 * The handling of the MSR bitmap for L2 guests is done in
2926 * nested_svm_vmrun_msrpm.
2927 * We update the L1 MSR bit as well since it will end up
2928 * touching the MSR anyway now.
2929 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002930 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002931 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002932 case MSR_IA32_PRED_CMD:
2933 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002934 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002935 return 1;
2936
2937 if (data & ~PRED_CMD_IBPB)
2938 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002939 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002940 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002941 if (!data)
2942 break;
2943
2944 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002945 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
Ashok Raj15d45072018-02-01 22:59:43 +01002946 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002947 case MSR_AMD64_VIRT_SPEC_CTRL:
2948 if (!msr->host_initiated &&
2949 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2950 return 1;
2951
2952 if (data & ~SPEC_CTRL_SSBD)
2953 return 1;
2954
2955 svm->virt_spec_ctrl = data;
2956 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002957 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002958 svm->vmcb01.ptr->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002960#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002962 svm->vmcb01.ptr->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002963 break;
2964 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002965 svm->vmcb01.ptr->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002966 break;
2967 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002968 svm->vmcb01.ptr->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002969 break;
2970 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002971 svm->vmcb01.ptr->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 break;
2973#endif
2974 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002975 svm->vmcb01.ptr->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002976 break;
2977 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002978 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2979 /*
2980 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2981 * when we spoof an Intel vendor ID (for cross vendor migration).
2982 * In this case we use this intercept to track the high
2983 * 32 bit part of these msrs to support Intel's
2984 * implementation of SYSENTER/SYSEXIT.
2985 */
2986 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002987 break;
2988 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002989 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2990 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002991 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002992 case MSR_TSC_AUX:
Sean Christophersondbd61272021-04-23 15:34:02 -07002993 /*
Sean Christopherson844d69c2021-04-23 15:34:04 -07002994 * TSC_AUX is usually changed only during boot and never read
2995 * directly. Intercept TSC_AUX instead of exposing it to the
2996 * guest via direct_access_msrs, and switch it via user return.
Paolo Bonzini46896c72015-11-12 14:49:16 +01002997 */
Sean Christopherson844d69c2021-04-23 15:34:04 -07002998 preempt_disable();
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002999 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
Sean Christopherson844d69c2021-04-23 15:34:04 -07003000 preempt_enable();
3001 if (r)
3002 return 1;
3003
Paolo Bonzini46896c72015-11-12 14:49:16 +01003004 svm->tsc_aux = data;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003005 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003006 case MSR_IA32_DEBUGCTLMSR:
Maxim Levitsky4c849262021-09-14 18:48:19 +03003007 if (!lbrv) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003008 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3009 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003010 break;
3011 }
3012 if (data & DEBUGCTL_RESERVED_BITS)
3013 return 1;
3014
3015 svm->vmcb->save.dbgctl = data;
Joerg Roedel06e78522020-06-25 10:03:23 +02003016 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003017 if (data & (1ULL<<0))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003018 svm_enable_lbrv(vcpu);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003019 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003020 svm_disable_lbrv(vcpu);
Joerg Roedela2938c82008-02-13 16:30:28 +01003021 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003022 case MSR_VM_HSAVE_PA:
Vitaly Kuznetsovfce7e152021-06-28 12:44:20 +02003023 /*
3024 * Old kernels did not validate the value written to
3025 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
3026 * value to allow live migrating buggy or malicious guests
3027 * originating from those kernels.
3028 */
3029 if (!msr->host_initiated && !page_address_valid(vcpu, data))
3030 return 1;
3031
3032 svm->nested.hsave_msr = data & PAGE_MASK;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003033 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003034 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003035 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003036 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003037 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003038 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003039 case MSR_F10H_DECFG: {
3040 struct kvm_msr_entry msr_entry;
3041
3042 msr_entry.index = msr->index;
3043 if (svm_get_msr_feature(&msr_entry))
3044 return 1;
3045
3046 /* Check the supported bits */
3047 if (data & ~msr_entry.data)
3048 return 1;
3049
3050 /* Don't allow the guest to change a bit, #GP */
3051 if (!msr->host_initiated && (data ^ msr_entry.data))
3052 return 1;
3053
3054 svm->msr_decfg = data;
3055 break;
3056 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003058 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 }
3060 return 0;
3061}
3062
Paolo Bonzini63129752021-03-02 14:40:39 -05003063static int msr_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003064{
Paolo Bonzini63129752021-03-02 14:40:39 -05003065 if (to_svm(vcpu)->vmcb->control.exit_info_1)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003066 return kvm_emulate_wrmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067 else
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003068 return kvm_emulate_rdmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003069}
3070
Paolo Bonzini63129752021-03-02 14:40:39 -05003071static int interrupt_window_interception(struct kvm_vcpu *vcpu)
Dor Laorc1150d82007-01-05 16:36:24 -08003072{
Paolo Bonzini63129752021-03-02 14:40:39 -05003073 kvm_make_request(KVM_REQ_EVENT, vcpu);
3074 svm_clear_vintr(to_svm(vcpu));
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003075
3076 /*
3077 * For AVIC, the only reason to end up here is ExtINTs.
3078 * In this case AVIC was temporarily disabled for
3079 * requesting the IRQ window and we have to re-enable it.
3080 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03003081 kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003082
Paolo Bonzini63129752021-03-02 14:40:39 -05003083 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003084 return 1;
3085}
3086
Paolo Bonzini63129752021-03-02 14:40:39 -05003087static int pause_interception(struct kvm_vcpu *vcpu)
Mark Langsdorf565d0992009-10-06 14:25:02 -05003088{
Tom Lendackyf1c63662020-12-14 10:29:50 -05003089 bool in_kernel;
3090
3091 /*
3092 * CPL is not made available for an SEV-ES guest, therefore
3093 * vcpu->arch.preempted_in_kernel can never be true. Just
3094 * set in_kernel to false as well.
3095 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003096 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003097
Wanpeng Li830f01b2020-07-31 11:12:21 +08003098 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04003099 grow_ple_window(vcpu);
3100
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003101 kvm_vcpu_on_spin(vcpu, in_kernel);
Sean Christophersonc8781fe2021-02-04 16:57:50 -08003102 return kvm_skip_emulated_instruction(vcpu);
Mark Langsdorf565d0992009-10-06 14:25:02 -05003103}
3104
Paolo Bonzini63129752021-03-02 14:40:39 -05003105static int invpcid_interception(struct kvm_vcpu *vcpu)
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003106{
Paolo Bonzini63129752021-03-02 14:40:39 -05003107 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger4407a792020-09-11 14:29:19 -05003108 unsigned long type;
3109 gva_t gva;
3110
3111 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3112 kvm_queue_exception(vcpu, UD_VECTOR);
3113 return 1;
3114 }
3115
3116 /*
3117 * For an INVPCID intercept:
3118 * EXITINFO1 provides the linear address of the memory operand.
3119 * EXITINFO2 provides the contents of the register operand.
3120 */
3121 type = svm->vmcb->control.exit_info_2;
3122 gva = svm->vmcb->control.exit_info_1;
3123
Babu Moger4407a792020-09-11 14:29:19 -05003124 return kvm_handle_invpcid(vcpu, type, gva);
3125}
3126
Paolo Bonzini63129752021-03-02 14:40:39 -05003127static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003128 [SVM_EXIT_READ_CR0] = cr_interception,
3129 [SVM_EXIT_READ_CR3] = cr_interception,
3130 [SVM_EXIT_READ_CR4] = cr_interception,
3131 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003132 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003133 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003134 [SVM_EXIT_WRITE_CR3] = cr_interception,
3135 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003136 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003137 [SVM_EXIT_READ_DR0] = dr_interception,
3138 [SVM_EXIT_READ_DR1] = dr_interception,
3139 [SVM_EXIT_READ_DR2] = dr_interception,
3140 [SVM_EXIT_READ_DR3] = dr_interception,
3141 [SVM_EXIT_READ_DR4] = dr_interception,
3142 [SVM_EXIT_READ_DR5] = dr_interception,
3143 [SVM_EXIT_READ_DR6] = dr_interception,
3144 [SVM_EXIT_READ_DR7] = dr_interception,
3145 [SVM_EXIT_WRITE_DR0] = dr_interception,
3146 [SVM_EXIT_WRITE_DR1] = dr_interception,
3147 [SVM_EXIT_WRITE_DR2] = dr_interception,
3148 [SVM_EXIT_WRITE_DR3] = dr_interception,
3149 [SVM_EXIT_WRITE_DR4] = dr_interception,
3150 [SVM_EXIT_WRITE_DR5] = dr_interception,
3151 [SVM_EXIT_WRITE_DR6] = dr_interception,
3152 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003153 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3154 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003155 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003156 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003157 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003158 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02003159 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003160 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003161 [SVM_EXIT_NMI] = nmi_interception,
Maxim Levitsky991afbb2021-07-07 15:50:58 +03003162 [SVM_EXIT_SMI] = smi_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003163 [SVM_EXIT_VINTR] = interrupt_window_interception,
Sean Christopherson32c23c72021-02-04 16:57:49 -08003164 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003165 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003166 [SVM_EXIT_IRET] = iret_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003167 [SVM_EXIT_INVD] = kvm_emulate_invd,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003168 [SVM_EXIT_PAUSE] = pause_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003169 [SVM_EXIT_HLT] = kvm_emulate_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003170 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003171 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003172 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 [SVM_EXIT_MSR] = msr_interception,
3174 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003175 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003176 [SVM_EXIT_VMRUN] = vmrun_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003177 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
Alexander Graf55426752008-11-25 20:17:06 +01003178 [SVM_EXIT_VMLOAD] = vmload_interception,
3179 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003180 [SVM_EXIT_STGI] = stgi_interception,
3181 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003182 [SVM_EXIT_SKINIT] = skinit_interception,
Sean Christopherson3b195ac2021-05-04 10:17:22 -07003183 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003184 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3185 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3186 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
Sean Christopherson92f98952021-02-04 16:57:46 -08003187 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003188 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
Tom Lendacky2985afb2020-12-10 11:09:55 -06003189 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
Tom Lendackyf27ad382020-12-10 11:09:56 -06003190 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
Tom Lendacky5b51cb12020-12-10 11:09:57 -06003191 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
Tom Lendackyd1949b92020-12-10 11:09:58 -06003192 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
Babu Moger4407a792020-09-11 14:29:19 -05003193 [SVM_EXIT_INVPCID] = invpcid_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003194 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06003195 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003196 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3197 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Tom Lendacky291bd202020-12-10 11:09:47 -06003198 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199};
3200
Joe Perchesae8cc052011-04-24 22:00:50 -07003201static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003202{
3203 struct vcpu_svm *svm = to_svm(vcpu);
3204 struct vmcb_control_area *control = &svm->vmcb->control;
3205 struct vmcb_save_area *save = &svm->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003206 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003207
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003208 if (!dump_invalid_vmcb) {
3209 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3210 return;
3211 }
3212
Jim Mattson18f63b12021-06-21 15:16:48 -07003213 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
3214 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003215 pr_err("VMCB Control Area:\n");
Babu Moger03bfeeb2020-09-11 14:28:05 -05003216 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3217 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
Babu Moger30abaa882020-09-11 14:28:12 -05003218 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3219 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
Babu Moger9780d512020-09-11 14:28:20 -05003220 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
Babu Mogerc62e2e92020-09-11 14:28:28 -05003221 pr_err("%-20s%08x %08x\n", "intercepts:",
3222 control->intercepts[INTERCEPT_WORD3],
3223 control->intercepts[INTERCEPT_WORD4]);
Joe Perchesae8cc052011-04-24 22:00:50 -07003224 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003225 pr_err("%-20s%d\n", "pause filter threshold:",
3226 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003227 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3228 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3229 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3230 pr_err("%-20s%d\n", "asid:", control->asid);
3231 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3232 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3233 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3234 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3235 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3236 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3237 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3238 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3239 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3240 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3241 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003242 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Tom Lendacky291bd202020-12-10 11:09:47 -06003243 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
Joe Perchesae8cc052011-04-24 22:00:50 -07003244 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3245 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003246 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003247 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003248 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3249 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3250 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Tom Lendacky376c6d22020-12-10 11:10:06 -06003251 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003252 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003253 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3254 "es:",
3255 save->es.selector, save->es.attrib,
3256 save->es.limit, save->es.base);
3257 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3258 "cs:",
3259 save->cs.selector, save->cs.attrib,
3260 save->cs.limit, save->cs.base);
3261 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3262 "ss:",
3263 save->ss.selector, save->ss.attrib,
3264 save->ss.limit, save->ss.base);
3265 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3266 "ds:",
3267 save->ds.selector, save->ds.attrib,
3268 save->ds.limit, save->ds.base);
3269 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3270 "fs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003271 save01->fs.selector, save01->fs.attrib,
3272 save01->fs.limit, save01->fs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003273 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3274 "gs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003275 save01->gs.selector, save01->gs.attrib,
3276 save01->gs.limit, save01->gs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003277 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3278 "gdtr:",
3279 save->gdtr.selector, save->gdtr.attrib,
3280 save->gdtr.limit, save->gdtr.base);
3281 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3282 "ldtr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003283 save01->ldtr.selector, save01->ldtr.attrib,
3284 save01->ldtr.limit, save01->ldtr.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003285 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3286 "idtr:",
3287 save->idtr.selector, save->idtr.attrib,
3288 save->idtr.limit, save->idtr.base);
3289 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3290 "tr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003291 save01->tr.selector, save01->tr.attrib,
3292 save01->tr.limit, save01->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003293 pr_err("cpl: %d efer: %016llx\n",
3294 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003295 pr_err("%-15s %016llx %-13s %016llx\n",
3296 "cr0:", save->cr0, "cr2:", save->cr2);
3297 pr_err("%-15s %016llx %-13s %016llx\n",
3298 "cr3:", save->cr3, "cr4:", save->cr4);
3299 pr_err("%-15s %016llx %-13s %016llx\n",
3300 "dr6:", save->dr6, "dr7:", save->dr7);
3301 pr_err("%-15s %016llx %-13s %016llx\n",
3302 "rip:", save->rip, "rflags:", save->rflags);
3303 pr_err("%-15s %016llx %-13s %016llx\n",
3304 "rsp:", save->rsp, "rax:", save->rax);
3305 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003306 "star:", save01->star, "lstar:", save01->lstar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003307 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003308 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
Joe Perchesae8cc052011-04-24 22:00:50 -07003309 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003310 "kernel_gs_base:", save01->kernel_gs_base,
3311 "sysenter_cs:", save01->sysenter_cs);
Joe Perchesae8cc052011-04-24 22:00:50 -07003312 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003313 "sysenter_esp:", save01->sysenter_esp,
3314 "sysenter_eip:", save01->sysenter_eip);
Joe Perchesae8cc052011-04-24 22:00:50 -07003315 pr_err("%-15s %016llx %-13s %016llx\n",
3316 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3317 pr_err("%-15s %016llx %-13s %016llx\n",
3318 "br_from:", save->br_from, "br_to:", save->br_to);
3319 pr_err("%-15s %016llx %-13s %016llx\n",
3320 "excp_from:", save->last_excp_from,
3321 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003322}
3323
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003324static bool svm_check_exit_valid(struct kvm_vcpu *vcpu, u64 exit_code)
3325{
3326 return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3327 svm_exit_handlers[exit_code]);
3328}
3329
Tom Lendackye9093fd42020-12-10 11:09:46 -06003330static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3331{
Tom Lendackye9093fd42020-12-10 11:09:46 -06003332 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3333 dump_vmcb(vcpu);
3334 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3335 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3336 vcpu->run->internal.ndata = 2;
3337 vcpu->run->internal.data[0] = exit_code;
3338 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003339 return 0;
Tom Lendackye9093fd42020-12-10 11:09:46 -06003340}
3341
Paolo Bonzini63129752021-03-02 14:40:39 -05003342int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
Tom Lendackye9093fd42020-12-10 11:09:46 -06003343{
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003344 if (!svm_check_exit_valid(vcpu, exit_code))
3345 return svm_handle_invalid_exit(vcpu, exit_code);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003346
3347#ifdef CONFIG_RETPOLINE
3348 if (exit_code == SVM_EXIT_MSR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003349 return msr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003350 else if (exit_code == SVM_EXIT_VINTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003351 return interrupt_window_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003352 else if (exit_code == SVM_EXIT_INTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003353 return intr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003354 else if (exit_code == SVM_EXIT_HLT)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003355 return kvm_emulate_halt(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003356 else if (exit_code == SVM_EXIT_NPF)
Paolo Bonzini63129752021-03-02 14:40:39 -05003357 return npf_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003358#endif
Paolo Bonzini63129752021-03-02 14:40:39 -05003359 return svm_exit_handlers[exit_code](vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003360}
3361
David Edmondson0a62a032021-09-20 11:37:35 +01003362static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
3363 u64 *info1, u64 *info2,
Sean Christopherson235ba742020-09-23 13:13:46 -07003364 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02003365{
3366 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3367
David Edmondson0a62a032021-09-20 11:37:35 +01003368 *reason = control->exit_code;
Avi Kivity586f9602010-11-18 13:09:54 +02003369 *info1 = control->exit_info_1;
3370 *info2 = control->exit_info_2;
Sean Christopherson235ba742020-09-23 13:13:46 -07003371 *intr_info = control->exit_int_info;
3372 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3373 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3374 *error_code = control->exit_int_info_err;
3375 else
3376 *error_code = 0;
Avi Kivity586f9602010-11-18 13:09:54 +02003377}
3378
Wanpeng Li404d5d72020-04-28 14:23:25 +08003379static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003380{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003381 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003382 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003383 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384
David Edmondson0a62a032021-09-20 11:37:35 +01003385 trace_kvm_exit(vcpu, KVM_ISA_SVM);
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003386
Tom Lendackyf1c63662020-12-14 10:29:50 -05003387 /* SEV-ES guests must use the CR write traps to track CR registers. */
3388 if (!sev_es_guest(vcpu->kvm)) {
3389 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3390 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3391 if (npt_enabled)
3392 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3393 }
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003394
Joerg Roedel20307532010-11-29 17:51:48 +01003395 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003396 int vmexit;
3397
David Edmondson0a62a032021-09-20 11:37:35 +01003398 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003399
Joerg Roedel410e4d52009-08-07 11:49:44 +02003400 vmexit = nested_svm_exit_special(svm);
3401
3402 if (vmexit == NESTED_EXIT_CONTINUE)
3403 vmexit = nested_svm_exit_handled(svm);
3404
3405 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003406 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003407 }
3408
Avi Kivity04d2cc72007-09-10 18:10:54 +03003409 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3410 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3411 kvm_run->fail_entry.hardware_entry_failure_reason
3412 = svm->vmcb->control.exit_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07003413 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003414 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003415 return 0;
3416 }
3417
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003418 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003419 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003420 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3421 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003422 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003423 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003424 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003425 exit_code);
3426
Wanpeng Li404d5d72020-04-28 14:23:25 +08003427 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003428 return 1;
Wanpeng Li404d5d72020-04-28 14:23:25 +08003429
Paolo Bonzini63129752021-03-02 14:40:39 -05003430 return svm_invoke_exit_handler(vcpu, exit_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003431}
3432
3433static void reload_tss(struct kvm_vcpu *vcpu)
3434{
Jim Mattson73cd6e52020-06-03 16:56:18 -07003435 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003436
Tejun Heo0fe1e002009-10-29 22:34:14 +09003437 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003438 load_TR_desc();
3439}
3440
Paolo Bonzini63129752021-03-02 14:40:39 -05003441static void pre_svm_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003442{
Paolo Bonzini63129752021-03-02 14:40:39 -05003443 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3444 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003445
Cathy Averyaf18fa72021-01-12 11:43:12 -05003446 /*
Sean Christopherson44f1b552021-04-06 10:18:11 -07003447 * If the previous vmrun of the vmcb occurred on a different physical
3448 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3449 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3450 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003451 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
Cathy Avery193015a2021-01-12 11:43:13 -05003452 svm->current_vmcb->asid_generation = 0;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003453 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05003454 svm->current_vmcb->cpu = vcpu->cpu;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003455 }
3456
Paolo Bonzini63129752021-03-02 14:40:39 -05003457 if (sev_guest(vcpu->kvm))
3458 return pre_sev_run(svm, vcpu->cpu);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003459
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003460 /* FIXME: handle wraparound of asid_generation */
Cathy Avery193015a2021-01-12 11:43:13 -05003461 if (svm->current_vmcb->asid_generation != sd->asid_generation)
Tejun Heo0fe1e002009-10-29 22:34:14 +09003462 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003463}
3464
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003465static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3466{
3467 struct vcpu_svm *svm = to_svm(vcpu);
3468
3469 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3470 vcpu->arch.hflags |= HF_NMI_MASK;
Paolo Bonzini63129752021-03-02 14:40:39 -05003471 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003472 svm_set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003473 ++vcpu->stat.nmi_injections;
3474}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003476static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003477{
3478 struct vcpu_svm *svm = to_svm(vcpu);
3479
Joerg Roedel2af91942009-08-07 11:49:28 +02003480 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003481
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003482 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3483 ++vcpu->stat.irq_injections;
3484
Alexander Graf219b65d2009-06-15 15:21:25 +02003485 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3486 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003487}
3488
Jason Baronb6a7cc32021-01-14 22:27:54 -05003489static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003490{
3491 struct vcpu_svm *svm = to_svm(vcpu);
3492
Tom Lendackyf1c63662020-12-14 10:29:50 -05003493 /*
3494 * SEV-ES guests must always keep the CR intercepts cleared. CR
3495 * tracking is done using the CR write traps.
3496 */
3497 if (sev_es_guest(vcpu->kvm))
3498 return;
3499
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003500 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003501 return;
3502
Babu Moger830bd712020-09-11 14:28:50 -05003503 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
Radim Krčmář596f3142014-03-11 19:11:18 +01003504
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003505 if (irr == -1)
3506 return;
3507
3508 if (tpr >= irr)
Babu Moger830bd712020-09-11 14:28:50 -05003509 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003510}
3511
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003512bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003513{
3514 struct vcpu_svm *svm = to_svm(vcpu);
3515 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson88c604b2020-04-22 19:25:41 -07003516 bool ret;
Cathy Avery9c3d3702020-04-14 16:11:06 -04003517
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003518 if (!gif_set(svm))
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -04003519 return true;
3520
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003521 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3522 return false;
3523
3524 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05003525 (vcpu->arch.hflags & HF_NMI_MASK);
Joerg Roedel924584c2010-04-22 12:33:07 +02003526
3527 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003528}
3529
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003530static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003531{
3532 struct vcpu_svm *svm = to_svm(vcpu);
3533 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003534 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003535
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003536 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3537 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003538 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003539
3540 return !svm_nmi_blocked(vcpu);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003541}
3542
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003543static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3544{
Paolo Bonzini63129752021-03-02 14:40:39 -05003545 return !!(vcpu->arch.hflags & HF_NMI_MASK);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003546}
3547
3548static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3549{
3550 struct vcpu_svm *svm = to_svm(vcpu);
3551
3552 if (masked) {
Paolo Bonzini63129752021-03-02 14:40:39 -05003553 vcpu->arch.hflags |= HF_NMI_MASK;
3554 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003555 svm_set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003556 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05003557 vcpu->arch.hflags &= ~HF_NMI_MASK;
3558 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003559 svm_clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003560 }
3561}
3562
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003563bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
Gleb Natapov78646122009-03-23 12:12:11 +02003564{
3565 struct vcpu_svm *svm = to_svm(vcpu);
3566 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003567
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003568 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003569 return true;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003570
Paolo Bonzini63129752021-03-02 14:40:39 -05003571 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyf1c63662020-12-14 10:29:50 -05003572 /*
3573 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3574 * bit to determine the state of the IF flag.
3575 */
3576 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3577 return true;
3578 } else if (is_guest_mode(vcpu)) {
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003579 /* As long as interrupts are being delivered... */
Paolo Bonzinie9fd7612020-05-13 13:28:23 -04003580 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
Cathy Avery4995a362021-01-13 07:07:52 -05003581 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003582 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3583 return true;
3584
3585 /* ... vmexits aren't blocked by the interrupt shadow */
3586 if (nested_exit_on_intr(svm))
3587 return false;
3588 } else {
3589 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3590 return true;
3591 }
3592
3593 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003594}
3595
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003596static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003597{
3598 struct vcpu_svm *svm = to_svm(vcpu);
3599 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003600 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003601
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003602 /*
3603 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3604 * e.g. if the IRQ arrived asynchronously after checking nested events.
3605 */
3606 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003607 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003608
3609 return !svm_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02003610}
3611
Jason Baronb6a7cc32021-01-14 22:27:54 -05003612static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003613{
Alexander Graf219b65d2009-06-15 15:21:25 +02003614 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003615
Joerg Roedele0231712010-02-24 18:59:10 +01003616 /*
3617 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3618 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3619 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003620 * we'll get the vintr intercept. However, if the vGIF feature is
3621 * enabled, the STGI interception will not occur. Enable the irq
3622 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003623 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003624 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003625 /*
3626 * IRQ window is not needed when AVIC is enabled,
3627 * unless we have pending ExtINT since it cannot be injected
3628 * via AVIC. In such case, we need to temporarily disable AVIC,
3629 * and fallback to injecting IRQ via V_IRQ.
3630 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03003631 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN);
Alexander Graf219b65d2009-06-15 15:21:25 +02003632 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003633 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003634}
3635
Jason Baronb6a7cc32021-01-14 22:27:54 -05003636static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003637{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003638 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003639
Paolo Bonzini63129752021-03-02 14:40:39 -05003640 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003641 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003642
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003643 if (!gif_set(svm)) {
3644 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02003645 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003646 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003647 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003648
Joerg Roedele0231712010-02-24 18:59:10 +01003649 /*
3650 * Something prevents NMI from been injected. Single step over possible
3651 * problem (IRET or exception injection or interrupt shadow)
3652 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003653 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003654 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003655 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003656}
3657
Izik Eiduscbc94022007-10-25 00:29:55 +02003658static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3659{
3660 return 0;
3661}
3662
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003663static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3664{
3665 return 0;
3666}
3667
Sean Christophersonf55ac302020-03-20 14:28:12 -07003668void svm_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003669{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003670 struct vcpu_svm *svm = to_svm(vcpu);
3671
Sean Christopherson4a41e432020-03-20 14:28:17 -07003672 /*
3673 * Flush only the current ASID even if the TLB flush was invoked via
3674 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3675 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3676 * unconditionally does a TLB flush on both nested VM-Enter and nested
3677 * VM-Exit (via kvm_mmu_reset_context()).
3678 */
Joerg Roedel38e5e922010-12-03 15:25:16 +01003679 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3680 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3681 else
Cathy Avery193015a2021-01-12 11:43:13 -05003682 svm->current_vmcb->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003683}
3684
Junaid Shahidfaff8752018-06-29 13:10:05 -07003685static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3686{
3687 struct vcpu_svm *svm = to_svm(vcpu);
3688
3689 invlpga(gva, svm->vmcb->control.asid);
3690}
3691
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003692static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3693{
3694 struct vcpu_svm *svm = to_svm(vcpu);
3695
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003696 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003697 return;
3698
Babu Moger830bd712020-09-11 14:28:50 -05003699 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003700 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003701 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003702 }
3703}
3704
Joerg Roedel649d6862008-04-16 16:51:15 +02003705static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3706{
3707 struct vcpu_svm *svm = to_svm(vcpu);
3708 u64 cr8;
3709
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003710 if (nested_svm_virtualize_tpr(vcpu) ||
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003711 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003712 return;
3713
Joerg Roedel649d6862008-04-16 16:51:15 +02003714 cr8 = kvm_get_cr8(vcpu);
3715 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3716 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3717}
3718
Paolo Bonzini63129752021-03-02 14:40:39 -05003719static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003720{
Paolo Bonzini63129752021-03-02 14:40:39 -05003721 struct vcpu_svm *svm = to_svm(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003722 u8 vector;
3723 int type;
3724 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003725 unsigned int3_injected = svm->int3_injected;
3726
3727 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003728
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003729 /*
3730 * If we've made progress since setting HF_IRET_MASK, we've
3731 * executed an IRET and can allow NMI injection.
3732 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003733 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3734 (sev_es_guest(vcpu->kvm) ||
3735 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3736 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3737 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003738 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003739
Paolo Bonzini63129752021-03-02 14:40:39 -05003740 vcpu->arch.nmi_injected = false;
3741 kvm_clear_exception_queue(vcpu);
3742 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003743
3744 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3745 return;
3746
Paolo Bonzini63129752021-03-02 14:40:39 -05003747 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003748
Gleb Natapov9222be12009-04-23 17:14:37 +03003749 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3750 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3751
3752 switch (type) {
3753 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05003754 vcpu->arch.nmi_injected = true;
Gleb Natapov9222be12009-04-23 17:14:37 +03003755 break;
3756 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003757 /*
Tom Lendackyf1c63662020-12-14 10:29:50 -05003758 * Never re-inject a #VC exception.
3759 */
3760 if (vector == X86_TRAP_VC)
3761 break;
3762
3763 /*
Jan Kiszka66b71382010-02-23 17:47:56 +01003764 * In case of software exceptions, do not reinject the vector,
3765 * but re-execute the instruction instead. Rewind RIP first
3766 * if we emulated INT3 before.
3767 */
3768 if (kvm_exception_is_soft(vector)) {
3769 if (vector == BP_VECTOR && int3_injected &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003770 kvm_is_linear_rip(vcpu, svm->int3_rip))
3771 kvm_rip_write(vcpu,
3772 kvm_rip_read(vcpu) - int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003773 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003774 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003775 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3776 u32 err = svm->vmcb->control.exit_int_info_err;
Paolo Bonzini63129752021-03-02 14:40:39 -05003777 kvm_requeue_exception_e(vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003778
3779 } else
Paolo Bonzini63129752021-03-02 14:40:39 -05003780 kvm_requeue_exception(vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003781 break;
3782 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05003783 kvm_queue_interrupt(vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003784 break;
3785 default:
3786 break;
3787 }
3788}
3789
Avi Kivityb463a6f2010-07-20 15:06:17 +03003790static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3791{
3792 struct vcpu_svm *svm = to_svm(vcpu);
3793 struct vmcb_control_area *control = &svm->vmcb->control;
3794
3795 control->exit_int_info = control->event_inj;
3796 control->exit_int_info_err = control->event_inj_err;
3797 control->event_inj = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003798 svm_complete_interrupts(vcpu);
Avi Kivityb463a6f2010-07-20 15:06:17 +03003799}
3800
Wanpeng Li404d5d72020-04-28 14:23:25 +08003801static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003802{
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003803 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003804 to_svm(vcpu)->vmcb->control.exit_info_1)
3805 return handle_fastpath_set_msr_irqoff(vcpu);
3806
3807 return EXIT_FASTPATH_NONE;
3808}
3809
Paolo Bonzini63129752021-03-02 14:40:39 -05003810static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
Thomas Gleixner135961e2020-07-08 21:51:58 +02003811{
Paolo Bonzini63129752021-03-02 14:40:39 -05003812 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christophersond1788192021-04-06 10:18:09 -07003813 unsigned long vmcb_pa = svm->current_vmcb->pa;
Paolo Bonzini63129752021-03-02 14:40:39 -05003814
Sean Christophersonbc908e02021-05-04 17:27:35 -07003815 kvm_guest_enter_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003816
Paolo Bonzini63129752021-03-02 14:40:39 -05003817 if (sev_es_guest(vcpu->kvm)) {
Sean Christophersond1788192021-04-06 10:18:09 -07003818 __svm_sev_es_vcpu_run(vmcb_pa);
Tom Lendacky16809ec2020-12-10 11:10:08 -06003819 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06003820 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3821
Sean Christophersond1788192021-04-06 10:18:09 -07003822 /*
3823 * Use a single vmcb (vmcb01 because it's always valid) for
3824 * context switching guest state via VMLOAD/VMSAVE, that way
3825 * the state doesn't need to be copied between vmcb01 and
3826 * vmcb02 when switching vmcbs for nested virtualization.
3827 */
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003828 vmload(svm->vmcb01.pa);
Sean Christophersond1788192021-04-06 10:18:09 -07003829 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003830 vmsave(svm->vmcb01.pa);
Thomas Gleixner135961e2020-07-08 21:51:58 +02003831
Michael Rothe79b91b2021-02-02 13:01:24 -06003832 vmload(__sme_page_pa(sd->save_area));
Tom Lendacky16809ec2020-12-10 11:10:08 -06003833 }
Thomas Gleixner135961e2020-07-08 21:51:58 +02003834
Sean Christophersonbc908e02021-05-04 17:27:35 -07003835 kvm_guest_exit_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003836}
3837
Qian Caib95273f2020-04-15 11:37:09 -04003838static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003839{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003840 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003841
Lorenzo Bresciad95df952020-12-23 14:45:07 +00003842 trace_kvm_entry(vcpu);
3843
Joerg Roedel2041a062010-04-22 12:33:08 +02003844 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3845 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3846 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3847
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003848 /*
Ladi Proseka12713c2017-06-21 09:07:00 +02003849 * Disable singlestep if we're injecting an interrupt/exception.
3850 * We don't want our modified rflags to be pushed on the stack where
3851 * we might not be able to easily reset them if we disabled NMI
3852 * singlestep later.
3853 */
3854 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3855 /*
3856 * Event injection happens before external interrupts cause a
3857 * vmexit and interrupts are disabled here, so smp_send_reschedule
3858 * is enough to force an immediate vmexit.
3859 */
3860 disable_nmi_singlestep(svm);
3861 smp_send_reschedule(vcpu->cpu);
3862 }
3863
Paolo Bonzini63129752021-03-02 14:40:39 -05003864 pre_svm_run(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003865
Joerg Roedel649d6862008-04-16 16:51:15 +02003866 sync_lapic_to_cr8(vcpu);
3867
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04003868 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3869 svm->vmcb->control.asid = svm->asid;
3870 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3871 }
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003872 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003873
Vineeth Pillai11836462021-06-03 15:14:40 +00003874 svm_hv_update_vp_id(svm->vmcb, vcpu);
3875
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003876 /*
3877 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3878 * of a #DB.
3879 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003880 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003881 svm_set_dr6(svm, vcpu->arch.dr6);
3882 else
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08003883 svm_set_dr6(svm, DR6_ACTIVE_LOW);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003884
Avi Kivity04d2cc72007-09-10 18:10:54 +03003885 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003886 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003887
Wanpeng Li010fd372020-09-10 17:50:41 +08003888 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003889
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003890 /*
3891 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3892 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3893 * is no need to worry about the conditional branch over the wrmsr
3894 * being speculatively taken.
3895 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003896 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3897 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003898
Paolo Bonzini63129752021-03-02 14:40:39 -05003899 svm_vcpu_enter_exit(vcpu);
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003900
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003901 /*
3902 * We do not use IBRS in the kernel. If this vCPU has used the
3903 * SPEC_CTRL MSR it may have left it on; save the value and
3904 * turn it off. This is much more efficient than blindly adding
3905 * it to the atomic save/restore list. Especially as the former
3906 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3907 *
3908 * For non-nested case:
3909 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3910 * save it.
3911 *
3912 * For nested case:
3913 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3914 * save it.
3915 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003916 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3917 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003918 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003919
Paolo Bonzini63129752021-03-02 14:40:39 -05003920 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky16809ec2020-12-10 11:10:08 -06003921 reload_tss(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003922
Babu Mogerd00b99c2021-02-17 10:56:04 -05003923 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3924 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003925
Paolo Bonzini63129752021-03-02 14:40:39 -05003926 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003927 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3928 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3929 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3930 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3931 }
Avi Kivity13c34e02010-10-21 12:20:31 +02003932
Joerg Roedel3781c012011-01-14 16:45:02 +01003933 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003934 kvm_before_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003935
Aaron Lewis139a12c2019-10-21 16:30:25 -07003936 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003937 stgi();
3938
3939 /* Any pending NMI will happen here */
3940
3941 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003942 kvm_after_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003943
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003944 sync_cr8_to_lapic(vcpu);
3945
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003946 svm->next_rip = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003947 if (is_guest_mode(vcpu)) {
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05003948 nested_sync_control_from_vmcb02(svm);
Krish Sadhukhanb93af022021-06-09 14:03:38 -04003949
3950 /* Track VMRUNs that have made past consistency checking */
3951 if (svm->nested.nested_run_pending &&
3952 svm->vmcb->control.exit_code != SVM_EXIT_ERR)
3953 ++vcpu->stat.nested_run;
3954
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -04003955 svm->nested.nested_run_pending = 0;
3956 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003957
Joerg Roedel38e5e922010-12-03 15:25:16 +01003958 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Wanpeng Lie42c6822020-09-12 02:16:39 -04003959 vmcb_mark_all_clean(svm->vmcb);
Joerg Roedel38e5e922010-12-03 15:25:16 +01003960
Gleb Natapov631bc482010-10-14 11:22:52 +02003961 /* if exit due to PF check for async PF */
3962 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003963 vcpu->arch.apf.host_apf_flags =
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003964 kvm_read_and_reset_apf_flags();
Gleb Natapov631bc482010-10-14 11:22:52 +02003965
Maxim Levitsky329675d2021-06-07 12:02:01 +03003966 if (npt_enabled)
3967 kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR);
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003968
3969 /*
3970 * We need to handle MC intercepts here before the vcpu has a chance to
3971 * change the physical cpu
3972 */
3973 if (unlikely(svm->vmcb->control.exit_code ==
3974 SVM_EXIT_EXCP_BASE + MC_VECTOR))
Paolo Bonzini63129752021-03-02 14:40:39 -05003975 svm_handle_mce(vcpu);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003976
Paolo Bonzini63129752021-03-02 14:40:39 -05003977 svm_complete_interrupts(vcpu);
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003978
3979 if (is_guest_mode(vcpu))
3980 return EXIT_FASTPATH_NONE;
3981
3982 return svm_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003983}
3984
Sean Christophersone83bc092021-03-05 10:31:13 -08003985static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
Sean Christopherson2a40b902020-07-15 20:41:18 -07003986 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003987{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003988 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003989 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003990
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003991 if (npt_enabled) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003992 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
Joerg Roedel06e78522020-06-25 10:03:23 +02003993 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003994
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00003995 hv_track_root_tdp(vcpu, root_hpa);
3996
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003997 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
Paolo Bonzini978ce582020-05-20 08:37:37 -04003998 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3999 return;
4000 cr3 = vcpu->arch.cr3;
Sean Christophersone83bc092021-03-05 10:31:13 -08004001 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson4a986232021-03-09 14:42:07 -08004002 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
Sean Christophersone83bc092021-03-05 10:31:13 -08004003 } else {
4004 /* PCID in the guest should be impossible with a 32-bit MMU. */
4005 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
4006 cr3 = root_hpa;
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01004007 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004008
Paolo Bonzini978ce582020-05-20 08:37:37 -04004009 svm->vmcb->save.cr3 = cr3;
Joerg Roedel06e78522020-06-25 10:03:23 +02004010 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004011}
4012
Avi Kivity6aa8b732006-12-10 02:21:36 -08004013static int is_disabled(void)
4014{
Joerg Roedel6031a612007-06-22 12:29:50 +03004015 u64 vm_cr;
4016
4017 rdmsrl(MSR_VM_CR, vm_cr);
4018 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4019 return 1;
4020
Avi Kivity6aa8b732006-12-10 02:21:36 -08004021 return 0;
4022}
4023
Ingo Molnar102d8322007-02-19 14:37:47 +02004024static void
4025svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4026{
4027 /*
4028 * Patch in the VMMCALL instruction:
4029 */
4030 hypercall[0] = 0x0f;
4031 hypercall[1] = 0x01;
4032 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004033}
4034
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004035static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004036{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004037 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004038}
4039
Avi Kivity774ead32007-12-26 13:57:04 +02004040static bool svm_cpu_has_accelerated_tpr(void)
4041{
4042 return false;
4043}
4044
Tom Lendacky57194552020-12-10 11:10:00 -06004045/*
4046 * The kvm parameter can be NULL (module initialization, or invocation before
4047 * VM creation). Be sure to check the kvm parameter before using it.
4048 */
4049static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004050{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004051 switch (index) {
4052 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02004053 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004054 return false;
Tom Lendacky57194552020-12-10 11:10:00 -06004055 case MSR_IA32_SMBASE:
4056 /* SEV-ES guests do not support SMM, so report false */
4057 if (kvm && sev_es_guest(kvm))
4058 return false;
4059 break;
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004060 default:
4061 break;
4062 }
4063
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004064 return true;
4065}
4066
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004067static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4068{
4069 return 0;
4070}
4071
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004072static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08004073{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004074 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger96308b02020-11-12 16:18:03 -06004075 struct kvm_cpuid_entry2 *best;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004076
Aaron Lewis72041602019-10-21 16:30:20 -07004077 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08004078 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07004079 boot_cpu_has(X86_FEATURE_XSAVES);
4080
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004081 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08004082 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
Paolo Bonzini63129752021-03-02 14:40:39 -05004083 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004084
Maxim Levitsky5228eb92021-09-14 18:48:24 +03004085 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
4086
Sean Christopherson3b195ac2021-05-04 10:17:22 -07004087 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05004088
Babu Moger96308b02020-11-12 16:18:03 -06004089 /* For sev guests, the memory encryption bit is not reserved in CR3. */
4090 if (sev_guest(vcpu->kvm)) {
4091 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
4092 if (best)
Sean Christophersonca29e142021-02-03 16:01:12 -08004093 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
Babu Moger96308b02020-11-12 16:18:03 -06004094 }
4095
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004096 if (kvm_vcpu_apicv_active(vcpu)) {
4097 /*
4098 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
4099 * is exposed to the guest, disable AVIC.
4100 */
4101 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
4102 kvm_request_apicv_update(vcpu->kvm, false,
4103 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004104
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004105 /*
4106 * Currently, AVIC does not work with nested virtualization.
4107 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
4108 */
4109 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4110 kvm_request_apicv_update(vcpu->kvm, false,
4111 APICV_INHIBIT_REASON_NESTED);
4112 }
Paolo Bonzini36e81942021-09-23 12:46:07 -04004113 init_vmcb_after_set_cpuid(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004114}
4115
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004116static bool svm_has_wbinvd_exit(void)
4117{
4118 return true;
4119}
4120
Joerg Roedel80612522011-04-04 12:39:33 +02004121#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004122 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004123#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004124 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004125#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004126 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004127
Mathias Krause09941fb2012-08-30 01:30:20 +02004128static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004129 u32 exit_code;
4130 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004131} x86_intercept_map[] = {
4132 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4133 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4134 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4135 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4136 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004137 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4138 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004139 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4140 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4141 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4142 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4143 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4144 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4145 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4146 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004147 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4148 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4149 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4150 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4151 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4152 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4153 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4154 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004155 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4156 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4157 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004158 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4159 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4160 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4161 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4162 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4163 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4164 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4165 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4166 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004167 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4168 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4169 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4170 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4171 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4172 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4173 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004174 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4175 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4176 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4177 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004178 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004179};
4180
Joerg Roedel80612522011-04-04 12:39:33 +02004181#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004182#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004183#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004184
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004185static int svm_check_intercept(struct kvm_vcpu *vcpu,
4186 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08004187 enum x86_intercept_stage stage,
4188 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004189{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004190 struct vcpu_svm *svm = to_svm(vcpu);
4191 int vmexit, ret = X86EMUL_CONTINUE;
4192 struct __x86_intercept icpt_info;
4193 struct vmcb *vmcb = svm->vmcb;
4194
4195 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4196 goto out;
4197
4198 icpt_info = x86_intercept_map[info->intercept];
4199
Avi Kivity40e19b52011-04-21 12:35:41 +03004200 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004201 goto out;
4202
4203 switch (icpt_info.exit_code) {
4204 case SVM_EXIT_READ_CR0:
4205 if (info->intercept == x86_intercept_cr_read)
4206 icpt_info.exit_code += info->modrm_reg;
4207 break;
4208 case SVM_EXIT_WRITE_CR0: {
4209 unsigned long cr0, val;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004210
4211 if (info->intercept == x86_intercept_cr_write)
4212 icpt_info.exit_code += info->modrm_reg;
4213
Jan Kiszka62baf442014-06-29 21:55:53 +02004214 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4215 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004216 break;
4217
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04004218 if (!(vmcb12_is_intercept(&svm->nested.ctl,
Babu Mogerc62e2e92020-09-11 14:28:28 -05004219 INTERCEPT_SELECTIVE_CR0)))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004220 break;
4221
4222 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4223 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4224
4225 if (info->intercept == x86_intercept_lmsw) {
4226 cr0 &= 0xfUL;
4227 val &= 0xfUL;
4228 /* lmsw can't clear PE - catch this here */
4229 if (cr0 & X86_CR0_PE)
4230 val |= X86_CR0_PE;
4231 }
4232
4233 if (cr0 ^ val)
4234 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4235
4236 break;
4237 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004238 case SVM_EXIT_READ_DR0:
4239 case SVM_EXIT_WRITE_DR0:
4240 icpt_info.exit_code += info->modrm_reg;
4241 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004242 case SVM_EXIT_MSR:
4243 if (info->intercept == x86_intercept_wrmsr)
4244 vmcb->control.exit_info_1 = 1;
4245 else
4246 vmcb->control.exit_info_1 = 0;
4247 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004248 case SVM_EXIT_PAUSE:
4249 /*
4250 * We get this for NOP only, but pause
4251 * is rep not, check this here
4252 */
4253 if (info->rep_prefix != REPE_PREFIX)
4254 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004255 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004256 case SVM_EXIT_IOIO: {
4257 u64 exit_info;
4258 u32 bytes;
4259
Joerg Roedelf6511932011-04-04 12:39:35 +02004260 if (info->intercept == x86_intercept_in ||
4261 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004262 exit_info = ((info->src_val & 0xffff) << 16) |
4263 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004264 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004265 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004266 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004267 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004268 }
4269
4270 if (info->intercept == x86_intercept_outs ||
4271 info->intercept == x86_intercept_ins)
4272 exit_info |= SVM_IOIO_STR_MASK;
4273
4274 if (info->rep_prefix)
4275 exit_info |= SVM_IOIO_REP_MASK;
4276
4277 bytes = min(bytes, 4u);
4278
4279 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4280
4281 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4282
4283 vmcb->control.exit_info_1 = exit_info;
4284 vmcb->control.exit_info_2 = info->next_rip;
4285
4286 break;
4287 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004288 default:
4289 break;
4290 }
4291
Bandan Dasf1047652015-06-11 02:05:33 -04004292 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4293 if (static_cpu_has(X86_FEATURE_NRIPS))
4294 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004295 vmcb->control.exit_code = icpt_info.exit_code;
4296 vmexit = nested_svm_exit_handled(svm);
4297
4298 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4299 : X86EMUL_CONTINUE;
4300
4301out:
4302 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004303}
4304
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07004305static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004306{
Yang Zhanga547c6d2013-04-11 19:25:10 +08004307}
4308
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004309static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4310{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004311 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04004312 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004313}
4314
Borislav Petkov74f16902017-03-26 23:51:24 +02004315static void svm_setup_mce(struct kvm_vcpu *vcpu)
4316{
4317 /* [63:9] are reserved. */
4318 vcpu->arch.mcg_cap &= 0x1ff;
4319}
4320
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004321bool svm_smi_blocked(struct kvm_vcpu *vcpu)
Ladi Prosek72d7b372017-10-11 16:54:41 +02004322{
Ladi Prosek05cade72017-10-11 16:54:45 +02004323 struct vcpu_svm *svm = to_svm(vcpu);
4324
4325 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4326 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004327 return true;
4328
4329 return is_smm(vcpu);
4330}
4331
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004332static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004333{
4334 struct vcpu_svm *svm = to_svm(vcpu);
4335 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004336 return -EBUSY;
Ladi Prosek05cade72017-10-11 16:54:45 +02004337
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004338 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4339 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004340 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004341
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004342 return !svm_smi_blocked(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02004343}
4344
Sean Christophersonecc513e2021-06-09 11:56:19 -07004345static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004346{
Ladi Prosek05cade72017-10-11 16:54:45 +02004347 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004348 struct kvm_host_map map_save;
Ladi Prosek05cade72017-10-11 16:54:45 +02004349 int ret;
4350
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004351 if (!is_guest_mode(vcpu))
4352 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004353
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004354 /* FED8h - SVM Guest */
4355 put_smstate(u64, smstate, 0x7ed8, 1);
4356 /* FEE0h - SVM Guest VMCB Physical Address */
4357 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
Ladi Prosek05cade72017-10-11 16:54:45 +02004358
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004359 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4360 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4361 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004362
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004363 ret = nested_svm_vmexit(svm);
4364 if (ret)
4365 return ret;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004366
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004367 /*
4368 * KVM uses VMCB01 to store L1 host state while L2 runs but
4369 * VMCB01 is going to be used during SMM and thus the state will
4370 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
4371 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
4372 * format of the area is identical to guest save area offsetted
4373 * by 0x400 (matches the offset of 'struct vmcb_save_area'
4374 * within 'struct vmcb'). Note: HSAVE area may also be used by
4375 * L1 hypervisor to save additional host context (e.g. KVM does
4376 * that, see svm_prepare_guest_switch()) which must be
4377 * preserved.
4378 */
4379 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
4380 &map_save) == -EINVAL)
4381 return 1;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004382
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004383 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004384
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004385 svm_copy_vmrun_state(map_save.hva + 0x400,
4386 &svm->vmcb01.ptr->save);
4387
4388 kvm_vcpu_unmap(vcpu, &map_save, true);
Ladi Prosek0234bf82017-10-11 16:54:40 +02004389 return 0;
4390}
4391
Sean Christophersonecc513e2021-06-09 11:56:19 -07004392static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004393{
Ladi Prosek05cade72017-10-11 16:54:45 +02004394 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004395 struct kvm_host_map map, map_save;
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004396 u64 saved_efer, vmcb12_gpa;
4397 struct vmcb *vmcb12;
4398 int ret;
Ladi Prosek05cade72017-10-11 16:54:45 +02004399
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004400 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
4401 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004402
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004403 /* Non-zero if SMI arrived while vCPU was in guest mode. */
4404 if (!GET_SMSTATE(u64, smstate, 0x7ed8))
4405 return 0;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004406
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004407 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4408 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004409
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004410 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4411 if (!(saved_efer & EFER_SVME))
4412 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004413
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004414 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4415 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4416 return 1;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03004417
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004418 ret = 1;
4419 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
4420 goto unmap_map;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004421
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004422 if (svm_allocate_nested(svm))
4423 goto unmap_save;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004424
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004425 /*
4426 * Restore L1 host state from L1 HSAVE area as VMCB01 was
4427 * used during SMM (see svm_enter_smm())
4428 */
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004429
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004430 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
Maxim Levitskye2e6e442021-09-13 17:09:49 +03004431
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004432 /*
4433 * Enter the nested guest now
4434 */
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004435
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004436 vmcb12 = map.hva;
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -04004437 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
Emanuele Giuseppe Espositof2740a82021-11-03 10:05:22 -04004438 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004439 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
4440
4441unmap_save:
4442 kvm_vcpu_unmap(vcpu, &map_save, true);
4443unmap_map:
4444 kvm_vcpu_unmap(vcpu, &map, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004445 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004446}
4447
Jason Baronb6a7cc32021-01-14 22:27:54 -05004448static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004449{
4450 struct vcpu_svm *svm = to_svm(vcpu);
4451
4452 if (!gif_set(svm)) {
4453 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02004454 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004455 /* STGI will cause a vm exit */
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004456 } else {
4457 /* We must be in SMM; RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004458 }
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004459}
4460
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004461static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004462{
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004463 bool smep, smap, is_user;
4464 unsigned long cr4;
Paolo Bonzinie72436b2020-04-17 12:21:06 -04004465
4466 /*
Tom Lendackybc624d92020-12-10 11:09:44 -06004467 * When the guest is an SEV-ES guest, emulation is not possible.
4468 */
4469 if (sev_es_guest(vcpu->kvm))
4470 return false;
4471
4472 /*
Liran Alon118154b2019-07-17 02:56:58 +03004473 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4474 *
4475 * Errata:
4476 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4477 * possible that CPU microcode implementing DecodeAssist will fail
4478 * to read bytes of instruction which caused #NPF. In this case,
4479 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4480 * return 0 instead of the correct guest instruction bytes.
4481 *
4482 * This happens because CPU microcode reading instruction bytes
4483 * uses a special opcode which attempts to read data using CPL=0
Ingo Molnard9f6e122021-03-18 15:28:01 +01004484 * privileges. The microcode reads CS:RIP and if it hits a SMAP
Liran Alon118154b2019-07-17 02:56:58 +03004485 * fault, it gives up and returns no instruction bytes.
4486 *
4487 * Detection:
4488 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4489 * returned 0 in GuestIntrBytes field of the VMCB.
4490 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4491 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4492 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4493 * a SMEP fault instead of #NPF).
4494 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4495 * As most guests enable SMAP if they have also enabled SMEP, use above
4496 * logic in order to attempt minimize false-positive of detecting errata
4497 * while still preserving all cases semantic correctness.
4498 *
4499 * Workaround:
4500 * To determine what instruction the guest was executing, the hypervisor
4501 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004502 *
4503 * In non SEV guest, hypervisor will be able to read the guest
4504 * memory to decode the instruction pointer when insn_len is zero
4505 * so we return true to indicate that decoding is possible.
4506 *
4507 * But in the SEV guest, the guest memory is encrypted with the
4508 * guest specific key and hypervisor will not be able to decode the
4509 * instruction pointer so we will not able to workaround it. Lets
4510 * print the error and request to kill the guest.
4511 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004512 if (likely(!insn || insn_len))
4513 return true;
4514
4515 /*
4516 * If RIP is invalid, go ahead with emulation which will cause an
4517 * internal error exit.
4518 */
4519 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4520 return true;
4521
4522 cr4 = kvm_read_cr4(vcpu);
4523 smep = cr4 & X86_CR4_SMEP;
4524 smap = cr4 & X86_CR4_SMAP;
4525 is_user = svm_get_cpl(vcpu) == 3;
Liran Alon118154b2019-07-17 02:56:58 +03004526 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004527 if (!sev_guest(vcpu->kvm))
4528 return true;
4529
Liran Alon118154b2019-07-17 02:56:58 +03004530 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004531 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4532 }
4533
4534 return false;
4535}
4536
Liran Alon4b9852f2019-08-26 13:24:49 +03004537static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4538{
4539 struct vcpu_svm *svm = to_svm(vcpu);
4540
4541 /*
4542 * TODO: Last condition latch INIT signals on vCPU when
4543 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
Paolo Bonzini33b22172020-04-17 10:24:18 -04004544 * To properly emulate the INIT intercept,
4545 * svm_check_nested_events() should call nested_svm_vmexit()
4546 * if an INIT signal is pending.
Liran Alon4b9852f2019-08-26 13:24:49 +03004547 */
4548 return !gif_set(svm) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05004549 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
Liran Alon4b9852f2019-08-26 13:24:49 +03004550}
4551
Tom Lendacky647daca2021-01-04 14:20:01 -06004552static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4553{
4554 if (!sev_es_guest(vcpu->kvm))
4555 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4556
4557 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4558}
4559
Joerg Roedeleaf78262020-03-24 10:41:54 +01004560static void svm_vm_destroy(struct kvm *kvm)
4561{
4562 avic_vm_destroy(kvm);
4563 sev_vm_destroy(kvm);
4564}
4565
4566static int svm_vm_init(struct kvm *kvm)
4567{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004568 if (!pause_filter_count || !pause_filter_thresh)
4569 kvm->arch.pause_in_guest = true;
4570
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02004571 if (enable_apicv) {
Joerg Roedeleaf78262020-03-24 10:41:54 +01004572 int ret = avic_vm_init(kvm);
4573 if (ret)
4574 return ret;
4575 }
4576
Joerg Roedeleaf78262020-03-24 10:41:54 +01004577 return 0;
4578}
4579
Sean Christopherson9c14ee22020-03-21 13:26:03 -07004580static struct kvm_x86_ops svm_x86_ops __initdata = {
Sean Christopherson9dadfc42021-10-18 11:39:28 -07004581 .name = "kvm_amd",
4582
Li RongQingdd58f3c2020-02-23 16:13:12 +08004583 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004584 .hardware_enable = svm_hardware_enable,
4585 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004586 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02004587 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004588
4589 .vcpu_create = svm_create_vcpu,
4590 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004591 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004592
Sean Christopherson562b6b02020-01-26 16:41:13 -08004593 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06004594 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004595 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004596
Avi Kivity04d2cc72007-09-10 18:10:54 +03004597 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004598 .vcpu_load = svm_vcpu_load,
4599 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05004600 .vcpu_blocking = svm_vcpu_blocking,
4601 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004602
Jason Baronb6a7cc32021-01-14 22:27:54 -05004603 .update_exception_bitmap = svm_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06004604 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605 .get_msr = svm_get_msr,
4606 .set_msr = svm_set_msr,
4607 .get_segment_base = svm_get_segment_base,
4608 .get_segment = svm_get_segment,
4609 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004610 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004611 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612 .set_cr0 = svm_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004613 .is_valid_cr4 = svm_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614 .set_cr4 = svm_set_cr4,
4615 .set_efer = svm_set_efer,
4616 .get_idt = svm_get_idt,
4617 .set_idt = svm_set_idt,
4618 .get_gdt = svm_get_gdt,
4619 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004620 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004621 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004622 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623 .get_rflags = svm_get_rflags,
4624 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004625
Sean Christopherson77809382020-03-20 14:28:18 -07004626 .tlb_flush_all = svm_flush_tlb,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004627 .tlb_flush_current = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07004628 .tlb_flush_gva = svm_flush_tlb_gva,
Sean Christopherson72b38322020-03-20 14:28:13 -07004629 .tlb_flush_guest = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630
Avi Kivity6aa8b732006-12-10 02:21:36 -08004631 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004632 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08004634 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004635 .set_interrupt_shadow = svm_set_interrupt_shadow,
4636 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004637 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004638 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004639 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004640 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004641 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004642 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004643 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004644 .get_nmi_mask = svm_get_nmi_mask,
4645 .set_nmi_mask = svm_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004646 .enable_nmi_window = svm_enable_nmi_window,
4647 .enable_irq_window = svm_enable_irq_window,
4648 .update_cr8_intercept = svm_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04004649 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004650 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06004651 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004652 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004653 .hwapic_irr_update = svm_hwapic_irr_update,
4654 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004655 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02004656
4657 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004658 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004659 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004660
Avi Kivity586f9602010-11-18 13:09:54 +02004661 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004662
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004663 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004664
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004665 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004666
Ilias Stamatis307a94c2021-05-26 19:44:13 +01004667 .get_l2_tsc_offset = svm_get_l2_tsc_offset,
4668 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
Ilias Stamatisedcfe542021-05-26 19:44:15 +01004669 .write_tsc_offset = svm_write_tsc_offset,
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004670 .write_tsc_multiplier = svm_write_tsc_multiplier,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004671
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004672 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004673
4674 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07004675 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004676
Sean Christophersond264ee02018-08-27 15:21:12 -07004677 .request_immediate_exit = __kvm_request_immediate_exit,
4678
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004679 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004680
4681 .pmu_ops = &amd_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04004682 .nested_ops = &svm_nested_ops,
4683
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004684 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08004685 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004686 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02004687 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004688
Ladi Prosek72d7b372017-10-11 16:54:41 +02004689 .smi_allowed = svm_smi_allowed,
Sean Christophersonecc513e2021-06-09 11:56:19 -07004690 .enter_smm = svm_enter_smm,
4691 .leave_smm = svm_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004692 .enable_smi_window = svm_enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004693
4694 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004695 .mem_enc_reg_region = svm_register_enc_region,
4696 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004697
Nathan Tempelman54526d12021-04-08 22:32:14 +00004698 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
Peter Gondab5663932021-10-21 10:43:00 -07004699 .vm_move_enc_context_from = svm_vm_migrate_from,
Nathan Tempelman54526d12021-04-08 22:32:14 +00004700
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004701 .can_emulate_instruction = svm_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03004702
4703 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Alexander Graffd6fa732020-09-25 16:34:19 +02004704
4705 .msr_filter_changed = svm_msr_filter_changed,
Tom Lendackyf1c63662020-12-14 10:29:50 -05004706 .complete_emulated_msr = svm_complete_emulated_msr,
Tom Lendacky647daca2021-01-04 14:20:01 -06004707
4708 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709};
4710
Sean Christophersond008dfd2020-03-21 13:25:56 -07004711static struct kvm_x86_init_ops svm_init_ops __initdata = {
4712 .cpu_has_kvm_support = has_svm,
4713 .disabled_by_bios = is_disabled,
4714 .hardware_setup = svm_hardware_setup,
4715 .check_processor_compatibility = svm_check_processor_compat,
4716
4717 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718};
4719
4720static int __init svm_init(void)
4721{
Tom Lendackyd07f46f2020-09-07 15:15:03 +02004722 __unused_size_checks();
4723
Sean Christophersond008dfd2020-03-21 13:25:56 -07004724 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004725 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726}
4727
4728static void __exit svm_exit(void)
4729{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004730 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731}
4732
4733module_init(svm_init)
4734module_exit(svm_exit)