blob: d13b72bfb736bd0ba49e9efeb8500d5063ba9e2a [file] [log] [blame]
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001#define pr_fmt(fmt) "SVM: " fmt
2
Avi Kivityedf88412007-12-16 11:02:48 +02003#include <linux/kvm_host.h>
4
Eddie Dong85f455f2007-07-06 12:20:49 +03005#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +08006#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03007#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +02008#include "x86.h"
Julian Stecklina66f7b722012-12-05 15:26:19 +01009#include "cpuid.h"
Wei Huang25462f72015-06-19 15:45:05 +020010#include "pmu.h"
Avi Kivitye4956062007-06-28 14:15:57 -040011
Avi Kivity6aa8b732006-12-10 02:21:36 -080012#include <linux/module.h>
Josh Triplettae759542012-03-28 11:32:28 -070013#include <linux/mod_devicetable.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020014#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080015#include <linux/vmalloc.h>
16#include <linux/highmem.h>
Joerg Roedelef0f6492020-03-31 12:17:38 -040017#include <linux/amd-iommu.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040018#include <linux/sched.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040019#include <linux/trace_events.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -050021#include <linux/hashtable.h>
Julien Thierry00089c02020-09-04 16:30:25 +010022#include <linux/objtool.h>
Brijesh Singhe9df0942017-12-04 10:57:33 -060023#include <linux/psp-sev.h>
Brijesh Singh1654efc2017-12-04 10:57:34 -060024#include <linux/file.h>
Brijesh Singh89c50582017-12-04 10:57:35 -060025#include <linux/pagemap.h>
26#include <linux/swap.h>
Tom Lendacky33af3a72019-10-03 21:17:48 +000027#include <linux/rwsem.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080028
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -050029#include <asm/apic.h>
Joerg Roedel1018faa2012-02-29 14:57:32 +010030#include <asm/perf_event.h>
Joerg Roedel67ec6602010-05-17 14:43:35 +020031#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032#include <asm/desc.h>
Paolo Bonzinifacb0132014-02-21 10:32:27 +010033#include <asm/debugreg.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -050035#include <asm/irq_remapping.h>
Thomas Gleixner28a27752018-04-29 15:01:37 +020036#include <asm/spec-ctrl.h>
Thomas Gleixnerba5bade2020-03-20 14:13:46 +010037#include <asm/cpu_device_id.h>
Tom Lendackyf1c63662020-12-14 10:29:50 -050038#include <asm/traps.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080039
Eduardo Habkost63d11422008-11-17 19:03:20 -020040#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030041#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020042
Joerg Roedel883b0a92020-03-24 10:41:52 +010043#include "svm.h"
Sean Christopherson35a78312020-12-30 16:27:00 -080044#include "svm_ops.h"
Joerg Roedel883b0a92020-03-24 10:41:52 +010045
Avi Kivity4ecac3f2008-05-13 13:23:38 +030046#define __ex(x) __kvm_handle_fault_on_reboot(x)
47
Avi Kivity6aa8b732006-12-10 02:21:36 -080048MODULE_AUTHOR("Qumranet");
49MODULE_LICENSE("GPL");
50
Valdis Klētnieks575b2552020-02-27 21:49:52 -050051#ifdef MODULE
Josh Triplettae759542012-03-28 11:32:28 -070052static const struct x86_cpu_id svm_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010053 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
Josh Triplettae759542012-03-28 11:32:28 -070054 {}
55};
56MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050057#endif
Josh Triplettae759542012-03-28 11:32:28 -070058
Avi Kivity6aa8b732006-12-10 02:21:36 -080059#define SEG_TYPE_LDT 2
60#define SEG_TYPE_BUSY_TSS16 3
61
Andre Przywara6bc31bd2010-04-11 23:07:28 +020062#define SVM_FEATURE_LBRV (1 << 1)
63#define SVM_FEATURE_SVML (1 << 2)
Andre Przywaraddce97a2010-12-21 11:12:03 +010064#define SVM_FEATURE_TSC_RATE (1 << 4)
65#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
66#define SVM_FEATURE_FLUSH_ASID (1 << 6)
67#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020068#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030069
Joerg Roedel24e09cb2008-02-13 18:58:47 +010070#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
71
Joerg Roedelfbc0db72011-03-25 09:44:46 +010072#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010073#define TSC_RATIO_MIN 0x0000000000000001ULL
74#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010075
Joerg Roedel67ec6602010-05-17 14:43:35 +020076static bool erratum_383_found __read_mostly;
77
Joerg Roedel883b0a92020-03-24 10:41:52 +010078u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
Joerg Roedel323c3d82010-03-01 15:34:37 +010079
Boris Ostrovsky2b036c62012-01-09 14:00:35 -050080/*
81 * Set osvw_len to higher value when updated Revision Guides
82 * are published and we know what the new status bits are
83 */
84static uint64_t osvw_len = 4, osvw_status;
85
Joerg Roedelfbc0db72011-03-25 09:44:46 +010086static DEFINE_PER_CPU(u64, current_tsc_ratio);
87#define TSC_RATIO_DEFAULT 0x0100000000ULL
88
Mathias Krause09941fb2012-08-30 01:30:20 +020089static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +010090 u32 index; /* Index of the MSR */
Tom Lendacky376c6d22020-12-10 11:10:06 -060091 bool always; /* True if intercept is initially cleared */
Alexander Graffd6fa732020-09-25 16:34:19 +020092} direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
Brian Gerst8c065852010-07-17 09:03:26 -040093 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010094 { .index = MSR_IA32_SYSENTER_CS, .always = true },
Maxim Levitskyadc2a232021-04-01 14:19:28 +030095 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
96 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010097#ifdef CONFIG_X86_64
98 { .index = MSR_GS_BASE, .always = true },
99 { .index = MSR_FS_BASE, .always = true },
100 { .index = MSR_KERNEL_GS_BASE, .always = true },
101 { .index = MSR_LSTAR, .always = true },
102 { .index = MSR_CSTAR, .always = true },
103 { .index = MSR_SYSCALL_MASK, .always = true },
104#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100105 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100106 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100107 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
108 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
109 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
110 { .index = MSR_IA32_LASTINTTOIP, .always = false },
Tom Lendacky376c6d22020-12-10 11:10:06 -0600111 { .index = MSR_EFER, .always = false },
112 { .index = MSR_IA32_CR_PAT, .always = false },
113 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100114 { .index = MSR_INVALID, .always = false },
Avi Kivity6c8166a2009-05-31 18:15:37 +0300115};
116
Babu Moger8566ac82018-03-16 16:37:26 -0400117/*
118 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
119 * pause_filter_count: On processors that support Pause filtering(indicated
120 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
121 * count value. On VMRUN this value is loaded into an internal counter.
122 * Each time a pause instruction is executed, this counter is decremented
123 * until it reaches zero at which time a #VMEXIT is generated if pause
124 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
125 * Intercept Filtering for more details.
126 * This also indicate if ple logic enabled.
127 *
128 * pause_filter_thresh: In addition, some processor families support advanced
129 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
130 * the amount of time a guest is allowed to execute in a pause loop.
131 * In this mode, a 16-bit pause filter threshold field is added in the
132 * VMCB. The threshold value is a cycle count that is used to reset the
133 * pause counter. As with simple pause filtering, VMRUN loads the pause
134 * count value from VMCB into an internal counter. Then, on each pause
135 * instruction the hardware checks the elapsed number of cycles since
136 * the most recent pause instruction against the pause filter threshold.
137 * If the elapsed cycle count is greater than the pause filter threshold,
138 * then the internal pause count is reloaded from the VMCB and execution
139 * continues. If the elapsed cycle count is less than the pause filter
140 * threshold, then the internal pause count is decremented. If the count
141 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
142 * triggered. If advanced pause filtering is supported and pause filter
143 * threshold field is set to zero, the filter will operate in the simpler,
144 * count only mode.
145 */
146
147static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
148module_param(pause_filter_thresh, ushort, 0444);
149
150static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
151module_param(pause_filter_count, ushort, 0444);
152
153/* Default doubles per-vcpu window every exit. */
154static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
155module_param(pause_filter_count_grow, ushort, 0444);
156
157/* Default resets per-vcpu window every exit to pause_filter_count. */
158static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
159module_param(pause_filter_count_shrink, ushort, 0444);
160
161/* Default is to compute the maximum so we can never overflow. */
162static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
163module_param(pause_filter_count_max, ushort, 0444);
164
Sean Christopherson99840a72021-03-04 18:16:37 -0800165/*
166 * Use nested page tables by default. Note, NPT may get forced off by
167 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
168 */
169bool npt_enabled = true;
170module_param_named(npt, npt_enabled, bool, 0444);
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100171
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100172/* allow nested virtualization in KVM/SVM */
173static int nested = true;
Alexander Graf236de052008-11-25 20:17:10 +0100174module_param(nested, int, S_IRUGO);
175
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200176/* enable/disable Next RIP Save */
177static int nrips = true;
178module_param(nrips, int, 0444);
179
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -0500180/* enable/disable Virtual VMLOAD VMSAVE */
181static int vls = true;
182module_param(vls, int, 0444);
183
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500184/* enable/disable Virtual GIF */
185static int vgif = true;
186module_param(vgif, int, 0444);
Suravee Suthikulpanit5ea11f22016-08-23 13:52:41 -0500187
Tom Lendacky291bd202020-12-10 11:09:47 -0600188bool __read_mostly dump_invalid_vmcb;
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200189module_param(dump_invalid_vmcb, bool, 0644);
190
Wei Yongjun2e215212021-02-10 07:59:58 +0000191static bool svm_gp_erratum_intercept = true;
Bandan Das82a11e9c2021-01-26 03:18:29 -0500192
Brijesh Singh7607b712018-02-19 10:14:44 -0600193static u8 rsm_ins_bytes[] = "\x0f\xaa";
194
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800195static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800196
197struct kvm_ldttss_desc {
198 u16 limit0;
199 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100200 unsigned base1:8, type:5, dpl:2, p:1;
201 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800202 u32 base3;
203 u32 zero1;
204} __attribute__((packed));
205
Joerg Roedeleaf78262020-03-24 10:41:54 +0100206DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207
Sean Christopherson844d69c2021-04-23 15:34:04 -0700208/*
209 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
210 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
211 *
212 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
213 * defer the restoration of TSC_AUX until the CPU returns to userspace.
214 */
Sean Christopherson0caa0a72021-05-04 10:17:25 -0700215static int tsc_aux_uret_slot __read_mostly = -1;
Sean Christopherson844d69c2021-04-23 15:34:04 -0700216
Mathias Krause09941fb2012-08-30 01:30:20 +0200217static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200219#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800220#define MSRS_RANGE_SIZE 2048
221#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
222
Joerg Roedel883b0a92020-03-24 10:41:52 +0100223u32 svm_msrpm_offset(u32 msr)
Joerg Roedel455716f2010-03-01 15:34:35 +0100224{
225 u32 offset;
226 int i;
227
228 for (i = 0; i < NUM_MSR_MAPS; i++) {
229 if (msr < msrpm_ranges[i] ||
230 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
231 continue;
232
233 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
234 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
235
236 /* Now we have the u8 offset - but need the u32 offset */
237 return offset / 4;
238 }
239
240 /* MSR not in any range */
241 return MSR_INVALID;
242}
243
Avi Kivity6aa8b732006-12-10 02:21:36 -0800244#define MAX_INST_SIZE 15
245
Sean Christophersond468d942020-07-15 20:41:20 -0700246static int get_max_npt_level(void)
Joerg Roedel4b161842010-09-10 17:31:03 +0200247{
248#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800249 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200250#else
251 return PT32E_ROOT_LEVEL;
252#endif
253}
254
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300255int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800256{
Paolo Bonzinic513f482020-05-18 13:08:37 -0400257 struct vcpu_svm *svm = to_svm(vcpu);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300258 u64 old_efer = vcpu->arch.efer;
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000259 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100260
261 if (!npt_enabled) {
262 /* Shadow paging assumes NX to be available. */
263 efer |= EFER_NX;
264
265 if (!(efer & EFER_LMA))
266 efer &= ~EFER_LME;
267 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800268
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300269 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
270 if (!(efer & EFER_SVME)) {
271 svm_leave_nested(svm);
272 svm_set_gif(svm, true);
Bandan Das82a11e9c2021-01-26 03:18:29 -0500273 /* #GP intercept is still needed for vmware backdoor */
274 if (!enable_vmware_backdoor)
275 clr_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300276
277 /*
278 * Free the nested guest state, unless we are in SMM.
279 * In this case we will return to the nested guest
280 * as soon as we leave SMM.
281 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500282 if (!is_smm(vcpu))
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300283 svm_free_nested(svm);
284
285 } else {
286 int ret = svm_allocate_nested(svm);
287
288 if (ret) {
289 vcpu->arch.efer = old_efer;
290 return ret;
291 }
Bandan Das82a11e9c2021-01-26 03:18:29 -0500292
293 if (svm_gp_erratum_intercept)
294 set_exception_intercept(svm, GP_VECTOR);
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300295 }
Paolo Bonzinic513f482020-05-18 13:08:37 -0400296 }
297
298 svm->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedel06e78522020-06-25 10:03:23 +0200299 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Maxim Levitsky72f211e2020-10-01 14:29:53 +0300300 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301}
302
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303static int is_external_interrupt(u32 info)
304{
305 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
306 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
307}
308
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200309static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400310{
311 struct vcpu_svm *svm = to_svm(vcpu);
312 u32 ret = 0;
313
314 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200315 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
316 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400317}
318
319static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
320{
321 struct vcpu_svm *svm = to_svm(vcpu);
322
323 if (mask == 0)
324 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
325 else
326 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
327
328}
329
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200330static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800331{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400332 struct vcpu_svm *svm = to_svm(vcpu);
333
Tom Lendackyf1c63662020-12-14 10:29:50 -0500334 /*
335 * SEV-ES does not expose the next RIP. The RIP update is controlled by
336 * the type of exit and the #VC handler in the guest.
337 */
338 if (sev_es_guest(vcpu->kvm))
339 goto done;
340
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200341 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200342 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200343 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400344 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200345
Sean Christopherson1957aa62019-08-27 14:40:39 -0700346 if (!svm->next_rip) {
347 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
348 return 0;
349 } else {
Sean Christopherson1957aa62019-08-27 14:40:39 -0700350 kvm_rip_write(vcpu, svm->next_rip);
351 }
Tom Lendackyf1c63662020-12-14 10:29:50 -0500352
353done:
Glauber Costa2809f5d2009-05-12 16:21:05 -0400354 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200355
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700356 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800357}
358
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700359static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100360{
361 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700362 unsigned nr = vcpu->arch.exception.nr;
363 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700364 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100365
Paolo Bonzini63129752021-03-02 14:40:39 -0500366 kvm_deliver_exception_payload(vcpu);
Jim Mattsonda998b42018-10-16 14:29:22 -0700367
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200368 if (nr == BP_VECTOR && !nrips) {
Paolo Bonzini63129752021-03-02 14:40:39 -0500369 unsigned long rip, old_rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100370
371 /*
372 * For guest debugging where we have to reinject #BP if some
373 * INT3 is guest-owned:
374 * Emulate nRIP by moving RIP forward. Will fail if injection
375 * raises a fault that is not intercepted. Still better than
376 * failing in all cases.
377 */
Paolo Bonzini63129752021-03-02 14:40:39 -0500378 (void)skip_emulated_instruction(vcpu);
379 rip = kvm_rip_read(vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100380 svm->int3_rip = rip + svm->vmcb->save.cs.base;
381 svm->int3_injected = rip - old_rip;
382 }
383
Jan Kiszka116a4752010-02-23 17:47:54 +0100384 svm->vmcb->control.event_inj = nr
385 | SVM_EVTINJ_VALID
386 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
387 | SVM_EVTINJ_TYPE_EXEPT;
388 svm->vmcb->control.event_inj_err = error_code;
389}
390
Joerg Roedel67ec6602010-05-17 14:43:35 +0200391static void svm_init_erratum_383(void)
392{
393 u32 low, high;
394 int err;
395 u64 val;
396
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100397 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200398 return;
399
400 /* Use _safe variants to not break nested virtualization */
401 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
402 if (err)
403 return;
404
405 val |= (1ULL << 47);
406
407 low = lower_32_bits(val);
408 high = upper_32_bits(val);
409
410 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
411
412 erratum_383_found = true;
413}
414
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500415static void svm_init_osvw(struct kvm_vcpu *vcpu)
416{
417 /*
418 * Guests should see errata 400 and 415 as fixed (assuming that
419 * HLT and IO instructions are intercepted).
420 */
421 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
422 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
423
424 /*
425 * By increasing VCPU's osvw.length to 3 we are telling the guest that
426 * all osvw.status bits inside that length, including bit 0 (which is
427 * reserved for erratum 298), are valid. However, if host processor's
428 * osvw_len is 0 then osvw_status[0] carries no information. We need to
429 * be conservative here and therefore we tell the guest that erratum 298
430 * is present (because we really don't know).
431 */
432 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
433 vcpu->arch.osvw.status |= 1;
434}
435
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436static int has_svm(void)
437{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200438 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439
Eduardo Habkost63d11422008-11-17 19:03:20 -0200440 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800441 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 return 0;
443 }
444
Sean Christophersonccd85d92021-02-02 13:20:17 -0800445 if (sev_active()) {
446 pr_info("KVM is unsupported when running as an SEV guest\n");
447 return 0;
448 }
449
Avi Kivity6aa8b732006-12-10 02:21:36 -0800450 return 1;
451}
452
Radim Krčmář13a34e02014-08-28 15:13:03 +0200453static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800454{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100455 /* Make sure we clean up behind us */
456 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
457 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
458
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200459 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100460
461 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462}
463
Radim Krčmář13a34e02014-08-28 15:13:03 +0200464static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800465{
466
Tejun Heo0fe1e002009-10-29 22:34:14 +0900467 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800469 struct desc_struct *gdt;
470 int me = raw_smp_processor_id();
471
Alexander Graf10474ae2009-09-15 11:37:46 +0200472 rdmsrl(MSR_EFER, efer);
473 if (efer & EFER_SVME)
474 return -EBUSY;
475
Avi Kivity6aa8b732006-12-10 02:21:36 -0800476 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200477 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200478 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900480 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900481 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200482 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200483 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 }
485
Tejun Heo0fe1e002009-10-29 22:34:14 +0900486 sd->asid_generation = 1;
487 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
488 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600489 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800490
Thomas Garnier45fc8752017-03-14 10:05:08 -0700491 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900492 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493
Alexander Graf9962d032008-11-25 20:17:02 +0100494 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600496 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
Alexander Graf10474ae2009-09-15 11:37:46 +0200497
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100498 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
499 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500500 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100501 }
502
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500503
504 /*
505 * Get OSVW bits.
506 *
507 * Note that it is possible to have a system with mixed processor
508 * revisions and therefore different OSVW bits. If bits are not the same
509 * on different processors then choose the worst case (i.e. if erratum
510 * is present on one processor and not on another then assume that the
511 * erratum is present everywhere).
512 */
513 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
514 uint64_t len, status = 0;
515 int err;
516
517 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
518 if (!err)
519 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
520 &err);
521
522 if (err)
523 osvw_status = osvw_len = 0;
524 else {
525 if (len < osvw_len)
526 osvw_len = len;
527 osvw_status |= status;
528 osvw_status &= (1ULL << osvw_len) - 1;
529 }
530 } else
531 osvw_status = osvw_len = 0;
532
Joerg Roedel67ec6602010-05-17 14:43:35 +0200533 svm_init_erratum_383();
534
Joerg Roedel1018faa2012-02-29 14:57:32 +0100535 amd_pmu_enable_virt();
536
Alexander Graf10474ae2009-09-15 11:37:46 +0200537 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800538}
539
Joerg Roedel0da1db752008-07-02 16:02:11 +0200540static void svm_cpu_uninit(int cpu)
541{
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800542 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200543
Tejun Heo0fe1e002009-10-29 22:34:14 +0900544 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200545 return;
546
Jacob Xua2b2d4b2020-12-03 12:59:39 -0800547 per_cpu(svm_data, cpu) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600548 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900549 __free_page(sd->save_area);
550 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200551}
552
Avi Kivity6aa8b732006-12-10 02:21:36 -0800553static int svm_cpu_init(int cpu)
554{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900555 struct svm_cpu_data *sd;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700556 int ret = -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800557
Tejun Heo0fe1e002009-10-29 22:34:14 +0900558 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
559 if (!sd)
Sean Christophersonb95c2212021-04-21 19:11:22 -0700560 return ret;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900561 sd->cpu = cpu;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600562 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900563 if (!sd->save_area)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800564 goto free_cpu_data;
Sean Christophersonb95c2212021-04-21 19:11:22 -0700565
Tom Lendacky85ca8be2020-12-10 11:10:04 -0600566 clear_page(page_address(sd->save_area));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800567
Sean Christophersonb95c2212021-04-21 19:11:22 -0700568 ret = sev_cpu_init(sd);
569 if (ret)
570 goto free_save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600571
Tejun Heo0fe1e002009-10-29 22:34:14 +0900572 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800573
574 return 0;
575
Miaohe Lind80b64f2020-01-04 16:56:49 +0800576free_save_area:
577 __free_page(sd->save_area);
578free_cpu_data:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900579 kfree(sd);
Sean Christophersonb95c2212021-04-21 19:11:22 -0700580 return ret;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800581
582}
583
Alexander Graffd6fa732020-09-25 16:34:19 +0200584static int direct_access_msr_slot(u32 msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800585{
Alexander Graffd6fa732020-09-25 16:34:19 +0200586 u32 i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800587
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100588 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
Alexander Graffd6fa732020-09-25 16:34:19 +0200589 if (direct_access_msrs[i].index == msr)
590 return i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800591
Alexander Graffd6fa732020-09-25 16:34:19 +0200592 return -ENOENT;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100593}
594
Alexander Graffd6fa732020-09-25 16:34:19 +0200595static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
596 int write)
597{
598 struct vcpu_svm *svm = to_svm(vcpu);
599 int slot = direct_access_msr_slot(msr);
600
601 if (slot == -ENOENT)
602 return;
603
604 /* Set the shadow bitmaps to the desired intercept states */
605 if (read)
606 set_bit(slot, svm->shadow_msr_intercept.read);
607 else
608 clear_bit(slot, svm->shadow_msr_intercept.read);
609
610 if (write)
611 set_bit(slot, svm->shadow_msr_intercept.write);
612 else
613 clear_bit(slot, svm->shadow_msr_intercept.write);
614}
615
616static bool valid_msr_intercept(u32 index)
617{
618 return direct_access_msr_slot(index) != -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619}
620
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200621static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100622{
623 u8 bit_write;
624 unsigned long tmp;
625 u32 offset;
626 u32 *msrpm;
627
628 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
629 to_svm(vcpu)->msrpm;
630
631 offset = svm_msrpm_offset(msr);
632 bit_write = 2 * (msr & 0x0f) + 1;
633 tmp = msrpm[offset];
634
635 BUG_ON(offset == MSR_INVALID);
636
637 return !!test_bit(bit_write, &tmp);
638}
639
Alexander Graffd6fa732020-09-25 16:34:19 +0200640static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
641 u32 msr, int read, int write)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800642{
Joerg Roedel455716f2010-03-01 15:34:35 +0100643 u8 bit_read, bit_write;
644 unsigned long tmp;
645 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100647 /*
648 * If this warning triggers extend the direct_access_msrs list at the
649 * beginning of the file
650 */
651 WARN_ON(!valid_msr_intercept(msr));
652
Alexander Graffd6fa732020-09-25 16:34:19 +0200653 /* Enforce non allowed MSRs to trap */
654 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
655 read = 0;
656
657 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
658 write = 0;
659
Joerg Roedel455716f2010-03-01 15:34:35 +0100660 offset = svm_msrpm_offset(msr);
661 bit_read = 2 * (msr & 0x0f);
662 bit_write = 2 * (msr & 0x0f) + 1;
663 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800664
Joerg Roedel455716f2010-03-01 15:34:35 +0100665 BUG_ON(offset == MSR_INVALID);
666
667 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
668 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
669
670 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671}
672
Tom Lendacky376c6d22020-12-10 11:10:06 -0600673void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
674 int read, int write)
Alexander Graffd6fa732020-09-25 16:34:19 +0200675{
676 set_shadow_msr_intercept(vcpu, msr, read, write);
677 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
678}
679
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300680u32 *svm_vcpu_alloc_msrpm(void)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100681{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400682 unsigned int order = get_order(MSRPM_SIZE);
683 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200684 u32 *msrpm;
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100685
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300686 if (!pages)
687 return NULL;
688
689 msrpm = page_address(pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400690 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100691
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200692 return msrpm;
693}
694
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300695void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800696{
Joerg Roedelf65c2292008-02-13 18:58:46 +0100697 int i;
698
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100699 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
700 if (!direct_access_msrs[i].always)
701 continue;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200702 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100703 }
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300704}
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705
Maxim Levitsky2fcf4872020-10-01 14:29:54 +0300706
707void svm_vcpu_free_msrpm(u32 *msrpm)
Maxim Levitskyf4c847a2020-08-27 20:11:40 +0300708{
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400709 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800710}
711
Alexander Graffd6fa732020-09-25 16:34:19 +0200712static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
713{
714 struct vcpu_svm *svm = to_svm(vcpu);
715 u32 i;
716
717 /*
718 * Set intercept permissions for all direct access MSRs again. They
719 * will automatically get filtered through the MSR filter, so we are
720 * back in sync after this.
721 */
722 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
723 u32 msr = direct_access_msrs[i].index;
724 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
725 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
726
727 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
Anthony Liguoric8681332007-04-30 09:48:11 +0300728 }
729}
730
Joerg Roedel323c3d82010-03-01 15:34:37 +0100731static void add_msr_offset(u32 offset)
732{
733 int i;
734
735 for (i = 0; i < MSRPM_OFFSETS; ++i) {
736
737 /* Offset already in list? */
738 if (msrpm_offsets[i] == offset)
739 return;
740
741 /* Slot used by another offset? */
742 if (msrpm_offsets[i] != MSR_INVALID)
743 continue;
744
745 /* Add offset to list */
746 msrpm_offsets[i] = offset;
747
748 return;
749 }
750
751 /*
752 * If this BUG triggers the msrpm_offsets table has an overflow. Just
753 * increase MSRPM_OFFSETS in this case.
754 */
755 BUG();
756}
757
758static void init_msrpm_offsets(void)
759{
760 int i;
761
762 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
763
764 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
765 u32 offset;
766
767 offset = svm_msrpm_offset(direct_access_msrs[i].index);
768 BUG_ON(offset == MSR_INVALID);
769
770 add_msr_offset(offset);
771 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800772}
773
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200774static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200776 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800777
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500778 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200779 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
780 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
781 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
782 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783}
784
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200785static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786{
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200787 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500789 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Aaron Lewis476c9bd2020-09-25 16:34:18 +0200790 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
791 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
792 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
793 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800794}
795
Joerg Roedel883b0a92020-03-24 10:41:52 +0100796void disable_nmi_singlestep(struct vcpu_svm *svm)
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200797{
798 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500799
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200800 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
801 /* Clear our flags if they were not set by the guest */
802 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
803 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
804 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
805 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
806 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200807}
808
Babu Moger8566ac82018-03-16 16:37:26 -0400809static void grow_ple_window(struct kvm_vcpu *vcpu)
810{
811 struct vcpu_svm *svm = to_svm(vcpu);
812 struct vmcb_control_area *control = &svm->vmcb->control;
813 int old = control->pause_filter_count;
814
815 control->pause_filter_count = __grow_ple_window(old,
816 pause_filter_count,
817 pause_filter_count_grow,
818 pause_filter_count_max);
819
Peter Xu4f75bcc2019-09-06 10:17:22 +0800820 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200821 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800822 trace_kvm_ple_window_update(vcpu->vcpu_id,
823 control->pause_filter_count, old);
824 }
Babu Moger8566ac82018-03-16 16:37:26 -0400825}
826
827static void shrink_ple_window(struct kvm_vcpu *vcpu)
828{
829 struct vcpu_svm *svm = to_svm(vcpu);
830 struct vmcb_control_area *control = &svm->vmcb->control;
831 int old = control->pause_filter_count;
832
833 control->pause_filter_count =
834 __shrink_ple_window(old,
835 pause_filter_count,
836 pause_filter_count_shrink,
837 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800838 if (control->pause_filter_count != old) {
Joerg Roedel06e78522020-06-25 10:03:23 +0200839 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800840 trace_kvm_ple_window_update(vcpu->vcpu_id,
841 control->pause_filter_count, old);
842 }
Babu Moger8566ac82018-03-16 16:37:26 -0400843}
844
Tom Lendacky52918ed2020-01-09 17:42:16 -0600845/*
846 * The default MMIO mask is a single bit (excluding the present bit),
847 * which could conflict with the memory encryption bit. Check for
848 * memory encryption support and override the default MMIO mask if
849 * memory encryption is enabled.
850 */
851static __init void svm_adjust_mmio_mask(void)
852{
853 unsigned int enc_bit, mask_bit;
854 u64 msr, mask;
855
856 /* If there is no memory encryption support, use existing mask */
857 if (cpuid_eax(0x80000000) < 0x8000001f)
858 return;
859
860 /* If memory encryption is not enabled, use existing mask */
861 rdmsrl(MSR_K8_SYSCFG, msr);
862 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
863 return;
864
865 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
866 mask_bit = boot_cpu_data.x86_phys_bits;
867
868 /* Increment the mask bit if it is the same as the encryption bit */
869 if (enc_bit == mask_bit)
870 mask_bit++;
871
872 /*
873 * If the mask bit location is below 52, then some bits above the
874 * physical addressing limit will always be reserved, so use the
875 * rsvd_bits() function to generate the mask. This mask, along with
876 * the present bit, will be used to generate a page fault with
877 * PFER.RSV = 1.
878 *
879 * If the mask bit location is 52 (or above), then clear the mask.
880 */
881 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
882
Sean Christopherson81203372021-02-25 12:47:35 -0800883 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
Tom Lendacky52918ed2020-01-09 17:42:16 -0600884}
885
Li RongQingdd58f3c2020-02-23 16:13:12 +0800886static void svm_hardware_teardown(void)
887{
888 int cpu;
889
Sean Christopherson4cafd0c2021-04-21 19:11:20 -0700890 sev_hardware_teardown();
Li RongQingdd58f3c2020-02-23 16:13:12 +0800891
892 for_each_possible_cpu(cpu)
893 svm_cpu_uninit(cpu);
894
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400895 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
896 get_order(IOPM_SIZE));
Li RongQingdd58f3c2020-02-23 16:13:12 +0800897 iopm_base = 0;
898}
899
Sean Christopherson9b58b982020-03-02 15:56:42 -0800900static __init void svm_set_cpu_caps(void)
901{
902 kvm_set_cpu_caps();
903
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100904 supported_xss = 0;
905
Sean Christophersona50718c2020-03-02 15:57:07 -0800906 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
907 if (nested) {
Sean Christopherson9b58b982020-03-02 15:56:42 -0800908 kvm_cpu_cap_set(X86_FEATURE_SVM);
909
Sean Christopherson4eb87462020-03-02 15:57:08 -0800910 if (nrips)
Sean Christophersona50718c2020-03-02 15:57:07 -0800911 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
912
913 if (npt_enabled)
914 kvm_cpu_cap_set(X86_FEATURE_NPT);
Wei Huang14c2bf82021-01-26 03:18:31 -0500915
916 /* Nested VM can receive #VMEXIT instead of triggering #GP */
917 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
Sean Christophersona50718c2020-03-02 15:57:07 -0800918 }
919
Sean Christopherson93c380e2020-03-02 15:56:54 -0800920 /* CPUID 0x80000008 */
921 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
922 boot_cpu_has(X86_FEATURE_AMD_SSBD))
923 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
Paolo Bonzinid9db0fd2021-04-21 19:11:15 -0700924
925 /* CPUID 0x8000001F (SME/SEV features) */
926 sev_set_cpu_caps();
Sean Christopherson9b58b982020-03-02 15:56:42 -0800927}
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929static __init int svm_hardware_setup(void)
930{
931 int cpu;
932 struct page *iopm_pages;
933 void *iopm_va;
934 int r;
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400935 unsigned int order = get_order(IOPM_SIZE);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400937 iopm_pages = alloc_pages(GFP_KERNEL, order);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800938
939 if (!iopm_pages)
940 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300941
942 iopm_va = page_address(iopm_pages);
Krish Sadhukhan47903dc2021-04-12 17:56:05 -0400943 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800944 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
945
Joerg Roedel323c3d82010-03-01 15:34:37 +0100946 init_msrpm_offsets();
947
Sean Christophersoncfc48182020-03-02 15:56:23 -0800948 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
949
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100950 if (boot_cpu_has(X86_FEATURE_NX))
951 kvm_enable_efer_bits(EFER_NX);
952
Alexander Graf1b2fd702009-02-02 16:23:51 +0100953 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
954 kvm_enable_efer_bits(EFER_FFXSR);
955
Joerg Roedel92a1f122011-03-25 09:44:51 +0100956 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100957 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800958 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
959 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100960 }
961
Sean Christophersone5fda4b2021-05-04 10:17:32 -0700962 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
Sean Christopherson844d69c2021-04-23 15:34:04 -0700963
Babu Moger8566ac82018-03-16 16:37:26 -0400964 /* Check for pause filtering support */
965 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
966 pause_filter_count = 0;
967 pause_filter_thresh = 0;
968 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
969 pause_filter_thresh = 0;
970 }
971
Alexander Graf236de052008-11-25 20:17:10 +0100972 if (nested) {
973 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200974 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100975 }
976
Sean Christopherson99840a72021-03-04 18:16:37 -0800977 /*
978 * KVM's MMU doesn't support using 2-level paging for itself, and thus
979 * NPT isn't supported if the host is using 2-level paging since host
980 * CR4 is unchanged on VMRUN.
981 */
982 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100983 npt_enabled = false;
984
Sean Christopherson99840a72021-03-04 18:16:37 -0800985 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100986 npt_enabled = false;
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100987
Sean Christopherson83013052020-07-15 20:41:22 -0700988 kvm_configure_mmu(npt_enabled, get_max_npt_level(), PG_LEVEL_1G);
Sean Christopherson213e0e12020-03-02 15:57:01 -0800989 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100990
Sean Christophersone8126bd2021-04-21 19:11:14 -0700991 /* Note, SEV setup consumes npt_enabled. */
992 sev_hardware_setup();
Sean Christophersonfa136802021-04-21 19:11:13 -0700993
994 svm_adjust_mmio_mask();
995
996 for_each_possible_cpu(cpu) {
997 r = svm_cpu_init(cpu);
998 if (r)
999 goto err;
1000 }
1001
Paolo Bonzinid647eb62019-06-20 14:13:33 +02001002 if (nrips) {
1003 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1004 nrips = false;
1005 }
1006
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001007 if (avic) {
1008 if (!npt_enabled ||
1009 !boot_cpu_has(X86_FEATURE_AVIC) ||
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001010 !IS_ENABLED(CONFIG_X86_LOCAL_APIC)) {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001011 avic = false;
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001012 } else {
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001013 pr_info("AVIC enabled\n");
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001014
Suravee Suthikulpanit5881f732016-08-23 13:52:42 -05001015 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1016 }
Suravee Suthikulpanit5b8abf12016-06-15 17:24:36 -05001017 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001018
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001019 if (vls) {
1020 if (!npt_enabled ||
Borislav Petkov5442c262017-08-01 20:55:52 +02001021 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001022 !IS_ENABLED(CONFIG_X86_64)) {
1023 vls = false;
1024 } else {
1025 pr_info("Virtual VMLOAD VMSAVE supported\n");
1026 }
1027 }
1028
Wei Huang3b9c7232021-01-26 03:18:30 -05001029 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
1030 svm_gp_erratum_intercept = false;
1031
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001032 if (vgif) {
1033 if (!boot_cpu_has(X86_FEATURE_VGIF))
1034 vgif = false;
1035 else
1036 pr_info("Virtual GIF supported\n");
1037 }
1038
Sean Christopherson9b58b982020-03-02 15:56:42 -08001039 svm_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08001040
Mohammed Gamal3edd6832020-07-10 17:48:11 +02001041 /*
1042 * It seems that on AMD processors PTE's accessed bit is
1043 * being set by the CPU hardware before the NPF vmexit.
1044 * This is not expected behaviour and our tests fail because
1045 * of it.
1046 * A workaround here is to disable support for
1047 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1048 * In this case userspace can know if there is support using
1049 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1050 * it
1051 * If future AMD CPU models change the behaviour described above,
1052 * this variable can be changed accordingly
1053 */
1054 allow_smaller_maxphyaddr = !npt_enabled;
1055
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056 return 0;
1057
Joerg Roedelf65c2292008-02-13 18:58:46 +01001058err:
Li RongQingdd58f3c2020-02-23 16:13:12 +08001059 svm_hardware_teardown();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001060 return r;
1061}
1062
Avi Kivity6aa8b732006-12-10 02:21:36 -08001063static void init_seg(struct vmcb_seg *seg)
1064{
1065 seg->selector = 0;
1066 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001067 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001068 seg->limit = 0xffff;
1069 seg->base = 0;
1070}
1071
1072static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1073{
1074 seg->selector = 0;
1075 seg->attrib = SVM_SELECTOR_P_MASK | type;
1076 seg->limit = 0xffff;
1077 seg->base = 0;
1078}
1079
Leonid Shatz326e7422018-11-06 12:14:25 +02001080static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001081{
1082 struct vcpu_svm *svm = to_svm(vcpu);
1083 u64 g_tsc_offset = 0;
1084
Joerg Roedel20307532010-11-29 17:51:48 +01001085 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001086 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001087 g_tsc_offset = svm->vmcb->control.tsc_offset -
Cathy Avery4995a362021-01-13 07:07:52 -05001088 svm->vmcb01.ptr->control.tsc_offset;
1089 svm->vmcb01.ptr->control.tsc_offset = offset;
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001090 }
1091
1092 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1093 svm->vmcb->control.tsc_offset - g_tsc_offset,
1094 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001095
1096 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001097
Joerg Roedel06e78522020-06-25 10:03:23 +02001098 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Leonid Shatz326e7422018-11-06 12:14:25 +02001099 return svm->vmcb->control.tsc_offset;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001100}
1101
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001102/* Evaluate instruction intercepts that depend on guest CPUID features. */
1103static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
1104 struct vcpu_svm *svm)
Babu Moger4407a792020-09-11 14:29:19 -05001105{
1106 /*
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001107 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1108 * roots, or if INVPCID is disabled in the guest to inject #UD.
Babu Moger4407a792020-09-11 14:29:19 -05001109 */
1110 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
Sean Christopherson0a8ed2e2021-02-11 16:34:09 -08001111 if (!npt_enabled ||
1112 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
Babu Moger4407a792020-09-11 14:29:19 -05001113 svm_set_intercept(svm, INTERCEPT_INVPCID);
1114 else
1115 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1116 }
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001117
1118 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
1119 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
1120 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
1121 else
1122 svm_set_intercept(svm, INTERCEPT_RDTSCP);
1123 }
Babu Moger4407a792020-09-11 14:29:19 -05001124}
1125
Paolo Bonzini63129752021-03-02 14:40:39 -05001126static void init_vmcb(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001127{
Paolo Bonzini63129752021-03-02 14:40:39 -05001128 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedele6101a92008-02-13 18:58:45 +01001129 struct vmcb_control_area *control = &svm->vmcb->control;
1130 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001131
Paolo Bonzini63129752021-03-02 14:40:39 -05001132 vcpu->arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001133
Babu Moger830bd712020-09-11 14:28:50 -05001134 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1135 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1136 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1137 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1138 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1139 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001140 if (!kvm_vcpu_apicv_active(vcpu))
Babu Moger830bd712020-09-11 14:28:50 -05001141 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142
Paolo Bonzini5315c712014-03-03 13:08:29 +01001143 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001144
Joerg Roedel18c918c2010-11-30 18:03:59 +01001145 set_exception_intercept(svm, PF_VECTOR);
1146 set_exception_intercept(svm, UD_VECTOR);
1147 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001148 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001149 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001150 /*
1151 * Guest access to VMware backdoor ports could legitimately
1152 * trigger #GP because of TSS I/O permission bitmap.
1153 * We intercept those #GP and allow access to them anyway
1154 * as VMware does.
1155 */
1156 if (enable_vmware_backdoor)
1157 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001158
Joerg Roedela284ba52020-06-25 10:03:24 +02001159 svm_set_intercept(svm, INTERCEPT_INTR);
1160 svm_set_intercept(svm, INTERCEPT_NMI);
1161 svm_set_intercept(svm, INTERCEPT_SMI);
1162 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1163 svm_set_intercept(svm, INTERCEPT_RDPMC);
1164 svm_set_intercept(svm, INTERCEPT_CPUID);
1165 svm_set_intercept(svm, INTERCEPT_INVD);
1166 svm_set_intercept(svm, INTERCEPT_INVLPG);
1167 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1168 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1169 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1170 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1171 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1172 svm_set_intercept(svm, INTERCEPT_VMRUN);
1173 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1174 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1175 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1176 svm_set_intercept(svm, INTERCEPT_STGI);
1177 svm_set_intercept(svm, INTERCEPT_CLGI);
1178 svm_set_intercept(svm, INTERCEPT_SKINIT);
1179 svm_set_intercept(svm, INTERCEPT_WBINVD);
1180 svm_set_intercept(svm, INTERCEPT_XSETBV);
1181 svm_set_intercept(svm, INTERCEPT_RDPRU);
1182 svm_set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001183
Paolo Bonzini63129752021-03-02 14:40:39 -05001184 if (!kvm_mwait_in_guest(vcpu->kvm)) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001185 svm_set_intercept(svm, INTERCEPT_MONITOR);
1186 svm_set_intercept(svm, INTERCEPT_MWAIT);
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001187 }
1188
Paolo Bonzini63129752021-03-02 14:40:39 -05001189 if (!kvm_hlt_in_guest(vcpu->kvm))
Joerg Roedela284ba52020-06-25 10:03:24 +02001190 svm_set_intercept(svm, INTERCEPT_HLT);
Wanpeng Licaa057a2018-03-12 04:53:03 -07001191
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001192 control->iopm_base_pa = __sme_set(iopm_base);
1193 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001194 control->int_ctl = V_INTR_MASKING_MASK;
1195
1196 init_seg(&save->es);
1197 init_seg(&save->ss);
1198 init_seg(&save->ds);
1199 init_seg(&save->fs);
1200 init_seg(&save->gs);
1201
1202 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001203 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001204 /* Executable/Readable Code Segment */
1205 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1206 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1207 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208
1209 save->gdtr.limit = 0xffff;
1210 save->idtr.limit = 0xffff;
1211
1212 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1213 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1214
Paolo Bonzini63129752021-03-02 14:40:39 -05001215 svm_set_cr4(vcpu, 0);
1216 svm_set_efer(vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001217 save->dr6 = 0xffff0ff0;
Paolo Bonzini63129752021-03-02 14:40:39 -05001218 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 save->rip = 0x0000fff0;
Paolo Bonzini63129752021-03-02 14:40:39 -05001220 vcpu->arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001221
Joerg Roedele0231712010-02-24 18:59:10 +01001222 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001223 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001224 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001226 svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
1227 kvm_mmu_reset_context(vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001228
Rusty Russell66aee912007-07-17 23:34:16 +10001229 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001230 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001231
1232 if (npt_enabled) {
1233 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001234 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedela284ba52020-06-25 10:03:24 +02001235 svm_clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001236 clr_exception_intercept(svm, PF_VECTOR);
Babu Moger830bd712020-09-11 14:28:50 -05001237 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1238 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
Paolo Bonzini63129752021-03-02 14:40:39 -05001239 save->g_pat = vcpu->arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001240 save->cr3 = 0;
1241 save->cr4 = 0;
1242 }
Cathy Avery193015a2021-01-12 11:43:13 -05001243 svm->current_vmcb->asid_generation = 0;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001244 svm->asid = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001245
Maxim Levitskyc74ad082021-05-03 15:54:43 +03001246 svm->nested.vmcb12_gpa = INVALID_GPA;
1247 svm->nested.last_vmcb12_gpa = INVALID_GPA;
Paolo Bonzini63129752021-03-02 14:40:39 -05001248 vcpu->arch.hflags = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001249
Paolo Bonzini63129752021-03-02 14:40:39 -05001250 if (!kvm_pause_in_guest(vcpu->kvm)) {
Babu Moger8566ac82018-03-16 16:37:26 -04001251 control->pause_filter_count = pause_filter_count;
1252 if (pause_filter_thresh)
1253 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedela284ba52020-06-25 10:03:24 +02001254 svm_set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001255 } else {
Joerg Roedela284ba52020-06-25 10:03:24 +02001256 svm_clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001257 }
1258
Sean Christopherson3b195ac2021-05-04 10:17:22 -07001259 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05001260
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001261 /*
Babu Mogerd00b99c2021-02-17 10:56:04 -05001262 * If the host supports V_SPEC_CTRL then disable the interception
1263 * of MSR_IA32_SPEC_CTRL.
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001264 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05001265 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1266 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1267
Paolo Bonzini63129752021-03-02 14:40:39 -05001268 if (kvm_vcpu_apicv_active(vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001269 avic_init_vmcb(svm);
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001270
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001271 if (vgif) {
Joerg Roedela284ba52020-06-25 10:03:24 +02001272 svm_clr_intercept(svm, INTERCEPT_STGI);
1273 svm_clr_intercept(svm, INTERCEPT_CLGI);
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001274 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1275 }
1276
Paolo Bonzini63129752021-03-02 14:40:39 -05001277 if (sev_guest(vcpu->kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001278 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001279 clr_exception_intercept(svm, UD_VECTOR);
Tom Lendacky376c6d22020-12-10 11:10:06 -06001280
Paolo Bonzini63129752021-03-02 14:40:39 -05001281 if (sev_es_guest(vcpu->kvm)) {
Tom Lendacky376c6d22020-12-10 11:10:06 -06001282 /* Perform SEV-ES specific VMCB updates */
1283 sev_es_init_vmcb(svm);
1284 }
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001285 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001286
Joerg Roedel06e78522020-06-25 10:03:23 +02001287 vmcb_mark_all_dirty(svm->vmcb);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001288
Joerg Roedel2af91942009-08-07 11:49:28 +02001289 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001290
1291}
1292
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001293static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001294{
1295 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001296 u32 dummy;
1297 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001298
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001299 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001300 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001301
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001302 if (!init_event) {
Paolo Bonzini63129752021-03-02 14:40:39 -05001303 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1304 MSR_IA32_APICBASE_ENABLE;
1305 if (kvm_vcpu_is_reset_bsp(vcpu))
1306 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001307 }
Paolo Bonzini63129752021-03-02 14:40:39 -05001308 init_vmcb(vcpu);
Avi Kivity70433382007-11-07 12:57:23 +02001309
Sean Christophersonf91af512020-03-04 17:34:37 -08001310 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001311 kvm_rdx_write(vcpu, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001312
1313 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1314 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001315}
1316
Cathy Avery4995a362021-01-13 07:07:52 -05001317void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1318{
1319 svm->current_vmcb = target_vmcb;
1320 svm->vmcb = target_vmcb->ptr;
Cathy Avery4995a362021-01-13 07:07:52 -05001321}
1322
Sean Christopherson987b2592019-12-18 13:54:55 -08001323static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001324{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001325 struct vcpu_svm *svm;
Cathy Avery4995a362021-01-13 07:07:52 -05001326 struct page *vmcb01_page;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001327 struct page *vmsa_page = NULL;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001328 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001329
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001330 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1331 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001332
Joerg Roedelf65c2292008-02-13 18:58:46 +01001333 err = -ENOMEM;
Cathy Avery4995a362021-01-13 07:07:52 -05001334 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1335 if (!vmcb01_page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001336 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001337
Paolo Bonzini63129752021-03-02 14:40:39 -05001338 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001339 /*
1340 * SEV-ES guests require a separate VMSA page used to contain
1341 * the encrypted register state of the guest.
1342 */
1343 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1344 if (!vmsa_page)
1345 goto error_free_vmcb_page;
Tom Lendackyed02b212020-12-10 11:10:01 -06001346
1347 /*
1348 * SEV-ES guests maintain an encrypted version of their FPU
1349 * state which is restored and saved on VMRUN and VMEXIT.
1350 * Free the fpu structure to prevent KVM from attempting to
1351 * access the FPU state.
1352 */
1353 kvm_free_guest_fpu(vcpu);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001354 }
1355
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001356 err = avic_init_vcpu(svm);
1357 if (err)
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001358 goto error_free_vmsa_page;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001359
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001360 /* We initialize this flag to true to make sure that the is_running
1361 * bit would be set the first time the vcpu is loaded.
1362 */
Suravee Suthikulpanit6c3e4422019-11-14 14:15:12 -06001363 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1364 svm->avic_is_running = true;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001365
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001366 svm->msrpm = svm_vcpu_alloc_msrpm();
Chen Zhou054409a2020-11-17 10:54:26 +08001367 if (!svm->msrpm) {
1368 err = -ENOMEM;
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001369 goto error_free_vmsa_page;
Chen Zhou054409a2020-11-17 10:54:26 +08001370 }
Alexander Grafb286d5d2008-11-25 20:17:05 +01001371
Aaron Lewis476c9bd2020-09-25 16:34:18 +02001372 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001373
Cathy Avery4995a362021-01-13 07:07:52 -05001374 svm->vmcb01.ptr = page_address(vmcb01_page);
1375 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001376
1377 if (vmsa_page)
1378 svm->vmsa = page_address(vmsa_page);
1379
Michael Rotha7fc06d2021-02-02 13:01:26 -06001380 svm->guest_state_loaded = false;
Cathy Avery4995a362021-01-13 07:07:52 -05001381
1382 svm_switch_vmcb(svm, &svm->vmcb01);
Paolo Bonzini63129752021-03-02 14:40:39 -05001383 init_vmcb(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
Sean Christopherson7f271792019-12-18 13:54:51 -08001385 svm_init_osvw(vcpu);
Paolo Bonzinibab0c312020-02-11 18:40:58 +01001386 vcpu->arch.microcode_version = 0x01000065;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001387
Paolo Bonzini63129752021-03-02 14:40:39 -05001388 if (sev_es_guest(vcpu->kvm))
Tom Lendacky376c6d22020-12-10 11:10:06 -06001389 /* Perform SEV-ES specific VMCB creation updates */
1390 sev_es_create_vcpu(svm);
1391
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001392 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001393
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001394error_free_vmsa_page:
1395 if (vmsa_page)
1396 __free_page(vmsa_page);
Maxim Levitsky8d22b902020-08-27 20:11:42 +03001397error_free_vmcb_page:
Cathy Avery4995a362021-01-13 07:07:52 -05001398 __free_page(vmcb01_page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001399out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001400 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401}
1402
Jim Mattsonfd65d312018-05-22 09:54:20 -07001403static void svm_clear_current_vmcb(struct vmcb *vmcb)
1404{
1405 int i;
1406
1407 for_each_online_cpu(i)
1408 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1409}
1410
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1412{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001413 struct vcpu_svm *svm = to_svm(vcpu);
1414
Jim Mattsonfd65d312018-05-22 09:54:20 -07001415 /*
1416 * The vmcb page can be recycled, causing a false negative in
1417 * svm_vcpu_load(). So, ensure that no logical CPU has this
1418 * vmcb page recorded as its current vmcb.
1419 */
1420 svm_clear_current_vmcb(svm->vmcb);
1421
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03001422 svm_free_nested(svm);
1423
Tom Lendackyadd5e2f2020-12-10 11:09:40 -06001424 sev_free_vcpu(vcpu);
1425
Cathy Avery4995a362021-01-13 07:07:52 -05001426 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
Krish Sadhukhan47903dc2021-04-12 17:56:05 -04001427 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001428}
1429
Michael Rotha7fc06d2021-02-02 13:01:26 -06001430static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001431{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001432 struct vcpu_svm *svm = to_svm(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001433 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity0cc50642007-03-25 12:07:27 +02001434
Michael Rotha7fc06d2021-02-02 13:01:26 -06001435 if (svm->guest_state_loaded)
1436 return;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001437
Michael Rotha7fc06d2021-02-02 13:01:26 -06001438 /*
Michael Rotha7fc06d2021-02-02 13:01:26 -06001439 * Save additional host state that will be restored on VMEXIT (sev-es)
1440 * or subsequent vmload of host save area.
1441 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001442 if (sev_es_guest(vcpu->kvm)) {
Michael Rotha7fc06d2021-02-02 13:01:26 -06001443 sev_es_prepare_guest_switch(svm, vcpu->cpu);
Tom Lendacky86137772020-12-10 11:10:07 -06001444 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06001445 vmsave(__sme_page_pa(sd->save_area));
Tom Lendacky86137772020-12-10 11:10:07 -06001446 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001447
Haozhong Zhangad7218832015-10-20 15:39:02 +08001448 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1449 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1450 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1451 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1452 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1453 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001454 }
Michael Rotha7fc06d2021-02-02 13:01:26 -06001455
Sean Christopherson0caa0a72021-05-04 10:17:25 -07001456 if (likely(tsc_aux_uret_slot >= 0))
1457 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001458
Michael Rotha7fc06d2021-02-02 13:01:26 -06001459 svm->guest_state_loaded = true;
1460}
1461
1462static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1463{
Sean Christopherson844d69c2021-04-23 15:34:04 -07001464 to_svm(vcpu)->guest_state_loaded = false;
Michael Rotha7fc06d2021-02-02 13:01:26 -06001465}
1466
1467static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1468{
1469 struct vcpu_svm *svm = to_svm(vcpu);
1470 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1471
Ashok Raj15d45072018-02-01 22:59:43 +01001472 if (sd->current_vmcb != svm->vmcb) {
1473 sd->current_vmcb = svm->vmcb;
1474 indirect_branch_prediction_barrier();
1475 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001476 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001477}
1478
1479static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1480{
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001481 avic_vcpu_put(vcpu);
Michael Rotha7fc06d2021-02-02 13:01:26 -06001482 svm_prepare_host_switch(vcpu);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001483
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001484 ++vcpu->stat.host_state_reload;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001485}
1486
Avi Kivity6aa8b732006-12-10 02:21:36 -08001487static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1488{
Ladi Prosek9b611742017-06-21 09:06:59 +02001489 struct vcpu_svm *svm = to_svm(vcpu);
1490 unsigned long rflags = svm->vmcb->save.rflags;
1491
1492 if (svm->nmi_singlestep) {
1493 /* Hide our flags if they were not set by the guest */
1494 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1495 rflags &= ~X86_EFLAGS_TF;
1496 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1497 rflags &= ~X86_EFLAGS_RF;
1498 }
1499 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001500}
1501
1502static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1503{
Ladi Prosek9b611742017-06-21 09:06:59 +02001504 if (to_svm(vcpu)->nmi_singlestep)
1505 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1506
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001507 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001508 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001509 * (caused by either a task switch or an inter-privilege IRET),
1510 * so we do not need to update the CPL here.
1511 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001512 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001513}
1514
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001515static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1516{
1517 switch (reg) {
1518 case VCPU_EXREG_PDPTR:
1519 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001520 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001521 break;
1522 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07001523 WARN_ON_ONCE(1);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001524 }
1525}
1526
Suravee Suthikulpanite14b7782020-05-06 08:17:55 -05001527static void svm_set_vintr(struct vcpu_svm *svm)
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001528{
1529 struct vmcb_control_area *control;
1530
1531 /* The following fields are ignored when AVIC is enabled */
1532 WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu));
Joerg Roedela284ba52020-06-25 10:03:24 +02001533 svm_set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001534
1535 /*
1536 * This is just a dummy VINTR to actually cause a vmexit to happen.
1537 * Actual injection of virtual interrupts happens through EVENTINJ.
1538 */
1539 control = &svm->vmcb->control;
1540 control->int_vector = 0x0;
1541 control->int_ctl &= ~V_INTR_PRIO_MASK;
1542 control->int_ctl |= V_IRQ_MASK |
1543 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
Joerg Roedel06e78522020-06-25 10:03:23 +02001544 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001545}
1546
Alexander Graff0b85052008-11-25 20:17:01 +01001547static void svm_clear_vintr(struct vcpu_svm *svm)
1548{
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001549 const u32 mask = V_TPR_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK | V_INTR_MASKING_MASK;
Joerg Roedela284ba52020-06-25 10:03:24 +02001550 svm_clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001551
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001552 /* Drop int_ctl fields related to VINTR injection. */
1553 svm->vmcb->control.int_ctl &= mask;
1554 if (is_guest_mode(&svm->vcpu)) {
Cathy Avery4995a362021-01-13 07:07:52 -05001555 svm->vmcb01.ptr->control.int_ctl &= mask;
Paolo Bonzinifb7333d2020-06-08 07:11:47 -04001556
Paolo Bonzinid8e4e582020-05-22 07:38:20 -04001557 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1558 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1559 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & ~mask;
1560 }
1561
Joerg Roedel06e78522020-06-25 10:03:23 +02001562 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001563}
1564
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1566{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001567 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001568 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569
1570 switch (seg) {
1571 case VCPU_SREG_CS: return &save->cs;
1572 case VCPU_SREG_DS: return &save->ds;
1573 case VCPU_SREG_ES: return &save->es;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001574 case VCPU_SREG_FS: return &save01->fs;
1575 case VCPU_SREG_GS: return &save01->gs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001576 case VCPU_SREG_SS: return &save->ss;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02001577 case VCPU_SREG_TR: return &save01->tr;
1578 case VCPU_SREG_LDTR: return &save01->ldtr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001579 }
1580 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001581 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001582}
1583
1584static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1585{
1586 struct vmcb_seg *s = svm_seg(vcpu, seg);
1587
1588 return s->base;
1589}
1590
1591static void svm_get_segment(struct kvm_vcpu *vcpu,
1592 struct kvm_segment *var, int seg)
1593{
1594 struct vmcb_seg *s = svm_seg(vcpu, seg);
1595
1596 var->base = s->base;
1597 var->limit = s->limit;
1598 var->selector = s->selector;
1599 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1600 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1601 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1602 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1603 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1604 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1605 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301606
1607 /*
1608 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1609 * However, the SVM spec states that the G bit is not observed by the
1610 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1611 * So let's synthesize a legal G bit for all segments, this helps
1612 * running KVM nested. It also helps cross-vendor migration, because
1613 * Intel's vmentry has a check on the 'G' bit.
1614 */
1615 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001616
Joerg Roedele0231712010-02-24 18:59:10 +01001617 /*
1618 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001619 * for cross vendor migration purposes by "not present"
1620 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001621 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001622
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001623 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001624 case VCPU_SREG_TR:
1625 /*
1626 * Work around a bug where the busy flag in the tr selector
1627 * isn't exposed
1628 */
Amit Shahc0d09822008-10-27 09:04:18 +00001629 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001630 break;
1631 case VCPU_SREG_DS:
1632 case VCPU_SREG_ES:
1633 case VCPU_SREG_FS:
1634 case VCPU_SREG_GS:
1635 /*
1636 * The accessed bit must always be set in the segment
1637 * descriptor cache, although it can be cleared in the
1638 * descriptor, the cached bit always remains at 1. Since
1639 * Intel has a check on this, set it here to support
1640 * cross-vendor migration.
1641 */
1642 if (!var->unusable)
1643 var->type |= 0x1;
1644 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001645 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001646 /*
1647 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001648 * descriptor is left as 1, although the whole segment has
1649 * been made unusable. Clear it here to pass an Intel VMX
1650 * entry check when cross vendor migrating.
1651 */
1652 if (var->unusable)
1653 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001654 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001655 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001656 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001657 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001658}
1659
Izik Eidus2e4d2652008-03-24 19:38:34 +02001660static int svm_get_cpl(struct kvm_vcpu *vcpu)
1661{
1662 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1663
1664 return save->cpl;
1665}
1666
Gleb Natapov89a27f42010-02-16 10:51:48 +02001667static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001668{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001669 struct vcpu_svm *svm = to_svm(vcpu);
1670
Gleb Natapov89a27f42010-02-16 10:51:48 +02001671 dt->size = svm->vmcb->save.idtr.limit;
1672 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001673}
1674
Gleb Natapov89a27f42010-02-16 10:51:48 +02001675static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001676{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001677 struct vcpu_svm *svm = to_svm(vcpu);
1678
Gleb Natapov89a27f42010-02-16 10:51:48 +02001679 svm->vmcb->save.idtr.limit = dt->size;
1680 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001681 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001682}
1683
Gleb Natapov89a27f42010-02-16 10:51:48 +02001684static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001685{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001686 struct vcpu_svm *svm = to_svm(vcpu);
1687
Gleb Natapov89a27f42010-02-16 10:51:48 +02001688 dt->size = svm->vmcb->save.gdtr.limit;
1689 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001690}
1691
Gleb Natapov89a27f42010-02-16 10:51:48 +02001692static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001693{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001694 struct vcpu_svm *svm = to_svm(vcpu);
1695
Gleb Natapov89a27f42010-02-16 10:51:48 +02001696 svm->vmcb->save.gdtr.limit = dt->size;
1697 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel06e78522020-06-25 10:03:23 +02001698 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001699}
1700
Joerg Roedel883b0a92020-03-24 10:41:52 +01001701void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001702{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001703 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001704 u64 hcr0 = cr0;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001705
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001706#ifdef CONFIG_X86_64
Tom Lendackyf1c63662020-12-14 10:29:50 -05001707 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001708 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001709 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001710 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001711 }
1712
Mike Dayd77c26f2007-10-08 09:02:08 -04001713 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001714 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001715 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716 }
1717 }
1718#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001719 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001720
1721 if (!npt_enabled)
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001722 hcr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001723
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001724 /*
1725 * re-enable caching here because the QEMU bios
1726 * does not do it - this results in some delay at
1727 * reboot
1728 */
1729 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001730 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1731
1732 svm->vmcb->save.cr0 = hcr0;
Joerg Roedel06e78522020-06-25 10:03:23 +02001733 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001734
1735 /*
1736 * SEV-ES guests must always keep the CR intercepts cleared. CR
1737 * tracking is done using the CR write traps.
1738 */
Paolo Bonzini63129752021-03-02 14:40:39 -05001739 if (sev_es_guest(vcpu->kvm))
Paolo Bonzini2a32a772021-02-18 09:51:06 -05001740 return;
1741
1742 if (hcr0 == cr0) {
1743 /* Selective CR0 write remains on. */
1744 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1745 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1746 } else {
1747 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1748 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1749 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001750}
1751
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07001752static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1753{
1754 return true;
1755}
1756
1757void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001758{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001759 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Paolo Bonzinidc924b02020-11-15 09:44:18 -05001760 unsigned long old_cr4 = vcpu->arch.cr4;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001761
1762 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Sean Christophersonf55ac302020-03-20 14:28:12 -07001763 svm_flush_tlb(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001764
Joerg Roedelec077262008-04-09 14:15:28 +02001765 vcpu->arch.cr4 = cr4;
1766 if (!npt_enabled)
1767 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001768 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001769 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedel06e78522020-06-25 10:03:23 +02001770 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Jim Mattson2259c172020-10-29 10:06:48 -07001771
1772 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1773 kvm_update_cpuid_runtime(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001774}
1775
1776static void svm_set_segment(struct kvm_vcpu *vcpu,
1777 struct kvm_segment *var, int seg)
1778{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001779 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001780 struct vmcb_seg *s = svm_seg(vcpu, seg);
1781
1782 s->base = var->base;
1783 s->limit = var->limit;
1784 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001785 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1786 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1787 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1788 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1789 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1790 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1791 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1792 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001793
1794 /*
1795 * This is always accurate, except if SYSRET returned to a segment
1796 * with SS.DPL != 3. Intel does not have this quirk, and always
1797 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1798 * would entail passing the CPL to userspace and back.
1799 */
1800 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001801 /* This is symmetric with svm_get_segment() */
1802 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001803
Joerg Roedel06e78522020-06-25 10:03:23 +02001804 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001805}
1806
Jason Baronb6a7cc32021-01-14 22:27:54 -05001807static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001808{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001809 struct vcpu_svm *svm = to_svm(vcpu);
1810
Joerg Roedel18c918c2010-11-30 18:03:59 +01001811 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001812
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001813 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001814 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001815 set_exception_intercept(svm, BP_VECTOR);
Paolo Bonzini69869822020-07-10 17:48:06 +02001816 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001817}
1818
Tejun Heo0fe1e002009-10-29 22:34:14 +09001819static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001820{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001821 if (sd->next_asid > sd->max_asid) {
1822 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001823 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001824 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001825 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001826 }
1827
Cathy Avery193015a2021-01-12 11:43:13 -05001828 svm->current_vmcb->asid_generation = sd->asid_generation;
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04001829 svm->asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001830}
1831
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001832static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001833{
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001834 struct vmcb *vmcb = svm->vmcb;
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001835
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001836 if (svm->vcpu.arch.guest_state_protected)
1837 return;
1838
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001839 if (unlikely(value != vmcb->save.dr6)) {
1840 vmcb->save.dr6 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001841 vmcb_mark_dirty(vmcb, VMCB_DR);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001842 }
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001843}
1844
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001845static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1846{
1847 struct vcpu_svm *svm = to_svm(vcpu);
1848
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001849 if (vcpu->arch.guest_state_protected)
1850 return;
1851
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001852 get_debugreg(vcpu->arch.db[0], 0);
1853 get_debugreg(vcpu->arch.db[1], 1);
1854 get_debugreg(vcpu->arch.db[2], 2);
1855 get_debugreg(vcpu->arch.db[3], 3);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001856 /*
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001857 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
Paolo Bonzinid67668e2020-05-06 06:40:04 -04001858 * because db_interception might need it. We can do it before vmentry.
1859 */
Paolo Bonzini5679b802020-05-04 11:28:25 -04001860 vcpu->arch.dr6 = svm->vmcb->save.dr6;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001861 vcpu->arch.dr7 = svm->vmcb->save.dr7;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001862 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1863 set_dr_intercepts(svm);
1864}
1865
Gleb Natapov020df072010-04-13 10:05:23 +03001866static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001867{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001868 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001869
Tom Lendacky8d4846b2020-12-10 11:09:43 -06001870 if (vcpu->arch.guest_state_protected)
1871 return;
1872
Gleb Natapov020df072010-04-13 10:05:23 +03001873 svm->vmcb->save.dr7 = value;
Joerg Roedel06e78522020-06-25 10:03:23 +02001874 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001875}
1876
Paolo Bonzini63129752021-03-02 14:40:39 -05001877static int pf_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001878{
Paolo Bonzini63129752021-03-02 14:40:39 -05001879 struct vcpu_svm *svm = to_svm(vcpu);
1880
Sean Christopherson6d1b8672021-03-04 17:10:56 -08001881 u64 fault_address = svm->vmcb->control.exit_info_2;
Wanpeng Li1261bfa2017-07-13 18:30:40 -07001882 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001883
Paolo Bonzini63129752021-03-02 14:40:39 -05001884 return kvm_handle_page_fault(vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001885 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1886 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001887 svm->vmcb->control.insn_len);
1888}
1889
Paolo Bonzini63129752021-03-02 14:40:39 -05001890static int npf_interception(struct kvm_vcpu *vcpu)
Paolo Bonzinid0006532017-08-11 18:36:43 +02001891{
Paolo Bonzini63129752021-03-02 14:40:39 -05001892 struct vcpu_svm *svm = to_svm(vcpu);
1893
Brijesh Singh0ede79e2017-12-04 10:57:39 -06001894 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02001895 u64 error_code = svm->vmcb->control.exit_info_1;
1896
1897 trace_kvm_page_fault(fault_address, error_code);
Paolo Bonzini63129752021-03-02 14:40:39 -05001898 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06001899 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1900 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02001901 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001902}
1903
Paolo Bonzini63129752021-03-02 14:40:39 -05001904static int db_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001905{
Paolo Bonzini63129752021-03-02 14:40:39 -05001906 struct kvm_run *kvm_run = vcpu->run;
1907 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03001908
Paolo Bonzini63129752021-03-02 14:40:39 -05001909 if (!(vcpu->guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001910 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001911 !svm->nmi_singlestep) {
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08001912 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
Paolo Bonzini63129752021-03-02 14:40:39 -05001913 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001914 return 1;
1915 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001916
Jan Kiszka6be7d302009-10-18 13:24:54 +02001917 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02001918 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02001919 /* Make sure we check for pending NMIs upon entry */
1920 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03001921 }
1922
Paolo Bonzini63129752021-03-02 14:40:39 -05001923 if (vcpu->guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001924 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001925 kvm_run->exit_reason = KVM_EXIT_DEBUG;
Paolo Bonzinidee919d2020-05-04 09:34:10 -04001926 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1927 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
Gleb Natapov44c11432009-05-11 13:35:52 +03001928 kvm_run->debug.arch.pc =
1929 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1930 kvm_run->debug.arch.exception = DB_VECTOR;
1931 return 0;
1932 }
1933
1934 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001935}
1936
Paolo Bonzini63129752021-03-02 14:40:39 -05001937static int bp_interception(struct kvm_vcpu *vcpu)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001938{
Paolo Bonzini63129752021-03-02 14:40:39 -05001939 struct vcpu_svm *svm = to_svm(vcpu);
1940 struct kvm_run *kvm_run = vcpu->run;
Avi Kivity851ba692009-08-24 11:10:17 +03001941
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001942 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1943 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1944 kvm_run->debug.arch.exception = BP_VECTOR;
1945 return 0;
1946}
1947
Paolo Bonzini63129752021-03-02 14:40:39 -05001948static int ud_interception(struct kvm_vcpu *vcpu)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001949{
Paolo Bonzini63129752021-03-02 14:40:39 -05001950 return handle_ud(vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001951}
1952
Paolo Bonzini63129752021-03-02 14:40:39 -05001953static int ac_interception(struct kvm_vcpu *vcpu)
Eric Northup54a20552015-11-03 18:03:53 +01001954{
Paolo Bonzini63129752021-03-02 14:40:39 -05001955 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
Eric Northup54a20552015-11-03 18:03:53 +01001956 return 1;
1957}
1958
Joerg Roedel67ec6602010-05-17 14:43:35 +02001959static bool is_erratum_383(void)
1960{
1961 int err, i;
1962 u64 value;
1963
1964 if (!erratum_383_found)
1965 return false;
1966
1967 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1968 if (err)
1969 return false;
1970
1971 /* Bit 62 may or may not be set for this mce */
1972 value &= ~(1ULL << 62);
1973
1974 if (value != 0xb600000000010015ULL)
1975 return false;
1976
1977 /* Clear MCi_STATUS registers */
1978 for (i = 0; i < 6; ++i)
1979 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1980
1981 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1982 if (!err) {
1983 u32 low, high;
1984
1985 value &= ~(1ULL << 2);
1986 low = lower_32_bits(value);
1987 high = upper_32_bits(value);
1988
1989 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1990 }
1991
1992 /* Flush tlb to evict multi-match entries */
1993 __flush_tlb_all();
1994
1995 return true;
1996}
1997
Paolo Bonzini63129752021-03-02 14:40:39 -05001998static void svm_handle_mce(struct kvm_vcpu *vcpu)
Joerg Roedel53371b52008-04-09 14:15:30 +02001999{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002000 if (is_erratum_383()) {
2001 /*
2002 * Erratum 383 triggered. Guest state is corrupt so kill the
2003 * guest.
2004 */
2005 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2006
Paolo Bonzini63129752021-03-02 14:40:39 -05002007 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002008
2009 return;
2010 }
2011
Joerg Roedel53371b52008-04-09 14:15:30 +02002012 /*
2013 * On an #MC intercept the MCE handler is not called automatically in
2014 * the host. So do it by hand here.
2015 */
Uros Bizjak1c164cb2020-04-11 17:36:27 +02002016 kvm_machine_check();
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002017}
2018
Paolo Bonzini63129752021-03-02 14:40:39 -05002019static int mc_interception(struct kvm_vcpu *vcpu)
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002020{
Joerg Roedel53371b52008-04-09 14:15:30 +02002021 return 1;
2022}
2023
Paolo Bonzini63129752021-03-02 14:40:39 -05002024static int shutdown_interception(struct kvm_vcpu *vcpu)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002025{
Paolo Bonzini63129752021-03-02 14:40:39 -05002026 struct kvm_run *kvm_run = vcpu->run;
2027 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002028
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002029 /*
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002030 * The VM save area has already been encrypted so it
2031 * cannot be reinitialized - just terminate.
2032 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002033 if (sev_es_guest(vcpu->kvm))
Tom Lendacky8164a5f2020-12-10 11:09:45 -06002034 return -EINVAL;
2035
2036 /*
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002037 * VMCB is undefined after a SHUTDOWN intercept
2038 * so reinitialize it.
2039 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002040 clear_page(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05002041 init_vmcb(vcpu);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002042
2043 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2044 return 0;
2045}
2046
Paolo Bonzini63129752021-03-02 14:40:39 -05002047static int io_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002048{
Paolo Bonzini63129752021-03-02 14:40:39 -05002049 struct vcpu_svm *svm = to_svm(vcpu);
Mike Dayd77c26f2007-10-08 09:02:08 -04002050 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002051 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002052 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002053
Paolo Bonzini63129752021-03-02 14:40:39 -05002054 ++vcpu->stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002055 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002056 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2057 port = io_info >> 16;
2058 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Tom Lendacky7ed9abf2020-12-10 11:09:54 -06002059
2060 if (string) {
2061 if (sev_es_guest(vcpu->kvm))
2062 return sev_es_string_io(svm, size, port, in);
2063 else
2064 return kvm_emulate_instruction(vcpu, 0);
2065 }
2066
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002067 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002068
Paolo Bonzini63129752021-03-02 14:40:39 -05002069 return kvm_fast_pio(vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002070}
2071
Paolo Bonzini63129752021-03-02 14:40:39 -05002072static int nmi_interception(struct kvm_vcpu *vcpu)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002073{
2074 return 1;
2075}
2076
Paolo Bonzini63129752021-03-02 14:40:39 -05002077static int intr_interception(struct kvm_vcpu *vcpu)
Joerg Roedela0698052008-04-30 17:56:01 +02002078{
Paolo Bonzini63129752021-03-02 14:40:39 -05002079 ++vcpu->stat.irq_exits;
Joerg Roedela0698052008-04-30 17:56:01 +02002080 return 1;
2081}
2082
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002083static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002084{
Paolo Bonzini63129752021-03-02 14:40:39 -05002085 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002086 struct vmcb *vmcb12;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002087 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002088 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002089
Paolo Bonzini63129752021-03-02 14:40:39 -05002090 if (nested_svm_check_permissions(vcpu))
Alexander Graf55426752008-11-25 20:17:06 +01002091 return 1;
2092
Paolo Bonzini63129752021-03-02 14:40:39 -05002093 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002094 if (ret) {
2095 if (ret == -EINVAL)
Paolo Bonzini63129752021-03-02 14:40:39 -05002096 kvm_inject_gp(vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002097 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002098 }
2099
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05002100 vmcb12 = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002101
Paolo Bonzini63129752021-03-02 14:40:39 -05002102 ret = kvm_skip_emulated_instruction(vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002103
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002104 if (vmload) {
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002105 nested_svm_vmloadsave(vmcb12, svm->vmcb);
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002106 svm->sysenter_eip_hi = 0;
2107 svm->sysenter_esp_hi = 0;
2108 } else
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002109 nested_svm_vmloadsave(svm->vmcb, vmcb12);
2110
Paolo Bonzini63129752021-03-02 14:40:39 -05002111 kvm_vcpu_unmap(vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002112
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002113 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002114}
2115
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002116static int vmload_interception(struct kvm_vcpu *vcpu)
Alexander Graf55426752008-11-25 20:17:06 +01002117{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002118 return vmload_vmsave_interception(vcpu, true);
Alexander Graf55426752008-11-25 20:17:06 +01002119}
2120
Paolo Bonzini63129752021-03-02 14:40:39 -05002121static int vmsave_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002122{
Sean Christopherson2ac636a2021-02-04 16:57:45 -08002123 return vmload_vmsave_interception(vcpu, false);
Alexander Grafc0725422008-11-25 20:17:03 +01002124}
2125
Paolo Bonzini63129752021-03-02 14:40:39 -05002126static int vmrun_interception(struct kvm_vcpu *vcpu)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002127{
Paolo Bonzini63129752021-03-02 14:40:39 -05002128 if (nested_svm_check_permissions(vcpu))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002129 return 1;
2130
Paolo Bonzini63129752021-03-02 14:40:39 -05002131 return nested_svm_vmrun(vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002132}
2133
Bandan Das82a11e9c2021-01-26 03:18:29 -05002134enum {
2135 NONE_SVM_INSTR,
2136 SVM_INSTR_VMRUN,
2137 SVM_INSTR_VMLOAD,
2138 SVM_INSTR_VMSAVE,
2139};
2140
2141/* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2142static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2143{
2144 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2145
2146 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2147 return NONE_SVM_INSTR;
2148
2149 switch (ctxt->modrm) {
2150 case 0xd8: /* VMRUN */
2151 return SVM_INSTR_VMRUN;
2152 case 0xda: /* VMLOAD */
2153 return SVM_INSTR_VMLOAD;
2154 case 0xdb: /* VMSAVE */
2155 return SVM_INSTR_VMSAVE;
2156 default:
2157 break;
2158 }
2159
2160 return NONE_SVM_INSTR;
2161}
2162
2163static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2164{
Wei Huang14c2bf82021-01-26 03:18:31 -05002165 const int guest_mode_exit_codes[] = {
2166 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2167 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2168 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2169 };
Paolo Bonzini63129752021-03-02 14:40:39 -05002170 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
Bandan Das82a11e9c2021-01-26 03:18:29 -05002171 [SVM_INSTR_VMRUN] = vmrun_interception,
2172 [SVM_INSTR_VMLOAD] = vmload_interception,
2173 [SVM_INSTR_VMSAVE] = vmsave_interception,
2174 };
2175 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002176 int ret;
Bandan Das82a11e9c2021-01-26 03:18:29 -05002177
Wei Huang14c2bf82021-01-26 03:18:31 -05002178 if (is_guest_mode(vcpu)) {
Sean Christopherson2df8d382021-02-23 16:56:26 -08002179 /* Returns '1' or -errno on failure, '0' on success. */
Sean Christopherson3a87c7e2021-03-02 09:45:15 -08002180 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
Sean Christopherson2df8d382021-02-23 16:56:26 -08002181 if (ret)
2182 return ret;
2183 return 1;
2184 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002185 return svm_instr_handlers[opcode](vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002186}
2187
2188/*
2189 * #GP handling code. Note that #GP can be triggered under the following two
2190 * cases:
2191 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2192 * some AMD CPUs when EAX of these instructions are in the reserved memory
2193 * regions (e.g. SMM memory on host).
2194 * 2) VMware backdoor
2195 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002196static int gp_interception(struct kvm_vcpu *vcpu)
Bandan Das82a11e9c2021-01-26 03:18:29 -05002197{
Paolo Bonzini63129752021-03-02 14:40:39 -05002198 struct vcpu_svm *svm = to_svm(vcpu);
Bandan Das82a11e9c2021-01-26 03:18:29 -05002199 u32 error_code = svm->vmcb->control.exit_info_1;
2200 int opcode;
2201
2202 /* Both #GP cases have zero error_code */
2203 if (error_code)
2204 goto reinject;
2205
2206 /* Decode the instruction for usage later */
2207 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2208 goto reinject;
2209
2210 opcode = svm_instr_opcode(vcpu);
2211
2212 if (opcode == NONE_SVM_INSTR) {
2213 if (!enable_vmware_backdoor)
2214 goto reinject;
2215
2216 /*
2217 * VMware backdoor emulation on #GP interception only handles
2218 * IN{S}, OUT{S}, and RDPMC.
2219 */
Wei Huang14c2bf82021-01-26 03:18:31 -05002220 if (!is_guest_mode(vcpu))
2221 return kvm_emulate_instruction(vcpu,
Bandan Das82a11e9c2021-01-26 03:18:29 -05002222 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2223 } else
2224 return emulate_svm_instr(vcpu, opcode);
2225
2226reinject:
2227 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2228 return 1;
2229}
2230
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002231void svm_set_gif(struct vcpu_svm *svm, bool value)
2232{
2233 if (value) {
2234 /*
2235 * If VGIF is enabled, the STGI intercept is only added to
2236 * detect the opening of the SMI/NMI window; remove it now.
2237 * Likewise, clear the VINTR intercept, we will set it
2238 * again while processing KVM_REQ_EVENT if needed.
2239 */
2240 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02002241 svm_clr_intercept(svm, INTERCEPT_STGI);
2242 if (svm_is_intercept(svm, INTERCEPT_VINTR))
Paolo Bonziniffdf7f92020-05-22 12:18:27 -04002243 svm_clear_vintr(svm);
2244
2245 enable_gif(svm);
2246 if (svm->vcpu.arch.smi_pending ||
2247 svm->vcpu.arch.nmi_pending ||
2248 kvm_cpu_has_injectable_intr(&svm->vcpu))
2249 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2250 } else {
2251 disable_gif(svm);
2252
2253 /*
2254 * After a CLGI no interrupts should come. But if vGIF is
2255 * in use, we still rely on the VINTR intercept (rather than
2256 * STGI) to detect an open interrupt window.
2257 */
2258 if (!vgif_enabled(svm))
2259 svm_clear_vintr(svm);
2260 }
2261}
2262
Paolo Bonzini63129752021-03-02 14:40:39 -05002263static int stgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002264{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002265 int ret;
2266
Paolo Bonzini63129752021-03-02 14:40:39 -05002267 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002268 return 1;
2269
Paolo Bonzini63129752021-03-02 14:40:39 -05002270 ret = kvm_skip_emulated_instruction(vcpu);
2271 svm_set_gif(to_svm(vcpu), true);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002272 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002273}
2274
Paolo Bonzini63129752021-03-02 14:40:39 -05002275static int clgi_interception(struct kvm_vcpu *vcpu)
Alexander Graf1371d902008-11-25 20:17:04 +01002276{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002277 int ret;
2278
Paolo Bonzini63129752021-03-02 14:40:39 -05002279 if (nested_svm_check_permissions(vcpu))
Alexander Graf1371d902008-11-25 20:17:04 +01002280 return 1;
2281
Paolo Bonzini63129752021-03-02 14:40:39 -05002282 ret = kvm_skip_emulated_instruction(vcpu);
2283 svm_set_gif(to_svm(vcpu), false);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002284 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002285}
2286
Paolo Bonzini63129752021-03-02 14:40:39 -05002287static int invlpga_interception(struct kvm_vcpu *vcpu)
Alexander Grafff092382009-06-15 15:21:24 +02002288{
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002289 gva_t gva = kvm_rax_read(vcpu);
2290 u32 asid = kvm_rcx_read(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002291
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002292 /* FIXME: Handle an address size prefix. */
2293 if (!is_long_mode(vcpu))
2294 gva = (u32)gva;
2295
2296 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
Joerg Roedelec1ff792009-10-09 16:08:31 +02002297
Alexander Grafff092382009-06-15 15:21:24 +02002298 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonbc9eff62021-04-21 19:21:27 -07002299 kvm_mmu_invlpg(vcpu, gva);
Alexander Grafff092382009-06-15 15:21:24 +02002300
Paolo Bonzini63129752021-03-02 14:40:39 -05002301 return kvm_skip_emulated_instruction(vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002302}
2303
Paolo Bonzini63129752021-03-02 14:40:39 -05002304static int skinit_interception(struct kvm_vcpu *vcpu)
Joerg Roedel532a46b2009-10-09 16:08:32 +02002305{
Paolo Bonzini63129752021-03-02 14:40:39 -05002306 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002307
Paolo Bonzini63129752021-03-02 14:40:39 -05002308 kvm_queue_exception(vcpu, UD_VECTOR);
Joerg Roedel532a46b2009-10-09 16:08:32 +02002309 return 1;
2310}
2311
Paolo Bonzini63129752021-03-02 14:40:39 -05002312static int task_switch_interception(struct kvm_vcpu *vcpu)
David Kaplandab429a2015-03-02 13:43:37 -06002313{
Paolo Bonzini63129752021-03-02 14:40:39 -05002314 struct vcpu_svm *svm = to_svm(vcpu);
Izik Eidus37817f22008-03-24 23:14:53 +02002315 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002316 int reason;
2317 int int_type = svm->vmcb->control.exit_int_info &
2318 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002319 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002320 uint32_t type =
2321 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2322 uint32_t idt_v =
2323 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002324 bool has_error_code = false;
2325 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002326
2327 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002328
Izik Eidus37817f22008-03-24 23:14:53 +02002329 if (svm->vmcb->control.exit_info_2 &
2330 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002331 reason = TASK_SWITCH_IRET;
2332 else if (svm->vmcb->control.exit_info_2 &
2333 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2334 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002335 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002336 reason = TASK_SWITCH_GATE;
2337 else
2338 reason = TASK_SWITCH_CALL;
2339
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002340 if (reason == TASK_SWITCH_GATE) {
2341 switch (type) {
2342 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05002343 vcpu->arch.nmi_injected = false;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002344 break;
2345 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002346 if (svm->vmcb->control.exit_info_2 &
2347 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2348 has_error_code = true;
2349 error_code =
2350 (u32)svm->vmcb->control.exit_info_2;
2351 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002352 kvm_clear_exception_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002353 break;
2354 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05002355 kvm_clear_interrupt_queue(vcpu);
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002356 break;
2357 default:
2358 break;
2359 }
2360 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002361
Gleb Natapov8317c292009-04-12 13:37:02 +03002362 if (reason != TASK_SWITCH_GATE ||
2363 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2364 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002365 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Paolo Bonzini63129752021-03-02 14:40:39 -05002366 if (!skip_emulated_instruction(vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002367 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002368 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002369
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002370 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2371 int_vec = -1;
2372
Paolo Bonzini63129752021-03-02 14:40:39 -05002373 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002374 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002375}
2376
Paolo Bonzini63129752021-03-02 14:40:39 -05002377static int iret_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002378{
Paolo Bonzini63129752021-03-02 14:40:39 -05002379 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002380
Paolo Bonzini63129752021-03-02 14:40:39 -05002381 ++vcpu->stat.nmi_window_exits;
2382 vcpu->arch.hflags |= HF_IRET_MASK;
2383 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002384 svm_clr_intercept(svm, INTERCEPT_IRET);
Paolo Bonzini63129752021-03-02 14:40:39 -05002385 svm->nmi_iret_rip = kvm_rip_read(vcpu);
Tom Lendacky4444dfe2020-12-14 11:16:03 -05002386 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002387 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002388 return 1;
2389}
2390
Paolo Bonzini63129752021-03-02 14:40:39 -05002391static int invlpg_interception(struct kvm_vcpu *vcpu)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002392{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002393 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002394 return kvm_emulate_instruction(vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002395
Paolo Bonzini63129752021-03-02 14:40:39 -05002396 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2397 return kvm_skip_emulated_instruction(vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002398}
2399
Paolo Bonzini63129752021-03-02 14:40:39 -05002400static int emulate_on_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002401{
Paolo Bonzini63129752021-03-02 14:40:39 -05002402 return kvm_emulate_instruction(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002403}
2404
Paolo Bonzini63129752021-03-02 14:40:39 -05002405static int rsm_interception(struct kvm_vcpu *vcpu)
Brijesh Singh7607b712018-02-19 10:14:44 -06002406{
Paolo Bonzini63129752021-03-02 14:40:39 -05002407 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002408}
2409
Paolo Bonzini63129752021-03-02 14:40:39 -05002410static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
Xiubo Li52eb5a62015-03-13 17:39:45 +08002411 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002412{
Paolo Bonzini63129752021-03-02 14:40:39 -05002413 struct vcpu_svm *svm = to_svm(vcpu);
2414 unsigned long cr0 = vcpu->arch.cr0;
Joerg Roedel628afd22011-04-04 12:39:36 +02002415 bool ret = false;
Joerg Roedel628afd22011-04-04 12:39:36 +02002416
Paolo Bonzini63129752021-03-02 14:40:39 -05002417 if (!is_guest_mode(vcpu) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05002418 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
Joerg Roedel628afd22011-04-04 12:39:36 +02002419 return false;
2420
2421 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2422 val &= ~SVM_CR0_SELECTIVE_MASK;
2423
2424 if (cr0 ^ val) {
2425 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2426 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2427 }
2428
2429 return ret;
2430}
2431
Andre Przywara7ff76d52010-12-21 11:12:04 +01002432#define CR_VALID (1ULL << 63)
2433
Paolo Bonzini63129752021-03-02 14:40:39 -05002434static int cr_interception(struct kvm_vcpu *vcpu)
Andre Przywara7ff76d52010-12-21 11:12:04 +01002435{
Paolo Bonzini63129752021-03-02 14:40:39 -05002436 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002437 int reg, cr;
2438 unsigned long val;
2439 int err;
2440
2441 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002442 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002443
2444 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
Paolo Bonzini63129752021-03-02 14:40:39 -05002445 return emulate_on_interception(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002446
2447 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002448 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2449 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2450 else
2451 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002452
2453 err = 0;
2454 if (cr >= 16) { /* mov to cr */
2455 cr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002456 val = kvm_register_read(vcpu, reg);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002457 trace_kvm_cr_write(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002458 switch (cr) {
2459 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002460 if (!check_selective_cr0_intercepted(vcpu, val))
2461 err = kvm_set_cr0(vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002462 else
2463 return 1;
2464
Andre Przywara7ff76d52010-12-21 11:12:04 +01002465 break;
2466 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002467 err = kvm_set_cr3(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002468 break;
2469 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002470 err = kvm_set_cr4(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002471 break;
2472 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002473 err = kvm_set_cr8(vcpu, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002474 break;
2475 default:
2476 WARN(1, "unhandled write to CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002477 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002478 return 1;
2479 }
2480 } else { /* mov from cr */
2481 switch (cr) {
2482 case 0:
Paolo Bonzini63129752021-03-02 14:40:39 -05002483 val = kvm_read_cr0(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002484 break;
2485 case 2:
Paolo Bonzini63129752021-03-02 14:40:39 -05002486 val = vcpu->arch.cr2;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002487 break;
2488 case 3:
Paolo Bonzini63129752021-03-02 14:40:39 -05002489 val = kvm_read_cr3(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002490 break;
2491 case 4:
Paolo Bonzini63129752021-03-02 14:40:39 -05002492 val = kvm_read_cr4(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002493 break;
2494 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002495 val = kvm_get_cr8(vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002496 break;
2497 default:
2498 WARN(1, "unhandled read from CR%d", cr);
Paolo Bonzini63129752021-03-02 14:40:39 -05002499 kvm_queue_exception(vcpu, UD_VECTOR);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002500 return 1;
2501 }
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002502 kvm_register_write(vcpu, reg, val);
Haiwei Li95b28ac2020-09-04 19:25:29 +08002503 trace_kvm_cr_read(cr, val);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002504 }
Paolo Bonzini63129752021-03-02 14:40:39 -05002505 return kvm_complete_insn_gp(vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002506}
2507
Paolo Bonzini63129752021-03-02 14:40:39 -05002508static int cr_trap(struct kvm_vcpu *vcpu)
Tom Lendackyf27ad382020-12-10 11:09:56 -06002509{
Paolo Bonzini63129752021-03-02 14:40:39 -05002510 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002511 unsigned long old_value, new_value;
2512 unsigned int cr;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002513 int ret = 0;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002514
2515 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2516
2517 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2518 switch (cr) {
2519 case 0:
2520 old_value = kvm_read_cr0(vcpu);
2521 svm_set_cr0(vcpu, new_value);
2522
2523 kvm_post_set_cr0(vcpu, old_value, new_value);
2524 break;
Tom Lendacky5b51cb12020-12-10 11:09:57 -06002525 case 4:
2526 old_value = kvm_read_cr4(vcpu);
2527 svm_set_cr4(vcpu, new_value);
2528
2529 kvm_post_set_cr4(vcpu, old_value, new_value);
2530 break;
Tom Lendackyd1949b92020-12-10 11:09:58 -06002531 case 8:
Paolo Bonzini63129752021-03-02 14:40:39 -05002532 ret = kvm_set_cr8(vcpu, new_value);
Tom Lendackyd1949b92020-12-10 11:09:58 -06002533 break;
Tom Lendackyf27ad382020-12-10 11:09:56 -06002534 default:
2535 WARN(1, "unhandled CR%d write trap", cr);
2536 kvm_queue_exception(vcpu, UD_VECTOR);
2537 return 1;
2538 }
2539
Tom Lendackyd1949b92020-12-10 11:09:58 -06002540 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendackyf27ad382020-12-10 11:09:56 -06002541}
2542
Paolo Bonzini63129752021-03-02 14:40:39 -05002543static int dr_interception(struct kvm_vcpu *vcpu)
Andre Przywaracae37972010-12-21 11:12:05 +01002544{
Paolo Bonzini63129752021-03-02 14:40:39 -05002545 struct vcpu_svm *svm = to_svm(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002546 int reg, dr;
2547 unsigned long val;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002548 int err = 0;
Andre Przywaracae37972010-12-21 11:12:05 +01002549
Paolo Bonzini63129752021-03-02 14:40:39 -05002550 if (vcpu->guest_debug == 0) {
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002551 /*
2552 * No more DR vmexits; force a reload of the debug registers
2553 * and reenter on this instruction. The next vmexit will
2554 * retrieve the full state of the debug registers.
2555 */
2556 clr_dr_intercepts(svm);
Paolo Bonzini63129752021-03-02 14:40:39 -05002557 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002558 return 1;
2559 }
2560
Andre Przywaracae37972010-12-21 11:12:05 +01002561 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
Paolo Bonzini63129752021-03-02 14:40:39 -05002562 return emulate_on_interception(vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002563
2564 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2565 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
Paolo Bonzini996ff542020-12-14 07:49:54 -05002566 if (dr >= 16) { /* mov to DRn */
2567 dr -= 16;
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002568 val = kvm_register_read(vcpu, reg);
Paolo Bonzini63129752021-03-02 14:40:39 -05002569 err = kvm_set_dr(vcpu, dr, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002570 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05002571 kvm_get_dr(vcpu, dr, &val);
Sean Christopherson27b4a9c42021-04-21 19:21:28 -07002572 kvm_register_write(vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002573 }
2574
Paolo Bonzini63129752021-03-02 14:40:39 -05002575 return kvm_complete_insn_gp(vcpu, err);
Andre Przywaracae37972010-12-21 11:12:05 +01002576}
2577
Paolo Bonzini63129752021-03-02 14:40:39 -05002578static int cr8_write_interception(struct kvm_vcpu *vcpu)
Joerg Roedel1d075432007-12-06 21:02:25 +01002579{
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002580 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002581
Paolo Bonzini63129752021-03-02 14:40:39 -05002582 u8 cr8_prev = kvm_get_cr8(vcpu);
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002583 /* instruction emulation calls kvm_set_cr8() */
Paolo Bonzini63129752021-03-02 14:40:39 -05002584 r = cr_interception(vcpu);
2585 if (lapic_in_kernel(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002586 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002587 if (cr8_prev <= kvm_get_cr8(vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002588 return r;
Paolo Bonzini63129752021-03-02 14:40:39 -05002589 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
Joerg Roedel1d075432007-12-06 21:02:25 +01002590 return 0;
2591}
2592
Paolo Bonzini63129752021-03-02 14:40:39 -05002593static int efer_trap(struct kvm_vcpu *vcpu)
Tom Lendacky2985afb2020-12-10 11:09:55 -06002594{
2595 struct msr_data msr_info;
2596 int ret;
2597
2598 /*
2599 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2600 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2601 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2602 * the guest doesn't have X86_FEATURE_SVM.
2603 */
2604 msr_info.host_initiated = false;
2605 msr_info.index = MSR_EFER;
Paolo Bonzini63129752021-03-02 14:40:39 -05002606 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2607 ret = kvm_set_msr_common(vcpu, &msr_info);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002608
Paolo Bonzini63129752021-03-02 14:40:39 -05002609 return kvm_complete_insn_gp(vcpu, ret);
Tom Lendacky2985afb2020-12-10 11:09:55 -06002610}
2611
Tom Lendacky801e4592018-02-21 13:39:51 -06002612static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2613{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002614 msr->data = 0;
2615
2616 switch (msr->index) {
2617 case MSR_F10H_DECFG:
2618 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2619 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2620 break;
Vitaly Kuznetsovd574c532020-07-10 17:25:59 +02002621 case MSR_IA32_PERF_CAPABILITIES:
2622 return 0;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002623 default:
Peter Xu12bc2132020-06-22 18:04:42 -04002624 return KVM_MSR_RET_INVALID;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002625 }
2626
2627 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002628}
2629
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002630static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002632 struct vcpu_svm *svm = to_svm(vcpu);
2633
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002634 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04002635 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002636 msr_info->data = svm->vmcb01.ptr->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002637 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002638#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002639 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002640 msr_info->data = svm->vmcb01.ptr->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 break;
2642 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002643 msr_info->data = svm->vmcb01.ptr->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 break;
2645 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002646 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002647 break;
2648 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002649 msr_info->data = svm->vmcb01.ptr->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002650 break;
2651#endif
2652 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002653 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002654 break;
2655 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002656 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2657 if (guest_cpuid_is_intel(vcpu))
2658 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002659 break;
2660 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002661 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2662 if (guest_cpuid_is_intel(vcpu))
2663 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002664 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002665 case MSR_TSC_AUX:
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002666 if (tsc_aux_uret_slot < 0)
Paolo Bonzini46896c72015-11-12 14:49:16 +01002667 return 1;
Sean Christopherson6f2b2962021-04-23 15:34:01 -07002668 if (!msr_info->host_initiated &&
Sean Christopherson36fa06f2021-05-04 10:17:26 -07002669 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
2670 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
Sean Christopherson6f2b2962021-04-23 15:34:01 -07002671 return 1;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002672 msr_info->data = svm->tsc_aux;
2673 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002674 /*
2675 * Nobody will change the following 5 values in the VMCB so we can
2676 * safely return them on rdmsr. They will always be 0 until LBRV is
2677 * implemented.
2678 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002679 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002680 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002681 break;
2682 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002683 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002684 break;
2685 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002686 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002687 break;
2688 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002689 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002690 break;
2691 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002692 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002693 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002694 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002695 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002696 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002697 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002698 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002699 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002700 case MSR_IA32_SPEC_CTRL:
2701 if (!msr_info->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002702 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002703 return 1;
2704
Babu Mogerd00b99c2021-02-17 10:56:04 -05002705 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2706 msr_info->data = svm->vmcb->save.spec_ctrl;
2707 else
2708 msr_info->data = svm->spec_ctrl;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002709 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002710 case MSR_AMD64_VIRT_SPEC_CTRL:
2711 if (!msr_info->host_initiated &&
2712 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2713 return 1;
2714
2715 msr_info->data = svm->virt_spec_ctrl;
2716 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002717 case MSR_F15H_IC_CFG: {
2718
2719 int family, model;
2720
2721 family = guest_cpuid_family(vcpu);
2722 model = guest_cpuid_model(vcpu);
2723
2724 if (family < 0 || model < 0)
2725 return kvm_get_msr_common(vcpu, msr_info);
2726
2727 msr_info->data = 0;
2728
2729 if (family == 0x15 &&
2730 (model >= 0x2 && model < 0x20))
2731 msr_info->data = 0x1E;
2732 }
2733 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002734 case MSR_F10H_DECFG:
2735 msr_info->data = svm->msr_decfg;
2736 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002737 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002738 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002739 }
2740 return 0;
2741}
2742
Tom Lendackyf1c63662020-12-14 10:29:50 -05002743static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2744{
2745 struct vcpu_svm *svm = to_svm(vcpu);
Tom Lendackya3ba26e2021-04-09 09:38:42 -05002746 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->ghcb))
Paolo Bonzini63129752021-03-02 14:40:39 -05002747 return kvm_complete_insn_gp(vcpu, err);
Tom Lendackyf1c63662020-12-14 10:29:50 -05002748
2749 ghcb_set_sw_exit_info_1(svm->ghcb, 1);
2750 ghcb_set_sw_exit_info_2(svm->ghcb,
2751 X86_TRAP_GP |
2752 SVM_EVTINJ_TYPE_EXEPT |
2753 SVM_EVTINJ_VALID);
2754 return 1;
2755}
2756
Joerg Roedel4a810182010-02-24 18:59:15 +01002757static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2758{
2759 struct vcpu_svm *svm = to_svm(vcpu);
2760 int svm_dis, chg_mask;
2761
2762 if (data & ~SVM_VM_CR_VALID_MASK)
2763 return 1;
2764
2765 chg_mask = SVM_VM_CR_VALID_MASK;
2766
2767 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2768 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2769
2770 svm->nested.vm_cr_msr &= ~chg_mask;
2771 svm->nested.vm_cr_msr |= (data & chg_mask);
2772
2773 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2774
2775 /* check for svm_disable while efer.svme is set */
2776 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2777 return 1;
2778
2779 return 0;
2780}
2781
Will Auld8fe8ab42012-11-29 12:42:12 -08002782static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002784 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002785 int r;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002786
Will Auld8fe8ab42012-11-29 12:42:12 -08002787 u32 ecx = msr->index;
2788 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002789 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02002790 case MSR_IA32_CR_PAT:
2791 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2792 return 1;
2793 vcpu->arch.pat = data;
Cathy Avery4995a362021-01-13 07:07:52 -05002794 svm->vmcb01.ptr->save.g_pat = data;
2795 if (is_guest_mode(vcpu))
2796 nested_vmcb02_compute_g_pat(svm);
Joerg Roedel06e78522020-06-25 10:03:23 +02002797 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Paolo Bonzini15038e12017-10-26 09:13:27 +02002798 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002799 case MSR_IA32_SPEC_CTRL:
2800 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002801 !guest_has_spec_ctrl_msr(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002802 return 1;
2803
Maxim Levitsky841c2be2020-07-08 14:57:31 +03002804 if (kvm_spec_ctrl_test_value(data))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002805 return 1;
2806
Babu Mogerd00b99c2021-02-17 10:56:04 -05002807 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2808 svm->vmcb->save.spec_ctrl = data;
2809 else
2810 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002811 if (!data)
2812 break;
2813
2814 /*
2815 * For non-nested:
2816 * When it's written (to non-zero) for the first time, pass
2817 * it through.
2818 *
2819 * For nested:
2820 * The handling of the MSR bitmap for L2 guests is done in
2821 * nested_svm_vmrun_msrpm.
2822 * We update the L1 MSR bit as well since it will end up
2823 * touching the MSR anyway now.
2824 */
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002825 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002826 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002827 case MSR_IA32_PRED_CMD:
2828 if (!msr->host_initiated &&
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002829 !guest_has_pred_cmd_msr(vcpu))
Ashok Raj15d45072018-02-01 22:59:43 +01002830 return 1;
2831
2832 if (data & ~PRED_CMD_IBPB)
2833 return 1;
Paolo Bonzini39485ed2020-12-03 09:40:15 -05002834 if (!boot_cpu_has(X86_FEATURE_IBPB))
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002835 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002836 if (!data)
2837 break;
2838
2839 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002840 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
Ashok Raj15d45072018-02-01 22:59:43 +01002841 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002842 case MSR_AMD64_VIRT_SPEC_CTRL:
2843 if (!msr->host_initiated &&
2844 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2845 return 1;
2846
2847 if (data & ~SPEC_CTRL_SSBD)
2848 return 1;
2849
2850 svm->virt_spec_ctrl = data;
2851 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002852 case MSR_STAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002853 svm->vmcb01.ptr->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002855#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 case MSR_LSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002857 svm->vmcb01.ptr->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002858 break;
2859 case MSR_CSTAR:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002860 svm->vmcb01.ptr->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 break;
2862 case MSR_KERNEL_GS_BASE:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002863 svm->vmcb01.ptr->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002864 break;
2865 case MSR_SYSCALL_MASK:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002866 svm->vmcb01.ptr->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 break;
2868#endif
2869 case MSR_IA32_SYSENTER_CS:
Maxim Levitskycc3ed802021-02-10 18:54:36 +02002870 svm->vmcb01.ptr->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002871 break;
2872 case MSR_IA32_SYSENTER_EIP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002873 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2874 /*
2875 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2876 * when we spoof an Intel vendor ID (for cross vendor migration).
2877 * In this case we use this intercept to track the high
2878 * 32 bit part of these msrs to support Intel's
2879 * implementation of SYSENTER/SYSEXIT.
2880 */
2881 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002882 break;
2883 case MSR_IA32_SYSENTER_ESP:
Maxim Levitskyadc2a232021-04-01 14:19:28 +03002884 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2885 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002886 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002887 case MSR_TSC_AUX:
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002888 if (tsc_aux_uret_slot < 0)
Paolo Bonzini46896c72015-11-12 14:49:16 +01002889 return 1;
2890
Sean Christopherson6f2b2962021-04-23 15:34:01 -07002891 if (!msr->host_initiated &&
Sean Christopherson36fa06f2021-05-04 10:17:26 -07002892 !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP) &&
2893 !guest_cpuid_has(vcpu, X86_FEATURE_RDPID))
Sean Christopherson6f2b2962021-04-23 15:34:01 -07002894 return 1;
2895
Paolo Bonzini46896c72015-11-12 14:49:16 +01002896 /*
Sean Christophersondbd61272021-04-23 15:34:02 -07002897 * Per Intel's SDM, bits 63:32 are reserved, but AMD's APM has
2898 * incomplete and conflicting architectural behavior. Current
2899 * AMD CPUs completely ignore bits 63:32, i.e. they aren't
2900 * reserved and always read as zeros. Emulate AMD CPU behavior
2901 * to avoid explosions if the vCPU is migrated from an AMD host
2902 * to an Intel host.
Paolo Bonzini46896c72015-11-12 14:49:16 +01002903 */
Sean Christophersondbd61272021-04-23 15:34:02 -07002904 data = (u32)data;
2905
2906 /*
Sean Christopherson844d69c2021-04-23 15:34:04 -07002907 * TSC_AUX is usually changed only during boot and never read
2908 * directly. Intercept TSC_AUX instead of exposing it to the
2909 * guest via direct_access_msrs, and switch it via user return.
Paolo Bonzini46896c72015-11-12 14:49:16 +01002910 */
Sean Christopherson844d69c2021-04-23 15:34:04 -07002911 preempt_disable();
Sean Christopherson0caa0a72021-05-04 10:17:25 -07002912 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
Sean Christopherson844d69c2021-04-23 15:34:04 -07002913 preempt_enable();
2914 if (r)
2915 return 1;
2916
Paolo Bonzini46896c72015-11-12 14:49:16 +01002917 svm->tsc_aux = data;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002918 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002919 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002920 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002921 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2922 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002923 break;
2924 }
2925 if (data & DEBUGCTL_RESERVED_BITS)
2926 return 1;
2927
2928 svm->vmcb->save.dbgctl = data;
Joerg Roedel06e78522020-06-25 10:03:23 +02002929 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002930 if (data & (1ULL<<0))
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002931 svm_enable_lbrv(vcpu);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002932 else
Aaron Lewis476c9bd2020-09-25 16:34:18 +02002933 svm_disable_lbrv(vcpu);
Joerg Roedela2938c82008-02-13 16:30:28 +01002934 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002935 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002936 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002937 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002938 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002939 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002940 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03002941 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002942 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002943 case MSR_F10H_DECFG: {
2944 struct kvm_msr_entry msr_entry;
2945
2946 msr_entry.index = msr->index;
2947 if (svm_get_msr_feature(&msr_entry))
2948 return 1;
2949
2950 /* Check the supported bits */
2951 if (data & ~msr_entry.data)
2952 return 1;
2953
2954 /* Don't allow the guest to change a bit, #GP */
2955 if (!msr->host_initiated && (data ^ msr_entry.data))
2956 return 1;
2957
2958 svm->msr_decfg = data;
2959 break;
2960 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002961 case MSR_IA32_APICBASE:
2962 if (kvm_vcpu_apicv_active(vcpu))
2963 avic_update_vapic_bar(to_svm(vcpu), data);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002964 fallthrough;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002965 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08002966 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002967 }
2968 return 0;
2969}
2970
Paolo Bonzini63129752021-03-02 14:40:39 -05002971static int msr_interception(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002972{
Paolo Bonzini63129752021-03-02 14:40:39 -05002973 if (to_svm(vcpu)->vmcb->control.exit_info_1)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002974 return kvm_emulate_wrmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002975 else
Sean Christopherson5ff3a352021-02-04 16:57:47 -08002976 return kvm_emulate_rdmsr(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002977}
2978
Paolo Bonzini63129752021-03-02 14:40:39 -05002979static int interrupt_window_interception(struct kvm_vcpu *vcpu)
Dor Laorc1150d82007-01-05 16:36:24 -08002980{
Paolo Bonzini63129752021-03-02 14:40:39 -05002981 kvm_make_request(KVM_REQ_EVENT, vcpu);
2982 svm_clear_vintr(to_svm(vcpu));
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002983
2984 /*
2985 * For AVIC, the only reason to end up here is ExtINTs.
2986 * In this case AVIC was temporarily disabled for
2987 * requesting the IRQ window and we have to re-enable it.
2988 */
Paolo Bonzini63129752021-03-02 14:40:39 -05002989 svm_toggle_avic_for_irq_window(vcpu, true);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002990
Paolo Bonzini63129752021-03-02 14:40:39 -05002991 ++vcpu->stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002992 return 1;
2993}
2994
Paolo Bonzini63129752021-03-02 14:40:39 -05002995static int pause_interception(struct kvm_vcpu *vcpu)
Mark Langsdorf565d0992009-10-06 14:25:02 -05002996{
Tom Lendackyf1c63662020-12-14 10:29:50 -05002997 bool in_kernel;
2998
2999 /*
3000 * CPL is not made available for an SEV-ES guest, therefore
3001 * vcpu->arch.preempted_in_kernel can never be true. Just
3002 * set in_kernel to false as well.
3003 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003004 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003005
Wanpeng Li830f01b2020-07-31 11:12:21 +08003006 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04003007 grow_ple_window(vcpu);
3008
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08003009 kvm_vcpu_on_spin(vcpu, in_kernel);
Sean Christophersonc8781fe2021-02-04 16:57:50 -08003010 return kvm_skip_emulated_instruction(vcpu);
Mark Langsdorf565d0992009-10-06 14:25:02 -05003011}
3012
Paolo Bonzini63129752021-03-02 14:40:39 -05003013static int invpcid_interception(struct kvm_vcpu *vcpu)
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003014{
Paolo Bonzini63129752021-03-02 14:40:39 -05003015 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger4407a792020-09-11 14:29:19 -05003016 unsigned long type;
3017 gva_t gva;
3018
3019 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3020 kvm_queue_exception(vcpu, UD_VECTOR);
3021 return 1;
3022 }
3023
3024 /*
3025 * For an INVPCID intercept:
3026 * EXITINFO1 provides the linear address of the memory operand.
3027 * EXITINFO2 provides the contents of the register operand.
3028 */
3029 type = svm->vmcb->control.exit_info_2;
3030 gva = svm->vmcb->control.exit_info_1;
3031
3032 if (type > 3) {
3033 kvm_inject_gp(vcpu, 0);
3034 return 1;
3035 }
3036
3037 return kvm_handle_invpcid(vcpu, type, gva);
3038}
3039
Paolo Bonzini63129752021-03-02 14:40:39 -05003040static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003041 [SVM_EXIT_READ_CR0] = cr_interception,
3042 [SVM_EXIT_READ_CR3] = cr_interception,
3043 [SVM_EXIT_READ_CR4] = cr_interception,
3044 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003045 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003046 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003047 [SVM_EXIT_WRITE_CR3] = cr_interception,
3048 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003049 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003050 [SVM_EXIT_READ_DR0] = dr_interception,
3051 [SVM_EXIT_READ_DR1] = dr_interception,
3052 [SVM_EXIT_READ_DR2] = dr_interception,
3053 [SVM_EXIT_READ_DR3] = dr_interception,
3054 [SVM_EXIT_READ_DR4] = dr_interception,
3055 [SVM_EXIT_READ_DR5] = dr_interception,
3056 [SVM_EXIT_READ_DR6] = dr_interception,
3057 [SVM_EXIT_READ_DR7] = dr_interception,
3058 [SVM_EXIT_WRITE_DR0] = dr_interception,
3059 [SVM_EXIT_WRITE_DR1] = dr_interception,
3060 [SVM_EXIT_WRITE_DR2] = dr_interception,
3061 [SVM_EXIT_WRITE_DR3] = dr_interception,
3062 [SVM_EXIT_WRITE_DR4] = dr_interception,
3063 [SVM_EXIT_WRITE_DR5] = dr_interception,
3064 [SVM_EXIT_WRITE_DR6] = dr_interception,
3065 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003066 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3067 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003068 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003069 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003070 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003071 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02003072 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003073 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003074 [SVM_EXIT_NMI] = nmi_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003075 [SVM_EXIT_SMI] = kvm_emulate_as_nop,
3076 [SVM_EXIT_INIT] = kvm_emulate_as_nop,
Dor Laorc1150d82007-01-05 16:36:24 -08003077 [SVM_EXIT_VINTR] = interrupt_window_interception,
Sean Christopherson32c23c72021-02-04 16:57:49 -08003078 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003079 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003080 [SVM_EXIT_IRET] = iret_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003081 [SVM_EXIT_INVD] = kvm_emulate_invd,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003082 [SVM_EXIT_PAUSE] = pause_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003083 [SVM_EXIT_HLT] = kvm_emulate_halt,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003084 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003085 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003086 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003087 [SVM_EXIT_MSR] = msr_interception,
3088 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003089 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003090 [SVM_EXIT_VMRUN] = vmrun_interception,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003091 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
Alexander Graf55426752008-11-25 20:17:06 +01003092 [SVM_EXIT_VMLOAD] = vmload_interception,
3093 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003094 [SVM_EXIT_STGI] = stgi_interception,
3095 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003096 [SVM_EXIT_SKINIT] = skinit_interception,
Sean Christopherson3b195ac2021-05-04 10:17:22 -07003097 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003098 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3099 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3100 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
Sean Christopherson92f98952021-02-04 16:57:46 -08003101 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003102 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
Tom Lendacky2985afb2020-12-10 11:09:55 -06003103 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
Tom Lendackyf27ad382020-12-10 11:09:56 -06003104 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
Tom Lendacky5b51cb12020-12-10 11:09:57 -06003105 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
Tom Lendackyd1949b92020-12-10 11:09:58 -06003106 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
Babu Moger4407a792020-09-11 14:29:19 -05003107 [SVM_EXIT_INVPCID] = invpcid_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003108 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06003109 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003110 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3111 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Tom Lendacky291bd202020-12-10 11:09:47 -06003112 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003113};
3114
Joe Perchesae8cc052011-04-24 22:00:50 -07003115static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003116{
3117 struct vcpu_svm *svm = to_svm(vcpu);
3118 struct vmcb_control_area *control = &svm->vmcb->control;
3119 struct vmcb_save_area *save = &svm->vmcb->save;
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003120 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003121
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003122 if (!dump_invalid_vmcb) {
3123 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3124 return;
3125 }
3126
Joerg Roedel3f10c842010-05-05 16:04:42 +02003127 pr_err("VMCB Control Area:\n");
Babu Moger03bfeeb2020-09-11 14:28:05 -05003128 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3129 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
Babu Moger30abaa882020-09-11 14:28:12 -05003130 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3131 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
Babu Moger9780d512020-09-11 14:28:20 -05003132 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
Babu Mogerc62e2e92020-09-11 14:28:28 -05003133 pr_err("%-20s%08x %08x\n", "intercepts:",
3134 control->intercepts[INTERCEPT_WORD3],
3135 control->intercepts[INTERCEPT_WORD4]);
Joe Perchesae8cc052011-04-24 22:00:50 -07003136 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003137 pr_err("%-20s%d\n", "pause filter threshold:",
3138 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003139 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3140 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3141 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3142 pr_err("%-20s%d\n", "asid:", control->asid);
3143 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3144 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3145 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3146 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3147 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3148 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3149 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3150 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3151 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3152 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3153 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003154 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Tom Lendacky291bd202020-12-10 11:09:47 -06003155 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
Joe Perchesae8cc052011-04-24 22:00:50 -07003156 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3157 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003158 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003159 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003160 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3161 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3162 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Tom Lendacky376c6d22020-12-10 11:10:06 -06003163 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003164 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003165 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3166 "es:",
3167 save->es.selector, save->es.attrib,
3168 save->es.limit, save->es.base);
3169 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3170 "cs:",
3171 save->cs.selector, save->cs.attrib,
3172 save->cs.limit, save->cs.base);
3173 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3174 "ss:",
3175 save->ss.selector, save->ss.attrib,
3176 save->ss.limit, save->ss.base);
3177 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3178 "ds:",
3179 save->ds.selector, save->ds.attrib,
3180 save->ds.limit, save->ds.base);
3181 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3182 "fs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003183 save01->fs.selector, save01->fs.attrib,
3184 save01->fs.limit, save01->fs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003185 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3186 "gs:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003187 save01->gs.selector, save01->gs.attrib,
3188 save01->gs.limit, save01->gs.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003189 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3190 "gdtr:",
3191 save->gdtr.selector, save->gdtr.attrib,
3192 save->gdtr.limit, save->gdtr.base);
3193 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3194 "ldtr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003195 save01->ldtr.selector, save01->ldtr.attrib,
3196 save01->ldtr.limit, save01->ldtr.base);
Joe Perchesae8cc052011-04-24 22:00:50 -07003197 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3198 "idtr:",
3199 save->idtr.selector, save->idtr.attrib,
3200 save->idtr.limit, save->idtr.base);
3201 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3202 "tr:",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003203 save01->tr.selector, save01->tr.attrib,
3204 save01->tr.limit, save01->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003205 pr_err("cpl: %d efer: %016llx\n",
3206 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003207 pr_err("%-15s %016llx %-13s %016llx\n",
3208 "cr0:", save->cr0, "cr2:", save->cr2);
3209 pr_err("%-15s %016llx %-13s %016llx\n",
3210 "cr3:", save->cr3, "cr4:", save->cr4);
3211 pr_err("%-15s %016llx %-13s %016llx\n",
3212 "dr6:", save->dr6, "dr7:", save->dr7);
3213 pr_err("%-15s %016llx %-13s %016llx\n",
3214 "rip:", save->rip, "rflags:", save->rflags);
3215 pr_err("%-15s %016llx %-13s %016llx\n",
3216 "rsp:", save->rsp, "rax:", save->rax);
3217 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003218 "star:", save01->star, "lstar:", save01->lstar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003219 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003220 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
Joe Perchesae8cc052011-04-24 22:00:50 -07003221 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003222 "kernel_gs_base:", save01->kernel_gs_base,
3223 "sysenter_cs:", save01->sysenter_cs);
Joe Perchesae8cc052011-04-24 22:00:50 -07003224 pr_err("%-15s %016llx %-13s %016llx\n",
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003225 "sysenter_esp:", save01->sysenter_esp,
3226 "sysenter_eip:", save01->sysenter_eip);
Joe Perchesae8cc052011-04-24 22:00:50 -07003227 pr_err("%-15s %016llx %-13s %016llx\n",
3228 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3229 pr_err("%-15s %016llx %-13s %016llx\n",
3230 "br_from:", save->br_from, "br_to:", save->br_to);
3231 pr_err("%-15s %016llx %-13s %016llx\n",
3232 "excp_from:", save->last_excp_from,
3233 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003234}
3235
Tom Lendackye9093fd42020-12-10 11:09:46 -06003236static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3237{
3238 if (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3239 svm_exit_handlers[exit_code])
3240 return 0;
3241
3242 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3243 dump_vmcb(vcpu);
3244 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3245 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3246 vcpu->run->internal.ndata = 2;
3247 vcpu->run->internal.data[0] = exit_code;
3248 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3249
3250 return -EINVAL;
3251}
3252
Paolo Bonzini63129752021-03-02 14:40:39 -05003253int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
Tom Lendackye9093fd42020-12-10 11:09:46 -06003254{
Paolo Bonzini63129752021-03-02 14:40:39 -05003255 if (svm_handle_invalid_exit(vcpu, exit_code))
Tom Lendackye9093fd42020-12-10 11:09:46 -06003256 return 0;
3257
3258#ifdef CONFIG_RETPOLINE
3259 if (exit_code == SVM_EXIT_MSR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003260 return msr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003261 else if (exit_code == SVM_EXIT_VINTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003262 return interrupt_window_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003263 else if (exit_code == SVM_EXIT_INTR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003264 return intr_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003265 else if (exit_code == SVM_EXIT_HLT)
Sean Christopherson5ff3a352021-02-04 16:57:47 -08003266 return kvm_emulate_halt(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003267 else if (exit_code == SVM_EXIT_NPF)
Paolo Bonzini63129752021-03-02 14:40:39 -05003268 return npf_interception(vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003269#endif
Paolo Bonzini63129752021-03-02 14:40:39 -05003270 return svm_exit_handlers[exit_code](vcpu);
Tom Lendackye9093fd42020-12-10 11:09:46 -06003271}
3272
Sean Christopherson235ba742020-09-23 13:13:46 -07003273static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3274 u32 *intr_info, u32 *error_code)
Avi Kivity586f9602010-11-18 13:09:54 +02003275{
3276 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3277
3278 *info1 = control->exit_info_1;
3279 *info2 = control->exit_info_2;
Sean Christopherson235ba742020-09-23 13:13:46 -07003280 *intr_info = control->exit_int_info;
3281 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3282 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3283 *error_code = control->exit_int_info_err;
3284 else
3285 *error_code = 0;
Avi Kivity586f9602010-11-18 13:09:54 +02003286}
3287
Wanpeng Li404d5d72020-04-28 14:23:25 +08003288static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003289{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003290 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003291 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003292 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003293
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003294 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3295
Tom Lendackyf1c63662020-12-14 10:29:50 -05003296 /* SEV-ES guests must use the CR write traps to track CR registers. */
3297 if (!sev_es_guest(vcpu->kvm)) {
3298 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3299 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3300 if (npt_enabled)
3301 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3302 }
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003303
Joerg Roedel20307532010-11-29 17:51:48 +01003304 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003305 int vmexit;
3306
Sean Christophersoncc167bd2020-09-23 13:13:48 -07003307 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003308
Joerg Roedel410e4d52009-08-07 11:49:44 +02003309 vmexit = nested_svm_exit_special(svm);
3310
3311 if (vmexit == NESTED_EXIT_CONTINUE)
3312 vmexit = nested_svm_exit_handled(svm);
3313
3314 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003315 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003316 }
3317
Avi Kivity04d2cc72007-09-10 18:10:54 +03003318 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3319 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3320 kvm_run->fail_entry.hardware_entry_failure_reason
3321 = svm->vmcb->control.exit_code;
Jim Mattson8a14fe42020-06-03 16:56:22 -07003322 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003323 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003324 return 0;
3325 }
3326
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003327 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003328 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003329 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3330 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003331 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003332 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003333 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334 exit_code);
3335
Wanpeng Li404d5d72020-04-28 14:23:25 +08003336 if (exit_fastpath != EXIT_FASTPATH_NONE)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003337 return 1;
Wanpeng Li404d5d72020-04-28 14:23:25 +08003338
Paolo Bonzini63129752021-03-02 14:40:39 -05003339 return svm_invoke_exit_handler(vcpu, exit_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340}
3341
3342static void reload_tss(struct kvm_vcpu *vcpu)
3343{
Jim Mattson73cd6e52020-06-03 16:56:18 -07003344 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345
Tejun Heo0fe1e002009-10-29 22:34:14 +09003346 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003347 load_TR_desc();
3348}
3349
Paolo Bonzini63129752021-03-02 14:40:39 -05003350static void pre_svm_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003351{
Paolo Bonzini63129752021-03-02 14:40:39 -05003352 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3353 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003354
Cathy Averyaf18fa72021-01-12 11:43:12 -05003355 /*
Sean Christopherson44f1b552021-04-06 10:18:11 -07003356 * If the previous vmrun of the vmcb occurred on a different physical
3357 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3358 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3359 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003360 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
Cathy Avery193015a2021-01-12 11:43:13 -05003361 svm->current_vmcb->asid_generation = 0;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003362 vmcb_mark_all_dirty(svm->vmcb);
Paolo Bonzini63129752021-03-02 14:40:39 -05003363 svm->current_vmcb->cpu = vcpu->cpu;
Cathy Averyaf18fa72021-01-12 11:43:12 -05003364 }
3365
Paolo Bonzini63129752021-03-02 14:40:39 -05003366 if (sev_guest(vcpu->kvm))
3367 return pre_sev_run(svm, vcpu->cpu);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003368
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003369 /* FIXME: handle wraparound of asid_generation */
Cathy Avery193015a2021-01-12 11:43:13 -05003370 if (svm->current_vmcb->asid_generation != sd->asid_generation)
Tejun Heo0fe1e002009-10-29 22:34:14 +09003371 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003372}
3373
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003374static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3375{
3376 struct vcpu_svm *svm = to_svm(vcpu);
3377
3378 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3379 vcpu->arch.hflags |= HF_NMI_MASK;
Paolo Bonzini63129752021-03-02 14:40:39 -05003380 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003381 svm_set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003382 ++vcpu->stat.nmi_injections;
3383}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003385static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003386{
3387 struct vcpu_svm *svm = to_svm(vcpu);
3388
Joerg Roedel2af91942009-08-07 11:49:28 +02003389 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003390
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003391 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3392 ++vcpu->stat.irq_injections;
3393
Alexander Graf219b65d2009-06-15 15:21:25 +02003394 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3395 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003396}
3397
Jason Baronb6a7cc32021-01-14 22:27:54 -05003398static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003399{
3400 struct vcpu_svm *svm = to_svm(vcpu);
3401
Tom Lendackyf1c63662020-12-14 10:29:50 -05003402 /*
3403 * SEV-ES guests must always keep the CR intercepts cleared. CR
3404 * tracking is done using the CR write traps.
3405 */
3406 if (sev_es_guest(vcpu->kvm))
3407 return;
3408
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003409 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003410 return;
3411
Babu Moger830bd712020-09-11 14:28:50 -05003412 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
Radim Krčmář596f3142014-03-11 19:11:18 +01003413
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003414 if (irr == -1)
3415 return;
3416
3417 if (tpr >= irr)
Babu Moger830bd712020-09-11 14:28:50 -05003418 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003419}
3420
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003421bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003422{
3423 struct vcpu_svm *svm = to_svm(vcpu);
3424 struct vmcb *vmcb = svm->vmcb;
Sean Christopherson88c604b2020-04-22 19:25:41 -07003425 bool ret;
Cathy Avery9c3d3702020-04-14 16:11:06 -04003426
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003427 if (!gif_set(svm))
Paolo Bonzinibbdad0b2020-04-23 08:06:43 -04003428 return true;
3429
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003430 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3431 return false;
3432
3433 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
Paolo Bonzini63129752021-03-02 14:40:39 -05003434 (vcpu->arch.hflags & HF_NMI_MASK);
Joerg Roedel924584c2010-04-22 12:33:07 +02003435
3436 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003437}
3438
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003439static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003440{
3441 struct vcpu_svm *svm = to_svm(vcpu);
3442 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003443 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003444
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003445 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3446 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003447 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003448
3449 return !svm_nmi_blocked(vcpu);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003450}
3451
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003452static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3453{
Paolo Bonzini63129752021-03-02 14:40:39 -05003454 return !!(vcpu->arch.hflags & HF_NMI_MASK);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003455}
3456
3457static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3458{
3459 struct vcpu_svm *svm = to_svm(vcpu);
3460
3461 if (masked) {
Paolo Bonzini63129752021-03-02 14:40:39 -05003462 vcpu->arch.hflags |= HF_NMI_MASK;
3463 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003464 svm_set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003465 } else {
Paolo Bonzini63129752021-03-02 14:40:39 -05003466 vcpu->arch.hflags &= ~HF_NMI_MASK;
3467 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky4444dfe2020-12-14 11:16:03 -05003468 svm_clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003469 }
3470}
3471
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003472bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
Gleb Natapov78646122009-03-23 12:12:11 +02003473{
3474 struct vcpu_svm *svm = to_svm(vcpu);
3475 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003476
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003477 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003478 return true;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003479
Paolo Bonzini63129752021-03-02 14:40:39 -05003480 if (sev_es_guest(vcpu->kvm)) {
Tom Lendackyf1c63662020-12-14 10:29:50 -05003481 /*
3482 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3483 * bit to determine the state of the IF flag.
3484 */
3485 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3486 return true;
3487 } else if (is_guest_mode(vcpu)) {
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003488 /* As long as interrupts are being delivered... */
Paolo Bonzinie9fd7612020-05-13 13:28:23 -04003489 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
Cathy Avery4995a362021-01-13 07:07:52 -05003490 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
Paolo Bonzinifc6f7c02020-04-23 18:02:45 -04003491 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3492 return true;
3493
3494 /* ... vmexits aren't blocked by the interrupt shadow */
3495 if (nested_exit_on_intr(svm))
3496 return false;
3497 } else {
3498 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3499 return true;
3500 }
3501
3502 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003503}
3504
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003505static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003506{
3507 struct vcpu_svm *svm = to_svm(vcpu);
3508 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003509 return -EBUSY;
Paolo Bonzinicae96af2020-04-23 14:19:26 -04003510
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003511 /*
3512 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3513 * e.g. if the IRQ arrived asynchronously after checking nested events.
3514 */
3515 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04003516 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04003517
3518 return !svm_interrupt_blocked(vcpu);
Gleb Natapov78646122009-03-23 12:12:11 +02003519}
3520
Jason Baronb6a7cc32021-01-14 22:27:54 -05003521static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003522{
Alexander Graf219b65d2009-06-15 15:21:25 +02003523 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003524
Joerg Roedele0231712010-02-24 18:59:10 +01003525 /*
3526 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3527 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3528 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003529 * we'll get the vintr intercept. However, if the vGIF feature is
3530 * enabled, the STGI interception will not occur. Enable the irq
3531 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003532 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003533 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003534 /*
3535 * IRQ window is not needed when AVIC is enabled,
3536 * unless we have pending ExtINT since it cannot be injected
3537 * via AVIC. In such case, we need to temporarily disable AVIC,
3538 * and fallback to injecting IRQ via V_IRQ.
3539 */
3540 svm_toggle_avic_for_irq_window(vcpu, false);
Alexander Graf219b65d2009-06-15 15:21:25 +02003541 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003542 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003543}
3544
Jason Baronb6a7cc32021-01-14 22:27:54 -05003545static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003546{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003547 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003548
Paolo Bonzini63129752021-03-02 14:40:39 -05003549 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003550 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003551
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003552 if (!gif_set(svm)) {
3553 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02003554 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003555 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003556 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003557
Joerg Roedele0231712010-02-24 18:59:10 +01003558 /*
3559 * Something prevents NMI from been injected. Single step over possible
3560 * problem (IRET or exception injection or interrupt shadow)
3561 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003562 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003563 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003564 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003565}
3566
Izik Eiduscbc94022007-10-25 00:29:55 +02003567static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3568{
3569 return 0;
3570}
3571
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003572static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3573{
3574 return 0;
3575}
3576
Sean Christophersonf55ac302020-03-20 14:28:12 -07003577void svm_flush_tlb(struct kvm_vcpu *vcpu)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003578{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003579 struct vcpu_svm *svm = to_svm(vcpu);
3580
Sean Christopherson4a41e432020-03-20 14:28:17 -07003581 /*
3582 * Flush only the current ASID even if the TLB flush was invoked via
3583 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3584 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3585 * unconditionally does a TLB flush on both nested VM-Enter and nested
3586 * VM-Exit (via kvm_mmu_reset_context()).
3587 */
Joerg Roedel38e5e922010-12-03 15:25:16 +01003588 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3589 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3590 else
Cathy Avery193015a2021-01-12 11:43:13 -05003591 svm->current_vmcb->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003592}
3593
Junaid Shahidfaff8752018-06-29 13:10:05 -07003594static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3595{
3596 struct vcpu_svm *svm = to_svm(vcpu);
3597
3598 invlpga(gva, svm->vmcb->control.asid);
3599}
3600
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003601static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3602{
3603 struct vcpu_svm *svm = to_svm(vcpu);
3604
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003605 if (nested_svm_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003606 return;
3607
Babu Moger830bd712020-09-11 14:28:50 -05003608 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003609 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003610 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003611 }
3612}
3613
Joerg Roedel649d6862008-04-16 16:51:15 +02003614static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3615{
3616 struct vcpu_svm *svm = to_svm(vcpu);
3617 u64 cr8;
3618
Joerg Roedel01c3b2b2020-06-25 10:03:25 +02003619 if (nested_svm_virtualize_tpr(vcpu) ||
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003620 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003621 return;
3622
Joerg Roedel649d6862008-04-16 16:51:15 +02003623 cr8 = kvm_get_cr8(vcpu);
3624 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3625 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3626}
3627
Paolo Bonzini63129752021-03-02 14:40:39 -05003628static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003629{
Paolo Bonzini63129752021-03-02 14:40:39 -05003630 struct vcpu_svm *svm = to_svm(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003631 u8 vector;
3632 int type;
3633 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003634 unsigned int3_injected = svm->int3_injected;
3635
3636 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003637
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003638 /*
3639 * If we've made progress since setting HF_IRET_MASK, we've
3640 * executed an IRET and can allow NMI injection.
3641 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003642 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3643 (sev_es_guest(vcpu->kvm) ||
3644 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3645 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3646 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003647 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003648
Paolo Bonzini63129752021-03-02 14:40:39 -05003649 vcpu->arch.nmi_injected = false;
3650 kvm_clear_exception_queue(vcpu);
3651 kvm_clear_interrupt_queue(vcpu);
Gleb Natapov9222be12009-04-23 17:14:37 +03003652
3653 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3654 return;
3655
Paolo Bonzini63129752021-03-02 14:40:39 -05003656 kvm_make_request(KVM_REQ_EVENT, vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03003657
Gleb Natapov9222be12009-04-23 17:14:37 +03003658 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3659 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3660
3661 switch (type) {
3662 case SVM_EXITINTINFO_TYPE_NMI:
Paolo Bonzini63129752021-03-02 14:40:39 -05003663 vcpu->arch.nmi_injected = true;
Gleb Natapov9222be12009-04-23 17:14:37 +03003664 break;
3665 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003666 /*
Tom Lendackyf1c63662020-12-14 10:29:50 -05003667 * Never re-inject a #VC exception.
3668 */
3669 if (vector == X86_TRAP_VC)
3670 break;
3671
3672 /*
Jan Kiszka66b71382010-02-23 17:47:56 +01003673 * In case of software exceptions, do not reinject the vector,
3674 * but re-execute the instruction instead. Rewind RIP first
3675 * if we emulated INT3 before.
3676 */
3677 if (kvm_exception_is_soft(vector)) {
3678 if (vector == BP_VECTOR && int3_injected &&
Paolo Bonzini63129752021-03-02 14:40:39 -05003679 kvm_is_linear_rip(vcpu, svm->int3_rip))
3680 kvm_rip_write(vcpu,
3681 kvm_rip_read(vcpu) - int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003682 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003683 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003684 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3685 u32 err = svm->vmcb->control.exit_int_info_err;
Paolo Bonzini63129752021-03-02 14:40:39 -05003686 kvm_requeue_exception_e(vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003687
3688 } else
Paolo Bonzini63129752021-03-02 14:40:39 -05003689 kvm_requeue_exception(vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003690 break;
3691 case SVM_EXITINTINFO_TYPE_INTR:
Paolo Bonzini63129752021-03-02 14:40:39 -05003692 kvm_queue_interrupt(vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003693 break;
3694 default:
3695 break;
3696 }
3697}
3698
Avi Kivityb463a6f2010-07-20 15:06:17 +03003699static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3700{
3701 struct vcpu_svm *svm = to_svm(vcpu);
3702 struct vmcb_control_area *control = &svm->vmcb->control;
3703
3704 control->exit_int_info = control->event_inj;
3705 control->exit_int_info_err = control->event_inj_err;
3706 control->event_inj = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003707 svm_complete_interrupts(vcpu);
Avi Kivityb463a6f2010-07-20 15:06:17 +03003708}
3709
Wanpeng Li404d5d72020-04-28 14:23:25 +08003710static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003711{
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003712 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07003713 to_svm(vcpu)->vmcb->control.exit_info_1)
3714 return handle_fastpath_set_msr_irqoff(vcpu);
3715
3716 return EXIT_FASTPATH_NONE;
3717}
3718
Paolo Bonzini63129752021-03-02 14:40:39 -05003719static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
Thomas Gleixner135961e2020-07-08 21:51:58 +02003720{
Paolo Bonzini63129752021-03-02 14:40:39 -05003721 struct vcpu_svm *svm = to_svm(vcpu);
Sean Christophersond1788192021-04-06 10:18:09 -07003722 unsigned long vmcb_pa = svm->current_vmcb->pa;
Paolo Bonzini63129752021-03-02 14:40:39 -05003723
Thomas Gleixner135961e2020-07-08 21:51:58 +02003724 /*
3725 * VMENTER enables interrupts (host state), but the kernel state is
3726 * interrupts disabled when this is invoked. Also tell RCU about
3727 * it. This is the same logic as for exit_to_user_mode().
3728 *
3729 * This ensures that e.g. latency analysis on the host observes
3730 * guest mode as interrupt enabled.
3731 *
3732 * guest_enter_irqoff() informs context tracking about the
3733 * transition to guest mode and if enabled adjusts RCU state
3734 * accordingly.
3735 */
3736 instrumentation_begin();
3737 trace_hardirqs_on_prepare();
3738 lockdep_hardirqs_on_prepare(CALLER_ADDR0);
3739 instrumentation_end();
3740
3741 guest_enter_irqoff();
3742 lockdep_hardirqs_on(CALLER_ADDR0);
3743
Paolo Bonzini63129752021-03-02 14:40:39 -05003744 if (sev_es_guest(vcpu->kvm)) {
Sean Christophersond1788192021-04-06 10:18:09 -07003745 __svm_sev_es_vcpu_run(vmcb_pa);
Tom Lendacky16809ec2020-12-10 11:10:08 -06003746 } else {
Michael Rothe79b91b2021-02-02 13:01:24 -06003747 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3748
Sean Christophersond1788192021-04-06 10:18:09 -07003749 /*
3750 * Use a single vmcb (vmcb01 because it's always valid) for
3751 * context switching guest state via VMLOAD/VMSAVE, that way
3752 * the state doesn't need to be copied between vmcb01 and
3753 * vmcb02 when switching vmcbs for nested virtualization.
3754 */
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003755 vmload(svm->vmcb01.pa);
Sean Christophersond1788192021-04-06 10:18:09 -07003756 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
Maxim Levitskycc3ed802021-02-10 18:54:36 +02003757 vmsave(svm->vmcb01.pa);
Thomas Gleixner135961e2020-07-08 21:51:58 +02003758
Michael Rothe79b91b2021-02-02 13:01:24 -06003759 vmload(__sme_page_pa(sd->save_area));
Tom Lendacky16809ec2020-12-10 11:10:08 -06003760 }
Thomas Gleixner135961e2020-07-08 21:51:58 +02003761
3762 /*
3763 * VMEXIT disables interrupts (host state), but tracing and lockdep
3764 * have them in state 'on' as recorded before entering guest mode.
3765 * Same as enter_from_user_mode().
3766 *
3767 * guest_exit_irqoff() restores host context and reinstates RCU if
3768 * enabled and required.
3769 *
3770 * This needs to be done before the below as native_read_msr()
3771 * contains a tracepoint and x86_spec_ctrl_restore_host() calls
3772 * into world and some more.
3773 */
3774 lockdep_hardirqs_off(CALLER_ADDR0);
3775 guest_exit_irqoff();
3776
3777 instrumentation_begin();
3778 trace_hardirqs_off_finish();
3779 instrumentation_end();
3780}
3781
Qian Caib95273f2020-04-15 11:37:09 -04003782static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003783{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003784 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003785
Lorenzo Bresciad95df952020-12-23 14:45:07 +00003786 trace_kvm_entry(vcpu);
3787
Joerg Roedel2041a062010-04-22 12:33:08 +02003788 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3789 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3790 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3791
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003792 /*
Ladi Proseka12713c2017-06-21 09:07:00 +02003793 * Disable singlestep if we're injecting an interrupt/exception.
3794 * We don't want our modified rflags to be pushed on the stack where
3795 * we might not be able to easily reset them if we disabled NMI
3796 * singlestep later.
3797 */
3798 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3799 /*
3800 * Event injection happens before external interrupts cause a
3801 * vmexit and interrupts are disabled here, so smp_send_reschedule
3802 * is enough to force an immediate vmexit.
3803 */
3804 disable_nmi_singlestep(svm);
3805 smp_send_reschedule(vcpu->cpu);
3806 }
3807
Paolo Bonzini63129752021-03-02 14:40:39 -05003808 pre_svm_run(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003809
Joerg Roedel649d6862008-04-16 16:51:15 +02003810 sync_lapic_to_cr8(vcpu);
3811
Cathy Avery7e8e6ee2020-10-11 14:48:17 -04003812 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3813 svm->vmcb->control.asid = svm->asid;
3814 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3815 }
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003816 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003817
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003818 /*
3819 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3820 * of a #DB.
3821 */
Paolo Bonzini63129752021-03-02 14:40:39 -05003822 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003823 svm_set_dr6(svm, vcpu->arch.dr6);
3824 else
Chenyi Qiang9a3ecd52021-02-02 17:04:31 +08003825 svm_set_dr6(svm, DR6_ACTIVE_LOW);
Paolo Bonzinid67668e2020-05-06 06:40:04 -04003826
Avi Kivity04d2cc72007-09-10 18:10:54 +03003827 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003828 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003829
Wanpeng Li010fd372020-09-10 17:50:41 +08003830 kvm_wait_lapic_expire(vcpu);
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003831
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003832 /*
3833 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3834 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3835 * is no need to worry about the conditional branch over the wrmsr
3836 * being speculatively taken.
3837 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003838 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3839 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003840
Paolo Bonzini63129752021-03-02 14:40:39 -05003841 svm_vcpu_enter_exit(vcpu);
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003842
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003843 /*
3844 * We do not use IBRS in the kernel. If this vCPU has used the
3845 * SPEC_CTRL MSR it may have left it on; save the value and
3846 * turn it off. This is much more efficient than blindly adding
3847 * it to the atomic save/restore list. Especially as the former
3848 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3849 *
3850 * For non-nested case:
3851 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3852 * save it.
3853 *
3854 * For nested case:
3855 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3856 * save it.
3857 */
Babu Mogerd00b99c2021-02-17 10:56:04 -05003858 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3859 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003860 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003861
Paolo Bonzini63129752021-03-02 14:40:39 -05003862 if (!sev_es_guest(vcpu->kvm))
Tom Lendacky16809ec2020-12-10 11:10:08 -06003863 reload_tss(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003864
Babu Mogerd00b99c2021-02-17 10:56:04 -05003865 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3866 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003867
Paolo Bonzini63129752021-03-02 14:40:39 -05003868 if (!sev_es_guest(vcpu->kvm)) {
Tom Lendacky16809ec2020-12-10 11:10:08 -06003869 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3870 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3871 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3872 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3873 }
Avi Kivity13c34e02010-10-21 12:20:31 +02003874
Joerg Roedel3781c012011-01-14 16:45:02 +01003875 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003876 kvm_before_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003877
Aaron Lewis139a12c2019-10-21 16:30:25 -07003878 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003879 stgi();
3880
3881 /* Any pending NMI will happen here */
3882
3883 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Paolo Bonzini63129752021-03-02 14:40:39 -05003884 kvm_after_interrupt(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003885
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003886 sync_cr8_to_lapic(vcpu);
3887
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003888 svm->next_rip = 0;
Paolo Bonzini63129752021-03-02 14:40:39 -05003889 if (is_guest_mode(vcpu)) {
Paolo Bonzini9e8f0fb2020-11-17 05:15:41 -05003890 nested_sync_control_from_vmcb02(svm);
Paolo Bonzini2d8a42b2020-05-22 03:50:14 -04003891 svm->nested.nested_run_pending = 0;
3892 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003893
Joerg Roedel38e5e922010-12-03 15:25:16 +01003894 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Wanpeng Lie42c6822020-09-12 02:16:39 -04003895 vmcb_mark_all_clean(svm->vmcb);
Joerg Roedel38e5e922010-12-03 15:25:16 +01003896
Gleb Natapov631bc482010-10-14 11:22:52 +02003897 /* if exit due to PF check for async PF */
3898 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Paolo Bonzini63129752021-03-02 14:40:39 -05003899 vcpu->arch.apf.host_apf_flags =
Vitaly Kuznetsov68fd66f2020-05-25 16:41:17 +02003900 kvm_read_and_reset_apf_flags();
Gleb Natapov631bc482010-10-14 11:22:52 +02003901
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003902 if (npt_enabled) {
3903 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3904 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3905 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003906
3907 /*
3908 * We need to handle MC intercepts here before the vcpu has a chance to
3909 * change the physical cpu
3910 */
3911 if (unlikely(svm->vmcb->control.exit_code ==
3912 SVM_EXIT_EXCP_BASE + MC_VECTOR))
Paolo Bonzini63129752021-03-02 14:40:39 -05003913 svm_handle_mce(vcpu);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003914
Paolo Bonzini63129752021-03-02 14:40:39 -05003915 svm_complete_interrupts(vcpu);
Wanpeng Li4e810ad2020-09-14 14:55:48 +08003916
3917 if (is_guest_mode(vcpu))
3918 return EXIT_FASTPATH_NONE;
3919
3920 return svm_exit_handlers_fastpath(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003921}
3922
Sean Christophersone83bc092021-03-05 10:31:13 -08003923static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
Sean Christopherson2a40b902020-07-15 20:41:18 -07003924 int root_level)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003925{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003926 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003927 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003928
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003929 if (npt_enabled) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003930 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
Joerg Roedel06e78522020-06-25 10:03:23 +02003931 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003932
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003933 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
Paolo Bonzini978ce582020-05-20 08:37:37 -04003934 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3935 return;
3936 cr3 = vcpu->arch.cr3;
Sean Christophersone83bc092021-03-05 10:31:13 -08003937 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
Sean Christopherson4a986232021-03-09 14:42:07 -08003938 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
Sean Christophersone83bc092021-03-05 10:31:13 -08003939 } else {
3940 /* PCID in the guest should be impossible with a 32-bit MMU. */
3941 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3942 cr3 = root_hpa;
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003943 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003944
Paolo Bonzini978ce582020-05-20 08:37:37 -04003945 svm->vmcb->save.cr3 = cr3;
Joerg Roedel06e78522020-06-25 10:03:23 +02003946 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003947}
3948
Avi Kivity6aa8b732006-12-10 02:21:36 -08003949static int is_disabled(void)
3950{
Joerg Roedel6031a612007-06-22 12:29:50 +03003951 u64 vm_cr;
3952
3953 rdmsrl(MSR_VM_CR, vm_cr);
3954 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3955 return 1;
3956
Avi Kivity6aa8b732006-12-10 02:21:36 -08003957 return 0;
3958}
3959
Ingo Molnar102d8322007-02-19 14:37:47 +02003960static void
3961svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3962{
3963 /*
3964 * Patch in the VMMCALL instruction:
3965 */
3966 hypercall[0] = 0x0f;
3967 hypercall[1] = 0x01;
3968 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003969}
3970
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003971static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003972{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003973 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003974}
3975
Avi Kivity774ead32007-12-26 13:57:04 +02003976static bool svm_cpu_has_accelerated_tpr(void)
3977{
3978 return false;
3979}
3980
Tom Lendacky57194552020-12-10 11:10:00 -06003981/*
3982 * The kvm parameter can be NULL (module initialization, or invocation before
3983 * VM creation). Be sure to check the kvm parameter before using it.
3984 */
3985static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003986{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003987 switch (index) {
3988 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02003989 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003990 return false;
Tom Lendacky57194552020-12-10 11:10:00 -06003991 case MSR_IA32_SMBASE:
3992 /* SEV-ES guests do not support SMM, so report false */
3993 if (kvm && sev_es_guest(kvm))
3994 return false;
3995 break;
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003996 default:
3997 break;
3998 }
3999
Paolo Bonzini6d396b52015-04-01 14:25:33 +02004000 return true;
4001}
4002
Paolo Bonzinifc07e762015-10-01 13:20:22 +02004003static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
4004{
4005 return 0;
4006}
4007
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004008static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
Sheng Yang0e851882009-12-18 16:48:46 +08004009{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004010 struct vcpu_svm *svm = to_svm(vcpu);
Babu Moger96308b02020-11-12 16:18:03 -06004011 struct kvm_cpuid_entry2 *best;
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004012
Aaron Lewis72041602019-10-21 16:30:20 -07004013 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08004014 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07004015 boot_cpu_has(X86_FEATURE_XSAVES);
4016
Joerg Roedel6092d3d2015-10-14 15:10:54 +02004017 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08004018 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
Paolo Bonzini63129752021-03-02 14:40:39 -05004019 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004020
Sean Christopherson3b195ac2021-05-04 10:17:22 -07004021 svm_recalc_instruction_intercepts(vcpu, svm);
Babu Moger4407a792020-09-11 14:29:19 -05004022
Babu Moger96308b02020-11-12 16:18:03 -06004023 /* For sev guests, the memory encryption bit is not reserved in CR3. */
4024 if (sev_guest(vcpu->kvm)) {
4025 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
4026 if (best)
Sean Christophersonca29e142021-02-03 16:01:12 -08004027 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
Babu Moger96308b02020-11-12 16:18:03 -06004028 }
4029
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004030 if (kvm_vcpu_apicv_active(vcpu)) {
4031 /*
4032 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
4033 * is exposed to the guest, disable AVIC.
4034 */
4035 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
4036 kvm_request_apicv_update(vcpu->kvm, false,
4037 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05004038
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004039 /*
4040 * Currently, AVIC does not work with nested virtualization.
4041 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
4042 */
4043 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4044 kvm_request_apicv_update(vcpu->kvm, false,
4045 APICV_INHIBIT_REASON_NESTED);
4046 }
Suravee Suthikulpanit9a0bf052019-11-14 14:15:14 -06004047
Maxim Levitskyadc2a232021-04-01 14:19:28 +03004048 if (guest_cpuid_is_intel(vcpu)) {
4049 /*
4050 * We must intercept SYSENTER_EIP and SYSENTER_ESP
4051 * accesses because the processor only stores 32 bits.
4052 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
4053 */
4054 svm_set_intercept(svm, INTERCEPT_VMLOAD);
4055 svm_set_intercept(svm, INTERCEPT_VMSAVE);
4056 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4057
4058 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
4059 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
4060 } else {
4061 /*
4062 * If hardware supports Virtual VMLOAD VMSAVE then enable it
4063 * in VMCB and clear intercepts to avoid #VMEXIT.
4064 */
4065 if (vls) {
4066 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
4067 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
4068 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4069 }
4070 /* No need to intercept these MSRs */
4071 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
4072 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
4073 }
Sheng Yang0e851882009-12-18 16:48:46 +08004074}
4075
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004076static bool svm_has_wbinvd_exit(void)
4077{
4078 return true;
4079}
4080
Joerg Roedel80612522011-04-04 12:39:33 +02004081#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004082 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004083#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004084 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004085#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03004086 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004087
Mathias Krause09941fb2012-08-30 01:30:20 +02004088static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004089 u32 exit_code;
4090 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004091} x86_intercept_map[] = {
4092 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4093 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4094 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4095 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4096 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02004097 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4098 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02004099 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4100 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4101 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4102 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4103 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4104 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4105 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4106 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02004107 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4108 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4109 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4110 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4111 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4112 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4113 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4114 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004115 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4116 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4117 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02004118 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4119 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4120 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4121 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4122 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4123 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4124 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4125 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4126 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02004127 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4128 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4129 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4130 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4131 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4132 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4133 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02004134 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4135 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4136 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4137 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02004138 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004139};
4140
Joerg Roedel80612522011-04-04 12:39:33 +02004141#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004142#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004143#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004144
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004145static int svm_check_intercept(struct kvm_vcpu *vcpu,
4146 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08004147 enum x86_intercept_stage stage,
4148 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004149{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004150 struct vcpu_svm *svm = to_svm(vcpu);
4151 int vmexit, ret = X86EMUL_CONTINUE;
4152 struct __x86_intercept icpt_info;
4153 struct vmcb *vmcb = svm->vmcb;
4154
4155 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4156 goto out;
4157
4158 icpt_info = x86_intercept_map[info->intercept];
4159
Avi Kivity40e19b52011-04-21 12:35:41 +03004160 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004161 goto out;
4162
4163 switch (icpt_info.exit_code) {
4164 case SVM_EXIT_READ_CR0:
4165 if (info->intercept == x86_intercept_cr_read)
4166 icpt_info.exit_code += info->modrm_reg;
4167 break;
4168 case SVM_EXIT_WRITE_CR0: {
4169 unsigned long cr0, val;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004170
4171 if (info->intercept == x86_intercept_cr_write)
4172 icpt_info.exit_code += info->modrm_reg;
4173
Jan Kiszka62baf442014-06-29 21:55:53 +02004174 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4175 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004176 break;
4177
Babu Mogerc62e2e92020-09-11 14:28:28 -05004178 if (!(vmcb_is_intercept(&svm->nested.ctl,
4179 INTERCEPT_SELECTIVE_CR0)))
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004180 break;
4181
4182 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4183 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4184
4185 if (info->intercept == x86_intercept_lmsw) {
4186 cr0 &= 0xfUL;
4187 val &= 0xfUL;
4188 /* lmsw can't clear PE - catch this here */
4189 if (cr0 & X86_CR0_PE)
4190 val |= X86_CR0_PE;
4191 }
4192
4193 if (cr0 ^ val)
4194 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4195
4196 break;
4197 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004198 case SVM_EXIT_READ_DR0:
4199 case SVM_EXIT_WRITE_DR0:
4200 icpt_info.exit_code += info->modrm_reg;
4201 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004202 case SVM_EXIT_MSR:
4203 if (info->intercept == x86_intercept_wrmsr)
4204 vmcb->control.exit_info_1 = 1;
4205 else
4206 vmcb->control.exit_info_1 = 0;
4207 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004208 case SVM_EXIT_PAUSE:
4209 /*
4210 * We get this for NOP only, but pause
4211 * is rep not, check this here
4212 */
4213 if (info->rep_prefix != REPE_PREFIX)
4214 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004215 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004216 case SVM_EXIT_IOIO: {
4217 u64 exit_info;
4218 u32 bytes;
4219
Joerg Roedelf6511932011-04-04 12:39:35 +02004220 if (info->intercept == x86_intercept_in ||
4221 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004222 exit_info = ((info->src_val & 0xffff) << 16) |
4223 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004224 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004225 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004226 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004227 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004228 }
4229
4230 if (info->intercept == x86_intercept_outs ||
4231 info->intercept == x86_intercept_ins)
4232 exit_info |= SVM_IOIO_STR_MASK;
4233
4234 if (info->rep_prefix)
4235 exit_info |= SVM_IOIO_REP_MASK;
4236
4237 bytes = min(bytes, 4u);
4238
4239 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4240
4241 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4242
4243 vmcb->control.exit_info_1 = exit_info;
4244 vmcb->control.exit_info_2 = info->next_rip;
4245
4246 break;
4247 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004248 default:
4249 break;
4250 }
4251
Bandan Dasf1047652015-06-11 02:05:33 -04004252 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4253 if (static_cpu_has(X86_FEATURE_NRIPS))
4254 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004255 vmcb->control.exit_code = icpt_info.exit_code;
4256 vmexit = nested_svm_exit_handled(svm);
4257
4258 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4259 : X86EMUL_CONTINUE;
4260
4261out:
4262 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004263}
4264
Wanpeng Lia9ab13f2020-04-10 10:47:03 -07004265static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004266{
Yang Zhanga547c6d2013-04-11 19:25:10 +08004267}
4268
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004269static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4270{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004271 if (!kvm_pause_in_guest(vcpu->kvm))
Babu Moger8566ac82018-03-16 16:37:26 -04004272 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004273}
4274
Borislav Petkov74f16902017-03-26 23:51:24 +02004275static void svm_setup_mce(struct kvm_vcpu *vcpu)
4276{
4277 /* [63:9] are reserved. */
4278 vcpu->arch.mcg_cap &= 0x1ff;
4279}
4280
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004281bool svm_smi_blocked(struct kvm_vcpu *vcpu)
Ladi Prosek72d7b372017-10-11 16:54:41 +02004282{
Ladi Prosek05cade72017-10-11 16:54:45 +02004283 struct vcpu_svm *svm = to_svm(vcpu);
4284
4285 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4286 if (!gif_set(svm))
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004287 return true;
4288
4289 return is_smm(vcpu);
4290}
4291
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004292static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004293{
4294 struct vcpu_svm *svm = to_svm(vcpu);
4295 if (svm->nested.nested_run_pending)
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004296 return -EBUSY;
Ladi Prosek05cade72017-10-11 16:54:45 +02004297
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004298 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4299 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004300 return -EBUSY;
Paolo Bonzinic300ab92020-04-23 14:08:58 -04004301
Paolo Bonzinicae96af2020-04-23 14:19:26 -04004302 return !svm_smi_blocked(vcpu);
Ladi Prosek72d7b372017-10-11 16:54:41 +02004303}
4304
Ladi Prosek0234bf82017-10-11 16:54:40 +02004305static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4306{
Ladi Prosek05cade72017-10-11 16:54:45 +02004307 struct vcpu_svm *svm = to_svm(vcpu);
4308 int ret;
4309
4310 if (is_guest_mode(vcpu)) {
4311 /* FED8h - SVM Guest */
4312 put_smstate(u64, smstate, 0x7ed8, 1);
4313 /* FEE0h - SVM Guest VMCB Physical Address */
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004314 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
Ladi Prosek05cade72017-10-11 16:54:45 +02004315
4316 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4317 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4318 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4319
4320 ret = nested_svm_vmexit(svm);
4321 if (ret)
4322 return ret;
4323 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02004324 return 0;
4325}
4326
Sean Christophersoned193212019-04-02 08:03:09 -07004327static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004328{
Ladi Prosek05cade72017-10-11 16:54:45 +02004329 struct vcpu_svm *svm = to_svm(vcpu);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01004330 struct kvm_host_map map;
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004331 int ret = 0;
Ladi Prosek05cade72017-10-11 16:54:45 +02004332
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004333 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
4334 u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4335 u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004336 u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
Ladi Prosek05cade72017-10-11 16:54:45 +02004337
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004338 if (guest) {
4339 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4340 return 1;
4341
4342 if (!(saved_efer & EFER_SVME))
4343 return 1;
4344
Paolo Bonzini63129752021-03-02 14:40:39 -05004345 if (kvm_vcpu_map(vcpu,
Maxim Levitsky0dd16b52020-08-27 20:11:39 +03004346 gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004347 return 1;
4348
Maxim Levitsky2fcf4872020-10-01 14:29:54 +03004349 if (svm_allocate_nested(svm))
4350 return 1;
4351
Paolo Bonzini63129752021-03-02 14:40:39 -05004352 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, map.hva);
4353 kvm_vcpu_unmap(vcpu, &map, true);
Maxim Levitsky3ebb5d22020-08-27 19:27:20 +03004354 }
Ladi Prosek05cade72017-10-11 16:54:45 +02004355 }
Vitaly Kuznetsov59cd9bc2020-07-10 16:11:52 +02004356
4357 return ret;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004358}
4359
Jason Baronb6a7cc32021-01-14 22:27:54 -05004360static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004361{
4362 struct vcpu_svm *svm = to_svm(vcpu);
4363
4364 if (!gif_set(svm)) {
4365 if (vgif_enabled(svm))
Joerg Roedela284ba52020-06-25 10:03:24 +02004366 svm_set_intercept(svm, INTERCEPT_STGI);
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004367 /* STGI will cause a vm exit */
Paolo Bonzinic9d40912020-05-22 11:21:49 -04004368 } else {
4369 /* We must be in SMM; RSM will cause a vmexit anyway. */
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004370 }
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004371}
4372
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004373static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004374{
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004375 bool smep, smap, is_user;
4376 unsigned long cr4;
Paolo Bonzinie72436b2020-04-17 12:21:06 -04004377
4378 /*
Tom Lendackybc624d92020-12-10 11:09:44 -06004379 * When the guest is an SEV-ES guest, emulation is not possible.
4380 */
4381 if (sev_es_guest(vcpu->kvm))
4382 return false;
4383
4384 /*
Liran Alon118154b2019-07-17 02:56:58 +03004385 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4386 *
4387 * Errata:
4388 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4389 * possible that CPU microcode implementing DecodeAssist will fail
4390 * to read bytes of instruction which caused #NPF. In this case,
4391 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4392 * return 0 instead of the correct guest instruction bytes.
4393 *
4394 * This happens because CPU microcode reading instruction bytes
4395 * uses a special opcode which attempts to read data using CPL=0
Ingo Molnard9f6e122021-03-18 15:28:01 +01004396 * privileges. The microcode reads CS:RIP and if it hits a SMAP
Liran Alon118154b2019-07-17 02:56:58 +03004397 * fault, it gives up and returns no instruction bytes.
4398 *
4399 * Detection:
4400 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4401 * returned 0 in GuestIntrBytes field of the VMCB.
4402 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4403 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4404 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4405 * a SMEP fault instead of #NPF).
4406 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4407 * As most guests enable SMAP if they have also enabled SMEP, use above
4408 * logic in order to attempt minimize false-positive of detecting errata
4409 * while still preserving all cases semantic correctness.
4410 *
4411 * Workaround:
4412 * To determine what instruction the guest was executing, the hypervisor
4413 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004414 *
4415 * In non SEV guest, hypervisor will be able to read the guest
4416 * memory to decode the instruction pointer when insn_len is zero
4417 * so we return true to indicate that decoding is possible.
4418 *
4419 * But in the SEV guest, the guest memory is encrypted with the
4420 * guest specific key and hypervisor will not be able to decode the
4421 * instruction pointer so we will not able to workaround it. Lets
4422 * print the error and request to kill the guest.
4423 */
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004424 if (likely(!insn || insn_len))
4425 return true;
4426
4427 /*
4428 * If RIP is invalid, go ahead with emulation which will cause an
4429 * internal error exit.
4430 */
4431 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4432 return true;
4433
4434 cr4 = kvm_read_cr4(vcpu);
4435 smep = cr4 & X86_CR4_SMEP;
4436 smap = cr4 & X86_CR4_SMAP;
4437 is_user = svm_get_cpl(vcpu) == 3;
Liran Alon118154b2019-07-17 02:56:58 +03004438 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004439 if (!sev_guest(vcpu->kvm))
4440 return true;
4441
Liran Alon118154b2019-07-17 02:56:58 +03004442 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00004443 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4444 }
4445
4446 return false;
4447}
4448
Liran Alon4b9852f2019-08-26 13:24:49 +03004449static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4450{
4451 struct vcpu_svm *svm = to_svm(vcpu);
4452
4453 /*
4454 * TODO: Last condition latch INIT signals on vCPU when
4455 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
Paolo Bonzini33b22172020-04-17 10:24:18 -04004456 * To properly emulate the INIT intercept,
4457 * svm_check_nested_events() should call nested_svm_vmexit()
4458 * if an INIT signal is pending.
Liran Alon4b9852f2019-08-26 13:24:49 +03004459 */
4460 return !gif_set(svm) ||
Babu Mogerc62e2e92020-09-11 14:28:28 -05004461 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
Liran Alon4b9852f2019-08-26 13:24:49 +03004462}
4463
Tom Lendacky647daca2021-01-04 14:20:01 -06004464static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4465{
4466 if (!sev_es_guest(vcpu->kvm))
4467 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4468
4469 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4470}
4471
Joerg Roedeleaf78262020-03-24 10:41:54 +01004472static void svm_vm_destroy(struct kvm *kvm)
4473{
4474 avic_vm_destroy(kvm);
4475 sev_vm_destroy(kvm);
4476}
4477
4478static int svm_vm_init(struct kvm *kvm)
4479{
Wanpeng Li830f01b2020-07-31 11:12:21 +08004480 if (!pause_filter_count || !pause_filter_thresh)
4481 kvm->arch.pause_in_guest = true;
4482
Joerg Roedeleaf78262020-03-24 10:41:54 +01004483 if (avic) {
4484 int ret = avic_vm_init(kvm);
4485 if (ret)
4486 return ret;
4487 }
4488
4489 kvm_apicv_init(kvm, avic);
4490 return 0;
4491}
4492
Sean Christopherson9c14ee22020-03-21 13:26:03 -07004493static struct kvm_x86_ops svm_x86_ops __initdata = {
Li RongQingdd58f3c2020-02-23 16:13:12 +08004494 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004495 .hardware_enable = svm_hardware_enable,
4496 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02004497 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02004498 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004499
4500 .vcpu_create = svm_create_vcpu,
4501 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004502 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004503
Sean Christopherson562b6b02020-01-26 16:41:13 -08004504 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06004505 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004506 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004507
Avi Kivity04d2cc72007-09-10 18:10:54 +03004508 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004509 .vcpu_load = svm_vcpu_load,
4510 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05004511 .vcpu_blocking = svm_vcpu_blocking,
4512 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004513
Jason Baronb6a7cc32021-01-14 22:27:54 -05004514 .update_exception_bitmap = svm_update_exception_bitmap,
Tom Lendacky801e4592018-02-21 13:39:51 -06004515 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004516 .get_msr = svm_get_msr,
4517 .set_msr = svm_set_msr,
4518 .get_segment_base = svm_get_segment_base,
4519 .get_segment = svm_get_segment,
4520 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02004521 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10004522 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004523 .set_cr0 = svm_set_cr0,
Sean Christophersonc2fe3cd2020-10-06 18:44:15 -07004524 .is_valid_cr4 = svm_is_valid_cr4,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004525 .set_cr4 = svm_set_cr4,
4526 .set_efer = svm_set_efer,
4527 .get_idt = svm_get_idt,
4528 .set_idt = svm_set_idt,
4529 .get_gdt = svm_get_gdt,
4530 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03004531 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01004532 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03004533 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004534 .get_rflags = svm_get_rflags,
4535 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08004536
Sean Christopherson77809382020-03-20 14:28:18 -07004537 .tlb_flush_all = svm_flush_tlb,
Sean Christophersoneeeb4f62020-03-20 14:28:20 -07004538 .tlb_flush_current = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07004539 .tlb_flush_gva = svm_flush_tlb_gva,
Sean Christopherson72b38322020-03-20 14:28:13 -07004540 .tlb_flush_guest = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004541
Avi Kivity6aa8b732006-12-10 02:21:36 -08004542 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03004543 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004544 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08004545 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04004546 .set_interrupt_shadow = svm_set_interrupt_shadow,
4547 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02004548 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03004549 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004550 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02004551 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03004552 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02004553 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03004554 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01004555 .get_nmi_mask = svm_get_nmi_mask,
4556 .set_nmi_mask = svm_set_nmi_mask,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004557 .enable_nmi_window = svm_enable_nmi_window,
4558 .enable_irq_window = svm_enable_irq_window,
4559 .update_cr8_intercept = svm_update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04004560 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03004561 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06004562 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06004563 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08004564 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05004565 .hwapic_irr_update = svm_hwapic_irr_update,
4566 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02004567 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05004568 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02004569
4570 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07004571 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08004572 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03004573
Avi Kivity586f9602010-11-18 13:09:54 +02004574 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02004575
Xiaoyao Li7c1b7612020-07-09 12:34:25 +08004576 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08004577
Sheng Yangf5f48ee2010-06-30 12:25:15 +08004578 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10004579
Leonid Shatz326e7422018-11-06 12:14:25 +02004580 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02004581
Paolo Bonzini727a7e22020-03-05 03:52:50 -05004582 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004583
4584 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07004585 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004586
Sean Christophersond264ee02018-08-27 15:21:12 -07004587 .request_immediate_exit = __kvm_request_immediate_exit,
4588
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004589 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02004590
4591 .pmu_ops = &amd_pmu_ops,
Paolo Bonzini33b22172020-04-17 10:24:18 -04004592 .nested_ops = &svm_nested_ops,
4593
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05004594 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08004595 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05004596 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02004597 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004598
Ladi Prosek72d7b372017-10-11 16:54:41 +02004599 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02004600 .pre_enter_smm = svm_pre_enter_smm,
4601 .pre_leave_smm = svm_pre_leave_smm,
Jason Baronb6a7cc32021-01-14 22:27:54 -05004602 .enable_smi_window = svm_enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06004603
4604 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004605 .mem_enc_reg_region = svm_register_enc_region,
4606 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02004607
Nathan Tempelman54526d12021-04-08 22:32:14 +00004608 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4609
Sean Christopherson09e3e2a2020-09-15 16:27:02 -07004610 .can_emulate_instruction = svm_can_emulate_instruction,
Liran Alon4b9852f2019-08-26 13:24:49 +03004611
4612 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Alexander Graffd6fa732020-09-25 16:34:19 +02004613
4614 .msr_filter_changed = svm_msr_filter_changed,
Tom Lendackyf1c63662020-12-14 10:29:50 -05004615 .complete_emulated_msr = svm_complete_emulated_msr,
Tom Lendacky647daca2021-01-04 14:20:01 -06004616
4617 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004618};
4619
Sean Christophersond008dfd2020-03-21 13:25:56 -07004620static struct kvm_x86_init_ops svm_init_ops __initdata = {
4621 .cpu_has_kvm_support = has_svm,
4622 .disabled_by_bios = is_disabled,
4623 .hardware_setup = svm_hardware_setup,
4624 .check_processor_compatibility = svm_check_processor_compat,
4625
4626 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08004627};
4628
4629static int __init svm_init(void)
4630{
Tom Lendackyd07f46f2020-09-07 15:15:03 +02004631 __unused_size_checks();
4632
Sean Christophersond008dfd2020-03-21 13:25:56 -07004633 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03004634 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004635}
4636
4637static void __exit svm_exit(void)
4638{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004639 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004640}
4641
4642module_init(svm_init)
4643module_exit(svm_exit)