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>
28 #include <linux/cc_platform.h>
31 #include <asm/perf_event.h>
32 #include <asm/tlbflush.h>
34 #include <asm/debugreg.h>
35 #include <asm/kvm_para.h>
36 #include <asm/irq_remapping.h>
37 #include <asm/spec-ctrl.h>
38 #include <asm/cpu_device_id.h>
39 #include <asm/traps.h>
40 #include <asm/fpu/api.h>
42 #include <asm/virtext.h>
48 #include "kvm_onhyperv.h"
49 #include "svm_onhyperv.h"
51 MODULE_AUTHOR("Qumranet");
52 MODULE_LICENSE("GPL");
55 static const struct x86_cpu_id svm_cpu_id[] = {
56 X86_MATCH_FEATURE(X86_FEATURE_SVM, NULL),
59 MODULE_DEVICE_TABLE(x86cpu, svm_cpu_id);
62 #define SEG_TYPE_LDT 2
63 #define SEG_TYPE_BUSY_TSS16 3
65 #define SVM_FEATURE_LBRV (1 << 1)
66 #define SVM_FEATURE_SVML (1 << 2)
67 #define SVM_FEATURE_TSC_RATE (1 << 4)
68 #define SVM_FEATURE_VMCB_CLEAN (1 << 5)
69 #define SVM_FEATURE_FLUSH_ASID (1 << 6)
70 #define SVM_FEATURE_DECODE_ASSIST (1 << 7)
71 #define SVM_FEATURE_PAUSE_FILTER (1 << 10)
73 #define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
75 #define TSC_RATIO_RSVD 0xffffff0000000000ULL
76 #define TSC_RATIO_MIN 0x0000000000000001ULL
77 #define TSC_RATIO_MAX 0x000000ffffffffffULL
79 static bool erratum_383_found __read_mostly;
81 u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
84 * Set osvw_len to higher value when updated Revision Guides
85 * are published and we know what the new status bits are
87 static uint64_t osvw_len = 4, osvw_status;
89 static DEFINE_PER_CPU(u64, current_tsc_ratio);
90 #define TSC_RATIO_DEFAULT 0x0100000000ULL
92 static const struct svm_direct_access_msrs {
93 u32 index; /* Index of the MSR */
94 bool always; /* True if intercept is initially cleared */
95 } direct_access_msrs[MAX_DIRECT_ACCESS_MSRS] = {
96 { .index = MSR_STAR, .always = true },
97 { .index = MSR_IA32_SYSENTER_CS, .always = true },
98 { .index = MSR_IA32_SYSENTER_EIP, .always = false },
99 { .index = MSR_IA32_SYSENTER_ESP, .always = false },
101 { .index = MSR_GS_BASE, .always = true },
102 { .index = MSR_FS_BASE, .always = true },
103 { .index = MSR_KERNEL_GS_BASE, .always = true },
104 { .index = MSR_LSTAR, .always = true },
105 { .index = MSR_CSTAR, .always = true },
106 { .index = MSR_SYSCALL_MASK, .always = true },
108 { .index = MSR_IA32_SPEC_CTRL, .always = false },
109 { .index = MSR_IA32_PRED_CMD, .always = false },
110 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
111 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
112 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
113 { .index = MSR_IA32_LASTINTTOIP, .always = false },
114 { .index = MSR_EFER, .always = false },
115 { .index = MSR_IA32_CR_PAT, .always = false },
116 { .index = MSR_AMD64_SEV_ES_GHCB, .always = true },
117 { .index = MSR_INVALID, .always = false },
121 * These 2 parameters are used to config the controls for Pause-Loop Exiting:
122 * pause_filter_count: On processors that support Pause filtering(indicated
123 * by CPUID Fn8000_000A_EDX), the VMCB provides a 16 bit pause filter
124 * count value. On VMRUN this value is loaded into an internal counter.
125 * Each time a pause instruction is executed, this counter is decremented
126 * until it reaches zero at which time a #VMEXIT is generated if pause
127 * intercept is enabled. Refer to AMD APM Vol 2 Section 15.14.4 Pause
128 * Intercept Filtering for more details.
129 * This also indicate if ple logic enabled.
131 * pause_filter_thresh: In addition, some processor families support advanced
132 * pause filtering (indicated by CPUID Fn8000_000A_EDX) upper bound on
133 * the amount of time a guest is allowed to execute in a pause loop.
134 * In this mode, a 16-bit pause filter threshold field is added in the
135 * VMCB. The threshold value is a cycle count that is used to reset the
136 * pause counter. As with simple pause filtering, VMRUN loads the pause
137 * count value from VMCB into an internal counter. Then, on each pause
138 * instruction the hardware checks the elapsed number of cycles since
139 * the most recent pause instruction against the pause filter threshold.
140 * If the elapsed cycle count is greater than the pause filter threshold,
141 * then the internal pause count is reloaded from the VMCB and execution
142 * continues. If the elapsed cycle count is less than the pause filter
143 * threshold, then the internal pause count is decremented. If the count
144 * value is less than zero and PAUSE intercept is enabled, a #VMEXIT is
145 * triggered. If advanced pause filtering is supported and pause filter
146 * threshold field is set to zero, the filter will operate in the simpler,
150 static unsigned short pause_filter_thresh = KVM_DEFAULT_PLE_GAP;
151 module_param(pause_filter_thresh, ushort, 0444);
153 static unsigned short pause_filter_count = KVM_SVM_DEFAULT_PLE_WINDOW;
154 module_param(pause_filter_count, ushort, 0444);
156 /* Default doubles per-vcpu window every exit. */
157 static unsigned short pause_filter_count_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
158 module_param(pause_filter_count_grow, ushort, 0444);
160 /* Default resets per-vcpu window every exit to pause_filter_count. */
161 static unsigned short pause_filter_count_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
162 module_param(pause_filter_count_shrink, ushort, 0444);
164 /* Default is to compute the maximum so we can never overflow. */
165 static unsigned short pause_filter_count_max = KVM_SVM_DEFAULT_PLE_WINDOW_MAX;
166 module_param(pause_filter_count_max, ushort, 0444);
169 * Use nested page tables by default. Note, NPT may get forced off by
170 * svm_hardware_setup() if it's unsupported by hardware or the host kernel.
172 bool npt_enabled = true;
173 module_param_named(npt, npt_enabled, bool, 0444);
175 /* allow nested virtualization in KVM/SVM */
176 static int nested = true;
177 module_param(nested, int, S_IRUGO);
179 /* enable/disable Next RIP Save */
180 static int nrips = true;
181 module_param(nrips, int, 0444);
183 /* enable/disable Virtual VMLOAD VMSAVE */
184 static int vls = true;
185 module_param(vls, int, 0444);
187 /* enable/disable Virtual GIF */
188 static int vgif = true;
189 module_param(vgif, int, 0444);
191 /* enable/disable LBR virtualization */
192 static int lbrv = true;
193 module_param(lbrv, int, 0444);
195 static int tsc_scaling = true;
196 module_param(tsc_scaling, int, 0444);
199 * enable / disable AVIC. Because the defaults differ for APICv
200 * support between VMX and SVM we cannot use module_param_named.
203 module_param(avic, bool, 0444);
205 bool __read_mostly dump_invalid_vmcb;
206 module_param(dump_invalid_vmcb, bool, 0644);
209 bool intercept_smi = true;
210 module_param(intercept_smi, bool, 0444);
213 static bool svm_gp_erratum_intercept = true;
215 static u8 rsm_ins_bytes[] = "\x0f\xaa";
217 static unsigned long iopm_base;
219 struct kvm_ldttss_desc {
222 unsigned base1:8, type:5, dpl:2, p:1;
223 unsigned limit1:4, zero0:3, g:1, base2:8;
226 } __attribute__((packed));
228 DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
231 * Only MSR_TSC_AUX is switched via the user return hook. EFER is switched via
232 * the VMCB, and the SYSCALL/SYSENTER MSRs are handled by VMLOAD/VMSAVE.
234 * RDTSCP and RDPID are not used in the kernel, specifically to allow KVM to
235 * defer the restoration of TSC_AUX until the CPU returns to userspace.
237 static int tsc_aux_uret_slot __read_mostly = -1;
239 static const u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
241 #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
242 #define MSRS_RANGE_SIZE 2048
243 #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
245 u32 svm_msrpm_offset(u32 msr)
250 for (i = 0; i < NUM_MSR_MAPS; i++) {
251 if (msr < msrpm_ranges[i] ||
252 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
255 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
256 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
258 /* Now we have the u8 offset - but need the u32 offset */
262 /* MSR not in any range */
266 #define MAX_INST_SIZE 15
268 static int get_npt_level(void)
271 return pgtable_l5_enabled() ? PT64_ROOT_5LEVEL : PT64_ROOT_4LEVEL;
273 return PT32E_ROOT_LEVEL;
277 int svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
279 struct vcpu_svm *svm = to_svm(vcpu);
280 u64 old_efer = vcpu->arch.efer;
281 vcpu->arch.efer = efer;
284 /* Shadow paging assumes NX to be available. */
287 if (!(efer & EFER_LMA))
291 if ((old_efer & EFER_SVME) != (efer & EFER_SVME)) {
292 if (!(efer & EFER_SVME)) {
293 svm_leave_nested(vcpu);
294 svm_set_gif(svm, true);
295 /* #GP intercept is still needed for vmware backdoor */
296 if (!enable_vmware_backdoor)
297 clr_exception_intercept(svm, GP_VECTOR);
300 * Free the nested guest state, unless we are in SMM.
301 * In this case we will return to the nested guest
302 * as soon as we leave SMM.
305 svm_free_nested(svm);
308 int ret = svm_allocate_nested(svm);
311 vcpu->arch.efer = old_efer;
316 * Never intercept #GP for SEV guests, KVM can't
317 * decrypt guest memory to workaround the erratum.
319 if (svm_gp_erratum_intercept && !sev_guest(vcpu->kvm))
320 set_exception_intercept(svm, GP_VECTOR);
324 svm->vmcb->save.efer = efer | EFER_SVME;
325 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
329 static int is_external_interrupt(u32 info)
331 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
332 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
335 static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu)
337 struct vcpu_svm *svm = to_svm(vcpu);
340 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
341 ret = KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
345 static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
347 struct vcpu_svm *svm = to_svm(vcpu);
350 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
352 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
356 static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
358 struct vcpu_svm *svm = to_svm(vcpu);
361 * SEV-ES does not expose the next RIP. The RIP update is controlled by
362 * the type of exit and the #VC handler in the guest.
364 if (sev_es_guest(vcpu->kvm))
367 if (nrips && svm->vmcb->control.next_rip != 0) {
368 WARN_ON_ONCE(!static_cpu_has(X86_FEATURE_NRIPS));
369 svm->next_rip = svm->vmcb->control.next_rip;
372 if (!svm->next_rip) {
373 if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
376 kvm_rip_write(vcpu, svm->next_rip);
380 svm_set_interrupt_shadow(vcpu, 0);
385 static void svm_queue_exception(struct kvm_vcpu *vcpu)
387 struct vcpu_svm *svm = to_svm(vcpu);
388 unsigned nr = vcpu->arch.exception.nr;
389 bool has_error_code = vcpu->arch.exception.has_error_code;
390 u32 error_code = vcpu->arch.exception.error_code;
392 kvm_deliver_exception_payload(vcpu);
394 if (nr == BP_VECTOR && !nrips) {
395 unsigned long rip, old_rip = kvm_rip_read(vcpu);
398 * For guest debugging where we have to reinject #BP if some
399 * INT3 is guest-owned:
400 * Emulate nRIP by moving RIP forward. Will fail if injection
401 * raises a fault that is not intercepted. Still better than
402 * failing in all cases.
404 (void)skip_emulated_instruction(vcpu);
405 rip = kvm_rip_read(vcpu);
406 svm->int3_rip = rip + svm->vmcb->save.cs.base;
407 svm->int3_injected = rip - old_rip;
410 svm->vmcb->control.event_inj = nr
412 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
413 | SVM_EVTINJ_TYPE_EXEPT;
414 svm->vmcb->control.event_inj_err = error_code;
417 static void svm_init_erratum_383(void)
423 if (!static_cpu_has_bug(X86_BUG_AMD_TLB_MMATCH))
426 /* Use _safe variants to not break nested virtualization */
427 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
433 low = lower_32_bits(val);
434 high = upper_32_bits(val);
436 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
438 erratum_383_found = true;
441 static void svm_init_osvw(struct kvm_vcpu *vcpu)
444 * Guests should see errata 400 and 415 as fixed (assuming that
445 * HLT and IO instructions are intercepted).
447 vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3;
448 vcpu->arch.osvw.status = osvw_status & ~(6ULL);
451 * By increasing VCPU's osvw.length to 3 we are telling the guest that
452 * all osvw.status bits inside that length, including bit 0 (which is
453 * reserved for erratum 298), are valid. However, if host processor's
454 * osvw_len is 0 then osvw_status[0] carries no information. We need to
455 * be conservative here and therefore we tell the guest that erratum 298
456 * is present (because we really don't know).
458 if (osvw_len == 0 && boot_cpu_data.x86 == 0x10)
459 vcpu->arch.osvw.status |= 1;
462 static int has_svm(void)
466 if (!cpu_has_svm(&msg)) {
467 printk(KERN_INFO "has_svm: %s\n", msg);
471 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
472 pr_info("KVM is unsupported when running as an SEV guest\n");
479 static void svm_hardware_disable(void)
481 /* Make sure we clean up behind us */
483 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
487 amd_pmu_disable_virt();
490 static int svm_hardware_enable(void)
493 struct svm_cpu_data *sd;
495 struct desc_struct *gdt;
496 int me = raw_smp_processor_id();
498 rdmsrl(MSR_EFER, efer);
499 if (efer & EFER_SVME)
503 pr_err("%s: err EOPNOTSUPP on %d\n", __func__, me);
506 sd = per_cpu(svm_data, me);
508 pr_err("%s: svm_data is NULL on %d\n", __func__, me);
512 sd->asid_generation = 1;
513 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
514 sd->next_asid = sd->max_asid + 1;
515 sd->min_asid = max_sev_asid + 1;
517 gdt = get_current_gdt_rw();
518 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
520 wrmsrl(MSR_EFER, efer | EFER_SVME);
522 wrmsrl(MSR_VM_HSAVE_PA, __sme_page_pa(sd->save_area));
524 if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) {
526 * Set the default value, even if we don't use TSC scaling
527 * to avoid having stale value in the msr
529 wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT);
530 __this_cpu_write(current_tsc_ratio, TSC_RATIO_DEFAULT);
537 * Note that it is possible to have a system with mixed processor
538 * revisions and therefore different OSVW bits. If bits are not the same
539 * on different processors then choose the worst case (i.e. if erratum
540 * is present on one processor and not on another then assume that the
541 * erratum is present everywhere).
543 if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) {
544 uint64_t len, status = 0;
547 len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err);
549 status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS,
553 osvw_status = osvw_len = 0;
557 osvw_status |= status;
558 osvw_status &= (1ULL << osvw_len) - 1;
561 osvw_status = osvw_len = 0;
563 svm_init_erratum_383();
565 amd_pmu_enable_virt();
570 static void svm_cpu_uninit(int cpu)
572 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
577 per_cpu(svm_data, cpu) = NULL;
578 kfree(sd->sev_vmcbs);
579 __free_page(sd->save_area);
583 static int svm_cpu_init(int cpu)
585 struct svm_cpu_data *sd;
588 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
592 sd->save_area = alloc_page(GFP_KERNEL | __GFP_ZERO);
596 ret = sev_cpu_init(sd);
600 per_cpu(svm_data, cpu) = sd;
605 __free_page(sd->save_area);
612 static int direct_access_msr_slot(u32 msr)
616 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
617 if (direct_access_msrs[i].index == msr)
623 static void set_shadow_msr_intercept(struct kvm_vcpu *vcpu, u32 msr, int read,
626 struct vcpu_svm *svm = to_svm(vcpu);
627 int slot = direct_access_msr_slot(msr);
632 /* Set the shadow bitmaps to the desired intercept states */
634 set_bit(slot, svm->shadow_msr_intercept.read);
636 clear_bit(slot, svm->shadow_msr_intercept.read);
639 set_bit(slot, svm->shadow_msr_intercept.write);
641 clear_bit(slot, svm->shadow_msr_intercept.write);
644 static bool valid_msr_intercept(u32 index)
646 return direct_access_msr_slot(index) != -ENOENT;
649 static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
656 msrpm = is_guest_mode(vcpu) ? to_svm(vcpu)->nested.msrpm:
659 offset = svm_msrpm_offset(msr);
660 bit_write = 2 * (msr & 0x0f) + 1;
663 BUG_ON(offset == MSR_INVALID);
665 return !!test_bit(bit_write, &tmp);
668 static void set_msr_interception_bitmap(struct kvm_vcpu *vcpu, u32 *msrpm,
669 u32 msr, int read, int write)
671 u8 bit_read, bit_write;
676 * If this warning triggers extend the direct_access_msrs list at the
677 * beginning of the file
679 WARN_ON(!valid_msr_intercept(msr));
681 /* Enforce non allowed MSRs to trap */
682 if (read && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
685 if (write && !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
688 offset = svm_msrpm_offset(msr);
689 bit_read = 2 * (msr & 0x0f);
690 bit_write = 2 * (msr & 0x0f) + 1;
693 BUG_ON(offset == MSR_INVALID);
695 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
696 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
700 svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
704 void set_msr_interception(struct kvm_vcpu *vcpu, u32 *msrpm, u32 msr,
707 set_shadow_msr_intercept(vcpu, msr, read, write);
708 set_msr_interception_bitmap(vcpu, msrpm, msr, read, write);
711 u32 *svm_vcpu_alloc_msrpm(void)
713 unsigned int order = get_order(MSRPM_SIZE);
714 struct page *pages = alloc_pages(GFP_KERNEL_ACCOUNT, order);
720 msrpm = page_address(pages);
721 memset(msrpm, 0xff, PAGE_SIZE * (1 << order));
726 void svm_vcpu_init_msrpm(struct kvm_vcpu *vcpu, u32 *msrpm)
730 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
731 if (!direct_access_msrs[i].always)
733 set_msr_interception(vcpu, msrpm, direct_access_msrs[i].index, 1, 1);
738 void svm_vcpu_free_msrpm(u32 *msrpm)
740 __free_pages(virt_to_page(msrpm), get_order(MSRPM_SIZE));
743 static void svm_msr_filter_changed(struct kvm_vcpu *vcpu)
745 struct vcpu_svm *svm = to_svm(vcpu);
749 * Set intercept permissions for all direct access MSRs again. They
750 * will automatically get filtered through the MSR filter, so we are
751 * back in sync after this.
753 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
754 u32 msr = direct_access_msrs[i].index;
755 u32 read = test_bit(i, svm->shadow_msr_intercept.read);
756 u32 write = test_bit(i, svm->shadow_msr_intercept.write);
758 set_msr_interception_bitmap(vcpu, svm->msrpm, msr, read, write);
762 static void add_msr_offset(u32 offset)
766 for (i = 0; i < MSRPM_OFFSETS; ++i) {
768 /* Offset already in list? */
769 if (msrpm_offsets[i] == offset)
772 /* Slot used by another offset? */
773 if (msrpm_offsets[i] != MSR_INVALID)
776 /* Add offset to list */
777 msrpm_offsets[i] = offset;
783 * If this BUG triggers the msrpm_offsets table has an overflow. Just
784 * increase MSRPM_OFFSETS in this case.
789 static void init_msrpm_offsets(void)
793 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
795 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
798 offset = svm_msrpm_offset(direct_access_msrs[i].index);
799 BUG_ON(offset == MSR_INVALID);
801 add_msr_offset(offset);
805 static void svm_enable_lbrv(struct kvm_vcpu *vcpu)
807 struct vcpu_svm *svm = to_svm(vcpu);
809 svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
810 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
811 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
812 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
813 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
816 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
818 struct vcpu_svm *svm = to_svm(vcpu);
820 svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
821 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
822 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
823 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
824 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
827 void disable_nmi_singlestep(struct vcpu_svm *svm)
829 svm->nmi_singlestep = false;
831 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) {
832 /* Clear our flags if they were not set by the guest */
833 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
834 svm->vmcb->save.rflags &= ~X86_EFLAGS_TF;
835 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
836 svm->vmcb->save.rflags &= ~X86_EFLAGS_RF;
840 static void grow_ple_window(struct kvm_vcpu *vcpu)
842 struct vcpu_svm *svm = to_svm(vcpu);
843 struct vmcb_control_area *control = &svm->vmcb->control;
844 int old = control->pause_filter_count;
846 control->pause_filter_count = __grow_ple_window(old,
848 pause_filter_count_grow,
849 pause_filter_count_max);
851 if (control->pause_filter_count != old) {
852 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
853 trace_kvm_ple_window_update(vcpu->vcpu_id,
854 control->pause_filter_count, old);
858 static void shrink_ple_window(struct kvm_vcpu *vcpu)
860 struct vcpu_svm *svm = to_svm(vcpu);
861 struct vmcb_control_area *control = &svm->vmcb->control;
862 int old = control->pause_filter_count;
864 control->pause_filter_count =
865 __shrink_ple_window(old,
867 pause_filter_count_shrink,
869 if (control->pause_filter_count != old) {
870 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
871 trace_kvm_ple_window_update(vcpu->vcpu_id,
872 control->pause_filter_count, old);
876 static void svm_hardware_teardown(void)
880 sev_hardware_teardown();
882 for_each_possible_cpu(cpu)
885 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT),
886 get_order(IOPM_SIZE));
890 static void init_seg(struct vmcb_seg *seg)
893 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
894 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
899 static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
902 seg->attrib = SVM_SELECTOR_P_MASK | type;
907 static u64 svm_get_l2_tsc_offset(struct kvm_vcpu *vcpu)
909 struct vcpu_svm *svm = to_svm(vcpu);
911 return svm->nested.ctl.tsc_offset;
914 static u64 svm_get_l2_tsc_multiplier(struct kvm_vcpu *vcpu)
916 struct vcpu_svm *svm = to_svm(vcpu);
918 return svm->tsc_ratio_msr;
921 static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
923 struct vcpu_svm *svm = to_svm(vcpu);
925 svm->vmcb01.ptr->control.tsc_offset = vcpu->arch.l1_tsc_offset;
926 svm->vmcb->control.tsc_offset = offset;
927 vmcb_mark_dirty(svm->vmcb, VMCB_INTERCEPTS);
930 void svm_write_tsc_multiplier(struct kvm_vcpu *vcpu, u64 multiplier)
932 wrmsrl(MSR_AMD64_TSC_RATIO, multiplier);
935 /* Evaluate instruction intercepts that depend on guest CPUID features. */
936 static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu,
937 struct vcpu_svm *svm)
940 * Intercept INVPCID if shadow paging is enabled to sync/free shadow
941 * roots, or if INVPCID is disabled in the guest to inject #UD.
943 if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
945 !guest_cpuid_has(&svm->vcpu, X86_FEATURE_INVPCID))
946 svm_set_intercept(svm, INTERCEPT_INVPCID);
948 svm_clr_intercept(svm, INTERCEPT_INVPCID);
951 if (kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) {
952 if (guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
953 svm_clr_intercept(svm, INTERCEPT_RDTSCP);
955 svm_set_intercept(svm, INTERCEPT_RDTSCP);
959 static inline void init_vmcb_after_set_cpuid(struct kvm_vcpu *vcpu)
961 struct vcpu_svm *svm = to_svm(vcpu);
963 if (guest_cpuid_is_intel(vcpu)) {
965 * We must intercept SYSENTER_EIP and SYSENTER_ESP
966 * accesses because the processor only stores 32 bits.
967 * For the same reason we cannot use virtual VMLOAD/VMSAVE.
969 svm_set_intercept(svm, INTERCEPT_VMLOAD);
970 svm_set_intercept(svm, INTERCEPT_VMSAVE);
971 svm->vmcb->control.virt_ext &= ~VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
973 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 0, 0);
974 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 0, 0);
977 * If hardware supports Virtual VMLOAD VMSAVE then enable it
978 * in VMCB and clear intercepts to avoid #VMEXIT.
981 svm_clr_intercept(svm, INTERCEPT_VMLOAD);
982 svm_clr_intercept(svm, INTERCEPT_VMSAVE);
983 svm->vmcb->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
985 /* No need to intercept these MSRs */
986 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_EIP, 1, 1);
987 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SYSENTER_ESP, 1, 1);
991 static void init_vmcb(struct kvm_vcpu *vcpu)
993 struct vcpu_svm *svm = to_svm(vcpu);
994 struct vmcb_control_area *control = &svm->vmcb->control;
995 struct vmcb_save_area *save = &svm->vmcb->save;
997 svm_set_intercept(svm, INTERCEPT_CR0_READ);
998 svm_set_intercept(svm, INTERCEPT_CR3_READ);
999 svm_set_intercept(svm, INTERCEPT_CR4_READ);
1000 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1001 svm_set_intercept(svm, INTERCEPT_CR3_WRITE);
1002 svm_set_intercept(svm, INTERCEPT_CR4_WRITE);
1003 if (!kvm_vcpu_apicv_active(vcpu))
1004 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
1006 set_dr_intercepts(svm);
1008 set_exception_intercept(svm, PF_VECTOR);
1009 set_exception_intercept(svm, UD_VECTOR);
1010 set_exception_intercept(svm, MC_VECTOR);
1011 set_exception_intercept(svm, AC_VECTOR);
1012 set_exception_intercept(svm, DB_VECTOR);
1014 * Guest access to VMware backdoor ports could legitimately
1015 * trigger #GP because of TSS I/O permission bitmap.
1016 * We intercept those #GP and allow access to them anyway
1017 * as VMware does. Don't intercept #GP for SEV guests as KVM can't
1018 * decrypt guest memory to decode the faulting instruction.
1020 if (enable_vmware_backdoor && !sev_guest(vcpu->kvm))
1021 set_exception_intercept(svm, GP_VECTOR);
1023 svm_set_intercept(svm, INTERCEPT_INTR);
1024 svm_set_intercept(svm, INTERCEPT_NMI);
1027 svm_set_intercept(svm, INTERCEPT_SMI);
1029 svm_set_intercept(svm, INTERCEPT_SELECTIVE_CR0);
1030 svm_set_intercept(svm, INTERCEPT_RDPMC);
1031 svm_set_intercept(svm, INTERCEPT_CPUID);
1032 svm_set_intercept(svm, INTERCEPT_INVD);
1033 svm_set_intercept(svm, INTERCEPT_INVLPG);
1034 svm_set_intercept(svm, INTERCEPT_INVLPGA);
1035 svm_set_intercept(svm, INTERCEPT_IOIO_PROT);
1036 svm_set_intercept(svm, INTERCEPT_MSR_PROT);
1037 svm_set_intercept(svm, INTERCEPT_TASK_SWITCH);
1038 svm_set_intercept(svm, INTERCEPT_SHUTDOWN);
1039 svm_set_intercept(svm, INTERCEPT_VMRUN);
1040 svm_set_intercept(svm, INTERCEPT_VMMCALL);
1041 svm_set_intercept(svm, INTERCEPT_VMLOAD);
1042 svm_set_intercept(svm, INTERCEPT_VMSAVE);
1043 svm_set_intercept(svm, INTERCEPT_STGI);
1044 svm_set_intercept(svm, INTERCEPT_CLGI);
1045 svm_set_intercept(svm, INTERCEPT_SKINIT);
1046 svm_set_intercept(svm, INTERCEPT_WBINVD);
1047 svm_set_intercept(svm, INTERCEPT_XSETBV);
1048 svm_set_intercept(svm, INTERCEPT_RDPRU);
1049 svm_set_intercept(svm, INTERCEPT_RSM);
1051 if (!kvm_mwait_in_guest(vcpu->kvm)) {
1052 svm_set_intercept(svm, INTERCEPT_MONITOR);
1053 svm_set_intercept(svm, INTERCEPT_MWAIT);
1056 if (!kvm_hlt_in_guest(vcpu->kvm))
1057 svm_set_intercept(svm, INTERCEPT_HLT);
1059 control->iopm_base_pa = __sme_set(iopm_base);
1060 control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
1061 control->int_ctl = V_INTR_MASKING_MASK;
1063 init_seg(&save->es);
1064 init_seg(&save->ss);
1065 init_seg(&save->ds);
1066 init_seg(&save->fs);
1067 init_seg(&save->gs);
1069 save->cs.selector = 0xf000;
1070 save->cs.base = 0xffff0000;
1071 /* Executable/Readable Code Segment */
1072 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
1073 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
1074 save->cs.limit = 0xffff;
1076 save->gdtr.base = 0;
1077 save->gdtr.limit = 0xffff;
1078 save->idtr.base = 0;
1079 save->idtr.limit = 0xffff;
1081 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
1082 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
1085 /* Setup VMCB for Nested Paging */
1086 control->nested_ctl |= SVM_NESTED_CTL_NP_ENABLE;
1087 svm_clr_intercept(svm, INTERCEPT_INVLPG);
1088 clr_exception_intercept(svm, PF_VECTOR);
1089 svm_clr_intercept(svm, INTERCEPT_CR3_READ);
1090 svm_clr_intercept(svm, INTERCEPT_CR3_WRITE);
1091 save->g_pat = vcpu->arch.pat;
1094 svm->current_vmcb->asid_generation = 0;
1097 svm->nested.vmcb12_gpa = INVALID_GPA;
1098 svm->nested.last_vmcb12_gpa = INVALID_GPA;
1100 if (!kvm_pause_in_guest(vcpu->kvm)) {
1101 control->pause_filter_count = pause_filter_count;
1102 if (pause_filter_thresh)
1103 control->pause_filter_thresh = pause_filter_thresh;
1104 svm_set_intercept(svm, INTERCEPT_PAUSE);
1106 svm_clr_intercept(svm, INTERCEPT_PAUSE);
1109 svm_recalc_instruction_intercepts(vcpu, svm);
1112 * If the host supports V_SPEC_CTRL then disable the interception
1113 * of MSR_IA32_SPEC_CTRL.
1115 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
1116 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
1118 if (kvm_vcpu_apicv_active(vcpu))
1119 avic_init_vmcb(svm);
1122 svm_clr_intercept(svm, INTERCEPT_STGI);
1123 svm_clr_intercept(svm, INTERCEPT_CLGI);
1124 svm->vmcb->control.int_ctl |= V_GIF_ENABLE_MASK;
1127 if (sev_guest(vcpu->kvm)) {
1128 svm->vmcb->control.nested_ctl |= SVM_NESTED_CTL_SEV_ENABLE;
1129 clr_exception_intercept(svm, UD_VECTOR);
1131 if (sev_es_guest(vcpu->kvm)) {
1132 /* Perform SEV-ES specific VMCB updates */
1133 sev_es_init_vmcb(svm);
1137 svm_hv_init_vmcb(svm->vmcb);
1138 init_vmcb_after_set_cpuid(vcpu);
1140 vmcb_mark_all_dirty(svm->vmcb);
1145 static void __svm_vcpu_reset(struct kvm_vcpu *vcpu)
1147 struct vcpu_svm *svm = to_svm(vcpu);
1149 svm_vcpu_init_msrpm(vcpu, svm->msrpm);
1151 svm_init_osvw(vcpu);
1152 vcpu->arch.microcode_version = 0x01000065;
1153 svm->tsc_ratio_msr = kvm_default_tsc_scaling_ratio;
1155 if (sev_es_guest(vcpu->kvm))
1156 sev_es_vcpu_reset(svm);
1159 static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
1161 struct vcpu_svm *svm = to_svm(vcpu);
1164 svm->virt_spec_ctrl = 0;
1169 __svm_vcpu_reset(vcpu);
1172 void svm_switch_vmcb(struct vcpu_svm *svm, struct kvm_vmcb_info *target_vmcb)
1174 svm->current_vmcb = target_vmcb;
1175 svm->vmcb = target_vmcb->ptr;
1178 static int svm_create_vcpu(struct kvm_vcpu *vcpu)
1180 struct vcpu_svm *svm;
1181 struct page *vmcb01_page;
1182 struct page *vmsa_page = NULL;
1185 BUILD_BUG_ON(offsetof(struct vcpu_svm, vcpu) != 0);
1189 vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1193 if (sev_es_guest(vcpu->kvm)) {
1195 * SEV-ES guests require a separate VMSA page used to contain
1196 * the encrypted register state of the guest.
1198 vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
1200 goto error_free_vmcb_page;
1203 * SEV-ES guests maintain an encrypted version of their FPU
1204 * state which is restored and saved on VMRUN and VMEXIT.
1205 * Mark vcpu->arch.guest_fpu->fpstate as scratch so it won't
1206 * do xsave/xrstor on it.
1208 fpstate_set_confidential(&vcpu->arch.guest_fpu);
1211 err = avic_init_vcpu(svm);
1213 goto error_free_vmsa_page;
1215 svm->msrpm = svm_vcpu_alloc_msrpm();
1218 goto error_free_vmsa_page;
1221 svm->vmcb01.ptr = page_address(vmcb01_page);
1222 svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
1223 svm_switch_vmcb(svm, &svm->vmcb01);
1226 svm->sev_es.vmsa = page_address(vmsa_page);
1228 svm->guest_state_loaded = false;
1232 error_free_vmsa_page:
1234 __free_page(vmsa_page);
1235 error_free_vmcb_page:
1236 __free_page(vmcb01_page);
1241 static void svm_clear_current_vmcb(struct vmcb *vmcb)
1245 for_each_online_cpu(i)
1246 cmpxchg(&per_cpu(svm_data, i)->current_vmcb, vmcb, NULL);
1249 static void svm_free_vcpu(struct kvm_vcpu *vcpu)
1251 struct vcpu_svm *svm = to_svm(vcpu);
1254 * The vmcb page can be recycled, causing a false negative in
1255 * svm_vcpu_load(). So, ensure that no logical CPU has this
1256 * vmcb page recorded as its current vmcb.
1258 svm_clear_current_vmcb(svm->vmcb);
1260 svm_free_nested(svm);
1262 sev_free_vcpu(vcpu);
1264 __free_page(pfn_to_page(__sme_clr(svm->vmcb01.pa) >> PAGE_SHIFT));
1265 __free_pages(virt_to_page(svm->msrpm), get_order(MSRPM_SIZE));
1268 static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
1270 struct vcpu_svm *svm = to_svm(vcpu);
1271 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
1273 if (sev_es_guest(vcpu->kvm))
1274 sev_es_unmap_ghcb(svm);
1276 if (svm->guest_state_loaded)
1280 * Save additional host state that will be restored on VMEXIT (sev-es)
1281 * or subsequent vmload of host save area.
1283 if (sev_es_guest(vcpu->kvm)) {
1284 sev_es_prepare_guest_switch(svm, vcpu->cpu);
1286 vmsave(__sme_page_pa(sd->save_area));
1290 u64 tsc_ratio = vcpu->arch.tsc_scaling_ratio;
1291 if (tsc_ratio != __this_cpu_read(current_tsc_ratio)) {
1292 __this_cpu_write(current_tsc_ratio, tsc_ratio);
1293 wrmsrl(MSR_AMD64_TSC_RATIO, tsc_ratio);
1297 if (likely(tsc_aux_uret_slot >= 0))
1298 kvm_set_user_return_msr(tsc_aux_uret_slot, svm->tsc_aux, -1ull);
1300 svm->guest_state_loaded = true;
1303 static void svm_prepare_host_switch(struct kvm_vcpu *vcpu)
1305 to_svm(vcpu)->guest_state_loaded = false;
1308 static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
1310 struct vcpu_svm *svm = to_svm(vcpu);
1311 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
1313 if (sd->current_vmcb != svm->vmcb) {
1314 sd->current_vmcb = svm->vmcb;
1315 indirect_branch_prediction_barrier();
1317 if (kvm_vcpu_apicv_active(vcpu))
1318 avic_vcpu_load(vcpu, cpu);
1321 static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1323 if (kvm_vcpu_apicv_active(vcpu))
1324 avic_vcpu_put(vcpu);
1326 svm_prepare_host_switch(vcpu);
1328 ++vcpu->stat.host_state_reload;
1331 static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1333 struct vcpu_svm *svm = to_svm(vcpu);
1334 unsigned long rflags = svm->vmcb->save.rflags;
1336 if (svm->nmi_singlestep) {
1337 /* Hide our flags if they were not set by the guest */
1338 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_TF))
1339 rflags &= ~X86_EFLAGS_TF;
1340 if (!(svm->nmi_singlestep_guest_rflags & X86_EFLAGS_RF))
1341 rflags &= ~X86_EFLAGS_RF;
1346 static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1348 if (to_svm(vcpu)->nmi_singlestep)
1349 rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
1352 * Any change of EFLAGS.VM is accompanied by a reload of SS
1353 * (caused by either a task switch or an inter-privilege IRET),
1354 * so we do not need to update the CPL here.
1356 to_svm(vcpu)->vmcb->save.rflags = rflags;
1359 static bool svm_get_if_flag(struct kvm_vcpu *vcpu)
1361 struct vmcb *vmcb = to_svm(vcpu)->vmcb;
1363 return sev_es_guest(vcpu->kvm)
1364 ? vmcb->control.int_state & SVM_GUEST_INTERRUPT_MASK
1365 : kvm_get_rflags(vcpu) & X86_EFLAGS_IF;
1368 static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1370 kvm_register_mark_available(vcpu, reg);
1373 case VCPU_EXREG_PDPTR:
1375 * When !npt_enabled, mmu->pdptrs[] is already available since
1376 * it is always updated per SDM when moving to CRs.
1379 load_pdptrs(vcpu, kvm_read_cr3(vcpu));
1382 KVM_BUG_ON(1, vcpu->kvm);
1386 static void svm_set_vintr(struct vcpu_svm *svm)
1388 struct vmcb_control_area *control;
1391 * The following fields are ignored when AVIC is enabled
1393 WARN_ON(kvm_apicv_activated(svm->vcpu.kvm));
1395 svm_set_intercept(svm, INTERCEPT_VINTR);
1398 * This is just a dummy VINTR to actually cause a vmexit to happen.
1399 * Actual injection of virtual interrupts happens through EVENTINJ.
1401 control = &svm->vmcb->control;
1402 control->int_vector = 0x0;
1403 control->int_ctl &= ~V_INTR_PRIO_MASK;
1404 control->int_ctl |= V_IRQ_MASK |
1405 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
1406 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1409 static void svm_clear_vintr(struct vcpu_svm *svm)
1411 svm_clr_intercept(svm, INTERCEPT_VINTR);
1413 /* Drop int_ctl fields related to VINTR injection. */
1414 svm->vmcb->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1415 if (is_guest_mode(&svm->vcpu)) {
1416 svm->vmcb01.ptr->control.int_ctl &= ~V_IRQ_INJECTION_BITS_MASK;
1418 WARN_ON((svm->vmcb->control.int_ctl & V_TPR_MASK) !=
1419 (svm->nested.ctl.int_ctl & V_TPR_MASK));
1421 svm->vmcb->control.int_ctl |= svm->nested.ctl.int_ctl &
1422 V_IRQ_INJECTION_BITS_MASK;
1424 svm->vmcb->control.int_vector = svm->nested.ctl.int_vector;
1427 vmcb_mark_dirty(svm->vmcb, VMCB_INTR);
1430 static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1432 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1433 struct vmcb_save_area *save01 = &to_svm(vcpu)->vmcb01.ptr->save;
1436 case VCPU_SREG_CS: return &save->cs;
1437 case VCPU_SREG_DS: return &save->ds;
1438 case VCPU_SREG_ES: return &save->es;
1439 case VCPU_SREG_FS: return &save01->fs;
1440 case VCPU_SREG_GS: return &save01->gs;
1441 case VCPU_SREG_SS: return &save->ss;
1442 case VCPU_SREG_TR: return &save01->tr;
1443 case VCPU_SREG_LDTR: return &save01->ldtr;
1449 static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1451 struct vmcb_seg *s = svm_seg(vcpu, seg);
1456 static void svm_get_segment(struct kvm_vcpu *vcpu,
1457 struct kvm_segment *var, int seg)
1459 struct vmcb_seg *s = svm_seg(vcpu, seg);
1461 var->base = s->base;
1462 var->limit = s->limit;
1463 var->selector = s->selector;
1464 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1465 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1466 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1467 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1468 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1469 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1470 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1473 * AMD CPUs circa 2014 track the G bit for all segments except CS.
1474 * However, the SVM spec states that the G bit is not observed by the
1475 * CPU, and some VMware virtual CPUs drop the G bit for all segments.
1476 * So let's synthesize a legal G bit for all segments, this helps
1477 * running KVM nested. It also helps cross-vendor migration, because
1478 * Intel's vmentry has a check on the 'G' bit.
1480 var->g = s->limit > 0xfffff;
1483 * AMD's VMCB does not have an explicit unusable field, so emulate it
1484 * for cross vendor migration purposes by "not present"
1486 var->unusable = !var->present;
1491 * Work around a bug where the busy flag in the tr selector
1501 * The accessed bit must always be set in the segment
1502 * descriptor cache, although it can be cleared in the
1503 * descriptor, the cached bit always remains at 1. Since
1504 * Intel has a check on this, set it here to support
1505 * cross-vendor migration.
1512 * On AMD CPUs sometimes the DB bit in the segment
1513 * descriptor is left as 1, although the whole segment has
1514 * been made unusable. Clear it here to pass an Intel VMX
1515 * entry check when cross vendor migrating.
1519 /* This is symmetric with svm_set_segment() */
1520 var->dpl = to_svm(vcpu)->vmcb->save.cpl;
1525 static int svm_get_cpl(struct kvm_vcpu *vcpu)
1527 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1532 static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1534 struct vcpu_svm *svm = to_svm(vcpu);
1536 dt->size = svm->vmcb->save.idtr.limit;
1537 dt->address = svm->vmcb->save.idtr.base;
1540 static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1542 struct vcpu_svm *svm = to_svm(vcpu);
1544 svm->vmcb->save.idtr.limit = dt->size;
1545 svm->vmcb->save.idtr.base = dt->address ;
1546 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1549 static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1551 struct vcpu_svm *svm = to_svm(vcpu);
1553 dt->size = svm->vmcb->save.gdtr.limit;
1554 dt->address = svm->vmcb->save.gdtr.base;
1557 static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
1559 struct vcpu_svm *svm = to_svm(vcpu);
1561 svm->vmcb->save.gdtr.limit = dt->size;
1562 svm->vmcb->save.gdtr.base = dt->address ;
1563 vmcb_mark_dirty(svm->vmcb, VMCB_DT);
1566 static void svm_post_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
1568 struct vcpu_svm *svm = to_svm(vcpu);
1571 * For guests that don't set guest_state_protected, the cr3 update is
1572 * handled via kvm_mmu_load() while entering the guest. For guests
1573 * that do (SEV-ES/SEV-SNP), the cr3 update needs to be written to
1574 * VMCB save area now, since the save area will become the initial
1575 * contents of the VMSA, and future VMCB save area updates won't be
1578 if (sev_es_guest(vcpu->kvm)) {
1579 svm->vmcb->save.cr3 = cr3;
1580 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1584 void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1586 struct vcpu_svm *svm = to_svm(vcpu);
1588 bool old_paging = is_paging(vcpu);
1590 #ifdef CONFIG_X86_64
1591 if (vcpu->arch.efer & EFER_LME && !vcpu->arch.guest_state_protected) {
1592 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
1593 vcpu->arch.efer |= EFER_LMA;
1594 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
1597 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
1598 vcpu->arch.efer &= ~EFER_LMA;
1599 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
1603 vcpu->arch.cr0 = cr0;
1606 hcr0 |= X86_CR0_PG | X86_CR0_WP;
1607 if (old_paging != is_paging(vcpu))
1608 svm_set_cr4(vcpu, kvm_read_cr4(vcpu));
1612 * re-enable caching here because the QEMU bios
1613 * does not do it - this results in some delay at
1616 if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
1617 hcr0 &= ~(X86_CR0_CD | X86_CR0_NW);
1619 svm->vmcb->save.cr0 = hcr0;
1620 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
1623 * SEV-ES guests must always keep the CR intercepts cleared. CR
1624 * tracking is done using the CR write traps.
1626 if (sev_es_guest(vcpu->kvm))
1630 /* Selective CR0 write remains on. */
1631 svm_clr_intercept(svm, INTERCEPT_CR0_READ);
1632 svm_clr_intercept(svm, INTERCEPT_CR0_WRITE);
1634 svm_set_intercept(svm, INTERCEPT_CR0_READ);
1635 svm_set_intercept(svm, INTERCEPT_CR0_WRITE);
1639 static bool svm_is_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1644 void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1646 unsigned long host_cr4_mce = cr4_read_shadow() & X86_CR4_MCE;
1647 unsigned long old_cr4 = vcpu->arch.cr4;
1649 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1650 svm_flush_tlb(vcpu);
1652 vcpu->arch.cr4 = cr4;
1656 if (!is_paging(vcpu))
1657 cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
1659 cr4 |= host_cr4_mce;
1660 to_svm(vcpu)->vmcb->save.cr4 = cr4;
1661 vmcb_mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR);
1663 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
1664 kvm_update_cpuid_runtime(vcpu);
1667 static void svm_set_segment(struct kvm_vcpu *vcpu,
1668 struct kvm_segment *var, int seg)
1670 struct vcpu_svm *svm = to_svm(vcpu);
1671 struct vmcb_seg *s = svm_seg(vcpu, seg);
1673 s->base = var->base;
1674 s->limit = var->limit;
1675 s->selector = var->selector;
1676 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1677 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1678 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1679 s->attrib |= ((var->present & 1) && !var->unusable) << SVM_SELECTOR_P_SHIFT;
1680 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1681 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1682 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1683 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1686 * This is always accurate, except if SYSRET returned to a segment
1687 * with SS.DPL != 3. Intel does not have this quirk, and always
1688 * forces SS.DPL to 3 on sysret, so we ignore that case; fixing it
1689 * would entail passing the CPL to userspace and back.
1691 if (seg == VCPU_SREG_SS)
1692 /* This is symmetric with svm_get_segment() */
1693 svm->vmcb->save.cpl = (var->dpl & 3);
1695 vmcb_mark_dirty(svm->vmcb, VMCB_SEG);
1698 static void svm_update_exception_bitmap(struct kvm_vcpu *vcpu)
1700 struct vcpu_svm *svm = to_svm(vcpu);
1702 clr_exception_intercept(svm, BP_VECTOR);
1704 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1705 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1706 set_exception_intercept(svm, BP_VECTOR);
1710 static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
1712 if (sd->next_asid > sd->max_asid) {
1713 ++sd->asid_generation;
1714 sd->next_asid = sd->min_asid;
1715 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
1716 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
1719 svm->current_vmcb->asid_generation = sd->asid_generation;
1720 svm->asid = sd->next_asid++;
1723 static void svm_set_dr6(struct vcpu_svm *svm, unsigned long value)
1725 struct vmcb *vmcb = svm->vmcb;
1727 if (svm->vcpu.arch.guest_state_protected)
1730 if (unlikely(value != vmcb->save.dr6)) {
1731 vmcb->save.dr6 = value;
1732 vmcb_mark_dirty(vmcb, VMCB_DR);
1736 static void svm_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
1738 struct vcpu_svm *svm = to_svm(vcpu);
1740 if (vcpu->arch.guest_state_protected)
1743 get_debugreg(vcpu->arch.db[0], 0);
1744 get_debugreg(vcpu->arch.db[1], 1);
1745 get_debugreg(vcpu->arch.db[2], 2);
1746 get_debugreg(vcpu->arch.db[3], 3);
1748 * We cannot reset svm->vmcb->save.dr6 to DR6_ACTIVE_LOW here,
1749 * because db_interception might need it. We can do it before vmentry.
1751 vcpu->arch.dr6 = svm->vmcb->save.dr6;
1752 vcpu->arch.dr7 = svm->vmcb->save.dr7;
1753 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
1754 set_dr_intercepts(svm);
1757 static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
1759 struct vcpu_svm *svm = to_svm(vcpu);
1761 if (vcpu->arch.guest_state_protected)
1764 svm->vmcb->save.dr7 = value;
1765 vmcb_mark_dirty(svm->vmcb, VMCB_DR);
1768 static int pf_interception(struct kvm_vcpu *vcpu)
1770 struct vcpu_svm *svm = to_svm(vcpu);
1772 u64 fault_address = svm->vmcb->control.exit_info_2;
1773 u64 error_code = svm->vmcb->control.exit_info_1;
1775 return kvm_handle_page_fault(vcpu, error_code, fault_address,
1776 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1777 svm->vmcb->control.insn_bytes : NULL,
1778 svm->vmcb->control.insn_len);
1781 static int npf_interception(struct kvm_vcpu *vcpu)
1783 struct vcpu_svm *svm = to_svm(vcpu);
1785 u64 fault_address = svm->vmcb->control.exit_info_2;
1786 u64 error_code = svm->vmcb->control.exit_info_1;
1788 trace_kvm_page_fault(fault_address, error_code);
1789 return kvm_mmu_page_fault(vcpu, fault_address, error_code,
1790 static_cpu_has(X86_FEATURE_DECODEASSISTS) ?
1791 svm->vmcb->control.insn_bytes : NULL,
1792 svm->vmcb->control.insn_len);
1795 static int db_interception(struct kvm_vcpu *vcpu)
1797 struct kvm_run *kvm_run = vcpu->run;
1798 struct vcpu_svm *svm = to_svm(vcpu);
1800 if (!(vcpu->guest_debug &
1801 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
1802 !svm->nmi_singlestep) {
1803 u32 payload = svm->vmcb->save.dr6 ^ DR6_ACTIVE_LOW;
1804 kvm_queue_exception_p(vcpu, DB_VECTOR, payload);
1808 if (svm->nmi_singlestep) {
1809 disable_nmi_singlestep(svm);
1810 /* Make sure we check for pending NMIs upon entry */
1811 kvm_make_request(KVM_REQ_EVENT, vcpu);
1814 if (vcpu->guest_debug &
1815 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
1816 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1817 kvm_run->debug.arch.dr6 = svm->vmcb->save.dr6;
1818 kvm_run->debug.arch.dr7 = svm->vmcb->save.dr7;
1819 kvm_run->debug.arch.pc =
1820 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1821 kvm_run->debug.arch.exception = DB_VECTOR;
1828 static int bp_interception(struct kvm_vcpu *vcpu)
1830 struct vcpu_svm *svm = to_svm(vcpu);
1831 struct kvm_run *kvm_run = vcpu->run;
1833 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1834 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1835 kvm_run->debug.arch.exception = BP_VECTOR;
1839 static int ud_interception(struct kvm_vcpu *vcpu)
1841 return handle_ud(vcpu);
1844 static int ac_interception(struct kvm_vcpu *vcpu)
1846 kvm_queue_exception_e(vcpu, AC_VECTOR, 0);
1850 static bool is_erratum_383(void)
1855 if (!erratum_383_found)
1858 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1862 /* Bit 62 may or may not be set for this mce */
1863 value &= ~(1ULL << 62);
1865 if (value != 0xb600000000010015ULL)
1868 /* Clear MCi_STATUS registers */
1869 for (i = 0; i < 6; ++i)
1870 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1872 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1876 value &= ~(1ULL << 2);
1877 low = lower_32_bits(value);
1878 high = upper_32_bits(value);
1880 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1883 /* Flush tlb to evict multi-match entries */
1889 static void svm_handle_mce(struct kvm_vcpu *vcpu)
1891 if (is_erratum_383()) {
1893 * Erratum 383 triggered. Guest state is corrupt so kill the
1896 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1898 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
1904 * On an #MC intercept the MCE handler is not called automatically in
1905 * the host. So do it by hand here.
1907 kvm_machine_check();
1910 static int mc_interception(struct kvm_vcpu *vcpu)
1915 static int shutdown_interception(struct kvm_vcpu *vcpu)
1917 struct kvm_run *kvm_run = vcpu->run;
1918 struct vcpu_svm *svm = to_svm(vcpu);
1921 * The VM save area has already been encrypted so it
1922 * cannot be reinitialized - just terminate.
1924 if (sev_es_guest(vcpu->kvm))
1928 * VMCB is undefined after a SHUTDOWN intercept. INIT the vCPU to put
1929 * the VMCB in a known good state. Unfortuately, KVM doesn't have
1930 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
1931 * userspace. At a platform view, INIT is acceptable behavior as
1932 * there exist bare metal platforms that automatically INIT the CPU
1933 * in response to shutdown.
1935 clear_page(svm->vmcb);
1936 kvm_vcpu_reset(vcpu, true);
1938 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1942 static int io_interception(struct kvm_vcpu *vcpu)
1944 struct vcpu_svm *svm = to_svm(vcpu);
1945 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
1946 int size, in, string;
1949 ++vcpu->stat.io_exits;
1950 string = (io_info & SVM_IOIO_STR_MASK) != 0;
1951 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
1952 port = io_info >> 16;
1953 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
1956 if (sev_es_guest(vcpu->kvm))
1957 return sev_es_string_io(svm, size, port, in);
1959 return kvm_emulate_instruction(vcpu, 0);
1962 svm->next_rip = svm->vmcb->control.exit_info_2;
1964 return kvm_fast_pio(vcpu, size, port, in);
1967 static int nmi_interception(struct kvm_vcpu *vcpu)
1972 static int smi_interception(struct kvm_vcpu *vcpu)
1977 static int intr_interception(struct kvm_vcpu *vcpu)
1979 ++vcpu->stat.irq_exits;
1983 static int vmload_vmsave_interception(struct kvm_vcpu *vcpu, bool vmload)
1985 struct vcpu_svm *svm = to_svm(vcpu);
1986 struct vmcb *vmcb12;
1987 struct kvm_host_map map;
1990 if (nested_svm_check_permissions(vcpu))
1993 ret = kvm_vcpu_map(vcpu, gpa_to_gfn(svm->vmcb->save.rax), &map);
1996 kvm_inject_gp(vcpu, 0);
2002 ret = kvm_skip_emulated_instruction(vcpu);
2005 svm_copy_vmloadsave_state(svm->vmcb, vmcb12);
2006 svm->sysenter_eip_hi = 0;
2007 svm->sysenter_esp_hi = 0;
2009 svm_copy_vmloadsave_state(vmcb12, svm->vmcb);
2012 kvm_vcpu_unmap(vcpu, &map, true);
2017 static int vmload_interception(struct kvm_vcpu *vcpu)
2019 return vmload_vmsave_interception(vcpu, true);
2022 static int vmsave_interception(struct kvm_vcpu *vcpu)
2024 return vmload_vmsave_interception(vcpu, false);
2027 static int vmrun_interception(struct kvm_vcpu *vcpu)
2029 if (nested_svm_check_permissions(vcpu))
2032 return nested_svm_vmrun(vcpu);
2042 /* Return NONE_SVM_INSTR if not SVM instrs, otherwise return decode result */
2043 static int svm_instr_opcode(struct kvm_vcpu *vcpu)
2045 struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt;
2047 if (ctxt->b != 0x1 || ctxt->opcode_len != 2)
2048 return NONE_SVM_INSTR;
2050 switch (ctxt->modrm) {
2051 case 0xd8: /* VMRUN */
2052 return SVM_INSTR_VMRUN;
2053 case 0xda: /* VMLOAD */
2054 return SVM_INSTR_VMLOAD;
2055 case 0xdb: /* VMSAVE */
2056 return SVM_INSTR_VMSAVE;
2061 return NONE_SVM_INSTR;
2064 static int emulate_svm_instr(struct kvm_vcpu *vcpu, int opcode)
2066 const int guest_mode_exit_codes[] = {
2067 [SVM_INSTR_VMRUN] = SVM_EXIT_VMRUN,
2068 [SVM_INSTR_VMLOAD] = SVM_EXIT_VMLOAD,
2069 [SVM_INSTR_VMSAVE] = SVM_EXIT_VMSAVE,
2071 int (*const svm_instr_handlers[])(struct kvm_vcpu *vcpu) = {
2072 [SVM_INSTR_VMRUN] = vmrun_interception,
2073 [SVM_INSTR_VMLOAD] = vmload_interception,
2074 [SVM_INSTR_VMSAVE] = vmsave_interception,
2076 struct vcpu_svm *svm = to_svm(vcpu);
2079 if (is_guest_mode(vcpu)) {
2080 /* Returns '1' or -errno on failure, '0' on success. */
2081 ret = nested_svm_simple_vmexit(svm, guest_mode_exit_codes[opcode]);
2086 return svm_instr_handlers[opcode](vcpu);
2090 * #GP handling code. Note that #GP can be triggered under the following two
2092 * 1) SVM VM-related instructions (VMRUN/VMSAVE/VMLOAD) that trigger #GP on
2093 * some AMD CPUs when EAX of these instructions are in the reserved memory
2094 * regions (e.g. SMM memory on host).
2095 * 2) VMware backdoor
2097 static int gp_interception(struct kvm_vcpu *vcpu)
2099 struct vcpu_svm *svm = to_svm(vcpu);
2100 u32 error_code = svm->vmcb->control.exit_info_1;
2103 /* Both #GP cases have zero error_code */
2107 /* Decode the instruction for usage later */
2108 if (x86_decode_emulated_instruction(vcpu, 0, NULL, 0) != EMULATION_OK)
2111 opcode = svm_instr_opcode(vcpu);
2113 if (opcode == NONE_SVM_INSTR) {
2114 if (!enable_vmware_backdoor)
2118 * VMware backdoor emulation on #GP interception only handles
2119 * IN{S}, OUT{S}, and RDPMC.
2121 if (!is_guest_mode(vcpu))
2122 return kvm_emulate_instruction(vcpu,
2123 EMULTYPE_VMWARE_GP | EMULTYPE_NO_DECODE);
2125 /* All SVM instructions expect page aligned RAX */
2126 if (svm->vmcb->save.rax & ~PAGE_MASK)
2129 return emulate_svm_instr(vcpu, opcode);
2133 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
2137 void svm_set_gif(struct vcpu_svm *svm, bool value)
2141 * If VGIF is enabled, the STGI intercept is only added to
2142 * detect the opening of the SMI/NMI window; remove it now.
2143 * Likewise, clear the VINTR intercept, we will set it
2144 * again while processing KVM_REQ_EVENT if needed.
2146 if (vgif_enabled(svm))
2147 svm_clr_intercept(svm, INTERCEPT_STGI);
2148 if (svm_is_intercept(svm, INTERCEPT_VINTR))
2149 svm_clear_vintr(svm);
2152 if (svm->vcpu.arch.smi_pending ||
2153 svm->vcpu.arch.nmi_pending ||
2154 kvm_cpu_has_injectable_intr(&svm->vcpu))
2155 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
2160 * After a CLGI no interrupts should come. But if vGIF is
2161 * in use, we still rely on the VINTR intercept (rather than
2162 * STGI) to detect an open interrupt window.
2164 if (!vgif_enabled(svm))
2165 svm_clear_vintr(svm);
2169 static int stgi_interception(struct kvm_vcpu *vcpu)
2173 if (nested_svm_check_permissions(vcpu))
2176 ret = kvm_skip_emulated_instruction(vcpu);
2177 svm_set_gif(to_svm(vcpu), true);
2181 static int clgi_interception(struct kvm_vcpu *vcpu)
2185 if (nested_svm_check_permissions(vcpu))
2188 ret = kvm_skip_emulated_instruction(vcpu);
2189 svm_set_gif(to_svm(vcpu), false);
2193 static int invlpga_interception(struct kvm_vcpu *vcpu)
2195 gva_t gva = kvm_rax_read(vcpu);
2196 u32 asid = kvm_rcx_read(vcpu);
2198 /* FIXME: Handle an address size prefix. */
2199 if (!is_long_mode(vcpu))
2202 trace_kvm_invlpga(to_svm(vcpu)->vmcb->save.rip, asid, gva);
2204 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2205 kvm_mmu_invlpg(vcpu, gva);
2207 return kvm_skip_emulated_instruction(vcpu);
2210 static int skinit_interception(struct kvm_vcpu *vcpu)
2212 trace_kvm_skinit(to_svm(vcpu)->vmcb->save.rip, kvm_rax_read(vcpu));
2214 kvm_queue_exception(vcpu, UD_VECTOR);
2218 static int task_switch_interception(struct kvm_vcpu *vcpu)
2220 struct vcpu_svm *svm = to_svm(vcpu);
2223 int int_type = svm->vmcb->control.exit_int_info &
2224 SVM_EXITINTINFO_TYPE_MASK;
2225 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
2227 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2229 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
2230 bool has_error_code = false;
2233 tss_selector = (u16)svm->vmcb->control.exit_info_1;
2235 if (svm->vmcb->control.exit_info_2 &
2236 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
2237 reason = TASK_SWITCH_IRET;
2238 else if (svm->vmcb->control.exit_info_2 &
2239 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2240 reason = TASK_SWITCH_JMP;
2242 reason = TASK_SWITCH_GATE;
2244 reason = TASK_SWITCH_CALL;
2246 if (reason == TASK_SWITCH_GATE) {
2248 case SVM_EXITINTINFO_TYPE_NMI:
2249 vcpu->arch.nmi_injected = false;
2251 case SVM_EXITINTINFO_TYPE_EXEPT:
2252 if (svm->vmcb->control.exit_info_2 &
2253 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2254 has_error_code = true;
2256 (u32)svm->vmcb->control.exit_info_2;
2258 kvm_clear_exception_queue(vcpu);
2260 case SVM_EXITINTINFO_TYPE_INTR:
2261 kvm_clear_interrupt_queue(vcpu);
2268 if (reason != TASK_SWITCH_GATE ||
2269 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2270 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
2271 (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) {
2272 if (!skip_emulated_instruction(vcpu))
2276 if (int_type != SVM_EXITINTINFO_TYPE_SOFT)
2279 return kvm_task_switch(vcpu, tss_selector, int_vec, reason,
2280 has_error_code, error_code);
2283 static int iret_interception(struct kvm_vcpu *vcpu)
2285 struct vcpu_svm *svm = to_svm(vcpu);
2287 ++vcpu->stat.nmi_window_exits;
2288 vcpu->arch.hflags |= HF_IRET_MASK;
2289 if (!sev_es_guest(vcpu->kvm)) {
2290 svm_clr_intercept(svm, INTERCEPT_IRET);
2291 svm->nmi_iret_rip = kvm_rip_read(vcpu);
2293 kvm_make_request(KVM_REQ_EVENT, vcpu);
2297 static int invlpg_interception(struct kvm_vcpu *vcpu)
2299 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2300 return kvm_emulate_instruction(vcpu, 0);
2302 kvm_mmu_invlpg(vcpu, to_svm(vcpu)->vmcb->control.exit_info_1);
2303 return kvm_skip_emulated_instruction(vcpu);
2306 static int emulate_on_interception(struct kvm_vcpu *vcpu)
2308 return kvm_emulate_instruction(vcpu, 0);
2311 static int rsm_interception(struct kvm_vcpu *vcpu)
2313 return kvm_emulate_instruction_from_buffer(vcpu, rsm_ins_bytes, 2);
2316 static bool check_selective_cr0_intercepted(struct kvm_vcpu *vcpu,
2319 struct vcpu_svm *svm = to_svm(vcpu);
2320 unsigned long cr0 = vcpu->arch.cr0;
2323 if (!is_guest_mode(vcpu) ||
2324 (!(vmcb12_is_intercept(&svm->nested.ctl, INTERCEPT_SELECTIVE_CR0))))
2327 cr0 &= ~SVM_CR0_SELECTIVE_MASK;
2328 val &= ~SVM_CR0_SELECTIVE_MASK;
2331 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
2332 ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE);
2338 #define CR_VALID (1ULL << 63)
2340 static int cr_interception(struct kvm_vcpu *vcpu)
2342 struct vcpu_svm *svm = to_svm(vcpu);
2347 if (!static_cpu_has(X86_FEATURE_DECODEASSISTS))
2348 return emulate_on_interception(vcpu);
2350 if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0))
2351 return emulate_on_interception(vcpu);
2353 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2354 if (svm->vmcb->control.exit_code == SVM_EXIT_CR0_SEL_WRITE)
2355 cr = SVM_EXIT_WRITE_CR0 - SVM_EXIT_READ_CR0;
2357 cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0;
2360 if (cr >= 16) { /* mov to cr */
2362 val = kvm_register_read(vcpu, reg);
2363 trace_kvm_cr_write(cr, val);
2366 if (!check_selective_cr0_intercepted(vcpu, val))
2367 err = kvm_set_cr0(vcpu, val);
2373 err = kvm_set_cr3(vcpu, val);
2376 err = kvm_set_cr4(vcpu, val);
2379 err = kvm_set_cr8(vcpu, val);
2382 WARN(1, "unhandled write to CR%d", cr);
2383 kvm_queue_exception(vcpu, UD_VECTOR);
2386 } else { /* mov from cr */
2389 val = kvm_read_cr0(vcpu);
2392 val = vcpu->arch.cr2;
2395 val = kvm_read_cr3(vcpu);
2398 val = kvm_read_cr4(vcpu);
2401 val = kvm_get_cr8(vcpu);
2404 WARN(1, "unhandled read from CR%d", cr);
2405 kvm_queue_exception(vcpu, UD_VECTOR);
2408 kvm_register_write(vcpu, reg, val);
2409 trace_kvm_cr_read(cr, val);
2411 return kvm_complete_insn_gp(vcpu, err);
2414 static int cr_trap(struct kvm_vcpu *vcpu)
2416 struct vcpu_svm *svm = to_svm(vcpu);
2417 unsigned long old_value, new_value;
2421 new_value = (unsigned long)svm->vmcb->control.exit_info_1;
2423 cr = svm->vmcb->control.exit_code - SVM_EXIT_CR0_WRITE_TRAP;
2426 old_value = kvm_read_cr0(vcpu);
2427 svm_set_cr0(vcpu, new_value);
2429 kvm_post_set_cr0(vcpu, old_value, new_value);
2432 old_value = kvm_read_cr4(vcpu);
2433 svm_set_cr4(vcpu, new_value);
2435 kvm_post_set_cr4(vcpu, old_value, new_value);
2438 ret = kvm_set_cr8(vcpu, new_value);
2441 WARN(1, "unhandled CR%d write trap", cr);
2442 kvm_queue_exception(vcpu, UD_VECTOR);
2446 return kvm_complete_insn_gp(vcpu, ret);
2449 static int dr_interception(struct kvm_vcpu *vcpu)
2451 struct vcpu_svm *svm = to_svm(vcpu);
2456 if (vcpu->guest_debug == 0) {
2458 * No more DR vmexits; force a reload of the debug registers
2459 * and reenter on this instruction. The next vmexit will
2460 * retrieve the full state of the debug registers.
2462 clr_dr_intercepts(svm);
2463 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
2467 if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS))
2468 return emulate_on_interception(vcpu);
2470 reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK;
2471 dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0;
2472 if (dr >= 16) { /* mov to DRn */
2474 val = kvm_register_read(vcpu, reg);
2475 err = kvm_set_dr(vcpu, dr, val);
2477 kvm_get_dr(vcpu, dr, &val);
2478 kvm_register_write(vcpu, reg, val);
2481 return kvm_complete_insn_gp(vcpu, err);
2484 static int cr8_write_interception(struct kvm_vcpu *vcpu)
2488 u8 cr8_prev = kvm_get_cr8(vcpu);
2489 /* instruction emulation calls kvm_set_cr8() */
2490 r = cr_interception(vcpu);
2491 if (lapic_in_kernel(vcpu))
2493 if (cr8_prev <= kvm_get_cr8(vcpu))
2495 vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
2499 static int efer_trap(struct kvm_vcpu *vcpu)
2501 struct msr_data msr_info;
2505 * Clear the EFER_SVME bit from EFER. The SVM code always sets this
2506 * bit in svm_set_efer(), but __kvm_valid_efer() checks it against
2507 * whether the guest has X86_FEATURE_SVM - this avoids a failure if
2508 * the guest doesn't have X86_FEATURE_SVM.
2510 msr_info.host_initiated = false;
2511 msr_info.index = MSR_EFER;
2512 msr_info.data = to_svm(vcpu)->vmcb->control.exit_info_1 & ~EFER_SVME;
2513 ret = kvm_set_msr_common(vcpu, &msr_info);
2515 return kvm_complete_insn_gp(vcpu, ret);
2518 static int svm_get_msr_feature(struct kvm_msr_entry *msr)
2522 switch (msr->index) {
2523 case MSR_F10H_DECFG:
2524 if (boot_cpu_has(X86_FEATURE_LFENCE_RDTSC))
2525 msr->data |= MSR_F10H_DECFG_LFENCE_SERIALIZE;
2527 case MSR_IA32_PERF_CAPABILITIES:
2530 return KVM_MSR_RET_INVALID;
2536 static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2538 struct vcpu_svm *svm = to_svm(vcpu);
2540 switch (msr_info->index) {
2541 case MSR_AMD64_TSC_RATIO:
2542 if (!msr_info->host_initiated && !svm->tsc_scaling_enabled)
2544 msr_info->data = svm->tsc_ratio_msr;
2547 msr_info->data = svm->vmcb01.ptr->save.star;
2549 #ifdef CONFIG_X86_64
2551 msr_info->data = svm->vmcb01.ptr->save.lstar;
2554 msr_info->data = svm->vmcb01.ptr->save.cstar;
2556 case MSR_KERNEL_GS_BASE:
2557 msr_info->data = svm->vmcb01.ptr->save.kernel_gs_base;
2559 case MSR_SYSCALL_MASK:
2560 msr_info->data = svm->vmcb01.ptr->save.sfmask;
2563 case MSR_IA32_SYSENTER_CS:
2564 msr_info->data = svm->vmcb01.ptr->save.sysenter_cs;
2566 case MSR_IA32_SYSENTER_EIP:
2567 msr_info->data = (u32)svm->vmcb01.ptr->save.sysenter_eip;
2568 if (guest_cpuid_is_intel(vcpu))
2569 msr_info->data |= (u64)svm->sysenter_eip_hi << 32;
2571 case MSR_IA32_SYSENTER_ESP:
2572 msr_info->data = svm->vmcb01.ptr->save.sysenter_esp;
2573 if (guest_cpuid_is_intel(vcpu))
2574 msr_info->data |= (u64)svm->sysenter_esp_hi << 32;
2577 msr_info->data = svm->tsc_aux;
2580 * Nobody will change the following 5 values in the VMCB so we can
2581 * safely return them on rdmsr. They will always be 0 until LBRV is
2584 case MSR_IA32_DEBUGCTLMSR:
2585 msr_info->data = svm->vmcb->save.dbgctl;
2587 case MSR_IA32_LASTBRANCHFROMIP:
2588 msr_info->data = svm->vmcb->save.br_from;
2590 case MSR_IA32_LASTBRANCHTOIP:
2591 msr_info->data = svm->vmcb->save.br_to;
2593 case MSR_IA32_LASTINTFROMIP:
2594 msr_info->data = svm->vmcb->save.last_excp_from;
2596 case MSR_IA32_LASTINTTOIP:
2597 msr_info->data = svm->vmcb->save.last_excp_to;
2599 case MSR_VM_HSAVE_PA:
2600 msr_info->data = svm->nested.hsave_msr;
2603 msr_info->data = svm->nested.vm_cr_msr;
2605 case MSR_IA32_SPEC_CTRL:
2606 if (!msr_info->host_initiated &&
2607 !guest_has_spec_ctrl_msr(vcpu))
2610 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2611 msr_info->data = svm->vmcb->save.spec_ctrl;
2613 msr_info->data = svm->spec_ctrl;
2615 case MSR_AMD64_VIRT_SPEC_CTRL:
2616 if (!msr_info->host_initiated &&
2617 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2620 msr_info->data = svm->virt_spec_ctrl;
2622 case MSR_F15H_IC_CFG: {
2626 family = guest_cpuid_family(vcpu);
2627 model = guest_cpuid_model(vcpu);
2629 if (family < 0 || model < 0)
2630 return kvm_get_msr_common(vcpu, msr_info);
2634 if (family == 0x15 &&
2635 (model >= 0x2 && model < 0x20))
2636 msr_info->data = 0x1E;
2639 case MSR_F10H_DECFG:
2640 msr_info->data = svm->msr_decfg;
2643 return kvm_get_msr_common(vcpu, msr_info);
2648 static int svm_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)
2650 struct vcpu_svm *svm = to_svm(vcpu);
2651 if (!err || !sev_es_guest(vcpu->kvm) || WARN_ON_ONCE(!svm->sev_es.ghcb))
2652 return kvm_complete_insn_gp(vcpu, err);
2654 ghcb_set_sw_exit_info_1(svm->sev_es.ghcb, 1);
2655 ghcb_set_sw_exit_info_2(svm->sev_es.ghcb,
2657 SVM_EVTINJ_TYPE_EXEPT |
2662 static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2664 struct vcpu_svm *svm = to_svm(vcpu);
2665 int svm_dis, chg_mask;
2667 if (data & ~SVM_VM_CR_VALID_MASK)
2670 chg_mask = SVM_VM_CR_VALID_MASK;
2672 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2673 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2675 svm->nested.vm_cr_msr &= ~chg_mask;
2676 svm->nested.vm_cr_msr |= (data & chg_mask);
2678 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2680 /* check for svm_disable while efer.svme is set */
2681 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2687 static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
2689 struct vcpu_svm *svm = to_svm(vcpu);
2692 u32 ecx = msr->index;
2693 u64 data = msr->data;
2695 case MSR_AMD64_TSC_RATIO:
2696 if (!msr->host_initiated && !svm->tsc_scaling_enabled)
2699 if (data & TSC_RATIO_RSVD)
2702 svm->tsc_ratio_msr = data;
2704 if (svm->tsc_scaling_enabled && is_guest_mode(vcpu))
2705 nested_svm_update_tsc_ratio_msr(vcpu);
2708 case MSR_IA32_CR_PAT:
2709 if (!kvm_mtrr_valid(vcpu, MSR_IA32_CR_PAT, data))
2711 vcpu->arch.pat = data;
2712 svm->vmcb01.ptr->save.g_pat = data;
2713 if (is_guest_mode(vcpu))
2714 nested_vmcb02_compute_g_pat(svm);
2715 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
2717 case MSR_IA32_SPEC_CTRL:
2718 if (!msr->host_initiated &&
2719 !guest_has_spec_ctrl_msr(vcpu))
2722 if (kvm_spec_ctrl_test_value(data))
2725 if (boot_cpu_has(X86_FEATURE_V_SPEC_CTRL))
2726 svm->vmcb->save.spec_ctrl = data;
2728 svm->spec_ctrl = data;
2734 * When it's written (to non-zero) for the first time, pass
2738 * The handling of the MSR bitmap for L2 guests is done in
2739 * nested_svm_vmrun_msrpm.
2740 * We update the L1 MSR bit as well since it will end up
2741 * touching the MSR anyway now.
2743 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1);
2745 case MSR_IA32_PRED_CMD:
2746 if (!msr->host_initiated &&
2747 !guest_has_pred_cmd_msr(vcpu))
2750 if (data & ~PRED_CMD_IBPB)
2752 if (!boot_cpu_has(X86_FEATURE_IBPB))
2757 wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2758 set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1);
2760 case MSR_AMD64_VIRT_SPEC_CTRL:
2761 if (!msr->host_initiated &&
2762 !guest_cpuid_has(vcpu, X86_FEATURE_VIRT_SSBD))
2765 if (data & ~SPEC_CTRL_SSBD)
2768 svm->virt_spec_ctrl = data;
2771 svm->vmcb01.ptr->save.star = data;
2773 #ifdef CONFIG_X86_64
2775 svm->vmcb01.ptr->save.lstar = data;
2778 svm->vmcb01.ptr->save.cstar = data;
2780 case MSR_KERNEL_GS_BASE:
2781 svm->vmcb01.ptr->save.kernel_gs_base = data;
2783 case MSR_SYSCALL_MASK:
2784 svm->vmcb01.ptr->save.sfmask = data;
2787 case MSR_IA32_SYSENTER_CS:
2788 svm->vmcb01.ptr->save.sysenter_cs = data;
2790 case MSR_IA32_SYSENTER_EIP:
2791 svm->vmcb01.ptr->save.sysenter_eip = (u32)data;
2793 * We only intercept the MSR_IA32_SYSENTER_{EIP|ESP} msrs
2794 * when we spoof an Intel vendor ID (for cross vendor migration).
2795 * In this case we use this intercept to track the high
2796 * 32 bit part of these msrs to support Intel's
2797 * implementation of SYSENTER/SYSEXIT.
2799 svm->sysenter_eip_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2801 case MSR_IA32_SYSENTER_ESP:
2802 svm->vmcb01.ptr->save.sysenter_esp = (u32)data;
2803 svm->sysenter_esp_hi = guest_cpuid_is_intel(vcpu) ? (data >> 32) : 0;
2807 * TSC_AUX is usually changed only during boot and never read
2808 * directly. Intercept TSC_AUX instead of exposing it to the
2809 * guest via direct_access_msrs, and switch it via user return.
2812 r = kvm_set_user_return_msr(tsc_aux_uret_slot, data, -1ull);
2817 svm->tsc_aux = data;
2819 case MSR_IA32_DEBUGCTLMSR:
2821 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
2825 if (data & DEBUGCTL_RESERVED_BITS)
2828 svm->vmcb->save.dbgctl = data;
2829 vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
2830 if (data & (1ULL<<0))
2831 svm_enable_lbrv(vcpu);
2833 svm_disable_lbrv(vcpu);
2835 case MSR_VM_HSAVE_PA:
2837 * Old kernels did not validate the value written to
2838 * MSR_VM_HSAVE_PA. Allow KVM_SET_MSR to set an invalid
2839 * value to allow live migrating buggy or malicious guests
2840 * originating from those kernels.
2842 if (!msr->host_initiated && !page_address_valid(vcpu, data))
2845 svm->nested.hsave_msr = data & PAGE_MASK;
2848 return svm_set_vm_cr(vcpu, data);
2850 vcpu_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2852 case MSR_F10H_DECFG: {
2853 struct kvm_msr_entry msr_entry;
2855 msr_entry.index = msr->index;
2856 if (svm_get_msr_feature(&msr_entry))
2859 /* Check the supported bits */
2860 if (data & ~msr_entry.data)
2863 /* Don't allow the guest to change a bit, #GP */
2864 if (!msr->host_initiated && (data ^ msr_entry.data))
2867 svm->msr_decfg = data;
2871 return kvm_set_msr_common(vcpu, msr);
2876 static int msr_interception(struct kvm_vcpu *vcpu)
2878 if (to_svm(vcpu)->vmcb->control.exit_info_1)
2879 return kvm_emulate_wrmsr(vcpu);
2881 return kvm_emulate_rdmsr(vcpu);
2884 static int interrupt_window_interception(struct kvm_vcpu *vcpu)
2886 kvm_make_request(KVM_REQ_EVENT, vcpu);
2887 svm_clear_vintr(to_svm(vcpu));
2890 * For AVIC, the only reason to end up here is ExtINTs.
2891 * In this case AVIC was temporarily disabled for
2892 * requesting the IRQ window and we have to re-enable it.
2894 kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN);
2896 ++vcpu->stat.irq_window_exits;
2900 static int pause_interception(struct kvm_vcpu *vcpu)
2905 * CPL is not made available for an SEV-ES guest, therefore
2906 * vcpu->arch.preempted_in_kernel can never be true. Just
2907 * set in_kernel to false as well.
2909 in_kernel = !sev_es_guest(vcpu->kvm) && svm_get_cpl(vcpu) == 0;
2911 if (!kvm_pause_in_guest(vcpu->kvm))
2912 grow_ple_window(vcpu);
2914 kvm_vcpu_on_spin(vcpu, in_kernel);
2915 return kvm_skip_emulated_instruction(vcpu);
2918 static int invpcid_interception(struct kvm_vcpu *vcpu)
2920 struct vcpu_svm *svm = to_svm(vcpu);
2924 if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
2925 kvm_queue_exception(vcpu, UD_VECTOR);
2930 * For an INVPCID intercept:
2931 * EXITINFO1 provides the linear address of the memory operand.
2932 * EXITINFO2 provides the contents of the register operand.
2934 type = svm->vmcb->control.exit_info_2;
2935 gva = svm->vmcb->control.exit_info_1;
2937 return kvm_handle_invpcid(vcpu, type, gva);
2940 static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
2941 [SVM_EXIT_READ_CR0] = cr_interception,
2942 [SVM_EXIT_READ_CR3] = cr_interception,
2943 [SVM_EXIT_READ_CR4] = cr_interception,
2944 [SVM_EXIT_READ_CR8] = cr_interception,
2945 [SVM_EXIT_CR0_SEL_WRITE] = cr_interception,
2946 [SVM_EXIT_WRITE_CR0] = cr_interception,
2947 [SVM_EXIT_WRITE_CR3] = cr_interception,
2948 [SVM_EXIT_WRITE_CR4] = cr_interception,
2949 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2950 [SVM_EXIT_READ_DR0] = dr_interception,
2951 [SVM_EXIT_READ_DR1] = dr_interception,
2952 [SVM_EXIT_READ_DR2] = dr_interception,
2953 [SVM_EXIT_READ_DR3] = dr_interception,
2954 [SVM_EXIT_READ_DR4] = dr_interception,
2955 [SVM_EXIT_READ_DR5] = dr_interception,
2956 [SVM_EXIT_READ_DR6] = dr_interception,
2957 [SVM_EXIT_READ_DR7] = dr_interception,
2958 [SVM_EXIT_WRITE_DR0] = dr_interception,
2959 [SVM_EXIT_WRITE_DR1] = dr_interception,
2960 [SVM_EXIT_WRITE_DR2] = dr_interception,
2961 [SVM_EXIT_WRITE_DR3] = dr_interception,
2962 [SVM_EXIT_WRITE_DR4] = dr_interception,
2963 [SVM_EXIT_WRITE_DR5] = dr_interception,
2964 [SVM_EXIT_WRITE_DR6] = dr_interception,
2965 [SVM_EXIT_WRITE_DR7] = dr_interception,
2966 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2967 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
2968 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
2969 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2970 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2971 [SVM_EXIT_EXCP_BASE + AC_VECTOR] = ac_interception,
2972 [SVM_EXIT_EXCP_BASE + GP_VECTOR] = gp_interception,
2973 [SVM_EXIT_INTR] = intr_interception,
2974 [SVM_EXIT_NMI] = nmi_interception,
2975 [SVM_EXIT_SMI] = smi_interception,
2976 [SVM_EXIT_VINTR] = interrupt_window_interception,
2977 [SVM_EXIT_RDPMC] = kvm_emulate_rdpmc,
2978 [SVM_EXIT_CPUID] = kvm_emulate_cpuid,
2979 [SVM_EXIT_IRET] = iret_interception,
2980 [SVM_EXIT_INVD] = kvm_emulate_invd,
2981 [SVM_EXIT_PAUSE] = pause_interception,
2982 [SVM_EXIT_HLT] = kvm_emulate_halt,
2983 [SVM_EXIT_INVLPG] = invlpg_interception,
2984 [SVM_EXIT_INVLPGA] = invlpga_interception,
2985 [SVM_EXIT_IOIO] = io_interception,
2986 [SVM_EXIT_MSR] = msr_interception,
2987 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
2988 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
2989 [SVM_EXIT_VMRUN] = vmrun_interception,
2990 [SVM_EXIT_VMMCALL] = kvm_emulate_hypercall,
2991 [SVM_EXIT_VMLOAD] = vmload_interception,
2992 [SVM_EXIT_VMSAVE] = vmsave_interception,
2993 [SVM_EXIT_STGI] = stgi_interception,
2994 [SVM_EXIT_CLGI] = clgi_interception,
2995 [SVM_EXIT_SKINIT] = skinit_interception,
2996 [SVM_EXIT_RDTSCP] = kvm_handle_invalid_op,
2997 [SVM_EXIT_WBINVD] = kvm_emulate_wbinvd,
2998 [SVM_EXIT_MONITOR] = kvm_emulate_monitor,
2999 [SVM_EXIT_MWAIT] = kvm_emulate_mwait,
3000 [SVM_EXIT_XSETBV] = kvm_emulate_xsetbv,
3001 [SVM_EXIT_RDPRU] = kvm_handle_invalid_op,
3002 [SVM_EXIT_EFER_WRITE_TRAP] = efer_trap,
3003 [SVM_EXIT_CR0_WRITE_TRAP] = cr_trap,
3004 [SVM_EXIT_CR4_WRITE_TRAP] = cr_trap,
3005 [SVM_EXIT_CR8_WRITE_TRAP] = cr_trap,
3006 [SVM_EXIT_INVPCID] = invpcid_interception,
3007 [SVM_EXIT_NPF] = npf_interception,
3008 [SVM_EXIT_RSM] = rsm_interception,
3009 [SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
3010 [SVM_EXIT_AVIC_UNACCELERATED_ACCESS] = avic_unaccelerated_access_interception,
3011 [SVM_EXIT_VMGEXIT] = sev_handle_vmgexit,
3014 static void dump_vmcb(struct kvm_vcpu *vcpu)
3016 struct vcpu_svm *svm = to_svm(vcpu);
3017 struct vmcb_control_area *control = &svm->vmcb->control;
3018 struct vmcb_save_area *save = &svm->vmcb->save;
3019 struct vmcb_save_area *save01 = &svm->vmcb01.ptr->save;
3021 if (!dump_invalid_vmcb) {
3022 pr_warn_ratelimited("set kvm_amd.dump_invalid_vmcb=1 to dump internal KVM state.\n");
3026 pr_err("VMCB %p, last attempted VMRUN on CPU %d\n",
3027 svm->current_vmcb->ptr, vcpu->arch.last_vmentry_cpu);
3028 pr_err("VMCB Control Area:\n");
3029 pr_err("%-20s%04x\n", "cr_read:", control->intercepts[INTERCEPT_CR] & 0xffff);
3030 pr_err("%-20s%04x\n", "cr_write:", control->intercepts[INTERCEPT_CR] >> 16);
3031 pr_err("%-20s%04x\n", "dr_read:", control->intercepts[INTERCEPT_DR] & 0xffff);
3032 pr_err("%-20s%04x\n", "dr_write:", control->intercepts[INTERCEPT_DR] >> 16);
3033 pr_err("%-20s%08x\n", "exceptions:", control->intercepts[INTERCEPT_EXCEPTION]);
3034 pr_err("%-20s%08x %08x\n", "intercepts:",
3035 control->intercepts[INTERCEPT_WORD3],
3036 control->intercepts[INTERCEPT_WORD4]);
3037 pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count);
3038 pr_err("%-20s%d\n", "pause filter threshold:",
3039 control->pause_filter_thresh);
3040 pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa);
3041 pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa);
3042 pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset);
3043 pr_err("%-20s%d\n", "asid:", control->asid);
3044 pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl);
3045 pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl);
3046 pr_err("%-20s%08x\n", "int_vector:", control->int_vector);
3047 pr_err("%-20s%08x\n", "int_state:", control->int_state);
3048 pr_err("%-20s%08x\n", "exit_code:", control->exit_code);
3049 pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1);
3050 pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2);
3051 pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info);
3052 pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err);
3053 pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl);
3054 pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3);
3055 pr_err("%-20s%016llx\n", "avic_vapic_bar:", control->avic_vapic_bar);
3056 pr_err("%-20s%016llx\n", "ghcb:", control->ghcb_gpa);
3057 pr_err("%-20s%08x\n", "event_inj:", control->event_inj);
3058 pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err);
3059 pr_err("%-20s%lld\n", "virt_ext:", control->virt_ext);
3060 pr_err("%-20s%016llx\n", "next_rip:", control->next_rip);
3061 pr_err("%-20s%016llx\n", "avic_backing_page:", control->avic_backing_page);
3062 pr_err("%-20s%016llx\n", "avic_logical_id:", control->avic_logical_id);
3063 pr_err("%-20s%016llx\n", "avic_physical_id:", control->avic_physical_id);
3064 pr_err("%-20s%016llx\n", "vmsa_pa:", control->vmsa_pa);
3065 pr_err("VMCB State Save Area:\n");
3066 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3068 save->es.selector, save->es.attrib,
3069 save->es.limit, save->es.base);
3070 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3072 save->cs.selector, save->cs.attrib,
3073 save->cs.limit, save->cs.base);
3074 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3076 save->ss.selector, save->ss.attrib,
3077 save->ss.limit, save->ss.base);
3078 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3080 save->ds.selector, save->ds.attrib,
3081 save->ds.limit, save->ds.base);
3082 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3084 save01->fs.selector, save01->fs.attrib,
3085 save01->fs.limit, save01->fs.base);
3086 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3088 save01->gs.selector, save01->gs.attrib,
3089 save01->gs.limit, save01->gs.base);
3090 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3092 save->gdtr.selector, save->gdtr.attrib,
3093 save->gdtr.limit, save->gdtr.base);
3094 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3096 save01->ldtr.selector, save01->ldtr.attrib,
3097 save01->ldtr.limit, save01->ldtr.base);
3098 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3100 save->idtr.selector, save->idtr.attrib,
3101 save->idtr.limit, save->idtr.base);
3102 pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n",
3104 save01->tr.selector, save01->tr.attrib,
3105 save01->tr.limit, save01->tr.base);
3106 pr_err("cpl: %d efer: %016llx\n",
3107 save->cpl, save->efer);
3108 pr_err("%-15s %016llx %-13s %016llx\n",
3109 "cr0:", save->cr0, "cr2:", save->cr2);
3110 pr_err("%-15s %016llx %-13s %016llx\n",
3111 "cr3:", save->cr3, "cr4:", save->cr4);
3112 pr_err("%-15s %016llx %-13s %016llx\n",
3113 "dr6:", save->dr6, "dr7:", save->dr7);
3114 pr_err("%-15s %016llx %-13s %016llx\n",
3115 "rip:", save->rip, "rflags:", save->rflags);
3116 pr_err("%-15s %016llx %-13s %016llx\n",
3117 "rsp:", save->rsp, "rax:", save->rax);
3118 pr_err("%-15s %016llx %-13s %016llx\n",
3119 "star:", save01->star, "lstar:", save01->lstar);
3120 pr_err("%-15s %016llx %-13s %016llx\n",
3121 "cstar:", save01->cstar, "sfmask:", save01->sfmask);
3122 pr_err("%-15s %016llx %-13s %016llx\n",
3123 "kernel_gs_base:", save01->kernel_gs_base,
3124 "sysenter_cs:", save01->sysenter_cs);
3125 pr_err("%-15s %016llx %-13s %016llx\n",
3126 "sysenter_esp:", save01->sysenter_esp,
3127 "sysenter_eip:", save01->sysenter_eip);
3128 pr_err("%-15s %016llx %-13s %016llx\n",
3129 "gpat:", save->g_pat, "dbgctl:", save->dbgctl);
3130 pr_err("%-15s %016llx %-13s %016llx\n",
3131 "br_from:", save->br_from, "br_to:", save->br_to);
3132 pr_err("%-15s %016llx %-13s %016llx\n",
3133 "excp_from:", save->last_excp_from,
3134 "excp_to:", save->last_excp_to);
3137 static bool svm_check_exit_valid(struct kvm_vcpu *vcpu, u64 exit_code)
3139 return (exit_code < ARRAY_SIZE(svm_exit_handlers) &&
3140 svm_exit_handlers[exit_code]);
3143 static int svm_handle_invalid_exit(struct kvm_vcpu *vcpu, u64 exit_code)
3145 vcpu_unimpl(vcpu, "svm: unexpected exit reason 0x%llx\n", exit_code);
3147 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
3148 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
3149 vcpu->run->internal.ndata = 2;
3150 vcpu->run->internal.data[0] = exit_code;
3151 vcpu->run->internal.data[1] = vcpu->arch.last_vmentry_cpu;
3155 int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
3157 if (!svm_check_exit_valid(vcpu, exit_code))
3158 return svm_handle_invalid_exit(vcpu, exit_code);
3160 #ifdef CONFIG_RETPOLINE
3161 if (exit_code == SVM_EXIT_MSR)
3162 return msr_interception(vcpu);
3163 else if (exit_code == SVM_EXIT_VINTR)
3164 return interrupt_window_interception(vcpu);
3165 else if (exit_code == SVM_EXIT_INTR)
3166 return intr_interception(vcpu);
3167 else if (exit_code == SVM_EXIT_HLT)
3168 return kvm_emulate_halt(vcpu);
3169 else if (exit_code == SVM_EXIT_NPF)
3170 return npf_interception(vcpu);
3172 return svm_exit_handlers[exit_code](vcpu);
3175 static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,
3176 u64 *info1, u64 *info2,
3177 u32 *intr_info, u32 *error_code)
3179 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
3181 *reason = control->exit_code;
3182 *info1 = control->exit_info_1;
3183 *info2 = control->exit_info_2;
3184 *intr_info = control->exit_int_info;
3185 if ((*intr_info & SVM_EXITINTINFO_VALID) &&
3186 (*intr_info & SVM_EXITINTINFO_VALID_ERR))
3187 *error_code = control->exit_int_info_err;
3192 static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
3194 struct vcpu_svm *svm = to_svm(vcpu);
3195 struct kvm_run *kvm_run = vcpu->run;
3196 u32 exit_code = svm->vmcb->control.exit_code;
3198 trace_kvm_exit(vcpu, KVM_ISA_SVM);
3200 /* SEV-ES guests must use the CR write traps to track CR registers. */
3201 if (!sev_es_guest(vcpu->kvm)) {
3202 if (!svm_is_intercept(svm, INTERCEPT_CR0_WRITE))
3203 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3205 vcpu->arch.cr3 = svm->vmcb->save.cr3;
3208 if (is_guest_mode(vcpu)) {
3211 trace_kvm_nested_vmexit(vcpu, KVM_ISA_SVM);
3213 vmexit = nested_svm_exit_special(svm);
3215 if (vmexit == NESTED_EXIT_CONTINUE)
3216 vmexit = nested_svm_exit_handled(svm);
3218 if (vmexit == NESTED_EXIT_DONE)
3222 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3223 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3224 kvm_run->fail_entry.hardware_entry_failure_reason
3225 = svm->vmcb->control.exit_code;
3226 kvm_run->fail_entry.cpu = vcpu->arch.last_vmentry_cpu;
3231 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
3232 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
3233 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3234 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
3235 printk(KERN_ERR "%s: unexpected exit_int_info 0x%x "
3237 __func__, svm->vmcb->control.exit_int_info,
3240 if (exit_fastpath != EXIT_FASTPATH_NONE)
3243 return svm_invoke_exit_handler(vcpu, exit_code);
3246 static void reload_tss(struct kvm_vcpu *vcpu)
3248 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3250 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
3254 static void pre_svm_run(struct kvm_vcpu *vcpu)
3256 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3257 struct vcpu_svm *svm = to_svm(vcpu);
3260 * If the previous vmrun of the vmcb occurred on a different physical
3261 * cpu, then mark the vmcb dirty and assign a new asid. Hardware's
3262 * vmcb clean bits are per logical CPU, as are KVM's asid assignments.
3264 if (unlikely(svm->current_vmcb->cpu != vcpu->cpu)) {
3265 svm->current_vmcb->asid_generation = 0;
3266 vmcb_mark_all_dirty(svm->vmcb);
3267 svm->current_vmcb->cpu = vcpu->cpu;
3270 if (sev_guest(vcpu->kvm))
3271 return pre_sev_run(svm, vcpu->cpu);
3273 /* FIXME: handle wraparound of asid_generation */
3274 if (svm->current_vmcb->asid_generation != sd->asid_generation)
3278 static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3280 struct vcpu_svm *svm = to_svm(vcpu);
3282 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3283 vcpu->arch.hflags |= HF_NMI_MASK;
3284 if (!sev_es_guest(vcpu->kvm))
3285 svm_set_intercept(svm, INTERCEPT_IRET);
3286 ++vcpu->stat.nmi_injections;
3289 static void svm_set_irq(struct kvm_vcpu *vcpu)
3291 struct vcpu_svm *svm = to_svm(vcpu);
3293 BUG_ON(!(gif_set(svm)));
3295 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3296 ++vcpu->stat.irq_injections;
3298 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3299 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
3302 static void svm_deliver_interrupt(struct kvm_lapic *apic, int delivery_mode,
3303 int trig_mode, int vector)
3305 struct kvm_vcpu *vcpu = apic->vcpu;
3307 if (svm_deliver_avic_intr(vcpu, vector)) {
3308 kvm_lapic_set_irr(vector, apic);
3309 kvm_make_request(KVM_REQ_EVENT, vcpu);
3310 kvm_vcpu_kick(vcpu);
3312 trace_kvm_apicv_accept_irq(vcpu->vcpu_id, delivery_mode,
3317 static void svm_update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3319 struct vcpu_svm *svm = to_svm(vcpu);
3322 * SEV-ES guests must always keep the CR intercepts cleared. CR
3323 * tracking is done using the CR write traps.
3325 if (sev_es_guest(vcpu->kvm))
3328 if (nested_svm_virtualize_tpr(vcpu))
3331 svm_clr_intercept(svm, INTERCEPT_CR8_WRITE);
3337 svm_set_intercept(svm, INTERCEPT_CR8_WRITE);
3340 bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
3342 struct vcpu_svm *svm = to_svm(vcpu);
3343 struct vmcb *vmcb = svm->vmcb;
3349 if (is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3352 ret = (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) ||
3353 (vcpu->arch.hflags & HF_NMI_MASK);
3358 static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3360 struct vcpu_svm *svm = to_svm(vcpu);
3361 if (svm->nested.nested_run_pending)
3364 /* An NMI must not be injected into L2 if it's supposed to VM-Exit. */
3365 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
3368 return !svm_nmi_blocked(vcpu);
3371 static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3373 return !!(vcpu->arch.hflags & HF_NMI_MASK);
3376 static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3378 struct vcpu_svm *svm = to_svm(vcpu);
3381 vcpu->arch.hflags |= HF_NMI_MASK;
3382 if (!sev_es_guest(vcpu->kvm))
3383 svm_set_intercept(svm, INTERCEPT_IRET);
3385 vcpu->arch.hflags &= ~HF_NMI_MASK;
3386 if (!sev_es_guest(vcpu->kvm))
3387 svm_clr_intercept(svm, INTERCEPT_IRET);
3391 bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
3393 struct vcpu_svm *svm = to_svm(vcpu);
3394 struct vmcb *vmcb = svm->vmcb;
3399 if (is_guest_mode(vcpu)) {
3400 /* As long as interrupts are being delivered... */
3401 if ((svm->nested.ctl.int_ctl & V_INTR_MASKING_MASK)
3402 ? !(svm->vmcb01.ptr->save.rflags & X86_EFLAGS_IF)
3403 : !(kvm_get_rflags(vcpu) & X86_EFLAGS_IF))
3406 /* ... vmexits aren't blocked by the interrupt shadow */
3407 if (nested_exit_on_intr(svm))
3410 if (!svm_get_if_flag(vcpu))
3414 return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
3417 static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
3419 struct vcpu_svm *svm = to_svm(vcpu);
3420 if (svm->nested.nested_run_pending)
3424 * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
3425 * e.g. if the IRQ arrived asynchronously after checking nested events.
3427 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
3430 return !svm_interrupt_blocked(vcpu);
3433 static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
3435 struct vcpu_svm *svm = to_svm(vcpu);
3438 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3439 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3440 * get that intercept, this function will be called again though and
3441 * we'll get the vintr intercept. However, if the vGIF feature is
3442 * enabled, the STGI interception will not occur. Enable the irq
3443 * window under the assumption that the hardware will set the GIF.
3445 if (vgif_enabled(svm) || gif_set(svm)) {
3447 * IRQ window is not needed when AVIC is enabled,
3448 * unless we have pending ExtINT since it cannot be injected
3449 * via AVIC. In such case, we need to temporarily disable AVIC,
3450 * and fallback to injecting IRQ via V_IRQ.
3452 kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN);
3457 static void svm_enable_nmi_window(struct kvm_vcpu *vcpu)
3459 struct vcpu_svm *svm = to_svm(vcpu);
3461 if ((vcpu->arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) == HF_NMI_MASK)
3462 return; /* IRET will cause a vm exit */
3464 if (!gif_set(svm)) {
3465 if (vgif_enabled(svm))
3466 svm_set_intercept(svm, INTERCEPT_STGI);
3467 return; /* STGI will cause a vm exit */
3471 * Something prevents NMI from been injected. Single step over possible
3472 * problem (IRET or exception injection or interrupt shadow)
3474 svm->nmi_singlestep_guest_rflags = svm_get_rflags(vcpu);
3475 svm->nmi_singlestep = true;
3476 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3479 static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3484 static int svm_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
3489 void svm_flush_tlb(struct kvm_vcpu *vcpu)
3491 struct vcpu_svm *svm = to_svm(vcpu);
3494 * Flush only the current ASID even if the TLB flush was invoked via
3495 * kvm_flush_remote_tlbs(). Although flushing remote TLBs requires all
3496 * ASIDs to be flushed, KVM uses a single ASID for L1 and L2, and
3497 * unconditionally does a TLB flush on both nested VM-Enter and nested
3498 * VM-Exit (via kvm_mmu_reset_context()).
3500 if (static_cpu_has(X86_FEATURE_FLUSHBYASID))
3501 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID;
3503 svm->current_vmcb->asid_generation--;
3506 static void svm_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t gva)
3508 struct vcpu_svm *svm = to_svm(vcpu);
3510 invlpga(gva, svm->vmcb->control.asid);
3513 static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3515 struct vcpu_svm *svm = to_svm(vcpu);
3517 if (nested_svm_virtualize_tpr(vcpu))
3520 if (!svm_is_intercept(svm, INTERCEPT_CR8_WRITE)) {
3521 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
3522 kvm_set_cr8(vcpu, cr8);
3526 static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3528 struct vcpu_svm *svm = to_svm(vcpu);
3531 if (nested_svm_virtualize_tpr(vcpu) ||
3532 kvm_vcpu_apicv_active(vcpu))
3535 cr8 = kvm_get_cr8(vcpu);
3536 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3537 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3540 static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
3542 struct vcpu_svm *svm = to_svm(vcpu);
3545 u32 exitintinfo = svm->vmcb->control.exit_int_info;
3546 unsigned int3_injected = svm->int3_injected;
3548 svm->int3_injected = 0;
3551 * If we've made progress since setting HF_IRET_MASK, we've
3552 * executed an IRET and can allow NMI injection.
3554 if ((vcpu->arch.hflags & HF_IRET_MASK) &&
3555 (sev_es_guest(vcpu->kvm) ||
3556 kvm_rip_read(vcpu) != svm->nmi_iret_rip)) {
3557 vcpu->arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3558 kvm_make_request(KVM_REQ_EVENT, vcpu);
3561 vcpu->arch.nmi_injected = false;
3562 kvm_clear_exception_queue(vcpu);
3563 kvm_clear_interrupt_queue(vcpu);
3565 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3568 kvm_make_request(KVM_REQ_EVENT, vcpu);
3570 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3571 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3574 case SVM_EXITINTINFO_TYPE_NMI:
3575 vcpu->arch.nmi_injected = true;
3577 case SVM_EXITINTINFO_TYPE_EXEPT:
3579 * Never re-inject a #VC exception.
3581 if (vector == X86_TRAP_VC)
3585 * In case of software exceptions, do not reinject the vector,
3586 * but re-execute the instruction instead. Rewind RIP first
3587 * if we emulated INT3 before.
3589 if (kvm_exception_is_soft(vector)) {
3590 if (vector == BP_VECTOR && int3_injected &&
3591 kvm_is_linear_rip(vcpu, svm->int3_rip))
3593 kvm_rip_read(vcpu) - int3_injected);
3596 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3597 u32 err = svm->vmcb->control.exit_int_info_err;
3598 kvm_requeue_exception_e(vcpu, vector, err);
3601 kvm_requeue_exception(vcpu, vector);
3603 case SVM_EXITINTINFO_TYPE_INTR:
3604 kvm_queue_interrupt(vcpu, vector, false);
3611 static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3613 struct vcpu_svm *svm = to_svm(vcpu);
3614 struct vmcb_control_area *control = &svm->vmcb->control;
3616 control->exit_int_info = control->event_inj;
3617 control->exit_int_info_err = control->event_inj_err;
3618 control->event_inj = 0;
3619 svm_complete_interrupts(vcpu);
3622 static int svm_vcpu_pre_run(struct kvm_vcpu *vcpu)
3627 static fastpath_t svm_exit_handlers_fastpath(struct kvm_vcpu *vcpu)
3629 if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_MSR &&
3630 to_svm(vcpu)->vmcb->control.exit_info_1)
3631 return handle_fastpath_set_msr_irqoff(vcpu);
3633 return EXIT_FASTPATH_NONE;
3636 static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
3638 struct vcpu_svm *svm = to_svm(vcpu);
3639 unsigned long vmcb_pa = svm->current_vmcb->pa;
3641 guest_state_enter_irqoff();
3643 if (sev_es_guest(vcpu->kvm)) {
3644 __svm_sev_es_vcpu_run(vmcb_pa);
3646 struct svm_cpu_data *sd = per_cpu(svm_data, vcpu->cpu);
3649 * Use a single vmcb (vmcb01 because it's always valid) for
3650 * context switching guest state via VMLOAD/VMSAVE, that way
3651 * the state doesn't need to be copied between vmcb01 and
3652 * vmcb02 when switching vmcbs for nested virtualization.
3654 vmload(svm->vmcb01.pa);
3655 __svm_vcpu_run(vmcb_pa, (unsigned long *)&vcpu->arch.regs);
3656 vmsave(svm->vmcb01.pa);
3658 vmload(__sme_page_pa(sd->save_area));
3661 guest_state_exit_irqoff();
3664 static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
3666 struct vcpu_svm *svm = to_svm(vcpu);
3668 trace_kvm_entry(vcpu);
3670 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3671 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3672 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3675 * Disable singlestep if we're injecting an interrupt/exception.
3676 * We don't want our modified rflags to be pushed on the stack where
3677 * we might not be able to easily reset them if we disabled NMI
3680 if (svm->nmi_singlestep && svm->vmcb->control.event_inj) {
3682 * Event injection happens before external interrupts cause a
3683 * vmexit and interrupts are disabled here, so smp_send_reschedule
3684 * is enough to force an immediate vmexit.
3686 disable_nmi_singlestep(svm);
3687 smp_send_reschedule(vcpu->cpu);
3692 sync_lapic_to_cr8(vcpu);
3694 if (unlikely(svm->asid != svm->vmcb->control.asid)) {
3695 svm->vmcb->control.asid = svm->asid;
3696 vmcb_mark_dirty(svm->vmcb, VMCB_ASID);
3698 svm->vmcb->save.cr2 = vcpu->arch.cr2;
3700 svm_hv_update_vp_id(svm->vmcb, vcpu);
3703 * Run with all-zero DR6 unless needed, so that we can get the exact cause
3706 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT))
3707 svm_set_dr6(svm, vcpu->arch.dr6);
3709 svm_set_dr6(svm, DR6_ACTIVE_LOW);
3712 kvm_load_guest_xsave_state(vcpu);
3714 kvm_wait_lapic_expire(vcpu);
3717 * If this vCPU has touched SPEC_CTRL, restore the guest's value if
3718 * it's non-zero. Since vmentry is serialising on affected CPUs, there
3719 * is no need to worry about the conditional branch over the wrmsr
3720 * being speculatively taken.
3722 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3723 x86_spec_ctrl_set_guest(svm->spec_ctrl, svm->virt_spec_ctrl);
3725 svm_vcpu_enter_exit(vcpu);
3728 * We do not use IBRS in the kernel. If this vCPU has used the
3729 * SPEC_CTRL MSR it may have left it on; save the value and
3730 * turn it off. This is much more efficient than blindly adding
3731 * it to the atomic save/restore list. Especially as the former
3732 * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
3734 * For non-nested case:
3735 * If the L01 MSR bitmap does not intercept the MSR, then we need to
3739 * If the L02 MSR bitmap does not intercept the MSR, then we need to
3742 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL) &&
3743 unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
3744 svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
3746 if (!sev_es_guest(vcpu->kvm))
3749 if (!static_cpu_has(X86_FEATURE_V_SPEC_CTRL))
3750 x86_spec_ctrl_restore_host(svm->spec_ctrl, svm->virt_spec_ctrl);
3752 if (!sev_es_guest(vcpu->kvm)) {
3753 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3754 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3755 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3756 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3758 vcpu->arch.regs_dirty = 0;
3760 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3761 kvm_before_interrupt(vcpu, KVM_HANDLING_NMI);
3763 kvm_load_host_xsave_state(vcpu);
3766 /* Any pending NMI will happen here */
3768 if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI))
3769 kvm_after_interrupt(vcpu);
3771 sync_cr8_to_lapic(vcpu);
3774 if (is_guest_mode(vcpu)) {
3775 nested_sync_control_from_vmcb02(svm);
3777 /* Track VMRUNs that have made past consistency checking */
3778 if (svm->nested.nested_run_pending &&
3779 svm->vmcb->control.exit_code != SVM_EXIT_ERR)
3780 ++vcpu->stat.nested_run;
3782 svm->nested.nested_run_pending = 0;
3785 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
3786 vmcb_mark_all_clean(svm->vmcb);
3788 /* if exit due to PF check for async PF */
3789 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3790 vcpu->arch.apf.host_apf_flags =
3791 kvm_read_and_reset_apf_flags();
3793 vcpu->arch.regs_avail &= ~SVM_REGS_LAZY_LOAD_SET;
3796 * We need to handle MC intercepts here before the vcpu has a chance to
3797 * change the physical cpu
3799 if (unlikely(svm->vmcb->control.exit_code ==
3800 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3801 svm_handle_mce(vcpu);
3803 svm_complete_interrupts(vcpu);
3805 if (is_guest_mode(vcpu))
3806 return EXIT_FASTPATH_NONE;
3808 return svm_exit_handlers_fastpath(vcpu);
3811 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, hpa_t root_hpa,
3814 struct vcpu_svm *svm = to_svm(vcpu);
3818 svm->vmcb->control.nested_cr3 = __sme_set(root_hpa);
3819 vmcb_mark_dirty(svm->vmcb, VMCB_NPT);
3821 hv_track_root_tdp(vcpu, root_hpa);
3823 cr3 = vcpu->arch.cr3;
3824 } else if (vcpu->arch.mmu->shadow_root_level >= PT64_ROOT_4LEVEL) {
3825 cr3 = __sme_set(root_hpa) | kvm_get_active_pcid(vcpu);
3827 /* PCID in the guest should be impossible with a 32-bit MMU. */
3828 WARN_ON_ONCE(kvm_get_active_pcid(vcpu));
3832 svm->vmcb->save.cr3 = cr3;
3833 vmcb_mark_dirty(svm->vmcb, VMCB_CR);
3836 static int is_disabled(void)
3840 rdmsrl(MSR_VM_CR, vm_cr);
3841 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3848 svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3851 * Patch in the VMMCALL instruction:
3853 hypercall[0] = 0x0f;
3854 hypercall[1] = 0x01;
3855 hypercall[2] = 0xd9;
3858 static int __init svm_check_processor_compat(void)
3863 static bool svm_cpu_has_accelerated_tpr(void)
3869 * The kvm parameter can be NULL (module initialization, or invocation before
3870 * VM creation). Be sure to check the kvm parameter before using it.
3872 static bool svm_has_emulated_msr(struct kvm *kvm, u32 index)
3875 case MSR_IA32_MCG_EXT_CTL:
3876 case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
3878 case MSR_IA32_SMBASE:
3879 /* SEV-ES guests do not support SMM, so report false */
3880 if (kvm && sev_es_guest(kvm))
3890 static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
3895 static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
3897 struct vcpu_svm *svm = to_svm(vcpu);
3898 struct kvm_cpuid_entry2 *best;
3900 vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
3901 boot_cpu_has(X86_FEATURE_XSAVE) &&
3902 boot_cpu_has(X86_FEATURE_XSAVES);
3904 /* Update nrips enabled cache */
3905 svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
3906 guest_cpuid_has(vcpu, X86_FEATURE_NRIPS);
3908 svm->tsc_scaling_enabled = tsc_scaling && guest_cpuid_has(vcpu, X86_FEATURE_TSCRATEMSR);
3910 svm_recalc_instruction_intercepts(vcpu, svm);
3912 /* For sev guests, the memory encryption bit is not reserved in CR3. */
3913 if (sev_guest(vcpu->kvm)) {
3914 best = kvm_find_cpuid_entry(vcpu, 0x8000001F, 0);
3916 vcpu->arch.reserved_gpa_bits &= ~(1UL << (best->ebx & 0x3f));
3919 if (kvm_vcpu_apicv_active(vcpu)) {
3921 * AVIC does not work with an x2APIC mode guest. If the X2APIC feature
3922 * is exposed to the guest, disable AVIC.
3924 if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3925 kvm_request_apicv_update(vcpu->kvm, false,
3926 APICV_INHIBIT_REASON_X2APIC);
3929 * Currently, AVIC does not work with nested virtualization.
3930 * So, we disable AVIC when cpuid for SVM is set in the L1 guest.
3932 if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3933 kvm_request_apicv_update(vcpu->kvm, false,
3934 APICV_INHIBIT_REASON_NESTED);
3936 init_vmcb_after_set_cpuid(vcpu);
3939 static bool svm_has_wbinvd_exit(void)
3944 #define PRE_EX(exit) { .exit_code = (exit), \
3945 .stage = X86_ICPT_PRE_EXCEPT, }
3946 #define POST_EX(exit) { .exit_code = (exit), \
3947 .stage = X86_ICPT_POST_EXCEPT, }
3948 #define POST_MEM(exit) { .exit_code = (exit), \
3949 .stage = X86_ICPT_POST_MEMACCESS, }
3951 static const struct __x86_intercept {
3953 enum x86_intercept_stage stage;
3954 } x86_intercept_map[] = {
3955 [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0),
3956 [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0),
3957 [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0),
3958 [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0),
3959 [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0),
3960 [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0),
3961 [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0),
3962 [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ),
3963 [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ),
3964 [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE),
3965 [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE),
3966 [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ),
3967 [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ),
3968 [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE),
3969 [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE),
3970 [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN),
3971 [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL),
3972 [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD),
3973 [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE),
3974 [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI),
3975 [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI),
3976 [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT),
3977 [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA),
3978 [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP),
3979 [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR),
3980 [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT),
3981 [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG),
3982 [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD),
3983 [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD),
3984 [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR),
3985 [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC),
3986 [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR),
3987 [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC),
3988 [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID),
3989 [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM),
3990 [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE),
3991 [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF),
3992 [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF),
3993 [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT),
3994 [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET),
3995 [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP),
3996 [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT),
3997 [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO),
3998 [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO),
3999 [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO),
4000 [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO),
4001 [x86_intercept_xsetbv] = PRE_EX(SVM_EXIT_XSETBV),
4008 static int svm_check_intercept(struct kvm_vcpu *vcpu,
4009 struct x86_instruction_info *info,
4010 enum x86_intercept_stage stage,
4011 struct x86_exception *exception)
4013 struct vcpu_svm *svm = to_svm(vcpu);
4014 int vmexit, ret = X86EMUL_CONTINUE;
4015 struct __x86_intercept icpt_info;
4016 struct vmcb *vmcb = svm->vmcb;
4018 if (info->intercept >= ARRAY_SIZE(x86_intercept_map))
4021 icpt_info = x86_intercept_map[info->intercept];
4023 if (stage != icpt_info.stage)
4026 switch (icpt_info.exit_code) {
4027 case SVM_EXIT_READ_CR0:
4028 if (info->intercept == x86_intercept_cr_read)
4029 icpt_info.exit_code += info->modrm_reg;
4031 case SVM_EXIT_WRITE_CR0: {
4032 unsigned long cr0, val;
4034 if (info->intercept == x86_intercept_cr_write)
4035 icpt_info.exit_code += info->modrm_reg;
4037 if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0 ||
4038 info->intercept == x86_intercept_clts)
4041 if (!(vmcb12_is_intercept(&svm->nested.ctl,
4042 INTERCEPT_SELECTIVE_CR0)))
4045 cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK;
4046 val = info->src_val & ~SVM_CR0_SELECTIVE_MASK;
4048 if (info->intercept == x86_intercept_lmsw) {
4051 /* lmsw can't clear PE - catch this here */
4052 if (cr0 & X86_CR0_PE)
4057 icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE;
4061 case SVM_EXIT_READ_DR0:
4062 case SVM_EXIT_WRITE_DR0:
4063 icpt_info.exit_code += info->modrm_reg;
4066 if (info->intercept == x86_intercept_wrmsr)
4067 vmcb->control.exit_info_1 = 1;
4069 vmcb->control.exit_info_1 = 0;
4071 case SVM_EXIT_PAUSE:
4073 * We get this for NOP only, but pause
4074 * is rep not, check this here
4076 if (info->rep_prefix != REPE_PREFIX)
4079 case SVM_EXIT_IOIO: {
4083 if (info->intercept == x86_intercept_in ||
4084 info->intercept == x86_intercept_ins) {
4085 exit_info = ((info->src_val & 0xffff) << 16) |
4087 bytes = info->dst_bytes;
4089 exit_info = (info->dst_val & 0xffff) << 16;
4090 bytes = info->src_bytes;
4093 if (info->intercept == x86_intercept_outs ||
4094 info->intercept == x86_intercept_ins)
4095 exit_info |= SVM_IOIO_STR_MASK;
4097 if (info->rep_prefix)
4098 exit_info |= SVM_IOIO_REP_MASK;
4100 bytes = min(bytes, 4u);
4102 exit_info |= bytes << SVM_IOIO_SIZE_SHIFT;
4104 exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1);
4106 vmcb->control.exit_info_1 = exit_info;
4107 vmcb->control.exit_info_2 = info->next_rip;
4115 /* TODO: Advertise NRIPS to guest hypervisor unconditionally */
4116 if (static_cpu_has(X86_FEATURE_NRIPS))
4117 vmcb->control.next_rip = info->next_rip;
4118 vmcb->control.exit_code = icpt_info.exit_code;
4119 vmexit = nested_svm_exit_handled(svm);
4121 ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED
4128 static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu)
4132 static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu)
4134 if (!kvm_pause_in_guest(vcpu->kvm))
4135 shrink_ple_window(vcpu);
4138 static void svm_setup_mce(struct kvm_vcpu *vcpu)
4140 /* [63:9] are reserved. */
4141 vcpu->arch.mcg_cap &= 0x1ff;
4144 bool svm_smi_blocked(struct kvm_vcpu *vcpu)
4146 struct vcpu_svm *svm = to_svm(vcpu);
4148 /* Per APM Vol.2 15.22.2 "Response to SMI" */
4152 return is_smm(vcpu);
4155 static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
4157 struct vcpu_svm *svm = to_svm(vcpu);
4158 if (svm->nested.nested_run_pending)
4161 /* An SMI must not be injected into L2 if it's supposed to VM-Exit. */
4162 if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
4165 return !svm_smi_blocked(vcpu);
4168 static int svm_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
4170 struct vcpu_svm *svm = to_svm(vcpu);
4171 struct kvm_host_map map_save;
4174 if (!is_guest_mode(vcpu))
4177 /* FED8h - SVM Guest */
4178 put_smstate(u64, smstate, 0x7ed8, 1);
4179 /* FEE0h - SVM Guest VMCB Physical Address */
4180 put_smstate(u64, smstate, 0x7ee0, svm->nested.vmcb12_gpa);
4182 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
4183 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
4184 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
4186 ret = nested_svm_vmexit(svm);
4191 * KVM uses VMCB01 to store L1 host state while L2 runs but
4192 * VMCB01 is going to be used during SMM and thus the state will
4193 * be lost. Temporary save non-VMLOAD/VMSAVE state to the host save
4194 * area pointed to by MSR_VM_HSAVE_PA. APM guarantees that the
4195 * format of the area is identical to guest save area offsetted
4196 * by 0x400 (matches the offset of 'struct vmcb_save_area'
4197 * within 'struct vmcb'). Note: HSAVE area may also be used by
4198 * L1 hypervisor to save additional host context (e.g. KVM does
4199 * that, see svm_prepare_guest_switch()) which must be
4202 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr),
4203 &map_save) == -EINVAL)
4206 BUILD_BUG_ON(offsetof(struct vmcb, save) != 0x400);
4208 svm_copy_vmrun_state(map_save.hva + 0x400,
4209 &svm->vmcb01.ptr->save);
4211 kvm_vcpu_unmap(vcpu, &map_save, true);
4215 static int svm_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
4217 struct vcpu_svm *svm = to_svm(vcpu);
4218 struct kvm_host_map map, map_save;
4219 u64 saved_efer, vmcb12_gpa;
4220 struct vmcb *vmcb12;
4223 if (!guest_cpuid_has(vcpu, X86_FEATURE_LM))
4226 /* Non-zero if SMI arrived while vCPU was in guest mode. */
4227 if (!GET_SMSTATE(u64, smstate, 0x7ed8))
4230 if (!guest_cpuid_has(vcpu, X86_FEATURE_SVM))
4233 saved_efer = GET_SMSTATE(u64, smstate, 0x7ed0);
4234 if (!(saved_efer & EFER_SVME))
4237 vmcb12_gpa = GET_SMSTATE(u64, smstate, 0x7ee0);
4238 if (kvm_vcpu_map(vcpu, gpa_to_gfn(vmcb12_gpa), &map) == -EINVAL)
4242 if (kvm_vcpu_map(vcpu, gpa_to_gfn(svm->nested.hsave_msr), &map_save) == -EINVAL)
4245 if (svm_allocate_nested(svm))
4249 * Restore L1 host state from L1 HSAVE area as VMCB01 was
4250 * used during SMM (see svm_enter_smm())
4253 svm_copy_vmrun_state(&svm->vmcb01.ptr->save, map_save.hva + 0x400);
4256 * Enter the nested guest now
4259 vmcb_mark_all_dirty(svm->vmcb01.ptr);
4262 nested_copy_vmcb_control_to_cache(svm, &vmcb12->control);
4263 nested_copy_vmcb_save_to_cache(svm, &vmcb12->save);
4264 ret = enter_svm_guest_mode(vcpu, vmcb12_gpa, vmcb12, false);
4269 svm->nested.nested_run_pending = 1;
4272 kvm_vcpu_unmap(vcpu, &map_save, true);
4274 kvm_vcpu_unmap(vcpu, &map, true);
4278 static void svm_enable_smi_window(struct kvm_vcpu *vcpu)
4280 struct vcpu_svm *svm = to_svm(vcpu);
4282 if (!gif_set(svm)) {
4283 if (vgif_enabled(svm))
4284 svm_set_intercept(svm, INTERCEPT_STGI);
4285 /* STGI will cause a vm exit */
4287 /* We must be in SMM; RSM will cause a vmexit anyway. */
4291 static bool svm_can_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
4292 void *insn, int insn_len)
4294 bool smep, smap, is_user;
4298 /* Emulation is always possible when KVM has access to all guest state. */
4299 if (!sev_guest(vcpu->kvm))
4302 /* #UD and #GP should never be intercepted for SEV guests. */
4303 WARN_ON_ONCE(emul_type & (EMULTYPE_TRAP_UD |
4304 EMULTYPE_TRAP_UD_FORCED |
4305 EMULTYPE_VMWARE_GP));
4308 * Emulation is impossible for SEV-ES guests as KVM doesn't have access
4309 * to guest register state.
4311 if (sev_es_guest(vcpu->kvm))
4315 * Emulation is possible if the instruction is already decoded, e.g.
4316 * when completing I/O after returning from userspace.
4318 if (emul_type & EMULTYPE_NO_DECODE)
4322 * Emulation is possible for SEV guests if and only if a prefilled
4323 * buffer containing the bytes of the intercepted instruction is
4324 * available. SEV guest memory is encrypted with a guest specific key
4325 * and cannot be decrypted by KVM, i.e. KVM would read cyphertext and
4328 * Inject #UD if KVM reached this point without an instruction buffer.
4329 * In practice, this path should never be hit by a well-behaved guest,
4330 * e.g. KVM doesn't intercept #UD or #GP for SEV guests, but this path
4331 * is still theoretically reachable, e.g. via unaccelerated fault-like
4332 * AVIC access, and needs to be handled by KVM to avoid putting the
4333 * guest into an infinite loop. Injecting #UD is somewhat arbitrary,
4334 * but its the least awful option given lack of insight into the guest.
4336 if (unlikely(!insn)) {
4337 kvm_queue_exception(vcpu, UD_VECTOR);
4342 * Emulate for SEV guests if the insn buffer is not empty. The buffer
4343 * will be empty if the DecodeAssist microcode cannot fetch bytes for
4344 * the faulting instruction because the code fetch itself faulted, e.g.
4345 * the guest attempted to fetch from emulated MMIO or a guest page
4346 * table used to translate CS:RIP resides in emulated MMIO.
4348 if (likely(insn_len))
4352 * Detect and workaround Errata 1096 Fam_17h_00_0Fh.
4355 * When CPU raises #NPF on guest data access and vCPU CR4.SMAP=1, it is
4356 * possible that CPU microcode implementing DecodeAssist will fail to
4357 * read guest memory at CS:RIP and vmcb.GuestIntrBytes will incorrectly
4358 * be '0'. This happens because microcode reads CS:RIP using a _data_
4359 * loap uop with CPL=0 privileges. If the load hits a SMAP #PF, ucode
4360 * gives up and does not fill the instruction bytes buffer.
4362 * As above, KVM reaches this point iff the VM is an SEV guest, the CPU
4363 * supports DecodeAssist, a #NPF was raised, KVM's page fault handler
4364 * triggered emulation (e.g. for MMIO), and the CPU returned 0 in the
4365 * GuestIntrBytes field of the VMCB.
4367 * This does _not_ mean that the erratum has been encountered, as the
4368 * DecodeAssist will also fail if the load for CS:RIP hits a legitimate
4369 * #PF, e.g. if the guest attempt to execute from emulated MMIO and
4370 * encountered a reserved/not-present #PF.
4372 * To hit the erratum, the following conditions must be true:
4373 * 1. CR4.SMAP=1 (obviously).
4374 * 2. CR4.SMEP=0 || CPL=3. If SMEP=1 and CPL<3, the erratum cannot
4375 * have been hit as the guest would have encountered a SMEP
4376 * violation #PF, not a #NPF.
4377 * 3. The #NPF is not due to a code fetch, in which case failure to
4378 * retrieve the instruction bytes is legitimate (see abvoe).
4380 * In addition, don't apply the erratum workaround if the #NPF occurred
4381 * while translating guest page tables (see below).
4383 error_code = to_svm(vcpu)->vmcb->control.exit_info_1;
4384 if (error_code & (PFERR_GUEST_PAGE_MASK | PFERR_FETCH_MASK))
4387 cr4 = kvm_read_cr4(vcpu);
4388 smep = cr4 & X86_CR4_SMEP;
4389 smap = cr4 & X86_CR4_SMAP;
4390 is_user = svm_get_cpl(vcpu) == 3;
4391 if (smap && (!smep || is_user)) {
4392 pr_err_ratelimited("KVM: SEV Guest triggered AMD Erratum 1096\n");
4395 * If the fault occurred in userspace, arbitrarily inject #GP
4396 * to avoid killing the guest and to hopefully avoid confusing
4397 * the guest kernel too much, e.g. injecting #PF would not be
4398 * coherent with respect to the guest's page tables. Request
4399 * triple fault if the fault occurred in the kernel as there's
4400 * no fault that KVM can inject without confusing the guest.
4401 * In practice, the triple fault is moot as no sane SEV kernel
4402 * will execute from user memory while also running with SMAP=1.
4405 kvm_inject_gp(vcpu, 0);
4407 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
4412 * If the erratum was not hit, simply resume the guest and let it fault
4413 * again. While awful, e.g. the vCPU may get stuck in an infinite loop
4414 * if the fault is at CPL=0, it's the lesser of all evils. Exiting to
4415 * userspace will kill the guest, and letting the emulator read garbage
4416 * will yield random behavior and potentially corrupt the guest.
4418 * Simply resuming the guest is technically not a violation of the SEV
4419 * architecture. AMD's APM states that all code fetches and page table
4420 * accesses for SEV guest are encrypted, regardless of the C-Bit. The
4421 * APM also states that encrypted accesses to MMIO are "ignored", but
4422 * doesn't explicitly define "ignored", i.e. doing nothing and letting
4423 * the guest spin is technically "ignoring" the access.
4428 static bool svm_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
4430 struct vcpu_svm *svm = to_svm(vcpu);
4433 * TODO: Last condition latch INIT signals on vCPU when
4434 * vCPU is in guest-mode and vmcb12 defines intercept on INIT.
4435 * To properly emulate the INIT intercept,
4436 * svm_check_nested_events() should call nested_svm_vmexit()
4437 * if an INIT signal is pending.
4439 return !gif_set(svm) ||
4440 (vmcb_is_intercept(&svm->vmcb->control, INTERCEPT_INIT));
4443 static void svm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
4445 if (!sev_es_guest(vcpu->kvm))
4446 return kvm_vcpu_deliver_sipi_vector(vcpu, vector);
4448 sev_vcpu_deliver_sipi_vector(vcpu, vector);
4451 static void svm_vm_destroy(struct kvm *kvm)
4453 avic_vm_destroy(kvm);
4454 sev_vm_destroy(kvm);
4457 static int svm_vm_init(struct kvm *kvm)
4459 if (!pause_filter_count || !pause_filter_thresh)
4460 kvm->arch.pause_in_guest = true;
4463 int ret = avic_vm_init(kvm);
4471 static struct kvm_x86_ops svm_x86_ops __initdata = {
4474 .hardware_unsetup = svm_hardware_teardown,
4475 .hardware_enable = svm_hardware_enable,
4476 .hardware_disable = svm_hardware_disable,
4477 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
4478 .has_emulated_msr = svm_has_emulated_msr,
4480 .vcpu_create = svm_create_vcpu,
4481 .vcpu_free = svm_free_vcpu,
4482 .vcpu_reset = svm_vcpu_reset,
4484 .vm_size = sizeof(struct kvm_svm),
4485 .vm_init = svm_vm_init,
4486 .vm_destroy = svm_vm_destroy,
4488 .prepare_guest_switch = svm_prepare_guest_switch,
4489 .vcpu_load = svm_vcpu_load,
4490 .vcpu_put = svm_vcpu_put,
4491 .vcpu_blocking = avic_vcpu_blocking,
4492 .vcpu_unblocking = avic_vcpu_unblocking,
4494 .update_exception_bitmap = svm_update_exception_bitmap,
4495 .get_msr_feature = svm_get_msr_feature,
4496 .get_msr = svm_get_msr,
4497 .set_msr = svm_set_msr,
4498 .get_segment_base = svm_get_segment_base,
4499 .get_segment = svm_get_segment,
4500 .set_segment = svm_set_segment,
4501 .get_cpl = svm_get_cpl,
4502 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
4503 .set_cr0 = svm_set_cr0,
4504 .post_set_cr3 = svm_post_set_cr3,
4505 .is_valid_cr4 = svm_is_valid_cr4,
4506 .set_cr4 = svm_set_cr4,
4507 .set_efer = svm_set_efer,
4508 .get_idt = svm_get_idt,
4509 .set_idt = svm_set_idt,
4510 .get_gdt = svm_get_gdt,
4511 .set_gdt = svm_set_gdt,
4512 .set_dr7 = svm_set_dr7,
4513 .sync_dirty_debug_regs = svm_sync_dirty_debug_regs,
4514 .cache_reg = svm_cache_reg,
4515 .get_rflags = svm_get_rflags,
4516 .set_rflags = svm_set_rflags,
4517 .get_if_flag = svm_get_if_flag,
4519 .tlb_flush_all = svm_flush_tlb,
4520 .tlb_flush_current = svm_flush_tlb,
4521 .tlb_flush_gva = svm_flush_tlb_gva,
4522 .tlb_flush_guest = svm_flush_tlb,
4524 .vcpu_pre_run = svm_vcpu_pre_run,
4525 .run = svm_vcpu_run,
4526 .handle_exit = handle_exit,
4527 .skip_emulated_instruction = skip_emulated_instruction,
4528 .update_emulated_instruction = NULL,
4529 .set_interrupt_shadow = svm_set_interrupt_shadow,
4530 .get_interrupt_shadow = svm_get_interrupt_shadow,
4531 .patch_hypercall = svm_patch_hypercall,
4532 .set_irq = svm_set_irq,
4533 .set_nmi = svm_inject_nmi,
4534 .queue_exception = svm_queue_exception,
4535 .cancel_injection = svm_cancel_injection,
4536 .interrupt_allowed = svm_interrupt_allowed,
4537 .nmi_allowed = svm_nmi_allowed,
4538 .get_nmi_mask = svm_get_nmi_mask,
4539 .set_nmi_mask = svm_set_nmi_mask,
4540 .enable_nmi_window = svm_enable_nmi_window,
4541 .enable_irq_window = svm_enable_irq_window,
4542 .update_cr8_intercept = svm_update_cr8_intercept,
4543 .set_virtual_apic_mode = svm_set_virtual_apic_mode,
4544 .refresh_apicv_exec_ctrl = svm_refresh_apicv_exec_ctrl,
4545 .check_apicv_inhibit_reasons = svm_check_apicv_inhibit_reasons,
4546 .load_eoi_exitmap = svm_load_eoi_exitmap,
4547 .hwapic_irr_update = svm_hwapic_irr_update,
4548 .hwapic_isr_update = svm_hwapic_isr_update,
4549 .apicv_post_state_restore = avic_post_state_restore,
4551 .set_tss_addr = svm_set_tss_addr,
4552 .set_identity_map_addr = svm_set_identity_map_addr,
4553 .get_mt_mask = svm_get_mt_mask,
4555 .get_exit_info = svm_get_exit_info,
4557 .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid,
4559 .has_wbinvd_exit = svm_has_wbinvd_exit,
4561 .get_l2_tsc_offset = svm_get_l2_tsc_offset,
4562 .get_l2_tsc_multiplier = svm_get_l2_tsc_multiplier,
4563 .write_tsc_offset = svm_write_tsc_offset,
4564 .write_tsc_multiplier = svm_write_tsc_multiplier,
4566 .load_mmu_pgd = svm_load_mmu_pgd,
4568 .check_intercept = svm_check_intercept,
4569 .handle_exit_irqoff = svm_handle_exit_irqoff,
4571 .request_immediate_exit = __kvm_request_immediate_exit,
4573 .sched_in = svm_sched_in,
4575 .pmu_ops = &amd_pmu_ops,
4576 .nested_ops = &svm_nested_ops,
4578 .deliver_interrupt = svm_deliver_interrupt,
4579 .dy_apicv_has_pending_interrupt = svm_dy_apicv_has_pending_interrupt,
4580 .update_pi_irte = svm_update_pi_irte,
4581 .setup_mce = svm_setup_mce,
4583 .smi_allowed = svm_smi_allowed,
4584 .enter_smm = svm_enter_smm,
4585 .leave_smm = svm_leave_smm,
4586 .enable_smi_window = svm_enable_smi_window,
4588 .mem_enc_op = svm_mem_enc_op,
4589 .mem_enc_reg_region = svm_register_enc_region,
4590 .mem_enc_unreg_region = svm_unregister_enc_region,
4592 .vm_copy_enc_context_from = svm_vm_copy_asid_from,
4593 .vm_move_enc_context_from = svm_vm_migrate_from,
4595 .can_emulate_instruction = svm_can_emulate_instruction,
4597 .apic_init_signal_blocked = svm_apic_init_signal_blocked,
4599 .msr_filter_changed = svm_msr_filter_changed,
4600 .complete_emulated_msr = svm_complete_emulated_msr,
4602 .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector,
4606 * The default MMIO mask is a single bit (excluding the present bit),
4607 * which could conflict with the memory encryption bit. Check for
4608 * memory encryption support and override the default MMIO mask if
4609 * memory encryption is enabled.
4611 static __init void svm_adjust_mmio_mask(void)
4613 unsigned int enc_bit, mask_bit;
4616 /* If there is no memory encryption support, use existing mask */
4617 if (cpuid_eax(0x80000000) < 0x8000001f)
4620 /* If memory encryption is not enabled, use existing mask */
4621 rdmsrl(MSR_AMD64_SYSCFG, msr);
4622 if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
4625 enc_bit = cpuid_ebx(0x8000001f) & 0x3f;
4626 mask_bit = boot_cpu_data.x86_phys_bits;
4628 /* Increment the mask bit if it is the same as the encryption bit */
4629 if (enc_bit == mask_bit)
4633 * If the mask bit location is below 52, then some bits above the
4634 * physical addressing limit will always be reserved, so use the
4635 * rsvd_bits() function to generate the mask. This mask, along with
4636 * the present bit, will be used to generate a page fault with
4639 * If the mask bit location is 52 (or above), then clear the mask.
4641 mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
4643 kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
4646 static __init void svm_set_cpu_caps(void)
4652 /* CPUID 0x80000001 and 0x8000000A (SVM features) */
4654 kvm_cpu_cap_set(X86_FEATURE_SVM);
4655 kvm_cpu_cap_set(X86_FEATURE_VMCBCLEAN);
4658 kvm_cpu_cap_set(X86_FEATURE_NRIPS);
4661 kvm_cpu_cap_set(X86_FEATURE_NPT);
4664 kvm_cpu_cap_set(X86_FEATURE_TSCRATEMSR);
4666 /* Nested VM can receive #VMEXIT instead of triggering #GP */
4667 kvm_cpu_cap_set(X86_FEATURE_SVME_ADDR_CHK);
4670 /* CPUID 0x80000008 */
4671 if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
4672 boot_cpu_has(X86_FEATURE_AMD_SSBD))
4673 kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
4675 /* AMD PMU PERFCTR_CORE CPUID */
4676 if (enable_pmu && boot_cpu_has(X86_FEATURE_PERFCTR_CORE))
4677 kvm_cpu_cap_set(X86_FEATURE_PERFCTR_CORE);
4679 /* CPUID 0x8000001F (SME/SEV features) */
4683 static __init int svm_hardware_setup(void)
4686 struct page *iopm_pages;
4689 unsigned int order = get_order(IOPM_SIZE);
4692 * NX is required for shadow paging and for NPT if the NX huge pages
4693 * mitigation is enabled.
4695 if (!boot_cpu_has(X86_FEATURE_NX)) {
4696 pr_err_ratelimited("NX (Execute Disable) not supported\n");
4699 kvm_enable_efer_bits(EFER_NX);
4701 iopm_pages = alloc_pages(GFP_KERNEL, order);
4706 iopm_va = page_address(iopm_pages);
4707 memset(iopm_va, 0xff, PAGE_SIZE * (1 << order));
4708 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
4710 init_msrpm_offsets();
4712 supported_xcr0 &= ~(XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR);
4714 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
4715 kvm_enable_efer_bits(EFER_FFXSR);
4718 if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) {
4719 tsc_scaling = false;
4721 pr_info("TSC scaling supported\n");
4722 kvm_has_tsc_control = true;
4723 kvm_max_tsc_scaling_ratio = TSC_RATIO_MAX;
4724 kvm_tsc_scaling_ratio_frac_bits = 32;
4728 tsc_aux_uret_slot = kvm_add_user_return_msr(MSR_TSC_AUX);
4730 /* Check for pause filtering support */
4731 if (!boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
4732 pause_filter_count = 0;
4733 pause_filter_thresh = 0;
4734 } else if (!boot_cpu_has(X86_FEATURE_PFTHRESHOLD)) {
4735 pause_filter_thresh = 0;
4739 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
4740 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
4744 * KVM's MMU doesn't support using 2-level paging for itself, and thus
4745 * NPT isn't supported if the host is using 2-level paging since host
4746 * CR4 is unchanged on VMRUN.
4748 if (!IS_ENABLED(CONFIG_X86_64) && !IS_ENABLED(CONFIG_X86_PAE))
4749 npt_enabled = false;
4751 if (!boot_cpu_has(X86_FEATURE_NPT))
4752 npt_enabled = false;
4754 /* Force VM NPT level equal to the host's paging level */
4755 kvm_configure_mmu(npt_enabled, get_npt_level(),
4756 get_npt_level(), PG_LEVEL_1G);
4757 pr_info("kvm: Nested Paging %sabled\n", npt_enabled ? "en" : "dis");
4759 /* Note, SEV setup consumes npt_enabled. */
4760 sev_hardware_setup();
4762 svm_hv_hardware_setup();
4764 svm_adjust_mmio_mask();
4766 for_each_possible_cpu(cpu) {
4767 r = svm_cpu_init(cpu);
4773 if (!boot_cpu_has(X86_FEATURE_NRIPS))
4777 enable_apicv = avic = avic && npt_enabled && boot_cpu_has(X86_FEATURE_AVIC);
4780 pr_info("AVIC enabled\n");
4782 amd_iommu_register_ga_log_notifier(&avic_ga_log_notifier);
4784 svm_x86_ops.vcpu_blocking = NULL;
4785 svm_x86_ops.vcpu_unblocking = NULL;
4790 !boot_cpu_has(X86_FEATURE_V_VMSAVE_VMLOAD) ||
4791 !IS_ENABLED(CONFIG_X86_64)) {
4794 pr_info("Virtual VMLOAD VMSAVE supported\n");
4798 if (boot_cpu_has(X86_FEATURE_SVME_ADDR_CHK))
4799 svm_gp_erratum_intercept = false;
4802 if (!boot_cpu_has(X86_FEATURE_VGIF))
4805 pr_info("Virtual GIF supported\n");
4809 if (!boot_cpu_has(X86_FEATURE_LBRV))
4812 pr_info("LBR virtualization supported\n");
4816 pr_info("PMU virtualization is disabled\n");
4821 * It seems that on AMD processors PTE's accessed bit is
4822 * being set by the CPU hardware before the NPF vmexit.
4823 * This is not expected behaviour and our tests fail because
4825 * A workaround here is to disable support for
4826 * GUEST_MAXPHYADDR < HOST_MAXPHYADDR if NPT is enabled.
4827 * In this case userspace can know if there is support using
4828 * KVM_CAP_SMALLER_MAXPHYADDR extension and decide how to handle
4830 * If future AMD CPU models change the behaviour described above,
4831 * this variable can be changed accordingly
4833 allow_smaller_maxphyaddr = !npt_enabled;
4838 svm_hardware_teardown();
4843 static struct kvm_x86_init_ops svm_init_ops __initdata = {
4844 .cpu_has_kvm_support = has_svm,
4845 .disabled_by_bios = is_disabled,
4846 .hardware_setup = svm_hardware_setup,
4847 .check_processor_compatibility = svm_check_processor_compat,
4849 .runtime_ops = &svm_x86_ops,
4852 static int __init svm_init(void)
4854 __unused_size_checks();
4856 return kvm_init(&svm_init_ops, sizeof(struct vcpu_svm),
4857 __alignof__(struct vcpu_svm), THIS_MODULE);
4860 static void __exit svm_exit(void)
4865 module_init(svm_init)
4866 module_exit(svm_exit)