blob: 2c99b18d76c0f9b5157b3999a733a6e46cbc8561 [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
Lai Jiangshan1af4a112021-11-18 19:08:07 +0800268static int get_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;
Lai Jiangshan58356762021-11-18 19:08:08 +0800588 sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO);
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
Sean Christophersonb95c2212021-04-21 19:11:22 -0700592 ret = sev_cpu_init(sd);
593 if (ret)
594 goto free_save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600595
Tejun Heo0fe1e002009-10-29 22:34:14 +0900596 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597
598 return 0;
599
Miaohe Lind80b64f2020-01-04 16:56:49 +0800600free_save_area:
601 __free_page(sd->save_area);
602free_cpu_data:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900603 kfree(sd);
Sean Christophersonb95c2212021-04-21 19:11:22 -0700604 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800605
606}
607
Alexander Graffd6fa732020-09-25 16:34:19 +0200608static int direct_access_msr_slot(u32 msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609{
Alexander Graffd6fa732020-09-25 16:34:19 +0200610 u32 i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800611
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100612 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
Alexander Graffd6fa732020-09-25 16:34:19 +0200613 if (direct_access_msrs[i].index == msr)
614 return i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615
Alexander Graffd6fa732020-09-25 16:34:19 +0200616 return -ENOENT;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100617}
618
Alexander Graffd6fa732020-09-25 16:34:19 +0200619static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
620 int write)
621{
622 struct vcpu_svm *svm = to_svm(vcpu);
623 int slot = direct_access_msr_slot(msr);
624
625 if (slot == -ENOENT)
626 return;
627
628 /* Set the shadow bitmaps to the desired intercept states */
629 if (read)
630 set_bit(slot, svm->shadow_msr_intercept.read);
631 else
632 clear_bit(slot, svm->shadow_msr_intercept.read);
633
634 if (write)
635 set_bit(slot, svm->shadow_msr_intercept.write);
636 else
637 clear_bit(slot, svm->shadow_msr_intercept.write);
638}
639
640static bool valid_msr_intercept(u32 index)
641{
642 return direct_access_msr_slot(index) != -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800643}
644
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200645static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100646{
647 u8 bit_write;
648 unsigned long tmp;
649 u32 offset;
650 u32 *msrpm;
651
652 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
653 to_svm(vcpu)->msrpm;
654
655 offset = svm_msrpm_offset(msr);
656 bit_write = 2 * (msr & 0x0f) + 1;
657 tmp = msrpm[offset];
658
659 BUG_ON(offset == MSR_INVALID);
660
661 return !!test_bit(bit_write, &tmp);
662}
663
Alexander Graffd6fa732020-09-25 16:34:19 +0200664static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
665 u32 msr, int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666{
Joerg Roedel455716f2010-03-01 15:34:35 +0100667 u8 bit_read, bit_write;
668 unsigned long tmp;
669 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800670
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100671 /*
672 * If this warning triggers extend the direct_access_msrs list at the
673 * beginning of the file
674 */
675 WARN_ON(!valid_msr_intercept(msr));
676
Alexander Graffd6fa732020-09-25 16:34:19 +0200677 /* Enforce non allowed MSRs to trap */
678 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
679 read = 0;
680
681 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
682 write = 0;
683
Joerg Roedel455716f2010-03-01 15:34:35 +0100684 offset = svm_msrpm_offset(msr);
685 bit_read = 2 * (msr & 0x0f);
686 bit_write = 2 * (msr & 0x0f) + 1;
687 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800688
Joerg Roedel455716f2010-03-01 15:34:35 +0100689 BUG_ON(offset == MSR_INVALID);
690
691 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
692 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
693
694 msrpm[offset] = tmp;
Vineeth Pillaic4327f12021-06-03 15:14:39 +0000695
696 svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
697
Avi Kivity6aa8b732006-12-10 02:21:36 -0800698}
699
Tom Lendacky376c6d22020-12-10 11:10:06 -0600700void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
701 int read, int write)
Alexander Graffd6fa732020-09-25 16:34:19 +0200702{
703 set_shadow_msr_intercept(vcpu, msr, read, write);
704 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
705}
706
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300707u32 *svm_vcpu_alloc_msrpm(void)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100708{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400709 unsigned int order = get_order(MSRPM_SIZE);
710 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200711 u32 *msrpm;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100712
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300713 if (!pages)
714 return NULL;
715
716 msrpm = page_address(pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400717 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100718
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200719 return msrpm;
720}
721
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300722void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800723{
Joerg Roedelf65c2292008-02-13 18:58:46 +0100724 int i;
725
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100726 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
727 if (!direct_access_msrs[i].always)
728 continue;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200729 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100730 }
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300731}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800732
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300733
734void svm_vcpu_free_msrpm(u32 *msrpm)
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300735{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400736 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800737}
738
Alexander Graffd6fa732020-09-25 16:34:19 +0200739static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
740{
741 struct vcpu_svm *svm = to_svm(vcpu);
742 u32 i;
743
744 /*
745 * Set intercept permissions for all direct access MSRs again. They
746 * will automatically get filtered through the MSR filter, so we are
747 * back in sync after this.
748 */
749 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
750 u32 msr = direct_access_msrs[i].index;
751 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
752 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
753
754 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
Anthony Liguoric8681332007-04-30 09:48:11 +0300755 }
756}
757
Joerg Roedel323c3d82010-03-01 15:34:37 +0100758static void add_msr_offset(u32 offset)
759{
760 int i;
761
762 for (i = 0; i < MSRPM_OFFSETS; ++i) {
763
764 /* Offset already in list? */
765 if (msrpm_offsets[i] == offset)
766 return;
767
768 /* Slot used by another offset? */
769 if (msrpm_offsets[i] != MSR_INVALID)
770 continue;
771
772 /* Add offset to list */
773 msrpm_offsets[i] = offset;
774
775 return;
776 }
777
778 /*
779 * If this BUG triggers the msrpm_offsets table has an overflow. Just
780 * increase MSRPM_OFFSETS in this case.
781 */
782 BUG();
783}
784
785static void init_msrpm_offsets(void)
786{
787 int i;
788
789 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
790
791 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
792 u32 offset;
793
794 offset = svm_msrpm_offset(direct_access_msrs[i].index);
795 BUG_ON(offset == MSR_INVALID);
796
797 add_msr_offset(offset);
798 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799}
800
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200801static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800802{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200803 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800804
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500805 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200806 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
807 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
808 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
809 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800810}
811
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200812static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800813{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200814 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800815
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500816 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200817 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
818 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
819 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
820 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821}
822
Joerg Roedel883b0a92020-03-24 10:41:52 +0100823void disable_nmi_singlestep(struct vcpu_svm *svm)
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200824{
825 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500826
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200827 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
828 /* Clear our flags if they were not set by the guest */
829 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
830 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
831 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
832 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
833 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200834}
835
Babu Moger8566ac82018-03-16 16:37:26 -0400836static void grow_ple_window(struct kvm_vcpu *vcpu)
837{
838 struct vcpu_svm *svm = to_svm(vcpu);
839 struct vmcb_control_area *control = &svm->vmcb->control;
840 int old = control->pause_filter_count;
841
842 control->pause_filter_count = __grow_ple_window(old,
843 pause_filter_count,
844 pause_filter_count_grow,
845 pause_filter_count_max);
846
Peter Xu4f75bcc2019-09-06 10:17:22 +0800847 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200848 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800849 trace_kvm_ple_window_update(vcpu->vcpu_id,
850 control->pause_filter_count, old);
851 }
Babu Moger8566ac82018-03-16 16:37:26 -0400852}
853
854static void shrink_ple_window(struct kvm_vcpu *vcpu)
855{
856 struct vcpu_svm *svm = to_svm(vcpu);
857 struct vmcb_control_area *control = &svm->vmcb->control;
858 int old = control->pause_filter_count;
859
860 control->pause_filter_count =
861 __shrink_ple_window(old,
862 pause_filter_count,
863 pause_filter_count_shrink,
864 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800865 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200866 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800867 trace_kvm_ple_window_update(vcpu->vcpu_id,
868 control->pause_filter_count, old);
869 }
Babu Moger8566ac82018-03-16 16:37:26 -0400870}
871
Li RongQingdd58f3c2020-02-23 16:13:12 +0800872static void svm_hardware_teardown(void)
873{
874 int cpu;
875
Sean Christopherson4cafd0c2021-04-21 19:11:20 -0700876 sev_hardware_teardown();
Li RongQingdd58f3c2020-02-23 16:13:12 +0800877
878 for_each_possible_cpu(cpu)
879 svm_cpu_uninit(cpu);
880
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400881 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
882 get_order(IOPM_SIZE));
Li RongQingdd58f3c2020-02-23 16:13:12 +0800883 iopm_base = 0;
884}
885
Avi Kivity6aa8b732006-12-10 02:21:36 -0800886static void init_seg(struct vmcb_seg *seg)
887{
888 seg->selector = 0;
889 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100890 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800891 seg->limit = 0xffff;
892 seg->base = 0;
893}
894
895static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
896{
897 seg->selector = 0;
898 seg->attrib = SVM_SELECTOR_P_MASK | type;
899 seg->limit = 0xffff;
900 seg->base = 0;
901}
902
Ilias Stamatis307a94c2021-05-26 19:44:13 +0100903static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
904{
905 struct vcpu_svm *svm = to_svm(vcpu);
906
907 return svm->nested.ctl.tsc_offset;
908}
909
910static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
911{
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300912 struct vcpu_svm *svm = to_svm(vcpu);
913
914 return svm->tsc_ratio_msr;
Ilias Stamatis307a94c2021-05-26 19:44:13 +0100915}
916
Ilias Stamatisedcfe542021-05-26 19:44:15 +0100917static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000918{
919 struct vcpu_svm *svm = to_svm(vcpu);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000920
Ilias Stamatisedcfe542021-05-26 19:44:15 +0100921 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
922 svm->vmcb->control.tsc_offset = offset;
Joerg Roedel06e78522020-06-25 10:03:23 +0200923 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000924}
925
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300926void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
Ilias Stamatis1ab92872021-06-07 11:54:38 +0100927{
928 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
929}
930
Sean Christopherson3b195ac2021-05-04 10:17:22 -0700931/* Evaluate instruction intercepts that depend on guest CPUID features. */
932static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
933 struct vcpu_svm *svm)
Babu Moger4407a792020-09-11 14:29:19 -0500934{
935 /*
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -0800936 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
937 * roots, or if INVPCID is disabled in the guest to inject #UD.
Babu Moger4407a792020-09-11 14:29:19 -0500938 */
939 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -0800940 if (!npt_enabled ||
941 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
Babu Moger4407a792020-09-11 14:29:19 -0500942 svm_set_intercept(svm, INTERCEPT_INVPCID);
943 else
944 svm_clr_intercept(svm, INTERCEPT_INVPCID);
945 }
Sean Christopherson3b195ac2021-05-04 10:17:22 -0700946
947 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
948 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
949 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
950 else
951 svm_set_intercept(svm, INTERCEPT_RDTSCP);
952 }
Babu Moger4407a792020-09-11 14:29:19 -0500953}
954
Paolo Bonzini36e81942021-09-23 12:46:07 -0400955static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
956{
957 struct vcpu_svm *svm = to_svm(vcpu);
958
959 if (guest_cpuid_is_intel(vcpu)) {
960 /*
961 * We must intercept SYSENTER_EIP and SYSENTER_ESP
962 * accesses because the processor only stores 32 bits.
963 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
964 */
965 svm_set_intercept(svm, INTERCEPT_VMLOAD);
966 svm_set_intercept(svm, INTERCEPT_VMSAVE);
967 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
968
969 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
970 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
971 } else {
972 /*
973 * If hardware supports Virtual VMLOAD VMSAVE then enable it
974 * in VMCB and clear intercepts to avoid #VMEXIT.
975 */
976 if (vls) {
977 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
978 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
979 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
980 }
981 /* No need to intercept these MSRs */
982 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
983 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
984 }
985}
986
Paolo Bonzini63129752021-03-02 14:40:39 -0500987static void init_vmcb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988{
Paolo Bonzini63129752021-03-02 14:40:39 -0500989 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedele6101a92008-02-13 18:58:45 +0100990 struct vmcb_control_area *control = &svm->vmcb->control;
991 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800992
Babu Moger830bd712020-09-11 14:28:50 -0500993 svm_set_intercept(svm, INTERCEPT_CR0_READ);
994 svm_set_intercept(svm, INTERCEPT_CR3_READ);
995 svm_set_intercept(svm, INTERCEPT_CR4_READ);
996 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
997 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
998 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -0500999 if (!kvm_vcpu_apicv_active(vcpu))
Babu Moger830bd712020-09-11 14:28:50 -05001000 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001
Paolo Bonzini5315c712014-03-03 13:08:29 +01001002 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001003
Joerg Roedel18c918c2010-11-30 18:03:59 +01001004 set_exception_intercept(svm, PF_VECTOR);
1005 set_exception_intercept(svm, UD_VECTOR);
1006 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001007 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001008 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001009 /*
1010 * Guest access to VMware backdoor ports could legitimately
1011 * trigger #GP because of TSS I/O permission bitmap.
1012 * We intercept those #GP and allow access to them anyway
1013 * as VMware does.
1014 */
1015 if (enable_vmware_backdoor)
1016 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001017
Joerg Roedela284ba52020-06-25 10:03:24 +02001018 svm_set_intercept(svm, INTERCEPT_INTR);
1019 svm_set_intercept(svm, INTERCEPT_NMI);
Maxim Levitsky4b639a92021-07-07 15:51:00 +03001020
1021 if (intercept_smi)
1022 svm_set_intercept(svm, INTERCEPT_SMI);
1023
Joerg Roedela284ba52020-06-25 10:03:24 +02001024 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1025 svm_set_intercept(svm, INTERCEPT_RDPMC);
1026 svm_set_intercept(svm, INTERCEPT_CPUID);
1027 svm_set_intercept(svm, INTERCEPT_INVD);
1028 svm_set_intercept(svm, INTERCEPT_INVLPG);
1029 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1030 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1031 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1032 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1033 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1034 svm_set_intercept(svm, INTERCEPT_VMRUN);
1035 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1036 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1037 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1038 svm_set_intercept(svm, INTERCEPT_STGI);
1039 svm_set_intercept(svm, INTERCEPT_CLGI);
1040 svm_set_intercept(svm, INTERCEPT_SKINIT);
1041 svm_set_intercept(svm, INTERCEPT_WBINVD);
1042 svm_set_intercept(svm, INTERCEPT_XSETBV);
1043 svm_set_intercept(svm, INTERCEPT_RDPRU);
1044 svm_set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045
Paolo Bonzini63129752021-03-02 14:40:39 -05001046 if (!kvm_mwait_in_guest(vcpu->kvm)) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001047 svm_set_intercept(svm, INTERCEPT_MONITOR);
1048 svm_set_intercept(svm, INTERCEPT_MWAIT);
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001049 }
1050
Paolo Bonzini63129752021-03-02 14:40:39 -05001051 if (!kvm_hlt_in_guest(vcpu->kvm))
Joerg Roedela284ba52020-06-25 10:03:24 +02001052 svm_set_intercept(svm, INTERCEPT_HLT);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001053
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001054 control->iopm_base_pa = __sme_set(iopm_base);
1055 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 control->int_ctl = V_INTR_MASKING_MASK;
1057
1058 init_seg(&save->es);
1059 init_seg(&save->ss);
1060 init_seg(&save->ds);
1061 init_seg(&save->fs);
1062 init_seg(&save->gs);
1063
1064 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001065 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001066 /* Executable/Readable Code Segment */
1067 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1068 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1069 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001070
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001071 save->gdtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001072 save->gdtr.limit = 0xffff;
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001073 save->idtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001074 save->idtr.limit = 0xffff;
1075
1076 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1077 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1078
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001079 if (npt_enabled) {
1080 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001081 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedela284ba52020-06-25 10:03:24 +02001082 svm_clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001083 clr_exception_intercept(svm, PF_VECTOR);
Babu Moger830bd712020-09-11 14:28:50 -05001084 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1085 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001086 save->g_pat = vcpu->arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001087 save->cr3 = 0;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001088 }
Cathy Avery193015a2021-01-12 11:43:13 -05001089 svm->current_vmcb->asid_generation = 0;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001090 svm->asid = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001091
Maxim Levitskyc74ad082021-05-03 15:54:43 +03001092 svm->nested.vmcb12_gpa = INVALID_GPA;
1093 svm->nested.last_vmcb12_gpa = INVALID_GPA;
Joerg Roedel2af91942009-08-07 11:49:28 +02001094
Paolo Bonzini63129752021-03-02 14:40:39 -05001095 if (!kvm_pause_in_guest(vcpu->kvm)) {
Babu Moger8566ac82018-03-16 16:37:26 -04001096 control->pause_filter_count = pause_filter_count;
1097 if (pause_filter_thresh)
1098 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedela284ba52020-06-25 10:03:24 +02001099 svm_set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001100 } else {
Joerg Roedela284ba52020-06-25 10:03:24 +02001101 svm_clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001102 }
1103
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001104 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05001105
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001106 /*
Babu Mogerd00b99c2021-02-17 10:56:04 -05001107 * If the host supports V_SPEC_CTRL then disable the interception
1108 * of MSR_IA32_SPEC_CTRL.
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001109 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05001110 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1111 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1112
Paolo Bonzini63129752021-03-02 14:40:39 -05001113 if (kvm_vcpu_apicv_active(vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001114 avic_init_vmcb(svm);
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001115
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001116 if (vgif) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001117 svm_clr_intercept(svm, INTERCEPT_STGI);
1118 svm_clr_intercept(svm, INTERCEPT_CLGI);
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001119 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1120 }
1121
Paolo Bonzini63129752021-03-02 14:40:39 -05001122 if (sev_guest(vcpu->kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001123 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001124 clr_exception_intercept(svm, UD_VECTOR);
Tom Lendacky376c6d22020-12-10 11:10:06 -06001125
Paolo Bonzini63129752021-03-02 14:40:39 -05001126 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky376c6d22020-12-10 11:10:06 -06001127 /* Perform SEV-ES specific VMCB updates */
1128 sev_es_init_vmcb(svm);
1129 }
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001130 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001131
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001132 svm_hv_init_vmcb(svm->vmcb);
Paolo Bonzini36e81942021-09-23 12:46:07 -04001133 init_vmcb_after_set_cpuid(vcpu);
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001134
Joerg Roedel06e78522020-06-25 10:03:23 +02001135 vmcb_mark_all_dirty(svm->vmcb);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001136
Joerg Roedel2af91942009-08-07 11:49:28 +02001137 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001138}
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001139
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001140static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
1141{
1142 struct vcpu_svm *svm = to_svm(vcpu);
1143
1144 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1145
1146 svm_init_osvw(vcpu);
1147 vcpu->arch.microcode_version = 0x01000065;
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001148 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio;
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001149
1150 if (sev_es_guest(vcpu->kvm))
1151 sev_es_vcpu_reset(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001152}
1153
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001154static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001155{
1156 struct vcpu_svm *svm = to_svm(vcpu);
1157
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001158 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001159 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001160
Paolo Bonzini63129752021-03-02 14:40:39 -05001161 init_vmcb(vcpu);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001162
1163 if (!init_event)
1164 __svm_vcpu_reset(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001165}
1166
Cathy Avery4995a362021-01-13 07:07:52 -05001167void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1168{
1169 svm->current_vmcb = target_vmcb;
1170 svm->vmcb = target_vmcb->ptr;
Cathy Avery4995a362021-01-13 07:07:52 -05001171}
1172
Sean Christopherson987b2592019-12-18 13:54:55 -08001173static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001174{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001175 struct vcpu_svm *svm;
Cathy Avery4995a362021-01-13 07:07:52 -05001176 struct page *vmcb01_page;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001177 struct page *vmsa_page = NULL;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001178 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001180 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1181 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001182
Joerg Roedelf65c2292008-02-13 18:58:46 +01001183 err = -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -05001184 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1185 if (!vmcb01_page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001186 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001187
Paolo Bonzini63129752021-03-02 14:40:39 -05001188 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001189 /*
1190 * SEV-ES guests require a separate VMSA page used to contain
1191 * the encrypted register state of the guest.
1192 */
1193 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1194 if (!vmsa_page)
1195 goto error_free_vmcb_page;
Tom Lendackyed02b212020-12-10 11:10:01 -06001196
1197 /*
1198 * SEV-ES guests maintain an encrypted version of their FPU
1199 * state which is restored and saved on VMRUN and VMEXIT.
Thomas Gleixnerd69c1382021-10-22 20:55:53 +02001200 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
1201 * do xsave/xrstor on it.
Tom Lendackyed02b212020-12-10 11:10:01 -06001202 */
Thomas Gleixnerd69c1382021-10-22 20:55:53 +02001203 fpstate_set_confidential(&vcpu->arch.guest_fpu);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001204 }
1205
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001206 err = avic_init_vcpu(svm);
1207 if (err)
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001208 goto error_free_vmsa_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001209
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001210 svm->msrpm = svm_vcpu_alloc_msrpm();
Chen Zhou054409a2020-11-17 10:54:26 +08001211 if (!svm->msrpm) {
1212 err = -ENOMEM;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001213 goto error_free_vmsa_page;
Chen Zhou054409a2020-11-17 10:54:26 +08001214 }
Alexander Grafb286d5d2008-11-25 20:17:05 +01001215
Cathy Avery4995a362021-01-13 07:07:52 -05001216 svm->vmcb01.ptr = page_address(vmcb01_page);
1217 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001218 svm_switch_vmcb(svm, &svm->vmcb01);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001219
1220 if (vmsa_page)
Peter Gondab67a4cc2021-10-21 10:42:59 -07001221 svm->sev_es.vmsa = page_address(vmsa_page);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001222
Michael Rotha7fc06d2021-02-02 13:01:26 -06001223 svm->guest_state_loaded = false;
Cathy Avery4995a362021-01-13 07:07:52 -05001224
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001225 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001226
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001227error_free_vmsa_page:
1228 if (vmsa_page)
1229 __free_page(vmsa_page);
Maxim Levitsky8d22b902020-08-27 20:11:42 +03001230error_free_vmcb_page:
Cathy Avery4995a362021-01-13 07:07:52 -05001231 __free_page(vmcb01_page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001232out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001233 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001234}
1235
Jim Mattsonfd65d312018-05-22 09:54:20 -07001236static void svm_clear_current_vmcb(struct vmcb *vmcb)
1237{
1238 int i;
1239
1240 for_each_online_cpu(i)
1241 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1242}
1243
Avi Kivity6aa8b732006-12-10 02:21:36 -08001244static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1245{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001246 struct vcpu_svm *svm = to_svm(vcpu);
1247
Jim Mattsonfd65d312018-05-22 09:54:20 -07001248 /*
1249 * The vmcb page can be recycled, causing a false negative in
1250 * svm_vcpu_load(). So, ensure that no logical CPU has this
1251 * vmcb page recorded as its current vmcb.
1252 */
1253 svm_clear_current_vmcb(svm->vmcb);
1254
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03001255 svm_free_nested(svm);
1256
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001257 sev_free_vcpu(vcpu);
1258
Cathy Avery4995a362021-01-13 07:07:52 -05001259 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
Krish Sadhukhan47903dc2021-04-12 17:56:05 -04001260 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261}
1262
Michael Rotha7fc06d2021-02-02 13:01:26 -06001263static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001264{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001265 struct vcpu_svm *svm = to_svm(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001266 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity0cc50642007-03-25 12:07:27 +02001267
Tom Lendackyce7ea0c2021-05-06 15:14:41 -05001268 if (sev_es_guest(vcpu->kvm))
1269 sev_es_unmap_ghcb(svm);
1270
Michael Rotha7fc06d2021-02-02 13:01:26 -06001271 if (svm->guest_state_loaded)
1272 return;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001273
Michael Rotha7fc06d2021-02-02 13:01:26 -06001274 /*
Michael Rotha7fc06d2021-02-02 13:01:26 -06001275 * Save additional host state that will be restored on VMEXIT (sev-es)
1276 * or subsequent vmload of host save area.
1277 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001278 if (sev_es_guest(vcpu->kvm)) {
Michael Rotha7fc06d2021-02-02 13:01:26 -06001279 sev_es_prepare_guest_switch(svm, vcpu->cpu);
Tom Lendacky86137772020-12-10 11:10:07 -06001280 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06001281 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06001282 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001283
Maxim Levitskyf8006502021-09-14 18:48:23 +03001284 if (tsc_scaling) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001285 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1286 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1287 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1288 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1289 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001290 }
Michael Rotha7fc06d2021-02-02 13:01:26 -06001291
Sean Christopherson0caa0a72021-05-04 10:17:25 -07001292 if (likely(tsc_aux_uret_slot >= 0))
1293 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001294
Michael Rotha7fc06d2021-02-02 13:01:26 -06001295 svm->guest_state_loaded = true;
1296}
1297
1298static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1299{
Sean Christopherson844d69c2021-04-23 15:34:04 -07001300 to_svm(vcpu)->guest_state_loaded = false;
Michael Rotha7fc06d2021-02-02 13:01:26 -06001301}
1302
1303static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1304{
1305 struct vcpu_svm *svm = to_svm(vcpu);
1306 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1307
Ashok Raj15d45072018-02-01 22:59:43 +01001308 if (sd->current_vmcb != svm->vmcb) {
1309 sd->current_vmcb = svm->vmcb;
1310 indirect_branch_prediction_barrier();
1311 }
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001312 if (kvm_vcpu_apicv_active(vcpu))
1313 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314}
1315
1316static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1317{
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001318 if (kvm_vcpu_apicv_active(vcpu))
1319 avic_vcpu_put(vcpu);
1320
Michael Rotha7fc06d2021-02-02 13:01:26 -06001321 svm_prepare_host_switch(vcpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001322
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001323 ++vcpu->stat.host_state_reload;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324}
1325
Avi Kivity6aa8b732006-12-10 02:21:36 -08001326static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1327{
Ladi Prosek9b611742017-06-21 09:06:59 +02001328 struct vcpu_svm *svm = to_svm(vcpu);
1329 unsigned long rflags = svm->vmcb->save.rflags;
1330
1331 if (svm->nmi_singlestep) {
1332 /* Hide our flags if they were not set by the guest */
1333 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1334 rflags &= ~X86_EFLAGS_TF;
1335 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1336 rflags &= ~X86_EFLAGS_RF;
1337 }
1338 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001339}
1340
1341static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1342{
Ladi Prosek9b611742017-06-21 09:06:59 +02001343 if (to_svm(vcpu)->nmi_singlestep)
1344 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1345
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001346 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001347 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001348 * (caused by either a task switch or an inter-privilege IRET),
1349 * so we do not need to update the CPL here.
1350 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001351 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001352}
1353
Marc Orrc5063552021-12-09 07:52:57 -08001354static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
1355{
1356 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
1357
1358 return sev_es_guest(vcpu->kvm)
1359 ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
1360 : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
1361}
1362
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001363static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1364{
Lai Jiangshan40e49c42021-11-08 20:43:55 +08001365 kvm_register_mark_available(vcpu, reg);
1366
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001367 switch (reg) {
1368 case VCPU_EXREG_PDPTR:
Lai Jiangshan40e49c42021-11-08 20:43:55 +08001369 /*
1370 * When !npt_enabled, mmu->pdptrs[] is already available since
1371 * it is always updated per SDM when moving to CRs.
1372 */
1373 if (npt_enabled)
Lai Jiangshan2df4a5e2021-11-24 20:20:52 +08001374 load_pdptrs(vcpu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001375 break;
1376 default:
Sean Christopherson67369272021-07-02 15:04:25 -07001377 KVM_BUG_ON(1, vcpu->kvm);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001378 }
1379}
1380
Suravee Suthikulpanite14b7782020-05-06 08:17:55 -05001381static void svm_set_vintr(struct vcpu_svm *svm)
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001382{
1383 struct vmcb_control_area *control;
1384
Maxim Levitskyf1577ab2021-07-13 17:20:16 +03001385 /*
1386 * The following fields are ignored when AVIC is enabled
1387 */
1388 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
1389
Joerg Roedela284ba52020-06-25 10:03:24 +02001390 svm_set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001391
1392 /*
1393 * This is just a dummy VINTR to actually cause a vmexit to happen.
1394 * Actual injection of virtual interrupts happens through EVENTINJ.
1395 */
1396 control = &svm->vmcb->control;
1397 control->int_vector = 0x0;
1398 control->int_ctl &= ~V_INTR_PRIO_MASK;
1399 control->int_ctl |= V_IRQ_MASK |
1400 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedel06e78522020-06-25 10:03:23 +02001401 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001402}
1403
Alexander Graff0b85052008-11-25 20:17:01 +01001404static void svm_clear_vintr(struct vcpu_svm *svm)
1405{
Joerg Roedela284ba52020-06-25 10:03:24 +02001406 svm_clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001407
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001408 /* Drop int_ctl fields related to VINTR injection. */
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001409 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001410 if (is_guest_mode(&svm->vcpu)) {
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001411 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinifb7333d2020-06-08 07:11:47 -04001412
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001413 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1414 (svm->nested.ctl.int_ctl & V_TPR_MASK));
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001415
1416 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1417 V_IRQ_INJECTION_BITS_MASK;
Maxim Levitskyaee77e12021-09-14 18:48:12 +03001418
1419 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001420 }
1421
Joerg Roedel06e78522020-06-25 10:03:23 +02001422 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001423}
1424
Avi Kivity6aa8b732006-12-10 02:21:36 -08001425static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1426{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001427 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001428 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001429
1430 switch (seg) {
1431 case VCPU_SREG_CS: return &save->cs;
1432 case VCPU_SREG_DS: return &save->ds;
1433 case VCPU_SREG_ES: return &save->es;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001434 case VCPU_SREG_FS: return &save01->fs;
1435 case VCPU_SREG_GS: return &save01->gs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436 case VCPU_SREG_SS: return &save->ss;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001437 case VCPU_SREG_TR: return &save01->tr;
1438 case VCPU_SREG_LDTR: return &save01->ldtr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001439 }
1440 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001441 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001442}
1443
1444static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1445{
1446 struct vmcb_seg *s = svm_seg(vcpu, seg);
1447
1448 return s->base;
1449}
1450
1451static void svm_get_segment(struct kvm_vcpu *vcpu,
1452 struct kvm_segment *var, int seg)
1453{
1454 struct vmcb_seg *s = svm_seg(vcpu, seg);
1455
1456 var->base = s->base;
1457 var->limit = s->limit;
1458 var->selector = s->selector;
1459 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1460 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1461 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1462 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1463 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1464 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1465 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301466
1467 /*
1468 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1469 * However, the SVM spec states that the G bit is not observed by the
1470 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1471 * So let's synthesize a legal G bit for all segments, this helps
1472 * running KVM nested. It also helps cross-vendor migration, because
1473 * Intel's vmentry has a check on the 'G' bit.
1474 */
1475 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001476
Joerg Roedele0231712010-02-24 18:59:10 +01001477 /*
1478 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001479 * for cross vendor migration purposes by "not present"
1480 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001481 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001482
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001483 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001484 case VCPU_SREG_TR:
1485 /*
1486 * Work around a bug where the busy flag in the tr selector
1487 * isn't exposed
1488 */
Amit Shahc0d09822008-10-27 09:04:18 +00001489 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001490 break;
1491 case VCPU_SREG_DS:
1492 case VCPU_SREG_ES:
1493 case VCPU_SREG_FS:
1494 case VCPU_SREG_GS:
1495 /*
1496 * The accessed bit must always be set in the segment
1497 * descriptor cache, although it can be cleared in the
1498 * descriptor, the cached bit always remains at 1. Since
1499 * Intel has a check on this, set it here to support
1500 * cross-vendor migration.
1501 */
1502 if (!var->unusable)
1503 var->type |= 0x1;
1504 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001505 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001506 /*
1507 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001508 * descriptor is left as 1, although the whole segment has
1509 * been made unusable. Clear it here to pass an Intel VMX
1510 * entry check when cross vendor migrating.
1511 */
1512 if (var->unusable)
1513 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001514 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001515 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001516 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001517 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001518}
1519
Izik Eidus2e4d2652008-03-24 19:38:34 +02001520static int svm_get_cpl(struct kvm_vcpu *vcpu)
1521{
1522 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1523
1524 return save->cpl;
1525}
1526
Gleb Natapov89a27f42010-02-16 10:51:48 +02001527static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001529 struct vcpu_svm *svm = to_svm(vcpu);
1530
Gleb Natapov89a27f42010-02-16 10:51:48 +02001531 dt->size = svm->vmcb->save.idtr.limit;
1532 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533}
1534
Gleb Natapov89a27f42010-02-16 10:51:48 +02001535static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001536{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001537 struct vcpu_svm *svm = to_svm(vcpu);
1538
Gleb Natapov89a27f42010-02-16 10:51:48 +02001539 svm->vmcb->save.idtr.limit = dt->size;
1540 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001541 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001542}
1543
Gleb Natapov89a27f42010-02-16 10:51:48 +02001544static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001545{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001546 struct vcpu_svm *svm = to_svm(vcpu);
1547
Gleb Natapov89a27f42010-02-16 10:51:48 +02001548 dt->size = svm->vmcb->save.gdtr.limit;
1549 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001550}
1551
Gleb Natapov89a27f42010-02-16 10:51:48 +02001552static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001553{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001554 struct vcpu_svm *svm = to_svm(vcpu);
1555
Gleb Natapov89a27f42010-02-16 10:51:48 +02001556 svm->vmcb->save.gdtr.limit = dt->size;
1557 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001558 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001559}
1560
Michael Roth405329f2021-12-16 11:13:54 -06001561static void svm_post_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1562{
1563 struct vcpu_svm *svm = to_svm(vcpu);
1564
1565 /*
1566 * For guests that don't set guest_state_protected, the cr3 update is
1567 * handled via kvm_mmu_load() while entering the guest. For guests
1568 * that do (SEV-ES/SEV-SNP), the cr3 update needs to be written to
1569 * VMCB save area now, since the save area will become the initial
1570 * contents of the VMSA, and future VMCB save area updates won't be
1571 * seen.
1572 */
1573 if (sev_es_guest(vcpu->kvm)) {
1574 svm->vmcb->save.cr3 = cr3;
1575 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1576 }
1577}
1578
Joerg Roedel883b0a92020-03-24 10:41:52 +01001579void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001580{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001581 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001582 u64 hcr0 = cr0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001583
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001584#ifdef CONFIG_X86_64
Tom Lendackyf1c63662020-12-14 10:29:50 -05001585 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001586 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001587 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001588 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001589 }
1590
Mike Dayd77c26f2007-10-08 09:02:08 -04001591 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001592 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001593 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001594 }
1595 }
1596#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001597 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001598
1599 if (!npt_enabled)
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001600 hcr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001601
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001602 /*
1603 * re-enable caching here because the QEMU bios
1604 * does not do it - this results in some delay at
1605 * reboot
1606 */
1607 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001608 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1609
1610 svm->vmcb->save.cr0 = hcr0;
Joerg Roedel06e78522020-06-25 10:03:23 +02001611 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001612
1613 /*
1614 * SEV-ES guests must always keep the CR intercepts cleared. CR
1615 * tracking is done using the CR write traps.
1616 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001617 if (sev_es_guest(vcpu->kvm))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001618 return;
1619
1620 if (hcr0 == cr0) {
1621 /* Selective CR0 write remains on. */
1622 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1623 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1624 } else {
1625 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1626 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1627 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001628}
1629
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001630static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1631{
1632 return true;
1633}
1634
1635void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001636{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001637 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Paolo Bonzinidc924b02020-11-15 09:44:18 -05001638 unsigned long old_cr4 = vcpu->arch.cr4;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001639
1640 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Sean Christophersonf55ac302020-03-20 14:28:12 -07001641 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001642
Joerg Roedelec077262008-04-09 14:15:28 +02001643 vcpu->arch.cr4 = cr4;
1644 if (!npt_enabled)
1645 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001646 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001647 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedel06e78522020-06-25 10:03:23 +02001648 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Jim Mattson2259c172020-10-29 10:06:48 -07001649
1650 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1651 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
1654static void svm_set_segment(struct kvm_vcpu *vcpu,
1655 struct kvm_segment *var, int seg)
1656{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001657 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658 struct vmcb_seg *s = svm_seg(vcpu, seg);
1659
1660 s->base = var->base;
1661 s->limit = var->limit;
1662 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001663 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1664 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1665 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1666 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1667 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1668 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1669 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1670 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001671
1672 /*
1673 * This is always accurate, except if SYSRET returned to a segment
1674 * with SS.DPL != 3. Intel does not have this quirk, and always
1675 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1676 * would entail passing the CPL to userspace and back.
1677 */
1678 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001679 /* This is symmetric with svm_get_segment() */
1680 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001681
Joerg Roedel06e78522020-06-25 10:03:23 +02001682 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001683}
1684
Jason Baronb6a7cc32021-01-14 22:27:54 -05001685static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001686{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001687 struct vcpu_svm *svm = to_svm(vcpu);
1688
Joerg Roedel18c918c2010-11-30 18:03:59 +01001689 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001690
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001691 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001692 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001693 set_exception_intercept(svm, BP_VECTOR);
Paolo Bonzini69869822020-07-10 17:48:06 +02001694 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001695}
1696
Tejun Heo0fe1e002009-10-29 22:34:14 +09001697static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001698{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001699 if (sd->next_asid > sd->max_asid) {
1700 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001701 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001702 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001703 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001704 }
1705
Cathy Avery193015a2021-01-12 11:43:13 -05001706 svm->current_vmcb->asid_generation = sd->asid_generation;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001707 svm->asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001708}
1709
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001710static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001711{
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001712 struct vmcb *vmcb = svm->vmcb;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001713
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001714 if (svm->vcpu.arch.guest_state_protected)
1715 return;
1716
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001717 if (unlikely(value != vmcb->save.dr6)) {
1718 vmcb->save.dr6 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001719 vmcb_mark_dirty(vmcb, VMCB_DR);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001720 }
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001721}
1722
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001723static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1724{
1725 struct vcpu_svm *svm = to_svm(vcpu);
1726
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001727 if (vcpu->arch.guest_state_protected)
1728 return;
1729
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001730 get_debugreg(vcpu->arch.db[0], 0);
1731 get_debugreg(vcpu->arch.db[1], 1);
1732 get_debugreg(vcpu->arch.db[2], 2);
1733 get_debugreg(vcpu->arch.db[3], 3);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001734 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001735 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001736 * because db_interception might need it. We can do it before vmentry.
1737 */
Paolo Bonzini5679b802020-05-04 11:28:25 -04001738 vcpu->arch.dr6 = svm->vmcb->save.dr6;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001739 vcpu->arch.dr7 = svm->vmcb->save.dr7;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001740 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1741 set_dr_intercepts(svm);
1742}
1743
Gleb Natapov020df072010-04-13 10:05:23 +03001744static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001746 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001747
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001748 if (vcpu->arch.guest_state_protected)
1749 return;
1750
Gleb Natapov020df072010-04-13 10:05:23 +03001751 svm->vmcb->save.dr7 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001752 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753}
1754
Paolo Bonzini63129752021-03-02 14:40:39 -05001755static int pf_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001756{
Paolo Bonzini63129752021-03-02 14:40:39 -05001757 struct vcpu_svm *svm = to_svm(vcpu);
1758
Sean Christopherson6d1b8672021-03-04 17:10:56 -08001759 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07001760 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001761
Paolo Bonzini63129752021-03-02 14:40:39 -05001762 return kvm_handle_page_fault(vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001763 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1764 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001765 svm->vmcb->control.insn_len);
1766}
1767
Paolo Bonzini63129752021-03-02 14:40:39 -05001768static int npf_interception(struct kvm_vcpu *vcpu)
Paolo Bonzinid0006532017-08-11 18:36:43 +02001769{
Paolo Bonzini63129752021-03-02 14:40:39 -05001770 struct vcpu_svm *svm = to_svm(vcpu);
1771
Sean Christopherson76ff3712021-06-24 19:03:54 -07001772 u64 fault_address = svm->vmcb->control.exit_info_2;
Paolo Bonzinid0006532017-08-11 18:36:43 +02001773 u64 error_code = svm->vmcb->control.exit_info_1;
1774
1775 trace_kvm_page_fault(fault_address, error_code);
Paolo Bonzini63129752021-03-02 14:40:39 -05001776 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001777 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1778 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001779 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780}
1781
Paolo Bonzini63129752021-03-02 14:40:39 -05001782static int db_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001783{
Paolo Bonzini63129752021-03-02 14:40:39 -05001784 struct kvm_run *kvm_run = vcpu->run;
1785 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001786
Paolo Bonzini63129752021-03-02 14:40:39 -05001787 if (!(vcpu->guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001788 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001789 !svm->nmi_singlestep) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001790 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
Paolo Bonzini63129752021-03-02 14:40:39 -05001791 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001792 return 1;
1793 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001794
Jan Kiszka6be7d302009-10-18 13:24:54 +02001795 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001796 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02001797 /* Make sure we check for pending NMIs upon entry */
1798 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001799 }
1800
Paolo Bonzini63129752021-03-02 14:40:39 -05001801 if (vcpu->guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001802 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001803 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Paolo Bonzinidee919d2020-05-04 09:34:10 -04001804 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1805 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
Gleb Natapov44c11432009-05-11 13:35:52 +03001806 kvm_run->debug.arch.pc =
1807 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1808 kvm_run->debug.arch.exception = DB_VECTOR;
1809 return 0;
1810 }
1811
1812 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001813}
1814
Paolo Bonzini63129752021-03-02 14:40:39 -05001815static int bp_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001816{
Paolo Bonzini63129752021-03-02 14:40:39 -05001817 struct vcpu_svm *svm = to_svm(vcpu);
1818 struct kvm_run *kvm_run = vcpu->run;
Avi Kivity851ba692009-08-24 11:10:17 +03001819
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001820 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1821 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1822 kvm_run->debug.arch.exception = BP_VECTOR;
1823 return 0;
1824}
1825
Paolo Bonzini63129752021-03-02 14:40:39 -05001826static int ud_interception(struct kvm_vcpu *vcpu)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001827{
Paolo Bonzini63129752021-03-02 14:40:39 -05001828 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001829}
1830
Paolo Bonzini63129752021-03-02 14:40:39 -05001831static int ac_interception(struct kvm_vcpu *vcpu)
Eric Northup54a20552015-11-03 18:03:53 +01001832{
Paolo Bonzini63129752021-03-02 14:40:39 -05001833 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
Eric Northup54a20552015-11-03 18:03:53 +01001834 return 1;
1835}
1836
Joerg Roedel67ec6602010-05-17 14:43:35 +02001837static bool is_erratum_383(void)
1838{
1839 int err, i;
1840 u64 value;
1841
1842 if (!erratum_383_found)
1843 return false;
1844
1845 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1846 if (err)
1847 return false;
1848
1849 /* Bit 62 may or may not be set for this mce */
1850 value &= ~(1ULL << 62);
1851
1852 if (value != 0xb600000000010015ULL)
1853 return false;
1854
1855 /* Clear MCi_STATUS registers */
1856 for (i = 0; i < 6; ++i)
1857 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1858
1859 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1860 if (!err) {
1861 u32 low, high;
1862
1863 value &= ~(1ULL << 2);
1864 low = lower_32_bits(value);
1865 high = upper_32_bits(value);
1866
1867 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1868 }
1869
1870 /* Flush tlb to evict multi-match entries */
1871 __flush_tlb_all();
1872
1873 return true;
1874}
1875
Paolo Bonzini63129752021-03-02 14:40:39 -05001876static void svm_handle_mce(struct kvm_vcpu *vcpu)
Joerg Roedel53371b52008-04-09 14:15:30 +02001877{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001878 if (is_erratum_383()) {
1879 /*
1880 * Erratum 383 triggered. Guest state is corrupt so kill the
1881 * guest.
1882 */
1883 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1884
Paolo Bonzini63129752021-03-02 14:40:39 -05001885 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001886
1887 return;
1888 }
1889
Joerg Roedel53371b52008-04-09 14:15:30 +02001890 /*
1891 * On an #MC intercept the MCE handler is not called automatically in
1892 * the host. So do it by hand here.
1893 */
Uros Bizjak1c164cb2020-04-11 17:36:27 +02001894 kvm_machine_check();
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001895}
1896
Paolo Bonzini63129752021-03-02 14:40:39 -05001897static int mc_interception(struct kvm_vcpu *vcpu)
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001898{
Joerg Roedel53371b52008-04-09 14:15:30 +02001899 return 1;
1900}
1901
Paolo Bonzini63129752021-03-02 14:40:39 -05001902static int shutdown_interception(struct kvm_vcpu *vcpu)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001903{
Paolo Bonzini63129752021-03-02 14:40:39 -05001904 struct kvm_run *kvm_run = vcpu->run;
1905 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001906
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001907 /*
Tom Lendacky8164a5f2020-12-10 11:09:45 -06001908 * The VM save area has already been encrypted so it
1909 * cannot be reinitialized - just terminate.
1910 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001911 if (sev_es_guest(vcpu->kvm))
Tom Lendacky8164a5f2020-12-10 11:09:45 -06001912 return -EINVAL;
1913
1914 /*
Sean Christopherson265e4352021-07-13 09:33:22 -07001915 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
1916 * the VMCB in a known good state. Unfortuately, KVM doesn't have
1917 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
1918 * userspace. At a platform view, INIT is acceptable behavior as
1919 * there exist bare metal platforms that automatically INIT the CPU
1920 * in response to shutdown.
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001921 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001922 clear_page(svm->vmcb);
Sean Christopherson265e4352021-07-13 09:33:22 -07001923 kvm_vcpu_reset(vcpu, true);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001924
1925 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1926 return 0;
1927}
1928
Paolo Bonzini63129752021-03-02 14:40:39 -05001929static int io_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001930{
Paolo Bonzini63129752021-03-02 14:40:39 -05001931 struct vcpu_svm *svm = to_svm(vcpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04001932 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08001933 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001934 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001935
Paolo Bonzini63129752021-03-02 14:40:39 -05001936 ++vcpu->stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001937 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001938 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1939 port = io_info >> 16;
1940 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06001941
1942 if (string) {
1943 if (sev_es_guest(vcpu->kvm))
1944 return sev_es_string_io(svm, size, port, in);
1945 else
1946 return kvm_emulate_instruction(vcpu, 0);
1947 }
1948
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001949 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001950
Paolo Bonzini63129752021-03-02 14:40:39 -05001951 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952}
1953
Paolo Bonzini63129752021-03-02 14:40:39 -05001954static int nmi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001955{
1956 return 1;
1957}
1958
Maxim Levitsky991afbb2021-07-07 15:50:58 +03001959static int smi_interception(struct kvm_vcpu *vcpu)
1960{
1961 return 1;
1962}
1963
Paolo Bonzini63129752021-03-02 14:40:39 -05001964static int intr_interception(struct kvm_vcpu *vcpu)
Joerg Roedela0698052008-04-30 17:56:01 +02001965{
Paolo Bonzini63129752021-03-02 14:40:39 -05001966 ++vcpu->stat.irq_exits;
Joerg Roedela0698052008-04-30 17:56:01 +02001967 return 1;
1968}
1969
Sean Christopherson2ac636a2021-02-04 16:57:45 -08001970static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001971{
Paolo Bonzini63129752021-03-02 14:40:39 -05001972 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001973 struct vmcb *vmcb12;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01001974 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02001975 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02001976
Paolo Bonzini63129752021-03-02 14:40:39 -05001977 if (nested_svm_check_permissions(vcpu))
Alexander Graf55426752008-11-25 20:17:06 +01001978 return 1;
1979
Paolo Bonzini63129752021-03-02 14:40:39 -05001980 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01001981 if (ret) {
1982 if (ret == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -05001983 kvm_inject_gp(vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02001984 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01001985 }
1986
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05001987 vmcb12 = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02001988
Paolo Bonzini63129752021-03-02 14:40:39 -05001989 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02001990
Maxim Levitskyadc2a232021-04-01 14:19:28 +03001991 if (vmload) {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02001992 svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
Maxim Levitskyadc2a232021-04-01 14:19:28 +03001993 svm->sysenter_eip_hi = 0;
1994 svm->sysenter_esp_hi = 0;
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02001995 } else {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02001996 svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02001997 }
Sean Christopherson2ac636a2021-02-04 16:57:45 -08001998
Paolo Bonzini63129752021-03-02 14:40:39 -05001999 kvm_vcpu_unmap(vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002000
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002001 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002002}
2003
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002004static int vmload_interception(struct kvm_vcpu *vcpu)
Alexander Graf55426752008-11-25 20:17:06 +01002005{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002006 return vmload_vmsave_interception(vcpu, true);
Alexander Graf55426752008-11-25 20:17:06 +01002007}
2008
Paolo Bonzini63129752021-03-02 14:40:39 -05002009static int vmsave_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002010{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002011 return vmload_vmsave_interception(vcpu, false);
Alexander Grafc0725422008-11-25 20:17:03 +01002012}
2013
Paolo Bonzini63129752021-03-02 14:40:39 -05002014static int vmrun_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002015{
Paolo Bonzini63129752021-03-02 14:40:39 -05002016 if (nested_svm_check_permissions(vcpu))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002017 return 1;
2018
Paolo Bonzini63129752021-03-02 14:40:39 -05002019 return nested_svm_vmrun(vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002020}
2021
Bandan Das82a11e9c2021-01-26 03:18:29 -05002022enum {
2023 NONE_SVM_INSTR,
2024 SVM_INSTR_VMRUN,
2025 SVM_INSTR_VMLOAD,
2026 SVM_INSTR_VMSAVE,
2027};
2028
2029/* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2030static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2031{
2032 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2033
2034 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2035 return NONE_SVM_INSTR;
2036
2037 switch (ctxt->modrm) {
2038 case 0xd8: /* VMRUN */
2039 return SVM_INSTR_VMRUN;
2040 case 0xda: /* VMLOAD */
2041 return SVM_INSTR_VMLOAD;
2042 case 0xdb: /* VMSAVE */
2043 return SVM_INSTR_VMSAVE;
2044 default:
2045 break;
2046 }
2047
2048 return NONE_SVM_INSTR;
2049}
2050
2051static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2052{
Wei Huang14c2bf82021-01-26 03:18:31 -05002053 const int guest_mode_exit_codes[] = {
2054 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2055 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2056 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2057 };
Paolo Bonzini63129752021-03-02 14:40:39 -05002058 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
Bandan Das82a11e9c2021-01-26 03:18:29 -05002059 [SVM_INSTR_VMRUN] = vmrun_interception,
2060 [SVM_INSTR_VMLOAD] = vmload_interception,
2061 [SVM_INSTR_VMSAVE] = vmsave_interception,
2062 };
2063 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002064 int ret;
Bandan Das82a11e9c2021-01-26 03:18:29 -05002065
Wei Huang14c2bf82021-01-26 03:18:31 -05002066 if (is_guest_mode(vcpu)) {
Sean Christopherson2df8d382021-02-23 16:56:26 -08002067 /* Returns '1' or -errno on failure, '0' on success. */
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08002068 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002069 if (ret)
2070 return ret;
2071 return 1;
2072 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002073 return svm_instr_handlers[opcode](vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002074}
2075
2076/*
2077 * #GP handling code. Note that #GP can be triggered under the following two
2078 * cases:
2079 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2080 * some AMD CPUs when EAX of these instructions are in the reserved memory
2081 * regions (e.g. SMM memory on host).
2082 * 2) VMware backdoor
2083 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002084static int gp_interception(struct kvm_vcpu *vcpu)
Bandan Das82a11e9c2021-01-26 03:18:29 -05002085{
Paolo Bonzini63129752021-03-02 14:40:39 -05002086 struct vcpu_svm *svm = to_svm(vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002087 u32 error_code = svm->vmcb->control.exit_info_1;
2088 int opcode;
2089
2090 /* Both #GP cases have zero error_code */
2091 if (error_code)
2092 goto reinject;
2093
Maxim Levitskyd1cba6c2021-09-14 18:48:14 +03002094 /* All SVM instructions expect page aligned RAX */
2095 if (svm->vmcb->save.rax & ~PAGE_MASK)
2096 goto reinject;
2097
Bandan Das82a11e9c2021-01-26 03:18:29 -05002098 /* Decode the instruction for usage later */
2099 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2100 goto reinject;
2101
2102 opcode = svm_instr_opcode(vcpu);
2103
2104 if (opcode == NONE_SVM_INSTR) {
2105 if (!enable_vmware_backdoor)
2106 goto reinject;
2107
2108 /*
2109 * VMware backdoor emulation on #GP interception only handles
2110 * IN{S}, OUT{S}, and RDPMC.
2111 */
Wei Huang14c2bf82021-01-26 03:18:31 -05002112 if (!is_guest_mode(vcpu))
2113 return kvm_emulate_instruction(vcpu,
Bandan Das82a11e9c2021-01-26 03:18:29 -05002114 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2115 } else
2116 return emulate_svm_instr(vcpu, opcode);
2117
2118reinject:
2119 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2120 return 1;
2121}
2122
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002123void svm_set_gif(struct vcpu_svm *svm, bool value)
2124{
2125 if (value) {
2126 /*
2127 * If VGIF is enabled, the STGI intercept is only added to
2128 * detect the opening of the SMI/NMI window; remove it now.
2129 * Likewise, clear the VINTR intercept, we will set it
2130 * again while processing KVM_REQ_EVENT if needed.
2131 */
2132 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02002133 svm_clr_intercept(svm, INTERCEPT_STGI);
2134 if (svm_is_intercept(svm, INTERCEPT_VINTR))
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002135 svm_clear_vintr(svm);
2136
2137 enable_gif(svm);
2138 if (svm->vcpu.arch.smi_pending ||
2139 svm->vcpu.arch.nmi_pending ||
2140 kvm_cpu_has_injectable_intr(&svm->vcpu))
2141 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2142 } else {
2143 disable_gif(svm);
2144
2145 /*
2146 * After a CLGI no interrupts should come. But if vGIF is
2147 * in use, we still rely on the VINTR intercept (rather than
2148 * STGI) to detect an open interrupt window.
2149 */
2150 if (!vgif_enabled(svm))
2151 svm_clear_vintr(svm);
2152 }
2153}
2154
Paolo Bonzini63129752021-03-02 14:40:39 -05002155static int stgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002156{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002157 int ret;
2158
Paolo Bonzini63129752021-03-02 14:40:39 -05002159 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002160 return 1;
2161
Paolo Bonzini63129752021-03-02 14:40:39 -05002162 ret = kvm_skip_emulated_instruction(vcpu);
2163 svm_set_gif(to_svm(vcpu), true);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002164 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002165}
2166
Paolo Bonzini63129752021-03-02 14:40:39 -05002167static int clgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002168{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002169 int ret;
2170
Paolo Bonzini63129752021-03-02 14:40:39 -05002171 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002172 return 1;
2173
Paolo Bonzini63129752021-03-02 14:40:39 -05002174 ret = kvm_skip_emulated_instruction(vcpu);
2175 svm_set_gif(to_svm(vcpu), false);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002176 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002177}
2178
Paolo Bonzini63129752021-03-02 14:40:39 -05002179static int invlpga_interception(struct kvm_vcpu *vcpu)
Alexander Grafff092382009-06-15 15:21:24 +02002180{
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002181 gva_t gva = kvm_rax_read(vcpu);
2182 u32 asid = kvm_rcx_read(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002183
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002184 /* FIXME: Handle an address size prefix. */
2185 if (!is_long_mode(vcpu))
2186 gva = (u32)gva;
2187
2188 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
Joerg Roedelec1ff792009-10-09 16:08:31 +02002189
Alexander Grafff092382009-06-15 15:21:24 +02002190 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002191 kvm_mmu_invlpg(vcpu, gva);
Alexander Grafff092382009-06-15 15:21:24 +02002192
Paolo Bonzini63129752021-03-02 14:40:39 -05002193 return kvm_skip_emulated_instruction(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002194}
2195
Paolo Bonzini63129752021-03-02 14:40:39 -05002196static int skinit_interception(struct kvm_vcpu *vcpu)
Joerg Roedel532a46b2009-10-09 16:08:32 +02002197{
Paolo Bonzini63129752021-03-02 14:40:39 -05002198 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002199
Paolo Bonzini63129752021-03-02 14:40:39 -05002200 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel532a46b2009-10-09 16:08:32 +02002201 return 1;
2202}
2203
Paolo Bonzini63129752021-03-02 14:40:39 -05002204static int task_switch_interception(struct kvm_vcpu *vcpu)
David Kaplandab429a2015-03-02 13:43:37 -06002205{
Paolo Bonzini63129752021-03-02 14:40:39 -05002206 struct vcpu_svm *svm = to_svm(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002207 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002208 int reason;
2209 int int_type = svm->vmcb->control.exit_int_info &
2210 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002211 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002212 uint32_t type =
2213 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2214 uint32_t idt_v =
2215 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002216 bool has_error_code = false;
2217 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002218
2219 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002220
Izik Eidus37817f22008-03-24 23:14:53 +02002221 if (svm->vmcb->control.exit_info_2 &
2222 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002223 reason = TASK_SWITCH_IRET;
2224 else if (svm->vmcb->control.exit_info_2 &
2225 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2226 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002227 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002228 reason = TASK_SWITCH_GATE;
2229 else
2230 reason = TASK_SWITCH_CALL;
2231
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002232 if (reason == TASK_SWITCH_GATE) {
2233 switch (type) {
2234 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05002235 vcpu->arch.nmi_injected = false;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002236 break;
2237 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002238 if (svm->vmcb->control.exit_info_2 &
2239 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2240 has_error_code = true;
2241 error_code =
2242 (u32)svm->vmcb->control.exit_info_2;
2243 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002244 kvm_clear_exception_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002245 break;
2246 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05002247 kvm_clear_interrupt_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002248 break;
2249 default:
2250 break;
2251 }
2252 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002253
Gleb Natapov8317c292009-04-12 13:37:02 +03002254 if (reason != TASK_SWITCH_GATE ||
2255 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2256 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002257 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002258 if (!skip_emulated_instruction(vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002259 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002260 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002261
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002262 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2263 int_vec = -1;
2264
Paolo Bonzini63129752021-03-02 14:40:39 -05002265 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002266 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002267}
2268
Paolo Bonzini63129752021-03-02 14:40:39 -05002269static int iret_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002270{
Paolo Bonzini63129752021-03-02 14:40:39 -05002271 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002272
Paolo Bonzini63129752021-03-02 14:40:39 -05002273 ++vcpu->stat.nmi_window_exits;
2274 vcpu->arch.hflags |= HF_IRET_MASK;
2275 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002276 svm_clr_intercept(svm, INTERCEPT_IRET);
Paolo Bonzini63129752021-03-02 14:40:39 -05002277 svm->nmi_iret_rip = kvm_rip_read(vcpu);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002278 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002279 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002280 return 1;
2281}
2282
Paolo Bonzini63129752021-03-02 14:40:39 -05002283static int invlpg_interception(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002284{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002285 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002286 return kvm_emulate_instruction(vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002287
Paolo Bonzini63129752021-03-02 14:40:39 -05002288 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2289 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002290}
2291
Paolo Bonzini63129752021-03-02 14:40:39 -05002292static int emulate_on_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002293{
Paolo Bonzini63129752021-03-02 14:40:39 -05002294 return kvm_emulate_instruction(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002295}
2296
Paolo Bonzini63129752021-03-02 14:40:39 -05002297static int rsm_interception(struct kvm_vcpu *vcpu)
Brijesh Singh7607b712018-02-19 10:14:44 -06002298{
Paolo Bonzini63129752021-03-02 14:40:39 -05002299 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002300}
2301
Paolo Bonzini63129752021-03-02 14:40:39 -05002302static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
Xiubo Li52eb5a62015-03-13 17:39:45 +08002303 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002304{
Paolo Bonzini63129752021-03-02 14:40:39 -05002305 struct vcpu_svm *svm = to_svm(vcpu);
2306 unsigned long cr0 = vcpu->arch.cr0;
Joerg Roedel628afd22011-04-04 12:39:36 +02002307 bool ret = false;
Joerg Roedel628afd22011-04-04 12:39:36 +02002308
Paolo Bonzini63129752021-03-02 14:40:39 -05002309 if (!is_guest_mode(vcpu) ||
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04002310 (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
Joerg Roedel628afd22011-04-04 12:39:36 +02002311 return false;
2312
2313 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2314 val &= ~SVM_CR0_SELECTIVE_MASK;
2315
2316 if (cr0 ^ val) {
2317 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2318 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2319 }
2320
2321 return ret;
2322}
2323
Andre Przywara7ff76d52010-12-21 11:12:04 +01002324#define CR_VALID (1ULL << 63)
2325
Paolo Bonzini63129752021-03-02 14:40:39 -05002326static int cr_interception(struct kvm_vcpu *vcpu)
Andre Przywara7ff76d52010-12-21 11:12:04 +01002327{
Paolo Bonzini63129752021-03-02 14:40:39 -05002328 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002329 int reg, cr;
2330 unsigned long val;
2331 int err;
2332
2333 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002334 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002335
2336 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
Paolo Bonzini63129752021-03-02 14:40:39 -05002337 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002338
2339 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002340 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2341 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2342 else
2343 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002344
2345 err = 0;
2346 if (cr >= 16) { /* mov to cr */
2347 cr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002348 val = kvm_register_read(vcpu, reg);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002349 trace_kvm_cr_write(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002350 switch (cr) {
2351 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002352 if (!check_selective_cr0_intercepted(vcpu, val))
2353 err = kvm_set_cr0(vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002354 else
2355 return 1;
2356
Andre Przywara7ff76d52010-12-21 11:12:04 +01002357 break;
2358 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002359 err = kvm_set_cr3(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002360 break;
2361 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002362 err = kvm_set_cr4(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002363 break;
2364 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002365 err = kvm_set_cr8(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002366 break;
2367 default:
2368 WARN(1, "unhandled write to CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002369 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002370 return 1;
2371 }
2372 } else { /* mov from cr */
2373 switch (cr) {
2374 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002375 val = kvm_read_cr0(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002376 break;
2377 case 2:
Paolo Bonzini63129752021-03-02 14:40:39 -05002378 val = vcpu->arch.cr2;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002379 break;
2380 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002381 val = kvm_read_cr3(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002382 break;
2383 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002384 val = kvm_read_cr4(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002385 break;
2386 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002387 val = kvm_get_cr8(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002388 break;
2389 default:
2390 WARN(1, "unhandled read from CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002391 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002392 return 1;
2393 }
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002394 kvm_register_write(vcpu, reg, val);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002395 trace_kvm_cr_read(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002396 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002397 return kvm_complete_insn_gp(vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002398}
2399
Paolo Bonzini63129752021-03-02 14:40:39 -05002400static int cr_trap(struct kvm_vcpu *vcpu)
Tom Lendackyf27ad382020-12-10 11:09:56 -06002401{
Paolo Bonzini63129752021-03-02 14:40:39 -05002402 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002403 unsigned long old_value, new_value;
2404 unsigned int cr;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002405 int ret = 0;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002406
2407 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2408
2409 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2410 switch (cr) {
2411 case 0:
2412 old_value = kvm_read_cr0(vcpu);
2413 svm_set_cr0(vcpu, new_value);
2414
2415 kvm_post_set_cr0(vcpu, old_value, new_value);
2416 break;
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002417 case 4:
2418 old_value = kvm_read_cr4(vcpu);
2419 svm_set_cr4(vcpu, new_value);
2420
2421 kvm_post_set_cr4(vcpu, old_value, new_value);
2422 break;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002423 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002424 ret = kvm_set_cr8(vcpu, new_value);
Tom Lendackyd1949b92020-12-10 11:09:58 -06002425 break;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002426 default:
2427 WARN(1, "unhandled CR%d write trap", cr);
2428 kvm_queue_exception(vcpu, UD_VECTOR);
2429 return 1;
2430 }
2431
Tom Lendackyd1949b92020-12-10 11:09:58 -06002432 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002433}
2434
Paolo Bonzini63129752021-03-02 14:40:39 -05002435static int dr_interception(struct kvm_vcpu *vcpu)
Andre Przywaracae37972010-12-21 11:12:05 +01002436{
Paolo Bonzini63129752021-03-02 14:40:39 -05002437 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002438 int reg, dr;
2439 unsigned long val;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002440 int err = 0;
Andre Przywaracae37972010-12-21 11:12:05 +01002441
Paolo Bonzini63129752021-03-02 14:40:39 -05002442 if (vcpu->guest_debug == 0) {
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002443 /*
2444 * No more DR vmexits; force a reload of the debug registers
2445 * and reenter on this instruction. The next vmexit will
2446 * retrieve the full state of the debug registers.
2447 */
2448 clr_dr_intercepts(svm);
Paolo Bonzini63129752021-03-02 14:40:39 -05002449 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002450 return 1;
2451 }
2452
Andre Przywaracae37972010-12-21 11:12:05 +01002453 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002454 return emulate_on_interception(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002455
2456 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2457 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002458 if (dr >= 16) { /* mov to DRn */
2459 dr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002460 val = kvm_register_read(vcpu, reg);
Paolo Bonzini63129752021-03-02 14:40:39 -05002461 err = kvm_set_dr(vcpu, dr, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002462 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05002463 kvm_get_dr(vcpu, dr, &val);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002464 kvm_register_write(vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002465 }
2466
Paolo Bonzini63129752021-03-02 14:40:39 -05002467 return kvm_complete_insn_gp(vcpu, err);
Andre Przywaracae37972010-12-21 11:12:05 +01002468}
2469
Paolo Bonzini63129752021-03-02 14:40:39 -05002470static int cr8_write_interception(struct kvm_vcpu *vcpu)
Joerg Roedel1d075432007-12-06 21:02:25 +01002471{
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002472 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002473
Paolo Bonzini63129752021-03-02 14:40:39 -05002474 u8 cr8_prev = kvm_get_cr8(vcpu);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002475 /* instruction emulation calls kvm_set_cr8() */
Paolo Bonzini63129752021-03-02 14:40:39 -05002476 r = cr_interception(vcpu);
2477 if (lapic_in_kernel(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002478 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002479 if (cr8_prev <= kvm_get_cr8(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002480 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002481 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Joerg Roedel1d075432007-12-06 21:02:25 +01002482 return 0;
2483}
2484
Paolo Bonzini63129752021-03-02 14:40:39 -05002485static int efer_trap(struct kvm_vcpu *vcpu)
Tom Lendacky2985afb2020-12-10 11:09:55 -06002486{
2487 struct msr_data msr_info;
2488 int ret;
2489
2490 /*
2491 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2492 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2493 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2494 * the guest doesn't have X86_FEATURE_SVM.
2495 */
2496 msr_info.host_initiated = false;
2497 msr_info.index = MSR_EFER;
Paolo Bonzini63129752021-03-02 14:40:39 -05002498 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2499 ret = kvm_set_msr_common(vcpu, &msr_info);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002500
Paolo Bonzini63129752021-03-02 14:40:39 -05002501 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002502}
2503
Tom Lendacky801e4592018-02-21 13:39:51 -06002504static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2505{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002506 msr->data = 0;
2507
2508 switch (msr->index) {
2509 case MSR_F10H_DECFG:
2510 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2511 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2512 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02002513 case MSR_IA32_PERF_CAPABILITIES:
2514 return 0;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002515 default:
Peter Xu12bc2132020-06-22 18:04:42 -04002516 return KVM_MSR_RET_INVALID;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002517 }
2518
2519 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002520}
2521
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002522static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002523{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002524 struct vcpu_svm *svm = to_svm(vcpu);
2525
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002526 switch (msr_info->index) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002527 case MSR_AMD64_TSC_RATIO:
2528 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2529 return 1;
2530 msr_info->data = svm->tsc_ratio_msr;
2531 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002532 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002533 msr_info->data = svm->vmcb01.ptr->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002534 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002535#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002536 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002537 msr_info->data = svm->vmcb01.ptr->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002538 break;
2539 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002540 msr_info->data = svm->vmcb01.ptr->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002541 break;
2542 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002543 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544 break;
2545 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002546 msr_info->data = svm->vmcb01.ptr->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002547 break;
2548#endif
2549 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002550 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002551 break;
2552 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002553 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2554 if (guest_cpuid_is_intel(vcpu))
2555 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 break;
2557 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002558 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2559 if (guest_cpuid_is_intel(vcpu))
2560 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002561 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002562 case MSR_TSC_AUX:
Paolo Bonzini46896c72015-11-12 14:49:16 +01002563 msr_info->data = svm->tsc_aux;
2564 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002565 /*
2566 * Nobody will change the following 5 values in the VMCB so we can
2567 * safely return them on rdmsr. They will always be 0 until LBRV is
2568 * implemented.
2569 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002570 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002571 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002572 break;
2573 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002574 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002575 break;
2576 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002577 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002578 break;
2579 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002580 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002581 break;
2582 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002583 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002584 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002585 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002586 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002587 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002588 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002589 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002590 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002591 case MSR_IA32_SPEC_CTRL:
2592 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002593 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002594 return 1;
2595
Babu Mogerd00b99c2021-02-17 10:56:04 -05002596 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2597 msr_info->data = svm->vmcb->save.spec_ctrl;
2598 else
2599 msr_info->data = svm->spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002600 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002601 case MSR_AMD64_VIRT_SPEC_CTRL:
2602 if (!msr_info->host_initiated &&
2603 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2604 return 1;
2605
2606 msr_info->data = svm->virt_spec_ctrl;
2607 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002608 case MSR_F15H_IC_CFG: {
2609
2610 int family, model;
2611
2612 family = guest_cpuid_family(vcpu);
2613 model = guest_cpuid_model(vcpu);
2614
2615 if (family < 0 || model < 0)
2616 return kvm_get_msr_common(vcpu, msr_info);
2617
2618 msr_info->data = 0;
2619
2620 if (family == 0x15 &&
2621 (model >= 0x2 && model < 0x20))
2622 msr_info->data = 0x1E;
2623 }
2624 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002625 case MSR_F10H_DECFG:
2626 msr_info->data = svm->msr_decfg;
2627 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002629 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630 }
2631 return 0;
2632}
2633
Tom Lendackyf1c63662020-12-14 10:29:50 -05002634static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2635{
2636 struct vcpu_svm *svm = to_svm(vcpu);
Peter Gondab67a4cc2021-10-21 10:42:59 -07002637 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
Paolo Bonzini63129752021-03-02 14:40:39 -05002638 return kvm_complete_insn_gp(vcpu, err);
Tom Lendackyf1c63662020-12-14 10:29:50 -05002639
Peter Gondab67a4cc2021-10-21 10:42:59 -07002640 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
2641 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
Tom Lendackyf1c63662020-12-14 10:29:50 -05002642 X86_TRAP_GP |
2643 SVM_EVTINJ_TYPE_EXEPT |
2644 SVM_EVTINJ_VALID);
2645 return 1;
2646}
2647
Joerg Roedel4a810182010-02-24 18:59:15 +01002648static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2649{
2650 struct vcpu_svm *svm = to_svm(vcpu);
2651 int svm_dis, chg_mask;
2652
2653 if (data & ~SVM_VM_CR_VALID_MASK)
2654 return 1;
2655
2656 chg_mask = SVM_VM_CR_VALID_MASK;
2657
2658 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2659 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2660
2661 svm->nested.vm_cr_msr &= ~chg_mask;
2662 svm->nested.vm_cr_msr |= (data & chg_mask);
2663
2664 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2665
2666 /* check for svm_disable while efer.svme is set */
2667 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2668 return 1;
2669
2670 return 0;
2671}
2672
Will Auld8fe8ab42012-11-29 12:42:12 -08002673static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002675 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002676 int r;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002677
Will Auld8fe8ab42012-11-29 12:42:12 -08002678 u32 ecx = msr->index;
2679 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002680 switch (ecx) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002681 case MSR_AMD64_TSC_RATIO:
2682 if (!msr->host_initiated && !svm->tsc_scaling_enabled)
2683 return 1;
2684
2685 if (data & TSC_RATIO_RSVD)
2686 return 1;
2687
2688 svm->tsc_ratio_msr = data;
2689
2690 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2691 nested_svm_update_tsc_ratio_msr(vcpu);
2692
2693 break;
Paolo Bonzini15038e12017-10-26 09:13:27 +02002694 case MSR_IA32_CR_PAT:
2695 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2696 return 1;
2697 vcpu->arch.pat = data;
Cathy Avery4995a362021-01-13 07:07:52 -05002698 svm->vmcb01.ptr->save.g_pat = data;
2699 if (is_guest_mode(vcpu))
2700 nested_vmcb02_compute_g_pat(svm);
Joerg Roedel06e78522020-06-25 10:03:23 +02002701 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Paolo Bonzini15038e12017-10-26 09:13:27 +02002702 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002703 case MSR_IA32_SPEC_CTRL:
2704 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002705 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002706 return 1;
2707
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002708 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002709 return 1;
2710
Babu Mogerd00b99c2021-02-17 10:56:04 -05002711 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2712 svm->vmcb->save.spec_ctrl = data;
2713 else
2714 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002715 if (!data)
2716 break;
2717
2718 /*
2719 * For non-nested:
2720 * When it's written (to non-zero) for the first time, pass
2721 * it through.
2722 *
2723 * For nested:
2724 * The handling of the MSR bitmap for L2 guests is done in
2725 * nested_svm_vmrun_msrpm.
2726 * We update the L1 MSR bit as well since it will end up
2727 * touching the MSR anyway now.
2728 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002729 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002730 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002731 case MSR_IA32_PRED_CMD:
2732 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002733 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002734 return 1;
2735
2736 if (data & ~PRED_CMD_IBPB)
2737 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002738 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002739 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002740 if (!data)
2741 break;
2742
2743 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002744 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
Ashok Raj15d45072018-02-01 22:59:43 +01002745 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002746 case MSR_AMD64_VIRT_SPEC_CTRL:
2747 if (!msr->host_initiated &&
2748 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2749 return 1;
2750
2751 if (data & ~SPEC_CTRL_SSBD)
2752 return 1;
2753
2754 svm->virt_spec_ctrl = data;
2755 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002756 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002757 svm->vmcb01.ptr->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002758 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002759#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002761 svm->vmcb01.ptr->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 break;
2763 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002764 svm->vmcb01.ptr->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002765 break;
2766 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002767 svm->vmcb01.ptr->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002768 break;
2769 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002770 svm->vmcb01.ptr->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002771 break;
2772#endif
2773 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002774 svm->vmcb01.ptr->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002775 break;
2776 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002777 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2778 /*
2779 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2780 * when we spoof an Intel vendor ID (for cross vendor migration).
2781 * In this case we use this intercept to track the high
2782 * 32 bit part of these msrs to support Intel's
2783 * implementation of SYSENTER/SYSEXIT.
2784 */
2785 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002786 break;
2787 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002788 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2789 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002791 case MSR_TSC_AUX:
Sean Christophersondbd61272021-04-23 15:34:02 -07002792 /*
Sean Christopherson844d69c2021-04-23 15:34:04 -07002793 * TSC_AUX is usually changed only during boot and never read
2794 * directly. Intercept TSC_AUX instead of exposing it to the
2795 * guest via direct_access_msrs, and switch it via user return.
Paolo Bonzini46896c72015-11-12 14:49:16 +01002796 */
Sean Christopherson844d69c2021-04-23 15:34:04 -07002797 preempt_disable();
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002798 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002799 preempt_enable();
2800 if (r)
2801 return 1;
2802
Paolo Bonzini46896c72015-11-12 14:49:16 +01002803 svm->tsc_aux = data;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002804 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002805 case MSR_IA32_DEBUGCTLMSR:
Maxim Levitsky4c849262021-09-14 18:48:19 +03002806 if (!lbrv) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002807 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2808 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002809 break;
2810 }
2811 if (data & DEBUGCTL_RESERVED_BITS)
2812 return 1;
2813
2814 svm->vmcb->save.dbgctl = data;
Joerg Roedel06e78522020-06-25 10:03:23 +02002815 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002816 if (data & (1ULL<<0))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002817 svm_enable_lbrv(vcpu);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002818 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002819 svm_disable_lbrv(vcpu);
Joerg Roedela2938c82008-02-13 16:30:28 +01002820 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002821 case MSR_VM_HSAVE_PA:
Vitaly Kuznetsovfce7e152021-06-28 12:44:20 +02002822 /*
2823 * Old kernels did not validate the value written to
2824 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
2825 * value to allow live migrating buggy or malicious guests
2826 * originating from those kernels.
2827 */
2828 if (!msr->host_initiated && !page_address_valid(vcpu, data))
2829 return 1;
2830
2831 svm->nested.hsave_msr = data & PAGE_MASK;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002832 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002833 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002834 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002835 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03002836 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002837 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002838 case MSR_F10H_DECFG: {
2839 struct kvm_msr_entry msr_entry;
2840
2841 msr_entry.index = msr->index;
2842 if (svm_get_msr_feature(&msr_entry))
2843 return 1;
2844
2845 /* Check the supported bits */
2846 if (data & ~msr_entry.data)
2847 return 1;
2848
2849 /* Don't allow the guest to change a bit, #GP */
2850 if (!msr->host_initiated && (data ^ msr_entry.data))
2851 return 1;
2852
2853 svm->msr_decfg = data;
2854 break;
2855 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08002857 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 }
2859 return 0;
2860}
2861
Paolo Bonzini63129752021-03-02 14:40:39 -05002862static int msr_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863{
Paolo Bonzini63129752021-03-02 14:40:39 -05002864 if (to_svm(vcpu)->vmcb->control.exit_info_1)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002865 return kvm_emulate_wrmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 else
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002867 return kvm_emulate_rdmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868}
2869
Paolo Bonzini63129752021-03-02 14:40:39 -05002870static int interrupt_window_interception(struct kvm_vcpu *vcpu)
Dor Laorc1150d82007-01-05 16:36:24 -08002871{
Paolo Bonzini63129752021-03-02 14:40:39 -05002872 kvm_make_request(KVM_REQ_EVENT, vcpu);
2873 svm_clear_vintr(to_svm(vcpu));
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002874
2875 /*
2876 * For AVIC, the only reason to end up here is ExtINTs.
2877 * In this case AVIC was temporarily disabled for
2878 * requesting the IRQ window and we have to re-enable it.
2879 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03002880 kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002881
Paolo Bonzini63129752021-03-02 14:40:39 -05002882 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002883 return 1;
2884}
2885
Paolo Bonzini63129752021-03-02 14:40:39 -05002886static int pause_interception(struct kvm_vcpu *vcpu)
Mark Langsdorf565d0992009-10-06 14:25:02 -05002887{
Tom Lendackyf1c63662020-12-14 10:29:50 -05002888 bool in_kernel;
2889
2890 /*
2891 * CPL is not made available for an SEV-ES guest, therefore
2892 * vcpu->arch.preempted_in_kernel can never be true. Just
2893 * set in_kernel to false as well.
2894 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002895 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002896
Wanpeng Li830f01b2020-07-31 11:12:21 +08002897 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04002898 grow_ple_window(vcpu);
2899
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002900 kvm_vcpu_on_spin(vcpu, in_kernel);
Sean Christophersonc8781fe2021-02-04 16:57:50 -08002901 return kvm_skip_emulated_instruction(vcpu);
Mark Langsdorf565d0992009-10-06 14:25:02 -05002902}
2903
Paolo Bonzini63129752021-03-02 14:40:39 -05002904static int invpcid_interception(struct kvm_vcpu *vcpu)
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04002905{
Paolo Bonzini63129752021-03-02 14:40:39 -05002906 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger4407a792020-09-11 14:29:19 -05002907 unsigned long type;
2908 gva_t gva;
2909
2910 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
2911 kvm_queue_exception(vcpu, UD_VECTOR);
2912 return 1;
2913 }
2914
2915 /*
2916 * For an INVPCID intercept:
2917 * EXITINFO1 provides the linear address of the memory operand.
2918 * EXITINFO2 provides the contents of the register operand.
2919 */
2920 type = svm->vmcb->control.exit_info_2;
2921 gva = svm->vmcb->control.exit_info_1;
2922
Babu Moger4407a792020-09-11 14:29:19 -05002923 return kvm_handle_invpcid(vcpu, type, gva);
2924}
2925
Paolo Bonzini63129752021-03-02 14:40:39 -05002926static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01002927 [SVM_EXIT_READ_CR0] = cr_interception,
2928 [SVM_EXIT_READ_CR3] = cr_interception,
2929 [SVM_EXIT_READ_CR4] = cr_interception,
2930 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06002931 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02002932 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01002933 [SVM_EXIT_WRITE_CR3] = cr_interception,
2934 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002935 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01002936 [SVM_EXIT_READ_DR0] = dr_interception,
2937 [SVM_EXIT_READ_DR1] = dr_interception,
2938 [SVM_EXIT_READ_DR2] = dr_interception,
2939 [SVM_EXIT_READ_DR3] = dr_interception,
2940 [SVM_EXIT_READ_DR4] = dr_interception,
2941 [SVM_EXIT_READ_DR5] = dr_interception,
2942 [SVM_EXIT_READ_DR6] = dr_interception,
2943 [SVM_EXIT_READ_DR7] = dr_interception,
2944 [SVM_EXIT_WRITE_DR0] = dr_interception,
2945 [SVM_EXIT_WRITE_DR1] = dr_interception,
2946 [SVM_EXIT_WRITE_DR2] = dr_interception,
2947 [SVM_EXIT_WRITE_DR3] = dr_interception,
2948 [SVM_EXIT_WRITE_DR4] = dr_interception,
2949 [SVM_EXIT_WRITE_DR5] = dr_interception,
2950 [SVM_EXIT_WRITE_DR6] = dr_interception,
2951 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002952 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2953 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002954 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002955 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002956 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01002957 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02002958 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002959 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002960 [SVM_EXIT_NMI] = nmi_interception,
Maxim Levitsky991afbb2021-07-07 15:50:58 +03002961 [SVM_EXIT_SMI] = smi_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002962 [SVM_EXIT_VINTR] = interrupt_window_interception,
Sean Christopherson32c23c72021-02-04 16:57:49 -08002963 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002964 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002965 [SVM_EXIT_IRET] = iret_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002966 [SVM_EXIT_INVD] = kvm_emulate_invd,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002967 [SVM_EXIT_PAUSE] = pause_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002968 [SVM_EXIT_HLT] = kvm_emulate_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002969 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002970 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002971 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972 [SVM_EXIT_MSR] = msr_interception,
2973 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002974 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002975 [SVM_EXIT_VMRUN] = vmrun_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002976 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
Alexander Graf55426752008-11-25 20:17:06 +01002977 [SVM_EXIT_VMLOAD] = vmload_interception,
2978 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002979 [SVM_EXIT_STGI] = stgi_interception,
2980 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002981 [SVM_EXIT_SKINIT] = skinit_interception,
Sean Christopherson3b195ac2021-05-04 10:17:22 -07002982 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002983 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
2984 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
2985 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
Sean Christopherson92f98952021-02-04 16:57:46 -08002986 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002987 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
Tom Lendacky2985afb2020-12-10 11:09:55 -06002988 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
Tom Lendackyf27ad382020-12-10 11:09:56 -06002989 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002990 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
Tom Lendackyd1949b92020-12-10 11:09:58 -06002991 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
Babu Moger4407a792020-09-11 14:29:19 -05002992 [SVM_EXIT_INVPCID] = invpcid_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002993 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06002994 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05002995 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
2996 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Tom Lendacky291bd202020-12-10 11:09:47 -06002997 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998};
2999
Joe Perchesae8cc052011-04-24 22:00:50 -07003000static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003001{
3002 struct vcpu_svm *svm = to_svm(vcpu);
3003 struct vmcb_control_area *control = &svm->vmcb->control;
3004 struct vmcb_save_area *save = &svm->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003005 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003006
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003007 if (!dump_invalid_vmcb) {
3008 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3009 return;
3010 }
3011
Jim Mattson18f63b12021-06-21 15:16:48 -07003012 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
3013 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003014 pr_err("VMCB Control Area:\n");
Babu Moger03bfeeb2020-09-11 14:28:05 -05003015 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3016 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
Babu Moger30abaa882020-09-11 14:28:12 -05003017 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3018 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
Babu Moger9780d512020-09-11 14:28:20 -05003019 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
Babu Mogerc62e2e92020-09-11 14:28:28 -05003020 pr_err("%-20s%08x %08x\n", "intercepts:",
3021 control->intercepts[INTERCEPT_WORD3],
3022 control->intercepts[INTERCEPT_WORD4]);
Joe Perchesae8cc052011-04-24 22:00:50 -07003023 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003024 pr_err("%-20s%d\n", "pause filter threshold:",
3025 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003026 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3027 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3028 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3029 pr_err("%-20s%d\n", "asid:", control->asid);
3030 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3031 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3032 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3033 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3034 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3035 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3036 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3037 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3038 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3039 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3040 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003041 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Tom Lendacky291bd202020-12-10 11:09:47 -06003042 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
Joe Perchesae8cc052011-04-24 22:00:50 -07003043 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3044 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003045 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003046 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003047 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3048 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3049 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Tom Lendacky376c6d22020-12-10 11:10:06 -06003050 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003051 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003052 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3053 "es:",
3054 save->es.selector, save->es.attrib,
3055 save->es.limit, save->es.base);
3056 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3057 "cs:",
3058 save->cs.selector, save->cs.attrib,
3059 save->cs.limit, save->cs.base);
3060 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3061 "ss:",
3062 save->ss.selector, save->ss.attrib,
3063 save->ss.limit, save->ss.base);
3064 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3065 "ds:",
3066 save->ds.selector, save->ds.attrib,
3067 save->ds.limit, save->ds.base);
3068 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3069 "fs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003070 save01->fs.selector, save01->fs.attrib,
3071 save01->fs.limit, save01->fs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003072 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3073 "gs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003074 save01->gs.selector, save01->gs.attrib,
3075 save01->gs.limit, save01->gs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003076 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3077 "gdtr:",
3078 save->gdtr.selector, save->gdtr.attrib,
3079 save->gdtr.limit, save->gdtr.base);
3080 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3081 "ldtr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003082 save01->ldtr.selector, save01->ldtr.attrib,
3083 save01->ldtr.limit, save01->ldtr.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003084 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3085 "idtr:",
3086 save->idtr.selector, save->idtr.attrib,
3087 save->idtr.limit, save->idtr.base);
3088 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3089 "tr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003090 save01->tr.selector, save01->tr.attrib,
3091 save01->tr.limit, save01->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003092 pr_err("cpl: %d efer: %016llx\n",
3093 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003094 pr_err("%-15s %016llx %-13s %016llx\n",
3095 "cr0:", save->cr0, "cr2:", save->cr2);
3096 pr_err("%-15s %016llx %-13s %016llx\n",
3097 "cr3:", save->cr3, "cr4:", save->cr4);
3098 pr_err("%-15s %016llx %-13s %016llx\n",
3099 "dr6:", save->dr6, "dr7:", save->dr7);
3100 pr_err("%-15s %016llx %-13s %016llx\n",
3101 "rip:", save->rip, "rflags:", save->rflags);
3102 pr_err("%-15s %016llx %-13s %016llx\n",
3103 "rsp:", save->rsp, "rax:", save->rax);
3104 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003105 "star:", save01->star, "lstar:", save01->lstar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003106 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003107 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
Joe Perchesae8cc052011-04-24 22:00:50 -07003108 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003109 "kernel_gs_base:", save01->kernel_gs_base,
3110 "sysenter_cs:", save01->sysenter_cs);
Joe Perchesae8cc052011-04-24 22:00:50 -07003111 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003112 "sysenter_esp:", save01->sysenter_esp,
3113 "sysenter_eip:", save01->sysenter_eip);
Joe Perchesae8cc052011-04-24 22:00:50 -07003114 pr_err("%-15s %016llx %-13s %016llx\n",
3115 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3116 pr_err("%-15s %016llx %-13s %016llx\n",
3117 "br_from:", save->br_from, "br_to:", save->br_to);
3118 pr_err("%-15s %016llx %-13s %016llx\n",
3119 "excp_from:", save->last_excp_from,
3120 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003121}
3122
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003123static bool svm_check_exit_valid(struct kvm_vcpu *vcpu, u64 exit_code)
3124{
3125 return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3126 svm_exit_handlers[exit_code]);
3127}
3128
Tom Lendackye9093fd42020-12-10 11:09:46 -06003129static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3130{
Tom Lendackye9093fd42020-12-10 11:09:46 -06003131 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3132 dump_vmcb(vcpu);
3133 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3134 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3135 vcpu->run->internal.ndata = 2;
3136 vcpu->run->internal.data[0] = exit_code;
3137 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003138 return 0;
Tom Lendackye9093fd42020-12-10 11:09:46 -06003139}
3140
Paolo Bonzini63129752021-03-02 14:40:39 -05003141int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
Tom Lendackye9093fd42020-12-10 11:09:46 -06003142{
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003143 if (!svm_check_exit_valid(vcpu, exit_code))
3144 return svm_handle_invalid_exit(vcpu, exit_code);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003145
3146#ifdef CONFIG_RETPOLINE
3147 if (exit_code == SVM_EXIT_MSR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003148 return msr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003149 else if (exit_code == SVM_EXIT_VINTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003150 return interrupt_window_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003151 else if (exit_code == SVM_EXIT_INTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003152 return intr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003153 else if (exit_code == SVM_EXIT_HLT)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003154 return kvm_emulate_halt(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003155 else if (exit_code == SVM_EXIT_NPF)
Paolo Bonzini63129752021-03-02 14:40:39 -05003156 return npf_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003157#endif
Paolo Bonzini63129752021-03-02 14:40:39 -05003158 return svm_exit_handlers[exit_code](vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003159}
3160
David Edmondson0a62a032021-09-20 11:37:35 +01003161static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
3162 u64 *info1, u64 *info2,
Sean Christopherson235ba742020-09-23 13:13:46 -07003163 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02003164{
3165 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3166
David Edmondson0a62a032021-09-20 11:37:35 +01003167 *reason = control->exit_code;
Avi Kivity586f9602010-11-18 13:09:54 +02003168 *info1 = control->exit_info_1;
3169 *info2 = control->exit_info_2;
Sean Christopherson235ba742020-09-23 13:13:46 -07003170 *intr_info = control->exit_int_info;
3171 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3172 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3173 *error_code = control->exit_int_info_err;
3174 else
3175 *error_code = 0;
Avi Kivity586f9602010-11-18 13:09:54 +02003176}
3177
Wanpeng Li404d5d72020-04-28 14:23:25 +08003178static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003179{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003180 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003181 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003182 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003183
David Edmondson0a62a032021-09-20 11:37:35 +01003184 trace_kvm_exit(vcpu, KVM_ISA_SVM);
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003185
Tom Lendackyf1c63662020-12-14 10:29:50 -05003186 /* SEV-ES guests must use the CR write traps to track CR registers. */
3187 if (!sev_es_guest(vcpu->kvm)) {
3188 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3189 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3190 if (npt_enabled)
3191 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3192 }
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003193
Joerg Roedel20307532010-11-29 17:51:48 +01003194 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003195 int vmexit;
3196
David Edmondson0a62a032021-09-20 11:37:35 +01003197 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003198
Joerg Roedel410e4d52009-08-07 11:49:44 +02003199 vmexit = nested_svm_exit_special(svm);
3200
3201 if (vmexit == NESTED_EXIT_CONTINUE)
3202 vmexit = nested_svm_exit_handled(svm);
3203
3204 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003205 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003206 }
3207
Avi Kivity04d2cc72007-09-10 18:10:54 +03003208 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3209 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3210 kvm_run->fail_entry.hardware_entry_failure_reason
3211 = svm->vmcb->control.exit_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07003212 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003213 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003214 return 0;
3215 }
3216
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003217 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003218 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003219 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3220 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003221 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003222 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003223 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003224 exit_code);
3225
Wanpeng Li404d5d72020-04-28 14:23:25 +08003226 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003227 return 1;
Wanpeng Li404d5d72020-04-28 14:23:25 +08003228
Paolo Bonzini63129752021-03-02 14:40:39 -05003229 return svm_invoke_exit_handler(vcpu, exit_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230}
3231
3232static void reload_tss(struct kvm_vcpu *vcpu)
3233{
Jim Mattson73cd6e52020-06-03 16:56:18 -07003234 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235
Tejun Heo0fe1e002009-10-29 22:34:14 +09003236 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 load_TR_desc();
3238}
3239
Paolo Bonzini63129752021-03-02 14:40:39 -05003240static void pre_svm_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241{
Paolo Bonzini63129752021-03-02 14:40:39 -05003242 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3243 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003244
Cathy Averyaf18fa72021-01-12 11:43:12 -05003245 /*
Sean Christopherson44f1b552021-04-06 10:18:11 -07003246 * If the previous vmrun of the vmcb occurred on a different physical
3247 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3248 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3249 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003250 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
Cathy Avery193015a2021-01-12 11:43:13 -05003251 svm->current_vmcb->asid_generation = 0;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003252 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05003253 svm->current_vmcb->cpu = vcpu->cpu;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003254 }
3255
Paolo Bonzini63129752021-03-02 14:40:39 -05003256 if (sev_guest(vcpu->kvm))
3257 return pre_sev_run(svm, vcpu->cpu);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003258
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003259 /* FIXME: handle wraparound of asid_generation */
Cathy Avery193015a2021-01-12 11:43:13 -05003260 if (svm->current_vmcb->asid_generation != sd->asid_generation)
Tejun Heo0fe1e002009-10-29 22:34:14 +09003261 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003262}
3263
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003264static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3265{
3266 struct vcpu_svm *svm = to_svm(vcpu);
3267
3268 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3269 vcpu->arch.hflags |= HF_NMI_MASK;
Paolo Bonzini63129752021-03-02 14:40:39 -05003270 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003271 svm_set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003272 ++vcpu->stat.nmi_injections;
3273}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003274
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003275static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003276{
3277 struct vcpu_svm *svm = to_svm(vcpu);
3278
Joerg Roedel2af91942009-08-07 11:49:28 +02003279 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003280
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003281 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3282 ++vcpu->stat.irq_injections;
3283
Alexander Graf219b65d2009-06-15 15:21:25 +02003284 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3285 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003286}
3287
Jason Baronb6a7cc32021-01-14 22:27:54 -05003288static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003289{
3290 struct vcpu_svm *svm = to_svm(vcpu);
3291
Tom Lendackyf1c63662020-12-14 10:29:50 -05003292 /*
3293 * SEV-ES guests must always keep the CR intercepts cleared. CR
3294 * tracking is done using the CR write traps.
3295 */
3296 if (sev_es_guest(vcpu->kvm))
3297 return;
3298
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003299 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003300 return;
3301
Babu Moger830bd712020-09-11 14:28:50 -05003302 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
Radim Krčmář596f3142014-03-11 19:11:18 +01003303
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003304 if (irr == -1)
3305 return;
3306
3307 if (tpr >= irr)
Babu Moger830bd712020-09-11 14:28:50 -05003308 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003309}
3310
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003311bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003312{
3313 struct vcpu_svm *svm = to_svm(vcpu);
3314 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson88c604b2020-04-22 19:25:41 -07003315 bool ret;
Cathy Avery9c3d3702020-04-14 16:11:06 -04003316
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003317 if (!gif_set(svm))
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -04003318 return true;
3319
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003320 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3321 return false;
3322
3323 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05003324 (vcpu->arch.hflags & HF_NMI_MASK);
Joerg Roedel924584c2010-04-22 12:33:07 +02003325
3326 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003327}
3328
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003329static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003330{
3331 struct vcpu_svm *svm = to_svm(vcpu);
3332 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003333 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003334
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003335 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3336 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003337 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003338
3339 return !svm_nmi_blocked(vcpu);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003340}
3341
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003342static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3343{
Paolo Bonzini63129752021-03-02 14:40:39 -05003344 return !!(vcpu->arch.hflags & HF_NMI_MASK);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003345}
3346
3347static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3348{
3349 struct vcpu_svm *svm = to_svm(vcpu);
3350
3351 if (masked) {
Paolo Bonzini63129752021-03-02 14:40:39 -05003352 vcpu->arch.hflags |= HF_NMI_MASK;
3353 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003354 svm_set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003355 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05003356 vcpu->arch.hflags &= ~HF_NMI_MASK;
3357 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003358 svm_clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003359 }
3360}
3361
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003362bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
Gleb Natapov78646122009-03-23 12:12:11 +02003363{
3364 struct vcpu_svm *svm = to_svm(vcpu);
3365 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003366
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003367 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003368 return true;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003369
Marc Orrc5063552021-12-09 07:52:57 -08003370 if (is_guest_mode(vcpu)) {
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003371 /* As long as interrupts are being delivered... */
Paolo Bonzinie9fd7612020-05-13 13:28:23 -04003372 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
Cathy Avery4995a362021-01-13 07:07:52 -05003373 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003374 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3375 return true;
3376
3377 /* ... vmexits aren't blocked by the interrupt shadow */
3378 if (nested_exit_on_intr(svm))
3379 return false;
3380 } else {
Marc Orrc5063552021-12-09 07:52:57 -08003381 if (!svm_get_if_flag(vcpu))
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003382 return true;
3383 }
3384
3385 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003386}
3387
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003388static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003389{
3390 struct vcpu_svm *svm = to_svm(vcpu);
3391 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003392 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003393
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003394 /*
3395 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3396 * e.g. if the IRQ arrived asynchronously after checking nested events.
3397 */
3398 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003399 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003400
3401 return !svm_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02003402}
3403
Jason Baronb6a7cc32021-01-14 22:27:54 -05003404static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003405{
Alexander Graf219b65d2009-06-15 15:21:25 +02003406 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003407
Joerg Roedele0231712010-02-24 18:59:10 +01003408 /*
3409 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3410 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3411 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003412 * we'll get the vintr intercept. However, if the vGIF feature is
3413 * enabled, the STGI interception will not occur. Enable the irq
3414 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003415 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003416 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003417 /*
3418 * IRQ window is not needed when AVIC is enabled,
3419 * unless we have pending ExtINT since it cannot be injected
3420 * via AVIC. In such case, we need to temporarily disable AVIC,
3421 * and fallback to injecting IRQ via V_IRQ.
3422 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03003423 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN);
Alexander Graf219b65d2009-06-15 15:21:25 +02003424 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003425 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003426}
3427
Jason Baronb6a7cc32021-01-14 22:27:54 -05003428static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003430 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003431
Paolo Bonzini63129752021-03-02 14:40:39 -05003432 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003433 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003434
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003435 if (!gif_set(svm)) {
3436 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02003437 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003438 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003439 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003440
Joerg Roedele0231712010-02-24 18:59:10 +01003441 /*
3442 * Something prevents NMI from been injected. Single step over possible
3443 * problem (IRET or exception injection or interrupt shadow)
3444 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003445 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003446 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003447 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003448}
3449
Izik Eiduscbc94022007-10-25 00:29:55 +02003450static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3451{
3452 return 0;
3453}
3454
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003455static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3456{
3457 return 0;
3458}
3459
Sean Christophersonf55ac302020-03-20 14:28:12 -07003460void svm_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003461{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003462 struct vcpu_svm *svm = to_svm(vcpu);
3463
Sean Christopherson4a41e432020-03-20 14:28:17 -07003464 /*
3465 * Flush only the current ASID even if the TLB flush was invoked via
3466 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3467 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3468 * unconditionally does a TLB flush on both nested VM-Enter and nested
3469 * VM-Exit (via kvm_mmu_reset_context()).
3470 */
Joerg Roedel38e5e922010-12-03 15:25:16 +01003471 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3472 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3473 else
Cathy Avery193015a2021-01-12 11:43:13 -05003474 svm->current_vmcb->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003475}
3476
Junaid Shahidfaff8752018-06-29 13:10:05 -07003477static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3478{
3479 struct vcpu_svm *svm = to_svm(vcpu);
3480
3481 invlpga(gva, svm->vmcb->control.asid);
3482}
3483
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003484static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3485{
3486 struct vcpu_svm *svm = to_svm(vcpu);
3487
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003488 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003489 return;
3490
Babu Moger830bd712020-09-11 14:28:50 -05003491 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003492 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003493 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003494 }
3495}
3496
Joerg Roedel649d6862008-04-16 16:51:15 +02003497static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3498{
3499 struct vcpu_svm *svm = to_svm(vcpu);
3500 u64 cr8;
3501
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003502 if (nested_svm_virtualize_tpr(vcpu) ||
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003503 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003504 return;
3505
Joerg Roedel649d6862008-04-16 16:51:15 +02003506 cr8 = kvm_get_cr8(vcpu);
3507 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3508 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3509}
3510
Paolo Bonzini63129752021-03-02 14:40:39 -05003511static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003512{
Paolo Bonzini63129752021-03-02 14:40:39 -05003513 struct vcpu_svm *svm = to_svm(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003514 u8 vector;
3515 int type;
3516 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003517 unsigned int3_injected = svm->int3_injected;
3518
3519 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003520
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003521 /*
3522 * If we've made progress since setting HF_IRET_MASK, we've
3523 * executed an IRET and can allow NMI injection.
3524 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003525 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3526 (sev_es_guest(vcpu->kvm) ||
3527 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3528 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3529 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003530 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003531
Paolo Bonzini63129752021-03-02 14:40:39 -05003532 vcpu->arch.nmi_injected = false;
3533 kvm_clear_exception_queue(vcpu);
3534 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003535
3536 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3537 return;
3538
Paolo Bonzini63129752021-03-02 14:40:39 -05003539 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003540
Gleb Natapov9222be12009-04-23 17:14:37 +03003541 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3542 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3543
3544 switch (type) {
3545 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05003546 vcpu->arch.nmi_injected = true;
Gleb Natapov9222be12009-04-23 17:14:37 +03003547 break;
3548 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003549 /*
Tom Lendackyf1c63662020-12-14 10:29:50 -05003550 * Never re-inject a #VC exception.
3551 */
3552 if (vector == X86_TRAP_VC)
3553 break;
3554
3555 /*
Jan Kiszka66b71382010-02-23 17:47:56 +01003556 * In case of software exceptions, do not reinject the vector,
3557 * but re-execute the instruction instead. Rewind RIP first
3558 * if we emulated INT3 before.
3559 */
3560 if (kvm_exception_is_soft(vector)) {
3561 if (vector == BP_VECTOR && int3_injected &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003562 kvm_is_linear_rip(vcpu, svm->int3_rip))
3563 kvm_rip_write(vcpu,
3564 kvm_rip_read(vcpu) - int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003565 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003566 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003567 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3568 u32 err = svm->vmcb->control.exit_int_info_err;
Paolo Bonzini63129752021-03-02 14:40:39 -05003569 kvm_requeue_exception_e(vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003570
3571 } else
Paolo Bonzini63129752021-03-02 14:40:39 -05003572 kvm_requeue_exception(vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003573 break;
3574 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05003575 kvm_queue_interrupt(vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003576 break;
3577 default:
3578 break;
3579 }
3580}
3581
Avi Kivityb463a6f2010-07-20 15:06:17 +03003582static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3583{
3584 struct vcpu_svm *svm = to_svm(vcpu);
3585 struct vmcb_control_area *control = &svm->vmcb->control;
3586
3587 control->exit_int_info = control->event_inj;
3588 control->exit_int_info_err = control->event_inj_err;
3589 control->event_inj = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003590 svm_complete_interrupts(vcpu);
Avi Kivityb463a6f2010-07-20 15:06:17 +03003591}
3592
Sean Christophersonfc4fad72021-12-28 23:24:36 +00003593static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
3594{
3595 return 1;
3596}
3597
Wanpeng Li404d5d72020-04-28 14:23:25 +08003598static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003599{
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003600 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003601 to_svm(vcpu)->vmcb->control.exit_info_1)
3602 return handle_fastpath_set_msr_irqoff(vcpu);
3603
3604 return EXIT_FASTPATH_NONE;
3605}
3606
Paolo Bonzini63129752021-03-02 14:40:39 -05003607static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
Thomas Gleixner135961e2020-07-08 21:51:58 +02003608{
Paolo Bonzini63129752021-03-02 14:40:39 -05003609 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christophersond1788192021-04-06 10:18:09 -07003610 unsigned long vmcb_pa = svm->current_vmcb->pa;
Paolo Bonzini63129752021-03-02 14:40:39 -05003611
Sean Christophersonbc908e02021-05-04 17:27:35 -07003612 kvm_guest_enter_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003613
Paolo Bonzini63129752021-03-02 14:40:39 -05003614 if (sev_es_guest(vcpu->kvm)) {
Sean Christophersond1788192021-04-06 10:18:09 -07003615 __svm_sev_es_vcpu_run(vmcb_pa);
Tom Lendacky16809ec2020-12-10 11:10:08 -06003616 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06003617 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3618
Sean Christophersond1788192021-04-06 10:18:09 -07003619 /*
3620 * Use a single vmcb (vmcb01 because it's always valid) for
3621 * context switching guest state via VMLOAD/VMSAVE, that way
3622 * the state doesn't need to be copied between vmcb01 and
3623 * vmcb02 when switching vmcbs for nested virtualization.
3624 */
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003625 vmload(svm->vmcb01.pa);
Sean Christophersond1788192021-04-06 10:18:09 -07003626 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003627 vmsave(svm->vmcb01.pa);
Thomas Gleixner135961e2020-07-08 21:51:58 +02003628
Michael Rothe79b91b2021-02-02 13:01:24 -06003629 vmload(__sme_page_pa(sd->save_area));
Tom Lendacky16809ec2020-12-10 11:10:08 -06003630 }
Thomas Gleixner135961e2020-07-08 21:51:58 +02003631
Sean Christophersonbc908e02021-05-04 17:27:35 -07003632 kvm_guest_exit_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003633}
3634
Qian Caib95273f2020-04-15 11:37:09 -04003635static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003637 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003638
Lorenzo Bresciad95df952020-12-23 14:45:07 +00003639 trace_kvm_entry(vcpu);
3640
Joerg Roedel2041a062010-04-22 12:33:08 +02003641 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3642 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3643 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3644
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003645 /*
Ladi Proseka12713c2017-06-21 09:07:00 +02003646 * Disable singlestep if we're injecting an interrupt/exception.
3647 * We don't want our modified rflags to be pushed on the stack where
3648 * we might not be able to easily reset them if we disabled NMI
3649 * singlestep later.
3650 */
3651 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3652 /*
3653 * Event injection happens before external interrupts cause a
3654 * vmexit and interrupts are disabled here, so smp_send_reschedule
3655 * is enough to force an immediate vmexit.
3656 */
3657 disable_nmi_singlestep(svm);
3658 smp_send_reschedule(vcpu->cpu);
3659 }
3660
Paolo Bonzini63129752021-03-02 14:40:39 -05003661 pre_svm_run(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003662
Joerg Roedel649d6862008-04-16 16:51:15 +02003663 sync_lapic_to_cr8(vcpu);
3664
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04003665 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3666 svm->vmcb->control.asid = svm->asid;
3667 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3668 }
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003669 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670
Vineeth Pillai11836462021-06-03 15:14:40 +00003671 svm_hv_update_vp_id(svm->vmcb, vcpu);
3672
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003673 /*
3674 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3675 * of a #DB.
3676 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003677 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003678 svm_set_dr6(svm, vcpu->arch.dr6);
3679 else
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08003680 svm_set_dr6(svm, DR6_ACTIVE_LOW);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003681
Avi Kivity04d2cc72007-09-10 18:10:54 +03003682 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003683 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003684
Wanpeng Li010fd372020-09-10 17:50:41 +08003685 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003686
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003687 /*
3688 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3689 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3690 * is no need to worry about the conditional branch over the wrmsr
3691 * being speculatively taken.
3692 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003693 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3694 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003695
Paolo Bonzini63129752021-03-02 14:40:39 -05003696 svm_vcpu_enter_exit(vcpu);
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003697
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003698 /*
3699 * We do not use IBRS in the kernel. If this vCPU has used the
3700 * SPEC_CTRL MSR it may have left it on; save the value and
3701 * turn it off. This is much more efficient than blindly adding
3702 * it to the atomic save/restore list. Especially as the former
3703 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3704 *
3705 * For non-nested case:
3706 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3707 * save it.
3708 *
3709 * For nested case:
3710 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3711 * save it.
3712 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003713 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3714 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003715 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003716
Paolo Bonzini63129752021-03-02 14:40:39 -05003717 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky16809ec2020-12-10 11:10:08 -06003718 reload_tss(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003719
Babu Mogerd00b99c2021-02-17 10:56:04 -05003720 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3721 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003722
Paolo Bonzini63129752021-03-02 14:40:39 -05003723 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003724 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3725 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3726 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3727 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3728 }
Paolo Bonzini41e68b62021-11-26 07:00:15 -05003729 vcpu->arch.regs_dirty = 0;
Avi Kivity13c34e02010-10-21 12:20:31 +02003730
Joerg Roedel3781c012011-01-14 16:45:02 +01003731 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Sean Christophersondb215752021-11-11 02:07:32 +00003732 kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
Joerg Roedel3781c012011-01-14 16:45:02 +01003733
Aaron Lewis139a12c2019-10-21 16:30:25 -07003734 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003735 stgi();
3736
3737 /* Any pending NMI will happen here */
3738
3739 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003740 kvm_after_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003741
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003742 sync_cr8_to_lapic(vcpu);
3743
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003744 svm->next_rip = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003745 if (is_guest_mode(vcpu)) {
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05003746 nested_sync_control_from_vmcb02(svm);
Krish Sadhukhanb93af022021-06-09 14:03:38 -04003747
3748 /* Track VMRUNs that have made past consistency checking */
3749 if (svm->nested.nested_run_pending &&
3750 svm->vmcb->control.exit_code != SVM_EXIT_ERR)
3751 ++vcpu->stat.nested_run;
3752
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -04003753 svm->nested.nested_run_pending = 0;
3754 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003755
Joerg Roedel38e5e922010-12-03 15:25:16 +01003756 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Wanpeng Lie42c6822020-09-12 02:16:39 -04003757 vmcb_mark_all_clean(svm->vmcb);
Joerg Roedel38e5e922010-12-03 15:25:16 +01003758
Gleb Natapov631bc482010-10-14 11:22:52 +02003759 /* if exit due to PF check for async PF */
3760 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003761 vcpu->arch.apf.host_apf_flags =
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003762 kvm_read_and_reset_apf_flags();
Gleb Natapov631bc482010-10-14 11:22:52 +02003763
Paolo Bonzini41e68b62021-11-26 07:00:15 -05003764 vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET;
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003765
3766 /*
3767 * We need to handle MC intercepts here before the vcpu has a chance to
3768 * change the physical cpu
3769 */
3770 if (unlikely(svm->vmcb->control.exit_code ==
3771 SVM_EXIT_EXCP_BASE + MC_VECTOR))
Paolo Bonzini63129752021-03-02 14:40:39 -05003772 svm_handle_mce(vcpu);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003773
Paolo Bonzini63129752021-03-02 14:40:39 -05003774 svm_complete_interrupts(vcpu);
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003775
3776 if (is_guest_mode(vcpu))
3777 return EXIT_FASTPATH_NONE;
3778
3779 return svm_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003780}
3781
Sean Christophersone83bc092021-03-05 10:31:13 -08003782static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
Sean Christopherson2a40b902020-07-15 20:41:18 -07003783 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003784{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003785 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003786 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003787
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003788 if (npt_enabled) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003789 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
Joerg Roedel06e78522020-06-25 10:03:23 +02003790 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003791
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00003792 hv_track_root_tdp(vcpu, root_hpa);
3793
Paolo Bonzini978ce582020-05-20 08:37:37 -04003794 cr3 = vcpu->arch.cr3;
Sean Christophersone83bc092021-03-05 10:31:13 -08003795 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003796 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
Sean Christophersone83bc092021-03-05 10:31:13 -08003797 } else {
3798 /* PCID in the guest should be impossible with a 32-bit MMU. */
3799 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3800 cr3 = root_hpa;
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003801 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003802
Paolo Bonzini978ce582020-05-20 08:37:37 -04003803 svm->vmcb->save.cr3 = cr3;
Joerg Roedel06e78522020-06-25 10:03:23 +02003804 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003805}
3806
Avi Kivity6aa8b732006-12-10 02:21:36 -08003807static int is_disabled(void)
3808{
Joerg Roedel6031a612007-06-22 12:29:50 +03003809 u64 vm_cr;
3810
3811 rdmsrl(MSR_VM_CR, vm_cr);
3812 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3813 return 1;
3814
Avi Kivity6aa8b732006-12-10 02:21:36 -08003815 return 0;
3816}
3817
Ingo Molnar102d8322007-02-19 14:37:47 +02003818static void
3819svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3820{
3821 /*
3822 * Patch in the VMMCALL instruction:
3823 */
3824 hypercall[0] = 0x0f;
3825 hypercall[1] = 0x01;
3826 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003827}
3828
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003829static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003830{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003831 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003832}
3833
Avi Kivity774ead32007-12-26 13:57:04 +02003834static bool svm_cpu_has_accelerated_tpr(void)
3835{
3836 return false;
3837}
3838
Tom Lendacky57194552020-12-10 11:10:00 -06003839/*
3840 * The kvm parameter can be NULL (module initialization, or invocation before
3841 * VM creation). Be sure to check the kvm parameter before using it.
3842 */
3843static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003844{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003845 switch (index) {
3846 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02003847 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003848 return false;
Tom Lendacky57194552020-12-10 11:10:00 -06003849 case MSR_IA32_SMBASE:
3850 /* SEV-ES guests do not support SMM, so report false */
3851 if (kvm && sev_es_guest(kvm))
3852 return false;
3853 break;
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003854 default:
3855 break;
3856 }
3857
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003858 return true;
3859}
3860
Paolo Bonzinifc07e762015-10-01 13:20:22 +02003861static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3862{
3863 return 0;
3864}
3865
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08003866static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08003867{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003868 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger96308b02020-11-12 16:18:03 -06003869 struct kvm_cpuid_entry2 *best;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003870
Aaron Lewis72041602019-10-21 16:30:20 -07003871 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08003872 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07003873 boot_cpu_has(X86_FEATURE_XSAVES);
3874
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003875 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08003876 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003877 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05003878
Maxim Levitsky5228eb92021-09-14 18:48:24 +03003879 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
3880
Sean Christopherson3b195ac2021-05-04 10:17:22 -07003881 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05003882
Babu Moger96308b02020-11-12 16:18:03 -06003883 /* For sev guests, the memory encryption bit is not reserved in CR3. */
3884 if (sev_guest(vcpu->kvm)) {
3885 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
3886 if (best)
Sean Christophersonca29e142021-02-03 16:01:12 -08003887 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
Babu Moger96308b02020-11-12 16:18:03 -06003888 }
3889
Maxim Levitskyadc2a232021-04-01 14:19:28 +03003890 if (kvm_vcpu_apicv_active(vcpu)) {
3891 /*
3892 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3893 * is exposed to the guest, disable AVIC.
3894 */
3895 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3896 kvm_request_apicv_update(vcpu->kvm, false,
3897 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05003898
Maxim Levitskyadc2a232021-04-01 14:19:28 +03003899 /*
3900 * Currently, AVIC does not work with nested virtualization.
3901 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3902 */
3903 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3904 kvm_request_apicv_update(vcpu->kvm, false,
3905 APICV_INHIBIT_REASON_NESTED);
3906 }
Paolo Bonzini36e81942021-09-23 12:46:07 -04003907 init_vmcb_after_set_cpuid(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08003908}
3909
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003910static bool svm_has_wbinvd_exit(void)
3911{
3912 return true;
3913}
3914
Joerg Roedel80612522011-04-04 12:39:33 +02003915#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003916 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003917#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003918 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003919#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003920 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003921
Mathias Krause09941fb2012-08-30 01:30:20 +02003922static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003923 u32 exit_code;
3924 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003925} x86_intercept_map[] = {
3926 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
3927 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
3928 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
3929 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
3930 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003931 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
3932 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003933 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
3934 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
3935 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
3936 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
3937 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
3938 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
3939 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
3940 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003941 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
3942 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
3943 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
3944 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
3945 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
3946 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
3947 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
3948 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003949 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
3950 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
3951 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02003952 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
3953 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
3954 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
3955 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
3956 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
3957 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
3958 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
3959 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
3960 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02003961 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
3962 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
3963 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
3964 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
3965 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
3966 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
3967 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02003968 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
3969 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
3970 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
3971 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02003972 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003973};
3974
Joerg Roedel80612522011-04-04 12:39:33 +02003975#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003976#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003977#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003978
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003979static int svm_check_intercept(struct kvm_vcpu *vcpu,
3980 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08003981 enum x86_intercept_stage stage,
3982 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02003983{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003984 struct vcpu_svm *svm = to_svm(vcpu);
3985 int vmexit, ret = X86EMUL_CONTINUE;
3986 struct __x86_intercept icpt_info;
3987 struct vmcb *vmcb = svm->vmcb;
3988
3989 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
3990 goto out;
3991
3992 icpt_info = x86_intercept_map[info->intercept];
3993
Avi Kivity40e19b52011-04-21 12:35:41 +03003994 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003995 goto out;
3996
3997 switch (icpt_info.exit_code) {
3998 case SVM_EXIT_READ_CR0:
3999 if (info->intercept == x86_intercept_cr_read)
4000 icpt_info.exit_code += info->modrm_reg;
4001 break;
4002 case SVM_EXIT_WRITE_CR0: {
4003 unsigned long cr0, val;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004004
4005 if (info->intercept == x86_intercept_cr_write)
4006 icpt_info.exit_code += info->modrm_reg;
4007
Jan Kiszka62baf442014-06-29 21:55:53 +02004008 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4009 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004010 break;
4011
Emanuele Giuseppe Esposito8fc78902021-11-03 10:05:26 -04004012 if (!(vmcb12_is_intercept(&svm->nested.ctl,
Babu Mogerc62e2e92020-09-11 14:28:28 -05004013 INTERCEPT_SELECTIVE_CR0)))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004014 break;
4015
4016 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4017 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4018
4019 if (info->intercept == x86_intercept_lmsw) {
4020 cr0 &= 0xfUL;
4021 val &= 0xfUL;
4022 /* lmsw can't clear PE - catch this here */
4023 if (cr0 & X86_CR0_PE)
4024 val |= X86_CR0_PE;
4025 }
4026
4027 if (cr0 ^ val)
4028 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4029
4030 break;
4031 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004032 case SVM_EXIT_READ_DR0:
4033 case SVM_EXIT_WRITE_DR0:
4034 icpt_info.exit_code += info->modrm_reg;
4035 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004036 case SVM_EXIT_MSR:
4037 if (info->intercept == x86_intercept_wrmsr)
4038 vmcb->control.exit_info_1 = 1;
4039 else
4040 vmcb->control.exit_info_1 = 0;
4041 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004042 case SVM_EXIT_PAUSE:
4043 /*
4044 * We get this for NOP only, but pause
4045 * is rep not, check this here
4046 */
4047 if (info->rep_prefix != REPE_PREFIX)
4048 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004049 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004050 case SVM_EXIT_IOIO: {
4051 u64 exit_info;
4052 u32 bytes;
4053
Joerg Roedelf6511932011-04-04 12:39:35 +02004054 if (info->intercept == x86_intercept_in ||
4055 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004056 exit_info = ((info->src_val & 0xffff) << 16) |
4057 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004058 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004059 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004060 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004061 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004062 }
4063
4064 if (info->intercept == x86_intercept_outs ||
4065 info->intercept == x86_intercept_ins)
4066 exit_info |= SVM_IOIO_STR_MASK;
4067
4068 if (info->rep_prefix)
4069 exit_info |= SVM_IOIO_REP_MASK;
4070
4071 bytes = min(bytes, 4u);
4072
4073 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4074
4075 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4076
4077 vmcb->control.exit_info_1 = exit_info;
4078 vmcb->control.exit_info_2 = info->next_rip;
4079
4080 break;
4081 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004082 default:
4083 break;
4084 }
4085
Bandan Dasf1047652015-06-11 02:05:33 -04004086 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4087 if (static_cpu_has(X86_FEATURE_NRIPS))
4088 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004089 vmcb->control.exit_code = icpt_info.exit_code;
4090 vmexit = nested_svm_exit_handled(svm);
4091
4092 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4093 : X86EMUL_CONTINUE;
4094
4095out:
4096 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004097}
4098
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07004099static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004100{
Yang Zhanga547c6d2013-04-11 19:25:10 +08004101}
4102
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004103static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4104{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004105 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04004106 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004107}
4108
Borislav Petkov74f16902017-03-26 23:51:24 +02004109static void svm_setup_mce(struct kvm_vcpu *vcpu)
4110{
4111 /* [63:9] are reserved. */
4112 vcpu->arch.mcg_cap &= 0x1ff;
4113}
4114
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004115bool svm_smi_blocked(struct kvm_vcpu *vcpu)
Ladi Prosek72d7b372017-10-11 16:54:41 +02004116{
Ladi Prosek05cade72017-10-11 16:54:45 +02004117 struct vcpu_svm *svm = to_svm(vcpu);
4118
4119 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4120 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004121 return true;
4122
4123 return is_smm(vcpu);
4124}
4125
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004126static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004127{
4128 struct vcpu_svm *svm = to_svm(vcpu);
4129 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004130 return -EBUSY;
Ladi Prosek05cade72017-10-11 16:54:45 +02004131
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004132 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4133 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004134 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004135
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004136 return !svm_smi_blocked(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02004137}
4138
Sean Christophersonecc513e2021-06-09 11:56:19 -07004139static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004140{
Ladi Prosek05cade72017-10-11 16:54:45 +02004141 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004142 struct kvm_host_map map_save;
Ladi Prosek05cade72017-10-11 16:54:45 +02004143 int ret;
4144
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004145 if (!is_guest_mode(vcpu))
4146 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004147
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004148 /* FED8h - SVM Guest */
4149 put_smstate(u64, smstate, 0x7ed8, 1);
4150 /* FEE0h - SVM Guest VMCB Physical Address */
4151 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
Ladi Prosek05cade72017-10-11 16:54:45 +02004152
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004153 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4154 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4155 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004156
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004157 ret = nested_svm_vmexit(svm);
4158 if (ret)
4159 return ret;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004160
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004161 /*
4162 * KVM uses VMCB01 to store L1 host state while L2 runs but
4163 * VMCB01 is going to be used during SMM and thus the state will
4164 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
4165 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
4166 * format of the area is identical to guest save area offsetted
4167 * by 0x400 (matches the offset of 'struct vmcb_save_area'
4168 * within 'struct vmcb'). Note: HSAVE area may also be used by
4169 * L1 hypervisor to save additional host context (e.g. KVM does
4170 * that, see svm_prepare_guest_switch()) which must be
4171 * preserved.
4172 */
4173 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
4174 &map_save) == -EINVAL)
4175 return 1;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004176
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004177 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004178
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004179 svm_copy_vmrun_state(map_save.hva + 0x400,
4180 &svm->vmcb01.ptr->save);
4181
4182 kvm_vcpu_unmap(vcpu, &map_save, true);
Ladi Prosek0234bf82017-10-11 16:54:40 +02004183 return 0;
4184}
4185
Sean Christophersonecc513e2021-06-09 11:56:19 -07004186static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004187{
Ladi Prosek05cade72017-10-11 16:54:45 +02004188 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004189 struct kvm_host_map map, map_save;
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004190 u64 saved_efer, vmcb12_gpa;
4191 struct vmcb *vmcb12;
4192 int ret;
Ladi Prosek05cade72017-10-11 16:54:45 +02004193
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004194 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
4195 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004196
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004197 /* Non-zero if SMI arrived while vCPU was in guest mode. */
4198 if (!GET_SMSTATE(u64, smstate, 0x7ed8))
4199 return 0;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004200
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004201 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4202 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004203
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004204 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4205 if (!(saved_efer & EFER_SVME))
4206 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004207
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004208 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4209 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4210 return 1;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03004211
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004212 ret = 1;
4213 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
4214 goto unmap_map;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004215
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004216 if (svm_allocate_nested(svm))
4217 goto unmap_save;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004218
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004219 /*
4220 * Restore L1 host state from L1 HSAVE area as VMCB01 was
4221 * used during SMM (see svm_enter_smm())
4222 */
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004223
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004224 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
Maxim Levitskye2e6e442021-09-13 17:09:49 +03004225
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004226 /*
4227 * Enter the nested guest now
4228 */
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004229
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004230 vmcb12 = map.hva;
Emanuele Giuseppe Esposito79071602021-11-03 10:05:23 -04004231 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
Emanuele Giuseppe Espositof2740a82021-11-03 10:05:22 -04004232 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004233 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
4234
4235unmap_save:
4236 kvm_vcpu_unmap(vcpu, &map_save, true);
4237unmap_map:
4238 kvm_vcpu_unmap(vcpu, &map, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004239 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004240}
4241
Jason Baronb6a7cc32021-01-14 22:27:54 -05004242static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004243{
4244 struct vcpu_svm *svm = to_svm(vcpu);
4245
4246 if (!gif_set(svm)) {
4247 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02004248 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004249 /* STGI will cause a vm exit */
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004250 } else {
4251 /* We must be in SMM; RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004252 }
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004253}
4254
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004255static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004256{
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004257 bool smep, smap, is_user;
4258 unsigned long cr4;
Paolo Bonzinie72436b2020-04-17 12:21:06 -04004259
4260 /*
Tom Lendackybc624d92020-12-10 11:09:44 -06004261 * When the guest is an SEV-ES guest, emulation is not possible.
4262 */
4263 if (sev_es_guest(vcpu->kvm))
4264 return false;
4265
4266 /*
Liran Alon118154b2019-07-17 02:56:58 +03004267 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4268 *
4269 * Errata:
4270 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4271 * possible that CPU microcode implementing DecodeAssist will fail
4272 * to read bytes of instruction which caused #NPF. In this case,
4273 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4274 * return 0 instead of the correct guest instruction bytes.
4275 *
4276 * This happens because CPU microcode reading instruction bytes
4277 * uses a special opcode which attempts to read data using CPL=0
Ingo Molnard9f6e122021-03-18 15:28:01 +01004278 * privileges. The microcode reads CS:RIP and if it hits a SMAP
Liran Alon118154b2019-07-17 02:56:58 +03004279 * fault, it gives up and returns no instruction bytes.
4280 *
4281 * Detection:
4282 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4283 * returned 0 in GuestIntrBytes field of the VMCB.
4284 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4285 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4286 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4287 * a SMEP fault instead of #NPF).
4288 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4289 * As most guests enable SMAP if they have also enabled SMEP, use above
4290 * logic in order to attempt minimize false-positive of detecting errata
4291 * while still preserving all cases semantic correctness.
4292 *
4293 * Workaround:
4294 * To determine what instruction the guest was executing, the hypervisor
4295 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004296 *
4297 * In non SEV guest, hypervisor will be able to read the guest
4298 * memory to decode the instruction pointer when insn_len is zero
4299 * so we return true to indicate that decoding is possible.
4300 *
4301 * But in the SEV guest, the guest memory is encrypted with the
4302 * guest specific key and hypervisor will not be able to decode the
4303 * instruction pointer so we will not able to workaround it. Lets
4304 * print the error and request to kill the guest.
4305 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004306 if (likely(!insn || insn_len))
4307 return true;
4308
4309 /*
4310 * If RIP is invalid, go ahead with emulation which will cause an
4311 * internal error exit.
4312 */
4313 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4314 return true;
4315
4316 cr4 = kvm_read_cr4(vcpu);
4317 smep = cr4 & X86_CR4_SMEP;
4318 smap = cr4 & X86_CR4_SMAP;
4319 is_user = svm_get_cpl(vcpu) == 3;
Liran Alon118154b2019-07-17 02:56:58 +03004320 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004321 if (!sev_guest(vcpu->kvm))
4322 return true;
4323
Liran Alon118154b2019-07-17 02:56:58 +03004324 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004325 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4326 }
4327
4328 return false;
4329}
4330
Liran Alon4b9852f2019-08-26 13:24:49 +03004331static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4332{
4333 struct vcpu_svm *svm = to_svm(vcpu);
4334
4335 /*
4336 * TODO: Last condition latch INIT signals on vCPU when
4337 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
Paolo Bonzini33b22172020-04-17 10:24:18 -04004338 * To properly emulate the INIT intercept,
4339 * svm_check_nested_events() should call nested_svm_vmexit()
4340 * if an INIT signal is pending.
Liran Alon4b9852f2019-08-26 13:24:49 +03004341 */
4342 return !gif_set(svm) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05004343 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
Liran Alon4b9852f2019-08-26 13:24:49 +03004344}
4345
Tom Lendacky647daca2021-01-04 14:20:01 -06004346static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4347{
4348 if (!sev_es_guest(vcpu->kvm))
4349 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4350
4351 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4352}
4353
Joerg Roedeleaf78262020-03-24 10:41:54 +01004354static void svm_vm_destroy(struct kvm *kvm)
4355{
4356 avic_vm_destroy(kvm);
4357 sev_vm_destroy(kvm);
4358}
4359
4360static int svm_vm_init(struct kvm *kvm)
4361{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004362 if (!pause_filter_count || !pause_filter_thresh)
4363 kvm->arch.pause_in_guest = true;
4364
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02004365 if (enable_apicv) {
Joerg Roedeleaf78262020-03-24 10:41:54 +01004366 int ret = avic_vm_init(kvm);
4367 if (ret)
4368 return ret;
4369 }
4370
Joerg Roedeleaf78262020-03-24 10:41:54 +01004371 return 0;
4372}
4373
Sean Christopherson9c14ee22020-03-21 13:26:03 -07004374static struct kvm_x86_ops svm_x86_ops __initdata = {
Sean Christopherson9dadfc42021-10-18 11:39:28 -07004375 .name = "kvm_amd",
4376
Li RongQingdd58f3c2020-02-23 16:13:12 +08004377 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004378 .hardware_enable = svm_hardware_enable,
4379 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004380 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02004381 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004382
4383 .vcpu_create = svm_create_vcpu,
4384 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004385 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004386
Sean Christopherson562b6b02020-01-26 16:41:13 -08004387 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06004388 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004389 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004390
Avi Kivity04d2cc72007-09-10 18:10:54 +03004391 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004392 .vcpu_load = svm_vcpu_load,
4393 .vcpu_put = svm_vcpu_put,
Sean Christophersona3c19d52021-12-08 01:52:33 +00004394 .vcpu_blocking = avic_vcpu_blocking,
4395 .vcpu_unblocking = avic_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004396
Jason Baronb6a7cc32021-01-14 22:27:54 -05004397 .update_exception_bitmap = svm_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06004398 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004399 .get_msr = svm_get_msr,
4400 .set_msr = svm_set_msr,
4401 .get_segment_base = svm_get_segment_base,
4402 .get_segment = svm_get_segment,
4403 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004404 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004405 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004406 .set_cr0 = svm_set_cr0,
Michael Roth405329f2021-12-16 11:13:54 -06004407 .post_set_cr3 = svm_post_set_cr3,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004408 .is_valid_cr4 = svm_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004409 .set_cr4 = svm_set_cr4,
4410 .set_efer = svm_set_efer,
4411 .get_idt = svm_get_idt,
4412 .set_idt = svm_set_idt,
4413 .get_gdt = svm_get_gdt,
4414 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004415 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004416 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004417 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004418 .get_rflags = svm_get_rflags,
4419 .set_rflags = svm_set_rflags,
Marc Orrc5063552021-12-09 07:52:57 -08004420 .get_if_flag = svm_get_if_flag,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004421
Sean Christopherson77809382020-03-20 14:28:18 -07004422 .tlb_flush_all = svm_flush_tlb,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004423 .tlb_flush_current = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07004424 .tlb_flush_gva = svm_flush_tlb_gva,
Sean Christopherson72b38322020-03-20 14:28:13 -07004425 .tlb_flush_guest = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004426
Sean Christophersonfc4fad72021-12-28 23:24:36 +00004427 .vcpu_pre_run = svm_vcpu_pre_run,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004428 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004429 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004430 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08004431 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004432 .set_interrupt_shadow = svm_set_interrupt_shadow,
4433 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004434 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004435 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004436 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004437 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004438 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004439 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004440 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004441 .get_nmi_mask = svm_get_nmi_mask,
4442 .set_nmi_mask = svm_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004443 .enable_nmi_window = svm_enable_nmi_window,
4444 .enable_irq_window = svm_enable_irq_window,
4445 .update_cr8_intercept = svm_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04004446 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004447 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06004448 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004449 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004450 .hwapic_irr_update = svm_hwapic_irr_update,
4451 .hwapic_isr_update = svm_hwapic_isr_update,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004452 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02004453
4454 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004455 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004456 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004457
Avi Kivity586f9602010-11-18 13:09:54 +02004458 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004459
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004460 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004461
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004462 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004463
Ilias Stamatis307a94c2021-05-26 19:44:13 +01004464 .get_l2_tsc_offset = svm_get_l2_tsc_offset,
4465 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
Ilias Stamatisedcfe542021-05-26 19:44:15 +01004466 .write_tsc_offset = svm_write_tsc_offset,
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004467 .write_tsc_multiplier = svm_write_tsc_multiplier,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004468
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004469 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004470
4471 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07004472 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004473
Sean Christophersond264ee02018-08-27 15:21:12 -07004474 .request_immediate_exit = __kvm_request_immediate_exit,
4475
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004476 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004477
4478 .pmu_ops = &amd_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04004479 .nested_ops = &svm_nested_ops,
4480
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004481 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08004482 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004483 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02004484 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004485
Ladi Prosek72d7b372017-10-11 16:54:41 +02004486 .smi_allowed = svm_smi_allowed,
Sean Christophersonecc513e2021-06-09 11:56:19 -07004487 .enter_smm = svm_enter_smm,
4488 .leave_smm = svm_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004489 .enable_smi_window = svm_enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004490
4491 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004492 .mem_enc_reg_region = svm_register_enc_region,
4493 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004494
Nathan Tempelman54526d12021-04-08 22:32:14 +00004495 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
Peter Gondab5663932021-10-21 10:43:00 -07004496 .vm_move_enc_context_from = svm_vm_migrate_from,
Nathan Tempelman54526d12021-04-08 22:32:14 +00004497
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004498 .can_emulate_instruction = svm_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03004499
4500 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Alexander Graffd6fa732020-09-25 16:34:19 +02004501
4502 .msr_filter_changed = svm_msr_filter_changed,
Tom Lendackyf1c63662020-12-14 10:29:50 -05004503 .complete_emulated_msr = svm_complete_emulated_msr,
Tom Lendacky647daca2021-01-04 14:20:01 -06004504
4505 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004506};
4507
Sean Christopherson54744e12021-12-08 01:52:32 +00004508/*
4509 * The default MMIO mask is a single bit (excluding the present bit),
4510 * which could conflict with the memory encryption bit. Check for
4511 * memory encryption support and override the default MMIO mask if
4512 * memory encryption is enabled.
4513 */
4514static __init void svm_adjust_mmio_mask(void)
4515{
4516 unsigned int enc_bit, mask_bit;
4517 u64 msr, mask;
4518
4519 /* If there is no memory encryption support, use existing mask */
4520 if (cpuid_eax(0x80000000) < 0x8000001f)
4521 return;
4522
4523 /* If memory encryption is not enabled, use existing mask */
4524 rdmsrl(MSR_AMD64_SYSCFG, msr);
4525 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
4526 return;
4527
4528 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
4529 mask_bit = boot_cpu_data.x86_phys_bits;
4530
4531 /* Increment the mask bit if it is the same as the encryption bit */
4532 if (enc_bit == mask_bit)
4533 mask_bit++;
4534
4535 /*
4536 * If the mask bit location is below 52, then some bits above the
4537 * physical addressing limit will always be reserved, so use the
4538 * rsvd_bits() function to generate the mask. This mask, along with
4539 * the present bit, will be used to generate a page fault with
4540 * PFER.RSV = 1.
4541 *
4542 * If the mask bit location is 52 (or above), then clear the mask.
4543 */
4544 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
4545
4546 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
4547}
4548
4549static __init void svm_set_cpu_caps(void)
4550{
4551 kvm_set_cpu_caps();
4552
4553 supported_xss = 0;
4554
4555 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
4556 if (nested) {
4557 kvm_cpu_cap_set(X86_FEATURE_SVM);
4558
4559 if (nrips)
4560 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
4561
4562 if (npt_enabled)
4563 kvm_cpu_cap_set(X86_FEATURE_NPT);
4564
4565 if (tsc_scaling)
4566 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
4567
4568 /* Nested VM can receive #VMEXIT instead of triggering #GP */
4569 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
4570 }
4571
4572 /* CPUID 0x80000008 */
4573 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
4574 boot_cpu_has(X86_FEATURE_AMD_SSBD))
4575 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
4576
4577 /* AMD PMU PERFCTR_CORE CPUID */
4578 if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
4579 kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
4580
4581 /* CPUID 0x8000001F (SME/SEV features) */
4582 sev_set_cpu_caps();
4583}
4584
4585static __init int svm_hardware_setup(void)
4586{
4587 int cpu;
4588 struct page *iopm_pages;
4589 void *iopm_va;
4590 int r;
4591 unsigned int order = get_order(IOPM_SIZE);
4592
4593 /*
4594 * NX is required for shadow paging and for NPT if the NX huge pages
4595 * mitigation is enabled.
4596 */
4597 if (!boot_cpu_has(X86_FEATURE_NX)) {
4598 pr_err_ratelimited("NX (Execute Disable) not supported\n");
4599 return -EOPNOTSUPP;
4600 }
4601 kvm_enable_efer_bits(EFER_NX);
4602
4603 iopm_pages = alloc_pages(GFP_KERNEL, order);
4604
4605 if (!iopm_pages)
4606 return -ENOMEM;
4607
4608 iopm_va = page_address(iopm_pages);
4609 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
4610 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
4611
4612 init_msrpm_offsets();
4613
4614 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
4615
4616 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
4617 kvm_enable_efer_bits(EFER_FFXSR);
4618
4619 if (tsc_scaling) {
4620 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
4621 tsc_scaling = false;
4622 } else {
4623 pr_info("TSC scaling supported\n");
4624 kvm_has_tsc_control = true;
4625 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
4626 kvm_tsc_scaling_ratio_frac_bits = 32;
4627 }
4628 }
4629
4630 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
4631
4632 /* Check for pause filtering support */
4633 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
4634 pause_filter_count = 0;
4635 pause_filter_thresh = 0;
4636 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
4637 pause_filter_thresh = 0;
4638 }
4639
4640 if (nested) {
4641 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
4642 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
4643 }
4644
4645 /*
4646 * KVM's MMU doesn't support using 2-level paging for itself, and thus
4647 * NPT isn't supported if the host is using 2-level paging since host
4648 * CR4 is unchanged on VMRUN.
4649 */
4650 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
4651 npt_enabled = false;
4652
4653 if (!boot_cpu_has(X86_FEATURE_NPT))
4654 npt_enabled = false;
4655
4656 /* Force VM NPT level equal to the host's paging level */
4657 kvm_configure_mmu(npt_enabled, get_npt_level(),
4658 get_npt_level(), PG_LEVEL_1G);
4659 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
4660
4661 /* Note, SEV setup consumes npt_enabled. */
4662 sev_hardware_setup();
4663
4664 svm_hv_hardware_setup();
4665
4666 svm_adjust_mmio_mask();
4667
4668 for_each_possible_cpu(cpu) {
4669 r = svm_cpu_init(cpu);
4670 if (r)
4671 goto err;
4672 }
4673
4674 if (nrips) {
4675 if (!boot_cpu_has(X86_FEATURE_NRIPS))
4676 nrips = false;
4677 }
4678
4679 enable_apicv = avic = avic && npt_enabled && boot_cpu_has(X86_FEATURE_AVIC);
4680
4681 if (enable_apicv) {
4682 pr_info("AVIC enabled\n");
4683
4684 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
Sean Christophersona3c19d52021-12-08 01:52:33 +00004685 } else {
4686 svm_x86_ops.vcpu_blocking = NULL;
4687 svm_x86_ops.vcpu_unblocking = NULL;
Sean Christopherson54744e12021-12-08 01:52:32 +00004688 }
4689
4690 if (vls) {
4691 if (!npt_enabled ||
4692 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
4693 !IS_ENABLED(CONFIG_X86_64)) {
4694 vls = false;
4695 } else {
4696 pr_info("Virtual VMLOAD VMSAVE supported\n");
4697 }
4698 }
4699
4700 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
4701 svm_gp_erratum_intercept = false;
4702
4703 if (vgif) {
4704 if (!boot_cpu_has(X86_FEATURE_VGIF))
4705 vgif = false;
4706 else
4707 pr_info("Virtual GIF supported\n");
4708 }
4709
4710 if (lbrv) {
4711 if (!boot_cpu_has(X86_FEATURE_LBRV))
4712 lbrv = false;
4713 else
4714 pr_info("LBR virtualization supported\n");
4715 }
4716
4717 if (!enable_pmu)
4718 pr_info("PMU virtualization is disabled\n");
4719
4720 svm_set_cpu_caps();
4721
4722 /*
4723 * It seems that on AMD processors PTE's accessed bit is
4724 * being set by the CPU hardware before the NPF vmexit.
4725 * This is not expected behaviour and our tests fail because
4726 * of it.
4727 * A workaround here is to disable support for
4728 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
4729 * In this case userspace can know if there is support using
4730 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
4731 * it
4732 * If future AMD CPU models change the behaviour described above,
4733 * this variable can be changed accordingly
4734 */
4735 allow_smaller_maxphyaddr = !npt_enabled;
4736
4737 return 0;
4738
4739err:
4740 svm_hardware_teardown();
4741 return r;
4742}
4743
4744
Sean Christophersond008dfd2020-03-21 13:25:56 -07004745static struct kvm_x86_init_ops svm_init_ops __initdata = {
4746 .cpu_has_kvm_support = has_svm,
4747 .disabled_by_bios = is_disabled,
4748 .hardware_setup = svm_hardware_setup,
4749 .check_processor_compatibility = svm_check_processor_compat,
4750
4751 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004752};
4753
4754static int __init svm_init(void)
4755{
Tom Lendackyd07f46f2020-09-07 15:15:03 +02004756 __unused_size_checks();
4757
Sean Christophersond008dfd2020-03-21 13:25:56 -07004758 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004759 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004760}
4761
4762static void __exit svm_exit(void)
4763{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004764 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004765}
4766
4767module_init(svm_init)
4768module_exit(svm_exit)