1 #define pr_fmt(fmt) "SVM: " fmt
3 #include <linux/kvm_host.h>
7 #include "kvm_cache_regs.h"
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/kernel.h>
15 #include <linux/vmalloc.h>
16 #include <linux/highmem.h>
17 #include <linux/amd-iommu.h>
18 #include <linux/sched.h>
19 #include <linux/trace_events.h>
20 #include <linux/slab.h>
21 #include <linux/hashtable.h>
22 #include <linux/objtool.h>
23 #include <linux/psp-sev.h>
24 #include <linux/file.h>
25 #include <linux/pagemap.h>
26 #include <linux/swap.h>
27 #include <linux/rwsem.h>
30 #include <asm/perf_event.h>
31 #include <asm/tlbflush.h>
33 #include <asm/debugreg.h>
34 #include <asm/kvm_para.h>
35 #include <asm/irq_remapping.h>
36 #include <asm/spec-ctrl.h>
37 #include <asm/cpu_device_id.h>
38 #include <asm/traps.h>
40 #include <asm/virtext.h>
46 #define __ex(x) __kvm_handle_fault_on_reboot(x)
48 MODULE_AUTHOR("Qumranet");
49 MODULE_LICENSE("GPL");
52 static const struct x86_cpu_id svm_cpu_id[] = {
53 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
56 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
59 #define SEG_TYPE_LDT 2
60 #define SEG_TYPE_BUSY_TSS16 3
62 #define SVM_FEATURE_LBRV (1 << 1)
63 #define SVM_FEATURE_SVML (1 << 2)
64 #define SVM_FEATURE_TSC_RATE (1 << 4)
65 #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
66 #define SVM_FEATURE_FLUSH_ASID (1 << 6)
67 #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
68 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
70 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
72 #define TSC_RATIO_RSVD 0xffffff0000000000ULL
73 #define TSC_RATIO_MIN 0x0000000000000001ULL
74 #define TSC_RATIO_MAX 0x000000ffffffffffULL
76 static bool erratum_383_found __read_mostly;
78 u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
81 * Set osvw_len to higher value when updated Revision Guides
82 * are published and we know what the new status bits are
84 static uint64_t osvw_len = 4, osvw_status;
86 static DEFINE_PER_CPU(u64, current_tsc_ratio);
87 #define TSC_RATIO_DEFAULT 0x0100000000ULL
89 static const struct svm_direct_access_msrs {
90 u32 index; /* Index of the MSR */
91 bool always; /* True if intercept is initially cleared */
92 } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
93 { .index = MSR_STAR, .always = true },
94 { .index = MSR_IA32_SYSENTER_CS, .always = true },
95 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
96 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
98 { .index = MSR_GS_BASE, .always = true },
99 { .index = MSR_FS_BASE, .always = true },
100 { .index = MSR_KERNEL_GS_BASE, .always = true },
101 { .index = MSR_LSTAR, .always = true },
102 { .index = MSR_CSTAR, .always = true },
103 { .index = MSR_SYSCALL_MASK, .always = true },
105 { .index = MSR_IA32_SPEC_CTRL, .always = false },
106 { .index = MSR_IA32_PRED_CMD, .always = false },
107 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
108 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
109 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
110 { .index = MSR_IA32_LASTINTTOIP, .always = false },
111 { .index = MSR_EFER, .always = false },
112 { .index = MSR_IA32_CR_PAT, .always = false },
113 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
114 { .index = MSR_INVALID, .always = false },
118 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
119 * pause_filter_count: On processors that support Pause filtering(indicated
120 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
121 * count value. On VMRUN this value is loaded into an internal counter.
122 * Each time a pause instruction is executed, this counter is decremented
123 * until it reaches zero at which time a #VMEXIT is generated if pause
124 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
125 * Intercept Filtering for more details.
126 * This also indicate if ple logic enabled.
128 * pause_filter_thresh: In addition, some processor families support advanced
129 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
130 * the amount of time a guest is allowed to execute in a pause loop.
131 * In this mode, a 16-bit pause filter threshold field is added in the
132 * VMCB. The threshold value is a cycle count that is used to reset the
133 * pause counter. As with simple pause filtering, VMRUN loads the pause
134 * count value from VMCB into an internal counter. Then, on each pause
135 * instruction the hardware checks the elapsed number of cycles since
136 * the most recent pause instruction against the pause filter threshold.
137 * If the elapsed cycle count is greater than the pause filter threshold,
138 * then the internal pause count is reloaded from the VMCB and execution
139 * continues. If the elapsed cycle count is less than the pause filter
140 * threshold, then the internal pause count is decremented. If the count
141 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
142 * triggered. If advanced pause filtering is supported and pause filter
143 * threshold field is set to zero, the filter will operate in the simpler,
147 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
148 module_param(pause_filter_thresh, ushort, 0444);
150 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
151 module_param(pause_filter_count, ushort, 0444);
153 /* Default doubles per-vcpu window every exit. */
154 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
155 module_param(pause_filter_count_grow, ushort, 0444);
157 /* Default resets per-vcpu window every exit to pause_filter_count. */
158 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
159 module_param(pause_filter_count_shrink, ushort, 0444);
161 /* Default is to compute the maximum so we can never overflow. */
162 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
163 module_param(pause_filter_count_max, ushort, 0444);
166 * Use nested page tables by default. Note, NPT may get forced off by
167 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
169 bool npt_enabled = true;
170 module_param_named(npt, npt_enabled, bool, 0444);
172 /* allow nested virtualization in KVM/SVM */
173 static int nested = true;
174 module_param(nested, int, S_IRUGO);
176 /* enable/disable Next RIP Save */
177 static int nrips = true;
178 module_param(nrips, int, 0444);
180 /* enable/disable Virtual VMLOAD VMSAVE */
181 static int vls = true;
182 module_param(vls, int, 0444);
184 /* enable/disable Virtual GIF */
185 static int vgif = true;
186 module_param(vgif, int, 0444);
188 bool __read_mostly dump_invalid_vmcb;
189 module_param(dump_invalid_vmcb, bool, 0644);
191 static bool svm_gp_erratum_intercept = true;
193 static u8 rsm_ins_bytes[] = "\x0f\xaa";
195 static unsigned long iopm_base;
197 struct kvm_ldttss_desc {
200 unsigned base1:8, type:5, dpl:2, p:1;
201 unsigned limit1:4, zero0:3, g:1, base2:8;
204 } __attribute__((packed));
206 DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
209 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
210 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
212 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
213 * defer the restoration of TSC_AUX until the CPU returns to userspace.
215 static int tsc_aux_uret_slot __read_mostly = -1;
217 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
219 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
220 #define MSRS_RANGE_SIZE 2048
221 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
223 u32 svm_msrpm_offset(u32 msr)
228 for (i = 0; i < NUM_MSR_MAPS; i++) {
229 if (msr < msrpm_ranges[i] ||
230 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
233 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
234 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
236 /* Now we have the u8 offset - but need the u32 offset */
240 /* MSR not in any range */
244 #define MAX_INST_SIZE 15
246 static int get_max_npt_level(void)
249 return PT64_ROOT_4LEVEL;
251 return PT32E_ROOT_LEVEL;
255 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
257 struct vcpu_svm *svm = to_svm(vcpu);
258 u64 old_efer = vcpu->arch.efer;
259 vcpu->arch.efer = efer;
262 /* Shadow paging assumes NX to be available. */
265 if (!(efer & EFER_LMA))
269 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
270 if (!(efer & EFER_SVME)) {
271 svm_leave_nested(svm);
272 svm_set_gif(svm, true);
273 /* #GP intercept is still needed for vmware backdoor */
274 if (!enable_vmware_backdoor)
275 clr_exception_intercept(svm, GP_VECTOR);
278 * Free the nested guest state, unless we are in SMM.
279 * In this case we will return to the nested guest
280 * as soon as we leave SMM.
283 svm_free_nested(svm);
286 int ret = svm_allocate_nested(svm);
289 vcpu->arch.efer = old_efer;
293 if (svm_gp_erratum_intercept)
294 set_exception_intercept(svm, GP_VECTOR);
298 svm->vmcb->save.efer = efer | EFER_SVME;
299 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
303 static int is_external_interrupt(u32 info)
305 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
306 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
309 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
311 struct vcpu_svm *svm = to_svm(vcpu);
314 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
315 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
319 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
321 struct vcpu_svm *svm = to_svm(vcpu);
324 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
326 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
330 static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
332 struct vcpu_svm *svm = to_svm(vcpu);
335 * SEV-ES does not expose the next RIP. The RIP update is controlled by
336 * the type of exit and the #VC handler in the guest.
338 if (sev_es_guest(vcpu->kvm))
341 if (nrips && svm->vmcb->control.next_rip != 0) {
342 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
343 svm->next_rip = svm->vmcb->control.next_rip;
346 if (!svm->next_rip) {
347 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
350 kvm_rip_write(vcpu, svm->next_rip);
354 svm_set_interrupt_shadow(vcpu, 0);
359 static void svm_queue_exception(struct kvm_vcpu *vcpu)
361 struct vcpu_svm *svm = to_svm(vcpu);
362 unsigned nr = vcpu->arch.exception.nr;
363 bool has_error_code = vcpu->arch.exception.has_error_code;
364 u32 error_code = vcpu->arch.exception.error_code;
366 kvm_deliver_exception_payload(vcpu);
368 if (nr == BP_VECTOR && !nrips) {
369 unsigned long rip, old_rip = kvm_rip_read(vcpu);
372 * For guest debugging where we have to reinject #BP if some
373 * INT3 is guest-owned:
374 * Emulate nRIP by moving RIP forward. Will fail if injection
375 * raises a fault that is not intercepted. Still better than
376 * failing in all cases.
378 (void)skip_emulated_instruction(vcpu);
379 rip = kvm_rip_read(vcpu);
380 svm->int3_rip = rip + svm->vmcb->save.cs.base;
381 svm->int3_injected = rip - old_rip;
384 svm->vmcb->control.event_inj = nr
386 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
387 | SVM_EVTINJ_TYPE_EXEPT;
388 svm->vmcb->control.event_inj_err = error_code;
391 static void svm_init_erratum_383(void)
397 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
400 /* Use _safe variants to not break nested virtualization */
401 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
407 low = lower_32_bits(val);
408 high = upper_32_bits(val);
410 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
412 erratum_383_found = true;
415 static void svm_init_osvw(struct kvm_vcpu *vcpu)
418 * Guests should see errata 400 and 415 as fixed (assuming that
419 * HLT and IO instructions are intercepted).
421 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
422 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
425 * By increasing VCPU's osvw.length to 3 we are telling the guest that
426 * all osvw.status bits inside that length, including bit 0 (which is
427 * reserved for erratum 298), are valid. However, if host processor's
428 * osvw_len is 0 then osvw_status[0] carries no information. We need to
429 * be conservative here and therefore we tell the guest that erratum 298
430 * is present (because we really don't know).
432 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
433 vcpu->arch.osvw.status |= 1;
436 static int has_svm(void)
440 if (!cpu_has_svm(&msg)) {
441 printk(KERN_INFO "has_svm: %s\n", msg);
446 pr_info("KVM is unsupported when running as an SEV guest\n");
450 if (pgtable_l5_enabled()) {
451 pr_info("KVM doesn't yet support 5-level paging on AMD SVM\n");
458 static void svm_hardware_disable(void)
460 /* Make sure we clean up behind us */
461 if (static_cpu_has(X86_FEATURE_TSCRATEMSR))
462 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
466 amd_pmu_disable_virt();
469 static int svm_hardware_enable(void)
472 struct svm_cpu_data *sd;
474 struct desc_struct *gdt;
475 int me = raw_smp_processor_id();
477 rdmsrl(MSR_EFER, efer);
478 if (efer & EFER_SVME)
482 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
485 sd = per_cpu(svm_data, me);
487 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
491 sd->asid_generation = 1;
492 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
493 sd->next_asid = sd->max_asid + 1;
494 sd->min_asid = max_sev_asid + 1;
496 gdt = get_current_gdt_rw();
497 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
499 wrmsrl(MSR_EFER, efer | EFER_SVME);
501 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
503 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
504 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
505 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
512 * Note that it is possible to have a system with mixed processor
513 * revisions and therefore different OSVW bits. If bits are not the same
514 * on different processors then choose the worst case (i.e. if erratum
515 * is present on one processor and not on another then assume that the
516 * erratum is present everywhere).
518 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
519 uint64_t len, status = 0;
522 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
524 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
528 osvw_status = osvw_len = 0;
532 osvw_status |= status;
533 osvw_status &= (1ULL << osvw_len) - 1;
536 osvw_status = osvw_len = 0;
538 svm_init_erratum_383();
540 amd_pmu_enable_virt();
545 static void svm_cpu_uninit(int cpu)
547 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
552 per_cpu(svm_data, cpu) = NULL;
553 kfree(sd->sev_vmcbs);
554 __free_page(sd->save_area);
558 static int svm_cpu_init(int cpu)
560 struct svm_cpu_data *sd;
563 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
567 sd->save_area = alloc_page(GFP_KERNEL);
571 clear_page(page_address(sd->save_area));
573 ret = sev_cpu_init(sd);
577 per_cpu(svm_data, cpu) = sd;
582 __free_page(sd->save_area);
589 static int direct_access_msr_slot(u32 msr)
593 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
594 if (direct_access_msrs[i].index == msr)
600 static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
603 struct vcpu_svm *svm = to_svm(vcpu);
604 int slot = direct_access_msr_slot(msr);
609 /* Set the shadow bitmaps to the desired intercept states */
611 set_bit(slot, svm->shadow_msr_intercept.read);
613 clear_bit(slot, svm->shadow_msr_intercept.read);
616 set_bit(slot, svm->shadow_msr_intercept.write);
618 clear_bit(slot, svm->shadow_msr_intercept.write);
621 static bool valid_msr_intercept(u32 index)
623 return direct_access_msr_slot(index) != -ENOENT;
626 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
633 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
636 offset = svm_msrpm_offset(msr);
637 bit_write = 2 * (msr & 0x0f) + 1;
640 BUG_ON(offset == MSR_INVALID);
642 return !!test_bit(bit_write, &tmp);
645 static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
646 u32 msr, int read, int write)
648 u8 bit_read, bit_write;
653 * If this warning triggers extend the direct_access_msrs list at the
654 * beginning of the file
656 WARN_ON(!valid_msr_intercept(msr));
658 /* Enforce non allowed MSRs to trap */
659 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
662 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
665 offset = svm_msrpm_offset(msr);
666 bit_read = 2 * (msr & 0x0f);
667 bit_write = 2 * (msr & 0x0f) + 1;
670 BUG_ON(offset == MSR_INVALID);
672 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
673 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
678 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
681 set_shadow_msr_intercept(vcpu, msr, read, write);
682 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
685 u32 *svm_vcpu_alloc_msrpm(void)
687 unsigned int order = get_order(MSRPM_SIZE);
688 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
694 msrpm = page_address(pages);
695 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
700 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
704 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
705 if (!direct_access_msrs[i].always)
707 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
712 void svm_vcpu_free_msrpm(u32 *msrpm)
714 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
717 static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
719 struct vcpu_svm *svm = to_svm(vcpu);
723 * Set intercept permissions for all direct access MSRs again. They
724 * will automatically get filtered through the MSR filter, so we are
725 * back in sync after this.
727 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
728 u32 msr = direct_access_msrs[i].index;
729 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
730 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
732 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
736 static void add_msr_offset(u32 offset)
740 for (i = 0; i < MSRPM_OFFSETS; ++i) {
742 /* Offset already in list? */
743 if (msrpm_offsets[i] == offset)
746 /* Slot used by another offset? */
747 if (msrpm_offsets[i] != MSR_INVALID)
750 /* Add offset to list */
751 msrpm_offsets[i] = offset;
757 * If this BUG triggers the msrpm_offsets table has an overflow. Just
758 * increase MSRPM_OFFSETS in this case.
763 static void init_msrpm_offsets(void)
767 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
769 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
772 offset = svm_msrpm_offset(direct_access_msrs[i].index);
773 BUG_ON(offset == MSR_INVALID);
775 add_msr_offset(offset);
779 static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
781 struct vcpu_svm *svm = to_svm(vcpu);
783 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
784 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
785 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
786 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
787 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
790 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
792 struct vcpu_svm *svm = to_svm(vcpu);
794 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
795 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
796 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
797 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
798 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
801 void disable_nmi_singlestep(struct vcpu_svm *svm)
803 svm->nmi_singlestep = false;
805 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
806 /* Clear our flags if they were not set by the guest */
807 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
808 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
809 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
810 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
814 static void grow_ple_window(struct kvm_vcpu *vcpu)
816 struct vcpu_svm *svm = to_svm(vcpu);
817 struct vmcb_control_area *control = &svm->vmcb->control;
818 int old = control->pause_filter_count;
820 control->pause_filter_count = __grow_ple_window(old,
822 pause_filter_count_grow,
823 pause_filter_count_max);
825 if (control->pause_filter_count != old) {
826 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
827 trace_kvm_ple_window_update(vcpu->vcpu_id,
828 control->pause_filter_count, old);
832 static void shrink_ple_window(struct kvm_vcpu *vcpu)
834 struct vcpu_svm *svm = to_svm(vcpu);
835 struct vmcb_control_area *control = &svm->vmcb->control;
836 int old = control->pause_filter_count;
838 control->pause_filter_count =
839 __shrink_ple_window(old,
841 pause_filter_count_shrink,
843 if (control->pause_filter_count != old) {
844 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
845 trace_kvm_ple_window_update(vcpu->vcpu_id,
846 control->pause_filter_count, old);
851 * The default MMIO mask is a single bit (excluding the present bit),
852 * which could conflict with the memory encryption bit. Check for
853 * memory encryption support and override the default MMIO mask if
854 * memory encryption is enabled.
856 static __init void svm_adjust_mmio_mask(void)
858 unsigned int enc_bit, mask_bit;
861 /* If there is no memory encryption support, use existing mask */
862 if (cpuid_eax(0x80000000) < 0x8000001f)
865 /* If memory encryption is not enabled, use existing mask */
866 rdmsrl(MSR_AMD64_SYSCFG, msr);
867 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
870 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
871 mask_bit = boot_cpu_data.x86_phys_bits;
873 /* Increment the mask bit if it is the same as the encryption bit */
874 if (enc_bit == mask_bit)
878 * If the mask bit location is below 52, then some bits above the
879 * physical addressing limit will always be reserved, so use the
880 * rsvd_bits() function to generate the mask. This mask, along with
881 * the present bit, will be used to generate a page fault with
884 * If the mask bit location is 52 (or above), then clear the mask.
886 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
888 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
891 static void svm_hardware_teardown(void)
895 sev_hardware_teardown();
897 for_each_possible_cpu(cpu)
900 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
901 get_order(IOPM_SIZE));
905 static __init void svm_set_cpu_caps(void)
911 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
913 kvm_cpu_cap_set(X86_FEATURE_SVM);
916 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
919 kvm_cpu_cap_set(X86_FEATURE_NPT);
921 /* Nested VM can receive #VMEXIT instead of triggering #GP */
922 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
925 /* CPUID 0x80000008 */
926 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
927 boot_cpu_has(X86_FEATURE_AMD_SSBD))
928 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
930 /* CPUID 0x8000001F (SME/SEV features) */
934 static __init int svm_hardware_setup(void)
937 struct page *iopm_pages;
940 unsigned int order = get_order(IOPM_SIZE);
942 iopm_pages = alloc_pages(GFP_KERNEL, order);
947 iopm_va = page_address(iopm_pages);
948 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
949 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
951 init_msrpm_offsets();
953 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
955 if (boot_cpu_has(X86_FEATURE_NX))
956 kvm_enable_efer_bits(EFER_NX);
958 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
959 kvm_enable_efer_bits(EFER_FFXSR);
961 if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
962 kvm_has_tsc_control = true;
963 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
964 kvm_tsc_scaling_ratio_frac_bits = 32;
967 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
969 /* Check for pause filtering support */
970 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
971 pause_filter_count = 0;
972 pause_filter_thresh = 0;
973 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
974 pause_filter_thresh = 0;
978 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
979 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
983 * KVM's MMU doesn't support using 2-level paging for itself, and thus
984 * NPT isn't supported if the host is using 2-level paging since host
985 * CR4 is unchanged on VMRUN.
987 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
990 if (!boot_cpu_has(X86_FEATURE_NPT))
993 kvm_configure_mmu(npt_enabled, get_max_npt_level(), PG_LEVEL_1G);
994 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
996 /* Note, SEV setup consumes npt_enabled. */
997 sev_hardware_setup();
999 svm_adjust_mmio_mask();
1001 for_each_possible_cpu(cpu) {
1002 r = svm_cpu_init(cpu);
1008 if (!boot_cpu_has(X86_FEATURE_NRIPS))
1013 if (!npt_enabled || !boot_cpu_has(X86_FEATURE_AVIC)) {
1016 pr_info("AVIC enabled\n");
1018 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
1024 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
1025 !IS_ENABLED(CONFIG_X86_64)) {
1028 pr_info("Virtual VMLOAD VMSAVE supported\n");
1032 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
1033 svm_gp_erratum_intercept = false;
1036 if (!boot_cpu_has(X86_FEATURE_VGIF))
1039 pr_info("Virtual GIF supported\n");
1045 * It seems that on AMD processors PTE's accessed bit is
1046 * being set by the CPU hardware before the NPF vmexit.
1047 * This is not expected behaviour and our tests fail because
1049 * A workaround here is to disable support for
1050 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
1051 * In this case userspace can know if there is support using
1052 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
1054 * If future AMD CPU models change the behaviour described above,
1055 * this variable can be changed accordingly
1057 allow_smaller_maxphyaddr = !npt_enabled;
1062 svm_hardware_teardown();
1066 static void init_seg(struct vmcb_seg *seg)
1069 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
1070 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
1071 seg->limit = 0xffff;
1075 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
1078 seg->attrib = SVM_SELECTOR_P_MASK | type;
1079 seg->limit = 0xffff;
1083 static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1085 struct vcpu_svm *svm = to_svm(vcpu);
1086 u64 g_tsc_offset = 0;
1088 if (is_guest_mode(vcpu)) {
1089 /* Write L1's TSC offset. */
1090 g_tsc_offset = svm->vmcb->control.tsc_offset -
1091 svm->vmcb01.ptr->control.tsc_offset;
1092 svm->vmcb01.ptr->control.tsc_offset = offset;
1095 trace_kvm_write_tsc_offset(vcpu->vcpu_id,
1096 svm->vmcb->control.tsc_offset - g_tsc_offset,
1099 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
1101 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
1102 return svm->vmcb->control.tsc_offset;
1105 /* Evaluate instruction intercepts that depend on guest CPUID features. */
1106 static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
1107 struct vcpu_svm *svm)
1110 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
1111 * roots, or if INVPCID is disabled in the guest to inject #UD.
1113 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
1115 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
1116 svm_set_intercept(svm, INTERCEPT_INVPCID);
1118 svm_clr_intercept(svm, INTERCEPT_INVPCID);
1121 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
1122 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
1123 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
1125 svm_set_intercept(svm, INTERCEPT_RDTSCP);
1129 static void init_vmcb(struct kvm_vcpu *vcpu)
1131 struct vcpu_svm *svm = to_svm(vcpu);
1132 struct vmcb_control_area *control = &svm->vmcb->control;
1133 struct vmcb_save_area *save = &svm->vmcb->save;
1135 vcpu->arch.hflags = 0;
1137 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1138 svm_set_intercept(svm, INTERCEPT_CR3_READ);
1139 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1140 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1141 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1142 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
1143 if (!kvm_vcpu_apicv_active(vcpu))
1144 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
1146 set_dr_intercepts(svm);
1148 set_exception_intercept(svm, PF_VECTOR);
1149 set_exception_intercept(svm, UD_VECTOR);
1150 set_exception_intercept(svm, MC_VECTOR);
1151 set_exception_intercept(svm, AC_VECTOR);
1152 set_exception_intercept(svm, DB_VECTOR);
1154 * Guest access to VMware backdoor ports could legitimately
1155 * trigger #GP because of TSS I/O permission bitmap.
1156 * We intercept those #GP and allow access to them anyway
1159 if (enable_vmware_backdoor)
1160 set_exception_intercept(svm, GP_VECTOR);
1162 svm_set_intercept(svm, INTERCEPT_INTR);
1163 svm_set_intercept(svm, INTERCEPT_NMI);
1164 svm_set_intercept(svm, INTERCEPT_SMI);
1165 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1166 svm_set_intercept(svm, INTERCEPT_RDPMC);
1167 svm_set_intercept(svm, INTERCEPT_CPUID);
1168 svm_set_intercept(svm, INTERCEPT_INVD);
1169 svm_set_intercept(svm, INTERCEPT_INVLPG);
1170 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1171 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1172 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1173 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1174 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1175 svm_set_intercept(svm, INTERCEPT_VMRUN);
1176 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1177 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1178 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1179 svm_set_intercept(svm, INTERCEPT_STGI);
1180 svm_set_intercept(svm, INTERCEPT_CLGI);
1181 svm_set_intercept(svm, INTERCEPT_SKINIT);
1182 svm_set_intercept(svm, INTERCEPT_WBINVD);
1183 svm_set_intercept(svm, INTERCEPT_XSETBV);
1184 svm_set_intercept(svm, INTERCEPT_RDPRU);
1185 svm_set_intercept(svm, INTERCEPT_RSM);
1187 if (!kvm_mwait_in_guest(vcpu->kvm)) {
1188 svm_set_intercept(svm, INTERCEPT_MONITOR);
1189 svm_set_intercept(svm, INTERCEPT_MWAIT);
1192 if (!kvm_hlt_in_guest(vcpu->kvm))
1193 svm_set_intercept(svm, INTERCEPT_HLT);
1195 control->iopm_base_pa = __sme_set(iopm_base);
1196 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
1197 control->int_ctl = V_INTR_MASKING_MASK;
1199 init_seg(&save->es);
1200 init_seg(&save->ss);
1201 init_seg(&save->ds);
1202 init_seg(&save->fs);
1203 init_seg(&save->gs);
1205 save->cs.selector = 0xf000;
1206 save->cs.base = 0xffff0000;
1207 /* Executable/Readable Code Segment */
1208 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1209 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1210 save->cs.limit = 0xffff;
1212 save->gdtr.limit = 0xffff;
1213 save->idtr.limit = 0xffff;
1215 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1216 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1218 svm_set_cr4(vcpu, 0);
1219 svm_set_efer(vcpu, 0);
1220 save->dr6 = 0xffff0ff0;
1221 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
1222 save->rip = 0x0000fff0;
1223 vcpu->arch.regs[VCPU_REGS_RIP] = save->rip;
1226 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
1227 * It also updates the guest-visible cr0 value.
1229 svm_set_cr0(vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
1230 kvm_mmu_reset_context(vcpu);
1232 save->cr4 = X86_CR4_PAE;
1236 /* Setup VMCB for Nested Paging */
1237 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
1238 svm_clr_intercept(svm, INTERCEPT_INVLPG);
1239 clr_exception_intercept(svm, PF_VECTOR);
1240 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1241 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
1242 save->g_pat = vcpu->arch.pat;
1246 svm->current_vmcb->asid_generation = 0;
1249 svm->nested.vmcb12_gpa = INVALID_GPA;
1250 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1251 vcpu->arch.hflags = 0;
1253 if (!kvm_pause_in_guest(vcpu->kvm)) {
1254 control->pause_filter_count = pause_filter_count;
1255 if (pause_filter_thresh)
1256 control->pause_filter_thresh = pause_filter_thresh;
1257 svm_set_intercept(svm, INTERCEPT_PAUSE);
1259 svm_clr_intercept(svm, INTERCEPT_PAUSE);
1262 svm_recalc_instruction_intercepts(vcpu, svm);
1265 * If the host supports V_SPEC_CTRL then disable the interception
1266 * of MSR_IA32_SPEC_CTRL.
1268 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1269 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1271 if (kvm_vcpu_apicv_active(vcpu))
1272 avic_init_vmcb(svm);
1275 svm_clr_intercept(svm, INTERCEPT_STGI);
1276 svm_clr_intercept(svm, INTERCEPT_CLGI);
1277 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1280 if (sev_guest(vcpu->kvm)) {
1281 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1282 clr_exception_intercept(svm, UD_VECTOR);
1284 if (sev_es_guest(vcpu->kvm)) {
1285 /* Perform SEV-ES specific VMCB updates */
1286 sev_es_init_vmcb(svm);
1290 vmcb_mark_all_dirty(svm->vmcb);
1296 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
1298 struct vcpu_svm *svm = to_svm(vcpu);
1303 svm->virt_spec_ctrl = 0;
1306 vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE |
1307 MSR_IA32_APICBASE_ENABLE;
1308 if (kvm_vcpu_is_reset_bsp(vcpu))
1309 vcpu->arch.apic_base |= MSR_IA32_APICBASE_BSP;
1313 kvm_cpuid(vcpu, &eax, &dummy, &dummy, &dummy, false);
1314 kvm_rdx_write(vcpu, eax);
1316 if (kvm_vcpu_apicv_active(vcpu) && !init_event)
1317 avic_update_vapic_bar(svm, APIC_DEFAULT_PHYS_BASE);
1320 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1322 svm->current_vmcb = target_vmcb;
1323 svm->vmcb = target_vmcb->ptr;
1326 static int svm_create_vcpu(struct kvm_vcpu *vcpu)
1328 struct vcpu_svm *svm;
1329 struct page *vmcb01_page;
1330 struct page *vmsa_page = NULL;
1333 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1337 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1341 if (sev_es_guest(vcpu->kvm)) {
1343 * SEV-ES guests require a separate VMSA page used to contain
1344 * the encrypted register state of the guest.
1346 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1348 goto error_free_vmcb_page;
1351 * SEV-ES guests maintain an encrypted version of their FPU
1352 * state which is restored and saved on VMRUN and VMEXIT.
1353 * Free the fpu structure to prevent KVM from attempting to
1354 * access the FPU state.
1356 kvm_free_guest_fpu(vcpu);
1359 err = avic_init_vcpu(svm);
1361 goto error_free_vmsa_page;
1363 /* We initialize this flag to true to make sure that the is_running
1364 * bit would be set the first time the vcpu is loaded.
1366 if (irqchip_in_kernel(vcpu->kvm) && kvm_apicv_activated(vcpu->kvm))
1367 svm->avic_is_running = true;
1369 svm->msrpm = svm_vcpu_alloc_msrpm();
1372 goto error_free_vmsa_page;
1375 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1377 svm->vmcb01.ptr = page_address(vmcb01_page);
1378 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
1381 svm->vmsa = page_address(vmsa_page);
1383 svm->guest_state_loaded = false;
1385 svm_switch_vmcb(svm, &svm->vmcb01);
1388 svm_init_osvw(vcpu);
1389 vcpu->arch.microcode_version = 0x01000065;
1391 if (sev_es_guest(vcpu->kvm))
1392 /* Perform SEV-ES specific VMCB creation updates */
1393 sev_es_create_vcpu(svm);
1397 error_free_vmsa_page:
1399 __free_page(vmsa_page);
1400 error_free_vmcb_page:
1401 __free_page(vmcb01_page);
1406 static void svm_clear_current_vmcb(struct vmcb *vmcb)
1410 for_each_online_cpu(i)
1411 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1414 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1416 struct vcpu_svm *svm = to_svm(vcpu);
1419 * The vmcb page can be recycled, causing a false negative in
1420 * svm_vcpu_load(). So, ensure that no logical CPU has this
1421 * vmcb page recorded as its current vmcb.
1423 svm_clear_current_vmcb(svm->vmcb);
1425 svm_free_nested(svm);
1427 sev_free_vcpu(vcpu);
1429 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
1430 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
1433 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1435 struct vcpu_svm *svm = to_svm(vcpu);
1436 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
1438 if (sev_es_guest(vcpu->kvm))
1439 sev_es_unmap_ghcb(svm);
1441 if (svm->guest_state_loaded)
1445 * Save additional host state that will be restored on VMEXIT (sev-es)
1446 * or subsequent vmload of host save area.
1448 if (sev_es_guest(vcpu->kvm)) {
1449 sev_es_prepare_guest_switch(svm, vcpu->cpu);
1451 vmsave(__sme_page_pa(sd->save_area));
1454 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
1455 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1456 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1457 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1458 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1462 if (likely(tsc_aux_uret_slot >= 0))
1463 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
1465 svm->guest_state_loaded = true;
1468 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1470 to_svm(vcpu)->guest_state_loaded = false;
1473 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1475 struct vcpu_svm *svm = to_svm(vcpu);
1476 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1478 if (sd->current_vmcb != svm->vmcb) {
1479 sd->current_vmcb = svm->vmcb;
1480 indirect_branch_prediction_barrier();
1482 avic_vcpu_load(vcpu, cpu);
1485 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1487 avic_vcpu_put(vcpu);
1488 svm_prepare_host_switch(vcpu);
1490 ++vcpu->stat.host_state_reload;
1493 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1495 struct vcpu_svm *svm = to_svm(vcpu);
1496 unsigned long rflags = svm->vmcb->save.rflags;
1498 if (svm->nmi_singlestep) {
1499 /* Hide our flags if they were not set by the guest */
1500 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1501 rflags &= ~X86_EFLAGS_TF;
1502 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1503 rflags &= ~X86_EFLAGS_RF;
1508 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1510 if (to_svm(vcpu)->nmi_singlestep)
1511 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1514 * Any change of EFLAGS.VM is accompanied by a reload of SS
1515 * (caused by either a task switch or an inter-privilege IRET),
1516 * so we do not need to update the CPL here.
1518 to_svm(vcpu)->vmcb->save.rflags = rflags;
1521 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1524 case VCPU_EXREG_PDPTR:
1525 BUG_ON(!npt_enabled);
1526 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
1533 static void svm_set_vintr(struct vcpu_svm *svm)
1535 struct vmcb_control_area *control;
1537 /* The following fields are ignored when AVIC is enabled */
1538 WARN_ON(kvm_vcpu_apicv_active(&svm->vcpu));
1539 svm_set_intercept(svm, INTERCEPT_VINTR);
1542 * This is just a dummy VINTR to actually cause a vmexit to happen.
1543 * Actual injection of virtual interrupts happens through EVENTINJ.
1545 control = &svm->vmcb->control;
1546 control->int_vector = 0x0;
1547 control->int_ctl &= ~V_INTR_PRIO_MASK;
1548 control->int_ctl |= V_IRQ_MASK |
1549 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1550 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1553 static void svm_clear_vintr(struct vcpu_svm *svm)
1555 const u32 mask = V_TPR_MASK | V_GIF_ENABLE_MASK | V_GIF_MASK | V_INTR_MASKING_MASK;
1556 svm_clr_intercept(svm, INTERCEPT_VINTR);
1558 /* Drop int_ctl fields related to VINTR injection. */
1559 svm->vmcb->control.int_ctl &= mask;
1560 if (is_guest_mode(&svm->vcpu)) {
1561 svm->vmcb01.ptr->control.int_ctl &= mask;
1563 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1564 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1565 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl & ~mask;
1568 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1571 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1573 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1574 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
1577 case VCPU_SREG_CS: return &save->cs;
1578 case VCPU_SREG_DS: return &save->ds;
1579 case VCPU_SREG_ES: return &save->es;
1580 case VCPU_SREG_FS: return &save01->fs;
1581 case VCPU_SREG_GS: return &save01->gs;
1582 case VCPU_SREG_SS: return &save->ss;
1583 case VCPU_SREG_TR: return &save01->tr;
1584 case VCPU_SREG_LDTR: return &save01->ldtr;
1590 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1592 struct vmcb_seg *s = svm_seg(vcpu, seg);
1597 static void svm_get_segment(struct kvm_vcpu *vcpu,
1598 struct kvm_segment *var, int seg)
1600 struct vmcb_seg *s = svm_seg(vcpu, seg);
1602 var->base = s->base;
1603 var->limit = s->limit;
1604 var->selector = s->selector;
1605 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1606 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1607 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1608 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1609 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1610 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1611 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1614 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1615 * However, the SVM spec states that the G bit is not observed by the
1616 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1617 * So let's synthesize a legal G bit for all segments, this helps
1618 * running KVM nested. It also helps cross-vendor migration, because
1619 * Intel's vmentry has a check on the 'G' bit.
1621 var->g = s->limit > 0xfffff;
1624 * AMD's VMCB does not have an explicit unusable field, so emulate it
1625 * for cross vendor migration purposes by "not present"
1627 var->unusable = !var->present;
1632 * Work around a bug where the busy flag in the tr selector
1642 * The accessed bit must always be set in the segment
1643 * descriptor cache, although it can be cleared in the
1644 * descriptor, the cached bit always remains at 1. Since
1645 * Intel has a check on this, set it here to support
1646 * cross-vendor migration.
1653 * On AMD CPUs sometimes the DB bit in the segment
1654 * descriptor is left as 1, although the whole segment has
1655 * been made unusable. Clear it here to pass an Intel VMX
1656 * entry check when cross vendor migrating.
1660 /* This is symmetric with svm_set_segment() */
1661 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
1666 static int svm_get_cpl(struct kvm_vcpu *vcpu)
1668 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1673 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1675 struct vcpu_svm *svm = to_svm(vcpu);
1677 dt->size = svm->vmcb->save.idtr.limit;
1678 dt->address = svm->vmcb->save.idtr.base;
1681 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1683 struct vcpu_svm *svm = to_svm(vcpu);
1685 svm->vmcb->save.idtr.limit = dt->size;
1686 svm->vmcb->save.idtr.base = dt->address ;
1687 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1690 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1692 struct vcpu_svm *svm = to_svm(vcpu);
1694 dt->size = svm->vmcb->save.gdtr.limit;
1695 dt->address = svm->vmcb->save.gdtr.base;
1698 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1700 struct vcpu_svm *svm = to_svm(vcpu);
1702 svm->vmcb->save.gdtr.limit = dt->size;
1703 svm->vmcb->save.gdtr.base = dt->address ;
1704 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1707 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1709 struct vcpu_svm *svm = to_svm(vcpu);
1712 #ifdef CONFIG_X86_64
1713 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
1714 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
1715 vcpu->arch.efer |= EFER_LMA;
1716 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
1719 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
1720 vcpu->arch.efer &= ~EFER_LMA;
1721 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
1725 vcpu->arch.cr0 = cr0;
1728 hcr0 |= X86_CR0_PG | X86_CR0_WP;
1731 * re-enable caching here because the QEMU bios
1732 * does not do it - this results in some delay at
1735 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1736 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1738 svm->vmcb->save.cr0 = hcr0;
1739 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1742 * SEV-ES guests must always keep the CR intercepts cleared. CR
1743 * tracking is done using the CR write traps.
1745 if (sev_es_guest(vcpu->kvm))
1749 /* Selective CR0 write remains on. */
1750 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1751 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1753 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1754 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1758 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1763 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1765 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
1766 unsigned long old_cr4 = vcpu->arch.cr4;
1768 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1769 svm_flush_tlb(vcpu);
1771 vcpu->arch.cr4 = cr4;
1774 cr4 |= host_cr4_mce;
1775 to_svm(vcpu)->vmcb->save.cr4 = cr4;
1776 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
1778 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1779 kvm_update_cpuid_runtime(vcpu);
1782 static void svm_set_segment(struct kvm_vcpu *vcpu,
1783 struct kvm_segment *var, int seg)
1785 struct vcpu_svm *svm = to_svm(vcpu);
1786 struct vmcb_seg *s = svm_seg(vcpu, seg);
1788 s->base = var->base;
1789 s->limit = var->limit;
1790 s->selector = var->selector;
1791 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1792 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1793 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1794 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1795 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1796 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1797 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1798 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1801 * This is always accurate, except if SYSRET returned to a segment
1802 * with SS.DPL != 3. Intel does not have this quirk, and always
1803 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1804 * would entail passing the CPL to userspace and back.
1806 if (seg == VCPU_SREG_SS)
1807 /* This is symmetric with svm_get_segment() */
1808 svm->vmcb->save.cpl = (var->dpl & 3);
1810 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
1813 static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
1815 struct vcpu_svm *svm = to_svm(vcpu);
1817 clr_exception_intercept(svm, BP_VECTOR);
1819 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1820 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1821 set_exception_intercept(svm, BP_VECTOR);
1825 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
1827 if (sd->next_asid > sd->max_asid) {
1828 ++sd->asid_generation;
1829 sd->next_asid = sd->min_asid;
1830 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
1831 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
1834 svm->current_vmcb->asid_generation = sd->asid_generation;
1835 svm->asid = sd->next_asid++;
1838 static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
1840 struct vmcb *vmcb = svm->vmcb;
1842 if (svm->vcpu.arch.guest_state_protected)
1845 if (unlikely(value != vmcb->save.dr6)) {
1846 vmcb->save.dr6 = value;
1847 vmcb_mark_dirty(vmcb, VMCB_DR);
1851 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1853 struct vcpu_svm *svm = to_svm(vcpu);
1855 if (vcpu->arch.guest_state_protected)
1858 get_debugreg(vcpu->arch.db[0], 0);
1859 get_debugreg(vcpu->arch.db[1], 1);
1860 get_debugreg(vcpu->arch.db[2], 2);
1861 get_debugreg(vcpu->arch.db[3], 3);
1863 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
1864 * because db_interception might need it. We can do it before vmentry.
1866 vcpu->arch.dr6 = svm->vmcb->save.dr6;
1867 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1868 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1869 set_dr_intercepts(svm);
1872 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
1874 struct vcpu_svm *svm = to_svm(vcpu);
1876 if (vcpu->arch.guest_state_protected)
1879 svm->vmcb->save.dr7 = value;
1880 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
1883 static int pf_interception(struct kvm_vcpu *vcpu)
1885 struct vcpu_svm *svm = to_svm(vcpu);
1887 u64 fault_address = svm->vmcb->control.exit_info_2;
1888 u64 error_code = svm->vmcb->control.exit_info_1;
1890 return kvm_handle_page_fault(vcpu, error_code, fault_address,
1891 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1892 svm->vmcb->control.insn_bytes : NULL,
1893 svm->vmcb->control.insn_len);
1896 static int npf_interception(struct kvm_vcpu *vcpu)
1898 struct vcpu_svm *svm = to_svm(vcpu);
1900 u64 fault_address = __sme_clr(svm->vmcb->control.exit_info_2);
1901 u64 error_code = svm->vmcb->control.exit_info_1;
1903 trace_kvm_page_fault(fault_address, error_code);
1904 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
1905 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1906 svm->vmcb->control.insn_bytes : NULL,
1907 svm->vmcb->control.insn_len);
1910 static int db_interception(struct kvm_vcpu *vcpu)
1912 struct kvm_run *kvm_run = vcpu->run;
1913 struct vcpu_svm *svm = to_svm(vcpu);
1915 if (!(vcpu->guest_debug &
1916 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1917 !svm->nmi_singlestep) {
1918 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
1919 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
1923 if (svm->nmi_singlestep) {
1924 disable_nmi_singlestep(svm);
1925 /* Make sure we check for pending NMIs upon entry */
1926 kvm_make_request(KVM_REQ_EVENT, vcpu);
1929 if (vcpu->guest_debug &
1930 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
1931 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1932 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1933 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
1934 kvm_run->debug.arch.pc =
1935 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1936 kvm_run->debug.arch.exception = DB_VECTOR;
1943 static int bp_interception(struct kvm_vcpu *vcpu)
1945 struct vcpu_svm *svm = to_svm(vcpu);
1946 struct kvm_run *kvm_run = vcpu->run;
1948 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1949 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1950 kvm_run->debug.arch.exception = BP_VECTOR;
1954 static int ud_interception(struct kvm_vcpu *vcpu)
1956 return handle_ud(vcpu);
1959 static int ac_interception(struct kvm_vcpu *vcpu)
1961 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
1965 static bool is_erratum_383(void)
1970 if (!erratum_383_found)
1973 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1977 /* Bit 62 may or may not be set for this mce */
1978 value &= ~(1ULL << 62);
1980 if (value != 0xb600000000010015ULL)
1983 /* Clear MCi_STATUS registers */
1984 for (i = 0; i < 6; ++i)
1985 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1987 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1991 value &= ~(1ULL << 2);
1992 low = lower_32_bits(value);
1993 high = upper_32_bits(value);
1995 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1998 /* Flush tlb to evict multi-match entries */
2004 static void svm_handle_mce(struct kvm_vcpu *vcpu)
2006 if (is_erratum_383()) {
2008 * Erratum 383 triggered. Guest state is corrupt so kill the
2011 pr_err("KVM: Guest triggered AMD Erratum 383\n");
2013 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
2019 * On an #MC intercept the MCE handler is not called automatically in
2020 * the host. So do it by hand here.
2022 kvm_machine_check();
2025 static int mc_interception(struct kvm_vcpu *vcpu)
2030 static int shutdown_interception(struct kvm_vcpu *vcpu)
2032 struct kvm_run *kvm_run = vcpu->run;
2033 struct vcpu_svm *svm = to_svm(vcpu);
2036 * The VM save area has already been encrypted so it
2037 * cannot be reinitialized - just terminate.
2039 if (sev_es_guest(vcpu->kvm))
2043 * VMCB is undefined after a SHUTDOWN intercept
2044 * so reinitialize it.
2046 clear_page(svm->vmcb);
2049 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
2053 static int io_interception(struct kvm_vcpu *vcpu)
2055 struct vcpu_svm *svm = to_svm(vcpu);
2056 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
2057 int size, in, string;
2060 ++vcpu->stat.io_exits;
2061 string = (io_info & SVM_IOIO_STR_MASK) != 0;
2062 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
2063 port = io_info >> 16;
2064 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
2067 if (sev_es_guest(vcpu->kvm))
2068 return sev_es_string_io(svm, size, port, in);
2070 return kvm_emulate_instruction(vcpu, 0);
2073 svm->next_rip = svm->vmcb->control.exit_info_2;
2075 return kvm_fast_pio(vcpu, size, port, in);
2078 static int nmi_interception(struct kvm_vcpu *vcpu)
2083 static int intr_interception(struct kvm_vcpu *vcpu)
2085 ++vcpu->stat.irq_exits;
2089 static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
2091 struct vcpu_svm *svm = to_svm(vcpu);
2092 struct vmcb *vmcb12;
2093 struct kvm_host_map map;
2096 if (nested_svm_check_permissions(vcpu))
2099 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
2102 kvm_inject_gp(vcpu, 0);
2108 ret = kvm_skip_emulated_instruction(vcpu);
2111 nested_svm_vmloadsave(vmcb12, svm->vmcb);
2112 svm->sysenter_eip_hi = 0;
2113 svm->sysenter_esp_hi = 0;
2115 nested_svm_vmloadsave(svm->vmcb, vmcb12);
2117 kvm_vcpu_unmap(vcpu, &map, true);
2122 static int vmload_interception(struct kvm_vcpu *vcpu)
2124 return vmload_vmsave_interception(vcpu, true);
2127 static int vmsave_interception(struct kvm_vcpu *vcpu)
2129 return vmload_vmsave_interception(vcpu, false);
2132 static int vmrun_interception(struct kvm_vcpu *vcpu)
2134 if (nested_svm_check_permissions(vcpu))
2137 return nested_svm_vmrun(vcpu);
2147 /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2148 static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2150 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2152 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2153 return NONE_SVM_INSTR;
2155 switch (ctxt->modrm) {
2156 case 0xd8: /* VMRUN */
2157 return SVM_INSTR_VMRUN;
2158 case 0xda: /* VMLOAD */
2159 return SVM_INSTR_VMLOAD;
2160 case 0xdb: /* VMSAVE */
2161 return SVM_INSTR_VMSAVE;
2166 return NONE_SVM_INSTR;
2169 static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2171 const int guest_mode_exit_codes[] = {
2172 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2173 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2174 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2176 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
2177 [SVM_INSTR_VMRUN] = vmrun_interception,
2178 [SVM_INSTR_VMLOAD] = vmload_interception,
2179 [SVM_INSTR_VMSAVE] = vmsave_interception,
2181 struct vcpu_svm *svm = to_svm(vcpu);
2184 if (is_guest_mode(vcpu)) {
2185 /* Returns '1' or -errno on failure, '0' on success. */
2186 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
2191 return svm_instr_handlers[opcode](vcpu);
2195 * #GP handling code. Note that #GP can be triggered under the following two
2197 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2198 * some AMD CPUs when EAX of these instructions are in the reserved memory
2199 * regions (e.g. SMM memory on host).
2200 * 2) VMware backdoor
2202 static int gp_interception(struct kvm_vcpu *vcpu)
2204 struct vcpu_svm *svm = to_svm(vcpu);
2205 u32 error_code = svm->vmcb->control.exit_info_1;
2208 /* Both #GP cases have zero error_code */
2212 /* Decode the instruction for usage later */
2213 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2216 opcode = svm_instr_opcode(vcpu);
2218 if (opcode == NONE_SVM_INSTR) {
2219 if (!enable_vmware_backdoor)
2223 * VMware backdoor emulation on #GP interception only handles
2224 * IN{S}, OUT{S}, and RDPMC.
2226 if (!is_guest_mode(vcpu))
2227 return kvm_emulate_instruction(vcpu,
2228 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2230 return emulate_svm_instr(vcpu, opcode);
2233 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2237 void svm_set_gif(struct vcpu_svm *svm, bool value)
2241 * If VGIF is enabled, the STGI intercept is only added to
2242 * detect the opening of the SMI/NMI window; remove it now.
2243 * Likewise, clear the VINTR intercept, we will set it
2244 * again while processing KVM_REQ_EVENT if needed.
2246 if (vgif_enabled(svm))
2247 svm_clr_intercept(svm, INTERCEPT_STGI);
2248 if (svm_is_intercept(svm, INTERCEPT_VINTR))
2249 svm_clear_vintr(svm);
2252 if (svm->vcpu.arch.smi_pending ||
2253 svm->vcpu.arch.nmi_pending ||
2254 kvm_cpu_has_injectable_intr(&svm->vcpu))
2255 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2260 * After a CLGI no interrupts should come. But if vGIF is
2261 * in use, we still rely on the VINTR intercept (rather than
2262 * STGI) to detect an open interrupt window.
2264 if (!vgif_enabled(svm))
2265 svm_clear_vintr(svm);
2269 static int stgi_interception(struct kvm_vcpu *vcpu)
2273 if (nested_svm_check_permissions(vcpu))
2276 ret = kvm_skip_emulated_instruction(vcpu);
2277 svm_set_gif(to_svm(vcpu), true);
2281 static int clgi_interception(struct kvm_vcpu *vcpu)
2285 if (nested_svm_check_permissions(vcpu))
2288 ret = kvm_skip_emulated_instruction(vcpu);
2289 svm_set_gif(to_svm(vcpu), false);
2293 static int invlpga_interception(struct kvm_vcpu *vcpu)
2295 gva_t gva = kvm_rax_read(vcpu);
2296 u32 asid = kvm_rcx_read(vcpu);
2298 /* FIXME: Handle an address size prefix. */
2299 if (!is_long_mode(vcpu))
2302 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
2304 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2305 kvm_mmu_invlpg(vcpu, gva);
2307 return kvm_skip_emulated_instruction(vcpu);
2310 static int skinit_interception(struct kvm_vcpu *vcpu)
2312 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
2314 kvm_queue_exception(vcpu, UD_VECTOR);
2318 static int task_switch_interception(struct kvm_vcpu *vcpu)
2320 struct vcpu_svm *svm = to_svm(vcpu);
2323 int int_type = svm->vmcb->control.exit_int_info &
2324 SVM_EXITINTINFO_TYPE_MASK;
2325 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
2327 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2329 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
2330 bool has_error_code = false;
2333 tss_selector = (u16)svm->vmcb->control.exit_info_1;
2335 if (svm->vmcb->control.exit_info_2 &
2336 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
2337 reason = TASK_SWITCH_IRET;
2338 else if (svm->vmcb->control.exit_info_2 &
2339 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2340 reason = TASK_SWITCH_JMP;
2342 reason = TASK_SWITCH_GATE;
2344 reason = TASK_SWITCH_CALL;
2346 if (reason == TASK_SWITCH_GATE) {
2348 case SVM_EXITINTINFO_TYPE_NMI:
2349 vcpu->arch.nmi_injected = false;
2351 case SVM_EXITINTINFO_TYPE_EXEPT:
2352 if (svm->vmcb->control.exit_info_2 &
2353 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2354 has_error_code = true;
2356 (u32)svm->vmcb->control.exit_info_2;
2358 kvm_clear_exception_queue(vcpu);
2360 case SVM_EXITINTINFO_TYPE_INTR:
2361 kvm_clear_interrupt_queue(vcpu);
2368 if (reason != TASK_SWITCH_GATE ||
2369 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2370 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
2371 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
2372 if (!skip_emulated_instruction(vcpu))
2376 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2379 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
2380 has_error_code, error_code);
2383 static int iret_interception(struct kvm_vcpu *vcpu)
2385 struct vcpu_svm *svm = to_svm(vcpu);
2387 ++vcpu->stat.nmi_window_exits;
2388 vcpu->arch.hflags |= HF_IRET_MASK;
2389 if (!sev_es_guest(vcpu->kvm)) {
2390 svm_clr_intercept(svm, INTERCEPT_IRET);
2391 svm->nmi_iret_rip = kvm_rip_read(vcpu);
2393 kvm_make_request(KVM_REQ_EVENT, vcpu);
2397 static int invlpg_interception(struct kvm_vcpu *vcpu)
2399 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2400 return kvm_emulate_instruction(vcpu, 0);
2402 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2403 return kvm_skip_emulated_instruction(vcpu);
2406 static int emulate_on_interception(struct kvm_vcpu *vcpu)
2408 return kvm_emulate_instruction(vcpu, 0);
2411 static int rsm_interception(struct kvm_vcpu *vcpu)
2413 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
2416 static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
2419 struct vcpu_svm *svm = to_svm(vcpu);
2420 unsigned long cr0 = vcpu->arch.cr0;
2423 if (!is_guest_mode(vcpu) ||
2424 (!(vmcb_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
2427 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2428 val &= ~SVM_CR0_SELECTIVE_MASK;
2431 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2432 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2438 #define CR_VALID (1ULL << 63)
2440 static int cr_interception(struct kvm_vcpu *vcpu)
2442 struct vcpu_svm *svm = to_svm(vcpu);
2447 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2448 return emulate_on_interception(vcpu);
2450 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2451 return emulate_on_interception(vcpu);
2453 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2454 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2455 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2457 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2460 if (cr >= 16) { /* mov to cr */
2462 val = kvm_register_read(vcpu, reg);
2463 trace_kvm_cr_write(cr, val);
2466 if (!check_selective_cr0_intercepted(vcpu, val))
2467 err = kvm_set_cr0(vcpu, val);
2473 err = kvm_set_cr3(vcpu, val);
2476 err = kvm_set_cr4(vcpu, val);
2479 err = kvm_set_cr8(vcpu, val);
2482 WARN(1, "unhandled write to CR%d", cr);
2483 kvm_queue_exception(vcpu, UD_VECTOR);
2486 } else { /* mov from cr */
2489 val = kvm_read_cr0(vcpu);
2492 val = vcpu->arch.cr2;
2495 val = kvm_read_cr3(vcpu);
2498 val = kvm_read_cr4(vcpu);
2501 val = kvm_get_cr8(vcpu);
2504 WARN(1, "unhandled read from CR%d", cr);
2505 kvm_queue_exception(vcpu, UD_VECTOR);
2508 kvm_register_write(vcpu, reg, val);
2509 trace_kvm_cr_read(cr, val);
2511 return kvm_complete_insn_gp(vcpu, err);
2514 static int cr_trap(struct kvm_vcpu *vcpu)
2516 struct vcpu_svm *svm = to_svm(vcpu);
2517 unsigned long old_value, new_value;
2521 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2523 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2526 old_value = kvm_read_cr0(vcpu);
2527 svm_set_cr0(vcpu, new_value);
2529 kvm_post_set_cr0(vcpu, old_value, new_value);
2532 old_value = kvm_read_cr4(vcpu);
2533 svm_set_cr4(vcpu, new_value);
2535 kvm_post_set_cr4(vcpu, old_value, new_value);
2538 ret = kvm_set_cr8(vcpu, new_value);
2541 WARN(1, "unhandled CR%d write trap", cr);
2542 kvm_queue_exception(vcpu, UD_VECTOR);
2546 return kvm_complete_insn_gp(vcpu, ret);
2549 static int dr_interception(struct kvm_vcpu *vcpu)
2551 struct vcpu_svm *svm = to_svm(vcpu);
2556 if (vcpu->guest_debug == 0) {
2558 * No more DR vmexits; force a reload of the debug registers
2559 * and reenter on this instruction. The next vmexit will
2560 * retrieve the full state of the debug registers.
2562 clr_dr_intercepts(svm);
2563 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
2567 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2568 return emulate_on_interception(vcpu);
2570 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2571 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2572 if (dr >= 16) { /* mov to DRn */
2574 val = kvm_register_read(vcpu, reg);
2575 err = kvm_set_dr(vcpu, dr, val);
2577 kvm_get_dr(vcpu, dr, &val);
2578 kvm_register_write(vcpu, reg, val);
2581 return kvm_complete_insn_gp(vcpu, err);
2584 static int cr8_write_interception(struct kvm_vcpu *vcpu)
2588 u8 cr8_prev = kvm_get_cr8(vcpu);
2589 /* instruction emulation calls kvm_set_cr8() */
2590 r = cr_interception(vcpu);
2591 if (lapic_in_kernel(vcpu))
2593 if (cr8_prev <= kvm_get_cr8(vcpu))
2595 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
2599 static int efer_trap(struct kvm_vcpu *vcpu)
2601 struct msr_data msr_info;
2605 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2606 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2607 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2608 * the guest doesn't have X86_FEATURE_SVM.
2610 msr_info.host_initiated = false;
2611 msr_info.index = MSR_EFER;
2612 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2613 ret = kvm_set_msr_common(vcpu, &msr_info);
2615 return kvm_complete_insn_gp(vcpu, ret);
2618 static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2622 switch (msr->index) {
2623 case MSR_F10H_DECFG:
2624 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2625 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2627 case MSR_IA32_PERF_CAPABILITIES:
2630 return KVM_MSR_RET_INVALID;
2636 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2638 struct vcpu_svm *svm = to_svm(vcpu);
2640 switch (msr_info->index) {
2642 msr_info->data = svm->vmcb01.ptr->save.star;
2644 #ifdef CONFIG_X86_64
2646 msr_info->data = svm->vmcb01.ptr->save.lstar;
2649 msr_info->data = svm->vmcb01.ptr->save.cstar;
2651 case MSR_KERNEL_GS_BASE:
2652 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
2654 case MSR_SYSCALL_MASK:
2655 msr_info->data = svm->vmcb01.ptr->save.sfmask;
2658 case MSR_IA32_SYSENTER_CS:
2659 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
2661 case MSR_IA32_SYSENTER_EIP:
2662 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2663 if (guest_cpuid_is_intel(vcpu))
2664 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
2666 case MSR_IA32_SYSENTER_ESP:
2667 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2668 if (guest_cpuid_is_intel(vcpu))
2669 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
2672 msr_info->data = svm->tsc_aux;
2675 * Nobody will change the following 5 values in the VMCB so we can
2676 * safely return them on rdmsr. They will always be 0 until LBRV is
2679 case MSR_IA32_DEBUGCTLMSR:
2680 msr_info->data = svm->vmcb->save.dbgctl;
2682 case MSR_IA32_LASTBRANCHFROMIP:
2683 msr_info->data = svm->vmcb->save.br_from;
2685 case MSR_IA32_LASTBRANCHTOIP:
2686 msr_info->data = svm->vmcb->save.br_to;
2688 case MSR_IA32_LASTINTFROMIP:
2689 msr_info->data = svm->vmcb->save.last_excp_from;
2691 case MSR_IA32_LASTINTTOIP:
2692 msr_info->data = svm->vmcb->save.last_excp_to;
2694 case MSR_VM_HSAVE_PA:
2695 msr_info->data = svm->nested.hsave_msr;
2698 msr_info->data = svm->nested.vm_cr_msr;
2700 case MSR_IA32_SPEC_CTRL:
2701 if (!msr_info->host_initiated &&
2702 !guest_has_spec_ctrl_msr(vcpu))
2705 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2706 msr_info->data = svm->vmcb->save.spec_ctrl;
2708 msr_info->data = svm->spec_ctrl;
2710 case MSR_AMD64_VIRT_SPEC_CTRL:
2711 if (!msr_info->host_initiated &&
2712 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2715 msr_info->data = svm->virt_spec_ctrl;
2717 case MSR_F15H_IC_CFG: {
2721 family = guest_cpuid_family(vcpu);
2722 model = guest_cpuid_model(vcpu);
2724 if (family < 0 || model < 0)
2725 return kvm_get_msr_common(vcpu, msr_info);
2729 if (family == 0x15 &&
2730 (model >= 0x2 && model < 0x20))
2731 msr_info->data = 0x1E;
2734 case MSR_F10H_DECFG:
2735 msr_info->data = svm->msr_decfg;
2738 return kvm_get_msr_common(vcpu, msr_info);
2743 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2745 struct vcpu_svm *svm = to_svm(vcpu);
2746 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->ghcb))
2747 return kvm_complete_insn_gp(vcpu, err);
2749 ghcb_set_sw_exit_info_1(svm->ghcb, 1);
2750 ghcb_set_sw_exit_info_2(svm->ghcb,
2752 SVM_EVTINJ_TYPE_EXEPT |
2757 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2759 struct vcpu_svm *svm = to_svm(vcpu);
2760 int svm_dis, chg_mask;
2762 if (data & ~SVM_VM_CR_VALID_MASK)
2765 chg_mask = SVM_VM_CR_VALID_MASK;
2767 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2768 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2770 svm->nested.vm_cr_msr &= ~chg_mask;
2771 svm->nested.vm_cr_msr |= (data & chg_mask);
2773 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2775 /* check for svm_disable while efer.svme is set */
2776 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2782 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
2784 struct vcpu_svm *svm = to_svm(vcpu);
2787 u32 ecx = msr->index;
2788 u64 data = msr->data;
2790 case MSR_IA32_CR_PAT:
2791 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2793 vcpu->arch.pat = data;
2794 svm->vmcb01.ptr->save.g_pat = data;
2795 if (is_guest_mode(vcpu))
2796 nested_vmcb02_compute_g_pat(svm);
2797 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
2799 case MSR_IA32_SPEC_CTRL:
2800 if (!msr->host_initiated &&
2801 !guest_has_spec_ctrl_msr(vcpu))
2804 if (kvm_spec_ctrl_test_value(data))
2807 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2808 svm->vmcb->save.spec_ctrl = data;
2810 svm->spec_ctrl = data;
2816 * When it's written (to non-zero) for the first time, pass
2820 * The handling of the MSR bitmap for L2 guests is done in
2821 * nested_svm_vmrun_msrpm.
2822 * We update the L1 MSR bit as well since it will end up
2823 * touching the MSR anyway now.
2825 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
2827 case MSR_IA32_PRED_CMD:
2828 if (!msr->host_initiated &&
2829 !guest_has_pred_cmd_msr(vcpu))
2832 if (data & ~PRED_CMD_IBPB)
2834 if (!boot_cpu_has(X86_FEATURE_IBPB))
2839 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2840 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
2842 case MSR_AMD64_VIRT_SPEC_CTRL:
2843 if (!msr->host_initiated &&
2844 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2847 if (data & ~SPEC_CTRL_SSBD)
2850 svm->virt_spec_ctrl = data;
2853 svm->vmcb01.ptr->save.star = data;
2855 #ifdef CONFIG_X86_64
2857 svm->vmcb01.ptr->save.lstar = data;
2860 svm->vmcb01.ptr->save.cstar = data;
2862 case MSR_KERNEL_GS_BASE:
2863 svm->vmcb01.ptr->save.kernel_gs_base = data;
2865 case MSR_SYSCALL_MASK:
2866 svm->vmcb01.ptr->save.sfmask = data;
2869 case MSR_IA32_SYSENTER_CS:
2870 svm->vmcb01.ptr->save.sysenter_cs = data;
2872 case MSR_IA32_SYSENTER_EIP:
2873 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2875 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2876 * when we spoof an Intel vendor ID (for cross vendor migration).
2877 * In this case we use this intercept to track the high
2878 * 32 bit part of these msrs to support Intel's
2879 * implementation of SYSENTER/SYSEXIT.
2881 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2883 case MSR_IA32_SYSENTER_ESP:
2884 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2885 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2889 * TSC_AUX is usually changed only during boot and never read
2890 * directly. Intercept TSC_AUX instead of exposing it to the
2891 * guest via direct_access_msrs, and switch it via user return.
2894 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
2899 svm->tsc_aux = data;
2901 case MSR_IA32_DEBUGCTLMSR:
2902 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
2903 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2907 if (data & DEBUGCTL_RESERVED_BITS)
2910 svm->vmcb->save.dbgctl = data;
2911 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
2912 if (data & (1ULL<<0))
2913 svm_enable_lbrv(vcpu);
2915 svm_disable_lbrv(vcpu);
2917 case MSR_VM_HSAVE_PA:
2918 svm->nested.hsave_msr = data;
2921 return svm_set_vm_cr(vcpu, data);
2923 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2925 case MSR_F10H_DECFG: {
2926 struct kvm_msr_entry msr_entry;
2928 msr_entry.index = msr->index;
2929 if (svm_get_msr_feature(&msr_entry))
2932 /* Check the supported bits */
2933 if (data & ~msr_entry.data)
2936 /* Don't allow the guest to change a bit, #GP */
2937 if (!msr->host_initiated && (data ^ msr_entry.data))
2940 svm->msr_decfg = data;
2943 case MSR_IA32_APICBASE:
2944 if (kvm_vcpu_apicv_active(vcpu))
2945 avic_update_vapic_bar(to_svm(vcpu), data);
2948 return kvm_set_msr_common(vcpu, msr);
2953 static int msr_interception(struct kvm_vcpu *vcpu)
2955 if (to_svm(vcpu)->vmcb->control.exit_info_1)
2956 return kvm_emulate_wrmsr(vcpu);
2958 return kvm_emulate_rdmsr(vcpu);
2961 static int interrupt_window_interception(struct kvm_vcpu *vcpu)
2963 kvm_make_request(KVM_REQ_EVENT, vcpu);
2964 svm_clear_vintr(to_svm(vcpu));
2967 * For AVIC, the only reason to end up here is ExtINTs.
2968 * In this case AVIC was temporarily disabled for
2969 * requesting the IRQ window and we have to re-enable it.
2971 svm_toggle_avic_for_irq_window(vcpu, true);
2973 ++vcpu->stat.irq_window_exits;
2977 static int pause_interception(struct kvm_vcpu *vcpu)
2982 * CPL is not made available for an SEV-ES guest, therefore
2983 * vcpu->arch.preempted_in_kernel can never be true. Just
2984 * set in_kernel to false as well.
2986 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
2988 if (!kvm_pause_in_guest(vcpu->kvm))
2989 grow_ple_window(vcpu);
2991 kvm_vcpu_on_spin(vcpu, in_kernel);
2992 return kvm_skip_emulated_instruction(vcpu);
2995 static int invpcid_interception(struct kvm_vcpu *vcpu)
2997 struct vcpu_svm *svm = to_svm(vcpu);
3001 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
3002 kvm_queue_exception(vcpu, UD_VECTOR);
3007 * For an INVPCID intercept:
3008 * EXITINFO1 provides the linear address of the memory operand.
3009 * EXITINFO2 provides the contents of the register operand.
3011 type = svm->vmcb->control.exit_info_2;
3012 gva = svm->vmcb->control.exit_info_1;
3015 kvm_inject_gp(vcpu, 0);
3019 return kvm_handle_invpcid(vcpu, type, gva);
3022 static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
3023 [SVM_EXIT_READ_CR0] = cr_interception,
3024 [SVM_EXIT_READ_CR3] = cr_interception,
3025 [SVM_EXIT_READ_CR4] = cr_interception,
3026 [SVM_EXIT_READ_CR8] = cr_interception,
3027 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
3028 [SVM_EXIT_WRITE_CR0] = cr_interception,
3029 [SVM_EXIT_WRITE_CR3] = cr_interception,
3030 [SVM_EXIT_WRITE_CR4] = cr_interception,
3031 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
3032 [SVM_EXIT_READ_DR0] = dr_interception,
3033 [SVM_EXIT_READ_DR1] = dr_interception,
3034 [SVM_EXIT_READ_DR2] = dr_interception,
3035 [SVM_EXIT_READ_DR3] = dr_interception,
3036 [SVM_EXIT_READ_DR4] = dr_interception,
3037 [SVM_EXIT_READ_DR5] = dr_interception,
3038 [SVM_EXIT_READ_DR6] = dr_interception,
3039 [SVM_EXIT_READ_DR7] = dr_interception,
3040 [SVM_EXIT_WRITE_DR0] = dr_interception,
3041 [SVM_EXIT_WRITE_DR1] = dr_interception,
3042 [SVM_EXIT_WRITE_DR2] = dr_interception,
3043 [SVM_EXIT_WRITE_DR3] = dr_interception,
3044 [SVM_EXIT_WRITE_DR4] = dr_interception,
3045 [SVM_EXIT_WRITE_DR5] = dr_interception,
3046 [SVM_EXIT_WRITE_DR6] = dr_interception,
3047 [SVM_EXIT_WRITE_DR7] = dr_interception,
3048 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
3049 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
3050 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
3051 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
3052 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
3053 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
3054 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
3055 [SVM_EXIT_INTR] = intr_interception,
3056 [SVM_EXIT_NMI] = nmi_interception,
3057 [SVM_EXIT_SMI] = kvm_emulate_as_nop,
3058 [SVM_EXIT_INIT] = kvm_emulate_as_nop,
3059 [SVM_EXIT_VINTR] = interrupt_window_interception,
3060 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
3061 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
3062 [SVM_EXIT_IRET] = iret_interception,
3063 [SVM_EXIT_INVD] = kvm_emulate_invd,
3064 [SVM_EXIT_PAUSE] = pause_interception,
3065 [SVM_EXIT_HLT] = kvm_emulate_halt,
3066 [SVM_EXIT_INVLPG] = invlpg_interception,
3067 [SVM_EXIT_INVLPGA] = invlpga_interception,
3068 [SVM_EXIT_IOIO] = io_interception,
3069 [SVM_EXIT_MSR] = msr_interception,
3070 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
3071 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
3072 [SVM_EXIT_VMRUN] = vmrun_interception,
3073 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
3074 [SVM_EXIT_VMLOAD] = vmload_interception,
3075 [SVM_EXIT_VMSAVE] = vmsave_interception,
3076 [SVM_EXIT_STGI] = stgi_interception,
3077 [SVM_EXIT_CLGI] = clgi_interception,
3078 [SVM_EXIT_SKINIT] = skinit_interception,
3079 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
3080 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
3081 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
3082 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
3083 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
3084 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
3085 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
3086 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
3087 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
3088 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
3089 [SVM_EXIT_INVPCID] = invpcid_interception,
3090 [SVM_EXIT_NPF] = npf_interception,
3091 [SVM_EXIT_RSM] = rsm_interception,
3092 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3093 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
3094 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
3097 static void dump_vmcb(struct kvm_vcpu *vcpu)
3099 struct vcpu_svm *svm = to_svm(vcpu);
3100 struct vmcb_control_area *control = &svm->vmcb->control;
3101 struct vmcb_save_area *save = &svm->vmcb->save;
3102 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
3104 if (!dump_invalid_vmcb) {
3105 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3109 pr_err("VMCB Control Area:\n");
3110 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3111 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
3112 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3113 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
3114 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
3115 pr_err("%-20s%08x %08x\n", "intercepts:",
3116 control->intercepts[INTERCEPT_WORD3],
3117 control->intercepts[INTERCEPT_WORD4]);
3118 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3119 pr_err("%-20s%d\n", "pause filter threshold:",
3120 control->pause_filter_thresh);
3121 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3122 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3123 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3124 pr_err("%-20s%d\n", "asid:", control->asid);
3125 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3126 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3127 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3128 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3129 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3130 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3131 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3132 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3133 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3134 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3135 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3136 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
3137 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
3138 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3139 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3140 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
3141 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
3142 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3143 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3144 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
3145 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
3146 pr_err("VMCB State Save Area:\n");
3147 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3149 save->es.selector, save->es.attrib,
3150 save->es.limit, save->es.base);
3151 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3153 save->cs.selector, save->cs.attrib,
3154 save->cs.limit, save->cs.base);
3155 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3157 save->ss.selector, save->ss.attrib,
3158 save->ss.limit, save->ss.base);
3159 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3161 save->ds.selector, save->ds.attrib,
3162 save->ds.limit, save->ds.base);
3163 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3165 save01->fs.selector, save01->fs.attrib,
3166 save01->fs.limit, save01->fs.base);
3167 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3169 save01->gs.selector, save01->gs.attrib,
3170 save01->gs.limit, save01->gs.base);
3171 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3173 save->gdtr.selector, save->gdtr.attrib,
3174 save->gdtr.limit, save->gdtr.base);
3175 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3177 save01->ldtr.selector, save01->ldtr.attrib,
3178 save01->ldtr.limit, save01->ldtr.base);
3179 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3181 save->idtr.selector, save->idtr.attrib,
3182 save->idtr.limit, save->idtr.base);
3183 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3185 save01->tr.selector, save01->tr.attrib,
3186 save01->tr.limit, save01->tr.base);
3187 pr_err("cpl: %d efer: %016llx\n",
3188 save->cpl, save->efer);
3189 pr_err("%-15s %016llx %-13s %016llx\n",
3190 "cr0:", save->cr0, "cr2:", save->cr2);
3191 pr_err("%-15s %016llx %-13s %016llx\n",
3192 "cr3:", save->cr3, "cr4:", save->cr4);
3193 pr_err("%-15s %016llx %-13s %016llx\n",
3194 "dr6:", save->dr6, "dr7:", save->dr7);
3195 pr_err("%-15s %016llx %-13s %016llx\n",
3196 "rip:", save->rip, "rflags:", save->rflags);
3197 pr_err("%-15s %016llx %-13s %016llx\n",
3198 "rsp:", save->rsp, "rax:", save->rax);
3199 pr_err("%-15s %016llx %-13s %016llx\n",
3200 "star:", save01->star, "lstar:", save01->lstar);
3201 pr_err("%-15s %016llx %-13s %016llx\n",
3202 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
3203 pr_err("%-15s %016llx %-13s %016llx\n",
3204 "kernel_gs_base:", save01->kernel_gs_base,
3205 "sysenter_cs:", save01->sysenter_cs);
3206 pr_err("%-15s %016llx %-13s %016llx\n",
3207 "sysenter_esp:", save01->sysenter_esp,
3208 "sysenter_eip:", save01->sysenter_eip);
3209 pr_err("%-15s %016llx %-13s %016llx\n",
3210 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3211 pr_err("%-15s %016llx %-13s %016llx\n",
3212 "br_from:", save->br_from, "br_to:", save->br_to);
3213 pr_err("%-15s %016llx %-13s %016llx\n",
3214 "excp_from:", save->last_excp_from,
3215 "excp_to:", save->last_excp_to);
3218 static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3220 if (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3221 svm_exit_handlers[exit_code])
3224 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3226 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3227 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3228 vcpu->run->internal.ndata = 2;
3229 vcpu->run->internal.data[0] = exit_code;
3230 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3235 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
3237 if (svm_handle_invalid_exit(vcpu, exit_code))
3240 #ifdef CONFIG_RETPOLINE
3241 if (exit_code == SVM_EXIT_MSR)
3242 return msr_interception(vcpu);
3243 else if (exit_code == SVM_EXIT_VINTR)
3244 return interrupt_window_interception(vcpu);
3245 else if (exit_code == SVM_EXIT_INTR)
3246 return intr_interception(vcpu);
3247 else if (exit_code == SVM_EXIT_HLT)
3248 return kvm_emulate_halt(vcpu);
3249 else if (exit_code == SVM_EXIT_NPF)
3250 return npf_interception(vcpu);
3252 return svm_exit_handlers[exit_code](vcpu);
3255 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2,
3256 u32 *intr_info, u32 *error_code)
3258 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3260 *info1 = control->exit_info_1;
3261 *info2 = control->exit_info_2;
3262 *intr_info = control->exit_int_info;
3263 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3264 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3265 *error_code = control->exit_int_info_err;
3270 static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
3272 struct vcpu_svm *svm = to_svm(vcpu);
3273 struct kvm_run *kvm_run = vcpu->run;
3274 u32 exit_code = svm->vmcb->control.exit_code;
3276 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
3278 /* SEV-ES guests must use the CR write traps to track CR registers. */
3279 if (!sev_es_guest(vcpu->kvm)) {
3280 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3281 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3283 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3286 if (is_guest_mode(vcpu)) {
3289 trace_kvm_nested_vmexit(exit_code, vcpu, KVM_ISA_SVM);
3291 vmexit = nested_svm_exit_special(svm);
3293 if (vmexit == NESTED_EXIT_CONTINUE)
3294 vmexit = nested_svm_exit_handled(svm);
3296 if (vmexit == NESTED_EXIT_DONE)
3300 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3301 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3302 kvm_run->fail_entry.hardware_entry_failure_reason
3303 = svm->vmcb->control.exit_code;
3304 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
3309 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
3310 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
3311 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3312 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
3313 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
3315 __func__, svm->vmcb->control.exit_int_info,
3318 if (exit_fastpath != EXIT_FASTPATH_NONE)
3321 return svm_invoke_exit_handler(vcpu, exit_code);
3324 static void reload_tss(struct kvm_vcpu *vcpu)
3326 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3328 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
3332 static void pre_svm_run(struct kvm_vcpu *vcpu)
3334 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3335 struct vcpu_svm *svm = to_svm(vcpu);
3338 * If the previous vmrun of the vmcb occurred on a different physical
3339 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3340 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3342 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
3343 svm->current_vmcb->asid_generation = 0;
3344 vmcb_mark_all_dirty(svm->vmcb);
3345 svm->current_vmcb->cpu = vcpu->cpu;
3348 if (sev_guest(vcpu->kvm))
3349 return pre_sev_run(svm, vcpu->cpu);
3351 /* FIXME: handle wraparound of asid_generation */
3352 if (svm->current_vmcb->asid_generation != sd->asid_generation)
3356 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3358 struct vcpu_svm *svm = to_svm(vcpu);
3360 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3361 vcpu->arch.hflags |= HF_NMI_MASK;
3362 if (!sev_es_guest(vcpu->kvm))
3363 svm_set_intercept(svm, INTERCEPT_IRET);
3364 ++vcpu->stat.nmi_injections;
3367 static void svm_set_irq(struct kvm_vcpu *vcpu)
3369 struct vcpu_svm *svm = to_svm(vcpu);
3371 BUG_ON(!(gif_set(svm)));
3373 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3374 ++vcpu->stat.irq_injections;
3376 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3377 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
3380 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3382 struct vcpu_svm *svm = to_svm(vcpu);
3385 * SEV-ES guests must always keep the CR intercepts cleared. CR
3386 * tracking is done using the CR write traps.
3388 if (sev_es_guest(vcpu->kvm))
3391 if (nested_svm_virtualize_tpr(vcpu))
3394 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
3400 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
3403 bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
3405 struct vcpu_svm *svm = to_svm(vcpu);
3406 struct vmcb *vmcb = svm->vmcb;
3412 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3415 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
3416 (vcpu->arch.hflags & HF_NMI_MASK);
3421 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3423 struct vcpu_svm *svm = to_svm(vcpu);
3424 if (svm->nested.nested_run_pending)
3427 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3428 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3431 return !svm_nmi_blocked(vcpu);
3434 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3436 return !!(vcpu->arch.hflags & HF_NMI_MASK);
3439 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3441 struct vcpu_svm *svm = to_svm(vcpu);
3444 vcpu->arch.hflags |= HF_NMI_MASK;
3445 if (!sev_es_guest(vcpu->kvm))
3446 svm_set_intercept(svm, INTERCEPT_IRET);
3448 vcpu->arch.hflags &= ~HF_NMI_MASK;
3449 if (!sev_es_guest(vcpu->kvm))
3450 svm_clr_intercept(svm, INTERCEPT_IRET);
3454 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
3456 struct vcpu_svm *svm = to_svm(vcpu);
3457 struct vmcb *vmcb = svm->vmcb;
3462 if (sev_es_guest(vcpu->kvm)) {
3464 * SEV-ES guests to not expose RFLAGS. Use the VMCB interrupt mask
3465 * bit to determine the state of the IF flag.
3467 if (!(vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK))
3469 } else if (is_guest_mode(vcpu)) {
3470 /* As long as interrupts are being delivered... */
3471 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
3472 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
3473 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3476 /* ... vmexits aren't blocked by the interrupt shadow */
3477 if (nested_exit_on_intr(svm))
3480 if (!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3484 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
3487 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3489 struct vcpu_svm *svm = to_svm(vcpu);
3490 if (svm->nested.nested_run_pending)
3494 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3495 * e.g. if the IRQ arrived asynchronously after checking nested events.
3497 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
3500 return !svm_interrupt_blocked(vcpu);
3503 static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
3505 struct vcpu_svm *svm = to_svm(vcpu);
3508 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3509 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3510 * get that intercept, this function will be called again though and
3511 * we'll get the vintr intercept. However, if the vGIF feature is
3512 * enabled, the STGI interception will not occur. Enable the irq
3513 * window under the assumption that the hardware will set the GIF.
3515 if (vgif_enabled(svm) || gif_set(svm)) {
3517 * IRQ window is not needed when AVIC is enabled,
3518 * unless we have pending ExtINT since it cannot be injected
3519 * via AVIC. In such case, we need to temporarily disable AVIC,
3520 * and fallback to injecting IRQ via V_IRQ.
3522 svm_toggle_avic_for_irq_window(vcpu, false);
3527 static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
3529 struct vcpu_svm *svm = to_svm(vcpu);
3531 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
3532 return; /* IRET will cause a vm exit */
3534 if (!gif_set(svm)) {
3535 if (vgif_enabled(svm))
3536 svm_set_intercept(svm, INTERCEPT_STGI);
3537 return; /* STGI will cause a vm exit */
3541 * Something prevents NMI from been injected. Single step over possible
3542 * problem (IRET or exception injection or interrupt shadow)
3544 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
3545 svm->nmi_singlestep = true;
3546 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3549 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3554 static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3559 void svm_flush_tlb(struct kvm_vcpu *vcpu)
3561 struct vcpu_svm *svm = to_svm(vcpu);
3564 * Flush only the current ASID even if the TLB flush was invoked via
3565 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3566 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3567 * unconditionally does a TLB flush on both nested VM-Enter and nested
3568 * VM-Exit (via kvm_mmu_reset_context()).
3570 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3571 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3573 svm->current_vmcb->asid_generation--;
3576 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3578 struct vcpu_svm *svm = to_svm(vcpu);
3580 invlpga(gva, svm->vmcb->control.asid);
3583 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3585 struct vcpu_svm *svm = to_svm(vcpu);
3587 if (nested_svm_virtualize_tpr(vcpu))
3590 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
3591 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
3592 kvm_set_cr8(vcpu, cr8);
3596 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3598 struct vcpu_svm *svm = to_svm(vcpu);
3601 if (nested_svm_virtualize_tpr(vcpu) ||
3602 kvm_vcpu_apicv_active(vcpu))
3605 cr8 = kvm_get_cr8(vcpu);
3606 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3607 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3610 static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
3612 struct vcpu_svm *svm = to_svm(vcpu);
3615 u32 exitintinfo = svm->vmcb->control.exit_int_info;
3616 unsigned int3_injected = svm->int3_injected;
3618 svm->int3_injected = 0;
3621 * If we've made progress since setting HF_IRET_MASK, we've
3622 * executed an IRET and can allow NMI injection.
3624 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3625 (sev_es_guest(vcpu->kvm) ||
3626 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3627 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3628 kvm_make_request(KVM_REQ_EVENT, vcpu);
3631 vcpu->arch.nmi_injected = false;
3632 kvm_clear_exception_queue(vcpu);
3633 kvm_clear_interrupt_queue(vcpu);
3635 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3638 kvm_make_request(KVM_REQ_EVENT, vcpu);
3640 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3641 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3644 case SVM_EXITINTINFO_TYPE_NMI:
3645 vcpu->arch.nmi_injected = true;
3647 case SVM_EXITINTINFO_TYPE_EXEPT:
3649 * Never re-inject a #VC exception.
3651 if (vector == X86_TRAP_VC)
3655 * In case of software exceptions, do not reinject the vector,
3656 * but re-execute the instruction instead. Rewind RIP first
3657 * if we emulated INT3 before.
3659 if (kvm_exception_is_soft(vector)) {
3660 if (vector == BP_VECTOR && int3_injected &&
3661 kvm_is_linear_rip(vcpu, svm->int3_rip))
3663 kvm_rip_read(vcpu) - int3_injected);
3666 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3667 u32 err = svm->vmcb->control.exit_int_info_err;
3668 kvm_requeue_exception_e(vcpu, vector, err);
3671 kvm_requeue_exception(vcpu, vector);
3673 case SVM_EXITINTINFO_TYPE_INTR:
3674 kvm_queue_interrupt(vcpu, vector, false);
3681 static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3683 struct vcpu_svm *svm = to_svm(vcpu);
3684 struct vmcb_control_area *control = &svm->vmcb->control;
3686 control->exit_int_info = control->event_inj;
3687 control->exit_int_info_err = control->event_inj_err;
3688 control->event_inj = 0;
3689 svm_complete_interrupts(vcpu);
3692 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
3694 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
3695 to_svm(vcpu)->vmcb->control.exit_info_1)
3696 return handle_fastpath_set_msr_irqoff(vcpu);
3698 return EXIT_FASTPATH_NONE;
3701 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
3703 struct vcpu_svm *svm = to_svm(vcpu);
3704 unsigned long vmcb_pa = svm->current_vmcb->pa;
3706 kvm_guest_enter_irqoff();
3708 if (sev_es_guest(vcpu->kvm)) {
3709 __svm_sev_es_vcpu_run(vmcb_pa);
3711 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3714 * Use a single vmcb (vmcb01 because it's always valid) for
3715 * context switching guest state via VMLOAD/VMSAVE, that way
3716 * the state doesn't need to be copied between vmcb01 and
3717 * vmcb02 when switching vmcbs for nested virtualization.
3719 vmload(svm->vmcb01.pa);
3720 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
3721 vmsave(svm->vmcb01.pa);
3723 vmload(__sme_page_pa(sd->save_area));
3726 kvm_guest_exit_irqoff();
3729 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
3731 struct vcpu_svm *svm = to_svm(vcpu);
3733 trace_kvm_entry(vcpu);
3735 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3736 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3737 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3740 * Disable singlestep if we're injecting an interrupt/exception.
3741 * We don't want our modified rflags to be pushed on the stack where
3742 * we might not be able to easily reset them if we disabled NMI
3745 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3747 * Event injection happens before external interrupts cause a
3748 * vmexit and interrupts are disabled here, so smp_send_reschedule
3749 * is enough to force an immediate vmexit.
3751 disable_nmi_singlestep(svm);
3752 smp_send_reschedule(vcpu->cpu);
3757 sync_lapic_to_cr8(vcpu);
3759 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3760 svm->vmcb->control.asid = svm->asid;
3761 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3763 svm->vmcb->save.cr2 = vcpu->arch.cr2;
3766 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3769 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
3770 svm_set_dr6(svm, vcpu->arch.dr6);
3772 svm_set_dr6(svm, DR6_ACTIVE_LOW);
3775 kvm_load_guest_xsave_state(vcpu);
3777 kvm_wait_lapic_expire(vcpu);
3780 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3781 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3782 * is no need to worry about the conditional branch over the wrmsr
3783 * being speculatively taken.
3785 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3786 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
3788 svm_vcpu_enter_exit(vcpu);
3791 * We do not use IBRS in the kernel. If this vCPU has used the
3792 * SPEC_CTRL MSR it may have left it on; save the value and
3793 * turn it off. This is much more efficient than blindly adding
3794 * it to the atomic save/restore list. Especially as the former
3795 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3797 * For non-nested case:
3798 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3802 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3805 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3806 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
3807 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
3809 if (!sev_es_guest(vcpu->kvm))
3812 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3813 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3815 if (!sev_es_guest(vcpu->kvm)) {
3816 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3817 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3818 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3819 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3822 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3823 kvm_before_interrupt(vcpu);
3825 kvm_load_host_xsave_state(vcpu);
3828 /* Any pending NMI will happen here */
3830 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3831 kvm_after_interrupt(vcpu);
3833 sync_cr8_to_lapic(vcpu);
3836 if (is_guest_mode(vcpu)) {
3837 nested_sync_control_from_vmcb02(svm);
3838 svm->nested.nested_run_pending = 0;
3841 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3842 vmcb_mark_all_clean(svm->vmcb);
3844 /* if exit due to PF check for async PF */
3845 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3846 vcpu->arch.apf.host_apf_flags =
3847 kvm_read_and_reset_apf_flags();
3850 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3851 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3855 * We need to handle MC intercepts here before the vcpu has a chance to
3856 * change the physical cpu
3858 if (unlikely(svm->vmcb->control.exit_code ==
3859 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3860 svm_handle_mce(vcpu);
3862 svm_complete_interrupts(vcpu);
3864 if (is_guest_mode(vcpu))
3865 return EXIT_FASTPATH_NONE;
3867 return svm_exit_handlers_fastpath(vcpu);
3870 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
3873 struct vcpu_svm *svm = to_svm(vcpu);
3877 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
3878 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
3880 /* Loading L2's CR3 is handled by enter_svm_guest_mode. */
3881 if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3883 cr3 = vcpu->arch.cr3;
3884 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
3885 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
3887 /* PCID in the guest should be impossible with a 32-bit MMU. */
3888 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3892 svm->vmcb->save.cr3 = cr3;
3893 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
3896 static int is_disabled(void)
3900 rdmsrl(MSR_VM_CR, vm_cr);
3901 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3908 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3911 * Patch in the VMMCALL instruction:
3913 hypercall[0] = 0x0f;
3914 hypercall[1] = 0x01;
3915 hypercall[2] = 0xd9;
3918 static int __init svm_check_processor_compat(void)
3923 static bool svm_cpu_has_accelerated_tpr(void)
3929 * The kvm parameter can be NULL (module initialization, or invocation before
3930 * VM creation). Be sure to check the kvm parameter before using it.
3932 static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
3935 case MSR_IA32_MCG_EXT_CTL:
3936 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3938 case MSR_IA32_SMBASE:
3939 /* SEV-ES guests do not support SMM, so report false */
3940 if (kvm && sev_es_guest(kvm))
3950 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3955 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
3957 struct vcpu_svm *svm = to_svm(vcpu);
3958 struct kvm_cpuid_entry2 *best;
3960 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3961 boot_cpu_has(X86_FEATURE_XSAVE) &&
3962 boot_cpu_has(X86_FEATURE_XSAVES);
3964 /* Update nrips enabled cache */
3965 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
3966 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
3968 svm_recalc_instruction_intercepts(vcpu, svm);
3970 /* For sev guests, the memory encryption bit is not reserved in CR3. */
3971 if (sev_guest(vcpu->kvm)) {
3972 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
3974 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
3977 if (kvm_vcpu_apicv_active(vcpu)) {
3979 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3980 * is exposed to the guest, disable AVIC.
3982 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3983 kvm_request_apicv_update(vcpu->kvm, false,
3984 APICV_INHIBIT_REASON_X2APIC);
3987 * Currently, AVIC does not work with nested virtualization.
3988 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3990 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3991 kvm_request_apicv_update(vcpu->kvm, false,
3992 APICV_INHIBIT_REASON_NESTED);
3995 if (guest_cpuid_is_intel(vcpu)) {
3997 * We must intercept SYSENTER_EIP and SYSENTER_ESP
3998 * accesses because the processor only stores 32 bits.
3999 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
4001 svm_set_intercept(svm, INTERCEPT_VMLOAD);
4002 svm_set_intercept(svm, INTERCEPT_VMSAVE);
4003 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4005 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
4006 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
4009 * If hardware supports Virtual VMLOAD VMSAVE then enable it
4010 * in VMCB and clear intercepts to avoid #VMEXIT.
4013 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
4014 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
4015 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
4017 /* No need to intercept these MSRs */
4018 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
4019 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
4023 static bool svm_has_wbinvd_exit(void)
4028 #define PRE_EX(exit) { .exit_code = (exit), \
4029 .stage = X86_ICPT_PRE_EXCEPT, }
4030 #define POST_EX(exit) { .exit_code = (exit), \
4031 .stage = X86_ICPT_POST_EXCEPT, }
4032 #define POST_MEM(exit) { .exit_code = (exit), \
4033 .stage = X86_ICPT_POST_MEMACCESS, }
4035 static const struct __x86_intercept {
4037 enum x86_intercept_stage stage;
4038 } x86_intercept_map[] = {
4039 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
4040 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
4041 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
4042 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
4043 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
4044 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
4045 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
4046 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
4047 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
4048 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
4049 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
4050 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
4051 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
4052 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
4053 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
4054 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
4055 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
4056 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
4057 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
4058 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
4059 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
4060 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
4061 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
4062 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
4063 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
4064 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
4065 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
4066 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
4067 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
4068 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
4069 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
4070 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
4071 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
4072 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
4073 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
4074 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
4075 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
4076 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
4077 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
4078 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
4079 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
4080 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
4081 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
4082 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
4083 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4084 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
4085 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
4092 static int svm_check_intercept(struct kvm_vcpu *vcpu,
4093 struct x86_instruction_info *info,
4094 enum x86_intercept_stage stage,
4095 struct x86_exception *exception)
4097 struct vcpu_svm *svm = to_svm(vcpu);
4098 int vmexit, ret = X86EMUL_CONTINUE;
4099 struct __x86_intercept icpt_info;
4100 struct vmcb *vmcb = svm->vmcb;
4102 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4105 icpt_info = x86_intercept_map[info->intercept];
4107 if (stage != icpt_info.stage)
4110 switch (icpt_info.exit_code) {
4111 case SVM_EXIT_READ_CR0:
4112 if (info->intercept == x86_intercept_cr_read)
4113 icpt_info.exit_code += info->modrm_reg;
4115 case SVM_EXIT_WRITE_CR0: {
4116 unsigned long cr0, val;
4118 if (info->intercept == x86_intercept_cr_write)
4119 icpt_info.exit_code += info->modrm_reg;
4121 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4122 info->intercept == x86_intercept_clts)
4125 if (!(vmcb_is_intercept(&svm->nested.ctl,
4126 INTERCEPT_SELECTIVE_CR0)))
4129 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4130 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4132 if (info->intercept == x86_intercept_lmsw) {
4135 /* lmsw can't clear PE - catch this here */
4136 if (cr0 & X86_CR0_PE)
4141 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4145 case SVM_EXIT_READ_DR0:
4146 case SVM_EXIT_WRITE_DR0:
4147 icpt_info.exit_code += info->modrm_reg;
4150 if (info->intercept == x86_intercept_wrmsr)
4151 vmcb->control.exit_info_1 = 1;
4153 vmcb->control.exit_info_1 = 0;
4155 case SVM_EXIT_PAUSE:
4157 * We get this for NOP only, but pause
4158 * is rep not, check this here
4160 if (info->rep_prefix != REPE_PREFIX)
4163 case SVM_EXIT_IOIO: {
4167 if (info->intercept == x86_intercept_in ||
4168 info->intercept == x86_intercept_ins) {
4169 exit_info = ((info->src_val & 0xffff) << 16) |
4171 bytes = info->dst_bytes;
4173 exit_info = (info->dst_val & 0xffff) << 16;
4174 bytes = info->src_bytes;
4177 if (info->intercept == x86_intercept_outs ||
4178 info->intercept == x86_intercept_ins)
4179 exit_info |= SVM_IOIO_STR_MASK;
4181 if (info->rep_prefix)
4182 exit_info |= SVM_IOIO_REP_MASK;
4184 bytes = min(bytes, 4u);
4186 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4188 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4190 vmcb->control.exit_info_1 = exit_info;
4191 vmcb->control.exit_info_2 = info->next_rip;
4199 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4200 if (static_cpu_has(X86_FEATURE_NRIPS))
4201 vmcb->control.next_rip = info->next_rip;
4202 vmcb->control.exit_code = icpt_info.exit_code;
4203 vmexit = nested_svm_exit_handled(svm);
4205 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4212 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
4216 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4218 if (!kvm_pause_in_guest(vcpu->kvm))
4219 shrink_ple_window(vcpu);
4222 static void svm_setup_mce(struct kvm_vcpu *vcpu)
4224 /* [63:9] are reserved. */
4225 vcpu->arch.mcg_cap &= 0x1ff;
4228 bool svm_smi_blocked(struct kvm_vcpu *vcpu)
4230 struct vcpu_svm *svm = to_svm(vcpu);
4232 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4236 return is_smm(vcpu);
4239 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
4241 struct vcpu_svm *svm = to_svm(vcpu);
4242 if (svm->nested.nested_run_pending)
4245 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4246 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
4249 return !svm_smi_blocked(vcpu);
4252 static int svm_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4254 struct vcpu_svm *svm = to_svm(vcpu);
4257 if (is_guest_mode(vcpu)) {
4258 /* FED8h - SVM Guest */
4259 put_smstate(u64, smstate, 0x7ed8, 1);
4260 /* FEE0h - SVM Guest VMCB Physical Address */
4261 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
4263 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4264 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4265 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4267 ret = nested_svm_vmexit(svm);
4274 static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
4276 struct vcpu_svm *svm = to_svm(vcpu);
4277 struct kvm_host_map map;
4280 if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) {
4281 u64 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4282 u64 guest = GET_SMSTATE(u64, smstate, 0x7ed8);
4283 u64 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4286 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4289 if (!(saved_efer & EFER_SVME))
4292 if (kvm_vcpu_map(vcpu,
4293 gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4296 if (svm_allocate_nested(svm))
4299 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, map.hva);
4300 kvm_vcpu_unmap(vcpu, &map, true);
4307 static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
4309 struct vcpu_svm *svm = to_svm(vcpu);
4311 if (!gif_set(svm)) {
4312 if (vgif_enabled(svm))
4313 svm_set_intercept(svm, INTERCEPT_STGI);
4314 /* STGI will cause a vm exit */
4316 /* We must be in SMM; RSM will cause a vmexit anyway. */
4320 static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, void *insn, int insn_len)
4322 bool smep, smap, is_user;
4326 * When the guest is an SEV-ES guest, emulation is not possible.
4328 if (sev_es_guest(vcpu->kvm))
4332 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4335 * When CPU raise #NPF on guest data access and vCPU CR4.SMAP=1, it is
4336 * possible that CPU microcode implementing DecodeAssist will fail
4337 * to read bytes of instruction which caused #NPF. In this case,
4338 * GuestIntrBytes field of the VMCB on a VMEXIT will incorrectly
4339 * return 0 instead of the correct guest instruction bytes.
4341 * This happens because CPU microcode reading instruction bytes
4342 * uses a special opcode which attempts to read data using CPL=0
4343 * privileges. The microcode reads CS:RIP and if it hits a SMAP
4344 * fault, it gives up and returns no instruction bytes.
4347 * We reach here in case CPU supports DecodeAssist, raised #NPF and
4348 * returned 0 in GuestIntrBytes field of the VMCB.
4349 * First, errata can only be triggered in case vCPU CR4.SMAP=1.
4350 * Second, if vCPU CR4.SMEP=1, errata could only be triggered
4351 * in case vCPU CPL==3 (Because otherwise guest would have triggered
4352 * a SMEP fault instead of #NPF).
4353 * Otherwise, vCPU CR4.SMEP=0, errata could be triggered by any vCPU CPL.
4354 * As most guests enable SMAP if they have also enabled SMEP, use above
4355 * logic in order to attempt minimize false-positive of detecting errata
4356 * while still preserving all cases semantic correctness.
4359 * To determine what instruction the guest was executing, the hypervisor
4360 * will have to decode the instruction at the instruction pointer.
4362 * In non SEV guest, hypervisor will be able to read the guest
4363 * memory to decode the instruction pointer when insn_len is zero
4364 * so we return true to indicate that decoding is possible.
4366 * But in the SEV guest, the guest memory is encrypted with the
4367 * guest specific key and hypervisor will not be able to decode the
4368 * instruction pointer so we will not able to workaround it. Lets
4369 * print the error and request to kill the guest.
4371 if (likely(!insn || insn_len))
4375 * If RIP is invalid, go ahead with emulation which will cause an
4376 * internal error exit.
4378 if (!kvm_vcpu_gfn_to_memslot(vcpu, kvm_rip_read(vcpu) >> PAGE_SHIFT))
4381 cr4 = kvm_read_cr4(vcpu);
4382 smep = cr4 & X86_CR4_SMEP;
4383 smap = cr4 & X86_CR4_SMAP;
4384 is_user = svm_get_cpl(vcpu) == 3;
4385 if (smap && (!smep || is_user)) {
4386 if (!sev_guest(vcpu->kvm))
4389 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
4390 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4396 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4398 struct vcpu_svm *svm = to_svm(vcpu);
4401 * TODO: Last condition latch INIT signals on vCPU when
4402 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
4403 * To properly emulate the INIT intercept,
4404 * svm_check_nested_events() should call nested_svm_vmexit()
4405 * if an INIT signal is pending.
4407 return !gif_set(svm) ||
4408 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
4411 static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4413 if (!sev_es_guest(vcpu->kvm))
4414 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4416 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4419 static void svm_vm_destroy(struct kvm *kvm)
4421 avic_vm_destroy(kvm);
4422 sev_vm_destroy(kvm);
4425 static int svm_vm_init(struct kvm *kvm)
4427 if (!pause_filter_count || !pause_filter_thresh)
4428 kvm->arch.pause_in_guest = true;
4431 int ret = avic_vm_init(kvm);
4436 kvm_apicv_init(kvm, avic);
4440 static struct kvm_x86_ops svm_x86_ops __initdata = {
4441 .hardware_unsetup = svm_hardware_teardown,
4442 .hardware_enable = svm_hardware_enable,
4443 .hardware_disable = svm_hardware_disable,
4444 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
4445 .has_emulated_msr = svm_has_emulated_msr,
4447 .vcpu_create = svm_create_vcpu,
4448 .vcpu_free = svm_free_vcpu,
4449 .vcpu_reset = svm_vcpu_reset,
4451 .vm_size = sizeof(struct kvm_svm),
4452 .vm_init = svm_vm_init,
4453 .vm_destroy = svm_vm_destroy,
4455 .prepare_guest_switch = svm_prepare_guest_switch,
4456 .vcpu_load = svm_vcpu_load,
4457 .vcpu_put = svm_vcpu_put,
4458 .vcpu_blocking = svm_vcpu_blocking,
4459 .vcpu_unblocking = svm_vcpu_unblocking,
4461 .update_exception_bitmap = svm_update_exception_bitmap,
4462 .get_msr_feature = svm_get_msr_feature,
4463 .get_msr = svm_get_msr,
4464 .set_msr = svm_set_msr,
4465 .get_segment_base = svm_get_segment_base,
4466 .get_segment = svm_get_segment,
4467 .set_segment = svm_set_segment,
4468 .get_cpl = svm_get_cpl,
4469 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
4470 .set_cr0 = svm_set_cr0,
4471 .is_valid_cr4 = svm_is_valid_cr4,
4472 .set_cr4 = svm_set_cr4,
4473 .set_efer = svm_set_efer,
4474 .get_idt = svm_get_idt,
4475 .set_idt = svm_set_idt,
4476 .get_gdt = svm_get_gdt,
4477 .set_gdt = svm_set_gdt,
4478 .set_dr7 = svm_set_dr7,
4479 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
4480 .cache_reg = svm_cache_reg,
4481 .get_rflags = svm_get_rflags,
4482 .set_rflags = svm_set_rflags,
4484 .tlb_flush_all = svm_flush_tlb,
4485 .tlb_flush_current = svm_flush_tlb,
4486 .tlb_flush_gva = svm_flush_tlb_gva,
4487 .tlb_flush_guest = svm_flush_tlb,
4489 .run = svm_vcpu_run,
4490 .handle_exit = handle_exit,
4491 .skip_emulated_instruction = skip_emulated_instruction,
4492 .update_emulated_instruction = NULL,
4493 .set_interrupt_shadow = svm_set_interrupt_shadow,
4494 .get_interrupt_shadow = svm_get_interrupt_shadow,
4495 .patch_hypercall = svm_patch_hypercall,
4496 .set_irq = svm_set_irq,
4497 .set_nmi = svm_inject_nmi,
4498 .queue_exception = svm_queue_exception,
4499 .cancel_injection = svm_cancel_injection,
4500 .interrupt_allowed = svm_interrupt_allowed,
4501 .nmi_allowed = svm_nmi_allowed,
4502 .get_nmi_mask = svm_get_nmi_mask,
4503 .set_nmi_mask = svm_set_nmi_mask,
4504 .enable_nmi_window = svm_enable_nmi_window,
4505 .enable_irq_window = svm_enable_irq_window,
4506 .update_cr8_intercept = svm_update_cr8_intercept,
4507 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
4508 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
4509 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
4510 .pre_update_apicv_exec_ctrl = svm_pre_update_apicv_exec_ctrl,
4511 .load_eoi_exitmap = svm_load_eoi_exitmap,
4512 .hwapic_irr_update = svm_hwapic_irr_update,
4513 .hwapic_isr_update = svm_hwapic_isr_update,
4514 .sync_pir_to_irr = kvm_lapic_find_highest_irr,
4515 .apicv_post_state_restore = avic_post_state_restore,
4517 .set_tss_addr = svm_set_tss_addr,
4518 .set_identity_map_addr = svm_set_identity_map_addr,
4519 .get_mt_mask = svm_get_mt_mask,
4521 .get_exit_info = svm_get_exit_info,
4523 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
4525 .has_wbinvd_exit = svm_has_wbinvd_exit,
4527 .write_l1_tsc_offset = svm_write_l1_tsc_offset,
4529 .load_mmu_pgd = svm_load_mmu_pgd,
4531 .check_intercept = svm_check_intercept,
4532 .handle_exit_irqoff = svm_handle_exit_irqoff,
4534 .request_immediate_exit = __kvm_request_immediate_exit,
4536 .sched_in = svm_sched_in,
4538 .pmu_ops = &amd_pmu_ops,
4539 .nested_ops = &svm_nested_ops,
4541 .deliver_posted_interrupt = svm_deliver_avic_intr,
4542 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
4543 .update_pi_irte = svm_update_pi_irte,
4544 .setup_mce = svm_setup_mce,
4546 .smi_allowed = svm_smi_allowed,
4547 .pre_enter_smm = svm_pre_enter_smm,
4548 .pre_leave_smm = svm_pre_leave_smm,
4549 .enable_smi_window = svm_enable_smi_window,
4551 .mem_enc_op = svm_mem_enc_op,
4552 .mem_enc_reg_region = svm_register_enc_region,
4553 .mem_enc_unreg_region = svm_unregister_enc_region,
4555 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4557 .can_emulate_instruction = svm_can_emulate_instruction,
4559 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
4561 .msr_filter_changed = svm_msr_filter_changed,
4562 .complete_emulated_msr = svm_complete_emulated_msr,
4564 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
4567 static struct kvm_x86_init_ops svm_init_ops __initdata = {
4568 .cpu_has_kvm_support = has_svm,
4569 .disabled_by_bios = is_disabled,
4570 .hardware_setup = svm_hardware_setup,
4571 .check_processor_compatibility = svm_check_processor_compat,
4573 .runtime_ops = &svm_x86_ops,
4576 static int __init svm_init(void)
4578 __unused_size_checks();
4580 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
4581 __alignof__(struct vcpu_svm), THIS_MODULE);
4584 static void __exit svm_exit(void)
4589 module_init(svm_init)
4590 module_exit(svm_exit)