blob: 89077160d463f205b6bf5579411b052f925ce6ab [file] [log] [blame]
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001#define pr_fmt(fmt) "SVM: " fmt
2
Avi Kivityedf88412007-12-16 11:02:48 +02003#include <linux/kvm_host.h>
4
Eddie Dong85f455f2007-07-06 12:20:49 +03005#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +08006#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02008#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +01009#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020010#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040011
Avi Kivity6aa8b732006-12-10 02:21:36 -080012#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070013#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020014#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080015#include <linux/vmalloc.h>
16#include <linux/highmem.h>
Joerg Roedelef0f6492020-03-31 12:17:38 -040017#include <linux/amd-iommu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040019#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050021#include <linux/hashtable.h>
Julien Thierry00089c02020-09-04 16:30:25 +010022#include <linux/objtool.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060023#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060024#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060025#include <linux/pagemap.h>
26#include <linux/swap.h>
Tom Lendacky33af3a72019-10-03 21:17:48 +000027#include <linux/rwsem.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050029#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010030#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020031#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010033#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050035#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020036#include <asm/spec-ctrl.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010037#include <asm/cpu_device_id.h>
Tom Lendackyf1c63662020-12-14 10:29:50 -050038#include <asm/traps.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039
Eduardo Habkost63d11422008-11-17 19:03:20 -020040#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020042
Joerg Roedel883b0a92020-03-24 10:41:52 +010043#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080044#include "svm_ops.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010045
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +000046#include "kvm_onhyperv.h"
47#include "svm_onhyperv.h"
48
Avi Kivity6aa8b732006-12-10 02:21:36 -080049MODULE_AUTHOR("Qumranet");
50MODULE_LICENSE("GPL");
51
Valdis Klētnieks575b2552020-02-27 21:49:52 -050052#ifdef MODULE
Josh Triplettae759542012-03-28 11:32:28 -070053static const struct x86_cpu_id svm_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010054 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
Josh Triplettae759542012-03-28 11:32:28 -070055 {}
56};
57MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050058#endif
Josh Triplettae759542012-03-28 11:32:28 -070059
Avi Kivity6aa8b732006-12-10 02:21:36 -080060#define SEG_TYPE_LDT 2
61#define SEG_TYPE_BUSY_TSS16 3
62
Andre Przywara6bc31bd2010-04-11 23:07:28 +020063#define SVM_FEATURE_LBRV (1 << 1)
64#define SVM_FEATURE_SVML (1 << 2)
Andre Przywaraddce97a2010-12-21 11:12:03 +010065#define SVM_FEATURE_TSC_RATE (1 << 4)
66#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
67#define SVM_FEATURE_FLUSH_ASID (1 << 6)
68#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020069#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030070
Joerg Roedel24e09cb2008-02-13 18:58:47 +010071#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
72
Joerg Roedelfbc0db72011-03-25 09:44:46 +010073#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010074#define TSC_RATIO_MIN 0x0000000000000001ULL
75#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010076
Joerg Roedel67ec6602010-05-17 14:43:35 +020077static bool erratum_383_found __read_mostly;
78
Joerg Roedel883b0a92020-03-24 10:41:52 +010079u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
Joerg Roedel323c3d82010-03-01 15:34:37 +010080
Boris Ostrovsky2b036c62012-01-09 14:00:35 -050081/*
82 * Set osvw_len to higher value when updated Revision Guides
83 * are published and we know what the new status bits are
84 */
85static uint64_t osvw_len = 4, osvw_status;
86
Joerg Roedelfbc0db72011-03-25 09:44:46 +010087static DEFINE_PER_CPU(u64, current_tsc_ratio);
88#define TSC_RATIO_DEFAULT 0x0100000000ULL
89
Mathias Krause09941fb2012-08-30 01:30:20 +020090static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +010091 u32 index; /* Index of the MSR */
Tom Lendacky376c6d22020-12-10 11:10:06 -060092 bool always; /* True if intercept is initially cleared */
Alexander Graffd6fa732020-09-25 16:34:19 +020093} direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
Brian Gerst8c065852010-07-17 09:03:26 -040094 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010095 { .index = MSR_IA32_SYSENTER_CS, .always = true },
Maxim Levitskyadc2a232021-04-01 14:19:28 +030096 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
97 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010098#ifdef CONFIG_X86_64
99 { .index = MSR_GS_BASE, .always = true },
100 { .index = MSR_FS_BASE, .always = true },
101 { .index = MSR_KERNEL_GS_BASE, .always = true },
102 { .index = MSR_LSTAR, .always = true },
103 { .index = MSR_CSTAR, .always = true },
104 { .index = MSR_SYSCALL_MASK, .always = true },
105#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100106 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100107 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100108 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
109 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
110 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
111 { .index = MSR_IA32_LASTINTTOIP, .always = false },
Tom Lendacky376c6d22020-12-10 11:10:06 -0600112 { .index = MSR_EFER, .always = false },
113 { .index = MSR_IA32_CR_PAT, .always = false },
114 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100115 { .index = MSR_INVALID, .always = false },
Avi Kivity6c8166a2009-05-31 18:15:37 +0300116};
117
Babu Moger8566ac82018-03-16 16:37:26 -0400118/*
119 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
120 * pause_filter_count: On processors that support Pause filtering(indicated
121 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
122 * count value. On VMRUN this value is loaded into an internal counter.
123 * Each time a pause instruction is executed, this counter is decremented
124 * until it reaches zero at which time a #VMEXIT is generated if pause
125 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
126 * Intercept Filtering for more details.
127 * This also indicate if ple logic enabled.
128 *
129 * pause_filter_thresh: In addition, some processor families support advanced
130 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
131 * the amount of time a guest is allowed to execute in a pause loop.
132 * In this mode, a 16-bit pause filter threshold field is added in the
133 * VMCB. The threshold value is a cycle count that is used to reset the
134 * pause counter. As with simple pause filtering, VMRUN loads the pause
135 * count value from VMCB into an internal counter. Then, on each pause
136 * instruction the hardware checks the elapsed number of cycles since
137 * the most recent pause instruction against the pause filter threshold.
138 * If the elapsed cycle count is greater than the pause filter threshold,
139 * then the internal pause count is reloaded from the VMCB and execution
140 * continues. If the elapsed cycle count is less than the pause filter
141 * threshold, then the internal pause count is decremented. If the count
142 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
143 * triggered. If advanced pause filtering is supported and pause filter
144 * threshold field is set to zero, the filter will operate in the simpler,
145 * count only mode.
146 */
147
148static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
149module_param(pause_filter_thresh, ushort, 0444);
150
151static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
152module_param(pause_filter_count, ushort, 0444);
153
154/* Default doubles per-vcpu window every exit. */
155static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
156module_param(pause_filter_count_grow, ushort, 0444);
157
158/* Default resets per-vcpu window every exit to pause_filter_count. */
159static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
160module_param(pause_filter_count_shrink, ushort, 0444);
161
162/* Default is to compute the maximum so we can never overflow. */
163static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
164module_param(pause_filter_count_max, ushort, 0444);
165
Sean Christopherson99840a72021-03-04 18:16:37 -0800166/*
167 * Use nested page tables by default. Note, NPT may get forced off by
168 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
169 */
170bool npt_enabled = true;
171module_param_named(npt, npt_enabled, bool, 0444);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100172
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100173/* allow nested virtualization in KVM/SVM */
174static int nested = true;
Alexander Graf236de052008-11-25 20:17:10 +0100175module_param(nested, int, S_IRUGO);
176
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200177/* enable/disable Next RIP Save */
178static int nrips = true;
179module_param(nrips, int, 0444);
180
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500181/* enable/disable Virtual VMLOAD VMSAVE */
182static int vls = true;
183module_param(vls, int, 0444);
184
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500185/* enable/disable Virtual GIF */
186static int vgif = true;
187module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500188
Maxim Levitsky4c849262021-09-14 18:48:19 +0300189/* enable/disable LBR virtualization */
190static int lbrv = true;
191module_param(lbrv, int, 0444);
192
Maxim Levitskyf8006502021-09-14 18:48:23 +0300193static int tsc_scaling = true;
194module_param(tsc_scaling, int, 0444);
195
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +0200196/*
197 * enable / disable AVIC. Because the defaults differ for APICv
198 * support between VMX and SVM we cannot use module_param_named.
199 */
200static bool avic;
201module_param(avic, bool, 0444);
202
Tom Lendacky291bd202020-12-10 11:09:47 -0600203bool __read_mostly dump_invalid_vmcb;
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200204module_param(dump_invalid_vmcb, bool, 0644);
205
Maxim Levitsky4b639a92021-07-07 15:51:00 +0300206
207bool intercept_smi = true;
208module_param(intercept_smi, bool, 0444);
209
210
Wei Yongjun2e215212021-02-10 07:59:58 +0000211static bool svm_gp_erratum_intercept = true;
Bandan Das82a11e9c2021-01-26 03:18:29 -0500212
Brijesh Singh7607b712018-02-19 10:14:44 -0600213static u8 rsm_ins_bytes[] = "\x0f\xaa";
214
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800215static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800216
217struct kvm_ldttss_desc {
218 u16 limit0;
219 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100220 unsigned base1:8, type:5, dpl:2, p:1;
221 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800222 u32 base3;
223 u32 zero1;
224} __attribute__((packed));
225
Joerg Roedeleaf78262020-03-24 10:41:54 +0100226DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227
Sean Christopherson844d69c2021-04-23 15:34:04 -0700228/*
229 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
230 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
231 *
232 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
233 * defer the restoration of TSC_AUX until the CPU returns to userspace.
234 */
Sean Christopherson0caa0a72021-05-04 10:17:25 -0700235static int tsc_aux_uret_slot __read_mostly = -1;
Sean Christopherson844d69c2021-04-23 15:34:04 -0700236
Mathias Krause09941fb2012-08-30 01:30:20 +0200237static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800238
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200239#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800240#define MSRS_RANGE_SIZE 2048
241#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
242
Joerg Roedel883b0a92020-03-24 10:41:52 +0100243u32 svm_msrpm_offset(u32 msr)
Joerg Roedel455716f2010-03-01 15:34:35 +0100244{
245 u32 offset;
246 int i;
247
248 for (i = 0; i < NUM_MSR_MAPS; i++) {
249 if (msr < msrpm_ranges[i] ||
250 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
251 continue;
252
253 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
254 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
255
256 /* Now we have the u8 offset - but need the u32 offset */
257 return offset / 4;
258 }
259
260 /* MSR not in any range */
261 return MSR_INVALID;
262}
263
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264#define MAX_INST_SIZE 15
265
Sean Christophersond468d942020-07-15 20:41:20 -0700266static int get_max_npt_level(void)
Joerg Roedel4b161842010-09-10 17:31:03 +0200267{
268#ifdef CONFIG_X86_64
Wei Huang43e540c2021-08-18 11:55:49 -0500269 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200270#else
271 return PT32E_ROOT_LEVEL;
272#endif
273}
274
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300275int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276{
Paolo Bonzinic513f482020-05-18 13:08:37 -0400277 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300278 u64 old_efer = vcpu->arch.efer;
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000279 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100280
281 if (!npt_enabled) {
282 /* Shadow paging assumes NX to be available. */
283 efer |= EFER_NX;
284
285 if (!(efer & EFER_LMA))
286 efer &= ~EFER_LME;
287 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800288
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300289 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
290 if (!(efer & EFER_SVME)) {
291 svm_leave_nested(svm);
292 svm_set_gif(svm, true);
Bandan Das82a11e9c2021-01-26 03:18:29 -0500293 /* #GP intercept is still needed for vmware backdoor */
294 if (!enable_vmware_backdoor)
295 clr_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300296
297 /*
298 * Free the nested guest state, unless we are in SMM.
299 * In this case we will return to the nested guest
300 * as soon as we leave SMM.
301 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500302 if (!is_smm(vcpu))
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300303 svm_free_nested(svm);
304
305 } else {
306 int ret = svm_allocate_nested(svm);
307
308 if (ret) {
309 vcpu->arch.efer = old_efer;
310 return ret;
311 }
Bandan Das82a11e9c2021-01-26 03:18:29 -0500312
313 if (svm_gp_erratum_intercept)
314 set_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300315 }
Paolo Bonzinic513f482020-05-18 13:08:37 -0400316 }
317
318 svm->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedel06e78522020-06-25 10:03:23 +0200319 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300320 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321}
322
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323static int is_external_interrupt(u32 info)
324{
325 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
326 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
327}
328
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200329static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400330{
331 struct vcpu_svm *svm = to_svm(vcpu);
332 u32 ret = 0;
333
334 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200335 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
336 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400337}
338
339static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
340{
341 struct vcpu_svm *svm = to_svm(vcpu);
342
343 if (mask == 0)
344 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
345 else
346 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
347
348}
349
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200350static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800351{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400352 struct vcpu_svm *svm = to_svm(vcpu);
353
Tom Lendackyf1c63662020-12-14 10:29:50 -0500354 /*
355 * SEV-ES does not expose the next RIP. The RIP update is controlled by
356 * the type of exit and the #VC handler in the guest.
357 */
358 if (sev_es_guest(vcpu->kvm))
359 goto done;
360
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200361 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200362 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200363 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400364 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200365
Sean Christopherson1957aa62019-08-27 14:40:39 -0700366 if (!svm->next_rip) {
367 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
368 return 0;
369 } else {
Sean Christopherson1957aa62019-08-27 14:40:39 -0700370 kvm_rip_write(vcpu, svm->next_rip);
371 }
Tom Lendackyf1c63662020-12-14 10:29:50 -0500372
373done:
Glauber Costa2809f5d2009-05-12 16:21:05 -0400374 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200375
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700376 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800377}
378
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700379static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100380{
381 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700382 unsigned nr = vcpu->arch.exception.nr;
383 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700384 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100385
Paolo Bonzini63129752021-03-02 14:40:39 -0500386 kvm_deliver_exception_payload(vcpu);
Jim Mattsonda998b42018-10-16 14:29:22 -0700387
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200388 if (nr == BP_VECTOR && !nrips) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500389 unsigned long rip, old_rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100390
391 /*
392 * For guest debugging where we have to reinject #BP if some
393 * INT3 is guest-owned:
394 * Emulate nRIP by moving RIP forward. Will fail if injection
395 * raises a fault that is not intercepted. Still better than
396 * failing in all cases.
397 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500398 (void)skip_emulated_instruction(vcpu);
399 rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100400 svm->int3_rip = rip + svm->vmcb->save.cs.base;
401 svm->int3_injected = rip - old_rip;
402 }
403
Jan Kiszka116a4752010-02-23 17:47:54 +0100404 svm->vmcb->control.event_inj = nr
405 | SVM_EVTINJ_VALID
406 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
407 | SVM_EVTINJ_TYPE_EXEPT;
408 svm->vmcb->control.event_inj_err = error_code;
409}
410
Joerg Roedel67ec6602010-05-17 14:43:35 +0200411static void svm_init_erratum_383(void)
412{
413 u32 low, high;
414 int err;
415 u64 val;
416
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100417 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200418 return;
419
420 /* Use _safe variants to not break nested virtualization */
421 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
422 if (err)
423 return;
424
425 val |= (1ULL << 47);
426
427 low = lower_32_bits(val);
428 high = upper_32_bits(val);
429
430 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
431
432 erratum_383_found = true;
433}
434
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500435static void svm_init_osvw(struct kvm_vcpu *vcpu)
436{
437 /*
438 * Guests should see errata 400 and 415 as fixed (assuming that
439 * HLT and IO instructions are intercepted).
440 */
441 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
442 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
443
444 /*
445 * By increasing VCPU's osvw.length to 3 we are telling the guest that
446 * all osvw.status bits inside that length, including bit 0 (which is
447 * reserved for erratum 298), are valid. However, if host processor's
448 * osvw_len is 0 then osvw_status[0] carries no information. We need to
449 * be conservative here and therefore we tell the guest that erratum 298
450 * is present (because we really don't know).
451 */
452 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
453 vcpu->arch.osvw.status |= 1;
454}
455
Avi Kivity6aa8b732006-12-10 02:21:36 -0800456static int has_svm(void)
457{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200458 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800459
Eduardo Habkost63d11422008-11-17 19:03:20 -0200460 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800461 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 return 0;
463 }
464
Sean Christophersonccd85d92021-02-02 13:20:17 -0800465 if (sev_active()) {
466 pr_info("KVM is unsupported when running as an SEV guest\n");
467 return 0;
468 }
469
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470 return 1;
471}
472
Radim Krčmář13a34e02014-08-28 15:13:03 +0200473static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100475 /* Make sure we clean up behind us */
Maxim Levitskyf8006502021-09-14 18:48:23 +0300476 if (tsc_scaling)
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100477 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
478
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200479 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100480
481 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800482}
483
Radim Krčmář13a34e02014-08-28 15:13:03 +0200484static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800485{
486
Tejun Heo0fe1e002009-10-29 22:34:14 +0900487 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800488 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 struct desc_struct *gdt;
490 int me = raw_smp_processor_id();
491
Alexander Graf10474ae2009-09-15 11:37:46 +0200492 rdmsrl(MSR_EFER, efer);
493 if (efer & EFER_SVME)
494 return -EBUSY;
495
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200497 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200498 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800499 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900500 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900501 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200502 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200503 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800504 }
505
Tejun Heo0fe1e002009-10-29 22:34:14 +0900506 sd->asid_generation = 1;
507 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
508 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600509 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800510
Thomas Garnier45fc8752017-03-14 10:05:08 -0700511 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900512 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513
Alexander Graf9962d032008-11-25 20:17:02 +0100514 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800515
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600516 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
Alexander Graf10474ae2009-09-15 11:37:46 +0200517
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100518 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Maxim Levitskyf8006502021-09-14 18:48:23 +0300519 /*
520 * Set the default value, even if we don't use TSC scaling
521 * to avoid having stale value in the msr
522 */
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100523 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500524 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100525 }
526
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500527
528 /*
529 * Get OSVW bits.
530 *
531 * Note that it is possible to have a system with mixed processor
532 * revisions and therefore different OSVW bits. If bits are not the same
533 * on different processors then choose the worst case (i.e. if erratum
534 * is present on one processor and not on another then assume that the
535 * erratum is present everywhere).
536 */
537 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
538 uint64_t len, status = 0;
539 int err;
540
541 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
542 if (!err)
543 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
544 &err);
545
546 if (err)
547 osvw_status = osvw_len = 0;
548 else {
549 if (len < osvw_len)
550 osvw_len = len;
551 osvw_status |= status;
552 osvw_status &= (1ULL << osvw_len) - 1;
553 }
554 } else
555 osvw_status = osvw_len = 0;
556
Joerg Roedel67ec6602010-05-17 14:43:35 +0200557 svm_init_erratum_383();
558
Joerg Roedel1018faa2012-02-29 14:57:32 +0100559 amd_pmu_enable_virt();
560
Alexander Graf10474ae2009-09-15 11:37:46 +0200561 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800562}
563
Joerg Roedel0da1db752008-07-02 16:02:11 +0200564static void svm_cpu_uninit(int cpu)
565{
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800566 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200567
Tejun Heo0fe1e002009-10-29 22:34:14 +0900568 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200569 return;
570
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800571 per_cpu(svm_data, cpu) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600572 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900573 __free_page(sd->save_area);
574 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200575}
576
Avi Kivity6aa8b732006-12-10 02:21:36 -0800577static int svm_cpu_init(int cpu)
578{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900579 struct svm_cpu_data *sd;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700580 int ret = -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581
Tejun Heo0fe1e002009-10-29 22:34:14 +0900582 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
583 if (!sd)
Sean Christophersonb95c2212021-04-21 19:11:22 -0700584 return ret;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900585 sd->cpu = cpu;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600586 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900587 if (!sd->save_area)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800588 goto free_cpu_data;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700589
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600590 clear_page(page_address(sd->save_area));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
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
Tom Lendacky52918ed2020-01-09 17:42:16 -0600872/*
873 * The default MMIO mask is a single bit (excluding the present bit),
874 * which could conflict with the memory encryption bit. Check for
875 * memory encryption support and override the default MMIO mask if
876 * memory encryption is enabled.
877 */
878static __init void svm_adjust_mmio_mask(void)
879{
880 unsigned int enc_bit, mask_bit;
881 u64 msr, mask;
882
883 /* If there is no memory encryption support, use existing mask */
884 if (cpuid_eax(0x80000000) < 0x8000001f)
885 return;
886
887 /* If memory encryption is not enabled, use existing mask */
Brijesh Singh059e5c32021-04-27 06:16:36 -0500888 rdmsrl(MSR_AMD64_SYSCFG, msr);
889 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
Tom Lendacky52918ed2020-01-09 17:42:16 -0600890 return;
891
892 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
893 mask_bit = boot_cpu_data.x86_phys_bits;
894
895 /* Increment the mask bit if it is the same as the encryption bit */
896 if (enc_bit == mask_bit)
897 mask_bit++;
898
899 /*
900 * If the mask bit location is below 52, then some bits above the
901 * physical addressing limit will always be reserved, so use the
902 * rsvd_bits() function to generate the mask. This mask, along with
903 * the present bit, will be used to generate a page fault with
904 * PFER.RSV = 1.
905 *
906 * If the mask bit location is 52 (or above), then clear the mask.
907 */
908 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
909
Sean Christopherson81203372021-02-25 12:47:35 -0800910 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
Tom Lendacky52918ed2020-01-09 17:42:16 -0600911}
912
Li RongQingdd58f3c2020-02-23 16:13:12 +0800913static void svm_hardware_teardown(void)
914{
915 int cpu;
916
Sean Christopherson4cafd0c2021-04-21 19:11:20 -0700917 sev_hardware_teardown();
Li RongQingdd58f3c2020-02-23 16:13:12 +0800918
919 for_each_possible_cpu(cpu)
920 svm_cpu_uninit(cpu);
921
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400922 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
923 get_order(IOPM_SIZE));
Li RongQingdd58f3c2020-02-23 16:13:12 +0800924 iopm_base = 0;
925}
926
Sean Christopherson9b58b982020-03-02 15:56:42 -0800927static __init void svm_set_cpu_caps(void)
928{
929 kvm_set_cpu_caps();
930
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100931 supported_xss = 0;
932
Sean Christophersona50718c2020-03-02 15:57:07 -0800933 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
934 if (nested) {
Sean Christopherson9b58b982020-03-02 15:56:42 -0800935 kvm_cpu_cap_set(X86_FEATURE_SVM);
936
Sean Christopherson4eb87462020-03-02 15:57:08 -0800937 if (nrips)
Sean Christophersona50718c2020-03-02 15:57:07 -0800938 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
939
940 if (npt_enabled)
941 kvm_cpu_cap_set(X86_FEATURE_NPT);
Wei Huang14c2bf82021-01-26 03:18:31 -0500942
Maxim Levitsky5228eb92021-09-14 18:48:24 +0300943 if (tsc_scaling)
944 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
945
Wei Huang14c2bf82021-01-26 03:18:31 -0500946 /* Nested VM can receive #VMEXIT instead of triggering #GP */
947 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
Sean Christophersona50718c2020-03-02 15:57:07 -0800948 }
949
Sean Christopherson93c380e2020-03-02 15:56:54 -0800950 /* CPUID 0x80000008 */
951 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
952 boot_cpu_has(X86_FEATURE_AMD_SSBD))
953 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700954
955 /* CPUID 0x8000001F (SME/SEV features) */
956 sev_set_cpu_caps();
Sean Christopherson9b58b982020-03-02 15:56:42 -0800957}
958
Avi Kivity6aa8b732006-12-10 02:21:36 -0800959static __init int svm_hardware_setup(void)
960{
961 int cpu;
962 struct page *iopm_pages;
963 void *iopm_va;
964 int r;
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400965 unsigned int order = get_order(IOPM_SIZE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800966
Sean Christophersonb26a71a2021-06-15 09:45:33 -0700967 /*
968 * NX is required for shadow paging and for NPT if the NX huge pages
969 * mitigation is enabled.
970 */
971 if (!boot_cpu_has(X86_FEATURE_NX)) {
972 pr_err_ratelimited("NX (Execute Disable) not supported\n");
973 return -EOPNOTSUPP;
974 }
975 kvm_enable_efer_bits(EFER_NX);
976
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400977 iopm_pages = alloc_pages(GFP_KERNEL, order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978
979 if (!iopm_pages)
980 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300981
982 iopm_va = page_address(iopm_pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400983 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
985
Joerg Roedel323c3d82010-03-01 15:34:37 +0100986 init_msrpm_offsets();
987
Sean Christophersoncfc48182020-03-02 15:56:23 -0800988 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
989
Alexander Graf1b2fd702009-02-02 16:23:51 +0100990 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
991 kvm_enable_efer_bits(EFER_FFXSR);
992
Maxim Levitskyf8006502021-09-14 18:48:23 +0300993 if (tsc_scaling) {
994 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
995 tsc_scaling = false;
996 } else {
997 pr_info("TSC scaling supported\n");
998 kvm_has_tsc_control = true;
999 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
1000 kvm_tsc_scaling_ratio_frac_bits = 32;
1001 }
Joerg Roedel92a1f122011-03-25 09:44:51 +01001002 }
1003
Sean Christophersone5fda4b2021-05-04 10:17:32 -07001004 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
Sean Christopherson844d69c2021-04-23 15:34:04 -07001005
Babu Moger8566ac82018-03-16 16:37:26 -04001006 /* Check for pause filtering support */
1007 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
1008 pause_filter_count = 0;
1009 pause_filter_thresh = 0;
1010 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
1011 pause_filter_thresh = 0;
1012 }
1013
Alexander Graf236de052008-11-25 20:17:10 +01001014 if (nested) {
1015 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +02001016 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +01001017 }
1018
Sean Christopherson99840a72021-03-04 18:16:37 -08001019 /*
1020 * KVM's MMU doesn't support using 2-level paging for itself, and thus
1021 * NPT isn't supported if the host is using 2-level paging since host
1022 * CR4 is unchanged on VMRUN.
1023 */
1024 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001025 npt_enabled = false;
1026
Sean Christopherson99840a72021-03-04 18:16:37 -08001027 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001028 npt_enabled = false;
Joerg Roedel6c7dac72008-02-07 13:47:40 +01001029
Wei Huang746700d2021-08-18 11:55:47 -05001030 /* Force VM NPT level equal to the host's max NPT level */
1031 kvm_configure_mmu(npt_enabled, get_max_npt_level(),
1032 get_max_npt_level(), PG_LEVEL_1G);
Sean Christopherson213e0e12020-03-02 15:57:01 -08001033 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001034
Sean Christophersone8126bd2021-04-21 19:11:14 -07001035 /* Note, SEV setup consumes npt_enabled. */
1036 sev_hardware_setup();
Sean Christophersonfa136802021-04-21 19:11:13 -07001037
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001038 svm_hv_hardware_setup();
1039
Sean Christophersonfa136802021-04-21 19:11:13 -07001040 svm_adjust_mmio_mask();
1041
1042 for_each_possible_cpu(cpu) {
1043 r = svm_cpu_init(cpu);
1044 if (r)
1045 goto err;
1046 }
1047
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001048 if (nrips) {
1049 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1050 nrips = false;
1051 }
1052
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02001053 enable_apicv = avic = avic && npt_enabled && boot_cpu_has(X86_FEATURE_AVIC);
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001054
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02001055 if (enable_apicv) {
1056 pr_info("AVIC enabled\n");
1057
1058 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001059 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001060
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001061 if (vls) {
1062 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001063 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001064 !IS_ENABLED(CONFIG_X86_64)) {
1065 vls = false;
1066 } else {
1067 pr_info("Virtual VMLOAD VMSAVE supported\n");
1068 }
1069 }
1070
Wei Huang3b9c7232021-01-26 03:18:30 -05001071 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
1072 svm_gp_erratum_intercept = false;
1073
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001074 if (vgif) {
1075 if (!boot_cpu_has(X86_FEATURE_VGIF))
1076 vgif = false;
1077 else
1078 pr_info("Virtual GIF supported\n");
1079 }
1080
Maxim Levitsky4c849262021-09-14 18:48:19 +03001081 if (lbrv) {
1082 if (!boot_cpu_has(X86_FEATURE_LBRV))
1083 lbrv = false;
1084 else
1085 pr_info("LBR virtualization supported\n");
1086 }
1087
Sean Christopherson9b58b982020-03-02 15:56:42 -08001088 svm_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08001089
Mohammed Gamal3edd6832020-07-10 17:48:11 +02001090 /*
1091 * It seems that on AMD processors PTE's accessed bit is
1092 * being set by the CPU hardware before the NPF vmexit.
1093 * This is not expected behaviour and our tests fail because
1094 * of it.
1095 * A workaround here is to disable support for
1096 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1097 * In this case userspace can know if there is support using
1098 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1099 * it
1100 * If future AMD CPU models change the behaviour described above,
1101 * this variable can be changed accordingly
1102 */
1103 allow_smaller_maxphyaddr = !npt_enabled;
1104
Avi Kivity6aa8b732006-12-10 02:21:36 -08001105 return 0;
1106
Joerg Roedelf65c2292008-02-13 18:58:46 +01001107err:
Li RongQingdd58f3c2020-02-23 16:13:12 +08001108 svm_hardware_teardown();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109 return r;
1110}
1111
Avi Kivity6aa8b732006-12-10 02:21:36 -08001112static void init_seg(struct vmcb_seg *seg)
1113{
1114 seg->selector = 0;
1115 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001116 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117 seg->limit = 0xffff;
1118 seg->base = 0;
1119}
1120
1121static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1122{
1123 seg->selector = 0;
1124 seg->attrib = SVM_SELECTOR_P_MASK | type;
1125 seg->limit = 0xffff;
1126 seg->base = 0;
1127}
1128
Ilias Stamatis307a94c2021-05-26 19:44:13 +01001129static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
1130{
1131 struct vcpu_svm *svm = to_svm(vcpu);
1132
1133 return svm->nested.ctl.tsc_offset;
1134}
1135
1136static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
1137{
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001138 struct vcpu_svm *svm = to_svm(vcpu);
1139
1140 return svm->tsc_ratio_msr;
Ilias Stamatis307a94c2021-05-26 19:44:13 +01001141}
1142
Ilias Stamatisedcfe542021-05-26 19:44:15 +01001143static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001144{
1145 struct vcpu_svm *svm = to_svm(vcpu);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001146
Ilias Stamatisedcfe542021-05-26 19:44:15 +01001147 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
1148 svm->vmcb->control.tsc_offset = offset;
Joerg Roedel06e78522020-06-25 10:03:23 +02001149 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001150}
1151
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001152void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
Ilias Stamatis1ab92872021-06-07 11:54:38 +01001153{
1154 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
1155}
1156
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001157/* Evaluate instruction intercepts that depend on guest CPUID features. */
1158static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
1159 struct vcpu_svm *svm)
Babu Moger4407a792020-09-11 14:29:19 -05001160{
1161 /*
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001162 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1163 * roots, or if INVPCID is disabled in the guest to inject #UD.
Babu Moger4407a792020-09-11 14:29:19 -05001164 */
1165 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001166 if (!npt_enabled ||
1167 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
Babu Moger4407a792020-09-11 14:29:19 -05001168 svm_set_intercept(svm, INTERCEPT_INVPCID);
1169 else
1170 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1171 }
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001172
1173 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
1174 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
1175 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
1176 else
1177 svm_set_intercept(svm, INTERCEPT_RDTSCP);
1178 }
Babu Moger4407a792020-09-11 14:29:19 -05001179}
1180
Paolo Bonzini36e81942021-09-23 12:46:07 -04001181static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
1182{
1183 struct vcpu_svm *svm = to_svm(vcpu);
1184
1185 if (guest_cpuid_is_intel(vcpu)) {
1186 /*
1187 * We must intercept SYSENTER_EIP and SYSENTER_ESP
1188 * accesses because the processor only stores 32 bits.
1189 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
1190 */
1191 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1192 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1193 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1194
1195 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
1196 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
1197 } else {
1198 /*
1199 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1200 * in VMCB and clear intercepts to avoid #VMEXIT.
1201 */
1202 if (vls) {
1203 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
1204 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
1205 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1206 }
1207 /* No need to intercept these MSRs */
1208 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
1209 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
1210 }
1211}
1212
Paolo Bonzini63129752021-03-02 14:40:39 -05001213static void init_vmcb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214{
Paolo Bonzini63129752021-03-02 14:40:39 -05001215 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedele6101a92008-02-13 18:58:45 +01001216 struct vmcb_control_area *control = &svm->vmcb->control;
1217 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001218
Babu Moger830bd712020-09-11 14:28:50 -05001219 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1220 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1221 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1222 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1223 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1224 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001225 if (!kvm_vcpu_apicv_active(vcpu))
Babu Moger830bd712020-09-11 14:28:50 -05001226 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227
Paolo Bonzini5315c712014-03-03 13:08:29 +01001228 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001229
Joerg Roedel18c918c2010-11-30 18:03:59 +01001230 set_exception_intercept(svm, PF_VECTOR);
1231 set_exception_intercept(svm, UD_VECTOR);
1232 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001233 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001234 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001235 /*
1236 * Guest access to VMware backdoor ports could legitimately
1237 * trigger #GP because of TSS I/O permission bitmap.
1238 * We intercept those #GP and allow access to them anyway
1239 * as VMware does.
1240 */
1241 if (enable_vmware_backdoor)
1242 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001243
Joerg Roedela284ba52020-06-25 10:03:24 +02001244 svm_set_intercept(svm, INTERCEPT_INTR);
1245 svm_set_intercept(svm, INTERCEPT_NMI);
Maxim Levitsky4b639a92021-07-07 15:51:00 +03001246
1247 if (intercept_smi)
1248 svm_set_intercept(svm, INTERCEPT_SMI);
1249
Joerg Roedela284ba52020-06-25 10:03:24 +02001250 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1251 svm_set_intercept(svm, INTERCEPT_RDPMC);
1252 svm_set_intercept(svm, INTERCEPT_CPUID);
1253 svm_set_intercept(svm, INTERCEPT_INVD);
1254 svm_set_intercept(svm, INTERCEPT_INVLPG);
1255 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1256 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1257 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1258 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1259 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1260 svm_set_intercept(svm, INTERCEPT_VMRUN);
1261 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1262 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1263 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1264 svm_set_intercept(svm, INTERCEPT_STGI);
1265 svm_set_intercept(svm, INTERCEPT_CLGI);
1266 svm_set_intercept(svm, INTERCEPT_SKINIT);
1267 svm_set_intercept(svm, INTERCEPT_WBINVD);
1268 svm_set_intercept(svm, INTERCEPT_XSETBV);
1269 svm_set_intercept(svm, INTERCEPT_RDPRU);
1270 svm_set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001271
Paolo Bonzini63129752021-03-02 14:40:39 -05001272 if (!kvm_mwait_in_guest(vcpu->kvm)) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001273 svm_set_intercept(svm, INTERCEPT_MONITOR);
1274 svm_set_intercept(svm, INTERCEPT_MWAIT);
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001275 }
1276
Paolo Bonzini63129752021-03-02 14:40:39 -05001277 if (!kvm_hlt_in_guest(vcpu->kvm))
Joerg Roedela284ba52020-06-25 10:03:24 +02001278 svm_set_intercept(svm, INTERCEPT_HLT);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001279
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001280 control->iopm_base_pa = __sme_set(iopm_base);
1281 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001282 control->int_ctl = V_INTR_MASKING_MASK;
1283
1284 init_seg(&save->es);
1285 init_seg(&save->ss);
1286 init_seg(&save->ds);
1287 init_seg(&save->fs);
1288 init_seg(&save->gs);
1289
1290 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001291 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001292 /* Executable/Readable Code Segment */
1293 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1294 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1295 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001297 save->gdtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001298 save->gdtr.limit = 0xffff;
Sean Christopherson4f117ce2021-07-13 09:32:41 -07001299 save->idtr.base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001300 save->idtr.limit = 0xffff;
1301
1302 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1303 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1304
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001305 if (npt_enabled) {
1306 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001307 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedela284ba52020-06-25 10:03:24 +02001308 svm_clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001309 clr_exception_intercept(svm, PF_VECTOR);
Babu Moger830bd712020-09-11 14:28:50 -05001310 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1311 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001312 save->g_pat = vcpu->arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001313 save->cr3 = 0;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001314 }
Cathy Avery193015a2021-01-12 11:43:13 -05001315 svm->current_vmcb->asid_generation = 0;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001316 svm->asid = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001317
Maxim Levitskyc74ad082021-05-03 15:54:43 +03001318 svm->nested.vmcb12_gpa = INVALID_GPA;
1319 svm->nested.last_vmcb12_gpa = INVALID_GPA;
Joerg Roedel2af91942009-08-07 11:49:28 +02001320
Paolo Bonzini63129752021-03-02 14:40:39 -05001321 if (!kvm_pause_in_guest(vcpu->kvm)) {
Babu Moger8566ac82018-03-16 16:37:26 -04001322 control->pause_filter_count = pause_filter_count;
1323 if (pause_filter_thresh)
1324 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedela284ba52020-06-25 10:03:24 +02001325 svm_set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001326 } else {
Joerg Roedela284ba52020-06-25 10:03:24 +02001327 svm_clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001328 }
1329
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001330 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05001331
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001332 /*
Babu Mogerd00b99c2021-02-17 10:56:04 -05001333 * If the host supports V_SPEC_CTRL then disable the interception
1334 * of MSR_IA32_SPEC_CTRL.
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001335 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05001336 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1337 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1338
Paolo Bonzini63129752021-03-02 14:40:39 -05001339 if (kvm_vcpu_apicv_active(vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001340 avic_init_vmcb(svm);
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001341
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001342 if (vgif) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001343 svm_clr_intercept(svm, INTERCEPT_STGI);
1344 svm_clr_intercept(svm, INTERCEPT_CLGI);
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001345 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1346 }
1347
Paolo Bonzini63129752021-03-02 14:40:39 -05001348 if (sev_guest(vcpu->kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001349 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001350 clr_exception_intercept(svm, UD_VECTOR);
Tom Lendacky376c6d22020-12-10 11:10:06 -06001351
Paolo Bonzini63129752021-03-02 14:40:39 -05001352 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky376c6d22020-12-10 11:10:06 -06001353 /* Perform SEV-ES specific VMCB updates */
1354 sev_es_init_vmcb(svm);
1355 }
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001356 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001357
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001358 svm_hv_init_vmcb(svm->vmcb);
Paolo Bonzini36e81942021-09-23 12:46:07 -04001359 init_vmcb_after_set_cpuid(vcpu);
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00001360
Joerg Roedel06e78522020-06-25 10:03:23 +02001361 vmcb_mark_all_dirty(svm->vmcb);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001362
Joerg Roedel2af91942009-08-07 11:49:28 +02001363 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001364}
1365
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001366static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
1367{
1368 struct vcpu_svm *svm = to_svm(vcpu);
1369
1370 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1371
1372 svm_init_osvw(vcpu);
1373 vcpu->arch.microcode_version = 0x01000065;
Maxim Levitsky5228eb92021-09-14 18:48:24 +03001374 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio;
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001375
1376 if (sev_es_guest(vcpu->kvm))
1377 sev_es_vcpu_reset(svm);
1378}
1379
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001380static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001381{
1382 struct vcpu_svm *svm = to_svm(vcpu);
1383
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001384 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001385 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001386
Paolo Bonzini63129752021-03-02 14:40:39 -05001387 init_vmcb(vcpu);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001388
1389 if (!init_event)
1390 __svm_vcpu_reset(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001391}
1392
Cathy Avery4995a362021-01-13 07:07:52 -05001393void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1394{
1395 svm->current_vmcb = target_vmcb;
1396 svm->vmcb = target_vmcb->ptr;
Cathy Avery4995a362021-01-13 07:07:52 -05001397}
1398
Sean Christopherson987b2592019-12-18 13:54:55 -08001399static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001400{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001401 struct vcpu_svm *svm;
Cathy Avery4995a362021-01-13 07:07:52 -05001402 struct page *vmcb01_page;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001403 struct page *vmsa_page = NULL;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001404 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001406 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1407 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001408
Joerg Roedelf65c2292008-02-13 18:58:46 +01001409 err = -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -05001410 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1411 if (!vmcb01_page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001412 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001413
Paolo Bonzini63129752021-03-02 14:40:39 -05001414 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001415 /*
1416 * SEV-ES guests require a separate VMSA page used to contain
1417 * the encrypted register state of the guest.
1418 */
1419 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1420 if (!vmsa_page)
1421 goto error_free_vmcb_page;
Tom Lendackyed02b212020-12-10 11:10:01 -06001422
1423 /*
1424 * SEV-ES guests maintain an encrypted version of their FPU
1425 * state which is restored and saved on VMRUN and VMEXIT.
1426 * Free the fpu structure to prevent KVM from attempting to
1427 * access the FPU state.
1428 */
1429 kvm_free_guest_fpu(vcpu);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001430 }
1431
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001432 err = avic_init_vcpu(svm);
1433 if (err)
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001434 goto error_free_vmsa_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001435
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001436 /* We initialize this flag to true to make sure that the is_running
1437 * bit would be set the first time the vcpu is loaded.
1438 */
Suravee Suthikulpanit6c3e4422019-11-14 14:15:12 -06001439 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1440 svm->avic_is_running = true;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001441
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001442 svm->msrpm = svm_vcpu_alloc_msrpm();
Chen Zhou054409a2020-11-17 10:54:26 +08001443 if (!svm->msrpm) {
1444 err = -ENOMEM;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001445 goto error_free_vmsa_page;
Chen Zhou054409a2020-11-17 10:54:26 +08001446 }
Alexander Grafb286d5d2008-11-25 20:17:05 +01001447
Cathy Avery4995a362021-01-13 07:07:52 -05001448 svm->vmcb01.ptr = page_address(vmcb01_page);
1449 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
Sean Christopherson9ebe5302021-09-20 17:03:02 -07001450 svm_switch_vmcb(svm, &svm->vmcb01);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001451
1452 if (vmsa_page)
1453 svm->vmsa = page_address(vmsa_page);
1454
Michael Rotha7fc06d2021-02-02 13:01:26 -06001455 svm->guest_state_loaded = false;
Cathy Avery4995a362021-01-13 07:07:52 -05001456
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001457 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001458
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001459error_free_vmsa_page:
1460 if (vmsa_page)
1461 __free_page(vmsa_page);
Maxim Levitsky8d22b902020-08-27 20:11:42 +03001462error_free_vmcb_page:
Cathy Avery4995a362021-01-13 07:07:52 -05001463 __free_page(vmcb01_page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001464out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001465 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001466}
1467
Jim Mattsonfd65d312018-05-22 09:54:20 -07001468static void svm_clear_current_vmcb(struct vmcb *vmcb)
1469{
1470 int i;
1471
1472 for_each_online_cpu(i)
1473 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1474}
1475
Avi Kivity6aa8b732006-12-10 02:21:36 -08001476static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1477{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001478 struct vcpu_svm *svm = to_svm(vcpu);
1479
Jim Mattsonfd65d312018-05-22 09:54:20 -07001480 /*
1481 * The vmcb page can be recycled, causing a false negative in
1482 * svm_vcpu_load(). So, ensure that no logical CPU has this
1483 * vmcb page recorded as its current vmcb.
1484 */
1485 svm_clear_current_vmcb(svm->vmcb);
1486
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03001487 svm_free_nested(svm);
1488
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001489 sev_free_vcpu(vcpu);
1490
Cathy Avery4995a362021-01-13 07:07:52 -05001491 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
Krish Sadhukhan47903dc2021-04-12 17:56:05 -04001492 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001493}
1494
Michael Rotha7fc06d2021-02-02 13:01:26 -06001495static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001496{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001497 struct vcpu_svm *svm = to_svm(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001498 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity0cc50642007-03-25 12:07:27 +02001499
Tom Lendackyce7ea0c2021-05-06 15:14:41 -05001500 if (sev_es_guest(vcpu->kvm))
1501 sev_es_unmap_ghcb(svm);
1502
Michael Rotha7fc06d2021-02-02 13:01:26 -06001503 if (svm->guest_state_loaded)
1504 return;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001505
Michael Rotha7fc06d2021-02-02 13:01:26 -06001506 /*
Michael Rotha7fc06d2021-02-02 13:01:26 -06001507 * Save additional host state that will be restored on VMEXIT (sev-es)
1508 * or subsequent vmload of host save area.
1509 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001510 if (sev_es_guest(vcpu->kvm)) {
Michael Rotha7fc06d2021-02-02 13:01:26 -06001511 sev_es_prepare_guest_switch(svm, vcpu->cpu);
Tom Lendacky86137772020-12-10 11:10:07 -06001512 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06001513 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06001514 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001515
Maxim Levitskyf8006502021-09-14 18:48:23 +03001516 if (tsc_scaling) {
Haozhong Zhangad7218832015-10-20 15:39:02 +08001517 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1518 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1519 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1520 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1521 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001522 }
Michael Rotha7fc06d2021-02-02 13:01:26 -06001523
Sean Christopherson0caa0a72021-05-04 10:17:25 -07001524 if (likely(tsc_aux_uret_slot >= 0))
1525 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001526
Michael Rotha7fc06d2021-02-02 13:01:26 -06001527 svm->guest_state_loaded = true;
1528}
1529
1530static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1531{
Sean Christopherson844d69c2021-04-23 15:34:04 -07001532 to_svm(vcpu)->guest_state_loaded = false;
Michael Rotha7fc06d2021-02-02 13:01:26 -06001533}
1534
1535static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1536{
1537 struct vcpu_svm *svm = to_svm(vcpu);
1538 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1539
Ashok Raj15d45072018-02-01 22:59:43 +01001540 if (sd->current_vmcb != svm->vmcb) {
1541 sd->current_vmcb = svm->vmcb;
1542 indirect_branch_prediction_barrier();
1543 }
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001544 if (kvm_vcpu_apicv_active(vcpu))
1545 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546}
1547
1548static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1549{
Maxim Levitskybf5f6b92021-08-10 23:52:49 +03001550 if (kvm_vcpu_apicv_active(vcpu))
1551 avic_vcpu_put(vcpu);
1552
Michael Rotha7fc06d2021-02-02 13:01:26 -06001553 svm_prepare_host_switch(vcpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001554
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001555 ++vcpu->stat.host_state_reload;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001556}
1557
Avi Kivity6aa8b732006-12-10 02:21:36 -08001558static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1559{
Ladi Prosek9b611742017-06-21 09:06:59 +02001560 struct vcpu_svm *svm = to_svm(vcpu);
1561 unsigned long rflags = svm->vmcb->save.rflags;
1562
1563 if (svm->nmi_singlestep) {
1564 /* Hide our flags if they were not set by the guest */
1565 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1566 rflags &= ~X86_EFLAGS_TF;
1567 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1568 rflags &= ~X86_EFLAGS_RF;
1569 }
1570 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001571}
1572
1573static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1574{
Ladi Prosek9b611742017-06-21 09:06:59 +02001575 if (to_svm(vcpu)->nmi_singlestep)
1576 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1577
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001578 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001579 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001580 * (caused by either a task switch or an inter-privilege IRET),
1581 * so we do not need to update the CPL here.
1582 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001583 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001584}
1585
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001586static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1587{
1588 switch (reg) {
1589 case VCPU_EXREG_PDPTR:
1590 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001591 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001592 break;
1593 default:
Sean Christopherson67369272021-07-02 15:04:25 -07001594 KVM_BUG_ON(1, vcpu->kvm);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001595 }
1596}
1597
Suravee Suthikulpanite14b7782020-05-06 08:17:55 -05001598static void svm_set_vintr(struct vcpu_svm *svm)
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001599{
1600 struct vmcb_control_area *control;
1601
Maxim Levitskyf1577ab2021-07-13 17:20:16 +03001602 /*
1603 * The following fields are ignored when AVIC is enabled
1604 */
1605 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
1606
Joerg Roedela284ba52020-06-25 10:03:24 +02001607 svm_set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001608
1609 /*
1610 * This is just a dummy VINTR to actually cause a vmexit to happen.
1611 * Actual injection of virtual interrupts happens through EVENTINJ.
1612 */
1613 control = &svm->vmcb->control;
1614 control->int_vector = 0x0;
1615 control->int_ctl &= ~V_INTR_PRIO_MASK;
1616 control->int_ctl |= V_IRQ_MASK |
1617 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedel06e78522020-06-25 10:03:23 +02001618 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001619}
1620
Alexander Graff0b85052008-11-25 20:17:01 +01001621static void svm_clear_vintr(struct vcpu_svm *svm)
1622{
Joerg Roedela284ba52020-06-25 10:03:24 +02001623 svm_clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001624
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001625 /* Drop int_ctl fields related to VINTR injection. */
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001626 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001627 if (is_guest_mode(&svm->vcpu)) {
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001628 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
Paolo Bonzinifb7333d2020-06-08 07:11:47 -04001629
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001630 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1631 (svm->nested.ctl.int_ctl & V_TPR_MASK));
Maxim Levitsky0f923e02021-07-15 01:56:24 +03001632
1633 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1634 V_IRQ_INJECTION_BITS_MASK;
Maxim Levitskyaee77e12021-09-14 18:48:12 +03001635
1636 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001637 }
1638
Joerg Roedel06e78522020-06-25 10:03:23 +02001639 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001640}
1641
Avi Kivity6aa8b732006-12-10 02:21:36 -08001642static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1643{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001644 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001645 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001646
1647 switch (seg) {
1648 case VCPU_SREG_CS: return &save->cs;
1649 case VCPU_SREG_DS: return &save->ds;
1650 case VCPU_SREG_ES: return &save->es;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001651 case VCPU_SREG_FS: return &save01->fs;
1652 case VCPU_SREG_GS: return &save01->gs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001653 case VCPU_SREG_SS: return &save->ss;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001654 case VCPU_SREG_TR: return &save01->tr;
1655 case VCPU_SREG_LDTR: return &save01->ldtr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001656 }
1657 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001658 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001659}
1660
1661static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1662{
1663 struct vmcb_seg *s = svm_seg(vcpu, seg);
1664
1665 return s->base;
1666}
1667
1668static void svm_get_segment(struct kvm_vcpu *vcpu,
1669 struct kvm_segment *var, int seg)
1670{
1671 struct vmcb_seg *s = svm_seg(vcpu, seg);
1672
1673 var->base = s->base;
1674 var->limit = s->limit;
1675 var->selector = s->selector;
1676 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1677 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1678 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1679 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1680 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1681 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1682 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301683
1684 /*
1685 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1686 * However, the SVM spec states that the G bit is not observed by the
1687 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1688 * So let's synthesize a legal G bit for all segments, this helps
1689 * running KVM nested. It also helps cross-vendor migration, because
1690 * Intel's vmentry has a check on the 'G' bit.
1691 */
1692 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001693
Joerg Roedele0231712010-02-24 18:59:10 +01001694 /*
1695 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001696 * for cross vendor migration purposes by "not present"
1697 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001698 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001699
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001700 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001701 case VCPU_SREG_TR:
1702 /*
1703 * Work around a bug where the busy flag in the tr selector
1704 * isn't exposed
1705 */
Amit Shahc0d09822008-10-27 09:04:18 +00001706 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001707 break;
1708 case VCPU_SREG_DS:
1709 case VCPU_SREG_ES:
1710 case VCPU_SREG_FS:
1711 case VCPU_SREG_GS:
1712 /*
1713 * The accessed bit must always be set in the segment
1714 * descriptor cache, although it can be cleared in the
1715 * descriptor, the cached bit always remains at 1. Since
1716 * Intel has a check on this, set it here to support
1717 * cross-vendor migration.
1718 */
1719 if (!var->unusable)
1720 var->type |= 0x1;
1721 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001722 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001723 /*
1724 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001725 * descriptor is left as 1, although the whole segment has
1726 * been made unusable. Clear it here to pass an Intel VMX
1727 * entry check when cross vendor migrating.
1728 */
1729 if (var->unusable)
1730 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001731 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001732 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001733 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001734 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001735}
1736
Izik Eidus2e4d2652008-03-24 19:38:34 +02001737static int svm_get_cpl(struct kvm_vcpu *vcpu)
1738{
1739 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1740
1741 return save->cpl;
1742}
1743
Gleb Natapov89a27f42010-02-16 10:51:48 +02001744static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001745{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001746 struct vcpu_svm *svm = to_svm(vcpu);
1747
Gleb Natapov89a27f42010-02-16 10:51:48 +02001748 dt->size = svm->vmcb->save.idtr.limit;
1749 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750}
1751
Gleb Natapov89a27f42010-02-16 10:51:48 +02001752static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001753{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001754 struct vcpu_svm *svm = to_svm(vcpu);
1755
Gleb Natapov89a27f42010-02-16 10:51:48 +02001756 svm->vmcb->save.idtr.limit = dt->size;
1757 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001758 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001759}
1760
Gleb Natapov89a27f42010-02-16 10:51:48 +02001761static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001762{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001763 struct vcpu_svm *svm = to_svm(vcpu);
1764
Gleb Natapov89a27f42010-02-16 10:51:48 +02001765 dt->size = svm->vmcb->save.gdtr.limit;
1766 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001767}
1768
Gleb Natapov89a27f42010-02-16 10:51:48 +02001769static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001770{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001771 struct vcpu_svm *svm = to_svm(vcpu);
1772
Gleb Natapov89a27f42010-02-16 10:51:48 +02001773 svm->vmcb->save.gdtr.limit = dt->size;
1774 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001775 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001776}
1777
Joerg Roedel883b0a92020-03-24 10:41:52 +01001778void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001779{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001780 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001781 u64 hcr0 = cr0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001782
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001783#ifdef CONFIG_X86_64
Tom Lendackyf1c63662020-12-14 10:29:50 -05001784 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001785 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001786 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001787 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001788 }
1789
Mike Dayd77c26f2007-10-08 09:02:08 -04001790 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001791 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001792 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001793 }
1794 }
1795#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001796 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001797
1798 if (!npt_enabled)
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001799 hcr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001800
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001801 /*
1802 * re-enable caching here because the QEMU bios
1803 * does not do it - this results in some delay at
1804 * reboot
1805 */
1806 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001807 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1808
1809 svm->vmcb->save.cr0 = hcr0;
Joerg Roedel06e78522020-06-25 10:03:23 +02001810 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001811
1812 /*
1813 * SEV-ES guests must always keep the CR intercepts cleared. CR
1814 * tracking is done using the CR write traps.
1815 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001816 if (sev_es_guest(vcpu->kvm))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001817 return;
1818
1819 if (hcr0 == cr0) {
1820 /* Selective CR0 write remains on. */
1821 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1822 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1823 } else {
1824 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1825 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1826 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827}
1828
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001829static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1830{
1831 return true;
1832}
1833
1834void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001835{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001836 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Paolo Bonzinidc924b02020-11-15 09:44:18 -05001837 unsigned long old_cr4 = vcpu->arch.cr4;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001838
1839 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Sean Christophersonf55ac302020-03-20 14:28:12 -07001840 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001841
Joerg Roedelec077262008-04-09 14:15:28 +02001842 vcpu->arch.cr4 = cr4;
1843 if (!npt_enabled)
1844 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001845 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001846 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedel06e78522020-06-25 10:03:23 +02001847 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Jim Mattson2259c172020-10-29 10:06:48 -07001848
1849 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1850 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001851}
1852
1853static void svm_set_segment(struct kvm_vcpu *vcpu,
1854 struct kvm_segment *var, int seg)
1855{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001856 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001857 struct vmcb_seg *s = svm_seg(vcpu, seg);
1858
1859 s->base = var->base;
1860 s->limit = var->limit;
1861 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001862 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1863 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1864 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1865 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1866 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1867 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1868 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1869 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001870
1871 /*
1872 * This is always accurate, except if SYSRET returned to a segment
1873 * with SS.DPL != 3. Intel does not have this quirk, and always
1874 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1875 * would entail passing the CPL to userspace and back.
1876 */
1877 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001878 /* This is symmetric with svm_get_segment() */
1879 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001880
Joerg Roedel06e78522020-06-25 10:03:23 +02001881 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001882}
1883
Jason Baronb6a7cc32021-01-14 22:27:54 -05001884static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001885{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001886 struct vcpu_svm *svm = to_svm(vcpu);
1887
Joerg Roedel18c918c2010-11-30 18:03:59 +01001888 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001889
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001890 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001891 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001892 set_exception_intercept(svm, BP_VECTOR);
Paolo Bonzini69869822020-07-10 17:48:06 +02001893 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001894}
1895
Tejun Heo0fe1e002009-10-29 22:34:14 +09001896static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001898 if (sd->next_asid > sd->max_asid) {
1899 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001900 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001901 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001902 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001903 }
1904
Cathy Avery193015a2021-01-12 11:43:13 -05001905 svm->current_vmcb->asid_generation = sd->asid_generation;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001906 svm->asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001907}
1908
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001909static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001910{
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001911 struct vmcb *vmcb = svm->vmcb;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001912
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001913 if (svm->vcpu.arch.guest_state_protected)
1914 return;
1915
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001916 if (unlikely(value != vmcb->save.dr6)) {
1917 vmcb->save.dr6 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001918 vmcb_mark_dirty(vmcb, VMCB_DR);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001919 }
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001920}
1921
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001922static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1923{
1924 struct vcpu_svm *svm = to_svm(vcpu);
1925
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001926 if (vcpu->arch.guest_state_protected)
1927 return;
1928
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001929 get_debugreg(vcpu->arch.db[0], 0);
1930 get_debugreg(vcpu->arch.db[1], 1);
1931 get_debugreg(vcpu->arch.db[2], 2);
1932 get_debugreg(vcpu->arch.db[3], 3);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001933 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001934 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001935 * because db_interception might need it. We can do it before vmentry.
1936 */
Paolo Bonzini5679b802020-05-04 11:28:25 -04001937 vcpu->arch.dr6 = svm->vmcb->save.dr6;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001938 vcpu->arch.dr7 = svm->vmcb->save.dr7;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001939 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1940 set_dr_intercepts(svm);
1941}
1942
Gleb Natapov020df072010-04-13 10:05:23 +03001943static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001944{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001945 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001946
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001947 if (vcpu->arch.guest_state_protected)
1948 return;
1949
Gleb Natapov020df072010-04-13 10:05:23 +03001950 svm->vmcb->save.dr7 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001951 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001952}
1953
Paolo Bonzini63129752021-03-02 14:40:39 -05001954static int pf_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001955{
Paolo Bonzini63129752021-03-02 14:40:39 -05001956 struct vcpu_svm *svm = to_svm(vcpu);
1957
Sean Christopherson6d1b8672021-03-04 17:10:56 -08001958 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07001959 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001960
Paolo Bonzini63129752021-03-02 14:40:39 -05001961 return kvm_handle_page_fault(vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001962 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1963 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001964 svm->vmcb->control.insn_len);
1965}
1966
Paolo Bonzini63129752021-03-02 14:40:39 -05001967static int npf_interception(struct kvm_vcpu *vcpu)
Paolo Bonzinid0006532017-08-11 18:36:43 +02001968{
Paolo Bonzini63129752021-03-02 14:40:39 -05001969 struct vcpu_svm *svm = to_svm(vcpu);
1970
Sean Christopherson76ff3712021-06-24 19:03:54 -07001971 u64 fault_address = svm->vmcb->control.exit_info_2;
Paolo Bonzinid0006532017-08-11 18:36:43 +02001972 u64 error_code = svm->vmcb->control.exit_info_1;
1973
1974 trace_kvm_page_fault(fault_address, error_code);
Paolo Bonzini63129752021-03-02 14:40:39 -05001975 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001976 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1977 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001978 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001979}
1980
Paolo Bonzini63129752021-03-02 14:40:39 -05001981static int db_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001982{
Paolo Bonzini63129752021-03-02 14:40:39 -05001983 struct kvm_run *kvm_run = vcpu->run;
1984 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001985
Paolo Bonzini63129752021-03-02 14:40:39 -05001986 if (!(vcpu->guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001987 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001988 !svm->nmi_singlestep) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001989 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
Paolo Bonzini63129752021-03-02 14:40:39 -05001990 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001991 return 1;
1992 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001993
Jan Kiszka6be7d302009-10-18 13:24:54 +02001994 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001995 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02001996 /* Make sure we check for pending NMIs upon entry */
1997 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001998 }
1999
Paolo Bonzini63129752021-03-02 14:40:39 -05002000 if (vcpu->guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002001 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002002 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Paolo Bonzinidee919d2020-05-04 09:34:10 -04002003 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
2004 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
Gleb Natapov44c11432009-05-11 13:35:52 +03002005 kvm_run->debug.arch.pc =
2006 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2007 kvm_run->debug.arch.exception = DB_VECTOR;
2008 return 0;
2009 }
2010
2011 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002012}
2013
Paolo Bonzini63129752021-03-02 14:40:39 -05002014static int bp_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002015{
Paolo Bonzini63129752021-03-02 14:40:39 -05002016 struct vcpu_svm *svm = to_svm(vcpu);
2017 struct kvm_run *kvm_run = vcpu->run;
Avi Kivity851ba692009-08-24 11:10:17 +03002018
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002019 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2020 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2021 kvm_run->debug.arch.exception = BP_VECTOR;
2022 return 0;
2023}
2024
Paolo Bonzini63129752021-03-02 14:40:39 -05002025static int ud_interception(struct kvm_vcpu *vcpu)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002026{
Paolo Bonzini63129752021-03-02 14:40:39 -05002027 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002028}
2029
Paolo Bonzini63129752021-03-02 14:40:39 -05002030static int ac_interception(struct kvm_vcpu *vcpu)
Eric Northup54a20552015-11-03 18:03:53 +01002031{
Paolo Bonzini63129752021-03-02 14:40:39 -05002032 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
Eric Northup54a20552015-11-03 18:03:53 +01002033 return 1;
2034}
2035
Joerg Roedel67ec6602010-05-17 14:43:35 +02002036static bool is_erratum_383(void)
2037{
2038 int err, i;
2039 u64 value;
2040
2041 if (!erratum_383_found)
2042 return false;
2043
2044 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2045 if (err)
2046 return false;
2047
2048 /* Bit 62 may or may not be set for this mce */
2049 value &= ~(1ULL << 62);
2050
2051 if (value != 0xb600000000010015ULL)
2052 return false;
2053
2054 /* Clear MCi_STATUS registers */
2055 for (i = 0; i < 6; ++i)
2056 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2057
2058 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2059 if (!err) {
2060 u32 low, high;
2061
2062 value &= ~(1ULL << 2);
2063 low = lower_32_bits(value);
2064 high = upper_32_bits(value);
2065
2066 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2067 }
2068
2069 /* Flush tlb to evict multi-match entries */
2070 __flush_tlb_all();
2071
2072 return true;
2073}
2074
Paolo Bonzini63129752021-03-02 14:40:39 -05002075static void svm_handle_mce(struct kvm_vcpu *vcpu)
Joerg Roedel53371b52008-04-09 14:15:30 +02002076{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002077 if (is_erratum_383()) {
2078 /*
2079 * Erratum 383 triggered. Guest state is corrupt so kill the
2080 * guest.
2081 */
2082 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2083
Paolo Bonzini63129752021-03-02 14:40:39 -05002084 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002085
2086 return;
2087 }
2088
Joerg Roedel53371b52008-04-09 14:15:30 +02002089 /*
2090 * On an #MC intercept the MCE handler is not called automatically in
2091 * the host. So do it by hand here.
2092 */
Uros Bizjak1c164cb2020-04-11 17:36:27 +02002093 kvm_machine_check();
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002094}
2095
Paolo Bonzini63129752021-03-02 14:40:39 -05002096static int mc_interception(struct kvm_vcpu *vcpu)
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002097{
Joerg Roedel53371b52008-04-09 14:15:30 +02002098 return 1;
2099}
2100
Paolo Bonzini63129752021-03-02 14:40:39 -05002101static int shutdown_interception(struct kvm_vcpu *vcpu)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002102{
Paolo Bonzini63129752021-03-02 14:40:39 -05002103 struct kvm_run *kvm_run = vcpu->run;
2104 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002105
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002106 /*
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002107 * The VM save area has already been encrypted so it
2108 * cannot be reinitialized - just terminate.
2109 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002110 if (sev_es_guest(vcpu->kvm))
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002111 return -EINVAL;
2112
2113 /*
Sean Christopherson265e4352021-07-13 09:33:22 -07002114 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
2115 * the VMCB in a known good state. Unfortuately, KVM doesn't have
2116 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
2117 * userspace. At a platform view, INIT is acceptable behavior as
2118 * there exist bare metal platforms that automatically INIT the CPU
2119 * in response to shutdown.
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002120 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002121 clear_page(svm->vmcb);
Sean Christopherson265e4352021-07-13 09:33:22 -07002122 kvm_vcpu_reset(vcpu, true);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002123
2124 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2125 return 0;
2126}
2127
Paolo Bonzini63129752021-03-02 14:40:39 -05002128static int io_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002129{
Paolo Bonzini63129752021-03-02 14:40:39 -05002130 struct vcpu_svm *svm = to_svm(vcpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002131 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002132 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002133 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002134
Paolo Bonzini63129752021-03-02 14:40:39 -05002135 ++vcpu->stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002136 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002137 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2138 port = io_info >> 16;
2139 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002140
2141 if (string) {
2142 if (sev_es_guest(vcpu->kvm))
2143 return sev_es_string_io(svm, size, port, in);
2144 else
2145 return kvm_emulate_instruction(vcpu, 0);
2146 }
2147
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002148 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002149
Paolo Bonzini63129752021-03-02 14:40:39 -05002150 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002151}
2152
Paolo Bonzini63129752021-03-02 14:40:39 -05002153static int nmi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002154{
2155 return 1;
2156}
2157
Maxim Levitsky991afbb2021-07-07 15:50:58 +03002158static int smi_interception(struct kvm_vcpu *vcpu)
2159{
2160 return 1;
2161}
2162
Paolo Bonzini63129752021-03-02 14:40:39 -05002163static int intr_interception(struct kvm_vcpu *vcpu)
Joerg Roedela0698052008-04-30 17:56:01 +02002164{
Paolo Bonzini63129752021-03-02 14:40:39 -05002165 ++vcpu->stat.irq_exits;
Joerg Roedela0698052008-04-30 17:56:01 +02002166 return 1;
2167}
2168
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002169static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002170{
Paolo Bonzini63129752021-03-02 14:40:39 -05002171 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002172 struct vmcb *vmcb12;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002173 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002174 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002175
Paolo Bonzini63129752021-03-02 14:40:39 -05002176 if (nested_svm_check_permissions(vcpu))
Alexander Graf55426752008-11-25 20:17:06 +01002177 return 1;
2178
Paolo Bonzini63129752021-03-02 14:40:39 -05002179 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002180 if (ret) {
2181 if (ret == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -05002182 kvm_inject_gp(vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002183 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002184 }
2185
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002186 vmcb12 = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002187
Paolo Bonzini63129752021-03-02 14:40:39 -05002188 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002189
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002190 if (vmload) {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02002191 svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002192 svm->sysenter_eip_hi = 0;
2193 svm->sysenter_esp_hi = 0;
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02002194 } else {
Vitaly Kuznetsov2bb16be2021-07-19 11:03:22 +02002195 svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
Vitaly Kuznetsov9a9e7482021-07-16 16:41:04 +02002196 }
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002197
Paolo Bonzini63129752021-03-02 14:40:39 -05002198 kvm_vcpu_unmap(vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002199
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002200 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002201}
2202
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002203static int vmload_interception(struct kvm_vcpu *vcpu)
Alexander Graf55426752008-11-25 20:17:06 +01002204{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002205 return vmload_vmsave_interception(vcpu, true);
Alexander Graf55426752008-11-25 20:17:06 +01002206}
2207
Paolo Bonzini63129752021-03-02 14:40:39 -05002208static int vmsave_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002209{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002210 return vmload_vmsave_interception(vcpu, false);
Alexander Grafc0725422008-11-25 20:17:03 +01002211}
2212
Paolo Bonzini63129752021-03-02 14:40:39 -05002213static int vmrun_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002214{
Paolo Bonzini63129752021-03-02 14:40:39 -05002215 if (nested_svm_check_permissions(vcpu))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002216 return 1;
2217
Paolo Bonzini63129752021-03-02 14:40:39 -05002218 return nested_svm_vmrun(vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002219}
2220
Bandan Das82a11e9c2021-01-26 03:18:29 -05002221enum {
2222 NONE_SVM_INSTR,
2223 SVM_INSTR_VMRUN,
2224 SVM_INSTR_VMLOAD,
2225 SVM_INSTR_VMSAVE,
2226};
2227
2228/* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2229static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2230{
2231 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2232
2233 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2234 return NONE_SVM_INSTR;
2235
2236 switch (ctxt->modrm) {
2237 case 0xd8: /* VMRUN */
2238 return SVM_INSTR_VMRUN;
2239 case 0xda: /* VMLOAD */
2240 return SVM_INSTR_VMLOAD;
2241 case 0xdb: /* VMSAVE */
2242 return SVM_INSTR_VMSAVE;
2243 default:
2244 break;
2245 }
2246
2247 return NONE_SVM_INSTR;
2248}
2249
2250static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2251{
Wei Huang14c2bf82021-01-26 03:18:31 -05002252 const int guest_mode_exit_codes[] = {
2253 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2254 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2255 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2256 };
Paolo Bonzini63129752021-03-02 14:40:39 -05002257 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
Bandan Das82a11e9c2021-01-26 03:18:29 -05002258 [SVM_INSTR_VMRUN] = vmrun_interception,
2259 [SVM_INSTR_VMLOAD] = vmload_interception,
2260 [SVM_INSTR_VMSAVE] = vmsave_interception,
2261 };
2262 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002263 int ret;
Bandan Das82a11e9c2021-01-26 03:18:29 -05002264
Wei Huang14c2bf82021-01-26 03:18:31 -05002265 if (is_guest_mode(vcpu)) {
Sean Christopherson2df8d382021-02-23 16:56:26 -08002266 /* Returns '1' or -errno on failure, '0' on success. */
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08002267 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002268 if (ret)
2269 return ret;
2270 return 1;
2271 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002272 return svm_instr_handlers[opcode](vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002273}
2274
2275/*
2276 * #GP handling code. Note that #GP can be triggered under the following two
2277 * cases:
2278 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2279 * some AMD CPUs when EAX of these instructions are in the reserved memory
2280 * regions (e.g. SMM memory on host).
2281 * 2) VMware backdoor
2282 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002283static int gp_interception(struct kvm_vcpu *vcpu)
Bandan Das82a11e9c2021-01-26 03:18:29 -05002284{
Paolo Bonzini63129752021-03-02 14:40:39 -05002285 struct vcpu_svm *svm = to_svm(vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002286 u32 error_code = svm->vmcb->control.exit_info_1;
2287 int opcode;
2288
2289 /* Both #GP cases have zero error_code */
2290 if (error_code)
2291 goto reinject;
2292
Maxim Levitskyd1cba6c2021-09-14 18:48:14 +03002293 /* All SVM instructions expect page aligned RAX */
2294 if (svm->vmcb->save.rax & ~PAGE_MASK)
2295 goto reinject;
2296
Bandan Das82a11e9c2021-01-26 03:18:29 -05002297 /* Decode the instruction for usage later */
2298 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2299 goto reinject;
2300
2301 opcode = svm_instr_opcode(vcpu);
2302
2303 if (opcode == NONE_SVM_INSTR) {
2304 if (!enable_vmware_backdoor)
2305 goto reinject;
2306
2307 /*
2308 * VMware backdoor emulation on #GP interception only handles
2309 * IN{S}, OUT{S}, and RDPMC.
2310 */
Wei Huang14c2bf82021-01-26 03:18:31 -05002311 if (!is_guest_mode(vcpu))
2312 return kvm_emulate_instruction(vcpu,
Bandan Das82a11e9c2021-01-26 03:18:29 -05002313 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2314 } else
2315 return emulate_svm_instr(vcpu, opcode);
2316
2317reinject:
2318 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2319 return 1;
2320}
2321
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002322void svm_set_gif(struct vcpu_svm *svm, bool value)
2323{
2324 if (value) {
2325 /*
2326 * If VGIF is enabled, the STGI intercept is only added to
2327 * detect the opening of the SMI/NMI window; remove it now.
2328 * Likewise, clear the VINTR intercept, we will set it
2329 * again while processing KVM_REQ_EVENT if needed.
2330 */
2331 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02002332 svm_clr_intercept(svm, INTERCEPT_STGI);
2333 if (svm_is_intercept(svm, INTERCEPT_VINTR))
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002334 svm_clear_vintr(svm);
2335
2336 enable_gif(svm);
2337 if (svm->vcpu.arch.smi_pending ||
2338 svm->vcpu.arch.nmi_pending ||
2339 kvm_cpu_has_injectable_intr(&svm->vcpu))
2340 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2341 } else {
2342 disable_gif(svm);
2343
2344 /*
2345 * After a CLGI no interrupts should come. But if vGIF is
2346 * in use, we still rely on the VINTR intercept (rather than
2347 * STGI) to detect an open interrupt window.
2348 */
2349 if (!vgif_enabled(svm))
2350 svm_clear_vintr(svm);
2351 }
2352}
2353
Paolo Bonzini63129752021-03-02 14:40:39 -05002354static int stgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002355{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002356 int ret;
2357
Paolo Bonzini63129752021-03-02 14:40:39 -05002358 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002359 return 1;
2360
Paolo Bonzini63129752021-03-02 14:40:39 -05002361 ret = kvm_skip_emulated_instruction(vcpu);
2362 svm_set_gif(to_svm(vcpu), true);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002363 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002364}
2365
Paolo Bonzini63129752021-03-02 14:40:39 -05002366static int clgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002367{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002368 int ret;
2369
Paolo Bonzini63129752021-03-02 14:40:39 -05002370 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002371 return 1;
2372
Paolo Bonzini63129752021-03-02 14:40:39 -05002373 ret = kvm_skip_emulated_instruction(vcpu);
2374 svm_set_gif(to_svm(vcpu), false);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002375 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002376}
2377
Paolo Bonzini63129752021-03-02 14:40:39 -05002378static int invlpga_interception(struct kvm_vcpu *vcpu)
Alexander Grafff092382009-06-15 15:21:24 +02002379{
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002380 gva_t gva = kvm_rax_read(vcpu);
2381 u32 asid = kvm_rcx_read(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002382
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002383 /* FIXME: Handle an address size prefix. */
2384 if (!is_long_mode(vcpu))
2385 gva = (u32)gva;
2386
2387 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
Joerg Roedelec1ff792009-10-09 16:08:31 +02002388
Alexander Grafff092382009-06-15 15:21:24 +02002389 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002390 kvm_mmu_invlpg(vcpu, gva);
Alexander Grafff092382009-06-15 15:21:24 +02002391
Paolo Bonzini63129752021-03-02 14:40:39 -05002392 return kvm_skip_emulated_instruction(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002393}
2394
Paolo Bonzini63129752021-03-02 14:40:39 -05002395static int skinit_interception(struct kvm_vcpu *vcpu)
Joerg Roedel532a46b2009-10-09 16:08:32 +02002396{
Paolo Bonzini63129752021-03-02 14:40:39 -05002397 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002398
Paolo Bonzini63129752021-03-02 14:40:39 -05002399 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel532a46b2009-10-09 16:08:32 +02002400 return 1;
2401}
2402
Paolo Bonzini63129752021-03-02 14:40:39 -05002403static int task_switch_interception(struct kvm_vcpu *vcpu)
David Kaplandab429a2015-03-02 13:43:37 -06002404{
Paolo Bonzini63129752021-03-02 14:40:39 -05002405 struct vcpu_svm *svm = to_svm(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002406 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002407 int reason;
2408 int int_type = svm->vmcb->control.exit_int_info &
2409 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002410 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002411 uint32_t type =
2412 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2413 uint32_t idt_v =
2414 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002415 bool has_error_code = false;
2416 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002417
2418 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002419
Izik Eidus37817f22008-03-24 23:14:53 +02002420 if (svm->vmcb->control.exit_info_2 &
2421 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002422 reason = TASK_SWITCH_IRET;
2423 else if (svm->vmcb->control.exit_info_2 &
2424 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2425 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002426 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002427 reason = TASK_SWITCH_GATE;
2428 else
2429 reason = TASK_SWITCH_CALL;
2430
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002431 if (reason == TASK_SWITCH_GATE) {
2432 switch (type) {
2433 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05002434 vcpu->arch.nmi_injected = false;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002435 break;
2436 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002437 if (svm->vmcb->control.exit_info_2 &
2438 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2439 has_error_code = true;
2440 error_code =
2441 (u32)svm->vmcb->control.exit_info_2;
2442 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002443 kvm_clear_exception_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002444 break;
2445 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05002446 kvm_clear_interrupt_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002447 break;
2448 default:
2449 break;
2450 }
2451 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002452
Gleb Natapov8317c292009-04-12 13:37:02 +03002453 if (reason != TASK_SWITCH_GATE ||
2454 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2455 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002456 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002457 if (!skip_emulated_instruction(vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002458 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002459 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002460
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002461 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2462 int_vec = -1;
2463
Paolo Bonzini63129752021-03-02 14:40:39 -05002464 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002465 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002466}
2467
Paolo Bonzini63129752021-03-02 14:40:39 -05002468static int iret_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002469{
Paolo Bonzini63129752021-03-02 14:40:39 -05002470 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002471
Paolo Bonzini63129752021-03-02 14:40:39 -05002472 ++vcpu->stat.nmi_window_exits;
2473 vcpu->arch.hflags |= HF_IRET_MASK;
2474 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002475 svm_clr_intercept(svm, INTERCEPT_IRET);
Paolo Bonzini63129752021-03-02 14:40:39 -05002476 svm->nmi_iret_rip = kvm_rip_read(vcpu);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002477 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002478 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002479 return 1;
2480}
2481
Paolo Bonzini63129752021-03-02 14:40:39 -05002482static int invlpg_interception(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002483{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002484 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002485 return kvm_emulate_instruction(vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002486
Paolo Bonzini63129752021-03-02 14:40:39 -05002487 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2488 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002489}
2490
Paolo Bonzini63129752021-03-02 14:40:39 -05002491static int emulate_on_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492{
Paolo Bonzini63129752021-03-02 14:40:39 -05002493 return kvm_emulate_instruction(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494}
2495
Paolo Bonzini63129752021-03-02 14:40:39 -05002496static int rsm_interception(struct kvm_vcpu *vcpu)
Brijesh Singh7607b712018-02-19 10:14:44 -06002497{
Paolo Bonzini63129752021-03-02 14:40:39 -05002498 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002499}
2500
Paolo Bonzini63129752021-03-02 14:40:39 -05002501static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
Xiubo Li52eb5a62015-03-13 17:39:45 +08002502 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002503{
Paolo Bonzini63129752021-03-02 14:40:39 -05002504 struct vcpu_svm *svm = to_svm(vcpu);
2505 unsigned long cr0 = vcpu->arch.cr0;
Joerg Roedel628afd22011-04-04 12:39:36 +02002506 bool ret = false;
Joerg Roedel628afd22011-04-04 12:39:36 +02002507
Paolo Bonzini63129752021-03-02 14:40:39 -05002508 if (!is_guest_mode(vcpu) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05002509 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
Joerg Roedel628afd22011-04-04 12:39:36 +02002510 return false;
2511
2512 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2513 val &= ~SVM_CR0_SELECTIVE_MASK;
2514
2515 if (cr0 ^ val) {
2516 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2517 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2518 }
2519
2520 return ret;
2521}
2522
Andre Przywara7ff76d52010-12-21 11:12:04 +01002523#define CR_VALID (1ULL << 63)
2524
Paolo Bonzini63129752021-03-02 14:40:39 -05002525static int cr_interception(struct kvm_vcpu *vcpu)
Andre Przywara7ff76d52010-12-21 11:12:04 +01002526{
Paolo Bonzini63129752021-03-02 14:40:39 -05002527 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002528 int reg, cr;
2529 unsigned long val;
2530 int err;
2531
2532 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002533 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002534
2535 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
Paolo Bonzini63129752021-03-02 14:40:39 -05002536 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002537
2538 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002539 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2540 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2541 else
2542 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002543
2544 err = 0;
2545 if (cr >= 16) { /* mov to cr */
2546 cr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002547 val = kvm_register_read(vcpu, reg);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002548 trace_kvm_cr_write(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002549 switch (cr) {
2550 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002551 if (!check_selective_cr0_intercepted(vcpu, val))
2552 err = kvm_set_cr0(vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002553 else
2554 return 1;
2555
Andre Przywara7ff76d52010-12-21 11:12:04 +01002556 break;
2557 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002558 err = kvm_set_cr3(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002559 break;
2560 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002561 err = kvm_set_cr4(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002562 break;
2563 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002564 err = kvm_set_cr8(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002565 break;
2566 default:
2567 WARN(1, "unhandled write to CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002568 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002569 return 1;
2570 }
2571 } else { /* mov from cr */
2572 switch (cr) {
2573 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002574 val = kvm_read_cr0(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002575 break;
2576 case 2:
Paolo Bonzini63129752021-03-02 14:40:39 -05002577 val = vcpu->arch.cr2;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002578 break;
2579 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002580 val = kvm_read_cr3(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002581 break;
2582 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002583 val = kvm_read_cr4(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002584 break;
2585 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002586 val = kvm_get_cr8(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002587 break;
2588 default:
2589 WARN(1, "unhandled read from CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002590 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002591 return 1;
2592 }
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002593 kvm_register_write(vcpu, reg, val);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002594 trace_kvm_cr_read(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002595 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002596 return kvm_complete_insn_gp(vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002597}
2598
Paolo Bonzini63129752021-03-02 14:40:39 -05002599static int cr_trap(struct kvm_vcpu *vcpu)
Tom Lendackyf27ad382020-12-10 11:09:56 -06002600{
Paolo Bonzini63129752021-03-02 14:40:39 -05002601 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002602 unsigned long old_value, new_value;
2603 unsigned int cr;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002604 int ret = 0;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002605
2606 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2607
2608 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2609 switch (cr) {
2610 case 0:
2611 old_value = kvm_read_cr0(vcpu);
2612 svm_set_cr0(vcpu, new_value);
2613
2614 kvm_post_set_cr0(vcpu, old_value, new_value);
2615 break;
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002616 case 4:
2617 old_value = kvm_read_cr4(vcpu);
2618 svm_set_cr4(vcpu, new_value);
2619
2620 kvm_post_set_cr4(vcpu, old_value, new_value);
2621 break;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002622 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002623 ret = kvm_set_cr8(vcpu, new_value);
Tom Lendackyd1949b92020-12-10 11:09:58 -06002624 break;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002625 default:
2626 WARN(1, "unhandled CR%d write trap", cr);
2627 kvm_queue_exception(vcpu, UD_VECTOR);
2628 return 1;
2629 }
2630
Tom Lendackyd1949b92020-12-10 11:09:58 -06002631 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002632}
2633
Paolo Bonzini63129752021-03-02 14:40:39 -05002634static int dr_interception(struct kvm_vcpu *vcpu)
Andre Przywaracae37972010-12-21 11:12:05 +01002635{
Paolo Bonzini63129752021-03-02 14:40:39 -05002636 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002637 int reg, dr;
2638 unsigned long val;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002639 int err = 0;
Andre Przywaracae37972010-12-21 11:12:05 +01002640
Paolo Bonzini63129752021-03-02 14:40:39 -05002641 if (vcpu->guest_debug == 0) {
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002642 /*
2643 * No more DR vmexits; force a reload of the debug registers
2644 * and reenter on this instruction. The next vmexit will
2645 * retrieve the full state of the debug registers.
2646 */
2647 clr_dr_intercepts(svm);
Paolo Bonzini63129752021-03-02 14:40:39 -05002648 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002649 return 1;
2650 }
2651
Andre Przywaracae37972010-12-21 11:12:05 +01002652 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002653 return emulate_on_interception(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002654
2655 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2656 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002657 if (dr >= 16) { /* mov to DRn */
2658 dr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002659 val = kvm_register_read(vcpu, reg);
Paolo Bonzini63129752021-03-02 14:40:39 -05002660 err = kvm_set_dr(vcpu, dr, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002661 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05002662 kvm_get_dr(vcpu, dr, &val);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002663 kvm_register_write(vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002664 }
2665
Paolo Bonzini63129752021-03-02 14:40:39 -05002666 return kvm_complete_insn_gp(vcpu, err);
Andre Przywaracae37972010-12-21 11:12:05 +01002667}
2668
Paolo Bonzini63129752021-03-02 14:40:39 -05002669static int cr8_write_interception(struct kvm_vcpu *vcpu)
Joerg Roedel1d075432007-12-06 21:02:25 +01002670{
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002671 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002672
Paolo Bonzini63129752021-03-02 14:40:39 -05002673 u8 cr8_prev = kvm_get_cr8(vcpu);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002674 /* instruction emulation calls kvm_set_cr8() */
Paolo Bonzini63129752021-03-02 14:40:39 -05002675 r = cr_interception(vcpu);
2676 if (lapic_in_kernel(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002677 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002678 if (cr8_prev <= kvm_get_cr8(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002679 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002680 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Joerg Roedel1d075432007-12-06 21:02:25 +01002681 return 0;
2682}
2683
Paolo Bonzini63129752021-03-02 14:40:39 -05002684static int efer_trap(struct kvm_vcpu *vcpu)
Tom Lendacky2985afb2020-12-10 11:09:55 -06002685{
2686 struct msr_data msr_info;
2687 int ret;
2688
2689 /*
2690 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2691 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2692 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2693 * the guest doesn't have X86_FEATURE_SVM.
2694 */
2695 msr_info.host_initiated = false;
2696 msr_info.index = MSR_EFER;
Paolo Bonzini63129752021-03-02 14:40:39 -05002697 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2698 ret = kvm_set_msr_common(vcpu, &msr_info);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002699
Paolo Bonzini63129752021-03-02 14:40:39 -05002700 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002701}
2702
Tom Lendacky801e4592018-02-21 13:39:51 -06002703static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2704{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002705 msr->data = 0;
2706
2707 switch (msr->index) {
2708 case MSR_F10H_DECFG:
2709 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2710 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2711 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02002712 case MSR_IA32_PERF_CAPABILITIES:
2713 return 0;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002714 default:
Peter Xu12bc2132020-06-22 18:04:42 -04002715 return KVM_MSR_RET_INVALID;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002716 }
2717
2718 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002719}
2720
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002721static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002722{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002723 struct vcpu_svm *svm = to_svm(vcpu);
2724
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002725 switch (msr_info->index) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002726 case MSR_AMD64_TSC_RATIO:
2727 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2728 return 1;
2729 msr_info->data = svm->tsc_ratio_msr;
2730 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002731 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002732 msr_info->data = svm->vmcb01.ptr->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002733 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002734#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002735 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002736 msr_info->data = svm->vmcb01.ptr->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 break;
2738 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002739 msr_info->data = svm->vmcb01.ptr->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002740 break;
2741 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002742 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002743 break;
2744 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002745 msr_info->data = svm->vmcb01.ptr->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002746 break;
2747#endif
2748 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002749 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 break;
2751 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002752 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2753 if (guest_cpuid_is_intel(vcpu))
2754 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 break;
2756 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002757 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2758 if (guest_cpuid_is_intel(vcpu))
2759 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002760 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002761 case MSR_TSC_AUX:
Paolo Bonzini46896c72015-11-12 14:49:16 +01002762 msr_info->data = svm->tsc_aux;
2763 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002764 /*
2765 * Nobody will change the following 5 values in the VMCB so we can
2766 * safely return them on rdmsr. They will always be 0 until LBRV is
2767 * implemented.
2768 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002769 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002770 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002771 break;
2772 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002773 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002774 break;
2775 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002776 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002777 break;
2778 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002779 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002780 break;
2781 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002782 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002783 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002784 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002785 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002786 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002787 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002788 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002789 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002790 case MSR_IA32_SPEC_CTRL:
2791 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002792 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002793 return 1;
2794
Babu Mogerd00b99c2021-02-17 10:56:04 -05002795 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2796 msr_info->data = svm->vmcb->save.spec_ctrl;
2797 else
2798 msr_info->data = svm->spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002799 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002800 case MSR_AMD64_VIRT_SPEC_CTRL:
2801 if (!msr_info->host_initiated &&
2802 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2803 return 1;
2804
2805 msr_info->data = svm->virt_spec_ctrl;
2806 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002807 case MSR_F15H_IC_CFG: {
2808
2809 int family, model;
2810
2811 family = guest_cpuid_family(vcpu);
2812 model = guest_cpuid_model(vcpu);
2813
2814 if (family < 0 || model < 0)
2815 return kvm_get_msr_common(vcpu, msr_info);
2816
2817 msr_info->data = 0;
2818
2819 if (family == 0x15 &&
2820 (model >= 0x2 && model < 0x20))
2821 msr_info->data = 0x1E;
2822 }
2823 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002824 case MSR_F10H_DECFG:
2825 msr_info->data = svm->msr_decfg;
2826 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002827 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002828 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002829 }
2830 return 0;
2831}
2832
Tom Lendackyf1c63662020-12-14 10:29:50 -05002833static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2834{
2835 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002836 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->ghcb))
Paolo Bonzini63129752021-03-02 14:40:39 -05002837 return kvm_complete_insn_gp(vcpu, err);
Tom Lendackyf1c63662020-12-14 10:29:50 -05002838
2839 ghcb_set_sw_exit_info_1(svm->ghcb, 1);
2840 ghcb_set_sw_exit_info_2(svm->ghcb,
2841 X86_TRAP_GP |
2842 SVM_EVTINJ_TYPE_EXEPT |
2843 SVM_EVTINJ_VALID);
2844 return 1;
2845}
2846
Joerg Roedel4a810182010-02-24 18:59:15 +01002847static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2848{
2849 struct vcpu_svm *svm = to_svm(vcpu);
2850 int svm_dis, chg_mask;
2851
2852 if (data & ~SVM_VM_CR_VALID_MASK)
2853 return 1;
2854
2855 chg_mask = SVM_VM_CR_VALID_MASK;
2856
2857 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2858 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2859
2860 svm->nested.vm_cr_msr &= ~chg_mask;
2861 svm->nested.vm_cr_msr |= (data & chg_mask);
2862
2863 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2864
2865 /* check for svm_disable while efer.svme is set */
2866 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2867 return 1;
2868
2869 return 0;
2870}
2871
Will Auld8fe8ab42012-11-29 12:42:12 -08002872static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002874 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002875 int r;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002876
Will Auld8fe8ab42012-11-29 12:42:12 -08002877 u32 ecx = msr->index;
2878 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 switch (ecx) {
Maxim Levitsky5228eb92021-09-14 18:48:24 +03002880 case MSR_AMD64_TSC_RATIO:
2881 if (!msr->host_initiated && !svm->tsc_scaling_enabled)
2882 return 1;
2883
2884 if (data & TSC_RATIO_RSVD)
2885 return 1;
2886
2887 svm->tsc_ratio_msr = data;
2888
2889 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2890 nested_svm_update_tsc_ratio_msr(vcpu);
2891
2892 break;
Paolo Bonzini15038e12017-10-26 09:13:27 +02002893 case MSR_IA32_CR_PAT:
2894 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2895 return 1;
2896 vcpu->arch.pat = data;
Cathy Avery4995a362021-01-13 07:07:52 -05002897 svm->vmcb01.ptr->save.g_pat = data;
2898 if (is_guest_mode(vcpu))
2899 nested_vmcb02_compute_g_pat(svm);
Joerg Roedel06e78522020-06-25 10:03:23 +02002900 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Paolo Bonzini15038e12017-10-26 09:13:27 +02002901 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002902 case MSR_IA32_SPEC_CTRL:
2903 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002904 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002905 return 1;
2906
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002907 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002908 return 1;
2909
Babu Mogerd00b99c2021-02-17 10:56:04 -05002910 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2911 svm->vmcb->save.spec_ctrl = data;
2912 else
2913 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002914 if (!data)
2915 break;
2916
2917 /*
2918 * For non-nested:
2919 * When it's written (to non-zero) for the first time, pass
2920 * it through.
2921 *
2922 * For nested:
2923 * The handling of the MSR bitmap for L2 guests is done in
2924 * nested_svm_vmrun_msrpm.
2925 * We update the L1 MSR bit as well since it will end up
2926 * touching the MSR anyway now.
2927 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002928 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002929 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002930 case MSR_IA32_PRED_CMD:
2931 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002932 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002933 return 1;
2934
2935 if (data & ~PRED_CMD_IBPB)
2936 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002937 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002938 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002939 if (!data)
2940 break;
2941
2942 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002943 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
Ashok Raj15d45072018-02-01 22:59:43 +01002944 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002945 case MSR_AMD64_VIRT_SPEC_CTRL:
2946 if (!msr->host_initiated &&
2947 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2948 return 1;
2949
2950 if (data & ~SPEC_CTRL_SSBD)
2951 return 1;
2952
2953 svm->virt_spec_ctrl = data;
2954 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002955 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002956 svm->vmcb01.ptr->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002957 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002958#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002959 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002960 svm->vmcb01.ptr->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002961 break;
2962 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002963 svm->vmcb01.ptr->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002964 break;
2965 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002966 svm->vmcb01.ptr->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 break;
2968 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002969 svm->vmcb01.ptr->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002970 break;
2971#endif
2972 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002973 svm->vmcb01.ptr->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002974 break;
2975 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002976 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2977 /*
2978 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2979 * when we spoof an Intel vendor ID (for cross vendor migration).
2980 * In this case we use this intercept to track the high
2981 * 32 bit part of these msrs to support Intel's
2982 * implementation of SYSENTER/SYSEXIT.
2983 */
2984 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985 break;
2986 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002987 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2988 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002990 case MSR_TSC_AUX:
Sean Christophersondbd61272021-04-23 15:34:02 -07002991 /*
Sean Christopherson844d69c2021-04-23 15:34:04 -07002992 * TSC_AUX is usually changed only during boot and never read
2993 * directly. Intercept TSC_AUX instead of exposing it to the
2994 * guest via direct_access_msrs, and switch it via user return.
Paolo Bonzini46896c72015-11-12 14:49:16 +01002995 */
Sean Christopherson844d69c2021-04-23 15:34:04 -07002996 preempt_disable();
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002997 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002998 preempt_enable();
2999 if (r)
3000 return 1;
3001
Paolo Bonzini46896c72015-11-12 14:49:16 +01003002 svm->tsc_aux = data;
Paolo Bonzini46896c72015-11-12 14:49:16 +01003003 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01003004 case MSR_IA32_DEBUGCTLMSR:
Maxim Levitsky4c849262021-09-14 18:48:19 +03003005 if (!lbrv) {
Christoffer Dalla737f252012-06-03 21:17:48 +03003006 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
3007 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003008 break;
3009 }
3010 if (data & DEBUGCTL_RESERVED_BITS)
3011 return 1;
3012
3013 svm->vmcb->save.dbgctl = data;
Joerg Roedel06e78522020-06-25 10:03:23 +02003014 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003015 if (data & (1ULL<<0))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003016 svm_enable_lbrv(vcpu);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01003017 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02003018 svm_disable_lbrv(vcpu);
Joerg Roedela2938c82008-02-13 16:30:28 +01003019 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003020 case MSR_VM_HSAVE_PA:
Vitaly Kuznetsovfce7e152021-06-28 12:44:20 +02003021 /*
3022 * Old kernels did not validate the value written to
3023 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
3024 * value to allow live migrating buggy or malicious guests
3025 * originating from those kernels.
3026 */
3027 if (!msr->host_initiated && !page_address_valid(vcpu, data))
3028 return 1;
3029
3030 svm->nested.hsave_msr = data & PAGE_MASK;
Alexander Grafb286d5d2008-11-25 20:17:05 +01003031 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003032 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01003033 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003034 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03003035 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02003036 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01003037 case MSR_F10H_DECFG: {
3038 struct kvm_msr_entry msr_entry;
3039
3040 msr_entry.index = msr->index;
3041 if (svm_get_msr_feature(&msr_entry))
3042 return 1;
3043
3044 /* Check the supported bits */
3045 if (data & ~msr_entry.data)
3046 return 1;
3047
3048 /* Don't allow the guest to change a bit, #GP */
3049 if (!msr->host_initiated && (data ^ msr_entry.data))
3050 return 1;
3051
3052 svm->msr_decfg = data;
3053 break;
3054 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003055 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08003056 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 }
3058 return 0;
3059}
3060
Paolo Bonzini63129752021-03-02 14:40:39 -05003061static int msr_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062{
Paolo Bonzini63129752021-03-02 14:40:39 -05003063 if (to_svm(vcpu)->vmcb->control.exit_info_1)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003064 return kvm_emulate_wrmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003065 else
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003066 return kvm_emulate_rdmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003067}
3068
Paolo Bonzini63129752021-03-02 14:40:39 -05003069static int interrupt_window_interception(struct kvm_vcpu *vcpu)
Dor Laorc1150d82007-01-05 16:36:24 -08003070{
Paolo Bonzini63129752021-03-02 14:40:39 -05003071 kvm_make_request(KVM_REQ_EVENT, vcpu);
3072 svm_clear_vintr(to_svm(vcpu));
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003073
3074 /*
3075 * For AVIC, the only reason to end up here is ExtINTs.
3076 * In this case AVIC was temporarily disabled for
3077 * requesting the IRQ window and we have to re-enable it.
3078 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03003079 kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003080
Paolo Bonzini63129752021-03-02 14:40:39 -05003081 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08003082 return 1;
3083}
3084
Paolo Bonzini63129752021-03-02 14:40:39 -05003085static int pause_interception(struct kvm_vcpu *vcpu)
Mark Langsdorf565d0992009-10-06 14:25:02 -05003086{
Tom Lendackyf1c63662020-12-14 10:29:50 -05003087 bool in_kernel;
3088
3089 /*
3090 * CPL is not made available for an SEV-ES guest, therefore
3091 * vcpu->arch.preempted_in_kernel can never be true. Just
3092 * set in_kernel to false as well.
3093 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003094 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003095
Wanpeng Li830f01b2020-07-31 11:12:21 +08003096 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04003097 grow_ple_window(vcpu);
3098
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003099 kvm_vcpu_on_spin(vcpu, in_kernel);
Sean Christophersonc8781fe2021-02-04 16:57:50 -08003100 return kvm_skip_emulated_instruction(vcpu);
Mark Langsdorf565d0992009-10-06 14:25:02 -05003101}
3102
Paolo Bonzini63129752021-03-02 14:40:39 -05003103static int invpcid_interception(struct kvm_vcpu *vcpu)
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003104{
Paolo Bonzini63129752021-03-02 14:40:39 -05003105 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger4407a792020-09-11 14:29:19 -05003106 unsigned long type;
3107 gva_t gva;
3108
3109 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3110 kvm_queue_exception(vcpu, UD_VECTOR);
3111 return 1;
3112 }
3113
3114 /*
3115 * For an INVPCID intercept:
3116 * EXITINFO1 provides the linear address of the memory operand.
3117 * EXITINFO2 provides the contents of the register operand.
3118 */
3119 type = svm->vmcb->control.exit_info_2;
3120 gva = svm->vmcb->control.exit_info_1;
3121
3122 if (type > 3) {
3123 kvm_inject_gp(vcpu, 0);
3124 return 1;
3125 }
3126
3127 return kvm_handle_invpcid(vcpu, type, gva);
3128}
3129
Paolo Bonzini63129752021-03-02 14:40:39 -05003130static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003131 [SVM_EXIT_READ_CR0] = cr_interception,
3132 [SVM_EXIT_READ_CR3] = cr_interception,
3133 [SVM_EXIT_READ_CR4] = cr_interception,
3134 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003135 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003136 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003137 [SVM_EXIT_WRITE_CR3] = cr_interception,
3138 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003139 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003140 [SVM_EXIT_READ_DR0] = dr_interception,
3141 [SVM_EXIT_READ_DR1] = dr_interception,
3142 [SVM_EXIT_READ_DR2] = dr_interception,
3143 [SVM_EXIT_READ_DR3] = dr_interception,
3144 [SVM_EXIT_READ_DR4] = dr_interception,
3145 [SVM_EXIT_READ_DR5] = dr_interception,
3146 [SVM_EXIT_READ_DR6] = dr_interception,
3147 [SVM_EXIT_READ_DR7] = dr_interception,
3148 [SVM_EXIT_WRITE_DR0] = dr_interception,
3149 [SVM_EXIT_WRITE_DR1] = dr_interception,
3150 [SVM_EXIT_WRITE_DR2] = dr_interception,
3151 [SVM_EXIT_WRITE_DR3] = dr_interception,
3152 [SVM_EXIT_WRITE_DR4] = dr_interception,
3153 [SVM_EXIT_WRITE_DR5] = dr_interception,
3154 [SVM_EXIT_WRITE_DR6] = dr_interception,
3155 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003156 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3157 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003158 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003159 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003160 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003161 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02003162 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003163 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003164 [SVM_EXIT_NMI] = nmi_interception,
Maxim Levitsky991afbb2021-07-07 15:50:58 +03003165 [SVM_EXIT_SMI] = smi_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003166 [SVM_EXIT_VINTR] = interrupt_window_interception,
Sean Christopherson32c23c72021-02-04 16:57:49 -08003167 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003168 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003169 [SVM_EXIT_IRET] = iret_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003170 [SVM_EXIT_INVD] = kvm_emulate_invd,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003171 [SVM_EXIT_PAUSE] = pause_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003172 [SVM_EXIT_HLT] = kvm_emulate_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003173 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003174 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003175 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003176 [SVM_EXIT_MSR] = msr_interception,
3177 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003178 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003179 [SVM_EXIT_VMRUN] = vmrun_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003180 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
Alexander Graf55426752008-11-25 20:17:06 +01003181 [SVM_EXIT_VMLOAD] = vmload_interception,
3182 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003183 [SVM_EXIT_STGI] = stgi_interception,
3184 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003185 [SVM_EXIT_SKINIT] = skinit_interception,
Sean Christopherson3b195ac2021-05-04 10:17:22 -07003186 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003187 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3188 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3189 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
Sean Christopherson92f98952021-02-04 16:57:46 -08003190 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003191 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
Tom Lendacky2985afb2020-12-10 11:09:55 -06003192 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
Tom Lendackyf27ad382020-12-10 11:09:56 -06003193 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
Tom Lendacky5b51cb12020-12-10 11:09:57 -06003194 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
Tom Lendackyd1949b92020-12-10 11:09:58 -06003195 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
Babu Moger4407a792020-09-11 14:29:19 -05003196 [SVM_EXIT_INVPCID] = invpcid_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003197 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06003198 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003199 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3200 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Tom Lendacky291bd202020-12-10 11:09:47 -06003201 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003202};
3203
Joe Perchesae8cc052011-04-24 22:00:50 -07003204static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003205{
3206 struct vcpu_svm *svm = to_svm(vcpu);
3207 struct vmcb_control_area *control = &svm->vmcb->control;
3208 struct vmcb_save_area *save = &svm->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003209 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003210
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003211 if (!dump_invalid_vmcb) {
3212 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3213 return;
3214 }
3215
Jim Mattson18f63b12021-06-21 15:16:48 -07003216 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
3217 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003218 pr_err("VMCB Control Area:\n");
Babu Moger03bfeeb2020-09-11 14:28:05 -05003219 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3220 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
Babu Moger30abaa882020-09-11 14:28:12 -05003221 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3222 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
Babu Moger9780d512020-09-11 14:28:20 -05003223 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
Babu Mogerc62e2e92020-09-11 14:28:28 -05003224 pr_err("%-20s%08x %08x\n", "intercepts:",
3225 control->intercepts[INTERCEPT_WORD3],
3226 control->intercepts[INTERCEPT_WORD4]);
Joe Perchesae8cc052011-04-24 22:00:50 -07003227 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003228 pr_err("%-20s%d\n", "pause filter threshold:",
3229 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003230 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3231 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3232 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3233 pr_err("%-20s%d\n", "asid:", control->asid);
3234 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3235 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3236 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3237 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3238 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3239 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3240 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3241 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3242 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3243 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3244 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003245 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Tom Lendacky291bd202020-12-10 11:09:47 -06003246 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
Joe Perchesae8cc052011-04-24 22:00:50 -07003247 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3248 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003249 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003250 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003251 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3252 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3253 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Tom Lendacky376c6d22020-12-10 11:10:06 -06003254 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003255 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003256 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3257 "es:",
3258 save->es.selector, save->es.attrib,
3259 save->es.limit, save->es.base);
3260 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3261 "cs:",
3262 save->cs.selector, save->cs.attrib,
3263 save->cs.limit, save->cs.base);
3264 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3265 "ss:",
3266 save->ss.selector, save->ss.attrib,
3267 save->ss.limit, save->ss.base);
3268 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3269 "ds:",
3270 save->ds.selector, save->ds.attrib,
3271 save->ds.limit, save->ds.base);
3272 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3273 "fs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003274 save01->fs.selector, save01->fs.attrib,
3275 save01->fs.limit, save01->fs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003276 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3277 "gs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003278 save01->gs.selector, save01->gs.attrib,
3279 save01->gs.limit, save01->gs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003280 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3281 "gdtr:",
3282 save->gdtr.selector, save->gdtr.attrib,
3283 save->gdtr.limit, save->gdtr.base);
3284 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3285 "ldtr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003286 save01->ldtr.selector, save01->ldtr.attrib,
3287 save01->ldtr.limit, save01->ldtr.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003288 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3289 "idtr:",
3290 save->idtr.selector, save->idtr.attrib,
3291 save->idtr.limit, save->idtr.base);
3292 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3293 "tr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003294 save01->tr.selector, save01->tr.attrib,
3295 save01->tr.limit, save01->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003296 pr_err("cpl: %d efer: %016llx\n",
3297 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003298 pr_err("%-15s %016llx %-13s %016llx\n",
3299 "cr0:", save->cr0, "cr2:", save->cr2);
3300 pr_err("%-15s %016llx %-13s %016llx\n",
3301 "cr3:", save->cr3, "cr4:", save->cr4);
3302 pr_err("%-15s %016llx %-13s %016llx\n",
3303 "dr6:", save->dr6, "dr7:", save->dr7);
3304 pr_err("%-15s %016llx %-13s %016llx\n",
3305 "rip:", save->rip, "rflags:", save->rflags);
3306 pr_err("%-15s %016llx %-13s %016llx\n",
3307 "rsp:", save->rsp, "rax:", save->rax);
3308 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003309 "star:", save01->star, "lstar:", save01->lstar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003310 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003311 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
Joe Perchesae8cc052011-04-24 22:00:50 -07003312 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003313 "kernel_gs_base:", save01->kernel_gs_base,
3314 "sysenter_cs:", save01->sysenter_cs);
Joe Perchesae8cc052011-04-24 22:00:50 -07003315 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003316 "sysenter_esp:", save01->sysenter_esp,
3317 "sysenter_eip:", save01->sysenter_eip);
Joe Perchesae8cc052011-04-24 22:00:50 -07003318 pr_err("%-15s %016llx %-13s %016llx\n",
3319 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3320 pr_err("%-15s %016llx %-13s %016llx\n",
3321 "br_from:", save->br_from, "br_to:", save->br_to);
3322 pr_err("%-15s %016llx %-13s %016llx\n",
3323 "excp_from:", save->last_excp_from,
3324 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003325}
3326
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003327static bool svm_check_exit_valid(struct kvm_vcpu *vcpu, u64 exit_code)
3328{
3329 return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3330 svm_exit_handlers[exit_code]);
3331}
3332
Tom Lendackye9093fd42020-12-10 11:09:46 -06003333static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3334{
Tom Lendackye9093fd42020-12-10 11:09:46 -06003335 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3336 dump_vmcb(vcpu);
3337 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3338 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3339 vcpu->run->internal.ndata = 2;
3340 vcpu->run->internal.data[0] = exit_code;
3341 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003342 return 0;
Tom Lendackye9093fd42020-12-10 11:09:46 -06003343}
3344
Paolo Bonzini63129752021-03-02 14:40:39 -05003345int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
Tom Lendackye9093fd42020-12-10 11:09:46 -06003346{
Maxim Levitsky7a4bca82021-08-11 15:29:22 +03003347 if (!svm_check_exit_valid(vcpu, exit_code))
3348 return svm_handle_invalid_exit(vcpu, exit_code);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003349
3350#ifdef CONFIG_RETPOLINE
3351 if (exit_code == SVM_EXIT_MSR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003352 return msr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003353 else if (exit_code == SVM_EXIT_VINTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003354 return interrupt_window_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003355 else if (exit_code == SVM_EXIT_INTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003356 return intr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003357 else if (exit_code == SVM_EXIT_HLT)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003358 return kvm_emulate_halt(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003359 else if (exit_code == SVM_EXIT_NPF)
Paolo Bonzini63129752021-03-02 14:40:39 -05003360 return npf_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003361#endif
Paolo Bonzini63129752021-03-02 14:40:39 -05003362 return svm_exit_handlers[exit_code](vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003363}
3364
Sean Christopherson235ba742020-09-23 13:13:46 -07003365static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3366 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02003367{
3368 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3369
3370 *info1 = control->exit_info_1;
3371 *info2 = control->exit_info_2;
Sean Christopherson235ba742020-09-23 13:13:46 -07003372 *intr_info = control->exit_int_info;
3373 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3374 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3375 *error_code = control->exit_int_info_err;
3376 else
3377 *error_code = 0;
Avi Kivity586f9602010-11-18 13:09:54 +02003378}
3379
Wanpeng Li404d5d72020-04-28 14:23:25 +08003380static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003381{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003382 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003383 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003384 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003385
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003386 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3387
Tom Lendackyf1c63662020-12-14 10:29:50 -05003388 /* SEV-ES guests must use the CR write traps to track CR registers. */
3389 if (!sev_es_guest(vcpu->kvm)) {
3390 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3391 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3392 if (npt_enabled)
3393 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3394 }
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003395
Joerg Roedel20307532010-11-29 17:51:48 +01003396 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003397 int vmexit;
3398
Sean Christophersoncc167bd2020-09-23 13:13:48 -07003399 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003400
Joerg Roedel410e4d52009-08-07 11:49:44 +02003401 vmexit = nested_svm_exit_special(svm);
3402
3403 if (vmexit == NESTED_EXIT_CONTINUE)
3404 vmexit = nested_svm_exit_handled(svm);
3405
3406 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003407 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003408 }
3409
Avi Kivity04d2cc72007-09-10 18:10:54 +03003410 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3411 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3412 kvm_run->fail_entry.hardware_entry_failure_reason
3413 = svm->vmcb->control.exit_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07003414 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003415 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003416 return 0;
3417 }
3418
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003419 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003420 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003421 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3422 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003423 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003424 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003425 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003426 exit_code);
3427
Wanpeng Li404d5d72020-04-28 14:23:25 +08003428 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003429 return 1;
Wanpeng Li404d5d72020-04-28 14:23:25 +08003430
Paolo Bonzini63129752021-03-02 14:40:39 -05003431 return svm_invoke_exit_handler(vcpu, exit_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432}
3433
3434static void reload_tss(struct kvm_vcpu *vcpu)
3435{
Jim Mattson73cd6e52020-06-03 16:56:18 -07003436 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437
Tejun Heo0fe1e002009-10-29 22:34:14 +09003438 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003439 load_TR_desc();
3440}
3441
Paolo Bonzini63129752021-03-02 14:40:39 -05003442static void pre_svm_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003443{
Paolo Bonzini63129752021-03-02 14:40:39 -05003444 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3445 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446
Cathy Averyaf18fa72021-01-12 11:43:12 -05003447 /*
Sean Christopherson44f1b552021-04-06 10:18:11 -07003448 * If the previous vmrun of the vmcb occurred on a different physical
3449 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3450 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3451 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003452 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
Cathy Avery193015a2021-01-12 11:43:13 -05003453 svm->current_vmcb->asid_generation = 0;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003454 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05003455 svm->current_vmcb->cpu = vcpu->cpu;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003456 }
3457
Paolo Bonzini63129752021-03-02 14:40:39 -05003458 if (sev_guest(vcpu->kvm))
3459 return pre_sev_run(svm, vcpu->cpu);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003460
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003461 /* FIXME: handle wraparound of asid_generation */
Cathy Avery193015a2021-01-12 11:43:13 -05003462 if (svm->current_vmcb->asid_generation != sd->asid_generation)
Tejun Heo0fe1e002009-10-29 22:34:14 +09003463 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003464}
3465
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003466static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3467{
3468 struct vcpu_svm *svm = to_svm(vcpu);
3469
3470 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3471 vcpu->arch.hflags |= HF_NMI_MASK;
Paolo Bonzini63129752021-03-02 14:40:39 -05003472 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003473 svm_set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003474 ++vcpu->stat.nmi_injections;
3475}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003477static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003478{
3479 struct vcpu_svm *svm = to_svm(vcpu);
3480
Joerg Roedel2af91942009-08-07 11:49:28 +02003481 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003482
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003483 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3484 ++vcpu->stat.irq_injections;
3485
Alexander Graf219b65d2009-06-15 15:21:25 +02003486 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3487 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003488}
3489
Jason Baronb6a7cc32021-01-14 22:27:54 -05003490static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003491{
3492 struct vcpu_svm *svm = to_svm(vcpu);
3493
Tom Lendackyf1c63662020-12-14 10:29:50 -05003494 /*
3495 * SEV-ES guests must always keep the CR intercepts cleared. CR
3496 * tracking is done using the CR write traps.
3497 */
3498 if (sev_es_guest(vcpu->kvm))
3499 return;
3500
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003501 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003502 return;
3503
Babu Moger830bd712020-09-11 14:28:50 -05003504 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
Radim Krčmář596f3142014-03-11 19:11:18 +01003505
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003506 if (irr == -1)
3507 return;
3508
3509 if (tpr >= irr)
Babu Moger830bd712020-09-11 14:28:50 -05003510 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003511}
3512
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003513bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003514{
3515 struct vcpu_svm *svm = to_svm(vcpu);
3516 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson88c604b2020-04-22 19:25:41 -07003517 bool ret;
Cathy Avery9c3d3702020-04-14 16:11:06 -04003518
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003519 if (!gif_set(svm))
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -04003520 return true;
3521
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003522 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3523 return false;
3524
3525 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05003526 (vcpu->arch.hflags & HF_NMI_MASK);
Joerg Roedel924584c2010-04-22 12:33:07 +02003527
3528 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003529}
3530
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003531static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003532{
3533 struct vcpu_svm *svm = to_svm(vcpu);
3534 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003535 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003536
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003537 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3538 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003539 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003540
3541 return !svm_nmi_blocked(vcpu);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003542}
3543
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003544static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3545{
Paolo Bonzini63129752021-03-02 14:40:39 -05003546 return !!(vcpu->arch.hflags & HF_NMI_MASK);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003547}
3548
3549static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3550{
3551 struct vcpu_svm *svm = to_svm(vcpu);
3552
3553 if (masked) {
Paolo Bonzini63129752021-03-02 14:40:39 -05003554 vcpu->arch.hflags |= HF_NMI_MASK;
3555 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003556 svm_set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003557 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05003558 vcpu->arch.hflags &= ~HF_NMI_MASK;
3559 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003560 svm_clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003561 }
3562}
3563
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003564bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
Gleb Natapov78646122009-03-23 12:12:11 +02003565{
3566 struct vcpu_svm *svm = to_svm(vcpu);
3567 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003568
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003569 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003570 return true;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003571
Paolo Bonzini63129752021-03-02 14:40:39 -05003572 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyf1c63662020-12-14 10:29:50 -05003573 /*
3574 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3575 * bit to determine the state of the IF flag.
3576 */
3577 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3578 return true;
3579 } else if (is_guest_mode(vcpu)) {
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003580 /* As long as interrupts are being delivered... */
Paolo Bonzinie9fd7612020-05-13 13:28:23 -04003581 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
Cathy Avery4995a362021-01-13 07:07:52 -05003582 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003583 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3584 return true;
3585
3586 /* ... vmexits aren't blocked by the interrupt shadow */
3587 if (nested_exit_on_intr(svm))
3588 return false;
3589 } else {
3590 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3591 return true;
3592 }
3593
3594 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003595}
3596
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003597static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003598{
3599 struct vcpu_svm *svm = to_svm(vcpu);
3600 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003601 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003602
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003603 /*
3604 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3605 * e.g. if the IRQ arrived asynchronously after checking nested events.
3606 */
3607 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003608 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003609
3610 return !svm_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02003611}
3612
Jason Baronb6a7cc32021-01-14 22:27:54 -05003613static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003614{
Alexander Graf219b65d2009-06-15 15:21:25 +02003615 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003616
Joerg Roedele0231712010-02-24 18:59:10 +01003617 /*
3618 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3619 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3620 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003621 * we'll get the vintr intercept. However, if the vGIF feature is
3622 * enabled, the STGI interception will not occur. Enable the irq
3623 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003624 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003625 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003626 /*
3627 * IRQ window is not needed when AVIC is enabled,
3628 * unless we have pending ExtINT since it cannot be injected
3629 * via AVIC. In such case, we need to temporarily disable AVIC,
3630 * and fallback to injecting IRQ via V_IRQ.
3631 */
Maxim Levitsky30eed562021-08-10 23:52:47 +03003632 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN);
Alexander Graf219b65d2009-06-15 15:21:25 +02003633 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003634 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003635}
3636
Jason Baronb6a7cc32021-01-14 22:27:54 -05003637static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003639 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003640
Paolo Bonzini63129752021-03-02 14:40:39 -05003641 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003642 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003643
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003644 if (!gif_set(svm)) {
3645 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02003646 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003647 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003648 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003649
Joerg Roedele0231712010-02-24 18:59:10 +01003650 /*
3651 * Something prevents NMI from been injected. Single step over possible
3652 * problem (IRET or exception injection or interrupt shadow)
3653 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003654 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003655 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003656 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003657}
3658
Izik Eiduscbc94022007-10-25 00:29:55 +02003659static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3660{
3661 return 0;
3662}
3663
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003664static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3665{
3666 return 0;
3667}
3668
Sean Christophersonf55ac302020-03-20 14:28:12 -07003669void svm_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003670{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003671 struct vcpu_svm *svm = to_svm(vcpu);
3672
Sean Christopherson4a41e432020-03-20 14:28:17 -07003673 /*
3674 * Flush only the current ASID even if the TLB flush was invoked via
3675 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3676 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3677 * unconditionally does a TLB flush on both nested VM-Enter and nested
3678 * VM-Exit (via kvm_mmu_reset_context()).
3679 */
Joerg Roedel38e5e922010-12-03 15:25:16 +01003680 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3681 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3682 else
Cathy Avery193015a2021-01-12 11:43:13 -05003683 svm->current_vmcb->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003684}
3685
Junaid Shahidfaff8752018-06-29 13:10:05 -07003686static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3687{
3688 struct vcpu_svm *svm = to_svm(vcpu);
3689
3690 invlpga(gva, svm->vmcb->control.asid);
3691}
3692
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003693static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3694{
3695 struct vcpu_svm *svm = to_svm(vcpu);
3696
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003697 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003698 return;
3699
Babu Moger830bd712020-09-11 14:28:50 -05003700 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003701 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003702 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003703 }
3704}
3705
Joerg Roedel649d6862008-04-16 16:51:15 +02003706static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3707{
3708 struct vcpu_svm *svm = to_svm(vcpu);
3709 u64 cr8;
3710
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003711 if (nested_svm_virtualize_tpr(vcpu) ||
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003712 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003713 return;
3714
Joerg Roedel649d6862008-04-16 16:51:15 +02003715 cr8 = kvm_get_cr8(vcpu);
3716 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3717 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3718}
3719
Paolo Bonzini63129752021-03-02 14:40:39 -05003720static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003721{
Paolo Bonzini63129752021-03-02 14:40:39 -05003722 struct vcpu_svm *svm = to_svm(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003723 u8 vector;
3724 int type;
3725 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003726 unsigned int3_injected = svm->int3_injected;
3727
3728 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003729
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003730 /*
3731 * If we've made progress since setting HF_IRET_MASK, we've
3732 * executed an IRET and can allow NMI injection.
3733 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003734 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3735 (sev_es_guest(vcpu->kvm) ||
3736 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3737 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3738 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003739 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003740
Paolo Bonzini63129752021-03-02 14:40:39 -05003741 vcpu->arch.nmi_injected = false;
3742 kvm_clear_exception_queue(vcpu);
3743 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003744
3745 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3746 return;
3747
Paolo Bonzini63129752021-03-02 14:40:39 -05003748 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003749
Gleb Natapov9222be12009-04-23 17:14:37 +03003750 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3751 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3752
3753 switch (type) {
3754 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05003755 vcpu->arch.nmi_injected = true;
Gleb Natapov9222be12009-04-23 17:14:37 +03003756 break;
3757 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003758 /*
Tom Lendackyf1c63662020-12-14 10:29:50 -05003759 * Never re-inject a #VC exception.
3760 */
3761 if (vector == X86_TRAP_VC)
3762 break;
3763
3764 /*
Jan Kiszka66b71382010-02-23 17:47:56 +01003765 * In case of software exceptions, do not reinject the vector,
3766 * but re-execute the instruction instead. Rewind RIP first
3767 * if we emulated INT3 before.
3768 */
3769 if (kvm_exception_is_soft(vector)) {
3770 if (vector == BP_VECTOR && int3_injected &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003771 kvm_is_linear_rip(vcpu, svm->int3_rip))
3772 kvm_rip_write(vcpu,
3773 kvm_rip_read(vcpu) - int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003774 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003775 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003776 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3777 u32 err = svm->vmcb->control.exit_int_info_err;
Paolo Bonzini63129752021-03-02 14:40:39 -05003778 kvm_requeue_exception_e(vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003779
3780 } else
Paolo Bonzini63129752021-03-02 14:40:39 -05003781 kvm_requeue_exception(vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003782 break;
3783 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05003784 kvm_queue_interrupt(vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003785 break;
3786 default:
3787 break;
3788 }
3789}
3790
Avi Kivityb463a6f2010-07-20 15:06:17 +03003791static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3792{
3793 struct vcpu_svm *svm = to_svm(vcpu);
3794 struct vmcb_control_area *control = &svm->vmcb->control;
3795
3796 control->exit_int_info = control->event_inj;
3797 control->exit_int_info_err = control->event_inj_err;
3798 control->event_inj = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003799 svm_complete_interrupts(vcpu);
Avi Kivityb463a6f2010-07-20 15:06:17 +03003800}
3801
Wanpeng Li404d5d72020-04-28 14:23:25 +08003802static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003803{
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003804 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003805 to_svm(vcpu)->vmcb->control.exit_info_1)
3806 return handle_fastpath_set_msr_irqoff(vcpu);
3807
3808 return EXIT_FASTPATH_NONE;
3809}
3810
Paolo Bonzini63129752021-03-02 14:40:39 -05003811static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
Thomas Gleixner135961e2020-07-08 21:51:58 +02003812{
Paolo Bonzini63129752021-03-02 14:40:39 -05003813 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christophersond1788192021-04-06 10:18:09 -07003814 unsigned long vmcb_pa = svm->current_vmcb->pa;
Paolo Bonzini63129752021-03-02 14:40:39 -05003815
Sean Christophersonbc908e02021-05-04 17:27:35 -07003816 kvm_guest_enter_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003817
Paolo Bonzini63129752021-03-02 14:40:39 -05003818 if (sev_es_guest(vcpu->kvm)) {
Sean Christophersond1788192021-04-06 10:18:09 -07003819 __svm_sev_es_vcpu_run(vmcb_pa);
Tom Lendacky16809ec2020-12-10 11:10:08 -06003820 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06003821 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3822
Sean Christophersond1788192021-04-06 10:18:09 -07003823 /*
3824 * Use a single vmcb (vmcb01 because it's always valid) for
3825 * context switching guest state via VMLOAD/VMSAVE, that way
3826 * the state doesn't need to be copied between vmcb01 and
3827 * vmcb02 when switching vmcbs for nested virtualization.
3828 */
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003829 vmload(svm->vmcb01.pa);
Sean Christophersond1788192021-04-06 10:18:09 -07003830 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003831 vmsave(svm->vmcb01.pa);
Thomas Gleixner135961e2020-07-08 21:51:58 +02003832
Michael Rothe79b91b2021-02-02 13:01:24 -06003833 vmload(__sme_page_pa(sd->save_area));
Tom Lendacky16809ec2020-12-10 11:10:08 -06003834 }
Thomas Gleixner135961e2020-07-08 21:51:58 +02003835
Sean Christophersonbc908e02021-05-04 17:27:35 -07003836 kvm_guest_exit_irqoff();
Thomas Gleixner135961e2020-07-08 21:51:58 +02003837}
3838
Qian Caib95273f2020-04-15 11:37:09 -04003839static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003840{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003841 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003842
Lorenzo Bresciad95df952020-12-23 14:45:07 +00003843 trace_kvm_entry(vcpu);
3844
Joerg Roedel2041a062010-04-22 12:33:08 +02003845 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3846 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3847 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3848
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003849 /*
Ladi Proseka12713c2017-06-21 09:07:00 +02003850 * Disable singlestep if we're injecting an interrupt/exception.
3851 * We don't want our modified rflags to be pushed on the stack where
3852 * we might not be able to easily reset them if we disabled NMI
3853 * singlestep later.
3854 */
3855 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3856 /*
3857 * Event injection happens before external interrupts cause a
3858 * vmexit and interrupts are disabled here, so smp_send_reschedule
3859 * is enough to force an immediate vmexit.
3860 */
3861 disable_nmi_singlestep(svm);
3862 smp_send_reschedule(vcpu->cpu);
3863 }
3864
Paolo Bonzini63129752021-03-02 14:40:39 -05003865 pre_svm_run(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003866
Maxim Levitsky4628efc2021-08-10 23:52:45 +03003867 WARN_ON_ONCE(kvm_apicv_activated(vcpu->kvm) != kvm_vcpu_apicv_active(vcpu));
3868
Joerg Roedel649d6862008-04-16 16:51:15 +02003869 sync_lapic_to_cr8(vcpu);
3870
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04003871 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3872 svm->vmcb->control.asid = svm->asid;
3873 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3874 }
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003875 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003876
Vineeth Pillai11836462021-06-03 15:14:40 +00003877 svm_hv_update_vp_id(svm->vmcb, vcpu);
3878
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003879 /*
3880 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3881 * of a #DB.
3882 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003883 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003884 svm_set_dr6(svm, vcpu->arch.dr6);
3885 else
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08003886 svm_set_dr6(svm, DR6_ACTIVE_LOW);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003887
Avi Kivity04d2cc72007-09-10 18:10:54 +03003888 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003889 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003890
Wanpeng Li010fd372020-09-10 17:50:41 +08003891 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003892
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003893 /*
3894 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3895 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3896 * is no need to worry about the conditional branch over the wrmsr
3897 * being speculatively taken.
3898 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003899 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3900 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003901
Paolo Bonzini63129752021-03-02 14:40:39 -05003902 svm_vcpu_enter_exit(vcpu);
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003903
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003904 /*
3905 * We do not use IBRS in the kernel. If this vCPU has used the
3906 * SPEC_CTRL MSR it may have left it on; save the value and
3907 * turn it off. This is much more efficient than blindly adding
3908 * it to the atomic save/restore list. Especially as the former
3909 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3910 *
3911 * For non-nested case:
3912 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3913 * save it.
3914 *
3915 * For nested case:
3916 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3917 * save it.
3918 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003919 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3920 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003921 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003922
Paolo Bonzini63129752021-03-02 14:40:39 -05003923 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky16809ec2020-12-10 11:10:08 -06003924 reload_tss(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925
Babu Mogerd00b99c2021-02-17 10:56:04 -05003926 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3927 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003928
Paolo Bonzini63129752021-03-02 14:40:39 -05003929 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003930 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3931 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3932 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3933 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3934 }
Avi Kivity13c34e02010-10-21 12:20:31 +02003935
Joerg Roedel3781c012011-01-14 16:45:02 +01003936 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003937 kvm_before_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003938
Aaron Lewis139a12c2019-10-21 16:30:25 -07003939 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003940 stgi();
3941
3942 /* Any pending NMI will happen here */
3943
3944 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003945 kvm_after_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003946
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003947 sync_cr8_to_lapic(vcpu);
3948
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003949 svm->next_rip = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003950 if (is_guest_mode(vcpu)) {
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05003951 nested_sync_control_from_vmcb02(svm);
Krish Sadhukhanb93af022021-06-09 14:03:38 -04003952
3953 /* Track VMRUNs that have made past consistency checking */
3954 if (svm->nested.nested_run_pending &&
3955 svm->vmcb->control.exit_code != SVM_EXIT_ERR)
3956 ++vcpu->stat.nested_run;
3957
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -04003958 svm->nested.nested_run_pending = 0;
3959 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003960
Joerg Roedel38e5e922010-12-03 15:25:16 +01003961 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Wanpeng Lie42c6822020-09-12 02:16:39 -04003962 vmcb_mark_all_clean(svm->vmcb);
Joerg Roedel38e5e922010-12-03 15:25:16 +01003963
Gleb Natapov631bc482010-10-14 11:22:52 +02003964 /* if exit due to PF check for async PF */
3965 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003966 vcpu->arch.apf.host_apf_flags =
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003967 kvm_read_and_reset_apf_flags();
Gleb Natapov631bc482010-10-14 11:22:52 +02003968
Maxim Levitsky329675d2021-06-07 12:02:01 +03003969 if (npt_enabled)
3970 kvm_register_clear_available(vcpu, VCPU_EXREG_PDPTR);
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003971
3972 /*
3973 * We need to handle MC intercepts here before the vcpu has a chance to
3974 * change the physical cpu
3975 */
3976 if (unlikely(svm->vmcb->control.exit_code ==
3977 SVM_EXIT_EXCP_BASE + MC_VECTOR))
Paolo Bonzini63129752021-03-02 14:40:39 -05003978 svm_handle_mce(vcpu);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003979
Paolo Bonzini63129752021-03-02 14:40:39 -05003980 svm_complete_interrupts(vcpu);
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003981
3982 if (is_guest_mode(vcpu))
3983 return EXIT_FASTPATH_NONE;
3984
3985 return svm_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003986}
3987
Sean Christophersone83bc092021-03-05 10:31:13 -08003988static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
Sean Christopherson2a40b902020-07-15 20:41:18 -07003989 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003990{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003991 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003992 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003993
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003994 if (npt_enabled) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003995 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
Joerg Roedel06e78522020-06-25 10:03:23 +02003996 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003997
Vineeth Pillai1e0c7d42021-06-03 15:14:38 +00003998 hv_track_root_tdp(vcpu, root_hpa);
3999
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01004000 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
Paolo Bonzini978ce582020-05-20 08:37:37 -04004001 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
4002 return;
4003 cr3 = vcpu->arch.cr3;
Sean Christophersone83bc092021-03-05 10:31:13 -08004004 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson4a986232021-03-09 14:42:07 -08004005 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
Sean Christophersone83bc092021-03-05 10:31:13 -08004006 } else {
4007 /* PCID in the guest should be impossible with a 32-bit MMU. */
4008 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
4009 cr3 = root_hpa;
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01004010 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004011
Paolo Bonzini978ce582020-05-20 08:37:37 -04004012 svm->vmcb->save.cr3 = cr3;
Joerg Roedel06e78522020-06-25 10:03:23 +02004013 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004014}
4015
Avi Kivity6aa8b732006-12-10 02:21:36 -08004016static int is_disabled(void)
4017{
Joerg Roedel6031a612007-06-22 12:29:50 +03004018 u64 vm_cr;
4019
4020 rdmsrl(MSR_VM_CR, vm_cr);
4021 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
4022 return 1;
4023
Avi Kivity6aa8b732006-12-10 02:21:36 -08004024 return 0;
4025}
4026
Ingo Molnar102d8322007-02-19 14:37:47 +02004027static void
4028svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
4029{
4030 /*
4031 * Patch in the VMMCALL instruction:
4032 */
4033 hypercall[0] = 0x0f;
4034 hypercall[1] = 0x01;
4035 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02004036}
4037
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004038static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03004039{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004040 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004041}
4042
Avi Kivity774ead32007-12-26 13:57:04 +02004043static bool svm_cpu_has_accelerated_tpr(void)
4044{
4045 return false;
4046}
4047
Tom Lendacky57194552020-12-10 11:10:00 -06004048/*
4049 * The kvm parameter can be NULL (module initialization, or invocation before
4050 * VM creation). Be sure to check the kvm parameter before using it.
4051 */
4052static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004053{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004054 switch (index) {
4055 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02004056 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004057 return false;
Tom Lendacky57194552020-12-10 11:10:00 -06004058 case MSR_IA32_SMBASE:
4059 /* SEV-ES guests do not support SMM, so report false */
4060 if (kvm && sev_es_guest(kvm))
4061 return false;
4062 break;
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01004063 default:
4064 break;
4065 }
4066
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004067 return true;
4068}
4069
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004070static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4071{
4072 return 0;
4073}
4074
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004075static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08004076{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004077 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger96308b02020-11-12 16:18:03 -06004078 struct kvm_cpuid_entry2 *best;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004079
Aaron Lewis72041602019-10-21 16:30:20 -07004080 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08004081 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07004082 boot_cpu_has(X86_FEATURE_XSAVES);
4083
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004084 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08004085 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
Paolo Bonzini63129752021-03-02 14:40:39 -05004086 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004087
Maxim Levitsky5228eb92021-09-14 18:48:24 +03004088 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
4089
Sean Christopherson3b195ac2021-05-04 10:17:22 -07004090 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05004091
Babu Moger96308b02020-11-12 16:18:03 -06004092 /* For sev guests, the memory encryption bit is not reserved in CR3. */
4093 if (sev_guest(vcpu->kvm)) {
4094 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
4095 if (best)
Sean Christophersonca29e142021-02-03 16:01:12 -08004096 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
Babu Moger96308b02020-11-12 16:18:03 -06004097 }
4098
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004099 if (kvm_vcpu_apicv_active(vcpu)) {
4100 /*
4101 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
4102 * is exposed to the guest, disable AVIC.
4103 */
4104 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
4105 kvm_request_apicv_update(vcpu->kvm, false,
4106 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004107
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004108 /*
4109 * Currently, AVIC does not work with nested virtualization.
4110 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
4111 */
4112 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4113 kvm_request_apicv_update(vcpu->kvm, false,
4114 APICV_INHIBIT_REASON_NESTED);
4115 }
Paolo Bonzini36e81942021-09-23 12:46:07 -04004116 init_vmcb_after_set_cpuid(vcpu);
Sheng Yang0e851882009-12-18 16:48:46 +08004117}
4118
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004119static bool svm_has_wbinvd_exit(void)
4120{
4121 return true;
4122}
4123
Joerg Roedel80612522011-04-04 12:39:33 +02004124#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004125 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004126#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004127 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004128#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004129 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004130
Mathias Krause09941fb2012-08-30 01:30:20 +02004131static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004132 u32 exit_code;
4133 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004134} x86_intercept_map[] = {
4135 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4136 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4137 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4138 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4139 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004140 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4141 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004142 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4143 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4144 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4145 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4146 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4147 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4148 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4149 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004150 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4151 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4152 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4153 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4154 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4155 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4156 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4157 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004158 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4159 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4160 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004161 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4162 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4163 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4164 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4165 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4166 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4167 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4168 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4169 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004170 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4171 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4172 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4173 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4174 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4175 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4176 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004177 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4178 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4179 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4180 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004181 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004182};
4183
Joerg Roedel80612522011-04-04 12:39:33 +02004184#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004185#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004186#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004187
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004188static int svm_check_intercept(struct kvm_vcpu *vcpu,
4189 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08004190 enum x86_intercept_stage stage,
4191 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004192{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004193 struct vcpu_svm *svm = to_svm(vcpu);
4194 int vmexit, ret = X86EMUL_CONTINUE;
4195 struct __x86_intercept icpt_info;
4196 struct vmcb *vmcb = svm->vmcb;
4197
4198 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4199 goto out;
4200
4201 icpt_info = x86_intercept_map[info->intercept];
4202
Avi Kivity40e19b52011-04-21 12:35:41 +03004203 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004204 goto out;
4205
4206 switch (icpt_info.exit_code) {
4207 case SVM_EXIT_READ_CR0:
4208 if (info->intercept == x86_intercept_cr_read)
4209 icpt_info.exit_code += info->modrm_reg;
4210 break;
4211 case SVM_EXIT_WRITE_CR0: {
4212 unsigned long cr0, val;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004213
4214 if (info->intercept == x86_intercept_cr_write)
4215 icpt_info.exit_code += info->modrm_reg;
4216
Jan Kiszka62baf442014-06-29 21:55:53 +02004217 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4218 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004219 break;
4220
Babu Mogerc62e2e92020-09-11 14:28:28 -05004221 if (!(vmcb_is_intercept(&svm->nested.ctl,
4222 INTERCEPT_SELECTIVE_CR0)))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004223 break;
4224
4225 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4226 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4227
4228 if (info->intercept == x86_intercept_lmsw) {
4229 cr0 &= 0xfUL;
4230 val &= 0xfUL;
4231 /* lmsw can't clear PE - catch this here */
4232 if (cr0 & X86_CR0_PE)
4233 val |= X86_CR0_PE;
4234 }
4235
4236 if (cr0 ^ val)
4237 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4238
4239 break;
4240 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004241 case SVM_EXIT_READ_DR0:
4242 case SVM_EXIT_WRITE_DR0:
4243 icpt_info.exit_code += info->modrm_reg;
4244 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004245 case SVM_EXIT_MSR:
4246 if (info->intercept == x86_intercept_wrmsr)
4247 vmcb->control.exit_info_1 = 1;
4248 else
4249 vmcb->control.exit_info_1 = 0;
4250 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004251 case SVM_EXIT_PAUSE:
4252 /*
4253 * We get this for NOP only, but pause
4254 * is rep not, check this here
4255 */
4256 if (info->rep_prefix != REPE_PREFIX)
4257 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004258 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004259 case SVM_EXIT_IOIO: {
4260 u64 exit_info;
4261 u32 bytes;
4262
Joerg Roedelf6511932011-04-04 12:39:35 +02004263 if (info->intercept == x86_intercept_in ||
4264 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004265 exit_info = ((info->src_val & 0xffff) << 16) |
4266 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004267 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004268 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004269 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004270 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004271 }
4272
4273 if (info->intercept == x86_intercept_outs ||
4274 info->intercept == x86_intercept_ins)
4275 exit_info |= SVM_IOIO_STR_MASK;
4276
4277 if (info->rep_prefix)
4278 exit_info |= SVM_IOIO_REP_MASK;
4279
4280 bytes = min(bytes, 4u);
4281
4282 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4283
4284 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4285
4286 vmcb->control.exit_info_1 = exit_info;
4287 vmcb->control.exit_info_2 = info->next_rip;
4288
4289 break;
4290 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004291 default:
4292 break;
4293 }
4294
Bandan Dasf1047652015-06-11 02:05:33 -04004295 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4296 if (static_cpu_has(X86_FEATURE_NRIPS))
4297 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004298 vmcb->control.exit_code = icpt_info.exit_code;
4299 vmexit = nested_svm_exit_handled(svm);
4300
4301 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4302 : X86EMUL_CONTINUE;
4303
4304out:
4305 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004306}
4307
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07004308static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004309{
Yang Zhanga547c6d2013-04-11 19:25:10 +08004310}
4311
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004312static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4313{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004314 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04004315 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004316}
4317
Borislav Petkov74f16902017-03-26 23:51:24 +02004318static void svm_setup_mce(struct kvm_vcpu *vcpu)
4319{
4320 /* [63:9] are reserved. */
4321 vcpu->arch.mcg_cap &= 0x1ff;
4322}
4323
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004324bool svm_smi_blocked(struct kvm_vcpu *vcpu)
Ladi Prosek72d7b372017-10-11 16:54:41 +02004325{
Ladi Prosek05cade72017-10-11 16:54:45 +02004326 struct vcpu_svm *svm = to_svm(vcpu);
4327
4328 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4329 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004330 return true;
4331
4332 return is_smm(vcpu);
4333}
4334
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004335static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004336{
4337 struct vcpu_svm *svm = to_svm(vcpu);
4338 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004339 return -EBUSY;
Ladi Prosek05cade72017-10-11 16:54:45 +02004340
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004341 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4342 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004343 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004344
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004345 return !svm_smi_blocked(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02004346}
4347
Sean Christophersonecc513e2021-06-09 11:56:19 -07004348static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004349{
Ladi Prosek05cade72017-10-11 16:54:45 +02004350 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004351 struct kvm_host_map map_save;
Ladi Prosek05cade72017-10-11 16:54:45 +02004352 int ret;
4353
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004354 if (!is_guest_mode(vcpu))
4355 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004356
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004357 /* FED8h - SVM Guest */
4358 put_smstate(u64, smstate, 0x7ed8, 1);
4359 /* FEE0h - SVM Guest VMCB Physical Address */
4360 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
Ladi Prosek05cade72017-10-11 16:54:45 +02004361
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004362 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4363 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4364 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004365
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004366 ret = nested_svm_vmexit(svm);
4367 if (ret)
4368 return ret;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004369
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004370 /*
4371 * KVM uses VMCB01 to store L1 host state while L2 runs but
4372 * VMCB01 is going to be used during SMM and thus the state will
4373 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
4374 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
4375 * format of the area is identical to guest save area offsetted
4376 * by 0x400 (matches the offset of 'struct vmcb_save_area'
4377 * within 'struct vmcb'). Note: HSAVE area may also be used by
4378 * L1 hypervisor to save additional host context (e.g. KVM does
4379 * that, see svm_prepare_guest_switch()) which must be
4380 * preserved.
4381 */
4382 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
4383 &map_save) == -EINVAL)
4384 return 1;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004385
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004386 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004387
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004388 svm_copy_vmrun_state(map_save.hva + 0x400,
4389 &svm->vmcb01.ptr->save);
4390
4391 kvm_vcpu_unmap(vcpu, &map_save, true);
Ladi Prosek0234bf82017-10-11 16:54:40 +02004392 return 0;
4393}
4394
Sean Christophersonecc513e2021-06-09 11:56:19 -07004395static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004396{
Ladi Prosek05cade72017-10-11 16:54:45 +02004397 struct vcpu_svm *svm = to_svm(vcpu);
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004398 struct kvm_host_map map, map_save;
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004399 u64 saved_efer, vmcb12_gpa;
4400 struct vmcb *vmcb12;
4401 int ret;
Ladi Prosek05cade72017-10-11 16:54:45 +02004402
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004403 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
4404 return 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004405
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004406 /* Non-zero if SMI arrived while vCPU was in guest mode. */
4407 if (!GET_SMSTATE(u64, smstate, 0x7ed8))
4408 return 0;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004409
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004410 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4411 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004412
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004413 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4414 if (!(saved_efer & EFER_SVME))
4415 return 1;
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004416
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004417 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4418 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4419 return 1;
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03004420
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004421 ret = 1;
4422 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
4423 goto unmap_map;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004424
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004425 if (svm_allocate_nested(svm))
4426 goto unmap_save;
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004427
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004428 /*
4429 * Restore L1 host state from L1 HSAVE area as VMCB01 was
4430 * used during SMM (see svm_enter_smm())
4431 */
Vitaly Kuznetsov37be4072021-06-28 12:44:23 +02004432
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004433 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
Maxim Levitskye2e6e442021-09-13 17:09:49 +03004434
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004435 /*
4436 * Enter the nested guest now
4437 */
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004438
Maxim Levitsky136a55c2021-09-22 10:28:43 -04004439 vmcb12 = map.hva;
4440 nested_load_control_from_vmcb12(svm, &vmcb12->control);
4441 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
4442
4443unmap_save:
4444 kvm_vcpu_unmap(vcpu, &map_save, true);
4445unmap_map:
4446 kvm_vcpu_unmap(vcpu, &map, true);
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004447 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004448}
4449
Jason Baronb6a7cc32021-01-14 22:27:54 -05004450static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004451{
4452 struct vcpu_svm *svm = to_svm(vcpu);
4453
4454 if (!gif_set(svm)) {
4455 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02004456 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004457 /* STGI will cause a vm exit */
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004458 } else {
4459 /* We must be in SMM; RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004460 }
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004461}
4462
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004463static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004464{
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004465 bool smep, smap, is_user;
4466 unsigned long cr4;
Paolo Bonzinie72436b2020-04-17 12:21:06 -04004467
4468 /*
Tom Lendackybc624d92020-12-10 11:09:44 -06004469 * When the guest is an SEV-ES guest, emulation is not possible.
4470 */
4471 if (sev_es_guest(vcpu->kvm))
4472 return false;
4473
4474 /*
Liran Alon118154b2019-07-17 02:56:58 +03004475 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4476 *
4477 * Errata:
4478 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4479 * possible that CPU microcode implementing DecodeAssist will fail
4480 * to read bytes of instruction which caused #NPF. In this case,
4481 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4482 * return 0 instead of the correct guest instruction bytes.
4483 *
4484 * This happens because CPU microcode reading instruction bytes
4485 * uses a special opcode which attempts to read data using CPL=0
Ingo Molnard9f6e122021-03-18 15:28:01 +01004486 * privileges. The microcode reads CS:RIP and if it hits a SMAP
Liran Alon118154b2019-07-17 02:56:58 +03004487 * fault, it gives up and returns no instruction bytes.
4488 *
4489 * Detection:
4490 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4491 * returned 0 in GuestIntrBytes field of the VMCB.
4492 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4493 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4494 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4495 * a SMEP fault instead of #NPF).
4496 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4497 * As most guests enable SMAP if they have also enabled SMEP, use above
4498 * logic in order to attempt minimize false-positive of detecting errata
4499 * while still preserving all cases semantic correctness.
4500 *
4501 * Workaround:
4502 * To determine what instruction the guest was executing, the hypervisor
4503 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004504 *
4505 * In non SEV guest, hypervisor will be able to read the guest
4506 * memory to decode the instruction pointer when insn_len is zero
4507 * so we return true to indicate that decoding is possible.
4508 *
4509 * But in the SEV guest, the guest memory is encrypted with the
4510 * guest specific key and hypervisor will not be able to decode the
4511 * instruction pointer so we will not able to workaround it. Lets
4512 * print the error and request to kill the guest.
4513 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004514 if (likely(!insn || insn_len))
4515 return true;
4516
4517 /*
4518 * If RIP is invalid, go ahead with emulation which will cause an
4519 * internal error exit.
4520 */
4521 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4522 return true;
4523
4524 cr4 = kvm_read_cr4(vcpu);
4525 smep = cr4 & X86_CR4_SMEP;
4526 smap = cr4 & X86_CR4_SMAP;
4527 is_user = svm_get_cpl(vcpu) == 3;
Liran Alon118154b2019-07-17 02:56:58 +03004528 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004529 if (!sev_guest(vcpu->kvm))
4530 return true;
4531
Liran Alon118154b2019-07-17 02:56:58 +03004532 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004533 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4534 }
4535
4536 return false;
4537}
4538
Liran Alon4b9852f2019-08-26 13:24:49 +03004539static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4540{
4541 struct vcpu_svm *svm = to_svm(vcpu);
4542
4543 /*
4544 * TODO: Last condition latch INIT signals on vCPU when
4545 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
Paolo Bonzini33b22172020-04-17 10:24:18 -04004546 * To properly emulate the INIT intercept,
4547 * svm_check_nested_events() should call nested_svm_vmexit()
4548 * if an INIT signal is pending.
Liran Alon4b9852f2019-08-26 13:24:49 +03004549 */
4550 return !gif_set(svm) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05004551 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
Liran Alon4b9852f2019-08-26 13:24:49 +03004552}
4553
Tom Lendacky647daca2021-01-04 14:20:01 -06004554static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4555{
4556 if (!sev_es_guest(vcpu->kvm))
4557 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4558
4559 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4560}
4561
Joerg Roedeleaf78262020-03-24 10:41:54 +01004562static void svm_vm_destroy(struct kvm *kvm)
4563{
4564 avic_vm_destroy(kvm);
4565 sev_vm_destroy(kvm);
4566}
4567
4568static int svm_vm_init(struct kvm *kvm)
4569{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004570 if (!pause_filter_count || !pause_filter_thresh)
4571 kvm->arch.pause_in_guest = true;
4572
Vitaly Kuznetsovfdf513e2021-06-09 17:09:08 +02004573 if (enable_apicv) {
Joerg Roedeleaf78262020-03-24 10:41:54 +01004574 int ret = avic_vm_init(kvm);
4575 if (ret)
4576 return ret;
4577 }
4578
Joerg Roedeleaf78262020-03-24 10:41:54 +01004579 return 0;
4580}
4581
Sean Christopherson9c14ee22020-03-21 13:26:03 -07004582static struct kvm_x86_ops svm_x86_ops __initdata = {
Li RongQingdd58f3c2020-02-23 16:13:12 +08004583 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004584 .hardware_enable = svm_hardware_enable,
4585 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004586 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02004587 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004588
4589 .vcpu_create = svm_create_vcpu,
4590 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004591 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004592
Sean Christopherson562b6b02020-01-26 16:41:13 -08004593 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06004594 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004595 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004596
Avi Kivity04d2cc72007-09-10 18:10:54 +03004597 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004598 .vcpu_load = svm_vcpu_load,
4599 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05004600 .vcpu_blocking = svm_vcpu_blocking,
4601 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004602
Jason Baronb6a7cc32021-01-14 22:27:54 -05004603 .update_exception_bitmap = svm_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06004604 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004605 .get_msr = svm_get_msr,
4606 .set_msr = svm_set_msr,
4607 .get_segment_base = svm_get_segment_base,
4608 .get_segment = svm_get_segment,
4609 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004610 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004611 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004612 .set_cr0 = svm_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004613 .is_valid_cr4 = svm_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004614 .set_cr4 = svm_set_cr4,
4615 .set_efer = svm_set_efer,
4616 .get_idt = svm_get_idt,
4617 .set_idt = svm_set_idt,
4618 .get_gdt = svm_get_gdt,
4619 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004620 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004621 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004622 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004623 .get_rflags = svm_get_rflags,
4624 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004625
Sean Christopherson77809382020-03-20 14:28:18 -07004626 .tlb_flush_all = svm_flush_tlb,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004627 .tlb_flush_current = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07004628 .tlb_flush_gva = svm_flush_tlb_gva,
Sean Christopherson72b38322020-03-20 14:28:13 -07004629 .tlb_flush_guest = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004630
Avi Kivity6aa8b732006-12-10 02:21:36 -08004631 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004632 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004633 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08004634 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004635 .set_interrupt_shadow = svm_set_interrupt_shadow,
4636 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004637 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004638 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004639 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004640 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004641 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004642 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004643 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004644 .get_nmi_mask = svm_get_nmi_mask,
4645 .set_nmi_mask = svm_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004646 .enable_nmi_window = svm_enable_nmi_window,
4647 .enable_irq_window = svm_enable_irq_window,
4648 .update_cr8_intercept = svm_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04004649 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004650 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06004651 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004652 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004653 .hwapic_irr_update = svm_hwapic_irr_update,
4654 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02004655 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004656 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02004657
4658 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004659 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004660 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004661
Avi Kivity586f9602010-11-18 13:09:54 +02004662 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004663
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004664 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004665
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004666 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004667
Ilias Stamatis307a94c2021-05-26 19:44:13 +01004668 .get_l2_tsc_offset = svm_get_l2_tsc_offset,
4669 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
Ilias Stamatisedcfe542021-05-26 19:44:15 +01004670 .write_tsc_offset = svm_write_tsc_offset,
Ilias Stamatis1ab92872021-06-07 11:54:38 +01004671 .write_tsc_multiplier = svm_write_tsc_multiplier,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004672
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004673 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004674
4675 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07004676 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004677
Sean Christophersond264ee02018-08-27 15:21:12 -07004678 .request_immediate_exit = __kvm_request_immediate_exit,
4679
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004680 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004681
4682 .pmu_ops = &amd_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04004683 .nested_ops = &svm_nested_ops,
4684
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004685 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08004686 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004687 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02004688 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004689
Ladi Prosek72d7b372017-10-11 16:54:41 +02004690 .smi_allowed = svm_smi_allowed,
Sean Christophersonecc513e2021-06-09 11:56:19 -07004691 .enter_smm = svm_enter_smm,
4692 .leave_smm = svm_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004693 .enable_smi_window = svm_enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004694
4695 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004696 .mem_enc_reg_region = svm_register_enc_region,
4697 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004698
Nathan Tempelman54526d12021-04-08 22:32:14 +00004699 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4700
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004701 .can_emulate_instruction = svm_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03004702
4703 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Alexander Graffd6fa732020-09-25 16:34:19 +02004704
4705 .msr_filter_changed = svm_msr_filter_changed,
Tom Lendackyf1c63662020-12-14 10:29:50 -05004706 .complete_emulated_msr = svm_complete_emulated_msr,
Tom Lendacky647daca2021-01-04 14:20:01 -06004707
4708 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004709};
4710
Sean Christophersond008dfd2020-03-21 13:25:56 -07004711static struct kvm_x86_init_ops svm_init_ops __initdata = {
4712 .cpu_has_kvm_support = has_svm,
4713 .disabled_by_bios = is_disabled,
4714 .hardware_setup = svm_hardware_setup,
4715 .check_processor_compatibility = svm_check_processor_compat,
4716
4717 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004718};
4719
4720static int __init svm_init(void)
4721{
Tom Lendackyd07f46f2020-09-07 15:15:03 +02004722 __unused_size_checks();
4723
Sean Christophersond008dfd2020-03-21 13:25:56 -07004724 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004725 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726}
4727
4728static void __exit svm_exit(void)
4729{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004730 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004731}
4732
4733module_init(svm_init)
4734module_exit(svm_exit)