blob: e32b4956fa0685e77b0f1f3c888cf745c12d2768 [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>
Josh Poimboeufc207aee2017-06-28 10:11:06 -050022#include <linux/frame.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>
Avi Kivity6aa8b732006-12-10 02:21:36 -080038
Eduardo Habkost63d11422008-11-17 19:03:20 -020039#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030040#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020041
Joerg Roedel883b0a92020-03-24 10:41:52 +010042#include "svm.h"
43
Avi Kivity4ecac3f2008-05-13 13:23:38 +030044#define __ex(x) __kvm_handle_fault_on_reboot(x)
45
Avi Kivity6aa8b732006-12-10 02:21:36 -080046MODULE_AUTHOR("Qumranet");
47MODULE_LICENSE("GPL");
48
Valdis Klētnieks575b2552020-02-27 21:49:52 -050049#ifdef MODULE
Josh Triplettae759542012-03-28 11:32:28 -070050static const struct x86_cpu_id svm_cpu_id[] = {
Thomas Gleixner320debe2020-03-20 14:13:50 +010051 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
Josh Triplettae759542012-03-28 11:32:28 -070052 {}
53};
54MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
Valdis Klētnieks575b2552020-02-27 21:49:52 -050055#endif
Josh Triplettae759542012-03-28 11:32:28 -070056
Avi Kivity6aa8b732006-12-10 02:21:36 -080057#define IOPM_ALLOC_ORDER 2
58#define MSRPM_ALLOC_ORDER 1
59
Avi Kivity6aa8b732006-12-10 02:21:36 -080060#define SEG_TYPE_LDT 2
61#define SEG_TYPE_BUSY_TSS16 3
62
Andre Przywara6bc31bd2010-04-11 23:07:28 +020063#define SVM_FEATURE_LBRV (1 << 1)
64#define SVM_FEATURE_SVML (1 << 2)
Andre Przywaraddce97a2010-12-21 11:12:03 +010065#define SVM_FEATURE_TSC_RATE (1 << 4)
66#define SVM_FEATURE_VMCB_CLEAN (1 << 5)
67#define SVM_FEATURE_FLUSH_ASID (1 << 6)
68#define SVM_FEATURE_DECODE_ASSIST (1 << 7)
Andre Przywara6bc31bd2010-04-11 23:07:28 +020069#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030070
Joerg Roedel24e09cb2008-02-13 18:58:47 +010071#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
72
Joerg Roedelfbc0db72011-03-25 09:44:46 +010073#define TSC_RATIO_RSVD 0xffffff0000000000ULL
Joerg Roedel92a1f122011-03-25 09:44:51 +010074#define TSC_RATIO_MIN 0x0000000000000001ULL
75#define TSC_RATIO_MAX 0x000000ffffffffffULL
Joerg Roedelfbc0db72011-03-25 09:44:46 +010076
Joerg Roedel67ec6602010-05-17 14:43:35 +020077static bool erratum_383_found __read_mostly;
78
Joerg Roedel883b0a92020-03-24 10:41:52 +010079u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
Joerg Roedel323c3d82010-03-01 15:34:37 +010080
Boris Ostrovsky2b036c62012-01-09 14:00:35 -050081/*
82 * Set osvw_len to higher value when updated Revision Guides
83 * are published and we know what the new status bits are
84 */
85static uint64_t osvw_len = 4, osvw_status;
86
Joerg Roedelfbc0db72011-03-25 09:44:46 +010087static DEFINE_PER_CPU(u64, current_tsc_ratio);
88#define TSC_RATIO_DEFAULT 0x0100000000ULL
89
Mathias Krause09941fb2012-08-30 01:30:20 +020090static const struct svm_direct_access_msrs {
Joerg Roedelac72a9b2010-03-01 15:34:36 +010091 u32 index; /* Index of the MSR */
92 bool always; /* True if intercept is always on */
93} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -040094 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +010095 { .index = MSR_IA32_SYSENTER_CS, .always = true },
96#ifdef CONFIG_X86_64
97 { .index = MSR_GS_BASE, .always = true },
98 { .index = MSR_FS_BASE, .always = true },
99 { .index = MSR_KERNEL_GS_BASE, .always = true },
100 { .index = MSR_LSTAR, .always = true },
101 { .index = MSR_CSTAR, .always = true },
102 { .index = MSR_SYSCALL_MASK, .always = true },
103#endif
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100104 { .index = MSR_IA32_SPEC_CTRL, .always = false },
Ashok Raj15d45072018-02-01 22:59:43 +0100105 { .index = MSR_IA32_PRED_CMD, .always = false },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100106 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
107 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
108 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
109 { .index = MSR_IA32_LASTINTTOIP, .always = false },
110 { .index = MSR_INVALID, .always = false },
Avi Kivity6c8166a2009-05-31 18:15:37 +0300111};
112
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100113/* enable NPT for AMD64 and X86 with PAE */
114#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
Joerg Roedel883b0a92020-03-24 10:41:52 +0100115bool npt_enabled = true;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100116#else
Joerg Roedel883b0a92020-03-24 10:41:52 +0100117bool npt_enabled;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100118#endif
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100119
Babu Moger8566ac82018-03-16 16:37:26 -0400120/*
121 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
122 * pause_filter_count: On processors that support Pause filtering(indicated
123 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
124 * count value. On VMRUN this value is loaded into an internal counter.
125 * Each time a pause instruction is executed, this counter is decremented
126 * until it reaches zero at which time a #VMEXIT is generated if pause
127 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
128 * Intercept Filtering for more details.
129 * This also indicate if ple logic enabled.
130 *
131 * pause_filter_thresh: In addition, some processor families support advanced
132 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
133 * the amount of time a guest is allowed to execute in a pause loop.
134 * In this mode, a 16-bit pause filter threshold field is added in the
135 * VMCB. The threshold value is a cycle count that is used to reset the
136 * pause counter. As with simple pause filtering, VMRUN loads the pause
137 * count value from VMCB into an internal counter. Then, on each pause
138 * instruction the hardware checks the elapsed number of cycles since
139 * the most recent pause instruction against the pause filter threshold.
140 * If the elapsed cycle count is greater than the pause filter threshold,
141 * then the internal pause count is reloaded from the VMCB and execution
142 * continues. If the elapsed cycle count is less than the pause filter
143 * threshold, then the internal pause count is decremented. If the count
144 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
145 * triggered. If advanced pause filtering is supported and pause filter
146 * threshold field is set to zero, the filter will operate in the simpler,
147 * count only mode.
148 */
149
150static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
151module_param(pause_filter_thresh, ushort, 0444);
152
153static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
154module_param(pause_filter_count, ushort, 0444);
155
156/* Default doubles per-vcpu window every exit. */
157static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
158module_param(pause_filter_count_grow, ushort, 0444);
159
160/* Default resets per-vcpu window every exit to pause_filter_count. */
161static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
162module_param(pause_filter_count_shrink, ushort, 0444);
163
164/* Default is to compute the maximum so we can never overflow. */
165static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
166module_param(pause_filter_count_max, ushort, 0444);
167
Davidlohr Buesoe2358852012-01-17 14:09:50 +0100168/* allow nested paging (virtualized MMU) for all guests */
169static int npt = true;
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100170module_param(npt, int, S_IRUGO);
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
Brijesh Singhe9df0942017-12-04 10:57:33 -0600188/* enable/disable SEV support */
189static int sev = IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT);
190module_param(sev, int, 0444);
191
Paolo Bonzini6f2f8452019-05-20 15:34:35 +0200192static bool __read_mostly dump_invalid_vmcb = 0;
193module_param(dump_invalid_vmcb, bool, 0644);
194
Brijesh Singh7607b712018-02-19 10:14:44 -0600195static u8 rsm_ins_bytes[] = "\x0f\xaa";
196
Joerg Roedela5c38322009-08-07 11:49:32 +0200197static void svm_complete_interrupts(struct vcpu_svm *svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -0500198
Tom Lendacky33af3a72019-10-03 21:17:48 +0000199static int sev_flush_asids(void);
200static DECLARE_RWSEM(sev_deactivate_lock);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +0000201static DEFINE_MUTEX(sev_bitmap_lock);
Brijesh Singhed3cd232017-12-04 10:57:32 -0600202static unsigned int max_sev_asid;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600203static unsigned int min_sev_asid;
204static unsigned long *sev_asid_bitmap;
Tom Lendacky33af3a72019-10-03 21:17:48 +0000205static unsigned long *sev_reclaim_asid_bitmap;
Brijesh Singh89c50582017-12-04 10:57:35 -0600206#define __sme_page_pa(x) __sme_set(page_to_pfn(x) << PAGE_SHIFT)
Brijesh Singh1654efc2017-12-04 10:57:34 -0600207
Brijesh Singh1e80fdc2017-12-04 10:57:38 -0600208struct enc_region {
209 struct list_head list;
210 unsigned long npages;
211 struct page **pages;
212 unsigned long uaddr;
213 unsigned long size;
214};
215
Sean Christopherson81811c12018-03-20 12:17:21 -0700216
Brijesh Singh1654efc2017-12-04 10:57:34 -0600217static inline bool svm_sev_enabled(void)
218{
Paolo Bonzini853c1102018-10-09 18:35:29 +0200219 return IS_ENABLED(CONFIG_KVM_AMD_SEV) ? max_sev_asid : 0;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600220}
221
222static inline bool sev_guest(struct kvm *kvm)
223{
Paolo Bonzini853c1102018-10-09 18:35:29 +0200224#ifdef CONFIG_KVM_AMD_SEV
Sean Christopherson81811c12018-03-20 12:17:21 -0700225 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -0600226
227 return sev->active;
Paolo Bonzini853c1102018-10-09 18:35:29 +0200228#else
229 return false;
230#endif
Brijesh Singh1654efc2017-12-04 10:57:34 -0600231}
Brijesh Singhed3cd232017-12-04 10:57:32 -0600232
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600233static inline int sev_get_asid(struct kvm *kvm)
234{
Sean Christopherson81811c12018-03-20 12:17:21 -0700235 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600236
237 return sev->asid;
238}
239
Harvey Harrison4866d5e2008-02-19 10:32:02 -0800240static unsigned long iopm_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800241
242struct kvm_ldttss_desc {
243 u16 limit0;
244 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100245 unsigned base1:8, type:5, dpl:2, p:1;
246 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800247 u32 base3;
248 u32 zero1;
249} __attribute__((packed));
250
251struct svm_cpu_data {
252 int cpu;
253
Avi Kivity5008fdf2007-04-02 13:05:50 +0300254 u64 asid_generation;
255 u32 max_asid;
256 u32 next_asid;
Brijesh Singh4faefff2017-12-04 10:57:25 -0600257 u32 min_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800258 struct kvm_ldttss_desc *tss_desc;
259
260 struct page *save_area;
Ashok Raj15d45072018-02-01 22:59:43 +0100261 struct vmcb *current_vmcb;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600262
263 /* index = sev_asid, value = vmcb pointer */
264 struct vmcb **sev_vmcbs;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800265};
266
267static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
268
Mathias Krause09941fb2012-08-30 01:30:20 +0200269static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
Avi Kivity6aa8b732006-12-10 02:21:36 -0800270
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200271#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272#define MSRS_RANGE_SIZE 2048
273#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
274
Joerg Roedel883b0a92020-03-24 10:41:52 +0100275u32 svm_msrpm_offset(u32 msr)
Joerg Roedel455716f2010-03-01 15:34:35 +0100276{
277 u32 offset;
278 int i;
279
280 for (i = 0; i < NUM_MSR_MAPS; i++) {
281 if (msr < msrpm_ranges[i] ||
282 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
283 continue;
284
285 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
286 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
287
288 /* Now we have the u8 offset - but need the u32 offset */
289 return offset / 4;
290 }
291
292 /* MSR not in any range */
293 return MSR_INVALID;
294}
295
Avi Kivity6aa8b732006-12-10 02:21:36 -0800296#define MAX_INST_SIZE 15
297
Avi Kivity6aa8b732006-12-10 02:21:36 -0800298static inline void clgi(void)
299{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100300 asm volatile (__ex("clgi"));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800301}
302
303static inline void stgi(void)
304{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100305 asm volatile (__ex("stgi"));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800306}
307
308static inline void invlpga(unsigned long addr, u32 asid)
309{
Uros Bizjakac5ffda22018-11-26 17:00:08 +0100310 asm volatile (__ex("invlpga %1, %0") : : "c"(asid), "a"(addr));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800311}
312
Yu Zhang855feb62017-08-24 20:27:55 +0800313static int get_npt_level(struct kvm_vcpu *vcpu)
Joerg Roedel4b161842010-09-10 17:31:03 +0200314{
315#ifdef CONFIG_X86_64
Yu Zhang2a7266a2017-08-24 20:27:54 +0800316 return PT64_ROOT_4LEVEL;
Joerg Roedel4b161842010-09-10 17:31:03 +0200317#else
318 return PT32E_ROOT_LEVEL;
319#endif
320}
321
Joerg Roedel883b0a92020-03-24 10:41:52 +0100322void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000324 vcpu->arch.efer = efer;
Paolo Bonzini9167ab72019-10-27 16:23:23 +0100325
326 if (!npt_enabled) {
327 /* Shadow paging assumes NX to be available. */
328 efer |= EFER_NX;
329
330 if (!(efer & EFER_LMA))
331 efer &= ~EFER_LME;
332 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333
Alexander Graf9962d032008-11-25 20:17:02 +0100334 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Joerg Roedeldcca1a62010-12-03 11:45:54 +0100335 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800336}
337
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338static int is_external_interrupt(u32 info)
339{
340 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
341 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
342}
343
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200344static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
Glauber Costa2809f5d2009-05-12 16:21:05 -0400345{
346 struct vcpu_svm *svm = to_svm(vcpu);
347 u32 ret = 0;
348
349 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Paolo Bonzini37ccdcb2014-05-20 14:29:47 +0200350 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
351 return ret;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400352}
353
354static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
355{
356 struct vcpu_svm *svm = to_svm(vcpu);
357
358 if (mask == 0)
359 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
360 else
361 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
362
363}
364
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200365static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800366{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400367 struct vcpu_svm *svm = to_svm(vcpu);
368
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200369 if (nrips && svm->vmcb->control.next_rip != 0) {
Dirk Müllerd2922422015-10-01 13:43:42 +0200370 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200371 svm->next_rip = svm->vmcb->control.next_rip;
Bandan Dasf1047652015-06-11 02:05:33 -0400372 }
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200373
Sean Christopherson1957aa62019-08-27 14:40:39 -0700374 if (!svm->next_rip) {
375 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
376 return 0;
377 } else {
378 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
379 pr_err("%s: ip 0x%lx next 0x%llx\n",
380 __func__, kvm_rip_read(vcpu), svm->next_rip);
381 kvm_rip_write(vcpu, svm->next_rip);
382 }
Glauber Costa2809f5d2009-05-12 16:21:05 -0400383 svm_set_interrupt_shadow(vcpu, 0);
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200384
Sean Christopherson60fc3d02019-08-27 14:40:38 -0700385 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800386}
387
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700388static void svm_queue_exception(struct kvm_vcpu *vcpu)
Jan Kiszka116a4752010-02-23 17:47:54 +0100389{
390 struct vcpu_svm *svm = to_svm(vcpu);
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700391 unsigned nr = vcpu->arch.exception.nr;
392 bool has_error_code = vcpu->arch.exception.has_error_code;
Wanpeng Li664f8e22017-08-24 03:35:09 -0700393 bool reinject = vcpu->arch.exception.injected;
Wanpeng Licfcd20e2017-07-13 18:30:39 -0700394 u32 error_code = vcpu->arch.exception.error_code;
Jan Kiszka116a4752010-02-23 17:47:54 +0100395
Joerg Roedele0231712010-02-24 18:59:10 +0100396 /*
397 * If we are within a nested VM we'd better #VMEXIT and let the guest
398 * handle the exception
399 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200400 if (!reinject &&
401 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100402 return;
403
Jim Mattsonda998b42018-10-16 14:29:22 -0700404 kvm_deliver_exception_payload(&svm->vcpu);
405
Paolo Bonzinid647eb62019-06-20 14:13:33 +0200406 if (nr == BP_VECTOR && !nrips) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100407 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
408
409 /*
410 * For guest debugging where we have to reinject #BP if some
411 * INT3 is guest-owned:
412 * Emulate nRIP by moving RIP forward. Will fail if injection
413 * raises a fault that is not intercepted. Still better than
414 * failing in all cases.
415 */
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +0200416 (void)skip_emulated_instruction(&svm->vcpu);
Jan Kiszka66b71382010-02-23 17:47:56 +0100417 rip = kvm_rip_read(&svm->vcpu);
418 svm->int3_rip = rip + svm->vmcb->save.cs.base;
419 svm->int3_injected = rip - old_rip;
420 }
421
Jan Kiszka116a4752010-02-23 17:47:54 +0100422 svm->vmcb->control.event_inj = nr
423 | SVM_EVTINJ_VALID
424 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
425 | SVM_EVTINJ_TYPE_EXEPT;
426 svm->vmcb->control.event_inj_err = error_code;
427}
428
Joerg Roedel67ec6602010-05-17 14:43:35 +0200429static void svm_init_erratum_383(void)
430{
431 u32 low, high;
432 int err;
433 u64 val;
434
Borislav Petkove6ee94d2013-03-20 15:07:27 +0100435 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200436 return;
437
438 /* Use _safe variants to not break nested virtualization */
439 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
440 if (err)
441 return;
442
443 val |= (1ULL << 47);
444
445 low = lower_32_bits(val);
446 high = upper_32_bits(val);
447
448 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
449
450 erratum_383_found = true;
451}
452
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500453static void svm_init_osvw(struct kvm_vcpu *vcpu)
454{
455 /*
456 * Guests should see errata 400 and 415 as fixed (assuming that
457 * HLT and IO instructions are intercepted).
458 */
459 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
460 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
461
462 /*
463 * By increasing VCPU's osvw.length to 3 we are telling the guest that
464 * all osvw.status bits inside that length, including bit 0 (which is
465 * reserved for erratum 298), are valid. However, if host processor's
466 * osvw_len is 0 then osvw_status[0] carries no information. We need to
467 * be conservative here and therefore we tell the guest that erratum 298
468 * is present (because we really don't know).
469 */
470 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
471 vcpu->arch.osvw.status |= 1;
472}
473
Avi Kivity6aa8b732006-12-10 02:21:36 -0800474static int has_svm(void)
475{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200476 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477
Eduardo Habkost63d11422008-11-17 19:03:20 -0200478 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800479 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 return 0;
481 }
482
Avi Kivity6aa8b732006-12-10 02:21:36 -0800483 return 1;
484}
485
Radim Krčmář13a34e02014-08-28 15:13:03 +0200486static void svm_hardware_disable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487{
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100488 /* Make sure we clean up behind us */
489 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
490 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
491
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200492 cpu_svm_disable();
Joerg Roedel1018faa2012-02-29 14:57:32 +0100493
494 amd_pmu_disable_virt();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800495}
496
Radim Krčmář13a34e02014-08-28 15:13:03 +0200497static int svm_hardware_enable(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800498{
499
Tejun Heo0fe1e002009-10-29 22:34:14 +0900500 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800501 uint64_t efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502 struct desc_struct *gdt;
503 int me = raw_smp_processor_id();
504
Alexander Graf10474ae2009-09-15 11:37:46 +0200505 rdmsrl(MSR_EFER, efer);
506 if (efer & EFER_SVME)
507 return -EBUSY;
508
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 if (!has_svm()) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200510 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200511 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900513 sd = per_cpu(svm_data, me);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900514 if (!sd) {
Borislav Petkov1f5b77f2012-10-20 20:20:04 +0200515 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200516 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800517 }
518
Tejun Heo0fe1e002009-10-29 22:34:14 +0900519 sd->asid_generation = 1;
520 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
521 sd->next_asid = sd->max_asid + 1;
Brijesh Singhed3cd232017-12-04 10:57:32 -0600522 sd->min_asid = max_sev_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800523
Thomas Garnier45fc8752017-03-14 10:05:08 -0700524 gdt = get_current_gdt_rw();
Tejun Heo0fe1e002009-10-29 22:34:14 +0900525 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526
Alexander Graf9962d032008-11-25 20:17:02 +0100527 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528
Linus Torvaldsd0316552009-12-14 09:58:24 -0800529 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200530
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100531 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
532 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500533 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
Joerg Roedelfbc0db72011-03-25 09:44:46 +0100534 }
535
Boris Ostrovsky2b036c62012-01-09 14:00:35 -0500536
537 /*
538 * Get OSVW bits.
539 *
540 * Note that it is possible to have a system with mixed processor
541 * revisions and therefore different OSVW bits. If bits are not the same
542 * on different processors then choose the worst case (i.e. if erratum
543 * is present on one processor and not on another then assume that the
544 * erratum is present everywhere).
545 */
546 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
547 uint64_t len, status = 0;
548 int err;
549
550 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
551 if (!err)
552 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
553 &err);
554
555 if (err)
556 osvw_status = osvw_len = 0;
557 else {
558 if (len < osvw_len)
559 osvw_len = len;
560 osvw_status |= status;
561 osvw_status &= (1ULL << osvw_len) - 1;
562 }
563 } else
564 osvw_status = osvw_len = 0;
565
Joerg Roedel67ec6602010-05-17 14:43:35 +0200566 svm_init_erratum_383();
567
Joerg Roedel1018faa2012-02-29 14:57:32 +0100568 amd_pmu_enable_virt();
569
Alexander Graf10474ae2009-09-15 11:37:46 +0200570 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800571}
572
Joerg Roedel0da1db752008-07-02 16:02:11 +0200573static void svm_cpu_uninit(int cpu)
574{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900575 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200576
Tejun Heo0fe1e002009-10-29 22:34:14 +0900577 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200578 return;
579
580 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600581 kfree(sd->sev_vmcbs);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900582 __free_page(sd->save_area);
583 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200584}
585
Avi Kivity6aa8b732006-12-10 02:21:36 -0800586static int svm_cpu_init(int cpu)
587{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900588 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800589
Tejun Heo0fe1e002009-10-29 22:34:14 +0900590 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
591 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800592 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900593 sd->cpu = cpu;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600594 sd->save_area = alloc_page(GFP_KERNEL);
Tejun Heo0fe1e002009-10-29 22:34:14 +0900595 if (!sd->save_area)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800596 goto free_cpu_data;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800597
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600598 if (svm_sev_enabled()) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700599 sd->sev_vmcbs = kmalloc_array(max_sev_asid + 1,
600 sizeof(void *),
601 GFP_KERNEL);
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600602 if (!sd->sev_vmcbs)
Miaohe Lind80b64f2020-01-04 16:56:49 +0800603 goto free_save_area;
Brijesh Singh70cd94e2017-12-04 10:57:34 -0600604 }
605
Tejun Heo0fe1e002009-10-29 22:34:14 +0900606 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800607
608 return 0;
609
Miaohe Lind80b64f2020-01-04 16:56:49 +0800610free_save_area:
611 __free_page(sd->save_area);
612free_cpu_data:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900613 kfree(sd);
Miaohe Lind80b64f2020-01-04 16:56:49 +0800614 return -ENOMEM;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800615
616}
617
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100618static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800619{
620 int i;
621
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100622 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
623 if (direct_access_msrs[i].index == index)
624 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800625
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100626 return false;
627}
628
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +0100629static bool msr_write_intercepted(struct kvm_vcpu *vcpu, unsigned msr)
630{
631 u8 bit_write;
632 unsigned long tmp;
633 u32 offset;
634 u32 *msrpm;
635
636 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
637 to_svm(vcpu)->msrpm;
638
639 offset = svm_msrpm_offset(msr);
640 bit_write = 2 * (msr & 0x0f) + 1;
641 tmp = msrpm[offset];
642
643 BUG_ON(offset == MSR_INVALID);
644
645 return !!test_bit(bit_write, &tmp);
646}
647
Avi Kivity6aa8b732006-12-10 02:21:36 -0800648static void set_msr_interception(u32 *msrpm, unsigned msr,
649 int read, int write)
650{
Joerg Roedel455716f2010-03-01 15:34:35 +0100651 u8 bit_read, bit_write;
652 unsigned long tmp;
653 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800654
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100655 /*
656 * If this warning triggers extend the direct_access_msrs list at the
657 * beginning of the file
658 */
659 WARN_ON(!valid_msr_intercept(msr));
660
Joerg Roedel455716f2010-03-01 15:34:35 +0100661 offset = svm_msrpm_offset(msr);
662 bit_read = 2 * (msr & 0x0f);
663 bit_write = 2 * (msr & 0x0f) + 1;
664 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800665
Joerg Roedel455716f2010-03-01 15:34:35 +0100666 BUG_ON(offset == MSR_INVALID);
667
668 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
669 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
670
671 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800672}
673
Joerg Roedelf65c2292008-02-13 18:58:46 +0100674static void svm_vcpu_init_msrpm(u32 *msrpm)
675{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100676 int i;
677
Joerg Roedelf65c2292008-02-13 18:58:46 +0100678 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
679
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100680 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
681 if (!direct_access_msrs[i].always)
682 continue;
683
684 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
685 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100686}
687
Joerg Roedel323c3d82010-03-01 15:34:37 +0100688static void add_msr_offset(u32 offset)
689{
690 int i;
691
692 for (i = 0; i < MSRPM_OFFSETS; ++i) {
693
694 /* Offset already in list? */
695 if (msrpm_offsets[i] == offset)
696 return;
697
698 /* Slot used by another offset? */
699 if (msrpm_offsets[i] != MSR_INVALID)
700 continue;
701
702 /* Add offset to list */
703 msrpm_offsets[i] = offset;
704
705 return;
706 }
707
708 /*
709 * If this BUG triggers the msrpm_offsets table has an overflow. Just
710 * increase MSRPM_OFFSETS in this case.
711 */
712 BUG();
713}
714
715static void init_msrpm_offsets(void)
716{
717 int i;
718
719 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
720
721 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
722 u32 offset;
723
724 offset = svm_msrpm_offset(direct_access_msrs[i].index);
725 BUG_ON(offset == MSR_INVALID);
726
727 add_msr_offset(offset);
728 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800729}
730
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100731static void svm_enable_lbrv(struct vcpu_svm *svm)
732{
733 u32 *msrpm = svm->msrpm;
734
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500735 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100736 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
737 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
738 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
739 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
740}
741
742static void svm_disable_lbrv(struct vcpu_svm *svm)
743{
744 u32 *msrpm = svm->msrpm;
745
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -0500746 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100747 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
748 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
749 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
750 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
751}
752
Joerg Roedel883b0a92020-03-24 10:41:52 +0100753void disable_nmi_singlestep(struct vcpu_svm *svm)
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200754{
755 svm->nmi_singlestep = false;
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -0500756
Ladi Prosekab2f4d732017-06-21 09:06:58 +0200757 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
758 /* Clear our flags if they were not set by the guest */
759 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
760 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
761 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
762 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
763 }
Ladi Prosek4aebd0e2017-06-21 09:06:57 +0200764}
765
Brijesh Singhe9df0942017-12-04 10:57:33 -0600766static __init int sev_hardware_setup(void)
767{
768 struct sev_user_data_status *status;
769 int rc;
770
771 /* Maximum number of encrypted guests supported simultaneously */
772 max_sev_asid = cpuid_ecx(0x8000001F);
773
774 if (!max_sev_asid)
775 return 1;
776
Brijesh Singh1654efc2017-12-04 10:57:34 -0600777 /* Minimum ASID value that should be used for SEV guest */
778 min_sev_asid = cpuid_edx(0x8000001F);
779
Tom Lendacky33af3a72019-10-03 21:17:48 +0000780 /* Initialize SEV ASID bitmaps */
Andy Shevchenkoa101c9d632018-08-30 14:49:59 +0300781 sev_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
Brijesh Singh1654efc2017-12-04 10:57:34 -0600782 if (!sev_asid_bitmap)
783 return 1;
784
Tom Lendacky33af3a72019-10-03 21:17:48 +0000785 sev_reclaim_asid_bitmap = bitmap_zalloc(max_sev_asid, GFP_KERNEL);
786 if (!sev_reclaim_asid_bitmap)
787 return 1;
788
Brijesh Singhe9df0942017-12-04 10:57:33 -0600789 status = kmalloc(sizeof(*status), GFP_KERNEL);
790 if (!status)
791 return 1;
792
793 /*
794 * Check SEV platform status.
795 *
796 * PLATFORM_STATUS can be called in any state, if we failed to query
797 * the PLATFORM status then either PSP firmware does not support SEV
798 * feature or SEV firmware is dead.
799 */
800 rc = sev_platform_status(status, NULL);
801 if (rc)
802 goto err;
803
804 pr_info("SEV supported\n");
805
806err:
807 kfree(status);
808 return rc;
809}
810
Babu Moger8566ac82018-03-16 16:37:26 -0400811static void grow_ple_window(struct kvm_vcpu *vcpu)
812{
813 struct vcpu_svm *svm = to_svm(vcpu);
814 struct vmcb_control_area *control = &svm->vmcb->control;
815 int old = control->pause_filter_count;
816
817 control->pause_filter_count = __grow_ple_window(old,
818 pause_filter_count,
819 pause_filter_count_grow,
820 pause_filter_count_max);
821
Peter Xu4f75bcc2019-09-06 10:17:22 +0800822 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -0400823 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800824 trace_kvm_ple_window_update(vcpu->vcpu_id,
825 control->pause_filter_count, old);
826 }
Babu Moger8566ac82018-03-16 16:37:26 -0400827}
828
829static void shrink_ple_window(struct kvm_vcpu *vcpu)
830{
831 struct vcpu_svm *svm = to_svm(vcpu);
832 struct vmcb_control_area *control = &svm->vmcb->control;
833 int old = control->pause_filter_count;
834
835 control->pause_filter_count =
836 __shrink_ple_window(old,
837 pause_filter_count,
838 pause_filter_count_shrink,
839 pause_filter_count);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800840 if (control->pause_filter_count != old) {
Babu Moger8566ac82018-03-16 16:37:26 -0400841 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Peter Xu4f75bcc2019-09-06 10:17:22 +0800842 trace_kvm_ple_window_update(vcpu->vcpu_id,
843 control->pause_filter_count, old);
844 }
Babu Moger8566ac82018-03-16 16:37:26 -0400845}
846
Tom Lendacky52918ed2020-01-09 17:42:16 -0600847/*
848 * The default MMIO mask is a single bit (excluding the present bit),
849 * which could conflict with the memory encryption bit. Check for
850 * memory encryption support and override the default MMIO mask if
851 * memory encryption is enabled.
852 */
853static __init void svm_adjust_mmio_mask(void)
854{
855 unsigned int enc_bit, mask_bit;
856 u64 msr, mask;
857
858 /* If there is no memory encryption support, use existing mask */
859 if (cpuid_eax(0x80000000) < 0x8000001f)
860 return;
861
862 /* If memory encryption is not enabled, use existing mask */
863 rdmsrl(MSR_K8_SYSCFG, msr);
864 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
865 return;
866
867 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
868 mask_bit = boot_cpu_data.x86_phys_bits;
869
870 /* Increment the mask bit if it is the same as the encryption bit */
871 if (enc_bit == mask_bit)
872 mask_bit++;
873
874 /*
875 * If the mask bit location is below 52, then some bits above the
876 * physical addressing limit will always be reserved, so use the
877 * rsvd_bits() function to generate the mask. This mask, along with
878 * the present bit, will be used to generate a page fault with
879 * PFER.RSV = 1.
880 *
881 * If the mask bit location is 52 (or above), then clear the mask.
882 */
883 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
884
885 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
886}
887
Li RongQingdd58f3c2020-02-23 16:13:12 +0800888static void svm_hardware_teardown(void)
889{
890 int cpu;
891
892 if (svm_sev_enabled()) {
893 bitmap_free(sev_asid_bitmap);
894 bitmap_free(sev_reclaim_asid_bitmap);
895
896 sev_flush_asids();
897 }
898
899 for_each_possible_cpu(cpu)
900 svm_cpu_uninit(cpu);
901
902 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
903 iopm_base = 0;
904}
905
Sean Christopherson9b58b982020-03-02 15:56:42 -0800906static __init void svm_set_cpu_caps(void)
907{
908 kvm_set_cpu_caps();
909
Paolo Bonzini408e9a32020-03-05 16:11:56 +0100910 supported_xss = 0;
911
Sean Christophersona50718c2020-03-02 15:57:07 -0800912 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
913 if (nested) {
Sean Christopherson9b58b982020-03-02 15:56:42 -0800914 kvm_cpu_cap_set(X86_FEATURE_SVM);
915
Sean Christopherson4eb87462020-03-02 15:57:08 -0800916 if (nrips)
Sean Christophersona50718c2020-03-02 15:57:07 -0800917 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
918
919 if (npt_enabled)
920 kvm_cpu_cap_set(X86_FEATURE_NPT);
921 }
922
Sean Christopherson93c380e2020-03-02 15:56:54 -0800923 /* CPUID 0x80000008 */
924 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
925 boot_cpu_has(X86_FEATURE_AMD_SSBD))
926 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
Sean Christopherson9b58b982020-03-02 15:56:42 -0800927}
928
Avi Kivity6aa8b732006-12-10 02:21:36 -0800929static __init int svm_hardware_setup(void)
930{
931 int cpu;
932 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100933 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800934 int r;
935
Avi Kivity6aa8b732006-12-10 02:21:36 -0800936 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
937
938 if (!iopm_pages)
939 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300940
941 iopm_va = page_address(iopm_pages);
942 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800943 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
944
Joerg Roedel323c3d82010-03-01 15:34:37 +0100945 init_msrpm_offsets();
946
Sean Christophersoncfc48182020-03-02 15:56:23 -0800947 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
948
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100949 if (boot_cpu_has(X86_FEATURE_NX))
950 kvm_enable_efer_bits(EFER_NX);
951
Alexander Graf1b2fd702009-02-02 16:23:51 +0100952 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
953 kvm_enable_efer_bits(EFER_FFXSR);
954
Joerg Roedel92a1f122011-03-25 09:44:51 +0100955 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
Joerg Roedel92a1f122011-03-25 09:44:51 +0100956 kvm_has_tsc_control = true;
Haozhong Zhangbc9b9612015-10-20 15:39:01 +0800957 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
958 kvm_tsc_scaling_ratio_frac_bits = 32;
Joerg Roedel92a1f122011-03-25 09:44:51 +0100959 }
960
Babu Moger8566ac82018-03-16 16:37:26 -0400961 /* Check for pause filtering support */
962 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
963 pause_filter_count = 0;
964 pause_filter_thresh = 0;
965 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
966 pause_filter_thresh = 0;
967 }
968
Alexander Graf236de052008-11-25 20:17:10 +0100969 if (nested) {
970 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200971 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100972 }
973
Brijesh Singhe9df0942017-12-04 10:57:33 -0600974 if (sev) {
975 if (boot_cpu_has(X86_FEATURE_SEV) &&
976 IS_ENABLED(CONFIG_KVM_AMD_SEV)) {
977 r = sev_hardware_setup();
978 if (r)
979 sev = false;
980 } else {
981 sev = false;
982 }
983 }
984
Tom Lendacky52918ed2020-01-09 17:42:16 -0600985 svm_adjust_mmio_mask();
986
Zachary Amsden3230bb42009-09-29 11:38:37 -1000987 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800988 r = svm_cpu_init(cpu);
989 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100990 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800991 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100992
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200993 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100994 npt_enabled = false;
995
Sean Christopherson213e0e12020-03-02 15:57:01 -0800996 if (npt_enabled && !npt)
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100997 npt_enabled = false;
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100998
Sean Christopherson703c3352020-03-02 15:57:03 -0800999 kvm_configure_mmu(npt_enabled, PT_PDPE_LEVEL);
Sean Christopherson213e0e12020-03-02 15:57:01 -08001000 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
Joerg Roedele3da3ac2008-02-07 13:47:39 +01001001
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
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001029 if (vgif) {
1030 if (!boot_cpu_has(X86_FEATURE_VGIF))
1031 vgif = false;
1032 else
1033 pr_info("Virtual GIF supported\n");
1034 }
1035
Sean Christopherson9b58b982020-03-02 15:56:42 -08001036 svm_set_cpu_caps();
Sean Christopherson66a69502020-03-02 15:56:41 -08001037
Avi Kivity6aa8b732006-12-10 02:21:36 -08001038 return 0;
1039
Joerg Roedelf65c2292008-02-13 18:58:46 +01001040err:
Li RongQingdd58f3c2020-02-23 16:13:12 +08001041 svm_hardware_teardown();
Avi Kivity6aa8b732006-12-10 02:21:36 -08001042 return r;
1043}
1044
Avi Kivity6aa8b732006-12-10 02:21:36 -08001045static void init_seg(struct vmcb_seg *seg)
1046{
1047 seg->selector = 0;
1048 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +01001049 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001050 seg->limit = 0xffff;
1051 seg->base = 0;
1052}
1053
1054static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1055{
1056 seg->selector = 0;
1057 seg->attrib = SVM_SELECTOR_P_MASK | type;
1058 seg->limit = 0xffff;
1059 seg->base = 0;
1060}
1061
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001062static u64 svm_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
1063{
1064 struct vcpu_svm *svm = to_svm(vcpu);
1065
1066 if (is_guest_mode(vcpu))
1067 return svm->nested.hsave->control.tsc_offset;
1068
1069 return vcpu->arch.tsc_offset;
1070}
1071
Leonid Shatz326e7422018-11-06 12:14:25 +02001072static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001073{
1074 struct vcpu_svm *svm = to_svm(vcpu);
1075 u64 g_tsc_offset = 0;
1076
Joerg Roedel20307532010-11-29 17:51:48 +01001077 if (is_guest_mode(vcpu)) {
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02001078 /* Write L1's TSC offset. */
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001079 g_tsc_offset = svm->vmcb->control.tsc_offset -
1080 svm->nested.hsave->control.tsc_offset;
1081 svm->nested.hsave->control.tsc_offset = offset;
Paolo Bonzini45c3af92018-11-25 18:45:35 +01001082 }
1083
1084 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1085 svm->vmcb->control.tsc_offset - g_tsc_offset,
1086 offset);
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001087
1088 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
Joerg Roedel116a0a22010-12-03 11:45:49 +01001089
1090 mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
Leonid Shatz326e7422018-11-06 12:14:25 +02001091 return svm->vmcb->control.tsc_offset;
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10001092}
1093
Paolo Bonzini56908912015-10-19 11:30:19 +02001094static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001095{
Joerg Roedele6101a92008-02-13 18:58:45 +01001096 struct vmcb_control_area *control = &svm->vmcb->control;
1097 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001098
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001099 svm->vcpu.arch.hflags = 0;
Avi Kivitybff78272010-01-07 13:16:08 +02001100
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001101 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1102 set_cr_intercept(svm, INTERCEPT_CR3_READ);
1103 set_cr_intercept(svm, INTERCEPT_CR4_READ);
1104 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
1105 set_cr_intercept(svm, INTERCEPT_CR3_WRITE);
1106 set_cr_intercept(svm, INTERCEPT_CR4_WRITE);
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05001107 if (!kvm_vcpu_apicv_active(&svm->vcpu))
1108 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109
Paolo Bonzini5315c712014-03-03 13:08:29 +01001110 set_dr_intercepts(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001111
Joerg Roedel18c918c2010-11-30 18:03:59 +01001112 set_exception_intercept(svm, PF_VECTOR);
1113 set_exception_intercept(svm, UD_VECTOR);
1114 set_exception_intercept(svm, MC_VECTOR);
Eric Northup54a20552015-11-03 18:03:53 +01001115 set_exception_intercept(svm, AC_VECTOR);
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001116 set_exception_intercept(svm, DB_VECTOR);
Liran Alon97184202018-03-12 13:12:52 +02001117 /*
1118 * Guest access to VMware backdoor ports could legitimately
1119 * trigger #GP because of TSS I/O permission bitmap.
1120 * We intercept those #GP and allow access to them anyway
1121 * as VMware does.
1122 */
1123 if (enable_vmware_backdoor)
1124 set_exception_intercept(svm, GP_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001126 set_intercept(svm, INTERCEPT_INTR);
1127 set_intercept(svm, INTERCEPT_NMI);
1128 set_intercept(svm, INTERCEPT_SMI);
1129 set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
Avi Kivity332b56e2011-11-10 14:57:24 +02001130 set_intercept(svm, INTERCEPT_RDPMC);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001131 set_intercept(svm, INTERCEPT_CPUID);
1132 set_intercept(svm, INTERCEPT_INVD);
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001133 set_intercept(svm, INTERCEPT_INVLPG);
1134 set_intercept(svm, INTERCEPT_INVLPGA);
1135 set_intercept(svm, INTERCEPT_IOIO_PROT);
1136 set_intercept(svm, INTERCEPT_MSR_PROT);
1137 set_intercept(svm, INTERCEPT_TASK_SWITCH);
1138 set_intercept(svm, INTERCEPT_SHUTDOWN);
1139 set_intercept(svm, INTERCEPT_VMRUN);
1140 set_intercept(svm, INTERCEPT_VMMCALL);
1141 set_intercept(svm, INTERCEPT_VMLOAD);
1142 set_intercept(svm, INTERCEPT_VMSAVE);
1143 set_intercept(svm, INTERCEPT_STGI);
1144 set_intercept(svm, INTERCEPT_CLGI);
1145 set_intercept(svm, INTERCEPT_SKINIT);
1146 set_intercept(svm, INTERCEPT_WBINVD);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01001147 set_intercept(svm, INTERCEPT_XSETBV);
Jim Mattson0cb84102019-09-19 15:59:17 -07001148 set_intercept(svm, INTERCEPT_RDPRU);
Brijesh Singh7607b712018-02-19 10:14:44 -06001149 set_intercept(svm, INTERCEPT_RSM);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001150
Wanpeng Li4d5422c2018-03-12 04:53:02 -07001151 if (!kvm_mwait_in_guest(svm->vcpu.kvm)) {
Michael S. Tsirkin668fffa2017-04-21 12:27:17 +02001152 set_intercept(svm, INTERCEPT_MONITOR);
1153 set_intercept(svm, INTERCEPT_MWAIT);
1154 }
1155
Wanpeng Licaa057a2018-03-12 04:53:03 -07001156 if (!kvm_hlt_in_guest(svm->vcpu.kvm))
1157 set_intercept(svm, INTERCEPT_HLT);
1158
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001159 control->iopm_base_pa = __sme_set(iopm_base);
1160 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
Avi Kivity6aa8b732006-12-10 02:21:36 -08001161 control->int_ctl = V_INTR_MASKING_MASK;
1162
1163 init_seg(&save->es);
1164 init_seg(&save->ss);
1165 init_seg(&save->ds);
1166 init_seg(&save->fs);
1167 init_seg(&save->gs);
1168
1169 save->cs.selector = 0xf000;
Paolo Bonzini04b66832013-03-19 16:30:26 +01001170 save->cs.base = 0xffff0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171 /* Executable/Readable Code Segment */
1172 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1173 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1174 save->cs.limit = 0xffff;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001175
1176 save->gdtr.limit = 0xffff;
1177 save->idtr.limit = 0xffff;
1178
1179 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1180 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1181
Paolo Bonzini56908912015-10-19 11:30:19 +02001182 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -04001183 save->dr6 = 0xffff0ff0;
Avi Kivityf6e78472010-08-02 15:30:20 +03001184 kvm_set_rflags(&svm->vcpu, 2);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001185 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001186 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187
Joerg Roedele0231712010-02-24 18:59:10 +01001188 /*
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001189 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001190 * It also updates the guest-visible cr0 value.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001191 */
Paolo Bonzini79a80592015-09-21 07:46:55 +02001192 svm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Igor Mammedovebae8712015-09-18 15:39:05 +02001193 kvm_mmu_reset_context(&svm->vcpu);
Eduardo Habkost18fa0002009-10-24 02:49:59 -02001194
Rusty Russell66aee912007-07-17 23:34:16 +10001195 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001196 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001197
1198 if (npt_enabled) {
1199 /* Setup VMCB for Nested Paging */
Tom Lendackycea3a192017-12-04 10:57:24 -06001200 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001201 clr_intercept(svm, INTERCEPT_INVLPG);
Joerg Roedel18c918c2010-11-30 18:03:59 +01001202 clr_exception_intercept(svm, PF_VECTOR);
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001203 clr_cr_intercept(svm, INTERCEPT_CR3_READ);
1204 clr_cr_intercept(svm, INTERCEPT_CR3_WRITE);
Radim Krčmář74545702015-04-27 15:11:25 +02001205 save->g_pat = svm->vcpu.arch.pat;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001206 save->cr3 = 0;
1207 save->cr4 = 0;
1208 }
Joerg Roedelf40f6a42010-12-03 15:25:15 +01001209 svm->asid_generation = 0;
Alexander Graf1371d902008-11-25 20:17:04 +01001210
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001211 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +02001212 svm->vcpu.arch.hflags = 0;
1213
Babu Moger8566ac82018-03-16 16:37:26 -04001214 if (pause_filter_count) {
1215 control->pause_filter_count = pause_filter_count;
1216 if (pause_filter_thresh)
1217 control->pause_filter_thresh = pause_filter_thresh;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001218 set_intercept(svm, INTERCEPT_PAUSE);
Babu Moger8566ac82018-03-16 16:37:26 -04001219 } else {
1220 clr_intercept(svm, INTERCEPT_PAUSE);
Mark Langsdorf565d0992009-10-06 14:25:02 -05001221 }
1222
Suravee Suthikulpanit67034bb2017-09-12 10:42:42 -05001223 if (kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001224 avic_init_vmcb(svm);
1225
Janakarajan Natarajan89c8a492017-07-06 15:50:47 -05001226 /*
1227 * If hardware supports Virtual VMLOAD VMSAVE then enable it
1228 * in VMCB and clear intercepts to avoid #VMEXIT.
1229 */
1230 if (vls) {
1231 clr_intercept(svm, INTERCEPT_VMLOAD);
1232 clr_intercept(svm, INTERCEPT_VMSAVE);
1233 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
1234 }
1235
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05001236 if (vgif) {
1237 clr_intercept(svm, INTERCEPT_STGI);
1238 clr_intercept(svm, INTERCEPT_CLGI);
1239 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1240 }
1241
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001242 if (sev_guest(svm->vcpu.kvm)) {
Brijesh Singh1654efc2017-12-04 10:57:34 -06001243 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
Brijesh Singh35c6f6492017-12-04 10:57:39 -06001244 clr_exception_intercept(svm, UD_VECTOR);
1245 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001246
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001247 mark_all_dirty(svm->vmcb);
1248
Joerg Roedel2af91942009-08-07 11:49:28 +02001249 enable_gif(svm);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001250
1251}
1252
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001253static void sev_asid_free(int asid)
Brijesh Singh1654efc2017-12-04 10:57:34 -06001254{
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001255 struct svm_cpu_data *sd;
1256 int cpu, pos;
Brijesh Singh1654efc2017-12-04 10:57:34 -06001257
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001258 mutex_lock(&sev_bitmap_lock);
1259
Brijesh Singh1654efc2017-12-04 10:57:34 -06001260 pos = asid - 1;
Tom Lendacky33af3a72019-10-03 21:17:48 +00001261 __set_bit(pos, sev_reclaim_asid_bitmap);
Brijesh Singh70cd94e2017-12-04 10:57:34 -06001262
1263 for_each_possible_cpu(cpu) {
1264 sd = per_cpu(svm_data, cpu);
1265 sd->sev_vmcbs[pos] = NULL;
1266 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06001267
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001268 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001269}
1270
Brijesh Singh59414c92017-12-04 10:57:35 -06001271static void sev_unbind_asid(struct kvm *kvm, unsigned int handle)
1272{
1273 struct sev_data_decommission *decommission;
1274 struct sev_data_deactivate *data;
1275
1276 if (!handle)
1277 return;
1278
1279 data = kzalloc(sizeof(*data), GFP_KERNEL);
1280 if (!data)
1281 return;
1282
1283 /* deactivate handle */
1284 data->handle = handle;
Tom Lendacky83af5e62019-10-03 21:17:45 +00001285
Tom Lendacky33af3a72019-10-03 21:17:48 +00001286 /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */
1287 down_read(&sev_deactivate_lock);
Brijesh Singh59414c92017-12-04 10:57:35 -06001288 sev_guest_deactivate(data, NULL);
Tom Lendacky33af3a72019-10-03 21:17:48 +00001289 up_read(&sev_deactivate_lock);
Tom Lendacky83af5e62019-10-03 21:17:45 +00001290
Brijesh Singh59414c92017-12-04 10:57:35 -06001291 kfree(data);
1292
1293 decommission = kzalloc(sizeof(*decommission), GFP_KERNEL);
1294 if (!decommission)
1295 return;
1296
1297 /* decommission handle */
1298 decommission->handle = handle;
1299 sev_guest_decommission(decommission, NULL);
1300
1301 kfree(decommission);
1302}
1303
Brijesh Singh89c50582017-12-04 10:57:35 -06001304static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
1305 unsigned long ulen, unsigned long *n,
1306 int write)
1307{
Sean Christopherson81811c12018-03-20 12:17:21 -07001308 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001309 unsigned long npages, npinned, size;
1310 unsigned long locked, lock_limit;
1311 struct page **pages;
Dan Carpenter86bf20c2018-05-19 09:01:36 +03001312 unsigned long first, last;
1313
1314 if (ulen == 0 || uaddr + ulen < uaddr)
1315 return NULL;
Brijesh Singh89c50582017-12-04 10:57:35 -06001316
1317 /* Calculate number of pages. */
1318 first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
1319 last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
1320 npages = (last - first + 1);
1321
1322 locked = sev->pages_locked + npages;
1323 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
1324 if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
1325 pr_err("SEV: %lu locked pages exceed the lock limit of %lu.\n", locked, lock_limit);
1326 return NULL;
1327 }
1328
1329 /* Avoid using vmalloc for smaller buffers. */
1330 size = npages * sizeof(struct page *);
1331 if (size > PAGE_SIZE)
Ben Gardon1ec69642019-02-11 11:02:51 -08001332 pages = __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_ZERO,
1333 PAGE_KERNEL);
Brijesh Singh89c50582017-12-04 10:57:35 -06001334 else
Ben Gardon1ec69642019-02-11 11:02:51 -08001335 pages = kmalloc(size, GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06001336
1337 if (!pages)
1338 return NULL;
1339
1340 /* Pin the user virtual address. */
Ira Weiny73b01402019-05-13 17:17:11 -07001341 npinned = get_user_pages_fast(uaddr, npages, FOLL_WRITE, pages);
Brijesh Singh89c50582017-12-04 10:57:35 -06001342 if (npinned != npages) {
1343 pr_err("SEV: Failure locking %lu pages.\n", npages);
1344 goto err;
1345 }
1346
1347 *n = npages;
1348 sev->pages_locked = locked;
1349
1350 return pages;
1351
1352err:
1353 if (npinned > 0)
1354 release_pages(pages, npinned);
1355
1356 kvfree(pages);
1357 return NULL;
1358}
1359
1360static void sev_unpin_memory(struct kvm *kvm, struct page **pages,
1361 unsigned long npages)
1362{
Sean Christopherson81811c12018-03-20 12:17:21 -07001363 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06001364
1365 release_pages(pages, npages);
1366 kvfree(pages);
1367 sev->pages_locked -= npages;
1368}
1369
1370static void sev_clflush_pages(struct page *pages[], unsigned long npages)
1371{
1372 uint8_t *page_virtual;
1373 unsigned long i;
1374
1375 if (npages == 0 || pages == NULL)
1376 return;
1377
1378 for (i = 0; i < npages; i++) {
1379 page_virtual = kmap_atomic(pages[i]);
1380 clflush_cache_range(page_virtual, PAGE_SIZE);
1381 kunmap_atomic(page_virtual);
1382 }
1383}
1384
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001385static void __unregister_enc_region_locked(struct kvm *kvm,
1386 struct enc_region *region)
1387{
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001388 sev_unpin_memory(kvm, region->pages, region->npages);
1389 list_del(&region->list);
1390 kfree(region);
1391}
1392
Brijesh Singh1654efc2017-12-04 10:57:34 -06001393static void sev_vm_destroy(struct kvm *kvm)
1394{
Sean Christopherson81811c12018-03-20 12:17:21 -07001395 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001396 struct list_head *head = &sev->regions_list;
1397 struct list_head *pos, *q;
Brijesh Singh59414c92017-12-04 10:57:35 -06001398
Brijesh Singh1654efc2017-12-04 10:57:34 -06001399 if (!sev_guest(kvm))
1400 return;
1401
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001402 mutex_lock(&kvm->lock);
1403
1404 /*
Tom Lendacky2e2409a2020-03-20 11:07:07 -05001405 * Ensure that all guest tagged cache entries are flushed before
1406 * releasing the pages back to the system for use. CLFLUSH will
1407 * not do this, so issue a WBINVD.
1408 */
1409 wbinvd_on_all_cpus();
1410
1411 /*
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06001412 * if userspace was terminated before unregistering the memory regions
1413 * then lets unpin all the registered memory.
1414 */
1415 if (!list_empty(head)) {
1416 list_for_each_safe(pos, q, head) {
1417 __unregister_enc_region_locked(kvm,
1418 list_entry(pos, struct enc_region, list));
1419 }
1420 }
1421
1422 mutex_unlock(&kvm->lock);
1423
Brijesh Singh59414c92017-12-04 10:57:35 -06001424 sev_unbind_asid(kvm, sev->handle);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00001425 sev_asid_free(sev->asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06001426}
1427
Brijesh Singh1654efc2017-12-04 10:57:34 -06001428static void svm_vm_destroy(struct kvm *kvm)
1429{
1430 avic_vm_destroy(kvm);
1431 sev_vm_destroy(kvm);
1432}
1433
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06001434static int svm_vm_init(struct kvm *kvm)
1435{
1436 if (avic) {
1437 int ret = avic_vm_init(kvm);
1438 if (ret)
1439 return ret;
1440 }
1441
Paolo Bonzinie8ef2a12020-01-22 17:02:36 +01001442 kvm_apicv_init(kvm, avic);
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06001443 return 0;
1444}
1445
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001446static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Avi Kivity04d2cc72007-09-10 18:10:54 +03001447{
1448 struct vcpu_svm *svm = to_svm(vcpu);
Julian Stecklina66f7b722012-12-05 15:26:19 +01001449 u32 dummy;
1450 u32 eax = 1;
Avi Kivity04d2cc72007-09-10 18:10:54 +03001451
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001452 svm->spec_ctrl = 0;
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02001453 svm->virt_spec_ctrl = 0;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01001454
Nadav Amitd28bc9d2015-04-13 14:34:08 +03001455 if (!init_event) {
1456 svm->vcpu.arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1457 MSR_IA32_APICBASE_ENABLE;
1458 if (kvm_vcpu_is_reset_bsp(&svm->vcpu))
1459 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
1460 }
Paolo Bonzini56908912015-10-19 11:30:19 +02001461 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +02001462
Sean Christophersonf91af512020-03-04 17:34:37 -08001463 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false);
Sean Christophersonde3cd112019-04-30 10:36:17 -07001464 kvm_rdx_write(vcpu, eax);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001465
1466 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1467 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
Avi Kivity04d2cc72007-09-10 18:10:54 +03001468}
1469
Sean Christopherson987b2592019-12-18 13:54:55 -08001470static int svm_create_vcpu(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001471{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001472 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001473 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001474 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +01001475 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001476 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001477 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001478
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001479 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1480 svm = to_svm(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10001481
Joerg Roedelf65c2292008-02-13 18:58:46 +01001482 err = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08001483 page = alloc_page(GFP_KERNEL_ACCOUNT);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001484 if (!page)
Sean Christopherson987b2592019-12-18 13:54:55 -08001485 goto out;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001486
Ben Gardon1ec69642019-02-11 11:02:51 -08001487 msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +01001488 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001489 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +01001490
Ben Gardon1ec69642019-02-11 11:02:51 -08001491 nested_msrpm_pages = alloc_pages(GFP_KERNEL_ACCOUNT, MSRPM_ALLOC_ORDER);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001492 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001493 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +01001494
Ben Gardon1ec69642019-02-11 11:02:51 -08001495 hsave_page = alloc_page(GFP_KERNEL_ACCOUNT);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001496 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001497 goto free_page3;
1498
Suravee Suthikulpanitdfa20092017-09-12 10:42:40 -05001499 err = avic_init_vcpu(svm);
1500 if (err)
1501 goto free_page4;
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001502
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001503 /* We initialize this flag to true to make sure that the is_running
1504 * bit would be set the first time the vcpu is loaded.
1505 */
Suravee Suthikulpanit6c3e4422019-11-14 14:15:12 -06001506 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1507 svm->avic_is_running = true;
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001508
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001509 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +01001510
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001511 svm->msrpm = page_address(msrpm_pages);
1512 svm_vcpu_init_msrpm(svm->msrpm);
1513
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001514 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001515 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01001516
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001517 svm->vmcb = page_address(page);
1518 clear_page(svm->vmcb);
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001519 svm->vmcb_pa = __sme_set(page_to_pfn(page) << PAGE_SHIFT);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001520 svm->asid_generation = 0;
Paolo Bonzini56908912015-10-19 11:30:19 +02001521 init_vmcb(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001522
Sean Christopherson7f271792019-12-18 13:54:51 -08001523 svm_init_osvw(vcpu);
Paolo Bonzinibab0c312020-02-11 18:40:58 +01001524 vcpu->arch.microcode_version = 0x01000065;
Boris Ostrovsky2b036c62012-01-09 14:00:35 -05001525
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001526 return 0;
Avi Kivity36241b82006-12-22 01:05:20 -08001527
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05001528free_page4:
1529 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +09001530free_page3:
1531 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
1532free_page2:
1533 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
1534free_page1:
1535 __free_page(page);
Sean Christopherson987b2592019-12-18 13:54:55 -08001536out:
Sean Christophersona9dd6f02019-12-18 13:54:52 -08001537 return err;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001538}
1539
Jim Mattsonfd65d312018-05-22 09:54:20 -07001540static void svm_clear_current_vmcb(struct vmcb *vmcb)
1541{
1542 int i;
1543
1544 for_each_online_cpu(i)
1545 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1546}
1547
Avi Kivity6aa8b732006-12-10 02:21:36 -08001548static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1549{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001550 struct vcpu_svm *svm = to_svm(vcpu);
1551
Jim Mattsonfd65d312018-05-22 09:54:20 -07001552 /*
1553 * The vmcb page can be recycled, causing a false negative in
1554 * svm_vcpu_load(). So, ensure that no logical CPU has this
1555 * vmcb page recorded as its current vmcb.
1556 */
1557 svm_clear_current_vmcb(svm->vmcb);
1558
Tom Lendackyd0ec49d2017-07-17 16:10:27 -05001559 __free_page(pfn_to_page(__sme_clr(svm->vmcb_pa) >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +01001560 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001561 __free_page(virt_to_page(svm->nested.hsave));
1562 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001563}
1564
Avi Kivity15ad7142007-07-11 18:17:21 +03001565static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001566{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001567 struct vcpu_svm *svm = to_svm(vcpu);
Ashok Raj15d45072018-02-01 22:59:43 +01001568 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03001569 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001570
Avi Kivity0cc50642007-03-25 12:07:27 +02001571 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001572 svm->asid_generation = 0;
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01001573 mark_all_dirty(svm->vmcb);
Avi Kivity0cc50642007-03-25 12:07:27 +02001574 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001575
Avi Kivity82ca2d12010-10-21 12:20:34 +02001576#ifdef CONFIG_X86_64
1577 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
1578#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +02001579 savesegment(fs, svm->host.fs);
1580 savesegment(gs, svm->host.gs);
1581 svm->host.ldt = kvm_read_ldt();
1582
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001583 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001584 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001585
Haozhong Zhangad7218832015-10-20 15:39:02 +08001586 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1587 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1588 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1589 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1590 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1591 }
Joerg Roedelfbc0db72011-03-25 09:44:46 +01001592 }
Paolo Bonzini46896c72015-11-12 14:49:16 +01001593 /* This assumes that the kernel never uses MSR_TSC_AUX */
1594 if (static_cpu_has(X86_FEATURE_RDTSCP))
1595 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001596
Ashok Raj15d45072018-02-01 22:59:43 +01001597 if (sd->current_vmcb != svm->vmcb) {
1598 sd->current_vmcb = svm->vmcb;
1599 indirect_branch_prediction_barrier();
1600 }
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001601 avic_vcpu_load(vcpu, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001602}
1603
1604static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1605{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001606 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001607 int i;
1608
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05001609 avic_vcpu_put(vcpu);
1610
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001611 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001612 kvm_load_ldt(svm->host.ldt);
1613#ifdef CONFIG_X86_64
1614 loadsegment(fs, svm->host.fs);
Andy Lutomirski296f7812016-04-26 12:23:29 -07001615 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gsbase);
Joerg Roedel893a5ab2011-01-14 16:45:01 +01001616 load_gs_index(svm->host.gs);
Avi Kivitydacccfd2010-10-21 12:20:33 +02001617#else
Avi Kivity831ca602011-03-08 16:09:51 +02001618#ifdef CONFIG_X86_32_LAZY_GS
Avi Kivitydacccfd2010-10-21 12:20:33 +02001619 loadsegment(gs, svm->host.gs);
1620#endif
Avi Kivity831ca602011-03-08 16:09:51 +02001621#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001622 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001623 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001624}
1625
Avi Kivity6aa8b732006-12-10 02:21:36 -08001626static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1627{
Ladi Prosek9b611742017-06-21 09:06:59 +02001628 struct vcpu_svm *svm = to_svm(vcpu);
1629 unsigned long rflags = svm->vmcb->save.rflags;
1630
1631 if (svm->nmi_singlestep) {
1632 /* Hide our flags if they were not set by the guest */
1633 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1634 rflags &= ~X86_EFLAGS_TF;
1635 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1636 rflags &= ~X86_EFLAGS_RF;
1637 }
1638 return rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001639}
1640
1641static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1642{
Ladi Prosek9b611742017-06-21 09:06:59 +02001643 if (to_svm(vcpu)->nmi_singlestep)
1644 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1645
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001646 /*
Andrea Gelminibb3541f2016-05-21 14:14:44 +02001647 * Any change of EFLAGS.VM is accompanied by a reload of SS
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001648 * (caused by either a task switch or an inter-privilege IRET),
1649 * so we do not need to update the CPL here.
1650 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001651 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001652}
1653
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001654static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1655{
1656 switch (reg) {
1657 case VCPU_EXREG_PDPTR:
1658 BUG_ON(!npt_enabled);
Avi Kivity9f8fe502010-12-05 17:30:00 +02001659 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001660 break;
1661 default:
Sean Christopherson34059c22019-09-27 14:45:23 -07001662 WARN_ON_ONCE(1);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001663 }
1664}
1665
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001666static inline void svm_enable_vintr(struct vcpu_svm *svm)
1667{
1668 struct vmcb_control_area *control;
1669
1670 /* The following fields are ignored when AVIC is enabled */
1671 WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu));
1672
1673 /*
1674 * This is just a dummy VINTR to actually cause a vmexit to happen.
1675 * Actual injection of virtual interrupts happens through EVENTINJ.
1676 */
1677 control = &svm->vmcb->control;
1678 control->int_vector = 0x0;
1679 control->int_ctl &= ~V_INTR_PRIO_MASK;
1680 control->int_ctl |= V_IRQ_MASK |
1681 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1682 mark_dirty(svm->vmcb, VMCB_INTR);
1683}
1684
Alexander Graff0b85052008-11-25 20:17:01 +01001685static void svm_set_vintr(struct vcpu_svm *svm)
1686{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001687 set_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001688 if (is_intercept(svm, INTERCEPT_VINTR))
1689 svm_enable_vintr(svm);
Alexander Graff0b85052008-11-25 20:17:01 +01001690}
1691
1692static void svm_clear_vintr(struct vcpu_svm *svm)
1693{
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01001694 clr_intercept(svm, INTERCEPT_VINTR);
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05001695
1696 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
1697 mark_dirty(svm->vmcb, VMCB_INTR);
Alexander Graff0b85052008-11-25 20:17:01 +01001698}
1699
Avi Kivity6aa8b732006-12-10 02:21:36 -08001700static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1701{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001702 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001703
1704 switch (seg) {
1705 case VCPU_SREG_CS: return &save->cs;
1706 case VCPU_SREG_DS: return &save->ds;
1707 case VCPU_SREG_ES: return &save->es;
1708 case VCPU_SREG_FS: return &save->fs;
1709 case VCPU_SREG_GS: return &save->gs;
1710 case VCPU_SREG_SS: return &save->ss;
1711 case VCPU_SREG_TR: return &save->tr;
1712 case VCPU_SREG_LDTR: return &save->ldtr;
1713 }
1714 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001715 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001716}
1717
1718static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1719{
1720 struct vmcb_seg *s = svm_seg(vcpu, seg);
1721
1722 return s->base;
1723}
1724
1725static void svm_get_segment(struct kvm_vcpu *vcpu,
1726 struct kvm_segment *var, int seg)
1727{
1728 struct vmcb_seg *s = svm_seg(vcpu, seg);
1729
1730 var->base = s->base;
1731 var->limit = s->limit;
1732 var->selector = s->selector;
1733 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1734 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1735 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1736 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1737 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1738 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1739 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
Jim Mattson80112c82014-07-08 09:47:41 +05301740
1741 /*
1742 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1743 * However, the SVM spec states that the G bit is not observed by the
1744 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1745 * So let's synthesize a legal G bit for all segments, this helps
1746 * running KVM nested. It also helps cross-vendor migration, because
1747 * Intel's vmentry has a check on the 'G' bit.
1748 */
1749 var->g = s->limit > 0xfffff;
Amit Shah25022ac2008-10-27 09:04:17 +00001750
Joerg Roedele0231712010-02-24 18:59:10 +01001751 /*
1752 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001753 * for cross vendor migration purposes by "not present"
1754 */
Gioh Kim8eae9572017-05-30 15:24:45 +02001755 var->unusable = !var->present;
Andre Przywara19bca6a2009-04-28 12:45:30 +02001756
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001757 switch (seg) {
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001758 case VCPU_SREG_TR:
1759 /*
1760 * Work around a bug where the busy flag in the tr selector
1761 * isn't exposed
1762 */
Amit Shahc0d09822008-10-27 09:04:18 +00001763 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001764 break;
1765 case VCPU_SREG_DS:
1766 case VCPU_SREG_ES:
1767 case VCPU_SREG_FS:
1768 case VCPU_SREG_GS:
1769 /*
1770 * The accessed bit must always be set in the segment
1771 * descriptor cache, although it can be cleared in the
1772 * descriptor, the cached bit always remains at 1. Since
1773 * Intel has a check on this, set it here to support
1774 * cross-vendor migration.
1775 */
1776 if (!var->unusable)
1777 var->type |= 0x1;
1778 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001779 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001780 /*
1781 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001782 * descriptor is left as 1, although the whole segment has
1783 * been made unusable. Clear it here to pass an Intel VMX
1784 * entry check when cross vendor migrating.
1785 */
1786 if (var->unusable)
1787 var->db = 0;
Roman Pend9c1b542017-06-01 10:55:03 +02001788 /* This is symmetric with svm_set_segment() */
Jan Kiszka33b458d2014-06-29 17:12:43 +02001789 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
Andre Przywarab586eb02009-04-28 12:45:43 +02001790 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001791 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001792}
1793
Izik Eidus2e4d2652008-03-24 19:38:34 +02001794static int svm_get_cpl(struct kvm_vcpu *vcpu)
1795{
1796 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1797
1798 return save->cpl;
1799}
1800
Gleb Natapov89a27f42010-02-16 10:51:48 +02001801static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001802{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001803 struct vcpu_svm *svm = to_svm(vcpu);
1804
Gleb Natapov89a27f42010-02-16 10:51:48 +02001805 dt->size = svm->vmcb->save.idtr.limit;
1806 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001807}
1808
Gleb Natapov89a27f42010-02-16 10:51:48 +02001809static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001810{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001811 struct vcpu_svm *svm = to_svm(vcpu);
1812
Gleb Natapov89a27f42010-02-16 10:51:48 +02001813 svm->vmcb->save.idtr.limit = dt->size;
1814 svm->vmcb->save.idtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001815 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001816}
1817
Gleb Natapov89a27f42010-02-16 10:51:48 +02001818static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001819{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001820 struct vcpu_svm *svm = to_svm(vcpu);
1821
Gleb Natapov89a27f42010-02-16 10:51:48 +02001822 dt->size = svm->vmcb->save.gdtr.limit;
1823 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001824}
1825
Gleb Natapov89a27f42010-02-16 10:51:48 +02001826static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001827{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001828 struct vcpu_svm *svm = to_svm(vcpu);
1829
Gleb Natapov89a27f42010-02-16 10:51:48 +02001830 svm->vmcb->save.gdtr.limit = dt->size;
1831 svm->vmcb->save.gdtr.base = dt->address ;
Joerg Roedel17a703c2010-12-03 11:45:56 +01001832 mark_dirty(svm->vmcb, VMCB_DT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001833}
1834
Avi Kivitye8467fd2009-12-29 18:43:06 +02001835static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1836{
1837}
1838
Anthony Liguori25c4c272007-04-27 09:29:21 +03001839static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001840{
1841}
1842
Avi Kivityd2251572010-01-06 10:55:27 +02001843static void update_cr0_intercept(struct vcpu_svm *svm)
1844{
1845 ulong gcr0 = svm->vcpu.arch.cr0;
1846 u64 *hcr0 = &svm->vmcb->save.cr0;
1847
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001848 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1849 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
Avi Kivityd2251572010-01-06 10:55:27 +02001850
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001851 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001852
Paolo Bonzinibd7e5b02017-02-03 21:18:52 -08001853 if (gcr0 == *hcr0) {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001854 clr_cr_intercept(svm, INTERCEPT_CR0_READ);
1855 clr_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001856 } else {
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01001857 set_cr_intercept(svm, INTERCEPT_CR0_READ);
1858 set_cr_intercept(svm, INTERCEPT_CR0_WRITE);
Avi Kivityd2251572010-01-06 10:55:27 +02001859 }
1860}
1861
Joerg Roedel883b0a92020-03-24 10:41:52 +01001862void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001863{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001864 struct vcpu_svm *svm = to_svm(vcpu);
1865
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001866#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001867 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001868 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001869 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001870 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001871 }
1872
Mike Dayd77c26f2007-10-08 09:02:08 -04001873 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001874 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001875 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001876 }
1877 }
1878#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001879 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001880
1881 if (!npt_enabled)
1882 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001883
Paolo Bonzinibcf166a2015-10-01 13:19:55 +02001884 /*
1885 * re-enable caching here because the QEMU bios
1886 * does not do it - this results in some delay at
1887 * reboot
1888 */
1889 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1890 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001891 svm->vmcb->save.cr0 = cr0;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001892 mark_dirty(svm->vmcb, VMCB_CR);
Avi Kivityd2251572010-01-06 10:55:27 +02001893 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001894}
1895
Joerg Roedel883b0a92020-03-24 10:41:52 +01001896int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001897{
Andy Lutomirski1e02ce42014-10-24 15:58:08 -07001898 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001899 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1900
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001901 if (cr4 & X86_CR4_VMXE)
1902 return 1;
1903
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001904 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
Wanpeng Lic2ba05c2017-12-12 17:33:03 -08001905 svm_flush_tlb(vcpu, true);
Joerg Roedel6394b642008-04-09 14:15:29 +02001906
Joerg Roedelec077262008-04-09 14:15:28 +02001907 vcpu->arch.cr4 = cr4;
1908 if (!npt_enabled)
1909 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001910 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001911 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Joerg Roedeldcca1a62010-12-03 11:45:54 +01001912 mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
Nadav Har'El5e1746d2011-05-25 23:03:24 +03001913 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001914}
1915
1916static void svm_set_segment(struct kvm_vcpu *vcpu,
1917 struct kvm_segment *var, int seg)
1918{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001919 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001920 struct vmcb_seg *s = svm_seg(vcpu, seg);
1921
1922 s->base = var->base;
1923 s->limit = var->limit;
1924 s->selector = var->selector;
Roman Pend9c1b542017-06-01 10:55:03 +02001925 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1926 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1927 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1928 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1929 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1930 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1931 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1932 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
Paolo Bonziniae9fedc2014-05-14 09:39:49 +02001933
1934 /*
1935 * This is always accurate, except if SYSRET returned to a segment
1936 * with SS.DPL != 3. Intel does not have this quirk, and always
1937 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1938 * would entail passing the CPL to userspace and back.
1939 */
1940 if (seg == VCPU_SREG_SS)
Roman Pend9c1b542017-06-01 10:55:03 +02001941 /* This is symmetric with svm_get_segment() */
1942 svm->vmcb->save.cpl = (var->dpl & 3);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001943
Joerg Roedel060d0c92010-12-03 11:45:57 +01001944 mark_dirty(svm->vmcb, VMCB_SEG);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001945}
1946
Paolo Bonzinicbdb9672015-11-10 09:14:39 +01001947static void update_bp_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001948{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001949 struct vcpu_svm *svm = to_svm(vcpu);
1950
Joerg Roedel18c918c2010-11-30 18:03:59 +01001951 clr_exception_intercept(svm, BP_VECTOR);
Gleb Natapov44c11432009-05-11 13:35:52 +03001952
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001953 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001954 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
Joerg Roedel18c918c2010-11-30 18:03:59 +01001955 set_exception_intercept(svm, BP_VECTOR);
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001956 } else
1957 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001958}
1959
Tejun Heo0fe1e002009-10-29 22:34:14 +09001960static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001961{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001962 if (sd->next_asid > sd->max_asid) {
1963 ++sd->asid_generation;
Brijesh Singh4faefff2017-12-04 10:57:25 -06001964 sd->next_asid = sd->min_asid;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001965 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001966 }
1967
Tejun Heo0fe1e002009-10-29 22:34:14 +09001968 svm->asid_generation = sd->asid_generation;
1969 svm->vmcb->control.asid = sd->next_asid++;
Joerg Roedeld48086d2010-12-03 11:45:51 +01001970
1971 mark_dirty(svm->vmcb, VMCB_ASID);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001972}
1973
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01001974static u64 svm_get_dr6(struct kvm_vcpu *vcpu)
1975{
1976 return to_svm(vcpu)->vmcb->save.dr6;
1977}
1978
1979static void svm_set_dr6(struct kvm_vcpu *vcpu, unsigned long value)
1980{
1981 struct vcpu_svm *svm = to_svm(vcpu);
1982
1983 svm->vmcb->save.dr6 = value;
1984 mark_dirty(svm->vmcb, VMCB_DR);
1985}
1986
Paolo Bonzinifacb0132014-02-21 10:32:27 +01001987static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1988{
1989 struct vcpu_svm *svm = to_svm(vcpu);
1990
1991 get_debugreg(vcpu->arch.db[0], 0);
1992 get_debugreg(vcpu->arch.db[1], 1);
1993 get_debugreg(vcpu->arch.db[2], 2);
1994 get_debugreg(vcpu->arch.db[3], 3);
1995 vcpu->arch.dr6 = svm_get_dr6(vcpu);
1996 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1997
1998 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1999 set_dr_intercepts(svm);
2000}
2001
Gleb Natapov020df072010-04-13 10:05:23 +03002002static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002003{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002004 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01002005
Gleb Natapov020df072010-04-13 10:05:23 +03002006 svm->vmcb->save.dr7 = value;
Joerg Roedel72214b92010-12-03 11:45:55 +01002007 mark_dirty(svm->vmcb, VMCB_DR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002008}
2009
Avi Kivity851ba692009-08-24 11:10:17 +03002010static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002011{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002012 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002013 u64 error_code = svm->vmcb->control.exit_info_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002014
Wanpeng Li1261bfa2017-07-13 18:30:40 -07002015 return kvm_handle_page_fault(&svm->vcpu, error_code, fault_address,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002016 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2017 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002018 svm->vmcb->control.insn_len);
2019}
2020
2021static int npf_interception(struct vcpu_svm *svm)
2022{
Brijesh Singh0ede79e2017-12-04 10:57:39 -06002023 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
Paolo Bonzinid0006532017-08-11 18:36:43 +02002024 u64 error_code = svm->vmcb->control.exit_info_1;
2025
2026 trace_kvm_page_fault(fault_address, error_code);
2027 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code,
Brijesh Singh00b10fe2017-12-04 10:57:40 -06002028 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
2029 svm->vmcb->control.insn_bytes : NULL,
Paolo Bonzinid0006532017-08-11 18:36:43 +02002030 svm->vmcb->control.insn_len);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002031}
2032
Avi Kivity851ba692009-08-24 11:10:17 +03002033static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002034{
Avi Kivity851ba692009-08-24 11:10:17 +03002035 struct kvm_run *kvm_run = svm->vcpu.run;
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002036 struct kvm_vcpu *vcpu = &svm->vcpu;
Avi Kivity851ba692009-08-24 11:10:17 +03002037
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002038 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03002039 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02002040 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002041 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
2042 return 1;
2043 }
Gleb Natapov44c11432009-05-11 13:35:52 +03002044
Jan Kiszka6be7d302009-10-18 13:24:54 +02002045 if (svm->nmi_singlestep) {
Ladi Prosek4aebd0e2017-06-21 09:06:57 +02002046 disable_nmi_singlestep(svm);
Vitaly Kuznetsov99c22172019-04-03 16:06:42 +02002047 /* Make sure we check for pending NMIs upon entry */
2048 kvm_make_request(KVM_REQ_EVENT, vcpu);
Gleb Natapov44c11432009-05-11 13:35:52 +03002049 }
2050
2051 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01002052 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03002053 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2054 kvm_run->debug.arch.pc =
2055 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2056 kvm_run->debug.arch.exception = DB_VECTOR;
2057 return 0;
2058 }
2059
2060 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002061}
2062
Avi Kivity851ba692009-08-24 11:10:17 +03002063static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002064{
Avi Kivity851ba692009-08-24 11:10:17 +03002065 struct kvm_run *kvm_run = svm->vcpu.run;
2066
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002067 kvm_run->exit_reason = KVM_EXIT_DEBUG;
2068 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
2069 kvm_run->debug.arch.exception = BP_VECTOR;
2070 return 0;
2071}
2072
Avi Kivity851ba692009-08-24 11:10:17 +03002073static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002074{
Wanpeng Li082d06e2018-04-03 16:28:48 -07002075 return handle_ud(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002076}
2077
Eric Northup54a20552015-11-03 18:03:53 +01002078static int ac_interception(struct vcpu_svm *svm)
2079{
2080 kvm_queue_exception_e(&svm->vcpu, AC_VECTOR, 0);
2081 return 1;
2082}
2083
Liran Alon97184202018-03-12 13:12:52 +02002084static int gp_interception(struct vcpu_svm *svm)
2085{
2086 struct kvm_vcpu *vcpu = &svm->vcpu;
2087 u32 error_code = svm->vmcb->control.exit_info_1;
Liran Alon97184202018-03-12 13:12:52 +02002088
2089 WARN_ON_ONCE(!enable_vmware_backdoor);
2090
Sean Christophersona6c6ed12019-08-27 14:40:30 -07002091 /*
2092 * VMware backdoor emulation on #GP interception only handles IN{S},
2093 * OUT{S}, and RDPMC, none of which generate a non-zero error code.
2094 */
2095 if (error_code) {
2096 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2097 return 1;
2098 }
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002099 return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
Liran Alon97184202018-03-12 13:12:52 +02002100}
2101
Joerg Roedel67ec6602010-05-17 14:43:35 +02002102static bool is_erratum_383(void)
2103{
2104 int err, i;
2105 u64 value;
2106
2107 if (!erratum_383_found)
2108 return false;
2109
2110 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
2111 if (err)
2112 return false;
2113
2114 /* Bit 62 may or may not be set for this mce */
2115 value &= ~(1ULL << 62);
2116
2117 if (value != 0xb600000000010015ULL)
2118 return false;
2119
2120 /* Clear MCi_STATUS registers */
2121 for (i = 0; i < 6; ++i)
2122 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
2123
2124 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
2125 if (!err) {
2126 u32 low, high;
2127
2128 value &= ~(1ULL << 2);
2129 low = lower_32_bits(value);
2130 high = upper_32_bits(value);
2131
2132 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
2133 }
2134
2135 /* Flush tlb to evict multi-match entries */
2136 __flush_tlb_all();
2137
2138 return true;
2139}
2140
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002141static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02002142{
Joerg Roedel67ec6602010-05-17 14:43:35 +02002143 if (is_erratum_383()) {
2144 /*
2145 * Erratum 383 triggered. Guest state is corrupt so kill the
2146 * guest.
2147 */
2148 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2149
Avi Kivitya8eeb042010-05-10 12:34:53 +03002150 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02002151
2152 return;
2153 }
2154
Joerg Roedel53371b52008-04-09 14:15:30 +02002155 /*
2156 * On an #MC intercept the MCE handler is not called automatically in
2157 * the host. So do it by hand here.
2158 */
2159 asm volatile (
2160 "int $0x12\n");
2161 /* not sure if we ever come back to this point */
2162
Joerg Roedelfe5913e2010-05-17 14:43:34 +02002163 return;
2164}
2165
2166static int mc_interception(struct vcpu_svm *svm)
2167{
Joerg Roedel53371b52008-04-09 14:15:30 +02002168 return 1;
2169}
2170
Avi Kivity851ba692009-08-24 11:10:17 +03002171static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002172{
Avi Kivity851ba692009-08-24 11:10:17 +03002173 struct kvm_run *kvm_run = svm->vcpu.run;
2174
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002175 /*
2176 * VMCB is undefined after a SHUTDOWN intercept
2177 * so reinitialize it.
2178 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002179 clear_page(svm->vmcb);
Paolo Bonzini56908912015-10-19 11:30:19 +02002180 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002181
2182 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2183 return 0;
2184}
2185
Avi Kivity851ba692009-08-24 11:10:17 +03002186static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002187{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002188 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04002189 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Sean Christophersondca7f122018-03-08 08:57:27 -08002190 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02002191 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002192
Rusty Russelle756fc62007-07-30 20:07:08 +10002193 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03002194 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02002195 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Tom Lendacky8370c3d2016-11-23 12:01:50 -05002196 if (string)
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002197 return kvm_emulate_instruction(vcpu, 0);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002198
Avi Kivity039576c2007-03-20 12:46:50 +02002199 port = io_info >> 16;
2200 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002201 svm->next_rip = svm->vmcb->control.exit_info_2;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02002202
Sean Christophersondca7f122018-03-08 08:57:27 -08002203 return kvm_fast_pio(&svm->vcpu, size, port, in);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002204}
2205
Avi Kivity851ba692009-08-24 11:10:17 +03002206static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02002207{
2208 return 1;
2209}
2210
Avi Kivity851ba692009-08-24 11:10:17 +03002211static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02002212{
2213 ++svm->vcpu.stat.irq_exits;
2214 return 1;
2215}
2216
Avi Kivity851ba692009-08-24 11:10:17 +03002217static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002218{
2219 return 1;
2220}
2221
Avi Kivity851ba692009-08-24 11:10:17 +03002222static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002223{
Rusty Russelle756fc62007-07-30 20:07:08 +10002224 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002225}
2226
Avi Kivity851ba692009-08-24 11:10:17 +03002227static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02002228{
Andrey Smetanin0d9c0552016-02-11 16:44:59 +03002229 return kvm_emulate_hypercall(&svm->vcpu);
Avi Kivity02e235b2007-02-19 14:37:47 +02002230}
2231
Avi Kivity851ba692009-08-24 11:10:17 +03002232static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002233{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002234 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002235 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002236 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002237
Alexander Graf55426752008-11-25 20:17:06 +01002238 if (nested_svm_check_permissions(svm))
2239 return 1;
2240
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002241 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2242 if (ret) {
2243 if (ret == -EINVAL)
2244 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002245 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002246 }
2247
2248 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002249
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002250 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002251
Joerg Roedel9966bf62009-08-07 11:49:40 +02002252 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002253 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002254
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002255 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002256}
2257
Avi Kivity851ba692009-08-24 11:10:17 +03002258static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002259{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002260 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002261 struct kvm_host_map map;
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002262 int ret;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002263
Alexander Graf55426752008-11-25 20:17:06 +01002264 if (nested_svm_check_permissions(svm))
2265 return 1;
2266
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002267 ret = kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2268 if (ret) {
2269 if (ret == -EINVAL)
2270 kvm_inject_gp(&svm->vcpu, 0);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002271 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002272 }
2273
2274 nested_vmcb = map.hva;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002275
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002276 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedele3e9ed32011-04-06 12:30:03 +02002277
Joerg Roedel9966bf62009-08-07 11:49:40 +02002278 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01002279 kvm_vcpu_unmap(&svm->vcpu, &map, true);
Alexander Graf55426752008-11-25 20:17:06 +01002280
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002281 return ret;
Alexander Graf55426752008-11-25 20:17:06 +01002282}
2283
Avi Kivity851ba692009-08-24 11:10:17 +03002284static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002285{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002286 if (nested_svm_check_permissions(svm))
2287 return 1;
2288
Vitaly Kuznetsove7134c12019-08-13 15:53:34 +02002289 return nested_svm_vmrun(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002290}
2291
Avi Kivity851ba692009-08-24 11:10:17 +03002292static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002293{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002294 int ret;
2295
Alexander Graf1371d902008-11-25 20:17:04 +01002296 if (nested_svm_check_permissions(svm))
2297 return 1;
2298
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05002299 /*
2300 * If VGIF is enabled, the STGI intercept is only added to
Ladi Prosekcc3d9672017-10-17 16:02:39 +02002301 * detect the opening of the SMI/NMI window; remove it now.
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05002302 */
2303 if (vgif_enabled(svm))
2304 clr_intercept(svm, INTERCEPT_STGI);
2305
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002306 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002307 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002308
Joerg Roedel2af91942009-08-07 11:49:28 +02002309 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002310
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002311 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002312}
2313
Avi Kivity851ba692009-08-24 11:10:17 +03002314static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002315{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002316 int ret;
2317
Alexander Graf1371d902008-11-25 20:17:04 +01002318 if (nested_svm_check_permissions(svm))
2319 return 1;
2320
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002321 ret = kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002322
Joerg Roedel2af91942009-08-07 11:49:28 +02002323 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002324
2325 /* After a CLGI no interrupts should come */
Paolo Bonzini64b5bd22020-03-04 13:12:35 -05002326 if (!kvm_vcpu_apicv_active(&svm->vcpu))
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05002327 svm_clear_vintr(svm);
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002328
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002329 return ret;
Alexander Graf1371d902008-11-25 20:17:04 +01002330}
2331
Avi Kivity851ba692009-08-24 11:10:17 +03002332static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002333{
2334 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002335
Sean Christophersonde3cd112019-04-30 10:36:17 -07002336 trace_kvm_invlpga(svm->vmcb->save.rip, kvm_rcx_read(&svm->vcpu),
2337 kvm_rax_read(&svm->vcpu));
Joerg Roedelec1ff792009-10-09 16:08:31 +02002338
Alexander Grafff092382009-06-15 15:21:24 +02002339 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
Sean Christophersonde3cd112019-04-30 10:36:17 -07002340 kvm_mmu_invlpg(vcpu, kvm_rax_read(&svm->vcpu));
Alexander Grafff092382009-06-15 15:21:24 +02002341
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002342 return kvm_skip_emulated_instruction(&svm->vcpu);
Alexander Grafff092382009-06-15 15:21:24 +02002343}
2344
Joerg Roedel532a46b2009-10-09 16:08:32 +02002345static int skinit_interception(struct vcpu_svm *svm)
2346{
Sean Christophersonde3cd112019-04-30 10:36:17 -07002347 trace_kvm_skinit(svm->vmcb->save.rip, kvm_rax_read(&svm->vcpu));
Joerg Roedel532a46b2009-10-09 16:08:32 +02002348
2349 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2350 return 1;
2351}
2352
David Kaplandab429a2015-03-02 13:43:37 -06002353static int wbinvd_interception(struct vcpu_svm *svm)
2354{
Kyle Huey6affcbe2016-11-29 12:40:40 -08002355 return kvm_emulate_wbinvd(&svm->vcpu);
David Kaplandab429a2015-03-02 13:43:37 -06002356}
2357
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002358static int xsetbv_interception(struct vcpu_svm *svm)
2359{
2360 u64 new_bv = kvm_read_edx_eax(&svm->vcpu);
Sean Christophersonde3cd112019-04-30 10:36:17 -07002361 u32 index = kvm_rcx_read(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002362
2363 if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) {
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002364 return kvm_skip_emulated_instruction(&svm->vcpu);
Joerg Roedel81dd35d2010-12-07 17:15:06 +01002365 }
2366
2367 return 1;
2368}
2369
Jim Mattson0cb84102019-09-19 15:59:17 -07002370static int rdpru_interception(struct vcpu_svm *svm)
2371{
2372 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2373 return 1;
2374}
2375
Avi Kivity851ba692009-08-24 11:10:17 +03002376static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377{
Izik Eidus37817f22008-03-24 23:14:53 +02002378 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002379 int reason;
2380 int int_type = svm->vmcb->control.exit_int_info &
2381 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002382 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002383 uint32_t type =
2384 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2385 uint32_t idt_v =
2386 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002387 bool has_error_code = false;
2388 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002389
2390 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002391
Izik Eidus37817f22008-03-24 23:14:53 +02002392 if (svm->vmcb->control.exit_info_2 &
2393 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002394 reason = TASK_SWITCH_IRET;
2395 else if (svm->vmcb->control.exit_info_2 &
2396 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2397 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002398 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002399 reason = TASK_SWITCH_GATE;
2400 else
2401 reason = TASK_SWITCH_CALL;
2402
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002403 if (reason == TASK_SWITCH_GATE) {
2404 switch (type) {
2405 case SVM_EXITINTINFO_TYPE_NMI:
2406 svm->vcpu.arch.nmi_injected = false;
2407 break;
2408 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002409 if (svm->vmcb->control.exit_info_2 &
2410 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2411 has_error_code = true;
2412 error_code =
2413 (u32)svm->vmcb->control.exit_info_2;
2414 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002415 kvm_clear_exception_queue(&svm->vcpu);
2416 break;
2417 case SVM_EXITINTINFO_TYPE_INTR:
2418 kvm_clear_interrupt_queue(&svm->vcpu);
2419 break;
2420 default:
2421 break;
2422 }
2423 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002424
Gleb Natapov8317c292009-04-12 13:37:02 +03002425 if (reason != TASK_SWITCH_GATE ||
2426 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2427 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002428 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002429 if (!skip_emulated_instruction(&svm->vcpu))
Sean Christopherson738fece2019-08-27 14:40:34 -07002430 return 0;
Vitaly Kuznetsovf8ea7c62019-08-13 15:53:30 +02002431 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002432
Kevin Wolf7f3d35f2012-02-08 14:34:38 +01002433 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2434 int_vec = -1;
2435
Sean Christopherson10517782019-08-27 14:40:35 -07002436 return kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason,
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002437 has_error_code, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002438}
2439
Avi Kivity851ba692009-08-24 11:10:17 +03002440static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002441{
Kyle Huey6a908b62016-11-29 12:40:37 -08002442 return kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002443}
2444
Avi Kivity851ba692009-08-24 11:10:17 +03002445static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002446{
2447 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01002448 clr_intercept(svm, INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002449 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02002450 svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu);
Radim Krčmářf303b4c2014-01-17 20:52:42 +01002451 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002452 return 1;
2453}
2454
Avi Kivity851ba692009-08-24 11:10:17 +03002455static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002456{
Andre Przywaradf4f31082010-12-21 11:12:06 +01002457 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002458 return kvm_emulate_instruction(&svm->vcpu, 0);
Andre Przywaradf4f31082010-12-21 11:12:06 +01002459
2460 kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1);
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002461 return kvm_skip_emulated_instruction(&svm->vcpu);
Marcelo Tosattia7052892008-09-23 13:18:35 -03002462}
2463
Avi Kivity851ba692009-08-24 11:10:17 +03002464static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002465{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002466 return kvm_emulate_instruction(&svm->vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002467}
2468
Brijesh Singh7607b712018-02-19 10:14:44 -06002469static int rsm_interception(struct vcpu_svm *svm)
2470{
Sean Christopherson60fc3d02019-08-27 14:40:38 -07002471 return kvm_emulate_instruction_from_buffer(&svm->vcpu, rsm_ins_bytes, 2);
Brijesh Singh7607b712018-02-19 10:14:44 -06002472}
2473
Avi Kivity332b56e2011-11-10 14:57:24 +02002474static int rdpmc_interception(struct vcpu_svm *svm)
2475{
2476 int err;
2477
Paolo Bonzinid647eb62019-06-20 14:13:33 +02002478 if (!nrips)
Avi Kivity332b56e2011-11-10 14:57:24 +02002479 return emulate_on_interception(svm);
2480
2481 err = kvm_rdpmc(&svm->vcpu);
Kyle Huey6affcbe2016-11-29 12:40:40 -08002482 return kvm_complete_insn_gp(&svm->vcpu, err);
Avi Kivity332b56e2011-11-10 14:57:24 +02002483}
2484
Xiubo Li52eb5a62015-03-13 17:39:45 +08002485static bool check_selective_cr0_intercepted(struct vcpu_svm *svm,
2486 unsigned long val)
Joerg Roedel628afd22011-04-04 12:39:36 +02002487{
2488 unsigned long cr0 = svm->vcpu.arch.cr0;
2489 bool ret = false;
2490 u64 intercept;
2491
2492 intercept = svm->nested.intercept;
2493
2494 if (!is_guest_mode(&svm->vcpu) ||
2495 (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))))
2496 return false;
2497
2498 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2499 val &= ~SVM_CR0_SELECTIVE_MASK;
2500
2501 if (cr0 ^ val) {
2502 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2503 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2504 }
2505
2506 return ret;
2507}
2508
Andre Przywara7ff76d52010-12-21 11:12:04 +01002509#define CR_VALID (1ULL << 63)
2510
2511static int cr_interception(struct vcpu_svm *svm)
2512{
2513 int reg, cr;
2514 unsigned long val;
2515 int err;
2516
2517 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2518 return emulate_on_interception(svm);
2519
2520 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2521 return emulate_on_interception(svm);
2522
2523 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
David Kaplan5e575182015-03-06 14:44:35 -06002524 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2525 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2526 else
2527 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
Andre Przywara7ff76d52010-12-21 11:12:04 +01002528
2529 err = 0;
2530 if (cr >= 16) { /* mov to cr */
2531 cr -= 16;
2532 val = kvm_register_read(&svm->vcpu, reg);
2533 switch (cr) {
2534 case 0:
Joerg Roedel628afd22011-04-04 12:39:36 +02002535 if (!check_selective_cr0_intercepted(svm, val))
2536 err = kvm_set_cr0(&svm->vcpu, val);
Joerg Roedel977b2d02011-04-18 11:42:52 +02002537 else
2538 return 1;
2539
Andre Przywara7ff76d52010-12-21 11:12:04 +01002540 break;
2541 case 3:
2542 err = kvm_set_cr3(&svm->vcpu, val);
2543 break;
2544 case 4:
2545 err = kvm_set_cr4(&svm->vcpu, val);
2546 break;
2547 case 8:
2548 err = kvm_set_cr8(&svm->vcpu, val);
2549 break;
2550 default:
2551 WARN(1, "unhandled write to CR%d", cr);
2552 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2553 return 1;
2554 }
2555 } else { /* mov from cr */
2556 switch (cr) {
2557 case 0:
2558 val = kvm_read_cr0(&svm->vcpu);
2559 break;
2560 case 2:
2561 val = svm->vcpu.arch.cr2;
2562 break;
2563 case 3:
Avi Kivity9f8fe502010-12-05 17:30:00 +02002564 val = kvm_read_cr3(&svm->vcpu);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002565 break;
2566 case 4:
2567 val = kvm_read_cr4(&svm->vcpu);
2568 break;
2569 case 8:
2570 val = kvm_get_cr8(&svm->vcpu);
2571 break;
2572 default:
2573 WARN(1, "unhandled read from CR%d", cr);
2574 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2575 return 1;
2576 }
2577 kvm_register_write(&svm->vcpu, reg, val);
2578 }
Kyle Huey6affcbe2016-11-29 12:40:40 -08002579 return kvm_complete_insn_gp(&svm->vcpu, err);
Andre Przywara7ff76d52010-12-21 11:12:04 +01002580}
2581
Andre Przywaracae37972010-12-21 11:12:05 +01002582static int dr_interception(struct vcpu_svm *svm)
2583{
2584 int reg, dr;
2585 unsigned long val;
Andre Przywaracae37972010-12-21 11:12:05 +01002586
Paolo Bonzinifacb0132014-02-21 10:32:27 +01002587 if (svm->vcpu.guest_debug == 0) {
2588 /*
2589 * No more DR vmexits; force a reload of the debug registers
2590 * and reenter on this instruction. The next vmexit will
2591 * retrieve the full state of the debug registers.
2592 */
2593 clr_dr_intercepts(svm);
2594 svm->vcpu.arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
2595 return 1;
2596 }
2597
Andre Przywaracae37972010-12-21 11:12:05 +01002598 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2599 return emulate_on_interception(svm);
2600
2601 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2602 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2603
2604 if (dr >= 16) { /* mov to DRn */
Nadav Amit16f8a6f2014-10-03 01:10:05 +03002605 if (!kvm_require_dr(&svm->vcpu, dr - 16))
2606 return 1;
Andre Przywaracae37972010-12-21 11:12:05 +01002607 val = kvm_register_read(&svm->vcpu, reg);
2608 kvm_set_dr(&svm->vcpu, dr - 16, val);
2609 } else {
Nadav Amit16f8a6f2014-10-03 01:10:05 +03002610 if (!kvm_require_dr(&svm->vcpu, dr))
2611 return 1;
2612 kvm_get_dr(&svm->vcpu, dr, &val);
2613 kvm_register_write(&svm->vcpu, reg, val);
Andre Przywaracae37972010-12-21 11:12:05 +01002614 }
2615
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002616 return kvm_skip_emulated_instruction(&svm->vcpu);
Andre Przywaracae37972010-12-21 11:12:05 +01002617}
2618
Avi Kivity851ba692009-08-24 11:10:17 +03002619static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002620{
Avi Kivity851ba692009-08-24 11:10:17 +03002621 struct kvm_run *kvm_run = svm->vcpu.run;
Andre Przywaraeea1cff2010-12-21 11:12:00 +01002622 int r;
Avi Kivity851ba692009-08-24 11:10:17 +03002623
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002624 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2625 /* instruction emulation calls kvm_set_cr8() */
Andre Przywara7ff76d52010-12-21 11:12:04 +01002626 r = cr_interception(svm);
Paolo Bonzini35754c92015-07-29 12:05:37 +02002627 if (lapic_in_kernel(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002628 return r;
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002629 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
Andre Przywara7ff76d52010-12-21 11:12:04 +01002630 return r;
Joerg Roedel1d075432007-12-06 21:02:25 +01002631 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2632 return 0;
2633}
2634
Tom Lendacky801e4592018-02-21 13:39:51 -06002635static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2636{
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002637 msr->data = 0;
2638
2639 switch (msr->index) {
2640 case MSR_F10H_DECFG:
2641 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2642 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2643 break;
2644 default:
2645 return 1;
2646 }
2647
2648 return 0;
Tom Lendacky801e4592018-02-21 13:39:51 -06002649}
2650
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002651static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002652{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002653 struct vcpu_svm *svm = to_svm(vcpu);
2654
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002655 switch (msr_info->index) {
Brian Gerst8c065852010-07-17 09:03:26 -04002656 case MSR_STAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002657 msr_info->data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002658 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002659#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660 case MSR_LSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002661 msr_info->data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 break;
2663 case MSR_CSTAR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002664 msr_info->data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002665 break;
2666 case MSR_KERNEL_GS_BASE:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002667 msr_info->data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002668 break;
2669 case MSR_SYSCALL_MASK:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002670 msr_info->data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002671 break;
2672#endif
2673 case MSR_IA32_SYSENTER_CS:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002674 msr_info->data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 break;
2676 case MSR_IA32_SYSENTER_EIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002677 msr_info->data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002678 break;
2679 case MSR_IA32_SYSENTER_ESP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002680 msr_info->data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002682 case MSR_TSC_AUX:
2683 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2684 return 1;
2685 msr_info->data = svm->tsc_aux;
2686 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002687 /*
2688 * Nobody will change the following 5 values in the VMCB so we can
2689 * safely return them on rdmsr. They will always be 0 until LBRV is
2690 * implemented.
2691 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002692 case MSR_IA32_DEBUGCTLMSR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002693 msr_info->data = svm->vmcb->save.dbgctl;
Joerg Roedela2938c82008-02-13 16:30:28 +01002694 break;
2695 case MSR_IA32_LASTBRANCHFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002696 msr_info->data = svm->vmcb->save.br_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002697 break;
2698 case MSR_IA32_LASTBRANCHTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002699 msr_info->data = svm->vmcb->save.br_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002700 break;
2701 case MSR_IA32_LASTINTFROMIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002702 msr_info->data = svm->vmcb->save.last_excp_from;
Joerg Roedela2938c82008-02-13 16:30:28 +01002703 break;
2704 case MSR_IA32_LASTINTTOIP:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002705 msr_info->data = svm->vmcb->save.last_excp_to;
Joerg Roedela2938c82008-02-13 16:30:28 +01002706 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002707 case MSR_VM_HSAVE_PA:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002708 msr_info->data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002709 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002710 case MSR_VM_CR:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002711 msr_info->data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002712 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002713 case MSR_IA32_SPEC_CTRL:
2714 if (!msr_info->host_initiated &&
Paolo Bonzinidf7e8812020-02-05 16:10:52 +01002715 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
2716 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04002717 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
2718 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002719 return 1;
2720
2721 msr_info->data = svm->spec_ctrl;
2722 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002723 case MSR_AMD64_VIRT_SPEC_CTRL:
2724 if (!msr_info->host_initiated &&
2725 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2726 return 1;
2727
2728 msr_info->data = svm->virt_spec_ctrl;
2729 break;
Borislav Petkovae8b7872015-11-23 11:12:23 +01002730 case MSR_F15H_IC_CFG: {
2731
2732 int family, model;
2733
2734 family = guest_cpuid_family(vcpu);
2735 model = guest_cpuid_model(vcpu);
2736
2737 if (family < 0 || model < 0)
2738 return kvm_get_msr_common(vcpu, msr_info);
2739
2740 msr_info->data = 0;
2741
2742 if (family == 0x15 &&
2743 (model >= 0x2 && model < 0x20))
2744 msr_info->data = 0x1E;
2745 }
2746 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002747 case MSR_F10H_DECFG:
2748 msr_info->data = svm->msr_decfg;
2749 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002750 default:
Paolo Bonzini609e36d2015-04-08 15:30:38 +02002751 return kvm_get_msr_common(vcpu, msr_info);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002752 }
2753 return 0;
2754}
2755
Avi Kivity851ba692009-08-24 11:10:17 +03002756static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002757{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07002758 return kvm_emulate_rdmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759}
2760
Joerg Roedel4a810182010-02-24 18:59:15 +01002761static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2762{
2763 struct vcpu_svm *svm = to_svm(vcpu);
2764 int svm_dis, chg_mask;
2765
2766 if (data & ~SVM_VM_CR_VALID_MASK)
2767 return 1;
2768
2769 chg_mask = SVM_VM_CR_VALID_MASK;
2770
2771 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2772 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2773
2774 svm->nested.vm_cr_msr &= ~chg_mask;
2775 svm->nested.vm_cr_msr |= (data & chg_mask);
2776
2777 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2778
2779 /* check for svm_disable while efer.svme is set */
2780 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2781 return 1;
2782
2783 return 0;
2784}
2785
Will Auld8fe8ab42012-11-29 12:42:12 -08002786static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002788 struct vcpu_svm *svm = to_svm(vcpu);
2789
Will Auld8fe8ab42012-11-29 12:42:12 -08002790 u32 ecx = msr->index;
2791 u64 data = msr->data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792 switch (ecx) {
Paolo Bonzini15038e12017-10-26 09:13:27 +02002793 case MSR_IA32_CR_PAT:
2794 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2795 return 1;
2796 vcpu->arch.pat = data;
2797 svm->vmcb->save.g_pat = data;
2798 mark_dirty(svm->vmcb, VMCB_NPT);
2799 break;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002800 case MSR_IA32_SPEC_CTRL:
2801 if (!msr->host_initiated &&
Paolo Bonzinidf7e8812020-02-05 16:10:52 +01002802 !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) &&
2803 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_STIBP) &&
Konrad Rzeszutek Wilk6ac2f492018-06-01 10:59:20 -04002804 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS) &&
2805 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002806 return 1;
2807
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002808 if (data & ~kvm_spec_ctrl_valid_bits(vcpu))
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002809 return 1;
2810
2811 svm->spec_ctrl = data;
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01002812 if (!data)
2813 break;
2814
2815 /*
2816 * For non-nested:
2817 * When it's written (to non-zero) for the first time, pass
2818 * it through.
2819 *
2820 * For nested:
2821 * The handling of the MSR bitmap for L2 guests is done in
2822 * nested_svm_vmrun_msrpm.
2823 * We update the L1 MSR bit as well since it will end up
2824 * touching the MSR anyway now.
2825 */
2826 set_msr_interception(svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
2827 break;
Ashok Raj15d45072018-02-01 22:59:43 +01002828 case MSR_IA32_PRED_CMD:
2829 if (!msr->host_initiated &&
Borislav Petkove7c587d2018-05-02 18:15:14 +02002830 !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBPB))
Ashok Raj15d45072018-02-01 22:59:43 +01002831 return 1;
2832
2833 if (data & ~PRED_CMD_IBPB)
2834 return 1;
Paolo Bonzini6441fa62020-01-20 16:33:06 +01002835 if (!boot_cpu_has(X86_FEATURE_AMD_IBPB))
2836 return 1;
Ashok Raj15d45072018-02-01 22:59:43 +01002837 if (!data)
2838 break;
2839
2840 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
Ashok Raj15d45072018-02-01 22:59:43 +01002841 set_msr_interception(svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
2842 break;
Tom Lendackybc226f02018-05-10 22:06:39 +02002843 case MSR_AMD64_VIRT_SPEC_CTRL:
2844 if (!msr->host_initiated &&
2845 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2846 return 1;
2847
2848 if (data & ~SPEC_CTRL_SSBD)
2849 return 1;
2850
2851 svm->virt_spec_ctrl = data;
2852 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002853 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002854 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002855 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002856#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002857 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002858 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 break;
2860 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002861 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002862 break;
2863 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002864 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002865 break;
2866 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002867 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 break;
2869#endif
2870 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002871 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002872 break;
2873 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002874 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002875 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002876 break;
2877 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002878 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002879 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 break;
Paolo Bonzini46896c72015-11-12 14:49:16 +01002881 case MSR_TSC_AUX:
2882 if (!boot_cpu_has(X86_FEATURE_RDTSCP))
2883 return 1;
2884
2885 /*
2886 * This is rare, so we update the MSR here instead of using
2887 * direct_access_msrs. Doing that would require a rdmsr in
2888 * svm_vcpu_put.
2889 */
2890 svm->tsc_aux = data;
2891 wrmsrl(MSR_TSC_AUX, svm->tsc_aux);
2892 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002893 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002894 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Christoffer Dalla737f252012-06-03 21:17:48 +03002895 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2896 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002897 break;
2898 }
2899 if (data & DEBUGCTL_RESERVED_BITS)
2900 return 1;
2901
2902 svm->vmcb->save.dbgctl = data;
Joerg Roedelb53ba3f2010-12-03 11:45:59 +01002903 mark_dirty(svm->vmcb, VMCB_LBR);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002904 if (data & (1ULL<<0))
2905 svm_enable_lbrv(svm);
2906 else
2907 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002908 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002909 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002910 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002911 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002912 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002913 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002914 case MSR_VM_IGNNE:
Christoffer Dalla737f252012-06-03 21:17:48 +03002915 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002916 break;
Tom Lendackyd1d93fa2018-02-24 00:18:20 +01002917 case MSR_F10H_DECFG: {
2918 struct kvm_msr_entry msr_entry;
2919
2920 msr_entry.index = msr->index;
2921 if (svm_get_msr_feature(&msr_entry))
2922 return 1;
2923
2924 /* Check the supported bits */
2925 if (data & ~msr_entry.data)
2926 return 1;
2927
2928 /* Don't allow the guest to change a bit, #GP */
2929 if (!msr->host_initiated && (data ^ msr_entry.data))
2930 return 1;
2931
2932 svm->msr_decfg = data;
2933 break;
2934 }
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05002935 case MSR_IA32_APICBASE:
2936 if (kvm_vcpu_apicv_active(vcpu))
2937 avic_update_vapic_bar(to_svm(vcpu), data);
Gustavo A. R. Silvab2869f22019-01-25 12:23:17 -06002938 /* Fall through */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002939 default:
Will Auld8fe8ab42012-11-29 12:42:12 -08002940 return kvm_set_msr_common(vcpu, msr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002941 }
2942 return 0;
2943}
2944
Avi Kivity851ba692009-08-24 11:10:17 +03002945static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002946{
Sean Christopherson1edce0a2019-09-05 14:22:55 -07002947 return kvm_emulate_wrmsr(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002948}
2949
Avi Kivity851ba692009-08-24 11:10:17 +03002950static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002951{
Rusty Russelle756fc62007-07-30 20:07:08 +10002952 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002953 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002954 else
Avi Kivity851ba692009-08-24 11:10:17 +03002955 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002956}
2957
Avi Kivity851ba692009-08-24 11:10:17 +03002958static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002959{
Avi Kivity3842d132010-07-27 12:30:24 +03002960 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002961 svm_clear_vintr(svm);
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06002962
2963 /*
2964 * For AVIC, the only reason to end up here is ExtINTs.
2965 * In this case AVIC was temporarily disabled for
2966 * requesting the IRQ window and we have to re-enable it.
2967 */
2968 svm_toggle_avic_for_irq_window(&svm->vcpu, true);
2969
Eddie Dong85f455f2007-07-06 12:20:49 +03002970 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Joerg Roedeldecdbf62010-12-03 11:45:52 +01002971 mark_dirty(svm->vmcb, VMCB_INTR);
Jason Wang675acb72012-03-08 18:07:56 +08002972 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002973 return 1;
2974}
2975
Mark Langsdorf565d0992009-10-06 14:25:02 -05002976static int pause_interception(struct vcpu_svm *svm)
2977{
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002978 struct kvm_vcpu *vcpu = &svm->vcpu;
2979 bool in_kernel = (svm_get_cpl(vcpu) == 0);
2980
Babu Moger8566ac82018-03-16 16:37:26 -04002981 if (pause_filter_thresh)
2982 grow_ple_window(vcpu);
2983
Longpeng(Mike)de63ad42017-08-08 12:05:33 +08002984 kvm_vcpu_on_spin(vcpu, in_kernel);
Mark Langsdorf565d0992009-10-06 14:25:02 -05002985 return 1;
2986}
2987
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04002988static int nop_interception(struct vcpu_svm *svm)
2989{
Ladi Prosekb742c1e2017-06-22 09:05:26 +02002990 return kvm_skip_emulated_instruction(&(svm->vcpu));
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04002991}
2992
2993static int monitor_interception(struct vcpu_svm *svm)
2994{
2995 printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
2996 return nop_interception(svm);
2997}
2998
2999static int mwait_interception(struct vcpu_svm *svm)
3000{
3001 printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
3002 return nop_interception(svm);
3003}
3004
Mathias Krause09941fb2012-08-30 01:30:20 +02003005static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
Andre Przywara7ff76d52010-12-21 11:12:04 +01003006 [SVM_EXIT_READ_CR0] = cr_interception,
3007 [SVM_EXIT_READ_CR3] = cr_interception,
3008 [SVM_EXIT_READ_CR4] = cr_interception,
3009 [SVM_EXIT_READ_CR8] = cr_interception,
David Kaplan5e575182015-03-06 14:44:35 -06003010 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
Joerg Roedel628afd22011-04-04 12:39:36 +02003011 [SVM_EXIT_WRITE_CR0] = cr_interception,
Andre Przywara7ff76d52010-12-21 11:12:04 +01003012 [SVM_EXIT_WRITE_CR3] = cr_interception,
3013 [SVM_EXIT_WRITE_CR4] = cr_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003014 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
Andre Przywaracae37972010-12-21 11:12:05 +01003015 [SVM_EXIT_READ_DR0] = dr_interception,
3016 [SVM_EXIT_READ_DR1] = dr_interception,
3017 [SVM_EXIT_READ_DR2] = dr_interception,
3018 [SVM_EXIT_READ_DR3] = dr_interception,
3019 [SVM_EXIT_READ_DR4] = dr_interception,
3020 [SVM_EXIT_READ_DR5] = dr_interception,
3021 [SVM_EXIT_READ_DR6] = dr_interception,
3022 [SVM_EXIT_READ_DR7] = dr_interception,
3023 [SVM_EXIT_WRITE_DR0] = dr_interception,
3024 [SVM_EXIT_WRITE_DR1] = dr_interception,
3025 [SVM_EXIT_WRITE_DR2] = dr_interception,
3026 [SVM_EXIT_WRITE_DR3] = dr_interception,
3027 [SVM_EXIT_WRITE_DR4] = dr_interception,
3028 [SVM_EXIT_WRITE_DR5] = dr_interception,
3029 [SVM_EXIT_WRITE_DR6] = dr_interception,
3030 [SVM_EXIT_WRITE_DR7] = dr_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003031 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3032 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05003033 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003034 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003035 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
Eric Northup54a20552015-11-03 18:03:53 +01003036 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
Liran Alon97184202018-03-12 13:12:52 +02003037 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003038 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02003039 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 [SVM_EXIT_SMI] = nop_on_interception,
3041 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08003042 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity332b56e2011-11-10 14:57:24 +02003043 [SVM_EXIT_RDPMC] = rdpmc_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003045 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02003046 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05003047 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003048 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03003049 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02003050 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01003051 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003052 [SVM_EXIT_MSR] = msr_interception,
3053 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08003054 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01003055 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02003056 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01003057 [SVM_EXIT_VMLOAD] = vmload_interception,
3058 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01003059 [SVM_EXIT_STGI] = stgi_interception,
3060 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02003061 [SVM_EXIT_SKINIT] = skinit_interception,
David Kaplandab429a2015-03-02 13:43:37 -06003062 [SVM_EXIT_WBINVD] = wbinvd_interception,
Gabriel L. Somlo87c00572014-05-07 16:52:13 -04003063 [SVM_EXIT_MONITOR] = monitor_interception,
3064 [SVM_EXIT_MWAIT] = mwait_interception,
Joerg Roedel81dd35d2010-12-07 17:15:06 +01003065 [SVM_EXIT_XSETBV] = xsetbv_interception,
Jim Mattson0cb84102019-09-19 15:59:17 -07003066 [SVM_EXIT_RDPRU] = rdpru_interception,
Paolo Bonzinid0006532017-08-11 18:36:43 +02003067 [SVM_EXIT_NPF] = npf_interception,
Brijesh Singh7607b712018-02-19 10:14:44 -06003068 [SVM_EXIT_RSM] = rsm_interception,
Suravee Suthikulpanit18f40c52016-05-04 14:09:48 -05003069 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3070 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071};
3072
Joe Perchesae8cc052011-04-24 22:00:50 -07003073static void dump_vmcb(struct kvm_vcpu *vcpu)
Joerg Roedel3f10c842010-05-05 16:04:42 +02003074{
3075 struct vcpu_svm *svm = to_svm(vcpu);
3076 struct vmcb_control_area *control = &svm->vmcb->control;
3077 struct vmcb_save_area *save = &svm->vmcb->save;
3078
Paolo Bonzini6f2f8452019-05-20 15:34:35 +02003079 if (!dump_invalid_vmcb) {
3080 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3081 return;
3082 }
3083
Joerg Roedel3f10c842010-05-05 16:04:42 +02003084 pr_err("VMCB Control Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003085 pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff);
3086 pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16);
3087 pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff);
3088 pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16);
3089 pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions);
3090 pr_err("%-20s%016llx\n", "intercepts:", control->intercept);
3091 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
Babu Moger1d8fb442018-03-16 16:37:25 -04003092 pr_err("%-20s%d\n", "pause filter threshold:",
3093 control->pause_filter_thresh);
Joe Perchesae8cc052011-04-24 22:00:50 -07003094 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3095 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3096 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3097 pr_err("%-20s%d\n", "asid:", control->asid);
3098 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3099 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3100 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3101 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3102 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3103 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3104 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3105 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3106 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3107 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3108 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003109 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
Joe Perchesae8cc052011-04-24 22:00:50 -07003110 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3111 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
Janakarajan Natarajan0dc92112017-07-06 15:50:45 -05003112 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
Joe Perchesae8cc052011-04-24 22:00:50 -07003113 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05003114 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3115 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3116 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003117 pr_err("VMCB State Save Area:\n");
Joe Perchesae8cc052011-04-24 22:00:50 -07003118 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3119 "es:",
3120 save->es.selector, save->es.attrib,
3121 save->es.limit, save->es.base);
3122 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3123 "cs:",
3124 save->cs.selector, save->cs.attrib,
3125 save->cs.limit, save->cs.base);
3126 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3127 "ss:",
3128 save->ss.selector, save->ss.attrib,
3129 save->ss.limit, save->ss.base);
3130 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3131 "ds:",
3132 save->ds.selector, save->ds.attrib,
3133 save->ds.limit, save->ds.base);
3134 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3135 "fs:",
3136 save->fs.selector, save->fs.attrib,
3137 save->fs.limit, save->fs.base);
3138 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3139 "gs:",
3140 save->gs.selector, save->gs.attrib,
3141 save->gs.limit, save->gs.base);
3142 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3143 "gdtr:",
3144 save->gdtr.selector, save->gdtr.attrib,
3145 save->gdtr.limit, save->gdtr.base);
3146 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3147 "ldtr:",
3148 save->ldtr.selector, save->ldtr.attrib,
3149 save->ldtr.limit, save->ldtr.base);
3150 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3151 "idtr:",
3152 save->idtr.selector, save->idtr.attrib,
3153 save->idtr.limit, save->idtr.base);
3154 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3155 "tr:",
3156 save->tr.selector, save->tr.attrib,
3157 save->tr.limit, save->tr.base);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003158 pr_err("cpl: %d efer: %016llx\n",
3159 save->cpl, save->efer);
Joe Perchesae8cc052011-04-24 22:00:50 -07003160 pr_err("%-15s %016llx %-13s %016llx\n",
3161 "cr0:", save->cr0, "cr2:", save->cr2);
3162 pr_err("%-15s %016llx %-13s %016llx\n",
3163 "cr3:", save->cr3, "cr4:", save->cr4);
3164 pr_err("%-15s %016llx %-13s %016llx\n",
3165 "dr6:", save->dr6, "dr7:", save->dr7);
3166 pr_err("%-15s %016llx %-13s %016llx\n",
3167 "rip:", save->rip, "rflags:", save->rflags);
3168 pr_err("%-15s %016llx %-13s %016llx\n",
3169 "rsp:", save->rsp, "rax:", save->rax);
3170 pr_err("%-15s %016llx %-13s %016llx\n",
3171 "star:", save->star, "lstar:", save->lstar);
3172 pr_err("%-15s %016llx %-13s %016llx\n",
3173 "cstar:", save->cstar, "sfmask:", save->sfmask);
3174 pr_err("%-15s %016llx %-13s %016llx\n",
3175 "kernel_gs_base:", save->kernel_gs_base,
3176 "sysenter_cs:", save->sysenter_cs);
3177 pr_err("%-15s %016llx %-13s %016llx\n",
3178 "sysenter_esp:", save->sysenter_esp,
3179 "sysenter_eip:", save->sysenter_eip);
3180 pr_err("%-15s %016llx %-13s %016llx\n",
3181 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3182 pr_err("%-15s %016llx %-13s %016llx\n",
3183 "br_from:", save->br_from, "br_to:", save->br_to);
3184 pr_err("%-15s %016llx %-13s %016llx\n",
3185 "excp_from:", save->last_excp_from,
3186 "excp_to:", save->last_excp_to);
Joerg Roedel3f10c842010-05-05 16:04:42 +02003187}
3188
Avi Kivity586f9602010-11-18 13:09:54 +02003189static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
3190{
3191 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3192
3193 *info1 = control->exit_info_1;
3194 *info2 = control->exit_info_2;
3195}
3196
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003197static int handle_exit(struct kvm_vcpu *vcpu,
3198 enum exit_fastpath_completion exit_fastpath)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003200 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003201 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003202 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003203
Paolo Bonzini8b89fe12015-12-10 18:37:32 +01003204 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3205
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003206 if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE))
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003207 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3208 if (npt_enabled)
3209 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003210
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003211 if (unlikely(svm->nested.exit_required)) {
3212 nested_svm_vmexit(svm);
3213 svm->nested.exit_required = false;
3214
3215 return 1;
3216 }
3217
Joerg Roedel20307532010-11-29 17:51:48 +01003218 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003219 int vmexit;
3220
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003221 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3222 svm->vmcb->control.exit_info_1,
3223 svm->vmcb->control.exit_info_2,
3224 svm->vmcb->control.exit_int_info,
Stefan Hajnoczie097e5f2011-07-22 12:46:52 +01003225 svm->vmcb->control.exit_int_info_err,
3226 KVM_ISA_SVM);
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003227
Joerg Roedel410e4d52009-08-07 11:49:44 +02003228 vmexit = nested_svm_exit_special(svm);
3229
3230 if (vmexit == NESTED_EXIT_CONTINUE)
3231 vmexit = nested_svm_exit_handled(svm);
3232
3233 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003234 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003235 }
3236
Joerg Roedela5c38322009-08-07 11:49:32 +02003237 svm_complete_interrupts(svm);
3238
Avi Kivity04d2cc72007-09-10 18:10:54 +03003239 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3240 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3241 kvm_run->fail_entry.hardware_entry_failure_reason
3242 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003243 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003244 return 0;
3245 }
3246
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003247 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003248 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003249 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3250 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Borislav Petkov6614c7d2013-04-26 00:22:01 +02003251 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003253 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003254 exit_code);
3255
Wanpeng Li1e9e2622019-11-21 11:17:11 +08003256 if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
3257 kvm_skip_emulated_instruction(vcpu);
3258 return 1;
3259 } else if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003260 || !svm_exit_handlers[exit_code]) {
Liran Alon7396d332019-08-26 13:16:43 +03003261 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%x\n", exit_code);
3262 dump_vmcb(vcpu);
3263 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3264 vcpu->run->internal.suberror =
3265 KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3266 vcpu->run->internal.ndata = 1;
3267 vcpu->run->internal.data[0] = exit_code;
3268 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003269 }
3270
Andrea Arcangeli3dcb2a32019-11-04 18:00:00 -05003271#ifdef CONFIG_RETPOLINE
3272 if (exit_code == SVM_EXIT_MSR)
3273 return msr_interception(svm);
3274 else if (exit_code == SVM_EXIT_VINTR)
3275 return interrupt_window_interception(svm);
3276 else if (exit_code == SVM_EXIT_INTR)
3277 return intr_interception(svm);
3278 else if (exit_code == SVM_EXIT_HLT)
3279 return halt_interception(svm);
3280 else if (exit_code == SVM_EXIT_NPF)
3281 return npf_interception(svm);
3282#endif
Avi Kivity851ba692009-08-24 11:10:17 +03003283 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003284}
3285
3286static void reload_tss(struct kvm_vcpu *vcpu)
3287{
3288 int cpu = raw_smp_processor_id();
3289
Tejun Heo0fe1e002009-10-29 22:34:14 +09003290 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3291 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003292 load_TR_desc();
3293}
3294
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003295static void pre_sev_run(struct vcpu_svm *svm, int cpu)
3296{
3297 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3298 int asid = sev_get_asid(svm->vcpu.kvm);
3299
3300 /* Assign the asid allocated with this SEV guest */
3301 svm->vmcb->control.asid = asid;
3302
3303 /*
3304 * Flush guest TLB:
3305 *
3306 * 1) when different VMCB for the same ASID is to be run on the same host CPU.
3307 * 2) or this VMCB was executed on different host CPU in previous VMRUNs.
3308 */
3309 if (sd->sev_vmcbs[asid] == svm->vmcb &&
3310 svm->last_cpu == cpu)
3311 return;
3312
3313 svm->last_cpu = cpu;
3314 sd->sev_vmcbs[asid] = svm->vmcb;
3315 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3316 mark_dirty(svm->vmcb, VMCB_ASID);
3317}
3318
Rusty Russelle756fc62007-07-30 20:07:08 +10003319static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003320{
3321 int cpu = raw_smp_processor_id();
3322
Tejun Heo0fe1e002009-10-29 22:34:14 +09003323 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003324
Brijesh Singh70cd94e2017-12-04 10:57:34 -06003325 if (sev_guest(svm->vcpu.kvm))
3326 return pre_sev_run(svm, cpu);
3327
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003328 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003329 if (svm->asid_generation != sd->asid_generation)
3330 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003331}
3332
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003333static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3334{
3335 struct vcpu_svm *svm = to_svm(vcpu);
3336
3337 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3338 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003339 set_intercept(svm, INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003340 ++vcpu->stat.nmi_injections;
3341}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003342
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003343static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003344{
3345 struct vcpu_svm *svm = to_svm(vcpu);
3346
Joerg Roedel2af91942009-08-07 11:49:28 +02003347 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003348
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003349 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3350 ++vcpu->stat.irq_injections;
3351
Alexander Graf219b65d2009-06-15 15:21:25 +02003352 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3353 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003354}
3355
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003356static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3357{
3358 struct vcpu_svm *svm = to_svm(vcpu);
3359
Liran Alon49d654d2019-11-11 14:26:21 +02003360 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003361 return;
3362
Radim Krčmář596f3142014-03-11 19:11:18 +01003363 clr_cr_intercept(svm, INTERCEPT_CR8_WRITE);
3364
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003365 if (irr == -1)
3366 return;
3367
3368 if (tpr >= irr)
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003369 set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003370}
3371
3372static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003373{
3374 struct vcpu_svm *svm = to_svm(vcpu);
3375 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003376 int ret;
3377 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3378 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3379 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3380
3381 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003382}
3383
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003384static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3385{
3386 struct vcpu_svm *svm = to_svm(vcpu);
3387
3388 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3389}
3390
3391static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3392{
3393 struct vcpu_svm *svm = to_svm(vcpu);
3394
3395 if (masked) {
3396 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003397 set_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003398 } else {
3399 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel8a05a1b82010-11-30 18:04:00 +01003400 clr_intercept(svm, INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003401 }
3402}
3403
Gleb Natapov78646122009-03-23 12:12:11 +02003404static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3405{
3406 struct vcpu_svm *svm = to_svm(vcpu);
3407 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003408
3409 if (!gif_set(svm) ||
3410 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3411 return 0;
3412
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003413 if (is_guest_mode(vcpu) && (svm->vcpu.arch.hflags & HF_VINTR_MASK))
3414 return !!(svm->vcpu.arch.hflags & HF_HIF_MASK);
3415 else
3416 return !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF);
Gleb Natapov78646122009-03-23 12:12:11 +02003417}
3418
Jan Kiszkac9a79532014-03-07 20:03:15 +01003419static void enable_irq_window(struct kvm_vcpu *vcpu)
Gleb Natapov9222be12009-04-23 17:14:37 +03003420{
Alexander Graf219b65d2009-06-15 15:21:25 +02003421 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003422
Joerg Roedele0231712010-02-24 18:59:10 +01003423 /*
3424 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3425 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3426 * get that intercept, this function will be called again though and
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003427 * we'll get the vintr intercept. However, if the vGIF feature is
3428 * enabled, the STGI interception will not occur. Enable the irq
3429 * window under the assumption that the hardware will set the GIF.
Joerg Roedele0231712010-02-24 18:59:10 +01003430 */
Paolo Bonzinib518ba92020-03-04 16:46:47 -05003431 if (vgif_enabled(svm) || gif_set(svm)) {
Suravee Suthikulpanitf3515dc2019-11-14 14:15:15 -06003432 /*
3433 * IRQ window is not needed when AVIC is enabled,
3434 * unless we have pending ExtINT since it cannot be injected
3435 * via AVIC. In such case, we need to temporarily disable AVIC,
3436 * and fallback to injecting IRQ via V_IRQ.
3437 */
3438 svm_toggle_avic_for_irq_window(vcpu, false);
Alexander Graf219b65d2009-06-15 15:21:25 +02003439 svm_set_vintr(svm);
Alexander Graf219b65d2009-06-15 15:21:25 +02003440 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003441}
3442
Jan Kiszkac9a79532014-03-07 20:03:15 +01003443static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003445 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003446
Gleb Natapov44c11432009-05-11 13:35:52 +03003447 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3448 == HF_NMI_MASK)
Jan Kiszkac9a79532014-03-07 20:03:15 +01003449 return; /* IRET will cause a vm exit */
Gleb Natapov44c11432009-05-11 13:35:52 +03003450
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003451 if (!gif_set(svm)) {
3452 if (vgif_enabled(svm))
3453 set_intercept(svm, INTERCEPT_STGI);
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003454 return; /* STGI will cause a vm exit */
Janakarajan Natarajan640bd6e2017-08-23 09:57:19 -05003455 }
Ladi Prosek1a5e1852017-06-21 09:07:01 +02003456
3457 if (svm->nested.exit_required)
3458 return; /* we're not going to run the guest yet */
3459
Joerg Roedele0231712010-02-24 18:59:10 +01003460 /*
3461 * Something prevents NMI from been injected. Single step over possible
3462 * problem (IRET or exception injection or interrupt shadow)
3463 */
Ladi Prosekab2f4d732017-06-21 09:06:58 +02003464 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
Jan Kiszka6be7d302009-10-18 13:24:54 +02003465 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003466 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
Eddie Dong85f455f2007-07-06 12:20:49 +03003467}
3468
Izik Eiduscbc94022007-10-25 00:29:55 +02003469static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3470{
3471 return 0;
3472}
3473
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07003474static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3475{
3476 return 0;
3477}
3478
Joerg Roedel883b0a92020-03-24 10:41:52 +01003479void svm_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
Avi Kivityd9e368d2007-06-07 19:18:30 +03003480{
Joerg Roedel38e5e922010-12-03 15:25:16 +01003481 struct vcpu_svm *svm = to_svm(vcpu);
3482
3483 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3484 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3485 else
3486 svm->asid_generation--;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003487}
3488
Junaid Shahidfaff8752018-06-29 13:10:05 -07003489static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3490{
3491 struct vcpu_svm *svm = to_svm(vcpu);
3492
3493 invlpga(gva, svm->vmcb->control.asid);
3494}
3495
Avi Kivity04d2cc72007-09-10 18:10:54 +03003496static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3497{
3498}
3499
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003500static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3501{
3502 struct vcpu_svm *svm = to_svm(vcpu);
3503
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003504 if (svm_nested_virtualize_tpr(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003505 return;
3506
Roedel, Joerg4ee546b2010-12-03 10:50:51 +01003507 if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) {
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003508 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003509 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003510 }
3511}
3512
Joerg Roedel649d6862008-04-16 16:51:15 +02003513static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3514{
3515 struct vcpu_svm *svm = to_svm(vcpu);
3516 u64 cr8;
3517
Suravee Suthikulpanit3bbf3562016-05-04 14:09:51 -05003518 if (svm_nested_virtualize_tpr(vcpu) ||
3519 kvm_vcpu_apicv_active(vcpu))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003520 return;
3521
Joerg Roedel649d6862008-04-16 16:51:15 +02003522 cr8 = kvm_get_cr8(vcpu);
3523 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3524 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3525}
3526
Gleb Natapov9222be12009-04-23 17:14:37 +03003527static void svm_complete_interrupts(struct vcpu_svm *svm)
3528{
3529 u8 vector;
3530 int type;
3531 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003532 unsigned int3_injected = svm->int3_injected;
3533
3534 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003535
Avi Kivitybd3d1ec2011-02-03 15:29:52 +02003536 /*
3537 * If we've made progress since setting HF_IRET_MASK, we've
3538 * executed an IRET and can allow NMI injection.
3539 */
3540 if ((svm->vcpu.arch.hflags & HF_IRET_MASK)
3541 && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003542 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003543 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3544 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003545
Gleb Natapov9222be12009-04-23 17:14:37 +03003546 svm->vcpu.arch.nmi_injected = false;
3547 kvm_clear_exception_queue(&svm->vcpu);
3548 kvm_clear_interrupt_queue(&svm->vcpu);
3549
3550 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3551 return;
3552
Avi Kivity3842d132010-07-27 12:30:24 +03003553 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3554
Gleb Natapov9222be12009-04-23 17:14:37 +03003555 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3556 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3557
3558 switch (type) {
3559 case SVM_EXITINTINFO_TYPE_NMI:
3560 svm->vcpu.arch.nmi_injected = true;
3561 break;
3562 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003563 /*
3564 * In case of software exceptions, do not reinject the vector,
3565 * but re-execute the instruction instead. Rewind RIP first
3566 * if we emulated INT3 before.
3567 */
3568 if (kvm_exception_is_soft(vector)) {
3569 if (vector == BP_VECTOR && int3_injected &&
3570 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3571 kvm_rip_write(&svm->vcpu,
3572 kvm_rip_read(&svm->vcpu) -
3573 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003574 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003575 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003576 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3577 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003578 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003579
3580 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003581 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003582 break;
3583 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003584 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003585 break;
3586 default:
3587 break;
3588 }
3589}
3590
Avi Kivityb463a6f2010-07-20 15:06:17 +03003591static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3592{
3593 struct vcpu_svm *svm = to_svm(vcpu);
3594 struct vmcb_control_area *control = &svm->vmcb->control;
3595
3596 control->exit_int_info = control->event_inj;
3597 control->exit_int_info_err = control->event_inj_err;
3598 control->event_inj = 0;
3599 svm_complete_interrupts(svm);
3600}
3601
Avi Kivity851ba692009-08-24 11:10:17 +03003602static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003603{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003604 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003605
Joerg Roedel2041a062010-04-22 12:33:08 +02003606 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3607 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3608 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3609
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003610 /*
3611 * A vmexit emulation is required before the vcpu can be executed
3612 * again.
3613 */
3614 if (unlikely(svm->nested.exit_required))
3615 return;
3616
Ladi Proseka12713c2017-06-21 09:07:00 +02003617 /*
3618 * Disable singlestep if we're injecting an interrupt/exception.
3619 * We don't want our modified rflags to be pushed on the stack where
3620 * we might not be able to easily reset them if we disabled NMI
3621 * singlestep later.
3622 */
3623 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3624 /*
3625 * Event injection happens before external interrupts cause a
3626 * vmexit and interrupts are disabled here, so smp_send_reschedule
3627 * is enough to force an immediate vmexit.
3628 */
3629 disable_nmi_singlestep(svm);
3630 smp_send_reschedule(vcpu->cpu);
3631 }
3632
Rusty Russelle756fc62007-07-30 20:07:08 +10003633 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003634
Joerg Roedel649d6862008-04-16 16:51:15 +02003635 sync_lapic_to_cr8(vcpu);
3636
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003637 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003638
Avi Kivity04d2cc72007-09-10 18:10:54 +03003639 clgi();
Aaron Lewis139a12c2019-10-21 16:30:25 -07003640 kvm_load_guest_xsave_state(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003641
Wanpeng Lib6c4bc62019-05-20 16:18:09 +08003642 if (lapic_in_kernel(vcpu) &&
3643 vcpu->arch.apic->lapic_timer.timer_advance_ns)
3644 kvm_wait_lapic_expire(vcpu);
3645
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003646 /*
3647 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3648 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3649 * is no need to worry about the conditional branch over the wrmsr
3650 * being speculatively taken.
3651 */
Thomas Gleixnerccbcd262018-05-09 23:01:01 +02003652 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003653
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003654 local_irq_enable();
3655
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 asm volatile (
Avi Kivity74547662012-09-16 15:10:59 +03003657 "push %%" _ASM_BP "; \n\t"
3658 "mov %c[rbx](%[svm]), %%" _ASM_BX " \n\t"
3659 "mov %c[rcx](%[svm]), %%" _ASM_CX " \n\t"
3660 "mov %c[rdx](%[svm]), %%" _ASM_DX " \n\t"
3661 "mov %c[rsi](%[svm]), %%" _ASM_SI " \n\t"
3662 "mov %c[rdi](%[svm]), %%" _ASM_DI " \n\t"
3663 "mov %c[rbp](%[svm]), %%" _ASM_BP " \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003664#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003665 "mov %c[r8](%[svm]), %%r8 \n\t"
3666 "mov %c[r9](%[svm]), %%r9 \n\t"
3667 "mov %c[r10](%[svm]), %%r10 \n\t"
3668 "mov %c[r11](%[svm]), %%r11 \n\t"
3669 "mov %c[r12](%[svm]), %%r12 \n\t"
3670 "mov %c[r13](%[svm]), %%r13 \n\t"
3671 "mov %c[r14](%[svm]), %%r14 \n\t"
3672 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003673#endif
3674
Avi Kivity6aa8b732006-12-10 02:21:36 -08003675 /* Enter guest mode */
Avi Kivity74547662012-09-16 15:10:59 +03003676 "push %%" _ASM_AX " \n\t"
3677 "mov %c[vmcb](%[svm]), %%" _ASM_AX " \n\t"
Uros Bizjakac5ffda22018-11-26 17:00:08 +01003678 __ex("vmload %%" _ASM_AX) "\n\t"
3679 __ex("vmrun %%" _ASM_AX) "\n\t"
3680 __ex("vmsave %%" _ASM_AX) "\n\t"
Avi Kivity74547662012-09-16 15:10:59 +03003681 "pop %%" _ASM_AX " \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682
3683 /* Save guest registers, load host registers */
Avi Kivity74547662012-09-16 15:10:59 +03003684 "mov %%" _ASM_BX ", %c[rbx](%[svm]) \n\t"
3685 "mov %%" _ASM_CX ", %c[rcx](%[svm]) \n\t"
3686 "mov %%" _ASM_DX ", %c[rdx](%[svm]) \n\t"
3687 "mov %%" _ASM_SI ", %c[rsi](%[svm]) \n\t"
3688 "mov %%" _ASM_DI ", %c[rdi](%[svm]) \n\t"
3689 "mov %%" _ASM_BP ", %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003690#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003691 "mov %%r8, %c[r8](%[svm]) \n\t"
3692 "mov %%r9, %c[r9](%[svm]) \n\t"
3693 "mov %%r10, %c[r10](%[svm]) \n\t"
3694 "mov %%r11, %c[r11](%[svm]) \n\t"
3695 "mov %%r12, %c[r12](%[svm]) \n\t"
3696 "mov %%r13, %c[r13](%[svm]) \n\t"
3697 "mov %%r14, %c[r14](%[svm]) \n\t"
3698 "mov %%r15, %c[r15](%[svm]) \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08003699 /*
3700 * Clear host registers marked as clobbered to prevent
3701 * speculative use.
3702 */
Uros Bizjak43ce76c2018-10-17 16:46:57 +02003703 "xor %%r8d, %%r8d \n\t"
3704 "xor %%r9d, %%r9d \n\t"
3705 "xor %%r10d, %%r10d \n\t"
3706 "xor %%r11d, %%r11d \n\t"
3707 "xor %%r12d, %%r12d \n\t"
3708 "xor %%r13d, %%r13d \n\t"
3709 "xor %%r14d, %%r14d \n\t"
3710 "xor %%r15d, %%r15d \n\t"
Jim Mattson0cb5b302018-01-03 14:31:38 -08003711#endif
Uros Bizjak43ce76c2018-10-17 16:46:57 +02003712 "xor %%ebx, %%ebx \n\t"
3713 "xor %%ecx, %%ecx \n\t"
3714 "xor %%edx, %%edx \n\t"
3715 "xor %%esi, %%esi \n\t"
3716 "xor %%edi, %%edi \n\t"
Avi Kivity74547662012-09-16 15:10:59 +03003717 "pop %%" _ASM_BP
Avi Kivity6aa8b732006-12-10 02:21:36 -08003718 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003719 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003720 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003721 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3722 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3723 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3724 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3725 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3726 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003727#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003728 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3729 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3730 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3731 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3732 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3733 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3734 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3735 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003736#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003737 : "cc", "memory"
3738#ifdef CONFIG_X86_64
Avi Kivity74547662012-09-16 15:10:59 +03003739 , "rbx", "rcx", "rdx", "rsi", "rdi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003740 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
Avi Kivity74547662012-09-16 15:10:59 +03003741#else
3742 , "ebx", "ecx", "edx", "esi", "edi"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003743#endif
3744 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003745
Thomas Gleixner15e6c222018-05-11 15:21:01 +02003746 /* Eliminate branch target predictions from guest mode */
3747 vmexit_fill_RSB();
3748
3749#ifdef CONFIG_X86_64
3750 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3751#else
3752 loadsegment(fs, svm->host.fs);
3753#ifndef CONFIG_X86_32_LAZY_GS
3754 loadsegment(gs, svm->host.gs);
3755#endif
3756#endif
3757
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003758 /*
3759 * We do not use IBRS in the kernel. If this vCPU has used the
3760 * SPEC_CTRL MSR it may have left it on; save the value and
3761 * turn it off. This is much more efficient than blindly adding
3762 * it to the atomic save/restore list. Especially as the former
3763 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3764 *
3765 * For non-nested case:
3766 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3767 * save it.
3768 *
3769 * For nested case:
3770 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3771 * save it.
3772 */
Paolo Bonzini946fbbc2018-02-22 16:43:18 +01003773 if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
Paolo Bonziniecb586b2018-02-22 16:43:17 +01003774 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
KarimAllah Ahmedb2ac58f2018-02-03 15:56:23 +01003775
Avi Kivity6aa8b732006-12-10 02:21:36 -08003776 reload_tss(vcpu);
3777
Avi Kivity56ba47d2007-11-07 17:14:18 +02003778 local_irq_disable();
3779
Thomas Gleixner024d83c2018-08-12 20:41:45 +02003780 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3781
Avi Kivity13c34e02010-10-21 12:20:31 +02003782 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3783 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3784 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3785 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3786
Joerg Roedel3781c012011-01-14 16:45:02 +01003787 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07003788 kvm_before_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003789
Aaron Lewis139a12c2019-10-21 16:30:25 -07003790 kvm_load_host_xsave_state(vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003791 stgi();
3792
3793 /* Any pending NMI will happen here */
3794
3795 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
Andi Kleendd60d212017-07-25 17:20:32 -07003796 kvm_after_interrupt(&svm->vcpu);
Joerg Roedel3781c012011-01-14 16:45:02 +01003797
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003798 sync_cr8_to_lapic(vcpu);
3799
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003800 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003801
Joerg Roedel38e5e922010-12-03 15:25:16 +01003802 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3803
Gleb Natapov631bc482010-10-14 11:22:52 +02003804 /* if exit due to PF check for async PF */
3805 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
Wanpeng Li1261bfa2017-07-13 18:30:40 -07003806 svm->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
Gleb Natapov631bc482010-10-14 11:22:52 +02003807
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003808 if (npt_enabled) {
3809 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3810 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3811 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003812
3813 /*
3814 * We need to handle MC intercepts here before the vcpu has a chance to
3815 * change the physical cpu
3816 */
3817 if (unlikely(svm->vmcb->control.exit_code ==
3818 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3819 svm_handle_mce(svm);
Roedel, Joerg8d28fec2010-12-03 13:15:21 +01003820
3821 mark_all_clean(svm->vmcb);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003822}
Josh Poimboeufc207aee2017-06-28 10:11:06 -05003823STACK_FRAME_NON_STANDARD(svm_vcpu_run);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003824
Paolo Bonzini727a7e22020-03-05 03:52:50 -05003825static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long root)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003826{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003827 struct vcpu_svm *svm = to_svm(vcpu);
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003828 bool update_guest_cr3 = true;
3829 unsigned long cr3;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003830
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003831 cr3 = __sme_set(root);
3832 if (npt_enabled) {
3833 svm->vmcb->control.nested_cr3 = cr3;
3834 mark_dirty(svm->vmcb, VMCB_NPT);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003835
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003836 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
3837 if (is_guest_mode(vcpu))
3838 update_guest_cr3 = false;
3839 else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3840 cr3 = vcpu->arch.cr3;
3841 else /* CR3 is already up-to-date. */
3842 update_guest_cr3 = false;
3843 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003844
Paolo Bonzini689f3bf2020-03-03 10:11:10 +01003845 if (update_guest_cr3) {
3846 svm->vmcb->save.cr3 = cr3;
3847 mark_dirty(svm->vmcb, VMCB_CR);
3848 }
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003849}
3850
Avi Kivity6aa8b732006-12-10 02:21:36 -08003851static int is_disabled(void)
3852{
Joerg Roedel6031a612007-06-22 12:29:50 +03003853 u64 vm_cr;
3854
3855 rdmsrl(MSR_VM_CR, vm_cr);
3856 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3857 return 1;
3858
Avi Kivity6aa8b732006-12-10 02:21:36 -08003859 return 0;
3860}
3861
Ingo Molnar102d8322007-02-19 14:37:47 +02003862static void
3863svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3864{
3865 /*
3866 * Patch in the VMMCALL instruction:
3867 */
3868 hypercall[0] = 0x0f;
3869 hypercall[1] = 0x01;
3870 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003871}
3872
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003873static int __init svm_check_processor_compat(void)
Yang, Sheng002c7f72007-07-31 14:23:01 +03003874{
Sean Christophersonf257d6d2019-04-19 22:18:17 -07003875 return 0;
Yang, Sheng002c7f72007-07-31 14:23:01 +03003876}
3877
Avi Kivity774ead32007-12-26 13:57:04 +02003878static bool svm_cpu_has_accelerated_tpr(void)
3879{
3880 return false;
3881}
3882
Tom Lendackybc226f02018-05-10 22:06:39 +02003883static bool svm_has_emulated_msr(int index)
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003884{
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003885 switch (index) {
3886 case MSR_IA32_MCG_EXT_CTL:
Paolo Bonzini95c5c7c2019-07-02 14:45:24 +02003887 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
Vitaly Kuznetsove87555e2018-12-19 12:06:13 +01003888 return false;
3889 default:
3890 break;
3891 }
3892
Paolo Bonzini6d396b52015-04-01 14:25:33 +02003893 return true;
3894}
3895
Paolo Bonzinifc07e762015-10-01 13:20:22 +02003896static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3897{
3898 return 0;
3899}
3900
Sheng Yang0e851882009-12-18 16:48:46 +08003901static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3902{
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003903 struct vcpu_svm *svm = to_svm(vcpu);
3904
Aaron Lewis72041602019-10-21 16:30:20 -07003905 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
Sean Christopherson96be4e02019-12-10 14:44:15 -08003906 boot_cpu_has(X86_FEATURE_XSAVE) &&
Aaron Lewis72041602019-10-21 16:30:20 -07003907 boot_cpu_has(X86_FEATURE_XSAVES);
3908
Joerg Roedel6092d3d2015-10-14 15:10:54 +02003909 /* Update nrips enabled cache */
Sean Christopherson4eb87462020-03-02 15:57:08 -08003910 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
3911 guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
Suravee Suthikulpanit46781ea2016-05-04 14:09:50 -05003912
3913 if (!kvm_vcpu_apicv_active(vcpu))
3914 return;
3915
Oliver Uptoncc7f5572020-02-28 00:59:04 -08003916 /*
3917 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3918 * is exposed to the guest, disable AVIC.
3919 */
3920 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3921 kvm_request_apicv_update(vcpu->kvm, false,
3922 APICV_INHIBIT_REASON_X2APIC);
Suravee Suthikulpanit9a0bf052019-11-14 14:15:14 -06003923
3924 /*
3925 * Currently, AVIC does not work with nested virtualization.
3926 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3927 */
3928 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3929 kvm_request_apicv_update(vcpu->kvm, false,
3930 APICV_INHIBIT_REASON_NESTED);
Sheng Yang0e851882009-12-18 16:48:46 +08003931}
3932
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003933static bool svm_has_wbinvd_exit(void)
3934{
3935 return true;
3936}
3937
Joerg Roedel80612522011-04-04 12:39:33 +02003938#define PRE_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003939 .stage = X86_ICPT_PRE_EXCEPT, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003940#define POST_EX(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003941 .stage = X86_ICPT_POST_EXCEPT, }
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003942#define POST_MEM(exit) { .exit_code = (exit), \
Avi Kivity40e19b52011-04-21 12:35:41 +03003943 .stage = X86_ICPT_POST_MEMACCESS, }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003944
Mathias Krause09941fb2012-08-30 01:30:20 +02003945static const struct __x86_intercept {
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003946 u32 exit_code;
3947 enum x86_intercept_stage stage;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003948} x86_intercept_map[] = {
3949 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
3950 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
3951 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
3952 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
3953 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
Joerg Roedel3b88e412011-04-04 12:39:29 +02003954 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
3955 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
Joerg Roedeldee6bb72011-04-04 12:39:30 +02003956 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
3957 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
3958 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
3959 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
3960 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
3961 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
3962 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
3963 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
Joerg Roedel01de8b02011-04-04 12:39:31 +02003964 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
3965 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
3966 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
3967 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
3968 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
3969 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
3970 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
3971 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
Joerg Roedeld7eb8202011-04-04 12:39:32 +02003972 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
3973 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
3974 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
Joerg Roedel80612522011-04-04 12:39:33 +02003975 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
3976 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
3977 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
3978 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
3979 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
3980 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
3981 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
3982 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
3983 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
Joerg Roedelbf608f82011-04-04 12:39:34 +02003984 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
3985 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
3986 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
3987 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
3988 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
3989 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
3990 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
Joerg Roedelf6511932011-04-04 12:39:35 +02003991 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
3992 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
3993 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
3994 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
Vitaly Kuznetsov02d41602019-08-13 15:53:32 +02003995 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003996};
3997
Joerg Roedel80612522011-04-04 12:39:33 +02003998#undef PRE_EX
Joerg Roedelcfec82c2011-04-04 12:39:28 +02003999#undef POST_EX
Joerg Roedeld7eb8202011-04-04 12:39:32 +02004000#undef POST_MEM
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004001
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004002static int svm_check_intercept(struct kvm_vcpu *vcpu,
4003 struct x86_instruction_info *info,
Sean Christopherson21f1b8f2020-02-18 15:29:42 -08004004 enum x86_intercept_stage stage,
4005 struct x86_exception *exception)
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004006{
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004007 struct vcpu_svm *svm = to_svm(vcpu);
4008 int vmexit, ret = X86EMUL_CONTINUE;
4009 struct __x86_intercept icpt_info;
4010 struct vmcb *vmcb = svm->vmcb;
4011
4012 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4013 goto out;
4014
4015 icpt_info = x86_intercept_map[info->intercept];
4016
Avi Kivity40e19b52011-04-21 12:35:41 +03004017 if (stage != icpt_info.stage)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004018 goto out;
4019
4020 switch (icpt_info.exit_code) {
4021 case SVM_EXIT_READ_CR0:
4022 if (info->intercept == x86_intercept_cr_read)
4023 icpt_info.exit_code += info->modrm_reg;
4024 break;
4025 case SVM_EXIT_WRITE_CR0: {
4026 unsigned long cr0, val;
4027 u64 intercept;
4028
4029 if (info->intercept == x86_intercept_cr_write)
4030 icpt_info.exit_code += info->modrm_reg;
4031
Jan Kiszka62baf442014-06-29 21:55:53 +02004032 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4033 info->intercept == x86_intercept_clts)
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004034 break;
4035
4036 intercept = svm->nested.intercept;
4037
4038 if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))
4039 break;
4040
4041 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4042 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4043
4044 if (info->intercept == x86_intercept_lmsw) {
4045 cr0 &= 0xfUL;
4046 val &= 0xfUL;
4047 /* lmsw can't clear PE - catch this here */
4048 if (cr0 & X86_CR0_PE)
4049 val |= X86_CR0_PE;
4050 }
4051
4052 if (cr0 ^ val)
4053 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4054
4055 break;
4056 }
Joerg Roedel3b88e412011-04-04 12:39:29 +02004057 case SVM_EXIT_READ_DR0:
4058 case SVM_EXIT_WRITE_DR0:
4059 icpt_info.exit_code += info->modrm_reg;
4060 break;
Joerg Roedel80612522011-04-04 12:39:33 +02004061 case SVM_EXIT_MSR:
4062 if (info->intercept == x86_intercept_wrmsr)
4063 vmcb->control.exit_info_1 = 1;
4064 else
4065 vmcb->control.exit_info_1 = 0;
4066 break;
Joerg Roedelbf608f82011-04-04 12:39:34 +02004067 case SVM_EXIT_PAUSE:
4068 /*
4069 * We get this for NOP only, but pause
4070 * is rep not, check this here
4071 */
4072 if (info->rep_prefix != REPE_PREFIX)
4073 goto out;
Jan H. Schönherr49a8afc2017-09-05 23:58:44 +02004074 break;
Joerg Roedelf6511932011-04-04 12:39:35 +02004075 case SVM_EXIT_IOIO: {
4076 u64 exit_info;
4077 u32 bytes;
4078
Joerg Roedelf6511932011-04-04 12:39:35 +02004079 if (info->intercept == x86_intercept_in ||
4080 info->intercept == x86_intercept_ins) {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004081 exit_info = ((info->src_val & 0xffff) << 16) |
4082 SVM_IOIO_TYPE_MASK;
Joerg Roedelf6511932011-04-04 12:39:35 +02004083 bytes = info->dst_bytes;
Jan Kiszka6493f152014-06-30 11:07:05 +02004084 } else {
Jan Kiszka6cbc5f52014-06-30 12:52:55 +02004085 exit_info = (info->dst_val & 0xffff) << 16;
Jan Kiszka6493f152014-06-30 11:07:05 +02004086 bytes = info->src_bytes;
Joerg Roedelf6511932011-04-04 12:39:35 +02004087 }
4088
4089 if (info->intercept == x86_intercept_outs ||
4090 info->intercept == x86_intercept_ins)
4091 exit_info |= SVM_IOIO_STR_MASK;
4092
4093 if (info->rep_prefix)
4094 exit_info |= SVM_IOIO_REP_MASK;
4095
4096 bytes = min(bytes, 4u);
4097
4098 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4099
4100 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4101
4102 vmcb->control.exit_info_1 = exit_info;
4103 vmcb->control.exit_info_2 = info->next_rip;
4104
4105 break;
4106 }
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004107 default:
4108 break;
4109 }
4110
Bandan Dasf1047652015-06-11 02:05:33 -04004111 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4112 if (static_cpu_has(X86_FEATURE_NRIPS))
4113 vmcb->control.next_rip = info->next_rip;
Joerg Roedelcfec82c2011-04-04 12:39:28 +02004114 vmcb->control.exit_code = icpt_info.exit_code;
4115 vmexit = nested_svm_exit_handled(svm);
4116
4117 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4118 : X86EMUL_CONTINUE;
4119
4120out:
4121 return ret;
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02004122}
4123
Wanpeng Li1e9e2622019-11-21 11:17:11 +08004124static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu,
4125 enum exit_fastpath_completion *exit_fastpath)
Yang Zhanga547c6d2013-04-11 19:25:10 +08004126{
Wanpeng Li1e9e2622019-11-21 11:17:11 +08004127 if (!is_guest_mode(vcpu) &&
Haiwei Liaaca2102020-03-02 20:19:28 +08004128 to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
4129 to_svm(vcpu)->vmcb->control.exit_info_1)
Wanpeng Li1e9e2622019-11-21 11:17:11 +08004130 *exit_fastpath = handle_fastpath_set_msr_irqoff(vcpu);
Yang Zhanga547c6d2013-04-11 19:25:10 +08004131}
4132
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004133static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4134{
Babu Moger8566ac82018-03-16 16:37:26 -04004135 if (pause_filter_thresh)
4136 shrink_ple_window(vcpu);
Radim Krčmářae97a3b2014-08-21 18:08:06 +02004137}
4138
Borislav Petkov74f16902017-03-26 23:51:24 +02004139static void svm_setup_mce(struct kvm_vcpu *vcpu)
4140{
4141 /* [63:9] are reserved. */
4142 vcpu->arch.mcg_cap &= 0x1ff;
4143}
4144
Ladi Prosek72d7b372017-10-11 16:54:41 +02004145static int svm_smi_allowed(struct kvm_vcpu *vcpu)
4146{
Ladi Prosek05cade72017-10-11 16:54:45 +02004147 struct vcpu_svm *svm = to_svm(vcpu);
4148
4149 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4150 if (!gif_set(svm))
4151 return 0;
4152
4153 if (is_guest_mode(&svm->vcpu) &&
4154 svm->nested.intercept & (1ULL << INTERCEPT_SMI)) {
4155 /* TODO: Might need to set exit_info_1 and exit_info_2 here */
4156 svm->vmcb->control.exit_code = SVM_EXIT_SMI;
4157 svm->nested.exit_required = true;
4158 return 0;
4159 }
4160
Ladi Prosek72d7b372017-10-11 16:54:41 +02004161 return 1;
4162}
4163
Ladi Prosek0234bf82017-10-11 16:54:40 +02004164static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4165{
Ladi Prosek05cade72017-10-11 16:54:45 +02004166 struct vcpu_svm *svm = to_svm(vcpu);
4167 int ret;
4168
4169 if (is_guest_mode(vcpu)) {
4170 /* FED8h - SVM Guest */
4171 put_smstate(u64, smstate, 0x7ed8, 1);
4172 /* FEE0h - SVM Guest VMCB Physical Address */
4173 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb);
4174
4175 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4176 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4177 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4178
4179 ret = nested_svm_vmexit(svm);
4180 if (ret)
4181 return ret;
4182 }
Ladi Prosek0234bf82017-10-11 16:54:40 +02004183 return 0;
4184}
4185
Sean Christophersoned193212019-04-02 08:03:09 -07004186static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
Ladi Prosek0234bf82017-10-11 16:54:40 +02004187{
Ladi Prosek05cade72017-10-11 16:54:45 +02004188 struct vcpu_svm *svm = to_svm(vcpu);
4189 struct vmcb *nested_vmcb;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01004190 struct kvm_host_map map;
Sean Christophersoned193212019-04-02 08:03:09 -07004191 u64 guest;
4192 u64 vmcb;
Ladi Prosek05cade72017-10-11 16:54:45 +02004193
Sean Christophersoned193212019-04-02 08:03:09 -07004194 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
4195 vmcb = GET_SMSTATE(u64, smstate, 0x7ee0);
Ladi Prosek05cade72017-10-11 16:54:45 +02004196
Sean Christophersoned193212019-04-02 08:03:09 -07004197 if (guest) {
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01004198 if (kvm_vcpu_map(&svm->vcpu, gpa_to_gfn(vmcb), &map) == -EINVAL)
Sean Christopherson9ec19492019-04-02 08:03:11 -07004199 return 1;
KarimAllah Ahmed8c5fbf12019-01-31 21:24:40 +01004200 nested_vmcb = map.hva;
4201 enter_svm_guest_mode(svm, vmcb, nested_vmcb, &map);
Ladi Prosek05cade72017-10-11 16:54:45 +02004202 }
Sean Christopherson9ec19492019-04-02 08:03:11 -07004203 return 0;
Ladi Prosek0234bf82017-10-11 16:54:40 +02004204}
4205
Ladi Prosekcc3d9672017-10-17 16:02:39 +02004206static int enable_smi_window(struct kvm_vcpu *vcpu)
4207{
4208 struct vcpu_svm *svm = to_svm(vcpu);
4209
4210 if (!gif_set(svm)) {
4211 if (vgif_enabled(svm))
4212 set_intercept(svm, INTERCEPT_STGI);
4213 /* STGI will cause a vm exit */
4214 return 1;
4215 }
4216 return 0;
4217}
4218
Tom Lendacky33af3a72019-10-03 21:17:48 +00004219static int sev_flush_asids(void)
4220{
4221 int ret, error;
4222
4223 /*
4224 * DEACTIVATE will clear the WBINVD indicator causing DF_FLUSH to fail,
4225 * so it must be guarded.
4226 */
4227 down_write(&sev_deactivate_lock);
4228
4229 wbinvd_on_all_cpus();
4230 ret = sev_guest_df_flush(&error);
4231
4232 up_write(&sev_deactivate_lock);
4233
4234 if (ret)
4235 pr_err("SEV: DF_FLUSH failed, ret=%d, error=%#x\n", ret, error);
4236
4237 return ret;
4238}
4239
4240/* Must be called with the sev_bitmap_lock held */
4241static bool __sev_recycle_asids(void)
4242{
4243 int pos;
4244
4245 /* Check if there are any ASIDs to reclaim before performing a flush */
4246 pos = find_next_bit(sev_reclaim_asid_bitmap,
4247 max_sev_asid, min_sev_asid - 1);
4248 if (pos >= max_sev_asid)
4249 return false;
4250
4251 if (sev_flush_asids())
4252 return false;
4253
4254 bitmap_xor(sev_asid_bitmap, sev_asid_bitmap, sev_reclaim_asid_bitmap,
4255 max_sev_asid);
4256 bitmap_zero(sev_reclaim_asid_bitmap, max_sev_asid);
4257
4258 return true;
4259}
4260
Brijesh Singh1654efc2017-12-04 10:57:34 -06004261static int sev_asid_new(void)
4262{
Tom Lendacky33af3a72019-10-03 21:17:48 +00004263 bool retry = true;
Brijesh Singh1654efc2017-12-04 10:57:34 -06004264 int pos;
4265
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004266 mutex_lock(&sev_bitmap_lock);
4267
Brijesh Singh1654efc2017-12-04 10:57:34 -06004268 /*
4269 * SEV-enabled guest must use asid from min_sev_asid to max_sev_asid.
4270 */
Tom Lendacky33af3a72019-10-03 21:17:48 +00004271again:
Brijesh Singh1654efc2017-12-04 10:57:34 -06004272 pos = find_next_zero_bit(sev_asid_bitmap, max_sev_asid, min_sev_asid - 1);
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004273 if (pos >= max_sev_asid) {
Tom Lendacky33af3a72019-10-03 21:17:48 +00004274 if (retry && __sev_recycle_asids()) {
4275 retry = false;
4276 goto again;
4277 }
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004278 mutex_unlock(&sev_bitmap_lock);
Brijesh Singh1654efc2017-12-04 10:57:34 -06004279 return -EBUSY;
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004280 }
Brijesh Singh1654efc2017-12-04 10:57:34 -06004281
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004282 __set_bit(pos, sev_asid_bitmap);
4283
4284 mutex_unlock(&sev_bitmap_lock);
4285
Brijesh Singh1654efc2017-12-04 10:57:34 -06004286 return pos + 1;
4287}
4288
4289static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp)
4290{
Sean Christopherson81811c12018-03-20 12:17:21 -07004291 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1654efc2017-12-04 10:57:34 -06004292 int asid, ret;
4293
4294 ret = -EBUSY;
David Rientjes3f14a892019-01-02 12:56:33 -08004295 if (unlikely(sev->active))
4296 return ret;
4297
Brijesh Singh1654efc2017-12-04 10:57:34 -06004298 asid = sev_asid_new();
4299 if (asid < 0)
4300 return ret;
4301
4302 ret = sev_platform_init(&argp->error);
4303 if (ret)
4304 goto e_free;
4305
4306 sev->active = true;
4307 sev->asid = asid;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06004308 INIT_LIST_HEAD(&sev->regions_list);
Brijesh Singh1654efc2017-12-04 10:57:34 -06004309
4310 return 0;
4311
4312e_free:
Tom Lendackye3b9a9e2019-10-03 21:17:43 +00004313 sev_asid_free(asid);
Brijesh Singh1654efc2017-12-04 10:57:34 -06004314 return ret;
4315}
4316
Brijesh Singh59414c92017-12-04 10:57:35 -06004317static int sev_bind_asid(struct kvm *kvm, unsigned int handle, int *error)
4318{
4319 struct sev_data_activate *data;
4320 int asid = sev_get_asid(kvm);
4321 int ret;
4322
Ben Gardon1ec69642019-02-11 11:02:51 -08004323 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06004324 if (!data)
4325 return -ENOMEM;
4326
4327 /* activate ASID on the given handle */
4328 data->handle = handle;
4329 data->asid = asid;
4330 ret = sev_guest_activate(data, error);
4331 kfree(data);
4332
4333 return ret;
4334}
4335
Brijesh Singh89c50582017-12-04 10:57:35 -06004336static int __sev_issue_cmd(int fd, int id, void *data, int *error)
Brijesh Singh59414c92017-12-04 10:57:35 -06004337{
4338 struct fd f;
4339 int ret;
4340
4341 f = fdget(fd);
4342 if (!f.file)
4343 return -EBADF;
4344
4345 ret = sev_issue_cmd_external_user(f.file, id, data, error);
4346
4347 fdput(f);
4348 return ret;
4349}
4350
Brijesh Singh89c50582017-12-04 10:57:35 -06004351static int sev_issue_cmd(struct kvm *kvm, int id, void *data, int *error)
4352{
Sean Christopherson81811c12018-03-20 12:17:21 -07004353 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06004354
4355 return __sev_issue_cmd(sev->fd, id, data, error);
4356}
4357
Brijesh Singh59414c92017-12-04 10:57:35 -06004358static int sev_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp)
4359{
Sean Christopherson81811c12018-03-20 12:17:21 -07004360 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh59414c92017-12-04 10:57:35 -06004361 struct sev_data_launch_start *start;
4362 struct kvm_sev_launch_start params;
4363 void *dh_blob, *session_blob;
4364 int *error = &argp->error;
4365 int ret;
4366
4367 if (!sev_guest(kvm))
4368 return -ENOTTY;
4369
4370 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
4371 return -EFAULT;
4372
Ben Gardon1ec69642019-02-11 11:02:51 -08004373 start = kzalloc(sizeof(*start), GFP_KERNEL_ACCOUNT);
Brijesh Singh59414c92017-12-04 10:57:35 -06004374 if (!start)
4375 return -ENOMEM;
4376
4377 dh_blob = NULL;
4378 if (params.dh_uaddr) {
4379 dh_blob = psp_copy_user_blob(params.dh_uaddr, params.dh_len);
4380 if (IS_ERR(dh_blob)) {
4381 ret = PTR_ERR(dh_blob);
4382 goto e_free;
4383 }
4384
4385 start->dh_cert_address = __sme_set(__pa(dh_blob));
4386 start->dh_cert_len = params.dh_len;
4387 }
4388
4389 session_blob = NULL;
4390 if (params.session_uaddr) {
4391 session_blob = psp_copy_user_blob(params.session_uaddr, params.session_len);
4392 if (IS_ERR(session_blob)) {
4393 ret = PTR_ERR(session_blob);
4394 goto e_free_dh;
4395 }
4396
4397 start->session_address = __sme_set(__pa(session_blob));
4398 start->session_len = params.session_len;
4399 }
4400
4401 start->handle = params.handle;
4402 start->policy = params.policy;
4403
4404 /* create memory encryption context */
Brijesh Singh89c50582017-12-04 10:57:35 -06004405 ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_LAUNCH_START, start, error);
Brijesh Singh59414c92017-12-04 10:57:35 -06004406 if (ret)
4407 goto e_free_session;
4408
4409 /* Bind ASID to this guest */
4410 ret = sev_bind_asid(kvm, start->handle, error);
4411 if (ret)
4412 goto e_free_session;
4413
4414 /* return handle to userspace */
4415 params.handle = start->handle;
4416 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params))) {
4417 sev_unbind_asid(kvm, start->handle);
4418 ret = -EFAULT;
4419 goto e_free_session;
4420 }
4421
4422 sev->handle = start->handle;
4423 sev->fd = argp->sev_fd;
4424
4425e_free_session:
4426 kfree(session_blob);
4427e_free_dh:
4428 kfree(dh_blob);
4429e_free:
4430 kfree(start);
4431 return ret;
4432}
4433
David Rientjesede885e2019-03-19 15:19:56 -07004434static unsigned long get_num_contig_pages(unsigned long idx,
4435 struct page **inpages, unsigned long npages)
Brijesh Singh89c50582017-12-04 10:57:35 -06004436{
4437 unsigned long paddr, next_paddr;
David Rientjesede885e2019-03-19 15:19:56 -07004438 unsigned long i = idx + 1, pages = 1;
Brijesh Singh89c50582017-12-04 10:57:35 -06004439
4440 /* find the number of contiguous pages starting from idx */
4441 paddr = __sme_page_pa(inpages[idx]);
4442 while (i < npages) {
4443 next_paddr = __sme_page_pa(inpages[i++]);
4444 if ((paddr + PAGE_SIZE) == next_paddr) {
4445 pages++;
4446 paddr = next_paddr;
4447 continue;
4448 }
4449 break;
4450 }
4451
4452 return pages;
4453}
4454
4455static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp)
4456{
David Rientjesede885e2019-03-19 15:19:56 -07004457 unsigned long vaddr, vaddr_end, next_vaddr, npages, pages, size, i;
Sean Christopherson81811c12018-03-20 12:17:21 -07004458 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh89c50582017-12-04 10:57:35 -06004459 struct kvm_sev_launch_update_data params;
4460 struct sev_data_launch_update_data *data;
4461 struct page **inpages;
David Rientjesede885e2019-03-19 15:19:56 -07004462 int ret;
Brijesh Singh89c50582017-12-04 10:57:35 -06004463
4464 if (!sev_guest(kvm))
4465 return -ENOTTY;
4466
4467 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
4468 return -EFAULT;
4469
Ben Gardon1ec69642019-02-11 11:02:51 -08004470 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh89c50582017-12-04 10:57:35 -06004471 if (!data)
4472 return -ENOMEM;
4473
4474 vaddr = params.uaddr;
4475 size = params.len;
4476 vaddr_end = vaddr + size;
4477
4478 /* Lock the user memory. */
4479 inpages = sev_pin_memory(kvm, vaddr, size, &npages, 1);
4480 if (!inpages) {
4481 ret = -ENOMEM;
4482 goto e_free;
4483 }
4484
4485 /*
4486 * The LAUNCH_UPDATE command will perform in-place encryption of the
4487 * memory content (i.e it will write the same memory region with C=1).
4488 * It's possible that the cache may contain the data with C=0, i.e.,
4489 * unencrypted so invalidate it first.
4490 */
4491 sev_clflush_pages(inpages, npages);
4492
4493 for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i += pages) {
4494 int offset, len;
4495
4496 /*
4497 * If the user buffer is not page-aligned, calculate the offset
4498 * within the page.
4499 */
4500 offset = vaddr & (PAGE_SIZE - 1);
4501
4502 /* Calculate the number of pages that can be encrypted in one go. */
4503 pages = get_num_contig_pages(i, inpages, npages);
4504
4505 len = min_t(size_t, ((pages * PAGE_SIZE) - offset), size);
4506
4507 data->handle = sev->handle;
4508 data->len = len;
4509 data->address = __sme_page_pa(inpages[i]) + offset;
4510 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_DATA, data, &argp->error);
4511 if (ret)
4512 goto e_unpin;
4513
4514 size -= len;
4515 next_vaddr = vaddr + len;
4516 }
4517
4518e_unpin:
4519 /* content of memory is updated, mark pages dirty */
4520 for (i = 0; i < npages; i++) {
4521 set_page_dirty_lock(inpages[i]);
4522 mark_page_accessed(inpages[i]);
4523 }
4524 /* unlock the user pages */
4525 sev_unpin_memory(kvm, inpages, npages);
4526e_free:
4527 kfree(data);
4528 return ret;
4529}
4530
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004531static int sev_launch_measure(struct kvm *kvm, struct kvm_sev_cmd *argp)
4532{
Brijesh Singh3e233382018-02-23 12:36:50 -06004533 void __user *measure = (void __user *)(uintptr_t)argp->data;
Sean Christopherson81811c12018-03-20 12:17:21 -07004534 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004535 struct sev_data_launch_measure *data;
4536 struct kvm_sev_launch_measure params;
Brijesh Singh3e233382018-02-23 12:36:50 -06004537 void __user *p = NULL;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004538 void *blob = NULL;
4539 int ret;
4540
4541 if (!sev_guest(kvm))
4542 return -ENOTTY;
4543
Brijesh Singh3e233382018-02-23 12:36:50 -06004544 if (copy_from_user(&params, measure, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004545 return -EFAULT;
4546
Ben Gardon1ec69642019-02-11 11:02:51 -08004547 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004548 if (!data)
4549 return -ENOMEM;
4550
4551 /* User wants to query the blob length */
4552 if (!params.len)
4553 goto cmd;
4554
Brijesh Singh3e233382018-02-23 12:36:50 -06004555 p = (void __user *)(uintptr_t)params.uaddr;
4556 if (p) {
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004557 if (params.len > SEV_FW_BLOB_MAX_SIZE) {
4558 ret = -EINVAL;
4559 goto e_free;
4560 }
4561
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004562 ret = -ENOMEM;
4563 blob = kmalloc(params.len, GFP_KERNEL);
4564 if (!blob)
4565 goto e_free;
4566
4567 data->address = __psp_pa(blob);
4568 data->len = params.len;
4569 }
4570
4571cmd:
4572 data->handle = sev->handle;
4573 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_MEASURE, data, &argp->error);
4574
4575 /*
4576 * If we query the session length, FW responded with expected data.
4577 */
4578 if (!params.len)
4579 goto done;
4580
4581 if (ret)
4582 goto e_free_blob;
4583
4584 if (blob) {
Brijesh Singh3e233382018-02-23 12:36:50 -06004585 if (copy_to_user(p, blob, params.len))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004586 ret = -EFAULT;
4587 }
4588
4589done:
4590 params.len = data->len;
Brijesh Singh3e233382018-02-23 12:36:50 -06004591 if (copy_to_user(measure, &params, sizeof(params)))
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004592 ret = -EFAULT;
4593e_free_blob:
4594 kfree(blob);
4595e_free:
4596 kfree(data);
4597 return ret;
4598}
4599
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06004600static int sev_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp)
4601{
Sean Christopherson81811c12018-03-20 12:17:21 -07004602 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06004603 struct sev_data_launch_finish *data;
4604 int ret;
4605
4606 if (!sev_guest(kvm))
4607 return -ENOTTY;
4608
Ben Gardon1ec69642019-02-11 11:02:51 -08004609 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06004610 if (!data)
4611 return -ENOMEM;
4612
4613 data->handle = sev->handle;
4614 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_FINISH, data, &argp->error);
4615
4616 kfree(data);
4617 return ret;
4618}
4619
Brijesh Singh255d9e72017-12-04 10:57:37 -06004620static int sev_guest_status(struct kvm *kvm, struct kvm_sev_cmd *argp)
4621{
Sean Christopherson81811c12018-03-20 12:17:21 -07004622 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh255d9e72017-12-04 10:57:37 -06004623 struct kvm_sev_guest_status params;
4624 struct sev_data_guest_status *data;
4625 int ret;
4626
4627 if (!sev_guest(kvm))
4628 return -ENOTTY;
4629
Ben Gardon1ec69642019-02-11 11:02:51 -08004630 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh255d9e72017-12-04 10:57:37 -06004631 if (!data)
4632 return -ENOMEM;
4633
4634 data->handle = sev->handle;
4635 ret = sev_issue_cmd(kvm, SEV_CMD_GUEST_STATUS, data, &argp->error);
4636 if (ret)
4637 goto e_free;
4638
4639 params.policy = data->policy;
4640 params.state = data->state;
4641 params.handle = data->handle;
4642
4643 if (copy_to_user((void __user *)(uintptr_t)argp->data, &params, sizeof(params)))
4644 ret = -EFAULT;
4645e_free:
4646 kfree(data);
4647 return ret;
4648}
4649
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004650static int __sev_issue_dbg_cmd(struct kvm *kvm, unsigned long src,
4651 unsigned long dst, int size,
4652 int *error, bool enc)
4653{
Sean Christopherson81811c12018-03-20 12:17:21 -07004654 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004655 struct sev_data_dbg *data;
4656 int ret;
4657
Ben Gardon1ec69642019-02-11 11:02:51 -08004658 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004659 if (!data)
4660 return -ENOMEM;
4661
4662 data->handle = sev->handle;
4663 data->dst_addr = dst;
4664 data->src_addr = src;
4665 data->len = size;
4666
4667 ret = sev_issue_cmd(kvm,
4668 enc ? SEV_CMD_DBG_ENCRYPT : SEV_CMD_DBG_DECRYPT,
4669 data, error);
4670 kfree(data);
4671 return ret;
4672}
4673
4674static int __sev_dbg_decrypt(struct kvm *kvm, unsigned long src_paddr,
4675 unsigned long dst_paddr, int sz, int *err)
4676{
4677 int offset;
4678
4679 /*
4680 * Its safe to read more than we are asked, caller should ensure that
4681 * destination has enough space.
4682 */
4683 src_paddr = round_down(src_paddr, 16);
4684 offset = src_paddr & 15;
4685 sz = round_up(sz + offset, 16);
4686
4687 return __sev_issue_dbg_cmd(kvm, src_paddr, dst_paddr, sz, err, false);
4688}
4689
4690static int __sev_dbg_decrypt_user(struct kvm *kvm, unsigned long paddr,
4691 unsigned long __user dst_uaddr,
4692 unsigned long dst_paddr,
4693 int size, int *err)
4694{
4695 struct page *tpage = NULL;
4696 int ret, offset;
4697
4698 /* if inputs are not 16-byte then use intermediate buffer */
4699 if (!IS_ALIGNED(dst_paddr, 16) ||
4700 !IS_ALIGNED(paddr, 16) ||
4701 !IS_ALIGNED(size, 16)) {
4702 tpage = (void *)alloc_page(GFP_KERNEL);
4703 if (!tpage)
4704 return -ENOMEM;
4705
4706 dst_paddr = __sme_page_pa(tpage);
4707 }
4708
4709 ret = __sev_dbg_decrypt(kvm, paddr, dst_paddr, size, err);
4710 if (ret)
4711 goto e_free;
4712
4713 if (tpage) {
4714 offset = paddr & 15;
4715 if (copy_to_user((void __user *)(uintptr_t)dst_uaddr,
4716 page_address(tpage) + offset, size))
4717 ret = -EFAULT;
4718 }
4719
4720e_free:
4721 if (tpage)
4722 __free_page(tpage);
4723
4724 return ret;
4725}
4726
Brijesh Singh7d1594f2017-12-04 10:57:37 -06004727static int __sev_dbg_encrypt_user(struct kvm *kvm, unsigned long paddr,
4728 unsigned long __user vaddr,
4729 unsigned long dst_paddr,
4730 unsigned long __user dst_vaddr,
4731 int size, int *error)
4732{
4733 struct page *src_tpage = NULL;
4734 struct page *dst_tpage = NULL;
4735 int ret, len = size;
4736
4737 /* If source buffer is not aligned then use an intermediate buffer */
4738 if (!IS_ALIGNED(vaddr, 16)) {
4739 src_tpage = alloc_page(GFP_KERNEL);
4740 if (!src_tpage)
4741 return -ENOMEM;
4742
4743 if (copy_from_user(page_address(src_tpage),
4744 (void __user *)(uintptr_t)vaddr, size)) {
4745 __free_page(src_tpage);
4746 return -EFAULT;
4747 }
4748
4749 paddr = __sme_page_pa(src_tpage);
4750 }
4751
4752 /*
4753 * If destination buffer or length is not aligned then do read-modify-write:
4754 * - decrypt destination in an intermediate buffer
4755 * - copy the source buffer in an intermediate buffer
4756 * - use the intermediate buffer as source buffer
4757 */
4758 if (!IS_ALIGNED(dst_vaddr, 16) || !IS_ALIGNED(size, 16)) {
4759 int dst_offset;
4760
4761 dst_tpage = alloc_page(GFP_KERNEL);
4762 if (!dst_tpage) {
4763 ret = -ENOMEM;
4764 goto e_free;
4765 }
4766
4767 ret = __sev_dbg_decrypt(kvm, dst_paddr,
4768 __sme_page_pa(dst_tpage), size, error);
4769 if (ret)
4770 goto e_free;
4771
4772 /*
4773 * If source is kernel buffer then use memcpy() otherwise
4774 * copy_from_user().
4775 */
4776 dst_offset = dst_paddr & 15;
4777
4778 if (src_tpage)
4779 memcpy(page_address(dst_tpage) + dst_offset,
4780 page_address(src_tpage), size);
4781 else {
4782 if (copy_from_user(page_address(dst_tpage) + dst_offset,
4783 (void __user *)(uintptr_t)vaddr, size)) {
4784 ret = -EFAULT;
4785 goto e_free;
4786 }
4787 }
4788
4789 paddr = __sme_page_pa(dst_tpage);
4790 dst_paddr = round_down(dst_paddr, 16);
4791 len = round_up(size, 16);
4792 }
4793
4794 ret = __sev_issue_dbg_cmd(kvm, paddr, dst_paddr, len, error, true);
4795
4796e_free:
4797 if (src_tpage)
4798 __free_page(src_tpage);
4799 if (dst_tpage)
4800 __free_page(dst_tpage);
4801 return ret;
4802}
4803
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004804static int sev_dbg_crypt(struct kvm *kvm, struct kvm_sev_cmd *argp, bool dec)
4805{
4806 unsigned long vaddr, vaddr_end, next_vaddr;
Colin Ian King0186ec82018-08-28 16:22:28 +01004807 unsigned long dst_vaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004808 struct page **src_p, **dst_p;
4809 struct kvm_sev_dbg debug;
4810 unsigned long n;
David Rientjesb86bc282019-03-25 11:47:31 -07004811 unsigned int size;
4812 int ret;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004813
4814 if (!sev_guest(kvm))
4815 return -ENOTTY;
4816
4817 if (copy_from_user(&debug, (void __user *)(uintptr_t)argp->data, sizeof(debug)))
4818 return -EFAULT;
4819
David Rientjesb86bc282019-03-25 11:47:31 -07004820 if (!debug.len || debug.src_uaddr + debug.len < debug.src_uaddr)
4821 return -EINVAL;
4822 if (!debug.dst_uaddr)
4823 return -EINVAL;
4824
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004825 vaddr = debug.src_uaddr;
4826 size = debug.len;
4827 vaddr_end = vaddr + size;
4828 dst_vaddr = debug.dst_uaddr;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004829
4830 for (; vaddr < vaddr_end; vaddr = next_vaddr) {
4831 int len, s_off, d_off;
4832
4833 /* lock userspace source and destination page */
4834 src_p = sev_pin_memory(kvm, vaddr & PAGE_MASK, PAGE_SIZE, &n, 0);
4835 if (!src_p)
4836 return -EFAULT;
4837
4838 dst_p = sev_pin_memory(kvm, dst_vaddr & PAGE_MASK, PAGE_SIZE, &n, 1);
4839 if (!dst_p) {
4840 sev_unpin_memory(kvm, src_p, n);
4841 return -EFAULT;
4842 }
4843
4844 /*
4845 * The DBG_{DE,EN}CRYPT commands will perform {dec,en}cryption of the
4846 * memory content (i.e it will write the same memory region with C=1).
4847 * It's possible that the cache may contain the data with C=0, i.e.,
4848 * unencrypted so invalidate it first.
4849 */
4850 sev_clflush_pages(src_p, 1);
4851 sev_clflush_pages(dst_p, 1);
4852
4853 /*
4854 * Since user buffer may not be page aligned, calculate the
4855 * offset within the page.
4856 */
4857 s_off = vaddr & ~PAGE_MASK;
4858 d_off = dst_vaddr & ~PAGE_MASK;
4859 len = min_t(size_t, (PAGE_SIZE - s_off), size);
4860
Brijesh Singh7d1594f2017-12-04 10:57:37 -06004861 if (dec)
4862 ret = __sev_dbg_decrypt_user(kvm,
4863 __sme_page_pa(src_p[0]) + s_off,
4864 dst_vaddr,
4865 __sme_page_pa(dst_p[0]) + d_off,
4866 len, &argp->error);
4867 else
4868 ret = __sev_dbg_encrypt_user(kvm,
4869 __sme_page_pa(src_p[0]) + s_off,
4870 vaddr,
4871 __sme_page_pa(dst_p[0]) + d_off,
4872 dst_vaddr,
4873 len, &argp->error);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004874
David Rientjesb86bc282019-03-25 11:47:31 -07004875 sev_unpin_memory(kvm, src_p, n);
4876 sev_unpin_memory(kvm, dst_p, n);
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004877
4878 if (ret)
4879 goto err;
4880
4881 next_vaddr = vaddr + len;
4882 dst_vaddr = dst_vaddr + len;
4883 size -= len;
4884 }
4885err:
4886 return ret;
4887}
4888
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004889static int sev_launch_secret(struct kvm *kvm, struct kvm_sev_cmd *argp)
4890{
Sean Christopherson81811c12018-03-20 12:17:21 -07004891 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004892 struct sev_data_launch_secret *data;
4893 struct kvm_sev_launch_secret params;
4894 struct page **pages;
4895 void *blob, *hdr;
4896 unsigned long n;
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06004897 int ret, offset;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004898
4899 if (!sev_guest(kvm))
4900 return -ENOTTY;
4901
4902 if (copy_from_user(&params, (void __user *)(uintptr_t)argp->data, sizeof(params)))
4903 return -EFAULT;
4904
4905 pages = sev_pin_memory(kvm, params.guest_uaddr, params.guest_len, &n, 1);
4906 if (!pages)
4907 return -ENOMEM;
4908
4909 /*
4910 * The secret must be copied into contiguous memory region, lets verify
4911 * that userspace memory pages are contiguous before we issue command.
4912 */
4913 if (get_num_contig_pages(0, pages, n) != n) {
4914 ret = -EINVAL;
4915 goto e_unpin_memory;
4916 }
4917
4918 ret = -ENOMEM;
Ben Gardon1ec69642019-02-11 11:02:51 -08004919 data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT);
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004920 if (!data)
4921 goto e_unpin_memory;
4922
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06004923 offset = params.guest_uaddr & (PAGE_SIZE - 1);
4924 data->guest_address = __sme_page_pa(pages[0]) + offset;
4925 data->guest_len = params.guest_len;
4926
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004927 blob = psp_copy_user_blob(params.trans_uaddr, params.trans_len);
4928 if (IS_ERR(blob)) {
4929 ret = PTR_ERR(blob);
4930 goto e_free;
4931 }
4932
4933 data->trans_address = __psp_pa(blob);
4934 data->trans_len = params.trans_len;
4935
4936 hdr = psp_copy_user_blob(params.hdr_uaddr, params.hdr_len);
4937 if (IS_ERR(hdr)) {
4938 ret = PTR_ERR(hdr);
4939 goto e_free_blob;
4940 }
Brijesh Singh9c5e0af2018-02-19 10:13:25 -06004941 data->hdr_address = __psp_pa(hdr);
4942 data->hdr_len = params.hdr_len;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004943
4944 data->handle = sev->handle;
4945 ret = sev_issue_cmd(kvm, SEV_CMD_LAUNCH_UPDATE_SECRET, data, &argp->error);
4946
4947 kfree(hdr);
4948
4949e_free_blob:
4950 kfree(blob);
4951e_free:
4952 kfree(data);
4953e_unpin_memory:
4954 sev_unpin_memory(kvm, pages, n);
4955 return ret;
4956}
4957
Brijesh Singh1654efc2017-12-04 10:57:34 -06004958static int svm_mem_enc_op(struct kvm *kvm, void __user *argp)
4959{
4960 struct kvm_sev_cmd sev_cmd;
4961 int r;
4962
4963 if (!svm_sev_enabled())
4964 return -ENOTTY;
4965
Paolo Bonzini2da1ed62020-03-20 13:34:50 -04004966 if (!argp)
4967 return 0;
4968
Brijesh Singh1654efc2017-12-04 10:57:34 -06004969 if (copy_from_user(&sev_cmd, argp, sizeof(struct kvm_sev_cmd)))
4970 return -EFAULT;
4971
4972 mutex_lock(&kvm->lock);
4973
4974 switch (sev_cmd.id) {
4975 case KVM_SEV_INIT:
4976 r = sev_guest_init(kvm, &sev_cmd);
4977 break;
Brijesh Singh59414c92017-12-04 10:57:35 -06004978 case KVM_SEV_LAUNCH_START:
4979 r = sev_launch_start(kvm, &sev_cmd);
4980 break;
Brijesh Singh89c50582017-12-04 10:57:35 -06004981 case KVM_SEV_LAUNCH_UPDATE_DATA:
4982 r = sev_launch_update_data(kvm, &sev_cmd);
4983 break;
Brijesh Singh0d0736f2017-12-04 10:57:36 -06004984 case KVM_SEV_LAUNCH_MEASURE:
4985 r = sev_launch_measure(kvm, &sev_cmd);
4986 break;
Brijesh Singh5bdb0e22017-12-04 10:57:36 -06004987 case KVM_SEV_LAUNCH_FINISH:
4988 r = sev_launch_finish(kvm, &sev_cmd);
4989 break;
Brijesh Singh255d9e72017-12-04 10:57:37 -06004990 case KVM_SEV_GUEST_STATUS:
4991 r = sev_guest_status(kvm, &sev_cmd);
4992 break;
Brijesh Singh24f41fb2017-12-04 10:57:37 -06004993 case KVM_SEV_DBG_DECRYPT:
4994 r = sev_dbg_crypt(kvm, &sev_cmd, true);
4995 break;
Brijesh Singh7d1594f2017-12-04 10:57:37 -06004996 case KVM_SEV_DBG_ENCRYPT:
4997 r = sev_dbg_crypt(kvm, &sev_cmd, false);
4998 break;
Brijesh Singh9f5b5b92017-12-04 10:57:38 -06004999 case KVM_SEV_LAUNCH_SECRET:
5000 r = sev_launch_secret(kvm, &sev_cmd);
5001 break;
Brijesh Singh1654efc2017-12-04 10:57:34 -06005002 default:
5003 r = -EINVAL;
5004 goto out;
5005 }
5006
5007 if (copy_to_user(argp, &sev_cmd, sizeof(struct kvm_sev_cmd)))
5008 r = -EFAULT;
5009
5010out:
5011 mutex_unlock(&kvm->lock);
5012 return r;
5013}
5014
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005015static int svm_register_enc_region(struct kvm *kvm,
5016 struct kvm_enc_region *range)
5017{
Sean Christopherson81811c12018-03-20 12:17:21 -07005018 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005019 struct enc_region *region;
5020 int ret = 0;
5021
5022 if (!sev_guest(kvm))
5023 return -ENOTTY;
5024
Dan Carpenter86bf20c2018-05-19 09:01:36 +03005025 if (range->addr > ULONG_MAX || range->size > ULONG_MAX)
5026 return -EINVAL;
5027
Ben Gardon1ec69642019-02-11 11:02:51 -08005028 region = kzalloc(sizeof(*region), GFP_KERNEL_ACCOUNT);
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005029 if (!region)
5030 return -ENOMEM;
5031
5032 region->pages = sev_pin_memory(kvm, range->addr, range->size, &region->npages, 1);
5033 if (!region->pages) {
5034 ret = -ENOMEM;
5035 goto e_free;
5036 }
5037
5038 /*
5039 * The guest may change the memory encryption attribute from C=0 -> C=1
5040 * or vice versa for this memory range. Lets make sure caches are
5041 * flushed to ensure that guest data gets written into memory with
5042 * correct C-bit.
5043 */
5044 sev_clflush_pages(region->pages, region->npages);
5045
5046 region->uaddr = range->addr;
5047 region->size = range->size;
5048
5049 mutex_lock(&kvm->lock);
5050 list_add_tail(&region->list, &sev->regions_list);
5051 mutex_unlock(&kvm->lock);
5052
5053 return ret;
5054
5055e_free:
5056 kfree(region);
5057 return ret;
5058}
5059
5060static struct enc_region *
5061find_enc_region(struct kvm *kvm, struct kvm_enc_region *range)
5062{
Sean Christopherson81811c12018-03-20 12:17:21 -07005063 struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info;
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005064 struct list_head *head = &sev->regions_list;
5065 struct enc_region *i;
5066
5067 list_for_each_entry(i, head, list) {
5068 if (i->uaddr == range->addr &&
5069 i->size == range->size)
5070 return i;
5071 }
5072
5073 return NULL;
5074}
5075
5076
5077static int svm_unregister_enc_region(struct kvm *kvm,
5078 struct kvm_enc_region *range)
5079{
5080 struct enc_region *region;
5081 int ret;
5082
5083 mutex_lock(&kvm->lock);
5084
5085 if (!sev_guest(kvm)) {
5086 ret = -ENOTTY;
5087 goto failed;
5088 }
5089
5090 region = find_enc_region(kvm, range);
5091 if (!region) {
5092 ret = -EINVAL;
5093 goto failed;
5094 }
5095
Tom Lendacky2e2409a2020-03-20 11:07:07 -05005096 /*
5097 * Ensure that all guest tagged cache entries are flushed before
5098 * releasing the pages back to the system for use. CLFLUSH will
5099 * not do this, so issue a WBINVD.
5100 */
5101 wbinvd_on_all_cpus();
5102
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005103 __unregister_enc_region_locked(kvm, region);
5104
5105 mutex_unlock(&kvm->lock);
5106 return 0;
5107
5108failed:
5109 mutex_unlock(&kvm->lock);
5110 return ret;
5111}
5112
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005113static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
5114{
Liran Alon118154b2019-07-17 02:56:58 +03005115 unsigned long cr4 = kvm_read_cr4(vcpu);
5116 bool smep = cr4 & X86_CR4_SMEP;
5117 bool smap = cr4 & X86_CR4_SMAP;
5118 bool is_user = svm_get_cpl(vcpu) == 3;
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005119
5120 /*
Liran Alon118154b2019-07-17 02:56:58 +03005121 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
5122 *
5123 * Errata:
5124 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
5125 * possible that CPU microcode implementing DecodeAssist will fail
5126 * to read bytes of instruction which caused #NPF. In this case,
5127 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
5128 * return 0 instead of the correct guest instruction bytes.
5129 *
5130 * This happens because CPU microcode reading instruction bytes
5131 * uses a special opcode which attempts to read data using CPL=0
5132 * priviledges. The microcode reads CS:RIP and if it hits a SMAP
5133 * fault, it gives up and returns no instruction bytes.
5134 *
5135 * Detection:
5136 * We reach here in case CPU supports DecodeAssist, raised #NPF and
5137 * returned 0 in GuestIntrBytes field of the VMCB.
5138 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
5139 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
5140 * in case vCPU CPL==3 (Because otherwise guest would have triggered
5141 * a SMEP fault instead of #NPF).
5142 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
5143 * As most guests enable SMAP if they have also enabled SMEP, use above
5144 * logic in order to attempt minimize false-positive of detecting errata
5145 * while still preserving all cases semantic correctness.
5146 *
5147 * Workaround:
5148 * To determine what instruction the guest was executing, the hypervisor
5149 * will have to decode the instruction at the instruction pointer.
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005150 *
5151 * In non SEV guest, hypervisor will be able to read the guest
5152 * memory to decode the instruction pointer when insn_len is zero
5153 * so we return true to indicate that decoding is possible.
5154 *
5155 * But in the SEV guest, the guest memory is encrypted with the
5156 * guest specific key and hypervisor will not be able to decode the
5157 * instruction pointer so we will not able to workaround it. Lets
5158 * print the error and request to kill the guest.
5159 */
Liran Alon118154b2019-07-17 02:56:58 +03005160 if (smap && (!smep || is_user)) {
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005161 if (!sev_guest(vcpu->kvm))
5162 return true;
5163
Liran Alon118154b2019-07-17 02:56:58 +03005164 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005165 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
5166 }
5167
5168 return false;
5169}
5170
Liran Alon4b9852f2019-08-26 13:24:49 +03005171static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
5172{
5173 struct vcpu_svm *svm = to_svm(vcpu);
5174
5175 /*
5176 * TODO: Last condition latch INIT signals on vCPU when
5177 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
5178 * To properly emulate the INIT intercept, SVM should implement
Sean Christophersonafaf0b22020-03-21 13:26:00 -07005179 * kvm_x86_ops.check_nested_events() and call nested_svm_vmexit()
Liran Alon4b9852f2019-08-26 13:24:49 +03005180 * there if an INIT signal is pending.
5181 */
5182 return !gif_set(svm) ||
5183 (svm->vmcb->control.intercept & (1ULL << INTERCEPT_INIT));
5184}
5185
Sean Christopherson9c14ee22020-03-21 13:26:03 -07005186static struct kvm_x86_ops svm_x86_ops __initdata = {
Li RongQingdd58f3c2020-02-23 16:13:12 +08005187 .hardware_unsetup = svm_hardware_teardown,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005188 .hardware_enable = svm_hardware_enable,
5189 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02005190 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Tom Lendackybc226f02018-05-10 22:06:39 +02005191 .has_emulated_msr = svm_has_emulated_msr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005192
5193 .vcpu_create = svm_create_vcpu,
5194 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005195 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005196
Sean Christopherson562b6b02020-01-26 16:41:13 -08005197 .vm_size = sizeof(struct kvm_svm),
Suravee Suthikulpanit4e19c362019-11-14 14:15:05 -06005198 .vm_init = svm_vm_init,
Brijesh Singh1654efc2017-12-04 10:57:34 -06005199 .vm_destroy = svm_vm_destroy,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005200
Avi Kivity04d2cc72007-09-10 18:10:54 +03005201 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005202 .vcpu_load = svm_vcpu_load,
5203 .vcpu_put = svm_vcpu_put,
Suravee Suthikulpanit8221c132016-05-04 14:09:52 -05005204 .vcpu_blocking = svm_vcpu_blocking,
5205 .vcpu_unblocking = svm_vcpu_unblocking,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005206
Paolo Bonzinia96036b2015-11-10 11:55:36 +01005207 .update_bp_intercept = update_bp_intercept,
Tom Lendacky801e4592018-02-21 13:39:51 -06005208 .get_msr_feature = svm_get_msr_feature,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005209 .get_msr = svm_get_msr,
5210 .set_msr = svm_set_msr,
5211 .get_segment_base = svm_get_segment_base,
5212 .get_segment = svm_get_segment,
5213 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02005214 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10005215 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02005216 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03005217 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005218 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005219 .set_cr4 = svm_set_cr4,
5220 .set_efer = svm_set_efer,
5221 .get_idt = svm_get_idt,
5222 .set_idt = svm_set_idt,
5223 .get_gdt = svm_get_gdt,
5224 .set_gdt = svm_set_gdt,
Jan Kiszka73aaf249e2014-01-04 18:47:16 +01005225 .get_dr6 = svm_get_dr6,
5226 .set_dr6 = svm_set_dr6,
Gleb Natapov020df072010-04-13 10:05:23 +03005227 .set_dr7 = svm_set_dr7,
Paolo Bonzinifacb0132014-02-21 10:32:27 +01005228 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03005229 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005230 .get_rflags = svm_get_rflags,
5231 .set_rflags = svm_set_rflags,
Huaitong Hanbe94f6b2016-03-22 16:51:20 +08005232
Avi Kivity6aa8b732006-12-10 02:21:36 -08005233 .tlb_flush = svm_flush_tlb,
Junaid Shahidfaff8752018-06-29 13:10:05 -07005234 .tlb_flush_gva = svm_flush_tlb_gva,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005235
Avi Kivity6aa8b732006-12-10 02:21:36 -08005236 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03005237 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005238 .skip_emulated_instruction = skip_emulated_instruction,
Oliver Upton5ef8acb2020-02-07 02:36:07 -08005239 .update_emulated_instruction = NULL,
Glauber Costa2809f5d2009-05-12 16:21:05 -04005240 .set_interrupt_shadow = svm_set_interrupt_shadow,
5241 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02005242 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03005243 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005244 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02005245 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03005246 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02005247 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005248 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01005249 .get_nmi_mask = svm_get_nmi_mask,
5250 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03005251 .enable_nmi_window = enable_nmi_window,
5252 .enable_irq_window = enable_irq_window,
5253 .update_cr8_intercept = update_cr8_intercept,
Jim Mattson8d860bb2018-05-09 16:56:05 -04005254 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
Andrey Smetanind62caab2015-11-10 15:36:33 +03005255 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
Suravee Suthikulpanitef8efd72019-11-14 14:15:10 -06005256 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
Suravee Suthikulpanit2de9d0c2019-11-14 14:15:11 -06005257 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
Yang Zhangc7c9c562013-01-25 10:18:51 +08005258 .load_eoi_exitmap = svm_load_eoi_exitmap,
Suravee Suthikulpanit44a95da2016-05-04 14:09:46 -05005259 .hwapic_irr_update = svm_hwapic_irr_update,
5260 .hwapic_isr_update = svm_hwapic_isr_update,
Liran Alonfa59cc02017-12-24 18:12:53 +02005261 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
Suravee Suthikulpanitbe8ca172016-05-04 14:09:49 -05005262 .apicv_post_state_restore = avic_post_state_restore,
Izik Eiduscbc94022007-10-25 00:29:55 +02005263
5264 .set_tss_addr = svm_set_tss_addr,
Sean Christopherson2ac52ab2018-03-20 12:17:19 -07005265 .set_identity_map_addr = svm_set_identity_map_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08005266 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08005267 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03005268
Avi Kivity586f9602010-11-18 13:09:54 +02005269 .get_exit_info = svm_get_exit_info,
Avi Kivity586f9602010-11-18 13:09:54 +02005270
Sheng Yang0e851882009-12-18 16:48:46 +08005271 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08005272
Sheng Yangf5f48ee2010-06-30 12:25:15 +08005273 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10005274
KarimAllah Ahmede79f2452018-04-14 05:10:52 +02005275 .read_l1_tsc_offset = svm_read_l1_tsc_offset,
Leonid Shatz326e7422018-11-06 12:14:25 +02005276 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02005277
Paolo Bonzini727a7e22020-03-05 03:52:50 -05005278 .load_mmu_pgd = svm_load_mmu_pgd,
Joerg Roedel8a76d7f2011-04-04 12:39:27 +02005279
5280 .check_intercept = svm_check_intercept,
Sean Christopherson95b5a482019-04-19 22:50:59 -07005281 .handle_exit_irqoff = svm_handle_exit_irqoff,
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005282
Sean Christophersond264ee02018-08-27 15:21:12 -07005283 .request_immediate_exit = __kvm_request_immediate_exit,
5284
Radim Krčmářae97a3b2014-08-21 18:08:06 +02005285 .sched_in = svm_sched_in,
Wei Huang25462f72015-06-19 15:45:05 +02005286
5287 .pmu_ops = &amd_pmu_ops,
Suravee Suthikulpanit340d3bc2016-05-04 14:09:47 -05005288 .deliver_posted_interrupt = svm_deliver_avic_intr,
Wanpeng Li17e433b2019-08-05 10:03:19 +08005289 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
Suravee Suthikulpanit411b44b2016-08-23 13:52:43 -05005290 .update_pi_irte = svm_update_pi_irte,
Borislav Petkov74f16902017-03-26 23:51:24 +02005291 .setup_mce = svm_setup_mce,
Ladi Prosek0234bf82017-10-11 16:54:40 +02005292
Ladi Prosek72d7b372017-10-11 16:54:41 +02005293 .smi_allowed = svm_smi_allowed,
Ladi Prosek0234bf82017-10-11 16:54:40 +02005294 .pre_enter_smm = svm_pre_enter_smm,
5295 .pre_leave_smm = svm_pre_leave_smm,
Ladi Prosekcc3d9672017-10-17 16:02:39 +02005296 .enable_smi_window = enable_smi_window,
Brijesh Singh1654efc2017-12-04 10:57:34 -06005297
5298 .mem_enc_op = svm_mem_enc_op,
Brijesh Singh1e80fdc2017-12-04 10:57:38 -06005299 .mem_enc_reg_region = svm_register_enc_region,
5300 .mem_enc_unreg_region = svm_unregister_enc_region,
Vitaly Kuznetsov57b119d2018-10-16 18:50:01 +02005301
Vitaly Kuznetsov956e2552019-08-28 09:59:04 +02005302 .nested_enable_evmcs = NULL,
Vitaly Kuznetsovea152982019-08-27 18:04:02 +02005303 .nested_get_evmcs_version = NULL,
Singh, Brijesh05d5a482019-02-15 17:24:12 +00005304
5305 .need_emulation_on_page_fault = svm_need_emulation_on_page_fault,
Liran Alon4b9852f2019-08-26 13:24:49 +03005306
5307 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
Paolo Bonzinib518ba92020-03-04 16:46:47 -05005308
5309 .check_nested_events = svm_check_nested_events,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005310};
5311
Sean Christophersond008dfd2020-03-21 13:25:56 -07005312static struct kvm_x86_init_ops svm_init_ops __initdata = {
5313 .cpu_has_kvm_support = has_svm,
5314 .disabled_by_bios = is_disabled,
5315 .hardware_setup = svm_hardware_setup,
5316 .check_processor_compatibility = svm_check_processor_compat,
5317
5318 .runtime_ops = &svm_x86_ops,
Avi Kivity6aa8b732006-12-10 02:21:36 -08005319};
5320
5321static int __init svm_init(void)
5322{
Sean Christophersond008dfd2020-03-21 13:25:56 -07005323 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03005324 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08005325}
5326
5327static void __exit svm_exit(void)
5328{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08005329 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08005330}
5331
5332module_init(svm_init)
5333module_exit(svm_exit)